~ubuntu-branches/ubuntu/vivid/inform/vivid

« back to all changes in this revision

Viewing changes to html/answers2/answer86.html

  • Committer: Bazaar Package Importer
  • Author(s): Jan Christoph Nordholz
  • Date: 2008-05-26 22:09:44 UTC
  • mfrom: (2.1.1 lenny)
  • Revision ID: james.westby@ubuntu.com-20080526220944-ba7phz0d1k4vo7wx
Tags: 6.31.1+dfsg-1
* Remove a considerable number of files from the package
  due to unacceptable licensing terms.
* Repair library symlinks.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<HTML><HEAD><TITLE>Answer to Exercise 86</TITLE></HEAD>
2
 
<BODY BGCOLOR="#FFFFFF">
3
 
<TABLE><TR><TD Valign="top"><IMG SRC="../icons/exercise.gif" ALT="??"><TD bgcolor="#FAA89B"><B>ANSWER TO EXERCISE 86</B><TR><TD><TD>
4
 
<P>
5
 
 
6
 
 
7
 
The label object itself is not too bad:
8
 
<PRE>
9
 
Object -&#62; label "red sticky label"
10
 
  with name "red" "sticky" "label",
11
 
       number 0,
12
 
       before
13
 
       [;  PutOn, Insert:
14
 
               if (self.number~=0)
15
 
               {   print "(first removing the label from ",
16
 
                   (the) self.number, ")^"; self.number=0; move self to player;
17
 
               }
18
 
               if (second==self) "That would only make a red mess.";
19
 
               self.number=second; remove self;
20
 
               print_ret "You affix the label to ", (the) second, ".";
21
 
       ],
22
 
       react_after
23
 
       [ x; x=self.number; if (x==0) rfalse;
24
 
           Look: if (x in location)
25
 
                   print "^The red sticky label is stuck to ", (the) x, ".^";
26
 
           Inv:  if (x in player) 
27
 
                   print "^The red sticky label is stuck to ", (the) x, ".^";
28
 
       ],
29
 
       each_turn
30
 
       [;  if (parent(self)~=0) self.number=0; ];
31
 
</PRE>
32
 
 
33
 
Note that <TT>label.number</TT> holds the object the label is stuck to, or
34
 
0 if it's unstuck: and that when it is stuck, it is removed from the
35
 
object tree.  It therefore has to be moved into scope, so we need the
36
 
rule: if the labelled object is in scope, then so is the label.
37
 
<PRE>
38
 
Global disable_self;
39
 
[ InScope actor i1 i2;
40
 
  if (label.number==0) rfalse; if (disable_self==1) rfalse;
41
 
  disable_self=1;
42
 
  i1 = TestScope(label, actor);
43
 
  i2 = TestScope(label.number, actor);
44
 
  disable_self=0;
45
 
  if (i1~=0) rfalse;
46
 
  if (i2~=0) PlaceInScope(label);
47
 
  rfalse;
48
 
];
49
 
</PRE>
50
 
 
51
 
This routine has two interesting points: firstly, it disables itself
52
 
while testing scope (since otherwise the game would go into an endless
53
 
recursion), and secondly it only puts the label in scope if it isn't
54
 
already there.  This is just a safety precaution to prevent the label
55
 
reacting twice to actions (and isn't really necessary since the label
56
 
can't already be in scope, but is included for the sake of example).
57
 
</TABLE>
58
 
<HR>Back to <A HREF="../section28.html#ex86">the exercise in section 28</A><HR>
59
 
<SMALL><I>Mechanically translated to HTML from third edition as revised 16 May 1997. Copyright &#169; Graham Nelson 1993, 1994, 1995, 1996, 1997: all rights reserved.</I></SMALL></BODY></HTML>