]> icculus.org git repositories - btb/d2x.git/blob - main/ai2.c
Build fixes. Now works with automake 1.5 and autoconf 2.52. --enable-editor option...
[btb/d2x.git] / main / ai2.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.  
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 #ifdef HAVE_CONFIG_H
15 #include <conf.h>
16 #endif
17
18 #ifdef RCS
19 static char rcsid[] = "$Id: ai2.c,v 1.2 2001-01-31 15:17:48 bradleyb Exp $";
20 #endif
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <time.h>
25
26 #include "inferno.h"
27 #include "game.h"
28 #include "mono.h"
29 #include "3d.h"
30
31 #include "u_mem.h"
32 #include "object.h"
33 #include "render.h"
34 #include "error.h"
35 #include "ai.h"
36 #include "laser.h"
37 #include "fvi.h"
38 #include "polyobj.h"
39 #include "bm.h"
40 #include "weapon.h"
41 #include "physics.h"
42 #include "collide.h"
43 #include "player.h"
44 #include "wall.h"
45 #include "vclip.h"
46 #include "digi.h"
47 #include "fireball.h"
48 #include "morph.h"
49 #include "effects.h"
50 #include "timer.h"
51 #include "sounds.h"
52 #include "cntrlcen.h"
53 #include "multibot.h"
54 #include "multi.h"
55 #include "network.h"
56 #include "gameseq.h"
57 #include "key.h"
58 #include "powerup.h"
59 #include "gauges.h"
60 #include "text.h"
61
62 #ifdef EDITOR
63 #include "editor\editor.h"
64 #include "editor\kdefs.h"
65 #endif
66
67 #ifndef NDEBUG
68 #include "string.h"
69 #include <time.h>
70 #endif
71
72 void teleport_boss(object *objp);
73 int boss_fits_in_seg(object *boss_objp, int segnum);
74
75
76 int     Flinch_scale = 4;
77 int     Attack_scale = 24;
78 byte    Mike_to_matt_xlate[] = {AS_REST, AS_REST, AS_ALERT, AS_ALERT, AS_FLINCH, AS_FIRE, AS_RECOIL, AS_REST};
79
80 //      Amount of time since the current robot was last processed for things such as movement.
81 //      It is not valid to use FrameTime because robots do not get moved every frame.
82
83 int     Num_boss_teleport_segs;
84 short   Boss_teleport_segs[MAX_BOSS_TELEPORT_SEGS];
85 int     Num_boss_gate_segs;
86 short   Boss_gate_segs[MAX_BOSS_TELEPORT_SEGS];
87
88 // ---------------------------------------------------------
89 //      On entry, N_robot_types had darn sure better be set.
90 //      Mallocs N_robot_types robot_info structs into global Robot_info.
91 void init_ai_system(void)
92 {
93 #if 0
94         int     i;
95
96         mprintf((0, "Trying to malloc %i bytes for Robot_info.\n", N_robot_types * sizeof(*Robot_info)));
97         Robot_info = (robot_info *) d_malloc( N_robot_types * sizeof(*Robot_info) );
98         mprintf((0, "Robot_info = %i\n", Robot_info));
99
100         for (i=0; i<N_robot_types; i++) {
101                 Robot_info[i].field_of_view = F1_0/2;
102                 Robot_info[i].firing_wait = F1_0;
103                 Robot_info[i].turn_time = F1_0*2;
104                 // -- Robot_info[i].fire_power = F1_0;
105                 // -- Robot_info[i].shield = F1_0/2;
106                 Robot_info[i].max_speed = F1_0*10;
107                 Robot_info[i].always_0xabcd = 0xabcd;
108         }
109 #endif
110
111 }
112
113 // ---------------------------------------------------------------------------------------------------------------------
114 //      Given a behavior, set initial mode.
115 int ai_behavior_to_mode(int behavior)
116 {
117         switch (behavior) {
118                 case AIB_STILL:                 return AIM_STILL;
119                 case AIB_NORMAL:                        return AIM_CHASE_OBJECT;
120                 case AIB_BEHIND:                        return AIM_BEHIND;
121                 case AIB_RUN_FROM:              return AIM_RUN_FROM_OBJECT;
122                 case AIB_SNIPE:                 return AIM_STILL;       //      Changed, 09/13/95, MK, snipers are still until they see you or are hit.
123                 case AIB_STATION:                       return AIM_STILL;
124                 case AIB_FOLLOW:                        return AIM_FOLLOW_PATH;
125                 default:        Int3(); //      Contact Mike: Error, illegal behavior type
126         }
127
128         return AIM_STILL;
129 }
130
131 // ---------------------------------------------------------------------------------------------------------------------
132 //      Call every time the player starts a new ship.
133 void ai_init_boss_for_ship(void)
134 {
135         Boss_hit_time = -F1_0*10;
136
137 }
138
139 // ---------------------------------------------------------------------------------------------------------------------
140 //      initial_mode == -1 means leave mode unchanged.
141 void init_ai_object(int objnum, int behavior, int hide_segment)
142 {
143         object  *objp = &Objects[objnum];
144         ai_static       *aip = &objp->ctype.ai_info;
145         ai_local                *ailp = &Ai_local_info[objnum];
146         robot_info      *robptr = &Robot_info[objp->id];
147
148         if (behavior == 0) {
149                 // mprintf((0, "Behavior of 0 for object #%i, bashing to AIB_NORMAL.\n", objnum));
150                 behavior = AIB_NORMAL;
151                 aip->behavior = behavior;
152         }
153         // mprintf((0, "Initializing object #%i\n", objnum));
154
155         //      mode is now set from the Robot dialog, so this should get overwritten.
156         ailp->mode = AIM_STILL;
157
158         ailp->previous_visibility = 0;
159
160         if (behavior != -1) {
161                 aip->behavior = behavior;
162                 ailp->mode = ai_behavior_to_mode(aip->behavior);
163         } else if (!((aip->behavior >= MIN_BEHAVIOR) && (aip->behavior <= MAX_BEHAVIOR))) {
164                 mprintf((0, "[obj %i -> normal] ", objnum));
165                 aip->behavior = AIB_NORMAL;
166         }
167
168         if (robptr->companion) {
169                 ailp->mode = AIM_GOTO_PLAYER;
170                 Escort_kill_object = -1;
171         }
172
173         if (robptr->thief) {
174                 aip->behavior = AIB_SNIPE;
175                 ailp->mode = AIM_THIEF_WAIT;
176         }
177
178         if (robptr->attack_type) {
179                 aip->behavior = AIB_NORMAL;
180                 ailp->mode = ai_behavior_to_mode(aip->behavior);
181         }
182
183         // This is astonishingly stupid!  This routine gets called by matcens! KILL KILL KILL!!! Point_segs_free_ptr = Point_segs;
184
185         vm_vec_zero(&objp->mtype.phys_info.velocity);
186         // -- ailp->wait_time = F1_0*5;
187         ailp->player_awareness_time = 0;
188         ailp->player_awareness_type = 0;
189         aip->GOAL_STATE = AIS_SRCH;
190         aip->CURRENT_STATE = AIS_REST;
191         ailp->time_player_seen = GameTime;
192         ailp->next_misc_sound_time = GameTime;
193         ailp->time_player_sound_attacked = GameTime;
194
195         if ((behavior == AIB_SNIPE) || (behavior == AIB_STATION) || (behavior == AIB_RUN_FROM) || (behavior == AIB_FOLLOW)) {
196                 aip->hide_segment = hide_segment;
197                 ailp->goal_segment = hide_segment;
198                 aip->hide_index = -1;                   // This means the path has not yet been created.
199                 aip->cur_path_index = 0;
200         }
201
202         aip->SKIP_AI_COUNT = 0;
203
204         if (robptr->cloak_type == RI_CLOAKED_ALWAYS)
205                 aip->CLOAKED = 1;
206         else
207                 aip->CLOAKED = 0;
208
209         objp->mtype.phys_info.flags |= (PF_BOUNCE | PF_TURNROLL);
210         
211         aip->REMOTE_OWNER = -1;
212
213         aip->dying_sound_playing = 0;
214         aip->dying_start_time = 0;
215
216 }
217
218
219 extern object * create_morph_robot( segment *segp, vms_vector *object_pos, int object_id);
220
221 // --------------------------------------------------------------------------------------------------------------------
222 //      Create a Buddy bot.
223 //      This automatically happens when you bring up the Buddy menu in a debug version.
224 //      It is available as a cheat in a non-debug (release) version.
225 void create_buddy_bot(void)
226 {
227         int     buddy_id;
228         vms_vector      object_pos;
229
230         for (buddy_id=0; buddy_id<N_robot_types; buddy_id++)
231                 if (Robot_info[buddy_id].companion)
232                         break;
233
234         if (buddy_id == N_robot_types) {
235                 mprintf((0, "Can't create Buddy.  No 'companion' bot found in Robot_info!\n"));
236                 return;
237         }
238
239         compute_segment_center(&object_pos, &Segments[ConsoleObject->segnum]);
240
241         create_morph_robot( &Segments[ConsoleObject->segnum], &object_pos, buddy_id);
242 }
243
244 #define QUEUE_SIZE      256
245
246 // --------------------------------------------------------------------------------------------------------------------
247 //      Create list of segments boss is allowed to teleport to at segptr.
248 //      Set *num_segs.
249 //      Boss is allowed to teleport to segments he fits in (calls object_intersects_wall) and
250 //      he can reach from his initial position (calls find_connected_distance).
251 //      If size_check is set, then only add segment if boss can fit in it, else any segment is legal.
252 //      one_wall_hack added by MK, 10/13/95: A mega-hack!  Set to !0 to ignore the 
253 void init_boss_segments(short segptr[], int *num_segs, int size_check, int one_wall_hack)
254 {
255         int                     boss_objnum=-1;
256         int                     i;
257
258         *num_segs = 0;
259 #ifdef EDITOR
260         N_selected_segs = 0;
261 #endif
262
263
264 if (size_check)
265         mprintf((0, "Boss fits in segments:\n"));
266         //      See if there is a boss.  If not, quick out.
267         for (i=0; i<=Highest_object_index; i++)
268                 if ((Objects[i].type == OBJ_ROBOT) && (Robot_info[Objects[i].id].boss_flag)) {
269                         if (boss_objnum != -1)          //      There are two bosses in this mine!  i and boss_objnum!
270                                 Int3();                 //do int3 here instead of assert so museum will work
271                         boss_objnum = i;
272                 }
273
274         if (boss_objnum != -1) {
275                 int                     original_boss_seg;
276                 vms_vector      original_boss_pos;
277                 object          *boss_objp = &Objects[boss_objnum];
278                 int                     head, tail;
279                 int                     seg_queue[QUEUE_SIZE];
280 //ALREADY IN RENDER.H           byte                    visited[MAX_SEGMENTS];
281                 fix                     boss_size_save;
282
283                 boss_size_save = boss_objp->size;
284                 // -- Causes problems!! -- boss_objp->size = fixmul((F1_0/4)*3, boss_objp->size);
285                 original_boss_seg = boss_objp->segnum;
286                 original_boss_pos = boss_objp->pos;
287                 head = 0;
288                 tail = 0;
289                 seg_queue[head++] = original_boss_seg;
290
291                 segptr[(*num_segs)++] = original_boss_seg;
292                 mprintf((0, "%4i ", original_boss_seg));
293                 #ifdef EDITOR
294                 Selected_segs[N_selected_segs++] = original_boss_seg;
295                 #endif
296
297                 for (i=0; i<=Highest_segment_index; i++)
298                         visited[i] = 0;
299
300                 while (tail != head) {
301                         int             sidenum;
302                         segment *segp = &Segments[seg_queue[tail++]];
303
304                         tail &= QUEUE_SIZE-1;
305
306                         for (sidenum=0; sidenum<MAX_SIDES_PER_SEGMENT; sidenum++) {
307                                 int     w;
308
309                                 if (((w = WALL_IS_DOORWAY(segp, sidenum)) & WID_FLY_FLAG) || one_wall_hack) {
310                                         //      If we get here and w == WID_WALL, then we want to process through this wall, else not.
311                                         if (IS_CHILD(segp->children[sidenum])) {
312                                                 if (one_wall_hack)
313                                                         one_wall_hack--;
314                                         } else
315                                                 continue;
316
317                                         if (visited[segp->children[sidenum]] == 0) {
318                                                 seg_queue[head++] = segp->children[sidenum];
319                                                 visited[segp->children[sidenum]] = 1;
320                                                 head &= QUEUE_SIZE-1;
321                                                 if (head > tail) {
322                                                         if (head == tail + QUEUE_SIZE-1)
323                                                                 Int3(); //      queue overflow.  Make it bigger!
324                                                 } else
325                                                         if (head+QUEUE_SIZE == tail + QUEUE_SIZE-1)
326                                                                 Int3(); //      queue overflow.  Make it bigger!
327         
328                                                 if ((!size_check) || boss_fits_in_seg(boss_objp, segp->children[sidenum])) {
329                                                         segptr[(*num_segs)++] = segp->children[sidenum];
330                                                         if (size_check) mprintf((0, "%4i ", segp->children[sidenum]));
331                                                         #ifdef EDITOR
332                                                         Selected_segs[N_selected_segs++] = segp->children[sidenum];
333                                                         #endif
334                                                         if (*num_segs >= MAX_BOSS_TELEPORT_SEGS) {
335                                                                 mprintf((1, "Warning: Too many boss teleport segments.  Found %i after searching %i/%i segments.\n", MAX_BOSS_TELEPORT_SEGS, segp->children[sidenum], Highest_segment_index+1));
336                                                                 tail = head;
337                                                         }
338                                                 }
339                                         }
340                                 }
341                         }
342
343                 }
344
345                 boss_objp->size = boss_size_save;
346                 boss_objp->pos = original_boss_pos;
347                 obj_relink(boss_objnum, original_boss_seg);
348
349         }
350
351 }
352
353 extern void init_buddy_for_level(void);
354
355 // ---------------------------------------------------------------------------------------------------------------------
356 void init_ai_objects(void)
357 {
358         int     i;
359
360         Point_segs_free_ptr = Point_segs;
361
362         for (i=0; i<MAX_OBJECTS; i++) {
363                 object *objp = &Objects[i];
364
365                 if (objp->control_type == CT_AI)
366                         init_ai_object(i, objp->ctype.ai_info.behavior, objp->ctype.ai_info.hide_segment);
367         }
368
369         init_boss_segments(Boss_gate_segs, &Num_boss_gate_segs, 0, 0);
370
371         init_boss_segments(Boss_teleport_segs, &Num_boss_teleport_segs, 1, 0);
372         if (Num_boss_teleport_segs == 1)
373                 init_boss_segments(Boss_teleport_segs, &Num_boss_teleport_segs, 1, 1);
374
375         Boss_dying_sound_playing = 0;
376         Boss_dying = 0;
377         // -- unused! MK, 10/21/95 -- Boss_been_hit = 0;
378         Gate_interval = F1_0*4 - Difficulty_level*i2f(2)/3;
379
380         Ai_initialized = 1;
381
382         ai_do_cloak_stuff();
383
384         init_buddy_for_level();
385
386         if (Current_level_num == Last_level) {
387                 Boss_teleport_interval = F1_0*10;
388                 Boss_cloak_interval = F1_0*15;                                  //      Time between cloaks
389         } else {
390                 Boss_teleport_interval = F1_0*7;
391                 Boss_cloak_interval = F1_0*10;                                  //      Time between cloaks
392         }
393 }
394
395 int     Lunacy = 0;
396 int     Diff_save = 1;
397
398 fix     Firing_wait_copy[MAX_ROBOT_TYPES];
399 fix     Firing_wait2_copy[MAX_ROBOT_TYPES];
400 byte    Rapidfire_count_copy[MAX_ROBOT_TYPES];
401
402 void do_lunacy_on(void)
403 {
404         int     i;
405
406         if (Lunacy)     //already on
407                 return;
408
409         Lunacy = 1;
410
411         Diff_save = Difficulty_level;
412         Difficulty_level = NDL-1;
413
414         for (i=0; i<MAX_ROBOT_TYPES; i++) {
415                 Firing_wait_copy[i] = Robot_info[i].firing_wait[NDL-1];
416                 Firing_wait2_copy[i] = Robot_info[i].firing_wait2[NDL-1];
417                 Rapidfire_count_copy[i] = Robot_info[i].rapidfire_count[NDL-1];
418
419                 Robot_info[i].firing_wait[NDL-1] = Robot_info[i].firing_wait[1];
420                 Robot_info[i].firing_wait2[NDL-1] = Robot_info[i].firing_wait2[1];
421                 Robot_info[i].rapidfire_count[NDL-1] = Robot_info[i].rapidfire_count[1];
422         }
423
424 }
425
426 void do_lunacy_off(void)
427 {
428         int     i;
429
430         if (!Lunacy)    //already off
431                 return;
432
433         Lunacy = 0;
434
435         for (i=0; i<MAX_ROBOT_TYPES; i++) {
436                 Robot_info[i].firing_wait[NDL-1] = Firing_wait_copy[i];
437                 Robot_info[i].firing_wait2[NDL-1] = Firing_wait2_copy[i];
438                 Robot_info[i].rapidfire_count[NDL-1] = Rapidfire_count_copy[i];
439         }
440
441         Difficulty_level = Diff_save;
442 }
443
444 //      ----------------------------------------------------------------
445 //      Do *dest = *delta unless:
446 //                              *delta is pretty small
447 //              and     they are of different signs.
448 void set_rotvel_and_saturate(fix *dest, fix delta)
449 {
450         if ((delta ^ *dest) < 0) {
451                 if (abs(delta) < F1_0/8) {
452                         // mprintf((0, "D"));
453                         *dest = delta/4;
454                 } else
455                         // mprintf((0, "d"));
456                         *dest = delta;
457         } else {
458                 // mprintf((0, "!"));
459                 *dest = delta;
460         }
461 }
462
463 //--debug-- #ifndef NDEBUG
464 //--debug-- int Total_turns=0;
465 //--debug-- int Prevented_turns=0;
466 //--debug-- #endif
467
468 #define AI_TURN_SCALE   1
469 #define BABY_SPIDER_ID  14
470 #define FIRE_AT_NEARBY_PLAYER_THRESHOLD (F1_0*40)
471
472 extern void physics_turn_towards_vector(vms_vector *goal_vector, object *obj, fix rate);
473 extern fix Seismic_tremor_magnitude;
474
475 //-------------------------------------------------------------------------------------------
476 void ai_turn_towards_vector(vms_vector *goal_vector, object *objp, fix rate)
477 {
478         vms_vector      new_fvec;
479         fix                     dot;
480
481         //      Not all robots can turn, eg, SPECIAL_REACTOR_ROBOT
482         if (rate == 0)
483                 return;
484
485         if ((objp->id == BABY_SPIDER_ID) && (objp->type == OBJ_ROBOT)) {
486                 physics_turn_towards_vector(goal_vector, objp, rate);
487                 return;
488         }
489
490         new_fvec = *goal_vector;
491
492         dot = vm_vec_dot(goal_vector, &objp->orient.fvec);
493
494         if (dot < (F1_0 - FrameTime/2)) {
495                 fix     mag;
496                 fix     new_scale = fixdiv(FrameTime * AI_TURN_SCALE, rate);
497                 vm_vec_scale(&new_fvec, new_scale);
498                 vm_vec_add2(&new_fvec, &objp->orient.fvec);
499                 mag = vm_vec_normalize_quick(&new_fvec);
500                 if (mag < F1_0/256) {
501                         mprintf((1, "Degenerate vector in ai_turn_towards_vector (mag = %7.3f)\n", f2fl(mag)));
502                         new_fvec = *goal_vector;                //      if degenerate vector, go right to goal
503                 }
504         }
505
506         if (Seismic_tremor_magnitude) {
507                 vms_vector      rand_vec;
508                 fix                     scale;
509                 make_random_vector(&rand_vec);
510                 scale = fixdiv(2*Seismic_tremor_magnitude, Robot_info[objp->id].mass);
511                 vm_vec_scale_add2(&new_fvec, &rand_vec, scale);
512         }
513
514         vm_vector_2_matrix(&objp->orient, &new_fvec, NULL, &objp->orient.rvec);
515 }
516
517 // -- unused, 08/07/95 -- // --------------------------------------------------------------------------------------------------------------------
518 // -- unused, 08/07/95 -- void ai_turn_randomly(vms_vector *vec_to_player, object *obj, fix rate, int previous_visibility)
519 // -- unused, 08/07/95 -- {
520 // -- unused, 08/07/95 --       vms_vector      curvec;
521 // -- unused, 08/07/95 -- 
522 // -- unused, 08/07/95 -- // -- MK, 06/09/95    //      Random turning looks too stupid, so 1/4 of time, cheat.
523 // -- unused, 08/07/95 -- // -- MK, 06/09/95    if (previous_visibility)
524 // -- unused, 08/07/95 -- // -- MK, 06/09/95            if (d_rand() > 0x7400) {
525 // -- unused, 08/07/95 -- // -- MK, 06/09/95                    ai_turn_towards_vector(vec_to_player, obj, rate);
526 // -- unused, 08/07/95 -- // -- MK, 06/09/95                    return;
527 // -- unused, 08/07/95 -- // -- MK, 06/09/95            }
528 // -- unused, 08/07/95 -- 
529 // -- unused, 08/07/95 --       curvec = obj->mtype.phys_info.rotvel;
530 // -- unused, 08/07/95 -- 
531 // -- unused, 08/07/95 --       curvec.y += F1_0/64;
532 // -- unused, 08/07/95 -- 
533 // -- unused, 08/07/95 --       curvec.x += curvec.y/6;
534 // -- unused, 08/07/95 --       curvec.y += curvec.z/4;
535 // -- unused, 08/07/95 --       curvec.z += curvec.x/10;
536 // -- unused, 08/07/95 -- 
537 // -- unused, 08/07/95 --       if (abs(curvec.x) > F1_0/8) curvec.x /= 4;
538 // -- unused, 08/07/95 --       if (abs(curvec.y) > F1_0/8) curvec.y /= 4;
539 // -- unused, 08/07/95 --       if (abs(curvec.z) > F1_0/8) curvec.z /= 4;
540 // -- unused, 08/07/95 -- 
541 // -- unused, 08/07/95 --       obj->mtype.phys_info.rotvel = curvec;
542 // -- unused, 08/07/95 -- 
543 // -- unused, 08/07/95 -- }
544
545 //      Overall_agitation affects:
546 //              Widens field of view.  Field of view is in range 0..1 (specified in bitmaps.tbl as N/360 degrees).
547 //                      Overall_agitation/128 subtracted from field of view, making robots see wider.
548 //              Increases distance to which robot will search to create path to player by Overall_agitation/8 segments.
549 //              Decreases wait between fire times by Overall_agitation/64 seconds.
550
551
552 // --------------------------------------------------------------------------------------------------------------------
553 //      Returns:
554 //              0               Player is not visible from object, obstruction or something.
555 //              1               Player is visible, but not in field of view.
556 //              2               Player is visible and in field of view.
557 //      Note: Uses Believed_player_pos as player's position for cloak effect.
558 //      NOTE: Will destructively modify *pos if *pos is outside the mine.
559 int player_is_visible_from_object(object *objp, vms_vector *pos, fix field_of_view, vms_vector *vec_to_player)
560 {
561         fix                     dot;
562         fvi_query       fq;
563
564         //      Assume that robot's gun tip is in same segment as robot's center.
565         objp->ctype.ai_info.SUB_FLAGS &= ~SUB_FLAGS_GUNSEG;
566
567         fq.p0                                           = pos;
568         if ((pos->x != objp->pos.x) || (pos->y != objp->pos.y) || (pos->z != objp->pos.z)) {
569                 int     segnum = find_point_seg(pos, objp->segnum);
570                 if (segnum == -1) {
571                         fq.startseg = objp->segnum;
572                         *pos = objp->pos;
573                         mprintf((1, "Object %i, gun is outside mine, moving towards center.\n", objp-Objects));
574                         move_towards_segment_center(objp);
575                 } else {
576                         if (segnum != objp->segnum) {
577                                 // -- mprintf((0, "Warning: Robot's gun tip not in same segment as robot center, frame %i.\n", FrameCount));
578                                 objp->ctype.ai_info.SUB_FLAGS |= SUB_FLAGS_GUNSEG;
579                         }
580                         fq.startseg = segnum;
581                 }
582         } else
583                 fq.startseg                     = objp->segnum;
584         fq.p1                                           = &Believed_player_pos;
585         fq.rad                                  = F1_0/4;
586         fq.thisobjnum                   = objp-Objects;
587         fq.ignore_obj_list      = NULL;
588         fq.flags                                        = FQ_TRANSWALL; // -- Why were we checking objects? | FQ_CHECK_OBJS;            //what about trans walls???
589
590         Hit_type = find_vector_intersection(&fq,&Hit_data);
591
592         Hit_pos = Hit_data.hit_pnt;
593         Hit_seg = Hit_data.hit_seg;
594
595         // -- when we stupidly checked objects -- if ((Hit_type == HIT_NONE) || ((Hit_type == HIT_OBJECT) && (Hit_data.hit_object == Players[Player_num].objnum))) {
596         if (Hit_type == HIT_NONE) {
597                 dot = vm_vec_dot(vec_to_player, &objp->orient.fvec);
598                 // mprintf((0, "Fvec = [%5.2f %5.2f %5.2f], vec_to_player = [%5.2f %5.2f %5.2f], dot = %7.3f\n", f2fl(objp->orient.fvec.x), f2fl(objp->orient.fvec.y), f2fl(objp->orient.fvec.z), f2fl(vec_to_player->x), f2fl(vec_to_player->y), f2fl(vec_to_player->z), f2fl(dot)));
599                 if (dot > field_of_view - (Overall_agitation << 9)) {
600                         return 2;
601                 } else {
602                         return 1;
603                 }
604         } else {
605                 return 0;
606         }
607 }
608
609 // ------------------------------------------------------------------------------------------------------------------
610 //      Return 1 if animates, else return 0
611 int do_silly_animation(object *objp)
612 {
613         int                             objnum = objp-Objects;
614         jointpos                *jp_list;
615         int                             robot_type, gun_num, robot_state, num_joint_positions;
616         polyobj_info    *pobj_info = &objp->rtype.pobj_info;
617         ai_static               *aip = &objp->ctype.ai_info;
618         // ai_local                     *ailp = &Ai_local_info[objnum];
619         int                             num_guns, at_goal;
620         int                             attack_type;
621         int                             flinch_attack_scale = 1;
622
623         robot_type = objp->id;
624         num_guns = Robot_info[robot_type].n_guns;
625         attack_type = Robot_info[robot_type].attack_type;
626
627         if (num_guns == 0) {
628                 // mprintf((0, "Object #%i of type #%i has 0 guns.\n", objp-Objects, robot_type));
629                 return 0;
630         }
631
632         //      This is a hack.  All positions should be based on goal_state, not GOAL_STATE.
633         robot_state = Mike_to_matt_xlate[aip->GOAL_STATE];
634         // previous_robot_state = Mike_to_matt_xlate[aip->CURRENT_STATE];
635
636         if (attack_type) // && ((robot_state == AS_FIRE) || (robot_state == AS_RECOIL)))
637                 flinch_attack_scale = Attack_scale;
638         else if ((robot_state == AS_FLINCH) || (robot_state == AS_RECOIL))
639                 flinch_attack_scale = Flinch_scale;
640
641         at_goal = 1;
642         for (gun_num=0; gun_num <= num_guns; gun_num++) {
643                 int     joint;
644
645                 num_joint_positions = robot_get_anim_state(&jp_list, robot_type, gun_num, robot_state);
646
647                 for (joint=0; joint<num_joint_positions; joint++) {
648                         fix                     delta_angle, delta_2;
649                         int                     jointnum = jp_list[joint].jointnum;
650                         vms_angvec      *jp = &jp_list[joint].angles;
651                         vms_angvec      *pobjp = &pobj_info->anim_angles[jointnum];
652
653                         if (jointnum >= Polygon_models[objp->rtype.pobj_info.model_num].n_models) {
654                                 Int3();         // Contact Mike: incompatible data, illegal jointnum, problem in pof file?
655                                 continue;
656                         }
657                         if (jp->p != pobjp->p) {
658                                 if (gun_num == 0)
659                                         at_goal = 0;
660                                 Ai_local_info[objnum].goal_angles[jointnum].p = jp->p;
661
662                                 delta_angle = jp->p - pobjp->p;
663                                 if (delta_angle >= F1_0/2)
664                                         delta_2 = -ANIM_RATE;
665                                 else if (delta_angle >= 0)
666                                         delta_2 = ANIM_RATE;
667                                 else if (delta_angle >= -F1_0/2)
668                                         delta_2 = -ANIM_RATE;
669                                 else
670                                         delta_2 = ANIM_RATE;
671
672                                 if (flinch_attack_scale != 1)
673                                         delta_2 *= flinch_attack_scale;
674
675                                 Ai_local_info[objnum].delta_angles[jointnum].p = delta_2/DELTA_ANG_SCALE;               // complete revolutions per second
676                         }
677
678                         if (jp->b != pobjp->b) {
679                                 if (gun_num == 0)
680                                         at_goal = 0;
681                                 Ai_local_info[objnum].goal_angles[jointnum].b = jp->b;
682
683                                 delta_angle = jp->b - pobjp->b;
684                                 if (delta_angle >= F1_0/2)
685                                         delta_2 = -ANIM_RATE;
686                                 else if (delta_angle >= 0)
687                                         delta_2 = ANIM_RATE;
688                                 else if (delta_angle >= -F1_0/2)
689                                         delta_2 = -ANIM_RATE;
690                                 else
691                                         delta_2 = ANIM_RATE;
692
693                                 if (flinch_attack_scale != 1)
694                                         delta_2 *= flinch_attack_scale;
695
696                                 Ai_local_info[objnum].delta_angles[jointnum].b = delta_2/DELTA_ANG_SCALE;               // complete revolutions per second
697                         }
698
699                         if (jp->h != pobjp->h) {
700                                 if (gun_num == 0)
701                                         at_goal = 0;
702                                 Ai_local_info[objnum].goal_angles[jointnum].h = jp->h;
703
704                                 delta_angle = jp->h - pobjp->h;
705                                 if (delta_angle >= F1_0/2)
706                                         delta_2 = -ANIM_RATE;
707                                 else if (delta_angle >= 0)
708                                         delta_2 = ANIM_RATE;
709                                 else if (delta_angle >= -F1_0/2)
710                                         delta_2 = -ANIM_RATE;
711                                 else
712                                         delta_2 = ANIM_RATE;
713
714                                 if (flinch_attack_scale != 1)
715                                         delta_2 *= flinch_attack_scale;
716
717                                 Ai_local_info[objnum].delta_angles[jointnum].h = delta_2/DELTA_ANG_SCALE;               // complete revolutions per second
718                         }
719                 }
720
721                 if (at_goal) {
722                         //ai_static     *aip = &objp->ctype.ai_info;
723                         ai_local                *ailp = &Ai_local_info[objp-Objects];
724                         ailp->achieved_state[gun_num] = ailp->goal_state[gun_num];
725                         if (ailp->achieved_state[gun_num] == AIS_RECO)
726                                 ailp->goal_state[gun_num] = AIS_FIRE;
727
728                         if (ailp->achieved_state[gun_num] == AIS_FLIN)
729                                 ailp->goal_state[gun_num] = AIS_LOCK;
730
731                 }
732         }
733
734         if (at_goal == 1) //num_guns)
735                 aip->CURRENT_STATE = aip->GOAL_STATE;
736
737         return 1;
738 }
739
740 //      ------------------------------------------------------------------------------------------
741 //      Move all sub-objects in an object towards their goals.
742 //      Current orientation of object is at:    pobj_info.anim_angles
743 //      Goal orientation of object is at:               ai_info.goal_angles
744 //      Delta orientation of object is at:              ai_info.delta_angles
745 void ai_frame_animation(object *objp)
746 {
747         int     objnum = objp-Objects;
748         int     joint;
749         int     num_joints;
750
751         num_joints = Polygon_models[objp->rtype.pobj_info.model_num].n_models;
752
753         for (joint=1; joint<num_joints; joint++) {
754                 fix                     delta_to_goal;
755                 fix                     scaled_delta_angle;
756                 vms_angvec      *curangp = &objp->rtype.pobj_info.anim_angles[joint];
757                 vms_angvec      *goalangp = &Ai_local_info[objnum].goal_angles[joint];
758                 vms_angvec      *deltaangp = &Ai_local_info[objnum].delta_angles[joint];
759
760                 delta_to_goal = goalangp->p - curangp->p;
761                 if (delta_to_goal > 32767)
762                         delta_to_goal = delta_to_goal - 65536;
763                 else if (delta_to_goal < -32767)
764                         delta_to_goal = 65536 + delta_to_goal;
765
766                 if (delta_to_goal) {
767                         scaled_delta_angle = fixmul(deltaangp->p, FrameTime) * DELTA_ANG_SCALE;
768                         curangp->p += scaled_delta_angle;
769                         if (abs(delta_to_goal) < abs(scaled_delta_angle))
770                                 curangp->p = goalangp->p;
771                 }
772
773                 delta_to_goal = goalangp->b - curangp->b;
774                 if (delta_to_goal > 32767)
775                         delta_to_goal = delta_to_goal - 65536;
776                 else if (delta_to_goal < -32767)
777                         delta_to_goal = 65536 + delta_to_goal;
778
779                 if (delta_to_goal) {
780                         scaled_delta_angle = fixmul(deltaangp->b, FrameTime) * DELTA_ANG_SCALE;
781                         curangp->b += scaled_delta_angle;
782                         if (abs(delta_to_goal) < abs(scaled_delta_angle))
783                                 curangp->b = goalangp->b;
784                 }
785
786                 delta_to_goal = goalangp->h - curangp->h;
787                 if (delta_to_goal > 32767)
788                         delta_to_goal = delta_to_goal - 65536;
789                 else if (delta_to_goal < -32767)
790                         delta_to_goal = 65536 + delta_to_goal;
791
792                 if (delta_to_goal) {
793                         scaled_delta_angle = fixmul(deltaangp->h, FrameTime) * DELTA_ANG_SCALE;
794                         curangp->h += scaled_delta_angle;
795                         if (abs(delta_to_goal) < abs(scaled_delta_angle))
796                                 curangp->h = goalangp->h;
797                 }
798
799         }
800
801 }
802
803 // ----------------------------------------------------------------------------------
804 void set_next_fire_time(object *objp, ai_local *ailp, robot_info *robptr, int gun_num)
805 {
806         //      For guys in snipe mode, they have a 50% shot of getting this shot in free.
807         if ((gun_num != 0) || (robptr->weapon_type2 == -1))
808                 if ((objp->ctype.ai_info.behavior != AIB_SNIPE) || (d_rand() > 16384))
809                         ailp->rapidfire_count++;
810
811         //      Old way, 10/15/95: Continuous rapidfire if rapidfire_count set.
812 // --   if (((robptr->weapon_type2 == -1) || (gun_num != 0)) && (ailp->rapidfire_count < robptr->rapidfire_count[Difficulty_level])) {
813 // --           ailp->next_fire = min(F1_0/8, robptr->firing_wait[Difficulty_level]/2);
814 // --   } else {
815 // --           if ((robptr->weapon_type2 == -1) || (gun_num != 0)) {
816 // --                   ailp->rapidfire_count = 0;
817 // --                   ailp->next_fire = robptr->firing_wait[Difficulty_level];
818 // --           } else
819 // --                   ailp->next_fire2 = robptr->firing_wait2[Difficulty_level];
820 // --   }
821
822         if (((gun_num != 0) || (robptr->weapon_type2 == -1)) && (ailp->rapidfire_count < robptr->rapidfire_count[Difficulty_level])) {
823                 ailp->next_fire = min(F1_0/8, robptr->firing_wait[Difficulty_level]/2);
824         } else {
825                 if ((robptr->weapon_type2 == -1) || (gun_num != 0)) {
826                         ailp->next_fire = robptr->firing_wait[Difficulty_level];
827                         if (ailp->rapidfire_count >= robptr->rapidfire_count[Difficulty_level])
828                                 ailp->rapidfire_count = 0;
829                 } else
830                         ailp->next_fire2 = robptr->firing_wait2[Difficulty_level];
831         }
832 }
833
834 // ----------------------------------------------------------------------------------
835 //      When some robots collide with the player, they attack.
836 //      If player is cloaked, then robot probably didn't actually collide, deal with that here.
837 void do_ai_robot_hit_attack(object *robot, object *playerobj, vms_vector *collision_point)
838 {
839         ai_local                *ailp = &Ai_local_info[robot-Objects];
840         robot_info *robptr = &Robot_info[robot->id];
841
842 //#ifndef NDEBUG
843         if (!Robot_firing_enabled)
844                 return;
845 //#endif
846
847         //      If player is dead, stop firing.
848         if (Objects[Players[Player_num].objnum].type == OBJ_GHOST)
849                 return;
850
851         if (robptr->attack_type == 1) {
852                 if (ailp->next_fire <= 0) {
853                         if (!(Players[Player_num].flags & PLAYER_FLAGS_CLOAKED))
854                                 if (vm_vec_dist_quick(&ConsoleObject->pos, &robot->pos) < robot->size + ConsoleObject->size + F1_0*2) {
855                                         collide_player_and_nasty_robot( playerobj, robot, collision_point );
856                                         if (robptr->energy_drain && Players[Player_num].energy) {
857                                                 Players[Player_num].energy -= robptr->energy_drain * F1_0;
858                                                 if (Players[Player_num].energy < 0)
859                                                         Players[Player_num].energy = 0;
860                                                 // -- unused, use claw_sound in bitmaps.tbl -- digi_link_sound_to_pos( SOUND_ROBOT_SUCKED_PLAYER, playerobj->segnum, 0, collision_point, 0, F1_0 );
861                                         }
862                                 }
863
864                         robot->ctype.ai_info.GOAL_STATE = AIS_RECO;
865                         set_next_fire_time(robot, ailp, robptr, 1);     //      1 = gun_num: 0 is special (uses next_fire2)
866                 }
867         }
868
869 }
870
871 #ifndef _OBJECT_H
872 extern int Player_exploded;
873 #endif
874
875 #define FIRE_K  8               //      Controls average accuracy of robot firing.  Smaller numbers make firing worse.  Being power of 2 doesn't matter.
876
877 // ====================================================================================================================
878
879 #define MIN_LEAD_SPEED          (F1_0*4)
880 #define MAX_LEAD_DISTANCE       (F1_0*200)
881 #define LEAD_RANGE                      (F1_0/2)
882
883 // --------------------------------------------------------------------------------------------------------------------
884 //      Computes point at which projectile fired by robot can hit player given positions, player vel, elapsed time
885 fix compute_lead_component(fix player_pos, fix robot_pos, fix player_vel, fix elapsed_time)
886 {
887         return fixdiv(player_pos - robot_pos, elapsed_time) + player_vel;
888 }
889
890 // --------------------------------------------------------------------------------------------------------------------
891 //      Lead the player, returning point to fire at in fire_point.
892 //      Rules:
893 //              Player not cloaked
894 //              Player must be moving at a speed >= MIN_LEAD_SPEED
895 //              Player not farther away than MAX_LEAD_DISTANCE
896 //              dot(vector_to_player, player_direction) must be in -LEAD_RANGE..LEAD_RANGE
897 //              if firing a matter weapon, less leading, based on skill level.
898 int lead_player(object *objp, vms_vector *fire_point, vms_vector *believed_player_pos, int gun_num, vms_vector *fire_vec)
899 {
900         fix                     dot, player_speed, dist_to_player, max_weapon_speed, projected_time;
901         vms_vector      player_movement_dir, vec_to_player;
902         int                     weapon_type;
903         weapon_info     *wptr;
904         robot_info      *robptr;
905
906         if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED)
907                 return 0;
908
909         player_movement_dir = ConsoleObject->mtype.phys_info.velocity;
910         player_speed = vm_vec_normalize_quick(&player_movement_dir);
911
912         if (player_speed < MIN_LEAD_SPEED)
913                 return 0;
914
915         vm_vec_sub(&vec_to_player, believed_player_pos, fire_point);
916         dist_to_player = vm_vec_normalize_quick(&vec_to_player);
917         if (dist_to_player > MAX_LEAD_DISTANCE)
918                 return 0;
919
920         dot = vm_vec_dot(&vec_to_player, &player_movement_dir);
921
922         if ((dot < -LEAD_RANGE) || (dot > LEAD_RANGE))
923                 return 0;
924
925         //      Looks like it might be worth trying to lead the player.
926         robptr = &Robot_info[objp->id];
927         weapon_type = robptr->weapon_type;
928         if (robptr->weapon_type2 != -1)
929                 if (gun_num == 0)
930                         weapon_type = robptr->weapon_type2;
931
932         wptr = &Weapon_info[weapon_type];
933         max_weapon_speed = wptr->speed[Difficulty_level];
934         if (max_weapon_speed < F1_0)
935                 return 0;
936
937         //      Matter weapons:
938         //      At Rookie or Trainee, don't lead at all.
939         //      At higher skill levels, don't lead as well.  Accomplish this by screwing up max_weapon_speed.
940         if (wptr->matter)
941         {
942                 if (Difficulty_level <= 1)
943                         return 0;
944                 else
945                         max_weapon_speed *= (NDL-Difficulty_level);
946         }
947
948         projected_time = fixdiv(dist_to_player, max_weapon_speed);
949
950         fire_vec->x = compute_lead_component(believed_player_pos->x, fire_point->x, ConsoleObject->mtype.phys_info.velocity.x, projected_time);
951         fire_vec->y = compute_lead_component(believed_player_pos->y, fire_point->y, ConsoleObject->mtype.phys_info.velocity.y, projected_time);
952         fire_vec->z = compute_lead_component(believed_player_pos->z, fire_point->z, ConsoleObject->mtype.phys_info.velocity.z, projected_time);
953
954         vm_vec_normalize_quick(fire_vec);
955
956         Assert(vm_vec_dot(fire_vec, &objp->orient.fvec) < 3*F1_0/2);
957
958         //      Make sure not firing at especially strange angle.  If so, try to correct.  If still bad, give up after one try.
959         if (vm_vec_dot(fire_vec, &objp->orient.fvec) < F1_0/2) {
960                 vm_vec_add2(fire_vec, &vec_to_player);
961                 vm_vec_scale(fire_vec, F1_0/2);
962                 if (vm_vec_dot(fire_vec, &objp->orient.fvec) < F1_0/2) {
963                         return 0;
964                 }
965         }
966
967         return 1;
968 }
969
970 // --------------------------------------------------------------------------------------------------------------------
971 //      Note: Parameter vec_to_player is only passed now because guns which aren't on the forward vector from the
972 //      center of the robot will not fire right at the player.  We need to aim the guns at the player.  Barring that, we cheat.
973 //      When this routine is complete, the parameter vec_to_player should not be necessary.
974 void ai_fire_laser_at_player(object *obj, vms_vector *fire_point, int gun_num, vms_vector *believed_player_pos)
975 {
976         int                     objnum = obj-Objects;
977         ai_local                *ailp = &Ai_local_info[objnum];
978         robot_info      *robptr = &Robot_info[obj->id];
979         vms_vector      fire_vec;
980         vms_vector      bpp_diff;
981         int                     weapon_type;
982         fix                     aim, dot;
983         int                     count;
984
985         Assert(robptr->attack_type == 0);       //      We should never be coming here for the green guy, as he has no laser!
986
987         //      If this robot is only awake because a camera woke it up, don't fire.
988         if (obj->ctype.ai_info.SUB_FLAGS & SUB_FLAGS_CAMERA_AWAKE)
989                 return;
990
991         if (!Robot_firing_enabled)
992                 return;
993
994         if (obj->control_type == CT_MORPH)
995                 return;
996
997         //      If player is exploded, stop firing.
998         if (Player_exploded)
999                 return;
1000
1001         if (obj->ctype.ai_info.dying_start_time)
1002                 return;         //      No firing while in death roll.
1003
1004         //      Don't let the boss fire while in death roll.  Sorry, this is the easiest way to do this.
1005         //      If you try to key the boss off obj->ctype.ai_info.dying_start_time, it will hose the endlevel stuff.
1006         if (Boss_dying_start_time & Robot_info[obj->id].boss_flag)
1007                 return;
1008
1009         //      If player is cloaked, maybe don't fire based on how long cloaked and randomness.
1010         if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED) {
1011                 fix     cloak_time = Ai_cloak_info[objnum % MAX_AI_CLOAK_INFO].last_time;
1012
1013                 if (GameTime - cloak_time > CLOAK_TIME_MAX/4)
1014                         if (d_rand() > fixdiv(GameTime - cloak_time, CLOAK_TIME_MAX)/2) {
1015                                 set_next_fire_time(obj, ailp, robptr, gun_num);
1016                                 return;
1017                         }
1018         }
1019
1020         //      Handle problem of a robot firing through a wall because its gun tip is on the other
1021         //      side of the wall than the robot's center.  For speed reasons, we normally only compute
1022         //      the vector from the gun point to the player.  But we need to know whether the gun point
1023         //      is separated from the robot's center by a wall.  If so, don't fire!
1024         if (obj->ctype.ai_info.SUB_FLAGS & SUB_FLAGS_GUNSEG) {
1025                 //      Well, the gun point is in a different segment than the robot's center.
1026                 //      This is almost always ok, but it is not ok if something solid is in between.
1027                 int     conn_side;
1028                 int     gun_segnum = find_point_seg(fire_point, obj->segnum);
1029
1030                 //      See if these segments are connected, which should almost always be the case.
1031                 conn_side = find_connect_side(&Segments[gun_segnum], &Segments[obj->segnum]);
1032                 if (conn_side != -1) {
1033                         //      They are connected via conn_side in segment obj->segnum.
1034                         //      See if they are unobstructed.
1035                         if (!(WALL_IS_DOORWAY(&Segments[obj->segnum], conn_side) & WID_FLY_FLAG)) {
1036                                 //      Can't fly through, so don't let this bot fire through!
1037                                 return;
1038                         }
1039                 } else {
1040                         //      Well, they are not directly connected, so use find_vector_intersection to see if they are unobstructed.
1041                         fvi_query       fq;
1042                         fvi_info                hit_data;
1043                         int                     fate;
1044
1045                         fq.startseg                             = obj->segnum;
1046                         fq.p0                                           = &obj->pos;
1047                         fq.p1                                           = fire_point;
1048                         fq.rad                                  = 0;
1049                         fq.thisobjnum                   = obj-Objects;
1050                         fq.ignore_obj_list      = NULL;
1051                         fq.flags                                        = FQ_TRANSWALL;
1052
1053                         fate = find_vector_intersection(&fq, &hit_data);
1054                         if (fate != HIT_NONE) {
1055                                 Int3();         //      This bot's gun is poking through a wall, so don't fire.
1056                                 move_towards_segment_center(obj);               //      And decrease chances it will happen again.
1057                                 return;
1058                         }
1059                 }
1060         }
1061
1062         // -- mprintf((0, "Firing from gun #%i at time = %7.3f\n", gun_num, f2fl(GameTime)));
1063
1064         //      Set position to fire at based on difficulty level and robot's aiming ability
1065         aim = FIRE_K*F1_0 - (FIRE_K-1)*(robptr->aim << 8);      //      F1_0 in bitmaps.tbl = same as used to be.  Worst is 50% more error.
1066
1067         //      Robots aim more poorly during seismic disturbance.
1068         if (Seismic_tremor_magnitude) {
1069                 fix     temp;
1070
1071                 temp = F1_0 - abs(Seismic_tremor_magnitude);
1072                 if (temp < F1_0/2)
1073                         temp = F1_0/2;
1074
1075                 aim = fixmul(aim, temp);
1076         }
1077
1078         //      Lead the player half the time.
1079         //      Note that when leading the player, aim is perfect.  This is probably acceptable since leading is so hacked in.
1080         //      Problem is all robots will lead equally badly.
1081         if (d_rand() < 16384) {
1082                 if (lead_player(obj, fire_point, believed_player_pos, gun_num, &fire_vec))              //      Stuff direction to fire at in fire_point.
1083                         goto player_led;
1084         }
1085
1086         dot = 0;
1087         count = 0;                      //      Don't want to sit in this loop forever...
1088         while ((count < 4) && (dot < F1_0/4)) {
1089                 bpp_diff.x = believed_player_pos->x + fixmul((d_rand()-16384) * (NDL-Difficulty_level-1) * 4, aim);
1090                 bpp_diff.y = believed_player_pos->y + fixmul((d_rand()-16384) * (NDL-Difficulty_level-1) * 4, aim);
1091                 bpp_diff.z = believed_player_pos->z + fixmul((d_rand()-16384) * (NDL-Difficulty_level-1) * 4, aim);
1092
1093                 vm_vec_normalized_dir_quick(&fire_vec, &bpp_diff, fire_point);
1094                 dot = vm_vec_dot(&obj->orient.fvec, &fire_vec);
1095                 count++;
1096         }
1097 player_led: ;
1098
1099         weapon_type = robptr->weapon_type;
1100         if (robptr->weapon_type2 != -1)
1101                 if (gun_num == 0)
1102                         weapon_type = robptr->weapon_type2;
1103
1104         Laser_create_new_easy( &fire_vec, fire_point, obj-Objects, weapon_type, 1);
1105
1106 #ifdef NETWORK
1107         if (Game_mode & GM_MULTI) {
1108                 ai_multi_send_robot_position(objnum, -1);
1109                 multi_send_robot_fire(objnum, obj->ctype.ai_info.CURRENT_GUN, &fire_vec);
1110         }
1111 #endif
1112
1113         create_awareness_event(obj, PA_NEARBY_ROBOT_FIRED);
1114
1115         set_next_fire_time(obj, ailp, robptr, gun_num);
1116
1117 }
1118
1119 // --------------------------------------------------------------------------------------------------------------------
1120 //      vec_goal must be normalized, or close to it.
1121 //      if dot_based set, then speed is based on direction of movement relative to heading
1122 void move_towards_vector(object *objp, vms_vector *vec_goal, int dot_based)
1123 {
1124         physics_info    *pptr = &objp->mtype.phys_info;
1125         fix                             speed, dot, max_speed;
1126         robot_info              *robptr = &Robot_info[objp->id];
1127         vms_vector              vel;
1128
1129         //      Trying to move towards player.  If forward vector much different than velocity vector,
1130         //      bash velocity vector twice as much towards player as usual.
1131
1132         vel = pptr->velocity;
1133         vm_vec_normalize_quick(&vel);
1134         dot = vm_vec_dot(&vel, &objp->orient.fvec);
1135
1136         if (robptr->thief)
1137                 dot = (F1_0+dot)/2;
1138
1139         if (dot_based && (dot < 3*F1_0/4)) {
1140                 //      This funny code is supposed to slow down the robot and move his velocity towards his direction
1141                 //      more quickly than the general code
1142                 pptr->velocity.x = pptr->velocity.x/2 + fixmul(vec_goal->x, FrameTime*32);
1143                 pptr->velocity.y = pptr->velocity.y/2 + fixmul(vec_goal->y, FrameTime*32);
1144                 pptr->velocity.z = pptr->velocity.z/2 + fixmul(vec_goal->z, FrameTime*32);
1145         } else {
1146                 pptr->velocity.x += fixmul(vec_goal->x, FrameTime*64) * (Difficulty_level+5)/4;
1147                 pptr->velocity.y += fixmul(vec_goal->y, FrameTime*64) * (Difficulty_level+5)/4;
1148                 pptr->velocity.z += fixmul(vec_goal->z, FrameTime*64) * (Difficulty_level+5)/4;
1149         }
1150
1151         speed = vm_vec_mag_quick(&pptr->velocity);
1152         max_speed = robptr->max_speed[Difficulty_level];
1153
1154         //      Green guy attacks twice as fast as he moves away.
1155         if ((robptr->attack_type == 1) || robptr->thief || robptr->kamikaze)
1156                 max_speed *= 2;
1157
1158         if (speed > max_speed) {
1159                 pptr->velocity.x = (pptr->velocity.x*3)/4;
1160                 pptr->velocity.y = (pptr->velocity.y*3)/4;
1161                 pptr->velocity.z = (pptr->velocity.z*3)/4;
1162         }
1163 }
1164
1165 // --------------------------------------------------------------------------------------------------------------------
1166 void move_towards_player(object *objp, vms_vector *vec_to_player)
1167 //      vec_to_player must be normalized, or close to it.
1168 {
1169         move_towards_vector(objp, vec_to_player, 1);
1170 }
1171
1172 // --------------------------------------------------------------------------------------------------------------------
1173 //      I am ashamed of this: fast_flag == -1 means normal slide about.  fast_flag = 0 means no evasion.
1174 void move_around_player(object *objp, vms_vector *vec_to_player, int fast_flag)
1175 {
1176         physics_info    *pptr = &objp->mtype.phys_info;
1177         fix                             speed;
1178         robot_info              *robptr = &Robot_info[objp->id];
1179         int                             objnum = objp-Objects;
1180         int                             dir;
1181         int                             dir_change;
1182         fix                             ft;
1183         vms_vector              evade_vector;
1184         int                             count=0;
1185
1186         if (fast_flag == 0)
1187                 return;
1188
1189         dir_change = 48;
1190         ft = FrameTime;
1191         if (ft < F1_0/32) {
1192                 dir_change *= 8;
1193                 count += 3;
1194         } else
1195                 while (ft < F1_0/4) {
1196                         dir_change *= 2;
1197                         ft *= 2;
1198                         count++;
1199                 }
1200
1201         dir = (FrameCount + (count+1) * (objnum*8 + objnum*4 + objnum)) & dir_change;
1202         dir >>= (4+count);
1203
1204         Assert((dir >= 0) && (dir <= 3));
1205
1206         switch (dir) {
1207                 case 0:
1208                         evade_vector.x = fixmul(vec_to_player->z, FrameTime*32);
1209                         evade_vector.y = fixmul(vec_to_player->y, FrameTime*32);
1210                         evade_vector.z = fixmul(-vec_to_player->x, FrameTime*32);
1211                         break;
1212                 case 1:
1213                         evade_vector.x = fixmul(-vec_to_player->z, FrameTime*32);
1214                         evade_vector.y = fixmul(vec_to_player->y, FrameTime*32);
1215                         evade_vector.z = fixmul(vec_to_player->x, FrameTime*32);
1216                         break;
1217                 case 2:
1218                         evade_vector.x = fixmul(-vec_to_player->y, FrameTime*32);
1219                         evade_vector.y = fixmul(vec_to_player->x, FrameTime*32);
1220                         evade_vector.z = fixmul(vec_to_player->z, FrameTime*32);
1221                         break;
1222                 case 3:
1223                         evade_vector.x = fixmul(vec_to_player->y, FrameTime*32);
1224                         evade_vector.y = fixmul(-vec_to_player->x, FrameTime*32);
1225                         evade_vector.z = fixmul(vec_to_player->z, FrameTime*32);
1226                         break;
1227                 default:
1228                         Error("Function move_around_player: Bad case.");
1229         }
1230
1231         //      Note: -1 means normal circling about the player.  > 0 means fast evasion.
1232         if (fast_flag > 0) {
1233                 fix     dot;
1234
1235                 //      Only take evasive action if looking at player.
1236                 //      Evasion speed is scaled by percentage of shields left so wounded robots evade less effectively.
1237
1238                 dot = vm_vec_dot(vec_to_player, &objp->orient.fvec);
1239                 if ((dot > robptr->field_of_view[Difficulty_level]) && !(ConsoleObject->flags & PLAYER_FLAGS_CLOAKED)) {
1240                         fix     damage_scale;
1241
1242                         if (robptr->strength)
1243                                 damage_scale = fixdiv(objp->shields, robptr->strength);
1244                         else
1245                                 damage_scale = F1_0;
1246                         if (damage_scale > F1_0)
1247                                 damage_scale = F1_0;            //      Just in case...
1248                         else if (damage_scale < 0)
1249                                 damage_scale = 0;                       //      Just in case...
1250
1251                         vm_vec_scale(&evade_vector, i2f(fast_flag) + damage_scale);
1252                 }
1253         }
1254
1255         pptr->velocity.x += evade_vector.x;
1256         pptr->velocity.y += evade_vector.y;
1257         pptr->velocity.z += evade_vector.z;
1258
1259         speed = vm_vec_mag_quick(&pptr->velocity);
1260         if ((objp-Objects != 1) && (speed > robptr->max_speed[Difficulty_level])) {
1261                 pptr->velocity.x = (pptr->velocity.x*3)/4;
1262                 pptr->velocity.y = (pptr->velocity.y*3)/4;
1263                 pptr->velocity.z = (pptr->velocity.z*3)/4;
1264         }
1265 }
1266
1267 // --------------------------------------------------------------------------------------------------------------------
1268 void move_away_from_player(object *objp, vms_vector *vec_to_player, int attack_type)
1269 {
1270         fix                             speed;
1271         physics_info    *pptr = &objp->mtype.phys_info;
1272         robot_info              *robptr = &Robot_info[objp->id];
1273         int                             objref;
1274
1275         pptr->velocity.x -= fixmul(vec_to_player->x, FrameTime*16);
1276         pptr->velocity.y -= fixmul(vec_to_player->y, FrameTime*16);
1277         pptr->velocity.z -= fixmul(vec_to_player->z, FrameTime*16);
1278
1279         if (attack_type) {
1280                 //      Get value in 0..3 to choose evasion direction.
1281                 objref = ((objp-Objects) ^ ((FrameCount + 3*(objp-Objects)) >> 5)) & 3;
1282
1283                 switch (objref) {
1284                         case 0: vm_vec_scale_add2(&pptr->velocity, &objp->orient.uvec, FrameTime << 5); break;
1285                         case 1: vm_vec_scale_add2(&pptr->velocity, &objp->orient.uvec, -FrameTime << 5);        break;
1286                         case 2: vm_vec_scale_add2(&pptr->velocity, &objp->orient.rvec, FrameTime << 5); break;
1287                         case 3: vm_vec_scale_add2(&pptr->velocity, &objp->orient.rvec, -FrameTime << 5);        break;
1288                         default:        Int3(); //      Impossible, bogus value on objref, must be in 0..3
1289                 }
1290         }
1291
1292
1293         speed = vm_vec_mag_quick(&pptr->velocity);
1294
1295         if (speed > robptr->max_speed[Difficulty_level]) {
1296                 pptr->velocity.x = (pptr->velocity.x*3)/4;
1297                 pptr->velocity.y = (pptr->velocity.y*3)/4;
1298                 pptr->velocity.z = (pptr->velocity.z*3)/4;
1299         }
1300
1301 }
1302
1303 // --------------------------------------------------------------------------------------------------------------------
1304 //      Move towards, away_from or around player.
1305 //      Also deals with evasion.
1306 //      If the flag evade_only is set, then only allowed to evade, not allowed to move otherwise (must have mode == AIM_STILL).
1307 void ai_move_relative_to_player(object *objp, ai_local *ailp, fix dist_to_player, vms_vector *vec_to_player, fix circle_distance, int evade_only, int player_visibility)
1308 {
1309         object          *dobjp;
1310         robot_info      *robptr = &Robot_info[objp->id];
1311
1312         Assert(player_visibility != -1);
1313
1314         //      See if should take avoidance.
1315
1316         // New way, green guys don't evade:     if ((robptr->attack_type == 0) && (objp->ctype.ai_info.danger_laser_num != -1)) {
1317         if (objp->ctype.ai_info.danger_laser_num != -1) {
1318                 dobjp = &Objects[objp->ctype.ai_info.danger_laser_num];
1319
1320                 if ((dobjp->type == OBJ_WEAPON) && (dobjp->signature == objp->ctype.ai_info.danger_laser_signature)) {
1321                         fix                     dot, dist_to_laser, field_of_view;
1322                         vms_vector      vec_to_laser, laser_fvec;
1323
1324                         field_of_view = Robot_info[objp->id].field_of_view[Difficulty_level];
1325
1326                         vm_vec_sub(&vec_to_laser, &dobjp->pos, &objp->pos);
1327                         dist_to_laser = vm_vec_normalize_quick(&vec_to_laser);
1328                         dot = vm_vec_dot(&vec_to_laser, &objp->orient.fvec);
1329
1330                         if ((dot > field_of_view) || (robptr->companion)) {
1331                                 fix                     laser_robot_dot;
1332                                 vms_vector      laser_vec_to_robot;
1333
1334                                 //      The laser is seen by the robot, see if it might hit the robot.
1335                                 //      Get the laser's direction.  If it's a polyobj, it can be gotten cheaply from the orientation matrix.
1336                                 if (dobjp->render_type == RT_POLYOBJ)
1337                                         laser_fvec = dobjp->orient.fvec;
1338                                 else {          //      Not a polyobj, get velocity and normalize.
1339                                         laser_fvec = dobjp->mtype.phys_info.velocity;   //dobjp->orient.fvec;
1340                                         vm_vec_normalize_quick(&laser_fvec);
1341                                 }
1342                                 vm_vec_sub(&laser_vec_to_robot, &objp->pos, &dobjp->pos);
1343                                 vm_vec_normalize_quick(&laser_vec_to_robot);
1344                                 laser_robot_dot = vm_vec_dot(&laser_fvec, &laser_vec_to_robot);
1345
1346                                 if ((laser_robot_dot > F1_0*7/8) && (dist_to_laser < F1_0*80)) {
1347                                         int     evade_speed;
1348
1349                                         ai_evaded = 1;
1350                                         evade_speed = Robot_info[objp->id].evade_speed[Difficulty_level];
1351
1352                                         move_around_player(objp, vec_to_player, evade_speed);
1353                                 }
1354                         }
1355                         return;
1356                 }
1357         }
1358
1359         //      If only allowed to do evade code, then done.
1360         //      Hmm, perhaps brilliant insight.  If want claw-type guys to keep coming, don't return here after evasion.
1361         if ((!robptr->attack_type) && (!robptr->thief) && evade_only)
1362                 return;
1363
1364         //      If we fall out of above, then no object to be avoided.
1365         objp->ctype.ai_info.danger_laser_num = -1;
1366
1367         //      Green guy selects move around/towards/away based on firing time, not distance.
1368         if (robptr->attack_type == 1) {
1369                 if (((ailp->next_fire > robptr->firing_wait[Difficulty_level]/4) && (dist_to_player < F1_0*30)) || Player_is_dead) {
1370                         //      1/4 of time, move around player, 3/4 of time, move away from player
1371                         if (d_rand() < 8192) {
1372                                 move_around_player(objp, vec_to_player, -1);
1373                         } else {
1374                                 move_away_from_player(objp, vec_to_player, 1);
1375                         }
1376                 } else {
1377                         move_towards_player(objp, vec_to_player);
1378                 }
1379         } else if (robptr->thief) {
1380                 move_towards_player(objp, vec_to_player);
1381         } else {
1382                 int     objval = ((objp-Objects) & 0x0f) ^ 0x0a;
1383
1384                 //      Changes here by MK, 12/29/95.  Trying to get rid of endless circling around bots in a large room.
1385                 if (robptr->kamikaze) {
1386                         move_towards_player(objp, vec_to_player);
1387                 } else if (dist_to_player < circle_distance)
1388                         move_away_from_player(objp, vec_to_player, 0);
1389                 else if ((dist_to_player < (3+objval)*circle_distance/2) && (ailp->next_fire > -F1_0)) {
1390                         move_around_player(objp, vec_to_player, -1);
1391                 } else {
1392                         if ((-ailp->next_fire > F1_0 + (objval << 12)) && player_visibility) {
1393                                 //      Usually move away, but sometimes move around player.
1394                                 if ((((GameTime >> 18) & 0x0f) ^ objval) > 4) {
1395                                         move_away_from_player(objp, vec_to_player, 0);
1396                                 } else {
1397                                         move_around_player(objp, vec_to_player, -1);
1398                                 }
1399                         } else
1400                                 move_towards_player(objp, vec_to_player);
1401                 }
1402         }
1403
1404 }
1405
1406 // --------------------------------------------------------------------------------------------------------------------
1407 //      Compute a somewhat random, normalized vector.
1408 void make_random_vector(vms_vector *vec)
1409 {
1410         vec->x = (d_rand() - 16384) | 1;        // make sure we don't create null vector
1411         vec->y = d_rand() - 16384;
1412         vec->z = d_rand() - 16384;
1413
1414         vm_vec_normalize_quick(vec);
1415 }
1416
1417 #ifndef NDEBUG
1418 void mprintf_animation_info(object *objp)
1419 {
1420         ai_static       *aip = &objp->ctype.ai_info;
1421         ai_local                *ailp = &Ai_local_info[objp-Objects];
1422
1423         if (!Ai_info_enabled)
1424                 return;
1425
1426         mprintf((0, "Goal = "));
1427
1428         switch (aip->GOAL_STATE) {
1429                 case AIS_NONE:  mprintf((0, "NONE "));  break;
1430                 case AIS_REST:  mprintf((0, "REST "));  break;
1431                 case AIS_SRCH:  mprintf((0, "SRCH "));  break;
1432                 case AIS_LOCK:  mprintf((0, "LOCK "));  break;
1433                 case AIS_FLIN:  mprintf((0, "FLIN "));  break;
1434                 case AIS_FIRE:  mprintf((0, "FIRE "));  break;
1435                 case AIS_RECO:  mprintf((0, "RECO "));  break;
1436                 case AIS_ERR_:  mprintf((0, "ERR_ "));  break;
1437         
1438         }
1439
1440         mprintf((0, " Cur = "));
1441
1442         switch (aip->CURRENT_STATE) {
1443                 case AIS_NONE:  mprintf((0, "NONE "));  break;
1444                 case AIS_REST:  mprintf((0, "REST "));  break;
1445                 case AIS_SRCH:  mprintf((0, "SRCH "));  break;
1446                 case AIS_LOCK:  mprintf((0, "LOCK "));  break;
1447                 case AIS_FLIN:  mprintf((0, "FLIN "));  break;
1448                 case AIS_FIRE:  mprintf((0, "FIRE "));  break;
1449                 case AIS_RECO:  mprintf((0, "RECO "));  break;
1450                 case AIS_ERR_:  mprintf((0, "ERR_ "));  break;
1451         }
1452
1453         mprintf((0, " Aware = "));
1454
1455         switch (ailp->player_awareness_type) {
1456                 case AIE_FIRE: mprintf((0, "FIRE ")); break;
1457                 case AIE_HITT: mprintf((0, "HITT ")); break;
1458                 case AIE_COLL: mprintf((0, "COLL ")); break;
1459                 case AIE_HURT: mprintf((0, "HURT ")); break;
1460         }
1461
1462         mprintf((0, "Next fire = %6.3f, Time = %6.3f\n", f2fl(ailp->next_fire), f2fl(ailp->player_awareness_time)));
1463
1464 }
1465 #endif
1466
1467 //      -------------------------------------------------------------------------------------------------------------------
1468 int     Break_on_object = -1;
1469
1470 void do_firing_stuff(object *obj, int player_visibility, vms_vector *vec_to_player)
1471 {
1472         if ((Dist_to_last_fired_upon_player_pos < FIRE_AT_NEARBY_PLAYER_THRESHOLD ) || (player_visibility >= 1)) {
1473                 //      Now, if in robot's field of view, lock onto player
1474                 fix     dot = vm_vec_dot(&obj->orient.fvec, vec_to_player);
1475                 if ((dot >= 7*F1_0/8) || (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED)) {
1476                         ai_static       *aip = &obj->ctype.ai_info;
1477                         ai_local                *ailp = &Ai_local_info[obj-Objects];
1478
1479                         switch (aip->GOAL_STATE) {
1480                                 case AIS_NONE:
1481                                 case AIS_REST:
1482                                 case AIS_SRCH:
1483                                 case AIS_LOCK:
1484                                         aip->GOAL_STATE = AIS_FIRE;
1485                                         if (ailp->player_awareness_type <= PA_NEARBY_ROBOT_FIRED) {
1486                                                 ailp->player_awareness_type = PA_NEARBY_ROBOT_FIRED;
1487                                                 ailp->player_awareness_time = PLAYER_AWARENESS_INITIAL_TIME;
1488                                         }
1489                                         break;
1490                         }
1491                 } else if (dot >= F1_0/2) {
1492                         ai_static       *aip = &obj->ctype.ai_info;
1493                         switch (aip->GOAL_STATE) {
1494                                 case AIS_NONE:
1495                                 case AIS_REST:
1496                                 case AIS_SRCH:
1497                                         aip->GOAL_STATE = AIS_LOCK;
1498                                         break;
1499                         }
1500                 }
1501         }
1502 }
1503
1504 // --------------------------------------------------------------------------------------------------------------------
1505 //      If a hiding robot gets bumped or hit, he decides to find another hiding place.
1506 void do_ai_robot_hit(object *objp, int type)
1507 {
1508         if (objp->control_type == CT_AI) {
1509                 if ((type == PA_WEAPON_ROBOT_COLLISION) || (type == PA_PLAYER_COLLISION))
1510                         switch (objp->ctype.ai_info.behavior) {
1511                                 case AIB_STILL:
1512                                 {
1513                                         int     r;
1514
1515                                         //      Attack robots (eg, green guy) shouldn't have behavior = still.
1516                                         Assert(Robot_info[objp->id].attack_type == 0);
1517
1518                                         r = d_rand();
1519                                         //      1/8 time, charge player, 1/4 time create path, rest of time, do nothing
1520                                         if (r < 4096) {
1521                                                 // -- mprintf((0, "Still guy switching to Station, creating path to player."));
1522                                                 create_path_to_player(objp, 10, 1);
1523                                                 objp->ctype.ai_info.behavior = AIB_STATION;
1524                                                 objp->ctype.ai_info.hide_segment = objp->segnum;
1525                                                 Ai_local_info[objp-Objects].mode = AIM_CHASE_OBJECT;
1526                                         } else if (r < 4096+8192) {
1527                                                 // -- mprintf((0, "Still guy creating n segment path."));
1528                                                 create_n_segment_path(objp, d_rand()/8192 + 2, -1);
1529                                                 Ai_local_info[objp-Objects].mode = AIM_FOLLOW_PATH;
1530                                         }
1531                                         break;
1532                                 }
1533                         }
1534         }
1535
1536 }
1537 #ifndef NDEBUG
1538 int     Do_ai_flag=1;
1539 int     Cvv_test=0;
1540 int     Cvv_last_time[MAX_OBJECTS];
1541 int     Gun_point_hack=0;
1542 #endif
1543
1544 int             Robot_sound_volume=DEFAULT_ROBOT_SOUND_VOLUME;
1545
1546 // --------------------------------------------------------------------------------------------------------------------
1547 //      Note: This function could be optimized.  Surely player_is_visible_from_object would benefit from the
1548 //      information of a normalized vec_to_player.
1549 //      Return player visibility:
1550 //              0               not visible
1551 //              1               visible, but robot not looking at player (ie, on an unobstructed vector)
1552 //              2               visible and in robot's field of view
1553 //              -1              player is cloaked
1554 //      If the player is cloaked, set vec_to_player based on time player cloaked and last uncloaked position.
1555 //      Updates ailp->previous_visibility if player is not cloaked, in which case the previous visibility is left unchanged
1556 //      and is copied to player_visibility
1557 void compute_vis_and_vec(object *objp, vms_vector *pos, ai_local *ailp, vms_vector *vec_to_player, int *player_visibility, robot_info *robptr, int *flag)
1558 {
1559         if (!*flag) {
1560                 if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED) {
1561                         fix                     delta_time, dist;
1562                         int                     cloak_index = (objp-Objects) % MAX_AI_CLOAK_INFO;
1563
1564                         delta_time = GameTime - Ai_cloak_info[cloak_index].last_time;
1565                         if (delta_time > F1_0*2) {
1566                                 vms_vector      randvec;
1567
1568                                 Ai_cloak_info[cloak_index].last_time = GameTime;
1569                                 make_random_vector(&randvec);
1570                                 vm_vec_scale_add2(&Ai_cloak_info[cloak_index].last_position, &randvec, 8*delta_time );
1571                         }
1572
1573                         dist = vm_vec_normalized_dir_quick(vec_to_player, &Ai_cloak_info[cloak_index].last_position, pos);
1574                         *player_visibility = player_is_visible_from_object(objp, pos, robptr->field_of_view[Difficulty_level], vec_to_player);
1575                         // *player_visibility = 2;
1576
1577                         if ((ailp->next_misc_sound_time < GameTime) && ((ailp->next_fire < F1_0) || (ailp->next_fire2 < F1_0)) && (dist < F1_0*20)) {
1578                                 // mprintf((0, "ANGRY! "));
1579                                 ailp->next_misc_sound_time = GameTime + (d_rand() + F1_0) * (7 - Difficulty_level) / 1;
1580                                 digi_link_sound_to_pos( robptr->see_sound, objp->segnum, 0, pos, 0 , Robot_sound_volume);
1581                         }
1582                 } else {
1583                         //      Compute expensive stuff -- vec_to_player and player_visibility
1584                         vm_vec_normalized_dir_quick(vec_to_player, &Believed_player_pos, pos);
1585                         if ((vec_to_player->x == 0) && (vec_to_player->y == 0) && (vec_to_player->z == 0)) {
1586                                 // -- mprintf((0, "Warning: Player and robot at exactly the same location.\n"));
1587                                 vec_to_player->x = F1_0;
1588                         }
1589                         *player_visibility = player_is_visible_from_object(objp, pos, robptr->field_of_view[Difficulty_level], vec_to_player);
1590
1591                         //      This horrible code added by MK in desperation on 12/13/94 to make robots wake up as soon as they
1592                         //      see you without killing frame rate.
1593                         {
1594                                 ai_static       *aip = &objp->ctype.ai_info;
1595                         if ((*player_visibility == 2) && (ailp->previous_visibility != 2))
1596                                 if ((aip->GOAL_STATE == AIS_REST) || (aip->CURRENT_STATE == AIS_REST)) {
1597                                         aip->GOAL_STATE = AIS_FIRE;
1598                                         aip->CURRENT_STATE = AIS_FIRE;
1599                                 }
1600                         }
1601
1602                         if ((ailp->previous_visibility != *player_visibility) && (*player_visibility == 2)) {
1603                                 if (ailp->previous_visibility == 0) {
1604                                         if (ailp->time_player_seen + F1_0/2 < GameTime) {
1605                                                 // -- mprintf((0, "SEE! "));
1606                                                 // -- if (Player_exploded)
1607                                                 // --   digi_link_sound_to_pos( robptr->taunt_sound, objp->segnum, 0, pos, 0 , Robot_sound_volume);
1608                                                 // -- else
1609                                                         digi_link_sound_to_pos( robptr->see_sound, objp->segnum, 0, pos, 0 , Robot_sound_volume);
1610                                                 ailp->time_player_sound_attacked = GameTime;
1611                                                 ailp->next_misc_sound_time = GameTime + F1_0 + d_rand()*4;
1612                                         }
1613                                 } else if (ailp->time_player_sound_attacked + F1_0/4 < GameTime) {
1614                                         // -- mprintf((0, "ANGRY! "));
1615                                         // -- if (Player_exploded)
1616                                         // --   digi_link_sound_to_pos( robptr->taunt_sound, objp->segnum, 0, pos, 0 , Robot_sound_volume);
1617                                         // -- else
1618                                                 digi_link_sound_to_pos( robptr->attack_sound, objp->segnum, 0, pos, 0 , Robot_sound_volume);
1619                                         ailp->time_player_sound_attacked = GameTime;
1620                                 }
1621                         } 
1622
1623                         if ((*player_visibility == 2) && (ailp->next_misc_sound_time < GameTime)) {
1624                                 // -- mprintf((0, "ATTACK! "));
1625                                 ailp->next_misc_sound_time = GameTime + (d_rand() + F1_0) * (7 - Difficulty_level) / 2;
1626                                 // -- if (Player_exploded)
1627                                 // --   digi_link_sound_to_pos( robptr->taunt_sound, objp->segnum, 0, pos, 0 , Robot_sound_volume);
1628                                 // -- else
1629                                         digi_link_sound_to_pos( robptr->attack_sound, objp->segnum, 0, pos, 0 , Robot_sound_volume);
1630                         }
1631                         ailp->previous_visibility = *player_visibility;
1632                 }
1633
1634                 *flag = 1;
1635
1636                 //      @mk, 09/21/95: If player view is not obstructed and awareness is at least as high as a nearby collision,
1637                 //      act is if robot is looking at player.
1638                 if (ailp->player_awareness_type >= PA_NEARBY_ROBOT_FIRED)
1639                         if (*player_visibility == 1)
1640                                 *player_visibility = 2;
1641                                 
1642                 if (*player_visibility) {
1643                         ailp->time_player_seen = GameTime;
1644                 }
1645         }
1646
1647 }
1648
1649 // --------------------------------------------------------------------------------------------------------------------
1650 //      Move the object objp to a spot in which it doesn't intersect a wall.
1651 //      It might mean moving it outside its current segment.
1652 void move_object_to_legal_spot(object *objp)
1653 {
1654         vms_vector      original_pos = objp->pos;
1655         int             i;
1656         segment *segp = &Segments[objp->segnum];
1657
1658         for (i=0; i<MAX_SIDES_PER_SEGMENT; i++) {
1659                 if (WALL_IS_DOORWAY(segp, i) & WID_FLY_FLAG) {
1660                         vms_vector      segment_center, goal_dir;
1661                         fix                     dist_to_center; // Value not used so far.
1662
1663                         compute_segment_center(&segment_center, &Segments[segp->children[i]]);
1664                         vm_vec_sub(&goal_dir, &segment_center, &objp->pos);
1665                         dist_to_center = vm_vec_normalize_quick(&goal_dir);
1666                         vm_vec_scale(&goal_dir, objp->size);
1667                         vm_vec_add2(&objp->pos, &goal_dir);
1668                         if (!object_intersects_wall(objp)) {
1669                                 int     new_segnum = find_point_seg(&objp->pos, objp->segnum);
1670
1671                                 if (new_segnum != -1) {
1672                                         obj_relink(objp-Objects, new_segnum);
1673                                         return;
1674                                 }
1675                         } else
1676                                 objp->pos = original_pos;
1677                 }
1678         }
1679
1680         if (Robot_info[objp->id].boss_flag) {
1681                 Int3();         //      Note: Boss is poking outside mine.  Will try to resolve.
1682                 teleport_boss(objp);
1683         } else {
1684                 mprintf((0, "Note: Killing robot #%i because he's badly stuck outside the mine.\n", objp-Objects));
1685                 apply_damage_to_robot(objp, objp->shields*2, objp-Objects);
1686         }
1687 }
1688
1689 // --------------------------------------------------------------------------------------------------------------------
1690 //      Move object one object radii from current position towards segment center.
1691 //      If segment center is nearer than 2 radii, move it to center.
1692 void move_towards_segment_center(object *objp)
1693 {
1694         int                     segnum = objp->segnum;
1695         fix                     dist_to_center;
1696         vms_vector      segment_center, goal_dir;
1697
1698         compute_segment_center(&segment_center, &Segments[segnum]);
1699
1700         vm_vec_sub(&goal_dir, &segment_center, &objp->pos);
1701         dist_to_center = vm_vec_normalize_quick(&goal_dir);
1702
1703         if (dist_to_center < objp->size) {
1704                 //      Center is nearer than the distance we want to move, so move to center.
1705                 objp->pos = segment_center;
1706                 mprintf((0, "Object #%i moved to center of segment #%i (%7.3f %7.3f %7.3f)\n", objp-Objects, objp->segnum, f2fl(objp->pos.x), f2fl(objp->pos.y), f2fl(objp->pos.z)));
1707                 if (object_intersects_wall(objp)) {
1708                         mprintf((0, "Object #%i still illegal, trying trickier move.\n"));
1709                         move_object_to_legal_spot(objp);
1710                 }
1711         } else {
1712                 int     new_segnum;
1713                 //      Move one radii towards center.
1714                 vm_vec_scale(&goal_dir, objp->size);
1715                 vm_vec_add2(&objp->pos, &goal_dir);
1716                 new_segnum = find_point_seg(&objp->pos, objp->segnum);
1717                 if (new_segnum == -1) {
1718                         objp->pos = segment_center;
1719                         move_object_to_legal_spot(objp);
1720                 }
1721                 // -- mprintf((0, "Obj %i moved twrds seg %i (%6.2f %6.2f %6.2f), dists: [%6.2f %6.2f]\n", objp-Objects, objp->segnum, f2fl(objp->pos.x), f2fl(objp->pos.y), f2fl(objp->pos.z), f2fl(vm_vec_dist_quick(&objp->pos, &segment_center)), f2fl(vm_vec_dist_quick(&objp->pos, &segment_center))));
1722         }
1723
1724 }
1725
1726 extern  int     Buddy_objnum;
1727
1728 //int   Buddy_got_stuck = 0;
1729
1730 //      -----------------------------------------------------------------------------------------------------------
1731 //      Return true if door can be flown through by a suitable type robot.
1732 //      Brains, avoid robots, companions can open doors.
1733 //      objp == NULL means treat as buddy.
1734 int ai_door_is_openable(object *objp, segment *segp, int sidenum)
1735 {
1736         int     wall_num;
1737         wall    *wallp;
1738
1739         if (!IS_CHILD(segp->children[sidenum]))
1740                 return 0;               //trap -2 (exit side)
1741
1742         wall_num = segp->sides[sidenum].wall_num;
1743
1744         if (wall_num == -1)             //if there's no door at all...
1745                 return 0;                               //..then say it can't be opened
1746
1747         //      The mighty console object can open all doors (for purposes of determining paths).
1748         if (objp == ConsoleObject) {
1749
1750                 if (Walls[wall_num].type == WALL_DOOR)
1751                         return 1;
1752         }
1753
1754         wallp = &Walls[wall_num];
1755
1756         if ((objp == NULL) || (Robot_info[objp->id].companion == 1)) {
1757                 int     ailp_mode;
1758
1759                 if (wallp->flags & WALL_BUDDY_PROOF) {
1760                         if ((wallp->type == WALL_DOOR) && (wallp->state == WALL_DOOR_CLOSED))
1761                                 return 0;
1762                         else if (wallp->type == WALL_CLOSED)
1763                                 return 0;
1764                         else if ((wallp->type == WALL_ILLUSION) && !(wallp->flags & WALL_ILLUSION_OFF))
1765                                 return 0;
1766                 }
1767                         
1768                 if (wallp->keys != KEY_NONE) {
1769                         if (wallp->keys == KEY_BLUE)
1770                                 return (Players[Player_num].flags & PLAYER_FLAGS_BLUE_KEY);
1771                         else if (wallp->keys == KEY_GOLD)
1772                                 return (Players[Player_num].flags & PLAYER_FLAGS_GOLD_KEY);
1773                         else if (wallp->keys == KEY_RED)
1774                                 return (Players[Player_num].flags & PLAYER_FLAGS_RED_KEY);
1775                 }
1776
1777                 if ((wallp->type != WALL_DOOR) && (wallp->type != WALL_CLOSED))
1778                         return 1;
1779
1780                 //      If Buddy is returning to player, don't let him think he can get through triggered doors.
1781                 //      It's only valid to think that if the player is going to get him through.  But if he's
1782                 //      going to the player, the player is probably on the opposite side.
1783                 if (objp == NULL)
1784                         ailp_mode = Ai_local_info[Buddy_objnum].mode;
1785                 else
1786                         ailp_mode = Ai_local_info[objp-Objects].mode;
1787
1788                 // -- if (Buddy_got_stuck) {
1789                 if (ailp_mode == AIM_GOTO_PLAYER) {
1790                         if ((wallp->type == WALL_BLASTABLE) && (wallp->state != WALL_BLASTED))
1791                                 return 0;
1792                         if (wallp->type == WALL_CLOSED)
1793                                 return 0;
1794                         if (wallp->type == WALL_DOOR) {
1795                                 if ((wallp->flags & WALL_DOOR_LOCKED) && (wallp->state == WALL_DOOR_CLOSED))
1796                                         return 0;
1797                         }
1798                 }
1799                 // -- }
1800
1801                 if ((ailp_mode != AIM_GOTO_PLAYER) && (wallp->controlling_trigger != -1)) {
1802                         int     clip_num = wallp->clip_num;
1803
1804                         if (clip_num == -1)
1805                                 return 1;
1806                         else if (WallAnims[clip_num].flags & WCF_HIDDEN) {
1807                                 if (wallp->state == WALL_DOOR_CLOSED)
1808                                         return 0;
1809                                 else
1810                                         return 1;
1811                         } else
1812                                 return 1;
1813                 }
1814
1815                 if (wallp->type == WALL_DOOR)  {
1816                         if (wallp->type == WALL_BLASTABLE)
1817                                 return 1;
1818                         else {
1819                                 int     clip_num = wallp->clip_num;
1820
1821                                 if (clip_num == -1)
1822                                         return 1;
1823                                 //      Buddy allowed to go through secret doors to get to player.
1824                                 else if ((ailp_mode != AIM_GOTO_PLAYER) && (WallAnims[clip_num].flags & WCF_HIDDEN)) {
1825                                         if (wallp->state == WALL_DOOR_CLOSED)
1826                                                 return 0;
1827                                         else
1828                                                 return 1;
1829                                 } else
1830                                         return 1;
1831                         }
1832                 }
1833         } else if ((objp->id == ROBOT_BRAIN) || (objp->ctype.ai_info.behavior == AIB_RUN_FROM) || (objp->ctype.ai_info.behavior == AIB_SNIPE)) {
1834                 if (wall_num != -1)
1835                 {
1836                         if ((wallp->type == WALL_DOOR) && (wallp->keys == KEY_NONE) && !(wallp->flags & WALL_DOOR_LOCKED))
1837                                 return 1;
1838                         else if (wallp->keys != KEY_NONE) {     //      Allow bots to open doors to which player has keys.
1839                                 if (wallp->keys & Players[Player_num].flags)
1840                                         return 1;
1841                         }
1842                 }
1843         }
1844         return 0;
1845 }
1846
1847 //      -----------------------------------------------------------------------------------------------------------
1848 //      Return side of openable door in segment, if any.  If none, return -1.
1849 int openable_doors_in_segment(int segnum)
1850 {
1851         int     i;
1852
1853         if ((segnum < 0) || (segnum > Highest_segment_index))
1854                 return -1;
1855
1856         for (i=0; i<MAX_SIDES_PER_SEGMENT; i++) {
1857                 if (Segments[segnum].sides[i].wall_num != -1) {
1858                         int     wall_num = Segments[segnum].sides[i].wall_num;
1859                         if ((Walls[wall_num].type == WALL_DOOR) && (Walls[wall_num].keys == KEY_NONE) && (Walls[wall_num].state == WALL_DOOR_CLOSED) && !(Walls[wall_num].flags & WALL_DOOR_LOCKED) && !(WallAnims[Walls[wall_num].clip_num].flags & WCF_HIDDEN))
1860                                 return i;
1861                 }
1862         }
1863
1864         return -1;
1865
1866 }
1867
1868 // -- // --------------------------------------------------------------------------------------------------------------------
1869 // -- //        Return true if a special object (player or control center) is in this segment.
1870 // -- int special_object_in_seg(int segnum)
1871 // -- {
1872 // --   int     objnum;
1873 // -- 
1874 // --   objnum = Segments[segnum].objects;
1875 // -- 
1876 // --   while (objnum != -1) {
1877 // --           if ((Objects[objnum].type == OBJ_PLAYER) || (Objects[objnum].type == OBJ_CNTRLCEN)) {
1878 // --                   mprintf((0, "Special object of type %i in segment %i\n", Objects[objnum].type, segnum));
1879 // --                   return 1;
1880 // --           } else
1881 // --                   objnum = Objects[objnum].next;
1882 // --   }
1883 // -- 
1884 // --   return 0;
1885 // -- }
1886
1887 // -- // --------------------------------------------------------------------------------------------------------------------
1888 // -- //        Randomly select a segment attached to *segp, reachable by flying.
1889 // -- int get_random_child(int segnum)
1890 // -- {
1891 // --   int     sidenum;
1892 // --   segment *segp = &Segments[segnum];
1893 // -- 
1894 // --   sidenum = (rand() * 6) >> 15;
1895 // -- 
1896 // --   while (!(WALL_IS_DOORWAY(segp, sidenum) & WID_FLY_FLAG))
1897 // --           sidenum = (rand() * 6) >> 15;
1898 // -- 
1899 // --   segnum = segp->children[sidenum];
1900 // -- 
1901 // --   return segnum;
1902 // -- }
1903
1904 // --------------------------------------------------------------------------------------------------------------------
1905 //      Return true if placing an object of size size at pos *pos intersects a (player or robot or control center) in segment *segp.
1906 int check_object_object_intersection(vms_vector *pos, fix size, segment *segp)
1907 {
1908         int             curobjnum;
1909
1910         //      If this would intersect with another object (only check those in this segment), then try to move.
1911         curobjnum = segp->objects;
1912         while (curobjnum != -1) {
1913                 object *curobjp = &Objects[curobjnum];
1914                 if ((curobjp->type == OBJ_PLAYER) || (curobjp->type == OBJ_ROBOT) || (curobjp->type == OBJ_CNTRLCEN)) {
1915                         if (vm_vec_dist_quick(pos, &curobjp->pos) < size + curobjp->size)
1916                                 return 1;
1917                 }
1918                 curobjnum = curobjp->next;
1919         }
1920
1921         return 0;
1922
1923 }
1924
1925 // --------------------------------------------------------------------------------------------------------------------
1926 //      Return objnum if object created, else return -1.
1927 //      If pos == NULL, pick random spot in segment.
1928 int create_gated_robot( int segnum, int object_id, vms_vector *pos)
1929 {
1930         int             objnum;
1931         object  *objp;
1932         segment *segp = &Segments[segnum];
1933         vms_vector      object_pos;
1934         robot_info      *robptr = &Robot_info[object_id];
1935         int             i, count=0;
1936         fix             objsize = Polygon_models[robptr->model_num].rad;
1937         int             default_behavior;
1938
1939         if (GameTime - Last_gate_time < Gate_interval)
1940                 return -1;
1941
1942         for (i=0; i<=Highest_object_index; i++)
1943                 if (Objects[i].type == OBJ_ROBOT)
1944                         if (Objects[i].matcen_creator == BOSS_GATE_MATCEN_NUM)
1945                                 count++;
1946
1947         if (count > 2*Difficulty_level + 6) {
1948                 //mprintf((0, "Cannot gate in a robot until you kill one.\n"));
1949                 Last_gate_time = GameTime - 3*Gate_interval/4;
1950                 return -1;
1951         }
1952
1953         compute_segment_center(&object_pos, segp);
1954         if (pos == NULL)
1955                 pick_random_point_in_seg(&object_pos, segp-Segments);
1956         else
1957                 object_pos = *pos;
1958
1959         //      See if legal to place object here.  If not, move about in segment and try again.
1960         if (check_object_object_intersection(&object_pos, objsize, segp)) {
1961                 //mprintf((0, "Can't get in because object collides with something.\n"));
1962                 Last_gate_time = GameTime - 3*Gate_interval/4;
1963                 return -1;
1964         }
1965
1966         objnum = obj_create(OBJ_ROBOT, object_id, segnum, &object_pos, &vmd_identity_matrix, objsize, CT_AI, MT_PHYSICS, RT_POLYOBJ);
1967
1968         if ( objnum < 0 ) {
1969                 // mprintf((1, "Can't get object to gate in robot.  Not gating in.\n"));
1970                 Last_gate_time = GameTime - 3*Gate_interval/4;
1971                 return -1;
1972         }
1973
1974         //mprintf((0, "Gating in object %i in segment %i\n", objnum, segp-Segments));
1975
1976         Objects[objnum].lifeleft = F1_0*30;     //      Gated in robots only live 30 seconds.
1977
1978 #ifdef NETWORK
1979         Net_create_objnums[0] = objnum; // A convenient global to get objnum back to caller for multiplayer
1980 #endif
1981
1982         objp = &Objects[objnum];
1983
1984         //Set polygon-object-specific data
1985
1986         objp->rtype.pobj_info.model_num = robptr->model_num;
1987         objp->rtype.pobj_info.subobj_flags = 0;
1988
1989         //set Physics info
1990
1991         objp->mtype.phys_info.mass = robptr->mass;
1992         objp->mtype.phys_info.drag = robptr->drag;
1993
1994         objp->mtype.phys_info.flags |= (PF_LEVELLING);
1995
1996         objp->shields = robptr->strength;
1997         objp->matcen_creator = BOSS_GATE_MATCEN_NUM;    //      flag this robot as having been created by the boss.
1998
1999         default_behavior = Robot_info[objp->id].behavior;
2000         init_ai_object(objp-Objects, default_behavior, -1 );            //      Note, -1 = segment this robot goes to to hide, should probably be something useful
2001
2002         object_create_explosion(segnum, &object_pos, i2f(10), VCLIP_MORPHING_ROBOT );
2003         digi_link_sound_to_pos( Vclip[VCLIP_MORPHING_ROBOT].sound_num, segnum, 0, &object_pos, 0 , F1_0);
2004         morph_start(objp);
2005
2006         Last_gate_time = GameTime;
2007
2008         Players[Player_num].num_robots_level++;
2009         Players[Player_num].num_robots_total++;
2010
2011         return objp-Objects;
2012 }
2013
2014 #define MAX_SPEW_BOT            3
2015
2016 int     Spew_bots[NUM_D2_BOSSES][MAX_SPEW_BOT] = {
2017         {38, 40, -1},
2018         {37, -1, -1},
2019         {43, 57, -1},
2020         {26, 27, 58},
2021         {59, 58, 54},
2022         {60, 61, 54},
2023
2024         {69, 29, 24},
2025         {72, 60, 73} 
2026 };
2027
2028 int     Max_spew_bots[NUM_D2_BOSSES] = {2, 1, 2, 3, 3, 3,  3, 3};
2029
2030 //      ----------------------------------------------------------------------------------------------------------
2031 //      objp points at a boss.  He was presumably just hit and he's supposed to create a bot at the hit location *pos.
2032 int boss_spew_robot(object *objp, vms_vector *pos)
2033 {
2034         int             objnum, segnum;
2035         int             boss_index;
2036
2037         boss_index = Robot_info[objp->id].boss_flag - BOSS_D2;
2038
2039         Assert((boss_index >= 0) && (boss_index < NUM_D2_BOSSES));
2040
2041         segnum = find_point_seg(pos, objp->segnum);
2042         if (segnum == -1) {
2043                 mprintf((0, "Tried to spew a bot outside the mine!  Aborting!\n"));
2044                 return -1;
2045         }       
2046
2047         objnum = create_gated_robot( segnum, Spew_bots[boss_index][(Max_spew_bots[boss_index] * d_rand()) >> 15], pos);
2048  
2049         //      Make spewed robot come tumbling out as if blasted by a flash missile.
2050         if (objnum != -1) {
2051                 object  *newobjp = &Objects[objnum];
2052                 int             force_val;
2053
2054                 force_val = F1_0/FrameTime;
2055
2056                 if (force_val) {
2057                         newobjp->ctype.ai_info.SKIP_AI_COUNT += force_val;
2058                         newobjp->mtype.phys_info.rotthrust.x = ((d_rand() - 16384) * force_val)/16;
2059                         newobjp->mtype.phys_info.rotthrust.y = ((d_rand() - 16384) * force_val)/16;
2060                         newobjp->mtype.phys_info.rotthrust.z = ((d_rand() - 16384) * force_val)/16;
2061                         newobjp->mtype.phys_info.flags |= PF_USES_THRUST;
2062
2063                         //      Now, give a big initial velocity to get moving away from boss.
2064                         vm_vec_sub(&newobjp->mtype.phys_info.velocity, pos, &objp->pos);
2065                         vm_vec_normalize_quick(&newobjp->mtype.phys_info.velocity);
2066                         vm_vec_scale(&newobjp->mtype.phys_info.velocity, F1_0*128);
2067                 }
2068         }
2069
2070         return objnum;
2071 }
2072
2073 // --------------------------------------------------------------------------------------------------------------------
2074 //      Call this each time the player starts a new ship.
2075 void init_ai_for_ship(void)
2076 {
2077         int     i;
2078
2079         for (i=0; i<MAX_AI_CLOAK_INFO; i++) {
2080                 Ai_cloak_info[i].last_time = GameTime;
2081                 Ai_cloak_info[i].last_segment = ConsoleObject->segnum;
2082                 Ai_cloak_info[i].last_position = ConsoleObject->pos;
2083         }
2084 }
2085
2086 // --------------------------------------------------------------------------------------------------------------------
2087 //      Make object objp gate in a robot.
2088 //      The process of him bringing in a robot takes one second.
2089 //      Then a robot appears somewhere near the player.
2090 //      Return objnum if robot successfully created, else return -1
2091 int gate_in_robot(int type, int segnum)
2092 {
2093         if (segnum < 0)
2094                 segnum = Boss_gate_segs[(d_rand() * Num_boss_gate_segs) >> 15];
2095
2096         Assert((segnum >= 0) && (segnum <= Highest_segment_index));
2097
2098         return create_gated_robot(segnum, type, NULL);
2099 }
2100
2101 // --------------------------------------------------------------------------------------------------------------------
2102 int boss_fits_in_seg(object *boss_objp, int segnum)
2103 {
2104         vms_vector      segcenter;
2105         int                     boss_objnum = boss_objp-Objects;
2106         int                     posnum;
2107
2108         compute_segment_center(&segcenter, &Segments[segnum]);
2109
2110         for (posnum=0; posnum<9; posnum++) {
2111                 if (posnum > 0) {
2112                         vms_vector      vertex_pos;
2113
2114                         Assert((posnum-1 >= 0) && (posnum-1 < 8));
2115                         vertex_pos = Vertices[Segments[segnum].verts[posnum-1]];
2116                         vm_vec_avg(&boss_objp->pos, &vertex_pos, &segcenter);
2117                 } else
2118                         boss_objp->pos = segcenter;
2119
2120                 obj_relink(boss_objnum, segnum);
2121                 if (!object_intersects_wall(boss_objp))
2122                         return 1;
2123         }
2124
2125         return 0;
2126 }
2127
2128 // --------------------------------------------------------------------------------------------------------------------
2129 void teleport_boss(object *objp)
2130 {
2131         int                     rand_segnum, rand_index;
2132         vms_vector      boss_dir;
2133         Assert(Num_boss_teleport_segs > 0);
2134
2135         //      Pick a random segment from the list of boss-teleportable-to segments.
2136         rand_index = (d_rand() * Num_boss_teleport_segs) >> 15; 
2137         rand_segnum = Boss_teleport_segs[rand_index];
2138         Assert((rand_segnum >= 0) && (rand_segnum <= Highest_segment_index));
2139
2140         //mprintf((0, "Frame %i: Boss teleporting to segment #%i, pos = {%8x %8x %8x}.\n", FrameCount, rand_segnum, objp->pos.x, objp->pos.y, objp->pos.z));
2141
2142 #ifdef NETWORK
2143         if (Game_mode & GM_MULTI)
2144                 multi_send_boss_actions(objp-Objects, 1, rand_segnum, 0);
2145 #endif
2146
2147         compute_segment_center(&objp->pos, &Segments[rand_segnum]);
2148         obj_relink(objp-Objects, rand_segnum);
2149
2150         Last_teleport_time = GameTime;
2151
2152         //      make boss point right at player
2153         vm_vec_sub(&boss_dir, &Objects[Players[Player_num].objnum].pos, &objp->pos);
2154         vm_vector_2_matrix(&objp->orient, &boss_dir, NULL, NULL);
2155
2156         digi_link_sound_to_pos( Vclip[VCLIP_MORPHING_ROBOT].sound_num, rand_segnum, 0, &objp->pos, 0 , F1_0);
2157         digi_kill_sound_linked_to_object( objp-Objects);
2158         digi_link_sound_to_object2( Robot_info[objp->id].see_sound, objp-Objects, 1, F1_0, F1_0*512 );  //      F1_0*512 means play twice as loud
2159
2160         //      After a teleport, boss can fire right away.
2161         Ai_local_info[objp-Objects].next_fire = 0;
2162         Ai_local_info[objp-Objects].next_fire2 = 0;
2163
2164 }
2165
2166 //      ----------------------------------------------------------------------
2167 void start_boss_death_sequence(object *objp)
2168 {
2169         if (Robot_info[objp->id].boss_flag) {
2170                 Boss_dying = 1;
2171                 Boss_dying_start_time = GameTime;
2172         }
2173
2174 }
2175
2176 //      ----------------------------------------------------------------------
2177 //      General purpose robot-dies-with-death-roll-and-groan code.
2178 //      Return true if object just died.
2179 //      scale: F1_0*4 for boss, much smaller for much smaller guys
2180 int do_robot_dying_frame(object *objp, fix start_time, fix roll_duration, byte *dying_sound_playing, int death_sound, fix expl_scale, fix sound_scale)
2181 {
2182         fix     roll_val, temp;
2183         fix     sound_duration;
2184
2185         if (!roll_duration)
2186                 roll_duration = F1_0/4;
2187
2188         roll_val = fixdiv(GameTime - start_time, roll_duration);
2189
2190         fix_sincos(fixmul(roll_val, roll_val), &temp, &objp->mtype.phys_info.rotvel.x);
2191         fix_sincos(roll_val, &temp, &objp->mtype.phys_info.rotvel.y);
2192         fix_sincos(roll_val-F1_0/8, &temp, &objp->mtype.phys_info.rotvel.z);
2193
2194         objp->mtype.phys_info.rotvel.x = (GameTime - start_time)/9;
2195         objp->mtype.phys_info.rotvel.y = (GameTime - start_time)/5;
2196         objp->mtype.phys_info.rotvel.z = (GameTime - start_time)/7;
2197
2198         if (digi_sample_rate)
2199                 sound_duration = fixdiv(GameSounds[digi_xlat_sound(death_sound)].length,digi_sample_rate);
2200         else
2201                 sound_duration = F1_0;
2202
2203         if (start_time + roll_duration - sound_duration < GameTime) {
2204                 if (!*dying_sound_playing) {
2205                         mprintf((0, "Starting death sound!\n"));
2206                         *dying_sound_playing = 1;
2207                         digi_link_sound_to_object2( death_sound, objp-Objects, 0, sound_scale, sound_scale*256 );       //      F1_0*512 means play twice as loud
2208                 } else if (d_rand() < FrameTime*16)
2209                         create_small_fireball_on_object(objp, (F1_0 + d_rand()) * (16 * expl_scale/F1_0)/8, 0);
2210         } else if (d_rand() < FrameTime*8)
2211                 create_small_fireball_on_object(objp, (F1_0/2 + d_rand()) * (16 * expl_scale/F1_0)/8, 1);
2212
2213         if (start_time + roll_duration < GameTime)
2214                 return 1;
2215         else
2216                 return 0;
2217 }
2218
2219 //      ----------------------------------------------------------------------
2220 void start_robot_death_sequence(object *objp)
2221 {
2222         objp->ctype.ai_info.dying_start_time = GameTime;
2223         objp->ctype.ai_info.dying_sound_playing = 0;
2224         objp->ctype.ai_info.SKIP_AI_COUNT = 0;
2225
2226 }
2227
2228 //      ----------------------------------------------------------------------
2229 void do_boss_dying_frame(object *objp)
2230 {
2231         int     rval;
2232
2233         rval = do_robot_dying_frame(objp, Boss_dying_start_time, BOSS_DEATH_DURATION, &Boss_dying_sound_playing, Robot_info[objp->id].deathroll_sound, F1_0*4, F1_0*4);
2234
2235         if (rval) {
2236                 do_controlcen_destroyed_stuff(NULL);
2237                 explode_object(objp, F1_0/4);
2238                 digi_link_sound_to_object2(SOUND_BADASS_EXPLOSION, objp-Objects, 0, F2_0, F1_0*512);
2239         }
2240 }
2241
2242 extern void recreate_thief(object *objp);
2243
2244 //      ----------------------------------------------------------------------
2245 int do_any_robot_dying_frame(object *objp)
2246 {
2247         if (objp->ctype.ai_info.dying_start_time) {
2248                 int     rval, death_roll;
2249
2250                 death_roll = Robot_info[objp->id].death_roll;
2251                 rval = do_robot_dying_frame(objp, objp->ctype.ai_info.dying_start_time, min(death_roll/2+1,6)*F1_0, &objp->ctype.ai_info.dying_sound_playing, Robot_info[objp->id].deathroll_sound, death_roll*F1_0/8, death_roll*F1_0/2);
2252
2253                 if (rval) {
2254                         explode_object(objp, F1_0/4);
2255                         digi_link_sound_to_object2(SOUND_BADASS_EXPLOSION, objp-Objects, 0, F2_0, F1_0*512);
2256                         if ((Current_level_num < 0) && (Robot_info[objp->id].thief))
2257                                 recreate_thief(objp);
2258                 }
2259
2260                 return 1;
2261         }
2262
2263         return 0;
2264 }
2265
2266 // --------------------------------------------------------------------------------------------------------------------
2267 //      Called for an AI object if it is fairly aware of the player.
2268 //      awareness_level is in 0..100.  Larger numbers indicate greater awareness (eg, 99 if firing at player).
2269 //      In a given frame, might not get called for an object, or might be called more than once.
2270 //      The fact that this routine is not called for a given object does not mean that object is not interested in the player.
2271 //      Objects are moved by physics, so they can move even if not interested in a player.  However, if their velocity or
2272 //      orientation is changing, this routine will be called.
2273 //      Return value:
2274 //              0       this player IS NOT allowed to move this robot.
2275 //              1       this player IS allowed to move this robot.
2276 int ai_multiplayer_awareness(object *objp, int awareness_level)
2277 {
2278         int     rval=1;
2279
2280 #ifdef NETWORK
2281         if (Game_mode & GM_MULTI) {
2282                 if (awareness_level == 0)
2283                         return 0;
2284                 rval = multi_can_move_robot(objp-Objects, awareness_level);
2285         }
2286 #endif
2287
2288         return rval;
2289
2290 }
2291
2292 #ifndef NDEBUG
2293 fix     Prev_boss_shields = -1;
2294 #endif
2295
2296 // --------------------------------------------------------------------------------------------------------------------
2297 //      Do special stuff for a boss.
2298 void do_boss_stuff(object *objp, int player_visibility)
2299 {
2300         int     boss_id, boss_index;
2301
2302         boss_id = Robot_info[objp->id].boss_flag;
2303
2304         Assert((boss_id >= BOSS_D2) && (boss_id < BOSS_D2 + NUM_D2_BOSSES));
2305
2306         boss_index = boss_id - BOSS_D2;
2307
2308 #ifndef NDEBUG
2309         if (objp->shields != Prev_boss_shields) {
2310                 mprintf((0, "Boss shields = %7.3f, object %i\n", f2fl(objp->shields), objp-Objects));
2311                 Prev_boss_shields = objp->shields;
2312         }
2313 #endif
2314
2315         //      New code, fixes stupid bug which meant boss never gated in robots if > 32767 seconds played.
2316         if (Last_teleport_time > GameTime)
2317                 Last_teleport_time = GameTime;
2318
2319         if (Last_gate_time > GameTime)
2320                 Last_gate_time = GameTime;
2321
2322         //      @mk, 10/13/95:  Reason:
2323         //              Level 4 boss behind locked door.  But he's allowed to teleport out of there.  So he
2324         //              teleports out of there right away, and blasts player right after first door.
2325         if (!player_visibility && (GameTime - Boss_hit_time > F1_0*2))
2326                 return;
2327
2328         if (!Boss_dying && Boss_teleports[boss_index]) {
2329                 if (objp->ctype.ai_info.CLOAKED == 1) {
2330                         Boss_hit_time = GameTime;       //      Keep the cloak:teleport process going.
2331                         if ((GameTime - Boss_cloak_start_time > BOSS_CLOAK_DURATION/3) && (Boss_cloak_end_time - GameTime > BOSS_CLOAK_DURATION/3) && (GameTime - Last_teleport_time > Boss_teleport_interval)) {
2332                                 if (ai_multiplayer_awareness(objp, 98))
2333                                         teleport_boss(objp);
2334                         } else if (GameTime - Boss_hit_time > F1_0*2) {
2335                                 Last_teleport_time -= Boss_teleport_interval/4;
2336                         }
2337
2338                         if (GameTime > Boss_cloak_end_time || GameTime < Boss_cloak_start_time)
2339                                 objp->ctype.ai_info.CLOAKED = 0;
2340                 } else if ((GameTime - Boss_cloak_end_time > Boss_cloak_interval) || (GameTime - Boss_cloak_end_time < -Boss_cloak_duration)) {
2341                         if (ai_multiplayer_awareness(objp, 95)) {
2342                                 Boss_cloak_start_time = GameTime;
2343                                 Boss_cloak_end_time = GameTime+Boss_cloak_duration;
2344                                 objp->ctype.ai_info.CLOAKED = 1;
2345 #ifdef NETWORK
2346                                 if (Game_mode & GM_MULTI)
2347                                         multi_send_boss_actions(objp-Objects, 2, 0, 0);
2348 #endif
2349                         }
2350                 }
2351         }
2352
2353 }
2354
2355 #define BOSS_TO_PLAYER_GATE_DISTANCE    (F1_0*200)
2356
2357 // -- Obsolete D1 code -- // --------------------------------------------------------------------------------------------------------------------
2358 // -- Obsolete D1 code -- //    Do special stuff for a boss.
2359 // -- Obsolete D1 code -- void do_super_boss_stuff(object *objp, fix dist_to_player, int player_visibility)
2360 // -- Obsolete D1 code -- {
2361 // -- Obsolete D1 code --       static int eclip_state = 0;
2362 // -- Obsolete D1 code -- 
2363 // -- Obsolete D1 code --       do_boss_stuff(objp, player_visibility);
2364 // -- Obsolete D1 code -- 
2365 // -- Obsolete D1 code --       // Only master player can cause gating to occur.
2366 // -- Obsolete D1 code --       if ((Game_mode & GM_MULTI) && !network_i_am_master())
2367 // -- Obsolete D1 code --               return; 
2368 // -- Obsolete D1 code -- 
2369 // -- Obsolete D1 code --       if ((dist_to_player < BOSS_TO_PLAYER_GATE_DISTANCE) || player_visibility || (Game_mode & GM_MULTI)) {
2370 // -- Obsolete D1 code --               if (GameTime - Last_gate_time > Gate_interval/2) {
2371 // -- Obsolete D1 code --                       restart_effect(BOSS_ECLIP_NUM);
2372 // -- Obsolete D1 code --                       if (eclip_state == 0) {
2373 // -- Obsolete D1 code --                               multi_send_boss_actions(objp-Objects, 4, 0, 0);
2374 // -- Obsolete D1 code --                               eclip_state = 1;
2375 // -- Obsolete D1 code --                       }
2376 // -- Obsolete D1 code --               }
2377 // -- Obsolete D1 code --               else {
2378 // -- Obsolete D1 code --                       stop_effect(BOSS_ECLIP_NUM);
2379 // -- Obsolete D1 code --                       if (eclip_state == 1) {
2380 // -- Obsolete D1 code --                               multi_send_boss_actions(objp-Objects, 5, 0, 0);
2381 // -- Obsolete D1 code --                               eclip_state = 0;
2382 // -- Obsolete D1 code --                       }
2383 // -- Obsolete D1 code --               }
2384 // -- Obsolete D1 code -- 
2385 // -- Obsolete D1 code --               if (GameTime - Last_gate_time > Gate_interval)
2386 // -- Obsolete D1 code --                       if (ai_multiplayer_awareness(objp, 99)) {
2387 // -- Obsolete D1 code --                               int     rtval;
2388 // -- Obsolete D1 code --                               int     randtype = (d_rand() * MAX_GATE_INDEX) >> 15;
2389 // -- Obsolete D1 code -- 
2390 // -- Obsolete D1 code --                               Assert(randtype < MAX_GATE_INDEX);
2391 // -- Obsolete D1 code --                               randtype = Super_boss_gate_list[randtype];
2392 // -- Obsolete D1 code --                               Assert(randtype < N_robot_types);
2393 // -- Obsolete D1 code -- 
2394 // -- Obsolete D1 code --                               rtval = gate_in_robot(randtype, -1);
2395 // -- Obsolete D1 code --                               if ((rtval != -1) && (Game_mode & GM_MULTI))
2396 // -- Obsolete D1 code --                               {
2397 // -- Obsolete D1 code --                                       multi_send_boss_actions(objp-Objects, 3, randtype, Net_create_objnums[0]);
2398 // -- Obsolete D1 code --                                       map_objnum_local_to_local(Net_create_objnums[0]);
2399 // -- Obsolete D1 code --                               }
2400 // -- Obsolete D1 code --                       }       
2401 // -- Obsolete D1 code --       }
2402 // -- Obsolete D1 code -- }
2403
2404 //int multi_can_move_robot(object *objp, int awareness_level)
2405 //{
2406 //      return 0;
2407 //}
2408
2409 void ai_multi_send_robot_position(int objnum, int force)
2410 {
2411 #ifdef NETWORK
2412         if (Game_mode & GM_MULTI) 
2413         {
2414                 if (force != -1)
2415                         multi_send_robot_position(objnum, 1);
2416                 else
2417                         multi_send_robot_position(objnum, 0);
2418         }
2419 #endif
2420         return;
2421 }
2422
2423 // --------------------------------------------------------------------------------------------------------------------
2424 //      Returns true if this object should be allowed to fire at the player.
2425 int maybe_ai_do_actual_firing_stuff(object *obj, ai_static *aip)
2426 {
2427 #ifdef NETWORK
2428         if (Game_mode & GM_MULTI)
2429                 if ((aip->GOAL_STATE != AIS_FLIN) && (obj->id != ROBOT_BRAIN))
2430                         if (aip->CURRENT_STATE == AIS_FIRE)
2431                                 return 1;
2432 #endif
2433
2434         return 0;
2435 }
2436
2437 vms_vector      Last_fired_upon_player_pos;
2438
2439 // --------------------------------------------------------------------------------------------------------------------
2440 //      If fire_anyway, fire even if player is not visible.  We're firing near where we believe him to be.  Perhaps he's
2441 //      lurking behind a corner.
2442 void ai_do_actual_firing_stuff(object *obj, ai_static *aip, ai_local *ailp, robot_info *robptr, vms_vector *vec_to_player, fix dist_to_player, vms_vector *gun_point, int player_visibility, int object_animates, int gun_num)
2443 {
2444         fix     dot;
2445
2446         if ((player_visibility == 2) || (Dist_to_last_fired_upon_player_pos < FIRE_AT_NEARBY_PLAYER_THRESHOLD )) {
2447                 vms_vector      fire_pos;
2448
2449                 fire_pos = Believed_player_pos;
2450
2451                 //      Hack: If visibility not == 2, we're here because we're firing at a nearby player.
2452                 //      So, fire at Last_fired_upon_player_pos instead of the player position.
2453                 if (!robptr->attack_type && (player_visibility != 2))
2454                         fire_pos = Last_fired_upon_player_pos;
2455
2456                 //      Changed by mk, 01/04/95, onearm would take about 9 seconds until he can fire at you.
2457                 //      Above comment corrected.  Date changed from 1994, to 1995.  Should fix some very subtle bugs, as well as not cause me to wonder, in the future, why I was writing AI code for onearm ten months before he existed.
2458                 if (!object_animates || ready_to_fire(robptr, ailp)) {
2459                         dot = vm_vec_dot(&obj->orient.fvec, vec_to_player);
2460                         if ((dot >= 7*F1_0/8) || ((dot > F1_0/4) &&  robptr->boss_flag)) {
2461
2462                                 if (gun_num < Robot_info[obj->id].n_guns) {
2463                                         if (robptr->attack_type == 1) {
2464                                                 if (!Player_exploded && (dist_to_player < obj->size + ConsoleObject->size + F1_0*2)) {          // robptr->circle_distance[Difficulty_level] + ConsoleObject->size) {
2465                                                         if (!ai_multiplayer_awareness(obj, ROBOT_FIRE_AGITATION-2))
2466                                                                 return;
2467                                                         do_ai_robot_hit_attack(obj, ConsoleObject, &obj->pos);
2468                                                 } else {
2469                                                         // mprintf((0, "Green won't fire: Too far: dist = %7.3f, threshold = %7.3f\n", f2fl(dist_to_player), f2fl(obj->size + ConsoleObject->size + F1_0*2)));
2470                                                         return;
2471                                                 }
2472                                         } else {
2473                                                 if ((gun_point->x == 0) && (gun_point->y == 0) && (gun_point->z == 0)) {
2474                                                         ; //mprintf((0, "Would like to fire gun, but gun not selected.\n"));
2475                                                 } else {
2476                                                         if (!ai_multiplayer_awareness(obj, ROBOT_FIRE_AGITATION))
2477                                                                 return;
2478                                                         //      New, multi-weapon-type system, 06/05/95 (life is slipping away...)
2479                                                         if (gun_num != 0) {
2480                                                                 if (ailp->next_fire <= 0) {
2481                                                                         ai_fire_laser_at_player(obj, gun_point, gun_num, &fire_pos);
2482                                                                         Last_fired_upon_player_pos = fire_pos;
2483                                                                 }
2484
2485                                                                 if ((ailp->next_fire2 <= 0) && (robptr->weapon_type2 != -1)) {
2486                                                                         calc_gun_point(gun_point, obj, 0);
2487                                                                         ai_fire_laser_at_player(obj, gun_point, 0, &fire_pos);
2488                                                                         Last_fired_upon_player_pos = fire_pos;
2489                                                                 }
2490
2491                                                         } else if (ailp->next_fire <= 0) {
2492                                                                 ai_fire_laser_at_player(obj, gun_point, gun_num, &fire_pos);
2493                                                                 Last_fired_upon_player_pos = fire_pos;
2494                                                         }
2495                                                 }
2496                                         }
2497
2498                                         //      Wants to fire, so should go into chase mode, probably.
2499                                         if ( (aip->behavior != AIB_RUN_FROM)
2500                                                  && (aip->behavior != AIB_STILL)
2501                                                  && (aip->behavior != AIB_SNIPE)
2502                                                  && (aip->behavior != AIB_FOLLOW)
2503                                                  && (!robptr->attack_type)
2504                                                  && ((ailp->mode == AIM_FOLLOW_PATH) || (ailp->mode == AIM_STILL)))
2505                                                 ailp->mode = AIM_CHASE_OBJECT;
2506                                 }
2507
2508                                 aip->GOAL_STATE = AIS_RECO;
2509                                 ailp->goal_state[aip->CURRENT_GUN] = AIS_RECO;
2510
2511                                 // Switch to next gun for next fire.  If has 2 gun types, select gun #1, if exists.
2512                                 aip->CURRENT_GUN++;
2513                                 if (aip->CURRENT_GUN >= Robot_info[obj->id].n_guns)
2514                                 {
2515                                         if ((Robot_info[obj->id].n_guns == 1) || (Robot_info[obj->id].weapon_type2 == -1))
2516                                                 aip->CURRENT_GUN = 0;
2517                                         else
2518                                                 aip->CURRENT_GUN = 1;
2519                                 }
2520                         }
2521                 }
2522         } else if ( ((!robptr->attack_type) && (Weapon_info[Robot_info[obj->id].weapon_type].homing_flag == 1)) || (((Robot_info[obj->id].weapon_type2 != -1) && (Weapon_info[Robot_info[obj->id].weapon_type2].homing_flag == 1))) ) {
2523                 //      Robots which fire homing weapons might fire even if they don't have a bead on the player.
2524                 if (((!object_animates) || (ailp->achieved_state[aip->CURRENT_GUN] == AIS_FIRE))
2525                          && (((ailp->next_fire <= 0) && (aip->CURRENT_GUN != 0)) || ((ailp->next_fire2 <= 0) && (aip->CURRENT_GUN == 0)))
2526                          && (vm_vec_dist_quick(&Hit_pos, &obj->pos) > F1_0*40)) {
2527                         if (!ai_multiplayer_awareness(obj, ROBOT_FIRE_AGITATION))
2528                                 return;
2529                         ai_fire_laser_at_player(obj, gun_point, gun_num, &Believed_player_pos);
2530
2531                         aip->GOAL_STATE = AIS_RECO;
2532                         ailp->goal_state[aip->CURRENT_GUN] = AIS_RECO;
2533
2534                         // Switch to next gun for next fire.
2535                         aip->CURRENT_GUN++;
2536                         if (aip->CURRENT_GUN >= Robot_info[obj->id].n_guns)
2537                                 aip->CURRENT_GUN = 0;
2538                 } else {
2539                         // Switch to next gun for next fire.
2540                         aip->CURRENT_GUN++;
2541                         if (aip->CURRENT_GUN >= Robot_info[obj->id].n_guns)
2542                                 aip->CURRENT_GUN = 0;
2543                 }
2544         } else {
2545
2546
2547         //      ---------------------------------------------------------------
2548
2549                 vms_vector      vec_to_last_pos;
2550
2551                 if (d_rand()/2 < fixmul(FrameTime, (Difficulty_level << 12) + 0x4000)) {
2552                 if ((!object_animates || ready_to_fire(robptr, ailp)) && (Dist_to_last_fired_upon_player_pos < FIRE_AT_NEARBY_PLAYER_THRESHOLD)) {
2553                         vm_vec_normalized_dir_quick(&vec_to_last_pos, &Believed_player_pos, &obj->pos);
2554                         dot = vm_vec_dot(&obj->orient.fvec, &vec_to_last_pos);
2555                         if (dot >= 7*F1_0/8) {
2556
2557                                 if (aip->CURRENT_GUN < Robot_info[obj->id].n_guns) {
2558                                         if (robptr->attack_type == 1) {
2559                                                 if (!Player_exploded && (dist_to_player < obj->size + ConsoleObject->size + F1_0*2)) {          // robptr->circle_distance[Difficulty_level] + ConsoleObject->size) {
2560                                                         if (!ai_multiplayer_awareness(obj, ROBOT_FIRE_AGITATION-2))
2561                                                                 return;
2562                                                         do_ai_robot_hit_attack(obj, ConsoleObject, &obj->pos);
2563                                                 } else {
2564                                                         // mprintf((0, "Green won't fire: Too far: dist = %7.3f, threshold = %7.3f\n", f2fl(dist_to_player), f2fl(obj->size + ConsoleObject->size + F1_0*2)));
2565                                                         return;
2566                                                 }
2567                                         } else {
2568                                                 if ((gun_point->x == 0) && (gun_point->y == 0) && (gun_point->z == 0)) {
2569                                                         ; //mprintf((0, "Would like to fire gun, but gun not selected.\n"));
2570                                                 } else {
2571                                                         if (!ai_multiplayer_awareness(obj, ROBOT_FIRE_AGITATION))
2572                                                                 return;
2573                                                         //      New, multi-weapon-type system, 06/05/95 (life is slipping away...)
2574                                                         if (gun_num != 0) {
2575                                                                 if (ailp->next_fire <= 0)
2576                                                                         ai_fire_laser_at_player(obj, gun_point, gun_num, &Last_fired_upon_player_pos);
2577
2578                                                                 if ((ailp->next_fire2 <= 0) && (robptr->weapon_type2 != -1)) {
2579                                                                         calc_gun_point(gun_point, obj, 0);
2580                                                                         ai_fire_laser_at_player(obj, gun_point, 0, &Last_fired_upon_player_pos);
2581                                                                 }
2582
2583                                                         } else if (ailp->next_fire <= 0)
2584                                                                 ai_fire_laser_at_player(obj, gun_point, gun_num, &Last_fired_upon_player_pos);
2585                                                 }
2586                                         }
2587
2588                                         //      Wants to fire, so should go into chase mode, probably.
2589                                         if ( (aip->behavior != AIB_RUN_FROM) && (aip->behavior != AIB_STILL) && (aip->behavior != AIB_SNIPE) && (aip->behavior != AIB_FOLLOW) && ((ailp->mode == AIM_FOLLOW_PATH) || (ailp->mode == AIM_STILL)))
2590                                                 ailp->mode = AIM_CHASE_OBJECT;
2591                                 }
2592                                 aip->GOAL_STATE = AIS_RECO;
2593                                 ailp->goal_state[aip->CURRENT_GUN] = AIS_RECO;
2594
2595                                 // Switch to next gun for next fire.
2596                                 aip->CURRENT_GUN++;
2597                                 if (aip->CURRENT_GUN >= Robot_info[obj->id].n_guns)
2598                                 {
2599                                         if (Robot_info[obj->id].n_guns == 1)
2600                                                 aip->CURRENT_GUN = 0;
2601                                         else
2602                                                 aip->CURRENT_GUN = 1;
2603                                 }
2604                         }
2605                 }
2606                 }
2607
2608
2609         //      ---------------------------------------------------------------
2610
2611
2612         }
2613
2614 }
2615
2616