The darn cat keeps opening doors!

Hello!

So, I have a cat in my game. We’ll call him Garfield.

I want him to wander from room to room in the current region, but I don’t want him to open any doors.

I thought I had this worked out, but he’s still opening doors, which is pretty silly. I mean, I guess some cats can do that…but I really don’t want this one to!

Here’s a simplified version of what I’m attempting:

"Test" by "Frotzing"

Include Basic Screen Effects by Emily Short.
Include Locksmith by Emily Short.
Include Menus by Emily Short.
Include Glulx Text Effects by Emily Short.
Include Variable Time Control by Eric Eve.
Include Basic Help Menu by Emily Short.
Use American Dialect.
Use scoring.
Include Rideable Vehicles by Graham Nelson.
Include Complex Listing by Emily Short.

Blue room is a room.

The plastic door is a door. The plastic door is east of Blue Room and west of Red Room. 

The wooden door is a door. The wooden door is south of Blue Room and north of Green Room. 

The  golden door is a door. The golden door is east of Green Room and west of White Room. 

The black door is a door. The black door is north of White Room and south of Red Room. 

Garfield is a male animal. Garfield is in Red Room.

Colors is a region. Blue Room and Red Room and Green Room and White Room are in Colors.

CatTraveling is a scene. CatTraveling begins when Garfield is visible for the first time.

Every turn during CatTraveling:
	if Garfield is in a room (called the current space) and the player is not carrying Garfield and a random chance of 1 in 3 succeeds:
		let current region be the map region of the current space;
		let next space be a random room which is in the current region;
		let the way be the best route from the current space to the next space, using doors;
		if the next space is not the current space and each door in the way is open:
			try Garfield going the way;

Here’s a Transcript:

Test
An Interactive Fiction by Frotzing
Release 1 / Serial number 200416 / Inform 7 build 6M62 (I6/v6.33 lib 6/12N) SD

Blue room
You can see a wooden door and a plastic door here.

>e
(first opening the plastic door)

Red Room
You can see a black door, a plastic door and Garfield here.

Garfield opens the black door.

Garfield goes south.

>close plastic door
You close the plastic door.

>z
Time passes.

Garfield arrives from the south.

>z
Time passes.

Garfield opens the plastic door.

Garfield goes west.

>z
Time passes.

>

I need Garfield to not attempt to go through closed doors. I thought “and each door in the way is open” would prevent that, but no dice.

I tried nixing the “using doors” part, but that caused the program to say “You’ll have to say which compass direction to go in. I didn’t understand that instruction.” when Garfield tries moving. So that won’t work.

Any help would be appreciated!

2 Likes

Have you tried just teleporting the cat? Like,

“Now Garfield is in the room way of current location” or something, and just manually writing

“Garfield goes [way].”

1 Like

Hmm…I’ll give it a try and get back to you.

Ok, so I replaced the last line of my code with this:

			say "Garfield goes [way].";
			now Garfield is in the room way of current space;

He’s still going through closed doors, although the game isn’t telling us he opened the door.

I’d also have to play around with it so that it isn’t telling the player what Garfield is doing when he shouldn’t be visible.

How about:

Instead of Garfield opening a door:
    if Garfield is visible, say "Garfield paws at [the noun], but [they] [don't] open.".

The standard library will have him try to open closed doors to go through, at which point this rule intervenes with an appropriate message.

3 Likes

Oooh! I like that!

I stepped away from my computer for a bit, but I’m definitely going to try that out when I get back to it.

Thanks!

Could you give rooms an adjective like “cattable” where you want to limit him and instead of picking a random room let him travel to a random cattable room. Then I believe you could eliminate using doors and Inform might not get confused.

1 Like

So I like the idea of Garfield pawing at the door that he can’t open, and I would guess that’s what you want to do with, but a couple of notes:

The issue with the original code is in this block, I think:

		let the way be the best route from the current space to the next space, using doors;
		if the next space is not the current space and each door in the way is open:
			try Garfield going the way;

“best route,” somewhat counterintuitively, is a direction rather than a full-on path. It’s the first step in the best route, really. And “way” is just a temporary variable name. So when you test “each door in the way is open,” that’s checking whether every door that is in the direction north is open–not every door that is north of you, every door that is actually in the “north” object that is part of the compass. Since there are no doors in the north object, this will always come out true.

If you want to check whether there is a door in that direction, you can use the “door… from… of” construction (documented in §6.14 of the documentation, along with “best route” and some other stuff):

Every turn during CatTraveling:
	if Garfield is in a room (called the current space) and the player is not carrying Garfield and a random chance of 1 in 3 succeeds:
		let current region be the map region of the current space;
		let next space be a random room which is in the current region;
		let the way be the best route from the current space to the next space, using doors;
		if the next space is not the current space:
			if the door the way from the current space is a closed door (called the obstacle):
				say "Garfield begins to move to [the way], bumps his head on [the obstacle], then sits down disdainfully as if that was exactly what he had meant to do.";
			otherwise:
				try Garfield going the way.

In addition to Hanon’s suggestion, you can also append “…through cattable rooms” for some suitable definition of “cattable” and then it will only choose routes through rooms that are entirely cattable (including the beginning and end rooms). Unfortunately I don’t think there’s a simple way to choose routes that only go through open doors–the default “using doors” forbids locked doors, and you can say “using even locked doors” to override it, but there’s no straightforward way to do open doors only.

