Debian Codegraph @

terps/hugo/source/hemisc.c

raw symbols SCIP
1 /*
2 HEMISC.C
3
4 Miscellaneous functions:
5
6 AP ParseCommandLine
7 CallRoutine PassLocals
8 ContextCommand Peek, PeekWord
9 Dict Poke, PokeWord
10 FatalError PrintHex
11 FileIO Printout
12 Flushpbuffer PromptMore
13 GetCommand RecordCommands
14 GetString SaveUndo
15 GetText SetStackFrame
16 GetWord SetupDisplay
17 HandleTailRecursion SpecialChar
18 InitGame TrytoOpen
19 LoadGame Undo
20
21 for the Hugo Engine
22
23 Copyright (c) 1995-2009 by Kent Tessman
24 */
25
26
27 #include "heheader.h"
28 #include <stdlib.h>
29
30
31 void Banner(void); /* from he.c */
32
33 void hugo_stopmusic(void);
34 void hugo_stopsample(void);
35 #ifndef COMPILE_V25
36 void hugo_stopvideo(void);
37 #endif
38
39
40 /* game_version is needed for backward compatibility. It holds the
41 version number of the game*10 + the revision number. The object
42 size of pre-v2.2 games was only 12 bytes.
43 */
44 int game_version;
45 int object_size = 24;
46
47 /* File pointers, etc. */
48 HUGO_FILE game = NULL;
49 HUGO_FILE script = NULL;
50 HUGO_FILE save = NULL;
51 HUGO_FILE record = NULL;
52 HUGO_FILE playback = NULL;
53 HUGO_FILE io = NULL; char ioblock = 0; char ioerror = 0;
54 char gamefile[MAXPATH];
55 char gamepath[MAXPATH];
56 #if !defined (GLK)
57 char scriptfile[MAXPATH];
58 char savefile[MAXPATH];
59 char recordfile[MAXPATH];
60 #endif
61
62 /* Header information */
63 char id[3];
64 char serial[9];
65 unsigned int codestart; /* start of executable code */
66 unsigned int objtable; /* object table */
67 unsigned int eventtable; /* event table */
68 unsigned int proptable; /* property table */
69 unsigned int arraytable; /* array data table */
70 unsigned int dicttable; /* dictionary */
71 unsigned int syntable; /* synonyms */
72 unsigned int initaddr; /* "Init" routine */
73 unsigned int mainaddr; /* "Main" */
74 unsigned int parseaddr; /* "Parse" */
75 unsigned int parseerroraddr; /* "ParseError" */
76 unsigned int findobjectaddr; /* "FindObject" */
77 unsigned int endgameaddr; /* "Endgame" */
78 unsigned int speaktoaddr; /* "SpeakTo" */
79 unsigned int performaddr; /* "Perform" */
80
81 /* Totals */
82 int objects;
83 int events;
84 int dictcount; /* dictionary entries */
85 int syncount; /* synonyms, etc. */
86
87 #if !defined (COMPILE_V25)
88 char context_command[MAX_CONTEXT_COMMANDS][64];
89 int context_commands;
90 #endif
91
92 /* Loaded memory image */
93 unsigned char *mem = NULL; /* the memory buffer */
94 int loaded_in_memory = true; /* i.e., the text bank */
95 unsigned int defseg; /* holds segment indicator */
96 unsigned int gameseg; /* code segment */
97 long codeptr; /* code pointer */
98 long codeend; /* end of loaded code */
99
100 /* Text output */
101 char pbuffer[MAXBUFFER*2+1]; /* print buffer for line-wrapping */
102 int currentpos = 0; /* column position (pixel or char) */
103 int currentline = 0; /* row number (line) */
104 int full = 0; /* page counter for PromptMore */
105 signed char fcolor = 16, /* default fore/background colors */
106 bgcolor = 17, /* (16 = default foreground, */
107 icolor = -1; /* 17 = default background) */
108 signed char default_bgcolor = 17; /* default for screen background */
109 int currentfont = NORMAL_FONT; /* current font bitmasks */
110 char capital = 0; /* if next letter is to be capital */
111 unsigned int textto = 0; /* for printing to an array */
112 int SCREENWIDTH, SCREENHEIGHT; /* screen dimensions */
113 /* (in pixels or characters) */
114 int physical_windowwidth, /* "physical_..." measurements */
115 physical_windowheight, /* are in pixels (or characters) */
116 physical_windowtop, physical_windowleft,
117 physical_windowbottom, physical_windowright;
118 int inwindow = 0;
119 int charwidth, lineheight, FIXEDCHARWIDTH, FIXEDLINEHEIGHT;
120 int current_text_x = 0, current_text_y = 0;
121
122 #ifdef USE_SMARTFORMATTING
123 int smartformatting = true;
124 char leftquote = true;
125 #endif
126
127 char skipping_more = false;
128
129 /* SaveUndo() and Undo() */
130 int undostack[MAXUNDO][5]; /* for saving undo information */
131 int undoptr = 0; /* number of operations undoable */
132 int undoturn = 0; /* number of operations this turn */
133 char undoinvalid = 0; /* for start of game, and restarts */
134 char undorecord = 0; /* true when recording */
135
136 #ifdef USE_TEXTBUFFER
137 static int bufferbreak = 0, bufferbreaklen = 0;
138 #endif
139
140 /* AP
141
142 The all-purpose printing routine that takes care of word-wrapping.
143 */
144
145 void AP (char *a)
146 {
147 char sticky = false, skipspchar = false, startofline = 0;
148 int i, alen, plen, cwidth;
149 char c = 0; /* current character */
150 #ifdef USE_SMARTFORMATTING
151 char lastc = 0; /* for smart formatting */
152 #endif
153
154 static int lastfcolor = 16, lastbgcolor = 17;
155 static int lastfont = NORMAL_FONT;
156 static int thisline = 0; /* width in pixels or characters */
157 static int linebreaklen = 0, linebreak = 0;
158 int tempfont;
159 char printed_something = false;
160 #ifdef USE_TEXTBUFFER
161 int bufferfont = currentfont;
162 #endif
163
164
165 /* Shameless little trick to override control characters in engine-
166 printed text, such as MS-DOS filenames that contain '\'s:
167 */
168 if (a[0]==NO_CONTROLCHAR)
169 {
170 skipspchar = true;
171 a++;
172 }
173
174 /* Semi-colon overrides LF */
175 if ((strlen(a)>=2) && a[strlen(a)-1]==';' && a[strlen(a)-2]=='\\')
176 {
177 sticky = true;
178 }
179
180 if (hugo_strlen(pbuffer))
181 printed_something = true;
182
183 plen = strlen(pbuffer);
184 if (plen==0)
185 {
186 thisline = 0;
187 linebreak = 0;
188 linebreaklen = 0;
189 lastfont = currentfont;
190 startofline = true;
191 #ifdef USE_TEXTBUFFER
192 bufferbreak = 0;
193 bufferbreaklen = 0;
194 #endif
195 #ifdef USE_SMARTFORMATTING
196 leftquote = true;
197 #endif
198 }
199
200 /* Check for color changes */
201 if ((a[0]) && (lastfcolor!=fcolor || lastbgcolor!=bgcolor || startofline))
202 {
203 if (plen >= MAXBUFFER*2-3) FatalError(OVERFLOW_E);
204 pbuffer[plen++] = COLOR_CHANGE;
205 pbuffer[plen++] = (char)(fcolor+1);
206 pbuffer[plen++] = (char)(bgcolor+1);
207 pbuffer[plen] = '\0';
208 lastfcolor = fcolor;
209 lastbgcolor = bgcolor;
210 }
211
212 /* Check for font changes--since fonts can only get changed
213 by printing, we don't check lastfont */
214 if ((a[0]) && startofline)
215 {
216 if (plen >= MAXBUFFER*2-2) FatalError(OVERFLOW_E);
217 pbuffer[plen++] = FONT_CHANGE;
218 pbuffer[plen++] = (char)(currentfont+1);
219 pbuffer[plen] = '\0';
220 lastfont = currentfont;
221 }
222
223 /* Begin by looping through the entire provided string: */
224
225 alen = (int)strlen(a);
226 if (sticky)
227 alen -= 2;
228
229 /* Not printing any actual text, so we won't need to go through
230 queued font changes
231 */
232 if (alen==0)
233 lastfont = currentfont;
234
235 for (i=0; i<alen; i++)
236 {
237 c = a[i];
238
239 /* Left-justification */
240 if (thisline==0 && c==' ' && !textto && currentpos==0)
241 continue;
242
243 /* First check control characters */
244 if (c=='\\' && !skipspchar)
245 {
246 c = a[++i];
247
248 switch (c)
249 {
250 case 'n':
251 {
252 c = '\n';
253 break;
254 }
255 case 'B':
256 {
257 currentfont |= BOLD_FONT;
258 goto AddFontCode;
259 }
260 case 'b':
261 {
262 currentfont &= ~BOLD_FONT;
263 goto AddFontCode;
264 }
265 case 'I':
266 {
267 currentfont |= ITALIC_FONT;
268 goto AddFontCode;
269 }
270 case 'i':
271 {
272 currentfont &= ~ITALIC_FONT;
273 goto AddFontCode;
274 }
275 case 'P':
276 {
277 currentfont |= PROP_FONT;
278 goto AddFontCode;
279 }
280 case 'p':
281 {
282 currentfont &= ~PROP_FONT;
283 goto AddFontCode;
284 }
285 case 'U':
286 {
287 currentfont |= UNDERLINE_FONT;
288 goto AddFontCode;
289 }
290 case 'u':
291 {
292 currentfont &= ~UNDERLINE_FONT;
293 AddFontCode:
294 if (!textto)
295 {
296 int m, n;
297 int newfull;
298 double ratio;
299
300 if (!printed_something)
301 {
302 for (m=0; m<plen;)
303 {
304 if (pbuffer[m]==FONT_CHANGE)
305 {
306 for (n=m; n<plen-2; n++)
307 {
308 pbuffer[n] = pbuffer[n+2];
309 }
310 plen-=2;
311 pbuffer[plen] = '\0';
312 lastfont = currentfont;
313 }
314 else if (pbuffer[m]==COLOR_CHANGE)
315 m+=3;
316 else
317 break;
318 }
319 }
320 #ifdef USE_TEXTBUFFER
321 if (hugo_strlen(pbuffer+bufferbreak)==0)
322 bufferfont = currentfont;
323 #endif
324 if (plen >= MAXBUFFER*2-2) FatalError(OVERFLOW_E);
325 pbuffer[plen+2] = '\0';
326 pbuffer[plen+1] = (char)(currentfont+1);
327 pbuffer[plen] = FONT_CHANGE;
328 plen+=2;
329
330 /* Convert full if font height changes, since
331 the amount of used screen real estate for
332 this particular font (in terms of lines)
333 will have changed:
334 */
335 ratio = (double)lineheight;
336 hugo_font(currentfont);
337 ratio /= (double)lineheight;
338 newfull = (int)(((double)full)*ratio+0.5);
339 if (newfull) full = newfull;
340 }
341 continue;
342 }
343 case '_': /* forced space */
344 {
345 if (textto) c = ' ';
346 else c = FORCED_SPACE;
347 break;
348 }
349 default:
350 c = SpecialChar(a, &i);
351 }
352 }
353 else if (game_version<=22)
354 {
355 if (c=='~')
356 c = '\"';
357 else if (c=='^')
358 c = '\n';
359 }
360
361 /* Add the new character */
362
363 /* Text may be sent to an address in the array table instead
364 of being output to the screen
365 */
366 if (textto)
367 {
368 /* space for array length */
369 int n = (game_version>23)?2:0;
370
371 if (c=='\n')
372 {
373 SETMEM(arraytable*16L + textto*2 + n, 0);
374 textto++;
375 }
376 else if ((unsigned char)c >= ' ')
377 {
378 SETMEM(arraytable*16L + textto*2 + n, c);
379 textto++;
380 }
381 /* Add a terminating zero in case we don't
382 print any more to the array */
383 SETMEM(arraytable*16L + textto*2 + n, 0);
384
385 if (i >= alen) return;
386
387 continue; /* back to for (i=0; i<slen; i++) */
388 }
389
390 printed_something = true;
391
392 /* Handle in-text newlines */
393 if (c=='\n')
394 {
395 hugo_font(currentfont = lastfont);
396 #ifdef USE_TEXTBUFFER
397 TB_AddWord(pbuffer+bufferbreak,
398 current_text_x+bufferbreaklen,
399 current_text_y,
400 current_text_x+thisline-1,
401 current_text_y+lineheight-1);
402 #endif
403 Printout(pbuffer);
404 lastfont = currentfont;
405 #ifdef USE_TEXTBUFFER
406 bufferfont = currentfont;
407 bufferbreak = 0;
408 bufferbreaklen = 0;
409 #endif
410 strcpy(pbuffer, "");
411 plen = 0;
412 linebreak = 0;
413 linebreaklen = 0;
414 thisline = 0;
415 #ifdef USE_SMARTFORMATTING
416 leftquote = true;
417 lastc = '\n';
418 #endif
419 pbuffer[plen++] = COLOR_CHANGE;
420 pbuffer[plen++] = (char)(fcolor+1);
421 pbuffer[plen++] = (char)(bgcolor+1);
422 pbuffer[plen] = '\0';
423
424
425 continue;
426 }
427
428 #ifdef USE_SMARTFORMATTING
429 /* Smart formatting only for non-fixed fonts */
430 if ((currentfont & PROP_FONT) && smartformatting)
431 {
432 if ((!strncmp(a+i, "--", 2)) && lastc!='-' && strncmp(a+i, "---", 3))
433 {
434 lastc = '-';
435 c = (char)151;
436 i++;
437 leftquote = false;
438 }
439 else if (c=='\"')
440 {
441 if (leftquote)
442 c = (char)147;
443 else
444 c = (char)148;
445 leftquote = false;
446 lastc = c;
447 }
448 else if (c=='\'')
449 {
450 if (leftquote)
451 c = (char)145;
452 else
453 c = (char)146;
454 leftquote = false;
455 lastc = c;
456 }
457 else
458 {
459 if (c==' ')
460 leftquote = true;
461 else
462 leftquote = false;
463 lastc = c;
464 }
465 }
466 #endif
467
468 /* Add the new character to the printing buffer */
469 if (plen >= MAXBUFFER*2-1) FatalError(OVERFLOW_E);
470 pbuffer[plen+1] = '\0';
471 pbuffer[plen] = c;
472 plen++;
473
474 cwidth = hugo_charwidth(c);
475
476 /* Check to see if we've overrun the current line */
477
478 if (thisline+cwidth+currentpos > physical_windowwidth)
479 {
480 char t;
481
482 if (!linebreak)
483 {
484 linebreak = plen-1;
485 linebreaklen = thisline;
486 }
487
488 t = pbuffer[linebreak];
489 pbuffer[linebreak] = '\0';
490
491 tempfont = currentfont;
492 hugo_font(currentfont = lastfont);
493 Printout(pbuffer);
494 lastfont = currentfont;
495 hugo_font(currentfont = tempfont);
496
497 pbuffer[linebreak] = t;
498 plen = strlen(pbuffer+linebreak);
499 memmove(pbuffer, pbuffer+linebreak, plen + 1);
500 thisline = thisline - linebreaklen;
501 linebreak = 0;
502 linebreaklen = 0;
503 startofline = 0;
504 #ifdef USE_TEXTBUFFER
505 bufferbreak = 0;
506 bufferbreaklen = 0;
507 #endif
508 }
509
510 thisline += cwidth;
511
512 #ifdef USE_TEXTBUFFER
513 if ((c==' ' || c==FORCED_SPACE) ||
514 (c=='/' && a[i+1]!='/') || (c=='-' && a[i+1]!='-'))
515 {
516 TB_AddWord(pbuffer+bufferbreak,
517 current_text_x+bufferbreaklen,
518 current_text_y,
519 current_text_x+thisline-1,
520 current_text_y+lineheight-1);
521
522 bufferbreak = plen;
523 bufferbreaklen = thisline;
524 bufferfont = currentfont;
525 }
526 #endif
527 if ((c==' ') || (c=='/' && a[i+1]!='/') || (c=='-' && a[i+1]!='-'))
528 {
529 linebreak = plen, linebreaklen = thisline;
530 }
531 }
532
533 #ifdef USE_TEXTBUFFER
534 if (!sticky || alen > 1)
535 {
536 tempfont = currentfont;
537 currentfont = bufferfont;
538
539 TB_AddWord(pbuffer+bufferbreak,
540 current_text_x+bufferbreaklen,
541 current_text_y,
542 current_text_x+thisline-1,
543 current_text_y+lineheight-1);
544
545 bufferbreak = plen;
546 bufferbreaklen = thisline;
547 currentfont = tempfont;
548 }
549 #endif
550 if (!sticky)
551 {
552 hugo_font(currentfont = lastfont);
553 Printout(pbuffer);
554 lastfont = currentfont;
555 strcpy(pbuffer, "");
556 linebreak = 0;
557 linebreaklen = 0;
558 thisline = 0;
559 plen = 0;
560 #ifdef USE_TEXTBUFFER
561 bufferbreak = 0;
562 bufferbreaklen = 0;
563 #endif
564 #ifdef USE_SMARTFORMATTING
565 leftquote = true;
566 #endif
567 }
568 }
569
570
571 /* CALLROUTINE
572
573 Used whenever a routine is called, assumes the routine address
574 and begins with the arguments (if any).
575 */
576
577 int CallRoutine(unsigned int addr)
578 {
579 int arg, i;
580 int val;
581 int templocals[MAXLOCALS], temppass[MAXLOCALS];
582 int temp_stack_depth;
583 long tempptr;
584 int potential_tail_recursion = tail_recursion;
585 #if defined (DEBUGGER)
586 int tempdbnest;
587 #endif
588 arg = 0;
589 tail_recursion = 0;
590
591 /* Pass local variables to routine, if specified */
592 if (MEM(codeptr)==OPEN_BRACKET_T)
593 {
594 codeptr++;
595 while (MEM(codeptr) != CLOSE_BRACKET_T)
596 {
597 if (arg)
598 {
599 for (i=0; i<arg; i++)
600 temppass[i] = passlocal[i];
601 }
602
603 passlocal[arg++] = GetValue();
604
605 if (arg > 1)
606 {
607 for (i=0; i<arg-1; i++)
608 passlocal[i] = temppass[i];
609 }
610
611 if (MEM(codeptr)==COMMA_T) codeptr++;
612 }
613 codeptr++;
614 }
615
616 /* TAIL_RECURSION_ROUTINE if we came from a routine call immediately
617 following a 'return' statement...
618 */
619 tail_recursion = potential_tail_recursion;
620 if (tail_recursion==TAIL_RECURSION_ROUTINE && MEM(codeptr)==EOL_T)
621 {
622 tail_recursion_addr = (long)addr*address_scale;
623 PassLocals(arg);
624 return 0;
625 }
626 /* ...but if we're not immediately followed by and end-of-line marker,
627 cancel the pending tail-recursion
628 */
629 else
630 {
631 tail_recursion = 0;
632 }
633
634 for (i=0; i<MAXLOCALS; i++)
635 templocals[i] = var[MAXGLOBALS+i];
636 PassLocals(arg);
637
638 temp_stack_depth = stack_depth;
639
640 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
641
642 tempptr = codeptr; /* store calling address */
643 ret = 0;
644
645 #if defined (DEBUGGER)
646 tempdbnest = dbnest;
647 DebugRunRoutine((long)addr*address_scale);
648 dbnest = tempdbnest;
649 #else
650 RunRoutine((long)addr*address_scale);
651 #endif
652 retflag = 0;
653 val = ret;
654 codeptr = tempptr;
655
656 stack_depth = temp_stack_depth;
657
658 for (i=0; i<MAXLOCALS; i++)
659 var[MAXGLOBALS+i] = templocals[i];
660
661 return val;
662 }
663
664
665 /* CONTEXTCOMMAND
666
667 Adds a command to the context command list. A zero value
668 (i.e., an empty string) resets the list.
669 */
670
671 void ContextCommand(void)
672 {
673 unsigned int n;
674
675 ContextCommandLoop:
676
677 codeptr++;
678
679 n = GetValue();
680 #if !defined (COMPILE_V25)
681 if (n==0)
682 {
683 context_commands = 0;
684 }
685 else if (context_commands < MAX_CONTEXT_COMMANDS)
686 {
687 char *cc;
688
689 strncpy(context_command[context_commands], cc = GetWord(n), 64);
690 context_command[context_commands][63] = '\0';
691 if (strlen(cc)>=64)
692 sprintf(context_command[context_commands]+60, "...");
693 context_commands++;
694 }
695 #endif
696 if (Peek(codeptr)==COMMA_T) goto ContextCommandLoop;
697 codeptr++;
698 }
699
700
701 /* DICT
702
703 Dynamically creates a new dictionary entry.
704 */
705
706 unsigned int Dict()
707 {
708 int i, len = 256;
709 unsigned int arr;
710 unsigned int pos = 2, loc;
711
712 codeptr += 2; /* "(" */
713
714 if (MEM(codeptr)==PARSE_T || MEM(codeptr)==WORD_T)
715 strcpy(line, GetWord(GetValue()));
716 else
717 {
718 /* Get the array address to read the to-be-
719 created dictionary entry from:
720 */
721 arr = GetValue();
722 if (game_version>=22)
723 {
724 /* Convert the address to a word
725 value:
726 */
727 arr*=2;
728
729 if (game_version>=23)
730 /* space for array length */
731 arr+=2;
732 }
733
734 defseg = arraytable;
735 for (i=0; i<len && PeekWord(arr+i*2)!=0; i++)
736 line[i] = (char)PeekWord(arr+i*2);
737 defseg = gameseg;
738 line[i] = '\0';
739 }
740
741 if (Peek(codeptr)==COMMA_T) codeptr++;
742 len = GetValue();
743
744 if ((loc = FindWord(line))!=UNKNOWN_WORD) return loc;
745
746 defseg = dicttable;
747
748 for (i=1; i<=dictcount; i++)
749 pos += Peek(pos) + 1;
750
751 loc = pos - 2;
752
753 if ((long)(pos+strlen(line)) > (long)(codeend-dicttable*16L))
754 {
755 #ifdef DEBUGGER
756 sprintf(debug_line, "$MAXDICTEXTEND dictionary space exceeded");
757 RuntimeWarning(debug_line);
758 #endif
759 defseg = gameseg;
760 return 0;
761 }
762
763 Poke(pos++, (unsigned char)strlen(line));
764 for (i=0; i<(int)strlen(line) && i<len; i++)
765 Poke(pos++, (unsigned char)(line[i]+CHAR_TRANSLATION));
766 PokeWord(0, ++dictcount);
767
768 defseg = gameseg;
769
770 SaveUndo(DICT_T, strlen(line), 0, 0, 0);
771
772 return loc;
773 }
774
775
776 /* FATALERROR */
777
778 void FatalError(int n)
779 {
780 char fatalerrorline[64];
781
782 #if defined (DEBUGGER)
783 hugo_stopmusic();
784 hugo_stopsample();
785
786 /* If the Debugger has already issued an error, it will
787 try to recover instead of issuing a stream of
788 identical errors.
789 */
790 if (runtime_error) return;
791 #else
792 hugo_cleanup_screen();
793 #endif
794
795 switch (n)
796 {
797 case MEMORY_E:
798 {sprintf(line, "Out of memory\n");
799 break;}
800
801 case OPEN_E:
802 {sprintf(line, "Cannot open file\n");
803 break;}
804
805 case READ_E:
806 {sprintf(line, "Cannot read from file\n");
807 break;}
808
809 case WRITE_E:
810 {sprintf(line, "Cannot write to save file\n");
811 break;}
812
813 case EXPECT_VAL_E:
814 {sprintf(line, "Expecting value at $%s\n", PrintHex(codeptr));
815 break;}
816
817 case UNKNOWN_OP_E:
818 {sprintf(line, "Unknown operation at $%s\n", PrintHex(codeptr));
819 break;}
820
821 case ILLEGAL_OP_E:
822 {sprintf(line, "Illegal operation at $%s\n", PrintHex(codeptr));
823 break;}
824
825 case OVERFLOW_E:
826 {sprintf(line, "Overflow at $%s\n", PrintHex(codeptr));
827 break;}
828
829 case DIVIDE_E:
830 {sprintf(line, "Divide by zero at $%s\n", PrintHex(codeptr));
831 break;}
832 }
833
834 #if defined (DEBUGGER)
835
836 if (routines > 0)
837 {
838 SwitchtoDebugger();
839
840 if (n==MEMORY_E) DebuggerFatal(D_MEMORY_ERROR);
841
842 RuntimeWarning(line);
843 debugger_interrupt = true;
844 debugger_skip = true;
845 runtime_error = true;
846
847 if (n!=EXPECT_VAL_E)
848 RecoverLastGood();
849
850 codeptr = this_codeptr;
851
852 return;
853 }
854
855 hugo_cleanup_screen();
856 #endif
857
858 /* crash dump */
859 /*
860 if (n==UNKNOWN_OP_E || n==ILLEGAL_OP_E || n==EXPECT_VAL_E || n==OVERFLOW_E)
861 {
862 for (n=-8; n<0; n++)
863 fprintf(stderr, " %c%2X", (n==-8)?'$':' ', MEM(codeptr+n));
864 fprintf(stderr, "\n");
865 for (n=-8; n<0; n++)
866 fprintf(stderr, " %3d", MEM(codeptr+n));
867 fprintf(stderr, "\n\n> %2X", MEM(codeptr));
868 for (n=1; n<8; n++)
869 fprintf(stderr, " %2X", MEM(codeptr+n));
870 fprintf(stderr, "\n");
871 fprintf(stderr, ">%3d", MEM(codeptr));
872 for (n=1; n<8; n++)
873 fprintf(stderr, " %3d", MEM(codeptr+n));
874 fprintf(stderr, "\n");
875 }
876 */
877 sprintf(fatalerrorline, "\nFatal Error: %s", line);
878 PRINTFATALERROR(fatalerrorline);
879
880 hugo_closefiles();
881 hugo_blockfree(mem);
882 mem = NULL;
883 exit(n);
884 }
885
886
887 /* FILEIO */
888
889 void FileIO(void)
890 {
891 #ifndef GLK
892 char drive[MAXDRIVE], dir[MAXDIR], fname[MAXFILENAME], ext[MAXEXT];
893 #endif
894 char fileiopath[MAXPATH];
895 char iotype;
896 unsigned int fnameval;
897 long skipaddr;
898 int i, temp_stack_depth = stack_depth;
899 #if defined (DEBUGGER)
900 int tempdbnest;
901 #endif
902
903 iotype = MEM(codeptr++);
904 skipaddr = (long)PeekWord(codeptr)*address_scale;
905 codeptr+=2;
906 fnameval = GetValue();
907 if (game_version>=23) codeptr++; /* eol */
908
909 ioerror = 0;
910
911 /* Make sure the filename is legal, 8 alphanumeric characters or less */
912 strcpy(line, GetWord(fnameval));
913 if (strlen(line) > 8) goto LeaveFileIO;
914 for (i=0; i<(int)strlen(line); i++)
915 {
916 if ((line[i]>='0' && line[i]<='9') || (line[i]>='A' && line[i]<='Z') ||
917 (line[i]>='a' && line[i]<='z'))
918 {
919 continue;
920 }
921 else
922 goto LeaveFileIO;
923 }
924
925 if (ioblock) goto LeaveFileIO; /* can't nest file operations */
926
927 #if !defined (GLK)
928 hugo_splitpath(program_path, drive, dir, fname, ext);
929 hugo_makepath(fileiopath, drive, dir, GetWord(fnameval), "");
930 #else
931 strcpy(fileiopath, GetWord(fnameval));
932 #endif
933
934 if (iotype==WRITEFILE_T) /* "writefile" */
935 {
936 #if !defined (GLK)
937 /* stdio implementation */
938 if ((io = HUGO_FOPEN(fileiopath, "wb"))==NULL) goto LeaveFileIO;
939 #else
940 /* Glk implementation */
941 frefid_t fref = NULL;
942
943 fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode,
944 fileiopath, 0);
945 io = glk_stream_open_file(fref, filemode_Write, 0);
946 glk_fileref_destroy(fref);
947 if (io==NULL) goto LeaveFileIO;
948 #endif
949 ioblock = 1;
950 }
951 else /* "readfile" */
952 {
953 #if !defined (GLK)
954 /* stdio implementation */
955 if ((io = HUGO_FOPEN(fileiopath, "rb"))==NULL) goto LeaveFileIO;
956 #else
957 /* Glk implementation */
958 frefid_t fref = NULL;
959
960 fref = glk_fileref_create_by_name(fileusage_Data | fileusage_BinaryMode,
961 fileiopath, 0);
962 if (glk_fileref_does_file_exist(fref))
963 {
964 io = glk_stream_open_file(fref, filemode_Read, 0);
965 }
966 else
967 {
968 io = NULL;
969 }
970 glk_fileref_destroy(fref);
971 if (io==NULL) goto LeaveFileIO;
972 #endif
973 ioblock = 2;
974 }
975
976 #if defined (DEBUGGER)
977 tempdbnest = dbnest++;
978 #endif
979
980 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
981
982 RunRoutine(codeptr);
983
984 #if defined (DEBUGGER)
985 dbnest = tempdbnest;
986 #endif
987
988 stack_depth = temp_stack_depth;
989
990 if (ioerror) retflag = 0;
991
992 fclose(io);
993 io = NULL;
994 ioblock = 0;
995
996 LeaveFileIO:
997 ioerror = 0;
998 codeptr = skipaddr;
999 }
1000
1001
1002 /* FLUSHPBUFFER */
1003
1004 void Flushpbuffer()
1005 {
1006 if (pbuffer[0]=='\0') return;
1007
1008 #ifdef USE_TEXTBUFFER
1009 /* For (single) characters left over from AP(), when Flushpbuffer() gets
1010 called, say, from RunWindow()
1011 */
1012 if (bufferbreaklen && pbuffer+bufferbreaklen && !(currentfont&PROP_FONT))
1013 {
1014 TB_AddWord(pbuffer+bufferbreak,
1015 current_text_x+bufferbreaklen,
1016 current_text_y,
1017 current_text_x+bufferbreaklen+FIXEDCHARWIDTH,
1018 current_text_y+lineheight-1);
1019 }
1020 #endif
1021
1022 pbuffer[strlen(pbuffer)+1] = '\0';
1023 pbuffer[strlen(pbuffer)] = (char)NO_NEWLINE;
1024 Printout(Ltrim(pbuffer));
1025 currentpos = hugo_textwidth(pbuffer); /* -charwidth; */
1026 strcpy(pbuffer, "");
1027 }
1028
1029
1030 /* GETCOMMAND */
1031
1032 void GetCommand(void)
1033 {
1034 char a[256];
1035 #ifdef USE_TEXTBUFFER
1036 int start, width, i, y;
1037 #endif
1038 Flushpbuffer();
1039 AP("");
1040
1041 hugo_settextcolor(fcolor);
1042 hugo_setbackcolor(bgcolor);
1043 if (icolor==-1)
1044 icolor = fcolor; /* check unset input color */
1045
1046 strncpy(a, GetWord(var[prompt]), 255);
1047 during_player_input = true;
1048 full = 0;
1049 #ifdef USE_TEXTBUFFER
1050 /* Add the prompt to the textbuffer (using TB_Add()) */
1051 y = current_text_y;
1052 width = hugo_textwidth(GetWord(var[prompt]));
1053 TB_AddWord(GetWord(var[prompt]), physical_windowleft, y,
1054 physical_windowleft+width, y+lineheight-1);
1055
1056 hugo_getline(a);
1057
1058 /* If hugo_scrollwindowup() called by hugo_getline() shifted things */
1059 if (current_text_y > y)
1060 {
1061 y += (current_text_y - y);
1062 }
1063
1064 /* Add each word in the input buffer */
1065 start = 0;
1066 for (i=0; i<(int)strlen(buffer); i++)
1067 {
1068 if (buffer[i]==' ')
1069 {
1070 buffer[i] = '\0';
1071 TB_AddWord(buffer+start, physical_windowleft+width, y-lineheight,
1072 physical_windowleft+width+hugo_textwidth(buffer+start), y-1);
1073 width += hugo_textwidth(buffer+start) + hugo_textwidth(" ");
1074 start = i+1;
1075 buffer[i] = ' ';
1076 }
1077 }
1078 /* Add the final word */
1079 TB_AddWord(buffer+start, physical_windowleft+width, y-lineheight,
1080 physical_windowleft+width+hugo_textwidth(buffer+start), y-1);
1081 #else
1082 hugo_getline(a);
1083 #endif
1084 during_player_input = false;
1085 strcpy(buffer, Rtrim(buffer));
1086
1087 strcpy(parseerr, "");
1088
1089 full = 1;
1090 remaining = 0;
1091 }
1092
1093
1094 /* GETSTRING
1095
1096 From any address <addr>; the segment must be defined prior to
1097 calling the function.
1098 */
1099
1100 char *GetString(long addr)
1101 {
1102 static char a[256];
1103 int i, length;
1104
1105 length = Peek(addr);
1106
1107 for (i=1; i<=length; i++)
1108 a[i-1] = (char)(Peek(addr + i) - CHAR_TRANSLATION);
1109 a[i-1] = '\0';
1110
1111 return a;
1112 }
1113
1114
1115 /* GETTEXT
1116
1117 Get text block from position <textaddr> in the text bank. If
1118 the game was not fully loaded in memory, i.e., if loaded_in_memory
1119 is not true, the block is read from disk.
1120 */
1121
1122 char *GetText(long textaddr)
1123 {
1124 static char g[1025];
1125 int i, a;
1126 int tdatal, tdatah, tlen; /* low byte, high byte, length */
1127
1128
1129 /* Read the string from memory... */
1130 if (loaded_in_memory)
1131 {
1132 tlen = MEM(codeend+textaddr) + MEM(codeend+textaddr+1)*256;
1133 for (i=0; i<tlen; i++)
1134 {
1135 g[i] = (char)(MEM(codeend+textaddr+2+i) - CHAR_TRANSLATION);
1136 }
1137 g[i] = '\0';
1138
1139 return g;
1140 }
1141
1142 /* ...Or load the string from disk */
1143 if (fseek(game, codeend+textaddr, SEEK_SET)) FatalError(READ_E);
1144
1145 tdatal = fgetc(game);
1146 tdatah = fgetc(game);
1147 if (tdatal==EOF || tdatah==EOF || ferror(game)) FatalError(READ_E);
1148
1149 tlen = tdatal + tdatah * 256;
1150
1151 for (i=0; i<tlen; i++)
1152 {
1153 if ((a = fgetc(game))==EOF) FatalError(READ_E);
1154 g[i] = (char)(a - CHAR_TRANSLATION);
1155 }
1156 g[i] = '\0';
1157
1158 return g;
1159 }
1160
1161
1162 /* GETWORD
1163
1164 From the dictionary table.
1165 */
1166
1167 char *GetWord(unsigned int w)
1168 {
1169 static char *b;
1170 unsigned short a;
1171
1172 a = w;
1173
1174 if (a==0) return "";
1175
1176 if (a==PARSE_STRING_VAL) return parseerr;
1177 if (a==SERIAL_STRING_VAL) return serial;
1178
1179 /* bounds-checking to avoid some sort of memory arena error */
1180 if ((long)(a+dicttable*16L) > codeend)
1181 {
1182 b = "";
1183 return b;
1184 }
1185
1186 defseg = dicttable;
1187 b = GetString((long)a + 2);
1188 defseg = gameseg;
1189
1190 return b;
1191 }
1192
1193
1194 /* HANDLETAILRECURSION */
1195
1196 void HandleTailRecursion(long addr)
1197 {
1198 codeptr = addr;
1199
1200 /* Set up proper default return value for property or routine */
1201 if (tail_recursion==TAIL_RECURSION_PROPERTY)
1202 ret = 1;
1203 else
1204 ret = 0;
1205
1206 /* Unstack until we get to any routine call that got us here */
1207 while (stack_depth)
1208 {
1209 if (code_block[stack_depth].type==RUNROUTINE_BLOCK)
1210 break;
1211 stack_depth--;
1212 }
1213
1214 #ifdef DEBUGGER
1215 currentroutine = (unsigned int)(codeptr/address_scale);
1216 call[window[VIEW_CALLS].count-1].addr = currentroutine;
1217 call[window[VIEW_CALLS].count-1].param = true;
1218
1219 sprintf(debug_line, "Calling: %s", RoutineName(currentroutine));
1220 /* Don't duplicate blank separator line in code window */
1221 if (codeline[window[CODE_WINDOW].count-1][0] != 0)
1222 AddStringtoCodeWindow("");
1223 AddStringtoCodeWindow(debug_line);
1224
1225 /* Adjust for very long runs */
1226 dbnest--;
1227 #endif
1228
1229 tail_recursion = 0;
1230 tail_recursion_addr = 0;
1231 }
1232
1233
1234 /* INITGAME */
1235
1236 void InitGame(void)
1237 {
1238 int i;
1239
1240 /* Stop any audio if this is a restart */
1241 hugo_stopsample();
1242 hugo_stopmusic();
1243
1244 #if !defined (COMPILE_V25)
1245 hugo_stopvideo();
1246 context_commands = 0;
1247 #endif
1248 game_reset = false;
1249
1250 SetStackFrame(stack_depth, RUNROUTINE_BLOCK, 0, 0);
1251
1252 /* Figure out which objects have either a noun or an adjective property;
1253 store it in obj_parselist, one bit per object */
1254 if ((!obj_parselist) && (obj_parselist = (char *)hugo_blockalloc(sizeof(char)*((objects+7)/8))))
1255 {
1256
1257 for (i=0; i<objects; i++)
1258 {
1259 if (i%8==0) obj_parselist[i/8] = 0;
1260
1261 if (PropAddr(i, adjective, 0) || PropAddr(i, noun, 0))
1262 obj_parselist[i/8] |= 1 << (i%8);
1263 else
1264 obj_parselist[i/8] &= ~(1 << (i%8));
1265 }
1266 }
1267
1268 #if defined (DEBUGGER)
1269 for (i=0; i<MAXLOCALS; i++) strcpy(localname[i], "");
1270 window[VIEW_LOCALS].count = current_locals = 0;
1271
1272 PassLocals(0);
1273 DebugRunRoutine((long)initaddr*address_scale);
1274 #else
1275 PassLocals(0);
1276 RunRoutine((long)initaddr*address_scale);
1277 #endif
1278
1279 ret = 0;
1280 retflag = 0;
1281 var[actor] = var[player];
1282 }
1283
1284
1285 /* LOADGAME */
1286
1287 void LoadGame(void)
1288 {
1289 int i, data;
1290 unsigned int synptr;
1291 size_t ccount;
1292 int check_version;
1293 long textbank, filelength;
1294 #ifndef LOADGAMEDATA_REPLACED
1295 long c;
1296 #endif
1297
1298 #if defined (DEBUGGER)
1299 if (!strcmp(gamefile, ""))
1300 {
1301 game = NULL;
1302 strcpy(gamefile, "(no file)");
1303 return;
1304 }
1305 #endif
1306
1307 #if !defined (GLK) /* since in Glk the game stream is always open */
1308 if ((game = TrytoOpen(gamefile, "rb", "games"))==NULL)
1309 {
1310 if ((game = TrytoOpen(gamefile, "rb", "object"))==NULL)
1311 FatalError(OPEN_E);
1312 }
1313 #endif
1314
1315 fseek(game, 0, SEEK_END);
1316 filelength = ftell(game);
1317 fseek(game, 0, SEEK_SET);
1318
1319 if (ferror(game)) FatalError(READ_E);
1320 if ((game_version = fgetc(game))==EOF) FatalError(OPEN_E);
1321
1322 /* Earlier versions of the compiler wrote the version code as
1323 1 or 2 instead of 10 or 20.
1324 */
1325 if (game_version==1 || game_version==2)
1326 game_version*=10;
1327
1328 if (game_version < 21) object_size = 12;
1329
1330 if (game_version < 31) address_scale = 4;
1331
1332 check_version = HEVERSION*10 + HEREVISION;
1333 #if defined (COMPILE_V25)
1334 if (check_version==25 && (game_version>=30 && game_version<=39))
1335 check_version = game_version;
1336 #endif
1337
1338 defseg = gameseg;
1339
1340 if (game_version < HEVERSION)
1341 {
1342 #if defined (DEBUGGER)
1343 hugo_cleanup_screen();
1344 hugo_clearfullscreen();
1345 #endif
1346 sprintf(line, "Hugo Compiler v%d.%d or later required.\n", HEVERSION, HEREVISION);
1347 if (game_version>0)
1348 sprintf(line+strlen(line), "File \"%s\" is v%d.%d.\n", gamefile, game_version/10, game_version%10);
1349
1350 #if defined (DEBUGGER_PRINTFATALERROR)
1351 DEBUGGER_PRINTFATALERROR(line);
1352 #else
1353 printf("%s", line);
1354 #endif
1355 hugo_closefiles();
1356 hugo_blockfree(mem);
1357 mem = NULL;
1358
1359 exit(OPEN_E);
1360 }
1361 else if (game_version > check_version)
1362 {
1363 #if defined (DEBUGGER)
1364 hugo_cleanup_screen();
1365 hugo_clearfullscreen();
1366 #endif
1367 sprintf(line, "File \"%s\" is incorrect or unknown version.\n", gamefile);
1368
1369 #if defined (DEBUGGER_PRINTFATALERROR)
1370 DEBUGGER_PRINTFATALERROR(line);
1371 #else
1372 printf("%s", line);
1373 #endif
1374 hugo_closefiles();
1375 hugo_blockfree(mem);
1376 mem = NULL;
1377 exit(OPEN_E); /* ditto */
1378 }
1379
1380 hugo_settextpos(1, physical_windowheight/lineheight);
1381
1382 if (game_version>=25)
1383 fseek(game, H_TEXTBANK, SEEK_SET);
1384 else
1385 /* Because pre-v2.5 didn't have performaddr in the header */
1386 fseek(game, H_TEXTBANK-2L, SEEK_SET);
1387
1388 data = fgetc(game);
1389 textbank = fgetc(game);
1390 if (data==EOF || textbank==EOF || ferror(game)) FatalError(READ_E);
1391 textbank = (textbank*256L + (long)data) * 16L;
1392 codeend = textbank;
1393
1394 /* Use a 1024-byte read block */
1395 ccount = 1024;
1396
1397 if (fseek(game, 0, SEEK_SET)) FatalError(READ_E);
1398
1399 #ifndef LOADGAMEDATA_REPLACED
1400 /* Allocate as much memory as is required */
1401 if ((!loaded_in_memory) || (mem = (unsigned char *)hugo_blockalloc(filelength))==NULL)
1402 {
1403 loaded_in_memory = 0;
1404 if ((mem = (unsigned char *)hugo_blockalloc(codeend))==NULL)
1405 FatalError(MEMORY_E);
1406 }
1407
1408 c = 0;
1409
1410 /* Load either the entire file or just up to the start of the
1411 text bank
1412 */
1413 while (c < (loaded_in_memory ? filelength:codeend))
1414 {
1415 /* Complicated, but basically just makes sure that
1416 the last read (whether loaded_in_memory or not)
1417 doesn't override the end of the file. Shouldn't
1418 normally be a problem for fread(), but it caused
1419 a crash under MSVC++.
1420 */
1421 i = fread((unsigned char *)&mem[c], sizeof(unsigned char),
1422 (loaded_in_memory)?
1423 ((filelength-c>(long)ccount)?ccount:(size_t)(filelength-c)):
1424 ((codeend-c>(long)ccount)?ccount:(size_t)(codeend-c)),
1425 game);
1426
1427 if (!i) break;
1428 c += i;
1429 }
1430 #else
1431 if (!LoadGameData(false)) FatalError(READ_E);
1432 #endif
1433
1434 if (ferror(game)) FatalError(READ_E);
1435
1436 defseg = gameseg;
1437
1438 /* Read header: */
1439
1440 id[0] = Peek(H_ID);
1441 id[1] = Peek(H_ID+1);
1442 id[2] = '\0';
1443
1444 for (i=0; i<8; i++)
1445 serial[i] = Peek(H_SERIAL+i);
1446 serial[8] = '\0';
1447
1448 codestart = PeekWord(H_CODESTART);
1449 objtable = PeekWord(H_OBJTABLE) + gameseg;
1450 proptable = PeekWord(H_PROPTABLE) + gameseg;
1451 eventtable = PeekWord(H_EVENTTABLE) + gameseg;
1452 arraytable = PeekWord(H_ARRAYTABLE) + gameseg;
1453 dicttable = PeekWord(H_DICTTABLE) + gameseg;
1454 syntable = PeekWord(H_SYNTABLE) + gameseg;
1455
1456 initaddr = PeekWord(H_INIT);
1457 mainaddr = PeekWord(H_MAIN);
1458 parseaddr = PeekWord(H_PARSE);
1459 parseerroraddr = PeekWord(H_PARSEERROR);
1460 findobjectaddr = PeekWord(H_FINDOBJECT);
1461 endgameaddr = PeekWord(H_ENDGAME);
1462 speaktoaddr = PeekWord(H_SPEAKTO);
1463 performaddr = PeekWord(H_PERFORM);
1464
1465
1466 /* Read totals: */
1467
1468 defseg = objtable;
1469 objects = PeekWord(0);
1470
1471 defseg = eventtable;
1472 events = PeekWord(0);
1473
1474 defseg = dicttable;
1475 dictcount = PeekWord(0);
1476
1477 defseg = syntable;
1478 syncount = PeekWord(0);
1479
1480
1481 /* Additional information to be found: */
1482
1483 /* display object */
1484 if (game_version >= 24)
1485 {
1486 data = FindWord("(display)");
1487
1488 for (i=0; i<objects; i++)
1489 {
1490 if (GetProp(i, 0, 1, true)==data)
1491 {
1492 display_object = i;
1493 break;
1494 }
1495 }
1496 }
1497
1498 /* build punctuation string (additional user-specified punctuation) */
1499 synptr = 2;
1500 strcpy(punc_string, "");
1501 for (i=1; i<=syncount; i++)
1502 {
1503 defseg = syntable;
1504 if (Peek(synptr)==3) /* 3 = punctuation */
1505 {
1506 strcpy(line, GetWord(PeekWord(synptr+1)));
1507 if (strlen(line) + strlen(punc_string) > 63) break;
1508 strcat(punc_string, line);
1509 }
1510 synptr+=5;
1511 }
1512 }
1513
1514
1515 /* PARSECOMMANDLINE */
1516
1517 #if !defined (GLK) /* ParseCommandLine() is omitted for Glk */
1518
1519 signed char def_fcolor = DEF_FCOLOR;
1520 signed char def_bgcolor = DEF_BGCOLOR;
1521 signed char def_slfcolor = DEF_SLFCOLOR;
1522 signed char def_slbgcolor = DEF_SLBGCOLOR;
1523
1524 void ParseCommandLine(int argc, char *argv[])
1525 {
1526 char drive[MAXDRIVE], dir[MAXDIR], fname[MAXFILENAME], ext[MAXEXT];
1527 char* game_file_arg = NULL;
1528
1529 #if defined(GCC_UNIX) && defined(DO_COLOR)
1530 int ch;
1531 /* Parse comand line options (colour switches) */
1532 while ((ch = getopt(argc, argv, "f:b:F:B:?h")) != -1) {
1533 switch (ch) {
1534 case 'f':
1535 def_fcolor = atoi(optarg);
1536 break;
1537 case 'b':
1538 def_bgcolor = atoi(optarg);
1539 break;
1540 case 'F':
1541 def_slfcolor = atoi(optarg);
1542 break;
1543 case 'B':
1544 def_slbgcolor = atoi(optarg);
1545 break;
1546 case 'h':
1547 case '?':
1548 default:
1549 Banner();
1550 if (mem) hugo_blockfree(mem);
1551 mem = NULL;
1552 exit(0);
1553 }
1554 }
1555 if ( optind < argc ) {
1556 game_file_arg = argv[optind];
1557 }
1558 #else
1559 if (argc>1) {
1560 game_file_arg = argv[1];
1561 }
1562 #endif
1563
1564 if (game_file_arg==NULL)
1565 {
1566 Banner();
1567 if (mem) hugo_blockfree(mem);
1568 mem = NULL;
1569 exit(0);
1570 }
1571
1572 hugo_splitpath(game_file_arg, drive, dir, fname, ext);
1573
1574 if (strcmp(ext, ""))
1575 strcpy(gamefile, game_file_arg);
1576 else
1577 hugo_makepath(gamefile, drive, dir, fname,
1578 #if defined (DEBUGGER)
1579 "hdx");
1580 #else
1581 "hex");
1582 #endif
1583
1584 if (getenv("HUGO_SAVE"))
1585 hugo_makepath(savefile, "", getenv("HUGO_SAVE"), fname, "sav");
1586 else
1587 hugo_makepath(savefile, drive, dir, fname, "sav");
1588
1589 if (getenv("HUGO_RECORD"))
1590 hugo_makepath(recordfile, "", getenv("HUGO_RECORD"), fname, "rec");
1591 else
1592 hugo_makepath(recordfile, drive, dir, fname, "rec");
1593
1594 strcpy(scriptfile, DEF_PRN);
1595
1596 hugo_makepath(gamepath, drive, dir, "", "");
1597 }
1598
1599 #endif /* GLK */
1600
1601
1602 /* PASSLOCALS
1603
1604 Must be called before running every new routine, i.e. before
1605 calling RunRoutine(). Unfortunately, the current locals must
1606 be saved in a temp array prior to calling. The argument n
1607 gives the number of arguments passed.
1608 */
1609
1610 void PassLocals(int n)
1611 {
1612 int i;
1613
1614 for (i=0; i<MAXLOCALS; i++)
1615 {
1616 var[MAXGLOBALS+i] = passlocal[i];
1617 passlocal[i] = 0;
1618 }
1619 arguments_passed = n;
1620 }
1621
1622
1623 #ifdef NO_INLINE_MEM_FUNCTIONS
1624
1625 /* PEEK */
1626
1627 unsigned char Peek(long a)
1628 {
1629 return MEM(defseg * 16L + a);
1630 }
1631
1632
1633 /* PEEKWORD */
1634
1635 unsigned int PeekWord(long a)
1636 {
1637 return (unsigned char)MEM(defseg*16L+a) + (unsigned char)MEM(defseg*16L+a+1)*256;
1638 }
1639
1640
1641 /* POKE */
1642
1643 void Poke(unsigned int a, unsigned char v)
1644 {
1645 SETMEM(defseg * 16L + a, v);
1646 }
1647
1648
1649 /* POKEWORD */
1650
1651 void PokeWord(unsigned int a, unsigned int v)
1652 {
1653 SETMEM(defseg * 16L + a, (char)(v%256));
1654 SETMEM(defseg * 16L + a + 1, (char)(v/256));
1655 }
1656
1657 #endif /* NO_INLINED_MEM_FUNCTIONS */
1658
1659
1660 /* PRINTHEX
1661
1662 Returns <a> as a hex-number string in XXXXXX format.
1663 */
1664
1665 char *PrintHex(long a)
1666 {
1667 static char hex[7];
1668 int h = 0;
1669
1670 strcpy(hex, "");
1671
1672 if (a < 0L) a = 0;
1673
1674 hex[h++] = '0';
1675 if (a < 65536L) hex[h++] = '0';
1676 if (a < 4096L) hex[h++] = '0';
1677 if (a < 256L) hex[h++] = '0';
1678 if (a < 16L) hex[h++] = '0';
1679
1680 sprintf(hex+h, "%lX", a);
1681
1682 return hex;
1683 }
1684
1685
1686 /* PRINTOUT
1687
1688 Print to client display taking into account cursor relocation,
1689 font changes, color setting, and window scrolling.
1690 */
1691
1692 void Printout(char *a)
1693 {
1694 char b[2], sticky = 0, trimmed = 0;
1695 char tempfcolor;
1696 int i, l;
1697 int n;
1698 int last_printed_font = currentfont;
1699
1700 /* hugo_font() should do this if necessary, but just in case */
1701 if (lineheight < FIXEDLINEHEIGHT)
1702 lineheight = FIXEDLINEHEIGHT;
1703
1704 tempfcolor = fcolor;
1705
1706 /* The before-check of the linecount: */
1707 if (full)
1708 {
1709 /* -1 here since it's before printing */
1710 if (full >= physical_windowheight/lineheight-1)
1711 PromptMore();
1712 }
1713
1714 if ((a[0]!='\0') && a[strlen(a)-1]==(char)NO_NEWLINE)
1715 {
1716 a[strlen(a)-1] = '\0';
1717 sticky = true;
1718 }
1719
1720 b[0] = b[1] = '\0';
1721
1722
1723 /* The easy part is just skimming <a> and processing each code
1724 or printed character, as the case may be:
1725 */
1726
1727 l = 0; /* physical length of string */
1728
1729 for (i=0; i<(int)strlen(a); i++)
1730 {
1731 if ((a[i]==' ') && !trimmed && currentpos==0)
1732 {
1733 continue;
1734 }
1735
1736 if ((unsigned char)a[i] > ' ' || a[i]==FORCED_SPACE)
1737 {
1738 trimmed = true;
1739 last_printed_font = currentfont;
1740 }
1741
1742 switch (b[0] = a[i])
1743 {
1744 case FONT_CHANGE:
1745 n = (int)(a[++i]-1);
1746 if (currentfont != n)
1747 hugo_font(currentfont = n);
1748 break;
1749
1750 case COLOR_CHANGE:
1751 fcolor = (char)(a[++i]-1);
1752 hugo_settextcolor((int)fcolor);
1753 hugo_setbackcolor((int)(a[++i]-1));
1754 hugo_font(currentfont);
1755 break;
1756
1757 default:
1758 if (b[0]==FORCED_SPACE) b[0] = ' ';
1759 l += hugo_charwidth(b[0]);
1760
1761 /* A minor adjustment for font changes and RunWindow() to make
1762 sure we're not printing unnecessarily downscreen
1763 */
1764 if ((just_left_window) && current_text_y > physical_windowbottom-lineheight)
1765 {
1766 current_text_y = physical_windowbottom-lineheight;
1767 }
1768 just_left_window = false;
1769
1770 hugo_print(b);
1771 }
1772
1773 if (script && (unsigned char)b[0]>=' ')
1774 /* fprintf() this way for Glk */
1775 if (fprintf(script, "%s", b) < 0) FatalError(WRITE_E);
1776
1777 #if defined (SCROLLBACK_DEFINED)
1778 if (!inwindow && (unsigned char)b[0]>=' ')
1779 {
1780 #ifdef USE_SMARTFORMATTING
1781 /* Undo smart-formatting for ASCII scrollback */
1782 switch ((unsigned char)b[0])
1783 {
1784 case 151:
1785 hugo_sendtoscrollback("--");
1786 continue;
1787 case 145:
1788 case 146:
1789 b[0] = '\'';
1790 break;
1791 case 147:
1792 case 148:
1793 b[0] = '\"';
1794 }
1795 #endif
1796 hugo_sendtoscrollback(b);
1797 }
1798 #endif
1799 }
1800
1801 /* If we've got a linefeed and didn't hit the right edge of the
1802 window
1803 */
1804 #ifdef NO_TERMINAL_LINEFEED
1805 if (!sticky)
1806 #else
1807 if (!sticky && currentpos+l < physical_windowwidth)
1808 #endif
1809 {
1810 /* The background color may have to be temporarily set if we're
1811 not in a window--the reason is that full lines of the
1812 current background color might be printed by the OS-specific
1813 scrolling function. (This behavior is overridden by the
1814 Hugo Engine for in-window printing, which always adds new
1815 lines in the current background color when scrolling.)
1816 */
1817 hugo_setbackcolor((inwindow)?bgcolor:default_bgcolor);
1818 hugo_print("\r");
1819
1820 i = currentfont;
1821 hugo_font(currentfont = last_printed_font);
1822
1823 #ifndef GLK
1824 if (currentline > physical_windowheight/lineheight)
1825 {
1826 int full_limit = physical_windowheight/lineheight;
1827
1828 hugo_scrollwindowup();
1829
1830 if ((current_text_y)
1831 && full >= full_limit-3
1832 && physical_windowbottom-current_text_y-lineheight > lineheight/2)
1833 {
1834 PromptMore();
1835 }
1836 currentline = full_limit;
1837 }
1838
1839 /* Don't scroll single-line windows before PromptMore() */
1840 else if (physical_windowheight/lineheight > 1)
1841 #endif
1842 {
1843 hugo_print("\n");
1844 }
1845
1846 hugo_font(currentfont = i);
1847 hugo_setbackcolor(bgcolor);
1848 }
1849
1850 #if defined (AMIGA)
1851 else
1852 {
1853 if (currentpos + l >= physical_windowwidth)
1854 AmigaForceFlush();
1855 }
1856 #endif
1857 just_left_window = false;
1858
1859 /* If no newline is to be printed after the current line: */
1860 if (sticky)
1861 {
1862 currentpos += l;
1863 }
1864
1865 /* Otherwise, take care of all the line-feeding, line-counting,
1866 etc.
1867 */
1868 else
1869 {
1870 currentpos = 0;
1871 if (currentline++ > physical_windowheight/lineheight)
1872 currentline = physical_windowheight/lineheight;
1873
1874 if (!playback) skipping_more = false;
1875
1876 ++full;
1877
1878 /* The after-check of the linecount: */
1879 if ((full) && full >= physical_windowheight/lineheight)
1880 {
1881 PromptMore();
1882 }
1883
1884 if (script)
1885 {
1886 /* fprintf() this way for Glk */
1887 if (fprintf(script, "%s", "\n")<0)
1888 FatalError(WRITE_E);
1889 }
1890
1891 #if defined (SCROLLBACK_DEFINED)
1892 if (!inwindow) hugo_sendtoscrollback("\n");
1893 #endif
1894 }
1895
1896 fcolor = tempfcolor;
1897 }
1898
1899
1900 /* PROMPTMORE */
1901
1902 #ifndef PROMPTMORE_REPLACED
1903
1904 void PromptMore(void)
1905 {
1906 char temp_during_player_input;
1907 int k, tempcurrentfont;
1908 int temp_current_text_y = current_text_y;
1909
1910 if (playback && skipping_more)
1911 {
1912 full = 0;
1913 return;
1914 }
1915 skipping_more = false;
1916
1917 /* Clear the key buffer */
1918 while (hugo_iskeywaiting()) hugo_getkey();
1919
1920 temp_during_player_input = during_player_input;
1921 during_player_input = false;
1922
1923 tempcurrentfont = currentfont;
1924 hugo_font(currentfont = NORMAL_FONT);
1925
1926 hugo_settextpos(1, physical_windowheight/lineheight);
1927
1928 #ifdef NO_TERMINAL_LINEFEED
1929 /* For ports where it's possible, do a better "MORE..." prompt
1930 without a flashing caret */
1931 if (default_bgcolor!=DEF_SLBGCOLOR)
1932 {
1933 /* system statusline colors */
1934 hugo_settextcolor(DEF_SLFCOLOR);
1935 hugo_setbackcolor(DEF_SLBGCOLOR);
1936 }
1937 else
1938 {
1939 /* system colors */
1940 if (DEF_FCOLOR < 8)
1941 hugo_settextcolor(DEF_FCOLOR+8); /* bright */
1942 else
1943 hugo_settextcolor(DEF_FCOLOR);
1944 hugo_setbackcolor(DEF_BGCOLOR);
1945 }
1946
1947 if (current_text_y)
1948 current_text_y = physical_windowbottom - lineheight;
1949
1950 /* Make sure we fit in a window */
1951 if (physical_windowwidth/FIXEDCHARWIDTH >= 19)
1952 hugo_print(" [MORE...] ");
1953 else
1954 hugo_print("[MORE...]");
1955
1956 if (!inwindow)
1957 hugo_setbackcolor(default_bgcolor);
1958 else
1959 hugo_setbackcolor(bgcolor);
1960
1961 /* Note that hugo_iskeywaiting() must flush the printing buffer,
1962 if one is being employed */
1963 while (!hugo_iskeywaiting())
1964 {
1965 hugo_timewait(100);
1966 }
1967
1968 k = hugo_waitforkey();
1969
1970 #else
1971 /* The normal, with-caret way of doing it */
1972 /* system colors */
1973 hugo_settextcolor(16); /* DEF_FCOLOR); */
1974 hugo_setbackcolor(17); /* DEF_BGCOLOR); */
1975 hugo_print("[MORE...]");
1976
1977 k = hugo_waitforkey();
1978
1979 if (!inwindow)
1980 hugo_setbackcolor(default_bgcolor);
1981 else
1982 hugo_setbackcolor(bgcolor);
1983 #endif
1984
1985 if (playback && k==27) /* if ESC is pressed during playback */
1986 {
1987 if (fclose(playback))
1988 FatalError(READ_E);
1989 playback = NULL;
1990 }
1991 else if (playback && k=='+')
1992 skipping_more = true;
1993
1994 hugo_settextpos(1, physical_windowheight/lineheight);
1995 #ifdef NO_TERMINAL_LINEFEED
1996 current_text_y = physical_windowbottom - lineheight;
1997 /* Make sure we fit in a window */
1998 if (physical_windowwidth/FIXEDCHARWIDTH >= 19)
1999 hugo_print(" ");
2000 else
2001 hugo_print(" ");
2002 #else
2003 hugo_print(" ");
2004 #endif
2005 hugo_font(currentfont = tempcurrentfont);
2006
2007 hugo_settextpos(1, physical_windowheight/lineheight);
2008 current_text_y = temp_current_text_y;
2009 full = 0;
2010
2011 hugo_settextcolor(fcolor);
2012 hugo_setbackcolor(bgcolor);
2013
2014 during_player_input = temp_during_player_input;
2015 }
2016
2017 #endif /* ifndef PROMPTMORE_REPLACED */
2018
2019
2020 /* RECORDCOMMANDS */
2021
2022 int RecordCommands(void)
2023 {
2024 remaining = 0;
2025 skipping_more = false;
2026
2027 switch (Peek(codeptr))
2028 {
2029 case RECORDON_T:
2030 {
2031 if (!record && !playback)
2032 {
2033 #if !defined (GLK)
2034 /* stdio implementation */
2035 hugo_getfilename("for command recording", recordfile);
2036 if (!strcmp(line, ""))
2037 return 0;
2038 if (!hugo_overwrite(line))
2039 return 0;
2040 if (!(record = HUGO_FOPEN(line, "wt")))
2041 return 0;
2042 strcpy(recordfile, line);
2043 #else
2044 /* Glk implementation */
2045 frefid_t fref = NULL;
2046
2047 fref = glk_fileref_create_by_prompt(fileusage_Transcript | fileusage_TextMode,
2048 filemode_Write, 0);
2049 record = glk_stream_open_file(fref, filemode_Write, 0);
2050 glk_fileref_destroy(fref);
2051 if (!record)
2052 return 0;
2053 #endif
2054 return 1;
2055 }
2056 break;
2057 }
2058
2059 case RECORDOFF_T:
2060 {
2061 if (playback) return 1;
2062
2063 if (record)
2064 {
2065 if (fclose(record)) return (0);
2066
2067 record = NULL;
2068 return 1;
2069 }
2070 break;
2071 }
2072
2073 case PLAYBACK_T:
2074 {
2075 if (!playback)
2076 {
2077 #if !defined (GLK)
2078 /* stdio implementation */
2079 hugo_getfilename("for command playback", recordfile);
2080 if (!strcmp(line, ""))
2081 return 0;
2082 if (!(playback = HUGO_FOPEN(line, "rt")))
2083 return 0;
2084 strcpy(recordfile, line);
2085 #else
2086 /* Glk implementation */
2087 frefid_t fref = NULL;
2088
2089 fref = glk_fileref_create_by_prompt(fileusage_InputRecord | fileusage_TextMode,
2090 filemode_Read, 0);
2091 if (glk_fileref_does_file_exist(fref))
2092 {
2093 playback = glk_stream_open_file(fref, filemode_Read, 0);
2094 }
2095 glk_fileref_destroy(fref);
2096 if (!playback)
2097 return 0;
2098 #endif
2099 return 1;
2100 }
2101 break;
2102 }
2103 }
2104 return 0;
2105 }
2106
2107
2108 /* SAVEUNDO
2109
2110 Formats:
2111
2112 end of turn: (0, undoturn, 0, 0, 0)
2113 move obj.: (MOVE_T, obj., parent, 0, 0)
2114 property: (PROP_T, obj., prop., # or PROP_ROUTINE, val.)
2115 attribute: (ATTR_T, obj., attr., 0 or 1, 0)
2116 variable: (VAR_T, var., value, 0, 0)
2117 array: (ARRAYDATA_T, array addr., element, val., 0)
2118 dict: (DICT_T, entry length, 0, 0, 0)
2119 word setting: (WORD_T, word number, new word, 0, 0)
2120 */
2121
2122 void SaveUndo(int a, int b, int c, int d, int e)
2123 {
2124 int tempptr;
2125
2126 if (undorecord)
2127 {
2128 undostack[undoptr][0] = a; /* save the operation */
2129 undostack[undoptr][1] = b;
2130 undostack[undoptr][2] = c;
2131 undostack[undoptr][3] = d;
2132 undostack[undoptr][4] = e;
2133
2134 /* Put zeroes at end of this operation in case
2135 the stack wraps around */
2136 tempptr = undoptr;
2137 if (++undoptr==MAXUNDO) undoptr = 0;
2138 undostack[undoptr][0] = 0;
2139 undostack[undoptr][1] = 0;
2140 undoptr = tempptr;
2141
2142 if (++undoturn==MAXUNDO) /* turn too complex */
2143 {undoptr = 0;
2144 undoturn = MAXUNDO;
2145 undoinvalid = 1;}
2146
2147 if (++undoptr==MAXUNDO) undoptr = 0;
2148 }
2149 }
2150
2151
2152 /* SETSTACKFRAME
2153
2154 Properly sets up the code_block structure for the current stack
2155 depth depending on if this is a called block (RUNROUTINE_BLOCK)
2156 or otherwise.
2157 */
2158
2159 void SetStackFrame(int depth, int type, long brk, long returnaddr)
2160 {
2161 if (depth==RESET_STACK_DEPTH) stack_depth = 0;
2162 else if (++stack_depth>=MAXSTACKDEPTH) FatalError(MEMORY_E);
2163
2164 code_block[stack_depth].type = type;
2165 code_block[stack_depth].brk = brk;
2166 code_block[stack_depth].returnaddr = returnaddr;
2167
2168 #if defined (DEBUGGER)
2169 code_block[stack_depth].dbnest = dbnest;
2170 #endif
2171 }
2172
2173
2174 /* SETUPDISPLAY */
2175
2176 void SetupDisplay(void)
2177 {
2178 hugo_settextmode();
2179
2180 hugo_settextwindow(1, 1,
2181 SCREENWIDTH/FIXEDCHARWIDTH, SCREENHEIGHT/FIXEDLINEHEIGHT);
2182
2183 last_window_left = 1;
2184 last_window_top = 1;
2185 last_window_right = SCREENWIDTH/FIXEDCHARWIDTH;
2186 last_window_bottom = SCREENHEIGHT/FIXEDLINEHEIGHT;
2187
2188 hugo_settextcolor(16);
2189 hugo_setbackcolor(17);
2190 hugo_clearfullscreen();
2191 }
2192
2193
2194 /* SPECIALCHAR
2195
2196 SpecialChar() is passed <a> as the string and <*i> as the
2197 position in the string. The character(s) at a[*i], a[*(i+1)],
2198 etc. are converted into a single Latin-1 (i.e., greater than
2199 127) character value.
2200
2201 Assume that the AP() has already encountered a control
2202 character ('\'), and that a[*i]... is one of:
2203
2204 `a accent grave on following character (e.g., 'a')
2205 'a accent acute on following character (e.g., 'a')
2206 ~n tilde on following (e.g., 'n' or 'N')
2207 :a umlaut on following (e.g., 'a')
2208 ^a circumflex on following (e.g., 'a')
2209 ,c cedilla on following (e.g., 'c' or 'C')
2210 < Spanish left quotation marks
2211 > Spanish right quotation marks
2212 ! upside-down exclamation mark
2213 ? upside-down question mark
2214 ae ae ligature
2215 AE AE ligature
2216 c cents symbol
2217 L British pound
2218 Y Japanese Yen
2219 - em (long) dash
2220 #nnn character value given by nnn
2221
2222 Note that the return value is a single character--which will
2223 be either unchanged or a Latin-1 character value.
2224 */
2225
2226 char SpecialChar(char *a, int *i)
2227 {
2228 char r, s, skipbracket = 0;
2229
2230 r = a[*i];
2231 s = r;
2232
2233 if (r=='\"') return r;
2234
2235 /* For a couple of versions, Hugo allowed Inform-style
2236 punctuation control characters; I don't remember
2237 exactly why.
2238 */
2239 if (game_version <= 22)
2240 if (r=='~' || r=='^') return r;
2241
2242 if (r=='(')
2243 {r = a[++*i];
2244 skipbracket = true;}
2245
2246 switch (r)
2247 {
2248 case '`': /* accent grave */
2249 {
2250 /* Note that the "s = '...'" characters are
2251 Latin-1 and may not display properly under,
2252 e.g., DOS */
2253
2254 s = a[++*i];
2255 #ifndef NO_LATIN1_CHARSET
2256 switch (s)
2257 {
2258 case 'a': s = (char)0xe0; break; /* à */
2259 case 'e': s = (char)0xe8; break; /* è */
2260 case 'i': s = (char)0xec; break; /* ì */
2261 case 'o': s = (char)0xf2; break; /* ò */
2262 case 'u': s = (char)0xf9; break; /* ù */
2263 case 'A': s = (char)0xc0; break; /* À */
2264 case 'E': s = (char)0xc8; break; /* È */
2265 case 'I': s = (char)0xcc; break; /* Ì */
2266 case 'O': s = (char)0xd2; break; /* Ò */
2267 case 'U': s = (char)0xd9; break; /* Ù */
2268 }
2269 #endif
2270 break;
2271 }
2272 case '\'': /* accent acute */
2273 {
2274 s = a[++*i];
2275 #ifndef NO_LATIN1_CHARSET
2276 switch (s)
2277 {
2278 case 'a': s = (char)0xe1; break; /* á */
2279 case 'e': s = (char)0xe9; break; /* é */
2280 case 'i': s = (char)0xed; break; /* í */
2281 case 'o': s = (char)0xf3; break; /* ó */
2282 case 'u': s = (char)0xfa; break; /* ú */
2283 case 'y': s = (char)0xfd; break;
2284 case 'A': s = (char)0xc1; break; /* Á */
2285 case 'E': s = (char)0xc9; break; /* É */
2286 case 'I': s = (char)0xcd; break; /* Í */
2287 case 'O': s = (char)0xd3; break; /* Ó */
2288 case 'U': s = (char)0xda; break; /* Ú */
2289 case 'Y': s = (char)0xdd; break; /* Ý */
2290 }
2291 #endif
2292 break;
2293 }
2294 case '~': /* tilde */
2295 {
2296 s = a[++*i];
2297 #ifndef NO_LATIN1_CHARSET
2298 switch (s)
2299 {
2300 case 'a': s = (char)0xe3; break; /* ã */
2301 case 'n': s = (char)0xf1; break; /* ñ */
2302 case 'o': s = (char)0xf5; break; /* õ */
2303 case 'A': s = (char)0xc3; break; /* Ã */
2304 case 'N': s = (char)0xd1; break; /* Ñ */
2305 case 'O': s = (char)0xd5; break; /* Õ */
2306 }
2307 #endif
2308 break;
2309 }
2310 case '^': /* circumflex */
2311 {
2312 s = a[++*i];
2313 #ifndef NO_LATIN1_CHARSET
2314 switch (s)
2315 {
2316 case 'a': s = (char)0xe2; break; /* â */
2317 case 'e': s = (char)0xea; break; /* ê */
2318 case 'i': s = (char)0xee; break; /* î */
2319 case 'o': s = (char)0xf4; break; /* ô */
2320 case 'u': s = (char)0xfb; break; /* û */
2321 case 'A': s = (char)0xc2; break; /* Â */
2322 case 'E': s = (char)0xca; break; /* Ê */
2323 case 'I': s = (char)0xce; break; /* Î */
2324 case 'O': s = (char)0xd4; break; /* Ô */
2325 case 'U': s = (char)0xdb; break; /* Û */
2326 }
2327 #endif
2328 break;
2329 }
2330 case ':': /* umlaut */
2331 {
2332 s = a[++*i];
2333 #ifndef NO_LATIN1_CHARSET
2334 switch (s)
2335 {
2336 case 'a': s = (char)0xe4; break; /* ä */
2337 case 'e': s = (char)0xeb; break; /* ë */
2338 case 'i': s = (char)0xef; break; /* ï */
2339 case 'o': s = (char)0xf6; break; /* ö */
2340 case 'u': s = (char)0xfc; break; /* ü */
2341 /* case 'y': s = (char)0xff; break; */ /* ÿ */
2342 case 'A': s = (char)0xc4; break; /* Ä */
2343 case 'E': s = (char)0xcb; break; /* Ë */
2344 case 'I': s = (char)0xcf; break; /* Ï */
2345 case 'O': s = (char)0xd6; break; /* Ö */
2346 case 'U': s = (char)0xdc; break; /* Ü */
2347 }
2348 #endif
2349 break;
2350 }
2351 case ',': /* cedilla */
2352 {
2353 s = a[++*i];
2354 #ifndef NO_LATIN1_CHARSET
2355 switch (s)
2356 {
2357 case 'C': s = (char)0xc7; break; /* Ç */
2358 case 'c': s = (char)0xe7; break; /* ç */
2359 }
2360 #endif
2361 break;
2362 }
2363 case '<': /* Spanish left quotation marks */
2364 #ifndef NO_LATIN1_CHARSET
2365 s = (char)0xab; /* « */
2366 #endif
2367 break;
2368 case '>': /* Spanish right quotation marks */
2369 #ifndef NO_LATIN1_CHARSET
2370 s = (char)0xbb; /* » */
2371 break;
2372 #endif
2373 case '!': /* upside-down exclamation mark */
2374 #ifndef NO_LATIN1_CHARSET
2375 s = (char)0xa1; /* ¡ */
2376 #endif
2377 break;
2378 case '?': /* upside-down question mark */
2379 #ifndef NO_LATIN1_CHARSET
2380 s = (char)0xbf; /* ¿ */
2381 #endif
2382 break;
2383 case 'a': /* ae ligature */
2384 #ifndef NO_LATIN1_CHARSET
2385 s = (char)0xe6; ++*i; /* æ */
2386 #else
2387 s = 'e'; ++*i;
2388 #endif
2389 break;
2390 case 'A': /* AE ligature */
2391 #ifndef NO_LATIN1_CHARSET
2392 s = (char)0xc6; ++*i; /* Æ */
2393 #else
2394 s = 'E'; ++*i;
2395 #endif
2396 break;
2397 case 'c': /* cents symbol */
2398 #ifndef NO_LATIN1_CHARSET
2399 s = (char)0xa2; /* ¢ */
2400 #endif
2401 break;
2402 case 'L': /* British pound */
2403 #ifndef NO_LATIN1_CHARSET
2404 s = (char)0xa3; /* £ */
2405 #endif
2406 break;
2407 case 'Y': /* Japanese Yen */
2408 #ifndef NO_LATIN1_CHARSET
2409 s = (char)0xa5; /* ¥ */
2410 #endif
2411 break;
2412 case '-': /* em dash */
2413 #ifndef NO_LATIN1_CHARSET
2414 /* s = (char)0x97; */ /* — */
2415 #endif
2416 break;
2417 case '#': /* 3-digit decimal code */
2418 {
2419 s = (char)((a[++*i]-'0')*100);
2420 s += (a[++*i]-'0')*10;
2421 s += (a[++*i]-'0');
2422 #ifdef NO_LATIN1_CHARSET
2423 if ((unsigned)s>127) s = '?';
2424 #endif
2425 }
2426 }
2427
2428 if (skipbracket)
2429 {
2430 ++*i;
2431 if (a[*i+1]==')') ++*i;
2432 if (s==')') s = r;
2433 }
2434
2435 return s;
2436 }
2437
2438
2439 /* TRYTOOPEN
2440
2441 Tries to open a particular filename (based on a given environment
2442 variable), trying the current directory first.
2443 */
2444
2445 #if !defined (GLK) /* not used for Glk */
2446
2447 HUGO_FILE TrytoOpen(char *f, char *p, char *d)
2448 {
2449 char drive[MAXDRIVE], dir[MAXDIR], fname[MAXFILENAME], ext[MAXEXT];
2450 char envvar[32];
2451 HUGO_FILE tempfile; char temppath[MAXPATH];
2452
2453 /* Try to open the given, vanilla filename */
2454 if ((strcmp(f, "")) && (tempfile = HUGO_FOPEN(f, p)))
2455 {
2456 return tempfile;
2457 }
2458
2459 hugo_splitpath(f, drive, dir, fname, ext); /* file to open */
2460
2461 /* If the given filename doesn't already specify where to find it */
2462 if (!strcmp(drive, "") && !strcmp(dir, ""))
2463 {
2464 /* Check gamefile directory */
2465 hugo_makepath(temppath, "", gamepath, fname, ext);
2466
2467 if ((tempfile = HUGO_FOPEN(temppath, p)))
2468 {
2469 strcpy(f, temppath); /* the new pathname */
2470 return tempfile;
2471 }
2472
2473 /* Check environment variables */
2474 strcpy(envvar, "hugo_"); /* the actual var. name */
2475 strcat(envvar, d);
2476
2477 if (getenv(strupr(envvar)))
2478 {
2479 hugo_makepath(temppath, "", getenv(strupr(envvar)), fname, ext);
2480
2481 if ((tempfile = HUGO_FOPEN(temppath, p)))
2482 {
2483 strcpy(f, temppath); /* the new pathname */
2484 return tempfile;
2485 }
2486 }
2487 }
2488
2489 return NULL; /* return NULL if not openable */
2490 }
2491
2492 #endif /* GLK */
2493
2494
2495 /* UNDO */
2496
2497 int Undo()
2498 {
2499 int count = 0, n;
2500 int turns, turncount, tempptr;
2501 int obj, prop, attr, v;
2502 unsigned int addr;
2503
2504 if (--undoptr < 0) undoptr = MAXUNDO-1;
2505
2506 if (undostack[undoptr][1]!=0)
2507 {
2508 /* Get the number of operations to be undone for
2509 the last turn.
2510 */
2511 if ((turns = undostack[undoptr][1]) >= MAXUNDO)
2512 goto CheckUndoFailed;
2513
2514 turns--;
2515
2516 turncount = 0;
2517 tempptr = undoptr;
2518
2519 /* Count the number of operations available to see if there
2520 are enough to undo the last turn (as per the number
2521 required in <turns>.
2522 */
2523 do
2524 {
2525 if (--undoptr < 0) undoptr = MAXUNDO-1;
2526 turncount++;
2527
2528 /* if end of turn */
2529 if (undostack[undoptr][0]==0)
2530 break;
2531 }
2532 while (true);
2533
2534 if (turncount<turns) goto CheckUndoFailed;
2535
2536 undoptr = tempptr;
2537
2538 if (--undoptr < 0) undoptr = MAXUNDO-1;
2539
2540 while (undostack[undoptr][0] != 0)
2541 {
2542 switch (undostack[undoptr][0])
2543 {
2544 case MOVE_T:
2545 {
2546 MoveObj(undostack[undoptr][1], undostack[undoptr][2]);
2547 count++;
2548 break;
2549 }
2550
2551 case PROP_T:
2552 {
2553 obj = undostack[undoptr][1];
2554 prop = undostack[undoptr][2];
2555 n = undostack[undoptr][3];
2556 v = undostack[undoptr][4];
2557
2558 if ((addr = PropAddr(obj, prop, 0))!=0)
2559 {
2560 defseg = proptable;
2561
2562 if (n==PROP_ROUTINE)
2563 {
2564 Poke(addr+1, PROP_ROUTINE);
2565 n = 1;
2566 }
2567
2568 /* Use this new prop count number if the
2569 existing one is too low or a prop routine
2570 */
2571 else if (Peek(addr+1)==PROP_ROUTINE || Peek(addr+1)<(unsigned char)n)
2572 Poke(addr+1, (unsigned char)n);
2573
2574 /* property length */
2575 if (n<=(int)Peek(addr+1))
2576 PokeWord(addr+2+(n-1)*2, v);
2577 }
2578 count++;
2579 break;
2580 }
2581
2582 case ATTR_T:
2583 {
2584 obj = undostack[undoptr][1];
2585 attr = undostack[undoptr][2];
2586 n = undostack[undoptr][3];
2587 SetAttribute(obj, attr, n);
2588 count++;
2589 break;
2590 }
2591
2592 case VAR_T:
2593 {
2594 n = undostack[undoptr][1];
2595 v = undostack[undoptr][2];
2596 var[n] = v;
2597 count++;
2598 break;
2599 }
2600
2601 case ARRAYDATA_T:
2602 {
2603 defseg = arraytable;
2604 addr = undostack[undoptr][1];
2605 n = undostack[undoptr][2];
2606 v = undostack[undoptr][3];
2607
2608 /* The array length was already accounted for before calling
2609 SaveUndo(), so there is no adjustment of
2610 +2 here.
2611 */
2612 PokeWord(addr+n*2, v);
2613 count++;
2614 break;
2615 }
2616
2617 case DICT_T:
2618 {
2619 defseg = dicttable;
2620 PokeWord(0, --dictcount);
2621 count++;
2622 break;
2623 }
2624 case WORD_T:
2625 {
2626 n = undostack[undoptr][1];
2627 v = undostack[undoptr][2];
2628 wd[n] = v;
2629 word[n] = GetWord(wd[n]);
2630 count++;
2631 }
2632 }
2633 defseg = gameseg;
2634
2635 if (--undoptr < 0) undoptr = MAXUNDO-1;
2636 }
2637 }
2638
2639 CheckUndoFailed:
2640 if (!count)
2641 {
2642 undoinvalid = 1;
2643 game_reset = false;
2644 return 0;
2645 }
2646
2647 game_reset = true;
2648
2649 undoptr++;
2650 return 1;
2651 }
2652
2653
2654 /*
2655 * Random-number generator replacement by Andrew Plotkin:
2656 *
2657 */
2658
2659 #if defined (BUILD_RANDOM)
2660
2661 static unsigned int rand_table[55]; /* state for the RNG */
2662 static int rand_index1, rand_index2;
2663
2664 int random()
2665 {
2666 rand_index1 = (rand_index1 + 1) % 55;
2667 rand_index2 = (rand_index2 + 1) % 55;
2668 rand_table[rand_index1] = rand_table[rand_index1] - rand_table[rand_index2];
2669 return rand_table[rand_index1];
2670 }
2671
2672 void srandom(int seed)
2673 {
2674 int k = 1;
2675 int i, loop;
2676
2677 rand_table[54] = seed;
2678 rand_index1 = 0;
2679 rand_index2 = 31;
2680
2681 for (i = 0; i < 55; i++)
2682 {
2683 int ii = (21 * i) % 55;
2684
2685 rand_table[ii] = k;
2686 k = seed - k;
2687 seed = rand_table[ii];
2688 }
2689
2690 for (loop = 0; loop < 4; loop++)
2691 {
2692 for (i = 0; i < 55; i++)
2693 rand_table[i] = rand_table[i] - rand_table[ (1 + i + 30) % 55];
2694 }
2695 }
2696
2697 #endif /* BUILD_RANDOM */