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