Debian Codegraph @

similar/main/object.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 * object rendering
23 *
24 */
25
26 #include <algorithm>
27 #include <cstdlib>
28 #include <stdio.h>
29
30 #include "inferno.h"
31 #include "game.h"
32 #include "gr.h"
33 #include "bm.h"
34 #include "3d.h"
35 #include "segment.h"
36 #include "texmap.h"
37 #include "laser.h"
38 #include "key.h"
39 #include "gameseg.h"
40 #include "textures.h"
41 #include "object.h"
42 #include "controls.h"
43 #include "physics.h"
44 #include "slew.h"
45 #include "render.h"
46 #include "wall.h"
47 #include "vclip.h"
48 #include "robot.h"
49 #include "interp.h"
50 #include "fireball.h"
51 #include "laser.h"
52 #include "dxxerror.h"
53 #include "ai.h"
54 #include "morph.h"
55 #include "cntrlcen.h"
56 #include "powerup.h"
57 #include "fuelcen.h"
58 #include "endlevel.h"
59 #include "hudmsg.h"
60 #include "sounds.h"
61 #include "collide.h"
62 #include "lighting.h"
63 #include "newdemo.h"
64 #include "player.h"
65 #include "weapon.h"
66 #include "gauges.h"
67 #include "multi.h"
68 #include "text.h"
69 #include "piggy.h"
70 #include "switch.h"
71 #include "gameseq.h"
72 #include "playsave.h"
73 #include "timer.h"
74 #if DXX_USE_EDITOR
75 #include "editor/editor.h"
76 #endif
77
78 #include "compiler-range_for.h"
79 #include "d_construct.h"
80 #include "d_range.h"
81 #include "d_levelstate.h"
82 #include "d_underlying_value.h"
83 #include "partial_range.h"
84 #include <utility>
85
86 using std::min;
87 using std::max;
88
89 namespace dsx {
90
91 namespace {
92
93 static void obj_detach_all(object_array &Objects, object_base &parent);
94 static objnum_t obj_detach_one(object_array &Objects, object &sub);
95
96 static int is_proximity_bomb_or_any_smart_mine(const weapon_id_type id)
97 {
98 const auto r = is_proximity_bomb_or_player_smart_mine(id);
99 #if DXX_BUILD_DESCENT == 2
100 if (r)
101 return r;
102 // superprox dropped by robots have their own ID not considered by is_proximity_bomb_or_player_smart_mine() and since that function is used in many other places, I didn't feel safe to add this weapon type in it
103 if (id == weapon_id_type::ROBOT_SUPERPROX_ID)
104 return 1;
105 #endif
106 return r;
107 }
108
109 }
110
111 /*
112 * Global variables
113 */
114
115 object *ConsoleObject; //the object that is the player
116
117 #ifndef RELEASE
118 //set viewer object to next object in array
119 void object_goto_next_viewer(const object_array &Objects, const object *&viewer)
120 {
121 const auto initial_viewer{viewer};
122 const auto oe{Objects.vcptr.end()};
123 /* Preconditions:
124 * - There exists an integer `i` such that `i < MAX_OBJECTS` and
125 * `&Objects[i] == initial_viewer`.
126 * - There exists at least one object not of type `OBJ_NONE`.
127 * If the first precondition holds, then:
128 * - The test for `candidate_object != initial_viewer` will always terminate the
129 * loop, even if there are no other objects of type other than
130 * `OBJ_NONE`.
131 * If both preconditions hold, then:
132 * - The loop will always find and return a value.
133 */
134 unsigned limit{0};
135 for (auto candidate_object = initial_viewer; ++candidate_object != initial_viewer && limit < MAX_OBJECTS; ++limit)
136 {
137 if (unlikely(candidate_object == oe))
138 /* Wrap around to the beginning. This is only computed if
139 * necessary, so if an object can be found without wrapping, then
140 * `begin()` never needs to be called.
141 */
142 candidate_object = &*Objects.vcptr.begin();
143 auto &objp = *candidate_object;
144 if (objp.type != OBJ_NONE)
145 {
146 viewer = &objp;
147 return;
148 }
149 }
150 }
151 #endif
152
153 imobjptridx_t obj_find_first_of_type(fvmobjptridx &vmobjptridx, const object_type_t type)
154 {
155 range_for (const auto &&i, vmobjptridx)
156 {
157 if (i->type==type)
158 return i;
159 }
160 return object_none;
161 }
162
163 }
164
165 namespace dcx {
166
167 namespace {
168
169 static powerup_type_t build_contained_object_powerup_id_from_untrusted(const uint8_t untrusted)
170 {
171 return untrusted < MAX_POWERUP_TYPES ? powerup_type_t{untrusted} : powerup_type_t{};
172 }
173
174 static robot_id build_contained_object_robot_id_from_untrusted(const uint8_t untrusted)
175 {
176 return untrusted < MAX_ROBOT_TYPES ? robot_id{untrusted} : robot_id{};
177 }
178
179 }
180
181 icobjidx_t laser_info::get_last_hitobj() const
182 {
183 if (!hitobj_count)
184 /* If no elements, return object_none */
185 return object_none;
186 /* Return the most recently written element. `hitobj_pos`
187 * indicates the element to write next, so return
188 * hitobj_values[hitobj_pos - 1]. When hitobj_pos == 0, the
189 * most recently written element is at the end of the array, not
190 * before the beginning of the array.
191 */
192 if (!hitobj_pos)
193 return hitobj_values.back();
194 return hitobj_values[hitobj_pos - 1];
195 }
196
197 contained_object_type build_contained_object_type_from_untrusted(const uint8_t untrusted)
198 {
199 switch (untrusted)
200 {
201 case object_type_t::OBJ_POWERUP:
202 case object_type_t::OBJ_ROBOT:
203 return contained_object_type{untrusted};
204 default:
205 return contained_object_type::None;
206 }
207 }
208
209 contained_object_parameters build_contained_object_parameters_from_untrusted(const uint8_t untrusted_type, const uint8_t untrusted_id, const uint8_t untrusted_contains_count)
210 {
211 if (untrusted_contains_count > 4)
212 /* This is an arbitrary cap, chosen to match GOODY_COUNT_MAX. Any
213 * value higher than this is likely an error, so force the object to
214 * contain nothing instead.
215 */
216 return {};
217 switch (const auto type = build_contained_object_type_from_untrusted(untrusted_type))
218 {
219 case contained_object_type::powerup:
220 return {
221 .type = type,
222 .id = contained_object_id{.powerup = build_contained_object_powerup_id_from_untrusted(untrusted_id)},
223 .count = untrusted_contains_count,
224 };
225 case contained_object_type::robot:
226 return {
227 .type = type,
228 .id = contained_object_id{.robot = build_contained_object_robot_id_from_untrusted(untrusted_id)},
229 .count = untrusted_contains_count,
230 };
231 default:
232 return {};
233 }
234 }
235
236 }
237
238 namespace dsx {
239
240 //draw an object that has one bitmap & doesn't rotate
241 void draw_object_blob(GameBitmaps_array &GameBitmaps, const object_base &Viewer, grs_canvas &canvas, const object_base &obj, const bitmap_index bmi)
242 {
243 auto &bm = GameBitmaps[bmi];
244 PIGGY_PAGE_IN( bmi );
245
246 const auto osize{obj.size};
247 // draw these with slight offset to viewer preventing too much ugly clipping
248 auto pos = obj.pos;
249 if (obj.type == OBJ_FIREBALL && get_fireball_id(obj) == vclip_index::volatile_wall_hit)
250 {
251 vms_vector offs_vec;
252 vm_vec_normalized_dir_quick(offs_vec, Viewer.pos, pos);
253 vm_vec_scale_add2(pos,offs_vec,F1_0);
254 }
255
256 using wh = std::pair<fix, fix>;
257 const auto bm_w = bm.bm_w;
258 const auto bm_h = bm.bm_h;
259 const auto p = (bm_w > bm_h)
260 ? wh(osize, fixmuldiv(osize, bm_h, bm_w))
261 : wh(fixmuldiv(osize, bm_w, bm_h), osize);
262 g3_draw_bitmap(canvas, pos, p.first, p.second, bm);
263 }
264
265 //draw an object that is a texture-mapped rod
266 void draw_object_tmap_rod(grs_canvas &canvas, const d_level_unique_light_state *const LevelUniqueLightState, const vcobjptridx_t obj, const bitmap_index bitmapi)
267 {
268 PIGGY_PAGE_IN(bitmapi);
269
270 auto &bitmap = GameBitmaps[bitmapi];
271
272 const auto delta{vm_vec_copy_scale(obj->orient.uvec, obj->size)};
273
274 const auto top_v = vm_vec_build_add(obj->pos, delta);
275
276 const auto top_p = g3_rotate_point(top_v);
277 const auto bot_p{g3_rotate_point(vm_vec_build_sub(obj->pos, delta))};
278
279 g3_draw_rod_tmap(canvas, bitmap, bot_p, obj->size, top_p, obj->size, (LevelUniqueLightState ? compute_object_light(*LevelUniqueLightState, obj) : g3s_lrgb{F1_0, F1_0, F1_0}), draw_tmap);
280 }
281
282 //used for robot engine glow
283 #define MAX_VELOCITY i2f(50)
284
285 //what darkening level to use when cloaked
286 #define CLOAKED_FADE_LEVEL 28
287
288 #define CLOAK_FADEIN_DURATION_PLAYER F2_0
289 #define CLOAK_FADEOUT_DURATION_PLAYER F2_0
290
291 #define CLOAK_FADEIN_DURATION_ROBOT F1_0
292 #define CLOAK_FADEOUT_DURATION_ROBOT F1_0
293
294 namespace {
295
296 //do special cloaked render
297 static void draw_cloaked_object(grs_canvas &canvas, const object_base &obj, const g3s_lrgb light, glow_values_t glow, const fix64 cloak_start_time, const fix total_cloaked_time, const fix Cloak_fadein_duration, const fix Cloak_fadeout_duration)
298 {
299 fix light_scale=F1_0;
300 int cloak_value{0};
301 bool fading{}; //if true, fading, else cloaking
302
303 if (const auto cloak_delta_time{GameTime64 - cloak_start_time}; cloak_delta_time < Cloak_fadein_duration/2) {
304
305 #if DXX_BUILD_DESCENT == 1
306 light_scale = Cloak_fadein_duration/2 - cloak_delta_time;
307 #elif DXX_BUILD_DESCENT == 2
308 light_scale = fixdiv(Cloak_fadein_duration/2 - cloak_delta_time,Cloak_fadein_duration/2);
309 #endif
310 fading = true;
311 }
312 else if (cloak_delta_time < Cloak_fadein_duration) {
313
314 #if DXX_BUILD_DESCENT == 1
315 cloak_value = f2i((cloak_delta_time - Cloak_fadein_duration/2) * CLOAKED_FADE_LEVEL);
316 #elif DXX_BUILD_DESCENT == 2
317 cloak_value = f2i(fixdiv(cloak_delta_time - Cloak_fadein_duration/2,Cloak_fadein_duration/2) * CLOAKED_FADE_LEVEL);
318 #endif
319
320 } else if (GameTime64 < (cloak_start_time + total_cloaked_time) -Cloak_fadeout_duration) {
321 static int cloak_delta=0,cloak_dir=1;
322 static fix cloak_timer=0;
323
324 //note, if more than one cloaked object is visible at once, the
325 //pulse rate will change!
326
327 cloak_timer -= FrameTime;
328 while (cloak_timer < 0) {
329
330 cloak_timer += Cloak_fadeout_duration/12;
331
332 cloak_delta += cloak_dir;
333
334 if (cloak_delta==0 || cloak_delta==4)
335 cloak_dir = -cloak_dir;
336 }
337
338 cloak_value = CLOAKED_FADE_LEVEL - cloak_delta;
339 } else if (GameTime64 < (cloak_start_time + total_cloaked_time) -Cloak_fadeout_duration/2) {
340
341 #if DXX_BUILD_DESCENT == 1
342 cloak_value = f2i((total_cloaked_time - Cloak_fadeout_duration/2 - cloak_delta_time) * CLOAKED_FADE_LEVEL);
343 #elif DXX_BUILD_DESCENT == 2
344 cloak_value = f2i(fixdiv(total_cloaked_time - Cloak_fadeout_duration/2 - cloak_delta_time,Cloak_fadeout_duration/2) * CLOAKED_FADE_LEVEL);
345 #endif
346
347 } else {
348
349 #if DXX_BUILD_DESCENT == 1
350 light_scale = Cloak_fadeout_duration/2 - (total_cloaked_time - cloak_delta_time);
351 #elif DXX_BUILD_DESCENT == 2
352 light_scale = fixdiv(Cloak_fadeout_duration/2 - (total_cloaked_time - cloak_delta_time),Cloak_fadeout_duration/2);
353 #endif
354 fading = true;
355 }
356
357 const auto alt_textures{
358 #if DXX_BUILD_DESCENT == 2
359 !fading
360 ? alternate_textures{}
361 :
362 #endif
363 ({
364 const std::size_t ati{static_cast<std::size_t>(obj.rtype.pobj_info.alt_textures) - 1u};
365 ati < std::size(multi_player_textures)
366 ? alternate_textures{multi_player_textures[ati]}
367 : alternate_textures{};
368 })
369 };
370
371 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
372 if (fading) {
373 glow[0] = fixmul(glow[0],light_scale);
374 draw_polygon_model(Polygon_models, canvas, draw_tmap, obj.pos,
375 obj.orient,
376 obj.rtype.pobj_info.anim_angles,
377 obj.rtype.pobj_info.model_num, obj.rtype.pobj_info.subobj_flags,
378 g3s_lrgb{
379 .r = fixmul(light.r, light_scale),
380 .g = fixmul(light.g, light_scale),
381 .b = fixmul(light.b, light_scale)
382 },
383 &glow,
384 alt_textures );
385 }
386 else {
387 gr_settransblend(canvas, static_cast<gr_fade_level>(cloak_value), gr_blend::normal);
388 //use special flat drawer
389 draw_polygon_model(Polygon_models, canvas, draw_tmap_flat, obj.pos,
390 obj.orient,
391 obj.rtype.pobj_info.anim_angles,
392 obj.rtype.pobj_info.model_num, obj.rtype.pobj_info.subobj_flags,
393 light,
394 &glow,
395 alt_textures );
396 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::normal);
397 }
398
399 }
400
401 //draw an object which renders as a polygon model
402 static void draw_polygon_object(grs_canvas &canvas, const d_level_unique_light_state &LevelUniqueLightState, const vcobjptridx_t obj)
403 {
404 auto &BossUniqueState = LevelUniqueObjectState.BossState;
405 auto &Robot_info = LevelSharedRobotInfoState.Robot_info;
406 g3s_lrgb light;
407 glow_values_t engine_glow_value;
408 engine_glow_value[0] = 0;
409 #if DXX_BUILD_DESCENT == 2
410 engine_glow_value[1] = -1; //element 0 is for engine glow, 1 for headlight
411 #endif
412
413 // If option set for bright players in netgame, brighten them!
414 light = unlikely(Netgame.BrightPlayers && (Game_mode & GM_MULTI) && obj->type == OBJ_PLAYER)
415 ? g3s_lrgb{F1_0 * 2, F1_0 * 2, F1_0 * 2}
416 : compute_object_light(LevelUniqueLightState, obj);
417
418 #if DXX_BUILD_DESCENT == 2
419 //make robots brighter according to robot glow field
420 if (obj->type == OBJ_ROBOT)
421 {
422 const auto glow = Robot_info[get_robot_id(obj)].glow<<12;
423 light.r += glow; //convert 4:4 to 16:16
424 light.g += glow; //convert 4:4 to 16:16
425 light.b += glow; //convert 4:4 to 16:16
426 }
427
428 if ((obj->type == OBJ_WEAPON &&
429 get_weapon_id(obj) == weapon_id_type::FLARE_ID) ||
430 obj->type == OBJ_MARKER
431 )
432 {
433 light.r += F1_0*2;
434 light.g += F1_0*2;
435 light.b += F1_0*2;
436 }
437 #endif
438
439 push_interpolation_method imsave(1, true);
440
441 //set engine glow value
442 engine_glow_value[0] = f1_0/5;
443 if (obj->movement_source == object::movement_type::physics) {
444
445 if (obj->mtype.phys_info.flags & PF_USES_THRUST && obj->type==OBJ_PLAYER && get_player_id(obj)==Player_num) {
446 const auto thrust_mag = vm_vec_mag_quick(obj->mtype.phys_info.thrust);
447 engine_glow_value[0] += (fixdiv(thrust_mag,Player_ship->max_thrust)*4)/5;
448 }
449 else {
450 const auto speed = vm_vec_mag_quick(obj->mtype.phys_info.velocity);
451 #if DXX_BUILD_DESCENT == 1
452 engine_glow_value[0] += (fixdiv(speed,MAX_VELOCITY)*4)/5;
453 #elif DXX_BUILD_DESCENT == 2
454 engine_glow_value[0] += (fixdiv(speed,MAX_VELOCITY)*3)/5;
455 #endif
456 }
457 }
458
459 #if DXX_BUILD_DESCENT == 2
460 //set value for player headlight
461 if (obj->type == OBJ_PLAYER) {
462 auto &player_flags = obj->ctype.player_info.powerup_flags;
463 if (player_flags & PLAYER_FLAGS_HEADLIGHT && !Endlevel_sequence)
464 if (player_flags & PLAYER_FLAGS_HEADLIGHT_ON)
465 engine_glow_value[1] = -2; //draw white!
466 else
467 engine_glow_value[1] = -1; //draw normal color (grey)
468 else
469 engine_glow_value[1] = -3; //don't draw
470 }
471 #endif
472
473 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
474 if (obj->rtype.pobj_info.tmap_override != -1) {
475 std::array<bitmap_index, 12> bm_ptrs;
476
477 //fill whole array, in case simple model needs more
478 bm_ptrs.fill(Textures[obj->rtype.pobj_info.tmap_override]);
479 draw_polygon_model(Polygon_models, canvas, draw_tmap, obj->pos,
480 obj->orient,
481 obj->rtype.pobj_info.anim_angles,
482 obj->rtype.pobj_info.model_num,
483 obj->rtype.pobj_info.subobj_flags,
484 light,
485 &engine_glow_value,
486 bm_ptrs);
487 }
488 else {
489 std::pair<fix64, fix> cloak_duration;
490 std::pair<fix, fix> cloak_fade;
491 if (obj->type==OBJ_PLAYER && (obj->ctype.player_info.powerup_flags & PLAYER_FLAGS_CLOAKED))
492 {
493 auto &cloak_time = obj->ctype.player_info.cloak_time;
494 cloak_duration = {cloak_time, CLOAK_TIME_MAX};
495 cloak_fade = {CLOAK_FADEIN_DURATION_PLAYER, CLOAK_FADEOUT_DURATION_PLAYER};
496 }
497 else if ((obj->type == OBJ_ROBOT) && (obj->ctype.ai_info.CLOAKED)) {
498 if (Robot_info[get_robot_id(obj)].boss_flag != boss_robot_id::None)
499 cloak_duration = {BossUniqueState.Boss_cloak_start_time, Boss_cloak_duration};
500 else
501 cloak_duration = {GameTime64-F1_0*10, F1_0 * 20};
502 cloak_fade = {CLOAK_FADEIN_DURATION_ROBOT, CLOAK_FADEOUT_DURATION_ROBOT};
503 } else {
504 #if DXX_BUILD_DESCENT == 2
505 if (obj->type == OBJ_ROBOT)
506 {
507 // Snipers get bright when they fire.
508 if (obj->ctype.ai_info.ail.next_fire < F1_0/8) {
509 if (obj->ctype.ai_info.behavior == ai_behavior::AIB_SNIPE)
510 {
511 light.r = 2*light.r + F1_0;
512 light.g = 2*light.g + F1_0;
513 light.b = 2*light.b + F1_0;
514 }
515 }
516 }
517 #endif
518
519 const auto alt_textures{
520 ({
521 const std::size_t ati{static_cast<std::size_t>(obj->rtype.pobj_info.alt_textures) - 1u};
522 ati < std::size(multi_player_textures)
523 ? alternate_textures{multi_player_textures[ati]}
524 : alternate_textures{};
525 })
526 };
527 const auto is_weapon_with_inner_model = (obj->type == OBJ_WEAPON && Weapon_info[get_weapon_id(obj)].model_num_inner != polygon_model_index::None);
528 bool draw_simple_model;
529 if (is_weapon_with_inner_model)
530 {
531 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::additive_a);
532 draw_simple_model = static_cast<fix>(vm_vec_dist_quick(Viewer->pos, obj->pos)) < Simple_model_threshhold_scale * F1_0*2;
533 if (draw_simple_model)
534 draw_polygon_model(Polygon_models, canvas, draw_tmap, obj->pos,
535 obj->orient,
536 obj->rtype.pobj_info.anim_angles,
537 Weapon_info[get_weapon_id(obj)].model_num_inner,
538 obj->rtype.pobj_info.subobj_flags,
539 light,
540 &engine_glow_value,
541 alt_textures);
542 }
543 draw_polygon_model(Polygon_models, canvas, draw_tmap, obj->pos,
544 obj->orient,
545 obj->rtype.pobj_info.anim_angles,obj->rtype.pobj_info.model_num,
546 obj->rtype.pobj_info.subobj_flags,
547 light,
548 &engine_glow_value,
549 alt_textures);
550
551 if (is_weapon_with_inner_model)
552 {
553 if constexpr (!DXX_USE_OGL) // in software rendering must draw inner model last
554 {
555 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::additive_a);
556 if (draw_simple_model)
557 draw_polygon_model(Polygon_models, canvas, draw_tmap, obj->pos,
558 obj->orient,
559 obj->rtype.pobj_info.anim_angles,
560 Weapon_info[obj->id].model_num_inner,
561 obj->rtype.pobj_info.subobj_flags,
562 light,
563 &engine_glow_value,
564 alt_textures);
565 }
566 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::normal);
567 }
568 return;
569 }
570 draw_cloaked_object(canvas, obj, light, engine_glow_value, cloak_duration.first, cloak_duration.second, cloak_fade.first, cloak_fade.second);
571 }
572 }
573
574 }
575
576 }
577
578 //------------------------------------------------------------------------------
579 // These variables are used to keep a list of the 3 closest robots to the viewer.
580 // The code works like this: Every time render object is called with a polygon model,
581 // it finds the distance of that robot to the viewer. If this distance if within 10
582 // segments of the viewer, it does the following: If there aren't already 3 robots in
583 // the closet-robots list, it just sticks that object into the list along with its distance.
584 // If the list already contains 3 robots, then it finds the robot in that list that is
585 // farthest from the viewer. If that object is farther than the object currently being
586 // rendered, then the new object takes over that far object's slot. *Then* after all
587 // objects are rendered, object_render_targets is called an it draws a target on top
588 // of all the objects.
589
590 //091494: #define MAX_CLOSE_ROBOTS 3
591 //--unused-- static int Object_draw_lock_boxes = 0;
592 //091494: static int Object_num_close = 0;
593 //091494: static object * Object_close_ones[MAX_CLOSE_ROBOTS];
594 //091494: static fix Object_close_distance[MAX_CLOSE_ROBOTS];
595
596 //091494: set_close_objects(object *obj)
597 //091494: {
598 //091494: fix dist;
599 //091494:
600 //091494: if ( (obj->type != OBJ_ROBOT) || (Object_draw_lock_boxes==0) )
601 //091494: return;
602 //091494:
603 //091494: // The following code keeps a list of the 10 closest robots to the
604 //091494: // viewer. See comments in front of this function for how this works.
605 //091494: dist = vm_vec_dist( &obj->pos, &Viewer->pos );
606 //091494: if ( dist < i2f(20*10) ) {
607 //091494: if ( Object_num_close < MAX_CLOSE_ROBOTS ) {
608 //091494: Object_close_ones[Object_num_close] = obj;
609 //091494: Object_close_distance[Object_num_close] = dist;
610 //091494: Object_num_close++;
611 //091494: } else {
612 //091494: int i, farthest_robot;
613 //091494: fix farthest_distance;
614 //091494: // Find the farthest robot in the list
615 //091494: farthest_robot = 0;
616 //091494: farthest_distance = Object_close_distance[0];
617 //091494: for (i=1; i<Object_num_close; i++ ) {
618 //091494: if ( Object_close_distance[i] > farthest_distance ) {
619 //091494: farthest_distance = Object_close_distance[i];
620 //091494: farthest_robot = i;
621 //091494: }
622 //091494: }
623 //091494: // If this object is closer to the viewer than
624 //091494: // the farthest in the list, replace the farthest with this object.
625 //091494: if ( farthest_distance > dist ) {
626 //091494: Object_close_ones[farthest_robot] = obj;
627 //091494: Object_close_distance[farthest_robot] = dist;
628 //091494: }
629 //091494: }
630 //091494: }
631 //091494: }
632
633 namespace dcx {
634 objnum_t Player_fired_laser_this_frame=object_none;
635
636 namespace {
637
638 static bool predicate_debris(const object_base &o)
639 {
640 return o.type == OBJ_DEBRIS;
641 }
642
643 static bool predicate_flare(const object_base &o)
644 {
645 return (o.type == OBJ_WEAPON) && (get_weapon_id(o) == weapon_id_type::FLARE_ID);
646 }
647
648 static bool predicate_nonflare_weapon(const object_base &o)
649 {
650 return (o.type == OBJ_WEAPON) && (get_weapon_id(o) != weapon_id_type::FLARE_ID);
651 }
652
653 }
654
655 }
656
657 namespace dsx {
658
659 namespace {
660
661 static bool predicate_fireball(const object &o)
662 {
663 return o.type == OBJ_FIREBALL && o.ctype.expl_info.delete_objnum == object_none;
664 }
665
666 // -----------------------------------------------------------------------------
667 //this routine checks to see if an robot rendered near the middle of
668 //the screen, and if so and the player had fired, "warns" the robot
669 static void set_robot_location_info(object &objp)
670 {
671 auto &Objects = LevelUniqueObjectState.Objects;
672 auto &vcobjptr = Objects.vcptr;
673 if (Player_fired_laser_this_frame != object_none) {
674 const auto &&temp = g3_rotate_point(objp.pos);
675 if ((temp.p3_codes & clipping_code::behind) != clipping_code::None) //robot behind the screen
676 return;
677
678 //the code below to check for object near the center of the screen
679 //completely ignores z, which may not be good
680
681 if ((abs(temp.p3_vec.x) < F1_0*4) && (abs(temp.p3_vec.y) < F1_0*4)) {
682 objp.ctype.ai_info.danger_laser_num = Player_fired_laser_this_frame;
683 objp.ctype.ai_info.danger_laser_signature = vcobjptr(Player_fired_laser_this_frame)->signature;
684 }
685 }
686 }
687
688 static const object *Viewer_save;
689 }
690
691 // ------------------------------------------------------------------------------------------------------------------
692 void create_small_fireball_on_object(const vmobjptridx_t objp, fix size_scale, int sound_flag)
693 {
694 auto &Objects = LevelUniqueObjectState.Objects;
695 fix size;
696 vms_vector pos;
697
698 pos = objp->pos;
699 auto rand_vec = make_random_vector();
700
701 vm_vec_scale(rand_vec, objp->size/2);
702
703 vm_vec_add2(pos, rand_vec);
704
705 #if DXX_BUILD_DESCENT == 1
706 size = fixmul(size_scale, F1_0 + d_rand()*4);
707 #elif DXX_BUILD_DESCENT == 2
708 size = fixmul(size_scale, F1_0/2 + d_rand()*4/2);
709 #endif
710
711 const auto &&segnum = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, pos, Segments.vmptridx(objp->segnum));
712 if (segnum != segment_none) {
713 const auto &&expl_obj = object_create_explosion_without_damage(Vclip, segnum, pos, size, vclip_index::small_explosion);
714 if (!expl_obj)
715 return;
716 obj_attach(Objects, objp, expl_obj);
717 if (d_rand() < 8192) {
718 fix vol = F1_0/2;
719 if (objp->type == OBJ_ROBOT)
720 vol *= 2;
721 if (sound_flag)
722 digi_link_sound_to_object(sound_effect::SOUND_EXPLODING_WALL, objp, 0, vol, sound_stack::allow_stacking);
723 }
724 }
725 }
726
727 // -- mk, 02/05/95 -- #define VCLIP_INVULNERABILITY_EFFECT vclip_index::small_explosion
728 // -- mk, 02/05/95 --
729 // -- mk, 02/05/95 -- // -----------------------------------------------------------------------------
730 // -- mk, 02/05/95 -- void do_player_invulnerability_effect(object *objp)
731 // -- mk, 02/05/95 -- {
732 // -- mk, 02/05/95 -- if (d_rand() < FrameTime*8) {
733 // -- mk, 02/05/95 -- create_vclip_on_object(objp, F1_0, VCLIP_INVULNERABILITY_EFFECT);
734 // -- mk, 02/05/95 -- }
735 // -- mk, 02/05/95 -- }
736
737 // -----------------------------------------------------------------------------
738 // Render an object. Calls one of several routines based on type
739 void render_object(grs_canvas &canvas, const d_level_unique_light_state &LevelUniqueLightState, const vmobjptridx_t obj)
740 {
741 if (unlikely(obj == Viewer))
742 return;
743 if (unlikely(obj->type==OBJ_NONE))
744 {
745 Int3();
746 return;
747 }
748
749 #if !DXX_USE_OGL
750 const auto mld_save = std::exchange(Max_linear_depth, Max_linear_depth_objects);
751 #endif
752
753 bool alpha = false;
754 switch (obj->render_type)
755 {
756 case render_type::RT_NONE:
757 break; //doesn't render, like the player
758
759 case render_type::RT_POLYOBJ:
760 #if DXX_BUILD_DESCENT == 2
761 if ( PlayerCfg.AlphaBlendMarkers && obj->type == OBJ_MARKER ) // set nice transparency/blending for certrain objects
762 {
763 alpha = true;
764 gr_settransblend(canvas, gr_fade_level{10}, gr_blend::additive_a);
765 }
766 #endif
767 draw_polygon_object(canvas, LevelUniqueLightState, obj);
768
769 if (obj->type == OBJ_ROBOT) //"warn" robot if being shot at
770 set_robot_location_info(obj);
771 break;
772
773 case render_type::RT_MORPH:
774 draw_morph_object(canvas, LevelUniqueLightState, obj);
775 break;
776
777 case render_type::RT_FIREBALL:
778 if (PlayerCfg.AlphaBlendFireballs) // set nice transparency/blending for certrain objects
779 {
780 alpha = true;
781 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::additive_c);
782 }
783
784 draw_fireball(Vclip, canvas, obj);
785 break;
786
787 case render_type::RT_WEAPON_VCLIP:
788 if (PlayerCfg.AlphaBlendWeapons && (!is_proximity_bomb_or_any_smart_mine(get_weapon_id(obj))
789 )) // set nice transparency/blending for certain objects
790 {
791 alpha = true;
792 gr_settransblend(canvas, gr_fade_level{7}, gr_blend::additive_a);
793 }
794
795 draw_weapon_vclip(Vclip, Weapon_info, canvas, obj);
796 break;
797
798 case render_type::RT_HOSTAGE:
799 draw_hostage(Vclip, canvas, LevelUniqueLightState, obj);
800 break;
801
802 case render_type::RT_POWERUP:
803 if (PlayerCfg.AlphaBlendPowerups) // set nice transparency/blending for certrain objects
804 switch ( get_powerup_id(obj) )
805 {
806 case powerup_type_t::POW_EXTRA_LIFE:
807 case powerup_type_t::POW_ENERGY:
808 case powerup_type_t::POW_SHIELD_BOOST:
809 case powerup_type_t::POW_CLOAK:
810 case powerup_type_t::POW_INVULNERABILITY:
811 #if DXX_BUILD_DESCENT == 2
812 case powerup_type_t::POW_HOARD_ORB:
813 #endif
814 alpha = true;
815 gr_settransblend(canvas, gr_fade_level{7}, gr_blend::additive_a);
816 break;
817 case powerup_type_t::POW_LASER:
818 case powerup_type_t::POW_KEY_BLUE:
819 case powerup_type_t::POW_KEY_RED:
820 case powerup_type_t::POW_KEY_GOLD:
821 case powerup_type_t::POW_MISSILE_1:
822 case powerup_type_t::POW_MISSILE_4:
823 case powerup_type_t::POW_QUAD_FIRE:
824 case powerup_type_t::POW_VULCAN_WEAPON:
825 case powerup_type_t::POW_SPREADFIRE_WEAPON:
826 case powerup_type_t::POW_PLASMA_WEAPON:
827 case powerup_type_t::POW_FUSION_WEAPON:
828 case powerup_type_t::POW_PROXIMITY_WEAPON:
829 case powerup_type_t::POW_HOMING_AMMO_1:
830 case powerup_type_t::POW_HOMING_AMMO_4:
831 case powerup_type_t::POW_SMARTBOMB_WEAPON:
832 case powerup_type_t::POW_MEGA_WEAPON:
833 case powerup_type_t::POW_VULCAN_AMMO:
834 case powerup_type_t::POW_TURBO:
835 case powerup_type_t::POW_MEGAWOW:
836 #if DXX_BUILD_DESCENT == 2
837 case powerup_type_t::POW_FULL_MAP:
838 case powerup_type_t::POW_HEADLIGHT:
839 case powerup_type_t::POW_GAUSS_WEAPON:
840 case powerup_type_t::POW_HELIX_WEAPON:
841 case powerup_type_t::POW_PHOENIX_WEAPON:
842 case powerup_type_t::POW_OMEGA_WEAPON:
843 case powerup_type_t::POW_SUPER_LASER:
844 case powerup_type_t::POW_CONVERTER:
845 case powerup_type_t::POW_AMMO_RACK:
846 case powerup_type_t::POW_AFTERBURNER:
847 case powerup_type_t::POW_SMISSILE1_1:
848 case powerup_type_t::POW_SMISSILE1_4:
849 case powerup_type_t::POW_GUIDED_MISSILE_1:
850 case powerup_type_t::POW_GUIDED_MISSILE_4:
851 case powerup_type_t::POW_SMART_MINE:
852 case powerup_type_t::POW_MERCURY_MISSILE_1:
853 case powerup_type_t::POW_MERCURY_MISSILE_4:
854 case powerup_type_t::POW_EARTHSHAKER_MISSILE:
855 case powerup_type_t::POW_FLAG_BLUE:
856 case powerup_type_t::POW_FLAG_RED:
857 #endif
858 break;
859 }
860
861 draw_powerup(Vclip, canvas, obj);
862 break;
863
864 case render_type::RT_LASER:
865 if (PlayerCfg.AlphaBlendLasers) // set nice transparency/blending for certrain objects
866 {
867 alpha = true;
868 gr_settransblend(canvas, gr_fade_level{7}, gr_blend::additive_a);
869 }
870
871 Laser_render(canvas, obj);
872 break;
873
874 default:
875 Error("Unknown render_type <%d>", underlying_value(obj->render_type));
876 }
877
878 if (alpha)
879 gr_settransblend(canvas, GR_FADE_OFF, gr_blend::normal); // revert any transparency/blending setting back to normal
880
881 if (obj->render_type != render_type::RT_NONE && Newdemo_state == ND_STATE_RECORDING)
882 newdemo_record_render_object(obj);
883 #if !DXX_USE_OGL
884 Max_linear_depth = mld_save;
885 #endif
886 }
887
888 void reset_player_object(object_base &ConsoleObject)
889 {
890 //Init physics
891
892 ConsoleObject.mtype.phys_info.velocity = {};
893 ConsoleObject.mtype.phys_info.thrust = {};
894 ConsoleObject.mtype.phys_info.rotvel = {};
895 ConsoleObject.mtype.phys_info.rotthrust = {};
896 ConsoleObject.mtype.phys_info.turnroll = 0;
897 ConsoleObject.mtype.phys_info.mass = Player_ship->mass;
898 ConsoleObject.mtype.phys_info.drag = Player_ship->drag;
899 ConsoleObject.mtype.phys_info.flags =
900 #ifndef DXX_FEATURE_DISABLE_SHIP_WIGGLE
901 PF_WIGGLE |
902 #endif
903 PF_TURNROLL | PF_LEVELLING | PF_USES_THRUST;
904
905 //Init render info
906
907 ConsoleObject.render_type = render_type::RT_POLYOBJ;
908 ConsoleObject.rtype.pobj_info.model_num = Player_ship->model_num; //what model is this?
909 ConsoleObject.rtype.pobj_info.subobj_flags = 0; //zero the flags
910 ConsoleObject.rtype.pobj_info.tmap_override = -1; //no tmap override!
911 ConsoleObject.rtype.pobj_info.anim_angles = {};
912
913 // Clear misc
914
915 ConsoleObject.flags = 0;
916 }
917
918 //make object0 the player, setting all relevant fields
919 void init_player_object(const d_level_shared_polygon_model_state &LevelSharedPolygonModelState, object_base &console)
920 {
921 console.type = OBJ_PLAYER;
922 set_player_id(console, 0); //no sub-types for player
923 console.signature = object_signature_t{0};
924 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
925 console.size = Polygon_models[Player_ship->model_num].rad;
926 console.control_source = object::control_type::slew; //default is player slewing
927 console.movement_source = object::movement_type::physics; //change this sometime
928 console.lifeleft = IMMORTAL_TIME;
929 console.attached_obj = object_none;
930 reset_player_object(console);
931 }
932
933 //sets up the free list & init player & whatever else
934 void init_objects()
935 {
936 auto &Objects = LevelUniqueObjectState.Objects;
937 auto &vmobjptr = Objects.vmptr;
938 for (objnum_t i = 0; i< MAX_OBJECTS; ++i)
939 {
940 LevelUniqueObjectState.free_obj_list[i] = i;
941 auto &obj = *vmobjptr(i);
942 DXX_POISON_VAR(obj, 0xfd);
943 obj.type = OBJ_NONE;
944 }
945
946 range_for (unique_segment &j, Segments)
947 j.objects = object_none;
948
949 Viewer = ConsoleObject = &Objects.front();
950
951 init_player_object(LevelSharedPolygonModelState, *ConsoleObject);
952 obj_link_unchecked(Objects.vmptr, Objects.vmptridx(ConsoleObject), Segments.vmptridx(segment_first)); //put in the world in segment 0
953 LevelUniqueObjectState.num_objects = 1; //just the player
954 Objects.set_count(1);
955 }
956
957 //after calling init_object(), the network code has grabbed specific
958 //object slots without allocating them. Go though the objects & build
959 //the free list, then set the appropriate globals
960 void special_reset_objects(d_level_unique_object_state &LevelUniqueObjectState, const d_robot_info_array &Robot_info)
961 {
962 unsigned num_objects = MAX_OBJECTS;
963
964 auto &Objects = LevelUniqueObjectState.get_objects();
965 Objects.set_count(1);
966 assert(Objects.front().type != OBJ_NONE); //0 should be used
967
968 DXX_POISON_VAR(LevelUniqueObjectState.free_obj_list, 0xfd);
969 #if DXX_BUILD_DESCENT == 1
970 /* Descent 1 does not have a guidebot, so there is nothing to fix up. For
971 * simplicity, both games pass the parameter.
972 */
973 (void)Robot_info;
974 #elif DXX_BUILD_DESCENT == 2
975 icobjidx_t Buddy_objnum = object_none;
976 #endif
977 for (objnum_t i = MAX_OBJECTS; i--;)
978 {
979 const auto &obj = *Objects.vcptr(i);
980 #if DXX_BUILD_DESCENT == 2
981 if (obj.type == OBJ_ROBOT)
982 {
983 auto &robptr = Robot_info[get_robot_id(obj)];
984 if (robot_is_companion(robptr))
985 Buddy_objnum = i;
986 }
987 #endif
988 if (obj.type == OBJ_NONE)
989 LevelUniqueObjectState.free_obj_list[--num_objects] = i;
990 else
991 if (i > Highest_object_index)
992 Objects.set_count(i + 1);
993 }
994 #if DXX_BUILD_DESCENT == 2
995 LevelUniqueObjectState.BuddyState.Buddy_objnum = Buddy_objnum;
996 #endif
997 LevelUniqueObjectState.num_objects = num_objects;
998 }
999
1000 void obj_link_unchecked(fvmobjptr &vmobjptr, const vmobjptridx_t obj, const vmsegptridx_t segnum)
1001 {
1002 obj->segnum = segnum;
1003 unique_segment &useg = segnum;
1004 obj->next = std::exchange(useg.objects, obj);
1005 obj->prev = object_none;
1006
1007 if (obj->next != object_none)
1008 vmobjptr(obj->next)->prev = obj;
1009 }
1010
1011 void obj_unlink(fvmobjptr &vmobjptr, fvmsegptr &vmsegptr, object_base &obj)
1012 {
1013 const auto next{obj.next};
1014 /* It is a bug elsewhere if vmsegptr ever fails here. However, it is
1015 * expensive to check, so only force verification in debug builds.
1016 *
1017 * In debug builds, always compute it, for the side effect of
1018 * validating the segment number.
1019 *
1020 * In release builds, compute it when it is needed.
1021 */
1022 #ifndef NDEBUG
1023 unique_segment &segp = vmsegptr(obj.segnum);
1024 #endif
1025 ((obj.prev == object_none)
1026 ? (
1027 #ifdef NDEBUG
1028 static_cast<unique_segment &>(vmsegptr(obj.segnum))
1029 #else
1030 segp
1031 #endif
1032 ).objects
1033 : vmobjptr(obj.prev)->next) = next;
1034
1035 obj.segnum = segment_none;
1036
1037 if (next != object_none)
1038 vmobjptr(next)->prev = obj.prev;
1039 DXX_POISON_VAR(obj.next, 0xfa);
1040 DXX_POISON_VAR(obj.prev, 0xfa);
1041 }
1042
1043 //returns the number of a free object, updating Highest_object_index.
1044 //Generally, obj_create() should be called to get an object, since it
1045 //fills in important fields and does the linking.
1046 //returns -1 if no free objects
1047 imobjptridx_t obj_allocate(d_level_unique_object_state &LevelUniqueObjectState)
1048 {
1049 auto &Objects = LevelUniqueObjectState.Objects;
1050 if (LevelUniqueObjectState.num_objects >= Objects.size())
1051 return object_none;
1052
1053 const auto objnum = LevelUniqueObjectState.free_obj_list[LevelUniqueObjectState.num_objects++];
1054 if (objnum >= Objects.get_count())
1055 {
1056 Objects.set_count(objnum + 1);
1057 }
1058 const auto &&r = Objects.vmptridx(objnum);
1059 assert(r->type == OBJ_NONE);
1060 return r;
1061 }
1062
1063 namespace {
1064
1065 //frees up an object. Generally, obj_delete() should be called to get
1066 //rid of an object. This function deallocates the object entry after
1067 //the object has been unlinked
1068 static void obj_free(d_level_unique_object_state &LevelUniqueObjectState, const vmobjidx_t objnum)
1069 {
1070 const auto num_objects = -- LevelUniqueObjectState.num_objects;
1071 assert(num_objects < LevelUniqueObjectState.free_obj_list.size());
1072 LevelUniqueObjectState.free_obj_list[num_objects] = objnum;
1073 auto &Objects = LevelUniqueObjectState.get_objects();
1074
1075 objnum_t o = objnum;
1076 if (o == Highest_object_index)
1077 {
1078 for (;;)
1079 {
1080 --o;
1081 if (Objects.vcptr(o)->type != OBJ_NONE)
1082 break;
1083 if (o == 0)
1084 break;
1085 }
1086 Objects.set_count(o + 1);
1087 }
1088 }
1089
1090 //-----------------------------------------------------------------------------
1091 // Scan the object list, freeing down to num_used objects
1092 // Returns number of slots freed.
1093 static void free_object_slots(uint_fast32_t num_used)
1094 {
1095 auto &Objects = LevelUniqueObjectState.Objects;
1096 auto &vmobjptr = Objects.vmptr;
1097 std::array<object *, MAX_OBJECTS> obj_list;
1098 unsigned num_already_free, num_to_free, olind = 0;
1099
1100 num_already_free = MAX_OBJECTS - Highest_object_index - 1;
1101
1102 if (MAX_OBJECTS - num_already_free < num_used)
1103 return;
1104
1105 for (auto &obj : vmobjptr)
1106 {
1107 if (obj.flags & OF_SHOULD_BE_DEAD)
1108 {
1109 num_already_free++;
1110 if (MAX_OBJECTS - num_already_free < num_used)
1111 return;
1112 } else
1113 switch (obj.type)
1114 {
1115 case OBJ_NONE:
1116 num_already_free++;
1117 if (MAX_OBJECTS - num_already_free < num_used)
1118 return;
1119 break;
1120 case OBJ_WALL:
1121 Int3(); // This is curious. What is an object that is a wall?
1122 break;
1123 case OBJ_FIREBALL:
1124 case OBJ_WEAPON:
1125 case OBJ_DEBRIS:
1126 obj_list[olind++] = &obj;
1127 break;
1128 case OBJ_ROBOT:
1129 case OBJ_HOSTAGE:
1130 case OBJ_PLAYER:
1131 case OBJ_CNTRLCEN:
1132 case OBJ_CLUTTER:
1133 case OBJ_GHOST:
1134 case OBJ_LIGHT:
1135 case OBJ_CAMERA:
1136 case OBJ_POWERUP:
1137 case OBJ_COOP:
1138 case OBJ_MARKER:
1139 break;
1140 }
1141
1142 }
1143
1144 num_to_free = MAX_OBJECTS - num_used - num_already_free;
1145
1146 if (num_to_free > olind) {
1147 num_to_free = olind;
1148 }
1149
1150 // Capture before num_to_free modified
1151 const auto &&r = partial_const_range(obj_list, num_to_free);
1152 auto l = [&vmobjptr, &r, &num_to_free](const auto predicate) -> bool {
1153 range_for (const auto i, r)
1154 {
1155 auto &o = *vmobjptr(i);
1156 if (predicate(o))
1157 {
1158 o.flags |= OF_SHOULD_BE_DEAD;
1159 if (!-- num_to_free)
1160 return true;
1161 }
1162 }
1163 return false;
1164 };
1165
1166 if (l(predicate_debris))
1167 return;
1168
1169 if (l(predicate_fireball))
1170 return;
1171
1172 if (l(predicate_flare))
1173 return;
1174
1175 if (l(predicate_nonflare_weapon))
1176 return;
1177 }
1178
1179 }
1180
1181 //-----------------------------------------------------------------------------
1182 //initialize a new object. adds to the list for the given segment
1183 //note that segnum is really just a suggestion, since this routine actually
1184 //searches for the correct segment
1185 //returns the object number
1186 imobjptridx_t obj_create(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const object_type_t type, const unsigned id, vmsegptridx_t segnum, const vms_vector &pos, const vms_matrix *const orient, const fix size, const typename object::control_type ctype, const typename object::movement_type mtype, const render_type rtype)
1187 {
1188 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
1189 auto &Objects = LevelUniqueObjectState.Objects;
1190 auto &Vertices = LevelSharedVertexState.get_vertices();
1191 // Some consistency checking. FIXME: Add more debug output here to probably trace all possible occurances back.
1192 assert(ctype <= object::control_type::cntrlcen);
1193
1194 if (type == OBJ_DEBRIS && LevelUniqueObjectState.Debris_object_count >= Max_debris_objects && !PERSISTENT_DEBRIS)
1195 return object_none;
1196
1197 auto &vcvertptr = Vertices.vcptr;
1198 if (get_seg_masks(vcvertptr, pos, segnum, 0).centermask != sidemask_t{})
1199 {
1200 const auto &&p = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, pos, segnum);
1201 if (p == segment_none) {
1202 return object_none; //don't create this object
1203 }
1204 segnum = p;
1205 }
1206
1207 // Find next free object
1208 const auto &&obj = obj_allocate(LevelUniqueObjectState);
1209
1210 if (obj == object_none) //no free objects
1211 return object_none;
1212
1213 // Zero out object structure to keep weird bugs from happening
1214 // in uninitialized fields.
1215 const auto signature = obj->signature;
1216 /* Test the version in the object structure, not the local copy.
1217 * This produces a more useful diagnostic from Valgrind if the
1218 * test reports a problem.
1219 */
1220 DXX_CHECK_VAR_IS_DEFINED(obj->signature);
1221 *obj = {};
1222 // Tell Valgrind to warn on any uninitialized fields.
1223 DXX_POISON_VAR(*obj, 0xfd);
1224
1225 obj->signature = next(signature);
1226 obj->type = type;
1227 obj->id = id;
1228 obj->pos = pos;
1229 obj->size = size;
1230 obj->flags = 0;
1231 //@@if (orient != NULL)
1232 //@@ obj->orient = *orient;
1233
1234 obj->orient = orient?*orient:vmd_identity_matrix;
1235
1236 obj->control_source = ctype;
1237 obj->movement_source = mtype;
1238 obj->render_type = rtype;
1239 obj->contains.count = 0;
1240 obj->matcen_creator = 0;
1241 obj->lifeleft = IMMORTAL_TIME; //assume immortal
1242 obj->attached_obj = object_none;
1243
1244 if (obj->control_source == object::control_type::powerup)
1245 {
1246 obj->ctype.powerup_info.count = 1;
1247 obj->ctype.powerup_info.flags = 0;
1248 obj->ctype.powerup_info.creation_time = {GameTime64};
1249 }
1250
1251 // Init physics info for this object
1252 if (obj->movement_source == object::movement_type::physics) {
1253 obj->mtype.phys_info = {};
1254 }
1255
1256 if (obj->render_type == render_type::RT_POLYOBJ)
1257 {
1258 obj->rtype.pobj_info.subobj_flags = 0;
1259 obj->rtype.pobj_info.tmap_override = -1;
1260 obj->rtype.pobj_info.alt_textures = 0;
1261 }
1262
1263 obj->shields = 20*F1_0;
1264
1265 {
1266 const auto &&p = find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, pos, segnum); //find correct segment
1267 // Previously this was only an assert check. Now it is also
1268 // checked at runtime.
1269 segnum = p;
1270 }
1271
1272 obj_link_unchecked(Objects.vmptr, obj, segnum);
1273
1274 if (obj->control_source == object::control_type::explosion)
1275 obj->ctype.expl_info.next_attach = obj->ctype.expl_info.prev_attach = obj->ctype.expl_info.attach_parent = object_none;
1276
1277 if (obj->type == OBJ_DEBRIS)
1278 ++ LevelUniqueObjectState.Debris_object_count;
1279 return obj;
1280 }
1281
1282 imobjptridx_t obj_weapon_create(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const weapon_info_array &Weapon_info, const unsigned id, const vmsegptridx_t segnum, const vms_vector &pos, const fix size, const render_type rtype)
1283 {
1284 constexpr auto ctype = object::control_type::weapon;
1285 constexpr auto mtype = object::movement_type::physics;
1286 constexpr const vms_matrix *orient = nullptr;
1287 const auto &&objp = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_WEAPON, id, segnum, pos, orient, size, ctype, mtype, rtype);
1288 if (objp == object_none)
1289 return objp;
1290 auto &obj = *objp;
1291 // Set (or not) persistent bit in phys_info.
1292 assert(obj.control_source == object::control_type::weapon);
1293 if (Weapon_info[id].persistent != weapon_info::persistence_flag::terminate_on_impact)
1294 obj.mtype.phys_info.flags |= PF_PERSISTENT;
1295 obj.ctype.laser_info.creation_time = {GameTime64};
1296 obj.ctype.laser_info.clear_hitobj();
1297 obj.ctype.laser_info.multiplier = F1_0;
1298 #if DXX_BUILD_DESCENT == 2
1299 obj.ctype.laser_info.last_afterburner_time = 0;
1300 #endif
1301 return objp;
1302 }
1303
1304 //create a copy of an object. returns new object number
1305 imobjptridx_t obj_create_copy(const object &srcobj, const vmsegptridx_t newsegnum)
1306 {
1307 auto &Objects = LevelUniqueObjectState.Objects;
1308 // Find next free object
1309 const auto &&obj = obj_allocate(LevelUniqueObjectState);
1310
1311 if (obj == object_none)
1312 return object_none;
1313
1314 const auto signature = obj->signature;
1315 *obj = srcobj;
1316
1317 obj_link_unchecked(Objects.vmptr, obj, newsegnum);
1318 obj->signature = next(signature);
1319
1320 //we probably should initialize sub-structures here
1321
1322 return obj;
1323 }
1324
1325 //remove object from the world
1326 void obj_delete(d_level_unique_object_state &LevelUniqueObjectState, segment_array &Segments, const vmobjptridx_t obj)
1327 {
1328 auto &Objects = LevelUniqueObjectState.get_objects();
1329 Assert(obj->type != OBJ_NONE);
1330 Assert(obj != ConsoleObject);
1331
1332 #if DXX_BUILD_DESCENT == 2
1333 if (obj->type==OBJ_WEAPON && get_weapon_id(obj)==weapon_id_type::GUIDEDMISS_ID && obj->ctype.laser_info.parent_type==OBJ_PLAYER)
1334 {
1335 const auto pnum = get_player_id(Objects.vcptr(obj->ctype.laser_info.parent_num));
1336 const auto &&gimobj = LevelUniqueObjectState.Guided_missile.get_player_active_guided_missile(Objects.vmptridx, pnum);
1337 if (gimobj == obj)
1338 {
1339 LevelUniqueObjectState.Guided_missile.clear_player_active_guided_missile(pnum);
1340 if (pnum == Player_num)
1341 {
1342 if (!PlayerCfg.GuidedInBigWindow)
1343 do_cockpit_window_view(gauge_inset_window_view::secondary, weapon_box_user::post_missile_static);
1344 if (Newdemo_state == ND_STATE_RECORDING)
1345 newdemo_record_guided_end();
1346 }
1347 }
1348 }
1349 else if (LevelUniqueObjectState.BuddyState.Buddy_objnum == obj)
1350 LevelUniqueObjectState.BuddyState.Buddy_objnum = object_none;
1351 #endif
1352
1353 if (obj == Viewer) //deleting the viewer?
1354 Viewer = ConsoleObject; //..make the player the viewer
1355
1356 if (obj->flags & OF_ATTACHED) //detach this from object
1357 obj_detach_one(Objects, obj);
1358
1359 if (obj->attached_obj != object_none) //detach all objects from this
1360 obj_detach_all(Objects, obj);
1361
1362 if (obj->type == OBJ_DEBRIS)
1363 -- LevelUniqueObjectState.Debris_object_count;
1364
1365 if (obj->movement_source == object::movement_type::physics && (obj->mtype.phys_info.flags & PF_STICK))
1366 LevelUniqueStuckObjectState.remove_stuck_object(obj);
1367 obj_unlink(Objects.vmptr, Segments.vmptr, obj);
1368 const auto signature = obj->signature;
1369 DXX_POISON_VAR(*obj, 0xfa);
1370 obj->type = OBJ_NONE; //unused!
1371 /* Preserve signature across the poison value. When the object slot
1372 * is reused, the allocator will need the old signature so that the
1373 * new one can be derived from it. No other sites should read it
1374 * until that happens.
1375 */
1376 obj->signature = signature;
1377 obj_free(LevelUniqueObjectState, obj);
1378 }
1379
1380 #define DEATH_SEQUENCE_LENGTH (F1_0*5)
1381 #define DEATH_SEQUENCE_EXPLODE_TIME (F1_0*2)
1382
1383 object *Dead_player_camera; // Object index of object watching deader.
1384 }
1385 namespace dcx {
1386 player_dead_state Player_dead_state = player_dead_state::no; // If !0, then player is dead, but game continues so he can watch.
1387 namespace {
1388 static int Player_flags_save;
1389 static fix Camera_to_player_dist_goal = F1_0*4;
1390 static typename object::control_type Control_type_save;
1391 static render_type Render_type_save;
1392 }
1393
1394 unsigned laser_parent_is_matching_signature(const laser_parent &l, const object_base &o)
1395 {
1396 if (l.parent_type != o.type)
1397 return 0;
1398 return l.parent_signature == o.signature;
1399 }
1400
1401 }
1402
1403 namespace dsx {
1404
1405 // ------------------------------------------------------------------------------------------------------------------
1406 void dead_player_end(void)
1407 {
1408 auto &Objects = LevelUniqueObjectState.Objects;
1409 auto &vmobjptridx = Objects.vmptridx;
1410 if (Player_dead_state == player_dead_state::no)
1411 return;
1412
1413 if (Newdemo_state == ND_STATE_RECORDING)
1414 newdemo_record_restore_cockpit();
1415
1416 Player_dead_state = player_dead_state::no;
1417 obj_delete(LevelUniqueObjectState, Segments, vmobjptridx(Dead_player_camera));
1418 Dead_player_camera = NULL;
1419 select_cockpit(PlayerCfg.CockpitMode[0]);
1420 Viewer = Viewer_save;
1421 ConsoleObject->type = OBJ_PLAYER;
1422 ConsoleObject->flags = Player_flags_save;
1423
1424 assert(Control_type_save == object::control_type::flying || Control_type_save == object::control_type::slew);
1425
1426 ConsoleObject->control_source = Control_type_save;
1427 ConsoleObject->render_type = Render_type_save;
1428 auto &player_info = ConsoleObject->ctype.player_info;
1429 player_info.powerup_flags &= ~PLAYER_FLAGS_INVULNERABLE;
1430 player_info.Player_eggs_dropped = false;
1431 }
1432
1433 namespace {
1434
1435 // ------------------------------------------------------------------------------------------------------------------
1436 // Camera is less than size of player away from
1437 static void set_camera_pos(vms_vector &camera_pos, const vcobjptridx_t objp)
1438 {
1439 int count{0};
1440 fix camera_player_dist;
1441 fix far_scale;
1442
1443 camera_player_dist = vm_vec_dist_quick(camera_pos, objp->pos);
1444
1445 if (camera_player_dist < Camera_to_player_dist_goal) {
1446 // Camera is too close to player object, so move it away.
1447 fvi_info hit_data;
1448
1449 auto player_camera_vec{vm_vec_build_sub(camera_pos, objp->pos)};
1450 if (player_camera_vec == vms_vector{})
1451 player_camera_vec.x += F1_0/16;
1452
1453 auto hit_type = fvi_hit_type::Wall;
1454 far_scale = F1_0;
1455
1456 while (hit_type != fvi_hit_type::None && count++ < 6)
1457 {
1458 vm_vec_normalize_quick(player_camera_vec);
1459 vm_vec_scale(player_camera_vec, Camera_to_player_dist_goal);
1460
1461 const auto closer_p1 = vm_vec_build_add(objp->pos, player_camera_vec); // This is the actual point we want to put the camera at.
1462 vm_vec_scale(player_camera_vec, far_scale); // ...but find a point 50% further away...
1463 const auto local_p1 = vm_vec_build_add(objp->pos, player_camera_vec); // ...so we won't have to do as many cuts.
1464
1465 hit_type = find_vector_intersection(fvi_query{
1466 objp->pos,
1467 local_p1,
1468 fvi_query::unused_ignore_obj_list,
1469 fvi_query::unused_LevelUniqueObjectState,
1470 fvi_query::unused_Robot_info,
1471 0,
1472 objp,
1473 }, objp->segnum, 0, hit_data);
1474 if (hit_type == fvi_hit_type::None)
1475 {
1476 camera_pos = closer_p1;
1477 } else {
1478 reconstruct_at(player_camera_vec, make_random_vector);
1479 far_scale = 3*F1_0/2;
1480 }
1481 }
1482 }
1483 }
1484
1485 }
1486
1487 // ------------------------------------------------------------------------------------------------------------------
1488 window_event_result dead_player_frame(const d_robot_info_array &Robot_info)
1489 {
1490 auto &Objects = LevelUniqueObjectState.Objects;
1491 auto &vcobjptridx = Objects.vcptridx;
1492 auto &vmobjptr = Objects.vmptr;
1493 auto &vmobjptridx = Objects.vmptridx;
1494 static fix time_dead = 0;
1495
1496 if (Player_dead_state != player_dead_state::no)
1497 {
1498 time_dead += FrameTime;
1499
1500 // If unable to create camera at time of death, create now.
1501 if (Dead_player_camera == Viewer_save) {
1502 const auto &player = get_local_plrobj();
1503 const auto &&objnum = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_CAMERA, 0, vmsegptridx(player.segnum), player.pos, &player.orient, 0, object::control_type::None, object::movement_type::None, render_type::RT_NONE);
1504
1505 if (objnum != object_none)
1506 Viewer = Dead_player_camera = objnum;
1507 else {
1508 Int3();
1509 }
1510 }
1511
1512 ConsoleObject->mtype.phys_info.rotvel.x = max(0, DEATH_SEQUENCE_EXPLODE_TIME - time_dead)/4;
1513 ConsoleObject->mtype.phys_info.rotvel.y = max(0, DEATH_SEQUENCE_EXPLODE_TIME - time_dead)/2;
1514 ConsoleObject->mtype.phys_info.rotvel.z = max(0, DEATH_SEQUENCE_EXPLODE_TIME - time_dead)/3;
1515
1516 Camera_to_player_dist_goal = min(time_dead*8, F1_0*20) + ConsoleObject->size;
1517
1518 set_camera_pos(Dead_player_camera->pos, vcobjptridx(ConsoleObject));
1519
1520 // the following line uncommented by WraithX, 4-12-00
1521 if (time_dead < DEATH_SEQUENCE_EXPLODE_TIME + F1_0 * 2)
1522 {
1523 const auto fvec{vm_vec_build_sub(ConsoleObject->pos, Dead_player_camera->pos)};
1524 reconstruct_at(Dead_player_camera->orient, vm_vector_to_matrix, fvec);
1525 Dead_player_camera->mtype.phys_info = ConsoleObject->mtype.phys_info;
1526
1527 // the following "if" added by WraithX to get rid of camera "wiggle"
1528 Dead_player_camera->mtype.phys_info.flags &= ~PF_WIGGLE;
1529 // end "if" added by WraithX, 4/13/00
1530
1531 // the following line uncommented by WraithX, 4-12-00
1532 }
1533 else
1534 {
1535 // the following line uncommented by WraithX, 4-11-00
1536 Dead_player_camera->movement_source = object::movement_type::physics;
1537 //Dead_player_camera->mtype.phys_info.rotvel.y = F1_0/8;
1538 // the following line uncommented by WraithX, 4-12-00
1539 }
1540 // end addition by WX
1541
1542 if (time_dead > DEATH_SEQUENCE_EXPLODE_TIME) {
1543 if (Player_dead_state != player_dead_state::exploded)
1544 {
1545 auto &player_info = get_local_plrobj().ctype.player_info;
1546 const auto hostages_lost = std::exchange(player_info.mission.hostages_on_board, 0);
1547
1548 if (hostages_lost > 1)
1549 HUD_init_message(HM_DEFAULT, TXT_SHIP_DESTROYED_2, hostages_lost);
1550 else
1551 HUD_init_message_literal(HM_DEFAULT, hostages_lost == 1 ? ( { const auto &&m = TXT_SHIP_DESTROYED_1; std::span<const char>(m, strlen(m)); } ) : ( { const auto &&m = TXT_SHIP_DESTROYED_0; std::span<const char>(m, strlen(m)); } ));
1552
1553 Player_dead_state = player_dead_state::exploded;
1554
1555 const auto cobjp = vmobjptridx(ConsoleObject);
1556 drop_player_eggs(cobjp);
1557 player_info.Player_eggs_dropped = true;
1558 if (Game_mode & GM_MULTI)
1559 {
1560 multi_send_player_deres(deres_explode);
1561 }
1562
1563 explode_badass_player(Robot_info, cobjp);
1564
1565 //is this next line needed, given the badass call above?
1566 explode_object(LevelUniqueObjectState, Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, cobjp, 0);
1567 ConsoleObject->flags &= ~OF_SHOULD_BE_DEAD; //don't really kill player
1568 ConsoleObject->render_type = render_type::RT_NONE; //..just make him disappear
1569 ConsoleObject->type = OBJ_GHOST; //..and kill intersections
1570 #if DXX_BUILD_DESCENT == 2
1571 player_info.powerup_flags &= ~PLAYER_FLAGS_HEADLIGHT_ON;
1572 #endif
1573 }
1574 } else {
1575 if (d_rand() < FrameTime*4) {
1576 if (Game_mode & GM_MULTI)
1577 multi_send_create_explosion(Player_num);
1578 create_small_fireball_on_object(vmobjptridx(ConsoleObject), F1_0, 1);
1579 }
1580 }
1581
1582
1583 if (GameViewUniqueState.Death_sequence_aborted)
1584 {
1585 auto &player_info = get_local_plrobj().ctype.player_info;
1586 if (!player_info.Player_eggs_dropped) {
1587 player_info.Player_eggs_dropped = true;
1588 drop_player_eggs(vmobjptridx(ConsoleObject));
1589 if (Game_mode & GM_MULTI)
1590 {
1591 multi_send_player_deres(deres_explode);
1592 }
1593 }
1594
1595 return DoPlayerDead(); //kill_player();
1596 }
1597 }
1598 else
1599 time_dead = 0;
1600
1601 return window_event_result::handled;
1602 }
1603
1604 namespace {
1605
1606 // ------------------------------------------------------------------------------------------------------------------
1607 static void start_player_death_sequence(object &player)
1608 {
1609 auto &Objects = LevelUniqueObjectState.Objects;
1610 #if DXX_BUILD_DESCENT == 2
1611 auto &vmobjptr = Objects.vmptr;
1612 #endif
1613 auto &vmobjptridx = Objects.vmptridx;
1614 assert(&player == ConsoleObject);
1615 if (Player_dead_state != player_dead_state::no ||
1616 Dead_player_camera != NULL ||
1617 ((Game_mode & GM_MULTI) && get_local_player().connected != player_connection_status::playing))
1618 return;
1619
1620 //Assert(Dead_player_camera == NULL);
1621
1622 reset_rear_view();
1623
1624 if (!(Game_mode & GM_MULTI))
1625 HUD_clear_messages();
1626
1627 GameViewUniqueState.Death_sequence_aborted = 0;
1628
1629 if (Game_mode & GM_MULTI)
1630 {
1631 #if DXX_BUILD_DESCENT == 2
1632 // If Hoard, increase number of orbs by 1. Only if you haven't killed yourself. This prevents cheating
1633 if (game_mode_hoard(Game_mode))
1634 {
1635 auto &player_info = player.ctype.player_info;
1636 auto &proximity = player_info.hoard.orbs;
1637 if (proximity < player_info.max_hoard_orbs)
1638 {
1639 const auto is_bad_kill = [&vmobjptr]{
1640 auto &lplr = get_local_player();
1641 auto &lplrobj = get_local_plrobj();
1642 const auto killer_objnum = lplrobj.ctype.player_info.killer_objnum;
1643 if (killer_objnum == lplr.objnum)
1644 /* Self kill */
1645 return true;
1646 if (killer_objnum == object_none)
1647 /* Non-player kill */
1648 return true;
1649 const auto &&killer_objp = vmobjptr(killer_objnum);
1650 if (killer_objp->type != OBJ_PLAYER)
1651 return true;
1652 if (!(Game_mode & GM_TEAM))
1653 return false;
1654 return multi_get_team_from_player(Netgame, Player_num) == multi_get_team_from_player(Netgame, get_player_id(killer_objp));
1655 };
1656 if (!is_bad_kill())
1657 ++ proximity;
1658 }
1659 }
1660 #endif
1661 multi_send_kill(vmobjptridx(get_local_player().objnum));
1662 }
1663
1664 PaletteRedAdd = 40;
1665 Player_dead_state = player_dead_state::yes;
1666
1667 player.mtype.phys_info.rotthrust = {};
1668 player.mtype.phys_info.thrust = {};
1669
1670 const auto &&objnum = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_CAMERA, 0, vmsegptridx(player.segnum), player.pos, &player.orient, 0, object::control_type::None, object::movement_type::None, render_type::RT_NONE);
1671 Viewer_save = Viewer;
1672 if (objnum != object_none)
1673 Viewer = Dead_player_camera = objnum;
1674 else {
1675 Int3();
1676 Dead_player_camera = ConsoleObject;
1677 }
1678
1679 select_cockpit(cockpit_mode_t::letterbox);
1680 if (Newdemo_state == ND_STATE_RECORDING)
1681 newdemo_record_letterbox();
1682
1683 Player_flags_save = player.flags;
1684 Control_type_save = player.control_source;
1685 Render_type_save = player.render_type;
1686
1687 player.flags &= ~OF_SHOULD_BE_DEAD;
1688 // Players[Player_num].flags |= PLAYER_FLAGS_INVULNERABLE;
1689 player.control_source = object::control_type::None;
1690
1691 PALETTE_FLASH_SET(0,0,0);
1692 }
1693
1694 // ------------------------------------------------------------------------------------------------------------------
1695 static void obj_delete_all_that_should_be_dead()
1696 {
1697 auto &Objects = LevelUniqueObjectState.Objects;
1698 auto &vmobjptridx = Objects.vmptridx;
1699 objnum_t local_dead_player_object=object_none;
1700
1701 // Move all objects
1702 range_for (const auto &&objp, vmobjptridx)
1703 {
1704 if ((objp->type!=OBJ_NONE) && (objp->flags&OF_SHOULD_BE_DEAD) ) {
1705 Assert(!(objp->type==OBJ_FIREBALL && objp->ctype.expl_info.delete_time!=-1));
1706 if (objp->type==OBJ_PLAYER) {
1707 if ( get_player_id(objp) == Player_num ) {
1708 if (local_dead_player_object == object_none) {
1709 start_player_death_sequence(objp);
1710 local_dead_player_object = objp;
1711 } else
1712 Int3(); // Contact Mike: Illegal, killed player twice in this frame!
1713 // Ok to continue, won't start death sequence again!
1714 // kill_player();
1715 }
1716 } else {
1717 obj_delete(LevelUniqueObjectState, Segments, objp);
1718 }
1719 }
1720 }
1721 }
1722
1723 }
1724
1725 //when an object has moved into a new segment, this function unlinks it
1726 //from its old segment, and links it into the new segment
1727 void obj_relink(fvmobjptr &vmobjptr, fvmsegptr &vmsegptr, const vmobjptridx_t objnum, const vmsegptridx_t newsegnum)
1728 {
1729 obj_unlink(vmobjptr, vmsegptr, objnum);
1730 obj_link_unchecked(vmobjptr, objnum, newsegnum);
1731 }
1732
1733 // for getting out of messed up linking situations (i.e. caused by demo playback)
1734 void obj_relink_all(void)
1735 {
1736 auto &Objects = LevelUniqueObjectState.Objects;
1737 for (unique_segment &useg : vmsegptr)
1738 {
1739 useg.objects = object_none;
1740 }
1741
1742 for (const auto &&obj : Objects.vmptridx)
1743 {
1744 if (obj->type != OBJ_NONE)
1745 {
1746 auto segnum = obj->segnum;
1747 if (segnum > Highest_segment_index)
1748 segnum = segment_first;
1749 obj_link_unchecked(Objects.vmptr, obj, Segments.vmptridx(segnum));
1750 }
1751 }
1752 }
1753
1754 namespace {
1755
1756 //process a continuously-spinning object
1757 static void spin_object(object_base &obj)
1758 {
1759 vms_angvec rotangs;
1760 assert(obj.movement_source == object::movement_type::spinning);
1761
1762 const fix frametime = FrameTime;
1763 rotangs.p = fixmul(obj.mtype.spin_rate.x, frametime);
1764 rotangs.h = fixmul(obj.mtype.spin_rate.y, frametime);
1765 rotangs.b = fixmul(obj.mtype.spin_rate.z, frametime);
1766
1767 const auto &&rotmat{vm_angles_2_matrix(rotangs)};
1768 obj.orient = vm_matrix_x_matrix(obj.orient, rotmat);
1769 check_and_fix_matrix(obj.orient);
1770 }
1771
1772 }
1773
1774 #if DXX_BUILD_DESCENT == 2
1775 imobjidx_t d_guided_missile_indices::get_player_active_guided_missile(const playernum_t pnum) const
1776 {
1777 return operator[](pnum);
1778 }
1779
1780 /* Place debug checks out of line so that they are shared among the
1781 * template instantiations.
1782 */
1783 bool d_guided_missile_indices::debug_check_current_object(const object_base &obj)
1784 {
1785 assert(obj.type == OBJ_WEAPON);
1786 const auto gmid = get_weapon_id(obj);
1787 if (obj.type != OBJ_WEAPON)
1788 return false;
1789 assert(gmid == weapon_id_type::GUIDEDMISS_ID);
1790 if (gmid != weapon_id_type::GUIDEDMISS_ID)
1791 return false;
1792 return true;
1793 }
1794
1795 template <typename result_type, typename object_factory>
1796 result_type d_guided_missile_indices::get_player_active_guided_missile_tmpl(object_factory &fobjptr, const playernum_t pnum) const
1797 {
1798 const auto gmidx = get_player_active_guided_missile(pnum);
1799 if (gmidx == object_none)
1800 return object_none;
1801 auto &&gmobj = fobjptr(gmidx);
1802 if (!debug_check_current_object(gmobj))
1803 return object_none;
1804 return gmobj;
1805 }
1806
1807 imobjptr_t d_guided_missile_indices::get_player_active_guided_missile(fvmobjptr &vmobjptr, const playernum_t pnum) const
1808 {
1809 return this->template get_player_active_guided_missile_tmpl<imobjptr_t>(vmobjptr, pnum);
1810 }
1811
1812 imobjptridx_t d_guided_missile_indices::get_player_active_guided_missile(fvmobjptridx &vmobjptridx, const playernum_t pnum) const
1813 {
1814 return this->template get_player_active_guided_missile_tmpl<imobjptridx_t>(vmobjptridx, pnum);
1815 }
1816
1817 void d_guided_missile_indices::set_player_active_guided_missile(const vmobjidx_t obji, const playernum_t pnum)
1818 {
1819 auto &i = operator[](pnum);
1820 i = obji;
1821 }
1822
1823 void d_guided_missile_indices::clear_player_active_guided_missile(const playernum_t pnum)
1824 {
1825 auto &i = operator[](pnum);
1826 i = object_none;
1827 }
1828
1829 int Drop_afterburner_blob_flag; //ugly hack
1830 //see if wall is volatile, and if so, cause damage to player
1831 //returns true if player is in lava
1832 #endif
1833
1834 namespace {
1835
1836 //--------------------------------------------------------------------
1837 //move an object for the current frame
1838 static window_event_result object_move_one(const d_level_shared_robot_info_state &LevelSharedRobotInfoState, const vmobjptridx_t obj, control_info &Controls)
1839 {
1840 #if DXX_BUILD_DESCENT == 2
1841 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
1842 auto &Vertices = LevelSharedVertexState.get_vertices();
1843 #endif
1844 auto &Objects = LevelUniqueObjectState.Objects;
1845 auto &vmobjptr = Objects.vmptr;
1846 const auto previous_segment = obj->segnum;
1847 auto result = window_event_result::handled;
1848
1849 const auto obj_previous_position = obj->pos; // Save the current position
1850
1851 if ((obj->type==OBJ_PLAYER) && (Player_num==get_player_id(obj))) {
1852 const auto &&segp = vmsegptr(obj->segnum);
1853 #if DXX_BUILD_DESCENT == 2
1854 if (game_mode_capture_flag(Game_mode))
1855 fuelcen_check_for_goal(obj, segp);
1856 else if (game_mode_hoard(Game_mode))
1857 fuelcen_check_for_hoard_goal(obj, segp);
1858 #endif
1859
1860 auto &player_info = obj->ctype.player_info;
1861 auto &energy = player_info.energy;
1862 const fix fuel = fuelcen_give_fuel(segp, INITIAL_ENERGY - energy);
1863 if (fuel > 0 ) {
1864 energy += fuel;
1865 }
1866 auto &pl_shields = obj->shields;
1867 const fix shields = repaircen_give_shields(segp, INITIAL_SHIELDS - pl_shields);
1868 if (shields > 0) {
1869 pl_shields += shields;
1870 }
1871 }
1872
1873 {
1874 auto lifeleft = obj->lifeleft;
1875 if (lifeleft != IMMORTAL_TIME) //if not immortal...
1876 {
1877 lifeleft -= FrameTime; //...inevitable countdown towards death
1878 #if DXX_BUILD_DESCENT == 2
1879 if (obj->type == OBJ_MARKER)
1880 {
1881 if (lifeleft < F1_0*1000)
1882 lifeleft += F1_0; // Make sure this object doesn't go out.
1883 }
1884 #endif
1885 obj->lifeleft = lifeleft;
1886 }
1887 }
1888 #if DXX_BUILD_DESCENT == 2
1889 Drop_afterburner_blob_flag = 0;
1890 #endif
1891
1892 switch (obj->control_source) {
1893
1894 case object::control_type::None:
1895 break;
1896
1897 case object::control_type::flying:
1898 read_flying_controls(obj, Controls);
1899 break;
1900
1901 case object::control_type::repaircen:
1902 Int3();
1903 // -- hey! these are no longer supported!! -- do_repair_sequence(obj);
1904 break;
1905
1906 case object::control_type::powerup:
1907 do_powerup_frame(Vclip, obj);
1908 break;
1909
1910 case object::control_type::morph: //morph implies AI
1911 do_morph_frame(obj);
1912 //NOTE: FALLS INTO AI HERE!!!!
1913 [[fallthrough]];
1914
1915 case object::control_type::ai:
1916 //NOTE LINK TO object::control_type::morph ABOVE!!!
1917 if (Game_suspended & SUSP_ROBOTS) return window_event_result::ignored;
1918 do_ai_frame(LevelSharedRobotInfoState, obj);
1919 break;
1920
1921 case object::control_type::weapon:
1922 Laser_do_weapon_sequence(LevelSharedRobotInfoState.Robot_info, obj);
1923 break;
1924 case object::control_type::explosion:
1925 do_explosion_sequence(LevelSharedRobotInfoState.Robot_info, obj);
1926 break;
1927
1928 case object::control_type::slew:
1929 #ifdef RELEASE
1930 obj->control_source = object::control_type::None;
1931 con_printf(CON_URGENT, DXX_STRINGIZE_FL(__FILE__, __LINE__, "BUG: object %hu has control type object::control_type::slew, sig/type/id = %hu/%i/%i"), static_cast<objnum_t>(obj), static_cast<uint16_t>(obj->signature), obj->type, obj->id);
1932 #else
1933 if ( keyd_pressed[KEY_PAD5] ) slew_stop();
1934 if ( keyd_pressed[KEY_NUMLOCK] ) {
1935 slew_reset_orient();
1936 }
1937 slew_frame(0 ); // Does velocity addition for us.
1938 #endif
1939 break;
1940
1941 // case object::control_type::flythrough:
1942 // do_flythrough(obj,0); // HACK:do_flythrough should operate on an object!!!!
1943 // //check_object_seg(obj);
1944 // return; // DON'T DO THE REST OF OBJECT STUFF SINCE THIS IS A SPECIAL CASE!!!
1945 // break;
1946
1947 case object::control_type::debris:
1948 do_debris_frame(LevelSharedRobotInfoState.Robot_info, obj);
1949 break;
1950
1951 case object::control_type::light:
1952 break; //doesn't do anything
1953
1954 case object::control_type::remote:
1955 break; //doesn't do anything
1956
1957 case object::control_type::cntrlcen:
1958 do_controlcen_frame(LevelSharedRobotInfoState.Robot_info, obj);
1959 break;
1960
1961 default:
1962 Error("Unknown control type %u in object %hu, sig/type/id = %i/%i/%i", static_cast<unsigned>(obj->control_source), static_cast<objnum_t>(obj), static_cast<uint16_t>(obj->signature), obj->type, obj->id);
1963 break;
1964
1965 }
1966
1967 if (obj->lifeleft < 0 ) { // We died of old age
1968 obj->flags |= OF_SHOULD_BE_DEAD;
1969 if ( obj->type==OBJ_WEAPON && Weapon_info[get_weapon_id(obj)].damage_radius )
1970 explode_badass_weapon(LevelSharedRobotInfoState.Robot_info, obj, obj->pos);
1971 #if DXX_BUILD_DESCENT == 2
1972 else if ( obj->type==OBJ_ROBOT) //make robots explode
1973 explode_object(LevelUniqueObjectState, LevelSharedRobotInfoState.Robot_info, LevelSharedSegmentState, LevelUniqueSegmentState, obj, 0);
1974 #endif
1975 }
1976
1977 if (obj->type == OBJ_NONE || obj->flags&OF_SHOULD_BE_DEAD)
1978 return window_event_result::ignored; // object has been deleted
1979
1980 bool prepare_seglist = false;
1981 phys_visited_seglist phys_visited_segs;
1982 switch (obj->movement_source) {
1983
1984 case object::movement_type::None:
1985 break; //this doesn't move
1986
1987 case object::movement_type::physics: //move by physics
1988 result = do_physics_sim(LevelSharedRobotInfoState.Robot_info, obj, obj_previous_position, obj->type == OBJ_PLAYER ? (prepare_seglist = true, phys_visited_segs.nsegs = 0, &phys_visited_segs) : nullptr);
1989 break;
1990
1991 case object::movement_type::spinning:
1992 spin_object(obj);
1993 break;
1994 }
1995
1996 #if DXX_BUILD_DESCENT == 2
1997 auto &Walls = LevelUniqueWallSubsystemState.Walls;
1998 auto &vcwallptr = Walls.vcptr;
1999 #endif
2000 // If player and moved to another segment, see if hit any triggers.
2001 // also check in player under a lavafall
2002 if (prepare_seglist)
2003 {
2004 if (previous_segment != obj->segnum && phys_visited_segs.nsegs > 1)
2005 {
2006 auto seg0 = vmsegptridx(phys_visited_segs.seglist[0]);
2007 #if DXX_BUILD_DESCENT == 2
2008 int old_level = Current_level_num;
2009 #endif
2010 range_for (const auto i, partial_const_range(phys_visited_segs.seglist, 1u, phys_visited_segs.nsegs))
2011 {
2012 const auto &&seg1 = seg0.absolute_sibling(i);
2013 const auto connect_side = find_connect_side(seg1, seg0);
2014 if (connect_side != side_none)
2015 {
2016 result = check_trigger(seg0, connect_side, get_local_plrobj(), obj, 0);
2017 #if DXX_BUILD_DESCENT == 2
2018 //maybe we've gone on to the next level. if so, bail!
2019 if (Current_level_num != old_level)
2020 return window_event_result::ignored;
2021 #endif
2022 }
2023 seg0 = seg1;
2024 }
2025 }
2026 #if DXX_BUILD_DESCENT == 2
2027 {
2028 bool under_lavafall = false;
2029
2030 auto &playing = obj->ctype.player_info.lavafall_hiss_playing;
2031 const auto &&segp = vcsegptr(obj->segnum);
2032 auto &vcvertptr = Vertices.vcptr;
2033 if (const auto sidemask = get_seg_masks(vcvertptr, obj->pos, segp, obj->size).sidemask; sidemask != sidemask_t{})
2034 {
2035 for (const auto sidenum : MAX_SIDES_PER_SEGMENT)
2036 {
2037 if (!(sidemask & build_sidemask(sidenum)))
2038 continue;
2039 const auto wall_num = segp->shared_segment::sides[sidenum].wall_num;
2040 if (wall_num != wall_none && vcwallptr(wall_num)->type == WALL_ILLUSION)
2041 {
2042 const auto type = check_volatile_wall(obj, segp->unique_segment::sides[sidenum]);
2043 if (type != volatile_wall_result::none)
2044 {
2045 under_lavafall = 1;
2046 if (!playing)
2047 {
2048 playing = 1;
2049 const auto sound = (type == volatile_wall_result::lava) ? sound_effect::SOUND_LAVAFALL_HISS : sound_effect::SOUND_SHIP_IN_WATERFALL;
2050 digi_link_sound_to_object3(sound, obj, 1, F1_0, sound_stack::allow_stacking, vm_distance{i2f(256)}, -1, -1);
2051 break;
2052 }
2053 }
2054 }
2055 }
2056 }
2057
2058 if (!under_lavafall && playing)
2059 {
2060 playing = 0;
2061 digi_kill_sound_linked_to_object( obj);
2062 }
2063 }
2064 #endif
2065 }
2066
2067 #if DXX_BUILD_DESCENT == 2
2068 //see if guided missile has flown through exit trigger
2069 if (obj == LevelUniqueObjectState.Guided_missile.get_player_active_guided_missile(Player_num))
2070 {
2071 if (previous_segment != obj->segnum) {
2072 const auto &&psegp = vcsegptr(previous_segment);
2073 const auto &&connect_side = find_connect_side(vcsegptridx(obj->segnum), psegp);
2074 if (connect_side != side_none)
2075 {
2076 const auto wall_num = psegp->shared_segment::sides[connect_side].wall_num;
2077 if ( wall_num != wall_none ) {
2078 auto trigger_num = vcwallptr(wall_num)->trigger;
2079 if (trigger_num != trigger_none)
2080 {
2081 auto &Triggers = LevelUniqueWallSubsystemState.Triggers;
2082 auto &vctrgptr = Triggers.vcptr;
2083 const auto &&t = vctrgptr(trigger_num);
2084 if (t->type == trigger_action::normal_exit)
2085 obj->lifeleft = 0;
2086 }
2087 }
2088 }
2089 }
2090 }
2091
2092 if (Drop_afterburner_blob_flag) {
2093 Assert(obj==ConsoleObject);
2094 drop_afterburner_blobs(obj, 2, i2f(5)/2, -1); // -1 means use default lifetime
2095 if (Game_mode & GM_MULTI)
2096 multi_send_drop_blobs(Player_num);
2097 Drop_afterburner_blob_flag = 0;
2098 }
2099
2100 if ((obj->type == OBJ_WEAPON) && (Weapon_info[get_weapon_id(obj)].afterburner_size)) {
2101 fix vel = vm_vec_mag_quick(obj->mtype.phys_info.velocity);
2102 fix delay, lifetime;
2103
2104 if (vel > F1_0*200)
2105 delay = F1_0/16;
2106 else if (vel > F1_0*40)
2107 delay = fixdiv(F1_0*13,vel);
2108 else
2109 delay = F1_0/4;
2110
2111 lifetime = (delay * 3)/2;
2112 if (!(Game_mode & GM_MULTI)) {
2113 delay /= 2;
2114 lifetime *= 2;
2115 }
2116
2117 assert(obj->control_source == object::control_type::weapon);
2118 if ((obj->ctype.laser_info.last_afterburner_time + delay < GameTime64) || (obj->ctype.laser_info.last_afterburner_time > GameTime64)) {
2119 obj->ctype.laser_info.last_afterburner_time = {GameTime64};
2120 drop_afterburner_blobs(obj, 1, i2f(Weapon_info[get_weapon_id(obj)].afterburner_size)/16, lifetime);
2121 }
2122 }
2123
2124 #endif
2125 return result;
2126 }
2127
2128 //--------------------------------------------------------------------
2129 //move all objects for the current frame
2130 static window_event_result object_move_all(const d_level_shared_robot_info_state &LevelSharedRobotInfoState)
2131 {
2132 auto &Objects = LevelUniqueObjectState.Objects;
2133 auto &vmobjptridx = Objects.vmptridx;
2134 auto result = window_event_result::ignored;
2135
2136 if (Highest_object_index > MAX_USED_OBJECTS)
2137 free_object_slots(MAX_USED_OBJECTS); // Free all possible object slots.
2138
2139 obj_delete_all_that_should_be_dead();
2140
2141 if (PlayerCfg.AutoLeveling)
2142 ConsoleObject->mtype.phys_info.flags |= PF_LEVELLING;
2143 else
2144 ConsoleObject->mtype.phys_info.flags &= ~PF_LEVELLING;
2145
2146 // Move all objects
2147 range_for (const auto &&objp, vmobjptridx)
2148 {
2149 if ( (objp->type != OBJ_NONE) && (!(objp->flags&OF_SHOULD_BE_DEAD)) ) {
2150 result = std::max(object_move_one(LevelSharedRobotInfoState, objp, Controls), result);
2151 }
2152 }
2153
2154 // check_duplicate_objects();
2155 // remove_incorrect_objects();
2156
2157 return result;
2158 }
2159
2160 }
2161
2162 window_event_result game_move_all_objects(const d_level_shared_robot_info_state &LevelSharedRobotInfoState)
2163 {
2164 LevelUniqueObjectState.last_console_player_position = ConsoleObject->pos;
2165 return object_move_all(LevelSharedRobotInfoState);
2166 }
2167
2168 window_event_result endlevel_move_all_objects(const d_level_shared_robot_info_state &LevelSharedRobotInfoState)
2169 {
2170 return object_move_all(LevelSharedRobotInfoState);
2171 }
2172
2173 //--unused-- // -----------------------------------------------------------
2174 //--unused-- // Moved here from eobject.c on 02/09/94 by MK.
2175 //--unused-- int find_last_obj(int i)
2176 //--unused-- {
2177 //--unused-- for (i=MAX_OBJECTS;--i>=0;)
2178 //--unused-- if (Objects[i].type != OBJ_NONE) break;
2179 //--unused--
2180 //--unused-- return i;
2181 //--unused--
2182 //--unused-- }
2183
2184 #if DXX_USE_EDITOR
2185 //make object array non-sparse
2186 void compress_objects(void)
2187 {
2188 auto &Objects = LevelUniqueObjectState.Objects;
2189 auto &vmobjptr = Objects.vmptr;
2190 auto &vmobjptridx = Objects.vmptridx;
2191 //last_i = find_last_obj(MAX_OBJECTS);
2192
2193 // Note: It's proper to do < (rather than <=) Highest_object_index here because we
2194 // are just removing gaps, and the last object can't be a gap.
2195 for (objnum_t start_i=0;start_i<Highest_object_index;start_i++)
2196 {
2197 const auto &&start_objp = vmobjptridx(start_i);
2198 if (start_objp->type == OBJ_NONE) {
2199 auto highest = Highest_object_index;
2200 const auto &&h = vmobjptr(static_cast<objnum_t>(highest));
2201 auto segnum_copy = h->segnum;
2202
2203 obj_unlink(Objects.vmptr, Segments.vmptr, h);
2204
2205 *start_objp = *h;
2206
2207 if (Cur_object_index == Highest_object_index)
2208 Cur_object_index = start_i;
2209
2210 h->type = OBJ_NONE;
2211
2212 {
2213 //link the object into the list for its segment
2214 const auto &&segnum = vmsegptridx(segnum_copy);
2215 #ifndef NDEBUG
2216 const auto &obj = *start_objp;
2217 assert(obj.segnum == segment_none);
2218 assert(obj.next == object_none);
2219 assert(obj.prev == object_none);
2220 #endif
2221 obj_link_unchecked(Objects.vmptr, start_objp, segnum);
2222 }
2223
2224 while (vmobjptr(static_cast<objnum_t>(--highest))->type == OBJ_NONE)
2225 {
2226 }
2227 Objects.set_count(highest + 1);
2228
2229 //last_i = find_last_obj(last_i);
2230
2231 }
2232 }
2233 reset_objects(LevelUniqueObjectState, LevelUniqueObjectState.num_objects);
2234 }
2235 #endif
2236
2237 //called after load. Takes number of objects, and objects should be
2238 //compressed. resets free list, marks unused objects as unused
2239 void reset_objects(d_level_unique_object_state &LevelUniqueObjectState, const unsigned n_objs)
2240 {
2241 LevelUniqueObjectState.Debris_object_count = 0;
2242 LevelUniqueObjectState.num_objects = n_objs;
2243 assert(LevelUniqueObjectState.num_objects > 0);
2244 auto &Objects = LevelUniqueObjectState.get_objects();
2245 assert(LevelUniqueObjectState.num_objects < Objects.size());
2246 Objects.set_count(n_objs);
2247 #if DXX_BUILD_DESCENT == 2
2248 if (LevelUniqueObjectState.BuddyState.Buddy_objnum.get_unchecked_index() >= n_objs)
2249 LevelUniqueObjectState.BuddyState.Buddy_objnum = object_none;
2250 #endif
2251
2252 for (objnum_t i = n_objs; i < MAX_OBJECTS; ++i)
2253 {
2254 LevelUniqueObjectState.free_obj_list[i] = i;
2255 auto &obj = *Objects.vmptr(i);
2256 DXX_POISON_VAR(obj, 0xfd);
2257 obj.type = OBJ_NONE;
2258 obj.signature = object_signature_t{0};
2259 }
2260 }
2261
2262 //Tries to find a segment for an object, using find_point_seg()
2263 imsegptridx_t find_object_seg(const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const object_base &obj)
2264 {
2265 auto &Segments = LevelUniqueSegmentState.get_segments();
2266 return find_point_seg(LevelSharedSegmentState, LevelUniqueSegmentState, obj.pos, Segments.vmptridx(obj.segnum));
2267 }
2268
2269
2270 //If an object is in a segment, set its segnum field and make sure it's
2271 //properly linked. If not in any segment, returns 0, else 1.
2272 //callers should generally use find_vector_intersection()
2273 int update_object_seg(fvmobjptr &vmobjptr, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const vmobjptridx_t obj)
2274 {
2275 const auto &&newseg = find_object_seg(LevelSharedSegmentState, LevelUniqueSegmentState, obj);
2276 if (newseg == segment_none)
2277 return 0;
2278
2279 if ( newseg != obj->segnum )
2280 obj_relink(vmobjptr, LevelUniqueSegmentState.get_segments().vmptr, obj, newseg);
2281
2282 return 1;
2283 }
2284
2285 unsigned laser_parent_is_player(fvcobjptr &vcobjptr, const laser_parent &l, const object_base &o)
2286 {
2287 /* Player objects are never recycled, so skip the signature check.
2288 */
2289 if (l.parent_type != OBJ_PLAYER)
2290 return 0;
2291 /* As a special case, let the player be recognized even if he died
2292 * before the weapon hit the target.
2293 */
2294 if (o.type != OBJ_PLAYER && o.type != OBJ_GHOST)
2295 return 0;
2296 auto &parent_object = *vcobjptr(l.parent_num);
2297 return (&parent_object == &o);
2298 }
2299
2300 unsigned laser_parent_is_object(fvcobjptr &vcobjptr, const laser_parent &l, const object_base &o)
2301 {
2302 auto &parent_object = *vcobjptr(l.parent_num);
2303 if (&parent_object != &o)
2304 return 0;
2305 return laser_parent_is_matching_signature(l, o);
2306 }
2307
2308 unsigned laser_parent_is_object(const laser_parent &l, const vcobjptridx_t o)
2309 {
2310 if (l.parent_num != o.get_unchecked_index())
2311 return 0;
2312 return laser_parent_is_matching_signature(l, *o);
2313 }
2314
2315 unsigned laser_parent_object_exists(fvcobjptr &vcobjptr, const laser_parent &l)
2316 {
2317 return laser_parent_is_matching_signature(l, *vcobjptr(l.parent_num));
2318 }
2319
2320 void set_powerup_id(const d_powerup_info_array &Powerup_info, const d_vclip_array &Vclip, object_base &o, powerup_type_t id)
2321 {
2322 o.id = underlying_value(id);
2323 o.size = Powerup_info[id].size;
2324 const auto vclip_num = Powerup_info[id].vclip_num;
2325 o.rtype.vclip_info.vclip_num = vclip_num;
2326 o.rtype.vclip_info.frametime = Vclip[vclip_num].frame_time;
2327 }
2328
2329 //go through all objects and make sure they have the correct segment numbers
2330 void fix_object_segs()
2331 {
2332 auto &LevelSharedVertexState = LevelSharedSegmentState.get_vertex_state();
2333 auto &Objects = LevelUniqueObjectState.Objects;
2334 auto &Vertices = LevelSharedVertexState.get_vertices();
2335 auto &vmobjptr = Objects.vmptr;
2336 auto &vmobjptridx = Objects.vmptridx;
2337 auto &vcvertptr = Vertices.vcptr;
2338 range_for (const auto &&o, vmobjptridx)
2339 {
2340 if (o->type != OBJ_NONE)
2341 {
2342 const auto oldsegnum = o->segnum;
2343 if (update_object_seg(vmobjptr, LevelSharedSegmentState, LevelUniqueSegmentState, o) == 0)
2344 {
2345 const auto pos = o->pos;
2346 const auto segnum = o->segnum;
2347 o->pos = compute_segment_center(vcvertptr, vcsegptr(segnum));
2348 con_printf(CON_URGENT, "Object %hu claims segment %hu, but has position {%i,%i,%i}; moving to %hu:{%i,%i,%i}", o.get_unchecked_index(), oldsegnum, pos.x, pos.y, pos.z, segnum, o->pos.x, o->pos.y, o->pos.z);
2349 }
2350 }
2351 }
2352 }
2353
2354
2355 //--unused-- void object_use_new_object_list( object * new_list )
2356 //--unused-- {
2357 //--unused-- int i, segnum;
2358 //--unused-- object *obj;
2359 //--unused--
2360 //--unused-- // First, unlink all the old objects for the segments array
2361 //--unused-- for (segnum=0; segnum <= Highest_segment_index; segnum++) {
2362 //--unused-- Segments[segnum].objects = -1;
2363 //--unused-- }
2364 //--unused-- // Then, erase all the objects
2365 //--unused-- reset_objects(1);
2366 //--unused--
2367 //--unused-- // Fill in the object array
2368 //--unused-- memcpy( Objects, new_list, sizeof(object)*MAX_OBJECTS );
2369 //--unused--
2370 //--unused-- Highest_object_index=-1;
2371 //--unused--
2372 //--unused-- // Relink 'em
2373 //--unused-- for (i=0; i<MAX_OBJECTS; i++ ) {
2374 //--unused-- obj = &Objects[i];
2375 //--unused-- if ( obj->type != OBJ_NONE ) {
2376 //--unused-- num_objects++;
2377 //--unused-- Highest_object_index = i;
2378 //--unused-- segnum = obj->segnum;
2379 //--unused-- obj->next = obj->prev = obj->segnum = -1;
2380 //--unused-- obj_link(i,segnum);
2381 //--unused-- } else {
2382 //--unused-- obj->next = obj->prev = obj->segnum = -1;
2383 //--unused-- }
2384 //--unused-- }
2385 //--unused--
2386 //--unused-- }
2387
2388 namespace {
2389
2390 #if DXX_BUILD_DESCENT == 1
2391 #define object_is_clearable_weapon(W,a,b) object_is_clearable_weapon(a,b)
2392 #endif
2393 static unsigned object_is_clearable_weapon(const weapon_info_array &Weapon_info, const object_base obj, const unsigned clear_all)
2394 {
2395 if (!(obj.type == OBJ_WEAPON))
2396 return 0;
2397 const auto weapon_id = get_weapon_id(obj);
2398 #if DXX_BUILD_DESCENT == 2
2399 if (Weapon_info[weapon_id].flags & WIF_PLACABLE)
2400 return 0;
2401 #endif
2402 if (clear_all)
2403 return clear_all;
2404 return !is_proximity_bomb_or_player_smart_mine(weapon_id);
2405 }
2406
2407 }
2408
2409 //delete objects, such as weapons & explosions, that shouldn't stay between levels
2410 // Changed by MK on 10/15/94, don't remove proximity bombs.
2411 //if clear_all is set, clear even proximity bombs
2412 void clear_transient_objects(int clear_all)
2413 {
2414 auto &Objects = LevelUniqueObjectState.Objects;
2415 auto &vmobjptridx = Objects.vmptridx;
2416 range_for (const auto &&obj, vmobjptridx)
2417 {
2418 if (object_is_clearable_weapon(Weapon_info, obj, clear_all) ||
2419 obj->type == OBJ_FIREBALL ||
2420 obj->type == OBJ_DEBRIS ||
2421 (obj->type!=OBJ_NONE && obj->flags & OF_EXPLODING)) {
2422 obj_delete(LevelUniqueObjectState, Segments, obj);
2423 }
2424 }
2425 }
2426
2427 //attaches an object, such as a fireball, to another object, such as a robot
2428 void obj_attach(object_array &Objects, const vmobjptridx_t parent, const vmobjptridx_t sub)
2429 {
2430 Assert(sub->type == OBJ_FIREBALL);
2431 assert(sub->control_source == object::control_type::explosion);
2432
2433 Assert(sub->ctype.expl_info.next_attach==object_none);
2434 Assert(sub->ctype.expl_info.prev_attach==object_none);
2435
2436 assert(parent->attached_obj == object_none || Objects.vcptr(parent->attached_obj)->ctype.expl_info.prev_attach == object_none);
2437
2438 sub->ctype.expl_info.next_attach = parent->attached_obj;
2439
2440 if (sub->ctype.expl_info.next_attach != object_none)
2441 Objects.vmptr(sub->ctype.expl_info.next_attach)->ctype.expl_info.prev_attach = sub;
2442
2443 parent->attached_obj = sub;
2444
2445 sub->ctype.expl_info.attach_parent = parent;
2446 sub->flags |= OF_ATTACHED;
2447
2448 Assert(sub->ctype.expl_info.next_attach != sub);
2449 Assert(sub->ctype.expl_info.prev_attach != sub);
2450 }
2451
2452 namespace {
2453
2454 //detaches one object
2455 static objnum_t obj_detach_one(object_array &Objects, object &sub)
2456 {
2457 Assert(sub.flags & OF_ATTACHED);
2458 Assert(sub.ctype.expl_info.attach_parent != object_none);
2459
2460 /* Check for the impossible case that the parent does not exist, or does
2461 * not have any attached children.
2462 */
2463 if (const auto &parent_objp = *Objects.vcptr(sub.ctype.expl_info.attach_parent); parent_objp.type == OBJ_NONE || parent_objp.attached_obj == object_none)
2464 {
2465 sub.flags &= ~OF_ATTACHED;
2466 return object_none;
2467 }
2468
2469 /* If this is not the last sub-object in the list */
2470 if (sub.ctype.expl_info.next_attach != object_none)
2471 {
2472 /* Find the prev pointer of the next object */
2473 auto &a = Objects.vmptr(sub.ctype.expl_info.next_attach)->ctype.expl_info.prev_attach;
2474 /* Check that sub->next->prev is `sub` */
2475 assert(Objects.vcptr(a) == &sub);
2476 /* Change the next object's prev pointer to point to `sub`'s
2477 * predecessor. This is one half of the work required to remove `sub`
2478 * from the chain of sub-objects. The other half is handled below.
2479 */
2480 a = sub.ctype.expl_info.prev_attach;
2481 }
2482
2483 /* If `sub` is the first sub-object in the chain, then `use_prev_attach` is
2484 * false and the update should target the parent object.
2485 *
2486 * If `sub` is not the first sub-object in the chain, then
2487 * `use_prev_attach` is true and the update should target the previous
2488 * object in the chain.
2489 */
2490 const auto use_prev_attach = (sub.ctype.expl_info.prev_attach != object_none);
2491 /* If `use_prev_attach` is true, set `o` to the previous object and change
2492 * `sub->prev` to object_none so that a future call will set
2493 * `use_prev_attach` to false.
2494 *
2495 * If `use_prev_attach` is false, set `o` to the parent object.
2496 */
2497 auto &o = *Objects.vmptr(use_prev_attach ? std::exchange(sub.ctype.expl_info.prev_attach, object_none) : sub.ctype.expl_info.attach_parent);
2498 /* If `use_prev_attach` is true, then update the `next` reference in the
2499 * previous object. This is the second half of removing `sub` from a chain
2500 * of objects.
2501 *
2502 * If `use_prev_attach` is false, update `attached_obj` on the parent
2503 * object, so that the parent has no attached objects. This assumes that
2504 * `use_prev_attach` is false if and only if `sub` is the first child of
2505 * the parent object. If `sub` is not the first child, then `sub` should
2506 * have a back pointer (and thus `use_prev_attach` would be true) referring
2507 * to a previous child, which may or may not be the first child.
2508 */
2509 auto &update_attach = use_prev_attach ? o.ctype.expl_info.next_attach : o.attached_obj;
2510 /* Assert that the link that will be overwritten points to `sub`. If the
2511 * link is from parent to child, then failure means that sub was not the
2512 * first child of the parent. If the link is from previous to next, then
2513 * failure means that sub pointed back to an element that did not point
2514 * forward to sub.
2515 */
2516 assert(Objects.vcptr(update_attach) == &sub);
2517 sub.flags &= ~OF_ATTACHED;
2518 const auto next_attach = std::exchange(sub.ctype.expl_info.next_attach, object_none);
2519 update_attach = next_attach;
2520 return next_attach;
2521 }
2522
2523 //detaches all objects from this object
2524 static void obj_detach_all(object_array &Objects, object_base &parent)
2525 {
2526 for (auto attached_obj = parent.attached_obj; attached_obj != object_none;)
2527 attached_obj = obj_detach_one(Objects, Objects.vmptr(attached_obj));
2528 }
2529
2530 }
2531
2532 #if DXX_BUILD_DESCENT == 2
2533 //creates a marker object in the world. returns the object number
2534 imobjptridx_t drop_marker_object(const vms_vector &pos, const vmsegptridx_t segnum, const vms_matrix &orient, const game_marker_index marker_num)
2535 {
2536 auto &Polygon_models = LevelSharedPolygonModelState.Polygon_models;
2537 const auto Marker_model_num = LevelSharedPolygonModelState.Marker_model_num;
2538 if (Marker_model_num == polygon_model_index::None)
2539 {
2540 con_printf(CON_URGENT, "%s:%u: failed to drop marker object: invalid model number %u", __FILE__, __LINE__, underlying_value(Marker_model_num));
2541 return object_none;
2542 }
2543 const auto movement_type =
2544 ((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_COOP) && Netgame.Allow_marker_view)
2545 ? object::movement_type::None
2546 : object::movement_type::spinning;
2547 const auto &&obj = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_MARKER, underlying_value(marker_num), segnum, pos, &orient, Polygon_models[Marker_model_num].rad, object::control_type::None, movement_type, render_type::RT_POLYOBJ);
2548 if (obj != object_none) {
2549 auto &o = *obj;
2550 o.rtype.pobj_info.model_num = Marker_model_num;
2551
2552 if (movement_type == object::movement_type::spinning)
2553 {
2554 constexpr fix scale = F1_0 / 2;
2555 const auto oi = obj.get_unchecked_index();
2556 auto &spin_vec = o.mtype.spin_rate;
2557 spin_vec = {};
2558 if (oi & 1)
2559 vm_vec_scale_add2(spin_vec, o.orient.fvec, (oi & 8) ? scale : -scale);
2560 if (oi & 2)
2561 vm_vec_scale_add2(spin_vec, o.orient.uvec, (oi & 16) ? scale : -scale);
2562 if (oi & 4)
2563 vm_vec_scale_add2(spin_vec, o.orient.rvec, (oi & 32) ? scale : -scale);
2564 }
2565
2566 // MK, 10/16/95: Using lifeleft to make it flash, thus able to trim lightlevel from all objects.
2567 o.lifeleft = IMMORTAL_TIME - 1;
2568 }
2569 return obj;
2570 }
2571
2572 // *viewer is a viewer, probably a missile.
2573 // wake up all robots that were rendered last frame subject to some constraints.
2574 void wake_up_rendered_objects(const object &viewer, window_rendered_data &window)
2575 {
2576 auto &Objects = LevelUniqueObjectState.Objects;
2577 auto &vmobjptr = Objects.vmptr;
2578 // Make sure that we are processing current data.
2579 if (timer_query() != window.time) {
2580 return;
2581 }
2582
2583 Ai_last_missile_camera = &viewer;
2584
2585 range_for (const auto objnum, window.rendered_robots)
2586 {
2587 const auto &&objp = vmobjptr(objnum);
2588 if (objp->type == OBJ_ROBOT) {
2589 if (vm_vec_dist_quick(viewer.pos, objp->pos) < F1_0*100)
2590 {
2591 ai_local *ailp = &objp->ctype.ai_info.ail;
2592 {
2593 objp->ctype.ai_info.SUB_FLAGS |= SUB_FLAGS_CAMERA_AWAKE;
2594 ailp->player_awareness_type = player_awareness_type_t::PA_WEAPON_ROBOT_COLLISION;
2595 ailp->player_awareness_time = F1_0*3;
2596 ailp->previous_visibility = player_visibility_state::visible_and_in_field_of_view;
2597 }
2598 }
2599 }
2600 }
2601 }
2602 #endif
2603
2604 // Swap endianess of given object_rw if swap == 1
2605 void object_rw_swap(object_rw *obj, const physfsx_endian swap)
2606 {
2607 if (swap == physfsx_endian::native)
2608 return;
2609
2610 obj->signature = SWAPINT(obj->signature);
2611 obj->next = SWAPSHORT(obj->next);
2612 obj->prev = SWAPSHORT(obj->prev);
2613 obj->segnum = SWAPSHORT(obj->segnum);
2614 obj->attached_obj = SWAPSHORT(obj->attached_obj);
2615 obj->pos.x = SWAPINT(obj->pos.x);
2616 obj->pos.y = SWAPINT(obj->pos.y);
2617 obj->pos.z = SWAPINT(obj->pos.z);
2618 obj->orient.rvec.x = SWAPINT(obj->orient.rvec.x);
2619 obj->orient.rvec.y = SWAPINT(obj->orient.rvec.y);
2620 obj->orient.rvec.z = SWAPINT(obj->orient.rvec.z);
2621 obj->orient.fvec.x = SWAPINT(obj->orient.fvec.x);
2622 obj->orient.fvec.y = SWAPINT(obj->orient.fvec.y);
2623 obj->orient.fvec.z = SWAPINT(obj->orient.fvec.z);
2624 obj->orient.uvec.x = SWAPINT(obj->orient.uvec.x);
2625 obj->orient.uvec.y = SWAPINT(obj->orient.uvec.y);
2626 obj->orient.uvec.z = SWAPINT(obj->orient.uvec.z);
2627 obj->size = SWAPINT(obj->size);
2628 obj->shields = SWAPINT(obj->shields);
2629 obj->last_pos.x = SWAPINT(obj->last_pos.x);
2630 obj->last_pos.y = SWAPINT(obj->last_pos.y);
2631 obj->last_pos.z = SWAPINT(obj->last_pos.z);
2632 obj->lifeleft = SWAPINT(obj->lifeleft);
2633
2634 switch (typename object::movement_type{obj->movement_source})
2635 {
2636 case object::movement_type::None:
2637 obj->mtype = {};
2638 break;
2639 case object::movement_type::physics:
2640 obj->mtype.phys_info.velocity.x = SWAPINT(obj->mtype.phys_info.velocity.x);
2641 obj->mtype.phys_info.velocity.y = SWAPINT(obj->mtype.phys_info.velocity.y);
2642 obj->mtype.phys_info.velocity.z = SWAPINT(obj->mtype.phys_info.velocity.z);
2643 obj->mtype.phys_info.thrust.x = SWAPINT(obj->mtype.phys_info.thrust.x);
2644 obj->mtype.phys_info.thrust.y = SWAPINT(obj->mtype.phys_info.thrust.y);
2645 obj->mtype.phys_info.thrust.z = SWAPINT(obj->mtype.phys_info.thrust.z);
2646 obj->mtype.phys_info.mass = SWAPINT(obj->mtype.phys_info.mass);
2647 obj->mtype.phys_info.drag = SWAPINT(obj->mtype.phys_info.drag);
2648 obj->mtype.phys_info.rotvel.x = SWAPINT(obj->mtype.phys_info.rotvel.x);
2649 obj->mtype.phys_info.rotvel.y = SWAPINT(obj->mtype.phys_info.rotvel.y);
2650 obj->mtype.phys_info.rotvel.z = SWAPINT(obj->mtype.phys_info.rotvel.z);
2651 obj->mtype.phys_info.rotthrust.x = SWAPINT(obj->mtype.phys_info.rotthrust.x);
2652 obj->mtype.phys_info.rotthrust.y = SWAPINT(obj->mtype.phys_info.rotthrust.y);
2653 obj->mtype.phys_info.rotthrust.z = SWAPINT(obj->mtype.phys_info.rotthrust.z);
2654 obj->mtype.phys_info.turnroll = SWAPINT(obj->mtype.phys_info.turnroll);
2655 obj->mtype.phys_info.flags = SWAPSHORT(obj->mtype.phys_info.flags);
2656 break;
2657
2658 case object::movement_type::spinning:
2659 obj->mtype.spin_rate.x = SWAPINT(obj->mtype.spin_rate.x);
2660 obj->mtype.spin_rate.y = SWAPINT(obj->mtype.spin_rate.y);
2661 obj->mtype.spin_rate.z = SWAPINT(obj->mtype.spin_rate.z);
2662 break;
2663 }
2664
2665 switch (typename object::control_type{obj->control_source})
2666 {
2667 case object::control_type::weapon:
2668 obj->ctype.laser_info.parent_type = SWAPSHORT(obj->ctype.laser_info.parent_type);
2669 obj->ctype.laser_info.parent_num = SWAPSHORT(obj->ctype.laser_info.parent_num);
2670 obj->ctype.laser_info.parent_signature = SWAPINT(obj->ctype.laser_info.parent_signature);
2671 obj->ctype.laser_info.creation_time = SWAPINT(obj->ctype.laser_info.creation_time);
2672 obj->ctype.laser_info.last_hitobj = SWAPSHORT(obj->ctype.laser_info.last_hitobj);
2673 obj->ctype.laser_info.track_goal = SWAPSHORT(obj->ctype.laser_info.track_goal);
2674 obj->ctype.laser_info.multiplier = SWAPINT(obj->ctype.laser_info.multiplier);
2675 break;
2676
2677 case object::control_type::explosion:
2678 obj->ctype.expl_info.spawn_time = SWAPINT(obj->ctype.expl_info.spawn_time);
2679 obj->ctype.expl_info.delete_time = SWAPINT(obj->ctype.expl_info.delete_time);
2680 obj->ctype.expl_info.delete_objnum = SWAPSHORT(obj->ctype.expl_info.delete_objnum);
2681 obj->ctype.expl_info.attach_parent = SWAPSHORT(obj->ctype.expl_info.attach_parent);
2682 obj->ctype.expl_info.prev_attach = SWAPSHORT(obj->ctype.expl_info.prev_attach);
2683 obj->ctype.expl_info.next_attach = SWAPSHORT(obj->ctype.expl_info.next_attach);
2684 break;
2685
2686 case object::control_type::ai:
2687 obj->ctype.ai_info.hide_segment = SWAPSHORT(obj->ctype.ai_info.hide_segment);
2688 obj->ctype.ai_info.hide_index = SWAPSHORT(obj->ctype.ai_info.hide_index);
2689 obj->ctype.ai_info.path_length = SWAPSHORT(obj->ctype.ai_info.path_length);
2690 #if DXX_BUILD_DESCENT == 1
2691 obj->ctype.ai_info.cur_path_index = SWAPSHORT(obj->ctype.ai_info.cur_path_index);
2692 #elif DXX_BUILD_DESCENT == 2
2693 obj->ctype.ai_info.dying_start_time = SWAPINT(obj->ctype.ai_info.dying_start_time);
2694 #endif
2695 obj->ctype.ai_info.danger_laser_num = SWAPSHORT(obj->ctype.ai_info.danger_laser_num);
2696 obj->ctype.ai_info.danger_laser_signature = SWAPINT(obj->ctype.ai_info.danger_laser_signature);
2697 break;
2698
2699 case object::control_type::light:
2700 obj->ctype.light_info.intensity = SWAPINT(obj->ctype.light_info.intensity);
2701 break;
2702
2703 case object::control_type::powerup:
2704 obj->ctype.powerup_info.count = SWAPINT(obj->ctype.powerup_info.count);
2705 #if DXX_BUILD_DESCENT == 2
2706 obj->ctype.powerup_info.creation_time = SWAPINT(obj->ctype.powerup_info.creation_time);
2707 obj->ctype.powerup_info.flags = SWAPINT(obj->ctype.powerup_info.flags);
2708 #endif
2709 break;
2710 case object::control_type::None:
2711 case object::control_type::flying:
2712 case object::control_type::slew:
2713 case object::control_type::flythrough:
2714 case object::control_type::repaircen:
2715 case object::control_type::morph:
2716 case object::control_type::debris:
2717 case object::control_type::remote:
2718 default:
2719 break;
2720 }
2721
2722 switch (render_type{obj->render_type})
2723 {
2724 case render_type::RT_NONE:
2725 if (obj->type != OBJ_GHOST) // HACK: when a player is dead or not connected yet, clients still expect to get polyobj data - even if render_type == RT_NONE at this time.
2726 break;
2727 [[fallthrough]];
2728 case render_type::RT_MORPH:
2729 case render_type::RT_POLYOBJ:
2730 {
2731 obj->rtype.pobj_info.model_num = SWAPINT(obj->rtype.pobj_info.model_num);
2732 for (uint_fast32_t i=0;i<MAX_SUBMODELS;i++)
2733 {
2734 obj->rtype.pobj_info.anim_angles[i].p = SWAPINT(obj->rtype.pobj_info.anim_angles[i].p);
2735 obj->rtype.pobj_info.anim_angles[i].b = SWAPINT(obj->rtype.pobj_info.anim_angles[i].b);
2736 obj->rtype.pobj_info.anim_angles[i].h = SWAPINT(obj->rtype.pobj_info.anim_angles[i].h);
2737 }
2738 obj->rtype.pobj_info.subobj_flags = SWAPINT(obj->rtype.pobj_info.subobj_flags);
2739 obj->rtype.pobj_info.tmap_override = SWAPINT(obj->rtype.pobj_info.tmap_override);
2740 obj->rtype.pobj_info.alt_textures = SWAPINT(obj->rtype.pobj_info.alt_textures);
2741 break;
2742 }
2743
2744 case render_type::RT_WEAPON_VCLIP:
2745 case render_type::RT_HOSTAGE:
2746 case render_type::RT_POWERUP:
2747 case render_type::RT_FIREBALL:
2748 obj->rtype.vclip_info.vclip_num = SWAPINT(obj->rtype.vclip_info.vclip_num);
2749 obj->rtype.vclip_info.frametime = SWAPINT(obj->rtype.vclip_info.frametime);
2750 break;
2751
2752 case render_type::RT_LASER:
2753 break;
2754
2755 }
2756 }
2757
2758 }
2759
2760 namespace dcx {
2761
2762 void (check_warn_object_type)(const object_base &o, object_type_t t, const char *file, unsigned line)
2763 {
2764 if (o.type != t)
2765 con_printf(CON_URGENT, "%s:%u: BUG: object %p has type %u, expected %u", file, line, &o, o.type, t);
2766 }
2767
2768 }