Debian Codegraph @

terps/scott/saga/saga.c

raw symbols SCIP
1 //
2 // saga.c
3 // part of ScottFree, an interpreter for adventures in Scott Adams format
4 //
5 // Created by Petter Sjölund on 2022-09-29.
6 //
7
8 #include <ctype.h>
9 #include <stdlib.h>
10 #include <string.h>
11
12 #include "apple2draw.h"
13 #include "atari8c64draw.h"
14 #include "detectgame.h"
15 #include "pcdraw.h"
16 #include "sagagraphics.h"
17 #include "scott.h"
18
19 #include "saga.h"
20
21 // clang-format off
22
23 static const char *DOSFilenames[] =
24 { "B01024R", "B01044I", "R0109", "R0190",
25 "B01001I", "B01025R", "B01045R", "R0112", "R0191",
26 "B01002I", "B01026R", "B01046I", "R0115", "R0192",
27 "B01003I", "B01029I", "B01048I", "R0116", "R0193",
28 "B01004I", "B01030I", "B01050I", "R0119", "R0194",
29 "B01012I", "B01031I", "B01051I", "R0120", "R0195",
30 "B01013R", "B01032I", "B01053R", "R0181", "R0196",
31 "B01016I", "B01033R", "B01070R", "R0182", "R0197",
32 "B01017R", "B01034R", "B01072R", "R0183", "R0198",
33 "B01018I", "B01035I", "R0184", "R0199", "B01019I",
34 "B01036R", "R0100", "R0185", "B01020R","B01037I",
35 "R0101", "R0186", "B01021I", "B01039R","R0102",
36 "R0187", "B01022R", "B01040R", "R0103", "R0188",
37 "B01023I", "B01042I", "R0104", "R0189", NULL };
38
39 // clang-format on
40
41 int LoadDOSImages(void)
42 {
43 USImages = new_image();
44
45 struct USImage *image = USImages;
46 size_t datasize;
47 int found = 0;
48
49 for (int i = 0; DOSFilenames[i] != NULL; i++) {
50 const char *shortname = DOSFilenames[i];
51 uint8_t *data = FindImageFile(shortname, &datasize);
52
53 if (data) {
54 found++;
55 image->datasize = datasize;
56 image->imagedata = MemAlloc(datasize);
57 memcpy(image->imagedata, data, datasize);
58 free(data);
59 image->systype = SYS_MSDOS;
60 if (shortname[0] == 'R') {
61 image->usage = IMG_ROOM;
62 image->index = shortname[4] - '0' + (shortname[3] - '0') * 10;
63 } else {
64 image->index = shortname[5] - '0' + (shortname[4] - '0') * 10;
65 if (shortname[6] == 'R') {
66 image->usage = IMG_ROOM_OBJ;
67 } else {
68 image->usage = IMG_INV_OBJ;
69 }
70 }
71 image->next = new_image();
72 image = image->next;
73 }
74 }
75 if (!found) {
76 free(USImages);
77 USImages = NULL;
78 return 0;
79 }
80 return 1;
81 }
82
83 uint8_t *ReadUSDictionary(uint8_t *ptr)
84 {
85 char dictword[GameHeader.WordLength + 2];
86 char c = 0;
87 int wordnum = 0;
88 int charindex = 0;
89
90 int nn = GameHeader.NumWords + 1;
91
92 do {
93 for (int i = 0; i < GameHeader.WordLength; i++) {
94 c = *(ptr++);
95 if (c == 0) {
96 if (charindex == 0) {
97 c = *(ptr++);
98 }
99 }
100 dictword[charindex] = c;
101 if (c == '*')
102 i--;
103 charindex++;
104
105 dictword[charindex] = 0;
106 }
107
108 if (wordnum < nn) {
109 Nouns[wordnum] = MemAlloc(charindex + 1);
110 memcpy((char *)Nouns[wordnum], dictword, charindex + 1);
111 debug_print("Nouns %d: \"%s\"\n", wordnum,
112 Nouns[wordnum]);
113 } else {
114 Verbs[wordnum - nn] = MemAlloc(charindex + 1);
115 memcpy((char *)Verbs[wordnum - nn], dictword, charindex + 1);
116 debug_print("Verbs %d: \"%s\"\n", wordnum - nn,
117 Verbs[wordnum - nn]);
118 }
119 wordnum++;
120
121 if (c > 127)
122 return ptr;
123
124 charindex = 0;
125 } while (wordnum <= GameHeader.NumWords * 2 + 1);
126
127 return ptr;
128 }
129
130 int DrawUSImage(USImage *image)
131 {
132 last_image_index = image->index;
133 if (image->systype == SYS_MSDOS)
134 return DrawDOSImage(image);
135 else if (image->systype == SYS_C64 || image->systype == SYS_ATARI8)
136 return DrawAtariC64Image(image);
137 else if (image->systype == SYS_APPLE2)
138 return DrawApple2Image(image);
139 return 0;
140 }
141
142 void DrawInventoryImages(void)
143 {
144 struct USImage *image = USImages;
145 if (image != NULL) {
146 do {
147 if (image->usage == IMG_INV_OBJ && Items[image->index].Location == CARRIED) {
148 DrawUSImage(image);
149 }
150 image = image->next;
151 } while (image != NULL);
152 }
153 }
154
155 void DrawRoomObjectImages(void)
156 {
157 struct USImage *image = USImages;
158 if (image != NULL) {
159 do {
160 if (image->usage == IMG_ROOM_OBJ && image->index <= GameHeader.NumItems && Items[image->index].Location == MyLoc) {
161 DrawUSImage(image);
162 }
163 image = image->next;
164 } while (image != NULL);
165 }
166 }
167
168 int DrawUSRoom(int room)
169 {
170 struct USImage *image = USImages;
171 if (image != NULL) {
172 do {
173 if (image->usage == IMG_ROOM && image->index == room) {
174 return DrawUSImage(image);
175 }
176 image = image->next;
177 } while (image != NULL);
178 }
179 return 0;
180 }
181
182 void DrawUSRoomObject(int item)
183 {
184 struct USImage *image = USImages;
185 if (image != NULL) {
186 do {
187 if (image->usage == IMG_ROOM_OBJ && image->index == item) {
188 DrawUSImage(image);
189 return;
190 }
191 image = image->next;
192 } while (image != NULL);
193 }
194 }
195
196 void LookUS(void)
197 {
198 if (!Graphics)
199 return;
200
201 glk_window_clear(Graphics);
202 glk_window_clear(Top);
203
204 int room = MyLoc;
205
206 if (CurrentGame == HULK_US && USImages && USImages->systype != SYS_APPLE2)
207 switch (MyLoc) {
208 case 5: // Tunnel going outside
209 case 6:
210 room = 3;
211 break;
212 case 7: // Field
213 case 8:
214 room = 4;
215 break;
216 case 10: // Hole
217 case 11:
218 room = 9;
219 break;
220 case 13: // Dome
221 case 14:
222 room = 2;
223 break;
224 case 17: // warp
225 case 18:
226 room = 16;
227 break;
228 default:
229 break;
230 }
231
232 if (!DrawUSRoom(room)) {
233 return;
234 }
235 DrawRoomObjectImages();
236
237 if (CurrentGame == HULK_US) {
238 if (Items[18].Location == MyLoc && MyLoc == Items[18].InitialLoc) // Bio Gem
239 DrawUSRoomObject(70);
240 if (Items[21].Location == MyLoc && MyLoc == Items[21].InitialLoc) // Wax
241 DrawUSRoomObject(72);
242 if (Items[14].Location == MyLoc || Items[15].Location == MyLoc) // Large pit
243 DrawUSRoomObject(13);
244 } else if (CurrentGame == COUNT_US) {
245 if (Items[17].Location == MyLoc && MyLoc == 8) // Only draw mirror in bathroom
246 DrawUSRoomObject(80);
247 if (Items[35].Location == MyLoc && MyLoc == 18) // Only draw other end of sheet in pit
248 DrawUSRoomObject(81);
249 if (Items[5].Location == MyLoc && MyLoc == 9) // Only draw coat-of-arms at gate
250 DrawUSRoomObject(82);
251 } else if (CurrentGame == VOODOO_CASTLE_US) {
252 if (Items[45].Location == MyLoc && MyLoc == 14) // Only draw boards in chimney
253 DrawUSRoomObject(80);
254 }
255 if (CurrentSys == SYS_APPLE2)
256 DrawApple2ImageFromVideoMem();
257 }
258
259 void InventoryUS(void)
260 {
261 if (!Graphics || !has_graphics())
262 return;
263 glk_window_clear(Graphics);
264 DrawUSRoom(98);
265 DrawInventoryImages();
266 if (CurrentSys == SYS_APPLE2)
267 DrawApple2ImageFromVideoMem();
268 if (!showing_inventory) {
269 showing_inventory = 1;
270 Output(sys[HIT_ENTER]);
271 HitEnter();
272 }
273 }
274
275 extern size_t hulk_coordinates;
276 extern size_t hulk_item_image_offsets;
277 extern size_t hulk_look_image_offsets;
278 extern size_t hulk_special_image_offsets;
279 extern size_t hulk_image_offset;
280
281 static int SanityCheckScottFreeHeader(int ni, int na, int nw, int nr, int mc)
282 {
283 int16_t v = header[1]; // items
284 if (v < 10 || v > 500)
285 return 0;
286 v = header[2]; // actions
287 if (v < 100 || v > 500)
288 return 0;
289 v = header[3]; // word pairs
290 if (v < 50 || v > 200)
291 return 0;
292 v = header[4]; // Number of rooms
293 if (v < 10 || v > 100)
294 return 0;
295 v = header[5]; // Number of Messages
296 if (v < 10 || v > 255)
297 return 0;
298
299 return 1;
300 }
301
302 uint8_t *Skip(uint8_t *ptr, int count, uint8_t *eof)
303 {
304 #if (DEBUG_PRINT)
305 for (int i = 0; i < count && ptr + i + 1 < eof; i += 2) {
306 uint16_t val = ptr[i] + ptr[i + 1] * 0x100;
307 debug_print("Unknown value %d: %d (%x)\n", i / 2, val, val);
308 }
309 #endif
310 return ptr + count;
311 }
312
313 GameIDType LoadBinaryDatabase(uint8_t *data, size_t length, struct GameInfo info, int dict_start)
314 {
315 int ni, na, nw, nr, mc, pr, tr, wl, lt, mn, trm;
316 int ct;
317
318 Action *ap;
319 Room *rp;
320 Item *ip;
321
322 /* Load the header */
323 uint8_t *ptr = data;
324
325 size_t offset;
326
327 if (dict_start) {
328 file_baseline_offset = dict_start - info.start_of_dictionary - 645;
329 debug_print("LoadBinaryDatabase: file baseline offset:%d\n",
330 file_baseline_offset);
331 offset = info.start_of_header + file_baseline_offset;
332 ptr = SeekToPos(data, offset);
333 } else {
334 int version = 0;
335 int adventure_number = 0;
336 for (int i = 0; i < 0x38; i += 2) {
337 int value = ptr[i] + ptr[i + 1] * 0x100;
338 if (value < 500) {
339 if (version == 0)
340 version = value;
341 else {
342 adventure_number = value;
343 break;
344 }
345 }
346 }
347 debug_print("Version: %d\n", version);
348 debug_print("Adventure number: %d\n", adventure_number);
349 if (adventure_number == 0)
350 return 0;
351 if (version == 127 && adventure_number == 1)
352 CurrentGame = HULK_US;
353 else if (version == 126) {
354 if (adventure_number == 2)
355 CurrentGame = HULK_US_PREL;
356 else if (adventure_number == 13)
357 CurrentGame = CLAYMORGUE_US_126;
358 } else
359 CurrentGame = adventure_number;
360 Game->type = US_VARIANT;
361 ptr = Skip(ptr, 0x38, data + length);
362 }
363
364 if (ptr == NULL)
365 return UNKNOWN_GAME;
366
367 ptr = ReadHeader(ptr);
368
369 ParseHeader(header, US_HEADER, &ni, &na, &nw, &nr, &mc, &pr, &tr,
370 &wl, &lt, &mn, &trm);
371
372 PrintHeaderInfo(header, ni, na, nw, nr, mc, pr, tr, wl, lt, mn, trm);
373
374 if (!SanityCheckScottFreeHeader(ni, na, nw, nr, mc))
375 return UNKNOWN_GAME;
376
377 GameHeader.NumItems = ni;
378 Items = (Item *)MemAlloc(sizeof(Item) * (ni + 1));
379 GameHeader.NumActions = na;
380 Actions = (Action *)MemAlloc(sizeof(Action) * (na + 1));
381 GameHeader.NumWords = nw;
382 GameHeader.WordLength = wl;
383 Verbs = MemAlloc(sizeof(char *) * (nw + 2));
384 Nouns = MemAlloc(sizeof(char *) * (nw + 2));
385 GameHeader.NumRooms = nr;
386 Rooms = (Room *)MemAlloc(sizeof(Room) * (nr + 1));
387 GameHeader.MaxCarry = mc;
388 GameHeader.PlayerRoom = pr;
389 GameHeader.Treasures = tr;
390 GameHeader.LightTime = lt;
391 LightRefill = lt;
392 GameHeader.NumMessages = mn;
393 Messages = MemAlloc(sizeof(char *) * (mn + 1));
394 GameHeader.TreasureRoom = trm;
395
396 /* if dict_start > 0, we are checking for the UK version of Questprobe featuring The Hulk */
397 if (dict_start) {
398 if (header[0] != info.word_length || header[1] != info.number_of_words || header[2] != info.number_of_actions || header[3] != info.number_of_items || header[4] != info.number_of_messages || header[5] != info.number_of_rooms || header[6] != info.max_carried) {
399 // debug_print("Non-matching header\n");
400 return UNKNOWN_GAME;
401 }
402 }
403
404 #pragma mark Dictionary
405
406 while (!(*ptr == 'A' && *(ptr + 1) == 'N' && *(ptr + 2) == 'Y') && ptr - data < length - 2)
407 ptr++;
408
409 if (ptr - data >= length - 2)
410 return UNKNOWN_GAME;
411
412 ptr = ReadUSDictionary(ptr);
413
414 #pragma mark Rooms
415
416 ct = 0;
417 rp = Rooms;
418
419 uint8_t string_length = 0;
420 do {
421 string_length = *(ptr++);
422 if (string_length == 0) {
423 rp->Text = ".\0";
424 } else {
425 rp->Text = MemAlloc(string_length + 1);
426 for (int i = 0; i < string_length; i++) {
427 rp->Text[i] = *(ptr++);
428 }
429 rp->Text[string_length] = 0;
430 }
431 debug_print("Room %d: \"%s\"\n", ct, rp->Text);
432 rp++;
433 ct++;
434 } while (ct < nr + 1);
435
436 #pragma mark Messages
437
438 ct = 0;
439 char *string;
440
441 do {
442 string_length = *(ptr++);
443 if (string_length == 0) {
444 string = ".\0";
445 } else {
446 string = MemAlloc(string_length + 1);
447 for (int i = 0; i < string_length; i++) {
448 string[i] = *(ptr++);
449 }
450 string[string_length] = 0;
451 }
452 Messages[ct] = string;
453 debug_print("Message %d: \"%s\"\n", ct, Messages[ct]);
454 ct++;
455 } while (ct < mn + 1);
456
457 #pragma mark Items
458
459 ct = 0;
460 ip = Items;
461
462 do {
463 string_length = *(ptr++);
464 if (string_length == 0) {
465 ip->Text = ".\0";
466 ip->AutoGet = NULL;
467 } else {
468 ip->Text = MemAlloc(string_length + 1);
469
470 for (int i = 0; i < string_length; i++) {
471 ip->Text[i] = *(ptr++);
472 }
473 ip->Text[string_length] = 0;
474 ip->AutoGet = strchr(ip->Text, '/');
475 /* Some games use // to mean no auto get/drop word! */
476 if (ip->AutoGet && strcmp(ip->AutoGet, "//") && strcmp(ip->AutoGet, "/*")) {
477 char *t;
478 *ip->AutoGet++ = 0;
479 t = strchr(ip->AutoGet, '/');
480 if (t != NULL)
481 *t = 0;
482 ptr++;
483 }
484 }
485
486 debug_print("Item %d: %s\n", ct, ip->Text);
487 if (ip->AutoGet)
488 debug_print("Autoget:%s\n", ip->AutoGet);
489
490 ct++;
491 ip++;
492 } while (ct <= ni);
493
494 #pragma mark item locations
495
496 ptr++;
497 ct = 0;
498 ip = Items;
499 while (ct < ni + 1) {
500 ip->Location = *ptr;
501 ip->InitialLoc = ip->Location;
502 debug_print("Item %d (%s) start location: %d\n", ct, Items[ct].Text, ip->Location);
503 ptr += 2;
504 ip++;
505 ct++;
506 }
507
508 // Image strings lookup table
509 ptr = Skip(ptr, (ni + 1) * 4, data + length);
510
511 #pragma mark Actions
512
513 ct = 0;
514 ap = Actions;
515
516 offset = ptr - data;
517 size_t offset2;
518
519 int verb, noun, value, value2, plus;
520 while (ct <= na) {
521 plus = na + 1;
522 verb = data[offset + ct];
523 noun = data[offset + ct + plus];
524
525 ap->Vocab = verb * 150 + noun;
526
527 for (int j = 0; j < 2; j++) {
528 plus += na + 1;
529 value = data[offset + ct + plus];
530 plus += na + 1;
531 value2 = data[offset + ct + plus];
532 ap->Subcommand[j] = 150 * value + value2;
533 }
534
535 offset2 = offset + 6 * (na + 1);
536 plus = 0;
537
538 for (int j = 0; j < 5; j++) {
539 value = data[offset2 + ct * 2 + plus];
540 value2 = data[offset2 + ct * 2 + plus + 1];
541 ptr = &data[offset2 + ct * 2 + plus + 2];
542 ap->Condition[j] = value + value2 * 0x100;
543 plus += (na + 1) * 2;
544 }
545
546 ap++;
547 ct++;
548 }
549
550 // Room descriptions lookup table
551 ptr = Skip(ptr, (nr + 1) * 2, data + length);
552
553 #pragma mark Room connections
554 /* The room connections are ordered by direction, not room, so all the North
555 * connections for all the rooms come first, then the South connections, and
556 * so on. */
557 for (int j = 0; j < 6; j++) {
558 ct = 0;
559 rp = Rooms;
560
561 while (ct < nr + 1) {
562 rp->Image = 255;
563 rp->Exits[j] = *ptr;
564 debug_print("Room %d (%s) exit %d (%s): %d\n", ct, Rooms[ct].Text, j, Nouns[j + 1], rp->Exits[j]);
565 ptr += 2;
566 ct++;
567 rp++;
568 }
569 }
570
571 // Return if not reading UK Hulk
572 if (!dict_start) {
573 ptr = Skip(ptr, 0xe4, data + length);
574 return CurrentGame;
575 }
576
577 #pragma mark room images
578
579 if (SeekIfNeeded(info.start_of_room_image_list, &offset, &ptr) == 0)
580 return 0;
581
582 rp = Rooms;
583
584 for (ct = 0; ct <= GameHeader.NumRooms; ct++) {
585 rp->Image = *(ptr++);
586 // debug_print("Room %d (%s) has image %d\n", ct, rp->Text,
587 // rp->Image );
588 rp++;
589 }
590
591 #pragma mark item images
592
593 if (SeekIfNeeded(info.start_of_item_image_list, &offset, &ptr) == 0)
594 return UNKNOWN_GAME;
595
596 ip = Items;
597
598 for (ct = 0; ct <= GameHeader.NumItems; ct++) {
599 ip->Image = 255;
600 ip++;
601 }
602
603 int index, image = 10;
604
605 do {
606 index = *ptr++;
607 if (index != 255)
608 Items[index].Image = image++;
609 } while (index != 255);
610
611 if (info.gameID == HULK_C64) {
612 hulk_coordinates = 0x22cd;
613 hulk_item_image_offsets = 0x2731;
614 hulk_look_image_offsets = 0x2761;
615 hulk_special_image_offsets = 0x2781;
616 hulk_image_offset = -0x7ff;
617 }
618
619 return info.gameID;
620 }
621
622 uint8_t *ReadFileIfExists(const char *name, size_t *size)
623 {
624 FILE *fptr = fopen(name, "r");
625
626 if (fptr == NULL)
627 return NULL;
628
629 *size = GetFileLength(fptr);
630 if (*size < 1)
631 return NULL;
632
633 uint8_t *result = MemAlloc(*size);
634 fseek(fptr, 0, SEEK_SET);
635 int bytesread = fread(result, 1, *size, fptr);
636
637 fclose(fptr);
638
639 if (bytesread == 0) {
640 free(result);
641 return NULL;
642 }
643
644 return result;
645 }
646
647 int CompareFilenames(const char *str1, size_t length1, const char *str2, size_t length2)
648 {
649 while (length1 > 0 && str1[length1] != '.') {
650 length1--;
651 }
652 while (length2 > 0 && str2[length2] != '.') {
653 length2--;
654 }
655 size_t length = MIN(length1, length2);
656 if (length <= 0)
657 return 0;
658 for (int i = length; i > 0; i--) {
659 if (str1[length1--] != str2[length2--]) {
660 return 0;
661 }
662 }
663 return 1;
664 }
665
666 const char *AddGameFileExtension(const char *filename, size_t gamefilelen, size_t *stringlength)
667 {
668 char *new = NULL;
669 size_t extpos = gamefilelen;
670 while (extpos && game_file[extpos] != '.')
671 extpos--;
672 size_t extensionlength = gamefilelen - extpos;
673 char *extension = MemAlloc((int)extensionlength);
674 memcpy(extension, &game_file[extpos + 1], extensionlength);
675 extpos = *stringlength;
676 while (extpos && filename[extpos] != '.')
677 extpos--;
678 *stringlength = extpos + extensionlength;
679 new = MemAlloc((int)*stringlength + 1);
680 memcpy(new, filename, extpos + 1);
681 memcpy(new + extpos + 1, extension, extensionlength);
682 new[*stringlength] = 0;
683 return new;
684 }
685
686 const char *LookForCompanionFilenameInDatabase(const pairrec list[][2], size_t stringlen, size_t *stringlength2)
687 {
688
689 for (int i = 0; list[i][0].filename != NULL; i++) {
690 *stringlength2 = list[i][0].stringlength;
691 if (*stringlength2 == 0) {
692 *stringlength2 = strlen(list[i][0].filename);
693 debug_print("length of string companionlist[%d][0] (%s): %zu\n", i, list[i][0].filename, *stringlength2);
694 }
695 if (CompareFilenames(game_file, stringlen, list[i][0].filename, *stringlength2) == 1) {
696 *stringlength2 = list[i][1].stringlength;
697 if (*stringlength2 == 0)
698 *stringlength2 = strlen(list[i][1].filename);
699 return AddGameFileExtension(list[i][1].filename, stringlen, stringlength2);
700 }
701
702 *stringlength2 = list[i][1].stringlength;
703 if (*stringlength2 == 0) {
704 *stringlength2 = strlen(list[i][1].filename);
705 debug_print("length of string companionlist[%d][1] (%s): %zu\n", i, list[i][1].filename, *stringlength2);
706 }
707 if (CompareFilenames(game_file, stringlen, list[i][1].filename, *stringlength2) == 1) {
708 *stringlength2 = list[i][0].stringlength;
709 if (*stringlength2 == 0)
710 *stringlength2 = strlen(list[i][0].filename);
711 return AddGameFileExtension(list[i][0].filename, stringlen, stringlength2);
712 }
713 }
714
715 return NULL;
716 }
717
718 char *LookInDatabase(const pairrec list[][2], size_t stringlen)
719 {
720 size_t resultlen;
721 const char *foundname = LookForCompanionFilenameInDatabase(list, stringlen, &resultlen);
722 if (foundname != NULL) {
723 while (stringlen > 0 && game_file[stringlen] != '/' && game_file[stringlen] != '\\')
724 stringlen--;
725 stringlen++;
726 size_t newlen = stringlen + resultlen;
727 char *path = MemAlloc(newlen + 1);
728 memcpy(path, game_file, stringlen);
729 memcpy(path + stringlen, foundname, resultlen);
730 path[newlen] = '\0';
731 return path;
732 }
733
734 return NULL;
735 }