Debian Codegraph @

manage/doorpage.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 --- HISTORICAL COMMENTS FOLLOW ---
19
20 * $Logfile: /DescentIII/Main/manage/doorpage.cpp $
21 * $Revision: 25 $
22 * $Date: 10/26/99 3:30p $
23 * $Author: Jeff $
24 *
25 * For loading/saving of door types
26 *
27 * $Log: /DescentIII/Main/manage/doorpage.cpp $
28 *
29 * 25 10/26/99 3:30p Jeff
30 * handle extra.gam addon tablefile
31 *
32 * 24 10/20/99 6:27p Jeff
33 * sped up addon page popping (by saving page offsets)
34 *
35 * 23 8/11/99 5:32p Jeff
36 * changes to fix addon tablefile support so it works correctly
37 *
38 * 22 4/14/99 1:33a Jeff
39 * fixed case mismatched #includes
40 *
41 * 21 3/04/99 4:47p Jason
42 * temp fix (ie BAD HACK) for OEM table file woes
43 *
44 * 20 2/04/99 2:05p Matt
45 * Added blastable doors
46 *
47 * 19 1/21/99 11:16p Jeff
48 * pulled out some structs and defines from header files and moved them
49 * into seperate header files so that multiplayer dlls don't require major
50 * game headers, just those new headers. Side effect is a shorter build
51 * time. Also cleaned up some header file #includes that weren't needed.
52 * This affected polymodel.h, object.h, player.h, vecmat.h, room.h,
53 * manage.h and multi.h
54 *
55 * 18 1/13/99 7:04a Jeff
56 * put #ifdef around #include <windows.h>
57 *
58 * 17 12/29/98 4:30p Jason
59 * added add-on data functionality
60 *
61 * 16 12/21/98 5:27p Jeff
62 * added osiris module information to door page. Fixed up dialogs for
63 * objects and doors for script interface (added a browse button)
64 *
65 * 15 12/17/98 12:50p Jason
66 * changed doorpage
67 *
68 * 14 11/06/98 12:35p Jason
69 * more speedups for manage system
70 *
71 * 13 11/05/98 7:54p Jason
72 * changes for new manage system
73 *
74 * 12 11/02/98 6:02p Jason
75 * made yes network updates much faster
76 *
77 * 11 10/09/98 2:27p Jason
78 * reorganized table file system
79 *
80 * 10 5/24/98 11:59p Mark
81 * Fixed a bug introduced with last revs
82 *
83 * 9 5/22/98 8:50p Matt
84 * Added sounds for doors
85 *
86 * 8 4/02/98 3:54p Jason
87 * first pass in getting polymodel paging to work
88 *
89 * 7 2/23/98 1:22p Jason
90 * made FindSpecific* read from the local drive, not the net drive - when
91 * starting the editor
92 *
93 * 6 2/02/98 7:07p Matt
94 * Added support for doors that can be seen through even when closed
95 *
96 * 5 1/15/98 6:22p Jason
97 * added safety checks so the network won't copy over a primitive you have
98 * held locally
99 *
100 * 4 10/06/97 6:39p Jason
101 * got terrain objects working with lightmaps/radiosity
102 *
103 * 3 10/06/97 4:05p Samir
104 * Jason forgot to put a break in a switch-case statement. I fixed
105 * this...
106 *
107 * 2 10/06/97 1:01p Jason
108 * made doors work with scripts (sort of)
109 *
110 * 9 5/08/97 12:41p Jason
111 * made manage system work with device dependant path names
112 *
113 * 8 4/25/97 6:16p Jason
114 * added switcheroo function
115 *
116 * 7 4/08/97 2:25a Chris
117 * Fixed a problem with uninitialized data. In addition it
118 * fixed a problem with the .used flag that would happen
119 * 1 out of 2^(sizeof(used_flag)) times (it would be zero
120 * when it was supposed to be non-zero)
121 *
122 * 6 3/17/97 6:39p Jason
123 * added staysopen page write
124 *
125 * 5 3/13/97 6:13p Jason
126 * got poly doors working
127 *
128 * 4 3/11/97 6:03p Chris
129 * Fixed the fix to a simple function. Isn't crossplatform development
130 * wonderful. :)
131 *
132 * 3 3/11/97 5:08 PM Jeremy
133 * in mng_WriteDoorPage there are two for loops. The second tried to use
134 * the int "i" which was declared in the scope of the first. This breaks
135 * under Codewarrior. I think that's how it's supposed to work under
136 * ANSI.
137 *
138 * 2 3/05/97 12:16p Jason
139 * added code to support our new 3d doors
140 *
141 * 1 3/05/97 11:04a Jason
142 *
143 *
144 * $NoKeywords: $
145 */
146
147 #include <cstring>
148 #include <filesystem>
149
150 #include "cfile.h"
151 #include "manage.h"
152 #include "door.h"
153 #include "doorpage.h"
154 #include "mono.h"
155 #include "pserror.h"
156 #include "polymodel.h"
157 #include "ddio.h"
158 #include "soundload.h"
159 #include "soundpage.h"
160 #include "args.h"
161
162 // doorpage commands that are read/written
163 // A command is followed by a byte count describing how many bytes
164 // are in the data for the command
165 #define DOORPAGE_COMMAND_NAME 1
166 #define DOORPAGE_COMMAND_END 2
167 #define DOORPAGE_COMMAND_IMAGE_NAME 3
168 #define DOORPAGE_COMMAND_VERSION 4
169 #define DOORPAGE_COMMAND_OPEN_TIME 5
170 #define DOORPAGE_COMMAND_CLOSE_TIME 6
171 #define DOORPAGE_COMMAND_STAYS_OPEN 7
172 #define DOORPAGE_COMMAND_SCRIPTNAME 8
173 #define DOORPAGE_COMMAND_FLAGS 9
174 #define DOORPAGE_COMMAND_OPEN_SOUND_NAME 10
175 #define DOORPAGE_COMMAND_CLOSE_SOUND_NAME 11
176
177 #define DOORPAGE_VERSION 3
178
179 extern char *TablefileNameOverride;
180
181 // Given an open file pointer and a door_page struct, writes that door page out
182 void mng_WriteDoorPage(CFILE *outfile, mngs_door_page *doorpage) {
183 int i;
184
185 ASSERT(outfile != NULL);
186 ASSERT(doorpage != NULL);
187
188 cf_WriteByte(outfile, PAGETYPE_DOOR);
189
190 cf_WriteByte(outfile, DOORPAGE_COMMAND_NAME); // write out Door name
191 cf_WriteByte(outfile, PAGENAME_LEN);
192 for (i = 0; i < PAGENAME_LEN; i++)
193 cf_WriteByte(outfile, doorpage->door_struct.name[i]);
194
195 // Write out its models name
196 cf_WriteByte(outfile, DOORPAGE_COMMAND_IMAGE_NAME); // get ready to write out name
197 cf_WriteByte(outfile, PAGENAME_LEN);
198 for (i = 0; i < PAGENAME_LEN; i++)
199 cf_WriteByte(outfile, doorpage->image_name[i]);
200
201 cf_WriteByte(outfile, DOORPAGE_COMMAND_OPEN_TIME);
202 cf_WriteByte(outfile, sizeof(float));
203 cf_WriteFloat(outfile, doorpage->door_struct.total_open_time);
204
205 cf_WriteByte(outfile, DOORPAGE_COMMAND_CLOSE_TIME);
206 cf_WriteByte(outfile, sizeof(float));
207 cf_WriteFloat(outfile, doorpage->door_struct.total_close_time);
208
209 cf_WriteByte(outfile, DOORPAGE_COMMAND_STAYS_OPEN);
210 cf_WriteByte(outfile, sizeof(float));
211 cf_WriteFloat(outfile, doorpage->door_struct.total_time_open);
212
213 cf_WriteByte(outfile, DOORPAGE_COMMAND_SCRIPTNAME);
214 cf_WriteByte(outfile, strlen("") + 1);
215 cf_WriteString(outfile, "");
216
217 cf_WriteByte(outfile, DOORPAGE_COMMAND_FLAGS);
218 cf_WriteByte(outfile, sizeof(int8_t));
219 cf_WriteByte(outfile, doorpage->door_struct.flags);
220
221 Int3(); // this shouldn't be called -- hit points not written out
222
223 cf_WriteByte(outfile, DOORPAGE_COMMAND_OPEN_SOUND_NAME);
224 cf_WriteByte(outfile, strlen(doorpage->open_sound_name) + 1);
225 cf_WriteString(outfile, doorpage->open_sound_name);
226
227 cf_WriteByte(outfile, DOORPAGE_COMMAND_CLOSE_SOUND_NAME);
228 cf_WriteByte(outfile, strlen(doorpage->close_sound_name) + 1);
229 cf_WriteString(outfile, doorpage->close_sound_name);
230
231 cf_WriteByte(outfile, DOORPAGE_COMMAND_END); // we're all done
232 cf_WriteByte(outfile, 0);
233 }
234
235 // Given an open file pointer and a door_page struct, writes that door page out
236 void mng_WriteNewDoorPage(CFILE *outfile, mngs_door_page *doorpage) {
237 ASSERT(outfile != NULL);
238 ASSERT(doorpage != NULL);
239
240 int offset = StartManagePage(outfile, PAGETYPE_DOOR);
241
242 cf_WriteShort(outfile, DOORPAGE_VERSION);
243 cf_WriteString(outfile, doorpage->door_struct.name);
244 cf_WriteString(outfile, doorpage->image_name);
245
246 cf_WriteFloat(outfile, doorpage->door_struct.total_open_time);
247 cf_WriteFloat(outfile, doorpage->door_struct.total_close_time);
248 cf_WriteFloat(outfile, doorpage->door_struct.total_time_open);
249
250 cf_WriteByte(outfile, doorpage->door_struct.flags);
251 cf_WriteShort(outfile, doorpage->door_struct.hit_points);
252
253 cf_WriteString(outfile, doorpage->open_sound_name);
254 cf_WriteString(outfile, doorpage->close_sound_name);
255
256 cf_WriteString(outfile, doorpage->door_struct.module_name);
257
258 EndManagePage(outfile, offset);
259 }
260
261 // Reads a door page from an open file. Returns 0 on error.
262 int mng_ReadNewDoorPage(CFILE *infile, mngs_door_page *doorpage) {
263 ASSERT(infile != NULL);
264
265 int version = cf_ReadShort(infile);
266
267 cf_ReadString(doorpage->door_struct.name, PAGENAME_LEN, infile);
268 cf_ReadString(doorpage->image_name, PAGENAME_LEN, infile);
269
270 doorpage->door_struct.total_open_time = cf_ReadFloat(infile);
271 doorpage->door_struct.total_close_time = cf_ReadFloat(infile);
272 doorpage->door_struct.total_time_open = cf_ReadFloat(infile);
273
274 doorpage->door_struct.flags = cf_ReadByte(infile);
275
276 if (version >= 3)
277 doorpage->door_struct.hit_points = cf_ReadShort(infile);
278 else
279 doorpage->door_struct.hit_points = 0;
280
281 cf_ReadString(doorpage->open_sound_name, PAGENAME_LEN, infile);
282 cf_ReadString(doorpage->close_sound_name, PAGENAME_LEN, infile);
283
284 if (version >= 2)
285 cf_ReadString(doorpage->door_struct.module_name, MAX_MODULENAME_LEN, infile);
286 else
287 doorpage->door_struct.module_name[0] = '\0';
288
289 // This is a valid new page
290 doorpage->door_struct.used = 1;
291
292 return 1; // successfully read
293 }
294
295 // Reads a door page from an open file. Returns 0 on error.
296 int mng_ReadDoorPage(CFILE *infile, mngs_door_page *doorpage) {
297 int done = 0;
298 char command;
299 uint8_t len;
300 int i;
301
302 if (!Old_table_method)
303 return mng_ReadNewDoorPage(infile, doorpage);
304
305 // doorpage->door_struct.script_name[0]=0;
306 strcpy(doorpage->open_sound_name, "INVALID SOUND NAME");
307 strcpy(doorpage->close_sound_name, "INVALID SOUND NAME");
308
309 ASSERT(infile != NULL);
310
311 while (!done) {
312 // Read in command byte then read in the length of that commands data
313
314 command = cf_ReadByte(infile);
315 len = cf_ReadByte(infile);
316
317 switch (command) {
318 case DOORPAGE_COMMAND_END:
319 done = 1;
320 break;
321 case DOORPAGE_COMMAND_IMAGE_NAME: // the name of the door model
322 for (i = 0; i < PAGENAME_LEN; i++)
323 doorpage->image_name[i] = cf_ReadByte(infile);
324 break;
325 case DOORPAGE_COMMAND_NAME:
326 for (i = 0; i < PAGENAME_LEN; i++)
327 doorpage->door_struct.name[i] = cf_ReadByte(infile);
328 break;
329 case DOORPAGE_COMMAND_OPEN_TIME:
330 doorpage->door_struct.total_open_time = cf_ReadFloat(infile);
331 break;
332 case DOORPAGE_COMMAND_STAYS_OPEN:
333 doorpage->door_struct.total_time_open = cf_ReadFloat(infile);
334 break;
335 case DOORPAGE_COMMAND_CLOSE_TIME:
336 doorpage->door_struct.total_close_time = cf_ReadFloat(infile);
337 break;
338 case DOORPAGE_COMMAND_SCRIPTNAME: {
339 char dummy[256];
340 cf_ReadString(dummy, len + 1, infile);
341 } break;
342 case DOORPAGE_COMMAND_FLAGS:
343 doorpage->door_struct.flags = cf_ReadByte(infile);
344 break;
345 case DOORPAGE_COMMAND_OPEN_SOUND_NAME:
346 cf_ReadString(doorpage->open_sound_name, len + 1, infile);
347 break;
348 case DOORPAGE_COMMAND_CLOSE_SOUND_NAME:
349 cf_ReadString(doorpage->close_sound_name, len + 1, infile);
350 break;
351 default:
352 // Ignore the ones we don't know
353 for (i = 0; i < len; i++)
354 cf_ReadByte(infile);
355 return 0;
356 }
357 }
358
359 // This is a valid new page
360 doorpage->door_struct.used = 1;
361
362 return 1; // successfully read
363 }
364
365 // Reads in the door named "name" into doorpage struct
366 // Returns 0 on error or couldn't find, else 1 if all is good
367 int mng_FindSpecificDoorPage(char *name, mngs_door_page *doorpage, int offset) {
368 CFILE *infile;
369 uint8_t pagetype;
370 int done = 0, found = 0;
371
372 std::filesystem::path tablename;
373
374 if (Loading_locals) {
375 infile = cfopen(LocalTableFilename, "rb");
376 } else if (Loading_addon_table != -1) {
377 infile = cfopen(AddOnDataTables[Loading_addon_table].AddOnTableFilename, "rb");
378 } else {
379 if (Network_up && Starting_editor) {
380 int farg = FindArg("-filter");
381
382 if (farg)
383 tablename = GameArgs[farg + 1];
384 else
385 tablename = LocalTableDir / NET_TABLE;
386
387 infile = cfopen(tablename, "rb");
388 } else {
389 infile = NULL;
390 if (TablefileNameOverride) {
391 infile = cfopen(TablefileNameOverride, "rb");
392 }
393
394 if (!infile)
395 infile = cfopen(TableFilename, "rb");
396 }
397 }
398
399 if (!infile) {
400 mprintf(0, "Couldn't open table file to find door!\n");
401 Int3();
402 return 0;
403 }
404
405 if (offset)
406 cfseek(infile, offset, SEEK_SET);
407
408 // Read in the entire page file until we find the page we want
409 while (!done) {
410 if (cfeof(infile)) {
411 done = 1;
412 continue;
413 }
414 pagetype = cf_ReadByte(infile);
415 int len = cf_ReadInt(infile);
416
417 // If not a door page, just read it in and ignore it
418 if (pagetype != PAGETYPE_DOOR) {
419 cfseek(infile, len - 4, SEEK_CUR);
420 continue;
421 }
422
423 mng_ReadNewDoorPage(infile, doorpage);
424
425 if (!stricmp(name, doorpage->door_struct.name)) {
426 // This is the page we want
427 found = 1;
428 done = 1;
429 }
430 }
431
432 cfclose(infile);
433
434 return found; // successful!
435 }
436
437 // Given a door page, allocs a door and calls AssignDoorPageToDoor to actually
438 // load models and values. Rturns door handle on success, -1 if fail
439 int mng_SetAndLoadDoor(mngs_door_page *doorpage) {
440 int n;
441
442 n = AllocDoor();
443 if (n < 0)
444 return -1;
445 if (!mng_AssignDoorPageToDoor(doorpage, n))
446 return -1;
447
448 return n;
449 }
450
451 // Given a doorpage and a door handle, attempts to make door n correspond to
452 // to the doorpage.
453 // Returns 1 on success, 0 otherwise
454 int mng_AssignDoorPageToDoor(mngs_door_page *doorpage, int n) {
455 door *doorpointer = &Doors[n];
456 int img_handle;
457
458 // copy our values
459 memcpy(doorpointer, &doorpage->door_struct, sizeof(door));
460 strcpy(doorpointer->name, doorpage->door_struct.name);
461
462 // First see if our image differs from the one on the net
463 // If it is, make a copy
464 // If its a release version, don't do any of this
465
466 #ifndef RELEASE
467 if (Network_up) {
468 UpdatePrimitive(LocalModelsDir / doorpage->image_name, NetModelsDir / doorpage->image_name, doorpage->image_name,
469 PAGETYPE_DOOR, doorpointer->name);
470 }
471 #endif
472
473 // Try and load our door model from the disk
474
475 img_handle = LoadPolyModel(doorpage->image_name, 1);
476
477 // Set sounds
478 doorpointer->open_sound = mng_GetGuaranteedSoundPage(doorpage->open_sound_name);
479 doorpointer->close_sound = mng_GetGuaranteedSoundPage(doorpage->close_sound_name);
480
481 if (img_handle < 0) {
482 mprintf(0, "Couldn't load file '%s' in AssignDoorPage...\n", doorpage->image_name);
483 doorpointer->model_handle = -1;
484 return 0;
485 } else
486 doorpointer->model_handle = img_handle;
487
488 return 1;
489 }
490
491 // Copies values from a door into a door_page
492 void mng_AssignDoorToDoorPage(int n, mngs_door_page *doorpage) {
493 door *doorpointer = &Doors[n];
494
495 // Assign the values
496 memcpy(&doorpage->door_struct, doorpointer, sizeof(door));
497
498 strcpy(doorpage->door_struct.name, doorpointer->name);
499
500 if (doorpointer->model_handle != -1)
501 strcpy(doorpage->image_name, Poly_models[doorpointer->model_handle].name);
502 else
503 strcpy(doorpage->image_name, "INVALID IMAGE NAME");
504
505 if (doorpointer->open_sound != -1)
506 strcpy(doorpage->open_sound_name, Sounds[doorpointer->open_sound].name);
507 else
508 strcpy(doorpage->open_sound_name, "INVALID SOUND NAME");
509
510 if (doorpointer->close_sound != -1)
511 strcpy(doorpage->close_sound_name, Sounds[doorpointer->close_sound].name);
512 else
513 strcpy(doorpage->close_sound_name, "INVALID SOUND NAME");
514 }
515
516 // Loads a door found in the net table file. It then allocs a door and
517 // then calls SetAndLoadDoor to actually load in any images/models associated
518 // with it
519 void mng_LoadNetDoorPage(CFILE *infile, bool overlay) {
520 mngs_door_page doorpage;
521 memset(&doorpage, 0, sizeof(mngs_door_page));
522
523 if (mng_ReadNewDoorPage(infile, &doorpage)) {
524 int n;
525 n = FindDoorName(doorpage.door_struct.name);
526 if (n != -1) {
527 if (overlay) {
528 mprintf(0, "OVERLAYING DOOR %s\n", doorpage.door_struct.name);
529 mng_FreePagetypePrimitives(PAGETYPE_DOOR, doorpage.door_struct.name, 0);
530 mng_AssignDoorPageToDoor(&doorpage, n);
531 }
532 return;
533 }
534
535 int ret = mng_SetAndLoadDoor(&doorpage);
536 ASSERT(ret >= 0);
537 } else
538 mprintf(0, "Could not load doorpage named %s!\n", doorpage.door_struct.name);
539 }
540
541 // Reads a door page from a local table file. It then allocs a door and
542 // loads any images/models associated with that door
543 void mng_LoadLocalDoorPage(CFILE *infile) {
544 mngs_door_page doorpage;
545 int ok = 0;
546 memset(&doorpage, 0, sizeof(mngs_door_page));
547
548 if (mng_ReadNewDoorPage(infile, &doorpage)) {
549 // Check to see if this is a local copy that is supposed
550 // to go over a network copy (supersede the net copy)
551
552 int i = FindDoorName(doorpage.door_struct.name);
553 if (i != -1) {
554 // Make sure we really have this page checked out
555 mngs_Pagelock pl;
556
557 strcpy(pl.name, doorpage.door_struct.name);
558 pl.pagetype = PAGETYPE_DOOR;
559
560 /*if (Network_up && Stand_alone==0)
561 {
562 int locked;
563 locked=mng_CheckIfPageOwned(&pl,TableUser);
564 if (locked!=1)
565 Int3(); // Your local vs net copies of the lock file do not match
566 }*/
567
568 ok = 1;
569 bool need_to_load_page = true;
570
571 if (Loading_addon_table != -1) {
572 AddOnTablefile *addon;
573 int tidx;
574
575 // see if we really need to load this page
576 // check to see if we already have loaded this page (because it was
577 // a dependancy of another)
578 addon = &AddOnDataTables[Loading_addon_table];
579 for (tidx = 0; tidx < addon->Num_addon_tracklocks; tidx++) {
580 if (addon->Addon_tracklocks[tidx].pagetype == PAGETYPE_DOOR &&
581 !stricmp(addon->Addon_tracklocks[tidx].name, doorpage.door_struct.name)) {
582 // found it!!
583 mprintf(0, "DoorPage: %s previously loaded\n", doorpage.door_struct.name);
584 need_to_load_page = false;
585 break;
586 }
587 }
588 }
589
590 if (need_to_load_page) {
591 mng_FreePagetypePrimitives(PAGETYPE_DOOR, doorpage.door_struct.name, 0);
592 mng_AssignDoorPageToDoor(&doorpage, i);
593
594 // For addon data
595 if (Loading_addon_table != -1) {
596 // this is an overlay of some sort..see which we are overlaying
597 int overlay = 1;
598 int addidx, tidx;
599 bool found = false;
600
601 for (addidx = Num_addon_tables - 1; addidx >= 0; addidx--) {
602 if (addidx == Loading_addon_table)
603 continue;
604 AddOnTablefile *addon = &AddOnDataTables[addidx];
605
606 // look for the page in this table file
607 for (tidx = 0; tidx < addon->Num_addon_tracklocks; tidx++) {
608 if (addon->Addon_tracklocks[tidx].pagetype == PAGETYPE_DOOR &&
609 !stricmp(addon->Addon_tracklocks[tidx].name, doorpage.door_struct.name)) {
610 // found it!!
611 found = true;
612 overlay = addidx + 2;
613 break;
614 }
615 }
616
617 if (found)
618 break;
619 }
620
621 mng_PushAddonPage(PAGETYPE_DOOR, doorpage.door_struct.name, overlay);
622 }
623 }
624 } else {
625 // This is a local door that has never been checked in
626 if ((i = mng_SetAndLoadDoor(&doorpage)) < 0)
627 ok = 0;
628 else
629 ok = 1;
630
631 // For addon data
632 if (ok && Loading_addon_table != -1)
633 mng_PushAddonPage(PAGETYPE_DOOR, doorpage.door_struct.name, 0);
634 }
635
636 ASSERT(ok == 1);
637
638 if (Loading_addon_table == -1)
639 mng_AllocTrackLock(doorpage.door_struct.name, PAGETYPE_DOOR);
640 } else
641
642 mprintf(0, "Could not load doorpage named %s!\n", doorpage.door_struct.name);
643 }