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