]> icculus.org git repositories - taylor/freespace2.git/blob - src/object/collidedebrisship.cpp
added copyright header
[taylor/freespace2.git] / src / object / collidedebrisship.cpp
1 /*
2  * Copyright (C) Volition, Inc. 1999.  All rights reserved.
3  *
4  * All source code herein is the property of Volition, Inc. You may not sell 
5  * or otherwise commercially exploit the source or things you created based on
6  * the source.
7  */
8
9 /*
10  * $Logfile: /Freespace2/code/Object/CollideDebrisShip.cpp $
11  * $Revision$
12  * $Date$
13  * $Author$
14  *
15  * Routines to detect collisions and do physics, damage, etc for ships and debris
16  *
17  * $Log$
18  * Revision 1.3  2002/06/09 04:41:24  relnev
19  * added copyright header
20  *
21  * Revision 1.2  2002/05/03 13:34:33  theoddone33
22  * More stuff compiles
23  *
24  * Revision 1.1.1.1  2002/05/03 03:28:10  root
25  * Initial import.
26  *
27  * 
28  * 12    9/14/99 2:59a Andsager
29  * Limit amount of damage done by debris colliding with ship for SUPERCAP
30  * 
31  * 11    8/31/99 11:43p Andsager
32  * No ship:debris damage to ship if debris' parent is ship itself
33  * 
34  * 10    8/10/99 5:49p Andsager
35  * Fix bug - no damage collision against debris.
36  * 
37  * 9     8/09/99 4:45p Andsager
38  * Add HUD "collision " waring when colliding with asteroids and debris.
39  * 
40  * 8     7/15/99 9:20a Andsager
41  * FS2_DEMO initial checkin
42  * 
43  * 7     6/14/99 3:21p Andsager
44  * Allow collisions between ship and its debris.  Fix up collision pairs
45  * when large ship is warping out.
46  * 
47  * 6     1/12/99 10:24a Andsager
48  * Fix asteroid-ship collision bug, with negative collision
49  * 
50  * 5     10/23/98 1:11p Andsager
51  * Make ship sparks emit correctly from rotating structures.
52  * 
53  * 4     10/20/98 1:39p Andsager
54  * Make so sparks follow animated ship submodels.  Modify
55  * ship_weapon_do_hit_stuff() and ship_apply_local_damage() to add
56  * submodel_num.  Add submodel_num to multiplayer hit packet.
57  * 
58  * 3     10/16/98 1:22p Andsager
59  * clean up header files
60  * 
61  * 2     10/07/98 10:53a Dave
62  * Initial checkin.
63  * 
64  * 1     10/07/98 10:50a Dave
65  * 
66  * 27    4/24/98 5:35p Andsager
67  * Fix sparks sometimes drawing not on model.  If ship is sphere in
68  * collision, don't draw sparks.  Modify ship_apply_local_damage() to take
69  * parameter no_spark.
70  * 
71  * 26    4/02/98 6:29p Lawrance
72  * compile out asteroid references for demo
73  * 
74  * 25    4/02/98 5:10p Mike
75  * Decrease damage yet more (by 3x) when an asteroid collides with a ship
76  * during warp out.
77  * 
78  * 24    4/02/98 4:25p Andsager
79  * fix bug calculating max_ship_impulse
80  * 
81  * 23    3/30/98 10:35a Andsager
82  * Attempt to optimize ship:asteroid collisions.  Maybe return if time.
83  * 
84  * 22    3/25/98 2:37p Andsager
85  * Modify maximum damage done by asteroids, depends on ship's max_vel, not
86  * vel when warping out.
87  * 
88  * 21    3/25/98 2:15p Andsager
89  * 
90  * 20    3/25/98 1:36p Mike
91  * Balancing of sm1-06a, Galatea mission.
92  * 
93  * 19    3/23/98 11:14a Andsager
94  * Modified collide_asteroid_ship to calculate next possible collision
95  * time based on ship afterburner speed.
96  * 
97  * 18    3/20/98 12:02a Mike
98  * Cap damage done by an asteroid on a huge ship to 1/8 ship strength if a
99  * large ship.  If <4000 hull, can do more than 1/8.
100  * 
101  * 17    3/05/98 3:12p Mike
102  * Fix bugs in asteroid collisions.  Throw asteroids at Galataea (needs to
103  * be made general).  Add skill level support for asteroid throwing.
104  * 
105  * 16    3/04/98 11:21p Mike
106  * Less rotation on huge ships in death roll.
107  * Less damage done to huge ships when hit an asteroid at high speed.
108  * 
109  * 15    3/02/98 2:58p Mike
110  * Make "asteroids" in debug console turn asteroids on/off.
111  * 
112  * 14    2/19/98 12:46a Lawrance
113  * Further work on asteroids.
114  * 
115  * 13    2/07/98 2:14p Mike
116  * Improve asteroid splitting.  Add ship:asteroid collisions.  Timestamp
117  * ship:debris collisions.
118  * 
119  * 12    2/06/98 7:29p John
120  * Added code to cull out some future asteroid-ship collisions.
121  * 
122  * 11    2/05/98 12:51a Mike
123  * Early asteroid stuff.
124  * 
125  * 10    2/04/98 8:57p Lawrance
126  * remove unused variable
127  * 
128  * 9     2/04/98 6:08p Lawrance
129  * Add a light collision sound, overlay a shield collide sound if
130  * applicable.
131  * 
132  * 8     1/05/98 9:07p Andsager
133  * Changed ship_shipor_debris_hit_info struct to more meaninful names
134  * 
135  * 7     1/02/98 9:08a Andsager
136  * Changed ship:ship and ship:debris collision detection to ignore shields
137  * and collide only against hull.  Also, significantly reduced radius of
138  * sphere.
139  * 
140  * 6     12/22/97 9:56p Andsager
141  * Implement ship:debris collisions.  Generalize and move
142  * ship_ship_or_debris_hit struct from CollideShipShip to ObjCollide.h
143  * 
144  * 5     11/03/97 11:08p Lawrance
145  * play correct collision sounds.
146  * 
147  * 4     10/27/97 8:35a John
148  * code for new player warpout sequence
149  * 
150  * 3     10/01/97 5:55p Lawrance
151  * change call to snd_play_3d() to allow for arbitrary listening position
152  * 
153  * 2     9/17/97 5:12p John
154  * Restructured collision routines.  Probably broke a lot of stuff.
155  * 
156  * 1     9/17/97 2:14p John
157  * Initial revision
158  *
159  * $NoKeywords: $
160  */
161
162
163 #include "objcollide.h"
164 #include "ship.h"
165 #include "debris.h"
166 #include "player.h"
167 #include "shiphit.h"
168 #include "timer.h"
169 #include "asteroid.h"
170 #include "hud.h"
171
172 void calculate_ship_ship_collision_physics(collision_info_struct *ship_ship_hit_info);
173
174 /*
175 extern int Framecount;
176 int Debris_ship_count = 0;
177 */
178
179 // Checks debris-ship collisions.  pair->a is debris and pair->b is ship.
180 // Returns 1 if all future collisions between these can be ignored
181 int collide_debris_ship( obj_pair * pair )
182 {
183         float dist;
184         object *pdebris = pair->a;
185         object *pship = pair->b;
186
187                 // Don't check collisions for warping out player
188         if ( Player->control_mode != PCM_NORMAL )       {
189                 if ( pship == Player_obj )
190                         return 0;
191         }
192
193         Assert( pdebris->type == OBJ_DEBRIS );
194         Assert( pship->type == OBJ_SHIP );
195
196 /*      Debris_ship_count++;
197         if (Debris_ship_count % 100 == 0)
198                 nprintf(("AI", "Done %i debris:ship checks in %i frames = %.2f checks/frame\n", Debris_ship_count, Framecount, (float) Debris_ship_count/Framecount));
199 */
200         dist = vm_vec_dist( &pdebris->pos, &pship->pos );
201         if ( dist < pdebris->radius + pship->radius )   {
202                 int hit;
203                 vector  hitpos;
204                 // create and initialize ship_ship_hit_info struct
205                 collision_info_struct debris_hit_info;
206                 memset( &debris_hit_info, -1, sizeof(collision_info_struct) );
207
208                 if ( pdebris->phys_info.mass > pship->phys_info.mass ) {
209                         debris_hit_info.heavy = pdebris;
210                         debris_hit_info.light = pship;
211                 } else {
212                         debris_hit_info.heavy = pship;
213                         debris_hit_info.light = pdebris;
214                 }
215
216                 hit = debris_check_collision(pdebris, pship, &hitpos, &debris_hit_info );
217                 if ( hit ) {
218                         float           ship_damage;    
219                         float           debris_damage;
220
221                         // do collision physics
222                         calculate_ship_ship_collision_physics( &debris_hit_info );
223
224                         if ( debris_hit_info.impulse < 0.5f )
225                                 return 0;
226
227                         // calculate ship damage
228                         ship_damage = 0.005f * debris_hit_info.impulse; //      Cut collision-based damage in half.
229                         //      Decrease heavy damage by 2x.
230                         if (ship_damage > 5.0f)
231                                 ship_damage = 5.0f + (ship_damage - 5.0f)/2.0f;
232
233                         // calculate debris damage and set debris damage to greater or debris and ship
234                         // debris damage is needed since we can really whack some small debris with afterburner and not do
235                         // significant damage to ship but the debris goes off faster than afterburner speed.
236                         debris_damage = debris_hit_info.impulse/pdebris->phys_info.mass;        // ie, delta velocity of debris
237                         debris_damage = (debris_damage > ship_damage) ? debris_damage : ship_damage;
238
239                         // supercaps cap damage at 10-20% max hull ship damage
240                         if (Ship_info[Ships[pship->instance].ship_info_index].flags & SIF_SUPERCAP) {
241                                 float cap_percent_damage = frand_range(0.1f, 0.2f);
242                                 ship_damage = min(ship_damage, cap_percent_damage * Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength);
243                         }
244
245                         // apply damage to debris
246                         debris_hit( pdebris, pship, &hitpos, debris_damage);            // speed => damage
247                         int quadrant_num, apply_ship_damage;
248
249                         // apply damage to ship unless 1) debris is from ship
250                         // apply_ship_damage = !((pship->signature == pdebris->parent_sig) && ship_is_beginning_warpout_speedup(pship));
251                         apply_ship_damage = !(pship->signature == pdebris->parent_sig);
252
253                         if ( debris_hit_info.heavy == pship ) {
254                                 quadrant_num = get_ship_quadrant_from_global(&hitpos, pship);
255                                 if ((pship->flags & OF_NO_SHIELDS) || !ship_is_shield_up(pship, quadrant_num) ) {
256                                         quadrant_num = -1;
257                                 }
258                                 if (apply_ship_damage) {
259                                         ship_apply_local_damage(debris_hit_info.heavy, debris_hit_info.light, &hitpos, ship_damage, quadrant_num, CREATE_SPARKS, debris_hit_info.submodel_num);
260                                 }
261                         } else {
262                                 // don't draw sparks using sphere hit position
263                                 if (apply_ship_damage) {
264                                         ship_apply_local_damage(debris_hit_info.light, debris_hit_info.heavy, &hitpos, ship_damage, MISS_SHIELDS, NO_SPARKS);
265                                 }
266                         }
267
268                         // maybe print Collision on HUD
269                         if ( pship == Player_obj ) {                                    
270                                 hud_start_text_flash(XSTR("Collision", 1431), 2000);
271                         }
272
273                         collide_ship_ship_do_sound(&hitpos, pship, pdebris, pship==Player_obj);
274
275                         return 0;
276                 }
277         } else {        //      Bounding spheres don't intersect, set timestamp for next collision check.
278                 float   ship_max_speed, debris_speed;
279                 float   time;
280                 ship *shipp;
281
282                 shipp = &Ships[pship->instance];
283
284                 if (ship_is_beginning_warpout_speedup(pship)) {
285                         ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
286                 } else {
287                         ship_max_speed = ship_get_max_speed(shipp);
288                 }
289                 ship_max_speed = max(ship_max_speed, 10.0f);
290                 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.z);
291
292                 debris_speed = pdebris->phys_info.speed;
293
294                 time = 1000.0f * (dist - pship->radius - pdebris->radius - 10.0f) / (ship_max_speed + debris_speed);            // 10.0f is a safety factor
295                 time -= 200.0f;         // allow one frame slow frame at ~5 fps
296
297                 if (time > 100) {
298                         //nprintf(("AI", "Ship %s debris #%i delay time = %.1f seconds\n", Ships[pship->instance].ship_name, pdebris-Objects, time/1000.0f));
299                         pair->next_check_time = timestamp( fl2i(time) );
300                 } else {
301                         pair->next_check_time = timestamp(0);   // check next time
302                 }
303         }
304
305         return 0;
306 }
307
308 // Checks asteroid-ship collisions.  pair->a is asteroid and pair->b is ship.
309 // Returns 1 if all future collisions between these can be ignored
310 int collide_asteroid_ship( obj_pair * pair )
311 {
312 #ifndef FS2_DEMO
313
314         if (!Asteroids_enabled)
315                 return 0;
316
317         float           dist;
318         object  *pasteroid = pair->a;
319         object  *pship = pair->b;
320
321                 // Don't check collisions for warping out player
322         if ( Player->control_mode != PCM_NORMAL )       {
323                 if ( pship == Player_obj ) return 0;
324         }
325
326         if (pasteroid->hull_strength < 0.0f)
327                 return 0;
328
329         Assert( pasteroid->type == OBJ_ASTEROID );
330         Assert( pship->type == OBJ_SHIP );
331
332         dist = vm_vec_dist( &pasteroid->pos, &pship->pos );
333
334         if ( dist < pasteroid->radius + pship->radius ) {
335                 int hit;
336                 vector  hitpos;
337                 // create and initialize ship_ship_hit_info struct
338                 collision_info_struct asteroid_hit_info;
339                 memset( &asteroid_hit_info, -1, sizeof(collision_info_struct) );
340
341                 if ( pasteroid->phys_info.mass > pship->phys_info.mass ) {
342                         asteroid_hit_info.heavy = pasteroid;
343                         asteroid_hit_info.light = pship;
344                 } else {
345                         asteroid_hit_info.heavy = pship;
346                         asteroid_hit_info.light = pasteroid;
347                 }
348
349                 hit = asteroid_check_collision(pasteroid, pship, &hitpos, &asteroid_hit_info );
350                 if ( hit ) {
351                         float           ship_damage;    
352                         float           asteroid_damage;
353
354                         vector asteroid_vel = pasteroid->phys_info.vel;
355
356                         // do collision physics
357                         calculate_ship_ship_collision_physics( &asteroid_hit_info );
358
359                         if ( asteroid_hit_info.impulse < 0.5f )
360                                 return 0;
361
362                         // limit damage from impulse by making max impulse (for damage) 2*m*v_max_relative
363                         float max_ship_impulse = (2.0f*pship->phys_info.max_vel.z+vm_vec_mag_quick(&asteroid_vel)) * 
364                                 (pship->phys_info.mass*pasteroid->phys_info.mass) / (pship->phys_info.mass + pasteroid->phys_info.mass);
365
366                         if (asteroid_hit_info.impulse > max_ship_impulse) {
367                                 ship_damage = 0.001f * max_ship_impulse;
368                         } else {
369                                 ship_damage = 0.001f * asteroid_hit_info.impulse;       //      Cut collision-based damage in half.
370                         }
371
372                         //      Decrease heavy damage by 2x.
373                         if (ship_damage > 5.0f)
374                                 ship_damage = 5.0f + (ship_damage - 5.0f)/2.0f;
375
376                         if ((ship_damage > 500.0f) && (ship_damage > Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength/8.0f)) {
377                                 ship_damage = Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength/8.0f;
378                                 nprintf(("AI", "Pinning damage to %s from asteroid at %7.3f (%7.3f percent)\n", Ships[pship->instance].ship_name, ship_damage, 100.0f * ship_damage/Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength));
379                         }
380
381                         //      Decrease damage during warp out because it's damn annoying when your escoree dies during warp out.
382                         if (Ai_info[Ships[pship->instance].ai_index].mode == AIM_WARP_OUT)
383                                 ship_damage /= 3.0f;
384
385                         //nprintf(("AI", "Asteroid damage on %s = %7.3f (%6.2f percent)\n", Ships[pship->instance].ship_name, ship_damage, 100.0f * ship_damage/Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength));
386
387                         // calculate asteroid damage and set asteroid damage to greater or asteroid and ship
388                         // asteroid damage is needed since we can really whack some small asteroid with afterburner and not do
389                         // significant damage to ship but the asteroid goes off faster than afterburner speed.
390                         asteroid_damage = asteroid_hit_info.impulse/pasteroid->phys_info.mass;  // ie, delta velocity of asteroid
391                         asteroid_damage = (asteroid_damage > ship_damage) ? asteroid_damage : ship_damage;
392
393                         // apply damage to asteroid
394                         asteroid_hit( pasteroid, pship, &hitpos, asteroid_damage);              // speed => damage
395
396                         //extern fix Missiontime;
397
398                         int quadrant_num;
399                         if ( asteroid_hit_info.heavy == pship ) {
400                                 quadrant_num = get_ship_quadrant_from_global(&hitpos, pship);
401                                 if ((pship->flags & OF_NO_SHIELDS) || !ship_is_shield_up(pship, quadrant_num) ) {
402                                         quadrant_num = -1;
403                                 }
404                                 ship_apply_local_damage(asteroid_hit_info.heavy, asteroid_hit_info.light, &hitpos, ship_damage, quadrant_num, CREATE_SPARKS, asteroid_hit_info.submodel_num);
405                                 //if (asteroid_hit_info.heavy->type == OBJ_SHIP) {
406                                 //      nprintf(("AI", "Time = %7.3f, asteroid #%i applying %7.3f damage to ship %s\n", f2fl(Missiontime), pasteroid-Objects, ship_damage, Ships[asteroid_hit_info.heavy->instance].ship_name));
407                                 //}
408                         } else {
409                                 // dont draw sparks (using sphere hitpos)
410                                 ship_apply_local_damage(asteroid_hit_info.light, asteroid_hit_info.heavy, &hitpos, ship_damage, MISS_SHIELDS, NO_SPARKS);
411                                 //if (asteroid_hit_info.light->type == OBJ_SHIP) {
412                                 //      nprintf(("AI", "Time = %7.3f, asteroid #%i applying %7.3f damage to ship %s\n", f2fl(Missiontime), pasteroid-Objects, ship_damage, Ships[asteroid_hit_info.light->instance].ship_name));
413                                 //}
414                         }
415
416                         // maybe print Collision on HUD
417                         if ( pship == Player_obj ) {                                    
418                                 hud_start_text_flash(XSTR("Collision", 1431), 2000);
419                         }
420
421                         collide_ship_ship_do_sound(&hitpos, pship, pasteroid, pship==Player_obj);
422
423                         return 0;
424                 }
425
426                 return 0;
427         } else {
428                 // estimate earliest time at which pair can hit
429                 float asteroid_max_speed, ship_max_speed, time;
430                 ship *shipp = &Ships[pship->instance];
431
432                 asteroid_max_speed = vm_vec_mag(&pasteroid->phys_info.vel);             // Asteroid... vel gets reset, not max vel.z
433                 asteroid_max_speed = max(asteroid_max_speed, 10.0f);
434
435                 if (ship_is_beginning_warpout_speedup(pship)) {
436                         ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
437                 } else {
438                         ship_max_speed = ship_get_max_speed(shipp);
439                 }
440                 ship_max_speed = max(ship_max_speed, 10.0f);
441                 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.z);
442
443
444                 time = 1000.0f * (dist - pship->radius - pasteroid->radius - 10.0f) / (asteroid_max_speed + ship_max_speed);            // 10.0f is a safety factor
445                 time -= 200.0f;         // allow one frame slow frame at ~5 fps
446
447                 if (time > 100) {
448                         pair->next_check_time = timestamp( fl2i(time) );
449                 } else {
450                         pair->next_check_time = timestamp(0);   // check next time
451                 }
452                 return 0;
453         }
454 #else
455         return 0;       // no asteroids in demo version
456 #endif
457 }
458