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