2 * Copyright (C) Volition, Inc. 1999. All rights reserved.
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
10 * $Logfile: /Freespace2/code/Object/CollideDebrisShip.cpp $
15 * Routines to detect collisions and do physics, damage, etc for ships and debris
18 * Revision 1.4 2002/06/17 06:33:10 relnev
19 * ryan's struct patch for gcc 2.95
21 * Revision 1.3 2002/06/09 04:41:24 relnev
22 * added copyright header
24 * Revision 1.2 2002/05/03 13:34:33 theoddone33
27 * Revision 1.1.1.1 2002/05/03 03:28:10 root
31 * 12 9/14/99 2:59a Andsager
32 * Limit amount of damage done by debris colliding with ship for SUPERCAP
34 * 11 8/31/99 11:43p Andsager
35 * No ship:debris damage to ship if debris' parent is ship itself
37 * 10 8/10/99 5:49p Andsager
38 * Fix bug - no damage collision against debris.
40 * 9 8/09/99 4:45p Andsager
41 * Add HUD "collision " waring when colliding with asteroids and debris.
43 * 8 7/15/99 9:20a Andsager
44 * FS2_DEMO initial checkin
46 * 7 6/14/99 3:21p Andsager
47 * Allow collisions between ship and its debris. Fix up collision pairs
48 * when large ship is warping out.
50 * 6 1/12/99 10:24a Andsager
51 * Fix asteroid-ship collision bug, with negative collision
53 * 5 10/23/98 1:11p Andsager
54 * Make ship sparks emit correctly from rotating structures.
56 * 4 10/20/98 1:39p Andsager
57 * Make so sparks follow animated ship submodels. Modify
58 * ship_weapon_do_hit_stuff() and ship_apply_local_damage() to add
59 * submodel_num. Add submodel_num to multiplayer hit packet.
61 * 3 10/16/98 1:22p Andsager
62 * clean up header files
64 * 2 10/07/98 10:53a Dave
67 * 1 10/07/98 10:50a Dave
69 * 27 4/24/98 5:35p Andsager
70 * Fix sparks sometimes drawing not on model. If ship is sphere in
71 * collision, don't draw sparks. Modify ship_apply_local_damage() to take
74 * 26 4/02/98 6:29p Lawrance
75 * compile out asteroid references for demo
77 * 25 4/02/98 5:10p Mike
78 * Decrease damage yet more (by 3x) when an asteroid collides with a ship
81 * 24 4/02/98 4:25p Andsager
82 * fix bug calculating max_ship_impulse
84 * 23 3/30/98 10:35a Andsager
85 * Attempt to optimize ship:asteroid collisions. Maybe return if time.
87 * 22 3/25/98 2:37p Andsager
88 * Modify maximum damage done by asteroids, depends on ship's max_vel, not
89 * vel when warping out.
91 * 21 3/25/98 2:15p Andsager
93 * 20 3/25/98 1:36p Mike
94 * Balancing of sm1-06a, Galatea mission.
96 * 19 3/23/98 11:14a Andsager
97 * Modified collide_asteroid_ship to calculate next possible collision
98 * time based on ship afterburner speed.
100 * 18 3/20/98 12:02a Mike
101 * Cap damage done by an asteroid on a huge ship to 1/8 ship strength if a
102 * large ship. If <4000 hull, can do more than 1/8.
104 * 17 3/05/98 3:12p Mike
105 * Fix bugs in asteroid collisions. Throw asteroids at Galataea (needs to
106 * be made general). Add skill level support for asteroid throwing.
108 * 16 3/04/98 11:21p Mike
109 * Less rotation on huge ships in death roll.
110 * Less damage done to huge ships when hit an asteroid at high speed.
112 * 15 3/02/98 2:58p Mike
113 * Make "asteroids" in debug console turn asteroids on/off.
115 * 14 2/19/98 12:46a Lawrance
116 * Further work on asteroids.
118 * 13 2/07/98 2:14p Mike
119 * Improve asteroid splitting. Add ship:asteroid collisions. Timestamp
120 * ship:debris collisions.
122 * 12 2/06/98 7:29p John
123 * Added code to cull out some future asteroid-ship collisions.
125 * 11 2/05/98 12:51a Mike
126 * Early asteroid stuff.
128 * 10 2/04/98 8:57p Lawrance
129 * remove unused variable
131 * 9 2/04/98 6:08p Lawrance
132 * Add a light collision sound, overlay a shield collide sound if
135 * 8 1/05/98 9:07p Andsager
136 * Changed ship_shipor_debris_hit_info struct to more meaninful names
138 * 7 1/02/98 9:08a Andsager
139 * Changed ship:ship and ship:debris collision detection to ignore shields
140 * and collide only against hull. Also, significantly reduced radius of
143 * 6 12/22/97 9:56p Andsager
144 * Implement ship:debris collisions. Generalize and move
145 * ship_ship_or_debris_hit struct from CollideShipShip to ObjCollide.h
147 * 5 11/03/97 11:08p Lawrance
148 * play correct collision sounds.
150 * 4 10/27/97 8:35a John
151 * code for new player warpout sequence
153 * 3 10/01/97 5:55p Lawrance
154 * change call to snd_play_3d() to allow for arbitrary listening position
156 * 2 9/17/97 5:12p John
157 * Restructured collision routines. Probably broke a lot of stuff.
159 * 1 9/17/97 2:14p John
166 #include "objcollide.h"
172 #include "asteroid.h"
175 void calculate_ship_ship_collision_physics(collision_info_struct *ship_ship_hit_info);
178 extern int Framecount;
179 int Debris_ship_count = 0;
182 // Checks debris-ship collisions. pair->a is debris and pair->b is ship.
183 // Returns 1 if all future collisions between these can be ignored
184 int collide_debris_ship( obj_pair * pair )
187 object *pdebris = pair->a;
188 object *pship = pair->b;
190 // Don't check collisions for warping out player
191 if ( Player->control_mode != PCM_NORMAL ) {
192 if ( pship == Player_obj )
196 Assert( pdebris->type == OBJ_DEBRIS );
197 Assert( pship->type == OBJ_SHIP );
199 /* Debris_ship_count++;
200 if (Debris_ship_count % 100 == 0)
201 nprintf(("AI", "Done %i debris:ship checks in %i frames = %.2f checks/frame\n", Debris_ship_count, Framecount, (float) Debris_ship_count/Framecount));
203 dist = vm_vec_dist( &pdebris->pos, &pship->pos );
204 if ( dist < pdebris->radius + pship->radius ) {
207 // create and initialize ship_ship_hit_info struct
208 collision_info_struct debris_hit_info;
209 memset( &debris_hit_info, -1, sizeof(collision_info_struct) );
211 if ( pdebris->phys_info.mass > pship->phys_info.mass ) {
212 debris_hit_info.heavy = pdebris;
213 debris_hit_info.light = pship;
215 debris_hit_info.heavy = pship;
216 debris_hit_info.light = pdebris;
219 hit = debris_check_collision(pdebris, pship, &hitpos, &debris_hit_info );
224 // do collision physics
225 calculate_ship_ship_collision_physics( &debris_hit_info );
227 if ( debris_hit_info.impulse < 0.5f )
230 // calculate ship damage
231 ship_damage = 0.005f * debris_hit_info.impulse; // Cut collision-based damage in half.
232 // Decrease heavy damage by 2x.
233 if (ship_damage > 5.0f)
234 ship_damage = 5.0f + (ship_damage - 5.0f)/2.0f;
236 // calculate debris damage and set debris damage to greater or debris and ship
237 // debris damage is needed since we can really whack some small debris with afterburner and not do
238 // significant damage to ship but the debris goes off faster than afterburner speed.
239 debris_damage = debris_hit_info.impulse/pdebris->phys_info.mass; // ie, delta velocity of debris
240 debris_damage = (debris_damage > ship_damage) ? debris_damage : ship_damage;
242 // supercaps cap damage at 10-20% max hull ship damage
243 if (Ship_info[Ships[pship->instance].ship_info_index].flags & SIF_SUPERCAP) {
244 float cap_percent_damage = frand_range(0.1f, 0.2f);
245 ship_damage = min(ship_damage, cap_percent_damage * Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength);
248 // apply damage to debris
249 debris_hit( pdebris, pship, &hitpos, debris_damage); // speed => damage
250 int quadrant_num, apply_ship_damage;
252 // apply damage to ship unless 1) debris is from ship
253 // apply_ship_damage = !((pship->signature == pdebris->parent_sig) && ship_is_beginning_warpout_speedup(pship));
254 apply_ship_damage = !(pship->signature == pdebris->parent_sig);
256 if ( debris_hit_info.heavy == pship ) {
257 quadrant_num = get_ship_quadrant_from_global(&hitpos, pship);
258 if ((pship->flags & OF_NO_SHIELDS) || !ship_is_shield_up(pship, quadrant_num) ) {
261 if (apply_ship_damage) {
262 ship_apply_local_damage(debris_hit_info.heavy, debris_hit_info.light, &hitpos, ship_damage, quadrant_num, CREATE_SPARKS, debris_hit_info.submodel_num);
265 // don't draw sparks using sphere hit position
266 if (apply_ship_damage) {
267 ship_apply_local_damage(debris_hit_info.light, debris_hit_info.heavy, &hitpos, ship_damage, MISS_SHIELDS, NO_SPARKS);
271 // maybe print Collision on HUD
272 if ( pship == Player_obj ) {
273 hud_start_text_flash(XSTR("Collision", 1431), 2000);
276 collide_ship_ship_do_sound(&hitpos, pship, pdebris, pship==Player_obj);
280 } else { // Bounding spheres don't intersect, set timestamp for next collision check.
281 float ship_max_speed, debris_speed;
285 shipp = &Ships[pship->instance];
287 if (ship_is_beginning_warpout_speedup(pship)) {
288 ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
290 ship_max_speed = ship_get_max_speed(shipp);
292 ship_max_speed = max(ship_max_speed, 10.0f);
293 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.xyz.z);
295 debris_speed = pdebris->phys_info.speed;
297 time = 1000.0f * (dist - pship->radius - pdebris->radius - 10.0f) / (ship_max_speed + debris_speed); // 10.0f is a safety factor
298 time -= 200.0f; // allow one frame slow frame at ~5 fps
301 //nprintf(("AI", "Ship %s debris #%i delay time = %.1f seconds\n", Ships[pship->instance].ship_name, pdebris-Objects, time/1000.0f));
302 pair->next_check_time = timestamp( fl2i(time) );
304 pair->next_check_time = timestamp(0); // check next time
311 // Checks asteroid-ship collisions. pair->a is asteroid and pair->b is ship.
312 // Returns 1 if all future collisions between these can be ignored
313 int collide_asteroid_ship( obj_pair * pair )
317 if (!Asteroids_enabled)
321 object *pasteroid = pair->a;
322 object *pship = pair->b;
324 // Don't check collisions for warping out player
325 if ( Player->control_mode != PCM_NORMAL ) {
326 if ( pship == Player_obj ) return 0;
329 if (pasteroid->hull_strength < 0.0f)
332 Assert( pasteroid->type == OBJ_ASTEROID );
333 Assert( pship->type == OBJ_SHIP );
335 dist = vm_vec_dist( &pasteroid->pos, &pship->pos );
337 if ( dist < pasteroid->radius + pship->radius ) {
340 // create and initialize ship_ship_hit_info struct
341 collision_info_struct asteroid_hit_info;
342 memset( &asteroid_hit_info, -1, sizeof(collision_info_struct) );
344 if ( pasteroid->phys_info.mass > pship->phys_info.mass ) {
345 asteroid_hit_info.heavy = pasteroid;
346 asteroid_hit_info.light = pship;
348 asteroid_hit_info.heavy = pship;
349 asteroid_hit_info.light = pasteroid;
352 hit = asteroid_check_collision(pasteroid, pship, &hitpos, &asteroid_hit_info );
355 float asteroid_damage;
357 vector asteroid_vel = pasteroid->phys_info.vel;
359 // do collision physics
360 calculate_ship_ship_collision_physics( &asteroid_hit_info );
362 if ( asteroid_hit_info.impulse < 0.5f )
365 // limit damage from impulse by making max impulse (for damage) 2*m*v_max_relative
366 float max_ship_impulse = (2.0f*pship->phys_info.max_vel.z+vm_vec_mag_quick(&asteroid_vel)) *
367 (pship->phys_info.mass*pasteroid->phys_info.mass) / (pship->phys_info.mass + pasteroid->phys_info.mass);
369 if (asteroid_hit_info.impulse > max_ship_impulse) {
370 ship_damage = 0.001f * max_ship_impulse;
372 ship_damage = 0.001f * asteroid_hit_info.impulse; // Cut collision-based damage in half.
375 // Decrease heavy damage by 2x.
376 if (ship_damage > 5.0f)
377 ship_damage = 5.0f + (ship_damage - 5.0f)/2.0f;
379 if ((ship_damage > 500.0f) && (ship_damage > Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength/8.0f)) {
380 ship_damage = Ship_info[Ships[pship->instance].ship_info_index].initial_hull_strength/8.0f;
381 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));
384 // Decrease damage during warp out because it's damn annoying when your escoree dies during warp out.
385 if (Ai_info[Ships[pship->instance].ai_index].mode == AIM_WARP_OUT)
388 //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));
390 // calculate asteroid damage and set asteroid damage to greater or asteroid and ship
391 // asteroid damage is needed since we can really whack some small asteroid with afterburner and not do
392 // significant damage to ship but the asteroid goes off faster than afterburner speed.
393 asteroid_damage = asteroid_hit_info.impulse/pasteroid->phys_info.mass; // ie, delta velocity of asteroid
394 asteroid_damage = (asteroid_damage > ship_damage) ? asteroid_damage : ship_damage;
396 // apply damage to asteroid
397 asteroid_hit( pasteroid, pship, &hitpos, asteroid_damage); // speed => damage
399 //extern fix Missiontime;
402 if ( asteroid_hit_info.heavy == pship ) {
403 quadrant_num = get_ship_quadrant_from_global(&hitpos, pship);
404 if ((pship->flags & OF_NO_SHIELDS) || !ship_is_shield_up(pship, quadrant_num) ) {
407 ship_apply_local_damage(asteroid_hit_info.heavy, asteroid_hit_info.light, &hitpos, ship_damage, quadrant_num, CREATE_SPARKS, asteroid_hit_info.submodel_num);
408 //if (asteroid_hit_info.heavy->type == OBJ_SHIP) {
409 // 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));
412 // dont draw sparks (using sphere hitpos)
413 ship_apply_local_damage(asteroid_hit_info.light, asteroid_hit_info.heavy, &hitpos, ship_damage, MISS_SHIELDS, NO_SPARKS);
414 //if (asteroid_hit_info.light->type == OBJ_SHIP) {
415 // 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));
419 // maybe print Collision on HUD
420 if ( pship == Player_obj ) {
421 hud_start_text_flash(XSTR("Collision", 1431), 2000);
424 collide_ship_ship_do_sound(&hitpos, pship, pasteroid, pship==Player_obj);
431 // estimate earliest time at which pair can hit
432 float asteroid_max_speed, ship_max_speed, time;
433 ship *shipp = &Ships[pship->instance];
435 asteroid_max_speed = vm_vec_mag(&pasteroid->phys_info.vel); // Asteroid... vel gets reset, not max vel.z
436 asteroid_max_speed = max(asteroid_max_speed, 10.0f);
438 if (ship_is_beginning_warpout_speedup(pship)) {
439 ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
441 ship_max_speed = ship_get_max_speed(shipp);
443 ship_max_speed = max(ship_max_speed, 10.0f);
444 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.z);
447 time = 1000.0f * (dist - pship->radius - pasteroid->radius - 10.0f) / (asteroid_max_speed + ship_max_speed); // 10.0f is a safety factor
448 time -= 200.0f; // allow one frame slow frame at ~5 fps
451 pair->next_check_time = timestamp( fl2i(time) );
453 pair->next_check_time = timestamp(0); // check next time
458 return 0; // no asteroids in demo version