Constant Story "A Surreal Experience";
Constant Headline
"^A Demonstration of Bitwise Attributes^";
Release 2;
constant MAX_SCORE = 1;
Include "Parser";
Include "VerbLib";
!----------------------------------------------------------------------------
! This little game demonstrates what I call bitwise attributes. It's a rather
! clumsy but powerful way to create attributes beyond the 16 available in v5
! games, a number which I find to be insufficient. Another advantage is they
! can be grouped together in some kind of relationship; in this example, a
! variable called "talked" is set up to track what has or hasn't been asked.
! You could just as easily create a
! variable called "pressed" for which buttons have been pressed on a machine,
! or if any object has been manipulated in any way.
!
! Although an extremely simple example, I hope that this game shows how
! bitwise attributes can contribute to create very dynamic interactions with
! objects, especially NPCs. Feel free to adopt this method in your own games
! if you find it useful. Maybe you can find a use for other bitwise operators
! such as XOR, NOT and SHIFT LEFT/RIGHT, or improve and expand on the model
! presented here. If you do, I'd be interested to hear from you.
!
! For those who have not come across binary digits before, have a quick read
! of the relevant parts of a C or C++ manual.
!
!
[email protected]
!----------------------------------------------------------------------------
object room "White Room"
with description "The room is white, luminous and almost completely empty.",
has light;
object -> man "man"
with name 'man',
description "He's eating an icecream and smoking a cigar.",
describe [;
if (room hasnt visited) "^A man stands before you, eating an icecream and smoking a cigar.^^~Hello.~ he says, ~Ask me about
myself to demonstrate bitwise attributes.~";
],
talked 0, ! When this variable is treated as a binary number, each bit can represent an attribute since attributes are
! basically just binary flags themselves. You therefore have 8 or even 16 new attributes to use. Unfortunately
! they can't really be named so it takes a bit of work to keep track of them, but if you keep a record of them
! somewhere it's not too difficult.
life [;
ask: ! To test if certain bits are on (and the state of the other bits don't matter), OR the variable with the
! COMPLEMENT of what your testing for. If the result is 255 ( = $$11111111) then the test is true.
switch (second) {
'man','himself':
if ((self.talked | $$10010101) == 255) {
score++;
print "~You've already asked me about everything I know.~^^He vanishes into a puff of smoke.";
deadflag = 2;
rtrue;
}
print "~Why don't you ask me about my ";
if (self.talked == 0) "icecream or my cigar?~";
if ((self.talked & $$01100000) == $$01100000) "icecream?~";
if ((self.talked & $$00001010) == $$00001010) "cigar?~";
'icecream':
if ((self.talked | $$11110101) == 255) "~Bits 4 and 2 (the icecream) have already been set.~";
self.talked = self.talked + $$00001010;
"~Okay, the icecream is represented by bits 4 and 2, so let's set them.^^00001010~";
'cigar':
if ((self.talked | $$10011111) == 255) "~Bits 7 and 8 (the cigar) have already been set.~";
self.talked = self.talked + $$01100000;
"~Okay, the cigar is represented by bits 7 and 8, so let's set them.^^01100000~";
'bitwise','attributes':
"~Have a look at the source code, the comments should explain everything. You could also
Email the author at: paris.downes@@64gmail.com~";
}
],
article "a",
has animate transparent;
object -> icecream "icecream"
! 00001010 = bit 4,2
with name 'icecream',
description "The man seems to be enjoying it immensely.",
has scenery;
object -> cigar "cigar"
! 01100000 = bits 7,8
with name 'cigar',
description "The man is taking short puffs of a thick, obnoxious smelling cigar.",
has scenery;
[ Initialise; location = room; ];
Include "Grammar";