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