Debian Codegraph @

terps/scott/ti994a/load_ti99_4a.c

raw symbols SCIP
1 //
2 // load_ti99_4a.c
3 // part of ScottFree, an interpreter for adventures in Scott Adams format
4 //
5 // Created by Petter Sjölund on 2022-02-12.
6 //
7
8 #include <assert.h>
9 #include <ctype.h>
10 #include <stdlib.h>
11 #include <string.h>
12
13 #include "load_ti99_4a.h"
14 #include "scott.h"
15
16 #include "detectgame.h"
17 #include "scottgameinfo.h"
18
19 #define PACKED __attribute__((__packed__))
20
21 // clang-format off
22
23 struct DATAHEADER {
24 uint8_t num_objects; /* number of objects */
25 uint8_t num_verbs; /* number of verbs */
26 uint8_t num_nouns; /* number of nouns */
27 uint8_t red_room; /* the red room (dead room) */
28 uint8_t max_items_carried; /* max number of items can be carried */
29 uint8_t begin_locn; /* room to start in */
30 uint8_t num_treasures; /* number of treasures */
31 uint8_t cmd_length; /* number of letters in commands */
32 uint16_t light_turns PACKED; /* max number of turns light lasts */
33 uint8_t treasure_locn; /* location of where to store treasures */
34 uint8_t strange; /* !?! not known. */
35
36 uint16_t p_obj_table PACKED; /* pointer to object table */
37 uint16_t p_orig_items PACKED; /* pointer to original items */
38 uint16_t p_obj_link PACKED; /* pointer to link table from noun to object */
39 uint16_t p_obj_descr PACKED; /* pointer to object descriptions */
40 uint16_t p_message PACKED; /* pointer to message pointers */
41 uint16_t p_room_exit PACKED; /* pointer to room exits table */
42 uint16_t p_room_descr PACKED; /* pointer to room descr table */
43
44 uint16_t p_noun_table PACKED; /* pointer to noun table */
45 uint16_t p_verb_table PACKED; /* pointer to verb table */
46
47 uint16_t p_explicit PACKED; /* pointer to explicit action table */
48 uint16_t p_implicit PACKED; /* pointer to implicit actions */
49 };
50
51 // clang-format on
52
53 int max_messages;
54 int max_item_descr;
55
56 uint8_t *ti99_implicit_actions = NULL;
57 uint8_t *ti99_explicit_actions = NULL;
58 size_t ti99_implicit_extent = 0;
59 size_t ti99_explicit_extent = 0;
60
61 uint8_t **VerbActionOffsets;
62
63 static uint16_t FixAddress(uint16_t ina)
64 {
65 return (ina - 0x380 + file_baseline_offset);
66 }
67
68 static uint16_t FixWord(uint16_t word)
69 {
70 if (WeAreBigEndian)
71 return word;
72 else
73 return (((word & 0xFF) << 8) | ((word >> 8) & 0xFF));
74 }
75
76 static uint16_t GetWord(uint8_t *mem)
77 {
78 if (mem < entire_file || mem + 1 >= entire_file + file_length)
79 return 0;
80 uint16_t x;
81 if (WeAreBigEndian) {
82 x = (*(mem + 0) << 8);
83 x += (*(mem + 1) & 0xFF);
84 } else {
85 x = *(uint16_t *)mem;
86 }
87 return FixWord(x);
88 }
89
90 static void GetMaxTI99Messages(struct DATAHEADER dh)
91 {
92 uint8_t *msg;
93 uint16_t msg1;
94
95 msg = (uint8_t *)entire_file + FixAddress(FixWord(dh.p_message));
96 msg1 = FixAddress(GetWord((uint8_t *)msg));
97 max_messages = (msg1 - FixAddress(FixWord(dh.p_message))) / 2;
98 }
99
100 static void GetMaxTI99Items(struct DATAHEADER dh)
101 {
102 uint8_t *msg;
103 uint16_t msg1;
104
105 msg = (uint8_t *)entire_file + FixAddress(FixWord(dh.p_obj_descr));
106 msg1 = FixAddress(GetWord((uint8_t *)msg));
107 max_item_descr = (msg1 - FixAddress(FixWord(dh.p_obj_descr))) / 2;
108 }
109
110 //static void PrintTI99HeaderInfo(struct DATAHEADER header)
111 //{
112 // debug_print("Number of items =\t%d\n", header.num_objects);
113 // debug_print("Number of verbs =\t%d\n", header.num_verbs);
114 // debug_print("Number of nouns =\t%d\n", header.num_nouns);
115 // debug_print("Deadroom =\t%d\n", header.red_room);
116 // debug_print("Max carried items =\t%d\n", header.max_items_carried);
117 // debug_print("Player start location: %d\n", header.begin_locn);
118 // debug_print("Word length =\t%d\n", header.cmd_length);
119 // debug_print("Treasure room: %d\n", header.treasure_locn);
120 // debug_print("Lightsource time left: %d\n", FixWord(header.light_turns));
121 // debug_print("Unknown: %d\n", header.strange);
122 //}
123
124 static GameIDType TryLoadingTI994A(struct DATAHEADER dh, int loud);
125
126 GameIDType DetectTI994A()
127 {
128 int offset = FindCode("\x30\x30\x30\x30\x00\x30\x30\x00\x28\x28", 10);
129 if (offset == -1)
130 return UNKNOWN_GAME;
131
132 file_baseline_offset = offset - 0x589;
133
134 struct DATAHEADER dh;
135 memcpy(&dh, entire_file + 0x8a0 + file_baseline_offset,
136 sizeof(struct DATAHEADER));
137
138 GetMaxTI99Messages(dh);
139 GetMaxTI99Items(dh);
140
141 /* test some pointers for valid game... */
142
143 assert(file_length >= FixAddress(FixWord(dh.p_obj_table)));
144 assert(file_length >= FixAddress(FixWord(dh.p_orig_items)));
145 assert(file_length >= FixAddress(FixWord(dh.p_obj_link)));
146 assert(file_length >= FixAddress(FixWord(dh.p_obj_descr)));
147 assert(file_length >= FixAddress(FixWord(dh.p_message)));
148 assert(file_length >= FixAddress(FixWord(dh.p_room_exit)));
149 assert(file_length >= FixAddress(FixWord(dh.p_room_descr)));
150 assert(file_length >= FixAddress(FixWord(dh.p_noun_table)));
151 assert(file_length >= FixAddress(FixWord(dh.p_verb_table)));
152 assert(file_length >= FixAddress(FixWord(dh.p_explicit)));
153 assert(file_length >= FixAddress(FixWord(dh.p_implicit)));
154
155 return TryLoadingTI994A(dh, Options & DEBUGGING);
156 }
157
158 static uint8_t *GetTI994AWord(uint8_t *string, uint8_t **result, size_t *length)
159 {
160 uint8_t *msg;
161
162 msg = string;
163 *length = msg[0];
164 if (*length == 0 || *length > 100) {
165 *length = 0;
166 *result = NULL;
167 return NULL;
168 }
169 msg++;
170 *result = MemAlloc(*length);
171 memcpy(*result, msg, *length);
172
173 msg += *length;
174
175 return (msg);
176 }
177
178 static char *GetTI994AString(uint16_t table, int table_offset)
179 {
180 uint8_t *msgx, *msgy, *nextword;
181 char *result;
182 uint16_t msg1, msg2;
183 uint8_t buffer[1024];
184 size_t length, total_length = 0;
185
186 uint8_t *game = entire_file;
187
188 msgx = game + FixAddress(FixWord(table));
189
190 msgx += table_offset * 2;
191 msg1 = FixAddress(GetWord((uint8_t *)msgx));
192 msg2 = FixAddress(GetWord((uint8_t *)msgx + 2));
193
194 msgy = game + msg2;
195 msgx = game + msg1;
196
197 while (msgx < msgy && msgx < entire_file + file_length) {
198 msgx = GetTI994AWord(msgx, &nextword, &length);
199 if (length == 0 || nextword == NULL) {
200 return NULL;
201 }
202 if (length > 100) {
203 free(nextword);
204 return NULL;
205 }
206 memcpy(buffer + total_length, nextword, length);
207 free(nextword);
208 total_length += length;
209 if (total_length > 1000)
210 break;
211 if (msgx < msgy)
212 buffer[total_length++] = ' ';
213 }
214 if (total_length == 0)
215 return NULL;
216 total_length++;
217 result = MemAlloc(total_length);
218 memcpy(result, buffer, total_length);
219 result[total_length - 1] = '\0';
220 return result;
221 }
222
223 static void LoadTI994ADict(uint16_t table, int num_words,
224 const char **dict)
225 {
226 uint16_t *wtable;
227 int i;
228 int word_len;
229 char *w1;
230 char *w2;
231
232 /* table is either verb or noun table */
233 wtable = (uint16_t *)(entire_file + FixAddress(FixWord(table)));
234
235 for (i = 0; i <= num_words; i++) {
236 do {
237 w1 = (char *)entire_file + FixAddress(GetWord((uint8_t *)wtable + (i * 2)));
238 w2 = (char *)entire_file + FixAddress(GetWord((uint8_t *)wtable + ((1 + i) * 2)));
239 } while (w1 == w2);
240
241 word_len = w2 - w1;
242
243 if (word_len < 20) {
244 char *text = MemAlloc(word_len + 1);
245 strncpy(text, w1, word_len);
246 text[word_len] = 0;
247 dict[i] = text;
248 }
249 }
250 }
251
252 static void ReadTI99ImplicitActions(struct DATAHEADER dh)
253 {
254 uint8_t *ptr, *implicit_start;
255 int loop_flag;
256
257 implicit_start = entire_file + FixAddress(FixWord(dh.p_implicit));
258 ptr = implicit_start;
259 loop_flag = 0;
260
261 /* fall out, if no auto acts in the game. */
262 if (*ptr == 0x0)
263 loop_flag = 1;
264
265 while (loop_flag == 0) {
266 if (ptr[1] == 0)
267 loop_flag = 1;
268
269 /* skip code chunk */
270 ptr += 1 + ptr[1];
271 }
272
273 ti99_implicit_extent = MIN(file_length - (implicit_start - entire_file), ptr - entire_file);
274 if (ti99_implicit_extent) {
275 ti99_implicit_actions = MemAlloc(ti99_implicit_extent);
276 memcpy(ti99_implicit_actions, implicit_start, ti99_implicit_extent);
277 }
278 }
279
280 static void ReadTI99ExplicitActions(struct DATAHEADER dh)
281 {
282 uint8_t *ptr, *start, *end, *blockstart;
283 uint16_t address;
284 int loop_flag;
285 int i;
286
287 start = entire_file + file_length;
288 end = entire_file;
289
290 size_t explicit_offset = FixAddress(FixWord(dh.p_explicit));
291 blockstart = entire_file + explicit_offset;
292
293 VerbActionOffsets = MemAlloc((dh.num_verbs + 1) * sizeof(uint8_t *));
294
295 for (i = 0; i <= dh.num_verbs; i++) {
296 ptr = blockstart;
297 address = GetWord(ptr + ((i)*2));
298
299 VerbActionOffsets[i] = NULL;
300
301 if (address != 0) {
302 ptr = entire_file + FixAddress(address);
303 if (ptr < start)
304 start = ptr;
305 VerbActionOffsets[i] = ptr;
306 loop_flag = 0;
307
308 while (loop_flag != 1) {
309 if (ptr[1] == 0)
310 loop_flag = 1;
311
312 /* go to next block. */
313 ptr += 1 + ptr[1];
314 if (ptr > end)
315 end = ptr;
316 }
317 }
318 }
319
320 ti99_explicit_extent = end - start;
321 ti99_explicit_actions = MemAlloc(ti99_explicit_extent);
322 memcpy(ti99_explicit_actions, start, ti99_explicit_extent);
323 for (i = 0; i <= dh.num_verbs; i++) {
324 if (VerbActionOffsets[i] != NULL) {
325 VerbActionOffsets[i] = ti99_explicit_actions + (VerbActionOffsets[i] - start);
326 }
327 }
328 }
329
330 static uint8_t *LoadTitleScreen(void)
331 {
332 char buf[3074];
333 int offset = 0;
334 uint8_t *p;
335 int lines;
336
337 /* title screen offset starts at 0x80 */
338 p = entire_file + 0x80 + file_baseline_offset;
339 if (p - entire_file > file_length)
340 return NULL;
341 int parens = 0;
342 for (lines = 0; lines < 24; lines++) {
343 for (int i = 0; i < 40; i++) {
344 char c = *(p++);
345 if (p - entire_file >= file_length)
346 return NULL;
347 if (c & 0x80) /* if not 7-bit ascii */
348 c = '?';
349 switch (c) {
350 case '\\':
351 c = ' ';
352 break;
353 case '(':
354 parens = 1;
355 break;
356 case ')':
357 if (!parens)
358 c = '@';
359 parens = 0;
360 break;
361 case '|':
362 if (*p != ' ')
363 c = 12;
364 break;
365 default:
366 break;
367 }
368 buf[offset++] = c;
369 if (offset >= 3072)
370 return NULL;
371 }
372 buf[offset++] = '\n';
373 }
374
375 buf[offset] = '\0';
376 uint8_t *result = MemAlloc(offset + 1);
377 memcpy(result, buf, offset + 1);
378 return result;
379 }
380
381 static GameIDType TryLoadingTI994A(struct DATAHEADER dh, int loud)
382 {
383 int ni, nw, nr, mc, pr, tr, wl, lt, mn, trm;
384 int ct;
385
386 Room *rp;
387 Item *ip;
388 /* Load the header */
389
390 ni = dh.num_objects;
391 nw = MAX(dh.num_verbs, dh.num_nouns);
392 nr = dh.red_room;
393 mc = dh.max_items_carried;
394 pr = dh.begin_locn;
395 tr = 0;
396 trm = dh.treasure_locn;
397 wl = dh.cmd_length;
398 lt = FixWord(dh.light_turns);
399 mn = max_messages;
400
401 uint8_t *ptr = entire_file;
402
403 GameHeader.NumItems = ni;
404 Items = (Item *)MemAlloc(sizeof(Item) * (ni + 1));
405 GameHeader.NumActions = 0;
406 GameHeader.NumWords = nw;
407 GameHeader.WordLength = wl;
408 Verbs = MemAlloc(sizeof(char *) * (nw + 2));
409 Nouns = MemAlloc(sizeof(char *) * (nw + 2));
410 GameHeader.NumRooms = nr;
411 Rooms = (Room *)MemAlloc(sizeof(Room) * (nr + 1));
412 GameHeader.MaxCarry = mc;
413 GameHeader.PlayerRoom = pr;
414 GameHeader.LightTime = lt;
415 LightRefill = lt;
416 GameHeader.NumMessages = mn;
417 Messages = MemAlloc(sizeof(char *) * (mn + 1));
418 GameHeader.TreasureRoom = trm;
419
420 size_t offset;
421
422 #if defined(__clang__)
423 #pragma mark rooms
424 #endif
425
426 if (SeekIfNeeded(FixAddress(FixWord(dh.p_room_descr)), &offset, &ptr) == 0)
427 return 0;
428
429 ct = 0;
430 rp = Rooms;
431
432 do {
433 rp->Text = GetTI994AString(dh.p_room_descr, ct);
434 if (rp->Text == NULL)
435 rp->Text = ".\0";
436 if (loud)
437 debug_print("Room %d: %s\n", ct, rp->Text);
438 rp->Image = 255;
439 ct++;
440 rp++;
441 } while (ct < nr + 1);
442
443 #if defined(__clang__)
444 #pragma mark messages
445 #endif
446 ct = 0;
447 while (ct < mn + 1) {
448 Messages[ct] = GetTI994AString(dh.p_message, ct);
449 if (Messages[ct] == NULL)
450 Messages[ct] = ".\0";
451 if (loud)
452 debug_print("Message %d: %s\n", ct, Messages[ct]);
453 ct++;
454 }
455
456 #if defined(__clang__)
457 #pragma mark items
458 #endif
459 ct = 0;
460 ip = Items;
461 do {
462 ip->Text = GetTI994AString(dh.p_obj_descr, ct);
463 if (ip->Text == NULL) {
464 ip->Text = ".\0";
465 ip->AutoGet = NULL;
466 }
467 if (ip->Text && ip->Text[0] == '*')
468 tr++;
469 if (loud)
470 debug_print("Item %d: %s\n", ct, ip->Text);
471 ct++;
472 ip++;
473 } while (ct < ni + 1);
474
475 GameHeader.Treasures = tr;
476 if (loud)
477 debug_print("Number of treasures %d\n", GameHeader.Treasures);
478
479 #if defined(__clang__)
480 #pragma mark room connections
481 #endif
482 if (SeekIfNeeded(FixAddress(FixWord(dh.p_room_exit)), &offset, &ptr) == 0)
483 return UNKNOWN_GAME;
484
485 ct = 0;
486 rp = Rooms;
487
488 while (ct < nr + 1) {
489 for (int j = 0; j < 6; j++) {
490 rp->Exits[j] = *(ptr++ - file_baseline_offset);
491 }
492 ct++;
493 rp++;
494 }
495
496 #if defined(__clang__)
497 #pragma mark item locations
498 #endif
499 if (SeekIfNeeded(FixAddress(FixWord(dh.p_orig_items)), &offset, &ptr) == 0)
500 return UNKNOWN_GAME;
501
502 ct = 0;
503 ip = Items;
504 while (ct < ni + 1) {
505 ip->Location = *(ptr++ - file_baseline_offset);
506 ip->InitialLoc = ip->Location;
507 ip++;
508 ct++;
509 }
510
511 #if defined(__clang__)
512 #pragma mark dictionary
513 #endif
514 LoadTI994ADict(dh.p_verb_table, dh.num_verbs + 1, Verbs);
515 LoadTI994ADict(dh.p_noun_table, dh.num_nouns + 1, Nouns);
516
517 for (int i = 1; i <= dh.num_nouns - dh.num_verbs; i++)
518 Verbs[dh.num_verbs + i] = ".\0";
519
520 for (int i = 1; i <= dh.num_verbs - dh.num_nouns; i++)
521 Nouns[dh.num_nouns + i] = ".\0";
522
523 if (loud) {
524 for (int i = 0; i <= GameHeader.NumWords; i++)
525 debug_print("Verb %d: %s\n", i, Verbs[i]);
526 for (int i = 0; i <= GameHeader.NumWords; i++)
527 debug_print("Noun %d: %s\n", i, Nouns[i]);
528 }
529
530 #if defined(__clang__)
531 #pragma mark autoget
532 #endif
533 ct = 0;
534 ip = Items;
535
536 if (ni >= 1024)
537 Fatal("Bad number of items");
538 int objectlinks[1024];
539
540 if (SeekIfNeeded(FixAddress(FixWord(dh.p_obj_link)), &offset, &ptr) == 0)
541 return UNKNOWN_GAME;
542
543 do {
544 objectlinks[ct] = *(ptr++ - file_baseline_offset);
545 if (objectlinks[ct] && objectlinks[ct] <= nw) {
546 ip->AutoGet = (char *)Nouns[objectlinks[ct]];
547 if (ct == 3 && strncmp("bird", Items[ct].Text, 4) == 0)
548 ip->AutoGet = "BIRD";
549 } else {
550 ip->AutoGet = NULL;
551 }
552 ct++;
553 ip++;
554 } while (ct < ni + 1);
555
556 ReadTI99ImplicitActions(dh);
557 ReadTI99ExplicitActions(dh);
558
559 AutoInventory = 1;
560 sys[INVENTORY] = "I'm carrying: ";
561
562 title_screen = (char *)LoadTitleScreen();
563 free(entire_file);
564
565 for (int i = 0; i < MAX_SYSMESS && sysdict_TI994A[i] != NULL; i++) {
566 sys[i] = sysdict_TI994A[i];
567 }
568
569 Options |= TI994A_STYLE;
570 CurrentSys = SYS_TI994A;
571 return TI994A;
572 }