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