# Navigation and map (/nqtr/navigation)



<CalloutContainer type="info">
  <CalloutTitle>
    Nomenclature
  </CalloutTitle>

  <CalloutDescription>
    Usually in video games, navigation elements used to identify the position of characters are called room, location (e.g. restaurant, house, hospital, etc.), and map. For this reason, names that follow this logic are used.

    This does not prevent you from using these elements to manage navigation with the same logic but with different terms. For example, in a game set in space where the player moves between planets, rooms can be the planets, locations can be the solar systems, and maps can be the galaxy.
  </CalloutDescription>
</CalloutContainer>

The navigation system is composed of the following elements:

| Element     | Description                                                                                |
| ----------- | ------------------------------------------------------------------------------------------ |
| `rooms`     | The core elements of navigation, from which the position of the `mc` and `npc` is deduced. |
| `locations` | A container of `rooms`.                                                                    |
| `maps`      | A container of `locations`.                                                                |

The player can move between `rooms`. The `location` and the `map` are also determined based on the `room` in which the player is located.

## Initialize [#initialize]

To initialize a `room`/`location`/`map`, create a new instance of the [`RoomBaseModel`](/jsdoc/nqtr/index/classes/RoomBaseModel) / [`LocationBaseModel`](/jsdoc/nqtr/index/classes/LocationBaseModel) / [`MapBaseModel`](/jsdoc/nqtr/index/classes/MapBaseModel) class (or your [custom class](#custom-class)) and add it to the game room/location/map dictionary when the game is initialized.

<CalloutContainer type="info">
  <CalloutTitle>
    Custom classes
  </CalloutTitle>

  <CalloutDescription>
    It is recommended to use [custom classes](#custom-class) to adapt `rooms`/`locations`/`maps` to your game as best as possible. The default templates already use custom classes.
  </CalloutDescription>
</CalloutContainer>

<CalloutContainer type="warning">
  <CalloutDescription>
    [`RegisteredRooms.add`](/jsdoc/nqtr/index/namespaces/RegisteredRooms/functions/add) / [`RegisteredLocations.add`](/jsdoc/nqtr/index/namespaces/RegisteredLocations/functions/add) / [`RegisteredMaps.add`](/jsdoc/nqtr/index/namespaces/RegisteredMaps/functions/add) is **required** to save the `rooms`/`locations`/`maps` in the game.
  </CalloutDescription>
</CalloutContainer>

<CodeBlockTabs defaultValue="content/rooms.ts">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="content/rooms.ts">
      content/rooms.ts
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="content/locations.ts">
      content/locations.ts
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="content/maps.ts">
      content/maps.ts
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="content/rooms.ts">
    ```ts
    import { RegisteredRooms, RoomBaseModel } from "@drincs/nqtr";
    import { mcHome } from "@/content/locations";

    export const mcRoom = new RoomBaseModel("mc_room", mcHome, {
        name: "MC room",
        image: "location_myroom",
        activities: ["bed"],
    });

    export const aliceRoom = new RoomBaseModel("alice_room", mcHome, {
        name: "Alice room",
        image: "location_aliceroom",
        routine: ["alice_sleep"],
    });

    export const bathroom = new RoomBaseModel("bathroom", mcHome, {
        name: "Bathroom",
        image: "location_bathroom",
    });

    export const terrace = new RoomBaseModel("terrace", mcHome, {
        name: "Terrace",
        image: "location_terrace",
        routine: ["alice_smokes"],
    });

    RegisteredRooms.add(mcRoom, aliceRoom, bathroom, terrace);
    ```
  </CodeBlockTab>

  <CodeBlockTab value="content/locations.ts">
    ```ts
    import {
        LocationBaseModel,
        navigator,
        RegisteredLocations,
    } from "@drincs/nqtr";
    import { mainMap } from "@/content/maps";

    export const mcHome = new LocationBaseModel("mc_home", mainMap, {
        name: "MC Home",
    });

    export const gym = new LocationBaseModel("gym", mainMap, {
        name: "Gym",
    });

    export const school = new LocationBaseModel("school", mainMap, {
        name: "School",
    });

    RegisteredLocations.add(mcHome, gym, school);
    ```
  </CodeBlockTab>

  <CodeBlockTab value="content/maps.ts">
    ```ts
    import { MapBaseModel, RegisteredMaps } from "@drincs/nqtr";

    export const mainMap = new MapBaseModel("main_map", {
        name: "Main Map",
        image: "map",
    });

    export const nightcityMap = new MapBaseModel("nightcity_map", {
        name: "Nightcity",
        image: "nightcity_map",
    });

    RegisteredMaps.add(mainMap, nightcityMap);
    ```
  </CodeBlockTab>
</CodeBlockTabs>

It is also recommended to [set the current room](#navigate) during the start of the game.

<CodeBlockTabs defaultValue="Typescript">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Typescript">
      Typescript
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="ink">
      ink
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Typescript">
    ```ts  title="content/labels/start.label.ts" groupId="narrative_language"
    import { navigator } from "@drincs/nqtr";
    import { newLabel } from "@drincs/pixi-vn";
    import { mcRoom } from "@/content/rooms";

    export const startLabel = newLabel("start", [
        async () => {
            navigator.currentRoom = mcRoom;
            // ... other initialization logic
        },
    ]);
    ```
  </CodeBlockTab>

  <CodeBlockTab value="ink">
    ```ink  title="ink/start.ink" groupId="narrative_language"
    === start ===
    // Set the current room to mcRoom at the start of the game
    # set current room mc_room
    // ... other initialization logic
    -> DONE
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Get [#get]

To get a `room`/`location`/`map` by its `id`, use the [`RegisteredRooms.get`](/jsdoc/nqtr/index/namespaces/RegisteredRooms/functions/get) / [`RegisteredLocations.get`](/jsdoc/nqtr/index/namespaces/RegisteredLocations/functions/get) / [`RegisteredMaps.get`](/jsdoc/nqtr/index/namespaces/RegisteredMaps/functions/get) function.

<CodeBlockTabs defaultValue="Rooms">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Rooms">
      Rooms
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Locations">
      Locations
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Maps">
      Maps
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Rooms">
    ```ts
    import { RegisteredRooms } from "@drincs/nqtr";

    const mcRoom = RegisteredRooms.get("mc_room");
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Locations">
    ```ts
    import { RegisteredLocations } from "@drincs/nqtr";

    const mcHome = RegisteredLocations.get("mc_home");
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Maps">
    ```ts
    import { RegisteredMaps } from "@drincs/nqtr";

    const mainMap = RegisteredMaps.get("main_map");
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Get all [#get-all]

To get all `rooms`/`locations`/`maps`, use the [`RegisteredRooms.values`](/jsdoc/nqtr/index/namespaces/RegisteredRooms/functions/values) / [`RegisteredLocations.values`](/jsdoc/nqtr/index/namespaces/RegisteredLocations/functions/values) / [`RegisteredMaps.values`](/jsdoc/nqtr/index/namespaces/RegisteredMaps/functions/values) function.

<CodeBlockTabs defaultValue="Rooms">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Rooms">
      Rooms
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Locations">
      Locations
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="Maps">
      Maps
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Rooms">
    ```ts
    import { RegisteredRooms } from "@drincs/nqtr";

    const rooms = RegisteredRooms.values();
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Locations">
    ```ts
    import { RegisteredLocations } from "@drincs/nqtr";

    const locations = RegisteredLocations.values();
    ```
  </CodeBlockTab>

  <CodeBlockTab value="Maps">
    ```ts
    import { RegisteredMaps } from "@drincs/nqtr";

    const maps = RegisteredMaps.values();
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Navigate [#navigate]

As explained above, the player can navigate between `rooms`, and the current `room` determines the current `location` and `map`.

To navigate to a `room`, set the [`navigator.currentRoom`](/jsdoc/nqtr/index/interfaces/NavigatorHandlerInterface#currentroom) property to the desired room instance. This will automatically update the current `location` and `map` based on the room's location and map.

<CodeBlockTabs defaultValue="Typescript">
  <CodeBlockTabsList>
    <CodeBlockTabsTrigger value="Typescript">
      Typescript
    </CodeBlockTabsTrigger>

    <CodeBlockTabsTrigger value="ink">
      ink
    </CodeBlockTabsTrigger>
  </CodeBlockTabsList>

  <CodeBlockTab value="Typescript">
    ```ts  title="index.ts" groupId="narrative_language"
    import { navigator } from "@drincs/nqtr";
    import { mcRoom } from "@/content/rooms";

    navigator.currentRoom = mcRoom;
    ```
  </CodeBlockTab>

  <CodeBlockTab value="ink">
    ```ink  title="ink/start.ink" groupId="narrative_language"
    // {roomId} is the id of the room
    # enter room {roomId}
    ```
  </CodeBlockTab>
</CodeBlockTabs>

## Other features [#other-features]

<Accordions>
  <Accordion title="Custom class" id="custom-class">
    <CalloutContainer type="info">
      <CalloutTitle>
        Templates
      </CalloutTitle>

      <CalloutDescription>
        In all templates, the `Room` / `Location` / `Map` class is already defined in the file `models/Room.ts` / `models/Location.ts` / `models/Map.ts`. You can use it directly or modify it to suit your needs.
      </CalloutDescription>
    </CalloutContainer>

    It is recommended to create your own class `Room` / `Location` / `Map` that extends [`RoomStoredClass`](/jsdoc/nqtr/index/classes/RoomStoredClass) / [`LocationStoredClass`](/jsdoc/nqtr/index/classes/LocationStoredClass) / [`MapStoredClass`](/jsdoc/nqtr/index/classes/MapStoredClass) and "override" the interface [`RoomInterface`](/jsdoc/nqtr/index/interfaces/RoomInterface) / [`LocationInterface`](/jsdoc/nqtr/index/interfaces/LocationInterface) / [`MapInterface`](/jsdoc/nqtr/index/interfaces/MapInterface) to add, edit, or remove properties or methods.

    For example, if you want to create a class `Room` / `Location` / `Map`, you must "override" the interface [`RoomInterface`](/jsdoc/nqtr/index/interfaces/RoomInterface) / [`LocationInterface`](/jsdoc/nqtr/index/interfaces/LocationInterface) / [`MapInterface`](/jsdoc/nqtr/index/interfaces/MapInterface) to use your properties or methods. (See the file `nqtr.d.ts`)

    Now you can create a class `Room` / `Location` / `Map` that extends [`RoomStoredClass`](/jsdoc/nqtr/index/classes/RoomStoredClass) / [`LocationStoredClass`](/jsdoc/nqtr/index/classes/LocationStoredClass) / [`MapStoredClass`](/jsdoc/nqtr/index/classes/MapStoredClass) and implements the [`RoomInterface`](/jsdoc/nqtr/index/interfaces/RoomInterface) / [`LocationInterface`](/jsdoc/nqtr/index/interfaces/LocationInterface) / [`MapInterface`](/jsdoc/nqtr/index/interfaces/MapInterface). (For more information on how to create a class in TypeScript, read [the official documentation](https://www.typescriptlang.org/docs/handbook/2/classes.html))

    To create a property that stores its value in the game storage, you can create [Getters/Setters](https://www.typescriptlang.org/docs/handbook/2/classes.html#getters--setters) and use the [`this.getStorageProperty()`](/jsdoc/nqtr/index/classes/RoomStoredClass#getstorageproperty) / [`this.setStorageProperty()`](/jsdoc/nqtr/index/classes/RoomStoredClass#setstorageproperty) methods. (See the file `Room.ts` / `Location.ts` / `Map.ts`)

    <CodeBlockTabs defaultValue="models/Room.ts">
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="models/Room.ts">
          models/Room.ts
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="models/Location.ts">
          models/Location.ts
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="models/Map.ts">
          models/Map.ts
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="nqtr.d.ts">
          nqtr.d.ts
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="models/Room.ts">
        ```ts
        import {
            type ActivityIdType,
            type ActivityInterface,
            type CommitmentIdType,
            type CommitmentInterface,
            type LocationInterface,
            type OnRunProps,
            type RoomInterface,
            RoomStoredClass,
        } from "@drincs/nqtr";
        import type { PixiUIParam, PixiUIProp } from "@/models/ui-elements";

        export default class Room extends RoomStoredClass implements RoomInterface {
            constructor(
                id: string,
                location: LocationInterface,
                props: {
                    name: string;
                    disabled?: boolean | (() => boolean);
                    hidden?: boolean | (() => boolean);
                    background: PixiUIParam<Room>;
                    activities?: (ActivityInterface | ActivityIdType)[];
                    routine?: (CommitmentInterface | CommitmentIdType)[];
                    isEntrance?: boolean;
                },
            ) {
                super(id, location, {
                    activities: props.activities || [],
                    routine: props.routine || [],
                });
                this.name = props.name;
                this._defaultDisabled = props.disabled || false;
                this._defaultHidden = props.hidden || false;
                this._background = props.background;
                this.isEntrance = props.isEntrance || false;
            }
            readonly name: string;
            private readonly _background: PixiUIParam<Room>;
            get background(): PixiUIProp {
                const background = this._background;
                if (typeof background === "function") {
                    return (runProps: OnRunProps) => background(this, runProps);
                }
                return background;
            }
            readonly isEntrance: boolean;
            private _defaultDisabled: boolean | (() => boolean) = false;
            get disabled(): boolean {
                const value = this.getStorageProperty<boolean>("disabled") || this._defaultDisabled;
                if (typeof value === "function") {
                    return value();
                }
                return value;
            }
            set disabled(value: boolean) {
                this.setStorageProperty("disabled", value);
            }
            private _defaultHidden: boolean | (() => boolean) = false;
            get hidden(): boolean {
                const value = this.getStorageProperty<boolean>("hidden") || this._defaultHidden;
                if (typeof value === "function") {
                    return value();
                }
                return value;
            }
            set hidden(value: boolean) {
                this.setStorageProperty("hidden", value);
            }
        }
        ```
      </CodeBlockTab>

      <CodeBlockTab value="models/Location.ts">
        ```ts
        import {
            type ActivityInterface,
            type LocationInterface,
            LocationStoredClass,
            type MapInterface,
            type OnRunProps,
            type RoomInterface,
        } from "@drincs/nqtr";
        import type { PixiUIParam, PixiUIProp } from "@/models/ui-elements";

        export default class Location extends LocationStoredClass implements LocationInterface {
            constructor(
                id: string,
                map: MapInterface,
                props: {
                    activities?: ActivityInterface[];
                    name: string;
                    disabled?: boolean | (() => boolean);
                    hidden?: boolean | (() => boolean);
                    sprite: PixiUIParam<Location>;
                },
            ) {
                super(id, map, props.activities);
                this.name = props.name;
                this._defaultDisabled = props.disabled || false;
                this._defaultHidden = props.hidden || false;
                this._sprite = props.sprite;
            }
            readonly name: string;
            private readonly _sprite: PixiUIParam<Location>;
            get sprite(): PixiUIProp {
                const sprite = this._sprite;
                if (typeof sprite === "function") {
                    return (runProps: OnRunProps) => sprite(this, runProps);
                }
                return sprite;
            }
            private _defaultDisabled: boolean | (() => boolean) = false;
            get disabled(): boolean {
                const value = this.getStorageProperty<boolean>("disabled") || this._defaultDisabled;
                if (typeof value === "function") {
                    return value();
                }
                return value;
            }
            set disabled(value: boolean) {
                this.setStorageProperty("disabled", value);
            }
            private _defaultHidden: boolean | (() => boolean) = false;
            get hidden(): boolean {
                const value = this.getStorageProperty<boolean>("hidden") || this._defaultHidden;
                if (typeof value === "function") {
                    return value();
                }
                return value;
            }
            set hidden(value: boolean) {
                this.setStorageProperty("hidden", value);
            }
            override get rooms(): RoomInterface[] {
                return super.rooms.filter((room) => !room.hidden);
            }
            get entrance(): RoomInterface | undefined {
                if (super.rooms.length === 0) {
                    return undefined;
                }
                return super.rooms.find((room) => room.isEntrance) || super.rooms[0];
            }
        }
        ```
      </CodeBlockTab>

      <CodeBlockTab value="models/Map.ts">
        ```ts
        import type { PixiUIParam, PixiUIProp } from "@/models/ui-elements";
        import {
            type ActivityInterface,
            type LocationInterface,
            type MapInterface,
            MapStoredClass,
            type OnRunProps,
        } from "@drincs/nqtr";

        export default class MapClass extends MapStoredClass implements MapInterface {
            constructor(
                id: string,
                props: {
                    activities?: ActivityInterface[];
                    name: string;
                    background: PixiUIParam<MapClass>;
                },
            ) {
                super(id, props.activities);
                this.name = props.name;
                this._background = props.background;
            }
            readonly name: string;
            private readonly _background: PixiUIParam<MapClass>;
            get background(): PixiUIProp {
                const background = this._background;
                if (typeof background === "function") {
                    return (runProps: OnRunProps) => background(this, runProps);
                }
                return background;
            }
            override get locations(): LocationInterface[] {
                return super.locations.filter((location) => !location.hidden);
            }
        }
        ```
      </CodeBlockTab>

      <CodeBlockTab value="nqtr.d.ts">
        ```ts
        import type { StepLabelProps } from "@drincs/pixi-vn";
        import type { PixiUIProp, ReactUIProp } from "@/models/ui-elements";

        declare module "@drincs/nqtr" {
            interface OnRunProps extends StepLabelProps {}
            interface ActivityInterface {
                /**
                 * The name of the activity.
                 */
                readonly name: string;
                /**
                 * Whether is disabled.
                 */
                disabled: boolean;
                /**
                 * Whether is hidden.
                 */
                hidden: boolean;
                /**
                 * The sprite of the activity.
                 */
                readonly sprite?: PixiUIProp;
                /**
                 * The React icon of the activity.
                 */
                readonly icon?: ReactUIProp;
            }
            interface CommitmentInterface {
                /**
                 * The name of the commitment.
                 */
                readonly name: string;
                /**
                 * The sprite of the commitment.
                 */
                readonly sprite?: PixiUIProp;
                /**
                 * The background of the commitment.
                 */
                readonly background?: PixiUIProp;
                /**
                 * The React icon of the commitment.
                 */
                readonly icon?: ReactUIProp;
                /**
                 * Whether is disabled.
                 */
                disabled: boolean;
                /**
                 * Whether is hidden.
                 */
                hidden: boolean;
            }
            interface LocationInterface {
                /**
                 * The name of the location.
                 * If you set undefined, it will return the initial value of name.
                 */
                readonly name: string;
                /**
                 * Whether is disabled.
                 */
                disabled: boolean;
                /**
                 * Whether is hidden.
                 */
                hidden: boolean;
                /**
                 * The sprite of the location.
                 */
                readonly sprite: PixiUIProp;
                /**
                 * The entrance room of the location.
                 */
                readonly entrance: RoomInterface | undefined;
            }
            interface MapInterface {
                /**
                 * The name of the map.
                 */
                readonly name: string;
                /**
                 * The background of the map.
                 */
                readonly background: PixiUIProp;
            }
        }
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </Accordion>

  <Accordion title="How do I set a room for a location that will open by default?" id="how-set-default-room-for-location">
    The developer can designate an "index" `room` for locations.

    To set a default room for a location, you can add in your [custom class](#custom-class) a property `isEntrance` to the `Room` class, which will be set to `true` for the default room of the location, and then use the `entrance` property of the `Location` class to get the default room.

    For example:

    <CodeBlockTabs defaultValue="content/mcRoom.ts">
      <CodeBlockTabsList>
        <CodeBlockTabsTrigger value="content/mcRoom.ts">
          content/mcRoom.ts
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="models/Room.ts">
          models/Room.ts
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="models/Location.ts">
          models/Location.ts
        </CodeBlockTabsTrigger>

        <CodeBlockTabsTrigger value="nqtr.d.ts">
          nqtr.d.ts
        </CodeBlockTabsTrigger>
      </CodeBlockTabsList>

      <CodeBlockTab value="content/mcRoom.ts">
        ```ts
        import { navigator, RegisteredRooms } from "@drincs/nqtr";
        import { ImageSprite } from "@drincs/pixi-vn";
        import { NAVIGATION_ROUTE } from "@/constans";
        import Location from "@/models/Location";
        import Room from "@/models/Room";
        import { mainMap } from "@/content/maps";

        export const mcHome = new Location("mc_home", mainMap, {
            name: "MC Home",
            icon: (location, { navigate }) => {
                const icon = new ImageSprite(
                    {
                        xAlign: 0.3,
                        yAlign: 0.2,
                        height: 120,
                        width: 120,
                        eventMode: "static",
                        cursor: "pointer",
                    },
                    "icon_location_home",
                );
                icon.on("pointerdown", () => {
                    const entrance = location.entrance;
                    if (entrance) {
                        navigator.currentRoom = entrance;
                        navigate(NAVIGATION_ROUTE);
                    }
                });
                icon.load();
                return icon;
            },
        });

        export const mcRoom = new Room("mc_room", mcHome, {
            name: "MC room",
            image: "location_myroom",
            isEntrance: true, // This room is the entrance of the location
        });

        RegisteredRooms.add(mcRoom);
        ```
      </CodeBlockTab>

      <CodeBlockTab value="models/Room.ts">
        ```ts
        import { RoomInterface, RoomStoredClass } from "@drincs/nqtr";

        export default class Room extends RoomStoredClass implements RoomInterface {
            constructor() {
                // ... other initializations
                this.isEntrance = props.isEntrance || false;
            }
            // ... other methods and properties
            readonly isEntrance: boolean;
        }
        ```
      </CodeBlockTab>

      <CodeBlockTab value="models/Location.ts">
        ```ts
        import {
            LocationInterface,
            LocationStoredClass,
            RoomInterface,
        } from "@drincs/nqtr";

        export default class Location
            extends LocationStoredClass
            implements LocationInterface
        {
            // ... other methods and properties
            get entrance(): RoomInterface | undefined {
                if (super.rooms.length === 0) {
                    return undefined;
                }
                return super.rooms.find((room) => room.isEntrance) || super.rooms[0];
            }
        }
        ```
      </CodeBlockTab>

      <CodeBlockTab value="nqtr.d.ts">
        ```ts
        declare module "@drincs/nqtr" {
            interface LocationInterface {
                /**
                 * The entrance room of the location.
                 */
                readonly entrance: RoomInterface | undefined;
            }
            interface RoomInterface {
                /**
                 * If is the entrance of the location. (the first room)
                 */
                readonly isEntrance: boolean;
            }
        }
        ```
      </CodeBlockTab>
    </CodeBlockTabs>
  </Accordion>
</Accordions>
