Debian Codegraph @

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