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