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