Inform 7 Gems – Pathfinding – 1.1

My last post on pathfinding in Inform 7, showed a simple beginning to the power of pathfinding in Inform 7.   I thought I’d expand on this slightly to show a little NPC movement that is a bit smarter than simply following the player around, as I was playing with the code, I found a bug in the code, that I thought I’d like to share the fix first before moving into new territory.

Bug fix from last post

When I fixed the code to be able to move through only open doors, I actually broke it so that if there was not a door between rooms, that the NPC would fail to follow.  So building upon the code from the previous post, we change our every turn rule to be:

Every turn:
	let course be the best route from location of the kitten to the location of the player through kitten-friendly rooms, using doors;
	if course is a direction:
		let the target be the room-or-door course from the location of the kitten;
		if target is not a door:
			try kitten going course;
		otherwise:
			 repeat with doorway running through visible doors:
				 if the other side of doorway from the location of the kitten is room course from the location of the kitten:
					if doorway is open:
						try kitten going course;
					otherwise:
						say "You hear a scratching from the other side of the door.";
	otherwise:
		if location of kitten is the location of the player:
			say "The kitten sits and looks up at you expectantly";
		else:
			say "The kitten tried to enter [the location], but thought better of it."

Here we simply add a check to see if the target of the direction the kitten is attempting to move, is a door or room.  If it’s not a door, then the kitten can simple head in that direction.  If the target is a door, then we drop into our normal code from last time where we check for the open / closed door.  The important section of code above is this:

		let the target be the room-or-door course from the location of the kitten;
		if target is not a door:
			try kitten going course;
		otherwise:

 

With that fixed, I do believe the code should work as described from last time.   Now I’ll move on to exploring pathfinding to try and make this all a bit smarter.

Extensions

I should also note that there are some extensions out there that will wrap a lot of this code up for you.  A few that I’ve looked at before include Patrollers and Planner.  And there is a newer one Problem-Solving Characters, that I want to look at a bit deeper.

So other than perhaps some simple situations we can use those instead.  I like to dig deeper into the code and know how things work, even if I use an extension….so I’ll continue to these subjects…well….because I think it’s fun!

Advertisements

One thought on “Inform 7 Gems – Pathfinding – 1.1

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s