| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <ctype.h> |
| 9 |
#include <stdlib.h> |
| 10 |
#include <string.h> |
| 11 |
|
| 12 |
#include "scott.h" |
| 13 |
|
| 14 |
#include "decompresstext.h" |
| 15 |
#include "detectgame.h" |
| 16 |
#include "line_drawing.h" |
| 17 |
#include "sagadraw.h" |
| 18 |
#include "scottgameinfo.h" |
| 19 |
|
| 20 |
#include "game_specific.h" |
| 21 |
#include "gremlins.h" |
| 22 |
#include "hulk.h" |
| 23 |
#include "robinofsherwood.h" |
| 24 |
#include "seasofblood.h" |
| 25 |
|
| 26 |
#include "apple2detect.h" |
| 27 |
#include "atari8detect.h" |
| 28 |
#include "c64decrunch.h" |
| 29 |
#include "decompressz80.h" |
| 30 |
#include "load_ti99_4a.h" |
| 31 |
|
| 32 |
#include "parser.h" |
| 33 |
|
| 34 |
extern const char *sysdict_zx[MAX_SYSMESS]; |
| 35 |
extern int header[]; |
| 36 |
|
| 37 |
struct dictionaryKey { |
| 38 |
DictionaryType dict; |
| 39 |
const char *signature; |
| 40 |
int len; |
| 41 |
}; |
| 42 |
|
| 43 |
struct dictionaryKey dictKeys[] = { |
| 44 |
{ FOUR_LETTER_UNCOMPRESSED, "AUTO\0GO\0", 8 }, |
| 45 |
{ THREE_LETTER_UNCOMPRESSED, "AUT\0GO\0", 7 }, |
| 46 |
{ FIVE_LETTER_UNCOMPRESSED, "GO\0\0\0\0*CROSS*RUN\0", 17 }, |
| 47 |
{ FOUR_LETTER_COMPRESSED, "aUTOgO\0", 7 }, |
| 48 |
{ GERMAN_C64, "gEHENSTEIGE", 11 }, |
| 49 |
{ GERMAN, "\xc7" |
| 50 |
"EHENSTEIGE", |
| 51 |
10 }, |
| 52 |
{ SPANISH, "\x81\0\0\0\xc9R\0\0ANDAENTR", 16 }, |
| 53 |
{ SPANISH_C64, "\x81\0\0\0iR\0\0ANDAENTR", 16 }, |
| 54 |
{ ITALIAN, "AUTO\0VAI\0\0*ENTR", 15 }, |
| 55 |
{ NOT_A_GAME, NULL, 0 } |
| 56 |
}; |
| 57 |
|
| 58 |
int FindCode(const char *x, int len) |
| 59 |
{ |
| 60 |
unsigned const char *p = entire_file; |
| 61 |
while (p < entire_file + file_length - len) { |
| 62 |
if (memcmp(p, x, len) == 0) { |
| 63 |
return p - entire_file; |
| 64 |
} |
| 65 |
p++; |
| 66 |
} |
| 67 |
return -1; |
| 68 |
} |
| 69 |
|
| 70 |
DictionaryType GetId(size_t *offset) |
| 71 |
{ |
| 72 |
for (int i = 0; dictKeys[i].dict != NOT_A_GAME; i++) { |
| 73 |
*offset = FindCode(dictKeys[i].signature, dictKeys[i].len); |
| 74 |
if (*offset != -1) { |
| 75 |
switch (dictKeys[i].dict) { |
| 76 |
case GERMAN_C64: |
| 77 |
case GERMAN: |
| 78 |
*offset -= 5; |
| 79 |
break; |
| 80 |
case FIVE_LETTER_UNCOMPRESSED: |
| 81 |
*offset -= 6; |
| 82 |
break; |
| 83 |
default: |
| 84 |
break; |
| 85 |
} |
| 86 |
return dictKeys[i].dict; |
| 87 |
} |
| 88 |
} |
| 89 |
|
| 90 |
return NOT_A_GAME; |
| 91 |
} |
| 92 |
|
| 93 |
uint8_t *ReadHeader(uint8_t *ptr) |
| 94 |
{ |
| 95 |
int i, value; |
| 96 |
for (i = 0; i < 15; i++) { |
| 97 |
value = *ptr + 256 * *(ptr + 1); |
| 98 |
header[i] = value; |
| 99 |
ptr += 2; |
| 100 |
} |
| 101 |
return ptr - 1; |
| 102 |
} |
| 103 |
|
| 104 |
int SanityCheckHeader(void) |
| 105 |
{ |
| 106 |
int16_t v = GameHeader.NumItems; |
| 107 |
if (v < 10 || v > 500) |
| 108 |
return 0; |
| 109 |
v = GameHeader.NumActions; |
| 110 |
if (v < 100 || v > 500) |
| 111 |
return 0; |
| 112 |
v = GameHeader.NumWords; |
| 113 |
if (v < 50 || v > 190) |
| 114 |
return 0; |
| 115 |
v = GameHeader.NumRooms; |
| 116 |
if (v < 10 || v > 100) |
| 117 |
return 0; |
| 118 |
|
| 119 |
return 1; |
| 120 |
} |
| 121 |
|
| 122 |
uint8_t *ReadDictionary(struct GameInfo info, uint8_t **pointer, int loud) |
| 123 |
{ |
| 124 |
uint8_t *ptr = *pointer; |
| 125 |
if (info.word_length + 2 > 1024) |
| 126 |
Fatal("Bad word length"); |
| 127 |
char dictword[1024]; |
| 128 |
char c = 0; |
| 129 |
int wordnum = 0; |
| 130 |
int charindex = 0; |
| 131 |
|
| 132 |
int nw = info.number_of_words; |
| 133 |
|
| 134 |
int nv = info.number_of_verbs; |
| 135 |
int nn = info.number_of_nouns; |
| 136 |
|
| 137 |
for (int i = 0; i < nw + 2; i++) { |
| 138 |
Verbs[i] = "."; |
| 139 |
Nouns[i] = "."; |
| 140 |
} |
| 141 |
|
| 142 |
do { |
| 143 |
for (int i = 0; i < info.word_length; i++) { |
| 144 |
c = *(ptr++); |
| 145 |
|
| 146 |
if (info.dictionary == FOUR_LETTER_COMPRESSED || info.dictionary == GERMAN_C64 || info.dictionary == SPANISH_C64) { |
| 147 |
if (charindex == 0) { |
| 148 |
if (c >= 'a') { |
| 149 |
c = toupper(c); |
| 150 |
} else if (c != '.' && c != 0) { |
| 151 |
dictword[charindex++] = '*'; |
| 152 |
} |
| 153 |
} |
| 154 |
dictword[charindex++] = c; |
| 155 |
} else if (info.subtype == LOCALIZED) { |
| 156 |
if (charindex == 0) { |
| 157 |
if (c & 0x80) { |
| 158 |
c = c & 0x7f; |
| 159 |
} else if (c != '.') { |
| 160 |
dictword[charindex++] = '*'; |
| 161 |
} |
| 162 |
} |
| 163 |
dictword[charindex++] = c; |
| 164 |
} else { |
| 165 |
if (c == 0) { |
| 166 |
if (charindex == 0) { |
| 167 |
c = *(ptr++); |
| 168 |
} |
| 169 |
} |
| 170 |
if (c != ' ' && charindex > 0 && dictword[charindex - 1] == ' ') { |
| 171 |
i--; |
| 172 |
charindex--; |
| 173 |
} |
| 174 |
if (c == '*') { |
| 175 |
if (charindex != 0) |
| 176 |
charindex = 0; |
| 177 |
i = -1; |
| 178 |
} |
| 179 |
dictword[charindex++] = c; |
| 180 |
} |
| 181 |
} |
| 182 |
dictword[charindex] = 0; |
| 183 |
if (wordnum < nv) { |
| 184 |
Verbs[wordnum] = MemAlloc(charindex + 1); |
| 185 |
memcpy((char *)Verbs[wordnum], dictword, charindex + 1); |
| 186 |
if (loud) |
| 187 |
debug_print("Verb %d: \"%s\"\n", wordnum, Verbs[wordnum]); |
| 188 |
} else { |
| 189 |
Nouns[wordnum - nv] = MemAlloc(charindex + 1); |
| 190 |
memcpy((char *)Nouns[wordnum - nv], dictword, charindex + 1); |
| 191 |
if (loud) |
| 192 |
debug_print("Noun %d: \"%s\"\n", wordnum - nv, Nouns[wordnum - nv]); |
| 193 |
} |
| 194 |
wordnum++; |
| 195 |
|
| 196 |
if (c > 127) |
| 197 |
return ptr; |
| 198 |
|
| 199 |
charindex = 0; |
| 200 |
} while (wordnum <= nv + nn); |
| 201 |
|
| 202 |
return ptr; |
| 203 |
} |
| 204 |
|
| 205 |
uint8_t *SeekToPos(uint8_t *buf, int offset) |
| 206 |
{ |
| 207 |
if (offset > file_length) |
| 208 |
return NULL; |
| 209 |
return buf + offset; |
| 210 |
} |
| 211 |
|
| 212 |
int SeekIfNeeded(int expected_start, size_t *offset, uint8_t **ptr) |
| 213 |
{ |
| 214 |
if (expected_start != FOLLOWS) { |
| 215 |
*offset = expected_start + file_baseline_offset; |
| 216 |
|
| 217 |
*ptr = SeekToPos(entire_file, *offset); |
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
if (*ptr == 0) |
| 222 |
return 0; |
| 223 |
} |
| 224 |
return 1; |
| 225 |
} |
| 226 |
|
| 227 |
int ParseHeader(int *h, HeaderType type, int *ni, int *na, int *nw, int *nr, |
| 228 |
int *mc, int *pr, int *tr, int *wl, int *lt, int *mn, |
| 229 |
int *trm) |
| 230 |
{ |
| 231 |
switch (type) { |
| 232 |
case NO_HEADER: |
| 233 |
return 0; |
| 234 |
case EARLY: |
| 235 |
*ni = h[1]; |
| 236 |
*na = h[2]; |
| 237 |
*nw = h[3]; |
| 238 |
*nr = h[4]; |
| 239 |
*mc = h[5]; |
| 240 |
*pr = h[6]; |
| 241 |
*tr = h[7]; |
| 242 |
*wl = h[8]; |
| 243 |
*lt = h[9]; |
| 244 |
*mn = h[10]; |
| 245 |
*trm = h[11]; |
| 246 |
break; |
| 247 |
case LATE: |
| 248 |
*ni = h[1]; |
| 249 |
*na = h[2]; |
| 250 |
*nw = h[3]; |
| 251 |
*nr = h[4]; |
| 252 |
*mc = h[5]; |
| 253 |
*wl = h[6]; |
| 254 |
*mn = h[7]; |
| 255 |
*pr = 1; |
| 256 |
*tr = 0; |
| 257 |
*lt = -1; |
| 258 |
*trm = 0; |
| 259 |
break; |
| 260 |
case US_HEADER: |
| 261 |
*ni = h[3]; |
| 262 |
*na = h[2]; |
| 263 |
*nw = h[1]; |
| 264 |
*nr = h[5]; |
| 265 |
*mc = h[6]; |
| 266 |
*pr = h[7]; |
| 267 |
*tr = h[8]; |
| 268 |
*wl = h[0]; |
| 269 |
*lt = h[9]; |
| 270 |
*mn = h[4]; |
| 271 |
*trm = h[10] >> 8; |
| 272 |
break; |
| 273 |
case ROBIN_C64_HEADER: |
| 274 |
*ni = h[1]; |
| 275 |
*na = h[2]; |
| 276 |
*nw = h[6]; |
| 277 |
*nr = h[4]; |
| 278 |
*mc = h[5]; |
| 279 |
*pr = 1; |
| 280 |
*tr = 0; |
| 281 |
*wl = h[7]; |
| 282 |
*lt = -1; |
| 283 |
*mn = h[3]; |
| 284 |
*trm = 0; |
| 285 |
break; |
| 286 |
case GREMLINS_C64_HEADER: |
| 287 |
*ni = h[1]; |
| 288 |
*na = h[2]; |
| 289 |
*nw = h[5]; |
| 290 |
*nr = h[3]; |
| 291 |
*mc = h[6]; |
| 292 |
*pr = h[8]; |
| 293 |
*tr = 0; |
| 294 |
*wl = h[7]; |
| 295 |
*lt = -1; |
| 296 |
*mn = 98; |
| 297 |
*trm = 0; |
| 298 |
break; |
| 299 |
case SUPERGRAN_C64_HEADER: |
| 300 |
*ni = h[3]; |
| 301 |
*na = h[1]; |
| 302 |
*nw = h[2]; |
| 303 |
*nr = h[4]; |
| 304 |
*mc = h[8]; |
| 305 |
*pr = 1; |
| 306 |
*tr = 0; |
| 307 |
*wl = h[6]; |
| 308 |
*lt = -1; |
| 309 |
*mn = h[5]; |
| 310 |
*trm = 0; |
| 311 |
break; |
| 312 |
case SEAS_OF_BLOOD_C64_HEADER: |
| 313 |
*ni = h[0]; |
| 314 |
*na = h[1]; |
| 315 |
*nw = 134; |
| 316 |
*nr = h[3]; |
| 317 |
*mc = h[4]; |
| 318 |
*pr = 1; |
| 319 |
*tr = 0; |
| 320 |
*wl = h[6]; |
| 321 |
*lt = -1; |
| 322 |
*mn = h[2]; |
| 323 |
*trm = 0; |
| 324 |
break; |
| 325 |
case MYSTERIOUS_C64_HEADER: |
| 326 |
*ni = h[1]; |
| 327 |
*na = h[2]; |
| 328 |
*nw = h[3]; |
| 329 |
*nr = h[4]; |
| 330 |
*mc = h[5] & 0xff; |
| 331 |
*pr = h[5] >> 8; |
| 332 |
*tr = h[6]; |
| 333 |
*wl = h[7]; |
| 334 |
*lt = h[8]; |
| 335 |
*mn = h[9]; |
| 336 |
*trm = 0; |
| 337 |
break; |
| 338 |
case ARROW_OF_DEATH_PT_2_C64_HEADER: |
| 339 |
*ni = h[3]; |
| 340 |
*na = h[1]; |
| 341 |
*nw = h[2]; |
| 342 |
*nr = h[4]; |
| 343 |
*mc = h[5] & 0xff; |
| 344 |
*pr = h[5] >> 8; |
| 345 |
*tr = h[6]; |
| 346 |
*wl = h[7]; |
| 347 |
*lt = h[8]; |
| 348 |
*mn = h[9]; |
| 349 |
*trm = 0; |
| 350 |
break; |
| 351 |
case INDIANS_C64_HEADER: |
| 352 |
*ni = h[1]; |
| 353 |
*na = h[2]; |
| 354 |
*nw = h[3]; |
| 355 |
*nr = h[4]; |
| 356 |
*mc = h[5] & 0xff; |
| 357 |
*pr = h[5] >> 8; |
| 358 |
*tr = h[6] & 0xff; |
| 359 |
*wl = h[6] >> 8; |
| 360 |
*lt = h[7] >> 8; |
| 361 |
*mn = h[8] >> 8; |
| 362 |
*trm = 0; |
| 363 |
break; |
| 364 |
default: |
| 365 |
debug_print("Unhandled header type!\n"); |
| 366 |
return 0; |
| 367 |
} |
| 368 |
return 1; |
| 369 |
} |
| 370 |
|
| 371 |
void PrintHeaderInfo(int *h, int ni, int na, int nw, int nr, int mc, int pr, |
| 372 |
int tr, int wl, int lt, int mn, int trm) |
| 373 |
{ |
| 374 |
#if (DEBUG_PRINT) |
| 375 |
uint16_t value; |
| 376 |
for (int i = 0; i < 13; i++) { |
| 377 |
value = h[i]; |
| 378 |
debug_print("b $%X %d: ", 0x494d + 0x3FE5 + i * 2, i); |
| 379 |
debug_print("\t%d (%04x)\n", value, value); |
| 380 |
} |
| 381 |
|
| 382 |
debug_print("Number of items =\t%d\n", ni); |
| 383 |
debug_print("Number of actions =\t%d\n", na); |
| 384 |
debug_print("Number of words =\t%d\n", nw); |
| 385 |
debug_print("Number of rooms =\t%d\n", nr); |
| 386 |
debug_print("Max carried items =\t%d\n", mc); |
| 387 |
debug_print("Word length =\t%d\n", wl); |
| 388 |
debug_print("Number of messages =\t%d\n", mn); |
| 389 |
debug_print("Player start location: %d\n", pr); |
| 390 |
debug_print("Treasure room: %d\n", trm); |
| 391 |
debug_print("Lightsource time left: %d\n", lt); |
| 392 |
debug_print("Number of treasures: %d\n", tr); |
| 393 |
#endif |
| 394 |
} |
| 395 |
|
| 396 |
typedef struct { |
| 397 |
uint8_t *imagedata; |
| 398 |
uint8_t background_colour; |
| 399 |
size_t size; |
| 400 |
} LineImage; |
| 401 |
|
| 402 |
void LoadVectorData(struct GameInfo info, uint8_t *ptr) |
| 403 |
{ |
| 404 |
size_t offset; |
| 405 |
|
| 406 |
if (info.start_of_image_data == FOLLOWS) |
| 407 |
ptr++; |
| 408 |
else if (SeekIfNeeded(info.start_of_image_data, &offset, &ptr) == 0) |
| 409 |
return; |
| 410 |
|
| 411 |
LineImages = MemAlloc(info.number_of_rooms * sizeof(struct line_image)); |
| 412 |
int ct = 0; |
| 413 |
struct line_image *lp = LineImages; |
| 414 |
uint8_t byte = *(ptr++); |
| 415 |
do { |
| 416 |
Rooms[ct].Image = 0; |
| 417 |
if (byte == 0xff) { |
| 418 |
lp->bgcolour = *(ptr++); |
| 419 |
lp->data = ptr; |
| 420 |
} else { |
| 421 |
debug_print("Error! Image data does not start with 0xff!\n"); |
| 422 |
} |
| 423 |
do { |
| 424 |
byte = *(ptr++); |
| 425 |
if (ptr - entire_file >= file_length) { |
| 426 |
debug_print("Error! Image data for image %d cut off!\n", ct); |
| 427 |
if (GameHeader.NumRooms - ct > 1) |
| 428 |
Display(Bottom, "[This copy has %d broken or missing pictures. These have been patched out.]\n\n", GameHeader.NumRooms - ct); |
| 429 |
if (lp->data >= ptr) |
| 430 |
lp->size = 0; |
| 431 |
else |
| 432 |
lp->size = ptr - lp->data - 1; |
| 433 |
for (int i = ct + 2; i < GameHeader.NumRooms; i++) |
| 434 |
Rooms[i].Image = 255; |
| 435 |
return; |
| 436 |
} |
| 437 |
} while (byte != 0xff); |
| 438 |
|
| 439 |
lp->size = ptr - lp->data; |
| 440 |
lp++; |
| 441 |
ct++; |
| 442 |
} while (ct < info.number_of_rooms); |
| 443 |
} |
| 444 |
|
| 445 |
struct LineImage *lineImages = NULL; |
| 446 |
|
| 447 |
GameIDType TryLoadingOld(struct GameInfo info, int dict_start) |
| 448 |
{ |
| 449 |
int ni, na, nw, nr, mc, pr, tr, wl, lt, mn, trm; |
| 450 |
int ct; |
| 451 |
|
| 452 |
Action *ap; |
| 453 |
Room *rp; |
| 454 |
Item *ip; |
| 455 |
|
| 456 |
|
| 457 |
uint8_t *ptr = entire_file; |
| 458 |
file_baseline_offset = dict_start - info.start_of_dictionary; |
| 459 |
debug_print("file_baseline_offset: %d\n", file_baseline_offset); |
| 460 |
size_t offset = info.start_of_header + file_baseline_offset; |
| 461 |
|
| 462 |
ptr = SeekToPos(entire_file, offset); |
| 463 |
if (ptr == 0) |
| 464 |
return UNKNOWN_GAME; |
| 465 |
|
| 466 |
ReadHeader(ptr); |
| 467 |
|
| 468 |
if (!ParseHeader(header, info.header_style, &ni, &na, &nw, &nr, &mc, &pr, |
| 469 |
&tr, &wl, <, &mn, &trm)) |
| 470 |
return UNKNOWN_GAME; |
| 471 |
|
| 472 |
if (ni != info.number_of_items || na != info.number_of_actions || nw != info.number_of_words || nr != info.number_of_rooms || mc != info.max_carried) { |
| 473 |
|
| 474 |
return UNKNOWN_GAME; |
| 475 |
} |
| 476 |
|
| 477 |
GameHeader.NumItems = ni; |
| 478 |
Items = (Item *)MemAlloc(sizeof(Item) * (ni + 1)); |
| 479 |
GameHeader.NumActions = na; |
| 480 |
Actions = (Action *)MemAlloc(sizeof(Action) * (na + 1)); |
| 481 |
GameHeader.NumWords = nw; |
| 482 |
GameHeader.WordLength = wl; |
| 483 |
Verbs = MemAlloc(sizeof(char *) * (nw + 2)); |
| 484 |
Nouns = MemAlloc(sizeof(char *) * (nw + 2)); |
| 485 |
GameHeader.NumRooms = nr; |
| 486 |
Rooms = (Room *)MemAlloc(sizeof(Room) * (nr + 1)); |
| 487 |
GameHeader.MaxCarry = mc; |
| 488 |
GameHeader.PlayerRoom = pr; |
| 489 |
GameHeader.Treasures = tr; |
| 490 |
GameHeader.LightTime = lt; |
| 491 |
LightRefill = lt; |
| 492 |
GameHeader.NumMessages = mn; |
| 493 |
Messages = MemAlloc(sizeof(char *) * (mn + 1)); |
| 494 |
GameHeader.TreasureRoom = trm; |
| 495 |
|
| 496 |
#pragma mark actions |
| 497 |
|
| 498 |
if (SeekIfNeeded(info.start_of_actions, &offset, &ptr) == 0) |
| 499 |
return UNKNOWN_GAME; |
| 500 |
|
| 501 |
ct = 0; |
| 502 |
|
| 503 |
ap = Actions; |
| 504 |
|
| 505 |
uint16_t value, cond, comm; |
| 506 |
|
| 507 |
while (ct < na + 1) { |
| 508 |
value = *(ptr++); |
| 509 |
value += *(ptr++) * 0x100; |
| 510 |
ap->Vocab = value; |
| 511 |
|
| 512 |
cond = 5; |
| 513 |
comm = 2; |
| 514 |
|
| 515 |
for (int j = 0; j < 5; j++) { |
| 516 |
if (j < cond) { |
| 517 |
value = *(ptr++); |
| 518 |
value += *(ptr++) * 0x100; |
| 519 |
} else |
| 520 |
value = 0; |
| 521 |
ap->Condition[j] = value; |
| 522 |
} |
| 523 |
for (int j = 0; j < 2; j++) { |
| 524 |
if (j < comm) { |
| 525 |
value = *(ptr++); |
| 526 |
value += *(ptr++) * 0x100; |
| 527 |
} else |
| 528 |
value = 0; |
| 529 |
ap->Subcommand[j] = value; |
| 530 |
} |
| 531 |
ap++; |
| 532 |
ct++; |
| 533 |
} |
| 534 |
|
| 535 |
#pragma mark room connections |
| 536 |
|
| 537 |
if (SeekIfNeeded(info.start_of_room_connections, &offset, &ptr) == 0) |
| 538 |
return UNKNOWN_GAME; |
| 539 |
|
| 540 |
ct = 0; |
| 541 |
rp = Rooms; |
| 542 |
|
| 543 |
while (ct < nr + 1) { |
| 544 |
for (int j = 0; j < 6; j++) { |
| 545 |
rp->Exits[j] = *(ptr++); |
| 546 |
} |
| 547 |
ct++; |
| 548 |
rp++; |
| 549 |
} |
| 550 |
|
| 551 |
#pragma mark item locations |
| 552 |
|
| 553 |
if (SeekIfNeeded(info.start_of_item_locations, &offset, &ptr) == 0) |
| 554 |
return UNKNOWN_GAME; |
| 555 |
|
| 556 |
ct = 0; |
| 557 |
ip = Items; |
| 558 |
while (ct < ni + 1) { |
| 559 |
ip->Location = *(ptr++); |
| 560 |
ip->InitialLoc = ip->Location; |
| 561 |
ip++; |
| 562 |
ct++; |
| 563 |
} |
| 564 |
|
| 565 |
#pragma mark dictionary |
| 566 |
|
| 567 |
if (SeekIfNeeded(info.start_of_dictionary, &offset, &ptr) == 0) |
| 568 |
return UNKNOWN_GAME; |
| 569 |
|
| 570 |
ptr = ReadDictionary(info, &ptr, 0); |
| 571 |
|
| 572 |
#pragma mark rooms |
| 573 |
|
| 574 |
if (SeekIfNeeded(info.start_of_room_descriptions, &offset, &ptr) == 0) |
| 575 |
return UNKNOWN_GAME; |
| 576 |
|
| 577 |
if (info.start_of_room_descriptions == FOLLOWS) |
| 578 |
ptr++; |
| 579 |
|
| 580 |
ct = 0; |
| 581 |
rp = Rooms; |
| 582 |
|
| 583 |
char text[256]; |
| 584 |
char c = 0; |
| 585 |
int charindex = 0; |
| 586 |
|
| 587 |
do { |
| 588 |
c = *(ptr++); |
| 589 |
text[charindex] = c; |
| 590 |
if (c == 0) { |
| 591 |
rp->Text = MemAlloc(charindex + 1); |
| 592 |
strncpy(rp->Text, text, charindex + 1); |
| 593 |
if (info.number_of_pictures > 0) |
| 594 |
rp->Image = ct - 1; |
| 595 |
else |
| 596 |
rp->Image = 255; |
| 597 |
ct++; |
| 598 |
rp++; |
| 599 |
charindex = 0; |
| 600 |
} else { |
| 601 |
charindex++; |
| 602 |
if (charindex > 255) |
| 603 |
break; |
| 604 |
} |
| 605 |
if (c > 127) |
| 606 |
return UNKNOWN_GAME; |
| 607 |
} while (ct < nr + 1); |
| 608 |
|
| 609 |
#pragma mark messages |
| 610 |
|
| 611 |
if (SeekIfNeeded(info.start_of_messages, &offset, &ptr) == 0) |
| 612 |
return UNKNOWN_GAME; |
| 613 |
|
| 614 |
ct = 0; |
| 615 |
charindex = 0; |
| 616 |
|
| 617 |
while (ct < mn + 1) { |
| 618 |
c = *(ptr++); |
| 619 |
text[charindex] = c; |
| 620 |
if (c == 0) { |
| 621 |
Messages[ct] = MemAlloc(charindex + 1); |
| 622 |
strncpy((char *)Messages[ct], text, charindex + 1); |
| 623 |
ct++; |
| 624 |
charindex = 0; |
| 625 |
} else { |
| 626 |
charindex++; |
| 627 |
if (charindex > 255) |
| 628 |
break; |
| 629 |
} |
| 630 |
} |
| 631 |
|
| 632 |
#pragma mark items |
| 633 |
|
| 634 |
if (SeekIfNeeded(info.start_of_item_descriptions, &offset, &ptr) == 0) |
| 635 |
return UNKNOWN_GAME; |
| 636 |
|
| 637 |
ct = 0; |
| 638 |
ip = Items; |
| 639 |
charindex = 0; |
| 640 |
|
| 641 |
do { |
| 642 |
uint8_t chr = *(ptr++); |
| 643 |
text[charindex] = chr; |
| 644 |
if (chr == 0 || chr > 126) { |
| 645 |
ip->Text = MemAlloc(charindex + 1); |
| 646 |
strncpy(ip->Text, text, charindex + 1); |
| 647 |
ip->Text[charindex] = 0; |
| 648 |
ip->AutoGet = strchr(ip->Text, '/'); |
| 649 |
|
| 650 |
if (ip->AutoGet && strcmp(ip->AutoGet, "//") && strcmp(ip->AutoGet, "/*")) { |
| 651 |
*ip->AutoGet++ = 0; |
| 652 |
char *t = strchr(ip->AutoGet, '/'); |
| 653 |
if (t != NULL) |
| 654 |
*t = 0; |
| 655 |
} |
| 656 |
ct++; |
| 657 |
ip++; |
| 658 |
charindex = 0; |
| 659 |
} else { |
| 660 |
charindex++; |
| 661 |
if (charindex > 255) |
| 662 |
break; |
| 663 |
} |
| 664 |
} while (ct < ni + 1); |
| 665 |
|
| 666 |
#pragma mark line images |
| 667 |
|
| 668 |
if (info.number_of_pictures > 0) { |
| 669 |
LoadVectorData(info, ptr); |
| 670 |
} |
| 671 |
|
| 672 |
#pragma mark System messages |
| 673 |
|
| 674 |
ct = 0; |
| 675 |
if (SeekIfNeeded(info.start_of_system_messages, &offset, &ptr) == 0) |
| 676 |
return UNKNOWN_GAME; |
| 677 |
|
| 678 |
charindex = 0; |
| 679 |
|
| 680 |
do { |
| 681 |
c = *(ptr++); |
| 682 |
text[charindex] = c; |
| 683 |
if (c == 0 || c == 0x0d) { |
| 684 |
if (charindex > 0) { |
| 685 |
if (c == 0x0d) |
| 686 |
charindex++; |
| 687 |
char *newmess = MemAlloc(charindex + 1); |
| 688 |
text[charindex] = '\0'; |
| 689 |
strncpy(newmess, text, charindex + 1); |
| 690 |
system_messages[ct] = newmess; |
| 691 |
ct++; |
| 692 |
charindex = 0; |
| 693 |
} |
| 694 |
} else { |
| 695 |
charindex++; |
| 696 |
} |
| 697 |
|
| 698 |
if (c != 0 && c != 0x0d && c != '\x83' && c != '\xc9' && c > 127) |
| 699 |
break; |
| 700 |
} while (ct < 40); |
| 701 |
|
| 702 |
charindex = 0; |
| 703 |
|
| 704 |
if (SeekIfNeeded(info.start_of_directions, &offset, &ptr) == 0) |
| 705 |
return UNKNOWN_GAME; |
| 706 |
|
| 707 |
ct = 0; |
| 708 |
do { |
| 709 |
c = *(ptr++); |
| 710 |
text[charindex] = c; |
| 711 |
if (c == 0 || c == 0x0d) { |
| 712 |
if (charindex > 0) { |
| 713 |
char *newmess = MemAlloc(charindex + 2); |
| 714 |
if (c == 0x0d) |
| 715 |
charindex++; |
| 716 |
text[charindex] = '\0'; |
| 717 |
strncpy(newmess, text, charindex + 1); |
| 718 |
sys[ct] = newmess; |
| 719 |
ct++; |
| 720 |
charindex = 0; |
| 721 |
} |
| 722 |
} else { |
| 723 |
charindex++; |
| 724 |
} |
| 725 |
|
| 726 |
if (c != 0 && c != 0x0d && c > 127) |
| 727 |
break; |
| 728 |
} while (ct < 6); |
| 729 |
|
| 730 |
return info.gameID; |
| 731 |
} |
| 732 |
|
| 733 |
GameIDType TryLoading(struct GameInfo info, int dict_start, int loud) |
| 734 |
{ |
| 735 |
|
| 736 |
if (info.gameID == HULK || info.gameID == HULK_C64) |
| 737 |
return LoadBinaryDatabase(entire_file, file_length, info, dict_start); |
| 738 |
|
| 739 |
if (info.type == OLD_STYLE) |
| 740 |
return TryLoadingOld(info, dict_start); |
| 741 |
|
| 742 |
int ni, na, nw, nr, mc, pr, tr, wl, lt, mn, trm; |
| 743 |
int ct; |
| 744 |
|
| 745 |
Action *ap; |
| 746 |
Room *rp; |
| 747 |
Item *ip; |
| 748 |
|
| 749 |
|
| 750 |
uint8_t *ptr = entire_file; |
| 751 |
|
| 752 |
if (loud) { |
| 753 |
debug_print("dict_start:%x\n", dict_start); |
| 754 |
debug_print(" info.start_of_dictionary:%x\n", info.start_of_dictionary); |
| 755 |
} |
| 756 |
file_baseline_offset = dict_start - info.start_of_dictionary; |
| 757 |
|
| 758 |
if (loud) |
| 759 |
debug_print("file_baseline_offset:%x (%d)\n", file_baseline_offset, |
| 760 |
file_baseline_offset); |
| 761 |
|
| 762 |
size_t offset = info.start_of_header + file_baseline_offset; |
| 763 |
|
| 764 |
ptr = SeekToPos(entire_file, offset); |
| 765 |
if (ptr == 0) |
| 766 |
return UNKNOWN_GAME; |
| 767 |
|
| 768 |
ReadHeader(ptr); |
| 769 |
|
| 770 |
if (!ParseHeader(header, info.header_style, &ni, &na, &nw, &nr, &mc, &pr, |
| 771 |
&tr, &wl, <, &mn, &trm)) |
| 772 |
return UNKNOWN_GAME; |
| 773 |
|
| 774 |
if (loud) |
| 775 |
PrintHeaderInfo(header, ni, na, nw, nr, mc, pr, tr, wl, lt, mn, trm); |
| 776 |
|
| 777 |
GameHeader.NumItems = ni; |
| 778 |
GameHeader.NumActions = na; |
| 779 |
GameHeader.NumWords = nw; |
| 780 |
GameHeader.WordLength = wl; |
| 781 |
GameHeader.NumRooms = nr; |
| 782 |
GameHeader.MaxCarry = mc; |
| 783 |
GameHeader.PlayerRoom = pr; |
| 784 |
GameHeader.Treasures = tr; |
| 785 |
GameHeader.LightTime = lt; |
| 786 |
LightRefill = lt; |
| 787 |
GameHeader.NumMessages = mn; |
| 788 |
GameHeader.TreasureRoom = trm; |
| 789 |
|
| 790 |
if (SanityCheckHeader() == 0) { |
| 791 |
return UNKNOWN_GAME; |
| 792 |
} |
| 793 |
|
| 794 |
if (loud) { |
| 795 |
debug_print("Found a valid header at position 0x%zx\n", offset); |
| 796 |
debug_print("Expected: 0x%x\n", |
| 797 |
info.start_of_header + file_baseline_offset); |
| 798 |
} |
| 799 |
|
| 800 |
if (ni != info.number_of_items || na != info.number_of_actions || nw != info.number_of_words || nr != info.number_of_rooms || mc != info.max_carried) { |
| 801 |
if (loud) |
| 802 |
debug_print("Non-matching header\n"); |
| 803 |
return UNKNOWN_GAME; |
| 804 |
} |
| 805 |
|
| 806 |
Items = (Item *)MemAlloc(sizeof(Item) * (ni + 1)); |
| 807 |
Actions = (Action *)MemAlloc(sizeof(Action) * (na + 1)); |
| 808 |
Verbs = MemAlloc(sizeof(char *) * (nw + 2)); |
| 809 |
Nouns = MemAlloc(sizeof(char *) * (nw + 2)); |
| 810 |
Rooms = (Room *)MemAlloc(sizeof(Room) * (nr + 1)); |
| 811 |
Messages = MemAlloc(sizeof(char *) * (mn + 1)); |
| 812 |
|
| 813 |
int compressed = (info.dictionary == FOUR_LETTER_COMPRESSED); |
| 814 |
|
| 815 |
#pragma mark room images |
| 816 |
|
| 817 |
if (info.start_of_room_image_list != 0) { |
| 818 |
if (SeekIfNeeded(info.start_of_room_image_list, &offset, &ptr) == 0) |
| 819 |
return UNKNOWN_GAME; |
| 820 |
|
| 821 |
rp = Rooms; |
| 822 |
|
| 823 |
for (ct = 0; ct <= GameHeader.NumRooms; ct++) { |
| 824 |
rp->Image = *(ptr++); |
| 825 |
rp++; |
| 826 |
} |
| 827 |
} |
| 828 |
#pragma mark Item flags |
| 829 |
|
| 830 |
if (info.start_of_item_flags != 0) { |
| 831 |
|
| 832 |
if (SeekIfNeeded(info.start_of_item_flags, &offset, &ptr) == 0) |
| 833 |
return UNKNOWN_GAME; |
| 834 |
|
| 835 |
ip = Items; |
| 836 |
|
| 837 |
for (ct = 0; ct <= GameHeader.NumItems; ct++) { |
| 838 |
ip->Flag = *(ptr++); |
| 839 |
ip++; |
| 840 |
} |
| 841 |
} |
| 842 |
|
| 843 |
#pragma mark item images |
| 844 |
|
| 845 |
if (info.start_of_item_image_list != 0) { |
| 846 |
|
| 847 |
if (SeekIfNeeded(info.start_of_item_image_list, &offset, &ptr) == 0) |
| 848 |
return UNKNOWN_GAME; |
| 849 |
|
| 850 |
ip = Items; |
| 851 |
|
| 852 |
for (ct = 0; ct <= GameHeader.NumItems; ct++) { |
| 853 |
ip->Image = *(ptr++); |
| 854 |
ip++; |
| 855 |
} |
| 856 |
if (loud) |
| 857 |
debug_print("Offset after reading item images: %lx\n", |
| 858 |
ptr - entire_file - file_baseline_offset); |
| 859 |
} |
| 860 |
|
| 861 |
#pragma mark actions |
| 862 |
|
| 863 |
if (SeekIfNeeded(info.start_of_actions, &offset, &ptr) == 0) |
| 864 |
return UNKNOWN_GAME; |
| 865 |
|
| 866 |
ct = 0; |
| 867 |
|
| 868 |
ap = Actions; |
| 869 |
|
| 870 |
uint16_t value, cond, comm; |
| 871 |
|
| 872 |
while (ct < na + 1) { |
| 873 |
value = *(ptr++); |
| 874 |
value += *(ptr++) * 0x100; |
| 875 |
ap->Vocab = value; |
| 876 |
|
| 877 |
if (info.actions_style == COMPRESSED) { |
| 878 |
value = *(ptr++); |
| 879 |
cond = value & 0x1f; |
| 880 |
if (cond > 5) { |
| 881 |
debug_print("Condition error at action %d!\n", ct); |
| 882 |
cond = 5; |
| 883 |
} |
| 884 |
comm = (value & 0xe0) >> 5; |
| 885 |
if (comm > 2) { |
| 886 |
debug_print("Command error at action %d!\n", ct); |
| 887 |
comm = 2; |
| 888 |
} |
| 889 |
} else { |
| 890 |
cond = 5; |
| 891 |
comm = 2; |
| 892 |
} |
| 893 |
for (int j = 0; j < 5; j++) { |
| 894 |
if (j < cond) { |
| 895 |
value = *(ptr++); |
| 896 |
value += *(ptr++) * 0x100; |
| 897 |
} else |
| 898 |
value = 0; |
| 899 |
ap->Condition[j] = value; |
| 900 |
} |
| 901 |
for (int j = 0; j < 2; j++) { |
| 902 |
if (j < comm) { |
| 903 |
value = *(ptr++); |
| 904 |
value += *(ptr++) * 0x100; |
| 905 |
} else |
| 906 |
value = 0; |
| 907 |
ap->Subcommand[j] = value; |
| 908 |
} |
| 909 |
|
| 910 |
ap++; |
| 911 |
ct++; |
| 912 |
} |
| 913 |
if (loud) |
| 914 |
debug_print("Offset after reading actions: %lx\n", |
| 915 |
ptr - entire_file - file_baseline_offset); |
| 916 |
|
| 917 |
#pragma mark dictionary |
| 918 |
|
| 919 |
if (SeekIfNeeded(info.start_of_dictionary, &offset, &ptr) == 0) |
| 920 |
return UNKNOWN_GAME; |
| 921 |
|
| 922 |
ptr = ReadDictionary(info, &ptr, loud); |
| 923 |
|
| 924 |
if (loud) |
| 925 |
debug_print("Offset after reading dictionary: %lx\n", |
| 926 |
ptr - entire_file - file_baseline_offset); |
| 927 |
|
| 928 |
#pragma mark rooms |
| 929 |
|
| 930 |
if (info.start_of_room_descriptions != 0) { |
| 931 |
if (SeekIfNeeded(info.start_of_room_descriptions, &offset, &ptr) == 0) |
| 932 |
return UNKNOWN_GAME; |
| 933 |
|
| 934 |
ct = 0; |
| 935 |
rp = Rooms; |
| 936 |
|
| 937 |
char text[256]; |
| 938 |
char c = 0; |
| 939 |
int charindex = 0; |
| 940 |
|
| 941 |
if (!compressed) { |
| 942 |
do { |
| 943 |
c = *(ptr++); |
| 944 |
text[charindex] = c; |
| 945 |
if (c == 0) { |
| 946 |
rp->Text = MemAlloc(charindex + 1); |
| 947 |
strncpy(rp->Text, text, charindex + 1); |
| 948 |
if (loud) |
| 949 |
debug_print("Room %d: %s\n", ct, rp->Text); |
| 950 |
ct++; |
| 951 |
rp++; |
| 952 |
charindex = 0; |
| 953 |
} else { |
| 954 |
charindex++; |
| 955 |
if (charindex > 255) |
| 956 |
break; |
| 957 |
} |
| 958 |
if (c > 127) |
| 959 |
return UNKNOWN_GAME; |
| 960 |
} while (ct < nr + 1); |
| 961 |
} else { |
| 962 |
do { |
| 963 |
rp->Text = DecompressText(ptr, ct); |
| 964 |
if (rp->Text == NULL) |
| 965 |
return UNKNOWN_GAME; |
| 966 |
*(rp->Text) = tolower(*(rp->Text)); |
| 967 |
ct++; |
| 968 |
rp++; |
| 969 |
} while (ct < nr); |
| 970 |
} |
| 971 |
} |
| 972 |
|
| 973 |
#pragma mark room connections |
| 974 |
|
| 975 |
if (SeekIfNeeded(info.start_of_room_connections, &offset, &ptr) == 0) |
| 976 |
return UNKNOWN_GAME; |
| 977 |
|
| 978 |
ct = 0; |
| 979 |
rp = Rooms; |
| 980 |
|
| 981 |
while (ct < nr + 1) { |
| 982 |
for (int j = 0; j < 6; j++) { |
| 983 |
rp->Exits[j] = *(ptr++); |
| 984 |
} |
| 985 |
|
| 986 |
ct++; |
| 987 |
rp++; |
| 988 |
} |
| 989 |
|
| 990 |
#pragma mark messages |
| 991 |
|
| 992 |
if (SeekIfNeeded(info.start_of_messages, &offset, &ptr) == 0) |
| 993 |
return UNKNOWN_GAME; |
| 994 |
|
| 995 |
ct = 0; |
| 996 |
char text[256]; |
| 997 |
char c = 0; |
| 998 |
int charindex = 0; |
| 999 |
|
| 1000 |
if (compressed) { |
| 1001 |
while (ct < mn + 1) { |
| 1002 |
Messages[ct] = DecompressText(ptr, ct); |
| 1003 |
if (loud) |
| 1004 |
debug_print("Message %d: \"%s\"\n", ct, Messages[ct]); |
| 1005 |
if (Messages[ct] == NULL) |
| 1006 |
return UNKNOWN_GAME; |
| 1007 |
ct++; |
| 1008 |
} |
| 1009 |
} else { |
| 1010 |
while (ct < mn + 1) { |
| 1011 |
c = *(ptr++); |
| 1012 |
text[charindex] = c; |
| 1013 |
if (c == 0) { |
| 1014 |
Messages[ct] = MemAlloc(charindex + 1); |
| 1015 |
strncpy((char *)Messages[ct], text, charindex + 1); |
| 1016 |
if (loud) |
| 1017 |
debug_print("Message %d: \"%s\"\n", ct, Messages[ct]); |
| 1018 |
ct++; |
| 1019 |
charindex = 0; |
| 1020 |
} else { |
| 1021 |
charindex++; |
| 1022 |
if (charindex > 255) |
| 1023 |
break; |
| 1024 |
} |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
#pragma mark items |
| 1029 |
|
| 1030 |
if (SeekIfNeeded(info.start_of_item_descriptions, &offset, &ptr) == 0) |
| 1031 |
return UNKNOWN_GAME; |
| 1032 |
|
| 1033 |
ct = 0; |
| 1034 |
ip = Items; |
| 1035 |
charindex = 0; |
| 1036 |
|
| 1037 |
if (compressed) { |
| 1038 |
do { |
| 1039 |
ip->Text = DecompressText(ptr, ct); |
| 1040 |
ip->AutoGet = NULL; |
| 1041 |
if (ip->Text != NULL && ip->Text[0] != '.') { |
| 1042 |
if (loud) |
| 1043 |
debug_print("Item %d: %s\n", ct, ip->Text); |
| 1044 |
ip->AutoGet = strchr(ip->Text, '.'); |
| 1045 |
if (ip->AutoGet) { |
| 1046 |
*ip->AutoGet++ = 0; |
| 1047 |
ip->AutoGet++; |
| 1048 |
char *t = strchr(ip->AutoGet, '.'); |
| 1049 |
if (t != NULL) |
| 1050 |
*t = 0; |
| 1051 |
for (int i = 1; i < GameHeader.WordLength; i++) |
| 1052 |
*(ip->AutoGet + i) = toupper(*(ip->AutoGet + i)); |
| 1053 |
} |
| 1054 |
} |
| 1055 |
ct++; |
| 1056 |
ip++; |
| 1057 |
} while (ct < ni + 1); |
| 1058 |
} else { |
| 1059 |
do { |
| 1060 |
c = *(ptr++); |
| 1061 |
text[charindex] = c; |
| 1062 |
if (c == 0) { |
| 1063 |
ip->Text = MemAlloc(charindex + 1); |
| 1064 |
strncpy(ip->Text, text, charindex + 1); |
| 1065 |
if (loud) |
| 1066 |
debug_print("Item %d: %s\n", ct, ip->Text); |
| 1067 |
ip->AutoGet = strchr(ip->Text, '/'); |
| 1068 |
|
| 1069 |
if (ip->AutoGet && strcmp(ip->AutoGet, "//") && strcmp(ip->AutoGet, "/*")) { |
| 1070 |
*ip->AutoGet++ = 0; |
| 1071 |
char *t = strchr(ip->AutoGet, '/'); |
| 1072 |
if (t != NULL) |
| 1073 |
*t = 0; |
| 1074 |
} |
| 1075 |
ct++; |
| 1076 |
ip++; |
| 1077 |
charindex = 0; |
| 1078 |
} else { |
| 1079 |
charindex++; |
| 1080 |
if (charindex > 255) |
| 1081 |
break; |
| 1082 |
} |
| 1083 |
} while (ct < ni + 1); |
| 1084 |
} |
| 1085 |
|
| 1086 |
#pragma mark item locations |
| 1087 |
|
| 1088 |
if (SeekIfNeeded(info.start_of_item_locations, &offset, &ptr) == 0) |
| 1089 |
return UNKNOWN_GAME; |
| 1090 |
|
| 1091 |
ct = 0; |
| 1092 |
ip = Items; |
| 1093 |
while (ct < ni + 1) { |
| 1094 |
ip->Location = *(ptr++); |
| 1095 |
ip->InitialLoc = ip->Location; |
| 1096 |
if (Items[ct].Text && ip->Location < nr && Rooms[ip->Location].Text) |
| 1097 |
debug_print("Location of item %d, \"%s\":%d\n", ct, Items[ct].Text, ip->Location); |
| 1098 |
ip++; |
| 1099 |
ct++; |
| 1100 |
} |
| 1101 |
|
| 1102 |
#pragma mark vector images |
| 1103 |
|
| 1104 |
if (info.number_of_pictures > 0 && info.picture_format_version == 99) { |
| 1105 |
LoadVectorData(info, ptr); |
| 1106 |
} |
| 1107 |
|
| 1108 |
#pragma mark System messages |
| 1109 |
|
| 1110 |
if (SeekIfNeeded(info.start_of_system_messages, &offset, &ptr) == 0) |
| 1111 |
return info.gameID; |
| 1112 |
jumpSysMess: |
| 1113 |
ptr = SeekToPos(entire_file, offset); |
| 1114 |
ct = 0; |
| 1115 |
charindex = 0; |
| 1116 |
|
| 1117 |
do { |
| 1118 |
c = *(ptr++); |
| 1119 |
if (ptr - entire_file > file_length || ptr < entire_file) { |
| 1120 |
debug_print("Read out of bounds!\n"); |
| 1121 |
return UNKNOWN_GAME; |
| 1122 |
} |
| 1123 |
if (charindex > 255) |
| 1124 |
charindex = 0; |
| 1125 |
text[charindex] = c; |
| 1126 |
if (c == 0 || c == 0x0d) { |
| 1127 |
if (charindex > 0) { |
| 1128 |
if (c == 0x0d) |
| 1129 |
charindex++; |
| 1130 |
if (ct == 0 && (info.subtype & (C64 | ENGLISH)) == (C64 | ENGLISH) && memcmp(text, "NORTH", 5) != 0) { |
| 1131 |
offset--; |
| 1132 |
goto jumpSysMess; |
| 1133 |
} |
| 1134 |
char *newmess = MemAlloc(charindex + 1); |
| 1135 |
text[charindex] = '\0'; |
| 1136 |
strncpy(newmess, text, charindex + 1); |
| 1137 |
system_messages[ct] = newmess; |
| 1138 |
if (loud) |
| 1139 |
debug_print("system_messages %d: \"%s\"\n", ct, |
| 1140 |
system_messages[ct]); |
| 1141 |
ct++; |
| 1142 |
charindex = 0; |
| 1143 |
} |
| 1144 |
} else { |
| 1145 |
charindex++; |
| 1146 |
} |
| 1147 |
} while (ct < 45); |
| 1148 |
|
| 1149 |
if (loud) |
| 1150 |
debug_print("Offset after reading system messages: %lx\n", |
| 1151 |
ptr - entire_file); |
| 1152 |
|
| 1153 |
if ((info.subtype & (C64 | ENGLISH)) == (C64 | ENGLISH)) { |
| 1154 |
return info.gameID; |
| 1155 |
} |
| 1156 |
|
| 1157 |
if (SeekIfNeeded(info.start_of_directions, &offset, &ptr) == 0) |
| 1158 |
return UNKNOWN_GAME; |
| 1159 |
|
| 1160 |
charindex = 0; |
| 1161 |
|
| 1162 |
ct = 0; |
| 1163 |
do { |
| 1164 |
c = *(ptr++); |
| 1165 |
text[charindex] = c; |
| 1166 |
if (c == 0 || c == 0x0d) { |
| 1167 |
if (charindex > 0) { |
| 1168 |
char *newmess = MemAlloc(charindex + 2); |
| 1169 |
if (c == 0x0d) |
| 1170 |
charindex++; |
| 1171 |
text[charindex] = '\0'; |
| 1172 |
strncpy(newmess, text, charindex + 1); |
| 1173 |
sys[ct] = newmess; |
| 1174 |
ct++; |
| 1175 |
charindex = 0; |
| 1176 |
} |
| 1177 |
} else { |
| 1178 |
charindex++; |
| 1179 |
} |
| 1180 |
|
| 1181 |
if (c != 0 && c != 0x0d && c > 127) |
| 1182 |
break; |
| 1183 |
} while (ct < 6); |
| 1184 |
|
| 1185 |
return info.gameID; |
| 1186 |
} |
| 1187 |
|
| 1188 |
int IsMysterious(void) |
| 1189 |
{ |
| 1190 |
for (int i = 0; games[i].Title != NULL; i++) { |
| 1191 |
if (games[i].subtype & MYSTERIOUS) { |
| 1192 |
if (games[i].number_of_items == GameHeader.NumItems && games[i].number_of_actions == GameHeader.NumActions && games[i].number_of_words == GameHeader.NumWords && games[i].number_of_rooms == GameHeader.NumRooms && games[i].max_carried == GameHeader.MaxCarry && games[i].word_length == GameHeader.WordLength && games[i].number_of_messages == GameHeader.NumMessages) |
| 1193 |
return 1; |
| 1194 |
} |
| 1195 |
} |
| 1196 |
|
| 1197 |
return 0; |
| 1198 |
} |
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
uint8_t *DecompressParsec(uint8_t *start, uint8_t *end, uint8_t *dataptr) |
| 1205 |
{ |
| 1206 |
if (dataptr - 3 < entire_file) { |
| 1207 |
return NULL; |
| 1208 |
} |
| 1209 |
uint8_t *pos = start; |
| 1210 |
|
| 1211 |
while (pos != end && dataptr - 3 > entire_file) { |
| 1212 |
if (pos <= entire_file) { |
| 1213 |
return NULL; |
| 1214 |
} |
| 1215 |
uint8_t *nextptr = dataptr - 2; |
| 1216 |
if ((*dataptr & 0x80) == 0) { |
| 1217 |
int repeats = *(dataptr - 1) + *dataptr * 0x100; |
| 1218 |
for (int i = 0; i < repeats && nextptr > entire_file && pos > entire_file; i++) { |
| 1219 |
*pos = *nextptr; |
| 1220 |
pos--; |
| 1221 |
nextptr--; |
| 1222 |
} |
| 1223 |
} else { |
| 1224 |
uint8_t val1 = *(dataptr - 1); |
| 1225 |
uint8_t val2 = *(dataptr - 2); |
| 1226 |
nextptr = dataptr - 3; |
| 1227 |
int repeats = *dataptr & 0x7f; |
| 1228 |
for (int i = 0; i < repeats && pos > entire_file; i++) { |
| 1229 |
*pos = val1; |
| 1230 |
pos--; |
| 1231 |
*pos = val2; |
| 1232 |
pos--; |
| 1233 |
} |
| 1234 |
} |
| 1235 |
dataptr = nextptr; |
| 1236 |
} |
| 1237 |
return dataptr; |
| 1238 |
} |
| 1239 |
|
| 1240 |
GameIDType DetectZXSpectrum(void) |
| 1241 |
{ |
| 1242 |
GameIDType detectedGame = UNKNOWN_GAME; |
| 1243 |
int wasz80 = 0; |
| 1244 |
uint8_t *uncompressed = DecompressZ80(entire_file, file_length); |
| 1245 |
if (uncompressed != NULL) { |
| 1246 |
wasz80 = 1; |
| 1247 |
free(entire_file); |
| 1248 |
entire_file = uncompressed; |
| 1249 |
file_length = 0xc000; |
| 1250 |
} |
| 1251 |
|
| 1252 |
size_t offset; |
| 1253 |
|
| 1254 |
DictionaryType dict_type = GetId(&offset); |
| 1255 |
|
| 1256 |
if (dict_type == NOT_A_GAME) |
| 1257 |
return UNKNOWN_GAME; |
| 1258 |
|
| 1259 |
for (int i = 0; games[i].Title != NULL; i++) { |
| 1260 |
if (games[i].dictionary == dict_type) { |
| 1261 |
detectedGame = TryLoading(games[i], offset, 0); |
| 1262 |
if (detectedGame != UNKNOWN_GAME) { |
| 1263 |
free(Game); |
| 1264 |
Game = &games[i]; |
| 1265 |
break; |
| 1266 |
} |
| 1267 |
} |
| 1268 |
} |
| 1269 |
|
| 1270 |
if (!detectedGame && wasz80) { |
| 1271 |
uint8_t *mem = entire_file; |
| 1272 |
uint16_t HL = mem[0x1b42] + mem[0x1b43] * 0x100 - 0x4000; |
| 1273 |
uint8_t *result = 0; |
| 1274 |
if (HL < file_length) |
| 1275 |
result = DecompressParsec(&mem[0x0fff], &mem[0x07ff], &mem[HL]); |
| 1276 |
if (result) { |
| 1277 |
uint16_t BC = mem[0x1b48] + mem[0x1b49] * 0x100 - 0x4000; |
| 1278 |
uint16_t DE = mem[0x1b4b] + mem[0x1b4c] * 0x100 - 0x4000; |
| 1279 |
DecompressParsec(&mem[BC], &mem[DE], result); |
| 1280 |
dict_type = GetId(&offset); |
| 1281 |
if (dict_type == NOT_A_GAME) |
| 1282 |
Fatal("Unsupported game!"); |
| 1283 |
for (int i = 0; games[i].Title != NULL; i++) { |
| 1284 |
if (games[i].dictionary == dict_type) { |
| 1285 |
if (TryLoading(games[i], offset, 1)) { |
| 1286 |
free(Game); |
| 1287 |
Game = &games[i]; |
| 1288 |
detectedGame = Game->gameID; |
| 1289 |
break; |
| 1290 |
} |
| 1291 |
} |
| 1292 |
} |
| 1293 |
} |
| 1294 |
} |
| 1295 |
|
| 1296 |
if (!detectedGame) |
| 1297 |
Fatal("Unsupported game!"); |
| 1298 |
|
| 1299 |
return detectedGame; |
| 1300 |
} |
| 1301 |
|
| 1302 |
GameIDType DetectGame(const char *file_name) |
| 1303 |
{ |
| 1304 |
FILE *f = fopen(file_name, "r"); |
| 1305 |
if (f == NULL) |
| 1306 |
Fatal("Cannot open game"); |
| 1307 |
|
| 1308 |
for (int i = 0; i < NUMBER_OF_DIRECTIONS; i++) |
| 1309 |
Directions[i] = EnglishDirections[i]; |
| 1310 |
for (int i = 0; i < NUMBER_OF_SKIPPABLE_WORDS; i++) |
| 1311 |
SkipList[i] = EnglishSkipList[i]; |
| 1312 |
for (int i = 0; i < NUMBER_OF_DELIMITERS; i++) |
| 1313 |
DelimiterList[i] = EnglishDelimiterList[i]; |
| 1314 |
for (int i = 0; i < NUMBER_OF_EXTRA_NOUNS; i++) |
| 1315 |
ExtraNouns[i] = EnglishExtraNouns[i]; |
| 1316 |
|
| 1317 |
file_length = GetFileLength(f); |
| 1318 |
|
| 1319 |
if (file_length > MAX_GAMEFILE_SIZE) { |
| 1320 |
debug_print("File too large to be a vaild game file (%zu bytes, max is %d)\n", |
| 1321 |
file_length, MAX_GAMEFILE_SIZE); |
| 1322 |
return UNKNOWN_GAME; |
| 1323 |
} |
| 1324 |
|
| 1325 |
Game = (struct GameInfo *)MemAlloc(sizeof(struct GameInfo)); |
| 1326 |
memset(Game, 0, sizeof(struct GameInfo)); |
| 1327 |
|
| 1328 |
|
| 1329 |
GameIDType detectedGame = LoadDatabase(f, Options & DEBUGGING); |
| 1330 |
|
| 1331 |
if (detectedGame == UNKNOWN_GAME) { |
| 1332 |
entire_file = MemAlloc(file_length); |
| 1333 |
fseek(f, 0, SEEK_SET); |
| 1334 |
size_t result = fread(entire_file, 1, file_length, f); |
| 1335 |
fclose(f); |
| 1336 |
if (result == 0) |
| 1337 |
Fatal("File empty or read error!"); |
| 1338 |
|
| 1339 |
detectedGame = DetectTI994A(); |
| 1340 |
|
| 1341 |
if (detectedGame == UNKNOWN_GAME) |
| 1342 |
detectedGame = DetectC64(&entire_file, &file_length); |
| 1343 |
|
| 1344 |
if (detectedGame == UNKNOWN_GAME) { |
| 1345 |
detectedGame = DetectAtari8(&entire_file, &file_length); |
| 1346 |
} |
| 1347 |
|
| 1348 |
if (detectedGame == UNKNOWN_GAME) { |
| 1349 |
detectedGame = DetectApple2(&entire_file, &file_length); |
| 1350 |
} |
| 1351 |
|
| 1352 |
if (detectedGame == UNKNOWN_GAME) { |
| 1353 |
detectedGame = DetectZXSpectrum(); |
| 1354 |
} |
| 1355 |
|
| 1356 |
if (detectedGame == UNKNOWN_GAME) |
| 1357 |
return UNKNOWN_GAME; |
| 1358 |
} |
| 1359 |
|
| 1360 |
if (detectedGame == HULK_US) { |
| 1361 |
CurrentGame = HULK_US; |
| 1362 |
Game->type = US_VARIANT; |
| 1363 |
} |
| 1364 |
|
| 1365 |
if (detectedGame == SCOTTFREE || detectedGame == TI994A) |
| 1366 |
CurrentGame = detectedGame; |
| 1367 |
|
| 1368 |
if (IsMysterious()) { |
| 1369 |
Options = Options | SCOTTLIGHT | PREHISTORIC_LAMP; |
| 1370 |
} |
| 1371 |
|
| 1372 |
if (detectedGame == SCOTTFREE || detectedGame == TI994A || Game->type == US_VARIANT) |
| 1373 |
return detectedGame; |
| 1374 |
|
| 1375 |
|
| 1376 |
for (int i = 6; i < MAX_SYSMESS && sysdict_zx[i] != NULL; i++) { |
| 1377 |
sys[i] = (char *)sysdict_zx[i]; |
| 1378 |
} |
| 1379 |
|
| 1380 |
switch (detectedGame) { |
| 1381 |
case ROBIN_OF_SHERWOOD: |
| 1382 |
LoadExtraSherwoodData(0); |
| 1383 |
break; |
| 1384 |
case ROBIN_OF_SHERWOOD_C64: |
| 1385 |
LoadExtraSherwoodData(1); |
| 1386 |
break; |
| 1387 |
case SEAS_OF_BLOOD: |
| 1388 |
LoadExtraSeasOfBloodData(0); |
| 1389 |
break; |
| 1390 |
case SEAS_OF_BLOOD_C64: |
| 1391 |
LoadExtraSeasOfBloodData(1); |
| 1392 |
break; |
| 1393 |
case CLAYMORGUE: |
| 1394 |
for (int i = OK; i <= RESUME_A_SAVED_GAME; i++) |
| 1395 |
sys[i] = system_messages[6 - OK + i]; |
| 1396 |
for (int i = PLAY_AGAIN; i <= ON_A_SCALE_THAT_RATES; i++) |
| 1397 |
sys[i] = system_messages[2 - PLAY_AGAIN + i]; |
| 1398 |
break; |
| 1399 |
case SECRET_MISSION: |
| 1400 |
case SECRET_MISSION_C64: |
| 1401 |
Items[3].Image = 23; |
| 1402 |
Items[3].Flag = 128 | 2; |
| 1403 |
Rooms[2].Image = 0; |
| 1404 |
if (detectedGame == SECRET_MISSION_C64) { |
| 1405 |
SecretMission64Sysmess(); |
| 1406 |
break; |
| 1407 |
} |
| 1408 |
case ADVENTURELAND: |
| 1409 |
for (int i = PLAY_AGAIN; i <= ON_A_SCALE_THAT_RATES; i++) |
| 1410 |
sys[i] = system_messages[2 - PLAY_AGAIN + i]; |
| 1411 |
for (int i = OK; i <= YOU_HAVENT_GOT_IT; i++) |
| 1412 |
sys[i] = system_messages[6 - OK + i]; |
| 1413 |
for (int i = YOU_DONT_SEE_IT; i <= RESUME_A_SAVED_GAME; i++) |
| 1414 |
sys[i] = system_messages[13 - YOU_DONT_SEE_IT + i]; |
| 1415 |
break; |
| 1416 |
case ADVENTURELAND_C64: |
| 1417 |
Adventureland64Sysmess(); |
| 1418 |
break; |
| 1419 |
case CLAYMORGUE_C64: |
| 1420 |
Claymorgue64Sysmess(); |
| 1421 |
break; |
| 1422 |
case GREMLINS_GERMAN_C64: |
| 1423 |
LoadExtraGermanGremlinsC64Data(); |
| 1424 |
break; |
| 1425 |
case SPIDERMAN_C64: |
| 1426 |
Spiderman64Sysmess(); |
| 1427 |
break; |
| 1428 |
case SUPERGRAN_C64: |
| 1429 |
Supergran64Sysmess(); |
| 1430 |
break; |
| 1431 |
case SAVAGE_ISLAND_C64: |
| 1432 |
Items[20].Image = 13; |
| 1433 |
|
| 1434 |
case SAVAGE_ISLAND2_C64: |
| 1435 |
Supergran64Sysmess(); |
| 1436 |
sys[IM_DEAD] = "I'm DEAD!! "; |
| 1437 |
if (detectedGame == SAVAGE_ISLAND2_C64) |
| 1438 |
Rooms[30].Image = 20; |
| 1439 |
break; |
| 1440 |
case SAVAGE_ISLAND: |
| 1441 |
Items[20].Image = 13; |
| 1442 |
|
| 1443 |
case SAVAGE_ISLAND2: |
| 1444 |
MyLoc = 30; |
| 1445 |
|
| 1446 |
case GREMLINS_GERMAN: |
| 1447 |
case GREMLINS: |
| 1448 |
case SUPERGRAN: |
| 1449 |
for (int i = DROPPED; i <= OK; i++) |
| 1450 |
sys[i] = system_messages[2 - DROPPED + i]; |
| 1451 |
for (int i = I_DONT_UNDERSTAND; i <= THATS_BEYOND_MY_POWER; i++) |
| 1452 |
sys[i] = system_messages[6 - I_DONT_UNDERSTAND + i]; |
| 1453 |
for (int i = YOU_ARE; i <= HIT_ENTER; i++) |
| 1454 |
sys[i] = system_messages[17 - YOU_ARE + i]; |
| 1455 |
sys[PLAY_AGAIN] = system_messages[5]; |
| 1456 |
sys[YOURE_CARRYING_TOO_MUCH] = system_messages[24]; |
| 1457 |
sys[IM_DEAD] = system_messages[25]; |
| 1458 |
sys[YOU_CANT_GO_THAT_WAY] = system_messages[14]; |
| 1459 |
break; |
| 1460 |
case GREMLINS_SPANISH: |
| 1461 |
LoadExtraSpanishGremlinsData(); |
| 1462 |
break; |
| 1463 |
case GREMLINS_SPANISH_C64: |
| 1464 |
LoadExtraSpanishGremlinsC64Data(); |
| 1465 |
break; |
| 1466 |
case HULK_C64: |
| 1467 |
case HULK: |
| 1468 |
for (int i = 0; i < 6; i++) { |
| 1469 |
sys[i] = (char *)sysdict_zx[i]; |
| 1470 |
} |
| 1471 |
break; |
| 1472 |
default: |
| 1473 |
if (!(Game->subtype & C64)) { |
| 1474 |
if (Game->subtype & MYSTERIOUS) { |
| 1475 |
for (int i = PLAY_AGAIN; i <= YOU_HAVENT_GOT_IT; i++) |
| 1476 |
sys[i] = system_messages[2 - PLAY_AGAIN + i]; |
| 1477 |
for (int i = YOU_DONT_SEE_IT; i <= WHAT_NOW; i++) |
| 1478 |
sys[i] = system_messages[15 - YOU_DONT_SEE_IT + i]; |
| 1479 |
for (int i = LIGHT_HAS_RUN_OUT; i <= RESUME_A_SAVED_GAME; i++) |
| 1480 |
sys[i] = system_messages[31 - LIGHT_HAS_RUN_OUT + i]; |
| 1481 |
sys[ITEM_DELIMITER] = " - "; |
| 1482 |
sys[MESSAGE_DELIMITER] = "\n"; |
| 1483 |
sys[YOU_SEE] = "\nThings I can see:\n"; |
| 1484 |
break; |
| 1485 |
} else { |
| 1486 |
for (int i = PLAY_AGAIN; i <= RESUME_A_SAVED_GAME; i++) |
| 1487 |
sys[i] = system_messages[2 - PLAY_AGAIN + i]; |
| 1488 |
} |
| 1489 |
} |
| 1490 |
break; |
| 1491 |
} |
| 1492 |
|
| 1493 |
switch (detectedGame) { |
| 1494 |
case GREMLINS_GERMAN: |
| 1495 |
LoadExtraGermanGremlinsData(); |
| 1496 |
break; |
| 1497 |
case GREMLINS_GERMAN_C64: |
| 1498 |
LoadExtraGermanGremlinsC64Data(); |
| 1499 |
break; |
| 1500 |
case PERSEUS_ITALIAN: |
| 1501 |
PerseusItalianSysmess(); |
| 1502 |
break; |
| 1503 |
default: |
| 1504 |
break; |
| 1505 |
} |
| 1506 |
|
| 1507 |
if ((Game->subtype & (MYSTERIOUS | C64)) == (MYSTERIOUS | C64)) |
| 1508 |
Mysterious64Sysmess(); |
| 1509 |
|
| 1510 |
|
| 1511 |
if (!(Game->subtype & (C64 | MYSTERIOUS)) && Game->number_of_pictures > 0) { |
| 1512 |
SagaSetup(0); |
| 1513 |
} |
| 1514 |
|
| 1515 |
return detectedGame; |
| 1516 |
} |