I’ve got an NPC I’d like to always try and stay in a room adjacent to the player.
There aren’t any closed/locked doors for the NPC to trip up on. There’s a room I’d like them to stay out of, but I’m just trying to get the basic code working before I worry about that. The “if” statement works, but I feel like it’s getting hung up on how the adjacent room is getting selected. I dunno. Any suggestions?
An NPC movement rule when the player is in NPCArea:
if the location of NPC is not adjacent to the location of the player:
let origin be the location of the player;
let nextdoor be a random room which is adjacent to origin;
let the heading be the best route from origin to nextdoor;
if the heading is a direction:
try NPC going the heading;
While there are no closed/locked doors to trip up the NPC, are there still any doors connecting the rooms the NPC would have to traverse? If so, then there is a suffix for best routes that allows open or openable and unlocked doors to be considered on the best route: using doors.
You may want to try:
let the heading be the best route from origin to nextdoor, using doors;
I think “random adjacent room” is the wrong call here—they might choose a room on the opposite side of the player, and run through the player’s room to reach it!
Instead, how about something like…
An NPC movement rule:
let the source be the location of the NPC;
let the destination be the location;
let the distance be the number of moves from the source to the destination;
if the distance is 1:
say "The NPC is comfortable where they are.";
otherwise if the distance is 2:
let the way be the best route from the source to the destination;
try the NPC going the way;
otherwise:
say "Hm. This is not ideal."
I understand the caution you’re trying to express here, but if a room on the opposite side of the player is selected and the NPC has to go through the room the player is in, then wouldn’t that mean that the NPC is already adjacent to the player?
Aside from that, I would agree that the logic of the alternative you’ve written out makes it easier to follow and seems like it would avoid the edge cases you’re cautioning about.
Not necessarily—imagine a situation with four rooms in a row, call them A, B, C, and D. The NPC is in A, the player is in B. The player moves to C; now the NPC chooses a random adjacent room to move to, and picks D. Now they’re going to try to go through the player’s room to get there.
In this scenario, wouldn’t the step be the NPC moves to B and be a legal move? The best route leads through the player, yes, but the move the NPC makes is presumably only into one room over from where the NPC is. (Edit: Remember, the concluding action in the OP is the NPC tries to go in a direction) Or is the assumption that the NPC would just move all the way through the player and into the room on the opposite side in one move?
I think the point you’re making is valid though. You would need to take care to institute that rule (or other intervening logic) to avoid the NPC moving through the player.
Fair point. If they only move once per turn, that wouldn’t be a problem.
In that case, imagine a two-by-three arrangement of rooms.
A--B
| |
C D
| |
E--F
The player is in C, with the NPC in E. The player moves to A. The NPC decides its target is B, and thus moves to F instead of to C. (When two routes are equally good, which one Inform chooses is not guaranteed.)
I appreciate the discussion! You’ve highlighted a lot of the struggle I was having, let alone figuring out the code itself. For further context: no doors in the level at all (I try to avoid doors if I can. lol.) The level is a mix of a 3x3 open grid, a circular hallway that loops similarly to how Draconis illustrated, and a long dead-end hallway with a few side rooms. Lots of opportunities for the NPC to get mixed up so I was struggling.
now I guess all that now remains for solving Austin’s problem is figuring how to write a rule for choosing between two equally good routes… dunno if is easy or complex or even require winning the next bubbling beaker award
It was stalling out when the player moved into the same room as the NPC so now that I’ve got the structure provided by Draconis, I tweaked it:
if the location of NPC is not adjacent to the location of the player and the location of NPC is not the location of the player:
let the source be the location of the NPC;
let the destination be the location;
let the distance be the number of moves from the source to the destination;
if the distance is 1:
say "The NPC is comfortable where they are.";
otherwise if the distance is 2:
let the way be the best route from the source to the destination;
try the NPC going the way;
otherwise if the location of NPC is the location of the player:
let the source be the location of the NPC;
let the destination be a random room adjacent to the location;
let the way be the best route from the source to the destination;
try the NPC going the way;
Next will be to keep the NPC from going to restricted rooms when they make that random adjacent choice. (Or eliminate the randomness of that decision.)
Doable, but it requires hacking out the routefinder and replacing it with a new one. My Dijkstra Pathing extension does that, which means you can make NPCs prefer a “straight” path (NORTH, NORTH, NORTH, NORTH) over a “winding” one (NORTHEAST, NORTHWEST, NORTHEAST, NORTHWEST).