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

« back to all changes in this revision

Viewing changes to include/waittime.h

  • 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
 
!Modified to work with the 6/3 library by Nicholas Daley
2
 
!-------------------------------------------------------------------------
3
 
! WaitTime - an Inform library to provide understanding of English time
4
 
!            strings, and give an enhanced Wait command. Time games only!
5
 
!            #include it just after Grammar.
6
 
!            by L. Ross Raszewski, 1996. Based on TimeWait.h by Andrew 
7
 
!           Clover.
8
 
!The ParseTime and WaitMoves functions are taken from TimeWait with only 
9
 
!minimal modification.  I worked this out after I found that I couldn't get
10
 
!Andrew's library to work.  I updated some of the code which I believe was, 
11
 
!outdated, and changed all the Wait subs so that they call WaitMovesSub.
12
 
!tw_waiting is used both as a sort of pseudo-global as Mr. Clover intended,
13
 
!and as an indicator.  I set up my programs to update the time through the
14
 
!TimePass routine by featuring the line:
15
 
!       if (tw_waiting hasnt locked) SetTime(the_time+1);
16
 
!       give tw_waiting ~locked;
17
 
!If an attempt is made to "Wait 0", or a function is called that is not meant
18
 
!to take any time, tw_waiting is given "locked", and the clock is not advanced.
19
 
!As with Andrew's TimeWait, any message that should halt the waiting process
20
 
!can give tw_waiting the "on" attribute, and discontinue waiting.  TimeWait
21
 
!featured a call to Time(), and wouldn't compile until I changed it to 
22
 
!TimePasses().  Even when I changed it, the program was still prone to getting
23
 
!stuck in infinite loops, which is why I converted the time passage into moves.
24
 
!the command "Wait 10" or "Wait 10 minutes" should now be equivalent to the player
25
 
!Typing "Wait" ten times.  During testing, I realized that calling TimePasses()
26
 
!did not perform all the tasks that occur at the end of the turn, so I changed it 
27
 
!again to End_Turn_Sequence().  I think this will make it operate satisfactorily.
28
 
!Also, I have added a MAX_WAIT, which should be defined as a constant before 
29
 
!inclusion.  Attempts to wait more than that many (minutes) will generate a
30
 
!"That's too long" message.  Inspired by "A Mind Forever Voyaging", I added
31
 
!a response to "Wait 0".  I'm not sure, but I think having a time passage rate
32
 
!other than 0 will give the program problems.  If you want more than 1 minute 
33
 
