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