Debian Codegraph @

scripts/CanyonsCTF.cpp

raw symbols SCIP
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
19 /////////////////////////////////////////////////////////////////////
20 // D.A.L.L.A.S. Generated Level Script - DLL Source File
21 //
22 // Filename: CanyonsCTF.cpp
23 // Version: 3
24 /////////////////////////////////////////////////////////////////////
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <ctype.h>
29 #include "osiris_import.h"
30 #include "osiris_common.h"
31 #include "DallasFuncs.cpp"
32
33 #include "module.h"
34
35 #ifdef __cplusplus
36 extern "C" {
37 #endif
38 DLLEXPORT char STDCALL InitializeDLL(tOSIRISModuleInit *func_list);
39 DLLEXPORT void STDCALL ShutdownDLL(void);
40 DLLEXPORT int STDCALL GetGOScriptID(const char *name, uint8_t is_door);
41 DLLEXPORT void STDCALLPTR CreateInstance(int id);
42 DLLEXPORT void STDCALL DestroyInstance(int id, void *ptr);
43 DLLEXPORT int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data);
44 DLLEXPORT int STDCALL GetTriggerScriptID(int trigger_room, int trigger_face);
45 DLLEXPORT int STDCALL GetCOScriptList(int **list, int **id_list);
46 DLLEXPORT int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state);
47 #ifdef __cplusplus
48 }
49 #endif
50
51 // =================
52 // Script ID Numbers
53 // =================
54 #define ID_LEVEL_0000 0x000
55
56 // ========================
57 // Script Class Definitions
58 // ========================
59
60 class BaseScript {
61 public:
62 BaseScript();
63 ~BaseScript();
64 virtual int16_t CallEvent(int event, tOSIRISEventInfo *data);
65 };
66
67 class LevelScript_0000 : public BaseScript {
68 public:
69 int16_t CallEvent(int event, tOSIRISEventInfo *data);
70 };
71
72 // ======================
73 // Global Action Counters
74 // ======================
75
76 #define MAX_ACTION_CTR_VALUE 100000
77
78 // ========================================
79 // Function to Clear Global Action Counters
80 // ========================================
81 void ClearGlobalActionCtrs(void) {}
82
83 // ========================================
84 // Function to Save Global Action Counters
85 // ========================================
86 void SaveGlobalActionCtrs(void *file_ptr) {}
87
88 // ===========================================
89 // Function to Restore Global Action Counters
90 // ===========================================
91 void RestoreGlobalActionCtrs(void *file_ptr) {}
92
93 // ===============================================================
94 // Start of Custom Script Block - DO NOT EDIT ANYTHING BEFORE THIS
95 // ===============================================================
96 /**{CUSTOM_SCRIPT_BLOCK_START}** DO NOT EDIT! **/
97
98 // Enter your custom script code here
99
100 /**{CUSTOM_SCRIPT_BLOCK_END}**** DO NOT EDIT! **/
101 // ============================================================
102 // End of Custom Script Block - DO NOT EDIT ANYTHING AFTER THIS
103 // ============================================================
104
105 // =================
106 // Message File Data
107 // =================
108
109 #define MAX_SCRIPT_MESSAGES 256
110 #define MAX_MSG_FILEBUF_LEN 1024
111 #define NO_MESSAGE_STRING "*Message Not Found*"
112 #define INV_MSGNAME_STRING "*Message Name Invalid*"
113 #define WHITESPACE_CHARS " \t\r\n"
114
115 // Structure for storing a script message
116 struct tScriptMessage {
117 char *name; // the name of the message
118 char *message; // the actual message text
119 };
120
121 // Global storage for level script messages
122 tScriptMessage *message_list[MAX_SCRIPT_MESSAGES];
123 int num_messages;
124
125 // ======================
126 // Message File Functions
127 // ======================
128
129 // Initializes the Message List
130 void InitMessageList(void) {
131 for (int j = 0; j < MAX_SCRIPT_MESSAGES; j++)
132 message_list[j] = NULL;
133 num_messages = 0;
134 }
135
136 // Clear the Message List
137 void ClearMessageList(void) {
138 for (int j = 0; j < num_messages; j++) {
139 free(message_list[j]->name);
140 free(message_list[j]->message);
141 free(message_list[j]);
142 message_list[j] = NULL;
143 }
144 num_messages = 0;
145 }
146
147 // Adds a message to the list
148 int AddMessageToList(char *name, char *msg) {
149 int pos;
150
151 // Make sure there is room in the list
152 if (num_messages >= MAX_SCRIPT_MESSAGES)
153 return false;
154
155 // Allocate memory for this message entry
156 pos = num_messages;
157 message_list[pos] = (tScriptMessage *)malloc(sizeof(tScriptMessage));
158 if (message_list[pos] == NULL)
159 return false;
160
161 // Allocate memory for the message name
162 message_list[pos]->name = (char *)malloc(strlen(name) + 1);
163 if (message_list[pos]->name == NULL) {
164 free(message_list[pos]);
165 return false;
166 }
167 strcpy(message_list[pos]->name, name);
168
169 // Allocate memory for the message name
170 message_list[pos]->message = (char *)malloc(strlen(msg) + 1);
171 if (message_list[pos]->message == NULL) {
172 free(message_list[pos]->name);
173 free(message_list[pos]);
174 return false;
175 }
176 strcpy(message_list[pos]->message, msg);
177 num_messages++;
178
179 return true;
180 }
181
182 // Removes any whitespace padding from the end of a string
183 void RemoveTrailingWhitespace(char *s) {
184 int last_char_pos;
185
186 last_char_pos = strlen(s) - 1;
187 while (last_char_pos >= 0 && isspace(s[last_char_pos])) {
188 s[last_char_pos] = '\0';
189 last_char_pos--;
190 }
191 }
192
193 // Returns a pointer to the first non-whitespace char in given string
194 char *SkipInitialWhitespace(char *s) {
195 while ((*s) != '\0' && isspace(*s))
196 s++;
197
198 return (s);
199 }
200
201 // Read in the Messages
202 int ReadMessageFile(const char *filename) {
203 void *infile;
204 char filebuffer[MAX_MSG_FILEBUF_LEN + 1];
205 char *line, *msg_start;
206 int line_num;
207 bool next_msgid_found;
208
209 // Try to open the file for loading
210 infile = File_Open(filename, "rt");
211 if (!infile)
212 return false;
213
214 line_num = 0;
215 next_msgid_found = true;
216
217 // Clear the message list
218 ClearMessageList();
219
220 // Read in and parse each line of the file
221 while (!File_eof(infile)) {
222
223 // Clear the buffer
224 strcpy(filebuffer, "");
225
226 // Read in a line from the file
227 File_ReadString(filebuffer, MAX_MSG_FILEBUF_LEN, infile);
228 line_num++;
229
230 // Remove whitespace padding at start and end of line
231 RemoveTrailingWhitespace(filebuffer);
232 line = SkipInitialWhitespace(filebuffer);
233
234 // If line is a comment, or empty, discard it
235 if (strlen(line) == 0 || strncmp(line, "//", 2) == 0)
236 continue;
237
238 if (!next_msgid_found) { // Parse out the last message ID number
239
240 // Grab the first keyword, make sure it's valid
241 line = strtok(line, WHITESPACE_CHARS);
242 if (line == NULL)
243 continue;
244
245 // Grab the second keyword, and assign it as the next message ID
246 line = strtok(NULL, WHITESPACE_CHARS);
247 if (line == NULL)
248 continue;
249
250 next_msgid_found = true;
251 } else { // Parse line as a message line
252
253 // Find the start of message, and mark it
254 msg_start = strchr(line, '=');
255 if (msg_start == NULL)
256 continue;
257 msg_start[0] = '\0';
258 msg_start++;
259
260 // Add the message to the list
261 AddMessageToList(line, msg_start);
262 }
263 }
264 File_Close(infile);
265
266 return true;
267 }
268
269 // Find a message
270 const char *GetMessage(const char *name) {
271 // Make sure given name is valid
272 if (name == NULL)
273 return INV_MSGNAME_STRING;
274
275 // Search message list for name
276 for (int j = 0; j < num_messages; j++)
277 if (strcmp(message_list[j]->name, name) == 0)
278 return (message_list[j]->message);
279
280 // Couldn't find it
281 return NO_MESSAGE_STRING;
282 }
283
284 //======================
285 // Name List Arrays
286 //======================
287
288 #define NUM_DOOR_NAMES 0
289 const char **Door_names = NULL;
290 int *Door_handles = NULL;
291
292 #define NUM_OBJECT_NAMES 0
293 const char **Object_names = NULL;
294 int *Object_handles = NULL;
295
296 #define NUM_ROOM_NAMES 0
297 const char **Room_names = NULL;
298 int *Room_indexes = NULL;
299
300 #define NUM_TRIGGER_NAMES 0
301 const char **Trigger_names = NULL;
302 int *Trigger_indexes = NULL;
303 int *Trigger_faces = NULL;
304 int *Trigger_rooms = NULL;
305
306 #define NUM_SOUND_NAMES 0
307 const char **Sound_names = NULL;
308 int *Sound_indexes = NULL;
309
310 #define NUM_TEXTURE_NAMES 0
311 const char **Texture_names = NULL;
312 int *Texture_indexes = NULL;
313
314 #define NUM_PATH_NAMES 0
315 const char **Path_names = NULL;
316 int *Path_indexes = NULL;
317
318 #define NUM_MATCEN_NAMES 0
319 const char **Matcen_names = NULL;
320 int *Matcen_indexes = NULL;
321
322 #define NUM_GOAL_NAMES 0
323 const char **Goal_names = NULL;
324 int *Goal_indexes = NULL;
325
326 #define NUM_MESSAGE_NAMES 0
327 const char **Message_names = NULL;
328 const char **Message_strings = NULL;
329
330 // ===============
331 // InitializeDLL()
332 // ===============
333 char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) {
334 osicommon_Initialize((tOSIRISModuleInit *)func_list);
335 if (func_list->game_checksum != CHECKSUM) {
336 mprintf(0, "Game-Checksum FAIL!!! (%ul!=%ul)\n", func_list->game_checksum, CHECKSUM);
337 mprintf(0, "RECOMPILE YOUR SCRIPTS!!!\n");
338 return 0;
339 }
340
341 ClearGlobalActionCtrs();
342 dfInit();
343 InitMessageList();
344
345 // Build the filename of the message file
346 char filename[_MAX_PATH + 32];
347 int lang_type;
348 if (func_list->script_identifier != NULL) {
349 _splitpath(func_list->script_identifier, NULL, NULL, filename, NULL);
350 lang_type = Game_GetLanguage();
351 if (lang_type == LANGUAGE_FRENCH)
352 strcat(filename, "_FRN");
353 else if (lang_type == LANGUAGE_GERMAN)
354 strcat(filename, "_GER");
355 else if (lang_type == LANGUAGE_ITALIAN)
356 strcat(filename, "_ITN");
357 else if (lang_type == LANGUAGE_SPANISH)
358 strcat(filename, "_SPN");
359 strcat(filename, ".msg");
360 } else {
361 strcpy(filename, "CanyonsCTF.msg");
362 lang_type = LANGUAGE_ENGLISH;
363 }
364 if (!ReadMessageFile(filename)) {
365 mprintf(0, "ERROR: Could not load message file - %s\n", filename);
366 }
367
368 int j;
369
370 // Do Door Index lookups
371 for (j = 0; j < NUM_DOOR_NAMES; j++)
372 Door_handles[j] = Scrpt_FindDoorName(Door_names[j]);
373
374 // Do Object Index lookups
375 for (j = 0; j < NUM_OBJECT_NAMES; j++)
376 Object_handles[j] = Scrpt_FindObjectName(Object_names[j]);
377
378 // Do Room Index lookups
379 for (j = 0; j < NUM_ROOM_NAMES; j++)
380 Room_indexes[j] = Scrpt_FindRoomName(Room_names[j]);
381
382 // Do Trigger Index lookups
383 for (j = 0; j < NUM_TRIGGER_NAMES; j++) {
384 Trigger_indexes[j] = Scrpt_FindTriggerName(Trigger_names[j]);
385 Trigger_faces[j] = Scrpt_GetTriggerFace(Trigger_indexes[j]);
386 Trigger_rooms[j] = Scrpt_GetTriggerRoom(Trigger_indexes[j]);
387 }
388
389 // Do Sound Index lookups
390 for (j = 0; j < NUM_SOUND_NAMES; j++)
391 Sound_indexes[j] = Scrpt_FindSoundName(Sound_names[j]);
392
393 // Do Texture Index lookups
394 for (j = 0; j < NUM_TEXTURE_NAMES; j++)
395 Texture_indexes[j] = Scrpt_FindTextureName(Texture_names[j]);
396
397 // Do Path Index lookups
398 for (j = 0; j < NUM_PATH_NAMES; j++)
399 Path_indexes[j] = Scrpt_FindPathName(Path_names[j]);
400
401 // Do Matcen Index lookups
402 for (j = 0; j < NUM_MATCEN_NAMES; j++)
403 Matcen_indexes[j] = Scrpt_FindMatcenName(Matcen_names[j]);
404
405 // Do Goal Index lookups
406 for (j = 0; j < NUM_GOAL_NAMES; j++)
407 Goal_indexes[j] = Scrpt_FindLevelGoalName(Goal_names[j]);
408
409 // Do Message Name lookups
410 for (j = 0; j < NUM_MESSAGE_NAMES; j++)
411 Message_strings[j] = GetMessage(Message_names[j]);
412
413 return 1;
414 }
415
416 // =============
417 // ShutdownDLL()
418 // =============
419 void STDCALL ShutdownDLL(void) { ClearMessageList(); }
420
421 // ===============
422 // GetGOScriptID()
423 // ===============
424 int STDCALL GetGOScriptID(const char *name, uint8_t isdoor) { return -1; }
425
426 // ================
427 // CreateInstance()
428 // ================
429 void STDCALLPTR CreateInstance(int id) {
430 switch (id) {
431 case ID_LEVEL_0000:
432 return new LevelScript_0000;
433 break;
434 default:
435 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
436 break;
437 }
438 return NULL;
439 }
440
441 // =================
442 // DestroyInstance()
443 // =================
444 void STDCALL DestroyInstance(int id, void *ptr) {
445 switch (id) {
446 case ID_LEVEL_0000:
447 delete ((LevelScript_0000 *)ptr);
448 default:
449 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
450 break;
451 }
452 }
453
454 // ===================
455 // CallInstanceEvent()
456 // ===================
457 int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data) {
458 switch (id) {
459 case ID_LEVEL_0000:
460 return ((BaseScript *)ptr)->CallEvent(event, data);
461 break;
462 default:
463 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
464 break;
465 }
466 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
467 }
468
469 // ==================
470 // SaveRestoreState()
471 // ==================
472 int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state) { return 0; }
473
474 // ====================
475 // GetTriggerScriptID()
476 // ====================
477 int STDCALL GetTriggerScriptID(int trigger_room, int trigger_face) { return -1; }
478
479 // =================
480 // GetCOScriptList()
481 // =================
482 int STDCALL GetCOScriptList(int **list, int **id_list) {
483 static int *cust_handle_list = NULL;
484 static int *cust_id_list = NULL;
485
486 *list = cust_handle_list;
487 *id_list = cust_id_list;
488
489 return 0;
490 }
491
492 //=======================
493 // Script Implementation
494 //=======================
495
496 BaseScript::BaseScript() {}
497
498 BaseScript::~BaseScript() {}
499
500 int16_t BaseScript::CallEvent(int event, tOSIRISEventInfo *data) {
501 mprintf(0, "BaseScript::CallEvent()\n");
502 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
503 }
504
505 int16_t LevelScript_0000::CallEvent(int event, tOSIRISEventInfo *data) {
506 switch (event) {
507 case EVT_SAVESTATE: {
508 tOSIRISEVTSAVESTATE *event_data = &data->evt_savestate;
509
510 SaveGlobalActionCtrs(event_data->fileptr);
511 dfSave(event_data->fileptr);
512 #ifdef ENABLE_CUSTOM_SAVE_AND_RESTORE
513 dsCustomSave(event_data->fileptr);
514 #endif
515 } break;
516 case EVT_RESTORESTATE: {
517 tOSIRISEVTRESTORESTATE *event_data = &data->evt_restorestate;
518
519 RestoreGlobalActionCtrs(event_data->fileptr);
520 dfRestore(event_data->fileptr);
521 #ifdef ENABLE_CUSTOM_SAVE_AND_RESTORE
522 dsCustomRestore(event_data->fileptr);
523 #endif
524 } break;
525 case EVT_LEVELSTART: {
526 tOSIRISEVTLEVELSTART *event_data = &data->evt_levelstart;
527
528 ClearGlobalActionCtrs();
529 dfInit();
530 } break;
531 }
532 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
533 }
534
535 /*********************************************************
536 Script Save Block: DO NOT TOUCH ANYTHING IN THIS BLOCK!!!
537 **********************************************************
538 $$SCRIPT_BLOCK_START
539
540 VERSION 3
541 NEXT_ID 1
542
543 // UserType value blocks
544 $$UTYPE_VALS_START UserVar
545 $$UTYPE_VALS_END
546
547 $$UTYPE_VALS_START UserFlag
548 $$UTYPE_VALS_END
549
550 $$UTYPE_VALS_START SpewHandle
551 $$UTYPE_VALS_END
552
553 $$UTYPE_VALS_START TimerID
554 $$UTYPE_VALS_END
555
556 $$UTYPE_VALS_START SavedObjectSlot
557 $$UTYPE_VALS_END
558
559 $$UTYPE_VALS_START GoalID
560 $$UTYPE_VALS_END
561
562
563 // Name Lists
564 $$DOOR_LIST_START
565 $$DOOR_LIST_END
566
567 $$OBJECT_LIST_START
568 $$OBJECT_LIST_END
569
570 $$ROOM_LIST_START
571 $$ROOM_LIST_END
572
573 $$TRIGGER_LIST_START
574 $$TRIGGER_LIST_END
575
576 $$SOUND_LIST_START
577 $$SOUND_LIST_END
578
579 $$TEXTURE_LIST_START
580 $$TEXTURE_LIST_END
581
582 $$SPECNAME_LIST_START
583 $$SPECNAME_LIST_END
584
585 $$PATH_LIST_START
586 $$PATH_LIST_END
587
588 $$MATCEN_LIST_START
589 $$MATCEN_LIST_END
590
591 $$GOAL_LIST_START
592 $$GOAL_LIST_END
593
594 $$STRM_AUDIO_LIST_START
595 $$STRM_AUDIO_LIST_END
596
597
598 // Script Tree Dump
599
600 $$SCRIPT_BLOCK_END
601 *********************************************************/