Debian Codegraph @

common/main/ai.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 * Header file for AI system.
23 *
24 */
25
26 #pragma once
27
28 #include <cstddef>
29 #include <utility>
30 #include <span>
31 #include "dxxsconf.h"
32 #include "dsx-ns.h"
33 #include "fmtcheck.h"
34 #include "vecmat.h"
35
36 #define PARALLAX 0 // If !0, then special debugging info for Parallax eyes only enabled.
37
38 #include "pstypes.h"
39 #include "fwd-object.h"
40 #include "fwd-segment.h"
41 #ifdef DXX_BUILD_DESCENT
42 #include "countarray.h"
43 #include "d_array.h"
44 #include "aistruct.h"
45 #include "robot.h"
46 #include "valptridx.h"
47 #include "physfsx.h"
48 #endif
49
50 namespace dcx {
51 struct point_seg;
52
53 #ifdef DXX_BUILD_DESCENT
54 struct awareness_event
55 {
56 segnum_t segnum; // segment the event occurred in
57 player_awareness_type_t type; // type of event, defines behavior
58 vms_vector pos; // absolute 3 space location of event
59 };
60
61 struct d_level_unique_robot_awareness_state
62 {
63 unsigned Num_awareness_events;
64 std::array<awareness_event, 64> Awareness_events;
65 };
66
67 extern d_level_unique_robot_awareness_state LevelUniqueRobotAwarenessState;
68 #endif
69
70 [[nodiscard]]
71 vms_vector make_random_vector();
72
73 }
74 struct PHYSFS_File;
75
76 #define PLAYER_AWARENESS_INITIAL_TIME (3*F1_0)
77 #define MAX_PATH_LENGTH 30 // Maximum length of path in ai path following.
78 #define MAX_DEPTH_TO_SEARCH_FOR_PLAYER 10
79 #define BOSS_GATE_MATCEN_NUM -1
80
81 #define ROBOT_FIRE_AGITATION 94
82
83 #ifdef DXX_BUILD_DESCENT
84 namespace dcx {
85
86 enum class boss_robot_id : uint8_t
87 {
88 None = 0,
89 d1_1 = 1,
90 d1_superboss = 2,
91 /* if DXX_BUILD_DESCENT == 2 */
92 cool = 21,
93 water,
94 fire,
95 ice,
96 alien1,
97 alien2,
98 d2_1 = cool, // Minimum D2 boss value.
99 /* endif */
100 };
101 static_assert(static_cast<uint8_t>(boss_robot_id::alien2) == 26);
102
103 }
104
105 #if DXX_BUILD_DESCENT == 2
106 #include "player-flags.h"
107 namespace dsx {
108 #define NUM_D2_BOSSES 8
109
110 enum class boss_robot_index : uint8_t
111 {
112 cool,
113 water,
114 fire,
115 ice,
116 alien1,
117 alien2,
118 d2_1 = cool,
119 };
120
121 static constexpr boss_robot_index build_boss_robot_index_from_boss_robot_id(const boss_robot_id id)
122 {
123 return static_cast<boss_robot_index>(static_cast<uint8_t>(id) - static_cast<uint8_t>(boss_robot_id::d2_1));
124 }
125
126 using boss_flags_t = enumerated_array<uint8_t, NUM_D2_BOSSES, boss_robot_index>;
127 extern const boss_flags_t Boss_spew_more; // Set byte if this boss can teleport
128 extern const boss_flags_t Boss_spews_bots_energy; // Set byte if boss spews bots when hit by energy weapon.
129 extern const boss_flags_t Boss_spews_bots_matter; // Set byte if boss spews bots when hit by matter weapon.
130 extern const boss_flags_t Boss_invulnerable_energy; // Set byte if boss is invulnerable to energy weapons.
131 extern const boss_flags_t Boss_invulnerable_matter; // Set byte if boss is invulnerable to matter weapons.
132 extern const boss_flags_t Boss_invulnerable_spot; // Set byte if boss is invulnerable in all but a certain spot. (Dot product fvec|vec_to_collision < BOSS_INVULNERABLE_DOT)
133 extern segnum_t Believed_player_seg;
134 extern const object *Ai_last_missile_camera;
135 }
136 #endif
137
138 namespace dsx {
139 void create_awareness_event(object &objp, player_awareness_type_t type, d_level_unique_robot_awareness_state &LevelUniqueRobotAwarenessState); // object *objp can create awareness of player, amount based on "type"
140 ai_mode ai_behavior_to_mode(ai_behavior behavior);
141 void init_ai_object(const d_robot_info_array &Robot_info, vmobjptridx_t objp, ai_behavior initial_mode, imsegidx_t hide_segment);
142 }
143
144 namespace dcx {
145
146 struct d_level_shared_boss_state
147 {
148 /* Descent 1 bosses use a compile-time constant interval between each cloak
149 * iteration and a constant, but different, interval between teach teleport
150 * iteration.
151 *
152 * Descent 2 bosses use a level-dependent interval between each cloak and
153 * each teleport. Record the Descent 1 constant intervals for static use
154 * in Descent 1, and for use by Descent 2 when emulating Descent 1.
155 */
156 using D1_Boss_cloak_interval = std::integral_constant<fix, F1_0 * 10>;
157 using D1_Boss_teleport_interval = std::integral_constant<fix, F1_0 * 8>;
158 struct special_segment_array_t : public count_array_t<vcsegidx_t, 100> {};
159 struct gate_segment_array_t : public special_segment_array_t {};
160 struct teleport_segment_array_t : public special_segment_array_t {};
161 gate_segment_array_t Gate_segs;
162 teleport_segment_array_t Teleport_segs;
163 };
164
165 constexpr fix Boss_cloak_duration = F1_0*7;
166
167 extern vms_vector Believed_player_pos;
168 }
169
170 namespace dsx {
171
172 struct d_level_shared_boss_state : ::dcx::d_level_shared_boss_state
173 {
174 /* Time in `fix`-seconds between successive uses of the cloak/teleport
175 * abilities.
176 */
177 #if DXX_BUILD_DESCENT == 1
178 static constexpr D1_Boss_cloak_interval Boss_cloak_interval{};
179 static constexpr D1_Boss_teleport_interval Boss_teleport_interval{};
180 #elif DXX_BUILD_DESCENT == 2
181 fix Boss_cloak_interval;
182 fix Boss_teleport_interval;
183 #endif
184 };
185
186 extern d_level_shared_boss_state LevelSharedBossState;
187 void move_towards_segment_center(const robot_info &robptr, const d_level_shared_segment_state &, object_base &objp);
188 imobjptridx_t gate_in_robot(const d_robot_info_array &Robot_info, robot_id type, vmsegptridx_t segnum);
189 void do_ai_frame(const d_level_shared_robot_info_state &LevelSharedRobotInfoState, vmobjptridx_t objp);
190 #if DXX_BUILD_DESCENT == 1
191 #define do_ai_frame_all(Robot_info) do_ai_frame_all()
192 #elif DXX_BUILD_DESCENT == 2
193 #undef do_ai_frame_all
194 #endif
195 void do_ai_frame_all(const d_robot_info_array &Robot_info);
196 }
197 extern void create_all_paths(void);
198 namespace dsx {
199 void create_path_to_station(vmobjptridx_t objp, const robot_info &robptr, int max_length);
200 #if DXX_BUILD_DESCENT == 1
201 #define ai_follow_path(Robot_info,o,pv,vec) ai_follow_path(Robot_info,o,pv)
202 #else
203 #undef ai_follow_path
204 #endif
205 void ai_follow_path(const d_robot_info_array &Robot_info, vmobjptridx_t objp, const player_visibility_state player_visibility, const vms_vector *vec_to_player);
206 void ai_turn_towards_vector(const vms_vector &vec_to_player, object_base &obj, fix rate);
207 void init_ai_objects(const d_robot_info_array &Robot_info);
208 void create_n_segment_path(vmobjptridx_t objp, const robot_info &robptr, unsigned path_length, imsegidx_t avoid_seg);
209 void create_n_segment_path_to_door(vmobjptridx_t objp, const robot_info &robptr, unsigned path_length);
210 void init_robots_for_level();
211 #if DXX_BUILD_DESCENT == 2
212 int polish_path(vmobjptridx_t objp, point_seg *psegs, int num_points);
213 void move_towards_player(const d_robot_info_array &Robot_info, object &objp, const vms_vector &vec_to_player);
214 #endif
215
216 // max_length is maximum depth of path to create.
217 // If -1, use default: MAX_DEPTH_TO_SEARCH_FOR_PLAYER
218 void attempt_to_resume_path(const d_robot_info_array &Robot_info, vmobjptridx_t objp);
219
220 // When a robot and a player collide, some robots attack!
221 void do_ai_robot_hit_attack(const d_robot_info_array &Robot_info, vmobjptridx_t robot, vmobjptridx_t player, const vms_vector &collision_point);
222 int ai_door_is_openable(
223 const object &, const robot_info *,
224 #if DXX_BUILD_DESCENT == 2
225 player_flags,
226 #endif
227 const shared_segment &segp, sidenum_t sidenum);
228 player_visibility_state player_is_visible_from_object(const d_robot_info_array &Robot_info, vmobjptridx_t objp, vms_vector &pos, fix field_of_view, const vms_vector &vec_to_player);
229 extern void ai_reset_all_paths(void); // Reset all paths. Call at the start of a level.
230
231 #if DXX_BUILD_DESCENT == 2
232 // In escort.c
233 void do_escort_frame(vmobjptridx_t objp, const robot_info &robptr, const object &plrobj, player_visibility_state player_visibility);
234 void do_snipe_frame(vmobjptridx_t objp, const robot_info &robptr, fix dist_to_player, player_visibility_state player_visibility, const vms_vector &vec_to_player);
235 void do_thief_frame(vmobjptridx_t objp, const robot_info &robptr, fix dist_to_player, player_visibility_state player_visibility, const vms_vector &vec_to_player);
236 #endif
237 }
238
239 namespace dsx {
240 void start_boss_death_sequence(d_level_unique_boss_state &BossUniqueState, const d_robot_info_array &Robot_info, object &objp);
241 extern void ai_init_boss_for_ship(void);
242 void init_ai_for_ship();
243
244 #if DXX_BUILD_DESCENT == 2
245 extern vms_vector Last_fired_upon_player_pos;
246
247 #define MIN_ESCORT_DISTANCE (F1_0*40)
248
249 #define SNIPE_RETREAT_TIME (F1_0*5)
250 #define SNIPE_ABORT_RETREAT_TIME (SNIPE_RETREAT_TIME/2) // Can abort a retreat with this amount of time left in retreat
251 #define SNIPE_ATTACK_TIME (F1_0*10)
252 #define SNIPE_WAIT_TIME (F1_0*5)
253 #define SNIPE_FIRE_TIME (F1_0*2)
254
255 #define THIEF_PROBABILITY 16384 // 50% chance of stealing an item at each attempt
256
257 void create_buddy_bot(const d_level_shared_robot_info_state &LevelSharedRobotInfoState);
258
259 imobjptridx_t boss_spew_robot(const d_robot_info_array &Robot_info, const object_base &objp, const vms_vector &pos);
260
261 // Amount of time since the current robot was last processed for things such as movement.
262 // It is not valid to use FrameTime because robots do not get moved every frame.
263
264 // --------- John: These variables must be saved as part of gamesave. ---------
265 // ------ John: End of variables which must be saved as part of gamesave. -----
266
267 // These globals are set by a call to find_vector_intersection, which is a slow routine,
268 // so we don't want to call it again (for this object) unless we have to.
269
270 // -- unused, 08/07/95 -- extern void ai_turn_randomly(vms_vector *vec_to_player, object *obj, fix rate, int previous_visibility);
271 void init_ai_frame(player_flags, const control_info &Controls);
272
273 [[nodiscard]]
274 std::size_t create_bfs_list(const object &robot, const robot_info &robptr, vcsegidx_t start_seg, player_flags, std::span<segnum_t> bfs_list);
275 extern void init_thief_for_level();
276
277 void start_robot_death_sequence(object &objp);
278 void buddy_message_str(const char * str) dxx_compiler_attribute_nonnull();
279 void buddy_message(const char *) = delete;
280 void buddy_message(const char * format, ... ) dxx_compiler_attribute_format_printf(1, 2);
281
282 extern void special_reactor_stuff(void);
283 #endif
284 }
285
286 namespace dcx {
287 struct point_seg_array_t : public std::array<point_seg, MAX_POINT_SEGS> {};
288 extern point_seg_array_t Point_segs;
289 extern point_seg_array_t::iterator Point_segs_free_ptr;
290 static inline std::size_t operator-(point_seg_array_t::iterator i, point_seg_array_t &p)
291 {
292 return std::distance(p.begin(), i);
293 }
294
295 enum class create_path_random_flag : bool
296 {
297 nonrandom,
298 random,
299 };
300
301 enum class create_path_safety_flag : bool
302 {
303 unsafe,
304 safe,
305 };
306
307 enum class create_path_result : bool
308 {
309 early,
310 finished,
311 };
312
313 }
314
315 namespace dsx {
316
317 constexpr const robot_info *create_path_unused_robot_info = nullptr;
318
319 #if DXX_BUILD_DESCENT == 1
320 #define do_ai_robot_hit(robot,robptr,type) do_ai_robot_hit(robot,type)
321 #elif DXX_BUILD_DESCENT == 2
322 #undef do_ai_robot_hit
323 void create_path_to_segment(vmobjptridx_t objp, const robot_info &robptr, segnum_t goalseg, unsigned max_length, create_path_safety_flag safety_flag);
324 #endif
325
326 void do_ai_robot_hit(vmobjptridx_t robot, const robot_info &robptr, player_awareness_type_t type);
327
328 void create_path_to_segment(vmobjptridx_t objp, const robot_info &robptr, unsigned max_length, create_path_safety_flag safety_flag, icsegidx_t);
329 void create_path_to_believed_player_segment(vmobjptridx_t objp, const robot_info &robptr, unsigned max_length, create_path_safety_flag safety_flag);
330 void create_path_to_guidebot_player_segment(vmobjptridx_t objp, const robot_info &robptr, unsigned max_length, create_path_safety_flag safety_flag);
331 std::pair<create_path_result, unsigned> create_path_points(vmobjptridx_t objp, const robot_info *robptr, vcsegidx_t start_seg, icsegidx_t end_seg, point_seg_array_t::iterator point_segs, unsigned max_depth, create_path_random_flag random_flag, create_path_safety_flag safety_flag, icsegidx_t avoid_seg);
332
333 void ai_save_state(PHYSFS_File * fp);
334 int ai_restore_state(const d_robot_info_array &Robot_info, NamedPHYSFS_File fp, int version, physfsx_endian swap);
335
336 #if DXX_USE_EDITOR
337 void player_follow_path(object &objp);
338 void check_create_player_path();
339 #endif
340 }
341 #endif