]> icculus.org git repositories - taylor/freespace2.git/blob - src/object/collidedebrisship.cpp
ryan's struct patch for gcc 2.95
[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.4  2002/06/17 06:33:10  relnev
19  * ryan's struct patch for gcc 2.95
20  *
21  * Revision 1.3  2002/06/09 04:41:24  relnev
22  * added copyright header
23  *
24  * Revision 1.2  2002/05/03 13:34:33  theoddone33
25  * More stuff compiles
26  *
27  * Revision 1.1.1.1  2002/05/03 03:28:10  root
28  * Initial import.
29  *
30  * 
31  * 12    9/14/99 2:59a Andsager
32  * Limit amount of damage done by debris colliding with ship for SUPERCAP
33  * 
34  * 11    8/31/99 11:43p Andsager
35  * No ship:debris damage to ship if debris' parent is ship itself
36  * 
37  * 10    8/10/99 5:49p Andsager
38  * Fix bug - no damage collision against debris.
39  * 
40  * 9     8/09/99 4:45p Andsager
41  * Add HUD "collision " waring when colliding with asteroids and debris.
42  * 
43  * 8     7/15/99 9:20a Andsager
44  * FS2_DEMO initial checkin
45  * 
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.
49  * 
50  * 6     1/12/99 10:24a Andsager
51  * Fix asteroid-ship collision bug, with negative collision
52  * 
53  * 5     10/23/98 1:11p Andsager
54  * Make ship sparks emit correctly from rotating structures.
55  * 
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.
60  * 
61  * 3     10/16/98 1:22p Andsager
62  * clean up header files
63  * 
64  * 2     10/07/98 10:53a Dave
65  * Initial checkin.
66  * 
67  * 1     10/07/98 10:50a Dave
68  * 
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
72  * parameter no_spark.
73  * 
74  * 26    4/02/98 6:29p Lawrance
75  * compile out asteroid references for demo
76  * 
77  * 25    4/02/98 5:10p Mike
78  * Decrease damage yet more (by 3x) when an asteroid collides with a ship
79  * during warp out.
80  * 
81  * 24    4/02/98 4:25p Andsager
82  * fix bug calculating max_ship_impulse
83  * 
84  * 23    3/30/98 10:35a Andsager
85  * Attempt to optimize ship:asteroid collisions.  Maybe return if time.
86  * 
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.
90  * 
91  * 21    3/25/98 2:15p Andsager
92  * 
93  * 20    3/25/98 1:36p Mike
94  * Balancing of sm1-06a, Galatea mission.
95  * 
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.
99  * 
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.
103  * 
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.
107  * 
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.
111  * 
112  * 15    3/02/98 2:58p Mike
113  * Make "asteroids" in debug console turn asteroids on/off.
114  * 
115  * 14    2/19/98 12:46a Lawrance
116  * Further work on asteroids.
117  * 
118  * 13    2/07/98 2:14p Mike
119  * Improve asteroid splitting.  Add ship:asteroid collisions.  Timestamp
120  * ship:debris collisions.
121  * 
122  * 12    2/06/98 7:29p John
123  * Added code to cull out some future asteroid-ship collisions.
124  * 
125  * 11    2/05/98 12:51a Mike
126  * Early asteroid stuff.
127  * 
128  * 10    2/04/98 8:57p Lawrance
129  * remove unused variable
130  * 
131  * 9     2/04/98 6:08p Lawrance
132  * Add a light collision sound, overlay a shield collide sound if
133  * applicable.
134  * 
135  * 8     1/05/98 9:07p Andsager
136  * Changed ship_shipor_debris_hit_info struct to more meaninful names
137  * 
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
141  * sphere.
142  * 
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
146  * 
147  * 5     11/03/97 11:08p Lawrance
148  * play correct collision sounds.
149  * 
150  * 4     10/27/97 8:35a John
151  * code for new player warpout sequence
152  * 
153  * 3     10/01/97 5:55p Lawrance
154  * change call to snd_play_3d() to allow for arbitrary listening position
155  * 
156  * 2     9/17/97 5:12p John
157  * Restructured collision routines.  Probably broke a lot of stuff.
158  * 
159  * 1     9/17/97 2:14p John
160  * Initial revision
161  *
162  * $NoKeywords: $
163  */
164
165
166 #include "objcollide.h"
167 #include "ship.h"
168 #include "debris.h"
169 #include "player.h"
170 #include "shiphit.h"
171 #include "timer.h"
172 #include "asteroid.h"
173 #include "hud.h"
174
175 void calculate_ship_ship_collision_physics(collision_info_struct *ship_ship_hit_info);
176
177 /*
178 extern int Framecount;
179 int Debris_ship_count = 0;
180 */
181
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 )
185 {
186         float dist;
187         object *pdebris = pair->a;
188         object *pship = pair->b;
189
190                 // Don't check collisions for warping out player
191         if ( Player->control_mode != PCM_NORMAL )       {
192                 if ( pship == Player_obj )
193                         return 0;
194         }
195
196         Assert( pdebris->type == OBJ_DEBRIS );
197         Assert( pship->type == OBJ_SHIP );
198
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));
202 */
203         dist = vm_vec_dist( &pdebris->pos, &pship->pos );
204         if ( dist < pdebris->radius + pship->radius )   {
205                 int hit;
206                 vector  hitpos;
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) );
210
211                 if ( pdebris->phys_info.mass > pship->phys_info.mass ) {
212                         debris_hit_info.heavy = pdebris;
213                         debris_hit_info.light = pship;
214                 } else {
215                         debris_hit_info.heavy = pship;
216                         debris_hit_info.light = pdebris;
217                 }
218
219                 hit = debris_check_collision(pdebris, pship, &hitpos, &debris_hit_info );
220                 if ( hit ) {
221                         float           ship_damage;    
222                         float           debris_damage;
223
224                         // do collision physics
225                         calculate_ship_ship_collision_physics( &debris_hit_info );
226
227                         if ( debris_hit_info.impulse < 0.5f )
228                                 return 0;
229
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;
235
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;
241
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);
246                         }
247
248                         // apply damage to debris
249                         debris_hit( pdebris, pship, &hitpos, debris_damage);            // speed => damage
250                         int quadrant_num, apply_ship_damage;
251
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);
255
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) ) {
259                                         quadrant_num = -1;
260                                 }
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);
263                                 }
264                         } else {
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);
268                                 }
269                         }
270
271                         // maybe print Collision on HUD
272                         if ( pship == Player_obj ) {                                    
273                                 hud_start_text_flash(XSTR("Collision", 1431), 2000);
274                         }
275
276                         collide_ship_ship_do_sound(&hitpos, pship, pdebris, pship==Player_obj);
277
278                         return 0;
279                 }
280         } else {        //      Bounding spheres don't intersect, set timestamp for next collision check.
281                 float   ship_max_speed, debris_speed;
282                 float   time;
283                 ship *shipp;
284
285                 shipp = &Ships[pship->instance];
286
287                 if (ship_is_beginning_warpout_speedup(pship)) {
288                         ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
289                 } else {
290                         ship_max_speed = ship_get_max_speed(shipp);
291                 }
292                 ship_max_speed = max(ship_max_speed, 10.0f);
293                 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.xyz.z);
294
295                 debris_speed = pdebris->phys_info.speed;
296
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
299
300                 if (time > 100) {
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) );
303                 } else {
304                         pair->next_check_time = timestamp(0);   // check next time
305                 }
306         }
307
308         return 0;
309 }
310
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 )
314 {
315 #ifndef FS2_DEMO
316
317         if (!Asteroids_enabled)
318                 return 0;
319
320         float           dist;
321         object  *pasteroid = pair->a;
322         object  *pship = pair->b;
323
324                 // Don't check collisions for warping out player
325         if ( Player->control_mode != PCM_NORMAL )       {
326                 if ( pship == Player_obj ) return 0;
327         }
328
329         if (pasteroid->hull_strength < 0.0f)
330                 return 0;
331
332         Assert( pasteroid->type == OBJ_ASTEROID );
333         Assert( pship->type == OBJ_SHIP );
334
335         dist = vm_vec_dist( &pasteroid->pos, &pship->pos );
336
337         if ( dist < pasteroid->radius + pship->radius ) {
338                 int hit;
339                 vector  hitpos;
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) );
343
344                 if ( pasteroid->phys_info.mass > pship->phys_info.mass ) {
345                         asteroid_hit_info.heavy = pasteroid;
346                         asteroid_hit_info.light = pship;
347                 } else {
348                         asteroid_hit_info.heavy = pship;
349                         asteroid_hit_info.light = pasteroid;
350                 }
351
352                 hit = asteroid_check_collision(pasteroid, pship, &hitpos, &asteroid_hit_info );
353                 if ( hit ) {
354                         float           ship_damage;    
355                         float           asteroid_damage;
356
357                         vector asteroid_vel = pasteroid->phys_info.vel;
358
359                         // do collision physics
360                         calculate_ship_ship_collision_physics( &asteroid_hit_info );
361
362                         if ( asteroid_hit_info.impulse < 0.5f )
363                                 return 0;
364
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);
368
369                         if (asteroid_hit_info.impulse > max_ship_impulse) {
370                                 ship_damage = 0.001f * max_ship_impulse;
371                         } else {
372                                 ship_damage = 0.001f * asteroid_hit_info.impulse;       //      Cut collision-based damage in half.
373                         }
374
375                         //      Decrease heavy damage by 2x.
376                         if (ship_damage > 5.0f)
377                                 ship_damage = 5.0f + (ship_damage - 5.0f)/2.0f;
378
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));
382                         }
383
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)
386                                 ship_damage /= 3.0f;
387
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));
389
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;
395
396                         // apply damage to asteroid
397                         asteroid_hit( pasteroid, pship, &hitpos, asteroid_damage);              // speed => damage
398
399                         //extern fix Missiontime;
400
401                         int quadrant_num;
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) ) {
405                                         quadrant_num = -1;
406                                 }
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));
410                                 //}
411                         } else {
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));
416                                 //}
417                         }
418
419                         // maybe print Collision on HUD
420                         if ( pship == Player_obj ) {                                    
421                                 hud_start_text_flash(XSTR("Collision", 1431), 2000);
422                         }
423
424                         collide_ship_ship_do_sound(&hitpos, pship, pasteroid, pship==Player_obj);
425
426                         return 0;
427                 }
428
429                 return 0;
430         } else {
431                 // estimate earliest time at which pair can hit
432                 float asteroid_max_speed, ship_max_speed, time;
433                 ship *shipp = &Ships[pship->instance];
434
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);
437
438                 if (ship_is_beginning_warpout_speedup(pship)) {
439                         ship_max_speed = max(ship_get_max_speed(shipp), ship_get_warp_speed(pship));
440                 } else {
441                         ship_max_speed = ship_get_max_speed(shipp);
442                 }
443                 ship_max_speed = max(ship_max_speed, 10.0f);
444                 ship_max_speed = max(ship_max_speed, pship->phys_info.vel.z);
445
446
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
449
450                 if (time > 100) {
451                         pair->next_check_time = timestamp( fl2i(time) );
452                 } else {
453                         pair->next_check_time = timestamp(0);   // check next time
454                 }
455                 return 0;
456         }
457 #else
458         return 0;       // no asteroids in demo version
459 #endif
460 }
461