Self Building Map Example/Framework [Inform 7]

Hi guys,

I’ve been working on a little example/framework for an Inform 7 game that would work in a fashion similar to “Zombies!!!” or “Betrayal At House On The Hill” in which the map is generated by the players moving and “room cards” being drawn, to create a new map with each play. Items tend to be randomly distributed as well (though some rooms could have constant items) if I remember correctly.

I have it at a pretty decent state (at least as the room exploration goes, no combat yet) for a proof of concept or a starting point for anyone looking to make a full version of this type of game. So, I thought I’d share what I have here, in case there’s someone who’s been looking for an example like it:

Source (also attached as a tar.gz):

"The Mansion" by "Phillip J Rhoades"
The story headline is "A new map with every play.".
The story genre is "Example".
The release number is 1.
The story description is "An example of a map that builds as the player moves with some room connections pre-defined and items/treasures that can be randomly distributed on the map.".
The story creation year is 2018.
Release along with the "Quixe" interpreter.

A room has a number called ways out. The ways out are usually 4. [Limit number of random exits a room has]
A room has a number called number of items. The number of items is usually 0. [How many random items a room has]

[These let you restrict the size of the map to less than the total available deck of unvisited rooms, for more variety in maps generated]
Maximum rooms is a number that varies.
Explored rooms is a number that varies. 
Explored rooms is 0. [It is already zero, but I like to be explicit]
Maximum rooms is 4. [Max number of unvisited rooms that can be explored]

[Begin: Anteroom (Where the player starts)]
The Anteroom is a room. The ways out is 1. The description is "[if the anteroom is unvisited]On a dare, you entered the rundown mansion at the end of the street in your neighborhood. It never fit in with the rest of the neighborhood... The mansion is well known for its haphazard layout of rooms. Some people even say the rooms shift around every time someone new steps into the mansion.[paragraph break][end if]You stand in a small anteroom. The way you came in is to the South, though the door slammed shut the second you were inside.". The player is here. 
Instead of going South when the player is in the Anteroom:
	if there are no unvisited rooms:
		say "The door flies open and your tumble out into the yard!";
		continue the action;
		say "You try to leave the creeptastic house, but the door is locked. No number of kicks and punches break it, though your hands and feet now feel a bit more tender.";
		stop the action.
[End: Anteroom (Where the player starts)]

[Begin: Visited Rooms (pre-defined room connections)]
The attic is up from the living room. The attic is visited.
The front yard is south of the Anteroom. The front yard is visited.
The Bathroom is south of the bedroom. "Whatever you do, don't look in the toilet!". The bathroom is visited.
Closet is west of the bathroom. Closet is visited.
The storeroom is a room. The storeroom is visited. [connected to nothing, for random items to be placed]
[End: End Visited Rooms]

[Begin: Unvisited Rooms (the deck of automagically connected rooms based on player movement)]
The bedroom is a room. The ways out are 3. "Peeling paint and dirt covered windows. Quaint."
The kitchen is a room. The ways out are 3. The number of items is 1. "You're pretty sure this is the kitchen... Though there's no stove, refrigerator, or anything else."
The living room is a room. The ways out are 4. The number of items is 4. The description is "It was once an elegant room full of cushy furnishings. Now, everything is dusty, a bit moldy, and not at all cozy."
The library is a room. The ways out are 4. The description is "Books line the shelves, but look worn, water damaged, and as if they would crumble from the lightest touch."
[End: Unvisited Rooms]

[Begin unplaced items]
A fish is thing in storeroom. The description is "A fish...".
A toothpick is a thing in storeroom. The description is "A little piece of wood to wedge between your teeth.".
A baseball bat is a thing in storeroom. The description is "A weapon or a toy... who knows?".
[End unplaced items]

[Begin: Build the map based on player moves and randomly chosen unvisited rooms]
Before going a direction (called way):
	[Begin: Allow all directions for pre-defined room connections]
	if the room the way from the location is not nothing:
		continue the action;
	[End: Allow all directions for pre-defined room connections]
	if there are unvisited rooms and the ways out of the location are not 0 and maximum rooms >= explored rooms:
		let reverse be the opposite of the way;
		let the next location be a random unvisited room;
		[Begin: Detect existing room connections]
		if the room reverse from the next location is not nothing:
			if the number of unvisited rooms is greater than 1:
				try going way;
				stop the action;
				say "You can't go that way.";
				stop the action;
		[End: detect existing room connections]
		if way is not north and way is not south and way is not east and way is not west:
		[Only allow NSEW directions for random connections, for simplicity]
			say "Only go North, South, East, or West...";
			continue the action;
		let further place be the room the way from the location;
		change the reverse exit of the next location to the location;
		change the way exit of the location to the next location;
		decrease the ways out of the location by 1;
		decrease the ways out of the next location by 1;
		increase explored rooms by 1;
		repeat with counter running from 1 to the number of items of the next location:
			let the treasure be a random thing in storeroom;
			if the treasure is not nothing:
				Move the treasure to the next location;
		continue the action.
[End: Build the map based on player moves and randomly chosen unvisited rooms]

I’m sure there’s some tightening up that can happen, etc. However, it seems to work pretty well.
RandomRooms.tar.gz (33.4 KB)

Example Output:

Cool stuff!

One thing about things like this is that when they get big they can lead to weird geometries–either the map has a tree structure or, if loops are allowed, you can get things where you go east three times and wind up back where you started. I wrote something that tried to take care of that by putting all the rooms on a grid.

Loops shouldn’t happen in this one, though layouts won’t (obviously) make as much sense as completely planned floor plans. The games I was thinking of don’t always have bedrooms and libraries where they should be.

The ability to have some rooms with pre-connected other rooms helps with that. The bathroom is always north of the bedroom. The closet is always west of the bathroom. That keeps some sanity in room layouts while allowing enough randomization to enjoy exploring the areas. Of course, some rooms could have non-random objects permanently placed as well.

Yeah, the question is partly whether loops are desirable. Should it be possible to go from the hall to the living room to the dining room to the kitchen and back to the hall?

You might be able to do that with some pre-defined connections.