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