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