| 1 |
/** |
| 2 |
* DinkC script engine |
| 3 |
|
| 4 |
* Copyright (C) 1997, 1998, 1999, 2002, 2003 Seth A. Robinson |
| 5 |
* Copyright (C) 2005, 2006 Dan Walma |
| 6 |
* Copyright (C) 2005, 2007, 2008, 2009, 2010, 2011, 2014, 2015 Sylvain Beucler |
| 7 |
|
| 8 |
* This file is part of GNU FreeDink |
| 9 |
|
| 10 |
* GNU FreeDink is free software; you can redistribute it and/or |
| 11 |
* modify it under the terms of the GNU General Public License as |
| 12 |
* published by the Free Software Foundation; either version 3 of the |
| 13 |
* License, or (at your option) any later version. |
| 14 |
|
| 15 |
* GNU FreeDink is distributed in the hope that it will be useful, but |
| 16 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 18 |
* General Public License for more details. |
| 19 |
|
| 20 |
* You should have received a copy of the GNU General Public License |
| 21 |
* along with this program. If not, see |
| 22 |
* <http://www.gnu.org/licenses/>. |
| 23 |
*/ |
| 24 |
|
| 25 |
#ifdef HAVE_CONFIG_H |
| 26 |
#include <config.h> |
| 27 |
#endif |
| 28 |
|
| 29 |
#include <stdio.h> |
| 30 |
#include <stdlib.h> |
| 31 |
#include <string.h> |
| 32 |
#include <strings.h> /* compare */ |
| 33 |
#include <xalloc.h> |
| 34 |
|
| 35 |
#include "gettext.h" |
| 36 |
#define _(String) gettext (String) |
| 37 |
|
| 38 |
#include "dinkc.h" |
| 39 |
#include "dinkc_bindings.h" |
| 40 |
#include "game_engine.h" |
| 41 |
#include "live_sprites_manager.h" |
| 42 |
#include "input.h" |
| 43 |
#include "paths.h" |
| 44 |
#include "str_util.h" |
| 45 |
#include "log.h" |
| 46 |
#include "editor_screen.h" |
| 47 |
#include "game_choice.h" |
| 48 |
#include "i18n.h" |
| 49 |
|
| 50 |
/* store current procedure arguments expanded values of type 'int' (see get_parms) */ |
| 51 |
static long nlist[10]; |
| 52 |
/* store current procedure arguments of type 'string' (idem) */ |
| 53 |
static char* slist[10]; |
| 54 |
char* cur_funcname = NULL; |
| 55 |
|
| 56 |
int returnint = 0; |
| 57 |
int bKeepReturnInt = 0; |
| 58 |
char returnstring[200]; |
| 59 |
/* Used to tell decipher_string about the currently selected savegame |
| 60 |
in a choice menu; also abuse to tell which key is selected in |
| 61 |
joystick remapping */ |
| 62 |
unsigned short decipher_savegame = 0; |
| 63 |
|
| 64 |
#define MAX_CALLBACKS 100 |
| 65 |
struct call_back |
| 66 |
{ |
| 67 |
int owner; // script ID |
| 68 |
/*bool*/int active; |
| 69 |
int type; |
| 70 |
char name[20]; |
| 71 |
int offset; |
| 72 |
long min, max; |
| 73 |
int lifespan; |
| 74 |
unsigned long timer; |
| 75 |
}; |
| 76 |
static struct call_back callback[MAX_CALLBACKS]; |
| 77 |
|
| 78 |
/* Number of reserved ASCII indexes in .d BPE compression format */ |
| 79 |
#define NB_PAIRS_MAX 128 |
| 80 |
|
| 81 |
|
| 82 |
struct refinfo *rinfo[MAX_SCRIPTS]; |
| 83 |
static char *rinfo_code[MAX_SCRIPTS]; |
| 84 |
|
| 85 |
int weapon_script = 0; |
| 86 |
int magic_script = 0; |
| 87 |
|
| 88 |
static enum dinkc_parser_state process_line (int script, char *s, /*bool*/int doelse); |
| 89 |
|
| 90 |
|
| 91 |
/** |
| 92 |
* Decompress a .d DinkC script; also clean newlines. Check |
| 93 |
* contrib/d2c.c for more explanation about the decompression process. |
| 94 |
*/ |
| 95 |
static char* byte_pair_decompress_stream(FILE* in) |
| 96 |
{ |
| 97 |
int step = 512; |
| 98 |
int nb_read = 0; |
| 99 |
char* ret = (char*)xmalloc(step); |
| 100 |
ret[0] = '\0'; |
| 101 |
|
| 102 |
unsigned char stack[NB_PAIRS_MAX+1], pairs[NB_PAIRS_MAX][2]; |
| 103 |
short c, top = -1; |
| 104 |
int nb_pairs = 0; |
| 105 |
|
| 106 |
/* Check for optional pair count and pair table */ |
| 107 |
if ((c = fgetc(in)) > 127) |
| 108 |
{ |
| 109 |
/* Read pairs table */ |
| 110 |
nb_pairs = c - 128; |
| 111 |
int i, j; |
| 112 |
for (i = 0; i < nb_pairs; i++) |
| 113 |
{ |
| 114 |
for (j = 0; j < 2; j++) |
| 115 |
{ |
| 116 |
int c = fgetc(in); |
| 117 |
if (c == EOF) |
| 118 |
{ |
| 119 |
log_error("decompress: invalid header: truncated pair table"); |
| 120 |
free(ret); |
| 121 |
return NULL; |
| 122 |
} |
| 123 |
if (c > i+128) |
| 124 |
{ |
| 125 |
log_error("decompress: invalid header: reference to a pair that is not registered yet"); |
| 126 |
free(ret); |
| 127 |
return NULL; |
| 128 |
} |
| 129 |
pairs[i][j] = c; |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
else |
| 134 |
{ |
| 135 |
/* Non-compressed file, put back the character we read */ |
| 136 |
ungetc(c, in); |
| 137 |
} |
| 138 |
|
| 139 |
for (;;) |
| 140 |
{ |
| 141 |
/* Pop byte from stack or read byte from file */ |
| 142 |
if (top >= 0) |
| 143 |
c = stack[top--]; |
| 144 |
else if ((c = fgetc(in)) == EOF) |
| 145 |
break; |
| 146 |
|
| 147 |
/* Push pair on stack or output byte to file */ |
| 148 |
if (c > 127) |
| 149 |
{ |
| 150 |
if ((c-128) >= nb_pairs) |
| 151 |
{ |
| 152 |
log_error("decompress: invalid body: references non-existent pair"); |
| 153 |
break; |
| 154 |
} |
| 155 |
stack[++top] = pairs[c-128][1]; |
| 156 |
stack[++top] = pairs[c-128][0]; |
| 157 |
} |
| 158 |
else |
| 159 |
{ |
| 160 |
ret[nb_read] = c; |
| 161 |
nb_read++; |
| 162 |
if ((nb_read % step) == 0) |
| 163 |
ret = xrealloc(ret, nb_read + step); |
| 164 |
} |
| 165 |
} |
| 166 |
ret[nb_read] = '\0'; /* safety */ |
| 167 |
ret = xrealloc(ret, nb_read+1); |
| 168 |
|
| 169 |
return ret; |
| 170 |
} |
| 171 |
|
| 172 |
static char* read_stream(FILE *in) |
| 173 |
{ |
| 174 |
int step = 512; |
| 175 |
int nb_read = 0; |
| 176 |
char* ret = (char*)xmalloc(step); |
| 177 |
ret[0] = '\0'; |
| 178 |
|
| 179 |
int c; |
| 180 |
while ((c = getc(in)) != EOF) |
| 181 |
{ |
| 182 |
ret[nb_read] = c; |
| 183 |
nb_read++; |
| 184 |
if ((nb_read % step) == 0) |
| 185 |
ret = xrealloc(ret, nb_read + step); |
| 186 |
} |
| 187 |
ret[nb_read] = '\0'; /* safety */ |
| 188 |
ret = xrealloc(ret, nb_read+1); |
| 189 |
|
| 190 |
return ret; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
/** |
| 195 |
* Only load game metadata (timetime). Used when displaying the list |
| 196 |
* of saved games (see decipher_string). |
| 197 |
*/ |
| 198 |
static /*bool*/int load_game_small(int num, char line[196], int *mytime) |
| 199 |
{ |
| 200 |
FILE *f = paths_savegame_fopen(num, "rb"); |
| 201 |
if (f == NULL) |
| 202 |
{ |
| 203 |
log_info("Couldn't quickload save game %d", num); |
| 204 |
return /*false*/0; |
| 205 |
} |
| 206 |
else |
| 207 |
{ |
| 208 |
//int version = read_lsb_int(f); |
| 209 |
fseek(f, 4, SEEK_CUR); |
| 210 |
|
| 211 |
fread(line, 196, 1, f); |
| 212 |
line[195] = '\0'; |
| 213 |
*mytime = read_lsb_int(f); |
| 214 |
fclose(f); |
| 215 |
|
| 216 |
return /*true*/1; |
| 217 |
} |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
/* Find available script slot */ |
| 222 |
int script_find_slot() { |
| 223 |
int k = 1; |
| 224 |
for (k = 1; k < MAX_SCRIPTS; k++) |
| 225 |
if (rinfo[k] == NULL) |
| 226 |
break; |
| 227 |
|
| 228 |
if (k < MAX_SCRIPTS) |
| 229 |
return k; |
| 230 |
else |
| 231 |
return -1; |
| 232 |
} |
| 233 |
|
| 234 |
/** |
| 235 |
* Initialize script structure |
| 236 |
*/ |
| 237 |
static int script_init(const char* name, char* code) { |
| 238 |
int script = script_find_slot(); |
| 239 |
if (script <= 0) |
| 240 |
return -1; |
| 241 |
|
| 242 |
rinfo[script] = XZALLOC(struct refinfo); |
| 243 |
if (rinfo[script] == NULL) |
| 244 |
return -1; |
| 245 |
|
| 246 |
memset(rinfo[script], 0, sizeof(struct refinfo)); |
| 247 |
|
| 248 |
rinfo[script]->name = strdup(name); |
| 249 |
/* For clarity: */ |
| 250 |
rinfo[script]->current = 0; |
| 251 |
rinfo[script]->cur_line = 1; |
| 252 |
rinfo[script]->cur_col = 0; |
| 253 |
rinfo[script]->debug_line = 1; |
| 254 |
|
| 255 |
rinfo_code[script] = code; |
| 256 |
rinfo[script]->end = strlen(code); |
| 257 |
|
| 258 |
return script; |
| 259 |
} |
| 260 |
/* export (non-static) for the test suite */ |
| 261 |
int ts_script_init(const char* name, char* code) { |
| 262 |
return script_init(name, code); |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Start a simple script, usually from the script console |
| 267 |
*/ |
| 268 |
int dinkc_execute_one_liner(char* line) |
| 269 |
{ |
| 270 |
char* code = strdup(line); |
| 271 |
int script = script_init("one-liner", code); |
| 272 |
if (script <= 0) { |
| 273 |
free(code); |
| 274 |
return -1; |
| 275 |
} |
| 276 |
|
| 277 |
rinfo[script]->sprite = 1000; /* survive screen change */ |
| 278 |
rinfo[script]->level = 1; /* skip 'void main(void) {' parsing */ |
| 279 |
|
| 280 |
process_line(script, rinfo_code[script], 0); |
| 281 |
return returnint; |
| 282 |
} |
| 283 |
|
| 284 |
static FILE* locate_script(char* script_name, int* compressed) { |
| 285 |
char temp[100]; |
| 286 |
FILE *in = NULL; |
| 287 |
|
| 288 |
log_info("LOADING %s", script_name); |
| 289 |
|
| 290 |
sprintf(temp, "story/%s.d", script_name); |
| 291 |
in = paths_dmodfile_fopen(temp, "rb"); |
| 292 |
if (in == NULL) |
| 293 |
{ |
| 294 |
sprintf(temp, "story/%s.c", script_name); |
| 295 |
in = paths_dmodfile_fopen(temp, "rb"); |
| 296 |
if (in == NULL) |
| 297 |
{ |
| 298 |
sprintf(temp, "story/%s.d", script_name); |
| 299 |
in = paths_fallbackfile_fopen(temp, "rb"); |
| 300 |
if (in == NULL) |
| 301 |
{ |
| 302 |
sprintf(temp, "story/%s.c", script_name); |
| 303 |
in = paths_fallbackfile_fopen(temp, "rb"); |
| 304 |
if (in == NULL) |
| 305 |
{ |
| 306 |
return NULL; |
| 307 |
} |
| 308 |
} |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
strtoupper(temp); |
| 313 |
log_debug("Located %s", temp); |
| 314 |
if (temp[strlen(temp)-1] == 'D') |
| 315 |
*compressed = 1; |
| 316 |
else |
| 317 |
*compressed = 0; |
| 318 |
|
| 319 |
return in; |
| 320 |
} |
| 321 |
|
| 322 |
/** |
| 323 |
* Load script from 'filename', save it in the first available script |
| 324 |
* slot, attach to game sprite #'sprite' if 'set_sprite' is 1. |
| 325 |
**/ |
| 326 |
int load_script(char* script_name, int sprite) |
| 327 |
{ |
| 328 |
// Locate script |
| 329 |
int compressed = 0; |
| 330 |
FILE* in = locate_script(script_name, &compressed); |
| 331 |
if (in == NULL) { |
| 332 |
log_warn("Script %s not found. (checked for .C and .D) (requested by %d?)", |
| 333 |
script_name, sprite); |
| 334 |
return 0; |
| 335 |
} |
| 336 |
|
| 337 |
// Read script |
| 338 |
char* script_code = NULL; |
| 339 |
if (compressed) |
| 340 |
{ |
| 341 |
log_debug("Decompressing..."); |
| 342 |
script_code = byte_pair_decompress_stream(in); |
| 343 |
} |
| 344 |
else |
| 345 |
{ |
| 346 |
log_debug("Reading from disk..."); |
| 347 |
script_code = read_stream(in); |
| 348 |
} |
| 349 |
fclose(in); |
| 350 |
|
| 351 |
if (script_code == NULL) |
| 352 |
{ |
| 353 |
log_error("Error reading %s", script_name); |
| 354 |
return 0; |
| 355 |
} |
| 356 |
|
| 357 |
// Create script |
| 358 |
int script = script_init(script_name, script_code); |
| 359 |
log_info("Loading script %s.. (slot %d)", script_name, script); |
| 360 |
if (script < 0) |
| 361 |
{ |
| 362 |
log_error("Couldn't find unused buffer for script."); |
| 363 |
free(script_code); |
| 364 |
return 0; |
| 365 |
} |
| 366 |
|
| 367 |
rinfo[script]->sprite = sprite; |
| 368 |
|
| 369 |
return script; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
/** |
| 374 |
* Remove leading spaces by shifting 'str' to the left, as much as |
| 375 |
* there is leading spaces. |
| 376 |
*/ |
| 377 |
void strip_beginning_spaces(char *str) |
| 378 |
{ |
| 379 |
char *pc = str; |
| 380 |
int diff = 0; |
| 381 |
|
| 382 |
/* Find first non-space character (pos) */ |
| 383 |
while (*pc == ' ') |
| 384 |
pc++; |
| 385 |
diff = pc - str; |
| 386 |
|
| 387 |
/* Shift string to the left from pos */ |
| 388 |
/* Don't use str(str, pc) to avoid memory overlap */ |
| 389 |
while (*pc != '\0') |
| 390 |
{ |
| 391 |
*(pc - diff) = *pc; |
| 392 |
pc++; |
| 393 |
} |
| 394 |
*(pc - diff) = '\0'; |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|
| 399 |
/** |
| 400 |
* Locate a procedure (such as "void hit()") |
| 401 |
*/ |
| 402 |
/*bool*/int locate(int script, char* lookup_proc) |
| 403 |
{ |
| 404 |
if (rinfo[script] == NULL) |
| 405 |
return 0; |
| 406 |
|
| 407 |
int save_current = rinfo[script]->current; |
| 408 |
int save_cur_line = rinfo[script]->cur_line; |
| 409 |
int save_cur_col = rinfo[script]->cur_col; |
| 410 |
int save_debug_line = rinfo[script]->debug_line; |
| 411 |
rinfo[script]->current = 0; |
| 412 |
rinfo[script]->cur_line = 1; |
| 413 |
rinfo[script]->cur_col = 0; |
| 414 |
rinfo[script]->debug_line = 1; |
| 415 |
|
| 416 |
char* line = NULL; |
| 417 |
char* word = NULL; |
| 418 |
|
| 419 |
while((line = read_next_line(script)) != NULL) |
| 420 |
{ |
| 421 |
strip_beginning_spaces(line); |
| 422 |
|
| 423 |
int is_proc = 0; |
| 424 |
word = get_word(line, 1); |
| 425 |
if (compare(word, "VOID")) |
| 426 |
is_proc = 1; |
| 427 |
free(word); |
| 428 |
if (is_proc) |
| 429 |
{ |
| 430 |
char* cur_proc = NULL; |
| 431 |
word = get_word(line, 2); |
| 432 |
cur_proc = separate_string(word, 1, '('); |
| 433 |
free(word); |
| 434 |
|
| 435 |
int is_right_proc = 0; |
| 436 |
if (compare(cur_proc, lookup_proc)) |
| 437 |
is_right_proc = 1; |
| 438 |
free(cur_proc); |
| 439 |
|
| 440 |
if (is_right_proc) |
| 441 |
{ |
| 442 |
//clean up vars so it is ready to run |
| 443 |
if (rinfo[script]->sprite != 1000) |
| 444 |
{ |
| 445 |
// TODO: move out so we don't depend on 'spr' |
| 446 |
// reset move/move_stop moves |
| 447 |
spr[rinfo[script]->sprite].move_active = 0; |
| 448 |
if (dversion >= 108) |
| 449 |
// also reinit move_nohard as in brain.cpp:done_move |
| 450 |
spr[rinfo[script]->sprite].move_nohard = 0; |
| 451 |
} |
| 452 |
rinfo[script]->skipnext = /*false*/0; |
| 453 |
rinfo[script]->onlevel = 0; |
| 454 |
rinfo[script]->level = 0; |
| 455 |
|
| 456 |
free(line); |
| 457 |
return 1; |
| 458 |
//this is desired proc |
| 459 |
} |
| 460 |
} |
| 461 |
free(line); |
| 462 |
} |
| 463 |
|
| 464 |
// Not found, restoring position |
| 465 |
rinfo[script]->current = save_current; |
| 466 |
rinfo[script]->cur_line = save_cur_line; |
| 467 |
rinfo[script]->cur_col = save_cur_col; |
| 468 |
rinfo[script]->debug_line = save_debug_line; |
| 469 |
return 0; |
| 470 |
} |
| 471 |
|
| 472 |
/** |
| 473 |
* Look for the 'label' label (e.g. 'loop:'), that is used by a "goto" |
| 474 |
* instruction. This sets the script->current field appropriately. |
| 475 |
**/ |
| 476 |
/*bool*/int locate_goto(char* expr, int script) |
| 477 |
{ |
| 478 |
replace_norealloc(";", "", expr); |
| 479 |
char* label = (char*)xmalloc(strlen(expr) + 1 + 1); |
| 480 |
sprintf(label, "%s:", expr); |
| 481 |
|
| 482 |
char* line = NULL; |
| 483 |
rinfo[script]->current = 0; |
| 484 |
rinfo[script]->cur_line = 1; |
| 485 |
while ((line = read_next_line(script)) != NULL) |
| 486 |
{ |
| 487 |
strip_beginning_spaces(line); |
| 488 |
|
| 489 |
int is_right_label = 0; |
| 490 |
char* word = get_word(line, 1); |
| 491 |
replace_norealloc("\n", "", word); |
| 492 |
if (compare(word, label)) |
| 493 |
is_right_label = 1; |
| 494 |
free(word); |
| 495 |
|
| 496 |
if (is_right_label) |
| 497 |
{ |
| 498 |
log_debug("Found goto : Line is %s, word is %s.", line, label); |
| 499 |
|
| 500 |
rinfo[script]->skipnext = /*false*/0; |
| 501 |
rinfo[script]->onlevel = 0; |
| 502 |
rinfo[script]->level = 0; |
| 503 |
|
| 504 |
free(label); |
| 505 |
free(line); |
| 506 |
return 1; |
| 507 |
//this is desired label |
| 508 |
} |
| 509 |
free(line); |
| 510 |
} |
| 511 |
|
| 512 |
log_warn("%s: cannot goto %s", rinfo[script]->name, label); |
| 513 |
free(label); |
| 514 |
return 0; |
| 515 |
} |
| 516 |
|
| 517 |
/** |
| 518 |
* Lookup variable with this precise scope |
| 519 |
*/ |
| 520 |
int lookup_var(char* variable, int scope) |
| 521 |
{ |
| 522 |
int i; |
| 523 |
for (i = 1; i < MAX_VARS; i++) |
| 524 |
if (play.var[i].active |
| 525 |
&& compare(play.var[i].name, variable) |
| 526 |
&& scope == play.var[i].scope) |
| 527 |
return i; |
| 528 |
return 0; |
| 529 |
} |
| 530 |
|
| 531 |
/** |
| 532 |
* v1.07-style scope. This function is buggy: the first memory slot |
| 533 |
* has precedence (independently of local/global scope). |
| 534 |
* |
| 535 |
* Return -1 if not found, slot index >1 if found. Slot 0 isn't |
| 536 |
* currently used by the engine. |
| 537 |
*/ |
| 538 |
static int lookup_var_local_global_107(char* variable, int var_scope) |
| 539 |
{ |
| 540 |
int i; |
| 541 |
for (i = 1; i < MAX_VARS; i ++) |
| 542 |
if (play.var[i].active == 1 |
| 543 |
&& ((play.var[i].scope == DINKC_GLOBAL_SCOPE) |
| 544 |
|| (play.var[i].scope == var_scope)) |
| 545 |
&& (compare(play.var[i].name, variable))) |
| 546 |
return i; |
| 547 |
return -1; /* not found */ |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* v1.08-style scope: local variables are searched before global |
| 552 |
* variables. |
| 553 |
* |
| 554 |
* Return -1 if not found, slot index >1 if found. Slot 0 isn't |
| 555 |
* currently used by the engine. |
| 556 |
*/ |
| 557 |
static int lookup_var_local_global_108(char* variable, int var_scope) |
| 558 |
{ |
| 559 |
int search_scope[2]; |
| 560 |
search_scope[0] = var_scope; /* first local scope */ |
| 561 |
search_scope[1] = DINKC_GLOBAL_SCOPE; /* then global scope */ |
| 562 |
|
| 563 |
int i; |
| 564 |
for (i = 0; i < 2; i++) |
| 565 |
{ |
| 566 |
//We'll start going through every var, starting at one |
| 567 |
int v; |
| 568 |
for (v = 1; v < MAX_VARS; v++) |
| 569 |
{ |
| 570 |
//Okay... make sure the var is active, |
| 571 |
//The scope should match the script, |
| 572 |
//Then make sure the name is the same. |
| 573 |
if (play.var[v].active |
| 574 |
&& play.var[v].scope == search_scope[i] |
| 575 |
&& compare (play.var[v].name, variable)) |
| 576 |
return v; |
| 577 |
} |
| 578 |
} |
| 579 |
return -1; |
| 580 |
} |
| 581 |
|
| 582 |
/** |
| 583 |
* Lookup variable with local->global nested scope |
| 584 |
*/ |
| 585 |
static int lookup_var_local_global(char* variable, int scope) |
| 586 |
{ |
| 587 |
if (dversion >= 108) |
| 588 |
return lookup_var_local_global_108(variable, scope); |
| 589 |
else |
| 590 |
return lookup_var_local_global_107(variable, scope); |
| 591 |
} |
| 592 |
|
| 593 |
/* Test suite proxy */ |
| 594 |
int ts_lookup_var_local_global(char* variable, int scope) { |
| 595 |
return lookup_var_local_global(variable, scope); |
| 596 |
} |
| 597 |
|
| 598 |
|
| 599 |
/** |
| 600 |
* Expand 'variable' in the scope of 'script' and return the integer |
| 601 |
* value. Only used in function 'get_parms'. |
| 602 |
*/ |
| 603 |
long decipher(char* variable, int script) |
| 604 |
{ |
| 605 |
// Special vars: ¤t_sprite and ¤t_script |
| 606 |
if (compare(variable, "¤t_sprite")) |
| 607 |
return rinfo[script]->sprite; |
| 608 |
if (compare(variable, "¤t_script")) |
| 609 |
return script; |
| 610 |
|
| 611 |
//v1.08 special variables. |
| 612 |
if (dversion >= 108) |
| 613 |
{ |
| 614 |
if (compare(variable, "&return")) |
| 615 |
return returnint; |
| 616 |
if (compare(variable, "&arg1")) |
| 617 |
return rinfo[script]->arg1; |
| 618 |
if (compare(variable, "&arg2")) |
| 619 |
return rinfo[script]->arg2; |
| 620 |
if (compare(variable, "&arg3")) |
| 621 |
return rinfo[script]->arg3; |
| 622 |
if (compare(variable, "&arg4")) |
| 623 |
return rinfo[script]->arg4; |
| 624 |
if (compare(variable, "&arg5")) |
| 625 |
return rinfo[script]->arg5; |
| 626 |
if (compare(variable, "&arg6")) |
| 627 |
return rinfo[script]->arg6; |
| 628 |
if (compare(variable, "&arg7")) |
| 629 |
return rinfo[script]->arg7; |
| 630 |
if (compare(variable, "&arg8")) |
| 631 |
return rinfo[script]->arg8; |
| 632 |
if (compare(variable, "&arg9")) |
| 633 |
return rinfo[script]->arg9; |
| 634 |
} |
| 635 |
|
| 636 |
// Check in local and global variables |
| 637 |
int i = lookup_var_local_global(variable, script); |
| 638 |
if (i != -1) |
| 639 |
return play.var[i].var; |
| 640 |
else |
| 641 |
return 0; // compatibility |
| 642 |
} |
| 643 |
|
| 644 |
|
| 645 |
/** |
| 646 |
* Replace all variables in a string; try longest variables |
| 647 |
* first. Known bug: may replace shorter variables (e.g. &gold instead |
| 648 |
* of &golden). |
| 649 |
*/ |
| 650 |
void var_replace_107(char** line_p, int scope) |
| 651 |
{ |
| 652 |
char crap[20]; |
| 653 |
int i; |
| 654 |
for (i = 1; i < MAX_VARS; i ++) |
| 655 |
if ((play.var[i].active == 1) |
| 656 |
&& ((play.var[i].scope == DINKC_GLOBAL_SCOPE) || (play.var[i].scope == scope))) |
| 657 |
{ |
| 658 |
sprintf(crap, "%d", play.var[i].var); |
| 659 |
replace(play.var[i].name, crap, line_p); |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
/** |
| 664 |
* Replace all variables in a string; try longest variables first. |
| 665 |
* |
| 666 |
* Possible improvements: |
| 667 |
* |
| 668 |
* - Copy play.var[] and sort it by variable length (and avoid the |
| 669 |
* recursion) |
| 670 |
* |
| 671 |
* - find vars in the string and replace them as-needed (requires |
| 672 |
* understanding what exactly is an end-of-variable delimiter, if |
| 673 |
* such a thing exists) |
| 674 |
*/ |
| 675 |
void var_replace_108(int i, int script, char** line_p, char *prevar) |
| 676 |
{ |
| 677 |
while (i < MAX_VARS) |
| 678 |
{ |
| 679 |
//First, make sure the variable is active. |
| 680 |
//Then, make sure it is in scope, |
| 681 |
//Then, see if the variable name is in the line |
| 682 |
//Then, prevar is null, or if prevar isn't null, see if current variable starts with prevar |
| 683 |
if (play.var[i].active |
| 684 |
&& i == lookup_var_local_global_108(play.var[i].name, script) |
| 685 |
&& strstr (*line_p, play.var[i].name) |
| 686 |
&& (prevar == NULL || (prevar != NULL && strstr (play.var[i].name, prevar)))) |
| 687 |
{ |
| 688 |
//Look for shorter variables |
| 689 |
var_replace_108(i + 1, script, line_p, play.var[i].name); |
| 690 |
//we didn't find any, so we replace! |
| 691 |
char crap[20]; |
| 692 |
sprintf(crap, "%d", play.var[i].var); |
| 693 |
replace(play.var[i].name, crap, line_p); |
| 694 |
} |
| 695 |
i++; |
| 696 |
} |
| 697 |
} |
| 698 |
|
| 699 |
/** |
| 700 |
* Replace all variables (&something) in 'line', with scope 'scope' |
| 701 |
*/ |
| 702 |
void var_replace(char** line_p, int scope) |
| 703 |
{ |
| 704 |
if (dversion >= 108) |
| 705 |
var_replace_108(1, scope, line_p, NULL); |
| 706 |
else |
| 707 |
var_replace_107(line_p, scope); |
| 708 |
} |
| 709 |
|
| 710 |
|
| 711 |
/** |
| 712 |
* Similar to decipher, plus: |
| 713 |
* - expand special choice variables &savegameinfo and &buttoninfo |
| 714 |
* - it can replace several variables in the same string |
| 715 |
* - with v1.07 it has a prefix bug (see var_replace_107) |
| 716 |
*/ |
| 717 |
void decipher_string(char** line_p, int script) |
| 718 |
{ |
| 719 |
char buffer[20 + 1]; |
| 720 |
|
| 721 |
/* Replace all valid variables in 'line' */ |
| 722 |
var_replace(line_p, script); |
| 723 |
|
| 724 |
if ((strchr(*line_p, '&') != NULL) && (script != 0)) |
| 725 |
{ |
| 726 |
sprintf(buffer, "%d", rinfo[script]->sprite); replace("¤t_sprite", buffer, line_p); |
| 727 |
sprintf(buffer, "%d", script); replace("¤t_script", buffer, line_p); |
| 728 |
|
| 729 |
if (dversion >= 108) |
| 730 |
{ |
| 731 |
//v1.08 special variables. |
| 732 |
sprintf(buffer, "%d", returnint); replace("&return", buffer, line_p); |
| 733 |
sprintf(buffer, "%d", rinfo[script]->arg1); replace("&arg1", buffer, line_p); |
| 734 |
sprintf(buffer, "%d", rinfo[script]->arg2); replace("&arg2", buffer, line_p); |
| 735 |
sprintf(buffer, "%d", rinfo[script]->arg3); replace("&arg3", buffer, line_p); |
| 736 |
sprintf(buffer, "%d", rinfo[script]->arg4); replace("&arg4", buffer, line_p); |
| 737 |
sprintf(buffer, "%d", rinfo[script]->arg5); replace("&arg5", buffer, line_p); |
| 738 |
sprintf(buffer, "%d", rinfo[script]->arg6); replace("&arg6", buffer, line_p); |
| 739 |
sprintf(buffer, "%d", rinfo[script]->arg7); replace("&arg7", buffer, line_p); |
| 740 |
sprintf(buffer, "%d", rinfo[script]->arg8); replace("&arg8", buffer, line_p); |
| 741 |
sprintf(buffer, "%d", rinfo[script]->arg9); replace("&arg9", buffer, line_p); |
| 742 |
} |
| 743 |
|
| 744 |
if (decipher_savegame != 0) |
| 745 |
{ |
| 746 |
// TODO: break dep on input, replace input_get_button_action by a callback |
| 747 |
int button_action = input_get_button_action(decipher_savegame-1); |
| 748 |
if (button_action == ACTION_ATTACK) replace("&buttoninfo", _("Attack"), line_p); |
| 749 |
else if (button_action == ACTION_TALK) replace("&buttoninfo", _("Talk/Examine"), line_p); |
| 750 |
else if (button_action == ACTION_MAGIC) replace("&buttoninfo", _("Magic"), line_p); |
| 751 |
else if (button_action == ACTION_INVENTORY) replace("&buttoninfo", _("Item Screen"), line_p); |
| 752 |
else if (button_action == ACTION_MENU) replace("&buttoninfo", _("Main Menu"), line_p); |
| 753 |
else if (button_action == ACTION_MAP) replace("&buttoninfo", _("Map"), line_p); |
| 754 |
else if (button_action == ACTION_BUTTON7) replace("&buttoninfo", _("Unused"), line_p); |
| 755 |
else if (button_action == ACTION_BUTTON8) replace("&buttoninfo", _("Unused"), line_p); |
| 756 |
else if (button_action == ACTION_BUTTON9) replace("&buttoninfo", _("Unused"), line_p); |
| 757 |
else if (button_action == ACTION_BUTTON10) replace("&buttoninfo", _("Unused"), line_p); |
| 758 |
else if (button_action == ACTION_DOWN) replace("&buttoninfo", _("Down"), line_p); |
| 759 |
else if (button_action == ACTION_LEFT) replace("&buttoninfo", _("Left"), line_p); |
| 760 |
else if (button_action == ACTION_RIGHT) replace("&buttoninfo", _("Right"), line_p); |
| 761 |
else if (button_action == ACTION_UP) replace("&buttoninfo", _("Up"), line_p); |
| 762 |
else replace("&buttoninfo", _("Error: not mapped"), line_p); |
| 763 |
} |
| 764 |
} |
| 765 |
|
| 766 |
if ((decipher_savegame != 0) && compare(*line_p, "&savegameinfo")) |
| 767 |
{ |
| 768 |
char gameinfo[196] = ""; |
| 769 |
int mytime = 0; |
| 770 |
|
| 771 |
free(*line_p); |
| 772 |
if (load_game_small(decipher_savegame, gameinfo, &mytime) == 1) |
| 773 |
asprintf(line_p, _("Slot %d - %d:%02d - %s"), decipher_savegame, |
| 774 |
mytime/60, mytime%60, gameinfo); |
| 775 |
else |
| 776 |
asprintf(line_p, _("Slot %d - Empty"), decipher_savegame); |
| 777 |
} |
| 778 |
} |
| 779 |
|
| 780 |
/** |
| 781 |
* |
| 782 |
* name: name of the procedure() to call |
| 783 |
* n1: wait at least n1 milliseconds before callback |
| 784 |
* n2: wait at most n1+n2 milliseconds before callback |
| 785 |
* script: number of the script currently running |
| 786 |
**/ |
| 787 |
int add_callback(char name[20], int n1, int n2, int script) |
| 788 |
{ |
| 789 |
int k; |
| 790 |
for (k = 1; k < MAX_CALLBACKS; k++) |
| 791 |
{ |
| 792 |
if (callback[k].active == /*false*/0) |
| 793 |
{ |
| 794 |
memset(&callback[k], 0, sizeof(callback[k])); |
| 795 |
|
| 796 |
callback[k].active = /*true*/1; |
| 797 |
callback[k].min = n1; |
| 798 |
callback[k].max = n2; |
| 799 |
callback[k].owner = script; |
| 800 |
strcpy(callback[k].name, name); |
| 801 |
|
| 802 |
log_debug("Callback added to %d.", k); |
| 803 |
return(k); |
| 804 |
} |
| 805 |
} |
| 806 |
|
| 807 |
log_error("Couldn't add callback, all out of space"); |
| 808 |
return 0; |
| 809 |
} |
| 810 |
|
| 811 |
void kill_callback(int cb) |
| 812 |
{ |
| 813 |
if (cb >= 0 && cb < MAX_CALLBACKS) |
| 814 |
callback[cb].active = /*false*/0; |
| 815 |
} |
| 816 |
|
| 817 |
void kill_callbacks_owned_by_script(int script) |
| 818 |
{ |
| 819 |
int i = 1; |
| 820 |
for (; i < MAX_CALLBACKS; i++) |
| 821 |
{ |
| 822 |
if (callback[i].owner == script) |
| 823 |
{ |
| 824 |
log_debug("Kill_all_callbacks just killed %d for script %d", i, script); |
| 825 |
//killed callback |
| 826 |
callback[i].active = /*false*/0; |
| 827 |
} |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
|
| 832 |
/** |
| 833 |
* Free resources used by this script. |
| 834 |
* |
| 835 |
* Caution: this makes references from spr[].script dangling |
| 836 |
*/ |
| 837 |
void kill_script(int k) |
| 838 |
{ |
| 839 |
if (rinfo[k] != NULL) |
| 840 |
{ |
| 841 |
int i; |
| 842 |
|
| 843 |
kill_callbacks_owned_by_script(k); |
| 844 |
|
| 845 |
// Now let's kill all local vars associated with this script |
| 846 |
for (i = 1; i < MAX_VARS; i++) |
| 847 |
{ |
| 848 |
if (play.var[i].active && play.var[i].scope == k) |
| 849 |
play.var[i].active = /*false*/0; |
| 850 |
} |
| 851 |
log_debug("Killed script %s. (num %d)", rinfo[k]->name, k); |
| 852 |
|
| 853 |
if (rinfo[k]->name != NULL) |
| 854 |
free(rinfo[k]->name); |
| 855 |
if (rinfo[k] != NULL) |
| 856 |
free(rinfo[k]); |
| 857 |
rinfo[k] = NULL; |
| 858 |
if (rinfo_code[k] != NULL) |
| 859 |
free(rinfo_code[k]); |
| 860 |
rinfo_code[k] = NULL; |
| 861 |
} |
| 862 |
} |
| 863 |
|
| 864 |
|
| 865 |
/** |
| 866 |
* Kill all scripts except those attached to pseudo-sprite 1000, which |
| 867 |
* is meant to survive across screen changes |
| 868 |
* (kill_all_scripts_for_real(...) is more brutal) |
| 869 |
* |
| 870 |
* Used by gfx_tiles only |
| 871 |
*/ |
| 872 |
void kill_all_scripts(void) |
| 873 |
{ |
| 874 |
/* Kill scripts (except if attached to pseudo-sprite 1000) */ |
| 875 |
int k = 1; |
| 876 |
for (; k < MAX_SCRIPTS; k++) |
| 877 |
{ |
| 878 |
if (rinfo[k] != NULL) |
| 879 |
if (rinfo[k]->sprite != 1000) |
| 880 |
kill_script(k); |
| 881 |
} |
| 882 |
|
| 883 |
/* Kill pending callbacks (except if attached to pseudo-sprite 1000) */ |
| 884 |
for (k = 1; k < MAX_CALLBACKS; k++) |
| 885 |
{ |
| 886 |
if (callback[k].active |
| 887 |
&& (!(rinfo[callback[k].owner] != NULL) |
| 888 |
&& (rinfo[callback[k].owner]->sprite == 1000))) |
| 889 |
{ |
| 890 |
log_debug("Killed callback %d. (was attached to script %d)", |
| 891 |
k, callback[k].owner); |
| 892 |
callback[k].active = 0; |
| 893 |
} |
| 894 |
} |
| 895 |
} |
| 896 |
|
| 897 |
/** |
| 898 |
* Kill all scripts including those attached to pseudo-sprite 1000 |
| 899 |
*/ |
| 900 |
void kill_all_scripts_for_real(void) |
| 901 |
{ |
| 902 |
int k = 1; |
| 903 |
for (k = 1; k < MAX_SCRIPTS; k++) |
| 904 |
{ |
| 905 |
if (rinfo[k] != NULL) |
| 906 |
kill_script(k); |
| 907 |
} |
| 908 |
|
| 909 |
for (k = 1; k < MAX_CALLBACKS; k++) |
| 910 |
{ |
| 911 |
callback[k].active = 0; |
| 912 |
} |
| 913 |
} |
| 914 |
|
| 915 |
/** |
| 916 |
* Reset all vars. |
| 917 |
* Used in test suite. |
| 918 |
*/ |
| 919 |
void kill_all_vars() |
| 920 |
{ |
| 921 |
memset(&play.var, 0, sizeof(play.var)); |
| 922 |
} |
| 923 |
|
| 924 |
|
| 925 |
/** |
| 926 |
* Return the next single line from rinfo_code[script], starting at |
| 927 |
* rinfo[script]->current. Update line/column counters. |
| 928 |
*/ |
| 929 |
char* read_next_line(int script) |
| 930 |
{ |
| 931 |
if (rinfo[script] == NULL) |
| 932 |
{ |
| 933 |
log_error("Tried to read script %d, it doesn't exist.", script); |
| 934 |
return NULL; |
| 935 |
} |
| 936 |
|
| 937 |
if (rinfo[script]->current >= rinfo[script]->end) |
| 938 |
{ |
| 939 |
//at end of buffer |
| 940 |
return NULL; |
| 941 |
} |
| 942 |
|
| 943 |
/* remember the beginning of the line to be parsed, we'll use it in |
| 944 |
the debugging messages */ |
| 945 |
rinfo[script]->debug_line = rinfo[script]->cur_line; |
| 946 |
|
| 947 |
int k = rinfo[script]->current; |
| 948 |
int start = k; |
| 949 |
for (; k < rinfo[script]->end; k++) |
| 950 |
{ |
| 951 |
rinfo[script]->current++; |
| 952 |
rinfo[script]->cur_col++; |
| 953 |
|
| 954 |
if (rinfo_code[script][k] == '\n') |
| 955 |
{ |
| 956 |
rinfo[script]->cur_line++; |
| 957 |
rinfo[script]->cur_col = 0; |
| 958 |
} |
| 959 |
if (rinfo_code[script][k] == '\n' || rinfo_code[script][k] == '\r') |
| 960 |
break; |
| 961 |
} |
| 962 |
|
| 963 |
if (k < rinfo[script]->end) |
| 964 |
{ |
| 965 |
int len = rinfo[script]->current - start; |
| 966 |
char* buf = (char*)xmalloc(len + 1); |
| 967 |
|
| 968 |
char* pc = buf; |
| 969 |
int k = start; |
| 970 |
for (; k < rinfo[script]->current; k++, pc++) |
| 971 |
{ |
| 972 |
*pc = rinfo_code[script][k]; |
| 973 |
|
| 974 |
/* Compatibility substitutions, important when parsing |
| 975 |
title_start/title_end, namely */ |
| 976 |
if (*pc == '\t') *pc = ' '; |
| 977 |
if (*pc == '\r') *pc = '\n'; |
| 978 |
} |
| 979 |
*pc = '\0'; /* for safety */ |
| 980 |
return buf; |
| 981 |
} |
| 982 |
else |
| 983 |
{ |
| 984 |
//at end of buffer |
| 985 |
return NULL; |
| 986 |
} |
| 987 |
} |
| 988 |
|
| 989 |
void kill_scripts_with_inactive_sprites() { |
| 990 |
for (int i = 1; i < MAX_SCRIPTS; i++) { |
| 991 |
if (rinfo[i] != NULL) { |
| 992 |
// TODO: move out so we don't depend on 'spr' |
| 993 |
if (rinfo[i]->sprite > 0 && rinfo[i]->sprite != 1000 |
| 994 |
&& !spr[rinfo[i]->sprite].active) { |
| 995 |
//kill this script, owner is dead |
| 996 |
log_debug("Killing script %s, owner sprite %d is dead.", |
| 997 |
rinfo[i]->name, rinfo[i]->sprite); |
| 998 |
kill_script(i); |
| 999 |
} |
| 1000 |
} |
| 1001 |
} |
| 1002 |
} |
| 1003 |
|
| 1004 |
/** |
| 1005 |
* Run callbacks, order by index. Sets the activation delay if |
| 1006 |
* necessary. Kill obsolete callbacks along the way. |
| 1007 |
* |
| 1008 |
* Callbacks are set by wait() and set_callback_random(). |
| 1009 |
* |
| 1010 |
* spawn()/external()/etc. use other mechanisms. say_stop*() also use |
| 1011 |
* callbacks, but implemented differently (spr[x].callback, processed |
| 1012 |
* in updateFrame()). |
| 1013 |
**/ |
| 1014 |
void process_callbacks(Uint32 now) |
| 1015 |
{ |
| 1016 |
for (int k = 1; k < MAX_CALLBACKS; k++) |
| 1017 |
{ |
| 1018 |
if (callback[k].active) |
| 1019 |
{ |
| 1020 |
if (callback[k].owner > 0 && rinfo[callback[k].owner] == NULL) |
| 1021 |
{ |
| 1022 |
//kill this process, it's owner sprite is 'effin dead. |
| 1023 |
log_debug("Killed callback %d because script %d is dead.", |
| 1024 |
k, callback[k].owner); |
| 1025 |
callback[k].active = /*false*/0; |
| 1026 |
} |
| 1027 |
else |
| 1028 |
{ |
| 1029 |
if (callback[k].timer == 0) |
| 1030 |
{ |
| 1031 |
//set timer |
| 1032 |
|
| 1033 |
if (callback[k].max > 0) |
| 1034 |
callback[k].timer = now + (rand() % callback[k].max) + callback[k].min; |
| 1035 |
else |
| 1036 |
callback[k].timer = now + callback[k].min; |
| 1037 |
} |
| 1038 |
else |
| 1039 |
{ |
| 1040 |
if (callback[k].timer < now) |
| 1041 |
{ |
| 1042 |
callback[k].timer = 0; |
| 1043 |
|
| 1044 |
if (compare(callback[k].name, "")) |
| 1045 |
{ |
| 1046 |
//callback defined no proc name, so lets assume they want to start the script where it |
| 1047 |
//left off |
| 1048 |
//kill this callback |
| 1049 |
callback[k].active = /*false*/0; |
| 1050 |
run_script(callback[k].owner); |
| 1051 |
log_debug("Called script %d from callback %d.", |
| 1052 |
callback[k].owner, k); |
| 1053 |
} |
| 1054 |
else |
| 1055 |
{ |
| 1056 |
log_debug("Called proc %s from callback %d.", callback[k].name, k); |
| 1057 |
|
| 1058 |
//callback defined a proc name |
| 1059 |
if (locate(callback[k].owner,callback[k].name)) |
| 1060 |
{ |
| 1061 |
//found proc, lets run it |
| 1062 |
run_script(callback[k].owner); |
| 1063 |
} |
| 1064 |
} |
| 1065 |
} |
| 1066 |
} |
| 1067 |
} |
| 1068 |
} |
| 1069 |
} |
| 1070 |
} |
| 1071 |
|
| 1072 |
|
| 1073 |
/** |
| 1074 |
* Kill all scripts attached to given sprite |
| 1075 |
*/ |
| 1076 |
void kill_scripts_owned_by(int sprite) |
| 1077 |
{ |
| 1078 |
int i; |
| 1079 |
for (i = 1; i < MAX_SCRIPTS; i++) |
| 1080 |
if (rinfo[i] != NULL |
| 1081 |
&& rinfo[i]->sprite == sprite) |
| 1082 |
kill_script(i); |
| 1083 |
} |
| 1084 |
|
| 1085 |
void kill_returning_stuff(int script) |
| 1086 |
{ |
| 1087 |
//Msg("Checking callbacks.."); |
| 1088 |
//check callbacks |
| 1089 |
|
| 1090 |
int i; |
| 1091 |
// callbacks from wait() and run_script_by_number() |
| 1092 |
for (i = 1; i < MAX_CALLBACKS; i++) |
| 1093 |
{ |
| 1094 |
if (callback[i].active && callback[i].owner == script) |
| 1095 |
// if (compare(callback[i].name, "")) |
| 1096 |
{ |
| 1097 |
log_debug("killed a returning callback, ha!"); |
| 1098 |
callback[i].active = /*false*/0; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
|
| 1102 |
// callbacks from say*() |
| 1103 |
for (i = 1; i <= last_sprite_created; i++) |
| 1104 |
{ |
| 1105 |
// TODO: move out so we don't depend on 'spr' |
| 1106 |
if (spr[i].active && spr[i].brain == 8 && spr[i].say_stop_callback == script) |
| 1107 |
{ |
| 1108 |
log_debug("Killed sprites callback command"); |
| 1109 |
spr[i].say_stop_callback = 0; |
| 1110 |
} |
| 1111 |
} |
| 1112 |
} |
| 1113 |
|
| 1114 |
|
| 1115 |
void run_script(int script) |
| 1116 |
{ |
| 1117 |
char* line = NULL; |
| 1118 |
|
| 1119 |
/* keep 'return' value? */ |
| 1120 |
if (dversion >= 108) |
| 1121 |
{ |
| 1122 |
if (bKeepReturnInt == 1) |
| 1123 |
{ |
| 1124 |
bKeepReturnInt = 0; |
| 1125 |
} |
| 1126 |
else |
| 1127 |
{ |
| 1128 |
returnint = 0; |
| 1129 |
} |
| 1130 |
} |
| 1131 |
else |
| 1132 |
{ |
| 1133 |
returnint = 0; |
| 1134 |
} |
| 1135 |
returnstring[0] = 0; |
| 1136 |
|
| 1137 |
|
| 1138 |
if (rinfo[script] != NULL) |
| 1139 |
{ |
| 1140 |
log_debug("Script %s is entered at %d:%d (offset %ld).", |
| 1141 |
rinfo[script]->name, |
| 1142 |
rinfo[script]->cur_line, rinfo[script]->cur_col, |
| 1143 |
rinfo[script]->current); |
| 1144 |
} |
| 1145 |
else |
| 1146 |
{ |
| 1147 |
log_error("Tried to run a script that doesn't exist in memory. Nice work."); |
| 1148 |
} |
| 1149 |
|
| 1150 |
int doelse_once = 0; |
| 1151 |
while ((line = read_next_line(script)) != NULL) |
| 1152 |
{ |
| 1153 |
while (1) |
| 1154 |
{ |
| 1155 |
strip_beginning_spaces(line); |
| 1156 |
if (strcmp(line, "\n") == 0) |
| 1157 |
break; |
| 1158 |
|
| 1159 |
|
| 1160 |
int doelse = 0; |
| 1161 |
if (doelse_once == 1) |
| 1162 |
{ |
| 1163 |
doelse = 1; |
| 1164 |
doelse_once = 0; |
| 1165 |
} |
| 1166 |
int result = process_line(script, line, doelse); |
| 1167 |
|
| 1168 |
|
| 1169 |
if (result == DCPS_DOELSE_ONCE) |
| 1170 |
{ |
| 1171 |
doelse_once = 1; |
| 1172 |
/* now process the rest of the line */ |
| 1173 |
} |
| 1174 |
|
| 1175 |
if (result == DCPS_YIELD) |
| 1176 |
{ |
| 1177 |
/* Quit script: */ |
| 1178 |
log_debug("giving script the boot"); |
| 1179 |
free(line); |
| 1180 |
return; |
| 1181 |
} |
| 1182 |
|
| 1183 |
if (result == DCPS_GOTO_NEXTLINE) |
| 1184 |
break; |
| 1185 |
|
| 1186 |
/* else result == DCPS_CONTINUE */ |
| 1187 |
} |
| 1188 |
free(line); |
| 1189 |
} |
| 1190 |
|
| 1191 |
if (rinfo[script] != NULL && rinfo[script]->proc_return != 0) |
| 1192 |
{ |
| 1193 |
run_script(rinfo[script]->proc_return); |
| 1194 |
kill_script(script); |
| 1195 |
} |
| 1196 |
} |
| 1197 |
|
| 1198 |
/** |
| 1199 |
* Make new global functions (v1.08) |
| 1200 |
*/ |
| 1201 |
void make_function(char file[10], char func[20]) |
| 1202 |
{ |
| 1203 |
//See if it already exists |
| 1204 |
|
| 1205 |
int exists = 0; |
| 1206 |
int i; |
| 1207 |
for (i = 0; strlen (play.func[i].func) > 0 && i < 100; i++) |
| 1208 |
{ |
| 1209 |
if (compare (func, play.func[i].func)) |
| 1210 |
{ |
| 1211 |
exists = 1; |
| 1212 |
break; |
| 1213 |
} |
| 1214 |
} |
| 1215 |
if (exists == 1) |
| 1216 |
{ |
| 1217 |
strncpy (play.func[i].file, file, 10); |
| 1218 |
} |
| 1219 |
else |
| 1220 |
{ |
| 1221 |
strncpy (play.func[0].file, file, 10); |
| 1222 |
strncpy (play.func[0].func, func, 20); |
| 1223 |
} |
| 1224 |
} |
| 1225 |
|
| 1226 |
|
| 1227 |
/** |
| 1228 |
* Declare a new variable |
| 1229 |
* - script: used for debug messages |
| 1230 |
*/ |
| 1231 |
void make_int(char* name, int value, int scope, int script) |
| 1232 |
{ |
| 1233 |
int dupe; |
| 1234 |
if (strlen(name) > 19) |
| 1235 |
{ |
| 1236 |
log_error("[DinkC] %s:%d: varname %s is too long", |
| 1237 |
rinfo[script]->name, rinfo[script]->debug_line, name); |
| 1238 |
return; |
| 1239 |
} |
| 1240 |
dupe = lookup_var(name, scope); |
| 1241 |
|
| 1242 |
if (dupe > 0) |
| 1243 |
{ |
| 1244 |
if (scope != DINKC_GLOBAL_SCOPE) |
| 1245 |
{ |
| 1246 |
log_warn("[DinkC] %s:%d: Local var %s already used in this procedure", |
| 1247 |
rinfo[script]->name, rinfo[script]->debug_line, |
| 1248 |
name); |
| 1249 |
|
| 1250 |
play.var[dupe].var = value; |
| 1251 |
} |
| 1252 |
else |
| 1253 |
{ |
| 1254 |
log_warn("[DinkC] %s:%d: var %s is already a global, not changing value", |
| 1255 |
rinfo[script]->name, rinfo[script]->debug_line, name); |
| 1256 |
} |
| 1257 |
return; |
| 1258 |
} |
| 1259 |
|
| 1260 |
|
| 1261 |
//make new var |
| 1262 |
int i; |
| 1263 |
for (i = 1; i < MAX_VARS; i++) |
| 1264 |
{ |
| 1265 |
if (play.var[i].active == /*false*/0) |
| 1266 |
{ |
| 1267 |
play.var[i].active = /*true*/1; |
| 1268 |
play.var[i].scope = scope; |
| 1269 |
strcpy(play.var[i].name, name); |
| 1270 |
//g("var %s created, used slot %d ", name,i); |
| 1271 |
play.var[i].var = value; |
| 1272 |
return; |
| 1273 |
} |
| 1274 |
} |
| 1275 |
|
| 1276 |
log_error("[DinkC] %s:%d: out of var space, all %d used", |
| 1277 |
rinfo[script]->name, rinfo[script]->debug_line, MAX_VARS); |
| 1278 |
} |
| 1279 |
|
| 1280 |
/** |
| 1281 |
* (re)Define variable |
| 1282 |
* |
| 1283 |
* name: variable name |
| 1284 |
* newname: new value (unless that's a function call, cf. 'rest') |
| 1285 |
* math: operator (one of '=', '+', '-', '*', '/') |
| 1286 |
* script: in-memory script identifier |
| 1287 |
* rest: text of the script after the operator (left-trimmed) |
| 1288 |
*/ |
| 1289 |
void var_equals(char* name, char* newname, char math, int script, char rest[200]) |
| 1290 |
{ |
| 1291 |
int newval = 0; |
| 1292 |
struct varman *lhs_var = NULL; |
| 1293 |
|
| 1294 |
/** Ensure left-hand side is an existing variable **/ |
| 1295 |
if (name[0] != '&') |
| 1296 |
{ |
| 1297 |
log_error("[DinkC] %s:%d:[var_equals]: unknown var %s", |
| 1298 |
rinfo[script]->name, rinfo[script]->debug_line, name); |
| 1299 |
return; |
| 1300 |
} |
| 1301 |
/* Find the variable slot */ |
| 1302 |
{ |
| 1303 |
int k = lookup_var_local_global(name, script); |
| 1304 |
if (k != -1) |
| 1305 |
lhs_var = &(play.var[k]); |
| 1306 |
|
| 1307 |
if (lhs_var == NULL) /* not found */ |
| 1308 |
{ |
| 1309 |
log_error("[DinkC] %s:%d:[var_equals]: unknown var %s", |
| 1310 |
rinfo[script]->name, rinfo[script]->debug_line, name); |
| 1311 |
return; |
| 1312 |
} |
| 1313 |
} |
| 1314 |
|
| 1315 |
/** Analyse right-hand side **/ |
| 1316 |
/* check if right-hand side is a function */ |
| 1317 |
if (strchr(rest, '(') != NULL) |
| 1318 |
{ |
| 1319 |
process_line(script, rest, /*false*/0); |
| 1320 |
newval = returnint; |
| 1321 |
goto next2; |
| 1322 |
} |
| 1323 |
|
| 1324 |
/* check if right-hand side is a variable to copy */ |
| 1325 |
/* remove trailing ';' */ |
| 1326 |
if (strchr(newname, ';') != NULL) |
| 1327 |
replace_norealloc(";", "", newname); |
| 1328 |
/* look for existing variable */ |
| 1329 |
{ |
| 1330 |
int k2 = lookup_var_local_global(newname, script); |
| 1331 |
if (k2 != -1) |
| 1332 |
{ |
| 1333 |
newval = play.var[k2].var; |
| 1334 |
//found var |
| 1335 |
goto next2; |
| 1336 |
} |
| 1337 |
} |
| 1338 |
/* also check special variables */ |
| 1339 |
if (compare(newname, "¤t_sprite")) |
| 1340 |
{ |
| 1341 |
newval = rinfo[script]->sprite; |
| 1342 |
goto next2; |
| 1343 |
} |
| 1344 |
if (compare(newname, "¤t_script")) |
| 1345 |
{ |
| 1346 |
newval = script; |
| 1347 |
goto next2; |
| 1348 |
} |
| 1349 |
if (dversion >= 108) |
| 1350 |
{ |
| 1351 |
//v1.08 special variables. |
| 1352 |
if (compare (newname, "&return")) |
| 1353 |
{ |
| 1354 |
newval = returnint; |
| 1355 |
goto next2; |
| 1356 |
} |
| 1357 |
if (compare (newname, "&arg1")) |
| 1358 |
{ |
| 1359 |
newval = rinfo[script]->arg1; |
| 1360 |
goto next2; |
| 1361 |
} |
| 1362 |
if (compare (newname, "&arg2")) |
| 1363 |
{ |
| 1364 |
newval = rinfo[script]->arg2; |
| 1365 |
goto next2; |
| 1366 |
} |
| 1367 |
if (compare (newname, "&arg3")) |
| 1368 |
{ |
| 1369 |
newval = rinfo[script]->arg3; |
| 1370 |
goto next2; |
| 1371 |
} |
| 1372 |
if (compare (newname, "&arg4")) |
| 1373 |
{ |
| 1374 |
newval = rinfo[script]->arg4; |
| 1375 |
goto next2; |
| 1376 |
} |
| 1377 |
if (compare (newname, "&arg5")) |
| 1378 |
{ |
| 1379 |
newval = rinfo[script]->arg5; |
| 1380 |
goto next2; |
| 1381 |
} |
| 1382 |
if (compare (newname, "&arg6")) |
| 1383 |
{ |
| 1384 |
newval = rinfo[script]->arg6; |
| 1385 |
goto next2; |
| 1386 |
} |
| 1387 |
if (compare (newname, "&arg7")) |
| 1388 |
{ |
| 1389 |
newval = rinfo[script]->arg7; |
| 1390 |
goto next2; |
| 1391 |
} |
| 1392 |
if (compare (newname, "&arg8")) |
| 1393 |
{ |
| 1394 |
newval = rinfo[script]->arg8; |
| 1395 |
goto next2; |
| 1396 |
} |
| 1397 |
if (compare (newname, "&arg9")) |
| 1398 |
{ |
| 1399 |
newval = rinfo[script]->arg9; |
| 1400 |
goto next2; |
| 1401 |
} |
| 1402 |
} |
| 1403 |
/* otherwise, assume right-hand side is an integer */ |
| 1404 |
newval = atol(newname); |
| 1405 |
|
| 1406 |
|
| 1407 |
next2: |
| 1408 |
/* Apply the right operation */ |
| 1409 |
if (math == '=') |
| 1410 |
lhs_var->var = newval; |
| 1411 |
if (math == '+') |
| 1412 |
lhs_var->var += newval; |
| 1413 |
if (math == '-') |
| 1414 |
lhs_var->var -= newval; |
| 1415 |
if (math == '/') |
| 1416 |
// TODO: do something if newval == 0 (crashes in Dink v1.08) |
| 1417 |
lhs_var->var = lhs_var->var / newval; |
| 1418 |
if (math == '*') |
| 1419 |
lhs_var->var = lhs_var->var * newval; |
| 1420 |
} |
| 1421 |
|
| 1422 |
/** |
| 1423 |
* Evaluate a value (variable, int, or maths), in the context of |
| 1424 |
* 'script'. |
| 1425 |
*/ |
| 1426 |
int var_figure(char* h, int script) |
| 1427 |
{ |
| 1428 |
char* word = NULL; |
| 1429 |
int ret = 0; |
| 1430 |
int n1 = 0, n2 = 0; |
| 1431 |
|
| 1432 |
int is_one_word_equation = 0; |
| 1433 |
word = get_word(h, 2); |
| 1434 |
if (compare(word, "")) |
| 1435 |
is_one_word_equation = 1; |
| 1436 |
free(word); |
| 1437 |
if (is_one_word_equation) |
| 1438 |
{ |
| 1439 |
// variable -> integer |
| 1440 |
if (h[0] == '&') |
| 1441 |
decipher_string(&h, script); |
| 1442 |
|
| 1443 |
// integer |
| 1444 |
ret = atol(h); |
| 1445 |
return ret; |
| 1446 |
} |
| 1447 |
|
| 1448 |
word = get_word(h, 1); |
| 1449 |
decipher_string(&word, script); |
| 1450 |
n1 = atol(word); |
| 1451 |
free(word); |
| 1452 |
|
| 1453 |
word = get_word(h, 3); |
| 1454 |
replace_norealloc(")", "", word); |
| 1455 |
decipher_string(&word, script); |
| 1456 |
n2 = atol(word); |
| 1457 |
free(word); |
| 1458 |
|
| 1459 |
word = get_word(h, 2); |
| 1460 |
log_debug("Compared %d to %d", n1, n2); |
| 1461 |
|
| 1462 |
if (compare(word, "==")) |
| 1463 |
{ |
| 1464 |
if (n1 == n2) ret = 1; else ret = 0; |
| 1465 |
free(word); |
| 1466 |
return ret; |
| 1467 |
} |
| 1468 |
|
| 1469 |
if (compare(word, ">")) |
| 1470 |
{ |
| 1471 |
if (n1 > n2) ret = 1; else ret = 0; |
| 1472 |
free(word); |
| 1473 |
return ret; |
| 1474 |
} |
| 1475 |
|
| 1476 |
if (compare(word, ">=")) |
| 1477 |
{ |
| 1478 |
if (n1 >= n2) ret = 1; else ret = 0; |
| 1479 |
free(word); |
| 1480 |
return ret; |
| 1481 |
} |
| 1482 |
|
| 1483 |
|
| 1484 |
if (compare(word, "<")) |
| 1485 |
{ |
| 1486 |
if (n1 < n2) ret = 1; else ret = 0; |
| 1487 |
free(word); |
| 1488 |
return ret; |
| 1489 |
} |
| 1490 |
|
| 1491 |
if (compare(word, "<=")) |
| 1492 |
{ |
| 1493 |
if (n1 <= n2) ret = 1; else ret = 0; |
| 1494 |
free(word); |
| 1495 |
return ret; |
| 1496 |
} |
| 1497 |
|
| 1498 |
if (compare(word, "!=")) |
| 1499 |
{ |
| 1500 |
if (n1 != n2) ret = 1; else ret = 0; |
| 1501 |
free(word); |
| 1502 |
return ret; |
| 1503 |
} |
| 1504 |
|
| 1505 |
free(word); |
| 1506 |
return ret; |
| 1507 |
} |
| 1508 |
|
| 1509 |
/** |
| 1510 |
* Check if 'line' is a valid variable declaration, and define the |
| 1511 |
* variable it to 0 (via make_int(...)). 'line' is modified. |
| 1512 |
*/ |
| 1513 |
void int_prepare(char* line, int script) |
| 1514 |
{ |
| 1515 |
char* hold = strdup(line); |
| 1516 |
|
| 1517 |
char* name = NULL; |
| 1518 |
char *temp = NULL; |
| 1519 |
replace_norealloc("=", " ", line); |
| 1520 |
temp = separate_string(line, 1, ';'); |
| 1521 |
strcpy(line, temp); // safe as strlen(line) <= strlen(temp) |
| 1522 |
free(temp); |
| 1523 |
name = get_word(line, 2); |
| 1524 |
|
| 1525 |
if (name[0] != '&') |
| 1526 |
{ |
| 1527 |
log_error("[DinkC] %s:%d: can't create var %s, should be &%s.", |
| 1528 |
rinfo[script]->name, rinfo[script]->debug_line, |
| 1529 |
name, name); |
| 1530 |
} |
| 1531 |
else |
| 1532 |
{ |
| 1533 |
make_int(name, 0, script, script); |
| 1534 |
|
| 1535 |
strcpy(line, hold); |
| 1536 |
} |
| 1537 |
free(name); |
| 1538 |
free(hold); |
| 1539 |
} |
| 1540 |
|
| 1541 |
/** |
| 1542 |
* Process DinkC dialog choice stanza |
| 1543 |
* Globals: game_choice, game_choice_start, game_choice_clear, i18n_translate |
| 1544 |
*/ |
| 1545 |
/*bool*/int dinkc_get_choices(int script) |
| 1546 |
{ |
| 1547 |
char* line = NULL; |
| 1548 |
int cur = 1; // TODO: make it stay <= 20 |
| 1549 |
int retnum = 0; |
| 1550 |
game_choice_clear(); |
| 1551 |
game_choice.newy = -5000; |
| 1552 |
while(1) |
| 1553 |
{ |
| 1554 |
redo: |
| 1555 |
line = read_next_line(script); |
| 1556 |
if (line == NULL) |
| 1557 |
line = strdup(""); // compatibility |
| 1558 |
|
| 1559 |
strip_beginning_spaces(line); |
| 1560 |
//Msg("Comparing to %s.", line); |
| 1561 |
|
| 1562 |
char* word = get_word(line, 1); |
| 1563 |
if (compare(word, "set_y")) |
| 1564 |
{ |
| 1565 |
free(word); |
| 1566 |
word = get_word(line, 2); |
| 1567 |
game_choice.newy = atol(word); |
| 1568 |
free(word); |
| 1569 |
free(line); |
| 1570 |
goto redo; |
| 1571 |
} |
| 1572 |
|
| 1573 |
if (compare(word, "set_title_color")) |
| 1574 |
{ |
| 1575 |
free(word); |
| 1576 |
word = get_word(line, 2); |
| 1577 |
game_choice.color = atol(word); |
| 1578 |
free(word); |
| 1579 |
free(line); |
| 1580 |
goto redo; |
| 1581 |
} |
| 1582 |
free(word); |
| 1583 |
|
| 1584 |
strip_beginning_spaces(line); |
| 1585 |
if (compare(line, "\n")) |
| 1586 |
{ |
| 1587 |
free(line); |
| 1588 |
goto redo; |
| 1589 |
} |
| 1590 |
|
| 1591 |
char* directive = NULL; |
| 1592 |
morestuff: |
| 1593 |
directive = separate_string(line, 1, '('); |
| 1594 |
strip_beginning_spaces(directive); |
| 1595 |
|
| 1596 |
if (compare(directive, "title_start")) |
| 1597 |
{ |
| 1598 |
free(line); |
| 1599 |
while((line = read_next_line(script)) != NULL) |
| 1600 |
{ |
| 1601 |
strip_beginning_spaces(line); |
| 1602 |
free(directive); |
| 1603 |
|
| 1604 |
directive = separate_string(line, 1, '('); |
| 1605 |
if (directive != NULL) |
| 1606 |
{ |
| 1607 |
strip_beginning_spaces(directive); |
| 1608 |
|
| 1609 |
if (compare(directive, "title_end")) |
| 1610 |
{ |
| 1611 |
replace_norealloc("\n\n\n\n", "\n \n", game_choice.buffer); |
| 1612 |
replace_norealloc("\n\n", "\n", game_choice.buffer); |
| 1613 |
free(directive); |
| 1614 |
free(line); |
| 1615 |
goto redo; |
| 1616 |
} |
| 1617 |
} |
| 1618 |
|
| 1619 |
/* drop '\n', this messes translations */ |
| 1620 |
line[strlen(line)-1] = '\0'; |
| 1621 |
/* Translate text (before variable substitution) */ |
| 1622 |
char* translation = i18n_translate(rinfo[script]->name, rinfo[script]->debug_line, line); |
| 1623 |
decipher_string(&translation, script); |
| 1624 |
int cur_len = strlen(game_choice.buffer); |
| 1625 |
strncat(game_choice.buffer, translation, TALK_TITLE_BUFSIZ - 1 - cur_len - 1); |
| 1626 |
free(translation); |
| 1627 |
/* put '\n' back */ |
| 1628 |
strcat(game_choice.buffer, "\n"); |
| 1629 |
game_choice.buffer[TALK_TITLE_BUFSIZ-1] = '\0'; |
| 1630 |
free(line); |
| 1631 |
} |
| 1632 |
|
| 1633 |
free(directive); |
| 1634 |
goto redo; |
| 1635 |
} |
| 1636 |
|
| 1637 |
if (compare(directive, "choice_end")) |
| 1638 |
{ |
| 1639 |
if (cur-1 == 0) |
| 1640 |
{ |
| 1641 |
log_debug("Error: choice() has 0 options in script %s, offset %ld.", |
| 1642 |
rinfo[script]->name, rinfo[script]->current); |
| 1643 |
|
| 1644 |
free(directive); |
| 1645 |
free(line); |
| 1646 |
return /*false*/0; |
| 1647 |
} |
| 1648 |
game_choice_start(script, cur-1); |
| 1649 |
free(directive); |
| 1650 |
free(line); |
| 1651 |
return /*true*/1; |
| 1652 |
} |
| 1653 |
free(directive); |
| 1654 |
|
| 1655 |
char* condition = separate_string(line, 1, '"'); |
| 1656 |
strip_beginning_spaces(condition); |
| 1657 |
|
| 1658 |
if (strlen(condition) > 2) |
| 1659 |
{ |
| 1660 |
//found conditional statement |
| 1661 |
if (strchr(condition, '(') == NULL) |
| 1662 |
{ |
| 1663 |
log_error("[DinkC] Error with choice() statement in script %s," |
| 1664 |
" offset %ld. (%s?)", |
| 1665 |
rinfo[script]->name, rinfo[script]->current, condition); |
| 1666 |
|
| 1667 |
free(condition); |
| 1668 |
free(line); |
| 1669 |
return /*false*/0; |
| 1670 |
} |
| 1671 |
|
| 1672 |
char* temp = separate_string(condition, 2, '('); |
| 1673 |
free(condition); |
| 1674 |
condition = separate_string(temp, 1, ')'); |
| 1675 |
free(temp); |
| 1676 |
|
| 1677 |
//Msg("Running %s through var figure..", check); |
| 1678 |
if (var_figure(condition, script) == 0) |
| 1679 |
{ |
| 1680 |
log_debug("Answer is no."); |
| 1681 |
retnum++; |
| 1682 |
|
| 1683 |
free(condition); |
| 1684 |
free(line); |
| 1685 |
goto redo; |
| 1686 |
//said NO to statement |
| 1687 |
} |
| 1688 |
//Msg("Answer is yes."); |
| 1689 |
free(condition); |
| 1690 |
|
| 1691 |
/* Resume processing stripping the first condition (there |
| 1692 |
may be several conditions on a single dialog ligne, which |
| 1693 |
are AND'ed) */ |
| 1694 |
char* p = strchr(line, ')') + 1; |
| 1695 |
int i = 0; |
| 1696 |
for (; *p != '\0'; i++, p++) |
| 1697 |
line[i] = *p; |
| 1698 |
line[i] = '\0'; |
| 1699 |
goto morestuff; |
| 1700 |
} |
| 1701 |
free(condition); |
| 1702 |
|
| 1703 |
retnum++; |
| 1704 |
char* text = separate_string(line, 2, '"'); |
| 1705 |
if (strlen(text) > 0) |
| 1706 |
{ |
| 1707 |
/* Translate text (before variable substitution) */ |
| 1708 |
char* translation = i18n_translate(rinfo[script]->name, rinfo[script]->debug_line, text); |
| 1709 |
strip_beginning_spaces(translation); |
| 1710 |
|
| 1711 |
decipher_savegame = retnum; |
| 1712 |
decipher_string(&translation, script); |
| 1713 |
decipher_savegame = 0; |
| 1714 |
strncpy(game_choice.line[cur], translation, TALK_LINE_BUFSIZ-1); |
| 1715 |
game_choice.line[cur][TALK_LINE_BUFSIZ-1] = '\0'; |
| 1716 |
free(translation); |
| 1717 |
} |
| 1718 |
else |
| 1719 |
{ |
| 1720 |
/* Handle empty text separately because _("") has a special |
| 1721 |
meaning (returns .mo meta-data). */ |
| 1722 |
strcpy(game_choice.line[cur], ""); |
| 1723 |
} |
| 1724 |
free(text); |
| 1725 |
game_choice.line_return[cur] = retnum; |
| 1726 |
cur++; |
| 1727 |
free(line); |
| 1728 |
} |
| 1729 |
} |
| 1730 |
|
| 1731 |
|
| 1732 |
/** |
| 1733 |
* Utility function for 'process_line', to separate and store the current procedure arguments. |
| 1734 |
* |
| 1735 |
* proc_name: named of the called function |
| 1736 |
* script: script id |
| 1737 |
* str_params: string to parse (what was after the function name) |
| 1738 |
* spec: describe the function's parameters: |
| 1739 |
* 1=int |
| 1740 |
* 2=string |
| 1741 |
* 0=no more args (10 args max) |
| 1742 |
* |
| 1743 |
* Known compatibility issue: passing no argument to a function |
| 1744 |
* expecting 1 int argument is considered valid.. |
| 1745 |
* |
| 1746 |
* Return: 0 if parse error, 1 if success |
| 1747 |
*/ |
| 1748 |
int get_parms(char proc_name[20], int script, char *str_params, int* spec) |
| 1749 |
{ |
| 1750 |
/* Clean-up parameters */ |
| 1751 |
memset(nlist, 0, 10 * sizeof (int)); |
| 1752 |
{ |
| 1753 |
int i = 0; |
| 1754 |
for (; i < 10; i++) |
| 1755 |
slist[i][0] = '\0'; |
| 1756 |
} |
| 1757 |
|
| 1758 |
/* Safety */ |
| 1759 |
char* limit = str_params + strlen(str_params); |
| 1760 |
|
| 1761 |
strip_beginning_spaces(str_params); |
| 1762 |
if (str_params[0] == '(') |
| 1763 |
{ |
| 1764 |
//Msg("Found first (."); |
| 1765 |
str_params++; |
| 1766 |
} |
| 1767 |
else |
| 1768 |
{ |
| 1769 |
log_error("[DinkC] Missing '(' in %s, offset %ld.", |
| 1770 |
rinfo[script]->name, rinfo[script]->current); |
| 1771 |
return 0; |
| 1772 |
} |
| 1773 |
|
| 1774 |
int i = 0; |
| 1775 |
for (; i < 10; i++) |
| 1776 |
{ |
| 1777 |
strip_beginning_spaces(str_params); |
| 1778 |
|
| 1779 |
if (spec[i] == 1) // type=int |
| 1780 |
{ |
| 1781 |
// Get next parameter (until ',' or ')' is reached) |
| 1782 |
char* parm = NULL; |
| 1783 |
if (strchr(str_params, ',') != NULL) |
| 1784 |
parm = separate_string(str_params, 1, ','); |
| 1785 |
else if (strchr(str_params, ')') != NULL) |
| 1786 |
parm = separate_string(str_params, 1, ')'); |
| 1787 |
else |
| 1788 |
parm = strdup(""); |
| 1789 |
|
| 1790 |
// move to next param |
| 1791 |
str_params += strlen(parm); |
| 1792 |
|
| 1793 |
int intval = -1; |
| 1794 |
if (parm[0] == '&') |
| 1795 |
{ |
| 1796 |
replace_norealloc(" ", "", parm); |
| 1797 |
intval = decipher(parm, script); |
| 1798 |
} |
| 1799 |
else |
| 1800 |
{ |
| 1801 |
intval = atol(parm); |
| 1802 |
} |
| 1803 |
// store parameter of type 'int' |
| 1804 |
nlist[i] = intval; |
| 1805 |
free(parm); |
| 1806 |
} |
| 1807 |
else if (spec[i] == 2) // type=string |
| 1808 |
{ |
| 1809 |
// Checking for string |
| 1810 |
char* parm = NULL; |
| 1811 |
parm = separate_string(str_params, 2, '"'); |
| 1812 |
|
| 1813 |
// replace DinkC string parameter |
| 1814 |
free(slist[i]); |
| 1815 |
slist[i] = parm; |
| 1816 |
|
| 1817 |
// move to next param |
| 1818 |
str_params += strlen(parm) + 2; // 2x" |
| 1819 |
if (str_params > limit) str_params = limit; |
| 1820 |
} |
| 1821 |
|
| 1822 |
if ((i+1) == 10 || spec[i+1] == 0) // this was the last arg |
| 1823 |
{ |
| 1824 |
//finish |
| 1825 |
strip_beginning_spaces(str_params); |
| 1826 |
|
| 1827 |
if (str_params[0] == ')') |
| 1828 |
{ |
| 1829 |
str_params++; |
| 1830 |
} |
| 1831 |
else |
| 1832 |
{ |
| 1833 |
return 0; |
| 1834 |
} |
| 1835 |
strip_beginning_spaces(str_params); |
| 1836 |
return 1; |
| 1837 |
} |
| 1838 |
|
| 1839 |
//got a parm, but there is more to get, lets make sure there is a comma there |
| 1840 |
strip_beginning_spaces(str_params); |
| 1841 |
|
| 1842 |
if (str_params[0] == ',') |
| 1843 |
{ |
| 1844 |
str_params++; |
| 1845 |
} |
| 1846 |
else |
| 1847 |
{ |
| 1848 |
return 0; |
| 1849 |
} |
| 1850 |
} |
| 1851 |
return 1; |
| 1852 |
} |
| 1853 |
|
| 1854 |
/** |
| 1855 |
* Are these 2 function signatures identical? |
| 1856 |
*/ |
| 1857 |
static int signatures_eq_p(int* params1, int* params2) |
| 1858 |
{ |
| 1859 |
int i = 0; |
| 1860 |
for (; i < 10; i++) |
| 1861 |
if (params1[i] != params2[i]) |
| 1862 |
return 0; |
| 1863 |
return 1; |
| 1864 |
} |
| 1865 |
|
| 1866 |
/** |
| 1867 |
* Process one line of DinkC and returns directive to the DinkC |
| 1868 |
* interpreter. |
| 1869 |
* |
| 1870 |
* Cf. doc/HACKING_dinkc.txt for understanding in progress ;) |
| 1871 |
* Globals: kill_text_owned_by |
| 1872 |
**/ |
| 1873 |
static enum dinkc_parser_state |
| 1874 |
process_line(int script, char *s, /*bool*/int doelse) |
| 1875 |
{ |
| 1876 |
char *h, *p; |
| 1877 |
char* ev[3]; |
| 1878 |
|
| 1879 |
memset(&ev, 0, sizeof(ev)); |
| 1880 |
|
| 1881 |
if (rinfo[script]->level < 1) |
| 1882 |
rinfo[script]->level = 1; |
| 1883 |
|
| 1884 |
h = s; |
| 1885 |
if (h[0] == '\0') |
| 1886 |
return DCPS_GOTO_NEXTLINE; |
| 1887 |
|
| 1888 |
if ((h[0] == '/') && (h[1] == '/')) |
| 1889 |
{ |
| 1890 |
//Msg("It was a comment!"); |
| 1891 |
goto bad; |
| 1892 |
} |
| 1893 |
|
| 1894 |
/* Cut line */ |
| 1895 |
ev[0] = separate_string(h, 1, ' '); |
| 1896 |
ev[1] = separate_string(h, 2, ' '); |
| 1897 |
ev[2] = separate_string(h, 3, ' '); |
| 1898 |
/* Prepare free on return */ |
| 1899 |
#define PL_RETURN(intval) {free(ev[0]), free(ev[1]), free(ev[2]); return intval;} |
| 1900 |
|
| 1901 |
if (compare(ev[0], "VOID")) |
| 1902 |
{ |
| 1903 |
if (rinfo[script]->proc_return != 0) |
| 1904 |
{ |
| 1905 |
run_script(rinfo[script]->proc_return); |
| 1906 |
kill_script(script); |
| 1907 |
} |
| 1908 |
PL_RETURN(DCPS_YIELD); |
| 1909 |
} |
| 1910 |
|
| 1911 |
/* goto label? */ |
| 1912 |
if (ev[0][strlen(ev[0]) -1] == ':' && strlen(ev[1]) < 2) |
| 1913 |
{ |
| 1914 |
if (dversion >= 108) |
| 1915 |
{ |
| 1916 |
/* Attempt to avoid considering: |
| 1917 |
say("bonus: 5 points", 1); // would not display any text at all! |
| 1918 |
as a label */ |
| 1919 |
if (strncmp (ev[0], "say", 3) != 0) |
| 1920 |
{ |
| 1921 |
PL_RETURN(DCPS_GOTO_NEXTLINE); //its a label |
| 1922 |
} |
| 1923 |
} |
| 1924 |
else |
| 1925 |
{ |
| 1926 |
PL_RETURN(DCPS_GOTO_NEXTLINE); //its a label |
| 1927 |
} |
| 1928 |
} |
| 1929 |
|
| 1930 |
/** Expression between parenthesis **/ |
| 1931 |
if (ev[0][0] == '(') |
| 1932 |
{ |
| 1933 |
//this procedure has been passed a conditional statement finder |
| 1934 |
//what kind of conditional statement is it? |
| 1935 |
p = h; |
| 1936 |
char* temp = separate_string(h, 2, ')'); |
| 1937 |
free(ev[0]); |
| 1938 |
ev[0] = separate_string(h, 1, ')'); |
| 1939 |
|
| 1940 |
// Msg("Ok, turned h %s to ev1 %s.",h,ev[0]); |
| 1941 |
p += strlen(ev[0]) + 1; |
| 1942 |
|
| 1943 |
strip_beginning_spaces(p); |
| 1944 |
|
| 1945 |
if (strchr(temp, '=') != NULL) |
| 1946 |
{ |
| 1947 |
h++; |
| 1948 |
strip_beginning_spaces(h); |
| 1949 |
process_line(script, h, /*false*/0); |
| 1950 |
replace_norealloc("==", "", temp); |
| 1951 |
char* expr = (char*)xmalloc(20 + 4 + strlen(temp) + 1); |
| 1952 |
sprintf(expr, "%d == %s", returnint, temp); |
| 1953 |
returnint = var_figure(expr, script); |
| 1954 |
strcpy(h, "\n"); |
| 1955 |
free(expr); |
| 1956 |
free(temp); |
| 1957 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 1958 |
} |
| 1959 |
|
| 1960 |
if (strchr(temp, '>') != NULL) |
| 1961 |
{ |
| 1962 |
h++; |
| 1963 |
strip_beginning_spaces(h); |
| 1964 |
process_line(script, h, /*false*/0); |
| 1965 |
replace_norealloc("==", "", temp); |
| 1966 |
char* expr = (char*)xmalloc(20 + 3 + strlen(temp) + 1); |
| 1967 |
sprintf(expr, "%d > %s", returnint, temp); |
| 1968 |
returnint = var_figure(expr, script); |
| 1969 |
strcpy(h, "\n"); |
| 1970 |
free(expr); |
| 1971 |
free(temp); |
| 1972 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 1973 |
} |
| 1974 |
|
| 1975 |
if (strchr(temp, '<') != NULL) |
| 1976 |
{ |
| 1977 |
h++; |
| 1978 |
strip_beginning_spaces(h); |
| 1979 |
process_line(script, h, /*false*/0); |
| 1980 |
replace_norealloc("==", "", temp); |
| 1981 |
char* expr = (char*)xmalloc(20 + 3 + strlen(temp) + 1); |
| 1982 |
sprintf(expr, "%d < %s", returnint, temp); |
| 1983 |
returnint = var_figure(expr, script); |
| 1984 |
strcpy(h, "\n"); |
| 1985 |
free(expr); |
| 1986 |
free(temp); |
| 1987 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 1988 |
} |
| 1989 |
|
| 1990 |
/* Beuc: This should be converted to a set of "if ... else |
| 1991 |
* if... else if ..." and multi-character constants should be |
| 1992 |
* removed. However, this may cause the interpreter to behave |
| 1993 |
* differently, so be careful. */ |
| 1994 |
/* For now, I'll rewrite the code in an equivalent warning-free |
| 1995 |
* inelegant way: strchr(str, 'ab') <=> strchr(str, 'b') */ |
| 1996 |
/* if (strchr (temp, '<=') != NULL) */ |
| 1997 |
if (strchr(temp, '=') != NULL) |
| 1998 |
{ |
| 1999 |
h++; |
| 2000 |
strip_beginning_spaces(h); |
| 2001 |
process_line(script, h, /*false*/0); |
| 2002 |
replace_norealloc("==", "", temp); |
| 2003 |
char* expr = (char*)xmalloc(20 + 4 + strlen(temp) + 1); |
| 2004 |
sprintf(expr, "%d <= %s", returnint, temp); |
| 2005 |
returnint = var_figure(expr, script); |
| 2006 |
strcpy(h, "\n"); |
| 2007 |
free(expr); |
| 2008 |
free(temp); |
| 2009 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2010 |
} |
| 2011 |
/* if (strchr (temp, '>=') != NULL) */ |
| 2012 |
if (strchr (temp, '=') != NULL) |
| 2013 |
{ |
| 2014 |
h++; |
| 2015 |
strip_beginning_spaces(h); |
| 2016 |
process_line(script, h, /*false*/0); |
| 2017 |
replace_norealloc("==", "", temp); |
| 2018 |
char* expr = (char*)xmalloc(20 + 4 + strlen(temp) + 1); |
| 2019 |
sprintf(expr, "%d >= %s", returnint, temp); |
| 2020 |
returnint = var_figure(expr, script); |
| 2021 |
strcpy(h, "\n"); |
| 2022 |
free(expr); |
| 2023 |
free(temp); |
| 2024 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2025 |
} |
| 2026 |
/* if (strchr (temp, '!=') != NULL) */ |
| 2027 |
if (strchr (temp, '=') != NULL) |
| 2028 |
{ |
| 2029 |
h++; |
| 2030 |
strip_beginning_spaces(h); |
| 2031 |
process_line(script, h, /*false*/0); |
| 2032 |
replace_norealloc("==", "", temp); |
| 2033 |
char* expr = (char*)xmalloc(20 + 4 + strlen(temp) + 1); |
| 2034 |
sprintf(expr, "%d != %s", returnint, temp); |
| 2035 |
returnint = var_figure(expr, script); |
| 2036 |
strcpy(h, "\n"); |
| 2037 |
free(expr); |
| 2038 |
free(temp); |
| 2039 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2040 |
} |
| 2041 |
free(temp); |
| 2042 |
|
| 2043 |
|
| 2044 |
if (p[0] == ')') |
| 2045 |
{ |
| 2046 |
//its a procedure in the if statement!!! |
| 2047 |
h++; |
| 2048 |
p++; |
| 2049 |
char* line_copy = strdup(p); |
| 2050 |
process_line(script, h, /*false*/0); |
| 2051 |
log_debug("Returned %d for the returnint", returnint); |
| 2052 |
strcpy(s, line_copy); /* strlen(s) >= strlen(line_copy) */ |
| 2053 |
free(line_copy); |
| 2054 |
h = s; |
| 2055 |
|
| 2056 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2057 |
} |
| 2058 |
else |
| 2059 |
{ |
| 2060 |
h++; |
| 2061 |
|
| 2062 |
char* expr = separate_string(h, 1,')'); |
| 2063 |
h += strlen(expr) + 1; |
| 2064 |
returnint = var_figure(expr, script); |
| 2065 |
free(expr); |
| 2066 |
|
| 2067 |
strcpy_nooverlap(s, h); |
| 2068 |
|
| 2069 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2070 |
} |
| 2071 |
|
| 2072 |
strip_beginning_spaces(h); |
| 2073 |
strip_beginning_spaces(ev[0]); |
| 2074 |
|
| 2075 |
s = h; |
| 2076 |
} /* END expression between parenthesis */ |
| 2077 |
|
| 2078 |
|
| 2079 |
if (strchr(ev[0], '(') != NULL) |
| 2080 |
{ |
| 2081 |
//Msg("Has a (, lets change it"); |
| 2082 |
free(ev[0]); |
| 2083 |
ev[0] = separate_string(h, 1, '('); |
| 2084 |
//Msg("Ok, first is now %s",ev[0]); |
| 2085 |
} |
| 2086 |
|
| 2087 |
/** { Bloc } **/ |
| 2088 |
{ |
| 2089 |
char first = ev[0][0]; |
| 2090 |
if (first == '{') |
| 2091 |
{ |
| 2092 |
rinfo[script]->level++; |
| 2093 |
//Msg("Went up level, now at %d.", rinfo[script]->level); |
| 2094 |
h++; |
| 2095 |
if (rinfo[script]->skipnext) |
| 2096 |
{ |
| 2097 |
/* Skip the whole { section } */ |
| 2098 |
rinfo[script]->skipnext = /*false*/0; |
| 2099 |
rinfo[script]->onlevel = ( rinfo[script]->level - 1); |
| 2100 |
} |
| 2101 |
goto good; |
| 2102 |
} |
| 2103 |
|
| 2104 |
if (first == '}') |
| 2105 |
{ |
| 2106 |
rinfo[script]->level--; |
| 2107 |
//Msg("Went down a level, now at %d.", rinfo[script]->level); |
| 2108 |
h++; |
| 2109 |
|
| 2110 |
if (rinfo[script]->onlevel > 0 && rinfo[script]->level == rinfo[script]->onlevel) |
| 2111 |
{ |
| 2112 |
/* Finished skipping the { section }, preparing to run 'else' */ |
| 2113 |
strip_beginning_spaces(h); |
| 2114 |
strcpy_nooverlap(s, h); |
| 2115 |
PL_RETURN(DCPS_DOELSE_ONCE); |
| 2116 |
} |
| 2117 |
goto good; |
| 2118 |
} |
| 2119 |
} |
| 2120 |
|
| 2121 |
/* Fix if there are too many closing '}' */ |
| 2122 |
if (rinfo[script]->level < 0) |
| 2123 |
{ |
| 2124 |
rinfo[script]->level = 0; |
| 2125 |
} |
| 2126 |
|
| 2127 |
|
| 2128 |
/* Note: that's the 2nd time we compare with "VOID" - |
| 2129 |
cf. above. However ev[0] was modified in between, so this |
| 2130 |
section may still be called if the first comparison didn't |
| 2131 |
match. */ |
| 2132 |
if (compare(ev[0], "void")) |
| 2133 |
{ |
| 2134 |
// Msg("Next procedure starting, lets quit"); |
| 2135 |
strcpy_nooverlap(s, h); |
| 2136 |
if (rinfo[script]->proc_return != 0) |
| 2137 |
{ |
| 2138 |
run_script(rinfo[script]->proc_return); |
| 2139 |
kill_script(script); |
| 2140 |
} |
| 2141 |
|
| 2142 |
PL_RETURN(DCPS_YIELD); |
| 2143 |
} |
| 2144 |
|
| 2145 |
|
| 2146 |
/* Stop processing if we're skipping the current { section } */ |
| 2147 |
if (rinfo[script]->onlevel > 0 && rinfo[script]->level > rinfo[script]->onlevel) |
| 2148 |
{ |
| 2149 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2150 |
} |
| 2151 |
|
| 2152 |
rinfo[script]->onlevel = 0; |
| 2153 |
|
| 2154 |
/* Skip the current line if the previous 'if' or 'else' said so */ |
| 2155 |
if (rinfo[script]->skipnext) |
| 2156 |
{ |
| 2157 |
//sorry, can't do it, you were told to skip the next thing |
| 2158 |
rinfo[script]->skipnext = /*false*/0; |
| 2159 |
strcpy(s, "\n"); /* jump to next line */ |
| 2160 |
//PL_RETURN(3); |
| 2161 |
PL_RETURN(DCPS_DOELSE_ONCE); |
| 2162 |
} |
| 2163 |
|
| 2164 |
|
| 2165 |
|
| 2166 |
if (compare(ev[0], "void")) |
| 2167 |
{ |
| 2168 |
log_error("[DinkC] Missing } in %s, offset %ld.", |
| 2169 |
rinfo[script]->name,rinfo[script]->current); |
| 2170 |
strcpy_nooverlap(s, h); |
| 2171 |
PL_RETURN(DCPS_YIELD); |
| 2172 |
} |
| 2173 |
|
| 2174 |
/** if **/ |
| 2175 |
if (compare(ev[0], "if")) |
| 2176 |
{ |
| 2177 |
h += strlen(ev[0]); |
| 2178 |
strip_beginning_spaces(h); |
| 2179 |
|
| 2180 |
process_line(script, h, /*false*/0); |
| 2181 |
// Result is 'returnint' |
| 2182 |
|
| 2183 |
if (returnint != 0) |
| 2184 |
{ |
| 2185 |
log_debug("If returned true"); |
| 2186 |
} |
| 2187 |
else |
| 2188 |
{ |
| 2189 |
//don't do it! |
| 2190 |
rinfo[script]->skipnext = /*true*/1; |
| 2191 |
log_debug("If returned false, skipping next thing"); |
| 2192 |
} |
| 2193 |
|
| 2194 |
strcpy_nooverlap(s, h); |
| 2195 |
//g("continuing to run line %s..", h); |
| 2196 |
|
| 2197 |
//PL_RETURN(5); |
| 2198 |
PL_RETURN(DCPS_DOELSE_ONCE); |
| 2199 |
/* state 5 should actually be state DCPS_CONTINUE, but keeping |
| 2200 |
it that way (e.g. with doelse=1 for the next line) for |
| 2201 |
compatibility, just in case somebody abused it */ |
| 2202 |
} |
| 2203 |
|
| 2204 |
if (compare(ev[0], "else")) |
| 2205 |
{ |
| 2206 |
//Msg("Found else!"); |
| 2207 |
h += strlen(ev[0]); |
| 2208 |
|
| 2209 |
if (doelse) |
| 2210 |
{ |
| 2211 |
// Yes to else |
| 2212 |
} |
| 2213 |
else |
| 2214 |
{ |
| 2215 |
// No to else... |
| 2216 |
// they shouldn't run the next thing |
| 2217 |
rinfo[script]->skipnext = /*true*/1; |
| 2218 |
} |
| 2219 |
strcpy_nooverlap(s, h); |
| 2220 |
PL_RETURN(DCPS_CONTINUE); |
| 2221 |
} |
| 2222 |
|
| 2223 |
/** Dialog **/ |
| 2224 |
if (compare(ev[0], "choice_start")) |
| 2225 |
{ |
| 2226 |
kill_text_owned_by(1); |
| 2227 |
if (dinkc_get_choices(script)) |
| 2228 |
{ |
| 2229 |
// Question(s) gathered successfully |
| 2230 |
PL_RETURN(DCPS_YIELD); |
| 2231 |
} |
| 2232 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2233 |
} |
| 2234 |
|
| 2235 |
/** Jump **/ |
| 2236 |
if (compare(ev[0], "goto")) |
| 2237 |
{ |
| 2238 |
locate_goto(ev[1], script); |
| 2239 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2240 |
} |
| 2241 |
|
| 2242 |
/** Definition **/ |
| 2243 |
if (compare(ev[0], "int")) |
| 2244 |
{ |
| 2245 |
int_prepare(h, script); |
| 2246 |
h += strlen(ev[0]); |
| 2247 |
|
| 2248 |
if (strchr(h, '=') != NULL) |
| 2249 |
{ |
| 2250 |
strip_beginning_spaces(h); |
| 2251 |
//Msg("Found =...continuing equation"); |
| 2252 |
strcpy_nooverlap(s, h); |
| 2253 |
PL_RETURN(DCPS_CONTINUE); |
| 2254 |
} |
| 2255 |
else |
| 2256 |
{ |
| 2257 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2258 |
} |
| 2259 |
} |
| 2260 |
|
| 2261 |
/** "return;" and "return something;" **/ |
| 2262 |
if (compare(ev[0], "return;")) |
| 2263 |
{ |
| 2264 |
log_debug("Found return; statement"); |
| 2265 |
|
| 2266 |
if (rinfo[script]->proc_return != 0) |
| 2267 |
{ |
| 2268 |
bKeepReturnInt = 1; /* v1.08 */ |
| 2269 |
run_script(rinfo[script]->proc_return); |
| 2270 |
kill_script(script); |
| 2271 |
} |
| 2272 |
|
| 2273 |
PL_RETURN(DCPS_YIELD); |
| 2274 |
} |
| 2275 |
|
| 2276 |
if (dversion >= 108) |
| 2277 |
{ |
| 2278 |
/* "return", without trailing ';' */ |
| 2279 |
/* added so we can have return values and crap. */ |
| 2280 |
/* see also "return;" above */ |
| 2281 |
if (compare (ev[0], "return")) |
| 2282 |
{ |
| 2283 |
log_debug("Found return; statement"); |
| 2284 |
h += strlen(ev[0]); |
| 2285 |
strip_beginning_spaces (h); |
| 2286 |
process_line (script, h, 0); |
| 2287 |
if (rinfo[script]->proc_return != 0) |
| 2288 |
{ |
| 2289 |
bKeepReturnInt = 1; |
| 2290 |
run_script (rinfo[script]->proc_return); |
| 2291 |
kill_script (script); |
| 2292 |
} |
| 2293 |
PL_RETURN(DCPS_YIELD); |
| 2294 |
} |
| 2295 |
} |
| 2296 |
|
| 2297 |
/********************/ |
| 2298 |
/* DinkC bindings */ |
| 2299 |
/* */ |
| 2300 |
/********************/ |
| 2301 |
|
| 2302 |
/** Lookup bindings **/ |
| 2303 |
{ |
| 2304 |
char* funcname = ev[0]; |
| 2305 |
char* str_args = h + strlen(ev[0]); |
| 2306 |
struct binding* pbd = NULL; |
| 2307 |
pbd = dinkc_bindings_lookup(bindings, funcname); |
| 2308 |
|
| 2309 |
if (pbd != NULL) |
| 2310 |
{ |
| 2311 |
/* Common arguments */ |
| 2312 |
int* yield = (int*)alloca(sizeof(int)*1); |
| 2313 |
yield[0] = 0; /* don't yield by default) */ |
| 2314 |
|
| 2315 |
/* Specific arguments */ |
| 2316 |
int* params = pbd->params; |
| 2317 |
if (params[0] != -1) /* no args == no checks*/ |
| 2318 |
{ |
| 2319 |
if (!get_parms(funcname, script, str_args, params)) |
| 2320 |
{ |
| 2321 |
/* Invalid parameters in the DinkC script - output an |
| 2322 |
error message */ |
| 2323 |
int i = 0; |
| 2324 |
while (params[i] != 0 && i < 10) |
| 2325 |
i++; |
| 2326 |
log_error("[DinkC] %s:%d: procedure '%s' takes %d parameters", |
| 2327 |
rinfo[script]->name, rinfo[script]->debug_line, |
| 2328 |
funcname, i); |
| 2329 |
|
| 2330 |
/* Set 'returnint' if necessary */ |
| 2331 |
if (pbd->badparams_returnint_p == 1) |
| 2332 |
returnint = pbd->badparams_returnint; |
| 2333 |
/* Fallback parser state */ |
| 2334 |
PL_RETURN(pbd->badparams_dcps); |
| 2335 |
} |
| 2336 |
} |
| 2337 |
|
| 2338 |
/* Call C function */ |
| 2339 |
cur_funcname = pbd->funcname; /* for error messages */ |
| 2340 |
int sig_void[10] = {-1,0,0,0,0,0,0,0,0,0}; |
| 2341 |
int sig_int[10] = {1,0,0,0,0,0,0,0,0,0}; |
| 2342 |
int sig_str[10] = {2,0,0,0,0,0,0,0,0,0}; |
| 2343 |
int sig_int_int[10] = {1,1,0,0,0,0,0,0,0,0}; |
| 2344 |
int sig_int_str[10] = {1,2,0,0,0,0,0,0,0,0}; |
| 2345 |
int sig_str_int[10] = {2,1,0,0,0,0,0,0,0,0}; |
| 2346 |
int sig_str_str[10] = {2,2,0,0,0,0,0,0,0,0}; |
| 2347 |
int sig_int_int_int[10] = {1,1,1,0,0,0,0,0,0,0}; |
| 2348 |
int sig_str_int_int[10] = {2,1,1,0,0,0,0,0,0,0}; |
| 2349 |
int sig_int_int_int_int[10] = {1,1,1,1,0,0,0,0,0,0}; |
| 2350 |
int sig_int_int_int_int_int[10] = {1,1,1,1,1,0,0,0,0,0}; |
| 2351 |
int sig_int_int_int_int_int_int[10] = {1,1,1,1,1,1,0,0,0,0}; |
| 2352 |
|
| 2353 |
/* {-1,0,0,0,0,0,0,0,0,0} */ |
| 2354 |
if (signatures_eq_p(pbd->params, sig_void)) |
| 2355 |
{ |
| 2356 |
void (*pf)(int, int*, int*) = (void(*)(int,int*,int*))pbd->func; |
| 2357 |
(*pf)(script, yield, &returnint); |
| 2358 |
} |
| 2359 |
/* {1,0,0,0,0,0,0,0,0,0} */ |
| 2360 |
else if (signatures_eq_p(pbd->params, sig_int)) |
| 2361 |
{ |
| 2362 |
void (*pf)(int, int*, int* , int) = (void(*)(int,int*,int*,int))pbd->func; |
| 2363 |
(*pf)(script, yield, &returnint , nlist[0]); |
| 2364 |
} |
| 2365 |
/* {2,0,0,0,0,0,0,0,0,0} */ |
| 2366 |
else if (signatures_eq_p(pbd->params, sig_str)) |
| 2367 |
{ |
| 2368 |
void (*pf)(int, int*, int* , char*) = (void(*)(int,int*,int*,char*))pbd->func; |
| 2369 |
(*pf)(script, yield, &returnint , slist[0]); |
| 2370 |
} |
| 2371 |
/* {1,1,0,0,0,0,0,0,0,0} */ |
| 2372 |
else if (signatures_eq_p(pbd->params, sig_int_int)) |
| 2373 |
{ |
| 2374 |
void (*pf)(int, int*, int* , int, int) = (void(*)(int,int*,int*,int,int))pbd->func; |
| 2375 |
(*pf)(script, yield, &returnint , nlist[0], nlist[1]); |
| 2376 |
} |
| 2377 |
/* {1,2,0,0,0,0,0,0,0,0} */ |
| 2378 |
else if (signatures_eq_p(pbd->params, sig_int_str)) |
| 2379 |
{ |
| 2380 |
void (*pf)(int, int*, int* , int, char*) = (void(*)(int,int*,int*,int,char*))pbd->func; |
| 2381 |
(*pf)(script, yield, &returnint , nlist[0], slist[1]); |
| 2382 |
} |
| 2383 |
/* {2,1,0,0,0,0,0,0,0,0} */ |
| 2384 |
else if (signatures_eq_p(pbd->params, sig_str_int)) |
| 2385 |
{ |
| 2386 |
void (*pf)(int, int*, int* , char*, int) = (void(*)(int,int*,int*,char*,int))pbd->func; |
| 2387 |
(*pf)(script, yield, &returnint , slist[0], nlist[1]); |
| 2388 |
} |
| 2389 |
/* {2,2,0,0,0,0,0,0,0,0} */ |
| 2390 |
else if (signatures_eq_p(pbd->params, sig_str_str)) |
| 2391 |
{ |
| 2392 |
void (*pf)(int, int*, int* , char*, char*) = (void(*)(int,int*,int*,char*,char*))pbd->func; |
| 2393 |
(*pf)(script, yield, &returnint , slist[0], slist[1]); |
| 2394 |
} |
| 2395 |
/* {1,1,1,0,0,0,0,0,0,0} */ |
| 2396 |
else if (signatures_eq_p(pbd->params, sig_int_int_int)) |
| 2397 |
{ |
| 2398 |
void (*pf)(int, int*, int* , int, int, int) = (void(*)(int,int*,int*,int,int,int))pbd->func; |
| 2399 |
(*pf)(script, yield, &returnint , nlist[0], nlist[1], nlist[2]); |
| 2400 |
} |
| 2401 |
/* {2,1,1,0,0,0,0,0,0,0} */ |
| 2402 |
else if (signatures_eq_p(pbd->params, sig_str_int_int)) |
| 2403 |
{ |
| 2404 |
void (*pf)(int, int*, int* , char*, int, int) = (void(*)(int,int*,int*,char*,int,int))pbd->func; |
| 2405 |
(*pf)(script, yield, &returnint , slist[0], nlist[1], nlist[2]); |
| 2406 |
} |
| 2407 |
/* {1,1,1,1,0,0,0,0,0,0} */ |
| 2408 |
else if (signatures_eq_p(pbd->params, sig_int_int_int_int)) |
| 2409 |
{ |
| 2410 |
void (*pf)(int, int*, int* , int, int, int, int) = (void(*)(int,int*,int*,int,int,int,int))pbd->func; |
| 2411 |
(*pf)(script, yield, &returnint , nlist[0], nlist[1], nlist[2], nlist[3]); |
| 2412 |
} |
| 2413 |
/* {1,1,1,1,1,0,0,0,0,0} */ |
| 2414 |
else if (signatures_eq_p(pbd->params, sig_int_int_int_int_int)) |
| 2415 |
{ |
| 2416 |
void (*pf)(int, int*, int* , int, int, int, int, int) = (void(*)(int,int*,int*,int,int,int,int,int))pbd->func; |
| 2417 |
(*pf)(script, yield, &returnint , nlist[0], nlist[1], nlist[2], nlist[3], nlist[4]); |
| 2418 |
} |
| 2419 |
/* {1,1,1,1,1,1,0,0,0,0} */ |
| 2420 |
else if (signatures_eq_p(pbd->params, sig_int_int_int_int_int_int)) |
| 2421 |
{ |
| 2422 |
void (*pf)(int, int*, int* , int, int, int, int, int, int) = (void(*)(int,int*,int*,int,int,int,int,int,int))pbd->func; |
| 2423 |
(*pf)(script, yield, &returnint , nlist[0], nlist[1], nlist[2], nlist[3], nlist[4], nlist[5]); |
| 2424 |
} |
| 2425 |
else |
| 2426 |
{ |
| 2427 |
log_fatal("Internal error: DinkC function %s has unknown signature", |
| 2428 |
pbd->funcname); |
| 2429 |
exit(EXIT_FAILURE); |
| 2430 |
} |
| 2431 |
cur_funcname = ""; |
| 2432 |
/* the function can manipulation returnint through argument #3 */ |
| 2433 |
|
| 2434 |
if (*yield == 0) |
| 2435 |
{ |
| 2436 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2437 |
} |
| 2438 |
else if (*yield == 1) |
| 2439 |
{ |
| 2440 |
PL_RETURN(DCPS_YIELD); |
| 2441 |
} |
| 2442 |
else |
| 2443 |
{ |
| 2444 |
log_fatal("Internal error: DinkC function %s requested invalid state %d", |
| 2445 |
pbd->funcname, *yield); |
| 2446 |
exit(EXIT_FAILURE); |
| 2447 |
} |
| 2448 |
} |
| 2449 |
} |
| 2450 |
|
| 2451 |
|
| 2452 |
/***************/ |
| 2453 |
/** Operators **/ |
| 2454 |
/** **/ |
| 2455 |
/***************/ |
| 2456 |
|
| 2457 |
/* Beware: this works on ev[1], not ev[0]; position in the code is |
| 2458 |
critical! */ |
| 2459 |
|
| 2460 |
if (compare(ev[1], "=")) |
| 2461 |
{ |
| 2462 |
h += strlen(ev[0]); |
| 2463 |
strip_beginning_spaces(h); |
| 2464 |
h++; |
| 2465 |
strip_beginning_spaces(h); |
| 2466 |
var_equals(ev[0], ev[2], '=', script, h); |
| 2467 |
strcpy_nooverlap(s, h); |
| 2468 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2469 |
} |
| 2470 |
|
| 2471 |
if (compare(ev[1], "+=")) |
| 2472 |
{ |
| 2473 |
h += strlen(ev[0]); |
| 2474 |
strip_beginning_spaces(h); |
| 2475 |
h += 2; |
| 2476 |
strip_beginning_spaces(h); |
| 2477 |
var_equals(ev[0], ev[2], '+', script, h); |
| 2478 |
strcpy_nooverlap(s, h); |
| 2479 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2480 |
} |
| 2481 |
|
| 2482 |
if (compare(ev[1], "*=")) |
| 2483 |
{ |
| 2484 |
h += strlen(ev[0]); |
| 2485 |
strip_beginning_spaces(h); |
| 2486 |
h += 2; |
| 2487 |
strip_beginning_spaces(h); |
| 2488 |
var_equals(ev[0], ev[2], '*', script, h); |
| 2489 |
strcpy_nooverlap(s, h); |
| 2490 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2491 |
} |
| 2492 |
|
| 2493 |
if (compare(ev[1], "-=")) |
| 2494 |
{ |
| 2495 |
h += strlen(ev[0]); |
| 2496 |
strip_beginning_spaces(h); |
| 2497 |
h += 2; |
| 2498 |
strip_beginning_spaces(h); |
| 2499 |
|
| 2500 |
var_equals(ev[0], ev[2], '-', script, h); |
| 2501 |
|
| 2502 |
strcpy_nooverlap(s, h); |
| 2503 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2504 |
} |
| 2505 |
|
| 2506 |
if (compare(ev[1], "/") |
| 2507 |
|| (dversion >= 108 && compare(ev[1], "/="))) |
| 2508 |
{ |
| 2509 |
h += strlen(ev[0]); |
| 2510 |
strip_beginning_spaces(h); |
| 2511 |
h++; |
| 2512 |
strip_beginning_spaces(h); |
| 2513 |
|
| 2514 |
var_equals(ev[0], ev[2], '/', script, h); |
| 2515 |
|
| 2516 |
strcpy_nooverlap(s, h); |
| 2517 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2518 |
} |
| 2519 |
|
| 2520 |
if (compare(ev[1], "*")) |
| 2521 |
{ |
| 2522 |
h += strlen(ev[0]); |
| 2523 |
strip_beginning_spaces(h); |
| 2524 |
h++; |
| 2525 |
strip_beginning_spaces(h); |
| 2526 |
|
| 2527 |
var_equals(ev[0], ev[2], '*', script, h); |
| 2528 |
|
| 2529 |
strcpy_nooverlap(s, h); |
| 2530 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2531 |
} |
| 2532 |
|
| 2533 |
|
| 2534 |
/***************************************/ |
| 2535 |
/** New DinkC user-defined procedures **/ |
| 2536 |
/** **/ |
| 2537 |
/***************************************/ |
| 2538 |
if (dversion >= 108) |
| 2539 |
{ |
| 2540 |
if (compare (ev[0], "external")) |
| 2541 |
{ |
| 2542 |
h += strlen(ev[0]); |
| 2543 |
int p[20] = { 2, 2, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 2544 |
{ |
| 2545 |
int i = 0; |
| 2546 |
for (; i < 10; i++) |
| 2547 |
slist[i][0] = '\0'; |
| 2548 |
} |
| 2549 |
get_parms(ev[0], script, h, p); |
| 2550 |
if (strlen(slist[0]) > 0 && strlen(slist[1]) > 0) |
| 2551 |
{ |
| 2552 |
int myscript1 = load_script(slist[0], rinfo[script]->sprite); |
| 2553 |
if (myscript1 == 0) |
| 2554 |
{ |
| 2555 |
log_error("[DinkC] external: Couldn't find %s.c (for procedure %s)", |
| 2556 |
slist[0], slist[1]); |
| 2557 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2558 |
} |
| 2559 |
rinfo[myscript1]->arg1 = nlist[2]; |
| 2560 |
rinfo[myscript1]->arg2 = nlist[3]; |
| 2561 |
rinfo[myscript1]->arg3 = nlist[4]; |
| 2562 |
rinfo[myscript1]->arg4 = nlist[5]; |
| 2563 |
rinfo[myscript1]->arg5 = nlist[6]; |
| 2564 |
rinfo[myscript1]->arg6 = nlist[7]; |
| 2565 |
rinfo[myscript1]->arg7 = nlist[8]; |
| 2566 |
rinfo[myscript1]->arg8 = nlist[9]; |
| 2567 |
if (locate (myscript1, slist[1])) |
| 2568 |
{ |
| 2569 |
rinfo[myscript1]->proc_return = script; |
| 2570 |
run_script (myscript1); |
| 2571 |
PL_RETURN(DCPS_YIELD); |
| 2572 |
} |
| 2573 |
else |
| 2574 |
{ |
| 2575 |
log_error("[DinkC] external: Couldn't find procedure %s in %s.", |
| 2576 |
slist[1], slist[0]); |
| 2577 |
kill_script (myscript1); |
| 2578 |
} |
| 2579 |
} |
| 2580 |
strcpy (s, h); |
| 2581 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2582 |
} |
| 2583 |
|
| 2584 |
if (strchr (h, '(') != NULL) |
| 2585 |
{ |
| 2586 |
//lets attempt to run a procedure |
| 2587 |
int myscript = load_script (rinfo[script]->name, rinfo[script]->sprite); |
| 2588 |
h += strlen(ev[0]); |
| 2589 |
int p[20] = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; |
| 2590 |
get_parms(ev[0], script, h, p); |
| 2591 |
if (locate(myscript, ev[0])) |
| 2592 |
{ |
| 2593 |
/* Custom procedure in the current script */ |
| 2594 |
rinfo[myscript]->arg1 = nlist[0]; |
| 2595 |
rinfo[myscript]->arg2 = nlist[1]; |
| 2596 |
rinfo[myscript]->arg3 = nlist[2]; |
| 2597 |
rinfo[myscript]->arg4 = nlist[3]; |
| 2598 |
rinfo[myscript]->arg5 = nlist[4]; |
| 2599 |
rinfo[myscript]->arg6 = nlist[5]; |
| 2600 |
rinfo[myscript]->arg7 = nlist[6]; |
| 2601 |
rinfo[myscript]->arg8 = nlist[7]; |
| 2602 |
rinfo[myscript]->arg9 = nlist[8]; |
| 2603 |
rinfo[myscript]->proc_return = script; |
| 2604 |
run_script(myscript); |
| 2605 |
PL_RETURN(DCPS_YIELD); |
| 2606 |
} |
| 2607 |
else |
| 2608 |
{ |
| 2609 |
/* Try custom global procedure */ |
| 2610 |
int i = 0; |
| 2611 |
for (; i < 100; i++) |
| 2612 |
{ |
| 2613 |
/* Skip empty slots */ |
| 2614 |
if (strlen (play.func[i].func) == 0) |
| 2615 |
continue; |
| 2616 |
|
| 2617 |
if (compare(play.func[i].func, ev[0])) |
| 2618 |
{ |
| 2619 |
myscript = load_script(play.func[i].file, rinfo[script]->sprite); |
| 2620 |
rinfo[myscript]->arg1 = nlist[0]; |
| 2621 |
rinfo[myscript]->arg2 = nlist[1]; |
| 2622 |
rinfo[myscript]->arg3 = nlist[2]; |
| 2623 |
rinfo[myscript]->arg4 = nlist[3]; |
| 2624 |
rinfo[myscript]->arg5 = nlist[4]; |
| 2625 |
rinfo[myscript]->arg6 = nlist[5]; |
| 2626 |
rinfo[myscript]->arg7 = nlist[6]; |
| 2627 |
rinfo[myscript]->arg8 = nlist[7]; |
| 2628 |
rinfo[myscript]->arg9 = nlist[8]; |
| 2629 |
if (locate(myscript, ev[0])) |
| 2630 |
{ |
| 2631 |
rinfo[myscript]->proc_return = script; |
| 2632 |
run_script (myscript); |
| 2633 |
PL_RETURN(DCPS_YIELD); |
| 2634 |
} |
| 2635 |
break; |
| 2636 |
} |
| 2637 |
} |
| 2638 |
log_error("[DinkC] Procedure void %s( void ); not found in script %s." |
| 2639 |
" (word 2 was \"%s\")", |
| 2640 |
ev[0], rinfo[myscript] != NULL ? rinfo[myscript]->name : "", |
| 2641 |
ev[1]); |
| 2642 |
kill_script (myscript); |
| 2643 |
} |
| 2644 |
|
| 2645 |
/*seperate_string(h, 1,'(',line); |
| 2646 |
|
| 2647 |
int myscript = load_script(rinfo[script]->name, rinfo[script]->sprite, false); |
| 2648 |
|
| 2649 |
if (locate( myscript, line)) |
| 2650 |
{ |
| 2651 |
rinfo[myscript]->proc_return = script; |
| 2652 |
run_script(myscript); |
| 2653 |
PL_RETURN(DCPS_YIELD); |
| 2654 |
} else |
| 2655 |
{ |
| 2656 |
Msg("ERROR: Procedure void %s( void ); not found in script %s. (word 2 was %s) ", line, |
| 2657 |
ev[1], rinfo[myscript]->name); |
| 2658 |
kill_script(myscript); |
| 2659 |
} */ |
| 2660 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2661 |
} |
| 2662 |
} |
| 2663 |
else |
| 2664 |
{ |
| 2665 |
/* v1.07 function that are implemented differently than in v1.08 */ |
| 2666 |
if (compare(ev[0], "external")) |
| 2667 |
{ |
| 2668 |
h += strlen(ev[0]); |
| 2669 |
int p[20] = {2,2,0,0,0,0,0,0,0,0}; |
| 2670 |
if (get_parms(ev[0], script, h, p)) |
| 2671 |
{ |
| 2672 |
int myscript1 = load_script(slist[0], rinfo[script]->sprite); |
| 2673 |
if (myscript1 == 0) |
| 2674 |
{ |
| 2675 |
log_error("[DinkC] external: Couldn't find %s.c (for procedure %s)", slist[0], slist[1]); |
| 2676 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2677 |
} |
| 2678 |
if (locate(myscript1, slist[1])) |
| 2679 |
{ |
| 2680 |
rinfo[myscript1]->proc_return = script; |
| 2681 |
run_script(myscript1); |
| 2682 |
PL_RETURN(DCPS_YIELD); |
| 2683 |
} |
| 2684 |
else |
| 2685 |
{ |
| 2686 |
log_error("[DinkC] external: Couldn't find procedure %s in %s.", slist[1], slist[0]); |
| 2687 |
kill_script(myscript1); |
| 2688 |
} |
| 2689 |
} |
| 2690 |
else |
| 2691 |
{ |
| 2692 |
log_error("[DinkC] %s: procedure 'external' takes 2 parameters" |
| 2693 |
" (offset %ld)", |
| 2694 |
rinfo[script]->name, rinfo[script]->current); |
| 2695 |
} |
| 2696 |
strcpy_nooverlap(s, h); |
| 2697 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2698 |
} |
| 2699 |
|
| 2700 |
if (strchr(h, '(') != NULL) |
| 2701 |
{ |
| 2702 |
//lets attempt to run a procedure |
| 2703 |
char* proc = separate_string(h, 1, '('); |
| 2704 |
int myscript = load_script(rinfo[script]->name, rinfo[script]->sprite); |
| 2705 |
|
| 2706 |
if (locate(myscript, proc)) |
| 2707 |
{ |
| 2708 |
rinfo[myscript]->proc_return = script; |
| 2709 |
run_script(myscript); |
| 2710 |
free(proc); |
| 2711 |
PL_RETURN(DCPS_YIELD); |
| 2712 |
} |
| 2713 |
else |
| 2714 |
{ |
| 2715 |
log_error("[DinkC] Procedure void %s( void ); not found in script %s. (word 2 was %s)", |
| 2716 |
proc, rinfo[script]->name, ev[1]); |
| 2717 |
kill_script(myscript); |
| 2718 |
} |
| 2719 |
free(proc); |
| 2720 |
PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2721 |
} |
| 2722 |
|
| 2723 |
log_error("[DinkC] \"%s\" unknown in %s, offset %ld.", |
| 2724 |
ev[0], rinfo[script]->name,rinfo[script]->current); |
| 2725 |
//in a thingie, ready to go |
| 2726 |
} |
| 2727 |
|
| 2728 |
bad: |
| 2729 |
strcpy(s, "\n"); /* jump to next line */ |
| 2730 |
//PL_RETURN(DCPS_GOTO_NEXTLINE); |
| 2731 |
PL_RETURN(DCPS_CONTINUE); |
| 2732 |
|
| 2733 |
good: |
| 2734 |
strcpy_nooverlap(s, h); |
| 2735 |
//s = h |
| 2736 |
//Msg("ok, continuing with running %s..",s); |
| 2737 |
PL_RETURN(DCPS_CONTINUE); |
| 2738 |
} |
| 2739 |
|
| 2740 |
|
| 2741 |
/****************/ |
| 2742 |
/* Hash table */ |
| 2743 |
/* */ |
| 2744 |
/****************/ |
| 2745 |
|
| 2746 |
/* Hash table of bindings, build dynamically (depending on 'dversion', |
| 2747 |
not statically) */ |
| 2748 |
Hash_table* bindings = NULL; |
| 2749 |
|
| 2750 |
/* Auxiliary functions for hash */ |
| 2751 |
static size_t dinkc_bindings_hasher(const void *x, size_t tablesize) |
| 2752 |
{ |
| 2753 |
return hash_string(((struct binding*)x)->funcname, tablesize); |
| 2754 |
// We could also call 'hash_pjw' from module 'hash-pjw' |
| 2755 |
} |
| 2756 |
|
| 2757 |
static bool dinkc_bindings_comparator(const void* a, const void* b) |
| 2758 |
{ |
| 2759 |
return !strcmp(((struct binding*)a)->funcname, |
| 2760 |
((struct binding*)b)->funcname); |
| 2761 |
} |
| 2762 |
|
| 2763 |
/** |
| 2764 |
* Search a binding by function name |
| 2765 |
*/ |
| 2766 |
struct binding* dinkc_bindings_lookup(Hash_table* hash, char* funcname) |
| 2767 |
{ |
| 2768 |
struct binding search; |
| 2769 |
struct binding* result; |
| 2770 |
char* lcfuncname = strdup(funcname); |
| 2771 |
char* pc; |
| 2772 |
for (pc = lcfuncname; *pc != '\0'; pc++) |
| 2773 |
*pc = tolower(*pc); |
| 2774 |
search.funcname = lcfuncname; |
| 2775 |
|
| 2776 |
result = (struct binding*)hash_lookup(hash, &search); |
| 2777 |
|
| 2778 |
free(lcfuncname); |
| 2779 |
return result; |
| 2780 |
} |
| 2781 |
|
| 2782 |
/** |
| 2783 |
* Add a new binding to hash table 'hash'. |
| 2784 |
*/ |
| 2785 |
void dinkc_bindings_add(Hash_table* hash, struct binding* pbd) |
| 2786 |
{ |
| 2787 |
void* slot = dinkc_bindings_lookup(hash, pbd->funcname); |
| 2788 |
if (slot != NULL) |
| 2789 |
{ |
| 2790 |
log_fatal("Internal error: attempting to redeclare DinkC function %s", pbd->funcname); |
| 2791 |
exit(EXIT_FAILURE); |
| 2792 |
} |
| 2793 |
|
| 2794 |
/* Copy uninitialized binding in hash table */ |
| 2795 |
struct binding* newslot = (struct binding*)malloc(sizeof(struct binding)); |
| 2796 |
*newslot = *pbd; |
| 2797 |
if (hash_insert(hash, newslot) == NULL) |
| 2798 |
{ |
| 2799 |
log_fatal("Not enough memory to declare DinkC function %s", pbd->funcname); |
| 2800 |
exit(EXIT_FAILURE); |
| 2801 |
} |
| 2802 |
} |
| 2803 |
|
| 2804 |
|
| 2805 |
void dinkc_init() |
| 2806 |
{ |
| 2807 |
memset(&rinfo, 0, sizeof(rinfo)); |
| 2808 |
game_choice_clear(); |
| 2809 |
|
| 2810 |
/* Set all string params pointers to NULL */ |
| 2811 |
int i = 0; |
| 2812 |
for (; i < 10; i++) |
| 2813 |
{ |
| 2814 |
/* alloc empty strings; will be replaced as needed in |
| 2815 |
get_parm(...) */ |
| 2816 |
slist[i] = strdup(""); |
| 2817 |
} |
| 2818 |
|
| 2819 |
Hash_tuning* default_tuner = NULL; |
| 2820 |
int start_size = 400; /* ~nbfuncs*2 to try and avoid collisions */ |
| 2821 |
bindings = hash_initialize(start_size, default_tuner, |
| 2822 |
dinkc_bindings_hasher, dinkc_bindings_comparator, |
| 2823 |
free); |
| 2824 |
} |
| 2825 |
|
| 2826 |
void dinkc_quit() |
| 2827 |
{ |
| 2828 |
int i = 0; |
| 2829 |
for (; i < 10; i++) |
| 2830 |
{ |
| 2831 |
if (slist[i] != NULL) |
| 2832 |
free(slist[i]); |
| 2833 |
slist[i] = NULL; |
| 2834 |
} |
| 2835 |
kill_all_vars(); |
| 2836 |
} |