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