]> icculus.org git repositories - taylor/freespace2.git/blob - src/debris/debris.cpp
Initial revision
[taylor/freespace2.git] / src / debris / debris.cpp
1 /*
2  * $Logfile: /Freespace2/code/Debris/Debris.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Code for the pieces of exploding object debris.
8  *
9  * $Log$
10  * Revision 1.1  2002/05/03 03:28:08  root
11  * Initial revision
12  *
13  * 
14  * 16    8/01/99 1:13p Dave
15  * Fixed objsnd problem with hull debris pieces.
16  * 
17  * 15    7/01/99 4:23p Dave
18  * Full support for multiple linked ambient engine sounds. Added "big
19  * damage" flag.
20  * 
21  * 14    7/01/99 11:44a Dave
22  * Updated object sound system to allow multiple obj sounds per ship.
23  * Added hit-by-beam sound. Added killed by beam sound.
24  * 
25  * 13    5/18/99 11:50a Andsager
26  * Remove unused object type OBJ_GHOST_SAVE
27  * 
28  * 12    5/14/99 11:50a Andsager
29  * Added vaporize for SMALL ships hit by HUGE beams.  Modified dying
30  * frame.  Enlarged debris shards and range at which visible.
31  * 
32  * 11    4/23/99 12:01p Johnson
33  * Added SIF_HUGE_SHIP
34  * 
35  * 10    2/26/99 4:14p Dave
36  * Put in the ability to have multiple shockwaves for ships.
37  * 
38  * 9     1/20/99 6:04p Dave
39  * Another bit of stuff for beam weapons. Ships will properly use them
40  * now, although they're really deadly.
41  * 
42  * 8     12/03/98 3:14p Andsager
43  * Check in code that checks rotating submodel actually has ship subsystem
44  * 
45  * 7     11/19/98 11:07p Andsager
46  * Check in of physics and collision detection of rotating submodels
47  * 
48  * 6     11/13/98 10:13a Andsager
49  * simplify collision code
50  * 
51  * 5     11/05/98 5:55p Dave
52  * Big pass at reducing #includes
53  * 
54  * 4     10/23/98 1:11p Andsager
55  * Make ship sparks emit correctly from rotating structures.
56  * 
57  * 3     10/16/98 1:22p Andsager
58  * clean up header files
59  * 
60  * 2     10/07/98 10:52a Dave
61  * Initial checkin.
62  * 
63  * 1     10/07/98 10:48a Dave
64  * 
65  * 119   8/28/98 3:28p Dave
66  * EMP effect done. AI effects may need some tweaking as required.
67  * 
68  * 118   5/03/98 5:41p Mike
69  * Add Framecount to nprintf.
70  * 
71  * 117   4/15/98 10:00p Allender
72  * make debris have own signature set.
73  * 
74  * 116   4/15/98 9:42a Adam
75  * added 2 more explosion types (1, actually, but placeholder for 2)
76  * 
77  * 115   4/13/98 4:52p Allender
78  * remove AI_frametime and us flFrametime instead.  Make lock warning work
79  * in multiplayer for aspect seeking missiles.  Debris fixups
80  * 
81  * 114   4/10/98 12:16p Allender
82  * fix ship hit kill and debris packets
83  * 
84  * 113   4/09/98 5:43p Allender
85  * multiplayer network object fixes.  debris and self destructed ships
86  * should all sync up.  Fix problem where debris pieces (hull pieces) were
87  * not getting a net signature
88  * 
89  * 112   4/01/98 5:34p John
90  * Made only the used POFs page in for a level.   Reduced some interp
91  * arrays.    Made custom detail level work differently.
92  * 
93  * 111   3/31/98 5:11p John
94  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
95  * bunch of debug stuff out of player file.  Made model code be able to
96  * unload models and malloc out only however many models are needed.
97  * 
98  * 110   3/26/98 5:21p John
99  * Added new code to preload all bitmaps at the start of a level.
100  * Commented it out, though.
101  * 
102  * 109   3/23/98 12:20p Andsager
103  * Enable collision from rotation in ship_debris and ship_debris
104  * collisions.
105  * 
106  * 108   3/19/98 12:09p John
107  * Fixed a bug using 6 characters.   r_heavy was using local coordinates
108  * instead of world so all asteroid-ship and debris-ship hitpos's were in
109  * the wrong spot.  Ok, I rearreanged some code to make it clearer also.
110  * 
111  * 
112  * 107   3/12/98 6:47p John
113  * MAde arcs on debris further than objrad*50 not render.
114  * 
115  * 106   3/09/98 2:10p Andsager
116  * Put in checks for debris (other) with excessive velocity.
117  * 
118  * 105   2/26/98 10:07p Hoffoss
119  * Rewrote state saving and restoring to fix bugs and simplify the code.
120  * 
121  * 104   2/22/98 12:19p John
122  * Externalized some strings
123  * 
124  * 103   2/20/98 8:31p Lawrance
125  * Add radius parm to sound_play_3d()
126  * 
127  * 102   2/10/98 6:43p Lawrance
128  * Moved asteroid code to a separate lib.
129  * 
130  * 101   2/07/98 2:14p Mike
131  * Improve asteroid splitting.  Add ship:asteroid collisions.  Timestamp
132  * ship:debris collisions.
133  * 
134  * 100   2/06/98 7:45p John
135  * Reversed order of asteroid models so blown up ones are smaller.  
136  * 
137  * 99    2/06/98 7:28p John
138  * Made debris and particles not get created if > 200 m from eye.   Added
139  * max_velocity to asteroid's physics info for aiding in throwing out
140  * collision pairs.
141  * 
142  * 98    2/06/98 3:08p Mike
143  * More asteroid stuff, including resolving conflicts between the two
144  * asteroid_field structs!
145  * 
146  * 97    2/06/98 12:25a Mike
147  * More asteroid stuff.
148  * 
149  * 96    2/05/98 9:41p Mike
150  * Asteroid work, intermediate checkin to resolve compile errors.
151  * 
152  * 95    2/05/98 9:21p John
153  * Some new Direct3D code.   Added code to monitor a ton of stuff in the
154  * game.
155  * 
156  * 94    2/05/98 12:51a Mike
157  * Early asteroid stuff.
158  * 
159  * 93    2/03/98 6:01p Andsager
160  * Fix excessive rotvel in debris_create.  Check using physics function
161  * check_rotvel_limit.
162  * 
163  * 92    2/03/98 11:14a Andsager
164  * Temp check in to stop debris being created with excess rotvel
165  * 
166  * 91    2/02/98 4:45p Mike
167  * Increase translational and rotational velocity imparted to debris
168  * pieces at request of Adam.
169  * 
170  * 90    1/30/98 2:56p John
171  * Made debris arcs jump around.  Made only 2/3 of the chunks have arcing
172  * 
173  * 89    1/30/98 11:48a John
174  * Made debris arcs cast light.  Added sound effects for them.
175  * 
176  * 88    1/29/98 5:50p John
177  * Made electrical arcing on debris pieces be persistent from frame to
178  * frame
179  * 
180  * 87    1/29/98 8:39a Andsager
181  * Changed mass and moment of intertia based area vs. volume
182  * 
183  * 86    1/27/98 11:02a John
184  * Added first rev of sparks.   Made all code that calls model_render call
185  * clear_instance first.   Made debris pieces not render by default when
186  * clear_instance is called.
187  * 
188  * 85    1/24/98 4:49p Lawrance
189  * Only delete hull piece if you can find an old one that isn't already
190  * about to die.
191  * 
192  * 84    1/23/98 5:06p John
193  * Took L out of vertex structure used B (blue) instead.   Took all small
194  * fireballs out of fireball types and used particles instead.  Fixed some
195  * debris explosion things.  Restructured fireball code.   Restructured
196  * some lighting code.   Made dynamic lighting on by default. Made groups
197  * of lasers only cast one light.  Made fireballs not cast light.
198  * 
199  * $NoKeywords: $
200  */
201
202 #include "3d.h"
203 #include "bmpman.h"
204 #include "object.h"
205 #include "debris.h"
206 #include "fireballs.h"
207 #include "radar.h"
208 #include "missionparse.h"               // For MAX_SPECIES_NAMES
209 #include "gamesnd.h"
210 #include "objectsnd.h"
211 #include "linklist.h"
212 #include "systemvars.h"
213 #include "multi.h"
214 #include "multimsgs.h"
215 #include "particle.h"
216 #include "freespace.h"
217 #include "multiutil.h"
218 #include "objcollide.h"
219 #include "timer.h"
220
221 #define MAX_LIFE                                                                        10.0f
222 #define MIN_RADIUS_FOR_PERSISTANT_DEBRIS        50              // ship radius at which debris from it becomes persistant
223 #define DEBRIS_SOUND_DELAY                                              2000    // time to start debris sound after created
224
225 // limit the number of hull debris chunks that can exist.  
226 #define MAX_HULL_PIECES         10
227 int             Num_hull_pieces;                // number of hull pieces in existance
228 debris  Hull_debris_list;               // head of linked list for hull debris chunks, for quick search
229
230 debris Debris[MAX_DEBRIS_PIECES];
231
232 int Num_debris_pieces = 0;
233 int Debris_inited = 0;
234
235 int Debris_model = -1;
236 int Debris_vaporize_model = -1;
237 int Debris_num_submodels = 0;
238 char * Debris_texture_files[MAX_SPECIES_NAMES] = { 
239         NOX("debris01a"),       // Terran
240         NOX("debris01b"),       // Species B
241         NOX("debris01c"),       // Shivan
242         };
243
244 int Debris_textures[MAX_SPECIES_NAMES];
245
246 #define MAX_DEBRIS_DIST                                 10000.0f                        //      Debris goes away if it's this far away.
247 #define DEBRIS_DISTANCE_CHECK_TIME              (10*1000)               //      Check every 10 seconds.
248 #define DEBRIS_INDEX(dp) (dp-Debris)
249
250 #define MAX_SPEED_SMALL_DEBRIS          200                                     // maximum velocity of small debris piece
251 #define MAX_SPEED_BIG_DEBRIS                    150                                     // maximum velocity of big debris piece
252 #define MAX_SPEED_CAPITAL_DEBRIS        100                                     // maximum velocity of capital debris piece
253 #define DEBRIS_SPEED_DEBUG
254
255 // ---------------------------------------------------------------------------------------
256 // debris_start_death_roll()
257 //
258 //      Start the sequence of a piece of debris writhing in unholy agony!!!
259 //
260 static void debris_start_death_roll(object *debris_obj, debris *debris_p)
261 {
262         if (debris_p->is_hull)  {
263                 // tell everyone else to blow up the piece of debris
264                 if( MULTIPLAYER_MASTER )
265                         send_debris_update_packet(debris_obj,DEBRIS_UPDATE_NUKE);
266
267                 int fireball_type = FIREBALL_EXPLOSION_LARGE1 + rand()%FIREBALL_NUM_LARGE_EXPLOSIONS;
268                 fireball_create( &debris_obj->pos, fireball_type, OBJ_INDEX(debris_obj), debris_obj->radius*1.75f);
269
270                 // only play debris destroy sound if hull piece and it has been around for at least 2 seconds
271                 if ( Missiontime > debris_p->time_started + 2*F1_0 ) {
272                         snd_play_3d( &Snds[SND_MISSILE_IMPACT1], &debris_obj->pos, &View_position, debris_obj->radius );
273                         
274                 }
275         }
276
277         debris_obj->flags |= OF_SHOULD_BE_DEAD;
278 //      demo_do_flag_dead(OBJ_INDEX(debris_obj));
279 }
280
281 // ---------------------------------------------------------------------------------------
282 // debris_init()
283 //
284 // This will get called at the start of each level.
285 //
286 void debris_init()
287 {
288         int i;
289
290         if ( !Debris_inited ) {
291                 Debris_inited = 1;
292         }
293
294         Debris_model = -1;
295         Debris_vaporize_model = -1;
296         Debris_num_submodels = 0;
297                 
298         // Reset everything between levels
299         Num_debris_pieces = 0;
300         for (i=0; i<MAX_DEBRIS_PIECES; i++ )    {
301                 Debris[i].flags = 0;
302                 Debris[i].sound_delay = 0;
303         }
304                 
305         Num_hull_pieces = 0;
306         list_init(&Hull_debris_list);
307 }
308
309 // Page in debris bitmaps at level load
310 void debris_page_in()
311 {
312         int i;
313
314         Debris_model = model_load( NOX("debris01.pof"), 0, NULL );
315         if (Debris_model>-1)    {
316                 polymodel * pm;
317                 pm = model_get(Debris_model);
318                 Debris_num_submodels = pm->n_models;
319         }
320
321         Debris_vaporize_model = model_load( NOX("debris02.pof"), 0, NULL );
322
323         for (i=0; i<MAX_SPECIES_NAMES; i++ )    {
324                 nprintf(( "Paging", "Paging in debris texture '%s'\n", Debris_texture_files[i] ));
325                 Debris_textures[i] = bm_load( Debris_texture_files[i] );
326                 if ( Debris_textures[i] < 0 ) { 
327                         Warning( LOCATION, "Couldn't load species %d debris\ntexture, '%s'\n", i, Debris_texture_files[i] );
328                 }
329                 bm_page_in_texture( Debris_textures[i] );
330         }
331         
332 }
333
334 MONITOR(NumSmallDebrisRend);
335 MONITOR(NumHullDebrisRend);
336
337 // ---------------------------------------------------------------------------------------
338 // debris_render()
339 //
340 //
341 void debris_render(object * obj)
342 {
343         int                     i, num, swapped;
344         polymodel       *pm;
345         debris          *db;
346
347
348         swapped = -1;
349         pm = NULL;      
350         num = obj->instance;
351
352         Assert(num >= 0 && num < MAX_DEBRIS_PIECES );
353         db = &Debris[num];
354
355         Assert( db->flags & DEBRIS_USED );
356
357         // Swap in a different texture depending on the species
358         if ( (db->species > -1) && (db->species < MAX_SPECIES_NAMES) )  {
359
360                 pm = model_get( db->model_num );
361
362                 if ( pm && (pm->n_textures == 1) ) {
363                         swapped = pm->textures[0];
364                         pm->textures[0] = Debris_textures[db->species];
365                 }
366         }
367
368         model_clear_instance( db->model_num );
369
370         // Only render electrical arcs if within 500m of the eye (for a 10m piece)
371         if ( vm_vec_dist_quick( &obj->pos, &Eye_position ) < obj->radius*50.0f )        {
372                 for (i=0; i<MAX_DEBRIS_ARCS; i++ )      {
373                         if ( timestamp_valid( db->arc_timestamp[i] ) )  {
374                                 model_add_arc( db->model_num, db->submodel_num, &db->arc_pts[i][0], &db->arc_pts[i][1], MARC_TYPE_NORMAL );
375                         }
376                 }
377         }
378
379         if ( db->is_hull )      {
380                 MONITOR_INC(NumHullDebrisRend,1);
381                 submodel_render( db->model_num, db->submodel_num, &obj->orient, &obj->pos );
382         } else {
383                 MONITOR_INC(NumSmallDebrisRend,1);
384                 submodel_render( db->model_num, db->submodel_num, &obj->orient, &obj->pos, MR_NO_LIGHTING );
385         }
386
387         if ((swapped!=-1) && pm)        {
388                 pm->textures[0] = swapped;
389         }
390 }
391
392 // Removed the DEBRIS_EXPIRE flag, and remove item from Hull_debris_list
393 void debris_clear_expired_flag(debris *db)
394 {
395         if ( db->flags & DEBRIS_EXPIRE ) {
396                 db->flags &= ~DEBRIS_EXPIRE;
397                 if ( db->is_hull ) {
398                         Num_hull_pieces--;
399                         list_remove(Hull_debris_list, db);
400                         Assert( Num_hull_pieces >= 0 );
401                 }
402         }
403 }
404
405 // ---------------------------------------------------------------------------------------
406 // debris_delete()
407 //
408 // Delete the debris object.  This is only ever called via obj_delete().  Do not call directly.
409 // Use debris_start_death_roll() if you want to force a debris piece to die.
410 //
411 void debris_delete( object * obj )
412 {
413         int             num;
414         debris  *db;
415
416         num = obj->instance;
417         Assert( Debris[num].objnum == OBJ_INDEX(obj));
418
419         db = &Debris[num];
420
421         Assert( Num_debris_pieces >= 0 );
422         if ( db->is_hull && (db->flags & DEBRIS_EXPIRE) ) {
423                 debris_clear_expired_flag(db);
424         }
425
426         db->flags = 0;
427         Num_debris_pieces--;
428 }
429
430 //      If debris piece *db is far away from all players, make it go away very soon.
431 //      In single player game, delete if MAX_DEBRIS_DIST from player.
432 //      In multiplayer game, delete if MAX_DEBRIS_DIST from all players.
433 void maybe_delete_debris(debris *db)
434 {
435         object  *objp;
436
437         if (timestamp_elapsed(db->next_distance_check)) {
438                 if (!(Game_mode & GM_MULTIPLAYER)) {            //      In single player game, just check against player.
439                         if (vm_vec_dist_quick(&Player_obj->pos, &Objects[db->objnum].pos) > MAX_DEBRIS_DIST)
440                                 db->lifeleft = 0.1f;
441                         else
442                                 db->next_distance_check = timestamp(DEBRIS_DISTANCE_CHECK_TIME);
443                 } else {
444                         for ( objp = GET_FIRST(&obj_used_list); objp !=END_OF_LIST(&obj_used_list); objp = GET_NEXT(objp) ) {
445                                 if (objp->flags & OF_PLAYER_SHIP) {
446                                         if (vm_vec_dist_quick(&objp->pos, &Objects[db->objnum].pos) < MAX_DEBRIS_DIST) {
447                                                 db->next_distance_check = timestamp(DEBRIS_DISTANCE_CHECK_TIME);
448                                                 return;
449                                         }
450                                 }
451                         }
452                         db->lifeleft = 0.1f;
453                 }
454         }
455 }
456
457 // broke debris_move into debris_process_pre and debris_process_post as was done with all
458 // *_move functions on 8/13 by MK and MA.
459 void debris_process_pre( object *objp, float frame_time)
460 {
461 }
462
463 MONITOR(NumSmallDebris);
464 MONITOR(NumHullDebris);
465
466 // ---------------------------------------------------------------------------------------
467 // debris_process_post()
468 //
469 // Do various updates to debris:  check if time to die, start fireballs
470 //
471 // parameters:          obj                     =>              pointer to debris object
472 //                                              frame_time      =>              time elapsed since last debris_move() called
473 //
474 //      Maybe delete debris if it's very far away from player.
475 void debris_process_post(object * obj, float frame_time)
476 {
477         int i, num;
478         num = obj->instance;
479
480         int objnum = OBJ_INDEX(obj);
481         Assert( Debris[num].objnum == objnum );
482         debris *db = &Debris[num];
483
484         if ( db->is_hull ) {
485                 MONITOR_INC(NumHullDebris,1);
486                 radar_plot_object( obj );
487
488                 if ( timestamp_elapsed(db->sound_delay) ) {
489                         obj_snd_assign(objnum, SND_DEBRIS, &vmd_zero_vector, 0);
490                         db->sound_delay = 0;
491                 }
492         } else {
493                 MONITOR_INC(NumSmallDebris,1);
494         }
495
496         if ( db->lifeleft >= 0.0f) {
497                 db->lifeleft -= frame_time;
498                 if ( db->lifeleft < 0.0f )      {
499                         debris_start_death_roll(obj, db);
500                 }
501         }
502
503         maybe_delete_debris(db);        //      Make this debris go away if it's very far away.
504
505         // ================== DO THE ELECTRIC ARCING STUFF =====================
506         if ( db->arc_frequency <= 0 )   {
507                 return;                 // If arc_frequency <= 0, this piece has no arcs on it
508         }
509
510         if ( !timestamp_elapsed(db->fire_timeout) && timestamp_elapsed(db->next_fireball))      {
511
512                 // start the next fireball up in the next 50 - 100 ms
513                 //db->next_fireball = timestamp_rand(60,80);            
514
515                 db->next_fireball = timestamp_rand(db->arc_frequency,db->arc_frequency*2 );
516                 db->arc_frequency += 100;       
517
518                 if (db->is_hull)        {
519
520                         int n, n_arcs = ((rand()>>5) % 3)+1;            // Create 1-3 sparks
521
522                         vector v1, v2, v3, v4;
523                         submodel_get_two_random_points( db->model_num, db->submodel_num, &v1, &v2 );
524                         submodel_get_two_random_points( db->model_num, db->submodel_num, &v3, &v4 );
525
526                         n = 0;
527
528                         int a = 100, b = 1000;
529                         int lifetime = (myrand()%((b)-(a)+1))+(a);
530
531                         // Create the spark effects
532                         for (i=0; i<MAX_DEBRIS_ARCS; i++ )      {
533                                 if ( !timestamp_valid( db->arc_timestamp[i] ) ) {
534                                         //db->arc_timestamp[i] = timestamp_rand(400,1000);      // live up to a second
535                                         db->arc_timestamp[i] = timestamp(lifetime);     // live up to a second
536
537                                         switch( n )     {
538                                         case 0:
539                                                 db->arc_pts[i][0] = v1;
540                                                 db->arc_pts[i][1] = v2;
541                                                 break;
542                                         case 1:
543                                                 db->arc_pts[i][0] = v2;
544                                                 db->arc_pts[i][1] = v3;
545                                                 break;
546
547                                         case 2:
548                                                 db->arc_pts[i][0] = v2;
549                                                 db->arc_pts[i][1] = v4;
550                                                 break;
551
552                                         default:
553                                                 Int3();
554                                         }
555                                                 
556                                         n++;
557                                         if ( n == n_arcs )
558                                                 break;  // Don't need to create anymore
559                                 }
560                         }
561
562         
563                         // rotate v2 out of local coordinates into world.
564                         // Use v2 since it is used in every bolt.  See above switch().
565                         vector snd_pos;
566                         vm_vec_unrotate(&snd_pos, &v2, &obj->orient);
567                         vm_vec_add2(&snd_pos, &obj->pos );
568
569                         //Play a sound effect
570                         if ( lifetime > 750 )   {
571                                 // 1.00 second effect
572                                 snd_play_3d( &Snds[SND_DEBRIS_ARC_05], &snd_pos, &View_position, obj->radius );
573                         } else if ( lifetime >  500 )   {
574                                 // 0.75 second effect
575                                 snd_play_3d( &Snds[SND_DEBRIS_ARC_04], &snd_pos, &View_position, obj->radius );
576                         } else if ( lifetime >  250 )   {
577                                 // 0.50 second effect
578                                 snd_play_3d( &Snds[SND_DEBRIS_ARC_03], &snd_pos, &View_position, obj->radius );
579                         } else if ( lifetime >  100 )   {
580                                 // 0.25 second effect
581                                 snd_play_3d( &Snds[SND_DEBRIS_ARC_02], &snd_pos, &View_position, obj->radius );
582                         } else {
583                                 // 0.10 second effect
584                                 snd_play_3d( &Snds[SND_DEBRIS_ARC_01], &snd_pos, &View_position, obj->radius );
585                         }
586
587                 }
588
589
590
591         }
592
593         for (i=0; i<MAX_DEBRIS_ARCS; i++ )      {
594                 if ( timestamp_valid( db->arc_timestamp[i] ) )  {
595                         if ( timestamp_elapsed( db->arc_timestamp[i] ) )        {
596                                 // Kill off the spark
597                                 db->arc_timestamp[i] = timestamp(-1);
598                         } else {
599                                 // Maybe move a vertex....  20% of the time maybe?
600                                 int mr = myrand();
601                                 if ( mr < RAND_MAX/5 )  {
602                                         vector v1, v2;
603                                         submodel_get_two_random_points( db->model_num, db->submodel_num, &v1, &v2 );
604                                         db->arc_pts[i][mr % 2] = v1;
605                                 }
606                         }
607                 }
608         }
609
610 }
611
612 // ---------------------------------------------------------------------------------------
613 // debris_find_oldest()
614 //
615 // Locate the oldest hull debris chunk.  Search through the Hull_debris_list, which is a list
616 // of all the hull debris chunks.
617 //
618 int debris_find_oldest()
619 {
620         int             oldest_index;
621         fix             oldest_time;
622         debris  *db;
623
624         oldest_index = -1;
625         oldest_time = 0x7fffffff;
626
627         for ( db = GET_FIRST(&Hull_debris_list); db != END_OF_LIST(&Hull_debris_list); db = GET_NEXT(db) ) {
628                 if ( (db->time_started < oldest_time) && !(Objects[db->objnum].flags & OF_SHOULD_BE_DEAD) ) {
629                         oldest_index = DEBRIS_INDEX(db);
630                         oldest_time = db->time_started;
631                 }
632         }
633
634         return oldest_index;
635 }
636
637 #define DEBRIS_ROTVEL_SCALE     5.0f
638 void calc_debris_physics_properties( physics_info *pi, vector *min, vector *max );
639 // ---------------------------------------------------------------------------------------
640 // debris_create()
641 //
642 // Create debris from an object
643 //
644 //      exp_force:      Explosion force, used to assign velocity to pieces.
645 //                                      1.0f assigns velocity like before.  2.0f assigns twice as much to non-inherited part of velocity
646 object *debris_create(object *source_obj, int model_num, int submodel_num, vector *pos, vector *exp_center, int hull_flag, float exp_force)
647 {
648         int             i, n, objnum, parent_objnum;
649         object  *obj;
650         ship            *shipp;
651         debris  *db;    
652         polymodel *pm;
653         int vaporize;
654
655         parent_objnum = OBJ_INDEX(source_obj);
656
657         Assert( (source_obj->type == OBJ_SHIP ) || (source_obj->type == OBJ_GHOST));
658         Assert( source_obj->instance >= 0 && source_obj->instance < MAX_SHIPS );        
659         shipp = &Ships[source_obj->instance];
660         vaporize = (shipp->flags &SF_VAPORIZE);
661
662         if ( !hull_flag )       {
663                 // Make vaporize debris seen from farther away
664                 float dist = vm_vec_dist_quick( pos, &Eye_position );
665                 if (vaporize) {
666                         dist /= 2.0f;
667                 }
668                 if ( dist > 200.0f ) {
669                         //mprintf(( "Not creating debris that is %.1f m away\n", dist ));
670                         return NULL;
671                 }
672         }
673
674         if ( hull_flag && (Num_hull_pieces >= MAX_HULL_PIECES ) ) {
675                 // cause oldest hull debris chunk to blow up
676                 n = debris_find_oldest();
677                 if ( n >= 0 ) {
678                         debris_start_death_roll(&Objects[Debris[n].objnum], &Debris[n] );
679                 }
680         }
681
682         for (n=0; n<MAX_DEBRIS_PIECES; n++ ) {
683                 if ( !(Debris[n].flags & DEBRIS_USED) )
684                         break;
685         }
686
687         if ( n == MAX_DEBRIS_PIECES ) {
688                 nprintf(("Warning","Frame %i: Could not create debris, no more slots left\n", Framecount));
689                 return NULL;
690         }
691
692         db = &Debris[n];
693
694         // Create Debris piece n!
695         if ( hull_flag ) {
696                 if (rand() < RAND_MAX/6)        // Make some pieces blow up shortly after explosion.
697                         db->lifeleft = 2.0f * ((float) myrand()/(float) RAND_MAX) + 0.5f;
698                 else
699                         db->lifeleft = -1.0f;           // large hull pieces stay around forever
700         }       else {
701                 db->lifeleft = (i2fl(myrand())/i2fl(RAND_MAX))*2.0f+0.1f;
702         }
703
704         // increase lifetime for vaporized debris
705         if (vaporize) {
706                 db->lifeleft *= 3.0f;
707         }
708         db->flags |= DEBRIS_USED;
709         db->is_hull = hull_flag;
710         db->source_objnum = parent_objnum;
711         db->source_sig = source_obj->signature;
712         db->ship_info_index = shipp->ship_info_index;
713         db->team = shipp->team;
714         db->fire_timeout = 0;   // if not changed, timestamp_elapsed() will return false
715         db->time_started = Missiontime;
716         db->species = Ship_info[shipp->ship_info_index].species;
717         db->next_distance_check = (myrand() % 2000) + 4*DEBRIS_DISTANCE_CHECK_TIME;
718
719         for (i=0; i<MAX_DEBRIS_ARCS; i++ )      {
720                 db->arc_timestamp[i] = timestamp(-1);
721                 //      vector  arc_pts[MAX_DEBRIS_ARCS][2];            // The endpoints of each arc
722         }
723
724         if ( db->is_hull )      {
725                 // Only make 1/2 of the pieces have arcs
726                 if ( myrand() < RAND_MAX*2/3 )  {
727                         db->arc_frequency = 1000;
728                 } else {
729                         db->arc_frequency = 0;
730                 }
731         } else {
732                 db->arc_frequency = 0;
733         }
734
735         if ( model_num < 0 )    {
736                 if (vaporize) {
737                         db->model_num = Debris_vaporize_model;
738                 } else {
739                         db->model_num = Debris_model;
740                 }
741                 db->submodel_num = (myrand()>>4) % Debris_num_submodels;
742         } else {
743                 db->model_num = model_num;
744                 db->submodel_num = submodel_num;
745         }
746         float radius = submodel_get_radius( db->model_num, db->submodel_num );
747
748         db->next_fireball = timestamp_rand(500,2000);   //start one 1/2 - 2 secs later
749
750         if ( pos == NULL )
751                 pos = &source_obj->pos;
752
753         uint flags = OF_RENDERS | OF_PHYSICS;
754         if ( hull_flag )        
755                 flags |= OF_COLLIDES;
756         objnum = obj_create( OBJ_DEBRIS, parent_objnum, n, &source_obj->orient, pos, radius, flags );
757         if ( objnum == -1 ) {
758                 mprintf(("Couldn't create debris object -- out of object slots\n"));
759                 return NULL;
760         }
761
762         db->objnum = objnum;
763         
764         obj = &Objects[objnum];
765
766         // assign the network signature.  The signature will be 0 for non-hull pieces, but since that
767         // is our invalid signature, it should be okay.
768         obj->net_signature = 0;
769         if ( (Game_mode & GM_MULTIPLAYER) && hull_flag ) {
770                 obj->net_signature = multi_get_next_network_signature( MULTI_SIG_DEBRIS );
771         }
772
773         // -- No long need shield: bset_shield_strength(obj, 100.0f);           //      Hey!  Set to some meaningful value!
774
775         if (source_obj->type == OBJ_SHIP) {
776                 obj->hull_strength = Ship_info[Ships[source_obj->instance].ship_info_index].initial_hull_strength/8.0f;
777         } else
778                 obj->hull_strength = 10.0f;
779
780         Num_debris_pieces++;
781
782         vector rotvel, radial_vel, to_center;
783
784         if ( exp_center )
785                 vm_vec_sub( &to_center,pos, exp_center );
786         else
787                 vm_vec_zero(&to_center);
788
789         float scale;
790
791         if ( hull_flag )        {
792                 float t;
793                 scale = exp_force * i2fl((myrand()%20) + 10);   // for radial_vel away from location of blast center
794                 db->sound_delay = timestamp(DEBRIS_SOUND_DELAY);
795
796                 // set up physics mass and I_inv for hull debris pieces
797                 pm = model_get(model_num);
798                 vector *min, *max;
799                 min = &pm->submodel[submodel_num].min;
800                 max = &pm->submodel[submodel_num].max;
801                 calc_debris_physics_properties( &obj->phys_info, min, max );
802
803                 // limit the amount of time that fireballs appear
804                 // let fireball length be linked to radius of ship.  Range is .33 radius => 3.33 radius seconds.
805                 t = 1000*Objects[db->source_objnum].radius/3 + myrand()%(fl2i(1000*3*Objects[db->source_objnum].radius));
806                 db->fire_timeout = timestamp(fl2i(t));          // fireballs last from 5 - 30 seconds
807                 
808                 if ( Objects[db->source_objnum].radius < MIN_RADIUS_FOR_PERSISTANT_DEBRIS ) {
809                         db->flags |= DEBRIS_EXPIRE;     // debris can expire
810                         Num_hull_pieces++;
811                         list_append(&Hull_debris_list, db);
812                 } else {
813                         nprintf(("Alan","A forever chunk of debris was created from ship with radius %f\n",Objects[db->source_objnum].radius));
814                 }
815         }
816         else {
817                 scale = exp_force * i2fl((myrand()%20) + 10);   // for radial_vel away from blast center (non-hull)
818         }
819
820         if ( vm_vec_mag_squared( &to_center ) < 0.1f )  {
821                 vm_vec_rand_vec_quick(&radial_vel);
822                 vm_vec_scale(&radial_vel, scale );
823         }
824         else {
825                 vm_vec_normalize(&to_center);
826                 vm_vec_copy_scale(&radial_vel, &to_center, scale );
827         }
828
829         //      MK: This next line causes debris pieces to get between 50% and 100% of the parent ship's
830         //      velocity.  What would be very cool is if the rotational velocity of the parent would become 
831         // translational velocity of the debris piece.  This would be based on the location of the debris
832         //      piece in the parent object.
833
834         // DA: here we need to vel_from_rot = w x to_center, where w is world is unrotated to world coords and offset is the 
835         // displacement fromt the center of the parent object to the center of the debris piece
836
837         vector world_rotvel, vel_from_rotvel;
838         vm_vec_unrotate ( &world_rotvel, &source_obj->phys_info.rotvel, &source_obj->orient );
839         vm_vec_crossprod ( &vel_from_rotvel, &world_rotvel, &to_center );
840         vm_vec_scale ( &vel_from_rotvel, DEBRIS_ROTVEL_SCALE);
841
842         vm_vec_add (&obj->phys_info.vel, &radial_vel, &source_obj->phys_info.vel);
843         vm_vec_add2(&obj->phys_info.vel, &vel_from_rotvel);
844
845 #ifdef DEBRIS_SPEED_DEBUG
846         // check that debris is not created with too high a velocity
847         if (hull_flag) {
848                 int ship_info_flag = Ship_info[Ships[source_obj->instance].ship_info_index].flags;
849                 if (ship_info_flag & (SIF_SMALL_SHIP | SIF_NOT_FLYABLE | SIF_HARMLESS)) {
850                         if (vm_vec_mag_squared(&obj->phys_info.vel) > MAX_SPEED_SMALL_DEBRIS*MAX_SPEED_SMALL_DEBRIS) {
851                                 float scale = MAX_SPEED_SMALL_DEBRIS / vm_vec_mag(&obj->phys_info.vel);
852                                 vm_vec_scale(&obj->phys_info.vel, scale);
853                         }
854                 } else if (ship_info_flag & SIF_BIG_SHIP) {
855                         if (vm_vec_mag_squared(&obj->phys_info.vel) > MAX_SPEED_BIG_DEBRIS*MAX_SPEED_BIG_DEBRIS) {
856                                 float scale = MAX_SPEED_BIG_DEBRIS / vm_vec_mag(&obj->phys_info.vel);
857                                 vm_vec_scale(&obj->phys_info.vel, scale);
858                         }
859                 } else if (ship_info_flag & SIF_HUGE_SHIP) {
860                         if (vm_vec_mag_squared(&obj->phys_info.vel) > MAX_SPEED_CAPITAL_DEBRIS*MAX_SPEED_CAPITAL_DEBRIS) {
861                                 float scale = MAX_SPEED_CAPITAL_DEBRIS / vm_vec_mag(&obj->phys_info.vel);
862                                 vm_vec_scale(&obj->phys_info.vel, scale);
863                         }
864                 } else {
865                         Warning(LOCATION, "Ship has info flag that is not among the following:  SMALL, NOT_FLYABLE, HARMLESS, BIG, CAPITAL, SUPERCAP");
866                 }
867         }
868 #endif
869
870 //      vm_vec_scale_add(&obj->phys_info.vel, &radial_vel, &source_obj->phys_info.vel, frand()/2.0f + 0.5f);
871 //      nprintf(("Andsager","object vel from rotvel: %0.2f, %0.2f, %0.2f\n",vel_from_rotvel.x, vel_from_rotvel.y, vel_from_rotvel.z));
872
873 // make sure rotational velocity does not get too high
874         if (radius < 1.0) {
875                 radius = 1.0f;
876         }
877
878         scale = ( 6.0f + i2fl(myrand()%4) ) / radius;
879
880         vm_vec_rand_vec_quick(&rotvel);
881         vm_vec_scale(&rotvel, scale);
882
883         obj->phys_info.flags |= PF_DEAD_DAMP;
884         obj->phys_info.rotvel = rotvel;
885         check_rotvel_limit( &obj->phys_info );
886
887
888         // blow out his reverse thrusters. Or drag, same thing.
889         obj->phys_info.rotdamp = 10000.0f;
890         obj->phys_info.side_slip_time_const = 10000.0f;
891         obj->phys_info.flags |= (PF_REDUCED_DAMP | PF_DEAD_DAMP);       // set damping equal for all axis and not changable
892
893         vm_vec_zero(&obj->phys_info.max_vel);           // make so he can't turn on his own VOLITION anymore.
894         vm_vec_zero(&obj->phys_info.max_rotvel);        // make so he can't change speed on his own VOLITION anymore.
895
896
897         // ensure vel is valid
898         Assert( !vm_is_vec_nan(&obj->phys_info.vel) );
899
900 //      if ( hull_flag )        {
901 //              vm_vec_zero(&obj->phys_info.vel);
902 //              vm_vec_zero(&obj->phys_info.rotvel);
903 //      }
904
905         return obj;
906 }
907
908 // ---------------------------------------------------------------------------------------
909 // debris_hit()
910 //
911 //      Alas, poor debris_obj got whacked.  Fortunately, we know who did it, where and how hard, so we
912 //      can do something about it.
913 //
914 void debris_hit(object *debris_obj, object *other_obj, vector *hitpos, float damage)
915 {
916         debris  *debris_p = &Debris[debris_obj->instance];
917
918
919         // Do a little particle spark shower to show we hit
920         {
921                 particle_emitter        pe;
922
923                 pe.pos = *hitpos;                                                               // Where the particles emit from
924                 pe.vel = debris_obj->phys_info.vel;             // Initial velocity of all the particles
925
926                 vector tmp_norm;
927                 vm_vec_sub( &tmp_norm, hitpos, &debris_obj->pos );
928                 vm_vec_normalize_safe(&tmp_norm);
929                         
930                 pe.normal = tmp_norm;                   // What normal the particle emit around
931                 pe.normal_variance = 0.3f;              //      How close they stick to that normal 0=good, 1=360 degree
932                 pe.min_rad = 0.20f;                             // Min radius
933                 pe.max_rad = 0.40f;                             // Max radius
934
935                 // Sparks for first time at this spot
936                 pe.num_low = 10;                                // Lowest number of particles to create
937                 pe.num_high = 10;                       // Highest number of particles to create
938                 pe.normal_variance = 0.3f;              //      How close they stick to that normal 0=good, 1=360 degree
939                 pe.min_vel = 0.0f;                              // How fast the slowest particle can move
940                 pe.max_vel = 10.0f;                             // How fast the fastest particle can move
941                 pe.min_life = 0.25f;                    // How long the particles live
942                 pe.max_life = 0.75f;                    // How long the particles live
943                 particle_emit( &pe, PARTICLE_FIRE, 0 );
944         }
945
946         // multiplayer clients bail here
947         if(MULTIPLAYER_CLIENT){
948                 return;
949         }
950
951         if ( damage < 0.0f ) {
952                 damage = 0.0f;
953         }
954
955         debris_obj->hull_strength -= damage;
956
957         if (debris_obj->hull_strength < 0.0f) {
958                 debris_start_death_roll(debris_obj, debris_p );
959         } else {
960                 // otherwise, give all the other players an update on the debris
961                 if(MULTIPLAYER_MASTER){
962                         send_debris_update_packet(debris_obj,DEBRIS_UPDATE_UPDATE);
963                 }
964         }
965 }
966
967 // ---------------------------------------------------------------------------------------
968 // debris_check_collision()
969 //
970 //      See if poor debris object *obj got whacked by evil *other_obj at point *hitpos.
971 // NOTE: debris_hit_info pointer NULL for debris:weapon collision, otherwise debris:ship collision.
972 //      Return true if hit, else return false.
973 //
974 #pragma warning ( push )
975 #pragma warning ( disable : 4701 )
976 int debris_check_collision(object *pdebris, object *other_obj, vector *hitpos, collision_info_struct *debris_hit_info)
977 {
978         mc_info mc;
979         int             num;
980
981         Assert( pdebris->type == OBJ_DEBRIS );
982
983         num = pdebris->instance;
984         Assert( num >= 0 );
985
986         Assert( Debris[num].objnum == OBJ_INDEX(pdebris));      
987
988         // debris_hit_info NULL - so debris-weapon collision
989         if ( debris_hit_info == NULL ) {
990                 // debris weapon collision
991                 Assert( other_obj->type == OBJ_WEAPON );
992                 mc.model_num = Debris[num].model_num;   // Fill in the model to check
993                 mc.submodel_num = Debris[num].submodel_num;
994                 model_clear_instance( mc.model_num );
995                 mc.orient = &pdebris->orient;                                   // The object's orient
996                 mc.pos = &pdebris->pos;                                                 // The object's position
997                 mc.p0 = &other_obj->last_pos;                           // Point 1 of ray to check
998                 mc.p1 = &other_obj->pos;                                        // Point 2 of ray to check
999                 mc.flags = (MC_CHECK_MODEL | MC_SUBMODEL);
1000
1001                 if (model_collide(&mc)) {
1002                         *hitpos = mc.hit_point_world;
1003                 }
1004
1005                 return mc.num_hits;
1006         }
1007         
1008         // debris ship collision -- use debris_hit_info to calculate physics
1009         object *ship_obj = other_obj;
1010         Assert( ship_obj->type == OBJ_SHIP );
1011
1012         object *heavy = debris_hit_info->heavy;
1013         object *light = debris_hit_info->light;
1014         object *heavy_obj = heavy;
1015         object *light_obj = light;
1016
1017         vector zero, p0, p1;
1018         vm_vec_zero(&zero);
1019         vm_vec_sub(&p0, &light->last_pos, &heavy->last_pos);
1020         vm_vec_sub(&p1, &light->pos, &heavy->pos);
1021
1022         mc.pos = &zero;                                                         // The object's position
1023         mc.p0 = &p0;                                                                    // Point 1 of ray to check
1024         mc.p1 = &p1;                                                                    // Point 2 of ray to check
1025
1026         // find the light object's position in the heavy object's reference frame at last frame and also in this frame.
1027         vector p0_temp, p0_rotated;
1028                 
1029         // Collision detection from rotation enabled if at rotaion is less than 30 degree in frame
1030         // This should account for all ships
1031         if ( (vm_vec_mag_squared(&heavy->phys_info.rotvel) * flFrametime*flFrametime) < (PI*PI/36) ) {
1032                 // collide_rotate calculate (1) start position and (2) relative velocity
1033                 debris_hit_info->collide_rotate = 1;
1034                 vm_vec_rotate(&p0_temp, &p0, &heavy->last_orient);
1035                 vm_vec_unrotate(&p0_rotated, &p0_temp, &heavy->orient);
1036                 mc.p0 = &p0_rotated;                            // Point 1 of ray to check
1037                 vm_vec_sub(&debris_hit_info->light_rel_vel, &p1, &p0_rotated);
1038                 vm_vec_scale(&debris_hit_info->light_rel_vel, 1/flFrametime);
1039         } else {
1040                 debris_hit_info->collide_rotate = 0;
1041                 vm_vec_sub(&debris_hit_info->light_rel_vel, &light->phys_info.vel, &heavy->phys_info.vel);
1042         }
1043
1044         int mc_ret_val = 0;
1045
1046         if ( debris_hit_info->heavy == ship_obj ) {     // ship is heavier, so debris is sphere. Check sphere collision against ship poly model
1047                 mc.model_num = Ships[ship_obj->instance].modelnum;              // Fill in the model to check
1048                 mc.orient = &ship_obj->orient;                                                          // The object's orient
1049                 mc.radius = pdebris->radius;
1050                 mc.flags = (MC_CHECK_MODEL | MC_CHECK_SPHERELINE);
1051
1052                 // copy important data
1053                 int copy_flags = mc.flags;  // make a copy of start end positions of sphere in  big ship RF
1054                 vector copy_p0, copy_p1;
1055                 copy_p0 = *mc.p0;
1056                 copy_p1 = *mc.p1;
1057
1058                 // first test against the sphere - if this fails then don't do any submodel tests
1059                 mc.flags = MC_ONLY_SPHERE | MC_CHECK_SPHERELINE;
1060
1061                 int submodel_list[MAX_ROTATING_SUBMODELS];
1062                 int num_rotating_submodels = 0;
1063                 polymodel *pm;
1064
1065                 ship_model_start(ship_obj);
1066
1067                 if (model_collide(&mc)) {
1068
1069                         // Set earliest hit time
1070                         debris_hit_info->hit_time = FLT_MAX;
1071
1072                         // Do collision the cool new way
1073                         if ( debris_hit_info->collide_rotate ) {
1074                                 // We collide with the sphere, find the list of rotating submodels and test one at a time
1075                                 model_get_rotating_submodel_list(submodel_list, &num_rotating_submodels, heavy_obj);
1076
1077                                 // Get polymodel and turn off all rotating submodels, collide against only 1 at a time.
1078                                 pm = model_get(Ships[heavy_obj->instance].modelnum);
1079
1080                                 // turn off all rotating submodels and test for collision
1081                                 for (int i=0; i<num_rotating_submodels; i++) {
1082                                         pm->submodel[submodel_list[i]].blown_off = 1;
1083                                 }
1084
1085                                 // reset flags to check MC_CHECK_MODEL | MC_CHECK_SPHERELINE and maybe MC_CHECK_INVISIBLE_FACES and MC_SUBMODEL_INSTANCE
1086                                 mc.flags = copy_flags | MC_SUBMODEL_INSTANCE;
1087
1088                                 // check each submodel in turn
1089                                 for (int i=0; i<num_rotating_submodels; i++) {
1090                                         // turn on submodel for collision test
1091                                         pm->submodel[submodel_list[i]].blown_off = 0;
1092
1093                                         // set angles for last frame (need to set to prev to get p0)
1094                                         angles copy_angles = pm->submodel[submodel_list[i]].angs;
1095
1096                                         // find the start and end positions of the sphere in submodel RF
1097                                         pm->submodel[submodel_list[i]].angs = pm->submodel[submodel_list[i]].sii->prev_angs;
1098                                         world_find_model_point(&p0, &light_obj->last_pos, pm, submodel_list[i], &heavy_obj->last_orient, &heavy_obj->last_pos);
1099
1100                                         pm->submodel[submodel_list[i]].angs = copy_angles;
1101                                         world_find_model_point(&p1, &light_obj->pos, pm, submodel_list[i], &heavy_obj->orient, &heavy_obj->pos);
1102
1103                                         mc.p0 = &p0;
1104                                         mc.p1 = &p1;
1105                                         // mc.pos = zero        // in submodel RF
1106
1107                                         mc.orient = &vmd_identity_matrix;
1108                                         mc.submodel_num = submodel_list[i];
1109
1110                                         if ( model_collide(&mc) ) {
1111                                                 if ( mc.hit_dist < debris_hit_info->hit_time ) {
1112                                                         mc_ret_val = 1;
1113
1114                                                         // set up debris_hit_info common
1115                                                         set_hit_struct_info(debris_hit_info, &mc, SUBMODEL_ROT_HIT);
1116                                                         model_find_world_point(&debris_hit_info->hit_pos, &mc.hit_point, mc.model_num, mc.hit_submodel, &heavy_obj->orient, &zero);
1117
1118                                                         // set up debris_hit_info for rotating submodel
1119                                                         if (debris_hit_info->edge_hit == 0) {
1120                                                                 model_find_obj_dir(&debris_hit_info->collision_normal, &mc.hit_normal, heavy_obj, mc.hit_submodel);
1121                                                         }
1122
1123                                                         // find position in submodel RF of light object at collison
1124                                                         vector int_light_pos, diff;
1125                                                         vm_vec_sub(&diff, mc.p1, mc.p0);
1126                                                         vm_vec_scale_add(&int_light_pos, mc.p0, &diff, mc.hit_dist);
1127                                                         model_find_world_point(&debris_hit_info->light_collision_cm_pos, &int_light_pos, mc.model_num, mc.hit_submodel, &heavy_obj->orient, &zero);
1128                                                 }
1129                                         }
1130                                         // Don't look at this submodel again
1131                                         pm->submodel[submodel_list[i]].blown_off = 1;
1132                                 }
1133
1134                         }
1135
1136                         // Recover and do usual ship_ship collision, but without rotating submodels
1137                         mc.flags = copy_flags;
1138                         *mc.p0 = copy_p0;
1139                         *mc.p1 = copy_p1;
1140                         mc.orient = &heavy_obj->orient;
1141
1142                         // usual ship_ship collision test
1143                         if ( model_collide(&mc) )       {
1144                                 // check if this is the earliest hit
1145                                 if (mc.hit_dist < debris_hit_info->hit_time) {
1146                                         mc_ret_val = 1;
1147
1148                                         set_hit_struct_info(debris_hit_info, &mc, SUBMODEL_NO_ROT_HIT);
1149
1150                                         // get collision normal if not edge hit
1151                                         if (debris_hit_info->edge_hit == 0) {
1152                                                 model_find_obj_dir(&debris_hit_info->collision_normal, &mc.hit_normal, heavy_obj, mc.hit_submodel);
1153                                         }
1154
1155                                         // find position in submodel RF of light object at collison
1156                                         vector diff;
1157                                         vm_vec_sub(&diff, mc.p1, mc.p0);
1158                                         vm_vec_scale_add(&debris_hit_info->light_collision_cm_pos, mc.p0, &diff, mc.hit_dist);
1159
1160                                 }
1161                         }
1162
1163                         ship_model_stop( ship_obj );
1164                 }
1165
1166         } else {
1167                 // Debris is heavier obj
1168                 mc.model_num = Debris[num].model_num;           // Fill in the model to check
1169                 mc.submodel_num = Debris[num].submodel_num;
1170                 model_clear_instance( mc.model_num );
1171                 mc.orient = &pdebris->orient;                           // The object's orient
1172                 mc.radius = model_get_core_radius( Ships[ship_obj->instance].modelnum );
1173
1174                 // check for collision between debris model and ship sphere
1175                 mc.flags = (MC_CHECK_MODEL | MC_SUBMODEL | MC_CHECK_SPHERELINE);
1176
1177                 mc_ret_val = model_collide(&mc);
1178
1179                 if (mc_ret_val) {
1180                         set_hit_struct_info(debris_hit_info, &mc, SUBMODEL_NO_ROT_HIT);
1181
1182                         // set normal if not edge hit
1183                         if ( !debris_hit_info->edge_hit ) {
1184                                 vm_vec_unrotate(&debris_hit_info->collision_normal, &mc.hit_normal, &heavy->orient);
1185                         }
1186
1187                         // find position in submodel RF of light object at collison
1188                         vector diff;
1189                         vm_vec_sub(&diff, mc.p1, mc.p0);
1190                         vm_vec_scale_add(&debris_hit_info->light_collision_cm_pos, mc.p0, &diff, mc.hit_dist);
1191
1192                 }
1193         }
1194
1195
1196         if ( mc_ret_val )       {
1197
1198                 // SET PHYSICS PARAMETERS
1199                 // already have (hitpos - heavy) and light_cm_pos
1200                 // get heavy cm pos - already have light_cm_pos
1201                 debris_hit_info->heavy_collision_cm_pos = zero;
1202
1203                 // get r_heavy and r_light
1204                 debris_hit_info->r_heavy = debris_hit_info->hit_pos;
1205                 vm_vec_sub(&debris_hit_info->r_light, &debris_hit_info->hit_pos, &debris_hit_info->light_collision_cm_pos);
1206
1207                 // set normal for edge hit
1208                 if ( debris_hit_info->edge_hit ) {
1209                         vm_vec_copy_normalize(&debris_hit_info->collision_normal, &debris_hit_info->r_light);
1210                         vm_vec_negate(&debris_hit_info->collision_normal);
1211                 }
1212
1213                 // get world hitpos
1214                 vm_vec_add(hitpos, &debris_hit_info->heavy->pos, &debris_hit_info->r_heavy);
1215
1216                 return 1;
1217         } else {
1218                 // no hit
1219                 return 0;
1220         }
1221 }
1222 #pragma warning ( pop )
1223
1224
1225 // ---------------------------------------------------------------------------------------
1226 // debris_get_team()
1227 //
1228 //      Return the team field for a debris object
1229 //
1230 int debris_get_team(object *objp)
1231 {
1232         Assert( objp->type == OBJ_DEBRIS );
1233         Assert( objp->instance >= 0 && objp->instance < MAX_DEBRIS_PIECES );
1234         return Debris[objp->instance].team;
1235 }
1236
1237 // fills in debris physics properties when created, specifically mass and moment of inertia
1238 void calc_debris_physics_properties( physics_info *pi, vector *mins, vector *maxs )
1239 {
1240         float dx, dy, dz, mass;
1241         dx = maxs->x - mins->x;
1242         dy = maxs->y - mins->y;
1243         dz = maxs->z - mins->z;
1244
1245         // John, with new bspgen, just set pi->mass = mass
1246         mass = 0.12f * dx * dy * dz;
1247         pi->mass = (float) pow(mass, 0.6666667f) * 4.65f;
1248
1249         pi->I_body_inv.rvec.x = 12.0f / (pi->mass *  (dy*dy + dz*dz));
1250         pi->I_body_inv.rvec.y = 0.0f;
1251         pi->I_body_inv.rvec.z = 0.0f;
1252
1253         pi->I_body_inv.uvec.x = 0.0f;
1254         pi->I_body_inv.uvec.y = 12.0f / (pi->mass *  (dx*dx + dz*dz));
1255         pi->I_body_inv.uvec.z = 0.0f;
1256
1257         pi->I_body_inv.fvec.x = 0.0f;
1258         pi->I_body_inv.fvec.y = 0.0f;
1259         pi->I_body_inv.fvec.z = 12.0f / (pi->mass *  (dx*dx + dy*dy));
1260 }
1261
1262