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.3 2002/06/09 04:41:24 relnev
19 * added copyright header
21 * Revision 1.2 2002/05/03 13:34:33 theoddone33
24 * Revision 1.1.1.1 2002/05/03 03:28:10 root
28 * 12 9/14/99 2:59a Andsager
29 * Limit amount of damage done by debris colliding with ship for SUPERCAP
31 * 11 8/31/99 11:43p Andsager
32 * No ship:debris damage to ship if debris' parent is ship itself
34 * 10 8/10/99 5:49p Andsager
35 * Fix bug - no damage collision against debris.
37 * 9 8/09/99 4:45p Andsager
38 * Add HUD "collision " waring when colliding with asteroids and debris.
40 * 8 7/15/99 9:20a Andsager
41 * FS2_DEMO initial checkin
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.
47 * 6 1/12/99 10:24a Andsager
48 * Fix asteroid-ship collision bug, with negative collision
50 * 5 10/23/98 1:11p Andsager
51 * Make ship sparks emit correctly from rotating structures.
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.
58 * 3 10/16/98 1:22p Andsager
59 * clean up header files
61 * 2 10/07/98 10:53a Dave
64 * 1 10/07/98 10:50a Dave
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
71 * 26 4/02/98 6:29p Lawrance
72 * compile out asteroid references for demo
74 * 25 4/02/98 5:10p Mike
75 * Decrease damage yet more (by 3x) when an asteroid collides with a ship
78 * 24 4/02/98 4:25p Andsager
79 * fix bug calculating max_ship_impulse
81 * 23 3/30/98 10:35a Andsager
82 * Attempt to optimize ship:asteroid collisions. Maybe return if time.
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.
88 * 21 3/25/98 2:15p Andsager
90 * 20 3/25/98 1:36p Mike
91 * Balancing of sm1-06a, Galatea mission.
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.
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.
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.
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.
109 * 15 3/02/98 2:58p Mike
110 * Make "asteroids" in debug console turn asteroids on/off.
112 * 14 2/19/98 12:46a Lawrance
113 * Further work on asteroids.
115 * 13 2/07/98 2:14p Mike
116 * Improve asteroid splitting. Add ship:asteroid collisions. Timestamp
117 * ship:debris collisions.
119 * 12 2/06/98 7:29p John
120 * Added code to cull out some future asteroid-ship collisions.
122 * 11 2/05/98 12:51a Mike
123 * Early asteroid stuff.
125 * 10 2/04/98 8:57p Lawrance
126 * remove unused variable
128 * 9 2/04/98 6:08p Lawrance
129 * Add a light collision sound, overlay a shield collide sound if
132 * 8 1/05/98 9:07p Andsager
133 * Changed ship_shipor_debris_hit_info struct to more meaninful names
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
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
144 * 5 11/03/97 11:08p Lawrance
145 * play correct collision sounds.
147 * 4 10/27/97 8:35a John
148 * code for new player warpout sequence
150 * 3 10/01/97 5:55p Lawrance
151 * change call to snd_play_3d() to allow for arbitrary listening position
153 * 2 9/17/97 5:12p John
154 * Restructured collision routines. Probably broke a lot of stuff.
156 * 1 9/17/97 2:14p John
163 #include "objcollide.h"
169 #include "asteroid.h"
172 void calculate_ship_ship_collision_physics(collision_info_struct *ship_ship_hit_info);
175 extern int Framecount;
176 int Debris_ship_count = 0;
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 )
184 object *pdebris = pair->a;
185 object *pship = pair->b;
187 // Don't check collisions for warping out player
188 if ( Player->control_mode != PCM_NORMAL ) {
189 if ( pship == Player_obj )
193 Assert( pdebris->type == OBJ_DEBRIS );
194 Assert( pship->type == OBJ_SHIP );
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));
200 dist = vm_vec_dist( &pdebris->pos, &pship->pos );
201 if ( dist < pdebris->radius + pship->radius ) {
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) );
208 if ( pdebris->phys_info.mass > pship->phys_info.mass ) {
209 debris_hit_info.heavy = pdebris;
210 debris_hit_info.light = pship;
212 debris_hit_info.heavy = pship;
213 debris_hit_info.light = pdebris;
216 hit = debris_check_collision(pdebris, pship, &hitpos, &debris_hit_info );
221 // do collision physics
222 calculate_ship_ship_collision_physics( &debris_hit_info );
224 if ( debris_hit_info.impulse < 0.5f )
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;
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;
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);
245 // apply damage to debris
246 debris_hit( pdebris, pship, &hitpos, debris_damage); // speed => damage
247 int quadrant_num, apply_ship_damage;
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);
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) ) {
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);
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);
268 // maybe print Collision on HUD
269 if ( pship == Player_obj ) {
270 hud_start_text_flash(XSTR("Collision", 1431), 2000);
273 collide_ship_ship_do_sound(&hitpos, pship, pdebris, pship==Player_obj);
277 } else { // Bounding spheres don't intersect, set timestamp for next collision check.
278 float ship_max_speed, debris_speed;
282 shipp = &Ships[pship->instance];
284 if (ship_is_beginning_warpout_speedup(pship)) {
285 ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
287 ship_max_speed = ship_get_max_speed(shipp);
289 ship_max_speed = max(ship_max_speed, 10.0f);
290 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.z);
292 debris_speed = pdebris->phys_info.speed;
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
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) );
301 pair->next_check_time = timestamp(0); // check next time
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 )
314 if (!Asteroids_enabled)
318 object *pasteroid = pair->a;
319 object *pship = pair->b;
321 // Don't check collisions for warping out player
322 if ( Player->control_mode != PCM_NORMAL ) {
323 if ( pship == Player_obj ) return 0;
326 if (pasteroid->hull_strength < 0.0f)
329 Assert( pasteroid->type == OBJ_ASTEROID );
330 Assert( pship->type == OBJ_SHIP );
332 dist = vm_vec_dist( &pasteroid->pos, &pship->pos );
334 if ( dist < pasteroid->radius + pship->radius ) {
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) );
341 if ( pasteroid->phys_info.mass > pship->phys_info.mass ) {
342 asteroid_hit_info.heavy = pasteroid;
343 asteroid_hit_info.light = pship;
345 asteroid_hit_info.heavy = pship;
346 asteroid_hit_info.light = pasteroid;
349 hit = asteroid_check_collision(pasteroid, pship, &hitpos, &asteroid_hit_info );
352 float asteroid_damage;
354 vector asteroid_vel = pasteroid->phys_info.vel;
356 // do collision physics
357 calculate_ship_ship_collision_physics( &asteroid_hit_info );
359 if ( asteroid_hit_info.impulse < 0.5f )
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);
366 if (asteroid_hit_info.impulse > max_ship_impulse) {
367 ship_damage = 0.001f * max_ship_impulse;
369 ship_damage = 0.001f * asteroid_hit_info.impulse; // Cut collision-based damage in half.
372 // Decrease heavy damage by 2x.
373 if (ship_damage > 5.0f)
374 ship_damage = 5.0f + (ship_damage - 5.0f)/2.0f;
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));
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)
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));
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;
393 // apply damage to asteroid
394 asteroid_hit( pasteroid, pship, &hitpos, asteroid_damage); // speed => damage
396 //extern fix Missiontime;
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) ) {
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));
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));
416 // maybe print Collision on HUD
417 if ( pship == Player_obj ) {
418 hud_start_text_flash(XSTR("Collision", 1431), 2000);
421 collide_ship_ship_do_sound(&hitpos, pship, pasteroid, pship==Player_obj);
428 // estimate earliest time at which pair can hit
429 float asteroid_max_speed, ship_max_speed, time;
430 ship *shipp = &Ships[pship->instance];
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);
435 if (ship_is_beginning_warpout_speedup(pship)) {
436 ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
438 ship_max_speed = ship_get_max_speed(shipp);
440 ship_max_speed = max(ship_max_speed, 10.0f);
441 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.z);
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
448 pair->next_check_time = timestamp( fl2i(time) );
450 pair->next_check_time = timestamp(0); // check next time
455 return 0; // no asteroids in demo version