| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
. |
| 6 |
|
| 7 |
|
| 8 |
#include <ctype.h> |
| 9 |
#include <stdio.h> |
| 10 |
#include <stdlib.h> |
| 11 |
#include <string.h> |
| 12 |
|
| 13 |
#include "common.h" |
| 14 |
#include "gameinfo.h" |
| 15 |
#include "graphics.h" |
| 16 |
#include "loaddatabase.h" |
| 17 |
|
| 18 |
Synonym *Substitutions; |
| 19 |
uint8_t *MysteryValues; |
| 20 |
|
| 21 |
static const char *ConditionList[] = { |
| 22 |
"<ERROR>", |
| 23 |
"CARRIED", |
| 24 |
"HERE", |
| 25 |
"PRESENT", |
| 26 |
"AT", |
| 27 |
"ROOMDESC", |
| 28 |
"NOP6", |
| 29 |
"NOP7", |
| 30 |
"NOTZERO", |
| 31 |
"ISZERO", |
| 32 |
"SOMETHING", |
| 33 |
"UNK11", |
| 34 |
"UNK12", |
| 35 |
"NODESTROYED", |
| 36 |
"DESTROYED", |
| 37 |
"CURLE", |
| 38 |
"CURGT", |
| 39 |
"CNTEQCNT", |
| 40 |
"CNTGTCNT", |
| 41 |
"CUREQ", |
| 42 |
"CNTEQ", |
| 43 |
"CNTGTE", |
| 44 |
"CNTZERO", |
| 45 |
"INROOMCNT", |
| 46 |
"NOP24", |
| 47 |
"NOP25", |
| 48 |
"OBJIN", |
| 49 |
"UNK27", |
| 50 |
"NOUNIS", |
| 51 |
"DICTWORD", |
| 52 |
"OBJFLAG", |
| 53 |
"MODE", |
| 54 |
"OUT OF RANGE", |
| 55 |
}; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
struct Keyword |
| 60 |
{ |
| 61 |
char *name; |
| 62 |
int opcode; |
| 63 |
int count; |
| 64 |
}; |
| 65 |
|
| 66 |
static const struct Keyword CommandList[] = |
| 67 |
{ |
| 68 |
{"NOP", 0, 0 }, |
| 69 |
{"GET", 52, 1 }, |
| 70 |
{"DROP", 53, 1 }, |
| 71 |
{"GOTO", 54, 1 }, |
| 72 |
{"MEANS", 55, 2 }, |
| 73 |
{"LIGHTSOUT", 56, 0 }, |
| 74 |
{"LIGHTSON", 57, 0 }, |
| 75 |
{"SET", 58, 1 }, |
| 76 |
{"DESTROY", 59, 1 }, |
| 77 |
{"CLEAR", 60, 1 }, |
| 78 |
{"DEATH", 61, 0 }, |
| 79 |
{"DROPIN", 62, 2 }, |
| 80 |
{"GAMEOVER", 63, 0 }, |
| 81 |
{"SETCNT", 64, 2 }, |
| 82 |
{"MULTIPLY?", 65, 2 }, |
| 83 |
{"INVENTORY", 66, 0 }, |
| 84 |
{"SET0FLAG", 67, 0 }, |
| 85 |
{"CLEAR0FLAG", 68, 0 }, |
| 86 |
{"DONE", 69, 0 }, |
| 87 |
{"CLS", 70, 0 }, |
| 88 |
{"SAVE", 71, 0 }, |
| 89 |
{"SWAP", 72, 2 }, |
| 90 |
{"CONTINUE", 73, 0 }, |
| 91 |
{"STEAL", 74, 1 }, |
| 92 |
{"SAME", 75, 2 }, |
| 93 |
{"LOOK", 76, 0 }, |
| 94 |
{"DECCUR", 77, 0 }, |
| 95 |
{"PRINTCUR", 78, 0 }, |
| 96 |
{"SETCUR", 79, 1 }, |
| 97 |
{"CNT2LOC", 80, 2 }, |
| 98 |
{"SWAPCUR", 81, 1 }, |
| 99 |
{"ADD", 82, 1 }, |
| 100 |
{"SUB", 83, 1 }, |
| 101 |
{"ECHONOUN", 84, 0 }, |
| 102 |
{"PRINTOBJ", 85, 1 }, |
| 103 |
{"ECHODICT", 86, 1 }, |
| 104 |
|
| 105 |
{"PAUSE", 88, 0 }, |
| 106 |
{"REDRAW?", 89, 0 }, |
| 107 |
{"SHOWIMG", 90, 1 }, |
| 108 |
{"DIVMOD", 91, 3 }, |
| 109 |
{"NODOMORE", 92, 0 }, |
| 110 |
{"DELAYQS", 93, 1 }, |
| 111 |
{"GETEXIT", 94, 3 }, |
| 112 |
{"SETEXIT", 95, 3 }, |
| 113 |
{"SWAPCNT", 97, 2 }, |
| 114 |
{"SETTOC", 98, 2 }, |
| 115 |
{"SETTOA", 99, 2 }, |
| 116 |
{"ADDCNT", 100, 2 }, |
| 117 |
{"DECCNT", 101, 2 }, |
| 118 |
{"PRNCNT", 102, 1 }, |
| 119 |
{"ABS", 103, 1 }, |
| 120 |
{"NEG", 104, 1 }, |
| 121 |
{"AND", 105, 2 }, |
| 122 |
{"OR", 106, 2 }, |
| 123 |
{"XOR", 107, 2 }, |
| 124 |
{"RAND", 108, 1 }, |
| 125 |
{"CLRCNT", 109, 1 }, |
| 126 |
{"SYSCMD", 110, 2 }, |
| 127 |
{"GOTO0", 111, 0 }, |
| 128 |
{"BEGIN", 112, 0 }, |
| 129 |
{"LOOPBACK", 113, 0 }, |
| 130 |
{"SKIPLP", 114, 0 }, |
| 131 |
{"SKIPLPNXT", 115, 0 }, |
| 132 |
{"DOMORE", 116, 0 }, |
| 133 |
{"FMATCH", 117, 0 }, |
| 134 |
{"DONE", 118, 0 }, |
| 135 |
{"SETPROMPT", 119, 1 }, |
| 136 |
{"GETWORDS", 120, 0 }, |
| 137 |
{"SETWORDS", 121, 0 }, |
| 138 |
{"PRNOBJART", 122, 1 }, |
| 139 |
{"ADDCNT", 123, 2 }, |
| 140 |
{"BREAK", 124, 0 }, |
| 141 |
{"CPYFLAG", 125, 2 }, |
| 142 |
{"INDTOCNT", 126, 2 }, |
| 143 |
{"CNTTOIND", 127, 2 }, |
| 144 |
{"PRINTMESS", 128, 1 }, |
| 145 |
{"", -1, 0 } |
| 146 |
}; |
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
static const char *ReadWholeLine(FILE *f) |
| 152 |
{ |
| 153 |
char tmp[1024]; |
| 154 |
char *t; |
| 155 |
int c; |
| 156 |
int ct = 0; |
| 157 |
do { |
| 158 |
c = fgetc(f); |
| 159 |
if (c == EOF) |
| 160 |
return NULL; |
| 161 |
if (c == '\"') { |
| 162 |
c = fgetc(f); |
| 163 |
if (c == '\"') |
| 164 |
return "\0"; |
| 165 |
else |
| 166 |
break; |
| 167 |
} |
| 168 |
} while (c != EOF && (isspace((unsigned char)c) || c == '\"')); |
| 169 |
do { |
| 170 |
if (c == 10 || c == 13 || c == EOF) |
| 171 |
break; |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
else if ((c >= 32 && c <= 126)) |
| 177 |
tmp[ct++] = c; |
| 178 |
else |
| 179 |
tmp[ct++] = '?'; |
| 180 |
c = fgetc(f); |
| 181 |
} while (1); |
| 182 |
|
| 183 |
for (int i = ct - 1; i > 0; i--) |
| 184 |
if (tmp[i] == '\"') { |
| 185 |
ct = i + 1; |
| 186 |
break; |
| 187 |
} |
| 188 |
if (ct > 1) |
| 189 |
ct--; |
| 190 |
else |
| 191 |
return "\0"; |
| 192 |
tmp[ct] = 0; |
| 193 |
t = MemAlloc(ct + 1); |
| 194 |
memcpy(t, tmp, ct + 1); |
| 195 |
return (t); |
| 196 |
} |
| 197 |
|
| 198 |
static char *ReadString(FILE *f, size_t *length) |
| 199 |
{ |
| 200 |
char tmp[1024]; |
| 201 |
char *t; |
| 202 |
int c, nc; |
| 203 |
int ct = 0; |
| 204 |
lookdeeper: |
| 205 |
do { |
| 206 |
c = fgetc(f); |
| 207 |
} while (c != EOF && isspace((unsigned char)c)); |
| 208 |
if (c != '"') { |
| 209 |
goto lookdeeper; |
| 210 |
} |
| 211 |
do { |
| 212 |
c = fgetc(f); |
| 213 |
if (c == EOF) |
| 214 |
Fatal("EOF in string"); |
| 215 |
if (c == '"') { |
| 216 |
nc = fgetc(f); |
| 217 |
if (nc != '"') { |
| 218 |
ungetc(nc, f); |
| 219 |
break; |
| 220 |
} |
| 221 |
} |
| 222 |
if (c == '`') |
| 223 |
c = '"'; |
| 224 |
|
| 225 |
|
| 226 |
if (c == '\n') |
| 227 |
tmp[ct++] = 10; |
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
else if (c == 13) |
| 232 |
; |
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
else if ((c >= 32 && c <= 126)) |
| 238 |
tmp[ct++] = c; |
| 239 |
else |
| 240 |
tmp[ct++] = '?'; |
| 241 |
} while (ct < 1000); |
| 242 |
tmp[ct] = 0; |
| 243 |
*length = ct + 1; |
| 244 |
t = MemAlloc((int)*length); |
| 245 |
memcpy(t, tmp, *length); |
| 246 |
return (t); |
| 247 |
} |
| 248 |
|
| 249 |
static DictWord *ReadDictWordsPC(FILE *f, int numstrings, int loud) |
| 250 |
{ |
| 251 |
DictWord dictionary[1024]; |
| 252 |
DictWord *dw = &dictionary[0]; |
| 253 |
char *str = NULL; |
| 254 |
int group = 0; |
| 255 |
int index = 0; |
| 256 |
for (int i = 0; i < numstrings; i++) { |
| 257 |
size_t length; |
| 258 |
str = ReadString(f, &length); |
| 259 |
if (str == NULL || str[0] == '\0') |
| 260 |
continue; |
| 261 |
int lastcomma = 0; |
| 262 |
int commapos = 0; |
| 263 |
for (int j = 0; j < length && str[j] != '\0'; j++) { |
| 264 |
if (str[j] == ',') { |
| 265 |
while (str[j] == ',') { |
| 266 |
str[j] = '\0'; |
| 267 |
commapos = j; |
| 268 |
j++; |
| 269 |
} |
| 270 |
int remaining = commapos - lastcomma; |
| 271 |
if (remaining > 0) { |
| 272 |
dw->Word = MemAlloc(remaining); |
| 273 |
memcpy(dw->Word, &str[lastcomma + 1], remaining); |
| 274 |
dw->Group = group; |
| 275 |
dw = &dictionary[++index]; |
| 276 |
} |
| 277 |
lastcomma = commapos; |
| 278 |
} |
| 279 |
} |
| 280 |
free(str); |
| 281 |
str = NULL; |
| 282 |
group++; |
| 283 |
} |
| 284 |
dictionary[index].Word = NULL; |
| 285 |
int dictsize = (index + 1) * sizeof(DictWord); |
| 286 |
DictWord *finaldict = (DictWord *)MemAlloc(dictsize); |
| 287 |
memcpy(finaldict, dictionary, dictsize); |
| 288 |
if (loud) |
| 289 |
for (int j = 0; dictionary[j].Word != NULL; j++) |
| 290 |
debug_print("Dictionary entry %d: \"%s\", group %d\n", j, finaldict[j].Word, finaldict[j].Group); |
| 291 |
return finaldict; |
| 292 |
} |
| 293 |
|
| 294 |
static Synonym *ReadSubstitutions(FILE *f, int numstrings, int loud) |
| 295 |
{ |
| 296 |
Synonym syn[1024]; |
| 297 |
Synonym *s = &syn[0]; |
| 298 |
|
| 299 |
char *str = NULL; |
| 300 |
char *replace = NULL; |
| 301 |
int index = 0; |
| 302 |
int firstsyn = 0; |
| 303 |
for (int i = 0; i < numstrings; i++) { |
| 304 |
size_t length; |
| 305 |
str = ReadString(f, &length); |
| 306 |
if (loud) |
| 307 |
debug_print("Read synonym string \"%s\"\n", str); |
| 308 |
if (str == NULL || str[0] == '\0') |
| 309 |
continue; |
| 310 |
int lastcomma = 0; |
| 311 |
int commapos = 0; |
| 312 |
int foundrep = 0; |
| 313 |
int nextisrep = 0; |
| 314 |
for (int j = 0; j < length && str[j] != '\0'; j++) { |
| 315 |
if (str[j] == ',') { |
| 316 |
while (str[j] == ',') { |
| 317 |
str[j] = '\0'; |
| 318 |
commapos = j; |
| 319 |
j++; |
| 320 |
} |
| 321 |
if (nextisrep) { |
| 322 |
foundrep = 1; |
| 323 |
nextisrep = 0; |
| 324 |
} else if (str[j] == '=') { |
| 325 |
nextisrep = 1; |
| 326 |
} |
| 327 |
int remaining = commapos - lastcomma - foundrep; |
| 328 |
if (remaining > 0) { |
| 329 |
if (foundrep) { |
| 330 |
if (replace) { |
| 331 |
free(replace); |
| 332 |
} |
| 333 |
replace = MemAlloc(remaining); |
| 334 |
memcpy(replace, &str[lastcomma + 2], remaining); |
| 335 |
if (loud) |
| 336 |
debug_print("Found new replacement string \"%s\"\n", replace); |
| 337 |
} else { |
| 338 |
s->SynonymString = MemAlloc(remaining); |
| 339 |
memcpy(s->SynonymString, &str[lastcomma + 1], remaining); |
| 340 |
if (loud) |
| 341 |
debug_print("Found new synonym string \"%s\"\n", s->SynonymString); |
| 342 |
s = &syn[++index]; |
| 343 |
} |
| 344 |
} |
| 345 |
if (foundrep) { |
| 346 |
for (int k = firstsyn; k < index; k++) { |
| 347 |
int len = (int)strlen(replace) + 1; |
| 348 |
syn[k].ReplacementString = MemAlloc(len); |
| 349 |
memcpy(syn[k].ReplacementString, replace, len); |
| 350 |
if (loud) |
| 351 |
debug_print("Setting replacement string of \"%s\" (%d) to \"%s\"\n", syn[k].SynonymString, k, syn[k].ReplacementString); |
| 352 |
} |
| 353 |
firstsyn = index; |
| 354 |
foundrep = 0; |
| 355 |
} |
| 356 |
lastcomma = commapos; |
| 357 |
} |
| 358 |
} |
| 359 |
free(str); |
| 360 |
str = NULL; |
| 361 |
} |
| 362 |
if (replace) |
| 363 |
free(replace); |
| 364 |
syn[index].SynonymString = NULL; |
| 365 |
int synsize = (index + 1) * sizeof(Synonym); |
| 366 |
Synonym *finalsyns = (Synonym *)MemAlloc(synsize); |
| 367 |
|
| 368 |
memcpy(finalsyns, syn, synsize); |
| 369 |
if (loud) |
| 370 |
for (int j = 0; syn[j].SynonymString != NULL; j++) |
| 371 |
debug_print("Synonym entry %d: \"%s\", Replacement \"%s\"\n", j, finalsyns[j].SynonymString, finalsyns[j].ReplacementString); |
| 372 |
return finalsyns; |
| 373 |
} |
| 374 |
|
| 375 |
static uint8_t *ReadPlusString(uint8_t *ptr, char **string, size_t *length); |
| 376 |
|
| 377 |
static Synonym *ReadSubstitutionsBinary(uint8_t **startpointer, int numstrings, int loud) |
| 378 |
{ |
| 379 |
Synonym syn[1024]; |
| 380 |
Synonym *s = &syn[0]; |
| 381 |
|
| 382 |
uint8_t *ptr = *startpointer; |
| 383 |
|
| 384 |
char *str = NULL; |
| 385 |
char *replace = NULL; |
| 386 |
int index = 0; |
| 387 |
int firstsyn = 0; |
| 388 |
for (int i = 0; i < numstrings; i++) { |
| 389 |
size_t length; |
| 390 |
ptr = ReadPlusString(ptr, &str, &length); |
| 391 |
while (length == 0 && i == 0) |
| 392 |
ptr = ReadPlusString(ptr, &str, &length); |
| 393 |
if (loud) |
| 394 |
debug_print("Read synonym string %d, \"%s\"\n", i, str); |
| 395 |
if (str == NULL || str[0] == 0) |
| 396 |
continue; |
| 397 |
int lastcomma = 0; |
| 398 |
int commapos = 0; |
| 399 |
int foundrep = 0; |
| 400 |
int nextisrep = 0; |
| 401 |
for (int j = 0; j < length && str[j] != '\0'; j++) { |
| 402 |
if (str[j] == ',') { |
| 403 |
while (str[j] == ',') { |
| 404 |
str[j] = '\0'; |
| 405 |
commapos = j; |
| 406 |
j++; |
| 407 |
} |
| 408 |
if (nextisrep) { |
| 409 |
foundrep = 1; |
| 410 |
nextisrep = 0; |
| 411 |
} else if (str[j] == '=') { |
| 412 |
nextisrep = 1; |
| 413 |
} |
| 414 |
int remaining = commapos - lastcomma - foundrep; |
| 415 |
if (remaining > 0) { |
| 416 |
if (foundrep) { |
| 417 |
if (replace) { |
| 418 |
free(replace); |
| 419 |
} |
| 420 |
replace = MemAlloc(remaining); |
| 421 |
memcpy(replace, &str[lastcomma + 2], remaining); |
| 422 |
if (loud) |
| 423 |
debug_print("Found new replacement string \"%s\"\n", replace); |
| 424 |
} else { |
| 425 |
s->SynonymString = MemAlloc(remaining); |
| 426 |
memcpy(s->SynonymString, &str[lastcomma + 1], remaining); |
| 427 |
if (loud) |
| 428 |
debug_print("Found new synonym string \"%s\"\n", s->SynonymString); |
| 429 |
s = &syn[++index]; |
| 430 |
} |
| 431 |
} |
| 432 |
if (foundrep) { |
| 433 |
for (int k = firstsyn; k < index; k++) { |
| 434 |
int len = (int)strlen(replace) + 1; |
| 435 |
syn[k].ReplacementString = MemAlloc(len); |
| 436 |
memcpy(syn[k].ReplacementString, replace, len); |
| 437 |
if (loud) |
| 438 |
debug_print("Setting replacement string of \"%s\" (%d) to \"%s\"\n", syn[k].SynonymString, k, syn[k].ReplacementString); |
| 439 |
} |
| 440 |
firstsyn = index; |
| 441 |
foundrep = 0; |
| 442 |
} |
| 443 |
lastcomma = commapos; |
| 444 |
} |
| 445 |
} |
| 446 |
free(str); |
| 447 |
str = NULL; |
| 448 |
} |
| 449 |
if (replace) |
| 450 |
free(replace); |
| 451 |
syn[index].SynonymString = NULL; |
| 452 |
int synsize = (index + 1) * sizeof(Synonym); |
| 453 |
Synonym *finalsyns = (Synonym *)MemAlloc(synsize); |
| 454 |
|
| 455 |
memcpy(finalsyns, syn, synsize); |
| 456 |
if (loud) |
| 457 |
for (int j = 0; syn[j].SynonymString != NULL; j++) |
| 458 |
debug_print("Synonym entry %d: \"%s\", Replacement \"%s\"\n", j, finalsyns[j].SynonymString, finalsyns[j].ReplacementString); |
| 459 |
*startpointer = ptr; |
| 460 |
return finalsyns; |
| 461 |
} |
| 462 |
|
| 463 |
char **Comments; |
| 464 |
|
| 465 |
static void ReadComments(FILE *f, int loud) |
| 466 |
{ |
| 467 |
const char *strings[1024]; |
| 468 |
const char *comment; |
| 469 |
int index = 0; |
| 470 |
do { |
| 471 |
comment = ReadWholeLine(f); |
| 472 |
strings[index++] = comment; |
| 473 |
} while (comment != NULL); |
| 474 |
if (index <= 1) |
| 475 |
return; |
| 476 |
else |
| 477 |
index--; |
| 478 |
Comments = MemAlloc((index + 1) * sizeof(char *)); |
| 479 |
for (int i = 0; i < index; i++) { |
| 480 |
int len = (int)strlen(strings[i]); |
| 481 |
Comments[i] = MemAlloc(len + 1); |
| 482 |
memcpy(Comments[i], strings[i], len + 1); |
| 483 |
if (loud) |
| 484 |
debug_print("Comment %d:\"%s\"\n", i, Comments[i]); |
| 485 |
} |
| 486 |
Comments[index] = NULL; |
| 487 |
} |
| 488 |
|
| 489 |
static int NumberOfArguments(int opcode) |
| 490 |
{ |
| 491 |
int i = 0; |
| 492 |
while (CommandList[i].opcode != -1) { |
| 493 |
if (CommandList[i].opcode == opcode) |
| 494 |
return CommandList[i].count; |
| 495 |
i++; |
| 496 |
} |
| 497 |
return 0; |
| 498 |
} |
| 499 |
|
| 500 |
void PrintDictWord(int idx, DictWord *dict) |
| 501 |
{ |
| 502 |
for (int i = 0; dict->Word != NULL; i++) { |
| 503 |
if (dict->Group == idx) { |
| 504 |
debug_print("%s", dict->Word); |
| 505 |
return; |
| 506 |
} |
| 507 |
dict++; |
| 508 |
} |
| 509 |
debug_print("%d", idx); |
| 510 |
} |
| 511 |
|
| 512 |
static void PrintCondition(uint8_t condition, uint16_t argument, int *negate, int *mult, int * or) |
| 513 |
{ |
| 514 |
if (condition == 0) { |
| 515 |
debug_print(" %d", argument); |
| 516 |
} else if (condition == 31) { |
| 517 |
if (argument == 1) { |
| 518 |
debug_print(" OR(31) %d ", argument); |
| 519 |
* or = 1; |
| 520 |
} else if (argument == 3 || argument == 4) { |
| 521 |
debug_print(" NOT(31) %d ", argument); |
| 522 |
*negate = 1; |
| 523 |
*mult = (argument != 3); |
| 524 |
} else if (argument == 6) { |
| 525 |
debug_print(" ( "); |
| 526 |
} else if (argument == 7) { |
| 527 |
debug_print(" ) "); |
| 528 |
} else { |
| 529 |
debug_print(" MOD(31) %d ", argument); |
| 530 |
if (argument == 0) |
| 531 |
* or = 0; |
| 532 |
} |
| 533 |
} else if (condition > 40) { |
| 534 |
debug_print(" Condition %d out of range! Arg:%d\n", condition, argument); |
| 535 |
} else { |
| 536 |
debug_print(" "); |
| 537 |
if (*negate) { |
| 538 |
debug_print("!"); |
| 539 |
if (!*mult) |
| 540 |
*negate = 0; |
| 541 |
} |
| 542 |
debug_print("%s(%d) %d", ConditionList[condition], condition, argument); |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
static void PrintDebugMess(int index) |
| 547 |
{ |
| 548 |
if (index > GameHeader.NumMessages) |
| 549 |
return; |
| 550 |
const char *message = Messages[index]; |
| 551 |
if (message != NULL && message[0] != 0) { |
| 552 |
debug_print("\"%s\" ", message); |
| 553 |
} |
| 554 |
} |
| 555 |
|
| 556 |
static void PrintCommand(uint8_t opcode, uint8_t arg, uint8_t arg2, uint8_t arg3, int numargs) |
| 557 |
{ |
| 558 |
if (opcode > 0 && opcode <= 51) { |
| 559 |
debug_print(" MESSAGE %d ", opcode); |
| 560 |
PrintDebugMess(opcode); |
| 561 |
debug_print("\n"); |
| 562 |
return; |
| 563 |
} |
| 564 |
|
| 565 |
int i = 0; |
| 566 |
int found = 0; |
| 567 |
while (CommandList[i].opcode != -1) { |
| 568 |
if (CommandList[i].opcode == opcode) { |
| 569 |
found = 1; |
| 570 |
debug_print(" %s(%d) ", CommandList[i].name, opcode); |
| 571 |
|
| 572 |
if (opcode == 128) { |
| 573 |
arg -= 76; |
| 574 |
debug_print("%d ", arg); |
| 575 |
PrintDebugMess(arg); |
| 576 |
} else if (numargs >= 1) { |
| 577 |
debug_print("%d ", arg); |
| 578 |
if (numargs >= 2) { |
| 579 |
debug_print("%d ", arg2); |
| 580 |
if (numargs >= 3) { |
| 581 |
debug_print("%d ", arg3); |
| 582 |
} |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
break; |
| 587 |
} |
| 588 |
i++; |
| 589 |
} |
| 590 |
if (found) |
| 591 |
debug_print("\n"); |
| 592 |
else { |
| 593 |
debug_print(" UNKNOWN(%d)\n", opcode); |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
static void DumpActions(void) |
| 598 |
{ |
| 599 |
int negate_condition = 0; |
| 600 |
int negate_multiple = 0; |
| 601 |
int or_condition = 0; |
| 602 |
Action *ap = Actions; |
| 603 |
for (int ct = 0; ct <= GameHeader.NumActions; ct++) { |
| 604 |
debug_print("\nAction %d: ", ct); |
| 605 |
if (ap->Verb > 0) { |
| 606 |
PrintDictWord(ap->Verb, Verbs); |
| 607 |
debug_print("(%d) ", ap->Verb); |
| 608 |
PrintDictWord(ap->NounOrChance, Nouns); |
| 609 |
debug_print("(%d)", ap->NounOrChance); |
| 610 |
} else { |
| 611 |
debug_print("(%d)(%d)", ap->Verb, ap->NounOrChance); |
| 612 |
} |
| 613 |
|
| 614 |
if (Comments && Comments[ct][0] != 0) |
| 615 |
debug_print("\nComment: \"%s\"\n", Comments[ct]); |
| 616 |
else |
| 617 |
debug_print("\n"); |
| 618 |
if (ap->NumWords) { |
| 619 |
debug_print("Extra words: "); |
| 620 |
int isobject = 0; |
| 621 |
for (int i = 0; i < ap->NumWords; i++) { |
| 622 |
debug_print("%d ", ap->Words[i]); |
| 623 |
if (isobject) { |
| 624 |
debug_print("(object:) "); |
| 625 |
isobject = 0; |
| 626 |
} else { |
| 627 |
switch (ap->Words[i] >> 6) { |
| 628 |
case 3: |
| 629 |
debug_print("(prep:) "); |
| 630 |
break; |
| 631 |
case 2: |
| 632 |
debug_print("(participle:) "); |
| 633 |
isobject = 1; |
| 634 |
break; |
| 635 |
case 1: |
| 636 |
debug_print("(adverb:) "); |
| 637 |
break; |
| 638 |
case 0: |
| 639 |
debug_print("(spare:) "); |
| 640 |
break; |
| 641 |
} |
| 642 |
} |
| 643 |
if (ap->Words[i] <= GameHeader.NumNouns) { |
| 644 |
debug_print("("); |
| 645 |
PrintDictWord(ap->Words[i], Nouns); |
| 646 |
debug_print(") "); |
| 647 |
} else if ((ap->Words[i] & 0x3f) <= GameHeader.NumPreps) { |
| 648 |
debug_print("("); |
| 649 |
PrintDictWord((ap->Words[i] & 0x3f), Prepositions); |
| 650 |
debug_print(") "); |
| 651 |
} else { |
| 652 |
debug_print("(%d) ", (ap->Words[i] & 0x3f)); |
| 653 |
} |
| 654 |
} |
| 655 |
debug_print("\n"); |
| 656 |
} |
| 657 |
negate_condition = 0; |
| 658 |
negate_multiple = 0; |
| 659 |
if (ap->Conditions[0] == 0 && ap->Conditions[1] == 0 && ap->Conditions[2] == 255) |
| 660 |
debug_print("No conditions"); |
| 661 |
else { |
| 662 |
debug_print("Conditions:"); |
| 663 |
for (int i = 0; ap->Conditions[i] != 255; i += 2) { |
| 664 |
PrintCondition(ap->Conditions[i], ap->Conditions[i + 1], &negate_condition, &negate_multiple, &or_condition); |
| 665 |
} |
| 666 |
} |
| 667 |
negate_condition = 0; |
| 668 |
negate_multiple = 0; |
| 669 |
debug_print("\n"); |
| 670 |
|
| 671 |
for (int i = 0; i <= ap->CommandLength; i++) { |
| 672 |
uint8_t command = ap->Commands[i]; |
| 673 |
int numargs = NumberOfArguments(command); |
| 674 |
|
| 675 |
if (numargs > 0 && i < ap->CommandLength - 2 && ap->Commands[i + 1] == 131 && ap->Commands[i + 2] == 230) { |
| 676 |
numargs++; |
| 677 |
} else if (command == 99 && ap->Commands[i + 2] > 63) { |
| 678 |
numargs = 3; |
| 679 |
} else if (command == 131 && ap->Commands[i + 2] > 31) { |
| 680 |
numargs = 3; |
| 681 |
} else if ((command == 82 || command == 119) && ap->Commands[i + 1] > 127) { |
| 682 |
numargs = 2; |
| 683 |
} |
| 684 |
|
| 685 |
int arg1 = 0; |
| 686 |
if (numargs >= 1) |
| 687 |
arg1 = ap->Commands[i + 1]; |
| 688 |
int arg2 = 0; |
| 689 |
if (numargs >= 2) |
| 690 |
arg2 = ap->Commands[i + 2]; |
| 691 |
int arg3 = 0; |
| 692 |
if (numargs >= 3) |
| 693 |
arg3 = ap->Commands[i + 3]; |
| 694 |
PrintCommand(command, arg1, arg2, arg3, numargs); |
| 695 |
i += numargs; |
| 696 |
} |
| 697 |
debug_print("\n"); |
| 698 |
ap++; |
| 699 |
} |
| 700 |
} |
| 701 |
|
| 702 |
static uint8_t ReadNum(FILE *f) |
| 703 |
{ |
| 704 |
int num; |
| 705 |
if (fscanf(f, "%d\n", &num) != 1) |
| 706 |
return 0; |
| 707 |
return num; |
| 708 |
} |
| 709 |
|
| 710 |
static void ReadAction(FILE *f, Action *ap) |
| 711 |
{ |
| 712 |
uint8_t length = ReadNum(f); |
| 713 |
int i; |
| 714 |
ap->NumWords = length >> 4; |
| 715 |
ap->CommandLength = length & 0xf; |
| 716 |
ap->Verb = ReadNum(f); |
| 717 |
if (ap->Verb & 128) { |
| 718 |
Fatal("2-byte verbs unimplemented\n"); |
| 719 |
} |
| 720 |
ap->NounOrChance = ReadNum(f); |
| 721 |
if (ap->NounOrChance & 128) { |
| 722 |
Fatal("2-byte nouns unimplemented\n"); |
| 723 |
} |
| 724 |
ap->Words = MemAlloc(ap->NumWords); |
| 725 |
|
| 726 |
for (i = 0; i < ap->NumWords; i++) { |
| 727 |
ap->Words[i] = ReadNum(f); |
| 728 |
} |
| 729 |
|
| 730 |
int reading_conditions = 1; |
| 731 |
int conditions_read = 0; |
| 732 |
uint16_t conditions[1024]; |
| 733 |
int condargs = 0; |
| 734 |
while (reading_conditions) { |
| 735 |
uint16_t argcond = ReadNum(f) * 256 + ReadNum(f); |
| 736 |
if (argcond & 0x8000) |
| 737 |
reading_conditions = 0; |
| 738 |
argcond = argcond & 0x7fff; |
| 739 |
uint16_t argument = argcond >> 5; |
| 740 |
uint16_t condition = argcond & 0x1f; |
| 741 |
conditions[condargs++] = condition; |
| 742 |
conditions[condargs++] = argument; |
| 743 |
if (condition != 0) |
| 744 |
conditions_read++; |
| 745 |
} |
| 746 |
ap->Conditions = MemAlloc((condargs + 1) * sizeof(uint16_t)); |
| 747 |
for (i = 0; i < condargs; i++) |
| 748 |
ap->Conditions[i] = conditions[i]; |
| 749 |
ap->Conditions[condargs] = 255; |
| 750 |
uint8_t commands[1024]; |
| 751 |
for (i = 0; i <= ap->CommandLength; i++) { |
| 752 |
commands[i] = ReadNum(f); |
| 753 |
} |
| 754 |
|
| 755 |
ap->Commands = MemAlloc(i); |
| 756 |
memcpy(ap->Commands, commands, i); |
| 757 |
} |
| 758 |
|
| 759 |
static void PrintHeaderInfo(Header h) |
| 760 |
{ |
| 761 |
debug_print("Number of items =\t%d\n", h.NumItems); |
| 762 |
debug_print("sum of actions =\t%d\n", h.ActionSum); |
| 763 |
debug_print("Number of nouns =\t%d\n", h.NumNouns); |
| 764 |
debug_print("Number of verbs =\t%d\n", h.NumVerbs); |
| 765 |
debug_print("Number of rooms =\t%d\n", h.NumRooms); |
| 766 |
debug_print("Max carried items =\t%d\n", h.MaxCarry); |
| 767 |
debug_print("Player start location =\t%d\n", h.PlayerRoom); |
| 768 |
debug_print("Number of messages =\t%d\n", h.NumMessages); |
| 769 |
debug_print("Treasure room =\t%d\n", h.TreasureRoom); |
| 770 |
debug_print("Light source turns =\t%d\n", h.LightTime); |
| 771 |
debug_print("Number of prepositions =\t%d\n", h.NumPreps); |
| 772 |
debug_print("Number of adverbs =\t%d\n", h.NumAdverbs); |
| 773 |
debug_print("Number of actions =\t%d\n", h.NumActions); |
| 774 |
debug_print("Number of treasures =\t%d\n", h.Treasures); |
| 775 |
debug_print("Number of synonym strings =\t%d\n", h.NumSubStr); |
| 776 |
debug_print("Unknown1 =\t%d\n", h.Unknown1); |
| 777 |
debug_print("Number of object images =\t%d\n", h.NumObjImg); |
| 778 |
debug_print("Unknown3 =\t%d\n", h.Unknown2); |
| 779 |
} |
| 780 |
|
| 781 |
static int SetGame(const char *id_string, size_t length) |
| 782 |
{ |
| 783 |
for (int i = 0; games[i].title != NULL; i++) |
| 784 |
if (strncmp(id_string, games[i].ID_string, length) == 0) { |
| 785 |
Game = &games[i]; |
| 786 |
return 1; |
| 787 |
} |
| 788 |
return 0; |
| 789 |
} |
| 790 |
|
| 791 |
int FindAndAddImageFile(char *shortname, struct imgrec *rec) |
| 792 |
{ |
| 793 |
int result = 0; |
| 794 |
size_t pathlen = DirPathLength + 9; |
| 795 |
char *filename = MemAlloc(pathlen); |
| 796 |
int n = snprintf(filename, pathlen, "%s%s.PAK", DirPath, shortname); |
| 797 |
if (n > 0) { |
| 798 |
FILE *infile = fopen(filename, "rb"); |
| 799 |
if (infile) { |
| 800 |
fseek(infile, 0, SEEK_END); |
| 801 |
size_t length = ftell(infile); |
| 802 |
if (length > 0) { |
| 803 |
debug_print("Found and read image file %s\n", filename); |
| 804 |
size_t namelen = strlen(shortname) + 1; |
| 805 |
rec->Filename = MemAlloc(namelen); |
| 806 |
memcpy(rec->Filename, shortname, namelen); |
| 807 |
fseek(infile, 0, SEEK_SET); |
| 808 |
rec->Data = MemAlloc(length); |
| 809 |
rec->Size = fread(rec->Data, 1, length, infile); |
| 810 |
result = 1; |
| 811 |
} |
| 812 |
fclose(infile); |
| 813 |
} else { |
| 814 |
debug_print("Could not find or read image file %s\n", filename); |
| 815 |
} |
| 816 |
} |
| 817 |
free(filename); |
| 818 |
return result; |
| 819 |
} |
| 820 |
|
| 821 |
int LoadDatabasePlaintext(FILE *f, int loud) |
| 822 |
{ |
| 823 |
int ni, as, na, nv, nn, nr, mc, pr, tr, lt, mn, trm, adv, prp, ss, unk1, oi, unk2; |
| 824 |
int ct; |
| 825 |
|
| 826 |
Action *ap; |
| 827 |
Room *rp; |
| 828 |
Item *ip; |
| 829 |
|
| 830 |
|
| 831 |
size_t length; |
| 832 |
char *title = ReadString(f, &length); |
| 833 |
|
| 834 |
if (!SetGame(title, length)) { |
| 835 |
free(title); |
| 836 |
return UNKNOWN_GAME; |
| 837 |
} |
| 838 |
|
| 839 |
if (fscanf(f, ",%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n", &ni, &as, &nn, &nv, &nr, &mc, &pr, |
| 840 |
&mn, &trm, <, &prp, &adv, &na, &tr, &ss, &unk1, &oi, &unk2) |
| 841 |
< 10) { |
| 842 |
if (loud) |
| 843 |
debug_print("Invalid database(bad header)\n"); |
| 844 |
return UNKNOWN_GAME; |
| 845 |
} |
| 846 |
GameHeader.NumItems = ni; |
| 847 |
Counters[43] = ni; |
| 848 |
Items = (Item *)MemAlloc(sizeof(Item) * (ni + 1)); |
| 849 |
GameHeader.NumActions = na; |
| 850 |
GameHeader.ActionSum = as; |
| 851 |
Actions = (Action *)MemAlloc(sizeof(Action) * (na + 1)); |
| 852 |
GameHeader.NumVerbs = nv; |
| 853 |
GameHeader.NumNouns = nn; |
| 854 |
GameHeader.NumRooms = nr; |
| 855 |
Rooms = (Room *)MemAlloc(sizeof(Room) * (nr + 1)); |
| 856 |
GameHeader.MaxCarry = mc; |
| 857 |
GameHeader.PlayerRoom = pr; |
| 858 |
MyLoc = pr; |
| 859 |
GameHeader.Treasures = tr; |
| 860 |
GameHeader.LightTime = lt; |
| 861 |
GameHeader.NumMessages = mn; |
| 862 |
Messages = MemAlloc(sizeof(char *) * (mn + 1)); |
| 863 |
GameHeader.TreasureRoom = trm; |
| 864 |
GameHeader.NumAdverbs = adv; |
| 865 |
GameHeader.NumPreps = prp; |
| 866 |
GameHeader.Unknown1 = unk1; |
| 867 |
GameHeader.NumSubStr = ss; |
| 868 |
GameHeader.Unknown2 = unk2; |
| 869 |
GameHeader.NumObjImg = oi; |
| 870 |
ObjectImages = (ObjectImage *)MemAlloc(sizeof(ObjectImage) * (oi + 1)); |
| 871 |
MysteryValues = MemAlloc(unk2 + 1); |
| 872 |
|
| 873 |
Counters[35] = nr; |
| 874 |
Counters[34] = trm; |
| 875 |
Counters[42] = mc; |
| 876 |
SetBit(35); |
| 877 |
|
| 878 |
if (loud) { |
| 879 |
PrintHeaderInfo(GameHeader); |
| 880 |
} |
| 881 |
|
| 882 |
|
| 883 |
|
| 884 |
ct = 0; |
| 885 |
ap = Actions; |
| 886 |
if (loud) |
| 887 |
debug_print("Reading %d actions.\n", na); |
| 888 |
while (ct < na + 1) { |
| 889 |
ReadAction(f, ap); |
| 890 |
ap++; |
| 891 |
ct++; |
| 892 |
} |
| 893 |
|
| 894 |
if (loud) |
| 895 |
debug_print("Reading %d verbs.\n", nv); |
| 896 |
Verbs = ReadDictWordsPC(f, GameHeader.NumVerbs + 1, loud); |
| 897 |
if (loud) |
| 898 |
debug_print("Reading %d nouns.\n", nn); |
| 899 |
Nouns = ReadDictWordsPC(f, GameHeader.NumNouns + 1, loud); |
| 900 |
|
| 901 |
rp = Rooms; |
| 902 |
if (loud) |
| 903 |
debug_print("Reading %d rooms.\n", nr); |
| 904 |
for (ct = 0; ct < nr + 1; ct++) { |
| 905 |
if (fscanf(f, "%d,%d,%d,%d,%d,%d,%d,%d\n", &rp->Exits[0], &rp->Exits[1], |
| 906 |
&rp->Exits[2], &rp->Exits[3], &rp->Exits[4], |
| 907 |
&rp->Exits[5], &rp->Exits[6], &rp->Image) |
| 908 |
!= 8) { |
| 909 |
debug_print("Bad room line (%d)\n", ct); |
| 910 |
|
| 911 |
return UNKNOWN_GAME; |
| 912 |
} |
| 913 |
rp++; |
| 914 |
} |
| 915 |
|
| 916 |
if (loud) |
| 917 |
debug_print("Reading %d messages.\n", mn); |
| 918 |
for (ct = 0; ct < mn + 1; ct++) { |
| 919 |
Messages[ct] = ReadString(f, &length); |
| 920 |
if (loud) |
| 921 |
debug_print("Message %d: \"%s\"\n", ct, Messages[ct]); |
| 922 |
} |
| 923 |
|
| 924 |
for (ct = 0; ct < nr + 1; ct++) { |
| 925 |
Rooms[ct].Text = Messages[Rooms[ct].Exits[6] - 76]; |
| 926 |
if (loud) |
| 927 |
debug_print("Room description of room %d: \"%s\"\n", ct, Rooms[ct].Text); |
| 928 |
} |
| 929 |
|
| 930 |
ct = 0; |
| 931 |
if (loud) |
| 932 |
debug_print("Reading %d items.\n", ni); |
| 933 |
int loc, dictword, flag; |
| 934 |
ip = Items; |
| 935 |
while (ct < ni + 1) { |
| 936 |
ip->Text = ReadString(f, &length); |
| 937 |
if (loud) |
| 938 |
debug_print("Item %d: \"%s\"\n", ct, ip->Text); |
| 939 |
if (fscanf(f, ",%d,%d,%d\n", &loc, &dictword, &flag) != 3) { |
| 940 |
debug_print("Bad item line (%d)\n", ct); |
| 941 |
|
| 942 |
return UNKNOWN_GAME; |
| 943 |
} |
| 944 |
ip->Location = (uint8_t)loc; |
| 945 |
ip->Dictword = (uint8_t)dictword; |
| 946 |
ip->Flag = (uint8_t)flag; |
| 947 |
if (loud && (ip->Location == CARRIED || ip->Location <= GameHeader.NumRooms)) |
| 948 |
debug_print("Location of item %d: %d, \"%s\"\n", ct, ip->Location, |
| 949 |
ip->Location == CARRIED ? "CARRIED" : Rooms[ip->Location].Text); |
| 950 |
ip->InitialLoc = ip->Location; |
| 951 |
ip++; |
| 952 |
ct++; |
| 953 |
} |
| 954 |
|
| 955 |
if (loud) |
| 956 |
debug_print("Reading %d adverbs.\n", GameHeader.NumAdverbs); |
| 957 |
Adverbs = ReadDictWordsPC(f, GameHeader.NumAdverbs + 1, loud); |
| 958 |
if (loud) |
| 959 |
debug_print("Reading %d prepositions.\n", GameHeader.NumPreps); |
| 960 |
Prepositions = ReadDictWordsPC(f, GameHeader.NumPreps + 1, loud); |
| 961 |
|
| 962 |
Substitutions = ReadSubstitutions(f, GameHeader.NumSubStr + 1, loud); |
| 963 |
|
| 964 |
ObjectImage *objimg = ObjectImages; |
| 965 |
if (loud) |
| 966 |
debug_print("Reading %d object image values.\n", oi + 1); |
| 967 |
for (ct = 0; ct <= oi; ct++) { |
| 968 |
if (fscanf(f, "%d,%d,%d\n", &objimg->Room, &objimg->Object, &objimg->Image) |
| 969 |
!= 3) { |
| 970 |
debug_print("Bad object image line (%d)\n", ct); |
| 971 |
return UNKNOWN_GAME; |
| 972 |
} |
| 973 |
if (loud) { |
| 974 |
debug_print("Object image %d ", ct); |
| 975 |
debug_print("room: %d object: %d image: %d\n", objimg->Room, objimg->Object, objimg->Image); |
| 976 |
} |
| 977 |
objimg++; |
| 978 |
} |
| 979 |
|
| 980 |
if (loud) |
| 981 |
debug_print("Reading %d unknown values.\n", unk2 + 1); |
| 982 |
for (ct = 0; ct < unk2 + 1; ct++) { |
| 983 |
MysteryValues[ct] = ReadNum(f); |
| 984 |
if (MysteryValues[ct] > 255) { |
| 985 |
debug_print("Bad unknown value (%d)\n", ct); |
| 986 |
|
| 987 |
return UNKNOWN_GAME; |
| 988 |
} |
| 989 |
} |
| 990 |
|
| 991 |
ReadComments(f, loud); |
| 992 |
|
| 993 |
if (loud) { |
| 994 |
debug_print("\nLoad Complete.\n\n"); |
| 995 |
DumpActions(); |
| 996 |
} |
| 997 |
fclose(f); |
| 998 |
|
| 999 |
int numimages = Game->no_of_room_images + Game->no_of_item_images + Game->no_of_special_images; |
| 1000 |
|
| 1001 |
Images = MemAlloc((numimages + 1) * sizeof(struct imgrec)); |
| 1002 |
debug_print("\nTotal number of images:%d\n", numimages); |
| 1003 |
|
| 1004 |
int i, recidx = 0, imgidx = 0; |
| 1005 |
char type = 'R'; |
| 1006 |
for (i = 0; i < numimages; i++) { |
| 1007 |
if (i == Game->no_of_room_images) { |
| 1008 |
type = 'B'; |
| 1009 |
imgidx = 0; |
| 1010 |
} |
| 1011 |
if (i == Game->no_of_room_images + Game->no_of_item_images) { |
| 1012 |
type = 'S'; |
| 1013 |
imgidx = 0; |
| 1014 |
} |
| 1015 |
char *shortname = ShortNameFromType(type, imgidx); |
| 1016 |
if (FindAndAddImageFile(shortname, &Images[recidx])) |
| 1017 |
recidx++; |
| 1018 |
free(shortname); |
| 1019 |
imgidx++; |
| 1020 |
} |
| 1021 |
Images[recidx].Filename = NULL; |
| 1022 |
|
| 1023 |
CurrentSys = SYS_MSDOS; |
| 1024 |
|
| 1025 |
return CurrentGame; |
| 1026 |
} |
| 1027 |
|
| 1028 |
static uint8_t *ReadPlusString(uint8_t *ptr, char **string, size_t *len) |
| 1029 |
{ |
| 1030 |
char tmp[1024]; |
| 1031 |
uint8_t length = *ptr++; |
| 1032 |
if (length == 0 || length == 255) { |
| 1033 |
*string = MemAlloc(1); |
| 1034 |
(*string)[0] = 0; |
| 1035 |
*len = 0; |
| 1036 |
return ptr; |
| 1037 |
} |
| 1038 |
for (int i = 0; i < length; i++) { |
| 1039 |
tmp[i] = *ptr++; |
| 1040 |
if (tmp[i] == '`') |
| 1041 |
tmp[i] = '\"'; |
| 1042 |
} |
| 1043 |
|
| 1044 |
tmp[length] = 0; |
| 1045 |
char *t = MemAlloc(length + 1); |
| 1046 |
memcpy(t, tmp, length + 1); |
| 1047 |
*string = t; |
| 1048 |
*len = length; |
| 1049 |
return ptr; |
| 1050 |
} |
| 1051 |
|
| 1052 |
char **LoadMessages(int numstrings, uint8_t **ptr) |
| 1053 |
{ |
| 1054 |
if (numstrings == 0) |
| 1055 |
numstrings = 0xffff; |
| 1056 |
char *str; |
| 1057 |
char **target = MemAlloc(numstrings * sizeof(char *)); |
| 1058 |
int i; |
| 1059 |
for (i = 0; i < numstrings; i++) { |
| 1060 |
size_t length; |
| 1061 |
*ptr = ReadPlusString(*ptr, &str, &length); |
| 1062 |
if (CurrentSys == SYS_ST) { |
| 1063 |
while (str[0] == 0 && i != 0) |
| 1064 |
*ptr = ReadPlusString(*ptr, &str, &length); |
| 1065 |
} |
| 1066 |
debug_print("Message %d: \"%s\"\n", i, str); |
| 1067 |
target[i] = str; |
| 1068 |
} |
| 1069 |
return target; |
| 1070 |
} |
| 1071 |
|
| 1072 |
uint8_t *SeekToPos(uint8_t *buf, int offset) |
| 1073 |
{ |
| 1074 |
if (offset > 0x10000) |
| 1075 |
return 0; |
| 1076 |
return buf + offset; |
| 1077 |
} |
| 1078 |
|
| 1079 |
int SeekIfNeeded(int expected_start, int *offset, uint8_t **ptr) |
| 1080 |
{ |
| 1081 |
if (expected_start != FOLLOWS) { |
| 1082 |
*offset = expected_start; |
| 1083 |
uint8_t *ptrbefore = *ptr; |
| 1084 |
*ptr = SeekToPos(mem, *offset); |
| 1085 |
if (*ptr == ptrbefore) |
| 1086 |
fprintf(stderr, "Seek unnecessary, could have been set to FOLLOWS.\n"); |
| 1087 |
if (*ptr == 0) |
| 1088 |
return 0; |
| 1089 |
} |
| 1090 |
return 1; |
| 1091 |
} |
| 1092 |
|
| 1093 |
void ParseHeader(uint16_t *h, int *ni, int *as, int *nn, int *nv, int *nr, int *mc, int *pr, int *mn, int *trm, int *lt, int *prp, int *adv, int *na, int *tr, int *ss, int *unk1, int *oi, int *unk2) |
| 1094 |
{ |
| 1095 |
*ni = h[0]; |
| 1096 |
*as = h[1]; |
| 1097 |
*nn = h[2]; |
| 1098 |
*nv = h[3]; |
| 1099 |
*nr = h[4]; |
| 1100 |
*mc = h[5]; |
| 1101 |
*pr = h[6]; |
| 1102 |
*mn = h[7]; |
| 1103 |
*trm = h[8]; |
| 1104 |
*lt = h[9]; |
| 1105 |
*prp = h[10]; |
| 1106 |
*adv = h[11]; |
| 1107 |
*na = 0; |
| 1108 |
*tr = 0; |
| 1109 |
*ss = h[12]; |
| 1110 |
*unk1 = h[15]; |
| 1111 |
*oi = h[13]; |
| 1112 |
*unk2 = 0; |
| 1113 |
} |
| 1114 |
|
| 1115 |
static uint8_t *ReadHeader(uint8_t *ptr) |
| 1116 |
{ |
| 1117 |
int i, value; |
| 1118 |
for (i = 0; i < 16; i++) { |
| 1119 |
if (CurrentSys == SYS_ST) |
| 1120 |
value = *ptr * 256 + *(ptr + 1); |
| 1121 |
else |
| 1122 |
value = *ptr + 256 * *(ptr + 1); |
| 1123 |
header[i] = value; |
| 1124 |
debug_print("Header value %d: %d\n", i, header[i]); |
| 1125 |
ptr += 2; |
| 1126 |
} |
| 1127 |
return ptr - 2; |
| 1128 |
} |
| 1129 |
|
| 1130 |
DictWord *ReadDictWords(uint8_t **pointer, int numstrings, int loud) |
| 1131 |
{ |
| 1132 |
uint8_t *ptr = *pointer; |
| 1133 |
DictWord dictionary[1024]; |
| 1134 |
DictWord *dw = dictionary; |
| 1135 |
char *str = NULL; |
| 1136 |
int group = 0; |
| 1137 |
int index = 0; |
| 1138 |
for (int i = 0; i < numstrings; i++) { |
| 1139 |
size_t strlength; |
| 1140 |
ptr = ReadPlusString(ptr, &str, &strlength); |
| 1141 |
while (str[0] == 0) |
| 1142 |
ptr = ReadPlusString(ptr, &str, &strlength); |
| 1143 |
debug_print("Read dictionary string \"%s\"\n", str); |
| 1144 |
int lastcomma = 0; |
| 1145 |
int commapos = 0; |
| 1146 |
for (int j = 0; j <= strlength && str[j] != '\0'; j++) { |
| 1147 |
if (str[j] == ',') { |
| 1148 |
while (str[j] == ',') { |
| 1149 |
str[j] = '\0'; |
| 1150 |
commapos = j; |
| 1151 |
j++; |
| 1152 |
} |
| 1153 |
int length = commapos - lastcomma; |
| 1154 |
if (length > 0) { |
| 1155 |
dw->Word = MemAlloc(length); |
| 1156 |
memcpy(dw->Word, &str[lastcomma + 1], length); |
| 1157 |
dw->Group = group; |
| 1158 |
debug_print("Dictword %d: %s (%d)\n", index, dw->Word, dw->Group); |
| 1159 |
dw = &dictionary[++index]; |
| 1160 |
} |
| 1161 |
lastcomma = commapos; |
| 1162 |
} |
| 1163 |
} |
| 1164 |
free(str); |
| 1165 |
str = NULL; |
| 1166 |
group++; |
| 1167 |
} |
| 1168 |
dictionary[index].Word = NULL; |
| 1169 |
int dictsize = (index + 1) * sizeof(DictWord); |
| 1170 |
DictWord *finaldict = (DictWord *)MemAlloc(dictsize); |
| 1171 |
memcpy(finaldict, dictionary, dictsize); |
| 1172 |
*pointer = ptr; |
| 1173 |
if (loud) |
| 1174 |
for (int j = 0; dictionary[j].Word != NULL; j++) |
| 1175 |
debug_print("Dictionary entry %d: \"%s\", group %d\n", j, finaldict[j].Word, finaldict[j].Group); |
| 1176 |
return finaldict; |
| 1177 |
} |
| 1178 |
|
| 1179 |
int SanityCheckHeader(void) |
| 1180 |
{ |
| 1181 |
int16_t v = GameHeader.NumItems; |
| 1182 |
if (v < 10 || v > 500) |
| 1183 |
return 0; |
| 1184 |
v = GameHeader.NumNouns; |
| 1185 |
if (v < 50 || v > 190) |
| 1186 |
return 0; |
| 1187 |
v = GameHeader.NumVerbs; |
| 1188 |
if (v < 30 || v > 190) |
| 1189 |
return 0; |
| 1190 |
v = GameHeader.NumRooms; |
| 1191 |
if (v < 10 || v > 100) |
| 1192 |
return 0; |
| 1193 |
|
| 1194 |
return 1; |
| 1195 |
} |
| 1196 |
|
| 1197 |
int LoadDatabaseBinary(void) |
| 1198 |
{ |
| 1199 |
int ni, as, na, nv, nn, nr, mc, pr, tr, lt, mn, trm, adv, prp, ss, unk1, oi, unk2; |
| 1200 |
int ct; |
| 1201 |
|
| 1202 |
Action *ap; |
| 1203 |
Room *rp; |
| 1204 |
Item *ip; |
| 1205 |
|
| 1206 |
|
| 1207 |
uint8_t *ptr = mem; |
| 1208 |
|
| 1209 |
int offset = 0x32; |
| 1210 |
|
| 1211 |
int isSTSpiderman = 0, isSTFF = 0; |
| 1212 |
|
| 1213 |
ptr = SeekToPos(mem, offset); |
| 1214 |
if (ptr == 0) |
| 1215 |
return 0; |
| 1216 |
|
| 1217 |
#pragma mark header |
| 1218 |
|
| 1219 |
ptr = ReadHeader(ptr); |
| 1220 |
|
| 1221 |
ParseHeader(header, &ni, &as, &nn, &nv, &nr, &mc, &pr, |
| 1222 |
&mn, &trm, <, &prp, &adv, &na, &tr, &ss, &unk1, &oi, &unk2); |
| 1223 |
|
| 1224 |
if (CurrentSys == SYS_ST && as == 5159) |
| 1225 |
isSTSpiderman = 1; |
| 1226 |
|
| 1227 |
if (CurrentSys == SYS_ST && as == 6991) |
| 1228 |
isSTFF = 1; |
| 1229 |
|
| 1230 |
GameHeader.NumItems = ni; |
| 1231 |
Counters[43] = ni; |
| 1232 |
GameHeader.ActionSum = as; |
| 1233 |
GameHeader.NumVerbs = nv; |
| 1234 |
GameHeader.NumNouns = nn; |
| 1235 |
GameHeader.NumRooms = nr; |
| 1236 |
|
| 1237 |
GameHeader.MaxCarry = mc; |
| 1238 |
GameHeader.PlayerRoom = pr; |
| 1239 |
MyLoc = pr; |
| 1240 |
GameHeader.Treasures = tr; |
| 1241 |
GameHeader.NumPreps = prp; |
| 1242 |
GameHeader.NumAdverbs = adv; |
| 1243 |
GameHeader.NumSubStr = ss; |
| 1244 |
GameHeader.NumObjImg = oi; |
| 1245 |
GameHeader.NumMessages = mn; |
| 1246 |
GameHeader.TreasureRoom = trm; |
| 1247 |
|
| 1248 |
PrintHeaderInfo(GameHeader); |
| 1249 |
|
| 1250 |
if (!SanityCheckHeader()) |
| 1251 |
return 0; |
| 1252 |
|
| 1253 |
Items = (Item *)MemAlloc(sizeof(Item) * (ni + 1)); |
| 1254 |
Verbs = MemAlloc(sizeof(char *) * (nv + 1)); |
| 1255 |
Nouns = MemAlloc(sizeof(char *) * (nn + 1)); |
| 1256 |
Rooms = (Room *)MemAlloc(sizeof(Room) * (nr + 1)); |
| 1257 |
ObjectImages = (ObjectImage *)MemAlloc(sizeof(ObjectImage) * (oi + 1)); |
| 1258 |
Messages = MemAlloc(sizeof(char *) * (mn + 1)); |
| 1259 |
|
| 1260 |
Counters[35] = nr; |
| 1261 |
Counters[34] = trm; |
| 1262 |
Counters[42] = mc; |
| 1263 |
SetBit(35); |
| 1264 |
|
| 1265 |
#pragma mark actions |
| 1266 |
|
| 1267 |
Actions = (Action *)MemAlloc(sizeof(Action) * 500); |
| 1268 |
|
| 1269 |
ct = 0; |
| 1270 |
|
| 1271 |
Action *PrelActions = (Action *)MemAlloc(sizeof(Action) * 524); |
| 1272 |
|
| 1273 |
ap = PrelActions; |
| 1274 |
|
| 1275 |
uint8_t *origptr = ptr; |
| 1276 |
|
| 1277 |
while (ptr - origptr <= GameHeader.ActionSum) { |
| 1278 |
uint8_t length = *ptr++; |
| 1279 |
ap->NumWords = length >> 4; |
| 1280 |
ap->CommandLength = length & 0xf; |
| 1281 |
ap->Verb = *ptr++; |
| 1282 |
ap->NounOrChance = *ptr++; |
| 1283 |
|
| 1284 |
ap->Words = MemAlloc(ap->NumWords); |
| 1285 |
|
| 1286 |
for (int i = 0; i < ap->NumWords; i++) { |
| 1287 |
ap->Words[i] = *ptr++; |
| 1288 |
} |
| 1289 |
|
| 1290 |
int reading_conditions = 1; |
| 1291 |
int conditions_read = 0; |
| 1292 |
uint16_t conditions[1024]; |
| 1293 |
int condargs = 0; |
| 1294 |
while (reading_conditions) { |
| 1295 |
uint8_t hi = *ptr++; |
| 1296 |
uint8_t lo = *ptr++; |
| 1297 |
uint16_t argcond = hi * 256 + lo; |
| 1298 |
if (argcond & 0x8000) |
| 1299 |
reading_conditions = 0; |
| 1300 |
argcond = argcond & 0x7fff; |
| 1301 |
uint16_t argument = argcond >> 5; |
| 1302 |
uint16_t condition = argcond & 0x1f; |
| 1303 |
conditions[condargs++] = condition; |
| 1304 |
conditions[condargs++] = argument; |
| 1305 |
if (condition != 0) |
| 1306 |
conditions_read++; |
| 1307 |
} |
| 1308 |
ap->Conditions = MemAlloc((condargs + 1) * sizeof(uint16_t)); |
| 1309 |
int i; |
| 1310 |
for (i = 0; i < condargs; i++) |
| 1311 |
ap->Conditions[i] = conditions[i]; |
| 1312 |
ap->Conditions[condargs] = 255; |
| 1313 |
uint8_t commands[1024]; |
| 1314 |
for (i = 0; i <= ap->CommandLength; i++) { |
| 1315 |
commands[i] = *ptr++; |
| 1316 |
} |
| 1317 |
|
| 1318 |
ap->Commands = MemAlloc(i); |
| 1319 |
memcpy(ap->Commands, commands, i); |
| 1320 |
|
| 1321 |
ap++; |
| 1322 |
ct++; |
| 1323 |
} |
| 1324 |
|
| 1325 |
GameHeader.NumActions = ct - 1; |
| 1326 |
|
| 1327 |
size_t actsize = sizeof(Action) * ct; |
| 1328 |
Actions = (Action *)MemAlloc((int)actsize); |
| 1329 |
memcpy(Actions, PrelActions, actsize); |
| 1330 |
free(PrelActions); |
| 1331 |
|
| 1332 |
#pragma mark dictionary |
| 1333 |
|
| 1334 |
Verbs = ReadDictWords(&ptr, GameHeader.NumVerbs + 1, 1); |
| 1335 |
Nouns = ReadDictWords(&ptr, GameHeader.NumNouns + 1, 1); |
| 1336 |
|
| 1337 |
#pragma mark room connections |
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
|
| 1342 |
for (int j = 0; j < 8; j++) { |
| 1343 |
ct = 0; |
| 1344 |
rp = Rooms; |
| 1345 |
|
| 1346 |
while (ct < nr + 1) { |
| 1347 |
rp->Exits[j] = *ptr++; |
| 1348 |
|
| 1349 |
if (CurrentSys == SYS_APPLE2 && j == 7) { |
| 1350 |
int adr = rp->Exits[j] * 0x100; |
| 1351 |
adr += *ptr++ * 0x10; |
| 1352 |
adr *= 0x10; |
| 1353 |
debug_print("Room image %d address:%x\n", ct, adr); |
| 1354 |
rp->Exits[j] = adr; |
| 1355 |
} else if (j > 5) { |
| 1356 |
rp->Exits[j] |= *ptr; |
| 1357 |
ptr++; |
| 1358 |
} |
| 1359 |
|
| 1360 |
if (j == 7) { |
| 1361 |
rp->Image = rp->Exits[j]; |
| 1362 |
} |
| 1363 |
|
| 1364 |
debug_print("Room %d exit %d: %d\n", ct, j, rp->Exits[j]); |
| 1365 |
ct++; |
| 1366 |
rp++; |
| 1367 |
} |
| 1368 |
if (isSTSpiderman || isSTFF) |
| 1369 |
ptr++; |
| 1370 |
} |
| 1371 |
|
| 1372 |
if (isSTSpiderman || isSTFF) { |
| 1373 |
ptr -= 2; |
| 1374 |
} |
| 1375 |
|
| 1376 |
#pragma mark messages |
| 1377 |
|
| 1378 |
Messages = LoadMessages(GameHeader.NumMessages + 1, &ptr); |
| 1379 |
|
| 1380 |
for (int i = 0; i <= GameHeader.NumRooms; i++) { |
| 1381 |
if (Rooms[i].Exits[6] == 0) |
| 1382 |
Rooms[i].Text = ""; |
| 1383 |
else |
| 1384 |
Rooms[i].Text = Messages[Rooms[i].Exits[6] - 76]; |
| 1385 |
debug_print("Room description of room %d: \"%s\"\n", i, Rooms[i].Text); |
| 1386 |
} |
| 1387 |
|
| 1388 |
#pragma mark items |
| 1389 |
|
| 1390 |
ct = 0; |
| 1391 |
ip = Items; |
| 1392 |
size_t length; |
| 1393 |
|
| 1394 |
if (CurrentSys == SYS_ST && !isSTFF) |
| 1395 |
ptr++; |
| 1396 |
|
| 1397 |
while (ct < ni + 1) { |
| 1398 |
ptr = ReadPlusString(ptr, &ip->Text, &length); |
| 1399 |
ip++; |
| 1400 |
ct++; |
| 1401 |
} |
| 1402 |
|
| 1403 |
#pragma mark item locations |
| 1404 |
if (isSTSpiderman || isSTFF) { |
| 1405 |
ptr++; |
| 1406 |
} |
| 1407 |
|
| 1408 |
for (ct = 0, ip = Items; ct <= ni; ct++, ip++) { |
| 1409 |
ip->Location = *ptr++; |
| 1410 |
ip->InitialLoc = ip->Location; |
| 1411 |
} |
| 1412 |
|
| 1413 |
if (CurrentSys == SYS_ST) { |
| 1414 |
ptr += 2; |
| 1415 |
} |
| 1416 |
|
| 1417 |
for (ct = 0, ip = Items; ct <= ni; ct++, ip++) { |
| 1418 |
ip->Dictword = *ptr++; |
| 1419 |
ip->Dictword += *ptr++ * 256; |
| 1420 |
} |
| 1421 |
|
| 1422 |
for (ct = 0, ip = Items; ct <= ni; ct++, ip++) { |
| 1423 |
ip->Flag = *ptr++; |
| 1424 |
ip->Flag += *ptr++ * 256; |
| 1425 |
debug_print("Item %d: \"%s\", %d, %d, %d\n", ct, ip->Text, ip->Location, ip->Dictword, ip->Flag); |
| 1426 |
} |
| 1427 |
|
| 1428 |
if (CurrentSys == SYS_ST) |
| 1429 |
ptr -= 1; |
| 1430 |
|
| 1431 |
Adverbs = ReadDictWords(&ptr, GameHeader.NumAdverbs + 1, 1); |
| 1432 |
Prepositions = ReadDictWords(&ptr, GameHeader.NumPreps + 1, 1); |
| 1433 |
|
| 1434 |
#pragma mark substitutions |
| 1435 |
|
| 1436 |
Substitutions = ReadSubstitutionsBinary(&ptr, GameHeader.NumSubStr + 1, 1); |
| 1437 |
|
| 1438 |
char *title = NULL; |
| 1439 |
ptr = ReadPlusString(ptr, &title, &length); |
| 1440 |
debug_print("Title: %s\n", title); |
| 1441 |
|
| 1442 |
if (!SetGame(title, length)) { |
| 1443 |
free(title); |
| 1444 |
return 0; |
| 1445 |
} |
| 1446 |
|
| 1447 |
if (isSTFF) |
| 1448 |
ptr++; |
| 1449 |
|
| 1450 |
for (ct = 0; ct <= oi; ct++) |
| 1451 |
ObjectImages[ct].Room = *ptr++; |
| 1452 |
|
| 1453 |
if (CurrentSys == SYS_ST && !isSTSpiderman) |
| 1454 |
ptr++; |
| 1455 |
|
| 1456 |
for (ct = 0; ct <= oi; ct++) |
| 1457 |
ObjectImages[ct].Object = *ptr++; |
| 1458 |
|
| 1459 |
if (CurrentSys == SYS_ST) { |
| 1460 |
ptr++; |
| 1461 |
if (!isSTSpiderman) |
| 1462 |
ptr++; |
| 1463 |
} |
| 1464 |
|
| 1465 |
for (ct = 0; ct <= oi; ct++) { |
| 1466 |
ObjectImages[ct].Image = *ptr++; |
| 1467 |
if (CurrentSys == SYS_APPLE2) { |
| 1468 |
int adr = ObjectImages[ct].Image * 0x100; |
| 1469 |
adr += *ptr++ * 0x10; |
| 1470 |
adr *= 0x10; |
| 1471 |
debug_print("Object image %d address:%x\n", ct, adr); |
| 1472 |
ObjectImages[ct].Image = adr; |
| 1473 |
} else |
| 1474 |
ptr++; |
| 1475 |
} |
| 1476 |
|
| 1477 |
for (ct = 0; ct <= oi; ct++) |
| 1478 |
debug_print("ObjectImages %d: room:%d object:%d image:%x\n", ct, ObjectImages[ct].Room, ObjectImages[ct].Object, ObjectImages[ct].Image); |
| 1479 |
|
| 1480 |
DumpActions(); |
| 1481 |
|
| 1482 |
return 1; |
| 1483 |
} |