]> icculus.org git repositories - taylor/freespace2.git/blob - src/ship/shield.cpp
added copyright header
[taylor/freespace2.git] / src / ship / shield.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Ship/Shield.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  *      Stuff pertaining to shield graphical effects, etc.
16  *
17  * $Log$
18  * Revision 1.3  2002/06/09 04:41:26  relnev
19  * added copyright header
20  *
21  * Revision 1.2  2002/05/07 03:16:52  theoddone33
22  * The Great Newline Fix
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:10  root
25  * Initial import.
26  *
27  * 
28  * 11    8/03/99 1:46p Dave
29  * Make opacity higher.
30  * 
31  * 10    8/03/99 11:28a Dave
32  * Fixed shield problem.
33  * 
34  * 9     8/02/99 10:42p Dave
35  * Make shields less opaque in the nebula.
36  * 
37  * 8     8/02/99 10:39p Dave
38  * Added colored shields. OoOoOoooOoo
39  * 
40  * 7     7/15/99 9:20a Andsager
41  * FS2_DEMO initial checkin
42  * 
43  * 6     3/10/99 6:50p Dave
44  * Changed the way we buffer packets for all clients. Optimized turret
45  * fired packets. Did some weapon firing optimizations.
46  * 
47  * 5     1/12/99 5:45p Dave
48  * Moved weapon pipeline in multiplayer to almost exclusively client side.
49  * Very good results. Bandwidth goes down, playability goes up for crappy
50  * connections. Fixed object update problem for ship subsystems.
51  * 
52  * 4     12/01/98 8:06a Dave
53  * Temporary checkin to fix some texture transparency problems in d3d.
54  * 
55  * 3     11/05/98 5:55p Dave
56  * Big pass at reducing #includes
57  * 
58  * 2     10/07/98 10:53a Dave
59  * Initial checkin.
60  * 
61  * 1     10/07/98 10:51a Dave
62  * 
63  * 65    5/25/98 12:12a Mike
64  * Make shield effect less CPU-intensive in software.  Always use lower
65  * detail versions.
66  * 
67  * 64    4/22/98 8:37p Mike
68  * Recover gracefully from exhausted MAX_GLOBAL_TRIS asserts.
69  * 
70  * 63    4/09/98 7:58p John
71  * Cleaned up tmapper code a bit.   Put NDEBUG around some ndebug stuff.
72  * Took out XPARENT flag settings in all the alpha-blended texture stuff.
73  * 
74  * 62    4/02/98 6:28p Lawrance
75  * remove shield code from demo build
76  * 
77  * 61    4/02/98 11:40a Lawrance
78  * check for #ifdef DEMO instead of #ifdef DEMO_RELEASE
79  * 
80  * 60    3/31/98 5:32p Mike
81  * Reduce size of some buffers.
82  * 
83  * 59    3/31/98 5:19p John
84  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
85  * bunch of debug stuff out of player file.  Made model code be able to
86  * unload models and malloc out only however many models are needed.
87  *  
88  * 
89  * 58    3/30/98 4:02p John
90  * Made machines with < 32 MB of RAM use every other frame of certain
91  * bitmaps.   Put in code to keep track of how much RAM we've malloc'd.
92  * 
93  * 57    3/30/98 12:18a Lawrance
94  * change some DEMO_RELEASE code to not compile code rather than return
95  * early
96  * 
97  * 56    3/29/98 11:33p Mike
98  * Speedup sheidl hit effect at detail levels 1, 3 (in 0..4).
99  * 
100  * 55    3/29/98 5:02p Adam
101  * changed ANIs referenced for shieldhit effect to all be
102  * shieldhit01a.ani.  There won't be different ones for different species
103  * like I once thought.
104  * 
105  * 54    3/29/98 4:05p John
106  * New paging code that loads everything necessary at level startup.
107  * 
108  * 53    3/27/98 9:06p Mike
109  * Make shield effect larger in lower detail levels (to match size in
110  * higher detail level) and handle changing detail level whiel shield
111  * effect playing.
112  * 
113  * 52    3/27/98 8:35p Mike
114  * Detail level support in shield effect system.
115  * 
116  * 51    3/16/98 4:07p Sandeep
117  * 
118  * 50    3/12/98 1:24p Mike
119  * When weapons linked, increase firing delay.
120  * Free up weapon slots for AI ships, if necessary.
121  * Backside render shield effect.
122  * Remove shield hit triangle if offscreen.
123  * 
124  * 49    3/02/98 5:42p John
125  * Removed WinAVI stuff from Freespace.  Made all HUD gauges wriggle from
126  * afterburner.  Made gr_set_clip work good with negative x &y.  Made
127  * model_caching be on by default.  Made each cached model have it's own
128  * bitmap id.  Made asteroids not rotate when model_caching is on.  
129  * 
130  * 48    2/22/98 4:17p John
131  * More string externalization classification... 190 left to go!
132  * 
133  * 47    2/14/98 11:13p Mike
134  * Optimize a bit by making old ones go away if Poly_count high.
135  * 
136  * 46    2/06/98 9:10a Allender
137  * removed an Assert for multiplayer clients
138  * 
139  * 45    2/05/98 9:21p John
140  * Some new Direct3D code.   Added code to monitor a ton of stuff in the
141  * game.
142  * 
143  * 44    1/28/98 11:15p Allender
144  * work on getting shield effect to show correctly in client side
145  * multiplayer
146  * 
147  * 43    12/30/97 4:26p Lawrance
148  * Remove .ani from shield animations, avoids bmpman warning
149  * 
150  * $NoKeywords: $
151  */
152
153 //      Detail level effects (Detail.shield_effects)
154 //              0               Nothing rendered
155 //              1               An animating bitmap rendered per hit, not shrink-wrapped.  Lasts half time.  One per ship.
156 //              2               Animating bitmap per hit, not shrink-wrapped.  Lasts full time.  Unlimited.
157 //              3               Shrink-wrapped texture.  Lasts half-time.
158 //              4               Shrink-wrapped texture.  Lasts full-time.
159
160 #include <math.h>
161
162 #include "2d.h"
163 #include "3d.h"
164 #include "model.h"
165 #include "tmapper.h"
166 #include "floating.h"
167 #include "fvi.h"
168 #include "lighting.h"
169 #include "fireballs.h"
170 #include "fix.h"
171 #include "bmpman.h"
172 #include        "object.h"
173 #include "player.h"             //      #include of "player.h" is only for debugging!
174 #include "timer.h"
175 #include "freespace.h"
176 #include "packunpack.h"
177 #include "animplay.h"
178 #include "shiphit.h"
179 #include "missionparse.h"
180 #include "multimsgs.h"
181 #include "multi.h"
182
183 int     New_shield_system = 1;
184 int     Show_shield_mesh = 0;
185
186 #ifndef DEMO // not for FS2_DEMO
187
188 //      One unit in 3d means this in the shield hit texture map.
189 //#define       SHIELD_HIT_SCALE        0.075f                  //      Scale decreased by MK on 12/18/97, made about 1/4x as large. Note, larger constant means smaller effect
190 #define SHIELD_HIT_SCALE        0.15f                   //      Doubled on 12/23/97 by MK.  Was overflowing.  See todo item #924.
191 //#define       MAX_SHIELD_HITS 20
192 #define MAX_TRIS_PER_HIT        40                                      //      Number of triangles per shield hit, maximum.
193 #define MAX_SHIELD_HITS 20                                      //      Maximum number of active shield hits.
194 #define MAX_SHIELD_TRI_BUFFER   (MAX_SHIELD_HITS*20) // Persistent buffer of triangle comprising all active shield hits.
195 #define SHIELD_HIT_DURATION     (3*F1_0/4)      //      Duration, in milliseconds, of shield hit effect
196
197 #define SH_UNUSED                       -1                                      //      Indicates an unused record in Shield_hits
198 #define SH_TYPE_1                       1                                       //      Indicates Shield_hits record is of type 1.
199
200 #define UV_MAX                          (63.95f/64.0f)  //      max allowed value until tmapper bugs fixed, 1/24/97
201
202 float   Shield_scale = SHIELD_HIT_SCALE;
203
204 //      Structure which mimics the shield_tri structure in model.h.  Since the global shield triangle
205 // array needs the vertex information, we will acutally store the information in this
206 // structure instead of the indices into the vertex list
207 typedef struct gshield_tri {
208         int                             used;                                                   //      Set if this triangle is currently in use.
209         int                             trinum;                                         //      a debug parameter
210         fix                             creation_time;                          //      time at which created.
211         shield_vertex   verts[4];                                       //      Triangles, but at lower detail level, a square.
212 } gshield_tri;
213
214 typedef struct shield_hit {
215         int     start_time;                                                             //      start time of this object
216         int     type;                                                                           //      type, probably the weapon type, to indicate the bitmap to use
217         int     objnum;                                                                 //      Object index, needed to get current orientation, position.
218         int     num_tris;                                                               //      Number of Shield_tris comprising this shield.
219         int     tri_list[MAX_TRIS_PER_HIT];             //      Indices into Shield_tris, triangles for this shield hit.
220         ubyte rgb[3];                                                                   // rgb colors
221 } shield_hit;
222
223 //      Stores point at which shield was hit.
224 //      Gets processed in frame interval.
225 typedef struct shield_point {
226         int             objnum;                                                         //      Object that was hit.
227         int             shield_tri;                                                     //      Triangle in shield mesh that took hit.
228         vector  hit_point;                                                      //      Point in global 3-space of hit.
229 } shield_point;
230
231 #define MAX_SHIELD_POINTS       100
232 shield_point    Shield_points[MAX_SHIELD_POINTS];
233 int             Num_shield_points;
234 int             Num_multi_shield_points;                                        // used by multiplayer clients
235
236 gshield_tri     Global_tris[MAX_SHIELD_TRI_BUFFER];     //      The persistent triangles, part of shield hits.
237 int     Num_tris;                                                               //      Number of triangles in current shield.  Would be a local, but needed in numerous routines.
238
239 shield_hit      Shield_hits[MAX_SHIELD_HITS];
240
241 typedef struct shield_ani {
242         char            *filename;
243         int             first_frame;
244         int             nframes;
245 } shield_ani;
246
247 //XSTR:OFF
248 #define MAX_SHIELD_ANIMS MAX_SPECIES_NAMES
249 shield_ani Sheild_ani[MAX_SHIELD_ANIMS] = {
250         { "shieldhit01a", -1, -1 },
251         { "shieldhit01a", -1, -1 },
252         { "shieldhit01a", -1, -1 },
253 };
254 //XSTR:ON
255
256 int Shield_bitmaps_loaded = 0;
257
258 //      This is a recursive function, so prototype it.
259 extern void create_shield_from_triangle(int trinum, matrix *orient, shield_info *shieldp, vector *tcp, vector *centerp, float radius, vector *rvec, vector *uvec);
260
261 void load_shield_hit_bitmap()
262 {
263         #ifndef DEMO // not for FS2_DEMO
264
265         int i;
266         // Check if we've already allocated the shield effect bitmaps
267         if ( Shield_bitmaps_loaded )
268                 return;
269
270         Shield_bitmaps_loaded = 1;
271
272         for (i=0; i<MAX_SHIELD_ANIMS; i++ )     {
273                 Sheild_ani[i].first_frame = bm_load_animation(Sheild_ani[i].filename, &Sheild_ani[i].nframes,NULL, 1);
274                 if ( Sheild_ani[i].first_frame < 0 )
275                         Int3();
276         }
277
278         #endif
279 }
280
281 void shield_hit_page_in()
282 {
283         int i;
284
285         if ( !Shield_bitmaps_loaded )   {
286                 load_shield_hit_bitmap();
287         }
288
289         for (i=0; i<MAX_SHIELD_ANIMS; i++ )     {
290                 bm_page_in_xparent_texture( Sheild_ani[i].first_frame, Sheild_ani[i].nframes );
291         }
292 }
293
294
295 //      Initialize shield hit system.  Called from game_level_init()
296 void shield_hit_init()
297 {
298         int     i;
299
300         for (i=0; i<MAX_SHIELD_HITS; i++)
301                 Shield_hits[i].type = SH_UNUSED;
302
303         for (i=0; i<MAX_SHIELD_TRI_BUFFER; i++) {
304                 Global_tris[i].used = 0;
305                 Global_tris[i].creation_time = Missiontime;
306         }
307
308         Num_multi_shield_points = 0;
309
310         load_shield_hit_bitmap();
311 }
312
313 // ---------------------------------------------------------------------
314 // release_shield_hit_bitmap()
315 //
316 // Release the storage allocated to store the shield effect.
317 //
318 void release_shield_hit_bitmap()
319 {
320         if ( !Shield_bitmaps_loaded )
321                 return;
322
323         // This doesn't need to do anything; the bitmap manager will
324         // release everything.
325 }
326
327 int     Poly_count = 0;
328
329 // ---------------------------------------------------------------------
330 // shield_hit_close()
331 //
332 // De-initalize the shield hit system.  Called from game_level_close().
333 //
334 // TODO: We should probably not bother releasing the shield hit bitmaps every level.
335 //
336 void shield_hit_close()
337 {
338         release_shield_hit_bitmap();
339 }
340
341 void shield_frame_init()
342 {
343         //nprintf(("AI", "Frame %i: Number of shield hits: %i, polycount = %i\n", Framecount, Num_shield_points, Poly_count));
344
345         Poly_count = 0;
346
347         Num_shield_points = 0;
348 }
349
350 void create_low_detail_poly(int global_index, vector *tcp, vector *rightv, vector *upv)
351 {
352         float           scale;
353         gshield_tri     *trip;
354
355         trip = &Global_tris[global_index];
356
357         scale = vm_vec_mag(tcp) * 2.0f;
358
359         vm_vec_scale_add(&trip->verts[0].pos, tcp, rightv, -scale/2.0f);
360         vm_vec_scale_add2(&trip->verts[0].pos, upv, scale/2.0f);
361
362         vm_vec_scale_add(&trip->verts[1].pos, &trip->verts[0].pos, rightv, scale);
363
364         vm_vec_scale_add(&trip->verts[2].pos, &trip->verts[1].pos, upv, -scale);
365
366         vm_vec_scale_add(&trip->verts[3].pos, &trip->verts[2].pos, rightv, -scale);
367
368         //      Set u, v coordinates.
369         //      Note, this need only be done once, as it's common for all explosions.
370         trip->verts[0].u = 0.0f;
371         trip->verts[0].v = 0.0f;
372
373         trip->verts[1].u = 1.0f;
374         trip->verts[1].v = 0.0f;
375
376         trip->verts[2].u = 1.0f;
377         trip->verts[2].v = 1.0f;
378
379         trip->verts[3].u = 0.0f;
380         trip->verts[3].v = 1.0f;
381
382 }
383
384 //      ----------------------------------------------------------------------------------------------------
385 //      Given a shield triangle, compute the uv coordinates at its vertices given
386 //      the center point of the explosion texture, distance to center of shield and
387 //      right and up vectors.
388 //      For small distances (relative to radius), coordinates can be computed using
389 //      distance.  For larger values, should comptue angle.
390 void rs_compute_uvs(shield_tri *stp, shield_vertex *verts, vector *tcp, float radius, vector *rightv, vector *upv)
391 {
392         int     i;
393         shield_vertex *sv;
394
395         for (i=0; i<3; i++) {
396                 vector  v2cp;
397
398                 sv = &verts[stp->verts[i]];
399
400                 vm_vec_sub(&v2cp, &sv->pos, tcp);
401                 sv->u = vm_vec_dot(&v2cp, rightv) * Shield_scale + 0.5f;
402                 sv->v = - vm_vec_dot(&v2cp, upv) * Shield_scale + 0.5f;
403
404                 if (sv->u > UV_MAX){
405                         sv->u = UV_MAX;
406                 }
407
408                 if (sv->u < 0.0f){
409                         sv->u = 0.0f;
410                 }
411
412                 if (sv->v > UV_MAX){
413                         sv->v = UV_MAX;
414                 }
415
416                 if (sv->v < 0.0f){
417                         sv->v = 0.0f;
418                 }
419
420                 // mprintf(("u, v = %7.3f %7.3f\n", stp->verts[i].u, stp->verts[i].v));
421         }
422
423         // mprintf(("\n"));
424 }
425
426 //      ----------------------------------------------------------------------------------------------------
427 //      Free records in Global_tris previously used by Shield_hits[shnum].tri_list
428 void free_global_tri_records(int shnum)
429 {
430         int     i;
431
432         Assert((shnum >= 0) && (shnum < MAX_SHIELD_HITS));
433
434         //mprintf(("Freeing up %i global records.\n", Shield_hits[shnum].num_tris));
435
436         for (i=0; i<Shield_hits[shnum].num_tris; i++){
437                 Global_tris[Shield_hits[shnum].tri_list[i]].used = 0;
438         }
439 }
440
441 void render_low_detail_shield_bitmap(gshield_tri *trip, matrix *orient, vector *pos, ubyte r, ubyte g, ubyte b)
442 {
443         matrix  m;
444         int             j;
445         vector  pnt;
446         vertex  verts[4];
447
448         vm_copy_transpose_matrix(&m,orient);
449
450         for (j=0; j<4; j++ )    {
451                 // Rotate point into world coordinates
452                 vm_vec_rotate(&pnt, &trip->verts[j].pos, &m);
453                 vm_vec_add2(&pnt, pos);
454
455                 // Pnt is now the x,y,z world coordinates of this vert.
456                 g3_rotate_vertex(&verts[j], &pnt);
457                 verts[j].u = trip->verts[j].u;
458                 verts[j].v = trip->verts[j].v;
459         }       
460
461         verts[0].r = r;
462         verts[0].g = g;
463         verts[0].b = b;
464         verts[1].r = r;
465         verts[1].g = g;
466         verts[1].b = b;
467         verts[2].r = r;
468         verts[2].g = g;
469         verts[2].b = b;
470         verts[3].r = r;
471         verts[3].g = g;
472         verts[3].b = b;
473
474         vector  norm;
475         vm_vec_perp(&norm, &trip->verts[0].pos, &trip->verts[1].pos, &trip->verts[2].pos);
476         vertex  *vertlist[4];
477         if ( vm_vec_dot(&norm, &trip->verts[1].pos ) < 0.0 )    {
478                 vertlist[0] = &verts[3]; 
479                 vertlist[1] = &verts[2];
480                 vertlist[2] = &verts[1]; 
481                 vertlist[3] = &verts[0]; 
482                 g3_draw_poly( 4, vertlist, TMAP_FLAG_TEXTURED | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD);
483         } else {
484                 vertlist[0] = &verts[0]; 
485                 vertlist[1] = &verts[1];
486                 vertlist[2] = &verts[2]; 
487                 vertlist[3] = &verts[3]; 
488                 g3_draw_poly( 4, vertlist, TMAP_FLAG_TEXTURED | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD);
489         }
490 }
491
492 //      Render one triangle of a shield hit effect on one ship.
493 //      Each frame, the triangle needs to be rotated into global coords.
494 //      trip            pointer to triangle in global array
495 //      orient  orientation of object shield is associated with
496 //      pos             center point of object
497 void render_shield_triangle(gshield_tri *trip, matrix *orient, vector *pos, ubyte r, ubyte g, ubyte b)
498 {
499         matrix  m;
500         int             j;
501         vector  pnt;
502         vertex  *verts[3];
503         vertex  points[3];
504
505         if (trip->trinum == -1)
506                 return; //      Means this is a quad, must have switched detail_level.
507
508         vm_copy_transpose_matrix(&m,orient);
509
510         for (j=0; j<3; j++ )    {
511                 // Rotate point into world coordinates
512                 vm_vec_rotate(&pnt, &trip->verts[j].pos, &m);
513                 vm_vec_add2(&pnt, pos);
514
515                 // Pnt is now the x,y,z world coordinates of this vert.
516                 // For this example, I am just drawing a sphere at that point.
517                 g3_rotate_vertex(&points[j], &pnt);
518                 points[j].u = trip->verts[j].u;
519                 points[j].v = trip->verts[j].v;
520                 Assert((trip->verts[j].u >= 0.0f) && (trip->verts[j].u <= UV_MAX));
521                 Assert((trip->verts[j].v >= 0.0f) && (trip->verts[j].v <= UV_MAX));
522                 verts[j] = &points[j];
523         }
524
525         verts[0]->r = r;
526         verts[0]->g = g;
527         verts[0]->b = b;
528         verts[1]->r = r;
529         verts[1]->g = g;
530         verts[1]->b = b;
531         verts[2]->r = r;
532         verts[2]->g = g;
533         verts[2]->b = b;
534
535         vector  norm;
536         Poly_count++;
537         vm_vec_perp(&norm,(vector *)&verts[0]->x,(vector *)&verts[1]->x,(vector*)&verts[2]->x);
538         if ( vm_vec_dot(&norm,(vector *)&verts[1]->x ) >= 0.0 ) {
539                 vertex  *vertlist[3];
540                 vertlist[0] = verts[2]; 
541                 vertlist[1] = verts[1]; 
542                 vertlist[2] = verts[0]; 
543                 g3_draw_poly( 3, vertlist, TMAP_FLAG_TEXTURED | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD);
544         } else {
545                 g3_draw_poly( 3, verts, TMAP_FLAG_TEXTURED | TMAP_FLAG_RGB | TMAP_FLAG_GOURAUD);
546         }
547 }
548
549 MONITOR(NumShieldRend);
550
551 //      Render a shield mesh in the global array Shield_hits[]
552 void render_shield(int shield_num) //, matrix *orient, vector *centerp)
553 {
554         int             i;
555         vector  *centerp;
556         matrix  *orient;
557         object  *objp;
558         ship            *shipp;
559         ship_info       *si;
560
561         if (Shield_hits[shield_num].type == SH_UNUSED)  {
562                 return;
563         }
564
565         Assert(Shield_hits[shield_num].objnum >= 0);
566
567         objp = &Objects[Shield_hits[shield_num].objnum];
568
569         if (objp->flags & OF_NO_SHIELDS)        {
570                 return;
571         }
572
573         //      If this object didn't get rendered, don't render its shields.  In fact, make the shield hit go away.
574         if (!(objp->flags & OF_WAS_RENDERED)) {
575                 Shield_hits[shield_num].type = SH_UNUSED;
576                 return;
577         }
578
579         //      At detail levels 1, 3, animations play at double speed to reduce load.
580         if (!D3D_enabled || (Detail.shield_effects == 1) || (Detail.shield_effects == 3)) {
581                 Shield_hits[shield_num].start_time -= Frametime;
582         }
583
584         MONITOR_INC(NumShieldRend,1);
585
586         shipp = &Ships[objp->instance];
587         si = &Ship_info[shipp->ship_info_index];
588
589         // objp, shipp, and si are now setup correctly
590
591         //      If this ship is in its deathroll, make the shield hit effects go away faster.
592         if (shipp->flags & SF_DYING)    {
593                 Shield_hits[shield_num].start_time -= fl2f(2*flFrametime);
594         }
595
596         //      Detail level stuff.  When lots of shield hits, maybe make them go away faster.
597         if (Poly_count > 50) {
598                 if (Shield_hits[shield_num].start_time + (SHIELD_HIT_DURATION*50)/Poly_count < Missiontime) {
599                         Shield_hits[shield_num].type = SH_UNUSED;
600                         free_global_tri_records(shield_num);
601                         // nprintf(("AI", "* "));
602                         return;
603                 }
604         } else if ((Shield_hits[shield_num].start_time + SHIELD_HIT_DURATION) < Missiontime) {
605                 Shield_hits[shield_num].type = SH_UNUSED;
606                 free_global_tri_records(shield_num);
607                 return;
608         }
609
610         orient = &objp->orient;
611         centerp = &objp->pos;
612
613         int bitmap_id, frame_num, n;
614
615         // mprintf(("Percent = %7.3f\n", f2fl(Missiontime - Shield_hits[shield_num].start_time)));
616
617         n = si->species;                
618         // Do some sanity checking
619         Assert( (n >=0) && (n<MAX_SPECIES_NAMES));
620         Assert( (n >=0) && (n<MAX_SHIELD_ANIMS));
621
622         frame_num = fl2i( f2fl(Missiontime - Shield_hits[shield_num].start_time) * Sheild_ani[n].nframes);
623         if ( frame_num >= Sheild_ani[n].nframes )       {
624                 frame_num = Sheild_ani[n].nframes - 1;
625         } else if ( frame_num < 0 )     {
626                 mprintf(( "HEY! Missiontime went backwards! (Shield.cpp)\n" ));
627                 frame_num = 0;
628         }
629         bitmap_id = Sheild_ani[n].first_frame + frame_num;
630
631         float alpha = 0.9999f;
632         if(The_mission.flags & MISSION_FLAG_FULLNEB){
633                 alpha *= 0.85f;
634         }
635         gr_set_bitmap(bitmap_id, GR_ALPHABLEND_FILTER, GR_BITBLT_MODE_NORMAL, alpha );
636
637         if (!D3D_enabled || (Detail.shield_effects == 1) || (Detail.shield_effects == 2)) {
638                 if ( bitmap_id != - 1 ) {
639                         render_low_detail_shield_bitmap(&Global_tris[Shield_hits[shield_num].tri_list[0]], orient, centerp, Shield_hits[shield_num].rgb[0], Shield_hits[shield_num].rgb[1], Shield_hits[shield_num].rgb[2]);
640                 }
641         } else {
642
643                 // AL 06/01/97 don't use Assert() until issue with Missiontime being reset to 0 are worked out
644                 if ( bitmap_id != - 1 ) {
645                         for (i=0; i<Shield_hits[shield_num].num_tris; i++) {
646                                 //if (Missiontime == Shield_hits[shield_num].start_time)
647                                 //      nprintf(("AI", "Frame %i: Render triangle %i.\n", Framecount, Global_tris[Shield_hits[shield_num].tri_list[i]].trinum));
648                                 render_shield_triangle(&Global_tris[Shield_hits[shield_num].tri_list[i]], orient, centerp, Shield_hits[shield_num].rgb[0], Shield_hits[shield_num].rgb[1], Shield_hits[shield_num].rgb[2]);
649                         }
650                 }
651         }
652 }
653
654 //      Render all the shield hits  in the global array Shield_hits[]
655 //      This is a temporary function.  Shield hit rendering will at least have to
656 // occur with the ship, perhaps even internal to the ship.
657 void render_shields()
658 {
659         int     i;
660
661         if (Detail.shield_effects == 0){
662                 return; //      No shield effect rendered at lowest detail level.
663         }
664
665         if (!New_shield_system){
666                 return;
667         }
668
669         for (i=0; i<MAX_SHIELD_HITS; i++){
670                 if (Shield_hits[i].type != SH_UNUSED){
671                         render_shield(i);
672                 }
673         }
674 }
675
676
677 // -----------------------------------------------------------------------------------------------------
678 void create_tris_containing(vector *vp, matrix *orient, shield_info *shieldp, vector *tcp, vector *centerp, float radius, vector *rvec, vector *uvec)
679 {
680         int     i, j;
681         shield_vertex *verts;
682
683         verts = shieldp->verts;
684
685         for (i=0; i<Num_tris; i++) {
686                 if ( !shieldp->tris[i].used ) {
687                         for (j=0; j<3; j++) {
688                                 vector v;
689
690                                 v = verts[shieldp->tris[i].verts[j]].pos;
691                                 if ((vp->x == v.x) && (vp->y == v.y) && (vp->z == v.z))
692                                         create_shield_from_triangle(i, orient, shieldp, tcp, centerp, radius, rvec, uvec);
693                         }
694                 }
695         }
696 }
697
698 void visit_children(int trinum, int vertex_index, matrix *orient, shield_info *shieldp, vector *tcp, vector *centerp, float radius, vector *rvec, vector *uvec)
699 {
700         shield_vertex *sv;
701
702         sv = &(shieldp->verts[shieldp->tris[trinum].verts[vertex_index]]);
703
704         if ( (sv->u > 0.0f) && (sv->u < UV_MAX) && (sv->v > 0.0f) && (sv->v < UV_MAX))
705                         create_tris_containing(&sv->pos, orient, shieldp, tcp, centerp, radius, rvec, uvec);
706 }
707
708 int     Gi_max = 0;
709
710 int get_free_global_shield_index()
711 {
712         int     gi = 0;
713
714         while ((gi < MAX_SHIELD_TRI_BUFFER) && (Global_tris[gi].used) && (Global_tris[gi].creation_time + SHIELD_HIT_DURATION > Missiontime)) {
715                 gi++;
716         }
717
718         //      If couldn't find one, choose a random one.
719         if (gi == MAX_SHIELD_TRI_BUFFER)
720                 gi = (int) (frand() * MAX_SHIELD_TRI_BUFFER);
721
722         return gi;
723 }
724
725 int get_global_shield_tri()
726 {
727         int     shnum;
728
729         //      Find unused shield hit buffer
730         for (shnum=0; shnum<MAX_SHIELD_HITS; shnum++)
731                 if (Shield_hits[shnum].type == SH_UNUSED)
732                         break;
733
734         if (shnum == MAX_SHIELD_HITS) {
735                 //nprintf(("AI", "Warning: Shield_hit buffer full!  Stealing an old one!\n"));
736                 shnum = myrand() % MAX_SHIELD_HITS;
737         }
738
739         Assert((shnum >= 0) && (shnum < MAX_SHIELD_HITS));
740
741         return shnum;
742 }
743
744 void create_shield_from_triangle(int trinum, matrix *orient, shield_info *shieldp, vector *tcp, vector *centerp, float radius, vector *rvec, vector *uvec)
745 {
746         //nprintf(("AI", "[%3i] ", trinum));
747
748         rs_compute_uvs( &shieldp->tris[trinum], shieldp->verts, tcp, radius, rvec, uvec);
749
750         //Assert(trinum < MAX_SHIELD_HITS);
751         shieldp->tris[trinum].used = 1;
752
753 //mprintf(("%i ", trinum));
754         visit_children(trinum, 0, orient, shieldp, tcp, centerp, radius, rvec, uvec);
755         visit_children(trinum, 1, orient, shieldp, tcp, centerp, radius, rvec, uvec);
756         visit_children(trinum, 2, orient, shieldp, tcp, centerp, radius, rvec, uvec);
757 }
758
759 //      Copy information from Current_tris to Global_tris, stuffing information
760 //      in a slot in Shield_hits.  The Global_tris array is not a shield_tri structure.
761 // We need to store vertex information in the global array since the vertex list
762 // will not be available to us when we actually use the array.
763 void copy_shield_to_globals( int objnum, shield_info *shieldp )
764 {
765         int     i, j;
766         int     gi = 0;
767         int     count = 0;                      //      Number of triangles in this shield hit.
768         int     shnum;                          //      shield hit number, index in Shield_hits.
769
770         shnum = get_global_shield_tri();
771         
772         Shield_hits[shnum].type = SH_TYPE_1;
773         // mprintf(("Creating hit #%i at time = %7.3f\n", shnum, f2fl(Missiontime)));
774
775         for (i = 0; i < shieldp->ntris; i++ ) {
776                 if ( shieldp->tris[i].used ) {
777                         while ( (gi < MAX_SHIELD_TRI_BUFFER) && (Global_tris[gi].used) && (Global_tris[gi].creation_time + SHIELD_HIT_DURATION > Missiontime)) {
778                                 gi++;
779                         }
780                         
781                         //      If couldn't find one, choose a random one.
782                         if (gi == MAX_SHIELD_TRI_BUFFER)
783                                 gi = (int) (frand() * MAX_SHIELD_TRI_BUFFER);
784
785                         Global_tris[gi].used = shieldp->tris[i].used;
786                         Global_tris[gi].trinum = i;
787                         Global_tris[gi].creation_time = Missiontime;
788
789                         // copy the pos/u/v elements of the shield_vertex structure into the shield vertex structure for this global triangle.
790                         for (j = 0; j < 3; j++)
791                                 Global_tris[gi].verts[j] = shieldp->verts[shieldp->tris[i].verts[j]];
792                         Shield_hits[shnum].tri_list[count++] = gi;
793
794                         if (count >= MAX_TRIS_PER_HIT) {
795                                 mprintf(("Warning: Too many triangles in shield hit.\n"));
796                                 break;
797                         }
798                 }
799         }
800
801         Shield_hits[shnum].num_tris = count;
802         Shield_hits[shnum].start_time = Missiontime;
803         Shield_hits[shnum].objnum = objnum;
804
805         Shield_hits[shnum].rgb[0] = 255;
806         Shield_hits[shnum].rgb[1] = 255;
807         Shield_hits[shnum].rgb[2] = 255;
808         if((objnum >= 0) && (objnum < MAX_OBJECTS) && (Objects[objnum].type == OBJ_SHIP) && (Objects[objnum].instance >= 0) && (Objects[objnum].instance < MAX_SHIPS) && (Ships[Objects[objnum].instance].ship_info_index >= 0) && (Ships[Objects[objnum].instance].ship_info_index < Num_ship_types)){
809                 ship_info *sip = &Ship_info[Ships[Objects[objnum].instance].ship_info_index];
810                 
811                 Shield_hits[shnum].rgb[0] = sip->shield_color[0];
812                 Shield_hits[shnum].rgb[1] = sip->shield_color[1];
813                 Shield_hits[shnum].rgb[2] = sip->shield_color[2];
814         }
815 }
816
817 //      ***** This is the version that works on a quadrant basis.
818 //      Return absolute amount of damage not applied.
819 float apply_damage_to_shield(object *objp, int shield_quadrant, float damage)
820 {
821         ai_info *aip;
822
823         // multiplayer clients bail here if nodamage
824         // if(MULTIPLAYER_CLIENT && (Netgame.debug_flags & NETD_FLAG_CLIENT_NODAMAGE)){
825         if(MULTIPLAYER_CLIENT){
826                 return damage;
827         }
828
829         if ( (shield_quadrant < 0)  || (shield_quadrant > 3) ) return damage;   
830         
831         Assert(objp->type == OBJ_SHIP);
832         aip = &Ai_info[Ships[objp->instance].ai_index];
833         aip->last_hit_quadrant = shield_quadrant;
834
835         objp->shields[shield_quadrant] -= damage;
836
837         if (objp->shields[shield_quadrant] < 0.0f) {
838                 float   remaining_damage;
839
840                 remaining_damage = -objp->shields[shield_quadrant];
841                 objp->shields[shield_quadrant] = 0.0f;
842                 //nprintf(("AI", "Applied %7.3f damage to quadrant #%i, %7.3f passes through\n", damage - remaining_damage, quadrant_num, remaining_damage));
843                 return remaining_damage;
844         } else {
845                 //nprintf(("AI", "Applied %7.3f damage to quadrant #%i\n", damage, quadrant_num));
846                 return 0.0f;
847         }
848                 
849 }
850
851 //      At lower detail levels, shield hit effects are a single texture, applied to one enlarged triangle.
852 void create_shield_low_detail(int objnum, int model_num, matrix *orient, vector *centerp, vector *tcp, int tr0, shield_info *shieldp)
853 {
854         matrix  tom;
855         int             gi;
856         int             shnum;
857
858         shnum = get_global_shield_tri();
859         Shield_hits[shnum].type = SH_TYPE_1;
860
861         gi = get_free_global_shield_index();
862
863         Global_tris[gi].used = 1;
864         Global_tris[gi].trinum = -1;            //      This tells triangle renderer to not render in case detail_level was switched.
865         Global_tris[gi].creation_time = Missiontime;
866
867         Shield_hits[shnum].tri_list[0] = gi;
868         Shield_hits[shnum].num_tris = 1;
869         Shield_hits[shnum].start_time = Missiontime;
870         Shield_hits[shnum].objnum = objnum;
871
872         Shield_hits[shnum].rgb[0] = 255;
873         Shield_hits[shnum].rgb[1] = 255;
874         Shield_hits[shnum].rgb[2] = 255;
875         if((objnum >= 0) && (objnum < MAX_OBJECTS) && (Objects[objnum].type == OBJ_SHIP) && (Objects[objnum].instance >= 0) && (Objects[objnum].instance < MAX_SHIPS) && (Ships[Objects[objnum].instance].ship_info_index >= 0) && (Ships[Objects[objnum].instance].ship_info_index < Num_ship_types)){
876                 ship_info *sip = &Ship_info[Ships[Objects[objnum].instance].ship_info_index];
877                 
878                 Shield_hits[shnum].rgb[0] = sip->shield_color[0];
879                 Shield_hits[shnum].rgb[1] = sip->shield_color[1];
880                 Shield_hits[shnum].rgb[2] = sip->shield_color[2];
881         }
882
883         vm_vector_2_matrix(&tom, &shieldp->tris[tr0].norm, NULL, NULL);
884         //rs_compute_uvs( &shieldp->tris[tr0], shieldp->verts, tcp, Objects[objnum].radius, &tom.rvec, &tom.uvec);
885
886         create_low_detail_poly(gi, tcp, &tom.rvec, &tom.uvec);
887
888 }
889
890 // Algorithm for shrink-wrapping a texture across a triangular mesh.
891 // 
892 // - Given a point of intersection, tcp (local to objnum)
893 // - Vector to center of shield from tcp is v2c.
894 // - Using v2c, compute right and down vectors.  These are the vectors of
895 //   increasing u and v, respectively.
896 // - Triangle of intersection of tcp is tr0.
897 // - For 3 points in tr0, compute u,v coordinates using up and down vectors
898 //   from center point, tcp.  Need to know size of explosion texture.  N units
899 //   along right vector corresponds to O units in explosion texture space.
900 // - For each edge, if either endpoint was outside texture bounds, recursively
901 //   apply previous and current step.
902 // 
903 // Output of above is a list of triangles with u,v coordinates.  These u,v
904 // coordinates will have to be clipped against the explosion texture bounds.
905
906 void create_shield_explosion(int objnum, int model_num, matrix *orient, vector *centerp, vector *tcp, int tr0)
907 {
908 //      vector  v2c;            //      Vector to center from point tcp
909         matrix  tom;            //      Texture Orientation Matrix
910 //      float           radius; // Radius of shield, computed as distance from tcp to objp->pos.
911         shield_info     *shieldp;
912         polymodel       *pm;
913         int             i;
914
915         if (!New_shield_system)
916                 return;
917
918         if (Objects[objnum].flags & OF_NO_SHIELDS)
919                 return;
920
921         pm = model_get(model_num);
922         Num_tris = pm->shield.ntris;
923         //Assert(Num_tris < MAX_SHIELD_HITS);
924         shieldp = &pm->shield;
925
926         if (Num_tris == 0)
927                 return;
928
929         //nprintf(("AI", "Frame %i: Creating explosion on %i.\n", Framecount, objnum));
930
931         if (!D3D_enabled || (Detail.shield_effects == 1) || (Detail.shield_effects == 2)) {
932                 create_shield_low_detail(objnum, model_num, orient, centerp, tcp, tr0, shieldp);
933                 return;
934         }
935
936         for (i=0; i<Num_tris; i++)
937                 shieldp->tris[i].used = 0;
938
939         //      Compute orientation matrix from normal of surface hit.
940         //      Note, this will cause the shape of the bitmap to change abruptly
941         //      as the impact point moves to another triangle.  To prevent this,
942         //      you could average the normals at the vertices, then interpolate the
943         //      normals from the vertices to get a smoothly changing normal across the face.
944         //      I had tried using the vector from the impact point to the center, which
945         //      changes smoothly, but this looked surprisingly bad.
946         vm_vector_2_matrix(&tom, &shieldp->tris[tr0].norm, NULL, NULL);
947         //vm_vec_sub(&v2c, tcp, &Objects[objnum].pos);
948
949         //      Create the shield from the current triangle, as well as its neighbors.
950         create_shield_from_triangle(tr0, orient, shieldp, tcp, centerp, Objects[objnum].radius, &tom.rvec, &tom.uvec);
951         //nprintf(("AI", "\n"));
952         for (i=0; i<3; i++)
953                 create_shield_from_triangle(shieldp->tris[tr0].neighbors[i], orient, shieldp, tcp, centerp, Objects[objnum].radius, &tom.rvec, &tom.uvec);
954         
955         copy_shield_to_globals(objnum, shieldp);
956         // render_shield(orient, centerp);
957 }
958
959 MONITOR(NumShieldHits);
960
961 //      Add data for a shield hit.
962 void add_shield_point(int objnum, int tri_num, vector *hit_pos)
963 {
964         //Assert(Num_shield_points < MAX_SHIELD_POINTS);
965         if (Num_shield_points >= MAX_SHIELD_POINTS)
966                 return;
967
968         MONITOR_INC(NumShieldHits,1);
969
970         Shield_points[Num_shield_points].objnum = objnum;
971         Shield_points[Num_shield_points].shield_tri = tri_num;
972         Shield_points[Num_shield_points].hit_point = *hit_pos;
973
974         Num_shield_points++;
975
976         // in multiplayer -- send the shield hit data to the clients
977         // if ( MULTIPLAYER_MASTER && !(Netgame.debug_flags & NETD_FLAG_CLIENT_NODAMAGE)){
978                 // send_shield_explosion_packet( objnum, tri_num, *hit_pos );
979         // }
980
981         Ships[Objects[objnum].instance].shield_hits++;
982 }
983
984 // ugh!  I wrote a special routine to store shield points for clients in multiplayer
985 // games.  Problem is initilization and flow control of normal gameplay make this problem
986 // more than trivial to solve.  Turns out that I think I can just keep track of the
987 // shield_points for multiplayer in a separate count -- then assign the multi count to
988 // the normal count at the correct time.
989 void add_shield_point_multi(int objnum, int tri_num, vector *hit_pos)
990 {
991         //Assert(Num_multi_shield_points < MAX_SHIELD_POINTS);
992
993         if (Num_multi_shield_points >= MAX_SHIELD_POINTS)
994                 return;
995
996         Shield_points[Num_shield_points].objnum = objnum;
997         Shield_points[Num_shield_points].shield_tri = tri_num;
998         Shield_points[Num_shield_points].hit_point = *hit_pos;
999
1000         Num_multi_shield_points++;
1001 }
1002
1003 // sets up the shield point hit information for multiplayer clients
1004 void shield_point_multi_setup()
1005 {
1006         int i;
1007
1008         Assert( MULTIPLAYER_CLIENT );
1009
1010         if ( Num_multi_shield_points == 0 )
1011                 return;
1012
1013         Num_shield_points = Num_multi_shield_points;
1014         for (i = 0; i < Num_shield_points; i++ ){
1015                 Ships[Objects[Shield_points[i].objnum].instance].shield_hits++;
1016         }
1017
1018         Num_multi_shield_points = 0;
1019 }
1020
1021
1022 //      Create all the shield explosions that occurred on object *objp this frame.
1023 void create_shield_explosion_all(object *objp)
1024 {
1025         int     i;
1026         int     num;
1027         int     count;
1028         int     objnum;
1029         ship    *shipp;
1030
1031         if (Detail.shield_effects == 0){
1032                 return; 
1033         }
1034
1035         num = objp->instance;
1036         shipp = &Ships[num];
1037
1038         count = shipp->shield_hits;
1039         objnum = objp-Objects;
1040
1041         for (i=0; i<Num_shield_points; i++) {
1042                 if (Shield_points[i].objnum == objnum) {
1043                         create_shield_explosion(objnum, shipp->modelnum, &objp->orient, &objp->pos, &Shield_points[i].hit_point, Shield_points[i].shield_tri);
1044                         count--;
1045                         if (count <= 0){
1046                                 break;
1047                         }
1048                 }
1049         }
1050
1051         //mprintf(("Creating %i explosions took %7.3f seconds\n", shipp->shield_hits, (float) (timer_get_milliseconds() - start_time)/1000.0f));
1052
1053         // some some reason, clients seem to have a bogus count valud on occation.  I"ll chalk it up
1054         // to missed packets :-)  MWA 2/6/98
1055         if ( !MULTIPLAYER_CLIENT ){
1056                 Assert(count == 0);     //      Couldn't find all the alleged shield hits.  Bogus!
1057         }
1058 }
1059
1060 int     Break_value = -1;
1061
1062 //      This is a debug function.
1063 //      Draw the whole shield as a wireframe mesh, not looking at the current
1064 //      integrity.
1065 #ifndef NDEBUG
1066 void ship_draw_shield( object *objp)
1067 {
1068         int             model_num;
1069         matrix  m;
1070         int             i;
1071         vector  pnt;
1072         polymodel * pm; 
1073
1074         if (!New_shield_system)
1075                 return;
1076
1077         if (objp->flags & OF_NO_SHIELDS)
1078                 return;
1079
1080         Assert(objp->instance >= 0);
1081
1082         model_num = Ships[objp->instance].modelnum;
1083
1084         if ( Fred_running ) return;
1085
1086         pm = model_get(model_num);
1087
1088         if (pm->shield.ntris<1) return;
1089
1090         vm_copy_transpose_matrix(&m, &objp->orient);
1091
1092         //      Scan all the triangles in the mesh.
1093         for (i=0; i<pm->shield.ntris; i++ )     {
1094                 int             j;
1095                 vector  gnorm, v2f, tri_point;
1096                 vertex prev_pnt, pnt0;
1097                 shield_tri *tri;
1098
1099                 tri = &pm->shield.tris[i];
1100
1101                 if (i == Break_value)
1102                         Int3();
1103
1104                 //      Hack! Only works for object in identity orientation.
1105                 //      Need to rotate eye position into object's reference frame.
1106                 //      Only draw facing triangles.
1107                 vm_vec_rotate(&tri_point, &pm->shield.verts[tri->verts[0]].pos, &Eye_matrix);
1108                 vm_vec_add2(&tri_point, &objp->pos);
1109
1110                 vm_vec_sub(&v2f, &tri_point, &Eye_position);
1111                 vm_vec_rotate(&gnorm, &tri->norm, &m);
1112
1113                 if (vm_vec_dot(&gnorm, &v2f) < 0.0f) {
1114                         int     intensity;
1115
1116                         intensity = (int) (Ships[objp->instance].shield_integrity[i] * 255);
1117
1118                         if (intensity < 0)
1119                                 intensity = 0;
1120                         else if (intensity > 255)
1121                                 intensity = 255;
1122                         
1123                         gr_set_color(0, 0, intensity);
1124
1125                         //      Process the vertices.
1126                         //      Note this rotates each vertex each time it's needed, very dumb.
1127                         for (j=0; j<3; j++ )    {
1128                                 vertex tmp;
1129
1130                                 // Rotate point into world coordinates
1131                                 vm_vec_rotate(&pnt, &pm->shield.verts[tri->verts[j]].pos, &m);
1132                                 //vm_vec_rotate(&pnt,&pm->shield[i].pnt[j],&m);
1133                                 vm_vec_add2(&pnt, &objp->pos);
1134
1135                                 // Pnt is now the x,y,z world coordinates of this vert.
1136                                 // For this example, I am just drawing a sphere at that
1137                                 // point.
1138                                 g3_rotate_vertex(&tmp, &pnt);
1139
1140                                 if (j)
1141                                         g3_draw_line(&prev_pnt, &tmp);
1142                                 else
1143                                         pnt0 = tmp;
1144                                 prev_pnt = tmp;
1145                         }
1146
1147                         g3_draw_line(&pnt0, &prev_pnt);
1148                 }
1149         }
1150 }
1151 #endif
1152
1153 // Returns true if the shield presents any opposition to something 
1154 // trying to force through it.
1155 // If quadrant is -1, looks at entire shield, otherwise
1156 // just one quadrant
1157 int ship_is_shield_up( object *obj, int quadrant )
1158 {
1159         if ( (quadrant>=0) && (quadrant<=3))    {
1160                 // Just check one quadrant
1161                 if (obj->shields[quadrant] > max(2.0f, 0.1f * Ship_info[Ships[obj->instance].ship_info_index].shields/4.0f))    {
1162                         return 1;
1163                 }
1164         } else {
1165                 // Check all quadrants
1166                 float strength = get_shield_strength(obj);
1167
1168                 if ( strength > max(2.0f*4.0f, 0.1f * Ship_info[Ships[obj->instance].ship_info_index].shields ))        {
1169                         return 1;
1170                 }
1171         }
1172         return 0;       // no shield strength
1173 }
1174
1175
1176 /*
1177 //-- CODE TO "BOUNCE" AN ARRAY FROM A GIVEN POINT.
1178 //-- LIKE A MATTRESS.
1179 #define BOUNCE_SIZE ???
1180
1181 byte Bouncer1[BOUNCE_SIZE];
1182 byte Bouncer2[BOUNCE_SIZE];
1183
1184 byte * Bouncer = Bouncer1;
1185 byte * OldBouncer = Bouncer2;
1186
1187 // To wiggle, add value to Bouncer[] 
1188
1189 void bounce_it()
1190 {
1191         int i, tmp;
1192
1193
1194         for (i=0; i<BOUNCE_SIZE; i++ )  {
1195                 int t = 0;
1196
1197                 t += OldBouncer[ LEFT ];
1198                 t += OldBouncer[ RIGHT ];
1199                 t += OldBouncer[ UP ];
1200                 t += OldBouncer[ DOWN ];
1201
1202                 t = (t/2) - Bouncer[i];
1203                 tmp = t - t/16;         // 8
1204                 
1205                 if ( tmp < -127 ) tmp = -127;
1206                 if ( tmp > 127 ) tmp = 127;
1207                 Bouncer[i] = tmp;
1208         }
1209
1210         if ( Bouncer == Bouncer1 )      {
1211                 OldBouncer = Bouncer1;
1212                 Bouncer = Bouncer2;
1213         } else {
1214                 OldBouncer = Bouncer2;
1215                 Bouncer = Bouncer1;
1216         }
1217 }
1218 */
1219
1220 #else 
1221
1222 // stub out shield functions for the demo
1223 void shield_hit_init() {}
1224 void create_shield_explosion_all(object *objp) {}
1225 void shield_frame_init() {}
1226 void add_shield_point(int objnum, int tri_num, vector *hit_pos) {}
1227 void add_shield_point_multi(int objnum, int tri_num, vector *hit_pos) {}
1228 void shield_point_multi_setup() {}
1229 void shield_hit_close() {}
1230 void ship_draw_shield( object *objp) {}
1231 void shield_hit_page_in() {}
1232 void render_shields() {}
1233 float apply_damage_to_shield(object *objp, int shield_quadrant, float damage) {return damage;} 
1234 int ship_is_shield_up( object *obj, int quadrant ) {return 0;}
1235
1236 #endif // DEMO
1237
1238
1239 //      return quadrant containing hit_pnt.
1240 //      \  1  /.
1241 //      3 \ / 0
1242 //        / \.
1243 //      /  2  \.
1244 //      Note: This is in the object's local reference frame.  Do _not_ pass a vector in the world frame.
1245 int get_quadrant(vector *hit_pnt)
1246 {
1247         int     result = 0;
1248
1249         if (hit_pnt->x < hit_pnt->z)
1250                 result |= 1;
1251
1252         if (hit_pnt->x < -hit_pnt->z)
1253                 result |= 2;
1254
1255         return result;
1256 }
1257