Debian Codegraph @

scripts/Paranoia.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: paranoia.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 0
295 const char **Object_names = NULL;
296 int *Object_handles = NULL;
297
298 #define NUM_ROOM_NAMES 4
299 const char *Room_names[NUM_ROOM_NAMES] = {"BlueUp", "BlueDown", "RedDown", "RedUp"};
300 int Room_indexes[NUM_ROOM_NAMES];
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 0
309 const char **Sound_names = NULL;
310 int *Sound_indexes = NULL;
311
312 #define NUM_TEXTURE_NAMES 0
313 const char **Texture_names = NULL;
314 int *Texture_indexes = NULL;
315
316 #define NUM_MESSAGE_NAMES 0
317 const char **Message_names = NULL;
318 const char **Message_strings = NULL;
319
320 // ===============
321 // InitializeDLL()
322 // ===============
323 char STDCALL InitializeDLL(tOSIRISModuleInit *func_list) {
324 osicommon_Initialize((tOSIRISModuleInit *)func_list);
325 if (func_list->game_checksum != CHECKSUM) {
326 mprintf(0, "Game-Checksum FAIL!!! (%ul!=%ul)\n", func_list->game_checksum, CHECKSUM);
327 mprintf(0, "RECOMPILE YOUR SCRIPTS!!!\n");
328 return 0;
329 }
330
331 ClearGlobalActionCtrs();
332 dfInit();
333 InitMessageList();
334 ReadMessageFile("paranoia.msg");
335
336 int j;
337
338 // Do Door Index lookups
339 for (j = 0; j < NUM_DOOR_NAMES; j++)
340 Door_handles[j] = Scrpt_FindDoorName(Door_names[j]);
341
342 // Do Object Index lookups
343 for (j = 0; j < NUM_OBJECT_NAMES; j++)
344 Object_handles[j] = Scrpt_FindObjectName(Object_names[j]);
345
346 // Do Room Index lookups
347 for (j = 0; j < NUM_ROOM_NAMES; j++)
348 Room_indexes[j] = Scrpt_FindRoomName(Room_names[j]);
349
350 // Do Trigger Index lookups
351 for (j = 0; j < NUM_TRIGGER_NAMES; j++) {
352 Trigger_indexes[j] = Scrpt_FindTriggerName(Trigger_names[j]);
353 Trigger_faces[j] = Scrpt_GetTriggerFace(Trigger_indexes[j]);
354 Trigger_rooms[j] = Scrpt_GetTriggerRoom(Trigger_indexes[j]);
355 }
356
357 // Do Sound Index lookups
358 for (j = 0; j < NUM_SOUND_NAMES; j++)
359 Sound_indexes[j] = Scrpt_FindSoundName(Sound_names[j]);
360
361 // Do Texture Index lookups
362 for (j = 0; j < NUM_TEXTURE_NAMES; j++)
363 Texture_indexes[j] = Scrpt_FindTextureName(Texture_names[j]);
364
365 // Do Message Name lookups
366 for (j = 0; j < NUM_MESSAGE_NAMES; j++)
367 Message_strings[j] = GetMessage(Message_names[j]);
368
369 return 1;
370 }
371
372 // =============
373 // ShutdownDLL()
374 // =============
375 void STDCALL ShutdownDLL(void) { ClearMessageList(); }
376
377 // ===============
378 // GetGOScriptID()
379 // ===============
380 int STDCALL GetGOScriptID(const char *name, uint8_t isdoor) { return -1; }
381
382 // ================
383 // CreateInstance()
384 // ================
385 void STDCALLPTR CreateInstance(int id) {
386 switch (id) {
387 case ID_LEVEL_0000:
388 return new LevelScript_0000;
389 break;
390 default:
391 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
392 break;
393 }
394 return NULL;
395 }
396
397 // =================
398 // DestroyInstance()
399 // =================
400 void STDCALL DestroyInstance(int id, void *ptr) {
401 switch (id) {
402 case ID_LEVEL_0000:
403 delete ((LevelScript_0000 *)ptr);
404 default:
405 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
406 break;
407 }
408 }
409
410 // ===================
411 // CallInstanceEvent()
412 // ===================
413 int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data) {
414 switch (id) {
415 case ID_LEVEL_0000:
416 return ((BaseScript *)ptr)->CallEvent(event, data);
417 break;
418 default:
419 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
420 break;
421 }
422 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
423 }
424
425 // ==================
426 // SaveRestoreState()
427 // ==================
428 int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state) { return 0; }
429
430 // ====================
431 // GetTriggerScriptID()
432 // ====================
433 int STDCALL GetTriggerScriptID(int trigger_room, int trigger_face) { return -1; }
434
435 // =================
436 // GetCOScriptList()
437 // =================
438 int STDCALL GetCOScriptList(int **list, int **id_list) {
439 static int *cust_handle_list = NULL;
440 static int *cust_id_list = NULL;
441
442 *list = cust_handle_list;
443 *id_list = cust_id_list;
444
445 return 0;
446 }
447
448 //=======================
449 // Script Implementation
450 //=======================
451
452 BaseScript::BaseScript() {}
453
454 BaseScript::~BaseScript() {}
455
456 int16_t BaseScript::CallEvent(int event, tOSIRISEventInfo *data) {
457 mprintf(0, "BaseScript::CallEvent()\n");
458 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
459 }
460
461 int16_t LevelScript_0000::CallEvent(int event, tOSIRISEventInfo *data) {
462 switch (event) {
463 case EVT_SAVESTATE: {
464 tOSIRISEVTSAVESTATE *event_data = &data->evt_savestate;
465
466 SaveGlobalActionCtrs(event_data->fileptr);
467 dfSave(event_data->fileptr);
468 } break;
469 case EVT_RESTORESTATE: {
470 tOSIRISEVTRESTORESTATE *event_data = &data->evt_restorestate;
471
472 RestoreGlobalActionCtrs(event_data->fileptr);
473 dfRestore(event_data->fileptr);
474 } break;
475 case EVT_LEVELSTART: {
476 tOSIRISEVTLEVELSTART *event_data = &data->evt_levelstart;
477
478 ClearGlobalActionCtrs();
479 dfInit();
480
481 // Script 000: Wind!
482 if (1) {
483 aRoomSetWind(Room_indexes[0], 0.000000f, 10.000000f, 0.000000f, 1.000000f);
484 aRoomSetWind(Room_indexes[1], 0.000000f, -10.000000f, 0.000000f, 1.000000f);
485 aRoomSetWind(Room_indexes[2], 0.000000f, -10.000000f, 0.000000f, 1.000000f);
486 aRoomSetWind(Room_indexes[3], 0.000000f, 10.000000f, 0.000000f, 1.000000f);
487
488 // Increment the script action counter
489 if (ScriptActionCtr_000 < MAX_ACTION_CTR_VALUE)
490 ScriptActionCtr_000++;
491 }
492 } break;
493 }
494 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
495 }
496
497 /*********************************************************
498 Script Save Block: DO NOT TOUCH ANYTHING IN THIS BLOCK!!!
499 **********************************************************
500 $$SCRIPT_BLOCK_START
501
502 VERSION 3
503 NEXT_ID 1
504
505 // UserType value blocks
506 $$UTYPE_VALS_START UserVar
507 $$UTYPE_VALS_END
508
509 $$UTYPE_VALS_START UserFlag
510 $$UTYPE_VALS_END
511
512 $$UTYPE_VALS_START SpewHandle
513 $$UTYPE_VALS_END
514
515 $$UTYPE_VALS_START TimerID
516 $$UTYPE_VALS_END
517
518 $$UTYPE_VALS_START SavedObjectSlot
519 $$UTYPE_VALS_END
520
521 $$UTYPE_VALS_START GoalID
522 $$UTYPE_VALS_END
523
524
525 // Name Lists
526 $$DOOR_LIST_START
527 $$DOOR_LIST_END
528
529 $$OBJECT_LIST_START
530 $$OBJECT_LIST_END
531
532 $$ROOM_LIST_START
533 BlueUp
534 BlueDown
535 RedDown
536 RedUp
537 $$ROOM_LIST_END
538
539 $$TRIGGER_LIST_START
540 $$TRIGGER_LIST_END
541
542 $$SOUND_LIST_START
543 $$SOUND_LIST_END
544
545 $$TEXTURE_LIST_START
546 $$TEXTURE_LIST_END
547
548 $$SPECNAME_LIST_START
549 $$SPECNAME_LIST_END
550
551
552 // Script Tree Dump
553 00:0:Wind!
554 $$CHILD_BLOCK_START
555 01:2:0
556 02:6
557 03:0
558 $$CHILD_BLOCK_START
559 05:0
560 $$CHILD_BLOCK_END
561 04:0:43246756:0
562 $$CHILD_BLOCK_START
563 08:aRoomSetWind
564 $$CHILD_BLOCK_START
565 10:2:0:Room
566 10:6:0.000000:X
567 10:6:10.000000:Y
568 10:6:0.000000:Z
569 10:6:1.000000:Speed
570 $$CHILD_BLOCK_END
571 08:aRoomSetWind
572 $$CHILD_BLOCK_START
573 10:2:1:Room
574 10:6:0.000000:X
575 10:6:-10.000000:Y
576 10:6:0.000000:Z
577 10:6:1.000000:Speed
578 $$CHILD_BLOCK_END
579 08:aRoomSetWind
580 $$CHILD_BLOCK_START
581 10:2:2:Room
582 10:6:0.000000:X
583 10:6:-10.000000:Y
584 10:6:0.000000:Z
585 10:6:1.000000:Speed
586 $$CHILD_BLOCK_END
587 08:aRoomSetWind
588 $$CHILD_BLOCK_START
589 10:2:3:Room
590 10:6:0.000000:X
591 10:6:10.000000:Y
592 10:6:0.000000:Z
593 10:6:1.000000:Speed
594 $$CHILD_BLOCK_END
595 $$CHILD_BLOCK_END
596 $$CHILD_BLOCK_END
597
598 $$SCRIPT_BLOCK_END
599 *********************************************************/