Debian Codegraph @

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