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