Debian Codegraph @

Descent3/doorway.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/doorway.cpp $
21 * $Revision: 25 $
22 * $Date: 4/23/99 8:28p $
23 * $Author: Kevin $
24 *
25 * Doorway code
26 *
27 * $Log: /DescentIII/main/doorway.cpp $
28 *
29 * 25 4/23/99 8:28p Kevin
30 * trying to get doors working in save/load games
31 *
32 * 24 4/20/99 8:35p Matt
33 * Changed ActivateDoorway() to not exit if a door is locked. It's the
34 * caller's responsibility to check if the door is openable (if he cares)
35 * before activating.
36 *
37 * 23 4/20/99 6:55p Matt
38 * Added code to keep a bitmask of all keys held by all players, and to
39 * allow a robot to open a door if any player has the key(s) for that
40 * door.
41 *
42 * 22 4/10/99 5:09p Samir
43 * prioritize sounds, pass 1
44 *
45 * 21 2/09/99 12:39p Matt
46 * Added notification events for door activation and door closed
47 *
48 * 20 2/06/99 10:03p Matt
49 * Added keys system
50 *
51 * 19 2/04/99 2:05p Matt
52 * Added blastable doors
53 *
54 * 18 1/29/99 2:41p Jeff
55 * removed ifdef Editor around DoorwayAdd, so Main can build
56 *
57 * 17 1/29/99 12:47p Matt
58 * Rewrote the doorway system
59 *
60 * 16 1/26/99 4:44p Jason
61 * fixed doorway bug
62 *
63 * 15 10/20/98 3:54p Matt
64 * If a door is activated while shutting, make it start opening again.
65 * If an object is in a door's room, the door will not shut.
66 *
67 * 14 9/28/98 10:55a Jason
68 * fixed some bugs that VC6 caught
69 *
70 * 13 9/08/98 12:06p Jason
71 * added automatic reinitting of doors when rooms get deleted
72 *
73 * 12 6/16/98 10:38a Jeff
74 * localization, strings pulled out to stringtable.h and d3.str
75 *
76 * 11 5/22/98 8:50p Matt
77 * Added sounds for doors
78 *
79 * 10 10/08/97 12:47p Jason
80 * Fixed bug with multiple active doorways list being corrupted
81 *
82 * 9 10/06/97 5:38p Jason
83 * fixed doors to work with timed models
84 *
85 * 8 10/03/97 12:48p Samir
86 * Return -1 if object passed is not a door in GetDoorwayNumFromObject.
87 *
88 * 7 10/03/97 12:51p Jason
89 * added GetDoorwayNumFromObject function
90 *
91 * 6 10/03/97 12:24p Jason
92 * added new keys for doorways
93 *
94 * 5 9/29/97 12:09p Jason
95 * added functionality to doorway system
96 *
97 * 4 9/18/97 1:27p Matt
98 * Cleaned up object struct
99 *
100 * 3 9/12/97 6:09p Jason
101 * fixed bug with load/save level doorways
102 * *
103 *
104 * 2 9/12/97 5:38p Jason
105 * got doors working
106 *
107 * 7 4/02/97 5:14p Samir
108 * Added better checking for ActivateDoorway and fixed GetDoorFlags to
109 * render when finding a 3d door segment.
110 *
111 * 6 3/17/97 6:33p Samir
112 * GetDoorFlags checks 3d door segments also.
113 *
114 * 5 3/14/97 12:51p Samir
115 * Added code to clear active state of doorways.
116 *
117 * 4 3/13/97 11:58a Samir
118 * Added stuff for active doorway and opening/closing
119 *
120 * 3 3/12/97 2:49p Samir
121 * Took out door.cpp globals.
122 *
123 * 2 3/12/97 2:15p Samir
124 * Added source comments and fixed typo.
125 *
126 * $NoKeywords: $
127 */
128
129 #include "doorway.h"
130 #include "door.h"
131 #include "pserror.h"
132 #include "mem.h"
133 #include "room.h"
134 #include "polymodel.h"
135 #include "game.h"
136 #include "hlsoundlib.h"
137 #include "stringtable.h"
138 #include "player.h"
139 #include "osiris_dll.h"
140
141 // ---------------------------------------------------------------------------
142 // Globals
143
144 int Num_active_doorways; // number of active doors in game
145 int Active_doorways[MAX_ACTIVE_DOORWAYS];
146
147 // This is a mask of all keys held by all players. Robots use this to determine if a door is openable.
148 int Global_keys;
149
150 // ---------------------------------------------------------------------------
151 // Prototypes
152
153 // TODO: MTS: none of the following were found
154 // void DoorwayOpen(room *rp);
155 // void DoorwayClose(room *rp);
156 // void DoorwayWait(room *rp);
157 object *GetDoorObject(room *rp);
158
159 // ---------------------------------------------------------------------------
160 // Functions
161
162 void RemoveActiveDoorway(int adway) {
163 room *rp = &Rooms[Active_doorways[adway]];
164
165 ASSERT(rp->flags & RF_DOOR);
166 ASSERT(rp->doorway_data != NULL);
167
168 rp->doorway_data->activenum = -1;
169
170 for (int i = adway; i < (Num_active_doorways - 1); i++) {
171 Active_doorways[i] = Active_doorways[i + 1];
172 Rooms[Active_doorways[i]].doorway_data->activenum--;
173 }
174
175 Num_active_doorways--;
176
177 mprintf(0, "ActiveDoorways = %d\n", Num_active_doorways);
178 }
179
180 void AddActiveDoorway(int roomnum) {
181 room *rp = &Rooms[roomnum];
182 ASSERT(rp->flags & RF_DOOR);
183
184 doorway *dp = rp->doorway_data;
185 ASSERT(dp != NULL);
186
187 if (dp->activenum != -1) { // already active
188 ASSERT(dp->activenum < Num_active_doorways);
189 ASSERT(Active_doorways[dp->activenum] == roomnum);
190 } else { // make new active
191 ASSERT(Num_active_doorways < MAX_ACTIVE_DOORWAYS);
192 Active_doorways[Num_active_doorways] = roomnum;
193 dp->activenum = Num_active_doorways++;
194 }
195
196 mprintf(0, "ActiveDoorways = %d\n", Num_active_doorways);
197 }
198
199 // Given an object handle, returns the doorway number
200 doorway *GetDoorwayFromObject(int door_obj_handle) {
201 object *objp = ObjGet(door_obj_handle);
202
203 if (!objp)
204 return NULL;
205
206 ASSERT(objp->type == OBJ_DOOR);
207
208 room *rp = &Rooms[objp->roomnum];
209
210 ASSERT(rp->flags & RF_DOOR);
211 ASSERT(rp->doorway_data != NULL);
212
213 return rp->doorway_data;
214 }
215
216 // Plays a sound for this door
217 void DoorwayPlaySound(object *objp) {
218 room *rp = &Rooms[objp->roomnum];
219 ASSERT(rp->flags & RF_DOOR);
220
221 doorway *dp = rp->doorway_data;
222 ASSERT(dp != NULL);
223
224 door *door = &Doors[dp->doornum];
225
226 // Stop sound if one playing
227 if (dp->sound_handle != -1) {
228 Sound_system.StopSoundImmediate(dp->sound_handle);
229 dp->sound_handle = -1;
230 }
231
232 // Play new sound, if this door has one
233 if ((dp->state == DOORWAY_OPENING) || (dp->state == DOORWAY_OPENING_AUTO)) {
234 if (door->open_sound != -1) {
235 float offset = dp->position * door->total_open_time;
236 dp->sound_handle =
237 Sound_system.Play3dSound(Doors[dp->doornum].open_sound, SND_PRIORITY_HIGH, objp, 1.0, 0, offset);
238 }
239 } else {
240 ASSERT(dp->state == DOORWAY_CLOSING);
241 if (door->close_sound != -1) {
242 float offset = (1.0 - dp->position) * door->total_close_time;
243 dp->sound_handle =
244 Sound_system.Play3dSound(Doors[dp->doornum].close_sound, SND_PRIORITY_HIGH, objp, 1.0, 0, offset);
245 }
246 }
247 }
248
249 // Opens a door and, if it has the auto flag set, closes it
250 // Does not check to see if the door is openable -- the caller must do that.
251 void DoorwayActivate(int door_obj_handle) {
252 object *objp;
253 doorway *dp;
254 door *door;
255
256 objp = ObjGet(door_obj_handle);
257
258 if (!objp)
259 return;
260
261 ASSERT(objp->type == OBJ_DOOR);
262
263 dp = GetDoorwayFromObject(door_obj_handle);
264 if (!dp)
265 return;
266
267 // If already blasted, bail
268 if (dp->flags & DF_BLASTED)
269 return;
270
271 // Get pointer to door
272 door = &Doors[dp->doornum];
273
274 // If already open or opening or waiting, do nothing
275 if ((dp->state == DOORWAY_OPENING) || (dp->state == DOORWAY_WAITING) || (dp->state == DOORWAY_OPENING_AUTO))
276 return;
277
278 // Store new desired position
279 dp->dest_pos = 1.0;
280
281 // If not already active, create active entry
282 AddActiveDoorway(objp->roomnum);
283
284 // Set new state
285 dp->state = (dp->flags & DF_AUTO) ? DOORWAY_OPENING_AUTO : DOORWAY_OPENING;
286
287 // Play sound
288 DoorwayPlaySound(objp);
289
290 // Send notification event
291 tOSIRISEventInfo ei;
292 Osiris_CallEvent(objp, EVT_DOOR_ACTIVATE, &ei);
293 }
294
295 // Sets the current position of the door. 0.0 = totally closed, 1.0 = totally open
296 // Does not check to see if the door is openable -- the caller must do that.
297 void DoorwaySetPosition(int door_obj_handle, float pos) {
298 object *objp;
299 doorway *dp;
300 door *door;
301
302 objp = ObjGet(door_obj_handle);
303
304 if (!objp)
305 return;
306
307 ASSERT(objp->type == OBJ_DOOR);
308
309 // Get pointer to doorway
310 dp = GetDoorwayFromObject(door_obj_handle);
311 if (!dp)
312 return;
313
314 // Get pointer to door
315 door = &Doors[dp->doornum];
316
317 // Set new dest position
318 dp->dest_pos = pos;
319
320 // Check if already there
321 if (dp->position == pos)
322 return;
323
324 // If not already active, create active entry
325 AddActiveDoorway(objp->roomnum);
326
327 // Set new state & play sound
328 if (dp->dest_pos > dp->position)
329 dp->state = DOORWAY_OPENING;
330 else
331 dp->state = DOORWAY_CLOSING;
332
333 // Play sound
334 DoorwayPlaySound(objp);
335 }
336
337 void DoorwayStop(int door_obj_handle) {
338 // Get pointer to doorway
339 doorway *dp = GetDoorwayFromObject(door_obj_handle);
340 if (!dp)
341 return;
342
343 // Set the door as stopped
344 dp->dest_pos = dp->position;
345 dp->state = DOORWAY_STOPPED;
346
347 // Stop the sound if one is playing
348 if (dp->sound_handle != -1) {
349 Sound_system.StopSoundImmediate(dp->sound_handle);
350 dp->sound_handle = -1;
351 }
352
353 // Remove from the active doorway list
354 if (dp->activenum != -1)
355 RemoveActiveDoorway(dp->activenum);
356 }
357
358 // Called when a door is blown up
359 void DoorwayDestroy(object *objp) {
360 room *rp = &Rooms[objp->roomnum];
361 doorway *dp = rp->doorway_data;
362
363 ASSERT(rp->flags & RF_DOOR);
364 ASSERT(dp);
365
366 dp->flags |= DF_BLASTED;
367 dp->position = 1.0;
368 dp->state = DOORWAY_STOPPED;
369
370 // Remove from the active doorway list
371 if (dp->activenum != -1)
372 RemoveActiveDoorway(dp->activenum);
373 }
374
375 // Stop all doors
376 void DoorwayDeactivateAll() {
377 int r;
378 room *rp;
379
380 // Go through all rooms and deactivate doors
381 for (r = 0, rp = Rooms; r <= Highest_room_index; r++) {
382 if (rp->used && (rp->flags & RF_DOOR)) {
383 if (rp->doorway_data->state != DOORWAY_STOPPED) {
384 doorway *dp = rp->doorway_data;
385 ASSERT(dp != NULL);
386 dp->position = dp->dest_pos;
387 dp->activenum = -1;
388 dp->state = DOORWAY_STOPPED;
389 if (dp->sound_handle != -1) {
390 Sound_system.StopSoundImmediate(dp->sound_handle);
391 dp->sound_handle = -1;
392 }
393 DoorwayUpdateAnimation(rp);
394 }
395 }
396 }
397
398 Num_active_doorways = 0;
399 }
400
401 // Sets the corresponding door objects animation frame
402 void DoorwayUpdateAnimation(room *rp) {
403 doorway *dway;
404 poly_model *pm;
405 int doornum;
406 float norm;
407
408 dway = rp->doorway_data;
409
410 object *objp = GetDoorObject(rp);
411 if (!objp)
412 return;
413
414 doornum = objp->id;
415 pm = &Poly_models[GetDoorImage(doornum)];
416
417 norm = DoorwayPosition(rp);
418
419 if (pm->flags & PMF_TIMED)
420 objp->rtype.pobj_info.anim_frame = pm->frame_max * norm;
421 else
422 objp->rtype.pobj_info.anim_frame = pm->max_keys * norm;
423 }
424
425 // Update all doorways currently active in the mine
426 void DoorwayDoFrame() {
427 int i_doorway;
428
429 for (i_doorway = 0; i_doorway < Num_active_doorways; i_doorway++) {
430 int roomnum = Active_doorways[i_doorway];
431 room *rp = &Rooms[roomnum];
432 ASSERT(rp->flags & RF_DOOR);
433 doorway *dway = rp->doorway_data;
434 ASSERT(dway != NULL);
435 door *door = &Doors[dway->doornum];
436 float delta; // movement delta
437
438 switch (dway->state) {
439 case DOORWAY_OPENING:
440 case DOORWAY_OPENING_AUTO: // doorway is opening
441 delta = Frametime / door->total_open_time;
442
443 dway->position += delta;
444
445 if (dway->position >= dway->dest_pos) {
446 dway->position = dway->dest_pos;
447
448 if (dway->state == DOORWAY_OPENING_AUTO) {
449 dway->state = DOORWAY_WAITING;
450 dway->dest_pos = door->total_time_open;
451 } else { // non automatic doors will stay open, hence are no longer active
452 dway->state = DOORWAY_STOPPED;
453 RemoveActiveDoorway(i_doorway);
454 }
455 }
456 break;
457
458 case DOORWAY_CLOSING: // doorway is closing
459 delta = Frametime / door->total_close_time;
460
461 dway->position -= delta;
462
463 if (dway->position <= dway->dest_pos) {
464 dway->position = dway->dest_pos;
465
466 dway->state = DOORWAY_STOPPED;
467 RemoveActiveDoorway(i_doorway);
468
469 // Send notification event
470 object *objp = GetDoorObject(rp);
471 ASSERT(objp != NULL);
472 tOSIRISEventInfo ei;
473 Osiris_CallEvent(objp, EVT_DOOR_CLOSE, &ei);
474 }
475 break;
476
477 case DOORWAY_WAITING: // doorway is in wait state
478
479 dway->dest_pos -= Frametime;
480
481 if (dway->dest_pos <= 0.0) {
482
483 object *door_objp = GetDoorObject(rp);
484 ASSERT(door_objp != NULL);
485
486 // Time to start closing. See if there's anything in the way
487 if (!((door_objp->next != -1) || (door_objp->prev != -1))) {
488
489 dway->dest_pos = 0.0;
490 dway->state = DOORWAY_CLOSING;
491
492 // Play sound
493 DoorwayPlaySound(door_objp);
494 }
495 }
496 break;
497
498 default:
499 Int3(); // these states shouldn't happen if door is active
500 }
501
502 // Update the animation state
503 DoorwayUpdateAnimation(rp);
504 }
505 }
506
507 // returns a pointer to the door object for the specified doorway
508 object *GetDoorObject(room *rp) {
509 ASSERT(rp->flags & RF_DOOR);
510
511 for (int objnum = rp->objects; (objnum != -1); objnum = Objects[objnum].next)
512 if (Objects[objnum].type == OBJ_DOOR)
513 return &Objects[objnum];
514
515 return NULL;
516 }
517
518 // Returns true if the doorway is locked, else false
519 bool DoorwayLocked(int door_obj_handle) {
520 doorway *dp = GetDoorwayFromObject(door_obj_handle);
521
522 if (!dp)
523 return 0;
524
525 return ((dp->flags & DF_LOCKED) != 0);
526 }
527
528 // Returns true if the doorway is locked, else false
529 bool DoorwayLocked(room *rp) {
530 ASSERT(rp->flags & RF_DOOR);
531
532 doorway *dp = rp->doorway_data;
533 ASSERT(dp != NULL);
534
535 return ((dp->flags & DF_LOCKED) != 0);
536 }
537
538 // Returns true if the doorway is openable by the specified object, else false
539 bool DoorwayOpenable(int door_obj_handle, int opener_handle) {
540 int keys;
541
542 // Get pointer to doorway
543 doorway *dp = GetDoorwayFromObject(door_obj_handle);
544 if (!dp)
545 return 0;
546
547 // If locked, no one can open it
548 if (dp->flags & DF_LOCKED)
549 return 0;
550
551 // If no keys needed, anyone can open
552 if (dp->keys_needed == 0)
553 return 1;
554
555 object *opener = ObjGet(opener_handle);
556
557 // If invalid object, can't open
558 if (!opener)
559 return 0;
560
561 // If a weapon, get the parent
562 if (opener->type == OBJ_WEAPON) {
563 opener = ObjGetUltimateParent(opener);
564 ASSERT(opener && (opener->type != OBJ_WEAPON));
565 }
566
567 // Get the opener's keys
568 if (opener->type == OBJ_PLAYER)
569 keys = Players[opener->id].keys;
570 else if ((opener->type == OBJ_ROBOT) || ((opener->type == OBJ_BUILDING) && opener->ai_info))
571 keys = Global_keys;
572 else
573 return 0; // If not a robot or a player, cannot open keyed doors
574
575 // Door is openenable if have proper keys
576 if (dp->flags & DF_KEY_ONLY_ONE)
577 return ((keys & dp->keys_needed) != 0);
578 else
579 return ((keys & dp->keys_needed) == dp->keys_needed);
580 }
581
582 // Returns the current state of the specified door
583 int DoorwayState(int door_obj_handle) {
584 doorway *dp = GetDoorwayFromObject(door_obj_handle);
585
586 if (!dp)
587 return DOORWAY_STOPPED;
588
589 return dp->state;
590 }
591
592 // Locks or unlocks a given doorway
593 void DoorwayLockUnlock(int door_obj_handle, bool state) {
594 doorway *dp = GetDoorwayFromObject(door_obj_handle);
595
596 if (!dp)
597 return;
598
599 if (state)
600 dp->flags |= DF_LOCKED;
601 else
602 dp->flags &= ~DF_LOCKED;
603 }
604
605 // Returns the current position of the door. 0.0 = totally closed, 1.0 = totally open
606 float DoorwayPosition(room *rp) {
607 ASSERT(rp->flags & RF_DOOR);
608 ASSERT(rp->doorway_data != NULL);
609
610 return rp->doorway_data->position;
611 }
612
613 // Returns the current position of the door. 0.0 = totally closed, 1.0 = totally open
614 float DoorwayPosition(int door_obj_handle) {
615 doorway *dp = GetDoorwayFromObject(door_obj_handle);
616
617 if (!dp)
618 return 0.0;
619
620 return dp->position;
621 }
622
623 // Called after loading a saved game to rebuild the active list
624 void DoorwayRebuildActiveList() {
625 int r;
626 room *rp;
627
628 // Reset active doorways count
629 Num_active_doorways = 0;
630
631 // Go through all rooms and look for active doors
632 for (r = 0, rp = Rooms; r <= Highest_room_index; r++) {
633 if (rp->used && (rp->flags & RF_DOOR)) {
634 ASSERT(rp->doorway_data != NULL);
635 if (rp->doorway_data->state != DOORWAY_STOPPED)
636 AddActiveDoorway(r);
637 }
638 }
639 }
640
641 // Adds a doorway to the specified room
642 // Returns a pointer to the doorway struct
643 doorway *DoorwayAdd(room *rp, int doornum) {
644 ASSERT(rp->doorway_data == NULL);
645
646 rp->flags |= RF_DOOR;
647
648 doorway *dp = rp->doorway_data = (doorway *)mem_malloc(sizeof(*rp->doorway_data));
649
650 // Initialize
651 dp->doornum = doornum;
652 dp->dest_pos = dp->position = 0.0;
653 dp->state = DOORWAY_STOPPED;
654 dp->flags = DF_AUTO;
655 dp->keys_needed = 0;
656 dp->activenum = -1;
657 dp->sound_handle = -1;
658
659 return dp;
660 }