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