| 1 | /* |
| 2 | * Descent 3 |
| 3 | * Copyright (C) 2024 Parallax Software |
| 4 | * |
| 5 | * This program is free software: you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation, either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 | |
| 18 | --- HISTORICAL COMMENTS FOLLOW --- |
| 19 | |
| 20 | * $Logfile: /DescentIII/Main/editor/ObjCScript.cpp $ |
| 21 | * $Revision: 1.1.1.1 $ |
| 22 | * $Date: 2003-08-26 03:57:38 $ |
| 23 | * $Author: kevinb $ |
| 24 | * |
| 25 | * Script loading and saving from editor. |
| 26 | * |
| 27 | * $Log: not supported by cvs2svn $ |
| 28 | * |
| 29 | * 29 10/08/98 4:24p Kevin |
| 30 | * Changed code to comply with memory library usage. Always use mem_malloc |
| 31 | * , mem_free and mem_strdup |
| 32 | * |
| 33 | * 28 6/23/98 3:04p Samir |
| 34 | * sped up script list generation. |
| 35 | * |
| 36 | * 27 5/04/98 3:22p Samir |
| 37 | * ability to select which scripts you want to generate scriptwiz info |
| 38 | * for. |
| 39 | * |
| 40 | * 26 4/21/98 12:16a Samir |
| 41 | * Hopefully if you change script definition, it's reflected as you edit |
| 42 | * it. |
| 43 | * |
| 44 | * 25 4/19/98 9:56p Chris |
| 45 | * AI system is integrated with OSIRIS. Path system is integrated with |
| 46 | * the AI system. Bugs will ensue. |
| 47 | * |
| 48 | * 24 4/15/98 5:56p Chris |
| 49 | * Made the AI system script friendly |
| 50 | * |
| 51 | * 23 3/16/98 11:19a Jason |
| 52 | * got scripts working with multiplayer |
| 53 | * |
| 54 | * 22 2/25/98 4:06p Samir |
| 55 | * Added function to generate a table of all scripts on disk. |
| 56 | * |
| 57 | * 21 1/22/98 6:22p Samir |
| 58 | * Parse script parameters now. |
| 59 | * |
| 60 | * 20 11/16/97 4:35p Samir |
| 61 | * Took out annoying mprintfs. |
| 62 | * |
| 63 | * 19 9/25/97 5:28p Samir |
| 64 | * Even newer script code due to more changes in ObjCScript.cpp |
| 65 | * |
| 66 | * 18 9/24/97 2:56p Samir |
| 67 | * New ScriptWizard functionality, without extra commenting in files. |
| 68 | * |
| 69 | * 17 9/22/97 5:59p Samir |
| 70 | * Changed ObjCScript system, so everything is broken, but it shouldn't |
| 71 | * crash the game. |
| 72 | * |
| 73 | * 16 9/12/97 4:14p Samir |
| 74 | * Fixed up list of events and strings. |
| 75 | * |
| 76 | * 15 9/10/97 1:57p Samir |
| 77 | * Redid FindScriptFile functions, adding scripts to script files involves |
| 78 | * specifying a type also. |
| 79 | * |
| 80 | * 14 9/09/97 1:04p Samir |
| 81 | * Added code to create new script and save level script name. |
| 82 | * |
| 83 | * 13 9/08/97 4:29p Samir |
| 84 | * Added EVT_DESTROYED support to editing code. |
| 85 | * |
| 86 | * 12 9/04/97 4:39p Matt |
| 87 | * Added includes needed as a result of removing includes from d3edit.h |
| 88 | * |
| 89 | * 11 8/21/97 6:00p Samir |
| 90 | * A whole lot of script editing stuff added for ScriptWizard. |
| 91 | * |
| 92 | * 10 8/18/97 12:14p Samir |
| 93 | * Added function to get the index of a script name in the script list. |
| 94 | * |
| 95 | * 9 8/15/97 6:32p Samir |
| 96 | * Use a more generic editor script interface instead of specifying |
| 97 | * default scripts, etc. |
| 98 | * |
| 99 | * 8 8/13/97 5:39p Samir |
| 100 | * Use CompileScript to compile a script now, and we compile default |
| 101 | * script at startup of editor. |
| 102 | * |
| 103 | * 7 8/12/97 3:23p Samir |
| 104 | * D3XReallocProgram has more arguments. |
| 105 | * |
| 106 | * 6 8/06/97 10:38a Samir |
| 107 | * Don't load in script obejct file. Don't need to. |
| 108 | * |
| 109 | * 5 8/04/97 6:45p Samir |
| 110 | * Fixed crash when freeing a null script. |
| 111 | * |
| 112 | * 4 8/04/97 4:13p Samir |
| 113 | * Added code to load and save the default script code. |
| 114 | * |
| 115 | * $NoKeywords: $ |
| 116 | */ |
| 117 | |
| 118 | #include "stdafx.h" |
| 119 | #include "ObjCScript.h" |
| 120 | #include "editor.h" |
| 121 | #include "cfile.h" |
| 122 | #include "ddio.h" |
| 123 | #include "d3x.h" |
| 124 | #include "manage.h" |
| 125 | #include "mission.h" |
| 126 | #include "gamefile.h" |
| 127 | #include "pserror.h" |
| 128 | #include "mem.h" |
| 129 | |
| 130 | #include <stdlib.h> |
| 131 | #include <stdio.h> |
| 132 | #include <string.h> |
| 133 | |
| 134 | #define MAX_SCRIPT_LINE_SIZE 800 |
| 135 | |
| 136 | // script name for current level |
| 137 | char Level_script_name[MAX_SCRFILENAME]; |
| 138 | |
| 139 | // Currently loaded script file |
| 140 | char *Default_script_source = NULL; |
| 141 | char *Level_script_source = NULL; |
| 142 | |
| 143 | // Table of scripts within script file |
| 144 | tScriptName Script_names[MAX_SCRIPTS]; |
| 145 | |
| 146 | #define MAX_SCREVTS 15 |
| 147 | // INCREMENT MAX_SCREVTS in ObjCScript when you add a new event! |
| 148 | char *Script_evt_names[MAX_SCREVTS] = { |
| 149 | "EVT_AI_FRAME", "EVT_AI_NOTIFY", "EVT_AI_INIT", |
| 150 | "EVT_CREATED", "EVT_COLLIDE", "EVT_DAMAGED", |
| 151 | "EVT_INTERVAL", "EVT_DESTROY", "EVT_USE", |
| 152 | "EVT_CLIENT_AI", "EVT_CLIENT_CREATED", "EVT_CLIENT_COLLIDE", |
| 153 | "EVT_CLIENT_DAMAGED", "EVT_CLIENT_INTERVAL", "EVT_CLIENT_DESTROY", |
| 154 | }; |
| 155 | |
| 156 | #if 0 // LGT - EVT_X undefined |
| 157 | uint16_t Script_evt_ids[MAX_SCREVTS] = { |
| 158 | EVT_AI_FRAME, |
| 159 | EVT_AI_NOTIFY, |
| 160 | EVT_AI_INIT, |
| 161 | EVT_CREATED, |
| 162 | EVT_COLLIDE, |
| 163 | EVT_DAMAGED, |
| 164 | EVT_INTERVAL, |
| 165 | EVT_DESTROY, |
| 166 | EVT_USE, |
| 167 | EVT_CLIENT_AI, |
| 168 | EVT_CLIENT_CREATED, |
| 169 | EVT_CLIENT_COLLIDE, |
| 170 | EVT_CLIENT_DAMAGED, |
| 171 | EVT_CLIENT_INTERVAL, |
| 172 | EVT_CLIENT_DESTROY |
| 173 | }; |
| 174 | #endif |
| 175 | |
| 176 | // function to find first free slot in script list. |
| 177 | int FindFreeScriptSlot(); |
| 178 | char *GotoScriptInText(char *text, const char *script); |
| 179 | int GenerateScriptParamInfo(int id, const char *script_text); |
| 180 | |
| 181 | bool NewScript(const char *filename) { |
| 182 | char path[_MAX_PATH]; |
| 183 | CFILE *file; |
| 184 | |
| 185 | ddio_MakePath(path, LocalLevelsDir, filename, NULL); |
| 186 | |
| 187 | // create the file |
| 188 | file = cfopen(path, "wt"); |
| 189 | if (!file) |
| 190 | return false; |
| 191 | |
| 192 | cfclose(file); |
| 193 | |
| 194 | return true; |
| 195 | } |
| 196 | |
| 197 | // Loads the script into into memory (Don't forget to call FreeScript() to free the memory when done) |
| 198 | // Returns true on success |
| 199 | char *LoadScript(const char *filename) { |
| 200 | char *source; |
| 201 | char file_path[256]; |
| 202 | CFILE *file; |
| 203 | CString temp; |
| 204 | int size; |
| 205 | char buffer[MAX_SCRIPT_LINE_SIZE]; |
| 206 | |
| 207 | memset(buffer, 0, MAX_SCRIPT_LINE_SIZE); |
| 208 | |
| 209 | ddio_MakePath(file_path, LocalLevelsDir, filename, NULL); |
| 210 | // mprintf(0,"Loading script from %s\n",file_path); |
| 211 | if (!cfexist(file_path)) { |
| 212 | return false; |
| 213 | } |
| 214 | |
| 215 | file = cfopen(file_path, "rt"); |
| 216 | do { |
| 217 | size = cf_ReadString(buffer, MAX_SCRIPT_LINE_SIZE - 2, file); |
| 218 | buffer[size] = '\xd'; |
| 219 | buffer[size + 1] = '\xa'; |
| 220 | buffer[size + 2] = '\0'; |
| 221 | temp += buffer; |
| 222 | memset(buffer, 0, size); |
| 223 | } while (!cfeof(file)); |
| 224 | |
| 225 | char *tmp_script; |
| 226 | size = strlen(temp.GetBuffer(1)); |
| 227 | tmp_script = (char *)mem_malloc(size + 1); |
| 228 | ASSERT(tmp_script); |
| 229 | strcpy(tmp_script, temp.GetBuffer(1)); |
| 230 | if (size > 2) |
| 231 | tmp_script[size - 2] = '\0'; |
| 232 | temp = tmp_script; |
| 233 | if (tmp_script) |
| 234 | mem_free(tmp_script); |
| 235 | |
| 236 | cfclose(file); |
| 237 | |
| 238 | source = (char *)mem_malloc(strlen(temp.GetBuffer(1)) + 1); |
| 239 | if (!source) |
| 240 | return false; |
| 241 | strcpy(source, temp.GetBuffer(1)); |
| 242 | |
| 243 | return source; |
| 244 | } |
| 245 | |
| 246 | void SaveScript(const char *filename, char *script) { |
| 247 | CFILE *file; |
| 248 | char file_path[256]; |
| 249 | |
| 250 | ddio_MakePath(file_path, LocalLevelsDir, filename, NULL); |
| 251 | |
| 252 | file = cfopen(file_path, "wt"); |
| 253 | cf_WriteString(file, script); |
| 254 | // mprintf(0,"Saving script to %s\n",file_path); |
| 255 | cfclose(file); |
| 256 | } |
| 257 | |
| 258 | void FreeScript(char *script) { mem_free(script); } |
| 259 | |
| 260 | // Generates a program from a script |
| 261 | bool CompileScript(tD3XProgram *program, char *script) { |
| 262 | #if 0 // LGT: D3XReallocProgram undefined. Legacy scripting system? |
| 263 | int nscr, nstr, d3xlen; |
| 264 | char *strbuf; // string buffer grabbed from compile |
| 265 | tD3XInstruction *ins; // temporary holder for code |
| 266 | tD3XPMap *map; // temporary holder for map |
| 267 | |
| 268 | if (!script) { |
| 269 | mprintf(1, "Unable to compile null script!\n"); |
| 270 | return false; |
| 271 | } |
| 272 | |
| 273 | if (program == NULL) { |
| 274 | mprintf(1, "You can't compile an uninitialized script!\n"); |
| 275 | return false; |
| 276 | } |
| 277 | |
| 278 | if(!osi_Compile(script, &d3xlen, &ins, &nscr, &map, &nstr, &strbuf)) { |
| 279 | mprintf(1, "Script failed to compile.\n"); |
| 280 | return false; |
| 281 | } |
| 282 | |
| 283 | D3XReallocProgram(program, d3xlen, nscr, nstr); |
| 284 | D3XLoadProgramFromComponents(program, ins, map, strbuf); |
| 285 | |
| 286 | return true; |
| 287 | #else |
| 288 | return false; |
| 289 | #endif |
| 290 | } |
| 291 | |
| 292 | void SaveScriptCode(const char *filename, tD3XProgram *program) { |
| 293 | CFILE *file; |
| 294 | char file_path[256]; |
| 295 | |
| 296 | // write out default script program |
| 297 | ddio_MakePath(file_path, LocalLevelsDir, filename, NULL); |
| 298 | file = cfopen(file_path, "wb"); |
| 299 | // D3XSaveProgram(file, program); // LGT: function undefined |
| 300 | mprintf(0, "Saving %s.\n", filename); |
| 301 | cfclose(file); |
| 302 | } |
| 303 | |
| 304 | ////////////////////////////////////////////////////////////////////////////// |
| 305 | // LIST FUNCTIONS |
| 306 | |
| 307 | inline int AddScriptToList(char *name, uint16_t type, bool custom) { |
| 308 | int slot = FindFreeScriptSlot(); |
| 309 | strcpy(Script_names[slot].name, name); |
| 310 | Script_names[slot].used = true; |
| 311 | Script_names[slot].type = type; |
| 312 | Script_names[slot].iscustom = custom; |
| 313 | Script_names[slot].free_parameters(); |
| 314 | return slot; |
| 315 | } |
| 316 | |
| 317 | // Resets all entries in script list to null. |
| 318 | void ResetScriptList() { |
| 319 | int i; |
| 320 | for (i = 0; i < MAX_SCRIPTS; i++) |
| 321 | Script_names[i].used = false; |
| 322 | } |
| 323 | |
| 324 | // This function generates ScriptWizard comment header information for script files |
| 325 | // if it doesn't already have it. This should be called whenever a script file is loaded and compiled |
| 326 | // via script wizard. |
| 327 | // returns true |
| 328 | |
| 329 | bool GenerateScriptWizardInfo(tD3XProgram *prog, bool custom) { |
| 330 | #if 0 // LGT: Legacy scripting system? |
| 331 | int i; |
| 332 | |
| 333 | if (!prog) { |
| 334 | Int3(); |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | for (i = 0; i < prog->nscripts; i++) |
| 339 | { |
| 340 | int slot; |
| 341 | slot = AddScriptToList(prog->map[i].name, prog->map[i].type, custom); |
| 342 | Script_names[slot].line = 0; |
| 343 | } |
| 344 | |
| 345 | return true; |
| 346 | #else |
| 347 | return false; |
| 348 | #endif |
| 349 | } |
| 350 | |
| 351 | // This function actually tries to go throught the script source to find script names. |
| 352 | // to find a script name we use the following algorithmn |
| 353 | // |
| 354 | // find one line containing: |
| 355 | // trigger || object || level && script in succession |
| 356 | // next word will be the script name |
| 357 | // following script name will be '(' then look for endscript |
| 358 | // repeat. |
| 359 | |
| 360 | bool GenerateScriptWizardInfo(char *script, bool custom) { |
| 361 | char word[256]; |
| 362 | bool inword; |
| 363 | int i, index, line; |
| 364 | int state; // 0 = nothing, 1 = typeword, 2 = scriptword |
| 365 | int slot, strsize; |
| 366 | uint16_t type = 0xffff; |
| 367 | |
| 368 | inword = false; |
| 369 | index = 0; |
| 370 | state = 0; |
| 371 | line = 1; |
| 372 | slot = 0; |
| 373 | |
| 374 | if (script == NULL) |
| 375 | return false; |
| 376 | |
| 377 | strsize = strlen(script); |
| 378 | |
| 379 | for (i = 0; i < strsize; i++) { |
| 380 | char ch; |
| 381 | |
| 382 | ch = script[i]; |
| 383 | |
| 384 | if (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n' && ch != '(' && ch != ')' && ch != ',') { |
| 385 | // no whitespace |
| 386 | if (!inword) |
| 387 | inword = true; |
| 388 | if (inword) |
| 389 | word[index++] = ch; |
| 390 | } else { |
| 391 | // whitespace |
| 392 | // word is complete, check if a keyword |
| 393 | if (ch == '\n') |
| 394 | line++; |
| 395 | word[index] = 0; |
| 396 | |
| 397 | if (state == 0) { |
| 398 | if (!strcmp(word, "object")) |
| 399 | type = REF_OBJTYPE; |
| 400 | else if (!strcmp(word, "trigger")) |
| 401 | type = REF_TRIGTYPE; |
| 402 | else if (!strcmp(word, "level")) |
| 403 | type = REF_LEVELTYPE; |
| 404 | else |
| 405 | type = 0xffff; |
| 406 | if (type != 0xffff) { |
| 407 | state = 1; |
| 408 | } |
| 409 | } else if (state == 1) { |
| 410 | if (!strcmp(word, "script")) |
| 411 | state = 2; |
| 412 | else |
| 413 | state = 0; |
| 414 | } else if (state == 2) { |
| 415 | // if we are in state 2, then we have PROBABLY FOUND a script name, saved_ptr defined. |
| 416 | if (inword) { |
| 417 | slot = AddScriptToList(word, type, custom); |
| 418 | Script_names[slot].line = line; |
| 419 | i += GenerateScriptParamInfo(slot, &script[i]); |
| 420 | state = 3; |
| 421 | } |
| 422 | } else if (state == 3) { |
| 423 | // check for endscript, then we go to state 0 |
| 424 | if (!strcmp(word, "endscript")) |
| 425 | state = 0; |
| 426 | } |
| 427 | index = 0; |
| 428 | inword = false; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | return true; |
| 433 | } |
| 434 | |
| 435 | // create paramameter table for script |
| 436 | int GenerateScriptParamInfo(int id, const char *script_text) { |
| 437 | char word[256]; |
| 438 | char saved_word[256]; |
| 439 | bool inword = false; |
| 440 | int index = 0; |
| 441 | int state = 0; |
| 442 | int i, strsize = strlen(script_text); |
| 443 | |
| 444 | for (i = 0; i < strsize; i++) { |
| 445 | char ch; |
| 446 | |
| 447 | ch = script_text[i]; |
| 448 | |
| 449 | if (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n' && ch != '(' && ch != ')' && ch != ',') { |
| 450 | // no whitespace |
| 451 | if (!inword) |
| 452 | inword = true; |
| 453 | if (inword) |
| 454 | word[index++] = ch; |
| 455 | } else { |
| 456 | // word is complete, check if a keyword |
| 457 | word[index] = 0; |
| 458 | // if we are in state 1, then we have PROBABLY FOUND a script name, make sure name matches |
| 459 | if (state == 0) { |
| 460 | // in parameter definition if we've reached a ')', reset state to start |
| 461 | if (Script_names[id].nparms == MAX_SCRPARAMS) { |
| 462 | OutrageMessageBox("Parameter parsing list size maxed."); |
| 463 | return i; |
| 464 | } else if (ch == ')') |
| 465 | break; |
| 466 | else if (inword) { |
| 467 | strcpy(saved_word, word); |
| 468 | state = 1; |
| 469 | } |
| 470 | } else if (state == 1) { |
| 471 | // in parameter name |
| 472 | if (inword) { |
| 473 | Script_names[id].add_parameter(saved_word, word); |
| 474 | state = 0; // read next parameter |
| 475 | } |
| 476 | if (ch == ')') |
| 477 | break; |
| 478 | } |
| 479 | index = 0; |
| 480 | inword = false; |
| 481 | } |
| 482 | } |
| 483 | |
| 484 | return i; |
| 485 | } |
| 486 | |
| 487 | char *GotoScriptInText(char *text, const char *script) { |
| 488 | char word[256]; |
| 489 | bool inword; |
| 490 | int i, index, line; |
| 491 | int state; // 0 = nothing, 1 = typeword, 2 = scriptword |
| 492 | int strsize = strlen(text); |
| 493 | |
| 494 | inword = false; |
| 495 | index = 0; |
| 496 | state = 0; |
| 497 | line = 1; |
| 498 | |
| 499 | for (i = 0; i < strsize; i++) { |
| 500 | char ch; |
| 501 | |
| 502 | ch = text[i]; |
| 503 | |
| 504 | if (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n' && ch != '(') { |
| 505 | // no whitespace |
| 506 | if (!inword) |
| 507 | inword = true; |
| 508 | if (inword) |
| 509 | word[index++] = ch; |
| 510 | } else { |
| 511 | // whitespace |
| 512 | if (ch == '\n') |
| 513 | line++; |
| 514 | if (inword) { |
| 515 | // word is complete, check if a keyword |
| 516 | word[index] = 0; |
| 517 | if (!strcmp(word, "trigger") || !strcmp(word, "object") || !strcmp(word, "level")) { |
| 518 | if (state == 0) |
| 519 | state = 1; |
| 520 | else |
| 521 | state = 0; // type word out of sequence |
| 522 | } else if (!strcmp(word, "script")) { |
| 523 | if (state == 1) |
| 524 | state = 2; |
| 525 | else |
| 526 | state = 0; |
| 527 | } else { |
| 528 | // if we are in state 2, then we have PROBABLY FOUND a script name |
| 529 | if (state == 2) { |
| 530 | if (!strcmp(word, script)) |
| 531 | return &text[i]; |
| 532 | else |
| 533 | state = 0; |
| 534 | } else |
| 535 | state = 0; |
| 536 | } |
| 537 | index = 0; |
| 538 | inword = false; |
| 539 | } |
| 540 | } |
| 541 | } |
| 542 | |
| 543 | return NULL; |
| 544 | } |
| 545 | |
| 546 | // These functions manage enumeration of all script files located in the script directory |
| 547 | static int hGameFile = -1, hInitialFile = -1; |
| 548 | static char TempStr[256]; |
| 549 | |
| 550 | char *StartScriptFileList() { |
| 551 | char path[_MAX_PATH]; |
| 552 | ddio_MakePath(path, LocalLevelsDir, "*.scr", NULL); |
| 553 | if (!ddio_FindFileStart(path, TempStr)) |
| 554 | return NULL; |
| 555 | |
| 556 | return TempStr; |
| 557 | } |
| 558 | |
| 559 | char *GetNextScriptFile() { |
| 560 | if (!ddio_FindNextFile(TempStr)) |
| 561 | return NULL; |
| 562 | return TempStr; |
| 563 | } |
| 564 | |
| 565 | void EndScriptFileList() { ddio_FindFileClose(); } |
| 566 | |
| 567 | ////////////////////////////////////////////////////////////////////////////// |
| 568 | // Script source management |
| 569 | |
| 570 | int FindScriptIDFromName(const char *name) { |
| 571 | int i; |
| 572 | |
| 573 | for (i = 0; i < MAX_SCRIPTS; i++) |
| 574 | if (Script_names[i].used && strcmp(name, Script_names[i].name) == 0) |
| 575 | return i; |
| 576 | |
| 577 | return -1; |
| 578 | } |
| 579 | |
| 580 | char *FindEventNameFromID(int id) { |
| 581 | #if 0 // LGT: MAX_SCREVTS undefined |
| 582 | for (int i = 0; i < MAX_SCREVTS; i++) |
| 583 | if (id == Script_evt_ids[i]) return Script_evt_names[i]; |
| 584 | #endif |
| 585 | return NULL; |
| 586 | } |
| 587 | |
| 588 | int FindEventIDFromName(const char *name) { |
| 589 | #if 0 // LGT: MAX_SCREVTS undefined |
| 590 | for (int i = 0; i < MAX_SCREVTS; i++) |
| 591 | if (strcmp(name, Script_evt_names[i]) == 0) return Script_evt_ids[i]; |
| 592 | #endif |
| 593 | return 0; |
| 594 | } |
| 595 | |
| 596 | // these functions add ScriptWizard formatting |
| 597 | |
| 598 | #define SCRHDR_FORMAT "%s script %s()" |
| 599 | #define SCRIPT_FORMAT "// %s\r\n//\t<comments here>\r\n%s\r\n\r\n\r\nendscript\r\n\r\n\r\n" |
| 600 | |
| 601 | char *AddScriptBlockToScript(char *script, const char *newname, const char *type_str) { |
| 602 | char *newsrc, *strline; |
| 603 | char newhdr[128]; |
| 604 | char newblk[384]; |
| 605 | int slot, line = 0, type = 0; |
| 606 | |
| 607 | strline = script; |
| 608 | while (strline) { |
| 609 | line++; |
| 610 | strline = strchr(strline, '\n'); |
| 611 | if (strline) |
| 612 | strline++; |
| 613 | } |
| 614 | if (!strcmp(type_str, "object")) |
| 615 | type = REF_OBJTYPE; |
| 616 | else if (!strcmp(type_str, "trigger")) |
| 617 | type = REF_TRIGTYPE; |
| 618 | else if (!strcmp(type_str, "level")) |
| 619 | type = REF_LEVELTYPE; |
| 620 | |
| 621 | sprintf(newhdr, SCRHDR_FORMAT, type_str, newname); |
| 622 | sprintf(newblk, SCRIPT_FORMAT, newname, newhdr); |
| 623 | |
| 624 | newsrc = (char *)mem_malloc(strlen(script) + strlen(newblk) + 1); |
| 625 | if (!newsrc) { |
| 626 | mprintf(1, "Allocation failure in creating new script buffer.\n"); |
| 627 | return NULL; |
| 628 | } |
| 629 | |
| 630 | sprintf(newsrc, "%s%s", script, newblk); |
| 631 | slot = AddScriptToList((char *)newname, type, false); |
| 632 | Script_names[slot].line = line; |
| 633 | |
| 634 | return newsrc; |
| 635 | } |
| 636 | |
| 637 | #define EVT_STATEMENT "\r\n\r\nevent '%s'\r\n//insert code here\r\ncomplete\r\n" |
| 638 | |
| 639 | char *AddEventBlockToScript(char *script, const char *evtname, const char *scriptname) { |
| 640 | // we must first find the requested script, then add the event BEFORE the next 'endscript' |
| 641 | char *txtstart, *newtxt; |
| 642 | char *preword; |
| 643 | char word[256]; |
| 644 | bool inword; |
| 645 | int i, index; |
| 646 | int strsize; |
| 647 | |
| 648 | inword = false; |
| 649 | index = 0; |
| 650 | |
| 651 | txtstart = GotoScriptInText(script, scriptname); |
| 652 | if (!txtstart) |
| 653 | return NULL; |
| 654 | |
| 655 | preword = txtstart; |
| 656 | strsize = strlen(script); |
| 657 | |
| 658 | for (i = 0; i < strsize; i++) { |
| 659 | char ch; |
| 660 | |
| 661 | ch = txtstart[i]; |
| 662 | |
| 663 | if (ch != ' ' && ch != '\t' && ch != '\r' && ch != '\n') { |
| 664 | // no whitespace |
| 665 | if (!inword) |
| 666 | inword = true; |
| 667 | if (inword) |
| 668 | word[index++] = ch; |
| 669 | } else { |
| 670 | // whitespace |
| 671 | if (inword) { |
| 672 | // word is complete, check if a keyword, save location of word if not a keyword |
| 673 | word[index] = 0; |
| 674 | if (!strcmp(word, "endscript")) { |
| 675 | // okay, we can now add the event block at preword. |
| 676 | int prelen = (int)(preword - script); |
| 677 | int postlen = (int)strlen(script) - prelen; |
| 678 | char *preblk = new char[prelen + 1]; |
| 679 | char *postblk = new char[postlen + 1]; |
| 680 | char evtblk[256]; |
| 681 | |
| 682 | strncpy(preblk, script, prelen); |
| 683 | preblk[prelen] = 0; |
| 684 | strncpy(postblk, preword, postlen); |
| 685 | postblk[postlen] = 0; |
| 686 | sprintf(evtblk, EVT_STATEMENT, evtname); |
| 687 | |
| 688 | newtxt = (char *)mem_malloc(strlen(preblk) + strlen(postblk) + strlen(evtblk) + 1); |
| 689 | sprintf(newtxt, "%s%s%s", preblk, evtblk, postblk); |
| 690 | |
| 691 | delete[] postblk; |
| 692 | delete[] preblk; |
| 693 | |
| 694 | return newtxt; |
| 695 | } else |
| 696 | preword = &txtstart[i]; |
| 697 | |
| 698 | index = 0; |
| 699 | inword = false; |
| 700 | } |
| 701 | } |
| 702 | } |
| 703 | |
| 704 | return NULL; |
| 705 | } |
| 706 | |
| 707 | // generates script list from all script files. |
| 708 | void GenerateScriptListFromAllFiles(int mask) { |
| 709 | char *filename; |
| 710 | //@@ tD3XProgram *m_ScriptCode; |
| 711 | |
| 712 | //@@ m_ScriptCode = D3XReallocProgram(NULL, 0, 0, 0); |
| 713 | |
| 714 | // initialize script list. |
| 715 | ResetScriptList(); |
| 716 | |
| 717 | // compile all script files and place into script list. |
| 718 | filename = StartScriptFileList(); |
| 719 | while (filename) { |
| 720 | // compile script first. if we failed, then display a messagebox giving the warning |
| 721 | if (!stricmp(filename, DEFAULT_SCRIPT_NAME) && (mask & DEFAULT_SCRIPT_MASK)) |
| 722 | GenerateScriptListFromFile(filename); |
| 723 | else if ((mask & CUSTOM_SCRIPT_MASK) && stricmp(filename, DEFAULT_SCRIPT_NAME)) |
| 724 | GenerateScriptListFromFile(filename); |
| 725 | |
| 726 | filename = GetNextScriptFile(); |
| 727 | } |
| 728 | EndScriptFileList(); |
| 729 | |
| 730 | //@@ D3XFreeProgram(m_ScriptCode); |
| 731 | } |
| 732 | |
| 733 | // generates script list from one file |
| 734 | void GenerateScriptListFromFile(const char *fname) { |
| 735 | char *script; |
| 736 | script = LoadScript(fname); |
| 737 | if (script) { |
| 738 | if (stricmp(fname, DEFAULT_SCRIPT_NAME) == 0) |
| 739 | GenerateScriptWizardInfo(script, false); |
| 740 | else |
| 741 | GenerateScriptWizardInfo(script, true); |
| 742 | |
| 743 | FreeScript(script); |
| 744 | } |
| 745 | } |
| 746 | |
| 747 | //@@ |
| 748 | //@@// **************************************************************************** |
| 749 | //@@// INTERNAL FUNCTIONS |
| 750 | //@@// **************************************************************************** |
| 751 | //@@ |
| 752 | //@@// function to find first free slot in default script list. |
| 753 | int FindFreeScriptSlot() { |
| 754 | int i; |
| 755 | for (i = 0; i < MAX_SCRIPTS; i++) |
| 756 | if (!Script_names[i].used) |
| 757 | return i; |
| 758 | |
| 759 | return -1; |
| 760 | } |
| 761 | |
| 762 | // other stuff |
| 763 | void tScriptName::add_parameter(const char *type, const char *parm) { |
| 764 | ASSERT(nparms < MAX_SCRPARAMS); |
| 765 | |
| 766 | paramtypes[nparms] = new char[strlen(type) + 1]; |
| 767 | parameters[nparms] = new char[strlen(parm) + 1]; |
| 768 | strcpy(parameters[nparms], parm); |
| 769 | strcpy(paramtypes[nparms], type); |
| 770 | nparms++; |
| 771 | } |
| 772 | |
| 773 | void tScriptName::free_parameters() { |
| 774 | for (int i = 0; i < nparms; i++) { |
| 775 | if (parameters[i]) { |
| 776 | delete[] (parameters[i]); |
| 777 | parameters[i] = NULL; |
| 778 | } |
| 779 | if (paramtypes[i]) { |
| 780 | delete[] (paramtypes[i]); |
| 781 | paramtypes[i] = NULL; |
| 782 | } |
| 783 | } |
| 784 | nparms = 0; |
| 785 | } |