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