]> icculus.org git repositories - taylor/freespace2.git/blob - src/object/objcollide.cpp
Initial revision
[taylor/freespace2.git] / src / object / objcollide.cpp
1 /*
2  * $Logfile: /Freespace2/code/Object/ObjCollide.cpp $
3  * $Revision$
4  * $Date$
5  * $Author$
6  *
7  * Helper routines for all the collision detection functions
8  * Also keeps track of all the object pairs.
9  *
10  * $Log$
11  * Revision 1.1  2002/05/03 03:28:10  root
12  * Initial revision
13  *
14  * 
15  * 18    7/15/99 9:20a Andsager
16  * FS2_DEMO initial checkin
17  * 
18  * 17    6/14/99 3:21p Andsager
19  * Allow collisions between ship and its debris.  Fix up collision pairs
20  * when large ship is warping out.
21  * 
22  * 16    5/08/99 8:25p Dave
23  * Upped object pairs. First run of nebula lightning.
24  * 
25  * 15    4/23/99 12:01p Johnson
26  * Added SIF_HUGE_SHIP
27  * 
28  * 14    4/21/99 6:15p Dave
29  * Did some serious housecleaning in the beam code. Made it ready to go
30  * for anti-fighter "pulse" weapons. Fixed collision pair creation. Added
31  * a handy macro for recalculating collision pairs for a given object.
32  * 
33  * 13    4/20/99 3:41p Andsager
34  * Modify objects_will_collide to use endpoints
35  * 
36  * 12    3/10/99 6:50p Dave
37  * Changed the way we buffer packets for all clients. Optimized turret
38  * fired packets. Did some weapon firing optimizations.
39  * 
40  * 11    3/05/99 3:32p Dan
41  * Make out of collision pars message npirntf with "collision"
42  * 
43  * 10    1/21/99 2:06p Dave
44  * Final checkin for multiplayer testing.
45  * 
46  * 9     1/21/99 10:44a Dave
47  * More beam weapon stuff. Put in warmdown time.
48  * 
49  * 8     1/15/99 5:01p Dave
50  * Fix for object collision problem.
51  * 
52  * 7     1/12/99 5:45p Dave
53  * Moved weapon pipeline in multiplayer to almost exclusively client side.
54  * Very good results. Bandwidth goes down, playability goes up for crappy
55  * connections. Fixed object update problem for ship subsystems.
56  * 
57  * 6     1/12/99 12:53a Dave
58  * More work on beam weapons - made collision detection very efficient -
59  * collide against all object types properly - made 3 movement types
60  * smooth. Put in test code to check for possible non-darkening pixels on
61  * object textures.
62  * 
63  * 5     11/19/98 11:08p Andsager
64  * Check in of physics and collision detection of rotating submodels
65  * 
66  * 4     11/05/98 5:55p Dave
67  * Big pass at reducing #includes
68  * 
69  * 3     10/26/98 9:42a Dave
70  * Early flak gun support.
71  * 
72  * 2     10/07/98 10:53a Dave
73  * Initial checkin.
74  * 
75  * 1     10/07/98 10:50a Dave
76  * 
77  * 38    7/29/98 3:24p Allender
78  * don't call collision code when either of the object pairs is NULL.
79  * 
80  * 37    4/08/98 8:38a Mike
81  * Make ships avoid player when attacking or guarding a faraway object.
82  * 
83  * 36    4/02/98 11:40a Lawrance
84  * check for #ifdef DEMO instead of #ifdef DEMO_RELEASE
85  * 
86  * 35    4/01/98 1:48p Allender
87  * major changes to ship collision in multiplayer.  Clients now do own
88  * ship/ship collisions (with their own ship only)  Modifed the hull
89  * update packet to be sent quicker when object is target of player.
90  * 
91  * 34    4/01/98 10:57a Mike
92  * Reduce array sizes to save memory.
93  * 
94  * 33    3/31/98 5:18p John
95  * Removed demo/save/restore.  Made NDEBUG defined compile.  Removed a
96  * bunch of debug stuff out of player file.  Made model code be able to
97  * unload models and malloc out only however many models are needed.
98  *  
99  * 
100  * 32    3/06/98 12:45a Lawrance
101  * Rmove a bogus assert in the object pair save/restore
102  * 
103  * 31    3/05/98 3:12p Mike
104  * Fix bugs in asteroid collisions.  Throw asteroids at Galataea (needs to
105  * be made general).  Add skill level support for asteroid throwing.
106  * 
107  * 30    3/05/98 12:12a Mike
108  * Make an asteroid in the way prevent player warpout.
109  * 
110  * 29    2/27/98 4:48p John
111  * Made objects keep track of number of pairs they have associated with
112  * them.  Then, I can early out of the obj_remove_all which was 2.5% of
113  * frametime at beginning of sm2-2 which then is 0% after this.
114  * 
115  * 28    2/26/98 10:07p Hoffoss
116  * Rewrote state saving and restoring to fix bugs and simplify the code.
117  * 
118  * 27    2/23/98 9:00a Andsager
119  * improved back face culling of object:laser pairs to check relative
120  * velocity dot laser forward < 0 vs. v_other dot v_laser < 0
121  * 
122  * 26    2/19/98 4:34p Lawrance
123  * Allow weapon-asteroid collisions
124  * 
125  * 25    2/19/98 10:52a Sandeep
126  * AL: Have asteroids collide with all ships
127  * 
128  * 24    2/12/98 11:45a Andsager
129  * Try to cull debris:weapon collisions before they collisoin pair is
130  * created.  Look at movement in lifetime and whether debris is behind
131  * laser.
132  * 
133  * 23    2/07/98 3:54p John
134  * Added FVI to show_game_resources
135  * 
136  * 22    2/06/98 7:29p John
137  * Made only players ships and player ship weapons do collisions with
138  * asteroids.
139  * 
140  * 21    2/05/98 11:49p Andsager
141  * Don't check laser:ship collisions on same team for small ships if laser
142  * source not player
143  * 
144  * 20    2/05/98 9:21p John
145  * Some new Direct3D code.   Added code to monitor a ton of stuff in the
146  * game.
147  * 
148  * 19    2/05/98 12:51a Mike
149  * Early asteroid stuff.
150  * 
151  * 18    2/03/98 9:40a Andsager
152  * Modify collision object pair creation code so that pairs between debris
153  * and weapons (other than player) are not created.
154  * 
155  * 17    1/28/98 11:06a Andsager
156  * Remove some collision pairs.  Add debug code for displaying collision
157  * pair info.  
158  * 
159  * 16    1/24/98 11:16a Andsager
160  * Remove debug  info.
161  * 
162  * 15    1/24/98 11:09a Andsager
163  * Fixed bug that was removing collision pairs when weapon within the
164  * radius of object.
165  * 
166  * 14    1/23/98 5:07p Andsager
167  * Added tests for object pairs.  (1)  for weapons that do not turn, find
168  * the soonest hit time.  (2)  Check if soonest hit time if after end of
169  * weapon lifetime.
170  * 
171  * 13    1/22/98 6:43p John
172  * Took out debug code.
173  * 
174  * 12    1/22/98 6:42p John
175  * Move game_flush out of debug console into freespace.     Made object
176  * pair code throw out some weapons that turn.   Added stats for how many
177  * object pair are actually checked.
178  * 
179  * 11    1/17/98 10:06p Lawrance
180  * For missiles, only collide if WIF_BOMB flag set.
181  * 
182  * 10    1/14/98 5:21p Allender
183  * system to delete object when buffer is nearly full.  System in place to
184  * delete weapons when nearly out of weapons slots
185  * 
186  * 9     1/13/98 8:09p John
187  * Removed the old collision system that checked all pairs.   Added code
188  * to disable collisions and particles.
189  * 
190  * 8     1/13/98 3:11p Allender
191  * new code to remove old weapons when no more weapon slots available.
192  * Currently not called anywhere pending further testing
193  * 
194  * 7     1/10/98 3:22p Allender
195  * temporary check in to get new code
196  * 
197  * 6     1/10/98 1:14p John
198  * Added explanation to debug console commands
199  * 
200  * 5     1/09/98 9:29a Mike
201  * Enable docked ships to warp out.  Make damage done to a ship not
202  * proportional to its own mass.
203  * 
204  * 4     1/08/98 12:12a Mike
205  * Make ships turn before warping out, if necessary, to avoid a collision.
206  * Warn player if his warpout will collide.  Abort if in stage1.
207  * 
208  * 3     12/21/97 4:33p John
209  * Made debug console functions a class that registers itself
210  * automatically, so you don't need to add the function to
211  * debugfunctions.cpp.  
212  * 
213  * 2     12/16/97 5:23p Allender
214  * don't deal with object collision paris on multiplayer clients
215  * 
216  * 1     9/17/97 2:14p John
217  * Initial revision
218  *
219  * $NoKeywords: $
220  */
221
222 #include "objcollide.h"
223 #include "linklist.h"
224 #include "systemvars.h"
225 #include "timer.h"
226 #include "multi.h"
227 #include "beam.h"
228
229 #ifdef FS2_DEMO
230         #define MAX_PAIRS 3000
231 #else
232         #define MAX_PAIRS 8000  //      Reduced from 10,000 to 6,000 by MK on 4/1/98.
233                                                                         //      Most I saw was 3400 in sm1-06a, the asteriod mission.  No other mission came close.
234 #endif
235
236 // the next 3 variables are used for pair statistics
237 // also in weapon.cpp there is Weapons_created.
238 int Pairs_created = 0;
239 int Num_pairs = 0;
240 int Num_pairs_checked = 0;
241 int pairs_not_created = 0;
242
243 int Num_pairs_hwm = 0;
244
245 obj_pair Obj_pairs[MAX_PAIRS];
246
247 obj_pair pair_used_list;
248 obj_pair pair_free_list;
249
250 void obj_reset_pairs()
251 {
252         int i;
253         
254 //      mprintf(( "Resetting object pairs...\n" ));
255
256         pair_used_list.a = pair_used_list.b = NULL;             
257         pair_used_list.next = NULL;
258         pair_free_list.a = pair_free_list.b = NULL;             
259         pair_free_list.next = &Obj_pairs[0];
260         
261         Num_pairs = 0;
262         for (i=0; i<MAX_PAIRS; i++ )    {
263                 Obj_pairs[i].a = Obj_pairs[i].b = NULL;
264                 Obj_pairs[i].next = &Obj_pairs[i+1];
265         }
266         Obj_pairs[MAX_PAIRS-1].next = NULL;
267 }
268
269 // returns true if we should reject object pair if one is child of other.
270 int reject_obj_pair_on_parent(object *A, object *B)
271 {
272         if (A->type == OBJ_SHIP) {
273                 if (B->type == OBJ_DEBRIS) {
274                         if (B->parent_sig == A->signature) {
275                                 return 0;
276                         }
277                 }
278         }
279
280         if (B->type == OBJ_SHIP) {
281                 if (A->type == OBJ_DEBRIS) {
282                         if (A->parent_sig == B->signature) {
283                                 return 0;
284                         }
285                 }
286         }
287
288         if (A->parent_sig == B->signature) {
289                 return 1;
290         }
291
292         if (B->parent_sig == A->signature) {
293                 return 1;
294         }
295
296         return 0;
297 }
298
299
300 // Adds the pair to the pair list
301 void obj_add_pair( object *A, object *B, int check_time, int add_to_end )
302 {
303         uint ctype;
304         int (*check_collision)( obj_pair *pair );
305         int swapped = 0;        
306         
307         check_collision = NULL;
308
309         if ( A==B ) return;             // Don't check collisions with yourself
310
311         if ( !(A->flags&OF_COLLIDES) ) return;          // This object doesn't collide with anything
312         if ( !(B->flags&OF_COLLIDES) ) return;          // This object doesn't collide with anything
313         
314         // Make sure you're not checking a parent with it's kid or vicy-versy
315 //      if ( A->parent_sig == B->signature && !(A->type == OBJ_SHIP && B->type == OBJ_DEBRIS) ) return;
316 //      if ( B->parent_sig == A->signature && !(A->type == OBJ_DEBRIS && B->type == OBJ_SHIP) ) return;
317         if ( reject_obj_pair_on_parent(A,B) ) {
318                 return;
319         }
320
321         Assert( A->type < 127 );
322         Assert( B->type < 127 );
323
324         ctype = COLLISION_OF(A->type,B->type);
325         switch( ctype ) {
326         case COLLISION_OF(OBJ_WEAPON,OBJ_SHIP):
327                 swapped = 1;
328                 check_collision = collide_ship_weapon;
329                 break;
330         case COLLISION_OF(OBJ_SHIP, OBJ_WEAPON):
331                 check_collision = collide_ship_weapon;
332                 break;
333         case COLLISION_OF(OBJ_DEBRIS, OBJ_WEAPON):
334                 check_collision = collide_debris_weapon;
335                 break;
336         case COLLISION_OF(OBJ_WEAPON, OBJ_DEBRIS):
337                 swapped = 1;
338                 check_collision = collide_debris_weapon;
339                 break;
340         case COLLISION_OF(OBJ_DEBRIS, OBJ_SHIP):
341                 check_collision = collide_debris_ship;          
342                 break;
343         case COLLISION_OF(OBJ_SHIP, OBJ_DEBRIS):
344                 check_collision = collide_debris_ship;
345                 swapped = 1;
346                 break;
347         case COLLISION_OF(OBJ_ASTEROID, OBJ_WEAPON):
348                 // Only check collision's with player weapons
349 //              if ( Objects[B->parent].flags & OF_PLAYER_SHIP ) {
350                         check_collision = collide_asteroid_weapon;
351 //              }
352                 break;
353         case COLLISION_OF(OBJ_WEAPON, OBJ_ASTEROID):
354                 swapped = 1;
355                 // Only check collision's with player weapons
356 //              if ( Objects[A->parent].flags & OF_PLAYER_SHIP ) {
357                         check_collision = collide_asteroid_weapon;
358 //              }
359                 break;
360         case COLLISION_OF(OBJ_ASTEROID, OBJ_SHIP):
361                 // Only check collisions with player ships
362 //              if ( B->flags & OF_PLAYER_SHIP )        {
363                         check_collision = collide_asteroid_ship;
364 //              }
365                 break;
366         case COLLISION_OF(OBJ_SHIP, OBJ_ASTEROID):
367                 // Only check collisions with player ships
368 //              if ( A->flags & OF_PLAYER_SHIP )        {
369                         check_collision = collide_asteroid_ship;
370 //              }
371                 swapped = 1;
372                 break;
373         case COLLISION_OF(OBJ_SHIP,OBJ_SHIP):
374                 check_collision = collide_ship_ship;
375                 break;  
376         
377         case COLLISION_OF(OBJ_BEAM, OBJ_SHIP):
378                 if(beam_collide_early_out(A, B)){
379                         return;
380                 }
381                 check_collision = beam_collide_ship;
382                 break;
383
384         case COLLISION_OF(OBJ_BEAM, OBJ_ASTEROID):
385                 if(beam_collide_early_out(A, B)){
386                         return;
387                 }
388                 check_collision = beam_collide_asteroid;
389                 break;
390
391         case COLLISION_OF(OBJ_BEAM, OBJ_DEBRIS):
392                 if(beam_collide_early_out(A, B)){
393                         return;
394                 }
395                 check_collision = beam_collide_debris;
396                 break;
397
398         case COLLISION_OF(OBJ_BEAM, OBJ_WEAPON):
399                 if(beam_collide_early_out(A, B)){
400                         return;
401                 }               
402                 check_collision = beam_collide_missile;
403                 break;
404
405         case COLLISION_OF(OBJ_WEAPON, OBJ_WEAPON): {
406                 weapon_info *awip, *bwip;
407                 awip = &Weapon_info[Weapons[A->instance].weapon_info_index];
408                 bwip = &Weapon_info[Weapons[B->instance].weapon_info_index];
409                 
410                 if (awip->subtype != WP_LASER || bwip->subtype != WP_LASER) {
411                         if (awip->subtype == WP_LASER) {
412                                 if ( bwip->wi_flags & WIF_BOMB ) {
413                                         check_collision = collide_weapon_weapon;
414                                 }
415                         } else if (bwip->subtype == WP_LASER) {
416                                 if ( awip->wi_flags & WIF_BOMB ) {
417                                         check_collision = collide_weapon_weapon;
418                                         swapped=1;                      
419                                 }
420                         } else {
421                                 if ( (awip->wi_flags&WIF_BOMB) || (bwip->wi_flags&WIF_BOMB) ) {
422                                         check_collision = collide_weapon_weapon;
423                                 }
424                         }
425                 }
426 /*
427                 int     atype, btype;
428
429                 atype = Weapon_info[Weapons[A->instance].weapon_info_index].subtype;
430                 btype = Weapon_info[Weapons[B->instance].weapon_info_index].subtype;
431
432                 if ((atype == WP_LASER) && (btype == WP_MISSILE))
433                         check_collision = collide_weapon_weapon;
434                 else if ((atype == WP_MISSILE) && (btype == WP_LASER)) {
435                         check_collision = collide_weapon_weapon;
436                         swapped = 1;
437                 } else if ((atype == WP_MISSILE) && (btype == WP_MISSILE))
438                         check_collision = collide_weapon_weapon;
439 */
440
441                 break;
442         }
443
444         default:
445                 return;
446         }
447
448         // Swap them if needed
449         if ( swapped )  {
450                 object *tmp = A;
451                 A = B;
452                 B = tmp;
453         }
454
455         // if there are any more obj_pair checks
456         // we should then add function int maybe_not_add_obj_pair()
457         // MWA -- 4/1/98 -- I'd do it, but I don't want to bust anything, so I'm doing my stuff here instead :-)
458         //if ( MULTIPLAYER_CLIENT && !(Netgame.debug_flags & NETD_FLAG_CLIENT_NODAMAGE)){
459                 // multiplayer clients will only do ship/ship collisions, and their own ship to boot
460         //      if ( check_collision != collide_ship_ship ){
461         //              return;
462         //      }
463
464         //      if ( (A != Player_obj) && (B != Player_obj) ){
465         //              return;
466         //      }
467         //}     
468
469         // only check debris:weapon collisions for player
470         if (check_collision == collide_debris_weapon) {
471                 // weapon is B
472                 if ( !(Weapon_info[Weapons[B->instance].weapon_info_index].wi_flags & WIF_TURNS) ) {
473                 // check for dumbfire weapon
474                         // check if debris is behind laser
475                         float vdot;
476                         if (Weapon_info[Weapons[B->instance].weapon_info_index].subtype == WP_LASER) {
477                                 vector velocity_rel_weapon;
478                                 vm_vec_sub(&velocity_rel_weapon, &B->phys_info.vel, &A->phys_info.vel);
479                                 vdot = -vm_vec_dot(&velocity_rel_weapon, &B->orient.fvec);
480                         } else {
481                                 vdot = vm_vec_dot( &A->phys_info.vel, &B->phys_info.vel);
482                         }
483                         if ( vdot <= 0.0f )     {
484                                 // They're heading in opposite directions...
485                                 // check their positions
486                                 vector weapon2other;
487                                 vm_vec_sub( &weapon2other, &A->pos, &B->pos );
488                                 float pdot = vm_vec_dot( &B->orient.fvec, &weapon2other );
489                                 if ( pdot <= -A->radius )       {
490                                         // The other object is behind the weapon by more than
491                                         // its radius, so it will never hit...
492                                         return;
493                                 }
494                         }
495
496                         // check dist vs. dist moved during weapon lifetime
497                         vector delta_v;
498                         vm_vec_sub(&delta_v, &B->phys_info.vel, &A->phys_info.vel);
499                         if (vm_vec_dist_squared(&A->pos, &B->pos) > (vm_vec_mag_squared(&delta_v)*Weapons[B->instance].lifeleft*Weapons[B->instance].lifeleft)) {
500                                 return;
501                         }
502
503                         // for nonplayer ships, only create collision pair if close enough
504                         if ( !(Objects[B->parent].flags & OF_PLAYER_SHIP) && (vm_vec_dist(&B->pos, &A->pos) < (4.0f*A->radius + 200.0f)) )
505                                 return;
506                 }
507         }
508
509         // don't check same team laser:ship collisions on small ships if not player
510         if (check_collision == collide_ship_weapon) {
511                 // weapon is B
512                 if ( !(Objects[B->parent].flags & OF_PLAYER_SHIP)
513                         && (Ships[Objects[B->parent].instance].team == Ships[A->instance].team) 
514                         && (Ship_info[Ships[A->instance].ship_info_index].flags & SIF_SMALL_SHIP) 
515                         && (Weapon_info[Weapons[B->instance].weapon_info_index].subtype == WP_LASER) ) {
516                         pairs_not_created++;
517                         return;
518                 }
519         }
520
521         if ( !check_collision ) return;
522         Pairs_created++;
523
524         // At this point, we have determined that collisions between
525         // these two should be checked, so add the pair to the
526         // collision pair list.
527
528         if ( pair_free_list.next == NULL )      {
529                 nprintf(( "collision", "Out of object pairs!! Not all collisions will work!\n" ));
530                 return;
531         }
532
533         // get a new obj_pair from the free list
534         obj_pair * new_pair = pair_free_list.next;
535         pair_free_list.next = new_pair->next;
536
537         if ( add_to_end ) {
538                 obj_pair *last, *tmp;
539
540                 last = tmp = pair_used_list.next;
541                 while( tmp != NULL )    {
542                         if ( tmp->next == NULL )
543                                 last = tmp;
544
545                         tmp = tmp->next;
546                 }
547
548                 if ( last == NULL )
549                         last = &pair_used_list;
550                         
551                 last->next = new_pair;
552                 Assert(new_pair != NULL);
553                 new_pair->next = NULL;
554         }
555         else {
556                 new_pair->next = pair_used_list.next;
557                 pair_used_list.next = new_pair;
558         }
559         
560         Num_pairs++;
561 /*      if (Num_pairs > Num_pairs_hwm) {
562                 Num_pairs_hwm = Num_pairs;
563                 //nprintf(("AI", "Num_pairs high water mark = %i\n", Num_pairs_hwm));
564         }
565 */
566
567         A->num_pairs++;
568         B->num_pairs++;
569         
570         new_pair->a = A;
571         new_pair->b = B;
572         new_pair->check_collision = check_collision;
573
574         if ( check_time == -1 ){
575                 new_pair->next_check_time = timestamp(0);       // 0 means instantly time out
576         } else {
577                 new_pair->next_check_time = check_time;
578         }
579
580 }
581
582 MONITOR(NumPairs);
583 MONITOR(NumPairsChecked);
584
585 void obj_check_all_collisions()
586 {
587         if ( !(Game_detail_flags & DETAIL_FLAG_COLLISION) )     return;
588
589         obj_pair *parent, *tmp; 
590         // debug info
591         float avg_time_to_next_check = 0.0f;
592                                                         
593         parent = &pair_used_list;
594         tmp = parent->next;
595
596         Num_pairs_checked = 0;
597
598         while( tmp != NULL )    {
599
600                 int removed = 0;
601                 if ( (tmp->a != NULL) && (tmp->b != NULL) && timestamp_elapsed(tmp->next_check_time) )  {
602                         Num_pairs_checked++;
603
604                         if ((*tmp->check_collision)(tmp))       {
605                                 // We never need to check this pair again.
606                                 #if 0   //def DONT_REMOVE_PAIRS
607                                         // Never check it again, but keep the pair around
608                                         // (useful for debugging)
609                                         tmp->next_check_time = timestamp(-1);           
610                                 #else
611                                         // Never check it again, so remove the pair
612                                         removed = 1;
613                                         tmp->a->num_pairs--;    
614                                         Assert( tmp->a->num_pairs > -1 );
615                                         tmp->b->num_pairs--;
616                                         Assert( tmp->b->num_pairs > -1 );
617                                         Num_pairs--;
618                                         // Assert(Num_pairs >= 0);
619                                         parent->next = tmp->next;
620                                         tmp->a = tmp->b = NULL;
621                                         tmp->next = pair_free_list.next;
622                                         pair_free_list.next = tmp;
623                                         tmp = parent->next;
624                                 #endif
625                         }
626                 } 
627
628                 if (!removed)   {
629                         parent = tmp;
630                         tmp = tmp->next;
631                         // debug info
632                         if (tmp) {
633                                 int add_time = timestamp_until( tmp->next_check_time );
634                                 if (add_time > 0)
635                                         avg_time_to_next_check += (float) add_time;
636                         }
637                 }
638         }
639
640         MONITOR_INC(NumPairs,Num_pairs);
641         MONITOR_INC(NumPairsChecked,Num_pairs_checked);
642
643         // #define PAIR_STATS
644         #ifdef PAIR_STATS
645         avg_time_to_next_check = avg_time_to_next_check / Num_pairs;
646         extern int Num_hull_pieces;
647         extern int Weapons_created;
648         // mprintf(( "[pairs checked: %d, start_pairs: %d, num obj: %d, avg next time: %f]\n", n, org_pairs, num_objects, avg_time_to_next_check ));
649         // mprintf(( "[Num_hull_pieces: %3d, Num_weapons_created: %3d, pairs_not_created: %3d, pairs_created: %3d, percent new saved: %9.5f]\n", Num_hull_pieces, Weapons_created, pairs_not_created, Pairs_created, 100.0f*(float)pairs_not_created/(float)(pairs_not_created + Pairs_created) ));
650          mprintf(( "[pairs_created: %3d, pairs_not_created: %3d, percent saved %6.3f]\n", Pairs_created, pairs_not_created, 100.0f*pairs_not_created/(Pairs_created+pairs_not_created) ));
651         pairs_not_created = 0;
652         Weapons_created = 0;
653         Pairs_created = 0;
654         #endif
655
656         // What percent of the pairs did we check?
657         // FYI: (n*(n-1))/2 is the total number of checks required for comparing n objects.
658
659 //      if ( org_pairs > 1 )    {
660 //              Object_checked_percentage = (i2fl(n)*100.0f) / i2fl(org_pairs);
661 //      } else {
662 //              Object_checked_percentage = 0.0f;
663 //      }
664
665 }
666
667 //      See if two lines intersect by doing recursive subdivision.
668 //      Bails out if larger distance traveled is less than sum of radii + 1.0f.
669 int collide_subdivide(vector *p0, vector *p1, float prad, vector *q0, vector *q1, float qrad)
670 {
671         float   a_dist, b_dist, ab_dist;
672
673         a_dist = vm_vec_dist(p0, p1);
674         b_dist = vm_vec_dist(q0, q1);
675
676         ab_dist = vm_vec_dist(p1, q1);
677
678         //      See if their spheres intersect
679         if (ab_dist < a_dist + b_dist + prad + qrad) {
680                 if (ab_dist  < prad + qrad)
681                         return 1;
682                 else if (vm_vec_dist(p0, q0) < prad + qrad)
683                         return 1;
684                 else if (max(a_dist, b_dist) < prad + qrad + 1.0f)
685                         return 0;
686                 else {
687                         int     r1, r2 = 0;
688                         vector  pa, qa;
689
690                         vm_vec_avg(&pa, p0, p1);
691                         vm_vec_avg(&qa, q0, q1);
692                         r1 = collide_subdivide(p0, &pa, prad, q0, &qa, qrad);
693                         if (!r1)
694                                 r2 = collide_subdivide(&pa, p1, prad, &qa, q1, qrad);
695
696                         return r1 | r2;
697                 }
698         } else
699                 return 0;
700 }
701
702
703
704 //      Return true if object A is expected to collide with object B within time duration
705 //      For purposes of this check, the first object moves from current location to predicted
706 //      location.  The second object is assumed to be where it will be at time duration, NOT
707 //      where it currently is.
708 //      radius_scale is used to control the precision of the check.
709 //              If 0.0, then use polygon models to perform check, slow and accurate
710 //              If !0.0, then use as a scale on the radius of the objects.  1.0 is Descent style
711 //                      collisions.  Larger values can be used to be sloppy about the collisions which
712 //                      is useful if a moving object wants to prevent a collision.
713 int objects_will_collide(object *A, object *B, float duration, float radius_scale)
714 {
715         object  A_future;
716         vector  hitpos;
717
718
719         A_future = *A;
720         vm_vec_scale_add2(&A_future.pos, &A->phys_info.vel, duration);
721
722         if (radius_scale == 0.0f) {
723                 return ship_check_collision_fast(B, &A_future, &hitpos );
724         } else {
725                 float           size_A, size_B, dist, r;
726                 vector  nearest_point;
727
728                 size_A = A->radius * radius_scale;
729                 size_B = B->radius * radius_scale;
730
731                 //      If A is moving, check along vector.
732                 if (A->phys_info.speed != 0.0f) {
733                         r = find_nearest_point_on_line(&nearest_point, &A->pos, &A_future.pos, &B->pos);
734                         if (r < 0) {
735                                 nearest_point = A->pos;
736                         } else if (r > 1) {
737                                 nearest_point = A_future.pos;
738                         }
739                         dist = vm_vec_dist_quick(&B->pos, &nearest_point);
740                         return (dist < size_A + size_B);
741                 } else {
742                         return vm_vec_dist_quick(&B->pos, &A->pos) < size_A + size_B;
743                 }
744         }
745 }
746
747 //      Return true if the vector from *start_pos to *end_pos is within objp->radius*radius_scale of *objp
748 int vector_object_collision(vector *start_pos, vector *end_pos, object *objp, float radius_scale)
749 {
750         float           dist, r;
751         vector  nearest_point;
752
753         r = find_nearest_point_on_line(&nearest_point, start_pos, end_pos, &objp->pos);
754         if ((r >= 0.0f) && (r <= 1.0f)) {
755                 dist = vm_vec_dist_quick(&objp->pos, &nearest_point);
756
757                 return (dist < objp->radius * radius_scale);
758         } else
759                 return 0;
760 }
761
762 // Returns TRUE if the weapon will never hit the other object.
763 // If it can it predicts how long until these two objects need
764 // to be checked and fills the time in in current_pair.
765 int weapon_will_never_hit( object *weapon, object *other, obj_pair * current_pair )
766 {
767
768         Assert( weapon->type == OBJ_WEAPON );
769
770 //      mprintf(( "Frame: %d,  Weapon=%d, Other=%d, pair=$%08x\n", G3_frame_count, OBJ_INDEX(weapon), OBJ_INDEX(other), current_pair ));
771         
772
773         // Do some checks for weapons that don't turn
774         if ( !(Weapon_info[Weapons[weapon->instance].weapon_info_index].wi_flags & WIF_TURNS) ) {
775
776                 // This first check is to see if a weapon is behind an object, and they
777                 // are heading in opposite directions.   If so, we don't need to ever check     
778                 // them again.   This is only valid for weapons that don't turn. 
779
780                 float vdot;
781                 if (Weapon_info[Weapons[weapon->instance].weapon_info_index].subtype == WP_LASER) {
782                         vector velocity_rel_weapon;
783                         vm_vec_sub(&velocity_rel_weapon, &weapon->phys_info.vel, &other->phys_info.vel);
784                         vdot = -vm_vec_dot(&velocity_rel_weapon, &weapon->orient.fvec);
785                 } else {
786                         vdot = vm_vec_dot( &other->phys_info.vel, &weapon->phys_info.vel);
787                 }
788                 if ( vdot <= 0.0f )     {
789                         // They're heading in opposite directions...
790                         // check their positions
791                         vector weapon2other;
792                         vm_vec_sub( &weapon2other, &other->pos, &weapon->pos );
793                         float pdot = vm_vec_dot( &weapon->orient.fvec, &weapon2other );
794                         if ( pdot <= -other->radius )   {
795                                 // The other object is behind the weapon by more than
796                                 // its radius, so it will never hit...
797                                 return 1;
798                         }
799                 }
800
801                 // FUTURE ENHANCEMENT IDEAS 
802
803                 // Given a laser does it hit a slow or not moving object
804                 // in its life or the next n seconds?  We'd actually need to check the 
805                 // model for this.
806                                 
807         }
808
809
810         // This check doesn't care about orient, only looks at the maximum speed
811         // of the two objects, so it knows that in the next n seconds, they can't
812         // go further than some distance, so don't bother checking collisions for 
813         // that time.   This is very rough, but is so general that it works for
814         // everything and immidiately gets rid of a lot of cases.
815         
816         if ( current_pair )     {
817                 // Find the time it will take before these get within each others distances.
818                 // tmp->next_check_time = timestamp(500);
819                 //vector        max_vel;                        //maximum foward velocity in x,y,z
820
821                 float max_vel_weapon, max_vel_other;
822                 max_vel_weapon = weapon->phys_info.max_vel.z;
823                 max_vel_other = other->phys_info.max_vel.z;
824                 if (max_vel_other < 10.0f) {
825                         if ( vm_vec_mag_squared( &other->phys_info.vel ) > 100 ) {
826                                 // bump up velocity from collision
827                                 max_vel_other = vm_vec_mag( &other->phys_info.vel ) + 10.0f;
828                         } else {
829                                 max_vel_other = 10.0f;          // object may move from collision
830                         }
831                 }
832
833                 // check weapon that does not turn against sphere expanding at ship maxvel
834                 // compare (weeapon) ray with expanding sphere (ship) to find earliest possible collision time
835                 // look for two time solutions to Xw = Xs, where Xw = Xw0 + Vwt*t  Xs = Xs + Vs*(t+dt), where Vs*dt = radius of ship 
836                 // Since direction of Vs is unknown, solve for (Vs*t) and find norm of both sides
837                 if ( !(Weapon_info[Weapons[weapon->instance].weapon_info_index].wi_flags & WIF_TURNS) ) {
838                         vector delta_x, laser_vel;
839                         float a,b,c, delta_x_dot_vl, delta_t;
840                         float root1, root2, root, earliest_time;
841
842                         vm_vec_sub( &delta_x, &weapon->pos, &other->pos );
843                         vm_vec_copy_scale( &laser_vel, &weapon->orient.fvec, max_vel_weapon );
844                         delta_t = (other->radius + 10.0f) / max_vel_other;              // time to get from center to radius of other obj
845                         delta_x_dot_vl = vm_vec_dotprod( &delta_x, &laser_vel );
846
847                         a = max_vel_weapon*max_vel_weapon - max_vel_other*max_vel_other;
848                         b = 2.0f * (delta_x_dot_vl - max_vel_other*max_vel_other*delta_t);
849                         c = vm_vec_mag_squared( &delta_x ) - max_vel_other*max_vel_other*delta_t*delta_t;
850
851                         float discriminant = b*b - 4.0f*a*c;
852                         if ( discriminant < 0) {
853                                 // never hit
854                                 return 1;
855                         } else {
856                                 root = fl_sqrt( discriminant );
857                                 root1 = (-b + root) / (2.0f * a) * 1000.0f;     // get time in ms
858                                 root2 = (-b - root) / (2.0f * a) * 1000.0f;     // get time in ms
859                         }
860
861                         // find earliest positive time
862                         if ( root1 > root2 ) {
863                                 float temp = root1;
864                                 root1 = root2;
865                                 root2 = temp;
866                         }
867
868                         if (root1 > 0) {
869                                 earliest_time = root1;
870                         } else if (root2 > 0) {
871                                 // root1 < 0 and root2 > 0, so we're inside sphere and next check should be next frame
872                                 current_pair->next_check_time = timestamp(0);   // check next time
873                                 return 0;
874                         } else {
875                                 // both times negative, so never collides
876                                 return 1;
877                         }
878
879
880
881                         // check if possible collision occurs after weapon expires
882                         if ( earliest_time > 1000*Weapons[weapon->instance].lifeleft )
883                                 return 1;
884
885                         // Allow one worst case frametime to elapse (~5 fps)
886                         earliest_time -= 200.0f;
887
888                         if (earliest_time > 100) {
889                                 current_pair->next_check_time = timestamp( fl2i(earliest_time) );
890                                 return 0;
891                         } else {
892                                 current_pair->next_check_time = timestamp(0);   // check next time
893                                 return 0;
894                         }
895
896                 } else {
897
898                         float dist, max_vel, time;
899
900                         max_vel = max_vel_weapon + max_vel_other;
901
902                         // suggest that fudge factor for other radius be changed to other_radius + const (~10)
903                         dist = vm_vec_dist( &other->pos, &weapon->pos ) - (other->radius + 10.0f);
904                         if ( dist > 0.0f )      {
905                                 time = (dist*1000.0f) / max_vel;
906                                 int time_ms = fl2i(time);
907
908                                 // check if possible collision occurs after weapon expires
909                                 if ( time_ms > 1000*Weapons[weapon->instance].lifeleft )
910                                         return 1;
911
912                                 time_ms -= 200; // Allow at least one worst case frametime to elapse (~5 fps)
913                                                 
914                                 if ( time_ms > 100 )    {               // If it takes longer than 1/10th of a second, then delay it
915                                         current_pair->next_check_time = timestamp(time_ms);
916                                         //mprintf(( "Delaying %d ms\n", time_ms ));
917                                         return 0;
918                                 }
919                         }
920                         current_pair->next_check_time = timestamp(0);   // check next time
921
922                 }
923         }
924
925         return 0;
926 }
927
928 //      Return true if vector from *curpos to *goalpos intersects with object *goalobjp
929 //      Else, return false.
930 //      radius is radius of object moving from curpos to goalpos.
931 int pp_collide(vector *curpos, vector *goalpos, object *goalobjp, float radius)
932 {
933         mc_info mc;
934
935         Assert(goalobjp->type == OBJ_SHIP);
936
937         ship_model_start(goalobjp);
938
939         mc.model_num = Ships[goalobjp->instance].modelnum;                      // Fill in the model to check
940         mc.orient = &goalobjp->orient;  // The object's orient
941         mc.pos = &goalobjp->pos;                        // The object's position
942         mc.p0 = curpos;                                 // Point 1 of ray to check
943         mc.p1 = goalpos;                                        // Point 2 of ray to check
944         mc.flags = MC_CHECK_MODEL | MC_CHECK_SPHERELINE;
945         mc.radius = radius;
946
947         model_collide(&mc);
948
949         ship_model_stop(goalobjp);
950
951         return mc.num_hits;
952 }
953
954 //      Setup and call pp_collide for collide_predict_large_ship
955 //      Returns true if objp will collide with objp2 before it reaches goal_pos.
956 int cpls_aux(vector *goal_pos, object *objp2, object *objp)
957 {
958         float   radius;
959         
960         radius = objp->radius;
961         if (1.5f * radius < 70.0f)
962                 radius *= 1.5f;
963         else
964                 radius = 70.0f;
965
966         if (pp_collide(&objp->pos, goal_pos, objp2, radius))
967                 return 1;
968         else
969                 return 0;
970 }
971
972 //      Return true if objp will collide with some large object.
973 //      Don't check for an object this ship is docked to.
974 int collide_predict_large_ship(object *objp, float distance)
975 {
976         object  *objp2;
977         vector  cur_pos, goal_pos;
978         object  *dock_objp;
979         ship_info       *sip;
980
981         sip = &Ship_info[Ships[objp->instance].ship_info_index];
982
983         dock_objp = NULL;
984         if (objp->type == OBJ_SHIP) {
985                 ai_info *aip = &Ai_info[Ships[objp->instance].ai_index];
986                 if (aip->dock_objnum != -1)
987                         dock_objp = &Objects[aip->dock_objnum];
988         }
989
990         cur_pos = objp->pos;
991
992         vm_vec_scale_add(&goal_pos, &cur_pos, &objp->orient.fvec, distance);
993
994         for ( objp2 = GET_FIRST(&obj_used_list); objp2 != END_OF_LIST(&obj_used_list); objp2 = GET_NEXT(objp2) ) {
995                 if ((objp != objp2) && (objp2->type == OBJ_SHIP)) {
996                         if (Ship_info[Ships[objp2->instance].ship_info_index].flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) {
997                                 if (objp2 == dock_objp)
998                                         continue;
999
1000                                 if (cpls_aux(&goal_pos, objp2, objp))
1001                                         return 1;
1002                         }
1003                 } else if (!(sip->flags & (SIF_BIG_SHIP | SIF_HUGE_SHIP)) && (objp2->type == OBJ_ASTEROID)) {
1004                         if (vm_vec_dist_quick(&objp2->pos, &objp->pos) < (distance + objp2->radius)*2.5f) {
1005                                 vector  pos, delvec;
1006                                 int             count;
1007                                 float           d1;
1008
1009                                 d1 = 2.5f * distance + objp2->radius;
1010                                 count = (int) (d1/(objp2->radius + objp->radius));      //      Scale up distance, else looks like there would be a collision.
1011                                 pos = cur_pos;
1012                                 vm_vec_normalized_dir(&delvec, &goal_pos, &cur_pos);
1013                                 vm_vec_scale(&delvec, d1/count);
1014
1015                                 for (; count>0; count--) {
1016                                         if (vm_vec_dist_quick(&pos, &objp2->pos) < objp->radius + objp2->radius)
1017                                                 return 1;
1018                                         vm_vec_add2(&pos, &delvec);
1019                                 }
1020                         }
1021                 }
1022         }
1023
1024         return 0;
1025 }
1026
1027 // function to iterate through all object collision pairs looking for weapons
1028 // which could be deleted since they are not going to hit anything.  Passed into this
1029 // function is a 'time' parameter used as watermark for which weapons to check.
1030
1031 #define CRW_NO_OBJECT           -1
1032 #define CRW_NO_PAIR                     0
1033 #define CRW_IN_PAIR                     1
1034 #define CRW_CAN_DELETE          2
1035
1036 #define CRW_MAX_TO_DELETE       4
1037
1038 char crw_status[MAX_WEAPONS];
1039
1040 void crw_check_weapon( int weapon_num, int collide_next_check )
1041 {
1042         float next_check_time;
1043         weapon *wp;
1044
1045         wp = &Weapons[weapon_num];
1046
1047         // if this weapons life left > time before next collision, then we cannot remove it
1048         crw_status[WEAPON_INDEX(wp)] = CRW_IN_PAIR;
1049         next_check_time = ((float)(timestamp_until(collide_next_check)) / 1000.0f);
1050         if ( wp->lifeleft < next_check_time )
1051                 crw_status[WEAPON_INDEX(wp)] = CRW_CAN_DELETE;
1052 }
1053
1054 int collide_remove_weapons( )
1055 {
1056         obj_pair *opp;
1057         int i, num_deleted, oldest_index, j, loop_count;
1058         float oldest_time;
1059
1060         // setup remove_weapon array.  assume we can remove it.
1061         for (i = 0; i < MAX_WEAPONS; i++ ) {
1062                 if ( Weapons[i].objnum == -1 )
1063                         crw_status[i] = CRW_NO_OBJECT;
1064                 else
1065                         crw_status[i] = CRW_NO_PAIR;
1066         }
1067
1068         // first pass is to see if any of the weapons don't have collision pairs.
1069
1070         opp = &pair_used_list;
1071         opp = opp->next;
1072         while( opp != NULL )    {
1073                 // for each collide pair, if the two objects can still collide, then set the remove_weapon
1074                 // parameter for the weapon to 0.  need to check both parameters
1075                 if ( opp->a->type == OBJ_WEAPON )
1076                         crw_check_weapon( opp->a->instance, opp->next_check_time );
1077
1078                 if ( opp->b->type == OBJ_WEAPON )
1079                         crw_check_weapon( opp->b->instance, opp->next_check_time );
1080
1081                 opp = opp->next;
1082         }
1083
1084         // for each weapon which could be removed, delete the object
1085         num_deleted = 0;
1086         for ( i = 0; i < MAX_WEAPONS; i++ ) {
1087                 if ( crw_status[i] == CRW_CAN_DELETE ) {
1088                         Assert( Weapons[i].objnum != -1 );
1089                         obj_delete( Weapons[i].objnum );
1090                         num_deleted++;
1091                 }
1092         }
1093
1094         if ( num_deleted )
1095                 return num_deleted;
1096
1097         // if we didn't remove any weapons, try to the N oldest weapons.  first checking for pairs, then
1098         // checking for oldest weapons in general.  We will go through the loop a max of 2 times.  first time
1099         // through, we check oldest weapons with pairs, next time through, for oldest weapons.
1100         loop_count = 0;
1101         do {
1102                 for ( j = 0; j < CRW_MAX_TO_DELETE; j++ ) {
1103                         oldest_time = 1000.0f;
1104                         oldest_index = -1;
1105                         for (i = 0; i < MAX_WEAPONS; i++ ) {
1106                                 if ( Weapons[i].objnum == -1 )                  // shouldn't happen, but this is the safe thing to do.
1107                                         continue;
1108                                 if ( ((loop_count || crw_status[i] == CRW_NO_PAIR)) && (Weapons[i].lifeleft < oldest_time) ) {
1109                                         oldest_time = Weapons[i].lifeleft;
1110                                         oldest_index = i;
1111                                 }
1112                         }
1113                         if ( oldest_index != -1 ) {
1114                                 obj_delete(Weapons[oldest_index].objnum);
1115                                 num_deleted++;
1116                         }
1117                 }
1118
1119                 // if we deleted some weapons, then we can break
1120                 if ( num_deleted )
1121                         break;
1122
1123                 loop_count++;
1124         } while ( loop_count < 2);
1125
1126         return num_deleted;
1127
1128 }
1129
1130 void set_hit_struct_info(collision_info_struct *hit, mc_info *mc, int submodel_rot_hit)
1131 {
1132         hit->edge_hit = mc->edge_hit;
1133         hit->hit_pos = mc->hit_point_world;
1134         hit->hit_time = mc->hit_dist;
1135         hit->submodel_num = mc->hit_submodel;
1136
1137         hit->submodel_rot_hit = submodel_rot_hit;
1138 }