]> icculus.org git repositories - btb/d2x.git/blob - main/wall.c
stop pointless vid mode changes
[btb/d2x.git] / main / wall.c
1 /* $Id: wall.c,v 1.10 2003-10-04 03:14:48 btb Exp $ */
2 /*
3 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
4 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
5 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
6 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
7 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
8 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
9 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
10 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
11 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
12 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
13 */
14
15 /*
16  *
17  * Destroyable wall stuff
18  *
19  * Old Log:
20  * Revision 1.1  1995/05/16  15:32:08  allender
21  * Initial revision
22  *
23  * Revision 2.1  1995/03/21  14:39:04  john
24  * Ifdef'd out the NETWORK code.
25  *
26  * Revision 2.0  1995/02/27  11:28:32  john
27  * New version 2.0, which has no anonymous unions, builds with
28  * Watcom 10.0, and doesn't require parsing BITMAPS.TBL.
29  *
30  * Revision 1.112  1995/02/22  13:53:07  allender
31  * remove anonymous unions from object structure
32  *
33  * Revision 1.111  1995/02/01  17:32:17  adam
34  * Took out a bogus int3.
35  *
36  * Revision 1.110  1995/02/01  17:20:24  john
37  * Lintized.
38  *
39  * Revision 1.109  1995/01/21  17:39:50  matt
40  * Cleaned up laser/player hit wall confusions
41  *
42  * Revision 1.108  1995/01/21  17:14:17  rob
43  * Fixed bug in multiplayer door-butting.
44  *
45  * Revision 1.107  1995/01/18  18:57:11  rob
46  * Added new hostage door hooks.
47  *
48  * Revision 1.106  1995/01/18  18:48:18  allender
49  * removed #ifdef newdemo's.  Added function call to record a door that
50  * starts to open. This fixes the rewind problem
51  *
52  * Revision 1.105  1995/01/16  11:55:39  mike
53  * make control center (and robots whose id == your playernum) not able to open doors.
54  *
55  * Revision 1.104  1994/12/11  23:07:21  matt
56  * Fixed stuck objects & blastable walls
57  *
58  * Revision 1.103  1994/12/10  16:44:34  matt
59  * Added debugging code to track down door that turns into rock
60  *
61  * Revision 1.102  1994/12/06  16:27:05  matt
62  * Added debugging
63  *
64  * Revision 1.101  1994/12/02  10:50:27  yuan
65  * Localization
66  *
67  * Revision 1.100  1994/11/30  19:41:22  rob
68  * Put in a fix so that door opening sounds travel through the door.
69  *
70  * Revision 1.99  1994/11/28  11:59:50  yuan
71  * *** empty log message ***
72  *
73  * Revision 1.98  1994/11/28  11:25:45  matt
74  * Cleaned up key hud messages
75  *
76  * Revision 1.97  1994/11/27  23:15:11  matt
77  * Made changes for new mprintf calling convention
78  *
79  * Revision 1.96  1994/11/19  15:18:29  mike
80  * rip out unused code and data.
81  *
82  * Revision 1.95  1994/11/17  14:57:12  mike
83  * moved segment validation functions from editor to main.
84  *
85  * Revision 1.94  1994/11/07  08:47:30  john
86  * Made wall state record.
87  *
88  * Revision 1.93  1994/11/04  16:06:37  rob
89  * Fixed network damage of blastable walls.
90  *
91  * Revision 1.92  1994/11/02  21:54:01  matt
92  * Don't let objects with zero size keep door from shutting
93  *
94  * Revision 1.91  1994/10/31  13:48:42  rob
95  * Fixed bug in opening doors over network/modem.  Added a new message
96  * type to multi.c that communicates door openings across the net.
97  * Changed includes in multi.c and wall.c to accomplish this.
98  *
99  * Revision 1.90  1994/10/28  14:42:41  john
100  * Added sound volumes to all sound calls.
101  *
102  * Revision 1.89  1994/10/23  19:16:55  matt
103  * Fixed bug with "no key" messages
104  *
105  */
106
107 #ifdef HAVE_CONFIG_H
108 #include <conf.h>
109 #endif
110
111 #ifdef RCS
112 static char rcsid[] = "$Id: wall.c,v 1.10 2003-10-04 03:14:48 btb Exp $";
113 #endif
114
115 #include <stdio.h>
116 #include <stdlib.h>
117 #include <math.h>
118 #include <string.h>
119
120 #include "pstypes.h"
121 #include "mono.h"
122 #include "gr.h"
123 #include "wall.h"
124 #include "switch.h"
125 #include "inferno.h"
126 #include "segment.h"
127 #include "error.h"
128 #include "gameseg.h"
129 #include "game.h"
130 #include "bm.h"
131 #include "vclip.h"
132 #include "player.h"
133 #include "gauges.h"
134 #include "text.h"
135 #include "fireball.h"
136 #include "textures.h"
137 #include "sounds.h"
138 #include "newdemo.h"
139 #include "multi.h"
140 #include "gameseq.h"
141 #include "laser.h"              //      For seeing if a flare is stuck in a wall.
142 #include "collide.h"
143 #include "effects.h"
144
145 #ifdef EDITOR
146 #include "editor/editor.h"
147 #endif
148
149 //      Special door on boss level which is locked if not in multiplayer...sorry for this awful solution --MK.
150 #define BOSS_LOCKED_DOOR_LEVEL  7
151 #define BOSS_LOCKED_DOOR_SEG            595
152 #define BOSS_LOCKED_DOOR_SIDE   5
153
154 wall Walls[MAX_WALLS];                                  // Master walls array
155 int Num_walls=0;                                                        // Number of walls
156
157 wclip WallAnims[MAX_WALL_ANIMS];                // Wall animations
158 int Num_wall_anims;
159 //--unused-- int walls_bm_num[MAX_WALL_ANIMS];
160
161 //door Doors[MAX_DOORS];                                        //      Master doors array
162
163 active_door ActiveDoors[MAX_DOORS];
164 int Num_open_doors;                                             // Number of open doors
165
166 #define CLOAKING_WALL_TIME f1_0
167
168 #define MAX_CLOAKING_WALLS 10
169 cloaking_wall CloakingWalls[MAX_CLOAKING_WALLS];
170 int Num_cloaking_walls;
171
172 //--unused-- grs_bitmap *wall_title_bms[MAX_WALL_ANIMS];
173
174 //#define BM_FLAG_TRANSPARENT                   1
175 //#define BM_FLAG_SUPER_TRANSPARENT     2
176
177 #ifdef EDITOR
178 char    Wall_names[7][10] = {
179         "NORMAL   ",
180         "BLASTABLE",
181         "DOOR     ",
182         "ILLUSION ",
183         "OPEN     ",
184         "CLOSED   ",
185         "EXTERNAL "
186 };
187 #endif
188
189 // Function prototypes
190 void kill_stuck_objects(int wallnum);
191
192
193
194 // This function determines whether the current segment/side is transparent
195 //              1 = YES
196 //              0 = NO
197 int check_transparency( segment * seg, int side )
198 {
199         if ( (seg->sides[side].tmap_num2 & 0x3FFF) == 0) {
200                 if (GameBitmaps[Textures[seg->sides[side].tmap_num].index].bm_flags & BM_FLAG_TRANSPARENT )
201                         return 1;
202                 else
203                         return 0;
204                 }
205
206         if (GameBitmaps[Textures[seg->sides[side].tmap_num2 & 0x3FFF ].index].bm_flags & BM_FLAG_SUPER_TRANSPARENT )
207                 return 1;
208         else
209                 return 0;
210 }
211
212 //define these here so I don't have to change wall_is_doorway and run
213 //the risk of screwing it up.
214 #define WID_WALL                                                2       // 0/1/0                wall    
215 #define WID_TRANSPARENT_WALL            6       //      0/1/1           transparent wall
216 #define WID_ILLUSORY_WALL                       3       //      1/1/0           illusory wall
217 #define WID_TRANSILLUSORY_WALL  7       //      1/1/1           transparent illusory wall
218 #define WID_NO_WALL                                     5       //      1/0/1           no wall, can fly through
219 #define WID_EXTERNAL                                    8       // 0/0/0/1      don't see it, dont fly through it
220
221 //-----------------------------------------------------------------
222 // This function checks whether we can fly through the given side.
223 //      In other words, whether or not we have a 'doorway'
224 //       Flags:
225 //              WID_FLY_FLAG                            1
226 //              WID_RENDER_FLAG                 2
227 //              WID_RENDPAST_FLAG                       4
228 //       Return values:
229 //              WID_WALL                                                2       // 0/1/0                wall    
230 //              WID_TRANSPARENT_WALL            6       //      0/1/1           transparent wall
231 //              WID_ILLUSORY_WALL                       3       //      1/1/0           illusory wall
232 //              WID_TRANSILLUSORY_WALL  7       //      1/1/1           transparent illusory wall
233 //              WID_NO_WALL                                     5       //      1/0/1           no wall, can fly through
234 int wall_is_doorway ( segment * seg, int side )
235 {
236         int flags, type;
237         int state;
238 //--Covered by macro    // No child.
239 //--Covered by macro    if (seg->children[side] == -1)
240 //--Covered by macro            return WID_WALL;
241
242 //--Covered by macro    if (seg->children[side] == -2)
243 //--Covered by macro            return WID_EXTERNAL_FLAG;
244
245 //--Covered by macro // No wall present.
246 //--Covered by macro    if (seg->sides[side].wall_num == -1)
247 //--Covered by macro            return WID_NO_WALL;
248
249         Assert(seg-Segments>=0 && seg-Segments<=Highest_segment_index);
250         Assert(side>=0 && side<6);
251
252         type = Walls[seg->sides[side].wall_num].type;
253         flags = Walls[seg->sides[side].wall_num].flags;
254
255         if (type == WALL_OPEN)
256                 return WID_NO_WALL;
257
258         if (type == WALL_ILLUSION) {
259                 if (Walls[seg->sides[side].wall_num].flags & WALL_ILLUSION_OFF)
260                         return WID_NO_WALL;
261                 else {
262                         if (check_transparency( seg, side))
263                                 return WID_TRANSILLUSORY_WALL;
264                         else
265                                 return WID_ILLUSORY_WALL;
266                 }
267         }
268
269         if (type == WALL_BLASTABLE) {
270                 if (flags & WALL_BLASTED)
271                         return WID_TRANSILLUSORY_WALL;
272
273                 if (check_transparency( seg, side))
274                         return WID_TRANSPARENT_WALL;
275                 else
276                         return WID_WALL;
277         }       
278         
279         if (flags & WALL_DOOR_OPENED)
280                 return WID_TRANSILLUSORY_WALL;
281         
282         if (type == WALL_CLOAKED)
283                 return WID_RENDER_FLAG | WID_RENDPAST_FLAG | WID_CLOAKED_FLAG;
284
285         state = Walls[seg->sides[side].wall_num].state;
286         if ((type == WALL_DOOR) && (state == WALL_DOOR_OPENING))
287                 return WID_TRANSPARENT_WALL;
288         
289 // If none of the above flags are set, there is no doorway.
290         if (check_transparency( seg, side))
291                 return WID_TRANSPARENT_WALL;
292         else
293                 return WID_WALL; // There are children behind the door.
294 }
295
296 #ifdef EDITOR
297 //-----------------------------------------------------------------
298 // Initializes all the walls (in other words, no special walls)
299 void wall_init()
300 {
301         int i;
302         
303         Num_walls = 0;
304         for (i=0;i<MAX_WALLS;i++) {
305                 Walls[i].segnum = Walls[i].sidenum = -1;
306                 Walls[i].type = WALL_NORMAL;
307                 Walls[i].flags = 0;
308                 Walls[i].hps = 0;
309                 Walls[i].trigger = -1;
310                 Walls[i].clip_num = -1;
311                 Walls[i].linked_wall = -1;
312                 }
313         Num_open_doors = 0;
314         Num_cloaking_walls = 0;
315
316 }
317
318 //-----------------------------------------------------------------
319 // Initializes one wall.
320 void wall_reset(segment *seg, int side)
321 {
322         int i;
323         
324         i = seg->sides[side].wall_num;
325
326         if (i==-1) {
327                 mprintf((0, "Resetting Illegal Wall\n"));
328                 return;
329         }
330
331         Walls[i].segnum = seg-Segments;
332         Walls[i].sidenum = side;
333         Walls[i].type = WALL_NORMAL;
334         Walls[i].flags = 0;
335         Walls[i].hps = 0;
336         Walls[i].trigger = -1;
337         Walls[i].clip_num = -1;
338         Walls[i].linked_wall = -1;
339 }
340 #endif
341
342 //set the tmap_num or tmap_num2 field for a wall/door
343 void wall_set_tmap_num(segment *seg,int side,segment *csegp,int cside,int anim_num,int frame_num)
344 {
345         wclip *anim = &WallAnims[anim_num];
346         int tmap = anim->frames[frame_num];
347
348         if ( Newdemo_state==ND_STATE_PLAYBACK ) return;
349
350         if (anim->flags & WCF_TMAP1)    {
351                 seg->sides[side].tmap_num = csegp->sides[cside].tmap_num = tmap;
352                 if ( Newdemo_state == ND_STATE_RECORDING )
353                         newdemo_record_wall_set_tmap_num1(seg-Segments,side,csegp-Segments,cside,tmap);
354         } else  {
355                 Assert(tmap!=0 && seg->sides[side].tmap_num2!=0);
356                 seg->sides[side].tmap_num2 = csegp->sides[cside].tmap_num2 = tmap;
357                 if ( Newdemo_state == ND_STATE_RECORDING )
358                         newdemo_record_wall_set_tmap_num2(seg-Segments,side,csegp-Segments,cside,tmap);
359         }
360 }
361
362
363 // -------------------------------------------------------------------------------
364 //when the wall has used all its hitpoints, this will destroy it
365 void blast_blastable_wall(segment *seg, int side)
366 {
367         int Connectside;
368         segment *csegp;
369         int a, n;
370
371         Assert(seg->sides[side].wall_num != -1);
372
373         Walls[seg->sides[side].wall_num].hps = -1;      //say it's blasted
374
375         csegp = &Segments[seg->children[side]];
376         Connectside = find_connect_side(seg, csegp);
377         Assert(Connectside != -1);
378
379         kill_stuck_objects(seg->sides[side].wall_num);
380         kill_stuck_objects(csegp->sides[Connectside].wall_num);
381
382         //if this is an exploding wall, explode it
383         if (WallAnims[Walls[seg->sides[side].wall_num].clip_num].flags & WCF_EXPLODES)
384                 explode_wall(seg-Segments,side);
385         else {
386                 //if not exploding, set final frame, and make door passable
387                 a = Walls[seg->sides[side].wall_num].clip_num;
388                 n = WallAnims[a].num_frames;
389                 wall_set_tmap_num(seg,side,csegp,Connectside,a,n-1);
390                 Walls[seg->sides[side].wall_num].flags |= WALL_BLASTED;
391                 Walls[csegp->sides[Connectside].wall_num].flags |= WALL_BLASTED;
392         }
393
394 }
395
396
397 //-----------------------------------------------------------------
398 // Destroys a blastable wall.
399 void wall_destroy(segment *seg, int side)
400 {
401         Assert(seg->sides[side].wall_num != -1);
402         Assert(seg-Segments != 0);
403
404         if (Walls[seg->sides[side].wall_num].type == WALL_BLASTABLE)
405                 blast_blastable_wall( seg, side );
406         else
407                 Error("Hey bub, you are trying to destroy an indestructable wall.");
408 }
409
410 //-----------------------------------------------------------------
411 // Deteriorate appearance of wall. (Changes bitmap (paste-ons))
412 void wall_damage(segment *seg, int side, fix damage)
413 {
414         int a, i, n;
415
416         if (seg->sides[side].wall_num == -1) {
417                 mprintf((0, "Damaging illegal wall\n"));
418                 return;
419         }
420
421         if (Walls[seg->sides[side].wall_num].type != WALL_BLASTABLE)
422                 return;
423         
424         if (!(Walls[seg->sides[side].wall_num].flags & WALL_BLASTED) && Walls[seg->sides[side].wall_num].hps >= 0)
425                 {
426                 int Connectside;
427                 segment *csegp;
428
429                 csegp = &Segments[seg->children[side]];
430                 Connectside = find_connect_side(seg, csegp);
431                 Assert(Connectside != -1);
432                 
433                 Walls[seg->sides[side].wall_num].hps -= damage;
434                 Walls[csegp->sides[Connectside].wall_num].hps -= damage;
435                         
436                 a = Walls[seg->sides[side].wall_num].clip_num;
437                 n = WallAnims[a].num_frames;
438                 
439                 if (Walls[seg->sides[side].wall_num].hps < WALL_HPS*1/n) {
440                         blast_blastable_wall( seg, side );                      
441                         #ifdef NETWORK
442                         if (Game_mode & GM_MULTI)
443                                 multi_send_door_open(seg-Segments, side,Walls[seg->sides[side].wall_num].flags);
444                         #endif
445                 }
446                 else
447                         for (i=0;i<n;i++)
448                                 if (Walls[seg->sides[side].wall_num].hps < WALL_HPS*(n-i)/n) {
449                                         wall_set_tmap_num(seg,side,csegp,Connectside,a,i);
450                                 }
451                 }
452 }
453
454
455 //-----------------------------------------------------------------
456 // Opens a door
457 void wall_open_door(segment *seg, int side)
458 {
459         wall *w;
460         active_door *d;
461         int Connectside;
462         segment *csegp;
463
464         Assert(seg->sides[side].wall_num != -1);        //Opening door on illegal wall
465
466         w = &Walls[seg->sides[side].wall_num];
467
468         //kill_stuck_objects(seg->sides[side].wall_num);
469
470         if ((w->state == WALL_DOOR_OPENING) ||          //already opening
471                  (w->state == WALL_DOOR_WAITING)        ||              //open, waiting to close
472                  (w->state == WALL_DOOR_OPEN))                  //open, & staying open
473                 return;
474
475         if (w->state == WALL_DOOR_CLOSING) {            //closing, so reuse door
476
477                 int i;
478         
479                 d = NULL;
480
481                 for (i=0;i<Num_open_doors;i++) {                //find door
482
483                         d = &ActiveDoors[i];
484         
485                         if (d->front_wallnum[0]==w-Walls || d->back_wallnum[0]==w-Walls || (d->n_parts==2 && (d->front_wallnum[1]==w-Walls || d->back_wallnum[1]==w-Walls)))
486                                 break;
487                 }
488
489                 if (i>=Num_open_doors && (Game_mode & GM_MULTI))        
490                                 goto FastFix;
491                 
492                 Assert(i<Num_open_doors);                               //didn't find door!
493                 Assert( d!=NULL ); // Get John!
494
495                 d->time = WallAnims[w->clip_num].play_time - d->time;
496
497                 if (d->time < 0)
498                         d->time = 0;
499         
500         }
501         else {                                                                                  //create new door
502                 Assert(w->state == WALL_DOOR_CLOSED);
503                 FastFix:
504                 d = &ActiveDoors[Num_open_doors];
505                 d->time = 0;
506                 Num_open_doors++;
507                 Assert( Num_open_doors < MAX_DOORS );
508         }
509
510
511         w->state = WALL_DOOR_OPENING;
512
513         // So that door can't be shot while opening
514         csegp = &Segments[seg->children[side]];
515         Connectside = find_connect_side(seg, csegp);
516         Assert(Connectside != -1);
517
518         Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_OPENING;
519
520         //kill_stuck_objects(csegp->sides[Connectside].wall_num);
521
522         d->front_wallnum[0] = seg->sides[side].wall_num;
523         d->back_wallnum[0] = csegp->sides[Connectside].wall_num;
524
525         Assert( seg-Segments != -1);
526
527         if (Newdemo_state == ND_STATE_RECORDING) {
528                 newdemo_record_door_opening(seg-Segments, side);
529         }
530
531         if (w->linked_wall != -1) {
532                 wall *w2;
533                 segment *seg2;
534
535                 w2              = &Walls[w->linked_wall];
536                 seg2    = &Segments[w2->segnum];
537
538                 Assert(w2->linked_wall == seg->sides[side].wall_num);
539                 //Assert(!(w2->flags & WALL_DOOR_OPENING  ||  w2->flags & WALL_DOOR_OPENED));
540
541                 w2->state = WALL_DOOR_OPENING;
542
543                 csegp = &Segments[seg2->children[w2->sidenum]];
544                 Connectside = find_connect_side(seg2, csegp);
545                 Assert(Connectside != -1);
546                 Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_OPENING;
547
548                 d->n_parts = 2;
549                 d->front_wallnum[1] = w->linked_wall;
550                 d->back_wallnum[1] = csegp->sides[Connectside].wall_num;
551         }
552         else
553                 d->n_parts = 1;
554
555
556         if ( Newdemo_state != ND_STATE_PLAYBACK )
557         {
558                 // NOTE THE LINK TO ABOVE!!!!
559                 vms_vector cp;
560                 compute_center_point_on_side(&cp, seg, side );
561                 if (WallAnims[w->clip_num].open_sound > -1 )
562                         digi_link_sound_to_pos( WallAnims[w->clip_num].open_sound, seg-Segments, side, &cp, 0, F1_0 );
563
564         }
565 }
566
567 //-----------------------------------------------------------------
568 // start the transition from closed -> open wall
569 void start_wall_cloak(segment *seg, int side)
570 {
571         wall *w;
572         cloaking_wall *d;
573         int Connectside;
574         segment *csegp;
575         int i;
576
577         if ( Newdemo_state==ND_STATE_PLAYBACK ) return;
578
579         Assert(seg->sides[side].wall_num != -1);        //Opening door on illegal wall
580
581         w = &Walls[seg->sides[side].wall_num];
582
583         if (w->type == WALL_OPEN || w->state == WALL_DOOR_CLOAKING)             //already open or cloaking
584                 return;
585
586         csegp = &Segments[seg->children[side]];
587         Connectside = find_connect_side(seg, csegp);
588         Assert(Connectside != -1);
589
590         if (w->state == WALL_DOOR_DECLOAKING) { //decloaking, so reuse door
591
592                 int i;
593
594                 d = NULL;
595
596                 for (i=0;i<Num_cloaking_walls;i++) {            //find door
597
598                         d = &CloakingWalls[i];
599         
600                         if (d->front_wallnum==w-Walls || d->back_wallnum==w-Walls )
601                                 break;
602                 }
603
604                 Assert(i<Num_cloaking_walls);                           //didn't find door!
605                 Assert( d!=NULL ); // Get John!
606
607                 d->time = CLOAKING_WALL_TIME - d->time;
608
609         }
610         else if (w->state == WALL_DOOR_CLOSED) {        //create new door
611                 d = &CloakingWalls[Num_cloaking_walls];
612                 d->time = 0;
613                 if (Num_cloaking_walls >= MAX_CLOAKING_WALLS) {         //no more!
614                         Int3();         //ran out of cloaking wall slots
615                         w->type = WALL_OPEN;
616                         Walls[csegp->sides[Connectside].wall_num].type = WALL_OPEN;
617                         return;
618                 }
619                 Num_cloaking_walls++;
620         }
621         else {
622                 Int3();         //unexpected wall state
623                 return;
624         }
625
626         w->state = WALL_DOOR_CLOAKING;
627         Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_CLOAKING;
628
629         d->front_wallnum = seg->sides[side].wall_num;
630         d->back_wallnum = csegp->sides[Connectside].wall_num;
631
632         Assert( seg-Segments != -1);
633
634         Assert(w->linked_wall == -1);
635
636         if ( Newdemo_state != ND_STATE_PLAYBACK ) {
637                 vms_vector cp;
638                 compute_center_point_on_side(&cp, seg, side );
639                 digi_link_sound_to_pos( SOUND_WALL_CLOAK_ON, seg-Segments, side, &cp, 0, F1_0 );
640         }
641
642         for (i=0;i<4;i++) {
643                 d->front_ls[i] = seg->sides[side].uvls[i].l;
644                 d->back_ls[i] = csegp->sides[Connectside].uvls[i].l;
645         }
646 }
647
648 //-----------------------------------------------------------------
649 // start the transition from open -> closed wall
650 void start_wall_decloak(segment *seg, int side)
651 {
652         wall *w;
653         cloaking_wall *d;
654         int Connectside;
655         segment *csegp;
656         int i;
657
658         if ( Newdemo_state==ND_STATE_PLAYBACK ) return;
659
660         Assert(seg->sides[side].wall_num != -1);        //Opening door on illegal wall
661
662         w = &Walls[seg->sides[side].wall_num];
663
664         if (w->type == WALL_CLOSED || w->state == WALL_DOOR_DECLOAKING)         //already closed or decloaking
665                 return;
666
667         if (w->state == WALL_DOOR_CLOAKING) {   //cloaking, so reuse door
668
669                 int i;
670
671                 d = NULL;
672
673                 for (i=0;i<Num_cloaking_walls;i++) {            //find door
674
675                         d = &CloakingWalls[i];
676         
677                         if (d->front_wallnum==w-Walls || d->back_wallnum==w-Walls )
678                                 break;
679                 }
680
681                 Assert(i<Num_cloaking_walls);                           //didn't find door!
682                 Assert( d!=NULL ); // Get John!
683
684                 d->time = CLOAKING_WALL_TIME - d->time;
685
686         }
687         else if (w->state == WALL_DOOR_CLOSED) {        //create new door
688                 d = &CloakingWalls[Num_cloaking_walls];
689                 d->time = 0;
690                 if (Num_cloaking_walls >= MAX_CLOAKING_WALLS) {         //no more!
691                         Int3();         //ran out of cloaking wall slots
692                         /* what is this _doing_ here?
693                         w->type = WALL_CLOSED;
694                         Walls[csegp->sides[Connectside].wall_num].type = WALL_CLOSED;
695                         */
696                         return;
697                 }
698                 Num_cloaking_walls++;
699         }
700         else {
701                 Int3();         //unexpected wall state
702                 return;
703         }
704
705         w->state = WALL_DOOR_DECLOAKING;
706
707         // So that door can't be shot while opening
708         csegp = &Segments[seg->children[side]];
709         Connectside = find_connect_side(seg, csegp);
710         Assert(Connectside != -1);
711
712         Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_DECLOAKING;
713
714         d->front_wallnum = seg->sides[side].wall_num;
715         d->back_wallnum = csegp->sides[Connectside].wall_num;
716
717         Assert( seg-Segments != -1);
718
719         Assert(w->linked_wall == -1);
720
721         if ( Newdemo_state != ND_STATE_PLAYBACK ) {
722                 vms_vector cp;
723                 compute_center_point_on_side(&cp, seg, side );
724                 digi_link_sound_to_pos( SOUND_WALL_CLOAK_OFF, seg-Segments, side, &cp, 0, F1_0 );
725         }
726
727         for (i=0;i<4;i++) {
728                 d->front_ls[i] = seg->sides[side].uvls[i].l;
729                 d->back_ls[i] = csegp->sides[Connectside].uvls[i].l;
730         }
731 }
732
733 //-----------------------------------------------------------------
734 // This function closes the specified door and restores the closed
735 //  door texture.  This is called when the animation is done
736 void wall_close_door_num(int door_num)
737 {
738         int p;
739         active_door *d;
740         int i;
741
742         d = &ActiveDoors[door_num];
743
744         for (p=0;p<d->n_parts;p++) {
745                 wall *w;
746                 int Connectside, side;
747                 segment *csegp, *seg;
748         
749                 w = &Walls[d->front_wallnum[p]];
750
751                 seg = &Segments[w->segnum];
752                 side = w->sidenum;
753         
754                 Assert(seg->sides[side].wall_num != -1);                //Closing door on illegal wall
755                 
756                 csegp = &Segments[seg->children[side]];
757                 Connectside = find_connect_side(seg, csegp);
758                 Assert(Connectside != -1);
759         
760                 Walls[seg->sides[side].wall_num].state = WALL_DOOR_CLOSED;
761                 Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_CLOSED;
762         
763                 wall_set_tmap_num(seg,side,csegp,Connectside,w->clip_num,0);
764
765         }
766         
767         for (i=door_num;i<Num_open_doors;i++)
768                 ActiveDoors[i] = ActiveDoors[i+1];
769
770         Num_open_doors--;
771
772 }
773
774 int check_poke(int objnum,int segnum,int side)
775 {
776         object *obj = &Objects[objnum];
777
778         //note: don't let objects with zero size block door
779
780         if (obj->size && get_seg_masks(&obj->pos,segnum,obj->size).sidemask & (1<<side))
781                 return 1;               //pokes through side!
782         else
783                 return 0;               //does not!
784
785 }
786
787 //returns true of door in unobjstructed (& thus can close)
788 int is_door_free(segment *seg,int side)
789 {
790         int Connectside;
791         segment *csegp;
792         int objnum;
793         
794         csegp = &Segments[seg->children[side]];
795         Connectside = find_connect_side(seg, csegp);
796         Assert(Connectside != -1);
797
798         //go through each object in each of two segments, and see if
799         //it pokes into the connecting seg
800
801         for (objnum=seg->objects;objnum!=-1;objnum=Objects[objnum].next)
802                 if (Objects[objnum].type!=OBJ_WEAPON && Objects[objnum].type!=OBJ_FIREBALL && check_poke(objnum,seg-Segments,side))
803                         return 0;       //not free
804
805         for (objnum=csegp->objects;objnum!=-1;objnum=Objects[objnum].next)
806                 if (Objects[objnum].type!=OBJ_WEAPON && Objects[objnum].type!=OBJ_FIREBALL && check_poke(objnum,csegp-Segments,Connectside))
807                         return 0;       //not free
808
809         return 1;       //doorway is free!
810 }
811
812
813
814 //-----------------------------------------------------------------
815 // Closes a door
816 void wall_close_door(segment *seg, int side)
817 {
818         wall *w;
819         active_door *d;
820         int Connectside;
821         segment *csegp;
822
823         Assert(seg->sides[side].wall_num != -1);        //Opening door on illegal wall
824
825         w = &Walls[seg->sides[side].wall_num];
826
827         if ((w->state == WALL_DOOR_CLOSING) ||          //already closing
828                  (w->state == WALL_DOOR_WAITING)        ||              //open, waiting to close
829                  (w->state == WALL_DOOR_CLOSED))                        //closed
830                 return;
831
832         if (!is_door_free(seg,side))
833                 return;
834
835         if (w->state == WALL_DOOR_OPENING) {    //reuse door
836
837                 int i;
838         
839                 d = NULL;
840
841                 for (i=0;i<Num_open_doors;i++) {                //find door
842
843                         d = &ActiveDoors[i];
844         
845                         if (d->front_wallnum[0]==w-Walls || d->back_wallnum[0]==w-Walls || (d->n_parts==2 && (d->front_wallnum[1]==w-Walls || d->back_wallnum[1]==w-Walls)))
846                                 break;
847                 }
848
849                 Assert(i<Num_open_doors);                               //didn't find door!
850                 Assert( d!=NULL ); // Get John!
851
852                 d->time = WallAnims[w->clip_num].play_time - d->time;
853
854                 if (d->time < 0)
855                         d->time = 0;
856         
857         }
858         else {                                                                                  //create new door
859                 Assert(w->state == WALL_DOOR_OPEN);
860                 d = &ActiveDoors[Num_open_doors];
861                 d->time = 0;
862                 Num_open_doors++;
863                 Assert( Num_open_doors < MAX_DOORS );
864         }
865
866         w->state = WALL_DOOR_CLOSING;
867
868         // So that door can't be shot while opening
869         csegp = &Segments[seg->children[side]];
870         Connectside = find_connect_side(seg, csegp);
871         Assert(Connectside != -1);
872
873         Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_CLOSING;
874
875         d->front_wallnum[0] = seg->sides[side].wall_num;
876         d->back_wallnum[0] = csegp->sides[Connectside].wall_num;
877
878         Assert( seg-Segments != -1);
879
880         if (Newdemo_state == ND_STATE_RECORDING) {
881                 newdemo_record_door_opening(seg-Segments, side);
882         }
883
884         if (w->linked_wall != -1) {
885                 Int3();         //don't think we ever used linked walls
886         }
887         else
888                 d->n_parts = 1;
889
890
891         if ( Newdemo_state != ND_STATE_PLAYBACK )
892         {
893                 // NOTE THE LINK TO ABOVE!!!!
894                 vms_vector cp;
895                 compute_center_point_on_side(&cp, seg, side );
896                 if (WallAnims[w->clip_num].open_sound > -1 )
897                         digi_link_sound_to_pos( WallAnims[w->clip_num].open_sound, seg-Segments, side, &cp, 0, F1_0 );
898
899         }
900 }
901
902 //-----------------------------------------------------------------
903 // Animates opening of a door.
904 // Called in the game loop.
905 void do_door_open(int door_num)
906 {
907         int p;
908         active_door *d;
909
910         Assert(door_num != -1);         //Trying to do_door_open on illegal door
911         
912         d = &ActiveDoors[door_num];
913
914         for (p=0;p<d->n_parts;p++) {
915                 wall *w;
916                 int Connectside, side;
917                 segment *csegp, *seg;
918                 fix time_elapsed, time_total, one_frame;
919                 int i, n;
920         
921                 w = &Walls[d->front_wallnum[p]];
922                 kill_stuck_objects(d->front_wallnum[p]);
923                 kill_stuck_objects(d->back_wallnum[p]);
924
925                 seg = &Segments[w->segnum];
926                 side = w->sidenum;
927         
928                 Assert(seg->sides[side].wall_num != -1);                //Trying to do_door_open on illegal wall
929         
930                 csegp = &Segments[seg->children[side]];
931                 Connectside = find_connect_side(seg, csegp);
932                 Assert(Connectside != -1);
933
934                 d->time += FrameTime;
935         
936                 time_elapsed = d->time;
937                 n = WallAnims[w->clip_num].num_frames;
938                 time_total = WallAnims[w->clip_num].play_time;
939         
940                 one_frame = time_total/n;       
941         
942                 i = time_elapsed/one_frame;
943         
944                 if (i < n)
945                         wall_set_tmap_num(seg,side,csegp,Connectside,w->clip_num,i);
946         
947                 if (i> n/2) {
948                         Walls[seg->sides[side].wall_num].flags |= WALL_DOOR_OPENED;
949                         Walls[csegp->sides[Connectside].wall_num].flags |= WALL_DOOR_OPENED;
950                 }
951         
952                 if (i >= n-1) {
953                         wall_set_tmap_num(seg,side,csegp,Connectside,w->clip_num,n-1);
954
955                         // If our door is not automatic just remove it from the list.
956                         if (!(Walls[seg->sides[side].wall_num].flags & WALL_DOOR_AUTO)) {
957                                 for (i=door_num;i<Num_open_doors;i++)
958                                         ActiveDoors[i] = ActiveDoors[i+1];
959                                 Num_open_doors--;
960                                 Walls[seg->sides[side].wall_num].state = WALL_DOOR_OPEN;
961                                 Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_OPEN;
962                         }
963                         else {
964
965                                 Walls[seg->sides[side].wall_num].state = WALL_DOOR_WAITING;
966                                 Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_WAITING;
967
968                                 ActiveDoors[Num_open_doors].time = 0;   //counts up
969                         }
970                 }
971
972         }
973
974 }
975
976 //-----------------------------------------------------------------
977 // Animates and processes the closing of a door.
978 // Called from the game loop.
979 void do_door_close(int door_num)
980 {
981         int p;
982         active_door *d;
983         wall *w;
984
985         Assert(door_num != -1);         //Trying to do_door_open on illegal door
986         
987         d = &ActiveDoors[door_num];
988
989         w = &Walls[d->front_wallnum[0]];
990
991         //check for objects in doorway before closing
992         if (w->flags & WALL_DOOR_AUTO)
993                 if (!is_door_free(&Segments[w->segnum],w->sidenum)) {
994                         digi_kill_sound_linked_to_segment(w->segnum,w->sidenum,-1);
995                         wall_open_door(&Segments[w->segnum],w->sidenum);                //re-open door
996                         return;
997                 }
998
999         for (p=0;p<d->n_parts;p++) {
1000                 wall *w;
1001                 int Connectside, side;
1002                 segment *csegp, *seg;
1003                 fix time_elapsed, time_total, one_frame;
1004                 int i, n;
1005         
1006                 w = &Walls[d->front_wallnum[p]];
1007
1008                 seg = &Segments[w->segnum];
1009                 side = w->sidenum;
1010         
1011                 if (seg->sides[side].wall_num == -1) {
1012                         mprintf((0, "Trying to do_door_close on Illegal wall\n"));
1013                         return;
1014                 }
1015         
1016                 //if here, must be auto door
1017 //              Assert(Walls[seg->sides[side].wall_num].flags & WALL_DOOR_AUTO);                
1018 //don't assert here, because now we have triggers to close non-auto doors
1019         
1020                 // Otherwise, close it.
1021                 csegp = &Segments[seg->children[side]];
1022                 Connectside = find_connect_side(seg, csegp);
1023                 Assert(Connectside != -1);
1024         
1025
1026                 if ( Newdemo_state != ND_STATE_PLAYBACK )
1027                         // NOTE THE LINK TO ABOVE!!
1028                         if (p==0)       //only play one sound for linked doors
1029                                 if ( d->time==0 )       {               //first time
1030                                         vms_vector cp;
1031                                         compute_center_point_on_side(&cp, seg, side );
1032                                         if (WallAnims[w->clip_num].close_sound  > -1 )
1033                                                 digi_link_sound_to_pos( WallAnims[Walls[seg->sides[side].wall_num].clip_num].close_sound, seg-Segments, side, &cp, 0, F1_0 );
1034                                 }
1035         
1036                 d->time += FrameTime;
1037
1038                 time_elapsed = d->time;
1039                 n = WallAnims[w->clip_num].num_frames;
1040                 time_total = WallAnims[w->clip_num].play_time;
1041         
1042                 one_frame = time_total/n;       
1043         
1044                 i = n-time_elapsed/one_frame-1;
1045         
1046                 if (i < n/2) {
1047                         Walls[seg->sides[side].wall_num].flags &= ~WALL_DOOR_OPENED;
1048                         Walls[csegp->sides[Connectside].wall_num].flags &= ~WALL_DOOR_OPENED;
1049                 }
1050         
1051                 // Animate door.
1052                 if (i > 0) {
1053                         wall_set_tmap_num(seg,side,csegp,Connectside,w->clip_num,i);
1054
1055                         Walls[seg->sides[side].wall_num].state = WALL_DOOR_CLOSING;
1056                         Walls[csegp->sides[Connectside].wall_num].state = WALL_DOOR_CLOSING;
1057
1058                         ActiveDoors[Num_open_doors].time = 0;           //counts up
1059
1060                 } else
1061                         wall_close_door_num(door_num);
1062         }
1063 }
1064
1065
1066 //-----------------------------------------------------------------
1067 // Turns off an illusionary wall (This will be used primarily for
1068 //  wall switches or triggers that can turn on/off illusionary walls.)
1069 void wall_illusion_off(segment *seg, int side)
1070 {
1071         segment *csegp;
1072         int cside;
1073
1074         csegp = &Segments[seg->children[side]];
1075         cside = find_connect_side(seg, csegp);
1076         Assert(cside != -1);
1077
1078         if (seg->sides[side].wall_num == -1) {
1079                 mprintf((0, "Trying to shut off illusion illegal wall\n"));
1080                 return;
1081         }
1082
1083         Walls[seg->sides[side].wall_num].flags |= WALL_ILLUSION_OFF;
1084         Walls[csegp->sides[cside].wall_num].flags |= WALL_ILLUSION_OFF;
1085
1086         kill_stuck_objects(seg->sides[side].wall_num);
1087         kill_stuck_objects(csegp->sides[cside].wall_num);
1088 }
1089
1090 //-----------------------------------------------------------------
1091 // Turns on an illusionary wall (This will be used primarily for
1092 //  wall switches or triggers that can turn on/off illusionary walls.)
1093 void wall_illusion_on(segment *seg, int side)
1094 {
1095         segment *csegp;
1096         int cside;
1097
1098         csegp = &Segments[seg->children[side]];
1099         cside = find_connect_side(seg, csegp);
1100         Assert(cside != -1);
1101
1102         if (seg->sides[side].wall_num == -1) {
1103                 mprintf((0, "Trying to turn on illusion illegal wall\n"));
1104                 return;
1105         }
1106
1107         Walls[seg->sides[side].wall_num].flags &= ~WALL_ILLUSION_OFF;
1108         Walls[csegp->sides[cside].wall_num].flags &= ~WALL_ILLUSION_OFF;
1109 }
1110
1111 //      -----------------------------------------------------------------------------
1112 //      Allowed to open the normally locked special boss door if in multiplayer mode.
1113 int special_boss_opening_allowed(int segnum, int sidenum)
1114 {
1115         if (Game_mode & GM_MULTI)
1116                 return (Current_level_num == BOSS_LOCKED_DOOR_LEVEL) && (segnum == BOSS_LOCKED_DOOR_SEG) && (sidenum == BOSS_LOCKED_DOOR_SIDE);
1117         else
1118                 return 0;
1119 }
1120
1121 //-----------------------------------------------------------------
1122 // Determines what happens when a wall is shot
1123 //returns info about wall.  see wall.h for codes
1124 //obj is the object that hit...either a weapon or the player himself
1125 //playernum is the number the player who hit the wall or fired the weapon,
1126 //or -1 if a robot fired the weapon
1127 int wall_hit_process(segment *seg, int side, fix damage, int playernum, object *obj )
1128 {
1129         wall    *w;
1130         fix     show_message;
1131
1132         Assert (seg-Segments != -1);
1133
1134         // If it is not a "wall" then just return.
1135         if ( seg->sides[side].wall_num < 0 )
1136                 return WHP_NOT_SPECIAL;
1137
1138         w = &Walls[seg->sides[side].wall_num];
1139
1140         if ( Newdemo_state == ND_STATE_RECORDING )
1141                 newdemo_record_wall_hit_process( seg-Segments, side, damage, playernum );
1142
1143         if (w->type == WALL_BLASTABLE) {
1144                 if (obj->ctype.laser_info.parent_type == OBJ_PLAYER)
1145                         wall_damage(seg, side, damage);
1146                 return WHP_BLASTABLE;
1147         }
1148
1149         if (playernum != Player_num)    //return if was robot fire
1150                 return WHP_NOT_SPECIAL;
1151
1152         Assert( playernum > -1 );
1153         
1154         //      Determine whether player is moving forward.  If not, don't say negative
1155         //      messages because he probably didn't intentionally hit the door.
1156         if (obj->type == OBJ_PLAYER)
1157                 show_message = (vm_vec_dot(&obj->orient.fvec, &obj->mtype.phys_info.velocity) > 0);
1158         else if (obj->type == OBJ_ROBOT)
1159                 show_message = 0;
1160         else if ((obj->type == OBJ_WEAPON) && (obj->ctype.laser_info.parent_type == OBJ_ROBOT))
1161                 show_message = 0;
1162         else
1163                 show_message = 1;
1164
1165         if (w->keys == KEY_BLUE)
1166                 if (!(Players[playernum].flags & PLAYER_FLAGS_BLUE_KEY)) {
1167                         if ( playernum==Player_num )
1168                                 if (show_message)
1169                                         HUD_init_message("%s %s",TXT_BLUE,TXT_ACCESS_DENIED);
1170                         return WHP_NO_KEY;
1171                 }
1172
1173         if (w->keys == KEY_RED)
1174                 if (!(Players[playernum].flags & PLAYER_FLAGS_RED_KEY)) {
1175                         if ( playernum==Player_num )
1176                                 if (show_message)
1177                                         HUD_init_message("%s %s",TXT_RED,TXT_ACCESS_DENIED);
1178                         return WHP_NO_KEY;
1179                 }
1180         
1181         if (w->keys == KEY_GOLD)
1182                 if (!(Players[playernum].flags & PLAYER_FLAGS_GOLD_KEY)) {
1183                         if ( playernum==Player_num )
1184                                 if (show_message)
1185                                         HUD_init_message("%s %s",TXT_YELLOW,TXT_ACCESS_DENIED);
1186                         return WHP_NO_KEY;
1187                 }
1188
1189         if (w->type == WALL_DOOR)
1190         {
1191                 if ((w->flags & WALL_DOOR_LOCKED ) && !(special_boss_opening_allowed(seg-Segments, side)) ) {
1192                         if ( playernum==Player_num )
1193                                 if (show_message)
1194                                         HUD_init_message(TXT_CANT_OPEN_DOOR);
1195                         return WHP_NO_KEY;
1196                 }
1197                 else {
1198                         if (w->state != WALL_DOOR_OPENING)
1199                         {
1200                                 wall_open_door(seg, side);
1201                         #ifdef NETWORK
1202                                 if (Game_mode & GM_MULTI)
1203                                         multi_send_door_open(seg-Segments, side,w->flags);
1204                         #endif
1205                         }
1206                         return WHP_DOOR;
1207                         
1208                 }
1209         }
1210
1211         return WHP_NOT_SPECIAL;         //default is treat like normal wall
1212 }
1213
1214 //-----------------------------------------------------------------
1215 // Opens doors/destroys wall/shuts off triggers.
1216 void wall_toggle(segment *seg, int side)
1217 {
1218         int wall_num;
1219
1220         if (seg - Segments > Highest_segment_index)
1221         {
1222                 Warning("Can't toggle side %d of segment %d - nonexistent segment!\n", side, seg-Segments);
1223                 return;
1224         }
1225         Assert( side < MAX_SIDES_PER_SEGMENT );
1226
1227         wall_num = seg->sides[side].wall_num;
1228
1229         if (wall_num == -1) {
1230                 mprintf((0, "Illegal wall_toggle\n"));
1231                 return;
1232         }
1233
1234         if ( Newdemo_state == ND_STATE_RECORDING )
1235                 newdemo_record_wall_toggle(seg-Segments, side );
1236
1237         if (Walls[wall_num].type == WALL_BLASTABLE)
1238                 wall_destroy(seg, side);
1239
1240         if ((Walls[wall_num].type == WALL_DOOR) && (Walls[wall_num].state == WALL_DOOR_CLOSED))
1241                 wall_open_door(seg, side);
1242
1243 }
1244
1245
1246 //-----------------------------------------------------------------
1247 // Tidy up Walls array for load/save purposes.
1248 void reset_walls()
1249 {
1250         int i;
1251
1252         if (Num_walls < 0) {
1253                 mprintf((0, "Illegal Num_walls\n"));
1254                 return;
1255         }
1256
1257         for (i=Num_walls;i<MAX_WALLS;i++) {
1258                 Walls[i].type = WALL_NORMAL;
1259                 Walls[i].flags = 0;
1260                 Walls[i].hps = 0;
1261                 Walls[i].trigger = -1;
1262                 Walls[i].clip_num = -1;
1263                 }
1264 }
1265
1266 void do_cloaking_wall_frame(int cloaking_wall_num)
1267 {
1268         cloaking_wall *d;
1269         wall *wfront,*wback;
1270
1271         if ( Newdemo_state==ND_STATE_PLAYBACK ) return;
1272
1273         d = &CloakingWalls[cloaking_wall_num];
1274         wfront = &Walls[d->front_wallnum];
1275         wback = &Walls[d->back_wallnum];
1276
1277         d->time += FrameTime;
1278
1279         if (d->time > CLOAKING_WALL_TIME) {
1280                 int i;
1281
1282                 wfront->type = wback->type = WALL_OPEN;
1283                 wfront->state = wback->state = WALL_DOOR_CLOSED;                //why closed? why not?
1284
1285                 for (i=cloaking_wall_num;i<Num_cloaking_walls;i++)
1286                         CloakingWalls[i] = CloakingWalls[i+1];
1287                 Num_cloaking_walls--;
1288
1289         }
1290         else if (d->time > CLOAKING_WALL_TIME/2) {
1291                 int old_type=wfront->type;
1292
1293                 wfront->cloak_value = wback->cloak_value = ((d->time - CLOAKING_WALL_TIME/2) * (GR_FADE_LEVELS-2)) / (CLOAKING_WALL_TIME/2);
1294
1295                 if (old_type != WALL_CLOAKED) {         //just switched
1296                         int i;
1297
1298                         wfront->type = wback->type = WALL_CLOAKED;
1299
1300                         for (i=0;i<4;i++) {
1301                                 Segments[wfront->segnum].sides[wfront->sidenum].uvls[i].l = d->front_ls[i];
1302                                 Segments[wback->segnum].sides[wback->sidenum].uvls[i].l = d->back_ls[i];
1303                         }
1304                 }
1305         }
1306         else {          //fading out
1307                 fix light_scale;
1308                 int i;
1309
1310                 light_scale = fixdiv(CLOAKING_WALL_TIME/2-d->time,CLOAKING_WALL_TIME/2);
1311
1312                 for (i=0;i<4;i++) {
1313                         Segments[wfront->segnum].sides[wfront->sidenum].uvls[i].l = fixmul(d->front_ls[i],light_scale);
1314                         Segments[wback->segnum].sides[wback->sidenum].uvls[i].l = fixmul(d->back_ls[i],light_scale);
1315                 }
1316         }
1317
1318         if ( Newdemo_state == ND_STATE_RECORDING )
1319                 newdemo_record_cloaking_wall(d->front_wallnum, d->back_wallnum, wfront->type, wfront->state, wfront->cloak_value, Segments[wfront->segnum].sides[wfront->sidenum].uvls[0].l, Segments[wfront->segnum].sides[wfront->sidenum].uvls[1].l, Segments[wfront->segnum].sides[wfront->sidenum].uvls[2].l, Segments[wfront->segnum].sides[wfront->sidenum].uvls[3].l);
1320
1321 }
1322
1323 void do_decloaking_wall_frame(int cloaking_wall_num)
1324 {
1325         cloaking_wall *d;
1326         wall *wfront,*wback;
1327
1328         if ( Newdemo_state==ND_STATE_PLAYBACK ) return;
1329
1330         d = &CloakingWalls[cloaking_wall_num];
1331         wfront = &Walls[d->front_wallnum];
1332         wback = &Walls[d->back_wallnum];
1333
1334         d->time += FrameTime;
1335
1336         if (d->time > CLOAKING_WALL_TIME) {
1337                 int i;
1338
1339                 wfront->state = wback->state = WALL_DOOR_CLOSED;
1340
1341                 for (i=0;i<4;i++) {
1342                         Segments[wfront->segnum].sides[wfront->sidenum].uvls[i].l = d->front_ls[i];
1343                         Segments[wback->segnum].sides[wback->sidenum].uvls[i].l = d->back_ls[i];
1344                 }
1345
1346                 for (i=cloaking_wall_num;i<Num_cloaking_walls;i++)
1347                         CloakingWalls[i] = CloakingWalls[i+1];
1348                 Num_cloaking_walls--;
1349
1350         }
1351         else if (d->time > CLOAKING_WALL_TIME/2) {              //fading in
1352                 fix light_scale;
1353                 int i;
1354
1355                 wfront->type = wback->type = WALL_CLOSED;
1356
1357                 light_scale = fixdiv(d->time-CLOAKING_WALL_TIME/2,CLOAKING_WALL_TIME/2);
1358
1359                 for (i=0;i<4;i++) {
1360                         Segments[wfront->segnum].sides[wfront->sidenum].uvls[i].l = fixmul(d->front_ls[i],light_scale);
1361                         Segments[wback->segnum].sides[wback->sidenum].uvls[i].l = fixmul(d->back_ls[i],light_scale);
1362                 }
1363         }
1364         else {          //cloaking in
1365                 wfront->cloak_value = wback->cloak_value = ((CLOAKING_WALL_TIME/2 - d->time) * (GR_FADE_LEVELS-2)) / (CLOAKING_WALL_TIME/2);
1366                 wfront->type = wback->type = WALL_CLOAKED;
1367         }
1368
1369         if ( Newdemo_state == ND_STATE_RECORDING )
1370                 newdemo_record_cloaking_wall(d->front_wallnum, d->back_wallnum, wfront->type, wfront->state, wfront->cloak_value, Segments[wfront->segnum].sides[wfront->sidenum].uvls[0].l, Segments[wfront->segnum].sides[wfront->sidenum].uvls[1].l, Segments[wfront->segnum].sides[wfront->sidenum].uvls[2].l, Segments[wfront->segnum].sides[wfront->sidenum].uvls[3].l);
1371
1372 }
1373
1374 void wall_frame_process()
1375 {
1376         int i;
1377
1378         for (i=0;i<Num_open_doors;i++) {
1379                 active_door *d;
1380                 wall *w;
1381
1382                 d = &ActiveDoors[i];
1383                 w = &Walls[d->front_wallnum[0]];
1384
1385                 if (w->state == WALL_DOOR_OPENING)
1386                         do_door_open(i);
1387                 else if (w->state == WALL_DOOR_CLOSING)
1388                         do_door_close(i);
1389                 else if (w->state == WALL_DOOR_WAITING) {
1390                         d->time += FrameTime;
1391
1392                         //set flags to fix occatsional netgame problem where door is
1393                         //waiting to close but open flag isn't set
1394                         Assert(d->n_parts == 1);
1395                         w->flags |= WALL_DOOR_OPENED;
1396                         Walls[d->back_wallnum[0]].flags |= WALL_DOOR_OPENED;
1397
1398                         if (d->time > DOOR_WAIT_TIME && is_door_free(&Segments[w->segnum],w->sidenum)) {
1399                                 w->state = WALL_DOOR_CLOSING;
1400                                 d->time = 0;
1401                         }
1402                 }
1403                 else if (w->state == WALL_DOOR_CLOSED || w->state == WALL_DOOR_OPEN) {
1404                         //this shouldn't happen.  if the wall is in one of these states,
1405                         //there shouldn't be an activedoor entry for it.  So we'll kill
1406                         //the activedoor entry.  Tres simple.
1407                         int t;
1408                         Int3();         //a bad thing has happened, but I'll try to fix it up
1409                         for (t=i;t<Num_open_doors;t++)
1410                                 ActiveDoors[t] = ActiveDoors[t+1];
1411                         Num_open_doors--;
1412                 }
1413         }
1414
1415         for (i=0;i<Num_cloaking_walls;i++) {
1416                 cloaking_wall *d;
1417                 wall *w;
1418
1419                 d = &CloakingWalls[i];
1420                 w = &Walls[d->front_wallnum];
1421
1422                 if (w->state == WALL_DOOR_CLOAKING)
1423                         do_cloaking_wall_frame(i);
1424                 else if (w->state == WALL_DOOR_DECLOAKING)
1425                         do_decloaking_wall_frame(i);
1426                 else
1427                         Int3(); //unexpected wall state
1428         }
1429 }
1430
1431 int     Num_stuck_objects=0;
1432
1433 stuckobj        Stuck_objects[MAX_STUCK_OBJECTS];
1434
1435 //      An object got stuck in a door (like a flare).
1436 //      Add global entry.
1437 void add_stuck_object(object *objp, int segnum, int sidenum)
1438 {
1439         int     i;
1440         int     wallnum;
1441
1442         wallnum = Segments[segnum].sides[sidenum].wall_num;
1443
1444         if (wallnum != -1) {
1445                 if (Walls[wallnum].flags & WALL_BLASTED)
1446                         objp->flags |= OF_SHOULD_BE_DEAD;
1447
1448                 for (i=0; i<MAX_STUCK_OBJECTS; i++) {
1449                         if (Stuck_objects[i].wallnum == -1) {
1450                                 Stuck_objects[i].wallnum = wallnum;
1451                                 Stuck_objects[i].objnum = objp-Objects;
1452                                 Stuck_objects[i].signature = objp->signature;
1453                                 // mprintf((0, "Added wall %i at index %i\n", wallnum, i));
1454                                 Num_stuck_objects++;
1455                                 break;
1456                         }
1457                 }
1458                 if (i == MAX_STUCK_OBJECTS)
1459                         mprintf((1, "Warning: Unable to add object %i which got stuck in wall %i to Stuck_objects\n", objp-Objects, wallnum));
1460         }
1461
1462
1463
1464 }
1465
1466 //      --------------------------------------------------------------------------------------------------
1467 //      Look at the list of stuck objects, clean up in case an object has gone away, but not been removed here.
1468 //      Removes up to one/frame.
1469 void remove_obsolete_stuck_objects(void)
1470 {
1471         int     objnum;
1472
1473         //      Safety and efficiency code.  If no stuck objects, should never get inside the IF, but this is faster.
1474         if (!Num_stuck_objects)
1475                 return;
1476
1477         objnum = FrameCount % MAX_STUCK_OBJECTS;
1478
1479         if (Stuck_objects[objnum].wallnum != -1)
1480                 if ((Walls[Stuck_objects[objnum].wallnum].state != WALL_DOOR_CLOSED) || (Objects[Stuck_objects[objnum].objnum].signature != Stuck_objects[objnum].signature)) {
1481                         Num_stuck_objects--;
1482                         Objects[Stuck_objects[objnum].objnum].lifeleft = F1_0/8;
1483                         Stuck_objects[objnum].wallnum = -1;
1484                 }
1485
1486 }
1487
1488 extern void flush_fcd_cache(void);
1489
1490 //      ----------------------------------------------------------------------------------------------------
1491 //      Door with wall index wallnum is opening, kill all objects stuck in it.
1492 void kill_stuck_objects(int wallnum)
1493 {
1494         int     i;
1495
1496         if (Num_stuck_objects == 0) {
1497                 return;
1498         }
1499
1500         Num_stuck_objects=0;
1501
1502         for (i=0; i<MAX_STUCK_OBJECTS; i++)
1503                 if (Stuck_objects[i].wallnum == wallnum) {
1504                         if (Objects[Stuck_objects[i].objnum].type == OBJ_WEAPON) {
1505                                 Objects[Stuck_objects[i].objnum].lifeleft = F1_0/8;
1506                         } else
1507                                 mprintf((1, "Warning: Stuck object of type %i, expected to be of type %i, see wall.c\n", Objects[Stuck_objects[i].objnum].type, OBJ_WEAPON));
1508                                 // Int3();      //      What?  This looks bad.  Object is not a weapon and it is stuck in a wall!
1509                         Stuck_objects[i].wallnum = -1;
1510                 } else if (Stuck_objects[i].wallnum != -1) {
1511                         Num_stuck_objects++;
1512                 }
1513         //      Ok, this is awful, but we need to do things whenever a door opens/closes/disappears, etc.
1514         flush_fcd_cache();
1515
1516 }
1517
1518
1519 // -- unused -- // -----------------------------------------------------------------------------------
1520 // -- unused -- //      Return object id of first flare found embedded in segp:sidenum.
1521 // -- unused -- //      If no flare, return -1.
1522 // -- unused -- int contains_flare(segment *segp, int sidenum)
1523 // -- unused -- {
1524 // -- unused --         int     i;
1525 // -- unused --
1526 // -- unused --         for (i=0; i<Num_stuck_objects; i++) {
1527 // -- unused --                 object  *objp = &Objects[Stuck_objects[i].objnum];
1528 // -- unused --
1529 // -- unused --                 if ((objp->type == OBJ_WEAPON) && (objp->id == FLARE_ID)) {
1530 // -- unused --                         if (Walls[Stuck_objects[i].wallnum].segnum == segp-Segments)
1531 // -- unused --                                 if (Walls[Stuck_objects[i].wallnum].sidenum == sidenum)
1532 // -- unused --                                         return objp-Objects;
1533 // -- unused --                 }
1534 // -- unused --         }
1535 // -- unused --
1536 // -- unused --         return -1;
1537 // -- unused -- }
1538
1539 // -----------------------------------------------------------------------------------
1540 // Initialize stuck objects array.  Called at start of level
1541 void init_stuck_objects(void)
1542 {
1543         int     i;
1544
1545         for (i=0; i<MAX_STUCK_OBJECTS; i++)
1546                 Stuck_objects[i].wallnum = -1;
1547
1548         Num_stuck_objects = 0;
1549 }
1550
1551 // -----------------------------------------------------------------------------------
1552 // Clear out all stuck objects.  Called for a new ship
1553 void clear_stuck_objects(void)
1554 {
1555         int     i;
1556
1557         for (i=0; i<MAX_STUCK_OBJECTS; i++) {
1558                 if (Stuck_objects[i].wallnum != -1) {
1559                         int     objnum;
1560
1561                         objnum = Stuck_objects[i].objnum;
1562
1563                         if ((Objects[objnum].type == OBJ_WEAPON) && (Objects[objnum].id == FLARE_ID))
1564                                 Objects[objnum].lifeleft = F1_0/8;
1565
1566                         Stuck_objects[i].wallnum = -1;
1567
1568                         Num_stuck_objects--;
1569                 }
1570         }
1571
1572         Assert(Num_stuck_objects == 0);
1573
1574 }
1575
1576 // -----------------------------------------------------------------------------------
1577 #define MAX_BLAST_GLASS_DEPTH   5
1578
1579 void bng_process_segment(object *objp, fix damage, segment *segp, int depth, sbyte *visited)
1580 {
1581         int     i, sidenum;
1582
1583         if (depth > MAX_BLAST_GLASS_DEPTH)
1584                 return;
1585
1586         depth++;
1587
1588         for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
1589                 int                     tm;
1590                 fix                     dist;
1591                 vms_vector      pnt;
1592
1593                 //      Process only walls which have glass.
1594                 if ((tm=segp->sides[sidenum].tmap_num2) != 0) {
1595                         int     ec, db;
1596
1597                         tm &= 0x3fff;                   //tm without flags
1598
1599                         if ((((ec=TmapInfo[tm].eclip_num)!=-1) && ((db=Effects[ec].dest_bm_num)!=-1 && !(Effects[ec].flags&EF_ONE_SHOT))) ||    (ec==-1 && (TmapInfo[tm].destroyed!=-1))) {
1600                                 compute_center_point_on_side(&pnt, segp, sidenum);
1601                                 dist = vm_vec_dist_quick(&pnt, &objp->pos);
1602                                 if (dist < damage/2) {
1603                                         dist = find_connected_distance(&pnt, segp-Segments, &objp->pos, objp->segnum, MAX_BLAST_GLASS_DEPTH, WID_RENDPAST_FLAG);
1604                                         if ((dist > 0) && (dist < damage/2))
1605                                                 check_effect_blowup(segp, sidenum, &pnt, &Objects[objp->ctype.laser_info.parent_num], 1);
1606                                 }
1607                         }
1608                 }
1609         }
1610
1611         for (i=0; i<MAX_SIDES_PER_SEGMENT; i++) {
1612                 int     segnum = segp->children[i];
1613
1614                 if (segnum != -1) {
1615                         if (!visited[segnum]) {
1616                                 if (WALL_IS_DOORWAY(segp, i) & WID_FLY_FLAG) {
1617                                         visited[segnum] = 1;
1618                                         bng_process_segment(objp, damage, &Segments[segnum], depth, visited);
1619                                 }
1620                         }
1621                 }
1622         }
1623 }
1624
1625 // -----------------------------------------------------------------------------------
1626 //      objp is going to detonate
1627 //      blast nearby monitors, lights, maybe other things
1628 void blast_nearby_glass(object *objp, fix damage)
1629 {
1630         int             i;
1631         sbyte   visited[MAX_SEGMENTS];
1632         segment *cursegp;
1633
1634         cursegp = &Segments[objp->segnum];
1635         for (i=0; i<=Highest_segment_index; i++)
1636                 visited[i] = 0;
1637
1638         visited[objp->segnum] = 1;
1639         bng_process_segment(objp, damage, cursegp, 0, visited);
1640
1641
1642 }
1643
1644 #define MAX_CLIP_FRAMES_D1 20
1645
1646 /*
1647  * reads a wclip structure from a CFILE
1648  */
1649 int wclip_read_n_d1(wclip *wc, int n, CFILE *fp)
1650 {
1651         int i, j;
1652
1653         for (i = 0; i < n; i++) {
1654                 wc[i].play_time = cfile_read_fix(fp);
1655                 wc[i].num_frames = cfile_read_short(fp);
1656                 for (j = 0; j < MAX_CLIP_FRAMES_D1; j++)
1657                         wc[i].frames[j] = cfile_read_short(fp);
1658                 wc[i].open_sound = cfile_read_short(fp);
1659                 wc[i].close_sound = cfile_read_short(fp);
1660                 wc[i].flags = cfile_read_short(fp);
1661                 cfread(wc[i].filename, 13, 1, fp);
1662                 wc[i].pad = cfile_read_byte(fp);
1663         }
1664         return i;
1665 }
1666
1667 #ifndef FAST_FILE_IO
1668 /*
1669  * reads a wclip structure from a CFILE
1670  */
1671 int wclip_read_n(wclip *wc, int n, CFILE *fp)
1672 {
1673         int i, j;
1674
1675         for (i = 0; i < n; i++) {
1676                 wc[i].play_time = cfile_read_fix(fp);
1677                 wc[i].num_frames = cfile_read_short(fp);
1678                 for (j = 0; j < MAX_CLIP_FRAMES; j++)
1679                         wc[i].frames[j] = cfile_read_short(fp);
1680                 wc[i].open_sound = cfile_read_short(fp);
1681                 wc[i].close_sound = cfile_read_short(fp);
1682                 wc[i].flags = cfile_read_short(fp);
1683                 cfread(wc[i].filename, 13, 1, fp);
1684                 wc[i].pad = cfile_read_byte(fp);
1685         }
1686         return i;
1687 }
1688
1689 /*
1690  * reads a v16_wall structure from a CFILE
1691  */
1692 extern void v16_wall_read(v16_wall *w, CFILE *fp)
1693 {
1694         w->type = cfile_read_byte(fp);
1695         w->flags = cfile_read_byte(fp);
1696         w->hps = cfile_read_fix(fp);
1697         w->trigger = cfile_read_byte(fp);
1698         w->clip_num = cfile_read_byte(fp);
1699         w->keys = cfile_read_byte(fp);
1700 }
1701
1702 /*
1703  * reads a v19_wall structure from a CFILE
1704  */
1705 extern void v19_wall_read(v19_wall *w, CFILE *fp)
1706 {
1707         w->segnum = cfile_read_int(fp);
1708         w->sidenum = cfile_read_int(fp);
1709         w->type = cfile_read_byte(fp);
1710         w->flags = cfile_read_byte(fp);
1711         w->hps = cfile_read_fix(fp);
1712         w->trigger = cfile_read_byte(fp);
1713         w->clip_num = cfile_read_byte(fp);
1714         w->keys = cfile_read_byte(fp);
1715         w->linked_wall = cfile_read_int(fp);
1716 }
1717
1718 /*
1719  * reads a wall structure from a CFILE
1720  */
1721 extern void wall_read(wall *w, CFILE *fp)
1722 {
1723         w->segnum = cfile_read_int(fp);
1724         w->sidenum = cfile_read_int(fp);
1725         w->hps = cfile_read_fix(fp);
1726         w->linked_wall = cfile_read_int(fp);
1727         w->type = cfile_read_byte(fp);
1728         w->flags = cfile_read_byte(fp);
1729         w->state = cfile_read_byte(fp);
1730         w->trigger = cfile_read_byte(fp);
1731         w->clip_num = cfile_read_byte(fp);
1732         w->keys = cfile_read_byte(fp);
1733         w->controlling_trigger = cfile_read_byte(fp);
1734         w->cloak_value = cfile_read_byte(fp);
1735 }
1736
1737 /*
1738  * reads a v19_door structure from a CFILE
1739  */
1740 extern void v19_door_read(v19_door *d, CFILE *fp)
1741 {
1742         d->n_parts = cfile_read_int(fp);
1743         d->seg[0] = cfile_read_short(fp);
1744         d->seg[1] = cfile_read_short(fp);
1745         d->side[0] = cfile_read_short(fp);
1746         d->side[1] = cfile_read_short(fp);
1747         d->type[0] = cfile_read_short(fp);
1748         d->type[1] = cfile_read_short(fp);
1749         d->open = cfile_read_fix(fp);
1750 }
1751
1752 /*
1753  * reads an active_door structure from a CFILE
1754  */
1755 extern void active_door_read(active_door *ad, CFILE *fp)
1756 {
1757         ad->n_parts = cfile_read_int(fp);
1758         ad->front_wallnum[0] = cfile_read_short(fp);
1759         ad->front_wallnum[1] = cfile_read_short(fp);
1760         ad->back_wallnum[0] = cfile_read_short(fp);
1761         ad->back_wallnum[1] = cfile_read_short(fp);
1762         ad->time = cfile_read_fix(fp);
1763 }
1764 #endif