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