NPC Movement

My last game Stab the Assassin! has 10 different NPCs wandering around a ship, and all of them have to follow the physical layout of the ship (instead of randomly appearing/disappearing) when moving from one room to another.

The way I did it was to create one file called “router.txt” that lists all possible routes that can be taken from any given room (although the player doesn’t ever see it, all rooms in the game are numbered, for the sake of making the coding easier. But this works equally well if rooms have LOWERCASE names with no spaces, i.e. “dungeon_room”).

Then, a *rand operation determines which room the monster moves to next from the available exits in the current room.

router.txt is split into labels with numbers, such as:

*label 1

*rand diceroll 1 3

*if dieroll = 1
  *set monster_route 13
 
*if dieroll = 2
  *set monster_route 5

*if dieroll = 3
  *set monster_route 11

*return

*label 2

*rand diceroll 1 4

*if diceroll = 1
  *set monster_route 6
 
*if diceroll = 2
  *set monster_route 1

*if diceroll = 3
  *set monster_route 27

*if diceroll = 4
  *set monster_route 5

*return

Each label (such as “1”) in route.txt is the route information for a particular room. Notice how Room #1 has three possible exits while Room #2 has four possible exits.

Then, in your main action scene, you write:

*gosub_scene router {current_room}

Where $current_room is the room the monster is in right now. This subroutine then identifies which room the monster can move to next based on where the monster is right now.

The next line in your action scene is:

*set current_room monster_route

The monster has now moved from its current room to the new room thanks to just two lines of code.

And if you wanted to, you could shorten it to just one line of code by putting the *set current_room monster_route line inside of route.txt.

PS - To see if the monster is in the same room as the player, just add *if player_room = current_room to trigger the text/choices for interacting with the monster.

3 Likes