Debian Codegraph @

similar/main/robot.cpp

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 * Code for handling robots
23 *
24 */
25
26
27 #include "dxxerror.h"
28 #include "inferno.h"
29 #include "robot.h"
30 #include "ai.h"
31 #include "object.h"
32 #include "polyobj.h"
33 #include "physfsx.h"
34 #include "d_levelstate.h"
35 #include "segment.h"
36
37 #include "d_enumerate.h"
38 #include "compiler-range_for.h"
39 #include "partial_range.h"
40
41 #if 0
42 static inline void PHYSFSX_writeAngleVec(PHYSFS_File *fp, const vms_angvec &v)
43 {
44 PHYSFS_writeSLE16(fp, v.p);
45 PHYSFS_writeSLE16(fp, v.b);
46 PHYSFS_writeSLE16(fp, v.h);
47 }
48 #endif
49
50 namespace dsx {
51
52 //given an object and a gun number, return position in 3-space of gun
53 //fills in gun_point
54 void calc_gun_point(const robot_info &r, vms_vector &gun_point, const object_base &obj, const robot_gun_number entry_gun_num)
55 {
56 assert(obj.render_type == render_type::RT_POLYOBJ || obj.render_type == render_type::RT_MORPH);
57 assert(underlying_value(get_robot_id(obj)) < LevelSharedRobotInfoState.N_robot_types);
58
59 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
60 const auto &pm = Polygon_models[r.model_num];
61
62 const auto gun_num = (underlying_value(entry_gun_num) >= r.n_guns)
63 ? robot_gun_number::_0
64 : entry_gun_num;
65
66 auto pnt = r.gun_points[gun_num];
67
68 //instance up the tree for this gun
69 auto &anim_angles = obj.rtype.pobj_info.anim_angles;
70 for (unsigned mn = r.gun_submodels[gun_num]; mn != 0; mn = pm.submodel_parents[mn])
71 {
72 const auto &&m = vm_transposed_matrix(vm_angles_2_matrix(anim_angles[mn]));
73 const auto tpnt{vm_vec_build_rotated(pnt, m)};
74
75 vm_vec_add(pnt, tpnt, pm.submodel_offsets[mn]);
76 }
77
78 //now instance for the entire object
79
80 const auto &&m = vm_transposed_matrix(obj.orient);
81 vm_vec_rotate(gun_point,pnt,m);
82 vm_vec_add2(gun_point, obj.pos);
83 }
84
85 //fills in ptr to list of joints, and returns the number of joints in list
86 //takes the robot type (object id), gun number, and desired state
87 std::ranges::subrange<const jointpos *> robot_get_anim_state(const d_robot_info_array &robot_info, const std::array<jointpos, MAX_ROBOT_JOINTS> &robot_joints, const robot_id robot_type, const robot_gun_animation_index gun_num, const robot_animation_state state)
88 {
89 auto &rirt = robot_info[robot_type];
90 auto &as = rirt.anim_states[gun_num][state];
91 const unsigned o = as.offset;
92 return partial_range(robot_joints, o, o + as.n_joints);
93 }
94
95 #ifndef NDEBUG
96 //for test, set a robot to a specific state
97 dxx_compiler_attribute_used
98 static void set_robot_state(object_base &obj, const robot_animation_state state)
99 {
100 auto &Robot_joints = LevelSharedRobotJointState.Robot_joints;
101 int j,jo;
102
103 assert(obj.type == OBJ_ROBOT);
104
105 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
106 auto &ri = Robot_info[get_robot_id(obj)];
107
108 for (auto &as : partial_range(ri.anim_states, std::size_t{ri.n_guns} + 1u))
109 {
110 const auto jl = &as[state];
111 jo = jl->offset;
112
113 for (j=0;j<jl->n_joints;j++,jo++) {
114 int jn;
115
116 jn = Robot_joints[jo].jointnum;
117
118 obj.rtype.pobj_info.anim_angles[jn] = Robot_joints[jo].angles;
119 }
120 }
121 }
122 #endif
123
124 //set the animation angles for this robot. Gun fields of robot info must
125 //be filled in.
126 void robot_set_angles(robot_info &r, const polymodel &pm, const enumerated_array<std::array<vms_angvec, MAX_SUBMODELS>, N_ANIM_STATES, robot_animation_state> &angs)
127 {
128 auto &Robot_joints = LevelSharedRobotJointState.Robot_joints;
129 std::array<robot_gun_animation_index, MAX_SUBMODELS> gun_nums; //which gun each submodel is part of
130 gun_nums[0] = robot_gun_animation_index::None; //body never animates, at least for now
131 /* Mark all other positions as part of the body initially. These markings
132 * may be overridden in the loop below.
133 */
134 std::ranges::fill(partial_range(gun_nums, 1u, pm.n_models), robot_gun_animation_index{r.n_guns});
135
136 for (const std::size_t bound{std::min(gun_nums.size(), pm.submodel_parents.size())}; auto [g, entry_m] : enumerate(partial_const_range(r.gun_submodels, r.n_guns)))
137 {
138 auto m = entry_m;
139 while (m != 0 && m < bound)
140 {
141 /* This cast is safe: `g` is `robot_gun_number` with a valid range of [0, MAX_GUNS - 1].
142 * `robot_gun_animation_index` has a valid range of [0, MAX_GUNS].
143 * Therefore, any valid `robot_gun_number` is a valid
144 * `robot_gun_animation_index`.
145 */
146 gun_nums[m] = static_cast<robot_gun_animation_index>(g); //...unless we find it in a gun
147 m = pm.submodel_parents[m];
148 }
149 }
150
151 const auto n_models = pm.n_models;
152 const auto &&gun_num_model_range = enumerate(partial_range(gun_nums, n_models));
153 for (auto &&[g, ras] : enumerate(partial_range(r.anim_states, std::size_t{r.n_guns} + 1u)))
154 {
155 for (auto &&[state, as] : enumerate(ras))
156 {
157 as.n_joints = 0;
158 as.offset = LevelSharedRobotJointState.N_robot_joints;
159
160 for (auto &&[m, gn] : gun_num_model_range)
161 {
162 if (gn == g)
163 {
164 const auto N_robot_joints = LevelSharedRobotJointState.N_robot_joints ++;
165 Robot_joints[N_robot_joints].jointnum = m;
166 Robot_joints[N_robot_joints].angles = angs[state][m];
167 ras[state].n_joints++;
168 Assert(N_robot_joints < MAX_ROBOT_JOINTS);
169 }
170 }
171 }
172 }
173
174 }
175
176 }
177
178 namespace {
179
180 /*
181 * reads n jointlist structs from a PHYSFS_File
182 */
183 static void jointlist_read(const NamedPHYSFS_File fp, std::array<jointlist, N_ANIM_STATES> &jl)
184 {
185 range_for (auto &i, jl)
186 {
187 i.n_joints = PHYSFSX_readSLE16(fp);
188 i.offset = PHYSFSX_readSLE16(fp);
189 if (!i.n_joints)
190 /* The custom campaign `Descent 2: Enemy Vignettes` has
191 * custom robots with invalid joints. These joints have
192 * invalid offsets, but `n_joints` of 0. This makes the
193 * invalid data easy to detect and clean.
194 *
195 * When the number of joints is zero, discard the loaded
196 * offset and set it to 0, which will always be in range.
197 */
198 i.offset = 0;
199 }
200 }
201
202 }
203
204 namespace dsx {
205
206 imobjptridx_t robot_create(const d_robot_info_array &Robot_info, const robot_id id, const vmsegptridx_t segnum, const vms_vector &pos, const vms_matrix *const orient, const fix size, const ai_behavior behavior, const imsegidx_t hide_segment)
207 {
208 const auto &&objp = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_ROBOT, underlying_value(id), segnum, pos, orient, size, object::control_type::ai, object::movement_type::physics, render_type::RT_POLYOBJ);
209 if (objp)
210 init_ai_object(Robot_info, objp, behavior, hide_segment);
211 return objp;
212 }
213
214 /*
215 * reads n robot_info structs from a PHYSFS_File
216 */
217 void robot_info_read(const NamedPHYSFS_File fp, robot_info &ri)
218 {
219 ri.model_num = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
220 #if DXX_BUILD_DESCENT == 1
221 ri.n_guns = PHYSFSX_readInt(fp);
222 #endif
223 range_for (auto &j, ri.gun_points)
224 PHYSFSX_readVector(fp, j);
225 range_for (auto &j, ri.gun_submodels)
226 j = PHYSFSX_readByte(fp);
227
228 ri.exp1_vclip_num = build_vclip_index_from_untrusted(PHYSFSX_readSLE16(fp));
229 ri.exp1_sound_num = build_sound_effect_from_untrusted(PHYSFSX_readSLE16(fp));
230
231 ri.exp2_vclip_num = build_vclip_index_from_untrusted(PHYSFSX_readSLE16(fp));
232 ri.exp2_sound_num = build_sound_effect_from_untrusted(PHYSFSX_readSLE16(fp));
233
234 #if DXX_BUILD_DESCENT == 1
235 ri.weapon_type = static_cast<weapon_id_type>(PHYSFSX_readSLE16(fp));
236 #elif DXX_BUILD_DESCENT == 2
237 ri.weapon_type = static_cast<weapon_id_type>(PHYSFSX_readByte(fp));
238 ri.weapon_type2 = static_cast<weapon_id_type>(PHYSFSX_readByte(fp));
239 ri.n_guns = PHYSFSX_readByte(fp);
240 #endif
241 const uint8_t contains_id = PHYSFSX_readByte(fp);
242 const uint8_t contains_count = PHYSFSX_readByte(fp);
243 ri.contains_prob = PHYSFSX_readByte(fp);
244 const auto contains_type = PHYSFSX_readByte(fp);
245 ri.contains = build_contained_object_parameters_from_untrusted(contains_type, contains_id, contains_count);
246 #if DXX_BUILD_DESCENT == 1
247 ri.score_value = PHYSFSX_readInt(fp);
248 #elif DXX_BUILD_DESCENT == 2
249 ri.kamikaze = PHYSFSX_readByte(fp);
250
251 ri.score_value = PHYSFSX_readSLE16(fp);
252 ri.badass = PHYSFSX_readByte(fp);
253 ri.energy_drain = PHYSFSX_readByte(fp);
254 #endif
255
256 ri.lighting = PHYSFSX_readFix(fp);
257 ri.strength = PHYSFSX_readFix(fp);
258
259 ri.mass = PHYSFSX_readFix(fp);
260 ri.drag = PHYSFSX_readFix(fp);
261
262 range_for (auto &j, ri.field_of_view)
263 j = PHYSFSX_readFix(fp);
264 range_for (auto &j, ri.firing_wait)
265 j = PHYSFSX_readFix(fp);
266 #if DXX_BUILD_DESCENT == 2
267 range_for (auto &j, ri.firing_wait2)
268 j = PHYSFSX_readFix(fp);
269 #endif
270 range_for (auto &j, ri.turn_time)
271 j = PHYSFSX_readFix(fp);
272 #if DXX_BUILD_DESCENT == 1
273 PHYSFSX_skipBytes<4 * (NDL * 2)>(fp);
274 #endif
275 range_for (auto &j, ri.max_speed)
276 j = PHYSFSX_readFix(fp);
277 range_for (auto &j, ri.circle_distance)
278 j = PHYSFSX_readFix(fp);
279 range_for (auto &j, ri.rapidfire_count)
280 j = PHYSFSX_readByte(fp);
281 range_for (auto &j, ri.evade_speed)
282 j = PHYSFSX_readByte(fp);
283
284 ri.cloak_type = PHYSFSX_readByte(fp);
285 ri.attack_type = PHYSFSX_readByte(fp);
286 #if DXX_BUILD_DESCENT == 1
287 const uint8_t boss_flag = PHYSFSX_readByte(fp);
288 ri.boss_flag = (boss_flag == static_cast<uint8_t>(boss_robot_id::d1_1) || boss_flag == static_cast<uint8_t>(boss_robot_id::d1_superboss)) ? boss_robot_id{boss_flag} : boss_robot_id::None;
289 #endif
290
291 ri.see_sound = build_sound_effect_from_untrusted(PHYSFSX_readByte(fp));
292 ri.attack_sound = build_sound_effect_from_untrusted(PHYSFSX_readByte(fp));
293 ri.claw_sound = build_sound_effect_from_untrusted(PHYSFSX_readByte(fp));
294 #if DXX_BUILD_DESCENT == 2
295 ri.taunt_sound = build_sound_effect_from_untrusted(PHYSFSX_readByte(fp));
296
297 const uint8_t boss_flag = PHYSFSX_readByte(fp);
298 ri.boss_flag = (boss_flag == static_cast<uint8_t>(boss_robot_id::d1_1) || boss_flag == static_cast<uint8_t>(boss_robot_id::d1_superboss) || Boss_spew_more.valid_index(build_boss_robot_index_from_boss_robot_id(boss_robot_id{boss_flag}))) ? boss_robot_id{boss_flag} : boss_robot_id::None;
299 ri.companion = PHYSFSX_readByte(fp);
300 ri.smart_blobs = PHYSFSX_readByte(fp);
301 ri.energy_blobs = PHYSFSX_readByte(fp);
302
303 ri.thief = PHYSFSX_readByte(fp);
304 ri.pursuit = PHYSFSX_readByte(fp);
305 ri.lightcast = PHYSFSX_readByte(fp);
306 ri.death_roll = PHYSFSX_readByte(fp);
307
308 ri.flags = PHYSFSX_readByte(fp);
309 PHYSFSX_skipBytes<3>(fp);
310
311 ri.deathroll_sound = build_sound_effect_from_untrusted(PHYSFSX_readByte(fp));
312 ri.glow = PHYSFSX_readByte(fp);
313 ri.behavior = static_cast<ai_behavior>(PHYSFSX_readByte(fp));
314 ri.aim = PHYSFSX_readByte(fp);
315 #endif
316
317 range_for (auto &j, ri.anim_states)
318 jointlist_read(fp, j);
319
320 ri.always_0xabcd = PHYSFSX_readInt(fp);
321 }
322 }
323
324 /*
325 * reads n jointpos structs from a PHYSFS_File
326 */
327 void jointpos_read(const NamedPHYSFS_File fp, jointpos &jp)
328 {
329 jp.jointnum = PHYSFSX_readSLE16(fp);
330 PHYSFSX_readAngleVec(fp, jp.angles);
331 }
332
333 #if 0
334 void jointpos_write(PHYSFS_File *fp, const jointpos &jp)
335 {
336 PHYSFS_writeSLE16(fp, jp.jointnum);
337 PHYSFSX_writeAngleVec(fp, jp.angles);
338 }
339 #endif