]> icculus.org git repositories - btb/d2x.git/blob - main/ai.c
remove rcs tags
[btb/d2x.git] / main / ai.c
1 /*
2 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
3 SOFTWARE CORPORATION ("PARALLAX").  PARALLAX, IN DISTRIBUTING THE CODE TO
4 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
5 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
6 IN USING, DISPLAYING,  AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
7 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
8 FREE PURPOSES.  IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
9 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES.  THE END-USER UNDERSTANDS
10 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
11 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION.  ALL RIGHTS RESERVED.
12 */
13
14 /*
15  *
16  * Autonomous Individual movement.
17  *
18  */
19
20
21 #ifdef HAVE_CONFIG_H
22 #include <conf.h>
23 #endif
24
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <time.h>
28
29 #include "inferno.h"
30 #include "game.h"
31 #include "mono.h"
32 #include "3d.h"
33
34 #include "object.h"
35 #include "render.h"
36 #include "error.h"
37 #include "ai.h"
38 #include "laser.h"
39 #include "fvi.h"
40 #include "polyobj.h"
41 #include "bm.h"
42 #include "weapon.h"
43 #include "physics.h"
44 #include "collide.h"
45 #include "player.h"
46 #include "wall.h"
47 #include "vclip.h"
48 #include "fireball.h"
49 #include "morph.h"
50 #include "effects.h"
51 #include "timer.h"
52 #include "sounds.h"
53 #include "cntrlcen.h"
54 #include "multibot.h"
55 #ifdef NETWORK
56 #include "multi.h"
57 #include "network.h"
58 #endif
59 #include "gameseq.h"
60 #include "key.h"
61 #include "powerup.h"
62 #include "gauges.h"
63 #include "text.h"
64 #include "fuelcen.h"
65 #include "controls.h"
66 #include "kconfig.h"
67
68 #ifdef EDITOR
69 #include "editor/editor.h"
70 #endif
71
72 #include "string.h"
73
74 #ifndef NDEBUG
75 #include <time.h>
76 #endif
77
78 // ---------- John: These variables must be saved as part of gamesave. --------
79 int             Ai_initialized = 0;
80 int             Overall_agitation;
81 ai_local        Ai_local_info[MAX_OBJECTS];
82 point_seg       Point_segs[MAX_POINT_SEGS];
83 point_seg       *Point_segs_free_ptr = Point_segs;
84 ai_cloak_info   Ai_cloak_info[MAX_AI_CLOAK_INFO];
85 fix             Boss_cloak_start_time = 0;
86 fix             Boss_cloak_end_time = 0;
87 fix             Last_teleport_time = 0;
88 fix             Boss_teleport_interval = F1_0*8;
89 fix             Boss_cloak_interval = F1_0*10;                    //    Time between cloaks
90 fix             Boss_cloak_duration = BOSS_CLOAK_DURATION;
91 fix             Last_gate_time = 0;
92 fix             Gate_interval = F1_0*6;
93 fix             Boss_dying_start_time;
94 fix             Boss_hit_time;
95 sbyte           Boss_dying, Boss_dying_sound_playing, unused123, unused234;
96
97 // -- MK, 10/21/95, unused! -- int             Boss_been_hit=0;
98
99
100 // ------ John: End of variables which must be saved as part of gamesave. -----
101
102
103 // -- ubyte Boss_cloaks[NUM_D2_BOSSES]              = {1,1,1,1,1,1};      // Set byte if this boss can cloak
104
105 ubyte Boss_teleports[NUM_D2_BOSSES]           = {1,1,1,1,1,1, 1,1}; // Set byte if this boss can teleport
106 ubyte Boss_spew_more[NUM_D2_BOSSES]           = {0,1,0,0,0,0, 0,0}; // If set, 50% of time, spew two bots.
107 ubyte Boss_spews_bots_energy[NUM_D2_BOSSES]   = {1,1,0,1,0,1, 1,1}; // Set byte if boss spews bots when hit by energy weapon.
108 ubyte Boss_spews_bots_matter[NUM_D2_BOSSES]   = {0,0,1,1,1,1, 0,1}; // Set byte if boss spews bots when hit by matter weapon.
109 ubyte Boss_invulnerable_energy[NUM_D2_BOSSES] = {0,0,1,1,0,0, 0,0}; // Set byte if boss is invulnerable to energy weapons.
110 ubyte Boss_invulnerable_matter[NUM_D2_BOSSES] = {0,0,0,0,1,1, 1,0}; // Set byte if boss is invulnerable to matter weapons.
111 ubyte Boss_invulnerable_spot[NUM_D2_BOSSES]   = {0,0,0,0,0,1, 0,1}; // Set byte if boss is invulnerable in all but a certain spot.  (Dot product fvec|vec_to_collision < BOSS_INVULNERABLE_DOT)
112
113 int ai_evaded=0;
114
115 // -- sbyte Super_boss_gate_list[MAX_GATE_INDEX] = {0, 1, 8, 9, 10, 11, 12, 15, 16, 18, 19, 20, 22, 0, 8, 11, 19, 20, 8, 20, 8};
116
117 int Robot_firing_enabled = 1;
118 int Animation_enabled = 1;
119
120 #ifndef NDEBUG
121 int Ai_info_enabled=0;
122 #endif
123
124
125 // These globals are set by a call to find_vector_intersection, which is a slow routine,
126 // so we don't want to call it again (for this object) unless we have to.
127 vms_vector  Hit_pos;
128 int         Hit_type, Hit_seg;
129 fvi_info    Hit_data;
130
131 int             Num_awareness_events = 0;
132 awareness_event Awareness_events[MAX_AWARENESS_EVENTS];
133
134 vms_vector      Believed_player_pos;
135 int             Believed_player_seg;
136
137 #ifndef NDEBUG
138 // Index into this array with ailp->mode
139 char *mode_text[18] = {
140         "STILL",
141         "WANDER",
142         "FOL_PATH",
143         "CHASE_OBJ",
144         "RUN_FROM",
145         "BEHIND",
146         "FOL_PATH2",
147         "OPEN_DOOR",
148         "GOTO_PLR",
149         "GOTO_OBJ",
150         "SN_ATT",
151         "SN_FIRE",
152         "SN_RETR",
153         "SN_RTBK",
154         "SN_WAIT",
155         "TH_ATTACK",
156         "TH_RETREAT",
157         "TH_WAIT",
158 };
159
160 //      Index into this array with aip->behavior
161 char behavior_text[6][9] = {
162         "STILL   ",
163         "NORMAL  ",
164         "HIDE    ",
165         "RUN_FROM",
166         "FOLPATH ",
167         "STATION "
168 };
169
170 // Index into this array with aip->GOAL_STATE or aip->CURRENT_STATE
171 char state_text[8][5] = {
172         "NONE",
173         "REST",
174         "SRCH",
175         "LOCK",
176         "FLIN",
177         "FIRE",
178         "RECO",
179         "ERR_",
180 };
181
182
183 #endif
184
185 // Current state indicates where the robot current is, or has just done.
186 // Transition table between states for an AI object.
187 // First dimension is trigger event.
188 // Second dimension is current state.
189 // Third dimension is goal state.
190 // Result is new goal state.
191 // ERR_ means something impossible has happened.
192 sbyte Ai_transition_table[AI_MAX_EVENT][AI_MAX_STATE][AI_MAX_STATE] = {
193         {
194                 // Event = AIE_FIRE, a nearby object fired
195                 // none     rest      srch      lock      flin      fire      reco        // CURRENT is rows, GOAL is columns
196                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO }, // none
197                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO }, // rest
198                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO }, // search
199                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO }, // lock
200                 { AIS_ERR_, AIS_REST, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FIRE, AIS_RECO }, // flinch
201                 { AIS_ERR_, AIS_FIRE, AIS_FIRE, AIS_FIRE, AIS_FLIN, AIS_FIRE, AIS_RECO }, // fire
202                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_FIRE }  // recoil
203         },
204
205         // Event = AIE_HITT, a nearby object was hit (or a wall was hit)
206         {
207                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
208                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
209                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
210                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
211                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FLIN},
212                 { AIS_ERR_, AIS_REST, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FIRE, AIS_RECO},
213                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_FIRE}
214         },
215
216         // Event = AIE_COLL, player collided with robot
217         {
218                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
219                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
220                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
221                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_RECO},
222                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_LOCK, AIS_FLIN, AIS_FLIN},
223                 { AIS_ERR_, AIS_REST, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FIRE, AIS_RECO},
224                 { AIS_ERR_, AIS_LOCK, AIS_LOCK, AIS_LOCK, AIS_FLIN, AIS_FIRE, AIS_FIRE}
225         },
226
227         // Event = AIE_HURT, player hurt robot (by firing at and hitting it)
228         // Note, this doesn't necessarily mean the robot JUST got hit, only that that is the most recent thing that happened.
229         {
230                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN},
231                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN},
232                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN},
233                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN},
234                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN},
235                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN},
236                 { AIS_ERR_, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN, AIS_FLIN}
237         }
238 };
239
240
241
242 fix Dist_to_last_fired_upon_player_pos = 0;
243
244 // ----------------------------------------------------------------------------
245 void init_ai_frame(void)
246 {
247         int ab_state;
248
249         Dist_to_last_fired_upon_player_pos = vm_vec_dist_quick(&Last_fired_upon_player_pos, &Believed_player_pos);
250
251         ab_state = Afterburner_charge && Controls.afterburner_state && (Players[Player_num].flags & PLAYER_FLAGS_AFTERBURNER);
252
253         if (!(Players[Player_num].flags & PLAYER_FLAGS_CLOAKED) || (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON) || ab_state) {
254                 ai_do_cloak_stuff();
255         }
256 }
257
258 // ----------------------------------------------------------------------------
259 // Return firing status.
260 // If ready to fire a weapon, return true, else return false.
261 // Ready to fire a weapon if next_fire <= 0 or next_fire2 <= 0.
262 int ready_to_fire(robot_info *robptr, ai_local *ailp)
263 {
264         if (robptr->weapon_type2 != -1)
265                 return (ailp->next_fire <= 0) || (ailp->next_fire2 <= 0);
266         else
267                 return (ailp->next_fire <= 0);
268 }
269
270 // ----------------------------------------------------------------------------
271 // Make a robot near the player snipe.
272 #define MNRS_SEG_MAX    70
273 void make_nearby_robot_snipe(void)
274 {
275         int bfs_length, i;
276         short bfs_list[MNRS_SEG_MAX];
277
278         create_bfs_list(ConsoleObject->segnum, bfs_list, &bfs_length, MNRS_SEG_MAX);
279
280         for (i=0; i<bfs_length; i++) {
281                 int objnum = Segments[bfs_list[i]].objects;
282
283                 while (objnum != -1) {
284                         object *objp = &Objects[objnum];
285                         robot_info *robptr = &Robot_info[objp->id];
286
287                         if ((objp->type == OBJ_ROBOT) && (objp->id != ROBOT_BRAIN)) {
288                                 if ((objp->ctype.ai_info.behavior != AIB_SNIPE) && (objp->ctype.ai_info.behavior != AIB_RUN_FROM) && !Robot_info[objp->id].boss_flag && !robptr->companion) {
289                                         objp->ctype.ai_info.behavior = AIB_SNIPE;
290                                         Ai_local_info[objnum].mode = AIM_SNIPE_ATTACK;
291                                         mprintf((0, "Making robot #%i go into snipe mode!\n", objnum));
292                                         return;
293                                 }
294                         }
295                         objnum = objp->next;
296                 }
297         }
298
299         mprintf((0, "Couldn't find a robot to make snipe!\n"));
300
301 }
302
303 int Ai_last_missile_camera;
304
305 int Robots_kill_robots_cheat = 0;
306
307 // --------------------------------------------------------------------------------------------------------------------
308 void do_ai_frame(object *obj)
309 {
310         int         objnum = OBJECT_NUMBER(obj);
311         ai_static   *aip = &obj->ctype.ai_info;
312         ai_local    *ailp = &Ai_local_info[objnum];
313         fix         dist_to_player;
314         vms_vector  vec_to_player;
315         fix         dot;
316         robot_info  *robptr;
317         int         player_visibility=-1;
318         int         obj_ref;
319         int         object_animates;
320         int         new_goal_state;
321         int         visibility_and_vec_computed = 0;
322         int         previous_visibility;
323         vms_vector  gun_point;
324         vms_vector  vis_vec_pos;
325
326         ailp->next_action_time -= FrameTime;
327
328         if (aip->SKIP_AI_COUNT) {
329                 aip->SKIP_AI_COUNT--;
330                 if (obj->mtype.phys_info.flags & PF_USES_THRUST) {
331                         obj->mtype.phys_info.rotthrust.x = (obj->mtype.phys_info.rotthrust.x * 15)/16;
332                         obj->mtype.phys_info.rotthrust.y = (obj->mtype.phys_info.rotthrust.y * 15)/16;
333                         obj->mtype.phys_info.rotthrust.z = (obj->mtype.phys_info.rotthrust.z * 15)/16;
334                         if (!aip->SKIP_AI_COUNT)
335                                 obj->mtype.phys_info.flags &= ~PF_USES_THRUST;
336                 }
337                 return;
338         }
339
340         robptr = &Robot_info[obj->id];
341         Assert(robptr->always_0xabcd == 0xabcd);
342
343         if (do_any_robot_dying_frame(obj))
344                 return;
345
346         // Kind of a hack.  If a robot is flinching, but it is time for it to fire, unflinch it.
347         // Else, you can turn a big nasty robot into a wimp by firing flares at it.
348         // This also allows the player to see the cool flinch effect for mechs without unbalancing the game.
349         if ((aip->GOAL_STATE == AIS_FLIN) && ready_to_fire(robptr, ailp)) {
350                 aip->GOAL_STATE = AIS_FIRE;
351         }
352
353 #ifndef NDEBUG
354         if ((aip->behavior == AIB_RUN_FROM) && (ailp->mode != AIM_RUN_FROM_OBJECT))
355                 Int3(); // This is peculiar.  Behavior is run from, but mode is not.  Contact Mike.
356
357         mprintf_animation_info((obj));
358
359         if (!Do_ai_flag)
360                 return;
361
362         if (Break_on_object != -1)
363                 if (OBJECT_NUMBER(obj) == Break_on_object)
364                         Int3(); // Contact Mike: This is a debug break
365 #endif
366
367         //mprintf((0, "Object %i: behavior = %02x, mode = %i, awareness = %i, time = %7.3f\n", OBJECT_NUMBER(obj), aip->behavior, ailp->mode, ailp->player_awareness_type, f2fl(ailp->player_awareness_time)));
368         //mprintf((0, "Object %i: behavior = %02x, mode = %i, awareness = %i, cur=%i, goal=%i\n", OBJECT_NUMBER(obj), aip->behavior, ailp->mode, ailp->player_awareness_type, aip->CURRENT_STATE, aip->GOAL_STATE));
369
370         //Assert((aip->behavior >= MIN_BEHAVIOR) && (aip->behavior <= MAX_BEHAVIOR));
371         if (!((aip->behavior >= MIN_BEHAVIOR) && (aip->behavior <= MAX_BEHAVIOR))) {
372                 //mprintf((0, "Object %i behavior is %i, setting to AIB_NORMAL, fix in editor!\n", objnum, aip->behavior));
373                 aip->behavior = AIB_NORMAL;
374         }
375
376         Assert(obj->segnum != -1);
377         Assert(obj->id < N_robot_types);
378
379         obj_ref = objnum ^ FrameCount;
380
381         if (ailp->next_fire > -F1_0*8)
382                 ailp->next_fire -= FrameTime;
383
384         if (robptr->weapon_type2 != -1) {
385                 if (ailp->next_fire2 > -F1_0*8)
386                         ailp->next_fire2 -= FrameTime;
387         } else
388                 ailp->next_fire2 = F1_0*8;
389
390         if (ailp->time_since_processed < F1_0*256)
391                 ailp->time_since_processed += FrameTime;
392
393         previous_visibility = ailp->previous_visibility;    //  Must get this before we toast the master copy!
394
395         // -- (No robots have this behavior...)
396         // -- // Deal with cloaking for robots which are cloaked except just before firing.
397         // -- if (robptr->cloak_type == RI_CLOAKED_EXCEPT_FIRING)
398         // --   if (ailp->next_fire < F1_0/2)
399         // --           aip->CLOAKED = 1;
400         // --   else
401         // --           aip->CLOAKED = 0;
402
403         // If only awake because of a camera, make that the believed player position.
404         if ((aip->SUB_FLAGS & SUB_FLAGS_CAMERA_AWAKE) && (Ai_last_missile_camera != -1))
405                 Believed_player_pos = Objects[Ai_last_missile_camera].pos;
406         else {
407                 if (Robots_kill_robots_cheat) {
408                         vis_vec_pos = obj->pos;
409                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
410                         if (player_visibility) {
411                                 int ii, min_obj = -1;
412                                 fix min_dist = F1_0*200, cur_dist;
413
414                                 for (ii=0; ii<=Highest_object_index; ii++)
415                                         if ((Objects[ii].type == OBJ_ROBOT) && (ii != objnum)) {
416                                                 cur_dist = vm_vec_dist_quick(&obj->pos, &Objects[ii].pos);
417
418                                                 if (cur_dist < F1_0*100)
419                                                         if (object_to_object_visibility(obj, &Objects[ii], FQ_TRANSWALL))
420                                                                 if (cur_dist < min_dist) {
421                                                                         min_obj = ii;
422                                                                         min_dist = cur_dist;
423                                                                 }
424                                         }
425                                 if (min_obj != -1) {
426                                         Believed_player_pos = Objects[min_obj].pos;
427                                         Believed_player_seg = Objects[min_obj].segnum;
428                                         vm_vec_normalized_dir_quick(&vec_to_player, &Believed_player_pos, &obj->pos);
429                                 } else
430                                         goto _exit_cheat;
431                         } else
432                                 goto _exit_cheat;
433                 } else {
434 _exit_cheat:
435                         visibility_and_vec_computed = 0;
436                         if (!(Players[Player_num].flags & PLAYER_FLAGS_CLOAKED))
437                                 Believed_player_pos = ConsoleObject->pos;
438                         else
439                                 Believed_player_pos = Ai_cloak_info[objnum & (MAX_AI_CLOAK_INFO-1)].last_position;
440                 }
441         }
442         dist_to_player = vm_vec_dist_quick(&Believed_player_pos, &obj->pos);
443         //if (robptr->companion)
444         //      mprintf((0, "%3i: %3i %8.3f %8s %8s [%3i %4i]\n", objnum, obj->segnum, f2fl(dist_to_player), mode_text[ailp->mode], behavior_text[aip->behavior-0x80], aip->hide_index, aip->path_length));
445
446         // If this robot can fire, compute visibility from gun position.
447         // Don't want to compute visibility twice, as it is expensive.  (So is call to calc_gun_point).
448         if ((previous_visibility || !(obj_ref & 3)) && ready_to_fire(robptr, ailp) && (dist_to_player < F1_0*200) && (robptr->n_guns) && !(robptr->attack_type)) {
449                 // Since we passed ready_to_fire(), either next_fire or next_fire2 <= 0.  calc_gun_point from relevant one.
450                 // If both are <= 0, we will deal with the mess in ai_do_actual_firing_stuff
451                 if (ailp->next_fire <= 0)
452                         calc_gun_point(&gun_point, obj, aip->CURRENT_GUN);
453                 else
454                         calc_gun_point(&gun_point, obj, 0);
455                 vis_vec_pos = gun_point;
456         } else {
457                 vis_vec_pos = obj->pos;
458                 vm_vec_zero(&gun_point);
459                 //mprintf((0, "Visibility = %i, computed from center.\n", player_visibility));
460         }
461
462 // MK: Debugging, July 26, 1995!
463 // if (objnum == 1)
464 // {
465 //      compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
466 //      mprintf((0, "Frame %i: dist=%7.3f, vecdot = %7.3f, mode=%i\n", FrameCount, f2fl(dist_to_player), f2fl(vm_vec_dot(&vec_to_player, &obj->orient.fvec)), ailp->mode));
467 // }
468         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - 
469         // Occasionally make non-still robots make a path to the player.  Based on agitation and distance from player.
470         if ((aip->behavior != AIB_SNIPE) && (aip->behavior != AIB_RUN_FROM) && (aip->behavior != AIB_STILL) && !(Game_mode & GM_MULTI) && (robptr->companion != 1) && (robptr->thief != 1))
471                 if (Overall_agitation > 70) {
472                         if ((dist_to_player < F1_0*200) && (d_rand() < FrameTime/4)) {
473                                 if (d_rand() * (Overall_agitation - 40) > F1_0*5) {
474                                         // -- mprintf((0, "(1) Object #%i going from still to path in frame %i.\n", objnum, FrameCount));
475                                         create_path_to_player(obj, 4 + Overall_agitation/8 + Difficulty_level, 1);
476                                         return;
477                                 }
478                         }
479                 }
480
481         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
482         // If retry count not 0, then add it into consecutive_retries.
483         // If it is 0, cut down consecutive_retries.
484         // This is largely a hack to speed up physics and deal with stupid
485         // AI.  This is low level communication between systems of a sort
486         // that should not be done.
487         if ((ailp->retry_count) && !(Game_mode & GM_MULTI)) {
488                 ailp->consecutive_retries += ailp->retry_count;
489                 ailp->retry_count = 0;
490                 if (ailp->consecutive_retries > 3) {
491                         switch (ailp->mode) {
492                                 case AIM_GOTO_PLAYER:
493                                         // -- mprintf((0, "Buddy stuck going to player...\n"));
494                                         // -- Buddy_got_stuck = 1;
495                                         move_towards_segment_center(obj);
496                                         create_path_to_player(obj, 100, 1);
497                                         // -- Buddy_got_stuck = 0;
498                                         break;
499                                 case AIM_GOTO_OBJECT:
500                                         // -- mprintf((0, "Buddy stuck going to object...\n"));
501                                         Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
502                                         //if (obj->segnum == ConsoleObject->segnum) {
503                                         //      if (Point_segs[aip->hide_index + aip->cur_path_index].segnum == obj->segnum)
504                                         //              if ((aip->cur_path_index + aip->PATH_DIR >= 0) && (aip->cur_path_index + aip->PATH_DIR < aip->path_length-1))
505                                         //                      aip->cur_path_index += aip->PATH_DIR;
506                                         //}
507                                         break;
508                                 case AIM_CHASE_OBJECT:
509                                         // -- mprintf((0, "(2) Object #%i, retries while chasing, creating path to player in frame %i\n", objnum, FrameCount));
510                                         create_path_to_player(obj, 4 + Overall_agitation/8 + Difficulty_level, 1);
511                                         break;
512                                 case AIM_STILL:
513                                         if (robptr->attack_type)
514                                                 move_towards_segment_center(obj);
515                                         else if (!((aip->behavior == AIB_STILL) || (aip->behavior == AIB_STATION) || (aip->behavior == AIB_FOLLOW)))    // Behavior is still, so don't follow path.
516                                                 attempt_to_resume_path(obj);
517                                         break;
518                                 case AIM_FOLLOW_PATH:
519                                         // mprintf((0, "Object %i following path got %i retries in frame %i\n", OBJECT_NUMBER(obj), ailp->consecutive_retries, FrameCount));
520                                         if (Game_mode & GM_MULTI) {
521                                                 ailp->mode = AIM_STILL;
522                                         } else
523                                                 attempt_to_resume_path(obj);
524                                         break;
525                                 case AIM_RUN_FROM_OBJECT:
526                                         move_towards_segment_center(obj);
527                                         obj->mtype.phys_info.velocity.x = 0;
528                                         obj->mtype.phys_info.velocity.y = 0;
529                                         obj->mtype.phys_info.velocity.z = 0;
530                                         create_n_segment_path(obj, 5, -1);
531                                         ailp->mode = AIM_RUN_FROM_OBJECT;
532                                         break;
533                                 case AIM_BEHIND:
534                                         mprintf((0, "Hiding robot (%i) collided much.\n", OBJECT_NUMBER(obj)));
535                                         move_towards_segment_center(obj);
536                                         obj->mtype.phys_info.velocity.x = 0;
537                                         obj->mtype.phys_info.velocity.y = 0;
538                                         obj->mtype.phys_info.velocity.z = 0;
539                                         break;
540                                 case AIM_OPEN_DOOR:
541                                         create_n_segment_path_to_door(obj, 5, -1);
542                                         break;
543                                 #ifndef NDEBUG
544                                 case AIM_FOLLOW_PATH_2:
545                                         Int3(); // Should never happen!
546                                         break;
547                                 #endif
548                         }
549                         ailp->consecutive_retries = 0;
550                 }
551         } else
552                 ailp->consecutive_retries /= 2;
553
554         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
555         // If in materialization center, exit
556         if (!(Game_mode & GM_MULTI) && (Segment2s[obj->segnum].special == SEGMENT_IS_ROBOTMAKER)) {
557                 if (Station[Segment2s[obj->segnum].value].Enabled) {
558                         ai_follow_path(obj, 1, 1, NULL);    // 1 = player is visible, which might be a lie, but it works.
559                         return;
560                 }
561         }
562
563         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
564         // Decrease player awareness due to the passage of time.
565         if (ailp->player_awareness_type) {
566                 if (ailp->player_awareness_time > 0) {
567                         ailp->player_awareness_time -= FrameTime;
568                         if (ailp->player_awareness_time <= 0) {
569                                 ailp->player_awareness_time = F1_0*2;   //new: 11/05/94
570                                 ailp->player_awareness_type--;          //new: 11/05/94
571                         }
572                 } else {
573                         ailp->player_awareness_type--;
574                         ailp->player_awareness_time = F1_0*2;
575                         //aip->GOAL_STATE = AIS_REST;
576                 }
577         } else
578                 aip->GOAL_STATE = AIS_REST;                     //new: 12/13/94
579
580
581         if (Player_is_dead && (ailp->player_awareness_type == 0))
582                 if ((dist_to_player < F1_0*200) && (d_rand() < FrameTime/8)) {
583                         if ((aip->behavior != AIB_STILL) && (aip->behavior != AIB_RUN_FROM)) {
584                                 if (!ai_multiplayer_awareness(obj, 30))
585                                         return;
586                                 #ifndef SHAREWARE
587                                 ai_multi_send_robot_position(objnum, -1);
588                                 #endif
589
590                                 if (!((ailp->mode == AIM_FOLLOW_PATH) && (aip->cur_path_index < aip->path_length-1)))
591                                         if ((aip->behavior != AIB_SNIPE) && (aip->behavior != AIB_RUN_FROM)) {
592                                                 if (dist_to_player < F1_0*30)
593                                                         create_n_segment_path(obj, 5, 1);
594                                                 else
595                                                         create_path_to_player(obj, 20, 1);
596                                         }
597                         }
598                 }
599
600         // -- // Make sure that if this guy got hit or bumped, then he's chasing player.
601         // -- if ((ailp->player_awareness_type == PA_WEAPON_ROBOT_COLLISION) || (ailp->player_awareness_type >= PA_PLAYER_COLLISION)) {
602         // --   if ((ailp->mode != AIM_BEHIND) && (aip->behavior != AIB_STILL) && (aip->behavior != AIB_SNIPE) && (aip->behavior != AIB_RUN_FROM) && (!robptr->companion) && (!robptr->thief) && (obj->id != ROBOT_BRAIN)) {
603         // --           ailp->mode = AIM_CHASE_OBJECT;
604         // --           ailp->player_awareness_type = 0;
605         // --           ailp->player_awareness_time = 0;
606         // --   }
607         // -- }
608
609         // Make sure that if this guy got hit or bumped, then he's chasing player.
610         if ((ailp->player_awareness_type == PA_WEAPON_ROBOT_COLLISION) || (ailp->player_awareness_type >= PA_PLAYER_COLLISION)) {
611                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
612                 if (player_visibility == 1) // Only increase visibility if unobstructed, else claw guys attack through doors.
613                         player_visibility = 2;
614         } else if (((obj_ref&3) == 0) && !previous_visibility && (dist_to_player < F1_0*100)) {
615                 fix sval, rval;
616
617                 rval = d_rand();
618                 sval = (dist_to_player * (Difficulty_level+1))/64;
619
620                 // -- mprintf((0, "Object #%3i: dist = %7.3f, rval = %8x, sval = %8x", OBJECT_NUMBER(obj), f2fl(dist_to_player), rval, sval));
621                 if ((fixmul(rval, sval) < FrameTime) || (Players[Player_num].flags & PLAYER_FLAGS_HEADLIGHT_ON)) {
622                         ailp->player_awareness_type = PA_PLAYER_COLLISION;
623                         ailp->player_awareness_time = F1_0*3;
624                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
625                         if (player_visibility == 1) {
626                                 player_visibility = 2;
627                                 // -- mprintf((0, "...SWITCH!"));
628                         }
629                 }
630
631                 // -- mprintf((0, "\n"));
632         }
633
634
635         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
636         if ((aip->GOAL_STATE == AIS_FLIN) && (aip->CURRENT_STATE == AIS_FLIN))
637                 aip->GOAL_STATE = AIS_LOCK;
638
639         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
640         // Note: Should only do these two function calls for objects which animate
641         if (Animation_enabled && (dist_to_player < F1_0*100)) { // && !(Game_mode & GM_MULTI)) {
642                 object_animates = do_silly_animation(obj);
643                 if (object_animates)
644                         ai_frame_animation(obj);
645                 //mprintf((0, "Object %i: goal=%i, current=%i\n", OBJECT_NUMBER(obj), obj->ctype.ai_info.GOAL_STATE, obj->ctype.ai_info.CURRENT_STATE));
646         } else {
647                 // If Object is supposed to animate, but we don't let it animate due to distance, then
648                 // we must change its state, else it will never update.
649                 aip->CURRENT_STATE = aip->GOAL_STATE;
650                 object_animates = 0;        // If we're not doing the animation, then should pretend it doesn't animate.
651         }
652
653         switch (Robot_info[obj->id].boss_flag) {
654         case 0:
655                 break;
656
657         case 1:
658         case 2:
659                 mprintf((1, "Warning: D1 boss detected.  Not supported!\n"));
660                 break;
661
662         default:
663                 {
664                         int     pv;
665                         fix     dtp = dist_to_player/4;
666
667                         if (aip->GOAL_STATE == AIS_FLIN)
668                                 aip->GOAL_STATE = AIS_FIRE;
669                         if (aip->CURRENT_STATE == AIS_FLIN)
670                                 aip->CURRENT_STATE = AIS_FIRE;
671
672                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
673
674                         pv = player_visibility;
675
676                         // If player cloaked, visibility is screwed up and superboss will gate in robots when not supposed to.
677                         if (Players[Player_num].flags & PLAYER_FLAGS_CLOAKED) {
678                                 pv = 0;
679                                 dtp = vm_vec_dist_quick(&ConsoleObject->pos, &obj->pos)/4;
680                         }
681
682                         do_boss_stuff(obj, pv);
683                 }
684                 break;
685         }
686
687         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
688         // Time-slice, don't process all the time, purely an efficiency hack.
689         // Guys whose behavior is station and are not at their hide segment get processed anyway.
690         if (!((aip->behavior == AIB_SNIPE) && (ailp->mode != AIM_SNIPE_WAIT)) && !robptr->companion && !robptr->thief && (ailp->player_awareness_type < PA_WEAPON_ROBOT_COLLISION-1)) { // If robot got hit, he gets to attack player always!
691 #ifndef NDEBUG
692                 if (Break_on_object != objnum) {    // don't time slice if we're interested in this object.
693 #endif
694                         if ((aip->behavior == AIB_STATION) && (ailp->mode == AIM_FOLLOW_PATH) && (aip->hide_segment != obj->segnum)) {
695                                 if (dist_to_player > F1_0*250)  // station guys not at home always processed until 250 units away.
696                                         return;
697                         } else if ((!ailp->previous_visibility) && ((dist_to_player >> 7) > ailp->time_since_processed)) {  // 128 units away (6.4 segments) processed after 1 second.
698                                 if (robptr->thief)
699                                         mprintf((0, "T"));
700                                 return;
701                         }
702 #ifndef NDEBUG
703                 }
704 #endif
705         }
706
707         // Reset time since processed, but skew objects so not everything
708         // processed synchronously, else we get fast frames with the
709         // occasional very slow frame.
710         // AI_proc_time = ailp->time_since_processed;
711         ailp->time_since_processed = - ((objnum & 0x03) * FrameTime ) / 2;
712
713         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
714         //      Perform special ability
715         switch (obj->id) {
716                 case ROBOT_BRAIN:
717                         // Robots function nicely if behavior is Station.  This
718                         // means they won't move until they can see the player, at
719                         // which time they will start wandering about opening doors.
720                         if (ConsoleObject->segnum == obj->segnum) {
721                                 if (!ai_multiplayer_awareness(obj, 97))
722                                         return;
723                                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
724                                 move_away_from_player(obj, &vec_to_player, 0);
725                                 ai_multi_send_robot_position(objnum, -1);
726                         } else if (ailp->mode != AIM_STILL) {
727                                 int r;
728
729                                 r = openable_doors_in_segment(obj->segnum);
730                                 if (r != -1) {
731                                         ailp->mode = AIM_OPEN_DOOR;
732                                         aip->GOALSIDE = r;
733                                 } else if (ailp->mode != AIM_FOLLOW_PATH) {
734                                         if (!ai_multiplayer_awareness(obj, 50))
735                                                 return;
736                                         create_n_segment_path_to_door(obj, 8+Difficulty_level, -1);     // third parameter is avoid_seg, -1 means avoid nothing.
737                                         ai_multi_send_robot_position(objnum, -1);
738                                 }
739
740                                 if (ailp->next_action_time < 0) {
741                                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
742                                         if (player_visibility) {
743                                                 make_nearby_robot_snipe();
744                                                 ailp->next_action_time = (NDL - Difficulty_level) * 2*F1_0;
745                                         }
746                                 }
747                         } else {
748                                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
749                                 if (player_visibility) {
750                                         if (!ai_multiplayer_awareness(obj, 50))
751                                                 return;
752                                         create_n_segment_path_to_door(obj, 8+Difficulty_level, -1);     // third parameter is avoid_seg, -1 means avoid nothing.
753                                         ai_multi_send_robot_position(objnum, -1);
754                                 }
755                         }
756                         break;
757                 default:
758                         break;
759         }
760
761         if (aip->behavior == AIB_SNIPE) {
762                 if ((Game_mode & GM_MULTI) && !robptr->thief) {
763                         aip->behavior = AIB_NORMAL;
764                         ailp->mode = AIM_CHASE_OBJECT;
765                         return;
766                 }
767
768                 if (!(obj_ref & 3) || previous_visibility) {
769                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
770
771                         // If this sniper is in still mode, if he was hit or can see player, switch to snipe mode.
772                         if (ailp->mode == AIM_STILL)
773                                 if (player_visibility || (ailp->player_awareness_type == PA_WEAPON_ROBOT_COLLISION))
774                                         ailp->mode = AIM_SNIPE_ATTACK;
775
776                         if (!robptr->thief && (ailp->mode != AIM_STILL))
777                                 do_snipe_frame(obj, dist_to_player, player_visibility, &vec_to_player);
778                 } else if (!robptr->thief && !robptr->companion)
779                         return;
780         }
781
782         // More special ability stuff, but based on a property of a robot, not its ID.
783         if (robptr->companion) {
784
785                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
786                 do_escort_frame(obj, dist_to_player, player_visibility);
787
788                 if (obj->ctype.ai_info.danger_laser_num != -1) {
789                         object *dobjp = &Objects[obj->ctype.ai_info.danger_laser_num];
790
791                         if ((dobjp->type == OBJ_WEAPON) && (dobjp->signature == obj->ctype.ai_info.danger_laser_signature)) {
792                                 fix circle_distance;
793                                 // -- mprintf((0, "Evading!  "));
794                                 circle_distance = robptr->circle_distance[Difficulty_level] + ConsoleObject->size;
795                                 ai_move_relative_to_player(obj, ailp, dist_to_player, &vec_to_player, circle_distance, 1, player_visibility);
796                         }
797                 }
798
799                 if (ready_to_fire(robptr, ailp)) {
800                         int do_stuff = 0;
801                         if (openable_doors_in_segment(obj->segnum) != -1)
802                                 do_stuff = 1;
803                         else if (openable_doors_in_segment(Point_segs[aip->hide_index + aip->cur_path_index + aip->PATH_DIR].segnum) != -1)
804                                 do_stuff = 1;
805                         else if (openable_doors_in_segment(Point_segs[aip->hide_index + aip->cur_path_index + 2*aip->PATH_DIR].segnum) != -1)
806                                 do_stuff = 1;
807                         else if ((ailp->mode == AIM_GOTO_PLAYER) && (dist_to_player < 3*MIN_ESCORT_DISTANCE/2) && (vm_vec_dot(&ConsoleObject->orient.fvec, &vec_to_player) > -F1_0/4)) {
808                                 // mprintf((0, "Firing at player because dot = %7.3f\n", f2fl(vm_vec_dot(&ConsoleObject->orient.fvec, &vec_to_player))));
809                                 do_stuff = 1;
810                         } else
811                                 ; // mprintf((0, "Not Firing at player because dot = %7.3f, dist = %7.3f\n", f2fl(vm_vec_dot(&ConsoleObject->orient.fvec, &vec_to_player)), f2fl(dist_to_player)));
812
813                         if (do_stuff) {
814                                 Laser_create_new_easy( &obj->orient.fvec, &obj->pos, OBJECT_NUMBER(obj), FLARE_ID, 1 );
815                                 ailp->next_fire = F1_0/2;
816                                 if (!Buddy_allowed_to_talk) // If buddy not talking, make him fire flares less often.
817                                         ailp->next_fire += d_rand()*4;
818                         }
819
820                 }
821         }
822
823         if (robptr->thief) {
824
825                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
826                 do_thief_frame(obj, dist_to_player, player_visibility, &vec_to_player);
827
828                 if (ready_to_fire(robptr, ailp)) {
829                         int do_stuff = 0;
830                         if (openable_doors_in_segment(obj->segnum) != -1)
831                                 do_stuff = 1;
832                         else if (openable_doors_in_segment(Point_segs[aip->hide_index + aip->cur_path_index + aip->PATH_DIR].segnum) != -1)
833                                 do_stuff = 1;
834                         else if (openable_doors_in_segment(Point_segs[aip->hide_index + aip->cur_path_index + 2*aip->PATH_DIR].segnum) != -1)
835                                 do_stuff = 1;
836
837                         if (do_stuff) {
838                                 // @mk, 05/08/95: Firing flare from center of object, this is dumb...
839                                 Laser_create_new_easy( &obj->orient.fvec, &obj->pos, OBJECT_NUMBER(obj), FLARE_ID, 1 );
840                                 ailp->next_fire = F1_0/2;
841                                 if (Stolen_item_index == 0)     // If never stolen an item, fire flares less often (bad: Stolen_item_index wraps, but big deal)
842                                         ailp->next_fire += d_rand()*4;
843                         }
844                 }
845         }
846
847         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
848         switch (ailp->mode) {
849                 case AIM_CHASE_OBJECT: {        // chasing player, sort of, chase if far, back off if close, circle in between
850                         fix circle_distance;
851
852                         circle_distance = robptr->circle_distance[Difficulty_level] + ConsoleObject->size;
853                         // Green guy doesn't get his circle distance boosted, else he might never attack.
854                         if (robptr->attack_type != 1)
855                                 circle_distance += (objnum&0xf) * F1_0/2;
856
857                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
858
859                         // @mk, 12/27/94, structure here was strange.  Would do both clauses of what are now this if/then/else.  Used to be if/then, if/then.
860                         if ((player_visibility < 2) && (previous_visibility == 2)) { // this is redundant: mk, 01/15/95: && (ailp->mode == AIM_CHASE_OBJECT)) {
861                                 // -- mprintf((0, "I used to be able to see the player!\n"));
862                                 if (!ai_multiplayer_awareness(obj, 53)) {
863                                         if (maybe_ai_do_actual_firing_stuff(obj, aip))
864                                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
865                                         return;
866                                 }
867                                 // -- mprintf((0, "(3) Object #%i going from chase to player path in frame %i.\n", objnum, FrameCount));
868                                 create_path_to_player(obj, 8, 1);
869                                 ai_multi_send_robot_position(objnum, -1);
870                         } else if ((player_visibility == 0) && (dist_to_player > F1_0*80) && (!(Game_mode & GM_MULTI))) {
871                                 // If pretty far from the player, player cannot be seen
872                                 // (obstructed) and in chase mode, switch to follow path mode.
873                                 // This has one desirable benefit of avoiding physics retries.
874                                 if (aip->behavior == AIB_STATION) {
875                                         ailp->goal_segment = aip->hide_segment;
876                                         // -- mprintf((0, "(1) Object #%i going from chase to STATION in frame %i.\n", objnum, FrameCount));
877                                         create_path_to_station(obj, 15);
878                                 } // -- this looks like a dumb thing to do...robots following paths far away from you! else create_n_segment_path(obj, 5, -1);
879                                 break;
880                         }
881
882                         if ((aip->CURRENT_STATE == AIS_REST) && (aip->GOAL_STATE == AIS_REST)) {
883                                 if (player_visibility) {
884                                         if (d_rand() < FrameTime*player_visibility) {
885                                                 if (dist_to_player/256 < d_rand()*player_visibility) {
886                                                         // mprintf((0, "Object %i searching for player.\n", OBJECT_NUMBER(obj)));
887                                                         aip->GOAL_STATE = AIS_SRCH;
888                                                         aip->CURRENT_STATE = AIS_SRCH;
889                                                 }
890                                         }
891                                 }
892                         }
893
894                         if (GameTime - ailp->time_player_seen > CHASE_TIME_LENGTH) {
895
896                                 if (Game_mode & GM_MULTI)
897                                         if (!player_visibility && (dist_to_player > F1_0*70)) {
898                                                 ailp->mode = AIM_STILL;
899                                                 return;
900                                         }
901
902                                 if (!ai_multiplayer_awareness(obj, 64)) {
903                                         if (maybe_ai_do_actual_firing_stuff(obj, aip))
904                                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
905                                         return;
906                                 }
907                                 // -- bad idea, robots charge player they've never seen! -- mprintf((0, "(4) Object #%i going from chase to player path in frame %i.\n", objnum, FrameCount));
908                                 // -- bad idea, robots charge player they've never seen! -- create_path_to_player(obj, 10, 1);
909                                 // -- bad idea, robots charge player they've never seen! -- ai_multi_send_robot_position(objnum, -1);
910                         } else if ((aip->CURRENT_STATE != AIS_REST) && (aip->GOAL_STATE != AIS_REST)) {
911                                 if (!ai_multiplayer_awareness(obj, 70)) {
912                                         if (maybe_ai_do_actual_firing_stuff(obj, aip))
913                                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
914                                         return;
915                                 }
916                                 ai_move_relative_to_player(obj, ailp, dist_to_player, &vec_to_player, circle_distance, 0, player_visibility);
917
918                                 if ((obj_ref & 1) && ((aip->GOAL_STATE == AIS_SRCH) || (aip->GOAL_STATE == AIS_LOCK))) {
919                                         if (player_visibility) // == 2)
920                                                 ai_turn_towards_vector(&vec_to_player, obj, robptr->turn_time[Difficulty_level]);
921                                 }
922
923                                 if (ai_evaded) {
924                                         ai_multi_send_robot_position(objnum, 1);
925                                         ai_evaded = 0;
926                                 } else
927                                         ai_multi_send_robot_position(objnum, -1);
928
929                                 do_firing_stuff(obj, player_visibility, &vec_to_player);
930                         }
931                         break;
932                 }
933
934                 case AIM_RUN_FROM_OBJECT:
935                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
936
937                         if (player_visibility) {
938                                 if (ailp->player_awareness_type == 0)
939                                         ailp->player_awareness_type = PA_WEAPON_ROBOT_COLLISION;
940
941                         }
942
943                         // If in multiplayer, only do if player visible.  If not multiplayer, do always.
944                         if (!(Game_mode & GM_MULTI) || player_visibility)
945                                 if (ai_multiplayer_awareness(obj, 75)) {
946                                         ai_follow_path(obj, player_visibility, previous_visibility, &vec_to_player);
947                                         ai_multi_send_robot_position(objnum, -1);
948                                 }
949
950                         if (aip->GOAL_STATE != AIS_FLIN)
951                                 aip->GOAL_STATE = AIS_LOCK;
952                         else if (aip->CURRENT_STATE == AIS_FLIN)
953                                 aip->GOAL_STATE = AIS_LOCK;
954
955                         // Bad to let run_from robot fire at player because it
956                         // will cause a war in which it turns towards the player
957                         // to fire and then towards its goal to move.
958                         // do_firing_stuff(obj, player_visibility, &vec_to_player);
959                         // Instead, do this:
960                         // (Note, only drop if player is visible.  This prevents
961                         // the bombs from being a giveaway, and also ensures that
962                         // the robot is moving while it is dropping.  Also means
963                         // fewer will be dropped.)
964                         if ((ailp->next_fire <= 0) && (player_visibility)) {
965                                 vms_vector fire_vec, fire_pos;
966
967                                 if (!ai_multiplayer_awareness(obj, 75))
968                                         return;
969
970                                 fire_vec = obj->orient.fvec;
971                                 vm_vec_negate(&fire_vec);
972                                 vm_vec_add(&fire_pos, &obj->pos, &fire_vec);
973
974                                 if (aip->SUB_FLAGS & SUB_FLAGS_SPROX)
975                                         Laser_create_new_easy( &fire_vec, &fire_pos, OBJECT_NUMBER(obj), ROBOT_SUPERPROX_ID, 1 );
976                                 else
977                                         Laser_create_new_easy( &fire_vec, &fire_pos, OBJECT_NUMBER(obj), PROXIMITY_ID, 1 );
978
979                                 ailp->next_fire = (F1_0/2)*(NDL+5 - Difficulty_level);      // Drop a proximity bomb every 5 seconds.
980
981 #ifdef NETWORK
982 #ifndef SHAREWARE
983                                 if (Game_mode & GM_MULTI)
984                                 {
985                                         ai_multi_send_robot_position(OBJECT_NUMBER(obj), -1);
986                                         if (aip->SUB_FLAGS & SUB_FLAGS_SPROX)
987                                                 multi_send_robot_fire(OBJECT_NUMBER(obj), -2, &fire_vec);
988                                         else
989                                                 multi_send_robot_fire(OBJECT_NUMBER(obj), -1, &fire_vec);
990                                 }
991 #endif
992 #endif
993                         }
994                         break;
995
996                 case AIM_GOTO_PLAYER:
997                 case AIM_GOTO_OBJECT:
998                         ai_follow_path(obj, 2, previous_visibility, &vec_to_player);    // Follows path as if player can see robot.
999                         ai_multi_send_robot_position(objnum, -1);
1000                         break;
1001
1002                 case AIM_FOLLOW_PATH: {
1003                         int anger_level = 65;
1004
1005                         if (aip->behavior == AIB_STATION)
1006                                 if (Point_segs[aip->hide_index + aip->path_length - 1].segnum == aip->hide_segment) {
1007                                         anger_level = 64;
1008                                         // mprintf((0, "Object %i, station, lowering anger to 64.\n"));
1009                                 }
1010
1011                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1012
1013                         if (Game_mode & (GM_MODEM | GM_SERIAL))
1014                                 if (!player_visibility && (dist_to_player > F1_0*70)) {
1015                                         ailp->mode = AIM_STILL;
1016                                         return;
1017                                 }
1018
1019                         if (!ai_multiplayer_awareness(obj, anger_level)) {
1020                                 if (maybe_ai_do_actual_firing_stuff(obj, aip)) {
1021                                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1022                                         ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1023                                 }
1024                                 return;
1025                         }
1026
1027                         ai_follow_path(obj, player_visibility, previous_visibility, &vec_to_player);
1028
1029                         if (aip->GOAL_STATE != AIS_FLIN)
1030                                 aip->GOAL_STATE = AIS_LOCK;
1031                         else if (aip->CURRENT_STATE == AIS_FLIN)
1032                                 aip->GOAL_STATE = AIS_LOCK;
1033
1034                         if (aip->behavior != AIB_RUN_FROM)
1035                                 do_firing_stuff(obj, player_visibility, &vec_to_player);
1036
1037                         if ((player_visibility == 2) && (aip->behavior != AIB_SNIPE) && (aip->behavior != AIB_FOLLOW) && (aip->behavior != AIB_RUN_FROM) && (obj->id != ROBOT_BRAIN) && (robptr->companion != 1) && (robptr->thief != 1)) {
1038                                 if (robptr->attack_type == 0)
1039                                         ailp->mode = AIM_CHASE_OBJECT;
1040                                 // This should not just be distance based, but also time-since-player-seen based.
1041                         } else if ((dist_to_player > F1_0*(20*(2*Difficulty_level + robptr->pursuit)))
1042                                                 && (GameTime - ailp->time_player_seen > (F1_0/2*(Difficulty_level+robptr->pursuit)))
1043                                                 && (player_visibility == 0)
1044                                                 && (aip->behavior == AIB_NORMAL)
1045                                                 && (ailp->mode == AIM_FOLLOW_PATH)) {
1046                                 ailp->mode = AIM_STILL;
1047                                 aip->hide_index = -1;
1048                                 aip->path_length = 0;
1049                         }
1050
1051                         ai_multi_send_robot_position(objnum, -1);
1052
1053                         break;
1054                 }
1055
1056                 case AIM_BEHIND:
1057                         if (!ai_multiplayer_awareness(obj, 71)) {
1058                                 if (maybe_ai_do_actual_firing_stuff(obj, aip)) {
1059                                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1060                                         ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1061                                 }
1062                                 return;
1063                         }
1064
1065                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1066
1067                         if (player_visibility == 2) {
1068                                 // Get behind the player.
1069                                 // Method:
1070                                 // If vec_to_player dot player_rear_vector > 0, behind is goal.
1071                                 // Else choose goal with larger dot from left, right.
1072                                 vms_vector  goal_point, goal_vector, vec_to_goal, rand_vec;
1073                                 fix         dot;
1074
1075                                 dot = vm_vec_dot(&ConsoleObject->orient.fvec, &vec_to_player);
1076                                 if (dot > 0) {          // Remember, we're interested in the rear vector dot being < 0.
1077                                         goal_vector = ConsoleObject->orient.fvec;
1078                                         vm_vec_negate(&goal_vector);
1079                                         // -- mprintf((0, "Goal is BEHIND\n"));
1080                                 } else {
1081                                         fix dot;
1082                                         dot = vm_vec_dot(&ConsoleObject->orient.rvec, &vec_to_player);
1083                                         goal_vector = ConsoleObject->orient.rvec;
1084                                         if (dot > 0) {
1085                                                 vm_vec_negate(&goal_vector);
1086                                                 // -- mprintf((0, "Goal is LEFT\n"));
1087                                         } else
1088                                                 ; // -- mprintf((0, "Goal is RIGHT\n"));
1089                                 }
1090
1091                                 vm_vec_scale(&goal_vector, 2*(ConsoleObject->size + obj->size + (((objnum*4 + FrameCount) & 63) << 12)));
1092                                 vm_vec_add(&goal_point, &ConsoleObject->pos, &goal_vector);
1093                                 make_random_vector(&rand_vec);
1094                                 vm_vec_scale_add2(&goal_point, &rand_vec, F1_0*8);
1095                                 vm_vec_sub(&vec_to_goal, &goal_point, &obj->pos);
1096                                 vm_vec_normalize_quick(&vec_to_goal);
1097                                 move_towards_vector(obj, &vec_to_goal, 0);
1098                                 ai_turn_towards_vector(&vec_to_player, obj, robptr->turn_time[Difficulty_level]);
1099                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1100                         }
1101
1102                         if (aip->GOAL_STATE != AIS_FLIN)
1103                                 aip->GOAL_STATE = AIS_LOCK;
1104                         else if (aip->CURRENT_STATE == AIS_FLIN)
1105                                 aip->GOAL_STATE = AIS_LOCK;
1106
1107                         ai_multi_send_robot_position(objnum, -1);
1108                         break;
1109
1110                 case AIM_STILL:
1111                         if ((dist_to_player < F1_0*120+Difficulty_level*F1_0*20) || (ailp->player_awareness_type >= PA_WEAPON_ROBOT_COLLISION-1)) {
1112                                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1113
1114                                 // turn towards vector if visible this time or last time, or rand
1115                                 // new!
1116                                 if ((player_visibility == 2) || (previous_visibility == 2)) { // -- MK, 06/09/95:  || ((d_rand() > 0x4000) && !(Game_mode & GM_MULTI))) {
1117                                         if (!ai_multiplayer_awareness(obj, 71)) {
1118                                                 if (maybe_ai_do_actual_firing_stuff(obj, aip))
1119                                                         ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1120                                                 return;
1121                                         }
1122                                         ai_turn_towards_vector(&vec_to_player, obj, robptr->turn_time[Difficulty_level]);
1123                                         ai_multi_send_robot_position(objnum, -1);
1124                                 }
1125
1126                                 do_firing_stuff(obj, player_visibility, &vec_to_player);
1127                                 if (player_visibility == 2) {  // Changed @mk, 09/21/95: Require that they be looking to evade.  Change, MK, 01/03/95 for Multiplayer reasons.  If robots can't see you (even with eyes on back of head), then don't do evasion.
1128                                         if (robptr->attack_type == 1) {
1129                                                 aip->behavior = AIB_NORMAL;
1130                                                 if (!ai_multiplayer_awareness(obj, 80)) {
1131                                                         if (maybe_ai_do_actual_firing_stuff(obj, aip))
1132                                                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1133                                                         return;
1134                                                 }
1135                                                 ai_move_relative_to_player(obj, ailp, dist_to_player, &vec_to_player, 0, 0, player_visibility);
1136                                                 if (ai_evaded) {
1137                                                         ai_multi_send_robot_position(objnum, 1);
1138                                                         ai_evaded = 0;
1139                                                 }
1140                                                 else
1141                                                         ai_multi_send_robot_position(objnum, -1);
1142                                         } else {
1143                                                 // Robots in hover mode are allowed to evade at half normal speed.
1144                                                 if (!ai_multiplayer_awareness(obj, 81)) {
1145                                                         if (maybe_ai_do_actual_firing_stuff(obj, aip))
1146                                                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1147                                                         return;
1148                                                 }
1149                                                 ai_move_relative_to_player(obj, ailp, dist_to_player, &vec_to_player, 0, 1, player_visibility);
1150                                                 if (ai_evaded) {
1151                                                         ai_multi_send_robot_position(objnum, -1);
1152                                                         ai_evaded = 0;
1153                                                 }
1154                                                 else
1155                                                         ai_multi_send_robot_position(objnum, -1);
1156                                         }
1157                                 } else if ((obj->segnum != aip->hide_segment) && (dist_to_player > F1_0*80) && (!(Game_mode & GM_MULTI))) {
1158                                         // If pretty far from the player, player cannot be
1159                                         // seen (obstructed) and in chase mode, switch to
1160                                         // follow path mode.
1161                                         // This has one desirable benefit of avoiding physics retries.
1162                                         if (aip->behavior == AIB_STATION) {
1163                                                 ailp->goal_segment = aip->hide_segment;
1164                                                 // -- mprintf((0, "(2) Object #%i going from STILL to STATION in frame %i.\n", objnum, FrameCount));
1165                                                 create_path_to_station(obj, 15);
1166                                         }
1167                                         break;
1168                                 }
1169                         }
1170
1171                         break;
1172                 case AIM_OPEN_DOOR: {       // trying to open a door.
1173                         vms_vector center_point, goal_vector;
1174                         Assert(obj->id == ROBOT_BRAIN);     // Make sure this guy is allowed to be in this mode.
1175
1176                         if (!ai_multiplayer_awareness(obj, 62))
1177                                 return;
1178                         compute_center_point_on_side(&center_point, &Segments[obj->segnum], aip->GOALSIDE);
1179                         vm_vec_sub(&goal_vector, &center_point, &obj->pos);
1180                         vm_vec_normalize_quick(&goal_vector);
1181                         ai_turn_towards_vector(&goal_vector, obj, robptr->turn_time[Difficulty_level]);
1182                         move_towards_vector(obj, &goal_vector, 0);
1183                         ai_multi_send_robot_position(objnum, -1);
1184
1185                         break;
1186                 }
1187
1188                 case AIM_SNIPE_WAIT:
1189                         break;
1190                 case AIM_SNIPE_RETREAT:
1191                         // -- if (ai_multiplayer_awareness(obj, 53))
1192                         // --   if (ailp->next_fire < -F1_0)
1193                         // --           ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1194                         break;
1195                 case AIM_SNIPE_RETREAT_BACKWARDS:
1196                 case AIM_SNIPE_ATTACK:
1197                 case AIM_SNIPE_FIRE:
1198                         if (ai_multiplayer_awareness(obj, 53)) {
1199                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1200                                 if (robptr->thief)
1201                                         ai_move_relative_to_player(obj, ailp, dist_to_player, &vec_to_player, 0, 0, player_visibility);
1202                                 break;
1203                         }
1204                         break;
1205
1206                 case AIM_THIEF_WAIT:
1207                 case AIM_THIEF_ATTACK:
1208                 case AIM_THIEF_RETREAT:
1209                 case AIM_WANDER:    // Used for Buddy Bot
1210                         break;
1211
1212                 default:
1213                         mprintf((0, "Unknown mode = %i in robot %i, behavior = %i\n", ailp->mode, OBJECT_NUMBER(obj), aip->behavior));
1214                         ailp->mode = AIM_CHASE_OBJECT;
1215                         break;
1216         }       // end: switch (ailp->mode) {
1217
1218         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
1219         // If the robot can see you, increase his awareness of you.
1220         // This prevents the problem of a robot looking right at you but doing nothing.
1221         // Assert(player_visibility != -1); // Means it didn't get initialized!
1222         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1223         if ((player_visibility == 2) && (aip->behavior != AIB_FOLLOW) && (!robptr->thief)) {
1224                 if ((ailp->player_awareness_type == 0) && (aip->SUB_FLAGS & SUB_FLAGS_CAMERA_AWAKE))
1225                         aip->SUB_FLAGS &= ~SUB_FLAGS_CAMERA_AWAKE;
1226                 else if (ailp->player_awareness_type == 0)
1227                         ailp->player_awareness_type = PA_PLAYER_COLLISION;
1228         }
1229
1230         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
1231         if (!object_animates) {
1232                 aip->CURRENT_STATE = aip->GOAL_STATE;
1233                 // mprintf((0, "Setting current to goal (%i) because object doesn't animate.\n", aip->GOAL_STATE));
1234         }
1235
1236         Assert(ailp->player_awareness_type <= AIE_MAX);
1237         Assert(aip->CURRENT_STATE < AIS_MAX);
1238         Assert(aip->GOAL_STATE < AIS_MAX);
1239
1240         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
1241         if (ailp->player_awareness_type) {
1242                 new_goal_state = Ai_transition_table[ailp->player_awareness_type-1][aip->CURRENT_STATE][aip->GOAL_STATE];
1243                 if (ailp->player_awareness_type == PA_WEAPON_ROBOT_COLLISION) {
1244                         // Decrease awareness, else this robot will flinch every frame.
1245                         ailp->player_awareness_type--;
1246                         ailp->player_awareness_time = F1_0*3;
1247                 }
1248
1249                 if (new_goal_state == AIS_ERR_)
1250                         new_goal_state = AIS_REST;
1251
1252                 if (aip->CURRENT_STATE == AIS_NONE)
1253                         aip->CURRENT_STATE = AIS_REST;
1254
1255                 aip->GOAL_STATE = new_goal_state;
1256
1257         }
1258
1259         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
1260         // If new state = fire, then set all gun states to fire.
1261         if ( aip->GOAL_STATE == AIS_FIRE ) {
1262                 int i,num_guns;
1263                 num_guns = Robot_info[obj->id].n_guns;
1264                 for (i=0; i<num_guns; i++)
1265                         ailp->goal_state[i] = AIS_FIRE;
1266         }
1267
1268         // - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  - -  -
1269         // Hack by mk on 01/04/94, if a guy hasn't animated to the firing state, but his next_fire says ok to fire, bash him there
1270         if (ready_to_fire(robptr, ailp) && (aip->GOAL_STATE == AIS_FIRE))
1271                 aip->CURRENT_STATE = AIS_FIRE;
1272
1273         if ((aip->GOAL_STATE != AIS_FLIN)  && (obj->id != ROBOT_BRAIN)) {
1274                 switch (aip->CURRENT_STATE) {
1275                 case AIS_NONE:
1276                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1277
1278                         dot = vm_vec_dot(&obj->orient.fvec, &vec_to_player);
1279                         if (dot >= F1_0/2)
1280                                 if (aip->GOAL_STATE == AIS_REST)
1281                                         aip->GOAL_STATE = AIS_SRCH;
1282                         break;
1283                 case AIS_REST:
1284                         if (aip->GOAL_STATE == AIS_REST) {
1285                                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1286                                 if (ready_to_fire(robptr, ailp) && (player_visibility)) {
1287                                         // mprintf((0, "Setting goal state to fire from rest.\n"));
1288                                         aip->GOAL_STATE = AIS_FIRE;
1289                                 }
1290                         }
1291                         break;
1292                 case AIS_SRCH:
1293                         if (!ai_multiplayer_awareness(obj, 60))
1294                                 return;
1295
1296                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1297
1298                         if (player_visibility == 2) {
1299                                 ai_turn_towards_vector(&vec_to_player, obj, robptr->turn_time[Difficulty_level]);
1300                                 ai_multi_send_robot_position(objnum, -1);
1301                         }
1302                         break;
1303                 case AIS_LOCK:
1304                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1305
1306                         if (!(Game_mode & GM_MULTI) || (player_visibility)) {
1307                                 if (!ai_multiplayer_awareness(obj, 68))
1308                                         return;
1309
1310                                 if (player_visibility == 2) {   // @mk, 09/21/95, require that they be looking towards you to turn towards you.
1311                                         ai_turn_towards_vector(&vec_to_player, obj, robptr->turn_time[Difficulty_level]);
1312                                         ai_multi_send_robot_position(objnum, -1);
1313                                 }
1314                         }
1315                         break;
1316                 case AIS_FIRE:
1317                         compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1318
1319                         if (player_visibility == 2) {
1320                                 if (!ai_multiplayer_awareness(obj, (ROBOT_FIRE_AGITATION-1))) {
1321                                         if (Game_mode & GM_MULTI) {
1322                                                 ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1323                                                 return;
1324                                         }
1325                                 }
1326                                 ai_turn_towards_vector(&vec_to_player, obj, robptr->turn_time[Difficulty_level]);
1327                                 ai_multi_send_robot_position(objnum, -1);
1328                         }
1329
1330                         // Fire at player, if appropriate.
1331                         ai_do_actual_firing_stuff(obj, aip, ailp, robptr, &vec_to_player, dist_to_player, &gun_point, player_visibility, object_animates, aip->CURRENT_GUN);
1332
1333                         break;
1334                 case AIS_RECO:
1335                         if (!(obj_ref & 3)) {
1336                                 compute_vis_and_vec(obj, &vis_vec_pos, ailp, &vec_to_player, &player_visibility, robptr, &visibility_and_vec_computed);
1337                                 if (player_visibility == 2) {
1338                                         if (!ai_multiplayer_awareness(obj, 69))
1339                                                 return;
1340                                         ai_turn_towards_vector(&vec_to_player, obj, robptr->turn_time[Difficulty_level]);
1341                                         ai_multi_send_robot_position(objnum, -1);
1342                                 } // -- MK, 06/09/95: else if (!(Game_mode & GM_MULTI)) {
1343                         }
1344                         break;
1345                 case AIS_FLIN:
1346                         // mprintf((0, "State = flinch, goal = %i.\n", aip->GOAL_STATE));
1347                         break;
1348                 default:
1349                         mprintf((1, "Unknown mode for AI object #%i\n", objnum));
1350                         aip->GOAL_STATE = AIS_REST;
1351                         aip->CURRENT_STATE = AIS_REST;
1352                         break;
1353                 }
1354         } // end of: if (aip->GOAL_STATE != AIS_FLIN) {
1355
1356         // Switch to next gun for next fire.
1357         if (player_visibility == 0) {
1358                 aip->CURRENT_GUN++;
1359                 if (aip->CURRENT_GUN >= Robot_info[obj->id].n_guns)
1360                 {
1361                         if ((robptr->n_guns == 1) || (robptr->weapon_type2 == -1))  // Two weapon types hack.
1362                                 aip->CURRENT_GUN = 0;
1363                         else
1364                                 aip->CURRENT_GUN = 1;
1365                 }
1366         }
1367
1368 }
1369
1370 // ----------------------------------------------------------------------------
1371 void ai_do_cloak_stuff(void)
1372 {
1373         int i;
1374
1375         for (i=0; i<MAX_AI_CLOAK_INFO; i++) {
1376                 Ai_cloak_info[i].last_position = ConsoleObject->pos;
1377                 Ai_cloak_info[i].last_segment = ConsoleObject->segnum;
1378                 Ai_cloak_info[i].last_time = GameTime;
1379         }
1380
1381         // Make work for control centers.
1382         Believed_player_pos = Ai_cloak_info[0].last_position;
1383         Believed_player_seg = Ai_cloak_info[0].last_segment;
1384
1385 }
1386
1387 // ----------------------------------------------------------------------------
1388 // Returns false if awareness is considered too puny to add, else returns true.
1389 int add_awareness_event(object *objp, int type)
1390 {
1391         // If player cloaked and hit a robot, then increase awareness
1392         if ((type == PA_WEAPON_ROBOT_COLLISION) || (type == PA_WEAPON_WALL_COLLISION) || (type == PA_PLAYER_COLLISION))
1393                 ai_do_cloak_stuff();
1394
1395         if (Num_awareness_events < MAX_AWARENESS_EVENTS) {
1396                 if ((type == PA_WEAPON_WALL_COLLISION) || (type == PA_WEAPON_ROBOT_COLLISION))
1397                         if (objp->id == VULCAN_ID)
1398                                 if (d_rand() > 3276)
1399                                         return 0;       // For vulcan cannon, only about 1/10 actually cause awareness
1400
1401                 Awareness_events[Num_awareness_events].segnum = objp->segnum;
1402                 Awareness_events[Num_awareness_events].pos = objp->pos;
1403                 Awareness_events[Num_awareness_events].type = type;
1404                 Num_awareness_events++;
1405         } else {
1406                 //Int3();   // Hey -- Overflowed Awareness_events, make more or something
1407                 // This just gets ignored, so you can just
1408                 // continue.
1409         }
1410         return 1;
1411
1412 }
1413
1414 // ----------------------------------------------------------------------------------
1415 // Robots will become aware of the player based on something that occurred.
1416 // The object (probably player or weapon) which created the awareness is objp.
1417 void create_awareness_event(object *objp, int type)
1418 {
1419         // If not in multiplayer, or in multiplayer with robots, do this, else unnecessary!
1420         if (!(Game_mode & GM_MULTI) || (Game_mode & GM_MULTI_ROBOTS)) {
1421                 if (add_awareness_event(objp, type)) {
1422                         if (((d_rand() * (type+4)) >> 15) > 4)
1423                                 Overall_agitation++;
1424                         if (Overall_agitation > OVERALL_AGITATION_MAX)
1425                                 Overall_agitation = OVERALL_AGITATION_MAX;
1426                 }
1427         }
1428 }
1429
1430 sbyte New_awareness[MAX_SEGMENTS];
1431
1432 // ----------------------------------------------------------------------------------
1433 void pae_aux(int segnum, int type, int level)
1434 {
1435         int j;
1436
1437         if (New_awareness[segnum] < type)
1438                 New_awareness[segnum] = type;
1439
1440         // Process children.
1441         for (j=0; j<MAX_SIDES_PER_SEGMENT; j++)
1442                 if (IS_CHILD(Segments[segnum].children[j]))
1443                 {
1444                         if (level <= 3)
1445                         {
1446                                 if (type == 4)
1447                                         pae_aux(Segments[segnum].children[j], type-1, level+1);
1448                                 else
1449                                         pae_aux(Segments[segnum].children[j], type, level+1);
1450                         }
1451                 }
1452 }
1453
1454
1455 // ----------------------------------------------------------------------------------
1456 void process_awareness_events(void)
1457 {
1458         int i;
1459
1460         if (!(Game_mode & GM_MULTI) || (Game_mode & GM_MULTI_ROBOTS)) {
1461                 memset(New_awareness, 0, sizeof(New_awareness[0]) * (Highest_segment_index+1));
1462
1463                 for (i=0; i<Num_awareness_events; i++)
1464                         pae_aux(Awareness_events[i].segnum, Awareness_events[i].type, 1);
1465
1466         }
1467
1468         Num_awareness_events = 0;
1469 }
1470
1471 // ----------------------------------------------------------------------------------
1472 void set_player_awareness_all(void)
1473 {
1474         int i;
1475
1476         process_awareness_events();
1477
1478         for (i=0; i<=Highest_object_index; i++)
1479                 if (Objects[i].control_type == CT_AI) {
1480                         if (New_awareness[Objects[i].segnum] > Ai_local_info[i].player_awareness_type) {
1481                                 Ai_local_info[i].player_awareness_type = New_awareness[Objects[i].segnum];
1482                                 Ai_local_info[i].player_awareness_time = PLAYER_AWARENESS_INITIAL_TIME;
1483                         }
1484
1485                         // Clear the bit that says this robot is only awake because a camera woke it up.
1486                         if (New_awareness[Objects[i].segnum] > Ai_local_info[i].player_awareness_type)
1487                                 Objects[i].ctype.ai_info.SUB_FLAGS &= ~SUB_FLAGS_CAMERA_AWAKE;
1488                 }
1489 }
1490
1491 #ifndef NDEBUG
1492 int Ai_dump_enable = 0;
1493
1494 FILE *Ai_dump_file = NULL;
1495
1496 char Ai_error_message[128] = "";
1497
1498 // ----------------------------------------------------------------------------------
1499 void force_dump_ai_objects_all(char *msg)
1500 {
1501         int tsave;
1502
1503         tsave = Ai_dump_enable;
1504
1505         Ai_dump_enable = 1;
1506
1507         sprintf(Ai_error_message, "%s\n", msg);
1508         //dump_ai_objects_all();
1509         Ai_error_message[0] = 0;
1510
1511         Ai_dump_enable = tsave;
1512 }
1513
1514 // ----------------------------------------------------------------------------------
1515 void turn_off_ai_dump(void)
1516 {
1517         if (Ai_dump_file != NULL)
1518                 fclose(Ai_dump_file);
1519
1520         Ai_dump_file = NULL;
1521 }
1522
1523 #endif
1524
1525 extern void do_boss_dying_frame(object *objp);
1526
1527 // ----------------------------------------------------------------------------------
1528 // Do things which need to get done for all AI objects each frame.
1529 // This includes:
1530 //  Setting player_awareness (a fix, time in seconds which object is aware of player)
1531 void do_ai_frame_all(void)
1532 {
1533 #ifndef NDEBUG
1534         //dump_ai_objects_all();
1535 #endif
1536
1537         set_player_awareness_all();
1538
1539         if (Ai_last_missile_camera != -1) {
1540                 // Clear if supposed misisle camera is not a weapon, or just every so often, just in case.
1541                 if (((FrameCount & 0x0f) == 0) || (Objects[Ai_last_missile_camera].type != OBJ_WEAPON)) {
1542                         int i;
1543
1544                         Ai_last_missile_camera = -1;
1545                         for (i=0; i<=Highest_object_index; i++)
1546                                 if (Objects[i].type == OBJ_ROBOT)
1547                                         Objects[i].ctype.ai_info.SUB_FLAGS &= ~SUB_FLAGS_CAMERA_AWAKE;
1548                 }
1549         }
1550
1551         // (Moved here from do_boss_stuff() because that only gets called if robot aware of player.)
1552         if (Boss_dying) {
1553                 int i;
1554
1555                 for (i=0; i<=Highest_object_index; i++)
1556                         if (Objects[i].type == OBJ_ROBOT)
1557                                 if (Robot_info[Objects[i].id].boss_flag)
1558                                         do_boss_dying_frame(&Objects[i]);
1559         }
1560 }
1561
1562
1563 extern int Final_boss_is_dead;
1564 extern fix Boss_invulnerable_dot;
1565
1566 // Initializations to be performed for all robots for a new level.
1567 void init_robots_for_level(void)
1568 {
1569         Overall_agitation = 0;
1570         Final_boss_is_dead=0;
1571
1572         Buddy_objnum = 0;
1573         Buddy_allowed_to_talk = 0;
1574
1575         Boss_invulnerable_dot = F1_0/4 - i2f(Difficulty_level)/8;
1576         Boss_dying_start_time = 0;
1577 }
1578
1579 int ai_save_state(PHYSFS_file *fp)
1580 {
1581         PHYSFS_write(fp, &Ai_initialized, sizeof(int), 1);
1582         PHYSFS_write(fp, &Overall_agitation, sizeof(int), 1);
1583         PHYSFS_write(fp, Ai_local_info, sizeof(ai_local) * MAX_OBJECTS, 1);
1584         PHYSFS_write(fp, Point_segs, sizeof(point_seg) * MAX_POINT_SEGS, 1);
1585         PHYSFS_write(fp, Ai_cloak_info, sizeof(ai_cloak_info) * MAX_AI_CLOAK_INFO, 1);
1586         PHYSFS_write(fp, &Boss_cloak_start_time, sizeof(fix), 1);
1587         PHYSFS_write(fp, &Boss_cloak_end_time, sizeof(fix), 1);
1588         PHYSFS_write(fp, &Last_teleport_time, sizeof(fix), 1);
1589         PHYSFS_write(fp, &Boss_teleport_interval, sizeof(fix), 1);
1590         PHYSFS_write(fp, &Boss_cloak_interval, sizeof(fix), 1);
1591         PHYSFS_write(fp, &Boss_cloak_duration, sizeof(fix), 1);
1592         PHYSFS_write(fp, &Last_gate_time, sizeof(fix), 1);
1593         PHYSFS_write(fp, &Gate_interval, sizeof(fix), 1);
1594         PHYSFS_write(fp, &Boss_dying_start_time, sizeof(fix), 1);
1595         PHYSFS_write(fp, &Boss_dying, sizeof(int), 1);
1596         PHYSFS_write(fp, &Boss_dying_sound_playing, sizeof(int), 1);
1597         PHYSFS_write(fp, &Boss_hit_time, sizeof(fix), 1);
1598         // -- MK, 10/21/95, unused! -- PHYSFS_write(fp, &Boss_been_hit, sizeof(int), 1);
1599
1600         PHYSFS_write(fp, &Escort_kill_object, sizeof(Escort_kill_object), 1);
1601         PHYSFS_write(fp, &Escort_last_path_created, sizeof(Escort_last_path_created), 1);
1602         PHYSFS_write(fp, &Escort_goal_object, sizeof(Escort_goal_object), 1);
1603         PHYSFS_write(fp, &Escort_special_goal, sizeof(Escort_special_goal), 1);
1604         PHYSFS_write(fp, &Escort_goal_index, sizeof(Escort_goal_index), 1);
1605         PHYSFS_write(fp, &Stolen_items, sizeof(Stolen_items[0])*MAX_STOLEN_ITEMS, 1);
1606
1607         {
1608                 int temp;
1609                 temp = Point_segs_free_ptr - Point_segs;
1610                 PHYSFS_write(fp, &temp, sizeof(int), 1);
1611         }
1612
1613         PHYSFS_write(fp, &Num_boss_teleport_segs, sizeof(Num_boss_teleport_segs), 1);
1614         PHYSFS_write(fp, &Num_boss_gate_segs, sizeof(Num_boss_gate_segs), 1);
1615
1616         if (Num_boss_gate_segs)
1617                 PHYSFS_write(fp, Boss_gate_segs, sizeof(Boss_gate_segs[0]), Num_boss_gate_segs);
1618
1619         if (Num_boss_teleport_segs)
1620                 PHYSFS_write(fp, Boss_teleport_segs, sizeof(Boss_teleport_segs[0]), Num_boss_teleport_segs);
1621
1622         return 1;
1623 }
1624
1625 int ai_restore_state(PHYSFS_file *fp, int version)
1626 {
1627         PHYSFS_read(fp, &Ai_initialized, sizeof(int), 1);
1628         PHYSFS_read(fp, &Overall_agitation, sizeof(int), 1);
1629         PHYSFS_read(fp, Ai_local_info, sizeof(ai_local) * MAX_OBJECTS, 1);
1630         PHYSFS_read(fp, Point_segs, sizeof(point_seg) * MAX_POINT_SEGS, 1);
1631         PHYSFS_read(fp, Ai_cloak_info, sizeof(ai_cloak_info) * MAX_AI_CLOAK_INFO, 1);
1632         PHYSFS_read(fp, &Boss_cloak_start_time, sizeof(fix), 1);
1633         PHYSFS_read(fp, &Boss_cloak_end_time, sizeof(fix), 1);
1634         PHYSFS_read(fp, &Last_teleport_time, sizeof(fix), 1);
1635         PHYSFS_read(fp, &Boss_teleport_interval, sizeof(fix), 1);
1636         PHYSFS_read(fp, &Boss_cloak_interval, sizeof(fix), 1);
1637         PHYSFS_read(fp, &Boss_cloak_duration, sizeof(fix), 1);
1638         PHYSFS_read(fp, &Last_gate_time, sizeof(fix), 1);
1639         PHYSFS_read(fp, &Gate_interval, sizeof(fix), 1);
1640         PHYSFS_read(fp, &Boss_dying_start_time, sizeof(fix), 1);
1641         PHYSFS_read(fp, &Boss_dying, sizeof(int), 1);
1642         PHYSFS_read(fp, &Boss_dying_sound_playing, sizeof(int), 1);
1643         PHYSFS_read(fp, &Boss_hit_time, sizeof(fix), 1);
1644         // -- MK, 10/21/95, unused! -- PHYSFS_read(fp, &Boss_been_hit, sizeof(int), 1);
1645
1646         if (version >= 8) {
1647                 PHYSFS_read(fp, &Escort_kill_object, sizeof(Escort_kill_object), 1);
1648                 PHYSFS_read(fp, &Escort_last_path_created, sizeof(Escort_last_path_created), 1);
1649                 PHYSFS_read(fp, &Escort_goal_object, sizeof(Escort_goal_object), 1);
1650                 PHYSFS_read(fp, &Escort_special_goal, sizeof(Escort_special_goal), 1);
1651                 PHYSFS_read(fp, &Escort_goal_index, sizeof(Escort_goal_index), 1);
1652                 PHYSFS_read(fp, &Stolen_items, sizeof(Stolen_items[0]) * MAX_STOLEN_ITEMS, 1);
1653         } else {
1654                 int i;
1655
1656                 Escort_kill_object = -1;
1657                 Escort_last_path_created = 0;
1658                 Escort_goal_object = ESCORT_GOAL_UNSPECIFIED;
1659                 Escort_special_goal = -1;
1660                 Escort_goal_index = -1;
1661
1662                 for (i=0; i<MAX_STOLEN_ITEMS; i++) {
1663                         Stolen_items[i] = 255;
1664                 }
1665
1666         }
1667
1668         if (version >= 15) {
1669                 int temp;
1670                 PHYSFS_read(fp, &temp, sizeof(int), 1);
1671                 Point_segs_free_ptr = &Point_segs[temp];
1672         } else
1673                 ai_reset_all_paths();
1674
1675         if (version >= 21) {
1676                 PHYSFS_read(fp, &Num_boss_teleport_segs, sizeof(Num_boss_teleport_segs), 1);
1677                 PHYSFS_read(fp, &Num_boss_gate_segs, sizeof(Num_boss_gate_segs), 1);
1678
1679                 if (Num_boss_gate_segs)
1680                         PHYSFS_read(fp, Boss_gate_segs, sizeof(Boss_gate_segs[0]), Num_boss_gate_segs);
1681
1682                 if (Num_boss_teleport_segs)
1683                         PHYSFS_read(fp, Boss_teleport_segs, sizeof(Boss_teleport_segs[0]), Num_boss_teleport_segs);
1684         } else {
1685                 // -- Num_boss_teleport_segs = 1;
1686                 // -- Num_boss_gate_segs = 1;
1687                 // -- Boss_teleport_segs[0] = 0;
1688                 // -- Boss_gate_segs[0] = 0;
1689                 // Note: Maybe better to leave alone...will probably be ok.
1690                 mprintf((1, "Warning: If you fight the boss, he might teleport to segment #0!\n"));
1691         }
1692
1693         return 1;
1694 }