Debian Codegraph @

terps/hugo/source/herun.c

raw symbols SCIP
1 /*
2 HERUN.C
3
4 contains Run...() routines
5
6 for the Hugo Engine
7
8 Copyright (c) 1995-2009 by Kent Tessman
9 */
10
11 #include "heheader.h"
12
13 /* Function prototypes: */
14 int GetVal(void); /* from heexpr.c */
15
16 void hugo_stopmusic(void);
17 void hugo_stopsample(void);
18
19 #ifdef NO_STRFTIME
20 extern void hugo_gettimeformatted(char *a);
21 #endif
22
23 int passlocal[MAXLOCALS]; /* locals passed to routine */
24 int arguments_passed; /* when calling routine */
25 int ret = 0; char retflag = 0; /* return value and returning flag */
26
27 char during_player_input = false;
28 char override_full = 0;
29
30 char game_reset = false; /* for restore, undo, etc. */
31
32 struct CODE_BLOCK code_block[MAXSTACKDEPTH];
33 int stack_depth;
34 int tail_recursion = 0;
35 long tail_recursion_addr = 0;
36
37 /* Used by RunWindow() for setting current window dimensions: */
38 int last_window_top, last_window_bottom, last_window_left, last_window_right;
39 int lowest_windowbottom = 0, /* in text lines */
40 physical_lowest_windowbottom; /* in pixels or text lines */
41 char just_left_window = false;
42
43 /* from heparse.c, for RunEvents() */
44 extern int parse_location;
45
46 #ifdef PALMOS
47 int AutoResume(void);
48 #endif
49
50
51 /* RUNDO */
52
53 void RunDo(void)
54 {
55 long skip, enterptr;
56
57 enterptr = ++codeptr;
58 skip = PeekWord(codeptr); /* remember the skip distance */
59 codeptr+=2;
60
61 SetStackFrame(stack_depth, DOWHILE_BLOCK, skip+enterptr, codeptr);
62
63 #if defined (DEBUGGER)
64 dbnest++;
65 #endif
66 }
67
68
69 /* RUNEVENTS */
70
71 void RunEvents(void)
72 {
73 int i, tempundo, flag, temp_ret;
74 int eventin, tempself;
75 int templocals[MAXLOCALS];
76 int temp_stack_depth;
77 int temp_parse_location;
78 long tempptr, eventaddr;
79 #if defined (DEBUGGER)
80 int tempdbnest;
81 #endif
82
83 tempundo = undorecord;
84 undorecord = true;
85
86 tempptr = codeptr;
87 tempself = var[self];
88 temp_ret = ret;
89 temp_parse_location = parse_location;
90
91 parse_location = var[location]; /* for Available() */
92
93 temp_stack_depth = stack_depth;
94
95 for (i=0; i<MAXLOCALS; i++)
96 templocals[i] = var[MAXGLOBALS+i];
97
98 for (i=0; i<events; i++)
99 {
100 defseg = eventtable;
101
102 eventin = PeekWord(2 + i * 4);
103 eventaddr = (long)PeekWord(2 + i * 4 + 2)*address_scale;
104 var[self] = eventin;
105
106 domain = 0, flag = 0;
107
108 #if defined (DEBUGGER)
109 /* Prevent premature stopping */
110 if (debugger_step_over && !debugger_finish)
111 debugger_run = true;
112 #endif
113 if (eventin==0 || GrandParent(eventin)==GrandParent(var[player]))
114 flag = 1;
115
116 /* true is to signal a non-grammar call */
117 else if (Available(eventin, true))
118 flag = 1;
119
120 if (flag)
121 {
122 PassLocals(0);
123
124 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
125 #if defined (DEBUGGER)
126 tempdbnest = dbnest;
127 DebugRunRoutine(eventaddr);
128 dbnest = tempdbnest;
129 #else
130 RunRoutine(eventaddr);
131 #endif
132 stack_depth = temp_stack_depth;
133
134 retflag = 0;
135 if (var[endflag]) break;
136 }
137 }
138
139 for (i=0; i<MAXLOCALS; i++)
140 var[MAXGLOBALS+i] = templocals[i];
141
142 codeptr = tempptr;
143 parse_location = temp_parse_location;
144 var[self] = tempself;
145 undorecord = (char)tempundo;
146 ret = temp_ret;
147 }
148
149
150 /* RUNGAME */
151
152 extern char reparse_everything; /* from ParseError() in heparse.c */
153
154 #if defined (DEBUGGER)
155 extern int original_dictcount;
156 #endif
157
158 void RunGame(void)
159 {
160 char jw = 0; /* just wrote undo info */
161 char wasxverb = 0, newinput;
162 int i, flag, mc, lastspeaking = 0, startlocation;
163
164 #ifdef USE_TEXTBUFFER
165 TB_Init();
166 #endif
167
168 #ifdef PALMOS
169 if (AutoResume())
170 {
171 goto FreshInput;
172 }
173 #endif
174
175 /* Set up initial screen position */
176 hugo_settextpos(1, physical_windowheight/lineheight);
177 display_needs_repaint = false;
178 full = 0;
179
180 /* Load globals */
181 defseg = arraytable;
182 for (i=0; i<MAXGLOBALS; i++)
183 var[i] = PeekWord(i*2);
184
185 /* Reset the speaking-to variable */
186 speaking = 0;
187
188 if (game_version < 22)
189 {
190 passlocal[0] = objects;
191 #if defined (ACTUAL_LINELENGTH)
192 passlocal[1] = ACTUAL_LINELENGTH();
193 #else
194 passlocal[1] = physical_windowwidth/FIXEDCHARWIDTH;
195 #endif
196 }
197
198 #if defined (DEBUGGER)
199 RestartDebugger:
200
201 dictcount = original_dictcount; /* see hd.c */
202
203 /* If no gamefile is loaded, jump immediately to the debugger
204 interrupt function.
205 */
206 if (game==NULL) Debugger();
207 #endif
208
209 stack_depth = RESET_STACK_DEPTH;
210
211 InitGame();
212
213 undoptr = 0;
214 undoturn = 0;
215 undoinvalid = 1;
216 undorecord = 0;
217
218 Start:
219 stack_depth = 0;
220
221 strcpy(errbuf, "");
222 strcpy(oops, "");
223
224 do
225 {
226 if (xverb==0)
227 {
228 undorecord = true;
229
230 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
231
232 PassLocals(0);
233
234 #if defined (DEBUGGER)
235 currentroutine = mainaddr;
236 window[VIEW_CALLS].count = 0;
237
238 /* If there is no Main routine */
239 if (currentroutine==0) goto NormalTermination;
240
241 DebugRunRoutine((long)mainaddr*address_scale);
242 #else
243 RunRoutine((long)mainaddr*address_scale);
244 #endif
245 undorecord = false;
246 if (retflag)
247 break;
248 }
249
250 if (!undoinvalid && !wasxverb)
251 {
252 undorecord = true;
253 SaveUndo(0, undoturn, 0, 0, 0);
254 undorecord = false;
255 undoturn = 0;
256 }
257 else if (undoinvalid)
258 {
259 undoptr = 0;
260 undoinvalid = 0;
261 undoturn = 0;
262 undorecord = true;
263 SaveUndo(0, 0, 0, 0, 0);
264 SaveUndo(0, 0, 0, 0, 0);
265 undorecord = false;
266 }
267
268 xverb = true;
269
270 jw = 1;
271 if (var[endflag])
272 break;
273 jw = 0;
274
275 full_buffer = 0;
276
277 /* Endless game loop begins here */
278
279 do
280 {
281 PassLocals(0);
282
283 /* If there's nothing waiting to be finished in the
284 input buffer:
285 */
286 if (!remaining)
287 {
288 newinput = true;
289 speaking = 0;
290 do
291 {
292 FreshInput:
293 if (full_buffer != true)
294 {
295 newinput = true;
296 speaking = 0;
297 var[actor] = var[player];
298 #if defined (DEBUGGER)
299 AddStringtoCodeWindow("[Waiting for input]");
300 buffered_code_lines = FORCE_REDRAW;
301 debugger_has_stepped_back = false;
302 window[VIEW_LOCALS].changed = true;
303 #endif
304 if (!playback)
305 {
306 GetCommand();
307 }
308 else
309 {
310 if (!fgets(buffer, MAXBUFFER, playback))
311 {
312 if (fclose(playback))
313 FatalError(READ_E);
314 playback = NULL;
315 GetCommand();
316 }
317 else
318 {
319 /* Remove CR/LF */
320 /*
321 buffer[strlen(buffer)-1] = '\0';
322 if (buffer[strlen(buffer)-1]==0x0d)
323 buffer[strlen(buffer)-1] = '\0';
324 */
325 while (buffer[strlen(buffer)-1]==0x0d || buffer[strlen(buffer)-1]==0x0a)
326 buffer[strlen(buffer)-1] = '\0';
327 sprintf(line, "\n%s%s", GetWord(var[prompt]), buffer);
328 if (script)
329 /* fprintf() this way for Glk */
330 if (fprintf(script, "%s", "\n")<0)
331 FatalError(WRITE_E);
332 #if defined (SCROLLBACK_DEFINED)
333 hugo_sendtoscrollback("\n");
334 #endif
335 AP(line);
336 }
337 }
338 #if defined (DEBUGGER)
339 if (debugger_collapsing)
340 goto NormalTermination;
341 runaway_counter = 0;
342 #endif
343
344 SeparateWords();
345
346 if (record)
347 {
348 for (i=1; i<=words; i++)
349 {
350 if (!strcmp(word[i], "."))
351 {
352 /* fprintf() this way for Glk */
353 if (fprintf(record, "%s", "\n")<0)
354 FatalError(WRITE_E);
355 if (i==words) goto RecordedNewline;
356 }
357 else if (fputs(word[i], record)<0
358 || fprintf(record, "%s", " ")<0)
359 {
360 FatalError(WRITE_E);
361 }
362 }
363 if (fprintf(record, "%s", "\n")<0) FatalError(WRITE_E);
364 RecordedNewline:;
365 }
366 }
367 else full_buffer = false;
368
369 if (!strcmp(buffer, "") || buffer[0]=='.')
370 {
371 strcpy(parseerr, "");
372
373 /* "What?" */
374 ParseError(0, 0);
375 goto FreshInput;
376 }
377 }
378
379 /* Loop until valid input */
380 while (Parse()==false && strcmp(buffer, ""));
381 }
382
383
384 /* Else if there's something left in the input buffer */
385 else
386 {
387 newinput = false;
388
389 /* Erase the just-parsed command, and check to
390 to see if what's left is just blanks
391 */
392 while (words > remaining)
393 KillWord(1);
394 flag = false;
395 for (i=1; i<=words; i++)
396 if (wd[i]!=0) flag = true;
397 if (!flag)
398 goto FreshInput;
399
400 if (words) AP("");
401
402 if (Parse()==false)
403 {
404 mc = false;
405 goto Skipmc;
406 }
407 }
408
409 /* Run the user Parse routine if one exists */
410 CallLibraryParse();
411
412 reparse_everything = false;
413 do
414 {
415 mc = MatchCommand();
416 if (mc==false)
417 {
418 remaining = 0;
419 }
420 } while (reparse_everything && !mc);
421 Skipmc:;
422 }
423 while (!mc);
424
425 if (!xverb) undorecord = true;
426
427 wasxverb = xverb;
428
429 /* If there's an unknown string to be put in parse$ */
430 if (parsestr[0]!='\0')
431 {
432 if (parsestr[0]=='\"')
433 {
434 strcpy(parseerr, Right(parsestr, strlen(parsestr)-1));
435 if (parseerr[strlen(parseerr)-1]=='\"')
436 parseerr[strlen(parseerr)-1] = '\0';
437 }
438 }
439 else
440 strcpy(parseerr, "");
441
442 /* default actor */
443 var[actor] = var[player];
444
445 if (!newinput && lastspeaking) speaking = lastspeaking;
446
447 if (var[verbroutine]!=0 || (speaking))
448 {
449 /* If command addresses an object/char. */
450 if (speaking)
451 {
452 lastspeaking = speaking;
453 var[actor] = speaking;
454
455 /* If user Speakto routine exists */
456 if (speaktoaddr)
457 {
458 if (objcount) var[object] = objlist[0];
459 ret = 0;
460
461 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
462
463 passlocal[0] = speaking;
464 PassLocals(1);
465 #if defined (DEBUGGER)
466 DebugRunRoutine((long)speaktoaddr*address_scale);
467 #else
468 RunRoutine((long)speaktoaddr*address_scale);
469 #endif
470 if (ret==0)
471 {
472 remaining = 0;
473 xverb = true;
474 }
475 retflag = 0;
476 }
477
478 else
479 {
480 /* "...start with a verb..." */
481 ParseError(2, 0);
482 xverb = true;
483 }
484
485 /* reset actor */
486 var[actor] = var[player];
487 }
488
489 /* Regular old vanilla command: */
490 else
491 {
492 speaking = 0;
493 lastspeaking = 0;
494
495 /* As of v2.5, the Perform junction routine takes care of calling the
496 before routines, verbroutine, etc.
497 */
498 if (game_version>=25 && performaddr!=0)
499 {
500 i = 0;
501 NextPerform:
502 if (objcount) var[object] = objlist[i];
503
504 /* Have to do this before passing locals, in case
505 Name() ends up calling a routine (which would
506 trash passlocal[])
507 */
508 if (parseerr[0]=='\0' && parsestr[0]=='\0')
509 strcpy(parseerr, Name(objlist[i]));
510
511 /* Set up arguments for Perform */
512 passlocal[0] = var[verbroutine];
513 passlocal[1] = var[object];
514 passlocal[2] = var[xobject];
515
516 /* 'queue' argument, >1 if objcount > 1, or if
517 "all" has been used to refer to object(s) */
518 passlocal[3] = (objcount>1)?(i+1):(parse_allflag?1:0);
519 /* -1 if object is a digit */
520 if (object_is_number) passlocal[3] = (short)-1;
521
522 /* 'isxverb' argument */
523 if (game_version>=31 && xverb)
524 passlocal[4] = 1;
525
526 obj_match_state = -1;
527 startlocation = var[location];
528 ret = 0;
529
530 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
531
532 PassLocals(4);
533 #if defined (DEBUGGER)
534 DebugRunRoutine((long)performaddr*address_scale);
535 #else
536 RunRoutine((long)performaddr*address_scale);
537 #endif
538 if (ret==0)
539 {
540 remaining = 0;
541 xverb = true;
542 }
543 retflag = 0;
544
545 /* Break if endflag is set or if the location has
546 changed from the first call to Perform
547 */
548 if (var[endflag] || startlocation!=var[location])
549 goto EndofCommand;
550
551 if (objcount>1 && ++i<objcount)
552 goto NextPerform;
553 }
554
555 /* v2.4 or earlier had to call the verb loop via the engine
556 (as does v2.5 with no Perform routine */
557
558 /* One or more objects specified */
559 else if (objcount > 0) /* "if (objcount > 0" for pre-v2.5 */
560 {
561 obj_match_state = 1;
562 startlocation = var[location];
563 for (i=0; i<objcount; i++)
564 {
565 if (parseerr[0]=='\0' && parsestr[0]=='\0')
566 strcpy(parseerr, Name(objlist[i]));
567
568 if (ValidObj(objlist[i]) &&
569 ((objcount>1 && objlist[i]!=var[xobject]) || objcount==1))
570 {
571 var[object] = objlist[i];
572 if (GetProp(var[player], before, 1, 0)==0)
573 if (GetProp(var[location], before, 1, 0)==0)
574 if (GetProp(var[xobject], before, 1, 0)==0)
575 {
576 /* If multiple objects are specified, print
577 "name: " for each:
578 */
579 if (objcount > 1)
580 {
581 sprintf(line, "%s: \\;", Name(var[object]));
582 AP(line);
583 }
584
585 obj_match_state = 0;
586
587 if ((object_is_number) || GetProp(var[object], before, 1, 0)==0)
588 {
589 obj_match_state = -1;
590 ret = 0;
591
592 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
593
594 PassLocals(0);
595 #if defined (DEBUGGER)
596 DebugRunRoutine((long)var[verbroutine]*address_scale);
597 #else
598 RunRoutine((long)var[verbroutine]*address_scale);
599 #endif
600 if (ret==0)
601 {
602 remaining = 0;
603 xverb = true;
604 }
605 retflag = 0;
606
607 GetProp(var[player], after, 1, 0);
608 GetProp(var[location], after, 1, 0);
609 }
610 }
611 }
612 if (var[endflag] || var[location]!=startlocation)
613 break;
614 }
615 }
616
617 /* No object(s) specified */
618 else
619 {
620 if (GetProp(var[player], before, 1, 0)==0)
621 {
622 if (GetProp(var[location], before, 1, 0)==0)
623 {
624 ret = 0;
625
626 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
627
628 PassLocals(0);
629 #if defined (DEBUGGER)
630 DebugRunRoutine((long)var[verbroutine]*address_scale);
631 #else
632 RunRoutine((long)var[verbroutine]*address_scale);
633 #endif
634 if (ret==0)
635 {
636 remaining = 0;
637 xverb = true;
638 }
639 retflag = 0;
640
641 GetProp(var[player], after, 1, 0);
642 GetProp(var[location], after, 1, 0);
643 }
644 }
645 }
646
647 /* (end of pre-v2.5 verbroutine-calling) */
648
649 }
650 }
651 EndofCommand:
652 if (var[endflag])
653 break;
654
655 undorecord = false;
656 }
657 while (true); /* endless loop back to start */
658
659 undorecord = false;
660
661 if (var[endflag]==-1)
662 #if defined (DEBUGGER)
663 goto NormalTermination;
664 #else
665 return;
666 #endif
667
668 if (!jw)
669 {
670 undorecord = true;
671 SaveUndo(0, undoturn, 0, 0, 0);
672 undorecord = false;
673 undoturn = 0;
674 }
675
676 if (playback)
677 {
678 if (fclose(playback)) FatalError(READ_E);
679 playback = NULL;
680 }
681
682 Flushpbuffer();
683
684
685 /* Run the user Endgame routine if one exists */
686
687 #if defined (DEBUGGER)
688 if (endgameaddr && !debugger_collapsing)
689 #else
690 if (endgameaddr)
691 #endif
692 {
693 passlocal[0] = var[endflag];
694
695 SetStackFrame(RESET_STACK_DEPTH, RUNROUTINE_BLOCK, 0, 0);
696
697 ret = 0;
698 var[endflag] = 0;
699 PassLocals(0);
700 #if defined (DEBUGGER)
701 DebugRunRoutine((long)endgameaddr*address_scale);
702 #else
703 RunRoutine((long)endgameaddr*address_scale);
704 #endif
705 retflag = false;
706 }
707 else
708 ret = 0;
709
710 xverb = true;
711 wasxverb = true;
712 if (ret) goto Start;
713
714 /* Stop all audio after running EndGame */
715 hugo_stopmusic();
716 hugo_stopsample();
717
718 /* The debugger will reset endflag anyway, but we need it to signal ports
719 (like Palm) that the game loop has exited.
720 */
721 var[endflag] = -1;
722
723 #if defined (DEBUGGER)
724
725 NormalTermination:
726
727 /* Normal program termination doesn't exit the debugger. */
728
729 debugger_interrupt = true;
730 debugger_run = false;
731 var[endflag] = false;
732
733 xverb = false;
734
735 SwitchtoDebugger();
736 UpdateDebugScreen();
737
738 if (debugger_collapsing!=2)
739 {
740 DebugMessageBox("Program Exiting", "Normal program termination");
741
742 }
743
744 debugger_collapsing = false;
745
746 if ((game!=NULL) && !RunRestart())
747 DebugMessageBox("Restart Error", "Unable to restart");
748
749 SwitchtoGame();
750
751 history_count = 0;
752 window[VIEW_CALLS].count = 0;
753
754 for (i=0; i<(int)window[CODE_WINDOW].count; i++)
755 free(codeline[i]);
756 window[CODE_WINDOW].count = 0;
757
758 /* Force Code window redraw */
759 buffered_code_lines = FORCE_REDRAW;
760
761 goto RestartDebugger;
762 #endif
763 }
764
765
766 /* RUNIF */
767
768 void RunIf(char override)
769 {
770 char t, tempinexpr;
771 long enterptr, skip;
772
773 switch (t = MEM(codeptr))
774 {
775 case CASE_T:
776 case IF_T:
777 case ELSEIF_T:
778 case WHILE_T:
779 case FOR_T:
780 {
781 codeptr++;
782 enterptr = codeptr;
783
784 /* Remember the skip distance */
785 skip = PeekWord(codeptr);
786 codeptr += 2;
787
788 /* Check if we've already done an elseif */
789 if (override && t==ELSEIF_T)
790 {
791 codeptr = skip+enterptr;
792 return;
793 }
794
795 /* Read the expression */
796 tempinexpr = inexpr;
797 inexpr = 1;
798 SetupExpr();
799 inexpr = tempinexpr;
800
801 /* If the expression is false, skip the
802 conditional block
803 */
804 if (EvalExpr(0)==0)
805 {
806 codeptr = skip+enterptr;
807 return;
808 }
809
810 /* Protect the stack if jumping backward */
811 if (MEM(codeptr)==JUMP_T)
812 {
813 if ((long)(PeekWord(codeptr+1)*address_scale) < codeptr)
814 if (--stack_depth < 0) stack_depth = 0;
815 }
816
817 /* Continue on into the conditional block if
818 the expression evaluated to non-zero
819 */
820 PasstoBlock:
821 if (t==WHILE_T || t==FOR_T)
822 SetStackFrame(stack_depth, CONDITIONAL_BLOCK, skip+enterptr, 0);
823
824 else /* no 'break' parameter */
825 SetStackFrame(stack_depth, CONDITIONAL_BLOCK, 0, 0);
826 #if defined (DEBUGGER)
827 dbnest++;
828 #endif
829 return;
830 }
831 case ELSE_T:
832 {
833 skip = PeekWord(++codeptr);
834 enterptr = codeptr;
835 codeptr += 2;
836
837 if (override)
838 {
839 codeptr = skip+enterptr;
840 return;
841 }
842
843 if (MEM(codeptr)==JUMP_T)
844 {
845 if ((long)(PeekWord(codeptr+1)*address_scale) < codeptr)
846 if (--stack_depth < 0) stack_depth = 0;
847 }
848
849 goto PasstoBlock;
850 }
851 }
852 }
853
854
855 /* RUNINPUT */
856
857 void RunInput(void)
858 {
859 int i;
860
861 strcpy(parseerr, "");
862
863 Flushpbuffer();
864
865 if (icolor==-1) icolor = fcolor; /* check unset input color */
866
867 hugo_getline("");
868
869 #if defined (DEBUGGER)
870 if (debugger_collapsing) return;
871 #endif
872
873 strcpy(buffer, Rtrim(strlwr(buffer)));
874
875 SeparateWords();
876
877 for (i=1; i<=words; i++)
878 {
879 wd[i] = FindWord(word[i]);
880
881 /* If a word isn't in the dictionary */
882 if (wd[i]==UNKNOWN_WORD)
883 {
884 wd[i] = 0;
885 strcpy(parseerr, word[i]);
886 if (parseerr[0]=='\"')
887 {
888 strcpy(parseerr, Right(parseerr, strlen(parseerr)-1));
889 if (parseerr[strlen(parseerr)-1]=='\"')
890 parseerr[strlen(parseerr)-1] = '\0';
891 }
892 }
893 }
894 currentpos = 0; /* left margin */
895 remaining = 0;
896 }
897
898
899 /* RUNMOVE */
900
901 /* (All the debugger range-checking is important because invalid memory
902 writes due to invalid object location calculations are a good way to
903 crash the system.)
904 */
905
906 void RunMove(void)
907 {
908 int obj, p;
909 #if defined (DEBUGGER)
910 char out_of_range = 0;
911 #endif
912
913 switch (MEM(codeptr))
914 {
915 case MOVE_T:
916 {
917 codeptr++;
918 obj = GetValue();
919
920 #if defined (DEBUGGER)
921 if (!CheckinRange(obj, objects, "object"))
922 out_of_range = true;
923 else
924 #endif
925 SaveUndo(MOVE_T, obj, Parent(obj), 0, 0);
926
927 codeptr++; /* skip "to" */
928 p = GetValue();
929
930 #if defined (DEBUGGER)
931 if (!CheckinRange(p, objects, "object"))
932 out_of_range = true;
933
934 if (!out_of_range)
935 #endif
936 MoveObj(obj, p);
937 break;
938 }
939
940 case REMOVE_T:
941 {
942 codeptr++;
943 obj = GetValue();
944
945 #if defined (DEBUGGER)
946 if (!CheckinRange(obj, objects, "object"))
947 out_of_range = true;
948
949 else
950 #endif
951 SaveUndo(MOVE_T, obj, Parent(obj), 0, 0);
952
953 #if defined (DEBUGGER)
954 if (!out_of_range)
955 #endif
956 MoveObj(obj, 0); /* move to parent 0 */
957 break;
958 }
959 }
960
961 if (game_version>=23) codeptr++; /* eol */
962 }
963
964
965 /* RUNPRINT */
966
967 void RunPrint(void)
968 {
969 char number = 0, hexnumber = 0;
970 int a;
971 int i, l;
972
973 codeptr++;
974
975 while (MEM(codeptr) != EOL_T)
976 {
977 strcpy(line, "");
978
979 switch (MEM(codeptr))
980 {
981 case NEWLINE_T:
982 {
983 codeptr++;
984 if (currentpos+hugo_textwidth(pbuffer)!=0)
985 AP("");
986 if (MEM(codeptr)==SEMICOLON_T) codeptr++;
987 continue;
988 }
989
990 case TO_T:
991 {
992 codeptr++;
993
994 #if !defined (ACTUAL_LINELENGTH)
995 if ((a = GetValue()) > physical_windowwidth/FIXEDCHARWIDTH)
996 a = physical_windowwidth/FIXEDCHARWIDTH;
997 #else
998 if ((a = GetValue()) > ACTUAL_LINELENGTH())
999 {
1000 double ratio;
1001
1002 ratio = (physical_windowwidth/FIXEDCHARWIDTH) / a;
1003 a = (int)(ACTUAL_LINELENGTH() / ratio);
1004 }
1005 #endif
1006 strcpy(line, "");
1007 l = 0;
1008 if (a*FIXEDCHARWIDTH >
1009 hugo_textwidth(pbuffer)+currentpos-hugo_charwidth(' '))
1010 {
1011 for (i=hugo_textwidth(pbuffer)+currentpos;
1012 #ifdef NO_TERMINAL_LINEFEED
1013 i<a*FIXEDCHARWIDTH;
1014 #else
1015 i<a*FIXEDCHARWIDTH && i<physical_windowright;
1016 #endif
1017 i+=hugo_charwidth(' '))
1018 {
1019 line[l++] = FORCED_SPACE;
1020 line[l] = '\0';
1021 }
1022
1023 }
1024 break;
1025 }
1026
1027 case CAPITAL_T:
1028 {
1029 codeptr++;
1030 capital = 1;
1031 continue;
1032 }
1033
1034 case NUMBER_T:
1035 {
1036 codeptr++;
1037 number = 1;
1038 continue;
1039 }
1040
1041 case HEX_T:
1042 {
1043 codeptr++;
1044 number = 1;
1045 hexnumber = 1;
1046 continue;
1047 }
1048
1049 case STRINGDATA_T:
1050 {
1051 codeptr++;
1052 if (game_version >= 24)
1053 l = PeekWord(codeptr++);
1054 else
1055 l = Peek(codeptr);
1056 for (i=0; i<l; i++)
1057 line[i] = (char)(MEM(++codeptr) - CHAR_TRANSLATION);
1058 line[i] = '\0';
1059 codeptr++;
1060 break;
1061 }
1062
1063 /* Anything else is treated as a value */
1064 default:
1065 {
1066 a = GetValue();
1067 if (!number)
1068 {
1069 strcpy(line, GetWord(a));
1070 }
1071 else
1072 {
1073 if (!hexnumber)
1074 {
1075 if (capital)
1076 itoa((unsigned int)a, line, 10);
1077 else
1078 itoa(a, line, 10);
1079 capital = 0;
1080 }
1081 else
1082 sprintf(line, "%X", a);
1083
1084 number = 0;
1085 hexnumber = 0;
1086 }
1087 break;
1088 }
1089 }
1090
1091 if (MEM(codeptr)==SEMICOLON_T)
1092 {
1093 codeptr++;
1094 strcat(line, "\\;");
1095 }
1096 if (capital)
1097 {
1098 capital = 0;
1099 if ((unsigned)line[0]<128)
1100 line[0] = (char)toupper((int)line[0]);
1101 else
1102 {
1103 /* Special conversion for non-Latin1
1104 (>127) lowercase characters
1105 */
1106 char diff;
1107 diff = 'a'-'A';
1108 if ((unsigned)line[0]+diff<=255 && (unsigned)line[0]-diff>127)
1109 line[0] -= diff;
1110 }
1111 }
1112
1113 AP(line);
1114 }
1115
1116 codeptr++;
1117 }
1118
1119
1120 /* RUNRESTART */
1121
1122 int RunRestart()
1123 {
1124 unsigned int a;
1125 long i = 0;
1126 HUGO_FILE file;
1127
1128 #ifndef LOADGAMEDATA_REPLACED
1129
1130 remaining = 0;
1131
1132 #if !defined (GLK) /* with Glk, game is never closed */
1133 /* Use file instead of game, just in case the call fails */
1134 if (!(file = HUGO_FOPEN(gamefile, "rb"))) goto RestartError;
1135 #else
1136 file = game;
1137 #endif
1138
1139 if (fseek(file, (objtable-gameseg)*16, SEEK_SET)) goto RestartError;
1140
1141 i = (objtable-gameseg)*16L;
1142 do
1143 {
1144 int val;
1145
1146 val = fgetc(file);
1147 SETMEM(i++, (unsigned char)val);
1148 if (val==EOF || ferror(file)) goto RestartError;
1149 }
1150 while (i < codeend);
1151
1152 #if !defined (GLK)
1153 if (fclose(file)) FatalError(READ_E);
1154 #endif
1155
1156 #else
1157 if (!(file = HUGO_FOPEN(gamefile, "rb"))) goto RestartError;
1158 LoadGameData(true);
1159 fclose(file);
1160 #endif /* LOADGAMEDATA_REPLACED */
1161
1162 defseg = arraytable;
1163 for (a=0; a<MAXGLOBALS; a++)
1164 var[a] = PeekWord(a*2);
1165
1166 i = codeptr;
1167
1168 if (game_version < 22)
1169 {
1170 passlocal[0] = objects;
1171 #if defined (ACTUAL_LINELENGTH)
1172 passlocal[1] = ACTUAL_LINELENGTH();
1173 #else
1174 passlocal[1] = physical_windowwidth/FIXEDCHARWIDTH;
1175 #endif
1176 }
1177
1178 #if defined (DEBUGGER)
1179 /* A restart can happen mid-playback from the debugger */
1180 if (playback)
1181 {
1182 if (fclose(playback))
1183 FatalError(READ_E);
1184 playback = NULL;
1185 }
1186
1187 if (active_screen!=DEBUGGER)
1188 #endif
1189 {
1190 InitGame();
1191
1192 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
1193
1194 PassLocals(0);
1195 #if defined (DEBUGGER)
1196 DebugRunRoutine((long)mainaddr*address_scale);
1197 #else
1198 RunRoutine((long)mainaddr*address_scale);
1199 #endif
1200 retflag = 0;
1201
1202 codeptr = i;
1203
1204 undoptr = 0;
1205 undoturn = 0;
1206 undoinvalid = 1;
1207 }
1208
1209 return 1;
1210
1211 RestartError:
1212 #if !defined (GLK)
1213 if (fclose(file)) FatalError(READ_E);
1214 #endif
1215
1216 return 0;
1217 }
1218
1219
1220 /* RUNRESTORE */
1221
1222 /* Note that under Glk, stdio f...() functions map to Glk functions */
1223
1224 #ifndef RESTOREGAMEDATA_REPLACED
1225
1226 int RestoreGameData(void)
1227 {
1228 char testid[3], testserial[9];
1229 int lbyte, hbyte;
1230 int j;
1231 unsigned int k, undosize;
1232 long i;
1233
1234 /* Check ID */
1235 testid[0] = (char)fgetc(save);
1236 testid[1] = (char)fgetc(save);
1237 testid[2] = '\0';
1238 if (ferror(save)) goto RestoreError;
1239
1240 if (strcmp(testid, id))
1241 {
1242 AP("Incorrect save file.");
1243 if (fclose(save)) FatalError(READ_E);
1244 save = NULL;
1245 return 0;
1246 }
1247
1248 /* Check serial number */
1249 if (!fgets(testserial, 9, save)) goto RestoreError;
1250 if (strcmp(testserial, serial))
1251 {
1252 AP("Save file created by different version.");
1253 if (fclose(save)) FatalError(READ_E);
1254 save = NULL;
1255 return 0;
1256 }
1257
1258 /* Restore variables */
1259 for (k=0; k<MAXGLOBALS+MAXLOCALS; k++)
1260 {
1261 if ((lbyte = fgetc(save))==EOF || (hbyte = fgetc(save))==EOF)
1262 goto RestoreError;
1263 var[k] = lbyte + hbyte * 256;
1264 }
1265
1266 /* Restore objtable and above */
1267
1268 if (fseek(game, objtable*16L, SEEK_SET)) goto RestoreError;
1269 i = 0;
1270
1271 while (i<codeend-(long)(objtable*16L))
1272 {
1273 if ((hbyte = fgetc(save))==EOF && ferror(save)) goto RestoreError;
1274
1275 if (hbyte==0)
1276 {
1277 if ((lbyte = fgetc(save))==EOF && ferror(save)) goto RestoreError;
1278 SETMEM(objtable*16L+i, (unsigned char)lbyte);
1279 i++;
1280
1281 /* Skip byte in game file */
1282 if (fgetc(game)==EOF) goto RestoreError;
1283 }
1284 else
1285 {
1286 while (hbyte--)
1287 {
1288 /* Get unchanged game file byte */
1289 if ((lbyte = fgetc(game))==EOF) goto RestoreError;
1290 SETMEM(objtable*16L+i, (unsigned char)lbyte);
1291 i++;
1292 }
1293 }
1294 }
1295
1296 /* Restore undo data */
1297 if ((lbyte = fgetc(save))==EOF || (hbyte = fgetc(save))==EOF)
1298 goto RestoreError;
1299 undosize = lbyte + hbyte*256;
1300
1301 /* We can only restore undo data if it was saved by a port with
1302 the same MAXUNDO as us */
1303 if (undosize==MAXUNDO)
1304 {
1305 for (k=0; k<MAXUNDO; k++)
1306 {
1307 for (j=0; j<5; j++)
1308 {
1309 if ((lbyte = fgetc(save))==EOF || (hbyte = fgetc(save))==EOF)
1310 goto RestoreError;
1311 undostack[k][j] = lbyte + hbyte*256;
1312 }
1313 }
1314 if ((lbyte = fgetc(save))==EOF || (hbyte = fgetc(save))==EOF) goto RestoreError;
1315 undoptr = lbyte + hbyte*256;
1316 if ((lbyte = fgetc(save))==EOF || (hbyte = fgetc(save))==EOF) goto RestoreError;
1317 undoturn = lbyte + hbyte*256;
1318 if ((lbyte = fgetc(save))==EOF || (hbyte = fgetc(save))==EOF) goto RestoreError;
1319 undoinvalid = (unsigned char)lbyte, undorecord = (unsigned char)hbyte;
1320 }
1321 else undoinvalid = true;
1322
1323 return true;
1324
1325 RestoreError:
1326 return false;
1327 }
1328
1329 #endif // RESTOREGAMEDATA_REPLACED
1330
1331 int RunRestore()
1332 {
1333 #if !defined (GLK)
1334 save = NULL;
1335
1336 /* stdio implementation */
1337 hugo_getfilename("to restore", savefile);
1338
1339 #if defined (DEBUGGER)
1340 if (debugger_collapsing) return 1;
1341 #endif
1342 if (!strcmp(line, "")) return 0;
1343 if (!(save = HUGO_FOPEN(line, "r+b"))) return 0;
1344
1345 #else
1346 /* Glk implementation */
1347 frefid_t savefile;
1348
1349 save = NULL;
1350
1351 savefile = glk_fileref_create_by_prompt(fileusage_SavedGame | fileusage_BinaryMode,
1352 filemode_Read, 0);
1353 if (!savefile) return 0;
1354 if (glk_fileref_does_file_exist(savefile))
1355 {
1356 save = glk_stream_open_file(savefile, filemode_Read, 0);
1357 }
1358 glk_fileref_destroy(savefile);
1359 if (!save) return 0;
1360
1361 #endif /* GLK */
1362
1363 if (!RestoreGameData()) goto RestoreError;
1364
1365 if (fclose(save)) FatalError(READ_E);
1366 save = NULL;
1367
1368 #if !defined (GLK)
1369 strcpy(savefile, line);
1370 #endif
1371
1372 game_reset = true;
1373
1374 return (1);
1375
1376 RestoreError:
1377 if ((save) && fclose(save)) FatalError(READ_E);
1378 save = NULL;
1379 game_reset = false;
1380 return 0;
1381 }
1382
1383
1384 /* RUNROUTINE
1385
1386 This is the main loop for running each line of code in sequence;
1387 the main switch statement is based on the first token in each line.
1388
1389 This routine is relatively complex, especially given the addition
1390 of debugger control. Basically it is structured like this:
1391
1392 1. If this is the debugger build, see what debugger
1393 information has to be set up upon calling this block
1394 of code
1395
1396 2. Get the next token, and as long as it isn't
1397 CLOSE_BRACE_T ('}')...
1398
1399 3. ...If this is the debugger build, see if there is a
1400 standing debugger_interrupt to pass control back to
1401 the debugger, and perform all operations for stepping
1402 tracing, breakpoints, etc.
1403
1404 4. ...See what token we're dealing with and execute
1405 accordingly
1406
1407 5. ...Loop back to (2)
1408
1409 6. If this is the debugger build, do whatever is
1410 necessary to tidy up after finishing this block of
1411 code
1412
1413 There's a bit of a trick involved since the original language
1414 design uses "{...}" structures for both conditionals and blocks
1415 that necessitate another (i.e., nested) call to RunRoutine().
1416 The call_block structure array and stack_depth variable are
1417 the navigation guides.
1418 */
1419
1420 void RunRoutine(long addr)
1421 {
1422 int null_count; /* for reading to next address boundary */
1423 char tempinexpr;
1424 int i, t, len, xpos, ypos;
1425 int initial_stack_depth, tempret;
1426 unsigned int routineptr = 0;
1427 long textaddr;
1428 #if defined (DEBUGGER)
1429 char wascalled = 0; /* distinguish routine calls from 'window'
1430 blocks, etc. */
1431 char *called_from;
1432 char trace_comp_prop = 0;
1433 unsigned char param_type;
1434 long param_start;
1435 unsigned int old_currentroutine = 0;
1436
1437 /* Because <debugdata#> and <label#> in-code data is
1438 not decompiled
1439 */
1440 int broke_on_nonstatement = 0;
1441 #endif
1442
1443 /* If routine doesn't exist */
1444 if (addr==0L) return;
1445
1446 initial_stack_depth = stack_depth;
1447 inexpr = 0;
1448
1449 #if !defined (DEBUGGER)
1450 #if defined (DEBUG_CODE)
1451 /*
1452 if (trce)
1453 {
1454 if (codeptr != addr)
1455 {
1456 sprintf(line, "[ROUTINE: $%6s]", PrintHex(addr));
1457 AP(line);
1458 wascalled = 1;
1459 }
1460 }
1461 */
1462 #endif
1463 #endif
1464
1465 #if defined (DEBUGGER)
1466
1467 /*
1468 * First see what debugger information has to be set up upon calling
1469 * this block of code
1470 */
1471
1472 /* If this is a routine call vs. other block, codeptr will be
1473 different from addr.
1474 */
1475 if (codeptr != addr)
1476 {
1477 wascalled = 1;
1478
1479 /* Checking to see if currentroutine is 0 is a way of seeing
1480 if the debugger has started properly. If, for example, a
1481 property routine runs while LoadGame() is searching for
1482 the display object, it may corrupt the uninitialized
1483 debugger arrays.
1484 */
1485 if ((old_currentroutine = currentroutine)==0) return;
1486
1487 currentroutine = (unsigned int)(addr/address_scale);
1488
1489 if (debugger_step_over)
1490 {
1491 step_nest++;
1492 debugger_interrupt = false;
1493 }
1494 else
1495 {
1496 /* Add a blank line if one hasn't been added
1497 already:
1498 */
1499 if ((window[CODE_WINDOW].count) && (codeline[window[CODE_WINDOW].count-1][0]&0x0FF)!='\0')
1500 AddStringtoCodeWindow("");
1501
1502 /* If this is a property routine, the debug_line array
1503 already holds the calling information
1504 */
1505 if (!trace_complex_prop_routine)
1506 sprintf(debug_line, "Calling: %s", RoutineName(currentroutine));
1507 else
1508 trace_comp_prop = true;
1509 trace_complex_prop_routine = false;
1510
1511 call[window[VIEW_CALLS].count].addr = currentroutine;
1512 call[window[VIEW_CALLS].count++].param = (char)arguments_passed;
1513 window[VIEW_CALLS].changed = true;
1514
1515 /* Revise call history */
1516 if (window[VIEW_CALLS].count==MAXCALLS)
1517 {
1518 for (i=0; i<MAXCALLS-1; i++)
1519 {
1520 call[i].addr = call[i+1].addr;
1521 call[i].param = call[i+1].param;
1522 }
1523 call[0].addr = 0;
1524 }
1525
1526 /* If not object.property or an event */
1527 if (strchr(debug_line, '.')==NULL && strstr(debug_line, "vent ")==NULL)
1528 {
1529 strcat(debug_line, "(");
1530 for (i=0; i<arguments_passed; i++)
1531 {
1532 sprintf(debug_line+strlen(debug_line), "%d", var[MAXGLOBALS+i]);
1533 if (i<arguments_passed-1)
1534 strcat(debug_line, ", ");
1535 }
1536 strcat(debug_line, ")");
1537 }
1538 AddStringtoCodeWindow(debug_line);
1539 }
1540 }
1541
1542 #endif /* defined (DEBUGGER) */
1543
1544 defseg = gameseg;
1545 codeptr = addr;
1546
1547 /*
1548 * Get the next token, so long as it isn't a CLOSE_BRACE_T ('}')
1549 * marking the end of this block of code
1550 */
1551
1552 ContinueRunning:
1553
1554 while (MEM(codeptr) != CLOSE_BRACE_T) /* until "}" */
1555 {
1556
1557 #if defined (DEBUGGER)
1558 /* Check if we're stepping over, and if we've returned to
1559 the original level of nesting:
1560 */
1561 if (debugger_step_over && step_nest==0)
1562 {
1563 debugger_step_over = false;
1564 debugger_interrupt = true;
1565 }
1566 #endif
1567 if (var[endflag]) return;
1568
1569 null_count = 0;
1570
1571 /* Read the next token */
1572 while ((t = MEM(codeptr))==0)
1573 {
1574 codeptr++;
1575
1576 /* Allow for padding zeroes. If address_scale
1577 zeroes are processed, we can't simply be
1578 eating up the null space before an address
1579 boundary.
1580 */
1581 if (++null_count > address_scale)
1582 FatalError(UNKNOWN_OP_E);
1583 }
1584
1585 #if !defined (DEBUGGER)
1586 #if defined (DEBUG_CODE)
1587 if (!inwindow)
1588 {
1589 sprintf(line, "[%6s: %s]", PrintHex(codeptr), token[t]);
1590 AP(line);
1591 }
1592 #endif
1593 #endif
1594 if (game_version < 22) if (t==TEXT_T) t = TEXTDATA_T;
1595
1596 #if defined (DEBUGGER)
1597 if (++runaway_counter>=65535 && runtime_warnings)
1598 {
1599 sprintf(debug_line, "Possible runaway loop (65535 unchecked steps)");
1600 RuntimeWarning(debug_line);
1601 buffered_code_lines = FORCE_REDRAW;
1602 runaway_counter = 0;
1603 }
1604
1605 if (t!=DEBUGDATA_T && t!=LABEL_T && !debugger_step_over)
1606 AddLinetoCodeWindow(codeptr);
1607
1608 if ((i = IsBreakpoint(codeptr)))
1609 {
1610 if (t==DEBUGDATA_T || t==LABEL_T)
1611 broke_on_nonstatement = i;
1612
1613 debugger_interrupt = true;
1614
1615 /* '<' for "<Unknown>" */
1616 if (breakpoint[--i].in[0]=='<')
1617 {
1618 breakpoint[i].in = RoutineName(currentroutine);
1619 window[VIEW_BREAKPOINTS].changed = true;
1620 }
1621 }
1622
1623 /* Don't add in-code data to the code window */
1624 if (t==DEBUGDATA_T || t==LABEL_T) goto ProcessToken;
1625
1626 /* Evaluate (only) any watch expressions set to break
1627 when true:
1628 */
1629 for (i=0; i<(int)window[VIEW_WATCH].count; i++)
1630 {
1631 if (watch[i].isbreak)
1632 {
1633 SetupWatchEval(i);
1634 if (EvalWatch())
1635 {
1636 debugger_interrupt = true;
1637 break;
1638 }
1639 }
1640 }
1641
1642 /* Always update the watch window */
1643 window[VIEW_WATCH].changed = true;
1644
1645 /*
1646 * Immediately following is the main Debugger interrupt point.
1647 * No matter where the engine is while running, if debugger_interrupt
1648 * is ever set during the execution of a command, execution is
1649 * paused and control passed to the Debugger.
1650 */
1651
1652 if (debugger_interrupt)
1653 {
1654 if (broke_on_nonstatement)
1655 {
1656 broke_on_nonstatement--;
1657 breakpoint[broke_on_nonstatement].addr = codeptr;
1658 window[VIEW_BREAKPOINTS].changed = true;
1659 broke_on_nonstatement = false;
1660 }
1661
1662 if (debugger_step_over)
1663 {
1664 AddStringtoCodeWindow("...");
1665 if (t!=DEBUGDATA_T && t!=LABEL_T)
1666 AddLinetoCodeWindow(codeptr);
1667 }
1668 Debugger();
1669 }
1670
1671
1672 /* Now, additional processing for flags that may have been
1673 set while execution was suspended:
1674 */
1675
1676 /* Collapsing the RunRoutine() call stack */
1677 if (debugger_collapsing) return;
1678
1679
1680 /* May be necessary to reset this if, for some
1681 reason, the line array was altered (see above)
1682 */
1683 if (!trace_complex_prop_routine)
1684 sprintf(debug_line, "Calling: %s", RoutineName(currentroutine));
1685 trace_complex_prop_routine = false;
1686
1687
1688 /* Add this statement to the code history */
1689 if (!debugger_step_back && !(debugger_step_over && step_nest>0))
1690 {
1691 if (++history_count>=MAX_CODE_HISTORY)
1692 history_count = MAX_CODE_HISTORY;
1693 code_history[history_last] = codeptr;
1694 dbnest_history[history_last] = dbnest;
1695 if (++history_last >= MAX_CODE_HISTORY)
1696 history_last = 0;
1697 }
1698
1699 /* If skipping next or stepping back */
1700 if (debugger_skip || debugger_step_back)
1701 {
1702 /* Debugger() has reset codeptr to next_codeptr */
1703 debugger_skip = false;
1704 continue;
1705 }
1706
1707 #endif /* defined (DEBUGGER) */
1708
1709
1710 /*
1711 * This is the heart of RunRoutine(): the switch statement
1712 * that executes the next engine operation based on what token
1713 * has been read
1714 */
1715 #if defined (DEBUGGER)
1716 ProcessToken:
1717 #endif
1718 switch (t)
1719 {
1720 /* First process any encoded, non-executable data: */
1721
1722 /* If this is v2.5 or later, the compiler will have
1723 noted the nesting level of this label to
1724 reconcile stack_depth
1725 */
1726 case LABEL_T:
1727 stack_depth = initial_stack_depth + MEM(++codeptr);
1728 codeptr++;
1729 break;
1730
1731 case DEBUGDATA_T:
1732 {
1733 switch (MEM(++codeptr))
1734 {
1735 case VAR_T: /* local variable name */
1736 {
1737 len = MEM(++codeptr);
1738 #if defined (DEBUGGER)
1739 if (!debugger_has_stepped_back)
1740 {
1741 /* Read the local variable name */
1742 for (i=0; i<len; i++)
1743 line[i] = MEM(codeptr+i+1);
1744 line[len] = '\0';
1745
1746 /* Check to make sure it doesn't already exist,
1747 for instance, if we've looped back to it
1748 */
1749 for (i=0; i<current_locals; i++)
1750 if (!strcmp(line, localname[i])) break;
1751
1752 /* If it doesn't exist, add it */
1753 if (i==current_locals)
1754 {
1755 strcpy(localname[current_locals], line);
1756 if (++current_locals==MAXLOCALS)
1757 current_locals--;
1758 window[VIEW_LOCALS].count = current_locals;
1759 }
1760 }
1761 #endif
1762 codeptr+=(len+1);
1763 break;
1764 }
1765 }
1766 break;
1767 }
1768
1769
1770 /* Then the executable statements: */
1771
1772 case TEXTDATA_T: /* printed text from file */
1773 {
1774 textaddr = Peek(codeptr+1)*65536L+(long)PeekWord(codeptr+2);
1775 strcpy(line, GetText(textaddr));
1776 codeptr += 4;
1777 if (Peek(codeptr)==SEMICOLON_T)
1778 {strcat(line, "\\;");
1779 codeptr++;}
1780 if (capital)
1781 {line[0] = (char)toupper((int)line[0]);
1782 capital = 0;}
1783 AP(line);
1784 break;
1785 }
1786
1787 case TEXT_T:
1788 {
1789 if (MEM(++codeptr)==TO_T)
1790 {
1791 codeptr++;
1792 #if defined (DEBUGGER)
1793 param_type = MEM(codeptr);
1794 param_start = codeptr;
1795 #endif
1796 textto = GetValue();
1797 if (game_version>=23) codeptr++; /* eol */
1798 #if defined (DEBUGGER)
1799 /* Check if textto is 0 but was not
1800 really "text to 0", but rather
1801 something that evaluated to 0
1802 */
1803 if (textto==0 && runtime_warnings)
1804 {
1805 if (param_type!=VALUE_T || param_start!=codeptr-4)
1806 RuntimeWarning("Text array address evaluates to zero");
1807 }
1808 #endif
1809 }
1810 else
1811 {
1812 SetupDisplay();
1813 }
1814 break;
1815 }
1816
1817 case MINUS_T: /* "--" */
1818 case PLUS_T: /* "++" */
1819 GetValue();
1820 codeptr++; /* eol */
1821 break;
1822
1823 case PRINT_T:
1824 RunPrint();
1825 break;
1826
1827 case PRINTCHAR_T:
1828 {
1829 Printcharloop:
1830 codeptr++;
1831 i = GetValue();
1832 if (capital) sprintf(line, "%c\\;", toupper(i));
1833 else sprintf(line, "%c\\;", i);
1834 capital = 0;
1835 AP(line);
1836 if (Peek(codeptr)==COMMA_T)
1837 goto Printcharloop;
1838 if (game_version>=23) codeptr++; /* eol */
1839 break;
1840 }
1841
1842 case STRING_T:
1843 RunString();
1844 break;
1845
1846 case WINDOW_T:
1847 RunWindow();
1848 break;
1849
1850 case LOCATE_T:
1851 {
1852 char adhere_to_bottom = false;
1853
1854 codeptr++;
1855
1856 Flushpbuffer();
1857
1858 xpos = GetValue();
1859 if (xpos > physical_windowwidth/FIXEDCHARWIDTH)
1860 xpos = physical_windowwidth/FIXEDCHARWIDTH;
1861
1862 if (Peek(codeptr)==COMMA_T)
1863 {
1864 codeptr++;
1865 ypos = GetValue();
1866 }
1867 else
1868 ypos = currentline;
1869
1870 full = ypos - 1;
1871
1872
1873 if (ypos >= physical_windowheight/lineheight)
1874 full = 0;
1875
1876 if (ypos > physical_windowheight/lineheight)
1877 {
1878 ypos = physical_windowheight/lineheight;
1879
1880 if (!inwindow && current_text_y && (currentfont & PROP_FONT))
1881 adhere_to_bottom = true;
1882 }
1883
1884 hugo_settextpos(xpos, ypos);
1885
1886 /* An adjustment for non-fixed-width font lineheight */
1887 if (adhere_to_bottom)
1888 current_text_y = physical_windowbottom - lineheight;
1889
1890 currentpos = (xpos-1)*FIXEDCHARWIDTH;
1891 currentline = ypos;
1892
1893 codeptr++; /* skip EOL */
1894 break;
1895 }
1896
1897 case SELECT_T:
1898 codeptr++;
1899 break;
1900
1901 case CASE_T:
1902 case IF_T:
1903 case ELSEIF_T:
1904 case ELSE_T:
1905 case WHILE_T:
1906 case FOR_T:
1907 RunIf(0);
1908 break;
1909
1910 case DO_T:
1911 RunDo();
1912 break;
1913
1914 case RUN_T:
1915 codeptr++;
1916 tempret = ret;
1917 GetValue(); /* object.property to run */
1918 ret = tempret;
1919 if (game_version>=23) codeptr++; /* eol */
1920 break;
1921
1922 case BREAK_T:
1923 {
1924 for (; stack_depth>0; stack_depth--)
1925 {
1926 if (code_block[stack_depth].brk)
1927 {
1928 codeptr = code_block[stack_depth].brk;
1929 #if defined (DEBUGGER)
1930 dbnest = code_block[stack_depth].dbnest;
1931 #endif
1932 --stack_depth;
1933 goto LeaveBreak;
1934 }
1935 }
1936 codeptr++;
1937 LeaveBreak:
1938 break;
1939 }
1940
1941 case RETURN_T:
1942 {
1943 codeptr++;
1944 i = inexpr; /* don't reuse tempinexpr */
1945 inexpr = 1;
1946
1947 /* Let 'return Routine()' or 'return obj.prop'
1948 set tail_recursion
1949 */
1950 tail_recursion = 0;
1951 tail_recursion_addr = 0;
1952
1953 SetupExpr();
1954 inexpr = (char)i;
1955
1956 /* If either a routine or property routine call has
1957 determined it's valid, we can use tail-recursion
1958 (with tail_recursion_addr having been set up)
1959 */
1960 if (tail_recursion)
1961 {
1962 HandleTailRecursion(tail_recursion_addr);
1963 break;
1964 }
1965 else
1966 {
1967 /* Clear these to be safe */
1968 tail_recursion = 0;
1969 tail_recursion_addr = 0;
1970
1971 ret = EvalExpr(0);
1972 retflag = true;
1973 goto LeaveRunRoutine;
1974 }
1975 }
1976
1977 case JUMP_T:
1978 {
1979 codeptr = (long)PeekWord(codeptr + 1)*address_scale;
1980 #if defined (DEBUGGER)
1981 if (MEM(codeptr)==LABEL_T)
1982 dbnest = 0; /* prevent "false" nesting */
1983 #endif
1984 break;
1985 }
1986
1987 case PARENT_T:
1988 case SIBLING_T:
1989 case CHILD_T:
1990 case YOUNGEST_T:
1991 case ELDEST_T:
1992 case YOUNGER_T:
1993 case ELDER_T:
1994 {
1995 inobj = true;
1996
1997 /* Note: GetValue() would actually get
1998 the property/attribute to be set
1999 */
2000 RunSet(GetVal());
2001
2002 inobj = false;
2003 break;
2004 }
2005
2006 case VAR_T:
2007 case OBJECTNUM_T:
2008 case VALUE_T:
2009 case WORD_T:
2010 case ARRAYDATA_T:
2011 case ARRAY_T:
2012 RunSet(-1);
2013 break;
2014
2015 case ROUTINE_T:
2016 case CALL_T:
2017 {
2018 switch (t)
2019 {
2020 case ROUTINE_T:
2021 {
2022 codeptr++;
2023 routineptr = PeekWord(codeptr);
2024 codeptr += 2;
2025 break;
2026 }
2027 case CALL_T:
2028 {
2029 codeptr++;
2030 routineptr = GetValue();
2031 }
2032 }
2033
2034 tempret = ret;
2035 CallRoutine(routineptr);
2036
2037 if (MEM(codeptr)==DECIMAL_T || MEM(codeptr)==IS_T)
2038 RunSet(ret);
2039 else if ((t==CALL_T) && game_version>=23)
2040 codeptr++; /* eol */
2041
2042 ret = tempret;
2043
2044 break;
2045 }
2046
2047 case MOVE_T:
2048 case REMOVE_T:
2049 RunMove();
2050 break;
2051
2052 case COLOR_T:
2053 case COLOUR_T:
2054 {
2055 codeptr++;
2056
2057 /* Get foreground color */
2058 fcolor = (char)GetValue();
2059 /* If background color is given */
2060 if (Peek(codeptr)==COMMA_T)
2061 {
2062 codeptr++;
2063 bgcolor = (char)GetValue();
2064
2065 /* If input color is given */
2066 if (Peek(codeptr)==COMMA_T)
2067 {
2068 codeptr++;
2069 icolor = (char)GetValue();
2070 }
2071 else
2072 icolor = fcolor;
2073 }
2074 else
2075 icolor = fcolor;
2076
2077 /* Only set the actual pen color now if
2078 there is no text buffered
2079 */
2080 if (pbuffer[0]=='\0')
2081 {
2082 hugo_settextcolor(fcolor);
2083 hugo_setbackcolor(bgcolor);
2084 }
2085
2086 if (inwindow)
2087 default_bgcolor = bgcolor;
2088
2089 codeptr++; /* skip EOL */
2090 break;
2091 }
2092
2093 case PAUSE_T:
2094 {
2095 full = 0;
2096 override_full = true;
2097 codeptr++;
2098 Flushpbuffer();
2099 /* Flush the key buffer first */
2100 while (hugo_iskeywaiting()) hugo_getkey();
2101 wd[0] = (unsigned int)hugo_waitforkey();
2102 #if defined (DEBUGGER)
2103 runaway_counter = 0;
2104 #endif
2105 break;
2106 }
2107
2108 case RUNEVENTS_T:
2109 codeptr++;
2110 RunEvents();
2111 break;
2112
2113 case QUIT_T:
2114 var[endflag] = -1;
2115 break;
2116
2117 case INPUT_T:
2118 RunInput();
2119 full = 1;
2120 override_full = true;
2121 codeptr++;
2122 break;
2123
2124 case SYSTEM_T:
2125 RunSystem();
2126 if (game_version>=23) codeptr++; /* eol */
2127 break;
2128
2129 case CLS_T:
2130 {
2131 hugo_settextcolor(fcolor);
2132 hugo_setbackcolor(bgcolor);
2133 hugo_clearwindow();
2134 hugo_settextpos(1, physical_windowheight/lineheight); /*+1);*/
2135
2136 if (!inwindow)
2137 {
2138 full = 0;
2139 }
2140 default_bgcolor = bgcolor;
2141
2142 codeptr++;
2143 pbuffer[0] = '\0';
2144 break;
2145 }
2146
2147 case WRITEFILE_T:
2148 case READFILE_T:
2149 FileIO();
2150 break;
2151
2152 case WRITEVAL_T:
2153 {
2154 Writevalloop:
2155 codeptr++;
2156 i = GetValue();
2157 #if !defined (MATH_16BIT)
2158 if (i < 0)
2159 i += 65536;
2160 #endif
2161 if (ioblock)
2162 {
2163 if ((ioblock==2)
2164 || fputc(i%256, io)==EOF
2165 || fputc(i/256, io)==EOF)
2166 {
2167 ioerror = true;
2168 retflag = true;
2169 break;
2170 }
2171 }
2172
2173 if (Peek(codeptr)==COMMA_T)
2174 goto Writevalloop;
2175
2176 if (game_version>=23) codeptr++; /* eol */
2177 break;
2178 }
2179
2180 case PICTURE_T:
2181 DisplayPicture();
2182 break;
2183
2184 case MUSIC_T:
2185 PlayMusic();
2186 break;
2187
2188 case SOUND_T:
2189 PlaySample();
2190 break;
2191
2192 case VIDEO_T:
2193 PlayVideo();
2194 break;
2195
2196 case ADDCONTEXT_T:
2197 ContextCommand();
2198 break;
2199
2200 /* Didn't match a command token, so throw up an
2201 "Unknown operation" error.
2202 */
2203 default:
2204 FatalError(UNKNOWN_OP_E);
2205 }
2206
2207 defseg = gameseg;
2208
2209 if (retflag) goto LeaveRunRoutine;
2210 }
2211
2212
2213 /* Process the closing '}': */
2214
2215 codeptr++;
2216
2217 #if defined (DEBUGGER)
2218 if (--dbnest < 0) dbnest = 0;
2219 #endif
2220 /* Continue executing this iteration of RunRoutine() if the
2221 '}' marks the end of a conditional block, i.e., one that
2222 didn't call RunRoutine() the way, e.g., 'window' does.
2223 Otherwise, get out of RunRoutine().
2224 */
2225 if (code_block[stack_depth--].type > RUNROUTINE_BLOCK)
2226 {
2227 /* Skip a following 'elseif', 'else', or 'case' */
2228 t = MEM(codeptr);
2229 while (t==ELSEIF_T || t==ELSE_T || t==CASE_T)
2230 {
2231 RunIf(1);
2232 t = MEM(codeptr);
2233 }
2234
2235 if (t==WHILE_T && code_block[stack_depth+1].type==DOWHILE_BLOCK)
2236 {
2237 codeptr+=3;
2238 tempinexpr = inexpr;
2239 inexpr = 1;
2240 SetupExpr();
2241 inexpr = tempinexpr;
2242
2243 if (EvalExpr(0))
2244 codeptr = code_block[++stack_depth].returnaddr;
2245 else
2246 codeptr = code_block[stack_depth+1].brk;
2247 }
2248
2249 /* Since this isn't a RUNROUTINE_BLOCK, keep running this
2250 iteration of RunRoutine()
2251 */
2252 goto ContinueRunning;
2253
2254 }
2255
2256 if (stack_depth<0) stack_depth = 0;
2257
2258 if (var[endflag]) return;
2259
2260
2261 LeaveRunRoutine:
2262
2263 #if defined (DEBUGGER)
2264
2265 /*
2266 * Finally, do any debugger-required cleaning-up
2267 */
2268
2269 /* As noted above, wascalled is true if this was a routine call
2270 as opposed to, e.g., a conditional block. In the former case,
2271 it is necessary to print the "Returning from..." message.
2272 */
2273 if (wascalled)
2274 {
2275 if (debugger_step_over)
2276 {
2277 if (--step_nest<=0)
2278 {
2279 debugger_step_over = false;
2280 debugger_interrupt = true;
2281
2282 if (debugger_finish || step_nest < 0)
2283 {
2284 debugger_finish = false;
2285 goto ReturnfromRoutine;
2286 }
2287 }
2288 }
2289
2290 else if (!debugger_step_over)
2291 {
2292 ReturnfromRoutine:
2293 sprintf(debug_line, "(Returning %d", ret);
2294
2295 /* Since a complex property routine will give "<Routine>" as the
2296 routine name, skip those
2297 */
2298 called_from = RoutineName(currentroutine);
2299 if (!trace_comp_prop && called_from[0]!='<')
2300 sprintf(debug_line+strlen(debug_line), " from %s", called_from);
2301
2302 if (old_currentroutine!=mainaddr && old_currentroutine!=initaddr
2303 && currentroutine!=mainaddr && currentroutine!=initaddr)
2304 {
2305 sprintf(debug_line+strlen(debug_line), " to %s", RoutineName(old_currentroutine));
2306 }
2307 strcat(debug_line, ")");
2308 AddStringtoCodeWindow(debug_line);
2309 AddStringtoCodeWindow("");
2310
2311 if ((signed)--window[VIEW_CALLS].count < 0)
2312 window[VIEW_CALLS].count = 0;
2313 window[VIEW_CALLS].changed = true;
2314 }
2315
2316 currentroutine = old_currentroutine;
2317 }
2318
2319 /*#elif defined (DEBUG_CODE)
2320 if (wascalled)
2321 {sprintf(line, "[RETURNING %d]", ret);
2322 AP(line);}
2323 */
2324 #endif
2325
2326 return;
2327 }
2328
2329
2330 /* RUNSAVE */
2331
2332 /* Note that under Glk, stdio f...() functions map to Glk functions */
2333
2334 #ifndef SAVEGAMEDATA_REPLACED
2335
2336 int SaveGameData(void)
2337 {
2338 int c, j;
2339 int lbyte, hbyte;
2340 long i;
2341 int samecount = 0;
2342
2343 /* Write ID */
2344 if (fputc(id[0], save)==EOF || fputc(id[1], save)==EOF) goto SaveError;
2345
2346 /* Write serial number */
2347 if (fputs(serial, save)==EOF) goto SaveError;
2348
2349 /* Save variables */
2350 for (c=0; c<MAXGLOBALS+MAXLOCALS; c++)
2351 {
2352 hbyte = (unsigned int)var[c] / 256;
2353 lbyte = (unsigned int)var[c] - hbyte * 256;
2354 if (fputc(lbyte, save)==EOF || fputc(hbyte, save)==EOF) goto SaveError;
2355 }
2356
2357 /* Save objtable to end of code space */
2358
2359 if (fseek(game, objtable*16L, SEEK_SET)) goto SaveError;
2360
2361 for (i=0; i<=codeend-(long)(objtable*16L); i++)
2362 {
2363 if ((lbyte = fgetc(game))==EOF) goto SaveError;
2364 hbyte = MEM(objtable*16L+i);
2365
2366 /* If memory same as original game file */
2367 if (lbyte==hbyte && samecount<255) samecount++;
2368
2369 /* If memory differs (or samecount exceeds 1 byte) */
2370 else
2371 {
2372 if (samecount)
2373 if (fputc(samecount, save)==EOF) goto SaveError;
2374
2375 if (lbyte!=hbyte)
2376 {
2377 if (fputc(0, save)==EOF) goto SaveError;
2378 if (fputc(hbyte, save)==EOF) goto SaveError;
2379 samecount = 0;
2380 }
2381 else samecount = 1;
2382 }
2383 }
2384 if (samecount)
2385 if (fputc(samecount, save)==EOF) goto SaveError;
2386
2387 /* Save undo data */
2388
2389 /* Save the number of turns in this port's undo stack */
2390 hbyte = (unsigned int)MAXUNDO / 256;
2391 lbyte = (unsigned int)MAXUNDO - hbyte*256;
2392 if (fputc(lbyte, save)==EOF || fputc(hbyte, save)==EOF)
2393 goto SaveError;
2394 for (c=0; c<MAXUNDO; c++)
2395 {
2396 for (j=0; j<5; j++)
2397 {
2398 hbyte = (unsigned int)undostack[c][j] / 256;
2399 lbyte = (unsigned int)undostack[c][j] - hbyte*256;
2400 if (fputc(lbyte, save)==EOF || fputc(hbyte, save)==EOF)
2401 goto SaveError;
2402 }
2403 }
2404 if (fputc(undoptr-(undoptr/256)*256, save)==EOF || fputc(undoptr/256, save)==EOF)
2405 goto SaveError;
2406 if (fputc(undoturn-(undoturn/256)*256, save)==EOF || fputc(undoturn/256, save)==EOF)
2407 goto SaveError;
2408 if (fputc(undoinvalid, save)==EOF || fputc(undorecord, save)==EOF)
2409 goto SaveError;
2410
2411 return true;
2412
2413 SaveError:
2414 return false;
2415 }
2416
2417 #endif // SAVEGAMEDATA_REPLACED
2418
2419 int RunSave()
2420 {
2421 #ifdef PALMOS
2422 /* Prevent simultaneous access to the same db record */
2423 int dummy = MEM(objtable*16L);
2424 #endif
2425
2426 #if !defined (GLK)
2427 save = NULL;
2428
2429 /* stdio implementation */
2430 hugo_getfilename("to save", savefile);
2431
2432 #if defined (DEBUGGER)
2433 if (debugger_collapsing) return 1;
2434 #endif
2435 if (!strcmp(line, gamefile)) return 0;
2436 if (!strcmp(line, "")) return 0;
2437 if (!hugo_overwrite(line)) return 0;
2438 if (!(save = HUGO_FOPEN(line, "w+b"))) return 0;
2439
2440 #else
2441 /* Glk implementation */
2442 frefid_t savefile;
2443
2444 save = NULL;
2445
2446 savefile = glk_fileref_create_by_prompt(fileusage_SavedGame | fileusage_BinaryMode,
2447 filemode_Write, 0);
2448 if (!savefile) return 0;
2449 save = glk_stream_open_file(savefile, filemode_Write, 0);
2450 glk_fileref_destroy(savefile);
2451 if (!save) return 0;
2452
2453 #endif /* GLK */
2454
2455 if (!SaveGameData()) goto SaveError;
2456
2457 if (fclose(save)) FatalError(WRITE_E);
2458 save = NULL;
2459
2460 #if !defined (GLK)
2461 strcpy(savefile, line);
2462 #endif
2463
2464 return(1);
2465
2466 SaveError:
2467 if ((save) && fclose(save)) FatalError(WRITE_E);
2468 save = NULL;
2469 return 0;
2470 }
2471
2472
2473 /* RUNSCRIPTSET */
2474
2475 int RunScriptSet()
2476 {
2477 remaining = 0;
2478
2479 switch (Peek(codeptr))
2480 {
2481 case SCRIPTON_T:
2482 {
2483 if (!script)
2484 {
2485 #if !defined (GLK)
2486 /* stdio implementation */
2487 hugo_getfilename("to begin transcription (or printer name)", scriptfile);
2488 #if defined (DEBUGGER)
2489 if (debugger_collapsing) return 1;
2490 #endif
2491 if (!strcmp(line, "")) return 0;
2492 if (!hugo_overwrite(line)) return 0;
2493 if (!(script = HUGO_FOPEN(line, "wt")))
2494 return (0);
2495 strcpy(scriptfile, line);
2496
2497 #else
2498 /* Glk implementation */
2499 frefid_t fref;
2500
2501 fref = glk_fileref_create_by_prompt(fileusage_Transcript | fileusage_TextMode,
2502 filemode_Write, 0);
2503 script = glk_stream_open_file(fref, filemode_Write, 0);
2504 glk_fileref_destroy(fref);
2505 if (!script) return (0);
2506 #endif /* GLK */
2507 return 1;
2508 }
2509 break;
2510 }
2511
2512 case SCRIPTOFF_T:
2513 {
2514 if (script)
2515 {
2516 if (fclose(script)) return (0);
2517 script = NULL;
2518 return 1;
2519 }
2520 break;
2521 }
2522 }
2523 return 0;
2524 }
2525
2526
2527 /* RUNSTRING
2528
2529 As in 'x = string(<array>, "<string>"[, maxlen]'.
2530 */
2531
2532 int RunString()
2533 {
2534 int i, pos;
2535 unsigned int aaddr; /* array address */
2536 unsigned int dword; /* dictionary word */
2537 unsigned int maxlen = 32767;
2538
2539 codeptr += 2; /* skip "(" */
2540
2541 aaddr = GetValue();
2542 if (game_version>=22)
2543 {
2544 /* Convert to 16-bit word value */
2545 aaddr*=2;
2546
2547 if (game_version>=23)
2548 {
2549 defseg = arraytable;
2550 maxlen = PeekWord(aaddr);
2551 defseg = gameseg;
2552
2553 /* Space for array length */
2554 aaddr+=2;
2555 }
2556 }
2557
2558 if (Peek(codeptr)==COMMA_T) codeptr++;
2559
2560 dword = GetValue();
2561
2562 if (Peek(codeptr)==COMMA_T) codeptr++;
2563
2564 if (Peek(codeptr)!=CLOSE_BRACKET_T)
2565 maxlen = GetValue();
2566 if (Peek(codeptr)==CLOSE_BRACKET_T) codeptr++;
2567
2568 strcpy(line, GetWord(dword));
2569
2570 defseg = arraytable;
2571 pos = 0;
2572 for (i=0; i<(int)strlen(line) && i<(int)maxlen; i++, pos++)
2573 {
2574 char a;
2575
2576 SaveUndo(ARRAYDATA_T, aaddr, i, PeekWord(aaddr+i*2), 0);
2577
2578 a = line[i];
2579 if (a=='\\')
2580 ++i, a = SpecialChar(line, &i);
2581 PokeWord(aaddr+pos*2, a);
2582 }
2583 PokeWord(aaddr+pos*2, 0);
2584
2585 defseg = gameseg;
2586
2587 return (i);
2588 }
2589
2590
2591 /* RUNSYSTEM */
2592
2593 #define STAT_UNAVAILABLE ((short)-1)
2594
2595 int RunSystem(void)
2596 {
2597 codeptr++;
2598
2599 /* Since the obsolete form of the system command is unimplemented,
2600 simply get the parameter (in order to skip it), and exit the
2601 function.
2602 */
2603 if (game_version < 25)
2604 {
2605 GetValue();
2606 return 0;
2607 }
2608
2609 /* Otherwise, process the following system calls: */
2610
2611 codeptr++; /* skip opening bracket */
2612
2613 var[system_status] = 0;
2614
2615 Flushpbuffer();
2616
2617 switch (GetValue())
2618 {
2619 case 11: /* READ_KEY */
2620 if (!hugo_iskeywaiting())
2621 return 0;
2622 else
2623 {
2624 full = 0;
2625 return hugo_getkey();
2626 }
2627
2628 case 21: /* NORMALIZE_RANDOM */
2629 #if !defined (RANDOM)
2630 srand(1);
2631 #else
2632 SRANDOM(1);
2633 #endif
2634 break;
2635 case 22: /* INIT_RANDOM */
2636 {
2637 time_t seed;
2638 #if !defined (RANDOM)
2639 srand((unsigned int)time((time_t *)&seed));
2640 #else
2641 SRANDOM((unsigned int)time((time_t *)&seed));
2642 #endif
2643 break;
2644 }
2645 case 31: /* PAUSE_SECOND */
2646 if (!hugo_timewait(1))
2647 var[system_status] = STAT_UNAVAILABLE;
2648 break;
2649
2650 case 32: /* PAUSE_100TH_SECOND */
2651 if (!hugo_timewait(100))
2652 var[system_status] = STAT_UNAVAILABLE;
2653 break;
2654
2655 case 41: /* GAME_RESET */
2656 {
2657 if (game_reset)
2658 {
2659 game_reset = 0;
2660 return true;
2661 }
2662 return false;
2663 }
2664
2665 case 51: /* SYSTEM_TIME */
2666 {
2667 #ifndef NO_STRFTIME
2668 time_t t;
2669 struct tm *lt;
2670 time(&t);
2671 lt = localtime(&t);
2672 strftime(parseerr, 20, "%Y-%m-%d %H:%M:%S", lt);
2673 #else
2674 hugo_gettimeformatted(parseerr);
2675 #endif
2676 return true;
2677 }
2678
2679 case 61: /* MINIMAL_INTERFACE */
2680 #ifdef MINIMAL_INTERFACE
2681 return true;
2682 #else
2683 return false;
2684 #endif
2685
2686 default:
2687 var[system_status] = STAT_UNAVAILABLE;
2688 }
2689
2690 return 0;
2691 }
2692
2693
2694 /* RUNWINDOW
2695
2696 As in 'window [n[, o, p, q]]'.
2697 */
2698
2699 struct SAVED_WINDOW_DATA
2700 {
2701 int left, top, right, bottom;
2702 int width, height, charwidth, lineheight;
2703 int currentpos, currentline;
2704 int currentfont;
2705 } SAVED_WINDOW_DATA;
2706
2707 void SaveWindowData(struct SAVED_WINDOW_DATA *spw)
2708 {
2709 spw->left = physical_windowleft;
2710 spw->top = physical_windowtop;
2711 spw->right = physical_windowright;
2712 spw->bottom = physical_windowbottom;
2713 spw->width = physical_windowwidth;
2714 spw->height = physical_windowheight;
2715 spw->currentfont = currentfont;
2716 spw->charwidth = charwidth;
2717 spw->lineheight = lineheight;
2718 spw->currentpos = currentpos;
2719 spw->currentline = currentline;
2720 }
2721
2722 void RestoreWindowData(struct SAVED_WINDOW_DATA *spw)
2723 {
2724 physical_windowleft = spw->left;
2725 physical_windowtop = spw->top;
2726 physical_windowright = spw->right;
2727 physical_windowbottom = spw->bottom;
2728 physical_windowwidth = spw->width;
2729 physical_windowheight = spw->height;
2730
2731 charwidth = spw->charwidth;
2732 lineheight = spw->lineheight;
2733 currentpos = spw->currentpos;
2734 currentline = spw->currentline;
2735
2736 /* if (currentfont!=spw->currentfont) hugo_font((currentfont = spw->currentfont)); */
2737 }
2738
2739 void RunWindow(void)
2740 {
2741 int top, bottom, left, right;
2742 struct SAVED_WINDOW_DATA restorewindow;
2743 int temp_current_text_y;
2744 char restore_default_bgcolor;
2745 int tempfull;
2746 int temp_stack_depth = stack_depth;
2747 HUGO_FILE tempscript;
2748 #ifdef MINIMAL_WINDOWING
2749 int last_lowest_windowbottom = lowest_windowbottom;
2750 #endif
2751
2752 #if defined (DEBUGGER)
2753 unsigned char param_type;
2754 int tempdbnest;
2755 long param_start;
2756 #endif
2757
2758 Flushpbuffer();
2759 tempfull = full;
2760 full = 0;
2761 override_full = false;
2762
2763 temp_current_text_y = current_text_y;
2764
2765 tempscript = script;
2766 script = false;
2767 restore_default_bgcolor = default_bgcolor;
2768
2769 /* v2.4 is the first version to support proper windowing */
2770 if (game_version>=24)
2771 {
2772 /* Set up default top, left, etc. as character coordinates,
2773 and save the current physical window data
2774 */
2775 left = physical_windowleft/FIXEDCHARWIDTH + 1;
2776 top = physical_windowtop/FIXEDLINEHEIGHT + 1;
2777 right = physical_windowright/FIXEDCHARWIDTH + 1;
2778 bottom = physical_windowbottom/FIXEDLINEHEIGHT + 1;
2779
2780 SaveWindowData(&restorewindow);
2781
2782 /* if "window x1, y1, x2, y2" or "window n"... */
2783 if (MEM(++codeptr)!=EOL_T)
2784 {
2785 #if defined (DEBUGGER)
2786 param_type = MEM(codeptr);
2787 param_start = codeptr;
2788 #endif
2789 left = GetValue();
2790 if (MEM(codeptr++)==COMMA_T)
2791 {
2792 top = GetValue();
2793 if (MEM(codeptr++)==COMMA_T)
2794 {
2795 right = GetValue();
2796 if (MEM(codeptr++)==COMMA_T)
2797 {
2798 bottom = GetValue();
2799 codeptr++;
2800 }
2801 }
2802 }
2803
2804 /* if only one parameter, i.e., "window n" */
2805 else
2806 {
2807 if (left!=0)
2808 {
2809 bottom = left;
2810 top = 1;
2811 left = 1;
2812 right = SCREENWIDTH/FIXEDCHARWIDTH;
2813 }
2814
2815 /* "window 0" restores full screen without
2816 running a code block
2817 */
2818 else
2819 {
2820 #if defined (DEBUGGER)
2821 /* Here, check to see if left was 0 but the
2822 statement wasn't really "window 0", but
2823 rather something that evaluated to zero
2824 */
2825 if (runtime_warnings)
2826 {
2827 if (param_type!=VALUE_T || param_start!=codeptr-4)
2828 RuntimeWarning("Window size evaluates to zero");
2829 }
2830 #endif
2831 left = 1, top = 1;
2832 right = SCREENWIDTH/FIXEDCHARWIDTH;
2833 bottom = SCREENHEIGHT/FIXEDLINEHEIGHT;
2834 physical_lowest_windowbottom = lowest_windowbottom = 0;
2835 hugo_settextwindow(left, top, right, bottom);
2836 goto LeaveWindow;
2837 }
2838 }
2839 }
2840
2841 /* ...or just "window", so use last window defaults */
2842 else
2843 {
2844 codeptr++; /* skip EOL */
2845
2846 left = last_window_left;
2847 top = last_window_top;
2848 right = last_window_right;
2849 bottom = last_window_bottom;
2850 }
2851
2852 /* Remember, these are character/text coordinates */
2853 if (top < 1) top = 1;
2854 if (left < 1) left = 1;
2855 if (bottom < 1) bottom = 1;
2856 if (right < 1) right = 1;
2857 if (top > SCREENHEIGHT/FIXEDLINEHEIGHT)
2858 top = SCREENHEIGHT/FIXEDLINEHEIGHT;
2859 if (left > SCREENWIDTH/FIXEDCHARWIDTH)
2860 left = SCREENWIDTH/FIXEDCHARWIDTH;
2861 if (bottom > SCREENHEIGHT/FIXEDLINEHEIGHT)
2862 bottom = SCREENHEIGHT/FIXEDLINEHEIGHT;
2863 if (right > SCREENWIDTH/FIXEDCHARWIDTH)
2864 right = SCREENWIDTH/FIXEDCHARWIDTH;
2865
2866 /* Set the new text window */
2867 inwindow = true;
2868 hugo_settextwindow(left, top, right, bottom);
2869 hugo_settextpos(1, 1);
2870
2871 #if defined (DEBUGGER)
2872 tempdbnest = dbnest++;
2873 #endif
2874 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
2875
2876 RunRoutine(codeptr);
2877
2878 #if defined (DEBUGGER)
2879 dbnest = tempdbnest;
2880 #endif
2881 stack_depth = temp_stack_depth;
2882
2883 Flushpbuffer();
2884
2885 /* Restore the old window parameters */
2886 last_window_top = top;
2887 last_window_bottom = bottom;
2888 last_window_left = left;
2889 last_window_right = right;
2890
2891 /* Figure out what the lowest window bottom is that we need
2892 to protect from scrolling
2893 */
2894 if (bottom > lowest_windowbottom)
2895 lowest_windowbottom = bottom;
2896
2897 #ifdef MINIMAL_WINDOWING
2898 if (minimal_windowing && illegal_window)
2899 lowest_windowbottom = last_lowest_windowbottom;
2900 #endif
2901 /* (error situation--shouldn't happen) */
2902 if (lowest_windowbottom>=SCREENHEIGHT/FIXEDLINEHEIGHT)
2903 lowest_windowbottom = 0;
2904
2905 /* Restore the old text window */
2906 RestoreWindowData(&restorewindow);
2907
2908 inwindow = false;
2909 hugo_settextwindow(physical_windowleft/FIXEDCHARWIDTH + 1,
2910 lowest_windowbottom + 1,
2911 physical_windowright/FIXEDCHARWIDTH + 1,
2912 physical_windowbottom/FIXEDLINEHEIGHT + 1);
2913
2914 physical_lowest_windowbottom = lowest_windowbottom*FIXEDLINEHEIGHT;
2915 }
2916
2917 /* v2.3 and earlier supported a very simple version of
2918 windowing: mainly just moving the top/scroll-off line
2919 of the printable area to the bottom of the text printed
2920 in the "window" block
2921 */
2922 else
2923 {
2924 inwindow = true;
2925 hugo_settextwindow(1, 1,
2926 SCREENWIDTH/FIXEDCHARWIDTH,
2927 SCREENHEIGHT/FIXEDLINEHEIGHT);
2928 hugo_settextpos(1, 1);
2929
2930 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
2931 RunRoutine(++codeptr);
2932 Flushpbuffer();
2933
2934 inwindow = false;
2935
2936 stack_depth = temp_stack_depth;
2937
2938 hugo_settextwindow(1, full+1,
2939 SCREENWIDTH/FIXEDCHARWIDTH,
2940 SCREENHEIGHT/FIXEDLINEHEIGHT);
2941
2942 physical_lowest_windowbottom = full*lineheight;
2943 }
2944
2945 LeaveWindow:
2946
2947 current_text_y = temp_current_text_y;
2948
2949 #ifndef PALMOS
2950 if (!current_text_y)
2951 hugo_settextpos(1, physical_windowheight/lineheight);
2952 #endif
2953 current_text_x = 0;
2954 currentpos = 0;
2955
2956 default_bgcolor = restore_default_bgcolor;
2957 script = tempscript;
2958
2959 if (!override_full)
2960 full = tempfull;
2961 override_full = false;
2962
2963 just_left_window = true;
2964 }