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