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