Debian Codegraph @

terps/agility/exec.c

raw symbols SCIP
1 /* exec.c-- player command execution utilities */
2 /* Copyright (C) 1996-1999,2001 Robert Masenten */
3 /* This program may be redistributed under the terms of the
4 GNU General Public License, version 2; see agility.h for details. */
5 /* */
6 /* This is part of the source for AGiliTy */
7
8 /* This file contains the wrapper for running player commands,
9 routines run at the end of the turn, and various other functions
10 needed by runverb.c and token.c. */
11
12 #define global
13
14 #include <assert.h>
15 #include <ctype.h>
16 #include <stdio.h>
17 #include <limits.h>
18
19 #include "agility.h"
20 #include "interp.h"
21 #include "exec.h"
22
23 static rbool pronoun_mode;
24
25 word realverb=0; /* Name of current verb. (normally ~= input[vp])*/
26
27
28 /* ------------------------------------------------------------------- */
29 /* High level output functions, used for printing messages, error */
30 /* messages, and everything else. They call the direct output functions */
31 /* in interface.c The reason they're in runverb.c is that they need to */
32 /* access item info in order to fill in the blanks */
33
34
35 /* This updates the contents of compass_rose, which can be used by the
36 OS layer to print out some sort of representation of which way the
37 player can go. */
38
39 static void set_compass_rose(void)
40 {
41 int i, bit;
42
43 compass_rose=0;
44 if (!islit()) return; /* No compass in darkness */
45 for(i=0,bit=1;i<12;i++,bit<<=1)
46 if (troom(room[loc].path[i])) compass_rose|=bit;
47 }
48
49 static void time_out(char *s)
50 {
51 int hr,min;
52
53 hr=curr_time/100;
54 min=curr_time%100;
55
56 if (milltime_mode)
57 sprintf(s,"%02d:%02d",hr,min);
58 else {
59 if (hr>12) hr=hr-12;
60 if (hr==0) hr=12;
61 sprintf(s,"%2d:%02d %s",hr,min,(curr_time>=1200)?"pm":"am");
62 }
63 }
64
65
66
67
68
69 void set_statline()
70 {
71 char timestr[20];
72
73 recompute_score();
74 set_compass_rose();
75
76 rstrncpy(l_stat,room[loc].name,81);
77
78 time_out(timestr);
79
80 switch(statusmode) {
81 case 0:sprintf(r_stat,"Score: %ld Moves: %d",tscore,turncnt);break;
82 case 1:sprintf(r_stat,"Score: %ld %s",tscore,timestr);break;
83 case 2:sprintf(r_stat,"Moves: %d",turncnt);break;
84 case 3:sprintf(r_stat,"%s",timestr);break;
85 case 4:r_stat[0]='\0';break; /* 'Trinity style' status line */
86 case 5:sprintf(r_stat,"Score: %ld",tscore);break;
87 }
88 }
89
90
91 /* -------------------------------------------------------------------- */
92 /* Message printing / $ substitution Routines */
93 /* -------------------------------------------------------------------- */
94
95 #define FILL_SIZE 100
96
97
98 /* Tries to convert *pstr to a number, which it returns.
99 If it fails, or if the number is not in the range 0..maxval,
100 it returns -1.
101 It advances *pstr to point after the number and after the
102 terminating character, if relevant.
103 <term_char> is the terminating character; if this is 0, then
104 the calling routine will worry about the terminating character.
105 <maxval> of 0 indicates no upper bound
106 */
107
108 static int extract_number(const char **pstr, int maxval,
109 char term_char)
110 {
111 const char *s;
112 long n; /* n holds the value to be returned; i holds
113 the number of characters parsed. */
114 n=0;
115 s=*pstr;
116 while(*s==' '||*s=='\t') s++;
117 for(;*s!=0;s++) {
118 if (*s<'0' || *s>'9') break;
119 n=10*n + (*s-'0');
120 if (maxval && n>maxval) return -1;
121 }
122 if (term_char) {
123 if (*s==term_char) s++;
124 else return -1;
125 }
126 *pstr=s;
127 return n;
128 }
129
130 #define BAD_PROP (-1000)
131
132 /* This is used by #PROP[obj].[prop]# and $ATTR[obj].[attr]$, etc. */
133 /* isprop: Are we looking for a property (as opposed to an attribute)? */
134 static void extract_prop_val(const char **pstr,
135 int *id, int *val,
136 rbool isprop, const char term_char)
137 {
138 const char *s;
139 int v; /* object number / final value */
140 int i; /* Attribute id */
141 rbool builtin; /* Expect builtin property or attribute? */
142
143 *id=i=BAD_PROP; *val=0; /* Failure case by default */
144 builtin=0;
145 s=*pstr;
146 if (match_str(&s,"NOUN")) v=dobj;
147 else if (match_str(&s,"OBJECT")) v=iobj;
148 else v=extract_number(&s,maxcreat,0); /* Must be object number */
149 while(*s=='.') {
150 s++;
151 if (*s=='-') {
152 builtin=1; s++;
153 } else
154 builtin=0;
155 i=extract_number(&s,0,0);
156 if (!troom(v) && !tnoun(v) && !tcreat(v)) {i=-1;continue;}
157 if (isprop || *s=='.') /* Treat as property */
158 v=builtin ? getprop(v,i) : op_objprop(2,v,i,0);
159 else /* Treat as attribute */
160 v=builtin ? getattr(v,i) : op_objflag(2,v,i);
161 }
162 if ( *s!=term_char) return;
163 *pstr=s+1;
164 if (i<0) return;
165 *id=builtin ? -1 : i;
166 *val=v;
167 }
168
169
170
171
172 static word it_pronoun(int item, rbool ind_form)
173 /* Return the correct pronoun to go with item;
174 ind_form is 1 if we want the object form, 0 if we want the
175 subject form */
176 {
177 if (it_plur(item))
178 return (ind_form ? ext_code[wthem] : ext_code[wthey]);
179 if (tcreat(item))
180 switch (creature[item-first_creat].gender) {
181 case 0:return ext_code[wit];
182 case 1:return (ind_form ? ext_code[wher] : ext_code[wshe]);
183 case 2:return (ind_form ? ext_code[whim] : ext_code[whe]);
184 }
185 return ext_code[wit];
186 }
187
188 /* This sets the value of "The" for the given noun. */
189 /* (In particular, proper nouns shouldn't have a "the") */
190 static void theset(char *buff, int item)
191 {
192 if (it_proper(item))
193 strcpy(buff,"");
194 else
195 strcpy(buff,"the ");
196 }
197
198
199 static void num_name_func(parse_rec *obj_rec,char *fill_buff, word prev_adj)
200 /* This is a subroutine to wordcode_match. */
201 /* It gives either a noun name or a number, depending. */
202 /* prev_adj is a word if this was preceded by its associated $adjective$;
203 the goal is to avoid having $adjective$ $noun$ expand to (e.g.)
204 'silver silver' when the player types in "get silver" to pick up
205 a magic charm with synonym 'silver'. */
206 {
207 word w;
208
209 if (obj_rec==NULL) {
210 strcpy(fill_buff,"");
211 return;
212 }
213
214 w=0;
215 if (obj_rec->noun!=0) w=obj_rec->noun;
216 if ( (w==0 || w==prev_adj) && obj_rec->obj!=0)
217 w=it_name(obj_rec->obj);
218
219 if (w==0) {
220 if (obj_rec->info==D_NUM) sprintf(fill_buff,"%ld",(long)obj_rec->num);
221 else strcpy(fill_buff,"");
222 #if 0
223 strcpy(fill_buff,"that"); /* We can try and hope */
224 #endif
225 return;
226 }
227
228 if (w==prev_adj) /* ... and prev_adj!=0 but we don't need to explicity
229 test that since w!=0 */
230 fill_buff[0]=0; /* i.e. an empty string */
231 else {
232 rstrncpy(fill_buff,dict[w],FILL_SIZE);
233 if (it_proper(obj_rec->obj)) fill_buff[0]=toupper(fill_buff[0]);
234 }
235 }
236
237 static word get_adj( parse_rec *obj_rec, char *buff)
238 {
239 word w;
240
241 if (obj_rec->adj!=0) w=obj_rec->adj;
242 else w=it_adj(obj_rec->obj);
243
244 if (w==0) strcpy(buff,"");
245 else {
246 rstrncpy(buff,dict[w],FILL_SIZE);
247 if (it_proper(obj_rec->obj)) buff[0]=toupper(buff[0]);
248 }
249
250 return w;
251 }
252
253
254
255 #define d2buff(i) {rstrncpy(fill_buff,dict[i],FILL_SIZE);return 1;}
256 #define num_name(obj_rec,jsa) {num_name_func(obj_rec,fill_buff,jsa);return 1;}
257 /* jsa= Just seen adj */
258 #define youme(mestr,youstr) {strcpy(fill_buff,irun_mode?mestr:youstr);\
259 return 1;}
260
261 word just_seen_adj; /* This determines if we just saw $adjective$; if so,
262 this is set to it, otherwise it is zero. See
263 num_name_func above. */
264
265 static int wordcode_match(const char **pvarname,char *fill_buff,
266 int context,const char *pword)
267 /* Check <*p*pvarname> for a match; put subs text in fill_buf
268 <context> indicates who called us; this determines
269 what substitutions are valid. See interp.h for possible
270 values. Move *p*pvarname after whatever is matched.
271 <pword> contains the parse word when context is MSG_PARSE. */
272 /* $ forms:
273 $verb$, $noun$, $adjective$, $prep$, $object$, $name$,
274 $n_pro$, $o_pro$, $n_indir$, $o_indir$,
275 $name_pro$, $name_indir$
276 $n_is$, $o_is$, $name_is$
277 $c_name$
278 $n_was$, $o_was$, $name_was$
279 $the_n$, $the_o$, $the_name$
280 */
281 /* Also $STRn$, $FLAGn$, $ONn$, $OPENn$, $LOCKEDn$ */
282 /* Support for FLAG, ON, OPEN, and LOCKED added by Mitch Mlinar */
283 /* Return 0 if no match, 1 if there is */
284 {
285 int hold_val, hold_id;
286
287 fill_buff[0]=0; /* By default, return "\0" string */
288 if (match_str(pvarname,"STR")) { /* String variable */
289 hold_id=extract_number(pvarname,MAX_USTR,'$');
290 if (hold_id<1) return 0;
291 rstrncpy(fill_buff,userstr[hold_id-1],FILL_SIZE);
292 return 1;
293 }
294 else if (match_str(pvarname,"VAR")) {
295 hold_id=extract_number(pvarname,VAR_NUM,'$');
296 if (hold_id<0) return 0;
297 hold_val=agt_var[hold_id];
298 rstrncpy(fill_buff,
299 get_objattr_str(AGT_VAR,hold_id,hold_val),FILL_SIZE);
300 return 1;
301 } else if (match_str(pvarname,"FLAG")) {
302 hold_id=extract_number(pvarname,FLAG_NUM,'$');
303 if (hold_id<0) return 0;
304 rstrncpy(fill_buff,
305 get_objattr_str(AGT_FLAG,hold_id,flag[hold_id]),FILL_SIZE);
306 return 1;
307 } else if (match_str(pvarname,"ATTR")) {
308 extract_prop_val(pvarname,&hold_id,&hold_val,0,'$');
309 if (hold_id==BAD_PROP) return 1;
310 rstrncpy(fill_buff,
311 get_objattr_str(AGT_OBJFLAG,hold_id,hold_val),FILL_SIZE);
312 return 1;
313 } else if (match_str(pvarname,"PROP")) {
314 extract_prop_val(pvarname,&hold_id,&hold_val,1,'$');
315 if (hold_id==BAD_PROP) return 1;
316 rstrncpy(fill_buff,
317 get_objattr_str(AGT_OBJPROP,hold_id,hold_val),FILL_SIZE);
318 return 1;
319 } else if (match_str(pvarname,"OPEN")) {
320 hold_val = extract_number(pvarname,maxnoun,'$');
321 strcpy(fill_buff,it_open(hold_val)? "open" : "closed");
322 return 1;
323 }
324 else if (match_str(pvarname,"ON")) {
325 hold_val = extract_number(pvarname,maxnoun,'$');
326 strcpy(fill_buff,it_on(hold_val)? "on" : "off");
327 return 1;
328 }
329 else if (match_str(pvarname,"LOCKED")) {
330 hold_val = extract_number(pvarname,maxnoun,'$');
331 strcpy(fill_buff,it_locked(hold_val,0) ? "locked" : "unlocked");
332 return 1;
333 }
334
335 if (context==MSG_PARSE) {
336 /* The only special subsitution allowed is $word$. */
337 if (match_str(pvarname,"WORD$")) {
338 if (pword==NULL) fill_buff[0]=0;
339 else rstrncpy(fill_buff,pword,FILL_SIZE);
340 return 1;
341 } else return 0;
342 }
343
344 if (context!=MSG_MAIN) {
345 /* d2buff is a macro that returns 1 */
346 if (match_str(pvarname,"NOUN$"))
347 num_name(dobj_rec,just_seen_adj);
348 just_seen_adj=0; /* It doesn't matter. */
349 if (match_str(pvarname,"VERB$"))
350 d2buff(realverb); /* auxsyn[vb][0] */
351 if (match_str(pvarname,"OBJECT$"))
352 num_name(iobj_rec,0);
353 if (match_str(pvarname,"NAME$"))
354 num_name(actor_rec,0);
355 if (match_str(pvarname,"ADJECTIVE$")) {
356 just_seen_adj=get_adj(dobj_rec,fill_buff);
357 return 1;
358 }
359 if (match_str(pvarname,"PREP$"))
360 d2buff(prep);
361 if (match_str(pvarname,"N_PRO$"))
362 d2buff(it_pronoun(dobj,0));
363 if (match_str(pvarname,"O_PRO$"))
364 d2buff(it_pronoun(iobj,0));
365 if (match_str(pvarname,"NAME_PRO$"))
366 d2buff(it_pronoun(actor,0));
367 if (match_str(pvarname,"N_INDIR$"))
368 d2buff(it_pronoun(dobj,1));
369 if (match_str(pvarname,"O_INDIR$"))
370 d2buff(it_pronoun(iobj,1));
371 if (match_str(pvarname,"NAME_INDIR$"))
372 d2buff(it_pronoun(actor,1));
373 if (match_str(pvarname,"N_IS$")) {
374 if (!it_plur(dobj)) d2buff(ext_code[wis])
375 else d2buff(ext_code[ware]);
376 }
377 if (match_str(pvarname,"O_IS$")) {
378 if (!it_plur(iobj)) d2buff(ext_code[wis])
379 else d2buff(ext_code[ware]);
380 }
381 if (match_str(pvarname,"NAME_IS$")) {
382 if (!it_plur(actor)) d2buff(ext_code[wis])
383 else d2buff(ext_code[ware]);
384 }
385
386 if (match_str(pvarname,"N_WAS$")) {
387 if (!it_plur(dobj)) d2buff(ext_code[wwas])
388 else d2buff(ext_code[wwere]);
389 }
390 if (match_str(pvarname,"O_WAS$")) {
391 if (!it_plur(iobj)) d2buff(ext_code[wwas])
392 else d2buff(ext_code[wwere]);
393 }
394 if (match_str(pvarname,"NAME_WAS$")) {
395 if (!it_plur(actor)) d2buff(ext_code[wwas])
396 else d2buff(ext_code[wwere]);
397 }
398 if (match_str(pvarname,"THE_N$"))
399 {theset(fill_buff,dobj);return 1;}
400 if (match_str(pvarname,"THE_O$"))
401 {theset(fill_buff,iobj);return 1;}
402 if (match_str(pvarname,"THE_NAME$"))
403 {theset(fill_buff,actor);return 1;}
404 if (match_str(pvarname,"THE_C$"))
405 {theset(fill_buff,curr_creat_rec->obj); return 1;}
406 if (match_str(pvarname,"C_NAME$"))
407 num_name(curr_creat_rec,0);
408 if (match_str(pvarname,"TIME$"))
409 {time_out(fill_buff);return 1;}
410 }
411
412 if (pronoun_mode && match_str(pvarname,"YOU$"))
413 youme("I","you");
414 if (pronoun_mode && match_str(pvarname,"ARE$"))
415 youme("am","are");
416 if (pronoun_mode && match_str(pvarname,"YOU_OBJ$"))
417 youme("me","you");
418 if (pronoun_mode && match_str(pvarname,"YOUR$"))
419 youme("my","your");
420 if (pronoun_mode && match_str(pvarname,"YOU'RE$"))
421 youme("i'm","you're");
422 return 0; /* Don't recognize $word$ */
423 }
424
425
426
427 static int capstate(const char *varname)
428 {
429 if (islower(varname[0])) return 0; /* $word$ */
430 if (islower(varname[1])) return 2; /* $Word$ */
431 if (!isalpha(varname[1]) && varname[1]!=0
432 && islower(varname[2])) return 2;
433 else return 1; /* $WORD$ */
434 }
435
436 static char fill_buff[FILL_SIZE]; /* Buffer to hold returned string */
437
438 static char *wordvar_match(const char **pvarname,char match_type,
439 int context, const char *pword)
440 /* Match_type=='#' for variables, '$' for parsed words */
441 /* Possible # forms: #VARn#, #CNTn# */
442 /* See above for $ forms */
443 /* Moves *pvarname to point after matched object */
444 {
445 int i, hold_val, hold_prop;
446 const char *start;
447
448 start=*pvarname;
449 if (match_type=='$') {
450 i=wordcode_match(pvarname,fill_buff,context,pword);
451 if (i==0) return NULL;
452 /* Now need to fix capitalization */
453 switch (capstate(start)) {
454 case 0:break; /* $word$ */
455 case 1: /* $WORD$ */
456 for(i=0;fill_buff[i]!='\0';i++)
457 fill_buff[i]=toupper(fill_buff[i]);
458 break;
459 case 2: /* $Word$ */
460 fill_buff[0]=toupper(fill_buff[0]);
461 break;
462 }
463 } else { /* So match type is '#' */
464 if (match_str(pvarname,"VAR")) {
465 hold_val=extract_number(pvarname,VAR_NUM,'#');
466 if (hold_val<0) return NULL;
467 hold_val=agt_var[hold_val];
468 }
469 else if (match_str(pvarname,"CNT")||
470 match_str(pvarname,"CTR")) {
471 hold_val=extract_number(pvarname,CNT_NUM,'#');
472 if (hold_val<0) return NULL;
473 hold_val=cnt_val(agt_counter[hold_val]);
474 }
475 else if (match_str(pvarname,"PROP")) {
476 extract_prop_val(pvarname,&hold_prop,&hold_val,1,'#');
477 if (hold_prop==BAD_PROP) hold_val=0;
478 }
479 else
480 return NULL;
481
482 /* Now to convert hold_val into a string */
483 sprintf(fill_buff,"%d",hold_val);
484
485 }
486 return fill_buff;
487 }
488
489 static char *format_line(const char *s,int context,const char *pword)
490 /* Do $word$ substituations; return the result */
491 {
492 char *t; /* The new string after all the substitutions. */
493 int t_size; /* How much space has been allocated for it. */
494 const char *p, *oldp; /* Pointer to the original string */
495 int i;
496 char *fill_word, *q; /* Word used to fill in the blanks, and a pointer
497 used to iterate through it*/
498 char fill_type; /* '#'=#variable#, '$'=$word$ */
499
500 /* Need to do subsitutions and also correct for tabs */
501 t_size=200;
502 t=rmalloc(t_size+FILL_SIZE+10);
503 just_seen_adj=0;
504
505 /* Note that I leave some margin here: t is 310 characters, but i will never
506 be allowed above around 200. This is to avoid having to put special
507 checking code throughout the following to make sure t isn't overrun */
508 for(p=s,i=0;*p!='\0';p++) {
509 if (i>=t_size) {
510 t_size=i+100;
511 t=rrealloc(t,t_size+FILL_SIZE+10);
512 }
513 if (!rspace(*p) && *p!='$')
514 just_seen_adj=0;
515 if (*p=='$' || *p=='#') {
516 /* Read in $word$ or #var# and do substitution */
517 fill_type=*p;
518 oldp=p++; /* Save old value in case we are wrong and then
519 increment p */
520 fill_word=wordvar_match(&p,fill_type,context,pword);
521 if (fill_word==NULL) {
522 /*i.e. no match-- so just copy it verbatim */
523 t[i++]=fill_type;
524 just_seen_adj=0;
525 p=oldp; /* Go back and try again... */
526 } else { /* Fill in word */
527 p--;
528 if (fill_word[0]=='\0') { /* Empty string */
529 /* We need to eliminate a 'double space' in this case */
530 if ( ( oldp==s || rspace(*(oldp-1)) ) && rspace(*(p+1)))
531 p++;
532 } else /* Normal case */
533 for(q=fill_word;*q!='\0';)
534 t[i++]=*q++;
535 }
536 } /* End $/# matcher */
537 else
538 t[i++]=*p;
539 } /* End scanning loop */
540
541 if (aver<AGX00 && i>0 && t[i-1]==' ') {
542 /* For pre-Magx, delete trailing spaces */
543 do
544 i--;
545 while(i>0 && t[i]==' ');
546 i++;
547 }
548 t[i]=0;
549 t=rrealloc(t,i+1);
550 return t;
551 }
552
553 void raw_lineout(const char *s, rbool do_repl, int context, const char *pword)
554 {
555 char *outstr;
556
557 if (do_repl) {
558 outstr=format_line(s,context,pword);
559 writestr(outstr);
560 rfree(outstr);
561 } else
562 writestr(s);
563 }
564
565
566 static void lineout(const char *s, rbool nl,int context,const char *pword)
567 {
568 raw_lineout(s,1,context,pword);
569 if (nl) writeln("");
570 else writestr(" ");
571 }
572
573 static void gen_print_descr(descr_ptr dp,rbool nl,
574 int context,const char *pword)
575 {
576 int j;
577 descr_line *txt;
578
579 agt_textcolor(7);
580 textbold=0;
581 agt_par(1);
582 txt=read_descr(dp.start,dp.size);
583 if (txt!=NULL)
584 for(j=0;txt[j]!=NULL;j++)
585 lineout(txt[j], nl || (txt[j+1]!=NULL), context,pword);
586 free_descr(txt);
587 agt_par(0);
588 agt_textcolor(7);
589 textbold=0;
590 }
591
592 void print_descr(descr_ptr dp,rbool nl)
593 {
594 gen_print_descr(dp,nl,MSG_DESC,NULL);
595 }
596
597 void quote(int msgnum)
598 {
599 char **qptr;
600 descr_line *txt;
601 int i;
602 int len;
603
604 txt=read_descr(msg_ptr[msgnum-1].start, msg_ptr[msgnum-1].size);
605 if (txt!=NULL) {
606 for(len=0;txt[len]!=NULL;len++);
607 qptr=rmalloc(len*sizeof(char*));
608 for(i=0;i<len;i++)
609 qptr[i]=format_line(txt[i],MSG_DESC,NULL);
610 free_descr(txt);
611 textbox(qptr,len,TB_BORDER|TB_CENTER);
612 rfree(qptr);
613 }
614 }
615
616
617 void msgout(int msgnum,rbool add_nl)
618 {
619 print_descr(msg_ptr[msgnum-1],add_nl);
620 }
621
622
623 #define MAX_NUM_ERR 240 /* Highest numbered STANDARD message */
624 #define OLD_MAX_STD_MSG 185
625
626 /* Fallback messages should always have msgid less than the original */
627 int stdmsg_fallback[MAX_NUM_ERR-OLD_MAX_STD_MSG]={
628 0,0,0,12,0, /* 186 - 190 */
629 0,0,0,0,0, /* 191 - 195 */
630 0,13,13,5,10, /* 196 - 200 */
631 10,61,10,16,59, /* 201 - 205 */
632 90,107,116,135,140, /* 206 - 210 */
633 184,3,47,185,61, /* 211 - 215 */
634 0,0,0,0,0, /* 216 - 220 */
635 0,0,0,0,0,0,0,0,0,0, /* 221 - 230 */
636 0,0,0,0,0,0,0,0,0,0 /* 231 - 240 */
637 };
638
639 void gen_sysmsg(int msgid,char *s,int context,const char *pword)
640 /* Prints either STANDARD message number <msgid> or default msg <s>;
641 A <msgid> of 0 means there is no standard message counterpart.
642 <context> determines what $$ substitutions are meaningful
643 <parseword> gives the $pword$ substitution for MSG_PARSE messages
644 msgid 3 should probably *not* be redirected to avoid giving hints to
645 the player as to what nouns exist in the game.
646 */
647 {
648 /* Use gamefile's redefined version of message? */
649 rbool use_game_msg;
650 rbool nl; /* Should it be followed by a newline? */
651
652 nl=1; /* By default, follow message with newline */
653
654 /* The following msgids shouldn't be followed by newlines: */
655 if (msgid==1 || msgid==145 || (msgid>=218 && msgid<=223)
656 || msgid==225)
657 nl=0;
658
659 if (DEBUG_SMSG) rprintf("\nSTD %d",msgid);
660
661 use_game_msg=( (PURE_SYSMSG || s==NULL)
662 && msgid!=0 && msgid<=NUM_ERR
663 && err_ptr!=NULL );
664
665 if (use_game_msg) {
666 /* Check for fall-back messages */
667 if (err_ptr[msgid-1].size<=0
668 && msgid>OLD_MAX_STD_MSG && msgid<=MAX_NUM_ERR) {
669 msgid=stdmsg_fallback[msgid-OLD_MAX_STD_MSG-1];
670 if (DEBUG_SMSG) rprintf("==> %3d");
671 }
672 if (msgid!=0 && err_ptr[msgid-1].size>0) {
673 if (DEBUG_SMSG) rprintf(" : From gamefile\n");
674 gen_print_descr(err_ptr[msgid-1],nl,context,pword);
675 } else use_game_msg=0;
676 }
677
678 if (DEBUG_SMSG && !use_game_msg) rprintf(" : Default\n");
679
680 if (!use_game_msg) {
681 /* Either the game doesn't redefine the message, or we're ignoring
682 redefinitions */
683 if (s==NULL) return;
684 pronoun_mode=1;
685 lineout(s,nl,context,pword);
686 pronoun_mode=!PURE_PROSUB;
687 }
688 }
689
690
691 void sysmsg(int msgid,char *s)
692 {
693 gen_sysmsg(msgid,s,MSG_RUN,NULL);
694 }
695
696
697 void alt_sysmsg(int msgid,char *s,
698 parse_rec *new_dobjrec,parse_rec *new_iobjrec)
699 {
700 parse_rec *save_dobjrec, *save_iobjrec;
701 integer save_dobj, save_iobj;
702
703 save_dobj=dobj; save_dobjrec=dobj_rec;
704 dobj=p_obj(new_dobjrec); dobj_rec=new_dobjrec;
705
706 save_iobj=iobj; save_iobjrec=iobj_rec;
707 iobj=p_obj(new_iobjrec); iobj_rec=new_iobjrec;
708
709 gen_sysmsg(msgid,s,MSG_RUN,NULL);
710
711 dobj=save_dobj; dobj_rec=save_dobjrec;
712 iobj=save_iobj; iobj_rec=save_iobjrec;
713 }
714
715
716 void sysmsgd(int msgid,char *s,parse_rec *new_dobjrec)
717 /* Front end for sysmsg w/alternative direct object */
718 {
719 alt_sysmsg(msgid,s,new_dobjrec,NULL);
720 }
721
722
723 /* -------------------------------------------------------------------- */
724 /* QUESTION and ANSWER processing */
725 /* -------------------------------------------------------------------- */
726
727
728 static char *match_string(char *ans,char *corr_ans,int n)
729 /* Searches for s (w/ surrounding whitespace removed) inside ans */
730 /* looking at only n characters of s */
731 {
732 char *s;
733 char *corr;
734 int i;
735
736 s=rstrdup(corr_ans);
737 for(i=n-1;i>0 && isspace(s[i]);i--); /* Kill trailing whitespace */
738 s[i+1]=0;
739 for(i=0;s[i]!=0;i++) s[i]=tolower(s[i]);
740 for(i=0;isspace(s[i]);i++); /* Kill leading whitespace */
741 corr=strstr(ans,s+i);
742 rfree(s);
743 return corr;
744 }
745
746
747 static rbool check_answer(char *ans,long start,long size)
748 /* qnum has already been fixed to start from 0 */
749 /* Master's edition answer checker. Master's edition answers can */
750 /* be seperate by AND and OR characters. If there is one OR in the */
751 /* answer, all ANDs will also be treated as ORs */
752 /* Furthermore, AND-delimited strings must appear in the correct order */
753 /* unless PURE_ANSWER is false */
754 {
755 char *corr, *corr2; /* Pointer into answer to match correct answers */
756 int match_mode; /* 0=AND mode, 1=OR mode */
757 descr_line *astr; /* Holds the answer string */
758 int i; /* Index to line of astr we're on. */
759 char *p,*q, *r; /* Used to break astr up into pieces and
760 loop over them */
761
762 astr=read_descr(start,size);
763 if (astr==NULL) {
764 if (!PURE_ERROR)
765 writeln("GAME ERROR: Empty answer field.");
766 return 1; }
767
768 match_mode=0;
769 for(i=0;astr[i]!=NULL;i++)
770 if (strstr(astr[i],"OR")!=NULL) {match_mode=1;break;}
771
772 corr=ans;
773 for(i=0;astr[i]!=NULL;i++) { /* loop over all lines of the answer */
774 p=astr[i];
775 do {
776 q=strstr(p,"OR");
777 r=strstr(p,"AND");
778 if (q==NULL || (r!=NULL && r<q)) q=r;
779 if (q==NULL) q=p+strlen(p); /* i.e. points at the concluding null */
780 corr2=match_string(corr,p,q-p);
781 if (corr2==NULL && match_mode==0) {free_descr(astr);return 0;}
782 if (corr2!=NULL && match_mode==1) {free_descr(astr);return 1;}
783 if (PURE_ANSWER && match_mode==0) corr=corr2;
784 if (*q=='O') p=q+2;
785 else if (*q=='A') p=q+3;
786 else assert(*q==0);
787 } while (*q!=0);
788 }
789 free_descr(astr);
790 if (match_mode==0) return 1; /* AND: Matched them all */
791 else return 0; /* OR: Didn't find a single match */
792 }
793
794
795 /* Does the answer in string ans match answer anum? */
796 /* Warning: this changes and then rfrees ans */
797 rbool match_answer(char *ans, int anum)
798 {
799 char *corr;
800 rbool ans_corr;
801
802 for(corr=ans;*corr!=0;corr++)
803 *corr=tolower(*corr);
804 if (answer!=NULL) {
805 /* corr=strstr(ans,answer[anum]); */
806 corr=match_string(ans,answer[anum],strlen(answer[anum]));
807 rfree(ans);
808 if (corr==NULL) return 0;
809 } else if (ans_ptr!=NULL) {
810 ans_corr=check_answer(ans,ans_ptr[anum].start,ans_ptr[anum].size);
811 rfree(ans);
812 return ans_corr;
813 }
814 else writeln("INT ERR: Invalid answer pointer.");
815 return 1;
816
817 }
818
819
820 rbool ask_question(int qnum)
821 /* 1=got it right, 0=got it wrong */
822 {
823 char *ans;
824
825 qnum--;
826
827 /* Now actually ask the question and compare the answers */
828 if (question!=NULL)
829 writeln(question[qnum]);
830 else if (quest_ptr!=NULL)
831 print_descr(quest_ptr[qnum],1);
832 else {writeln("INT ERR: Invalid question pointer");return 1;}
833 ans=agt_readline(2);
834 return match_answer(ans,qnum);
835 }
836
837
838 /* -------------------------------------------------------------------- */
839 /* Miscellaneous support routines */
840 /* -------------------------------------------------------------------- */
841
842 long read_number(void)
843 {
844 char *s, *err;
845 long n;
846
847 n=1;
848 do {
849 if (n!=1) gen_sysmsg(218,"Please enter a *number*. ",MSG_MAIN,NULL);
850 s=agt_readline(1);
851 n=strtol(s,&err,10);
852 if (err==s) err=NULL;
853 rfree(s);
854 } while (!quitflag && (err==NULL || n<=INT_MIN || n>=INT_MAX));
855 return n;
856 }
857
858
859
860
861 void runptr(int i, descr_ptr dp[], char *msg, int msgid,
862 parse_rec *nounrec, parse_rec *objrec)
863 /* Prints out description unless it doesn't exist, in which
864 case it prints out either system message #msgid or the message
865 contained in msg. */
866 {
867 if (dp[i].size>0) print_descr(dp[i],1);
868 else alt_sysmsg(msgid,msg,nounrec,objrec);
869 }
870
871
872
873 /* Score modes:
874 S:Score, R:Room +=list '(out of..'), -=don't list at all.
875 0-- S+ R+
876 1-- S+ R
877 2-- S R+
878 3-- S R
879 4-- S+ R-
880 5-- S R-
881 6-- S- R+
882 7-- S- R
883 8-- S- R- and disable SCORE.
884 */
885
886
887 void print_score(void)
888 {
889 char s[80];
890 int i,rmcnt, totroom;
891
892 if (score_mode<5) {
893 if (score_mode==0 || score_mode==1 || score_mode==4)
894 sprintf(s,"Your score is %ld (out of %ld possible).",tscore,max_score);
895 else sprintf(s,"Your score is %ld.",tscore);
896 writeln(s);
897 }
898
899 if (score_mode<4 || score_mode==6 || score_mode==7) {
900 rmcnt=0;
901 totroom=0;
902 for(i=0;i<=maxroom-first_room;i++)
903 if (!room[i].unused) {
904 if (room[i].seen) rmcnt++;
905 /* Should really compute this once at the beginning, but */
906 /* I don't want to add yet another global variable, particulary */
907 /* since this is only used here. */
908 totroom++;
909 }
910 if (score_mode%2==0)
911 sprintf(s,"You have visited %d locations (out of %d in the game)",rmcnt,
912 totroom);
913 else sprintf(s,"You have visited %d locations.",rmcnt);
914 writeln(s);
915 }
916 }
917
918
919 int normalize_time(int tnum) /* Convert hhmm so mm<60 */
920 {
921 int min,hr;
922
923 min=tnum % 100; /* The minutes */
924 hr=tnum / 100; /* The hours */
925 hr+=min/60; min=min%60;
926 while(hr<0) hr+=24;
927 hr=hr%24;
928 return hr*100+min;
929 }
930
931
932 void add_time(int dt)
933 {
934 int min,hr;
935
936 min=curr_time % 100; /* The minutes */
937 hr=curr_time / 100; /* The hours */
938 if (aver==AGT183) min+=dt; /* AGT 1.83 version */
939 else { /* Normal version */
940 min+=dt%100;
941 hr+=dt/100;}
942 while(min<0) {min=min+60;hr++;}
943 hr+=min/60; min=min%60;
944 while(hr<0) hr+=24;
945 hr=hr%24;
946 curr_time=hr*100+min;
947 }
948
949
950 void look_room(void)
951 {
952 compute_seen();
953 writeln("");
954 if (islit()) {
955 if (room[loc].name!=NULL && room[loc].name[0]!=0 &&
956 (!PURE_ROOMTITLE) )
957 {
958 agt_textcolor(-1); /* Emphasized text on */
959 writestr(room[loc].name);
960 agt_textcolor(-2);
961 writeln("");
962 } /* Emphasized text off */
963 if (room_firstdesc && room[loc].initdesc!=0)
964 msgout(room[loc].initdesc,1);
965 else if (room_ptr[loc].size>0)
966 print_descr(room_ptr[loc],1);
967 print_contents(loc+first_room,1);
968 if (listexit_flag)
969 v_listexit();
970 }
971 else
972 sysmsg( room[loc].light==1?6:7,
973 "It is dark. $You$ can't see anything.");
974 room_firstdesc=0;
975 do_look=0;
976 }
977
978
979 static void run_autoverb(void)
980 {
981 int v0; /* Will hold the verb number of the autoverb */
982 int savevb;
983 integer saveactor, savedobj, saveiobj;
984 parse_rec *save_actor_rec, *save_dobj_rec, *save_iobj_rec;
985 word saveprep;
986
987
988 beforecmd=1;
989
990 /* This is the penalty for vb, actor, etc being global variables. */
991 savevb=vb;
992 saveactor=actor; savedobj=dobj; saveprep=prep; saveiobj=iobj;
993 save_actor_rec=copy_parserec(actor_rec);
994 save_dobj_rec=copy_parserec(dobj_rec);
995 save_iobj_rec=copy_parserec(iobj_rec);
996
997 if (room[loc].autoverb!=0) {
998 v0=verb_code(room[loc].autoverb);
999 (void)scan_metacommand(0,v0,0,0,0,NULL);
1000 }
1001 free_all_parserec();
1002 vb=savevb;
1003 actor=saveactor; dobj=savedobj; iobj=saveiobj;
1004 actor_rec=save_actor_rec; dobj_rec=save_dobj_rec; iobj_rec=save_iobj_rec;
1005 prep=saveprep;
1006 }
1007
1008
1009
1010 /* ------------------------------------------------------------------- */
1011 /* MAIN COMMAND EXECUTION ROUTINES-- */
1012 /* These routines handle the execution of player commands */
1013 /* Then they change the status line, update counters, etc. */
1014 /* ------------------------------------------------------------------- */
1015
1016 static void creat_initdesc(void)
1017 {
1018 int i;
1019
1020 creatloop(i)
1021 if (creature[i].location==loc+first_room &&
1022 creature[i].initdesc!=0) {
1023 msgout(creature[i].initdesc,1);
1024 creature[i].initdesc=0;
1025 }
1026 }
1027
1028 /* Print out picture names, remember to put intro before first one. */
1029 /* This should be called with s==NULL before and after:
1030 before to reset it, after to put the trailing newline on. */
1031 void listpictname(char *s)
1032 {
1033 static rbool first_pict=1; /* True until we output first picture */
1034
1035 if (s==NULL) {
1036 if (!first_pict) writeln(""); /* Trailing newline */
1037 first_pict=1;
1038 return;
1039 }
1040 if (first_pict) {
1041 writeln(""); /* Skip a line */
1042 sysmsg(219," Illustrations:");
1043 first_pict=0;
1044 }
1045 writestr(" ");
1046 writestr(s);
1047 }
1048
1049
1050 void listpict(int obj)
1051 {
1052 char *s;
1053
1054 if (it_pict(obj)!=0 ) {
1055 s=objname(obj);
1056 listpictname(s);
1057 rfree(s);
1058 }
1059 }
1060
1061
1062 void list_viewable(void)
1063 /* List pictures that can be viewed, if any */
1064 {
1065 int i;
1066
1067 listpictname(NULL);
1068
1069 if (room[loc].pict!=0)
1070 listpictname("scene");
1071 contloop(i,1)
1072 listpict(i);
1073 contloop(i,1000)
1074 listpict(i);
1075 contloop(i,loc+first_room)
1076 listpict(i);
1077
1078 for(i=0;i<maxpix;i++)
1079 if (room[loc].PIX_bits & (1L<<i))
1080 listpictname(dict[pix_name[i]]);
1081 listpictname(NULL);
1082 }
1083
1084
1085
1086 void newroom(void)
1087 {
1088 rbool save_do_look;
1089 integer prevloc;
1090
1091 do {
1092 save_do_look=do_look;
1093 if (do_look==1) look_room();
1094 creat_initdesc();
1095 if (save_do_look==1 && aver>=AGTME10)
1096 list_viewable(); /* Print out picts that can be viewed here. */
1097 do_look=0;
1098
1099 prevloc=loc;
1100 if (do_autoverb) {
1101 do_autoverb=0;
1102 run_autoverb();
1103 }
1104
1105 if (!room[loc].seen) { /* This only runs on the first turn */
1106 room[loc].seen=1;
1107 tscore+=room[loc].points;
1108 }
1109 } while (prevloc!=loc); /* Autoverb could move player */
1110 }
1111
1112
1113 static int min_delta(void)
1114 {
1115 return (aver==AGT183) ? 1 : 0 ;
1116 }
1117
1118
1119 void increment_turn(void)
1120 {
1121 int i;
1122
1123 compute_seen();
1124
1125 newlife_flag=0;
1126
1127 if (quitflag) return;
1128
1129 newroom();
1130
1131 if (winflag || deadflag || endflag) return;
1132
1133 if (was_metaverb) return; /* No time should pass during a metaverb. */
1134
1135 turncnt++;
1136 /* Now increment the time counter */
1137 if (delta_time>0) {
1138 if (PURE_TIME)
1139 add_time(get_random(min_delta(),delta_time));
1140 else /* if !PURE_TIME */
1141 add_time(delta_time);
1142 }
1143
1144 for(i=0;i<=CNT_NUM;i++)
1145 if (agt_counter[i]>=0) ++agt_counter[i];
1146 creatloop(i)
1147 if (creature[i].location==loc+first_room && creature[i].hostile &&
1148 creature[i].timethresh>0)
1149 {
1150 parse_rec tmpcreat; /* Used for creature messages */
1151 make_parserec(i+first_creat,&tmpcreat);
1152 curr_creat_rec=&tmpcreat;
1153
1154 if (++creature[i].timecounter>=creature[i].timethresh)
1155 { /* Creature attacks */
1156 sysmsg(16,"$The_c$$c_name$ suddenly attacks $you_obj$!");
1157 sysmsg(creature[i].gender==0 ? 17 : 18,
1158 " $You$ try to defend $your$self, but $the_c$$c_name$ "
1159 "kills $you_obj$ anyhow.");
1160 deadflag=1;
1161 }
1162 else /* 'Angrier' messages */
1163 if (creature[i].timethresh>0 &&
1164 creature[i].timecounter>creature[i].timethresh-3)
1165 sysmsg(15,"$The_c$$c_name$ seems to be getting angrier.");
1166 }
1167 }
1168
1169
1170 /* Wrapper for increment_turn used by exec routines below.
1171 This just checks to make sure we're not one of the 1.8x versions
1172 (which call increment turn from elsewhere) */
1173 static void exec_increment_turn(void)
1174 {
1175 if (PURE_AFTER) increment_turn();
1176 }
1177
1178 static void end_turn(void)
1179 {
1180 if (textbold) agt_textcolor(-2);
1181 textbold=0;
1182 set_statline();
1183
1184 if (quitflag) return;
1185
1186 if (notify_flag && !was_metaverb) {
1187 if (old_score<tscore)
1188 sysmsg(227," [Your score just went up]");
1189 else if (old_score>tscore)
1190 sysmsg(228," [Your score just went down]");
1191 }
1192 old_score=tscore;
1193
1194 }
1195
1196
1197
1198 static void set_pronoun(int item)
1199 {
1200 if (item==0) return;
1201 switch (it_gender(item)) {
1202 case 0:
1203 if (it_plur(item))
1204 last_they=item;
1205 last_it=item; /* Change: last_it will be set even if the
1206 noun is plural */
1207 break;
1208 case 1: last_she=item;break;
1209 case 2: last_he=item;break;
1210 }
1211 }
1212
1213
1214 /* True if the current noun is the last one in the list. */
1215 static rbool lastnoun(parse_rec *list)
1216 {
1217 if (list->info==D_END) return 1;
1218 list++;
1219 while(list->info==D_AND) list++;
1220 return (list->info==D_END);
1221 }
1222
1223
1224
1225
1226 static void runverbs(parse_rec *actor0,int vnum,
1227 parse_rec *lnoun,word prep0,parse_rec *iobj0)
1228 /* The zeros are postpended simply to avoid a name conflict */
1229 {
1230 parse_rec *currnoun;
1231
1232 textbold=0;
1233 do_look=0;do_autoverb=0;
1234 was_metaverb=0;
1235 actor=actor0->obj;
1236 actor_rec=copy_parserec(actor0);
1237 vb=vnum;
1238 dobj=lnoun[0].obj;
1239 dobj_rec=copy_parserec(lnoun);
1240 prep=prep0;
1241 iobj=iobj0->obj;
1242 iobj_rec=copy_parserec(iobj0);
1243 set_pronoun(actor); /* Basically the last one that isn't 0 will stick */
1244 set_pronoun(iobj0->obj);
1245 was_metaverb=0; /* Most verbs are not metaverbs; assume this by default */
1246 start_of_turn=1; end_of_turn=0;
1247
1248 if (lnoun[0].info==D_END || lnoun[0].info==D_ALL) {
1249 end_of_turn=1;
1250 exec_verb();
1251 if (doing_restore) {free_all_parserec();return;}
1252 if (PURE_AND) exec_increment_turn();
1253 }
1254 else for(currnoun=lnoun;currnoun->info!=D_END;currnoun++)
1255 if (currnoun->info!=D_AND) {
1256 free_all_parserec();
1257 end_of_turn=lastnoun(currnoun);
1258 actor=actor0->obj;
1259 actor_rec=copy_parserec(actor0);
1260 vb=vnum;
1261 dobj=currnoun->obj;
1262 dobj_rec=copy_parserec(currnoun);
1263 iobj=iobj0->obj;
1264 iobj_rec=copy_parserec(iobj0);
1265 set_pronoun(dobj);
1266 exec_verb();
1267 if (doing_restore) return;
1268 if (PURE_AND)
1269 exec_increment_turn();
1270 else
1271 start_of_turn=0;
1272 if (quitflag || winflag || deadflag || endflag)
1273 break;
1274 }
1275 assert(end_of_turn);
1276 if (!PURE_AND) exec_increment_turn();
1277 end_turn();
1278 free_all_parserec();
1279 }
1280
1281
1282 /* The following store values for use by AGAIN */
1283 /* (so AGAIN can be implemented just by executing runverbs w/ the saved
1284 values) */
1285 static int save_vnum;
1286 static word save_prep;
1287 static parse_rec save_actor;
1288 static parse_rec save_obj;
1289 parse_rec *save_lnoun=NULL;
1290
1291
1292
1293 void exec(parse_rec *actor, int vnum,
1294 parse_rec *lnoun, word prep, parse_rec *iobj)
1295 {
1296
1297 cmd_saveable=0;
1298 pronoun_mode=!PURE_PROSUB;
1299
1300 if (vnum==verb_code(ext_code[wagain]) && lnoun[0].info==D_END
1301 && iobj->info==D_END &&
1302 (actor->info==D_END || actor->obj==save_actor.obj) )
1303 if (save_lnoun==NULL) {
1304 rfree(lnoun);
1305 sysmsg(186,
1306 "You can't use AGAIN until you've entered at least one command.");
1307 return;
1308 } else {
1309 memcpy(actor,&save_actor,sizeof(parse_rec));
1310 vnum=save_vnum;
1311 prep=save_prep;
1312 memcpy(iobj,&save_obj,sizeof(parse_rec));
1313 rfree(lnoun);
1314 lnoun=save_lnoun;save_lnoun=NULL;
1315 }
1316 else
1317 realverb=input[vp];
1318
1319
1320 runverbs(actor,vnum,lnoun,prep,iobj);
1321
1322 if (cmd_saveable) {
1323 if (save_lnoun!=NULL) rfree(save_lnoun);
1324
1325 memcpy(&save_actor,actor,sizeof(parse_rec));
1326 save_vnum=vnum;
1327 save_lnoun=lnoun; lnoun=NULL;
1328 save_prep=prep;
1329 memcpy(&save_obj,iobj,sizeof(parse_rec));
1330 }
1331 else
1332 rfree(lnoun);
1333 }