…there’s an awful kludge involving temporarily locking every closed door, doing the route-finding, and then setting the doors back to their original state, I guess:

Seriously, don't do this
A door can be actually locked.

To cache locked doors:
	now every locked door is actually locked;
	now every unlocked door is not actually locked;
	now every closed door is locked;
	now every open door is unlocked.
	
To restore locked doors:
	now every actually locked door is locked;
	now every not actually locked door is unlocked.

To decide which object is best route from (R1 - object) to (R2 - object) using open doors:
	cache locked doors;
	let the way be the best route from R1 to R2, using doors;
	restore locked doors;
	decide on the way.
To decide which number is number of moves from (R1 - object) to (R2 - object) using open doors:
	cache locked doors;
	let N be the number of moves from R1 to R2, using doors;
	restore locked doors;
	decide on N.
To decide which object is best route from (R1 - object) to (R2 - object) through
	(RS - description of objects) using open doors:
	cache locked doors;
	let the way be the best route from R1 to R2 through RS, using doors;
	restore locked doors;
	decide on the way.
To decide which number is number of moves from (R1 - object) to (R2 - object) through
	(RS - description of objects) using open doors:
	cache locked doors;
	let N be the number of moves from R1 to R2 through RS, using doors;
	restore locked doors;
	decide on N.

By the way! The reason that nixing “using doors” can get you “You’ll have to say which compass direction to go in. I didn’t understand that instruction" is that, when the only available path is through a door, “the best route…” is nothing if you don’t choose “using doors.” And you don’t check whether the best route is a direction, so when you try Garfield going the way, the action tried is “Garfield going nothing,” which leads to problems. A way to make sure this doesn’t happen is, when you set “the way” to a best route, always check whether “the way is a direction” before you do anything with it, in case the route was nonexistent.

5 Likes

Thanks!

Unfortunately, when I tried using the cattable adjective and eliminating the “using doors,” I got the same error message mentioned above. That’s not to say I didn’t mess it up somehow.

Here’s my code:

"Test" by "Frotzing"

Include Basic Screen Effects by Emily Short.
Include Locksmith by Emily Short.
Include Menus by Emily Short.
Include Glulx Text Effects by Emily Short.
Include Variable Time Control by Eric Eve.
Include Basic Help Menu by Emily Short.
Use American Dialect.
Use scoring.
Include Rideable Vehicles by Graham Nelson.
Include Complex Listing by Emily Short.

A room can be cattable or uncattable. A room is usually uncattable.

Blue Room is a cattable room. 

Red Room is a cattable room.

Green Room is a cattable room.

White Room is a cattable room.

The plastic door is a door. The plastic door is east of Blue Room and west of Red Room. 

The wooden door is a door. The wooden door is south of Blue Room and north of Green Room. 

The  golden door is a door. The golden door is east of Green Room and west of White Room. 

The black door is a door. The black door is north of White Room and south of Red Room. 

Garfield is a male animal. Garfield is in Red Room.

Colors is a region. Blue Room and Red Room and Green Room and White Room are in Colors.

CatTraveling is a scene. CatTraveling begins when Garfield is visible for the first time.

Every turn during CatTraveling:
	if Garfield is in a room (called the current space) and the player is not carrying Garfield and a random chance of 1 in 3 succeeds:
		let next space be a random cattable room;
		let the way be the best route from the current space to the next space;
		if the next space is not the current space and each door in the way is open:
			try Garfield going the way;

Oh, that makes sense! I couldn’t figure out why “every door is open” was both understood by the program as perfectly valid code, yet still doing nothing. Now I get it.

I don’t think it ever would have occurred to me that Inform was understanding my code as saying a direction literally had a door, rather than the idea that a door existed on a path in a direction from the player (which is what I was trying to say), but it seems obvious in retrospect.

That makes sense. Thank you!

Yeah, early on in this process I very hopefully tried “using open doors,” but obviously that didn’t work.

Ha! Well, I’ll take your word for it that I shouldn’t do that. I do try to avoid the bulldozer approach when I can, but sometimes when I can’t think of anything else, it’s time to fire up the bulldozer and give it a go!

Oh,ok. That makes sense.

Thanks so much for all your help! I’ll be back after I’ve tried all this and let you know what happens.

I don’t know if it was clear, but if you click on “Seriously, don’t do this,” you get the code for doing it. My suggestion not to do this is based largely on the idea that it’s not super safe to mess with the locked/unlocked status of the doors every time you try to find a route.

Also I don’t know how much sense it makes to have the cat sniff out whether a door three rooms away is open and avoid going somewhere based on that! Daniel’s idea of preventing the cat from opening the door still seems like it makes the most sense to me, and is nice and simple.

1 Like

Thanks, yes I saw the code, but I agree that it seems dangerous to mess with all that.

I’m working on my third Inform game for this year’s fall comp and each year (I hope!) I’ve gotten better at not needed to temporarily break something big (like the locked status of every door) in order to get something to work and then put whatever I broke back together again.

But every once in a while I start bulldozing things. Thanks to you all, I don’t think I’ll have to this time!

Worked like a charm! Thanks again!