]> icculus.org git repositories - btb/d2x.git/blob - main/fireball.c
use the orientation parameter of g3_draw_bitmap
[btb/d2x.git] / main / fireball.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Code for rendering & otherwise dealing with explosions
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <conf.h>
22 #endif
23
24 #include <stdlib.h>
25 #include <stdio.h>
26 #include <string.h>
27
28 #include "dxxerror.h"
29 #include "maths.h"
30 #include "gr.h"
31 #include "3d.h"
32 #include "inferno.h"
33 #include "mono.h"
34 #include "timer.h"
35
36
37 #define EXPLOSION_SCALE (F1_0*5/2)              //explosion is the obj size times this 
38
39 fix     Flash_effect=0;
40 //--unused-- ubyte      Frame_processed[MAX_OBJECTS];
41
42 int     PK1=1, PK2=8;
43
44 object *object_create_explosion_sub(object *objp, short segnum, vms_vector * position, fix size, int vclip_type, fix maxdamage, fix maxdistance, fix maxforce, int parent )
45 {
46         int objnum;
47         object *obj;
48
49         objnum = obj_create( OBJ_FIREBALL,vclip_type,segnum,position,&vmd_identity_matrix,size,
50                                         CT_EXPLOSION,MT_NONE,RT_FIREBALL);
51
52         if (objnum < 0 ) {
53                 mprintf((1, "Can't create object in object_create_explosion_sub.\n"));
54                 return NULL;
55         }
56
57         obj = &Objects[objnum];
58
59         //mprintf( 0, "Fireball created at %d, %d, %d\n", obj->pos.x, obj->pos.y, obj->pos.z );
60
61         //now set explosion-specific data
62
63         obj->lifeleft = Vclip[vclip_type ].play_time;
64         obj->ctype.expl_info.spawn_time = -1;
65         obj->ctype.expl_info.delete_objnum = -1;
66         obj->ctype.expl_info.delete_time = -1;
67
68         if (maxdamage > 0) {
69                 fix dist, force;
70                 vms_vector pos_hit, vforce;
71                 fix damage;
72                 int i;
73                 object * obj0p = &Objects[0];
74                                          
75                 // -- now legal for badass explosions on a wall. Assert(objp != NULL);
76
77                 for (i=0; i<=Highest_object_index; i++ )        {
78                         //      Weapons used to be affected by badass explosions, but this introduces serious problems.
79                         //      When a smart bomb blows up, if one of its children goes right towards a nearby wall, it will
80                         //      blow up, blowing up all the children.  So I remove it.  MK, 09/11/94
81                         if ( (obj0p!=objp) && !(obj0p->flags&OF_SHOULD_BE_DEAD) && ((obj0p->type==OBJ_WEAPON && (obj0p->id==PROXIMITY_ID || obj0p->id==SUPERPROX_ID || obj0p->id==PMINE_ID)) || (obj0p->type == OBJ_CNTRLCEN) || (obj0p->type==OBJ_PLAYER) || ((obj0p->type==OBJ_ROBOT) && ((Objects[parent].type != OBJ_ROBOT) || (Objects[parent].id != obj0p->id))))) {
82                                 dist = vm_vec_dist_quick( &obj0p->pos, &obj->pos );
83                                 // Make damage be from 'maxdamage' to 0.0, where 0.0 is 'maxdistance' away;
84                                 if ( dist < maxdistance ) {
85                                         if (object_to_object_visibility(obj, obj0p, FQ_TRANSWALL)) {
86
87                                                 damage = maxdamage - fixmuldiv( dist, maxdamage, maxdistance );
88                                                 force = maxforce - fixmuldiv( dist, maxforce, maxdistance );
89
90                                                 // Find the force vector on the object
91                                                 vm_vec_normalized_dir_quick( &vforce, &obj0p->pos, &obj->pos );
92                                                 vm_vec_scale(&vforce, force );
93         
94                                                 // Find where the point of impact is... ( pos_hit )
95                                                 vm_vec_scale(vm_vec_sub(&pos_hit, &obj->pos, &obj0p->pos), fixdiv(obj0p->size, obj0p->size + dist));
96         
97                                                 switch ( obj0p->type )  {
98                                                         case OBJ_WEAPON:
99                                                                 phys_apply_force(obj0p,&vforce);
100
101                                                                 if (obj0p->id == PROXIMITY_ID || obj0p->id == SUPERPROX_ID) {           //prox bombs have chance of blowing up
102                                                                         if (fixmul(dist,force) > i2f(8000)) {
103                                                                                 obj0p->flags |= OF_SHOULD_BE_DEAD;
104                                                                                 explode_badass_weapon(obj0p,&obj0p->pos);
105                                                                         }
106                                                                 }
107                                                                 break;
108
109                                                         case OBJ_ROBOT:
110                                                                 {
111                                                                 phys_apply_force(obj0p,&vforce);
112
113                                                                 //      If not a boss, stun for 2 seconds at 32 force, 1 second at 16 force
114                                                                 if ((objp != NULL) && (!Robot_info[obj0p->id].boss_flag) && (Weapon_info[objp->id].flash)) {
115                                                                         ai_static       *aip = &obj0p->ctype.ai_info;
116                                                                         int                     force_val = f2i(fixdiv(vm_vec_mag_quick(&vforce) * Weapon_info[objp->id].flash, FrameTime)/128) + 2;
117
118                                                                         if (obj->ctype.ai_info.SKIP_AI_COUNT * FrameTime < F1_0) {
119                                                                                 aip->SKIP_AI_COUNT += force_val;
120                                                                                 obj0p->mtype.phys_info.rotthrust.x = ((d_rand() - 16384) * force_val)/16;
121                                                                                 obj0p->mtype.phys_info.rotthrust.y = ((d_rand() - 16384) * force_val)/16;
122                                                                                 obj0p->mtype.phys_info.rotthrust.z = ((d_rand() - 16384) * force_val)/16;
123                                                                                 obj0p->mtype.phys_info.flags |= PF_USES_THRUST;
124
125                                                                                 //@@if (Robot_info[obj0p->id].companion)
126                                                                                 //@@    buddy_message("Daisy, Daisy, Give me...");
127                                                                         } else
128                                                                                 aip->SKIP_AI_COUNT--;
129
130                                                                 }
131
132                                                                 //      When a robot gets whacked by a badass force, he looks towards it because robots tend to get blasted from behind.
133                                                                 {
134                                                                         vms_vector neg_vforce;
135                                                                         neg_vforce.x = vforce.x * -2 * (7 - Difficulty_level)/8;
136                                                                         neg_vforce.y = vforce.y * -2 * (7 - Difficulty_level)/8;
137                                                                         neg_vforce.z = vforce.z * -2 * (7 - Difficulty_level)/8;
138                                                                         phys_apply_rot(obj0p,&neg_vforce);
139                                                                 }
140                                                                 if ( obj0p->shields >= 0 ) {
141                                                                         if (Robot_info[obj0p->id].boss_flag)
142                                                                                 if (Boss_invulnerable_matter[Robot_info[obj0p->id].boss_flag-BOSS_D2])
143                                                                                         damage /= 4;
144
145                                                                         if (apply_damage_to_robot(obj0p, damage, parent))
146                                                                                 if ((objp != NULL) && (parent == Players[Player_num].objnum))
147                                                                                         add_points_to_score(Robot_info[obj0p->id].score_value);
148                                                                 }
149
150                                                                 if ((objp != NULL) && (Robot_info[obj0p->id].companion) && (!Weapon_info[objp->id].flash)) {
151                                                                         int     i, count;
152                                                                         char    ouch_str[6*4 + 2];
153
154                                                                         count = f2i(damage/8);
155                                                                         if (count > 4)
156                                                                                 count = 4;
157                                                                         else if (count <= 0)
158                                                                                 count = 1;
159                                                                         ouch_str[0] = 0;
160                                                                         for (i=0; i<count; i++)
161                                                                                 strcat(ouch_str, "ouch! ");
162
163                                                                         buddy_message(ouch_str);
164                                                                 }
165                                                                 break;
166                                                                 }
167                                                         case OBJ_CNTRLCEN:
168                                                                 if ( obj0p->shields >= 0 ) {
169                                                                         apply_damage_to_controlcen(obj0p, damage, parent );
170                                                                 }
171                                                                 break;
172                                                         case OBJ_PLAYER:        {
173                                                                 object * killer=NULL;
174                                                                 vms_vector      vforce2;
175
176                                                                 //      Hack! Warning! Test code!
177                                                                 if ((objp != NULL) && Weapon_info[objp->id].flash && obj0p->id==Player_num) {
178                                                                         int     fe;
179
180                                                                         fe = min(F1_0*4, force*Weapon_info[objp->id].flash/32); //      For four seconds or less
181
182                                                                         if (objp->ctype.laser_info.parent_signature == ConsoleObject->signature) {
183                                                                                 fe /= 2;
184                                                                                 force /= 2;
185                                                                         }
186                                                                         if (force > F1_0) {
187                                                                                 Flash_effect = fe;
188                                                                                 PALETTE_FLASH_ADD(PK1 + f2i(PK2*force), PK1 + f2i(PK2*force), PK1 + f2i(PK2*force));
189                                                                                 mprintf((0, "force = %7.3f, adding %i\n", f2fl(force), PK1 + f2i(PK2*force)));
190                                                                         }
191                                                                 }
192
193                                                                 if ((objp != NULL) && (Game_mode & GM_MULTI) && (objp->type == OBJ_PLAYER)) {
194                                                                         killer = objp;
195                                                                 }
196                                                                 vforce2 = vforce;
197                                                                 if (parent > -1 ) {
198                                                                         killer = &Objects[parent];
199                                                                         if (killer != ConsoleObject)            // if someone else whacks you, cut force by 2x
200                                                                         {
201                                                                                 vforce2.x /= 2; vforce2.y /= 2; vforce2.z /= 2;
202                                                                         }
203                                                                 }
204                                                                 vforce2.x /= 2; vforce2.y /= 2; vforce2.z /= 2;
205
206                                                                 phys_apply_force(obj0p,&vforce);
207                                                                 phys_apply_rot(obj0p,&vforce2);
208                                                                 if ( obj0p->shields >= 0 )
209                                                                 {
210                                                                         if (Difficulty_level == 0)
211                                                                                 damage /= 4;
212                                                                         apply_damage_to_player(obj0p, killer, damage );
213                                                                 }
214                                                         }
215                                                                 break;
216
217                                                         default:
218                                                                 Int3(); //      Illegal object type
219                                                 }       // end switch
220                                         } else {
221                                                 ; // mprintf((0, "No badass: robot=%2i, dist=%7.3f, maxdistance=%7.3f .\n", i, f2fl(dist), f2fl(maxdistance)));
222                                         }       // end if (object_to_object_visibility...
223                                 }       // end if (dist < maxdistance)
224                         }
225                         obj0p++;
226                 }       // end for
227         }       // end if (maxdamage...
228
229 //      mprintf(0, "\n");
230
231         return obj;
232
233 }
234
235
236 object *object_create_muzzle_flash(short segnum, vms_vector * position, fix size, int vclip_type )
237 {
238         return object_create_explosion_sub(NULL, segnum, position, size, vclip_type, 0, 0, 0, -1 );
239 }
240
241 object *object_create_explosion(short segnum, vms_vector * position, fix size, int vclip_type )
242 {
243         return object_create_explosion_sub(NULL, segnum, position, size, vclip_type, 0, 0, 0, -1 );
244 }
245
246 object *object_create_badass_explosion(object *objp, short segnum, vms_vector * position, fix size, int vclip_type, fix maxdamage, fix maxdistance, fix maxforce, int parent )
247 {
248         object  *rval;
249
250         rval = object_create_explosion_sub(objp, segnum, position, size, vclip_type, maxdamage, maxdistance, maxforce, parent );
251
252         if ((objp != NULL) && (objp->type == OBJ_WEAPON))
253                 create_smart_children(objp, NUM_SMART_CHILDREN);
254
255 // --   if (objp->type == OBJ_ROBOT)
256 // --           if (Robot_info[objp->id].smart_blobs)
257 // --                   create_smart_children(objp, Robot_info[objp->id].smart_blobs);
258
259         return rval;
260 }
261
262 //blows up a badass weapon, creating the badass explosion
263 //return the explosion object
264 object *explode_badass_weapon(object *obj,vms_vector *pos)
265 {
266         weapon_info *wi = &Weapon_info[obj->id];
267
268         Assert(wi->damage_radius);
269
270         if ((obj->id == EARTHSHAKER_ID) || (obj->id == ROBOT_EARTHSHAKER_ID))
271                 smega_rock_stuff();
272
273         digi_link_sound_to_object(SOUND_BADASS_EXPLOSION, OBJECT_NUMBER(obj), 0, F1_0);
274
275         return object_create_badass_explosion(obj, obj->segnum, pos,
276                                               wi->impact_size,
277                                               wi->robot_hit_vclip,
278                                               wi->strength[Difficulty_level],
279                                               wi->damage_radius,wi->strength[Difficulty_level],
280                                               obj->ctype.laser_info.parent_num);
281
282 }
283
284 object *explode_badass_object(object *objp, fix damage, fix distance, fix force)
285 {
286
287         object  *rval;
288
289         rval = object_create_badass_explosion(objp, objp->segnum, &objp->pos, objp->size,
290                                         get_explosion_vclip(objp, 0),
291                                         damage, distance, force,
292                                         OBJECT_NUMBER(objp));
293         if (rval)
294                 digi_link_sound_to_object(SOUND_BADASS_EXPLOSION, OBJECT_NUMBER(rval), 0, F1_0);
295
296         return (rval);
297
298 }
299
300 //blows up the player with a badass explosion
301 //return the explosion object
302 object *explode_badass_player(object *objp)
303 {
304         return explode_badass_object(objp, F1_0*50, F1_0*40, F1_0*150);
305 }
306
307
308 #define DEBRIS_LIFE (f1_0 * 2)          //lifespan in seconds
309
310 object *object_create_debris(object *parent, int subobj_num)
311 {
312         int objnum;
313         object *obj;
314         polymodel *po;
315
316         Assert((parent->type == OBJ_ROBOT) || (parent->type == OBJ_PLAYER)  );
317
318         objnum = obj_create(OBJ_DEBRIS,0,parent->segnum,&parent->pos,
319                                 &parent->orient,Polygon_models[parent->rtype.pobj_info.model_num].submodel_rads[subobj_num],
320                                 CT_DEBRIS,MT_PHYSICS,RT_POLYOBJ);
321
322         if ((objnum < 0 ) && (Highest_object_index >= MAX_OBJECTS-1)) {
323                 mprintf((1, "Can't create object in object_create_debris.\n"));
324 //              Int3(); // this happens often and is normal :-)
325                 return NULL;
326         }
327         if ( objnum < 0 )
328                 return NULL;                            // Not enough debris slots!
329         obj = &Objects[objnum];
330
331         Assert(subobj_num < 32);
332
333         //Set polygon-object-specific data
334
335         obj->rtype.pobj_info.model_num = parent->rtype.pobj_info.model_num;
336         obj->rtype.pobj_info.subobj_flags = 1<<subobj_num;
337         obj->rtype.pobj_info.tmap_override = parent->rtype.pobj_info.tmap_override;
338
339         //Set physics data for this object
340
341         po = &Polygon_models[obj->rtype.pobj_info.model_num];
342
343         obj->mtype.phys_info.velocity.x = D_RAND_MAX/2 - d_rand();
344         obj->mtype.phys_info.velocity.y = D_RAND_MAX/2 - d_rand();
345         obj->mtype.phys_info.velocity.z = D_RAND_MAX/2 - d_rand();
346         vm_vec_normalize_quick(&obj->mtype.phys_info.velocity);
347         vm_vec_scale(&obj->mtype.phys_info.velocity,i2f(10 + (30 * d_rand() / D_RAND_MAX)));
348
349         vm_vec_add2(&obj->mtype.phys_info.velocity,&parent->mtype.phys_info.velocity);
350
351         // -- used to be: Notice, not random! vm_vec_make(&obj->mtype.phys_info.rotvel,10*0x2000/3,10*0x4000/3,10*0x7000/3);
352         vm_vec_make(&obj->mtype.phys_info.rotvel, d_rand() + 0x1000, d_rand()*2 + 0x4000, d_rand()*3 + 0x2000);
353         vm_vec_zero(&obj->mtype.phys_info.rotthrust);
354
355         obj->lifeleft = 3*DEBRIS_LIFE/4 + fixmul(d_rand(), DEBRIS_LIFE);        //      Some randomness, so they don't all go away at the same time.
356
357         obj->mtype.phys_info.mass = fixmuldiv(parent->mtype.phys_info.mass,obj->size,parent->size);
358         obj->mtype.phys_info.drag = 0; //fl2f(0.2);             //parent->mtype.phys_info.drag;
359
360         return obj;
361
362 }
363
364 void draw_fireball(object *obj)
365 {
366         //mprintf( 0, "[Drawing obj %d type %d fireball size %x]\n", OBJECT_NUMBER(obj), obj->id, obj->size );
367
368         if ( obj->lifeleft > 0 )
369                 draw_vclip_object(obj,obj->lifeleft,0, obj->id);
370
371 }
372
373 // --------------------------------------------------------------------------------------------------------------------
374 //      Return true if there is a door here and it is openable
375 //      It is assumed that the player has all keys.
376 int door_is_openable_by_player(segment *segp, int sidenum)
377 {
378         int     wall_num, wall_type;
379
380         wall_num = segp->sides[sidenum].wall_num;
381         wall_type = Walls[wall_num].type;
382
383         if (wall_num == -1)
384                 return 0;                                               //      no wall here.
385
386         //      Can't open locked doors.
387         if (((wall_type == WALL_DOOR) && (Walls[wall_num].flags & WALL_DOOR_LOCKED)) || (wall_type == WALL_CLOSED))
388                 return 0;
389
390         return 1;
391
392 }
393
394 #define QUEUE_SIZE      64
395
396 // --------------------------------------------------------------------------------------------------------------------
397 //      Return a segment %i segments away from initial segment.
398 //      Returns -1 if can't find a segment that distance away.
399 int pick_connected_segment(object *objp, int max_depth)
400 {
401         int             i;
402         int             cur_depth;
403         int             start_seg;
404         int             head, tail;
405         int             seg_queue[QUEUE_SIZE*2];
406         sbyte   visited[MAX_SEGMENTS];
407         sbyte   depth[MAX_SEGMENTS];
408         sbyte   side_rand[MAX_SIDES_PER_SEGMENT];
409
410 //      mprintf((0, "Finding a segment %i segments away from segment %i: ", max_depth, objp->segnum));
411
412         start_seg = objp->segnum;
413         head = 0;
414         tail = 0;
415         seg_queue[head++] = start_seg;
416
417         memset(visited, 0, Highest_segment_index+1);
418         memset(depth, 0, Highest_segment_index+1);
419         cur_depth = 0;
420
421         for (i=0; i<MAX_SIDES_PER_SEGMENT; i++)
422                 side_rand[i] = i;
423
424         //      Now, randomize a bit to start, so we don't always get started in the same direction.
425         for (i=0; i<4; i++) {
426                 int     ind1, temp;
427
428                 ind1 = (d_rand() * MAX_SIDES_PER_SEGMENT) >> 15;
429                 temp = side_rand[ind1];
430                 side_rand[ind1] = side_rand[i];
431                 side_rand[i] = temp;
432         }
433
434
435         while (tail != head) {
436                 int             sidenum, count;
437                 segment *segp;
438                 int             ind1, ind2, temp;
439
440                 if (cur_depth >= max_depth) {
441 //                      mprintf((0, "selected segment %i\n", seg_queue[tail]));
442                         return seg_queue[tail];
443                 }
444
445                 segp = &Segments[seg_queue[tail++]];
446                 tail &= QUEUE_SIZE-1;
447
448                 //      to make random, switch a pair of entries in side_rand.
449                 ind1 = (d_rand() * MAX_SIDES_PER_SEGMENT) >> 15;
450                 ind2 = (d_rand() * MAX_SIDES_PER_SEGMENT) >> 15;
451                 temp = side_rand[ind1];
452                 side_rand[ind1] = side_rand[ind2];
453                 side_rand[ind2] = temp;
454
455                 count = 0;
456                 for (sidenum=ind1; count<MAX_SIDES_PER_SEGMENT; count++) {
457                         int     snrand, wall_num;
458
459                         if (sidenum == MAX_SIDES_PER_SEGMENT)
460                                 sidenum = 0;
461
462                         snrand = side_rand[sidenum];
463                         wall_num = segp->sides[snrand].wall_num;
464                         sidenum++;
465
466                         if (((wall_num == -1) && (segp->children[snrand] > -1)) || door_is_openable_by_player(segp, snrand)) {
467                                 if (visited[segp->children[snrand]] == 0) {
468                                         seg_queue[head++] = segp->children[snrand];
469                                         visited[segp->children[snrand]] = 1;
470                                         depth[segp->children[snrand]] = cur_depth+1;
471                                         head &= QUEUE_SIZE-1;
472                                         if (head > tail) {
473                                                 if (head == tail + QUEUE_SIZE-1)
474                                                         Int3(); //      queue overflow.  Make it bigger!
475                                         } else
476                                                 if (head+QUEUE_SIZE == tail + QUEUE_SIZE-1)
477                                                         Int3(); //      queue overflow.  Make it bigger!
478                                 }
479                         }
480                 }
481                 if ((seg_queue[tail] < 0) || (seg_queue[tail] > Highest_segment_index)) {
482                         // -- Int3();   //      Something bad has happened.  Queue is trashed.  --MK, 12/13/94
483                         return -1;
484                 }
485                 cur_depth = depth[seg_queue[tail]];
486         }
487
488         mprintf((0, "...failed at depth %i, returning -1\n", cur_depth));
489         return -1;
490 }
491
492 #ifdef NETWORK
493 #define BASE_NET_DROP_DEPTH     8
494
495 //      ------------------------------------------------------------------------------------------------------
496 //      Choose segment to drop a powerup in.
497 //      For all active net players, try to create a N segment path from the player.  If possible, return that
498 //      segment.  If not possible, try another player.  After a few tries, use a random segment.
499 //      Don't drop if control center in segment.
500 int choose_drop_segment()
501 {
502         int     pnum = 0;
503         int     segnum = -1;
504         int     cur_drop_depth;
505         int     count;
506         int     player_seg;
507         vms_vector tempv,*player_pos;
508
509         mprintf((0,"choose_drop_segment:"));
510
511         d_srand(timer_get_fixed_seconds());
512
513         cur_drop_depth = BASE_NET_DROP_DEPTH + ((d_rand() * BASE_NET_DROP_DEPTH*2) >> 15);
514
515         player_pos = &Objects[Players[Player_num].objnum].pos;
516         player_seg = Objects[Players[Player_num].objnum].segnum;
517
518         while ((segnum == -1) && (cur_drop_depth > BASE_NET_DROP_DEPTH/2)) {
519                 pnum = (d_rand() * N_players) >> 15;
520                 count = 0;
521                 while ((count < N_players) && ((Players[pnum].connected == 0) || (pnum==Player_num) || ((Game_mode & (GM_TEAM|GM_CAPTURE)) && (get_team(pnum)==get_team(Player_num))))) {
522                         pnum = (pnum+1)%N_players;
523                         count++;
524                 }
525
526                 if (count == N_players) {
527                         //if can't valid non-player person, use the player
528                         pnum = Player_num;
529
530                         //mprintf((1, "Warning: choose_drop_segment: Couldn't find legal drop segment because no connected players.\n"));
531                         //return (d_rand() * Highest_segment_index) >> 15;
532                 }
533
534                 segnum = pick_connected_segment(&Objects[Players[pnum].objnum], cur_drop_depth);
535                 mprintf((0," %d",segnum));
536                 if (segnum == -1)
537                 {
538                         cur_drop_depth--;
539                         continue;
540                 }
541                 if (Segment2s[segnum].special == SEGMENT_IS_CONTROLCEN)
542                         {segnum = -1; mprintf((0,"C")); }
543                 else {  //don't drop in any children of control centers
544                         int i;
545                         for (i=0;i<6;i++) {
546                                 int ch = Segments[segnum].children[i];
547                                 if (IS_CHILD(ch) && Segment2s[ch].special == SEGMENT_IS_CONTROLCEN) {
548                                         mprintf((0,"c"));
549                                         segnum = -1;
550                                         break;
551                                 }
552                         }
553                 }
554
555                 //bail if not far enough from original position
556                 if (segnum != -1) {
557                         compute_segment_center(&tempv, &Segments[segnum]);
558                         if (find_connected_distance(player_pos,player_seg,&tempv,segnum,-1,WID_FLY_FLAG) < i2f(20)*cur_drop_depth) {
559                                 mprintf((0,"D"));
560                                 segnum = -1;
561                         }
562                 }
563
564                 cur_drop_depth--;
565         }
566
567         if (segnum != -1)
568                 mprintf((0," dist=%x\n",find_connected_distance(player_pos,player_seg,&tempv,segnum,-1,WID_FLY_FLAG)));
569
570         if (segnum == -1) {
571                 mprintf((1, "Warning: Unable to find a connected segment.  Picking a random one.\n"));
572                 return (d_rand() * Highest_segment_index) >> 15;
573         } else
574                 return segnum;
575
576 }
577
578 #endif // NETWORK
579 #ifdef NETWORK
580 //      ------------------------------------------------------------------------------------------------------
581 //      Drop cloak powerup if in a network game.
582
583 extern char PowerupsInMine[],MaxPowerupsAllowed[];
584
585 void maybe_drop_net_powerup(int powerup_type)
586 {
587         if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP)) {
588                 int     segnum, objnum;
589                 vms_vector      new_pos;
590
591                 if (Game_mode & GM_NETWORK)
592                         {
593                                 if (PowerupsInMine[powerup_type]>=MaxPowerupsAllowed[powerup_type])
594                                         return;
595                         }
596
597
598                 if (Control_center_destroyed || Endlevel_sequence)
599                         return;
600
601                 segnum = choose_drop_segment();
602 //--old--               segnum = (d_rand() * Highest_segment_index) >> 15;
603 //--old--               Assert((segnum >= 0) && (segnum <= Highest_segment_index));
604 //--old--               if (segnum < 0)
605 //--old--                       segnum = -segnum;
606 //--old--               while (segnum > Highest_segment_index)
607 //--old--                       segnum /= 2;
608
609                 Net_create_loc = 0;
610                 objnum = call_object_create_egg(&Objects[Players[Player_num].objnum], 1, OBJ_POWERUP, powerup_type);
611
612                 if (objnum < 0)
613                         return;
614
615                 pick_random_point_in_seg(&new_pos, segnum);
616
617                 multi_send_create_powerup(powerup_type, segnum, objnum, &new_pos);
618
619                 Objects[objnum].pos = new_pos;
620                 vm_vec_zero(&Objects[objnum].mtype.phys_info.velocity);
621                 obj_relink(objnum, segnum);
622
623                 object_create_explosion(segnum, &new_pos, i2f(5), VCLIP_POWERUP_DISAPPEARANCE );
624 //              mprintf(0, "Creating net powerup in segment %i at %7.3f %7.3f %7.3f\n", segnum, f2fl(new_pos.x), f2fl(new_pos.y), f2fl(new_pos.z));
625         }
626 }
627 #endif
628
629 //      ------------------------------------------------------------------------------------------------------
630 //      Return true if current segment contains some object.
631 int segment_contains_object(int obj_type, int obj_id, int segnum)
632 {
633         int     objnum;
634
635         if (segnum == -1)
636                 return 0;
637
638         objnum = Segments[segnum].objects;
639
640         while (objnum != -1)
641                 if ((Objects[objnum].type == obj_type) && (Objects[objnum].id == obj_id))
642                         return 1;
643                 else
644                         objnum = Objects[objnum].next;
645
646         return 0;
647 }
648
649 //      ------------------------------------------------------------------------------------------------------
650 int object_nearby_aux(int segnum, int object_type, int object_id, int depth)
651 {
652         int     i;
653
654         if (depth == 0)
655                 return 0;
656
657         if (segment_contains_object(object_type, object_id, segnum))
658                 return 1;
659
660         for (i=0; i<MAX_SIDES_PER_SEGMENT; i++) {
661                 int     seg2 = Segments[segnum].children[i];
662
663                 if (seg2 != -1)
664                         if (object_nearby_aux(seg2, object_type, object_id, depth-1))
665                                 return 1;
666         }
667
668         return 0;
669 }
670
671
672 //      ------------------------------------------------------------------------------------------------------
673 //      Return true if some powerup is nearby (within 3 segments).
674 int weapon_nearby(object *objp, int weapon_id)
675 {
676         return object_nearby_aux(objp->segnum, OBJ_POWERUP, weapon_id, 3);
677 }
678
679 //      ------------------------------------------------------------------------------------------------------
680 void maybe_replace_powerup_with_energy(object *del_obj)
681 {
682         int     weapon_index=-1;
683
684         if (del_obj->contains_type != OBJ_POWERUP)
685                 return;
686
687         if (del_obj->contains_id == POW_CLOAK) {
688                 if (weapon_nearby(del_obj, del_obj->contains_id)) {
689                         mprintf((0, "Bashing cloak into nothing because there's one nearby.\n"));
690                         del_obj->contains_count = 0;
691                 }
692                 return;
693         }
694         switch (del_obj->contains_id) {
695                 case POW_VULCAN_WEAPON:                 weapon_index = VULCAN_INDEX;            break;
696                 case POW_GAUSS_WEAPON:                  weapon_index = GAUSS_INDEX;             break;
697                 case POW_SPREADFIRE_WEAPON:     weapon_index = SPREADFIRE_INDEX;        break;
698                 case POW_PLASMA_WEAPON:                 weapon_index = PLASMA_INDEX;            break;
699                 case POW_FUSION_WEAPON:                 weapon_index = FUSION_INDEX;            break;
700
701                 case POW_HELIX_WEAPON:                  weapon_index = HELIX_INDEX;             break;
702                 case POW_PHOENIX_WEAPON:                weapon_index = PHOENIX_INDEX;           break;
703                 case POW_OMEGA_WEAPON:                  weapon_index = OMEGA_INDEX;             break;
704
705         }
706
707         //      Don't drop vulcan ammo if player maxed out.
708         if (((weapon_index == VULCAN_INDEX) || (del_obj->contains_id == POW_VULCAN_AMMO)) && (Players[Player_num].primary_ammo[VULCAN_INDEX] >= VULCAN_AMMO_MAX))
709                 del_obj->contains_count = 0;
710         else if (((weapon_index == GAUSS_INDEX) || (del_obj->contains_id == POW_VULCAN_AMMO)) && (Players[Player_num].primary_ammo[VULCAN_INDEX] >= VULCAN_AMMO_MAX))
711                 del_obj->contains_count = 0;
712         else if (weapon_index != -1) {
713                 if ((player_has_weapon(weapon_index, 0) & HAS_WEAPON_FLAG) || weapon_nearby(del_obj, del_obj->contains_id)) {
714                         if (d_rand() > 16384) {
715                                 del_obj->contains_type = OBJ_POWERUP;
716                                 if (weapon_index == VULCAN_INDEX) {
717                                         del_obj->contains_id = POW_VULCAN_AMMO;
718                                 } else if (weapon_index == GAUSS_INDEX) {
719                                         del_obj->contains_id = POW_VULCAN_AMMO;
720                                 } else {
721                                         del_obj->contains_id = POW_ENERGY;
722                                 }
723                         } else {
724                                 del_obj->contains_type = OBJ_POWERUP;
725                                 del_obj->contains_id = POW_SHIELD_BOOST;
726                         }
727                 }
728         } else if (del_obj->contains_id == POW_QUAD_FIRE)
729                 if ((Players[Player_num].flags & PLAYER_FLAGS_QUAD_LASERS) || weapon_nearby(del_obj, del_obj->contains_id)) {
730                         if (d_rand() > 16384) {
731                                 del_obj->contains_type = OBJ_POWERUP;
732                                 del_obj->contains_id = POW_ENERGY;
733                         } else {
734                                 del_obj->contains_type = OBJ_POWERUP;
735                                 del_obj->contains_id = POW_SHIELD_BOOST;
736                         }
737                 }
738
739         //      If this robot was gated in by the boss and it now contains energy, make it contain nothing,
740         //      else the room gets full of energy.
741         if ( (del_obj->matcen_creator == BOSS_GATE_MATCEN_NUM) && (del_obj->contains_id == POW_ENERGY) && (del_obj->contains_type == OBJ_POWERUP) ) {
742                 mprintf((0, "Converting energy powerup to nothing because robot %i gated in by boss.\n", OBJECT_NUMBER(del_obj)));
743                 del_obj->contains_count = 0;
744         }
745
746         // Change multiplayer extra-lives into invulnerability
747         if ((Game_mode & GM_MULTI) && (del_obj->contains_id == POW_EXTRA_LIFE))
748         {
749                 del_obj->contains_id = POW_INVULNERABILITY;
750         }
751 }
752
753 int drop_powerup(int type, int id, int num, vms_vector *init_vel, vms_vector *pos, int segnum)
754 {
755         int             objnum=-1;
756         object  *obj;
757         vms_vector      new_velocity, new_pos;
758         fix             old_mag;
759    int             count;
760
761         switch (type) {
762                 case OBJ_POWERUP:
763                         for (count=0; count<num; count++) {
764                                 int     rand_scale;
765                                 new_velocity = *init_vel;
766                                 old_mag = vm_vec_mag_quick(init_vel);
767
768                                 //      We want powerups to move more in network mode.
769                                 if ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_ROBOTS)) {
770                                         rand_scale = 4;
771                                         //      extra life powerups are converted to invulnerability in multiplayer, for what is an extra life, anyway?
772                                         if (id == POW_EXTRA_LIFE)
773                                                 id = POW_INVULNERABILITY;
774                                 } else
775                                         rand_scale = 2;
776
777                                 new_velocity.x += fixmul(old_mag+F1_0*32, d_rand()*rand_scale - 16384*rand_scale);
778                                 new_velocity.y += fixmul(old_mag+F1_0*32, d_rand()*rand_scale - 16384*rand_scale);
779                                 new_velocity.z += fixmul(old_mag+F1_0*32, d_rand()*rand_scale - 16384*rand_scale);
780
781                                 // Give keys zero velocity so they can be tracked better in multi
782
783                                 if ((Game_mode & GM_MULTI) && (id >= POW_KEY_BLUE) && (id <= POW_KEY_GOLD))
784                                         vm_vec_zero(&new_velocity);
785
786                                 new_pos = *pos;
787 //                              new_pos.x += (d_rand()-16384)*8;
788 //                              new_pos.y += (d_rand()-16384)*8;
789 //                              new_pos.z += (d_rand()-16384)*8;
790
791 #ifdef NETWORK
792                                 if (Game_mode & GM_MULTI)
793                                 {       
794                                         if (Net_create_loc >= MAX_NET_CREATE_OBJECTS)
795                                         {
796                                                 mprintf( (1, "Not enough slots to drop all powerups!\n" ));
797                                                 return (-1);
798                                         }
799                                         if ((Game_mode & GM_NETWORK) && Network_status == NETSTAT_ENDLEVEL)
800                                          return (-1);
801                                 }
802 #endif
803                                 objnum = obj_create( type, id, segnum, &new_pos, &vmd_identity_matrix, Powerup_info[id].size, CT_POWERUP, MT_PHYSICS, RT_POWERUP);
804
805                                 if (objnum < 0 ) {
806                                         mprintf((1, "Can't create object in object_create_egg.  Aborting.\n"));
807                                         Int3();
808                                         return objnum;
809                                 }
810
811 #ifdef NETWORK
812                                 if (Game_mode & GM_MULTI)
813                                 {
814                                         Net_create_objnums[Net_create_loc++] = objnum;
815                                 }
816 #endif
817
818                                 obj = &Objects[objnum];
819
820                                 obj->mtype.phys_info.velocity = new_velocity;
821
822 //                              mprintf(0, "Created powerup, object #%i, velocity = %7.3f %7.3f %7.3f\n", objnum, f2fl(new_velocity.x), f2fl(new_velocity.y), f2fl(new_velocity.z));
823 //                              mprintf(0, "                             pos = x=%d y=%d z=%d\n", obj->pos.x, obj->pos.y, obj->pos.z);
824
825                                 obj->mtype.phys_info.drag = 512;        //1024;
826                                 obj->mtype.phys_info.mass = F1_0;
827
828                                 obj->mtype.phys_info.flags = PF_BOUNCE;
829
830                                 obj->rtype.vclip_info.vclip_num = Powerup_info[obj->id].vclip_num;
831                                 obj->rtype.vclip_info.frametime = Vclip[obj->rtype.vclip_info.vclip_num].frame_time;
832                                 obj->rtype.vclip_info.framenum = 0;
833
834                                 switch (obj->id) {
835                                         case POW_MISSILE_1:
836                                         case POW_MISSILE_4:
837                                         case POW_SHIELD_BOOST:
838                                         case POW_ENERGY:
839                                                 obj->lifeleft = (d_rand() + F1_0*3) * 64;               //      Lives for 3 to 3.5 binary minutes (a binary minute is 64 seconds)
840                                                 if (Game_mode & GM_MULTI)
841                                                         obj->lifeleft /= 2;
842                                                 break;
843                                         default:
844 //                                              if (Game_mode & GM_MULTI)
845 //                                                      obj->lifeleft = (d_rand() + F1_0*3) * 64;               //      Lives for 5 to 5.5 binary minutes (a binary minute is 64 seconds)
846                                                 break;
847                                 }
848                         }
849                         break;
850
851                 case OBJ_ROBOT:
852                         for (count=0; count<num; count++) {
853                                 int     rand_scale;
854                                 new_velocity = *init_vel;
855                                 old_mag = vm_vec_mag_quick(init_vel);
856
857                                 vm_vec_normalize_quick(&new_velocity);
858
859                                 //      We want powerups to move more in network mode.
860 //                              if (Game_mode & GM_MULTI)
861 //                                      rand_scale = 4;
862 //                              else
863                                         rand_scale = 2;
864
865                                 new_velocity.x += (d_rand()-16384)*2;
866                                 new_velocity.y += (d_rand()-16384)*2;
867                                 new_velocity.z += (d_rand()-16384)*2;
868
869                                 vm_vec_normalize_quick(&new_velocity);
870                                 vm_vec_scale(&new_velocity, (F1_0*32 + old_mag) * rand_scale);
871                                 new_pos = *pos;
872                                 //      This is dangerous, could be outside mine.
873 //                              new_pos.x += (d_rand()-16384)*8;
874 //                              new_pos.y += (d_rand()-16384)*7;
875 //                              new_pos.z += (d_rand()-16384)*6;
876
877                                 objnum = obj_create(OBJ_ROBOT, id, segnum, &new_pos, &vmd_identity_matrix, Polygon_models[Robot_info[id].model_num].rad, CT_AI, MT_PHYSICS, RT_POLYOBJ);
878
879                                 if ( objnum < 0 ) {
880                                         mprintf((1, "Can't create object in object_create_egg, robots.  Aborting.\n"));
881                                         Int3();
882                                         return objnum;
883                                 }
884
885 #ifdef NETWORK
886                                 if (Game_mode & GM_MULTI)
887                                 {
888                                         Net_create_objnums[Net_create_loc++] = objnum;
889                                 }
890 #endif
891
892                                 obj = &Objects[objnum];
893
894                                 //@@Took out this ugly hack 1/12/96, because Mike has added code
895                                 //@@that should fix it in a better way.
896                                 //@@//this is a super-ugly hack.  Since the baby stripe robots have
897                                 //@@//their firing point on their bounding sphere, the firing points
898                                 //@@//can poke through a wall if the robots are very close to it. So
899                                 //@@//we make their radii bigger so the guns can't get too close to
900                                 //@@//the walls
901                                 //@@if (Robot_info[obj->id].flags & RIF_BIG_RADIUS)
902                                 //@@    obj->size = (obj->size*3)/2;
903
904                                 //Set polygon-object-specific data
905
906                                 obj->rtype.pobj_info.model_num = Robot_info[obj->id].model_num;
907                                 obj->rtype.pobj_info.subobj_flags = 0;
908
909                                 //set Physics info
910                 
911                                 obj->mtype.phys_info.velocity = new_velocity;
912
913                                 obj->mtype.phys_info.mass = Robot_info[obj->id].mass;
914                                 obj->mtype.phys_info.drag = Robot_info[obj->id].drag;
915
916                                 obj->mtype.phys_info.flags |= (PF_LEVELLING);
917
918                                 obj->shields = Robot_info[obj->id].strength;
919
920                                 obj->ctype.ai_info.behavior = AIB_NORMAL;
921                                 Ai_local_info[OBJECT_NUMBER(obj)].player_awareness_type = PA_WEAPON_ROBOT_COLLISION;
922                                 Ai_local_info[OBJECT_NUMBER(obj)].player_awareness_time = F1_0*3;
923                                 obj->ctype.ai_info.CURRENT_STATE = AIS_LOCK;
924                                 obj->ctype.ai_info.GOAL_STATE = AIS_LOCK;
925                                 obj->ctype.ai_info.REMOTE_OWNER = -1;
926                         }
927
928                         // At JasenW's request, robots which contain robots
929                         // sometimes drop shields.
930                         if (d_rand() > 16384)
931                                 drop_powerup(OBJ_POWERUP, POW_SHIELD_BOOST, 1, init_vel, pos, segnum);
932
933                         break;
934
935                 default:
936                         Error("Error: Illegal type (%i) in object spawning.\n", type);
937         }
938
939         return objnum;
940 }
941
942 // ----------------------------------------------------------------------------
943 // Returns created object number.
944 // If object dropped by player, set flag.
945 int object_create_egg(object *objp)
946 {
947         int     rval;
948
949         if (!(Game_mode & GM_MULTI) & (objp->type != OBJ_PLAYER))
950         {
951                 if (objp->contains_type == OBJ_POWERUP)
952                 {
953                         if (objp->contains_id == POW_SHIELD_BOOST) {
954                                 if (Players[Player_num].shields >= i2f(100)) {
955                                         if (d_rand() > 16384) {
956                                                 mprintf((0, "Not dropping shield!\n"));
957                                                 return -1;
958                                         }
959                                 } else  if (Players[Player_num].shields >= i2f(150)) {
960                                         if (d_rand() > 8192) {
961                                                 mprintf((0, "Not dropping shield!\n"));
962                                                 return -1;
963                                         }
964                                 }
965                         } else if (objp->contains_id == POW_ENERGY) {
966                                 if (Players[Player_num].energy >= i2f(100)) {
967                                         if (d_rand() > 16384) {
968                                                 mprintf((0, "Not dropping energy!\n"));
969                                                 return -1;
970                                         }
971                                 } else  if (Players[Player_num].energy >= i2f(150)) {
972                                         if (d_rand() > 8192) {
973                                                 mprintf((0, "Not dropping energy!\n"));
974                                                 return -1;
975                                         }
976                                 }
977                         }
978                 }
979         }
980
981         rval = drop_powerup(objp->contains_type, objp->contains_id, objp->contains_count, &objp->mtype.phys_info.velocity, &objp->pos, objp->segnum);
982
983         if (rval != -1)
984         {
985                 if ((objp->type == OBJ_PLAYER) && (objp->id == Player_num))
986                         Objects[rval].flags |= OF_PLAYER_DROPPED;
987
988                 if (objp->type == OBJ_ROBOT && objp->contains_type==OBJ_POWERUP)
989                 {
990                         if (objp->contains_id==POW_VULCAN_WEAPON || objp->contains_id==POW_GAUSS_WEAPON)
991                                 Objects[rval].ctype.powerup_info.count = VULCAN_WEAPON_AMMO_AMOUNT;
992                         else if (objp->contains_id==POW_OMEGA_WEAPON)
993                                 Objects[rval].ctype.powerup_info.count = MAX_OMEGA_CHARGE;
994                 }
995         }
996
997         return rval;
998 }
999
1000 // -- extern int Items_destroyed;
1001
1002 //      -------------------------------------------------------------------------------------------------------
1003 //      Put count objects of type type (eg, powerup), id = id (eg, energy) into *objp, then drop them!  Yippee!
1004 //      Returns created object number.
1005 int call_object_create_egg(object *objp, int count, int type, int id)
1006 {
1007 // --   if (!(Game_mode & GM_MULTI) && (objp == ConsoleObject))
1008 // --           if (d_rand() < 32767/6) {
1009 // --                   Items_destroyed++;
1010 // --                   return -1;
1011 // --           }
1012
1013         if (count > 0) {
1014                 objp->contains_count = count;
1015                 objp->contains_type = type;
1016                 objp->contains_id = id;
1017                 return object_create_egg(objp);
1018         }
1019
1020         return -1;
1021 }
1022
1023 //what vclip does this explode with?
1024 int get_explosion_vclip(object *obj,int stage)
1025 {
1026         if (obj->type==OBJ_ROBOT) {
1027
1028                 if (stage==0 && Robot_info[obj->id].exp1_vclip_num>-1)
1029                                 return Robot_info[obj->id].exp1_vclip_num;
1030                 else if (stage==1 && Robot_info[obj->id].exp2_vclip_num>-1)
1031                                 return Robot_info[obj->id].exp2_vclip_num;
1032
1033         }
1034         else if (obj->type==OBJ_PLAYER && Player_ship->expl_vclip_num>-1)
1035                         return Player_ship->expl_vclip_num;
1036
1037         return VCLIP_SMALL_EXPLOSION;           //default
1038 }
1039
1040 //blow up a polygon model
1041 void explode_model(object *obj)
1042 {
1043         Assert(obj->render_type == RT_POLYOBJ);
1044
1045         if (Dying_modelnums[obj->rtype.pobj_info.model_num] != -1)
1046                 obj->rtype.pobj_info.model_num = Dying_modelnums[obj->rtype.pobj_info.model_num];
1047
1048         if (Polygon_models[obj->rtype.pobj_info.model_num].n_models > 1) {
1049                 int i;
1050
1051                 for (i=1;i<Polygon_models[obj->rtype.pobj_info.model_num].n_models;i++)
1052                         if (!(obj->type == OBJ_ROBOT && obj->id == 44 && i == 5))       //energy sucker energy part
1053                                 object_create_debris(obj,i);
1054
1055                 //make parent object only draw center part
1056                 obj->rtype.pobj_info.subobj_flags=1;
1057         }
1058 }
1059
1060 //if the object has a destroyed model, switch to it.  Otherwise, delete it.
1061 void maybe_delete_object(object *del_obj)
1062 {
1063         if (Dead_modelnums[del_obj->rtype.pobj_info.model_num] != -1) {
1064                 del_obj->rtype.pobj_info.model_num = Dead_modelnums[del_obj->rtype.pobj_info.model_num];
1065                 del_obj->flags |= OF_DESTROYED;
1066         }
1067         else {          //normal, multi-stage explosion
1068                 if (del_obj->type == OBJ_PLAYER)
1069                         del_obj->render_type = RT_NONE;
1070                 else
1071                         del_obj->flags |= OF_SHOULD_BE_DEAD;
1072         }
1073 }
1074
1075 //      -------------------------------------------------------------------------------------------------------
1076 //blow up an object.  Takes the object to destroy, and the point of impact
1077 void explode_object(object *hitobj,fix delay_time)
1078 {
1079         if (hitobj->flags & OF_EXPLODING) return;
1080
1081         if (delay_time) {               //wait a little while before creating explosion
1082                 int objnum;
1083                 object *obj;
1084
1085                 //create a placeholder object to do the delay, with id==-1
1086
1087                 objnum = obj_create( OBJ_FIREBALL,-1,hitobj->segnum,&hitobj->pos,&vmd_identity_matrix,0,
1088                                                 CT_EXPLOSION,MT_NONE,RT_NONE);
1089         
1090                 if (objnum < 0 ) {
1091                         maybe_delete_object(hitobj);            //no explosion, die instantly
1092                         mprintf((1,"Couldn't start explosion, deleting object now\n"));
1093                         Int3();
1094                         return;
1095                 }
1096         
1097                 obj = &Objects[objnum];
1098         
1099                 //now set explosion-specific data
1100         
1101                 obj->lifeleft = delay_time;
1102                 obj->ctype.expl_info.delete_objnum = OBJECT_NUMBER(hitobj);
1103 #ifndef NDEBUG
1104                 if (obj->ctype.expl_info.delete_objnum < 0)
1105                  Int3(); // See Rob!
1106 #endif
1107                 obj->ctype.expl_info.delete_time = -1;
1108                 obj->ctype.expl_info.spawn_time = 0;
1109
1110         }
1111         else {
1112                 object *expl_obj;
1113                 int vclip_num;
1114
1115                 vclip_num = get_explosion_vclip(hitobj,0);
1116
1117                 expl_obj = object_create_explosion(hitobj->segnum, &hitobj->pos, fixmul(hitobj->size,EXPLOSION_SCALE), vclip_num );
1118         
1119                 if (! expl_obj) {
1120                         maybe_delete_object(hitobj);            //no explosion, die instantly
1121                         mprintf((0,"Couldn't start explosion, deleting object now\n"));
1122                         return;
1123                 }
1124
1125                 //don't make debris explosions have physics, because they often
1126                 //happen when the debris has hit the wall, so the fireball is trying
1127                 //to move into the wall, which shows off FVI problems.          
1128                 if (hitobj->type!=OBJ_DEBRIS && hitobj->movement_type==MT_PHYSICS) {
1129                         expl_obj->movement_type = MT_PHYSICS;
1130                         expl_obj->mtype.phys_info = hitobj->mtype.phys_info;
1131                 }
1132         
1133                 if (hitobj->render_type==RT_POLYOBJ && hitobj->type!=OBJ_DEBRIS)
1134                         explode_model(hitobj);
1135
1136                 maybe_delete_object(hitobj);
1137         }
1138
1139         hitobj->flags |= OF_EXPLODING;          //say that this is blowing up
1140         hitobj->control_type = CT_NONE;         //become inert while exploding
1141
1142 }
1143
1144
1145 //do whatever needs to be done for this piece of debris for this frame
1146 void do_debris_frame(object *obj)
1147 {
1148         Assert(obj->control_type == CT_DEBRIS);
1149
1150         if (obj->lifeleft < 0)
1151                 explode_object(obj,0);
1152
1153 }
1154
1155 extern void drop_stolen_items(object *objp);
1156
1157 //do whatever needs to be done for this explosion for this frame
1158 void do_explosion_sequence(object *obj)
1159 {
1160         Assert(obj->control_type == CT_EXPLOSION);
1161
1162         //mprintf( 0, "Object %d life left is %d\n", OBJECT_NUMBER(obj), obj->lifeleft );
1163
1164         //See if we should die of old age
1165         if (obj->lifeleft <= 0 )        {       // We died of old age
1166                 obj->flags |= OF_SHOULD_BE_DEAD;
1167                 obj->lifeleft = 0;
1168         }
1169
1170         //See if we should create a secondary explosion
1171         if (obj->lifeleft <= obj->ctype.expl_info.spawn_time) {
1172                 object *expl_obj,*del_obj;
1173                 int vclip_num;
1174                 vms_vector *spawn_pos;
1175
1176                 if ((obj->ctype.expl_info.delete_objnum < 0) || (obj->ctype.expl_info.delete_objnum > Highest_object_index)) {
1177                         mprintf((0, "Illegal value for delete_objnum in fireball.c\n"));
1178                         Int3(); // get Rob, please... thanks
1179                         return;
1180                 }
1181
1182                 del_obj = &Objects[obj->ctype.expl_info.delete_objnum];
1183
1184                 spawn_pos = &del_obj->pos;
1185
1186                 if (!((del_obj->type==OBJ_ROBOT || del_obj->type==OBJ_CLUTTER || del_obj->type==OBJ_CNTRLCEN || del_obj->type == OBJ_PLAYER) && (del_obj->segnum != -1))) {
1187                         Int3(); //pretty bad
1188                         return;
1189                 }
1190
1191                 vclip_num = get_explosion_vclip(del_obj,1);
1192
1193                 if (del_obj->type == OBJ_ROBOT && Robot_info[del_obj->id].badass)
1194                         expl_obj = object_create_badass_explosion( NULL, del_obj->segnum, spawn_pos, fixmul(del_obj->size, EXPLOSION_SCALE), vclip_num, F1_0*Robot_info[del_obj->id].badass, i2f(4)*Robot_info[del_obj->id].badass, i2f(35)*Robot_info[del_obj->id].badass, -1 );
1195                 else
1196                         expl_obj = object_create_explosion( del_obj->segnum, spawn_pos, fixmul(del_obj->size, EXPLOSION_SCALE), vclip_num );
1197
1198                 if ((del_obj->contains_count > 0) && !(Game_mode & GM_MULTI)) { // Multiplayer handled outside of this code!!
1199                         //      If dropping a weapon that the player has, drop energy instead, unless it's vulcan, in which case drop vulcan ammo.
1200                         if (del_obj->contains_type == OBJ_POWERUP)
1201                                 maybe_replace_powerup_with_energy(del_obj);
1202                         object_create_egg(del_obj);
1203                 } else if ((del_obj->type == OBJ_ROBOT) && !(Game_mode & GM_MULTI)) { // Multiplayer handled outside this code!!
1204                         robot_info      *robptr = &Robot_info[del_obj->id];
1205                         if (robptr->contains_count) {
1206                                 if (((d_rand() * 16) >> 15) < robptr->contains_prob) {
1207                                         del_obj->contains_count = ((d_rand() * robptr->contains_count) >> 15) + 1;
1208                                         del_obj->contains_type = robptr->contains_type;
1209                                         del_obj->contains_id = robptr->contains_id;
1210                                         maybe_replace_powerup_with_energy(del_obj);
1211                                         object_create_egg(del_obj);
1212                                 }
1213                         }
1214
1215                         if (robptr->thief)
1216                                 drop_stolen_items(del_obj);
1217
1218                         if (robptr->companion) {
1219                                 DropBuddyMarker(del_obj);
1220                         }
1221                 }
1222
1223                 if ( Robot_info[del_obj->id].exp2_sound_num > -1 )
1224                         digi_link_sound_to_pos( Robot_info[del_obj->id].exp2_sound_num, del_obj->segnum, 0, spawn_pos, 0, F1_0 );
1225                         //PLAY_SOUND_3D( Robot_info[del_obj->id].exp2_sound_num, spawn_pos, del_obj->segnum  );
1226
1227                 // mprintf( 0, "Spawned an explosion of type %d\n", Robot_info[del_obj->id].exp2_vclip_num );
1228
1229                 //mprintf( 0, "Object %d spawned.\n", OBJECT_NUMBER(obj) );
1230                 //mprintf( 0, "Explosion at %d,%d,%d\n", obj->pos.x, obj->pos.y, obj->pos.z );
1231                 //mprintf( 0, "Explosion at %d,%d,%d\n", obj->pos.x, obj->pos.y, obj->pos.z );
1232                 //mprintf( 0, "Spawned exp at %d,%d,%d\n", expl_obj->pos.x, expl_obj->pos.y, expl_obj->pos.z );
1233
1234                 obj->ctype.expl_info.spawn_time = -1;
1235
1236                 //make debris
1237                 if (del_obj->render_type==RT_POLYOBJ)
1238                         explode_model(del_obj);         //explode a polygon model
1239
1240                 //set some parm in explosion
1241                 if (expl_obj) {
1242
1243                         if (del_obj->movement_type == MT_PHYSICS) {
1244                                 expl_obj->movement_type = MT_PHYSICS;
1245                                 expl_obj->mtype.phys_info = del_obj->mtype.phys_info;
1246                         }
1247
1248                         expl_obj->ctype.expl_info.delete_time = expl_obj->lifeleft/2;
1249                         expl_obj->ctype.expl_info.delete_objnum = OBJECT_NUMBER(del_obj);
1250 #ifndef NDEBUG
1251                         if (obj->ctype.expl_info.delete_objnum < 0)
1252                                 Int3(); // See Rob!
1253 #endif
1254
1255                 }
1256                 else {
1257                         maybe_delete_object(del_obj);
1258                         mprintf((0,"Couldn't create secondary explosion, deleting object now\n"));
1259                 }
1260
1261         }
1262
1263         //See if we should delete an object
1264         if (obj->lifeleft <= obj->ctype.expl_info.delete_time) {
1265                 object *del_obj = &Objects[obj->ctype.expl_info.delete_objnum];
1266
1267                 obj->ctype.expl_info.delete_time = -1;
1268
1269                 maybe_delete_object(del_obj);
1270         }
1271 }
1272
1273 #define EXPL_WALL_TIME                                  (f1_0)
1274 #define EXPL_WALL_TOTAL_FIREBALLS       32
1275 #define EXPL_WALL_FIREBALL_SIZE                 (0x48000*6/10)  //smallest size
1276
1277 expl_wall expl_wall_list[MAX_EXPLODING_WALLS];
1278
1279 void init_exploding_walls()
1280 {
1281         int i;
1282
1283         for (i=0;i<MAX_EXPLODING_WALLS;i++)
1284                 expl_wall_list[i].segnum = -1;
1285 }
1286
1287 //explode the given wall
1288 void explode_wall(int segnum,int sidenum)
1289 {
1290         int i;
1291         vms_vector pos;
1292
1293         //find a free slot
1294
1295         for (i=0;i<MAX_EXPLODING_WALLS && expl_wall_list[i].segnum != -1;i++);
1296
1297         if (i==MAX_EXPLODING_WALLS) {           //didn't find slot.
1298                 mprintf((0,"Couldn't find free slot for exploding wall!\n"));
1299                 Int3();
1300                 return;
1301         }
1302
1303         expl_wall_list[i].segnum        = segnum;
1304         expl_wall_list[i].sidenum       = sidenum;
1305         expl_wall_list[i].time          = 0;
1306
1307         //play one long sound for whole door wall explosion
1308         compute_center_point_on_side(&pos,&Segments[segnum],sidenum);
1309         digi_link_sound_to_pos( SOUND_EXPLODING_WALL,segnum, sidenum, &pos, 0, F1_0 );
1310
1311 }
1312
1313 //handle walls for this frame
1314 //note: this wall code assumes the wall is not triangulated
1315 void do_exploding_wall_frame()
1316 {
1317         int i;
1318
1319         for (i=0;i<MAX_EXPLODING_WALLS;i++) {
1320                 int segnum = expl_wall_list[i].segnum;
1321
1322                 if (segnum != -1) {
1323                         int sidenum = expl_wall_list[i].sidenum;
1324                         fix oldfrac,newfrac;
1325                         int old_count,new_count,e;              //n,
1326
1327                         oldfrac = fixdiv(expl_wall_list[i].time,EXPL_WALL_TIME);
1328
1329                         expl_wall_list[i].time += FrameTime;
1330                         if (expl_wall_list[i].time > EXPL_WALL_TIME)
1331                                 expl_wall_list[i].time = EXPL_WALL_TIME;
1332
1333                         if (expl_wall_list[i].time>(EXPL_WALL_TIME*3)/4) {
1334                                 segment *seg,*csegp;
1335                                 int cside,a,n;
1336
1337                                 seg = &Segments[segnum];
1338
1339                                 a = Walls[seg->sides[sidenum].wall_num].clip_num;
1340                                 n = WallAnims[a].num_frames;
1341
1342                                 csegp = &Segments[seg->children[sidenum]];
1343                                 cside = find_connect_side(seg, csegp);
1344
1345                                 wall_set_tmap_num(seg,sidenum,csegp,cside,a,n-1);
1346
1347                                 Walls[seg->sides[sidenum].wall_num].flags |= WALL_BLASTED;
1348                                 Walls[csegp->sides[cside].wall_num].flags |= WALL_BLASTED;
1349
1350                         }
1351
1352                         newfrac = fixdiv(expl_wall_list[i].time,EXPL_WALL_TIME);
1353
1354                         old_count = f2i(EXPL_WALL_TOTAL_FIREBALLS * fixmul(oldfrac,oldfrac));
1355                         new_count = f2i(EXPL_WALL_TOTAL_FIREBALLS * fixmul(newfrac,newfrac));
1356
1357                         //n = new_count - old_count;
1358
1359 //                      mprintf(0,"Creating %d new explosions\n",new_count-old_count);
1360
1361                         //now create all the next explosions
1362
1363                         for (e=old_count;e<new_count;e++) {
1364                                 short                   vertnum_list[4];
1365                                 vms_vector      *v0,*v1,*v2;
1366                                 vms_vector      vv0,vv1,pos;
1367                                 fix                     size;
1368
1369                                 //calc expl position
1370
1371                                 get_side_verts(vertnum_list,segnum,sidenum);
1372
1373                                 v0 = &Vertices[vertnum_list[0]];
1374                                 v1 = &Vertices[vertnum_list[1]];
1375                                 v2 = &Vertices[vertnum_list[2]];
1376
1377                                 vm_vec_sub(&vv0,v0,v1);
1378                                 vm_vec_sub(&vv1,v2,v1);
1379
1380                                 vm_vec_scale_add(&pos,v1,&vv0,d_rand()*2);
1381                                 vm_vec_scale_add2(&pos,&vv1,d_rand()*2);
1382
1383                                 size = EXPL_WALL_FIREBALL_SIZE + (2*EXPL_WALL_FIREBALL_SIZE * e / EXPL_WALL_TOTAL_FIREBALLS);
1384
1385                                 //fireballs start away from door, with subsequent ones getting closer
1386                                 #ifdef COMPACT_SEGS     
1387                                         {
1388                                         vms_vector _vn;
1389                                         get_side_normal(&Segments[segnum], sidenum, 0, &_vn );
1390                                         vm_vec_scale_add2(&pos,&_vn,size*(EXPL_WALL_TOTAL_FIREBALLS-e)/EXPL_WALL_TOTAL_FIREBALLS);
1391                                         }
1392                                 #else
1393                                         vm_vec_scale_add2(&pos,&Segments[segnum].sides[sidenum].normals[0],size*(EXPL_WALL_TOTAL_FIREBALLS-e)/EXPL_WALL_TOTAL_FIREBALLS);
1394                                 #endif
1395
1396                                 if (e & 3)              //3 of 4 are normal
1397                                         object_create_explosion(expl_wall_list[i].segnum,&pos,size,VCLIP_SMALL_EXPLOSION);
1398                                 else
1399                                         object_create_badass_explosion( NULL, expl_wall_list[i].segnum, &pos,
1400                                         size,
1401                                         VCLIP_SMALL_EXPLOSION,
1402                                         i2f(4),         // damage strength
1403                                         i2f(20),                //      damage radius
1404                                         i2f(50),                //      damage force
1405                                         -1              //      parent id
1406                                         );
1407
1408
1409                         }
1410
1411                         if (expl_wall_list[i].time >= EXPL_WALL_TIME)
1412                                 expl_wall_list[i].segnum = -1;  //flag this slot as free
1413
1414                 }
1415         }
1416
1417 }
1418
1419
1420 //creates afterburner blobs behind the specified object
1421 void drop_afterburner_blobs(object *obj, int count, fix size_scale, fix lifetime)
1422 {
1423         vms_vector pos_left,pos_right;
1424         int segnum;
1425
1426         vm_vec_scale_add(&pos_left, &obj->pos, &obj->orient.fvec, -obj->size);
1427         vm_vec_scale_add2(&pos_left, &obj->orient.rvec, -obj->size/4);
1428         vm_vec_scale_add(&pos_right, &pos_left, &obj->orient.rvec, obj->size/2);
1429
1430         if (count == 1)
1431                 vm_vec_avg(&pos_left, &pos_left, &pos_right);
1432
1433         segnum = find_point_seg(&pos_left, obj->segnum);
1434         if (segnum != -1)
1435                 object_create_explosion(segnum, &pos_left, size_scale, VCLIP_AFTERBURNER_BLOB );
1436
1437         if (count > 1) {
1438                 segnum = find_point_seg(&pos_right, obj->segnum);
1439                 if (segnum != -1) {
1440                         object  *blob_obj;
1441                         blob_obj = object_create_explosion(segnum, &pos_right, size_scale, VCLIP_AFTERBURNER_BLOB );
1442                         if (lifetime != -1)
1443                                 blob_obj->lifeleft = lifetime;
1444                 }
1445         }
1446 }
1447
1448