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