Debian Codegraph @

common/main/aistruct.h

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Structs and constants for AI system.
23 * object.h depends on this.
24 * ai.h depends on object.h.
25 * Get it?
26 *
27 */
28
29 #pragma once
30
31 #include <physfs.h>
32 #include <optional>
33 #include "polyobj.h"
34 #include "pack.h"
35 #include "objnum.h"
36 #include "fwd-powerup.h"
37 #include "fwd-segment.h"
38
39 #ifdef DXX_BUILD_DESCENT
40 #if DXX_BUILD_DESCENT == 2
41 #include "fwd-object.h"
42 #include "fwd-vclip.h"
43 #endif
44 #endif
45
46 #define GREEN_GUY 1
47
48 #define MAX_SEGMENTS_PER_PATH 20
49
50 namespace dcx {
51
52 enum class contained_object_type : uint8_t;
53
54 enum class player_awareness_type_t : uint8_t
55 {
56 PA_NONE,
57 PA_NEARBY_ROBOT_FIRED = 1, // Level of robot awareness after nearby robot fires a weapon
58 PA_WEAPON_WALL_COLLISION = 2, // Level of robot awareness after player weapon hits nearby wall
59 // PA_PLAYER_VISIBLE = 2, // Level of robot awareness if robot is looking towards player, and player not hidden
60 PA_PLAYER_COLLISION = 3, // Level of robot awareness after player bumps into robot
61 PA_WEAPON_ROBOT_COLLISION = 4, // Level of robot awareness after player weapon hits nearby robot
62 };
63
64 enum class player_visibility_state : int8_t
65 {
66 no_line_of_sight,
67 visible_not_in_field_of_view,
68 visible_and_in_field_of_view,
69 };
70
71 enum ai_static_state : uint8_t
72 {
73 AIS_NONE = 0,
74 AIS_REST = 1,
75 AIS_SRCH = 2,
76 AIS_LOCK = 3,
77 AIS_FLIN = 4,
78 AIS_FIRE = 5,
79 AIS_RECO = 6,
80 AIS_ERR_ = 7,
81 };
82
83 enum class robot_gun_number : uint8_t
84 {
85 /* This enum is used to index an array[8], so only values in [0, 7] are
86 * valid indices. All others are special flag values that must not be
87 * stored into the robot structure.
88 */
89 _0,
90 _1,
91 /* if DXX_BUILD_DESCENT == 2 */
92 smart_mine = 0xfe,
93 /* endif */
94 proximity = 0xff,
95 };
96
97 static inline unsigned player_is_visible(const player_visibility_state s)
98 {
99 return static_cast<unsigned>(s) > 0;
100 }
101
102 [[nodiscard]]
103 std::optional<ai_static_state> build_ai_state_from_untrusted(uint8_t untrusted);
104
105 }
106
107 // Constants indicating currently moving forward or backward through
108 // path. Note that you can add aip->direction to aip_path_index to
109 // get next segment on path.
110 #define AI_DIR_FORWARD 1
111 #define AI_DIR_BACKWARD (-AI_DIR_FORWARD)
112
113 #ifdef DXX_BUILD_DESCENT
114 namespace dsx {
115
116 enum class ai_behavior : uint8_t
117 {
118 // Behaviors
119 AIB_STILL = 0x80,
120 AIB_NORMAL = 0x81,
121 AIB_RUN_FROM = 0x83,
122 AIB_STATION = 0x85,
123 #if DXX_BUILD_DESCENT == 1
124 AIB_HIDE = 0x82,
125 AIB_FOLLOW_PATH = 0x84,
126 #elif DXX_BUILD_DESCENT == 2
127 AIB_BEHIND = 0x82,
128 AIB_SNIPE = 0x84,
129 AIB_FOLLOW = 0x86,
130 #endif
131 };
132
133 #define MIN_BEHAVIOR 0x80
134 #if DXX_BUILD_DESCENT == 1
135 #define MAX_BEHAVIOR 0x85
136 #elif DXX_BUILD_DESCENT == 2
137 #define MAX_BEHAVIOR 0x86
138 #endif
139
140 enum class ai_mode : uint8_t
141 {
142 // Modes
143 AIM_STILL = 0,
144 AIM_WANDER = 1,
145 AIM_FOLLOW_PATH = 2,
146 AIM_CHASE_OBJECT = 3,
147 AIM_RUN_FROM_OBJECT = 4,
148 AIM_FOLLOW_PATH_2 = 6,
149 AIM_OPEN_DOOR = 7,
150 #if DXX_BUILD_DESCENT == 1
151 AIM_HIDE = 5,
152 #elif DXX_BUILD_DESCENT == 2
153 AIM_BEHIND = 5,
154 AIM_GOTO_PLAYER = 8, // Only for escort behavior
155 AIM_GOTO_OBJECT = 9, // Only for escort behavior
156
157 AIM_SNIPE_ATTACK = 10,
158 AIM_SNIPE_FIRE = 11,
159 AIM_SNIPE_RETREAT = 12,
160 AIM_SNIPE_RETREAT_BACKWARDS = 13,
161 AIM_SNIPE_WAIT = 14,
162
163 AIM_THIEF_ATTACK = 15,
164 AIM_THIEF_RETREAT = 16,
165 AIM_THIEF_WAIT = 17,
166 #endif
167 };
168
169 }
170 #endif
171
172 #define AISM_GOHIDE 0
173 #define AISM_HIDING 1
174
175 #define AI_MAX_STATE 7
176
177 #define AIE_FIRE 0
178 #define AIE_HITT 1
179 #define AIE_COLL 2
180 #define AIE_HURT 3
181
182 //typedef struct opath {
183 // sbyte path_index; // current index of path
184 // sbyte path_direction; // current path direction
185 // sbyte path_length; // length of current path
186 // sbyte nothing;
187 // short path[MAX_SEGMENTS_PER_PATH];
188 // short always_0xabc; // If this is ever not 0xabc, then someone overwrote
189 //} opath;
190 //
191 //typedef struct oai_state {
192 // short mode; //
193 // short counter; // kind of a hack, frame countdown until switch modes
194 // opath paths[2];
195 // vms_vector movement_vector; // movement vector for one second
196 //} oai_state;
197
198 #ifdef DXX_BUILD_DESCENT
199 #if DXX_BUILD_DESCENT == 2
200 #define SUB_FLAGS_GUNSEG 0x01
201 #define SUB_FLAGS_SPROX 0x02 // If set, then this bot drops a super prox, not a prox, when it's time to drop something
202 #define SUB_FLAGS_CAMERA_AWAKE 0x04 // If set, a camera (on a missile) woke this robot up, so don't fire at player. Can look real stupid!
203 #endif
204 #endif
205
206 // This is the stuff that is permanent for an AI object.
207 #ifdef DXX_BUILD_DESCENT
208 namespace dsx {
209
210 // Rather temporal AI stuff.
211 struct ai_local : public prohibit_void_ptr<ai_local>
212 {
213 // These used to be bytes, changed to ints so I could set watchpoints on them.
214 player_awareness_type_t player_awareness_type = player_awareness_type_t::PA_NONE; // type of awareness of player
215 uint8_t retry_count{0}; // number of retries in physics last time this object got moved.
216 uint8_t consecutive_retries{0}; // number of retries in consecutive frames (ie, without a retry_count of 0)
217 player_visibility_state previous_visibility{}; // Visibility of player last time we checked.
218 uint8_t rapidfire_count{0}; // number of shots fired rapidly
219 ai_mode mode{}; // current mode within behavior
220 segnum_t goal_segment{}; // goal segment for current path
221 fix next_action_time{0}; // time in seconds until something happens, mode dependent
222 fix next_fire{0}; // time in seconds until can fire again
223 #if DXX_BUILD_DESCENT == 2
224 fix next_fire2{0}; // time in seconds until can fire again from second weapon
225 #endif
226 fix player_awareness_time{0}; // time in seconds robot will be aware of player, 0 means not aware of player
227 fix time_since_processed{0}; // time since this robot last processed in do_ai_frame
228 fix64 time_player_seen{0}; // absolute time in seconds at which player was last seen, might cause to go into follow_path mode
229 fix64 time_player_sound_attacked{0}; // absolute time in seconds at which player was last seen with visibility of 2.
230 fix64 next_misc_sound_time{0}; // absolute time in seconds at which this robot last made an angry or lurking sound.
231 std::array<vms_angvec, MAX_SUBMODELS> goal_angles{}; // angles for each subobject
232 std::array<vms_angvec, MAX_SUBMODELS> delta_angles{}; // angles for each subobject
233 enumerated_array<ai_static_state, MAX_SUBMODELS, robot_gun_number> goal_state{}; // Goal state for this sub-object
234 enumerated_array<ai_static_state, MAX_SUBMODELS, robot_gun_number> achieved_state{}; // Last achieved state
235 };
236
237 struct ai_static : public prohibit_void_ptr<ai_static>
238 {
239 ai_behavior behavior{}; //
240 robot_gun_number CURRENT_GUN;
241 ai_static_state CURRENT_STATE;
242 ai_static_state GOAL_STATE;
243 int8_t PATH_DIR;
244 #if DXX_BUILD_DESCENT == 1
245 int8_t SUBMODE; // submode, eg AISM_HIDING if mode == AIM_HIDE
246 #elif DXX_BUILD_DESCENT == 2
247 int8_t SUB_FLAGS; // bit 0: Set -> Robot's current gun in different segment than robot's center.
248 #endif
249 sidenum_t GOALSIDE; // for guys who open doors, this is the side they are going after.
250 int8_t CLOAKED{}; // Cloaked now.
251 int8_t SKIP_AI_COUNT{}; // Skip AI this frame, but decrement in do_ai_frame.
252 int8_t REMOTE_OWNER{}; // Who is controlling this remote AI object (multiplayer use only)
253 int8_t REMOTE_SLOT_NUM{}; // What slot # is this robot in for remote control purposes (multiplayer use only)
254 segnum_t hide_segment{}; // Segment to go to for hiding.
255 short hide_index{}; // Index in Path_seg_points
256 short path_length{}; // Length of hide path.
257 #if DXX_BUILD_DESCENT == 1
258 short cur_path_index{}; // Current index in path.
259 #elif DXX_BUILD_DESCENT == 2
260 sbyte cur_path_index{}; // Current index in path.
261 sbyte dying_sound_playing{}; // !0 if this robot is playing its dying sound.
262 #endif
263 objnum_t danger_laser_num{};
264 object_signature_t danger_laser_signature;
265 #if DXX_BUILD_DESCENT == 2
266 fix64 dying_start_time{}; // Time at which this robot started dying.
267 #endif
268 ai_local ail;
269 };
270
271 // Same as above but structure Savegames/Multiplayer objects expect
272 struct ai_static_rw
273 {
274 ubyte behavior; //
275 int8_t flags[11]; // various flags, meaning defined by constants
276 uint16_t hide_segment; // Segment to go to for hiding.
277 short hide_index; // Index in Path_seg_points
278 short path_length; // Length of hide path.
279 #if DXX_BUILD_DESCENT == 1
280 short cur_path_index; // Current index in path.
281 short follow_path_start_seg; // Start segment for robot which follows path.
282 short follow_path_end_seg; // End segment for robot which follows path.
283 int danger_laser_signature;
284 short danger_laser_num;
285 #elif DXX_BUILD_DESCENT == 2
286 sbyte cur_path_index; // Current index in path.
287 sbyte dying_sound_playing; // !0 if this robot is playing its dying sound.
288 short danger_laser_num;
289 int danger_laser_signature;
290 fix dying_start_time; // Time at which this robot started dying.
291 #endif
292 } __pack__;
293
294 static_assert(sizeof(ai_static_rw) == 30);
295
296 // Same as above but structure Savegames expect
297 struct ai_local_rw
298 {
299 // These used to be bytes, changed to ints so I could set watchpoints on them.
300 #if DXX_BUILD_DESCENT == 1
301 sbyte player_awareness_type; // type of awareness of player
302 sbyte retry_count; // number of retries in physics last time this object got moved.
303 sbyte consecutive_retries; // number of retries in consecutive frames (ie, without a retry_count of 0)
304 sbyte mode; // current mode within behavior
305 sbyte previous_visibility; // Visibility of player last time we checked.
306 sbyte rapidfire_count; // number of shots fired rapidly
307 short goal_segment; // goal segment for current path
308 fix last_see_time, last_attack_time; // For sound effects, time at which player last seen, attacked
309 #elif DXX_BUILD_DESCENT == 2
310 int player_awareness_type; // type of awareness of player
311 int retry_count; // number of retries in physics last time this object got moved.
312 int consecutive_retries; // number of retries in consecutive frames (ie, without a retry_count of 0)
313 int mode; // current mode within behavior
314 int previous_visibility; // Visibility of player last time we checked.
315 int rapidfire_count; // number of shots fired rapidly
316 int goal_segment; // goal segment for current path
317 #endif
318 fix next_action_time; // time in seconds until something happens, mode dependent
319 fix next_fire; // time in seconds until can fire again
320 #if DXX_BUILD_DESCENT == 2
321 fix next_fire2; // time in seconds until can fire again from second weapon
322 #endif
323 fix player_awareness_time; // time in seconds robot will be aware of player, 0 means not aware of player
324 fix time_player_seen; // absolute time in seconds at which player was last seen, might cause to go into follow_path mode
325 fix time_player_sound_attacked; // absolute time in seconds at which player was last seen with visibility of 2.
326 fix next_misc_sound_time; // absolute time in seconds at which this robot last made an angry or lurking sound.
327 fix time_since_processed; // time since this robot last processed in do_ai_frame
328 vms_angvec goal_angles[MAX_SUBMODELS]; // angles for each subobject
329 vms_angvec delta_angles[MAX_SUBMODELS]; // angles for each subobject
330 sbyte goal_state[MAX_SUBMODELS]; // Goal state for this sub-object
331 sbyte achieved_state[MAX_SUBMODELS]; // Last achieved state
332 };
333
334 struct ai_cloak_info : public prohibit_void_ptr<ai_cloak_info>
335 {
336 fix64 last_time;
337 #if DXX_BUILD_DESCENT == 2
338 segnum_t last_segment;
339 #endif
340 vms_vector last_position;
341 };
342
343 // Same as above but structure Savegames expect
344 struct ai_cloak_info_rw
345 {
346 fix last_time;
347 #if DXX_BUILD_DESCENT == 2
348 int last_segment;
349 #endif
350 vms_vector last_position;
351 };
352
353 #if DXX_BUILD_DESCENT == 2
354
355 // Maximum number kept track of, will keep stealing, causes stolen weapons to be lost!
356 struct d_thief_unique_state
357 {
358 static constexpr std::size_t MAX_STOLEN_ITEMS{10u};
359 static constexpr powerup_type_t stolen_item_type_none{UINT8_MAX};
360 using stolen_item_storage = std::array<powerup_type_t, MAX_STOLEN_ITEMS>;
361 unsigned Stolen_item_index; // Used in ai.c for controlling rate of Thief flare firing.
362 stolen_item_storage Stolen_items;
363 };
364
365 void drop_stolen_items_local(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const d_vclip_array &Vclip, vmsegptridx_t segp, const vms_vector &thief_velocity, const vms_vector &thief_position, d_thief_unique_state::stolen_item_storage &);
366
367 #endif
368 }
369 #endif
370
371 namespace dcx {
372
373 struct point_seg : prohibit_void_ptr<point_seg> {
374 segnum_t segnum;
375 vms_vector point;
376 };
377
378 struct seg_seg
379 {
380 segnum_t start, end;
381 };
382
383 constexpr std::integral_constant<unsigned, 2500> MAX_POINT_SEGS{};
384
385 }
386
387 // These are the information for a robot describing the location of
388 // the player last time he wasn't cloaked, and the time at which he
389 // was uncloaked. We should store this for each robot, but that's
390 // memory expensive.
391 //extern fix Last_uncloaked_time;
392 //extern vms_vector Last_uncloaked_position;
393
394 #ifdef DXX_BUILD_DESCENT
395 namespace dsx {
396 extern void ai_do_cloak_stuff(void);
397 }
398 #endif