Allow objects to be toggled, cleanly.
skx opened this issue · comments
For the purpose of this bug I'm considering only the
TRAPDOOR
, however a similar process applies to theTORCH
- we pretend we have state "TORCH ON" vs. "TORCH OFF", but instead have two objects "TORCH" which is unlit, and "TORCH-LIT" which has text saying "We're on".
At the moment we have two items in the game:
- TRAPDOOR
- TRAPDOOR-OPEN
When the appropriate action is carried out we remove TRAPDOOR from the world, and replace it with TRAPDOOR-OPEN. Doing the switch in reverse is difficult because if we tried to handle:
>CLOSE TRAPDOOR
We'd then use our find-object-by-name
code and find TRAPDOOR
, and never find TRAPDOOR-OPEN
.
So we either need to rename the items, dynamically, allow aliases, or handle things in a different way. What we could do is remove TRAPDOOR-OPEN
and instead use a state-flag to determine the state. This would be clean and easy:
- Each item does have a "STATE" flag.
- We can decide "STATE: 0 == closed".
- We can decide "STATE: 1 == opened".
The only problem with this approach is the description of the item. We do have an examine function
pointer so we can use a dedicated "examine_trapdoor" function:
- if state == 0 : show "this is a trapdoor .."
- if state == 1 : show "this is an open trapdoor .."
The place where this falls down is the "LOOK" handler, which will not call the examine handler, so it will always show " the contents of the items description attribute.
Either we add a function "LOOK_fn" to call, or we have to manually update the pointer to make the appearance dynamic. I think the latter is possible, but it might be a pain to code.
Updating the pointers will add extra logic to the "use_xxx_fn" but in some ways that has to be easier than swapping locations and duplicating objects?
Removing the extra items (TRAPDOOR-OPEN
and TORCH-LIT
) should remove code too. So for academic interest here's the size of our binaries before making any changes:
$ ls -l *.tap *.com lighthouse
-rwxr-xr-x 1 skx skx 37872 Apr 30 11:42 lighthouse
-rw-r--r-- 1 skx skx 12010 Apr 30 11:42 lihouse.com
-rw-r--r-- 1 skx skx 11877 Apr 30 11:42 lihouse.tap
Adding the code for allowing the torch to be on or off actually increased the size of the game.
Not hugely, but still. A surprise:
$ ls -l *.tap *.com lighthouse
-rwxr-xr-x 1 skx skx 37872 Apr 30 11:42 lighthouse
-rw-r--r-- 1 skx skx 12081 Apr 30 16:19 lihouse.com
-rw-r--r-- 1 skx skx 11948 Apr 30 16:19 lihouse.tap
We'll see what happens when we update the trapdoor - which I realize now needs three states:
- Hidden.
- Opened.
- Closed.
Now the trapdoor has state, and a single object and we got bigger still, a shame:
$ ls -l *.tap *.com lighthouse
-rwxr-xr-x 1 skx skx 37872 Apr 30 20:00 lighthouse
-rw-r--r-- 1 skx skx 12115 Apr 30 20:00 lihouse.com
-rw-r--r-- 1 skx skx 11982 Apr 30 20:00 lihouse.tap