Debian Codegraph @

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