Debian Codegraph @

scripts/barney.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: barney.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 ReadMessageFile("barney.msg");
345
346 int j;
347
348 // Do Door Index lookups
349 for (j = 0; j < NUM_DOOR_NAMES; j++)
350 Door_handles[j] = Scrpt_FindDoorName(Door_names[j]);
351
352 // Do Object Index lookups
353 for (j = 0; j < NUM_OBJECT_NAMES; j++)
354 Object_handles[j] = Scrpt_FindObjectName(Object_names[j]);
355
356 // Do Room Index lookups
357 for (j = 0; j < NUM_ROOM_NAMES; j++)
358 Room_indexes[j] = Scrpt_FindRoomName(Room_names[j]);
359
360 // Do Trigger Index lookups
361 for (j = 0; j < NUM_TRIGGER_NAMES; j++) {
362 Trigger_indexes[j] = Scrpt_FindTriggerName(Trigger_names[j]);
363 Trigger_faces[j] = Scrpt_GetTriggerFace(Trigger_indexes[j]);
364 Trigger_rooms[j] = Scrpt_GetTriggerRoom(Trigger_indexes[j]);
365 }
366
367 // Do Sound Index lookups
368 for (j = 0; j < NUM_SOUND_NAMES; j++)
369 Sound_indexes[j] = Scrpt_FindSoundName(Sound_names[j]);
370
371 // Do Texture Index lookups
372 for (j = 0; j < NUM_TEXTURE_NAMES; j++)
373 Texture_indexes[j] = Scrpt_FindTextureName(Texture_names[j]);
374
375 // Do Path Index lookups
376 for (j = 0; j < NUM_PATH_NAMES; j++)
377 Path_indexes[j] = Scrpt_FindPathName(Path_names[j]);
378
379 // Do Matcen Index lookups
380 for (j = 0; j < NUM_MATCEN_NAMES; j++)
381 Matcen_indexes[j] = Scrpt_FindMatcenName(Matcen_names[j]);
382
383 // Do Goal Index lookups
384 for (j = 0; j < NUM_GOAL_NAMES; j++)
385 Goal_indexes[j] = Scrpt_FindLevelGoalName(Goal_names[j]);
386
387 // Do Message Name lookups
388 for (j = 0; j < NUM_MESSAGE_NAMES; j++)
389 Message_strings[j] = GetMessage(Message_names[j]);
390
391 return 1;
392 }
393
394 // =============
395 // ShutdownDLL()
396 // =============
397 void STDCALL ShutdownDLL(void) { ClearMessageList(); }
398
399 // ===============
400 // GetGOScriptID()
401 // ===============
402 int STDCALL GetGOScriptID(const char *name, uint8_t isdoor) { return -1; }
403
404 // ================
405 // CreateInstance()
406 // ================
407 void STDCALLPTR CreateInstance(int id) {
408 switch (id) {
409 case ID_LEVEL_0000:
410 return new LevelScript_0000;
411 break;
412 default:
413 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
414 break;
415 }
416 return NULL;
417 }
418
419 // =================
420 // DestroyInstance()
421 // =================
422 void STDCALL DestroyInstance(int id, void *ptr) {
423 switch (id) {
424 case ID_LEVEL_0000:
425 delete ((LevelScript_0000 *)ptr);
426 default:
427 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
428 break;
429 }
430 }
431
432 // ===================
433 // CallInstanceEvent()
434 // ===================
435 int16_t STDCALL CallInstanceEvent(int id, void *ptr, int event, tOSIRISEventInfo *data) {
436 switch (id) {
437 case ID_LEVEL_0000:
438 return ((BaseScript *)ptr)->CallEvent(event, data);
439 break;
440 default:
441 mprintf(0, "SCRIPT: Illegal ID (%d)\n", id);
442 break;
443 }
444 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
445 }
446
447 // ==================
448 // SaveRestoreState()
449 // ==================
450 int STDCALL SaveRestoreState(void *file_ptr, uint8_t saving_state) { return 0; }
451
452 // ====================
453 // GetTriggerScriptID()
454 // ====================
455 int STDCALL GetTriggerScriptID(int trigger_room, int trigger_face) { return -1; }
456
457 // =================
458 // GetCOScriptList()
459 // =================
460 int STDCALL GetCOScriptList(int **list, int **id_list) {
461 static int *cust_handle_list = NULL;
462 static int *cust_id_list = NULL;
463
464 *list = cust_handle_list;
465 *id_list = cust_id_list;
466
467 return 0;
468 }
469
470 //=======================
471 // Script Implementation
472 //=======================
473
474 BaseScript::BaseScript() {}
475
476 BaseScript::~BaseScript() {}
477
478 int16_t BaseScript::CallEvent(int event, tOSIRISEventInfo *data) {
479 mprintf(0, "BaseScript::CallEvent()\n");
480 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
481 }
482
483 int16_t LevelScript_0000::CallEvent(int event, tOSIRISEventInfo *data) {
484 switch (event) {
485 case EVT_SAVESTATE: {
486 tOSIRISEVTSAVESTATE *event_data = &data->evt_savestate;
487
488 SaveGlobalActionCtrs(event_data->fileptr);
489 dfSave(event_data->fileptr);
490 } break;
491 case EVT_RESTORESTATE: {
492 tOSIRISEVTRESTORESTATE *event_data = &data->evt_restorestate;
493
494 RestoreGlobalActionCtrs(event_data->fileptr);
495 dfRestore(event_data->fileptr);
496 } break;
497 case EVT_LEVELSTART: {
498 tOSIRISEVTLEVELSTART *event_data = &data->evt_levelstart;
499
500 ClearGlobalActionCtrs();
501 dfInit();
502 } break;
503 }
504 return CONTINUE_CHAIN | CONTINUE_DEFAULT;
505 }
506
507 /*********************************************************
508 Script Save Block: DO NOT TOUCH ANYTHING IN THIS BLOCK!!!
509 **********************************************************
510 $$SCRIPT_BLOCK_START
511
512 VERSION 3
513 NEXT_ID 0
514
515 // UserType value blocks
516 $$UTYPE_VALS_START UserVar
517 $$UTYPE_VALS_END
518
519 $$UTYPE_VALS_START UserFlag
520 $$UTYPE_VALS_END
521
522 $$UTYPE_VALS_START SpewHandle
523 $$UTYPE_VALS_END
524
525 $$UTYPE_VALS_START TimerID
526 $$UTYPE_VALS_END
527
528 $$UTYPE_VALS_START SavedObjectSlot
529 $$UTYPE_VALS_END
530
531 $$UTYPE_VALS_START GoalID
532 $$UTYPE_VALS_END
533
534
535 // Name Lists
536 $$DOOR_LIST_START
537 $$DOOR_LIST_END
538
539 $$OBJECT_LIST_START
540 $$OBJECT_LIST_END
541
542 $$ROOM_LIST_START
543 $$ROOM_LIST_END
544
545 $$TRIGGER_LIST_START
546 $$TRIGGER_LIST_END
547
548 $$SOUND_LIST_START
549 $$SOUND_LIST_END
550
551 $$TEXTURE_LIST_START
552 $$TEXTURE_LIST_END
553
554 $$SPECNAME_LIST_START
555 $$SPECNAME_LIST_END
556
557 $$PATH_LIST_START
558 $$PATH_LIST_END
559
560 $$MATCEN_LIST_START
561 $$MATCEN_LIST_END
562
563 $$GOAL_LIST_START
564 $$GOAL_LIST_END
565
566 $$STRM_AUDIO_LIST_START
567 $$STRM_AUDIO_LIST_END
568
569
570 // Script Tree Dump
571
572 $$SCRIPT_BLOCK_END
573 *********************************************************/