I'm currently working on Hunt The Wumpus in Twine as an experiment (sugarcube 2). Eventually I plan to make a succinct tutorial that touches on a wide range of Twine features, while building a functional and polished game. The point is to focus on using Twine as naturally as possible, and not get bogged down too much with custom code.
Right now I've run into a snag that I think demands the use of some custom code, and I'm trying to figure out how to do it elegantly.
Each room is its own passage, and the Wumpus is assigned a room randomly in the Storyinit passage.
<<set $wumpus =
"room2" : either
"Boiler Room", "Heat Exchanger Room", "Water Reclaimation",
"Dynamo Room", "Noisy Room", "Pipe Nightmare",
"Breaker Room", "Hotel Lobby"
I have a generic "Room" passage that checks for the presence of a Wumpus. This cuts down on the repeated code.
<<print passage()>><<set $player.room2 = passage()>>
<<if ($player.room2 == $wumpus.room2)>>Wumpus is here!<</if>>
Each room consists of little more than an include for this generic passage, some description text, and a set of links to connecting rooms.
Steam billows forth from a tangle of pipes wrapped like vines around a massive round boiler.
Nothing<-|->[[Heat Exchanger Room]]
At this point in the game's build, I need to allow each room (the generic room code) to detect whether there is a Wumpus in the adjascent rooms. In order to do that, I need to detect which passages the current passage links to. I can check that information against the Wumpus's assigned room, and if it's present I can add the "I smell a Wumpus" text to the current room.
- Break the room text apart, searching for all links and their passage text.
- Add tags to each room, defining which rooms it links to. Use Story.lookup() to list all rooms with the current room tagged.
Option 2 requires more work than I think is necessary. Every time a room is added, I'd not only need to repeat each link in the tags for that room. I'd also need to go to each connected room and add the new room there.
Option 1 seems clumsy and not very robust. The code would get very complicated if I wanted to check for different link types, or account for conditionals or <<include>> passages.