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