!to pass for each turn, some modification of the code will be necessary (Don't
34
 
!ask me how, I'm surprised I even got this far!).
35
 
!-------------------------------------------------------------------------
36
 
object tw_waiting "tw";
37
 
#IFNDEF MAX_WAIT; Constant MAX_WAIT 1000; #ENDIF;
38
 
!Action routines for Wait command...
39
 
 
40
 
[ WaitMovesSub i;
41
 
  if (noun > MAX_WAIT) {give tw_waiting locked; "That's too long to wait.";};
42
 
  if (noun==0)
43
 
    {print "Time doesn't pass.^";give tw_waiting locked;rtrue;};
44
 
  if (noun==1)
45
 
    <<Wait>>;
46
 
  print "Time passes.^";
47
 
  give tw_waiting ~on;
48
 
  for (i=noun:(i>1)&&(deadflag==0)&&(tw_waiting hasnt on):i--)
49
 
    {
50
 
    TimePasses();
51
 
#ifdef InformLibrary;
52
 
    InformLibrary.End_Turn_Sequence();
53
 
#ifnot;
54
 
 #ifdef EndTurnSequence;
55
 
    EndTurnSequence();
56
 
 #ifnot;
57
 
    Message fatalerror "waittime.h requires \
58
 
                        InformLibrary.End_Turn_Sequence() or \
59
 
                        EndTurnSequence() to be defined (this should be done \
60
 
                        by the Inform Library)";
61
 
 #endif;
62
 
#endif;
63
 
    DisplayStatus();
64
 
    #IFV5;
65
 
    DrawStatusLine();
66
 
    #ENDIF;
67
 
    }
68
 
  if ((tw_waiting has on)&&(i>1)&&(deadflag==0))
69
 
    print "^(waiting stopped)^";
70
 
];
71
 
 
72
 
[ WaitHoursSub;
73
 
  noun=noun*60;
74
 
  WaitMovesSub();
75
 
];
76
 
 
77
 
[ WaitUntilSub;
78
 
  if (parsed_number>=the_time) noun=parsed_number-the_time;
79
 
  if (parsed_number<the_time) 
80
 
      {parsed_number=the_time-parsed_number;
81
 
      noun=1440-parsed_number;
82
 
      print "(tomorrow)^^";
83
 
      }
84
 
  WaitMovesSub();   
85
 
];
86
 
 
87
 
!ParseTime takes data from the next words (using wn) and returns a
88
 
!the_time format time number, or -1 for unrecognisable. It can recognise
89
 
!time expressed in any of the following formats:
90
 
!!a. <"0"-"59">|<"one"-"twenty">|"half"|"quarter" ["minutes"|"minute"]
91
 
!   "past"|"to" <"1"-"12">|<"one"-"twelve"> ["am"|"pm"]
92
 
!b. <"1"-"12">|<"one"-"twelve"> ["o'clock"] ["am"|"pm"]
93
 
!c. <"1"-"12">|<"one"-"twelve"> <"0"-"59">|<"one"-"twenty"> ["am"|"pm"]
94
 
!d. <"1"-"12">":"<"0"-"59"> ["am"|"pm"]
95
 
!e. "midnight"|"midday"|"noon"
96
 
!!If no am/pm is specified, the next time likely to come up is chosen; that
97
 
!is, the one that's just ahead of the current time. However, if this
98
 
!happens, the tw_waiting object is given the 'general' attribute. Thus you
99
 
!can change the time returned by twelve hours in an action like SetClock
100
 
!if necessary.
101
 
!!The next dictionary command is there to allow us to compare a typed
102
 
!string with "o'clock", something we can't normally do as it is bounded by
103
 
!single quotes.
104
 
 
105
 
constant tw_oclock 'o^clock';
106
 
 
107
 
[ ParseTime i j k flg loop dig hr mn;
108
 
  give tw_waiting ~general;
109
 
  i=NextWord();
110
 
  if (i=='midday' or 'noon' or 'midnight')           ! then case (e) applies
111
 
    {
112
 
    if (i=='midnight')
113
 
      hr=0;
114
 
    else
115
 
      hr=12;
116
 
    mn=0;
117
 
    parsed_number=(hr*60+mn);
118
 
    }
119
 
  else
120
 
    {
121
 
    k=(wn-1)*4+1;                                    ! test for case (d)
122
 
    j=parse->k;
123
 
    j=j+buffer;
124
 
    k=parse->(k-1);
125
 
    flg=0;
126
 
    for (loop=0:loop<k:loop++)
127
 
      {
128
 
      dig=j->loop;
129
 
      if (dig==':')
130
 
        flg=1;
131
 
      }
132
 
    if ((k>2)&&(k<6)&&(flg==1))                      ! then case (d) applies
133
 
      {
134
 
      hr=0;
135
 
      mn=0;
136
 
      loop=0;
137
 
      .tw_diglph;
138
 
      dig=j->loop;
139
 
      loop++;
140
 
      if (dig~=':')
141
 
        hr=hr*10;
142
 
      if (dig=='0') { hr=hr+0; jump tw_diglph; }
143
 
      if (dig=='1') { hr=hr+1; jump tw_diglph; }
144
 
      if (dig=='2') { hr=hr+2; jump tw_diglph; }
145
 
      if (dig=='3') { hr=hr+3; jump tw_diglph; }
146
 
      if (dig=='4') { hr=hr+4; jump tw_diglph; }
147
 
      if (dig=='5') { hr=hr+5; jump tw_diglph; }
148
 
      if (dig=='6') { hr=hr+6; jump tw_diglph; }
149
 
      if (dig=='7') { hr=hr+7; jump tw_diglph; }
150
 
      if (dig=='8') { hr=hr+8; jump tw_diglph; }
151
 
      if (dig=='9') { hr=hr+9; jump tw_diglph; }
152
 
      if (dig~=':') return -1;
153
 
      while (loop<k)
154
 
        {
155
 
        dig=j->loop;
156
 
        mn=mn*10;
157
 
        if (dig=='0') { mn=mn+0; jump tw_digokm; }
158
 
        if (dig=='1') { mn=mn+1; jump tw_digokm; }
159
 
        if (dig=='2') { mn=mn+2; jump tw_digokm; }
160
 
        if (dig=='3') { mn=mn+3; jump tw_digokm; }
161
 
        if (dig=='4') { mn=mn+4; jump tw_digokm; }
162
 
        if (dig=='5') { mn=mn+5; jump tw_digokm; }
163
 
        if (dig=='6') { mn=mn+6; jump tw_digokm; }
164
 
        if (dig=='7') { mn=mn+7; jump tw_digokm; }
165
 
        if (dig=='8') { mn=mn+8; jump tw_digokm; }
166
 
        if (dig=='9') { mn=mn+9; jump tw_digokm; }
167
 
        return -1;
168
 
        .tw_digokm;
169
 
        loop++;
170
 
        }                                            ! decode digital time
171
 
      }
172
 
    else
173
 
      {
174
 
      j=NextWordStopped();
175
 
      if ((j==tw_oclock or -1)||(j=='am' or 'pm'))   ! then case (c) applies
176
 
        {
177
 
        hr=TryNumber(wn-2);
178
 
        mn=0;
179
 
        if (j~=tw_oclock)
180
 
          wn--;
181
 
        }
182
 
      else
183
 
        {
184
 
        k=TryNumber(wn-1);
185
 
        if (k~=-1000)                                ! then case (b) applies
186
 
          {
187
 
          mn=k;
188
 
          hr=TryNumber(wn-2);
189
 
          }
190
 
        else                                         ! well, must be case (a)
191
 
          {
192
 
          mn=TryNumber(wn-2);
193
 
          if (i=='quarter')
194
 
            mn=15;
195
 
          if (i=='twenty-five')
196
 
            mn=25;
197
 
          if (i=='half' or 'thirty')
198
 
            mn=30;
199
 
          if (j=='minute' or 'minutes')
200
 
            j=NextWord();                            ! ignore 'minutes'
201
 
          hr=TryNumber(wn);
202
 
          wn++;
203
 
          if (j~='past' or 'to')
204
 
            hr=-1;
205
 
          if (j=='to')
206
 
            {
207
 
            hr--;
208
 
            mn=60-mn;
209
 
            if (hr==0)
210
 
              hr=12;
211
 
            }
212
 
          }
213
 
        }
214
 
      }
215
 
    if ((hr>12)||(hr<1)||(mn>59)||(mn<0))
216
 
      parsed_number=-1;
217
 
    else
218
 
      {
219
 
      if (hr==12)                                    ! now sort out am/pm
220
 
        hr=0;
221
 
      i=NextWord();
222
 
      if (i=='pm')
223
 
        hr=hr+12;
224
 
      else
225
 
        if (i~='am')                                 ! am or pm implied, then?
226
 
          {
227
 
          give tw_waiting general;
228
 
          wn--;
229
 
          i