Debian Codegraph @

similar/main/cntrlcen.cpp

raw
1 /*
2 * Portions of this file are copyright Rebirth contributors and licensed as
3 * described in COPYING.txt.
4 * Portions of this file are copyright Parallax Software and licensed
5 * according to the Parallax license below.
6 * See COPYING.txt for license details.
7
8 THE COMPUTER CODE CONTAINED HEREIN IS THE SOLE PROPERTY OF PARALLAX
9 SOFTWARE CORPORATION ("PARALLAX"). PARALLAX, IN DISTRIBUTING THE CODE TO
10 END-USERS, AND SUBJECT TO ALL OF THE TERMS AND CONDITIONS HEREIN, GRANTS A
11 ROYALTY-FREE, PERPETUAL LICENSE TO SUCH END-USERS FOR USE BY SUCH END-USERS
12 IN USING, DISPLAYING, AND CREATING DERIVATIVE WORKS THEREOF, SO LONG AS
13 SUCH USE, DISPLAY OR CREATION IS FOR NON-COMMERCIAL, ROYALTY OR REVENUE
14 FREE PURPOSES. IN NO EVENT SHALL THE END-USER USE THE COMPUTER CODE
15 CONTAINED HEREIN FOR REVENUE-BEARING PURPOSES. THE END-USER UNDERSTANDS
16 AND AGREES TO THE TERMS HEREIN AND ACCEPTS THE SAME BY USE OF THIS FILE.
17 COPYRIGHT 1993-1999 PARALLAX SOFTWARE CORPORATION. ALL RIGHTS RESERVED.
18 */
19
20 /*
21 *
22 * Code for the control center
23 *
24 */
25
26
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <ranges>
30 #if !defined(_WIN32) && !defined(macintosh)
31 #include <unistd.h>
32 #endif
33 #include "pstypes.h"
34 #include "dxxerror.h"
35 #include "inferno.h"
36 #include "cntrlcen.h"
37 #include "game.h"
38 #include "laser.h"
39 #include "gameseq.h"
40 #include "ai.h"
41 #include "player.h"
42 #include "mission.h"
43 #include "multi.h"
44 #include "fwd-wall.h"
45 #include "segment.h"
46 #include "object.h"
47 #include "robot.h"
48 #include "physfs-serial.h"
49 #include "endlevel.h"
50 #include "state.h"
51 #include "args.h"
52 #include "wall.h"
53
54 #include "compiler-range_for.h"
55 #include "d_array.h"
56 #include "d_construct.h"
57 #include "d_enumerate.h"
58 #include "d_levelstate.h"
59 #include "partial_range.h"
60 #include "d_zip.h"
61
62 namespace dcx {
63 namespace {
64
65 constexpr enumerated_array<uint8_t, NDL, Difficulty_level_type> D1_Alan_pavlish_reactor_times = {{{
66 50, 45, 40, 35, 30
67 }}};
68
69 }
70 }
71
72 namespace dsx {
73 std::array<reactor, MAX_REACTORS> Reactors;
74 #if DXX_BUILD_DESCENT == 2
75 unsigned Num_reactors;
76 #endif
77
78 namespace {
79
80 static window_event_result do_countdown_frame();
81
82 static inline const reactor &get_reactor_definition(const std::size_t id)
83 {
84 #if DXX_BUILD_DESCENT == 1
85 (void)id;
86 return Reactors[0];
87 #elif DXX_BUILD_DESCENT == 2
88 return Reactors[id];
89 #endif
90 }
91
92 }
93
94 void calc_controlcen_gun_point(object &obj)
95 {
96 assert(obj.type == OBJ_CNTRLCEN);
97 assert(obj.render_type == render_type::RT_POLYOBJ);
98 auto &reactor = get_reactor_definition(get_reactor_id(obj));
99 for (uint_fast32_t i = reactor.n_guns; i--;)
100 {
101 /* Set the position and direction of reactor gun number `gun_num` based
102 * on the definition of a reactor, the current position of an object
103 * that is of type OBJ_CNTRLCEN, and the orientation of that object.
104 */
105 const auto &&m{vm_transposed_matrix(obj.orient)};
106 reconstruct_at(obj.ctype.reactor_info.gun_pos[i], vm_vec_build_add, obj.pos, vm_vec_build_rotated(reactor.gun_points[i], m));
107 reconstruct_at(obj.ctype.reactor_info.gun_dir[i], vm_vec_build_rotated, reactor.gun_dirs[i], m);
108 }
109 }
110
111 namespace {
112
113 #if DXX_BUILD_DESCENT == 2
114 constexpr enumerated_array<uint8_t, NDL, Difficulty_level_type> D2_Alan_pavlish_reactor_times = {{{
115 90, 60, 45, 35, 30
116 }}};
117 #endif
118
119 // -----------------------------------------------------------------------------
120 // Look at control center guns, find best one to fire at the position `objpos`.
121 // Return best gun number (one whose direction dotted with vector to player is largest).
122 // If best gun has negative dot, return an empty std::optional, meaning no gun is good.
123 static std::optional<std::size_t> calc_best_gun(const std::size_t num_guns, const object &objreactor, const vms_vector &objpos)
124 {
125 constexpr std::size_t invalid_best_gun{SIZE_MAX};
126 fix best_dot{-F1_0*2};
127 std::size_t best_gun{invalid_best_gun};
128 for (const auto &&[i, gun_dir, gun_pos] : enumerate(zip(std::span(objreactor.ctype.reactor_info.gun_dir).first(num_guns), objreactor.ctype.reactor_info.gun_pos)))
129 {
130 const auto gun_vec{vm_vec_normalized_quick(vm_vec_build_sub(objpos, gun_pos))};
131 const fix dot{vm_vec_build_dot(gun_dir, gun_vec)};
132 if (best_dot < dot)
133 {
134 best_dot = dot;
135 best_gun = i;
136 }
137 }
138
139 assert(best_gun != invalid_best_gun); // Contact Mike. This is impossible. Or maybe you're getting an unnormalized vector somewhere.
140
141 if (best_dot < 0)
142 {
143 [[unlikely]];
144 return std::nullopt;
145 }
146 else
147 /* On this path, `best_gun` must be a valid index into `gun_dir`. If
148 * the loop never found a good `dot`, then `best_dot` is unchanged from
149 * its initial value, and the `if` branch will be taken. If the loop
150 * found any good `dot`, then `best_gun` is set to the valid index
151 * which was declared good.
152 */
153 return best_gun;
154 }
155
156 }
157
158 }
159
160 namespace dcx {
161 control_center_triggers ControlCenterTriggers;
162 }
163 namespace dsx {
164
165 // -----------------------------------------------------------------------------
166 // Called every frame. If control center been destroyed, then actually do something.
167 window_event_result do_controlcen_dead_frame()
168 {
169 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
170 auto &Objects = LevelUniqueObjectState.Objects;
171 auto &vmobjptridx = Objects.vmptridx;
172 if ((Game_mode & GM_MULTI) && (get_local_player().connected != player_connection_status::playing)) // if out of level already there's no need for this
173 return window_event_result::ignored;
174
175 const auto Dead_controlcen_object_num = LevelUniqueControlCenterState.Dead_controlcen_object_num;
176 if (Dead_controlcen_object_num != object_none && LevelUniqueControlCenterState.Countdown_seconds_left > 0)
177 if (d_rand() < FrameTime*4)
178 #if DXX_BUILD_DESCENT == 1
179 #define CC_FIREBALL_SCALE F1_0*3
180 #elif DXX_BUILD_DESCENT == 2
181 #define CC_FIREBALL_SCALE F1_0
182 #endif
183 create_small_fireball_on_object(vmobjptridx(Dead_controlcen_object_num), CC_FIREBALL_SCALE, 1);
184
185 if (LevelUniqueControlCenterState.Control_center_destroyed && !Endlevel_sequence)
186 return do_countdown_frame();
187
188 return window_event_result::ignored;
189 }
190
191 #define COUNTDOWN_VOICE_TIME fl2f(12.75)
192
193 namespace {
194
195 window_event_result do_countdown_frame()
196 {
197 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
198 fix old_time;
199
200 if (!LevelUniqueControlCenterState.Control_center_destroyed)
201 return window_event_result::ignored;
202
203 #if DXX_BUILD_DESCENT == 2
204 if (!is_D2_OEM && !is_MAC_SHARE && !is_SHAREWARE) // get countdown in OEM and SHAREWARE only
205 {
206 // On last level, we don't want a countdown.
207 if (PLAYING_BUILTIN_MISSION && Current_level_num == Current_mission->last_level)
208 {
209 if (!(Game_mode & GM_MULTI))
210 return window_event_result::ignored;
211 if (Game_mode & GM_MULTI_ROBOTS)
212 return window_event_result::ignored;
213 }
214 }
215 #endif
216
217 // Control center destroyed, rock the player's ship.
218 if (d_tick_step)
219 {
220 auto &rotvel = ConsoleObject->mtype.phys_info.rotvel;
221 const auto get_base_disturbance = [fc = std::min(LevelUniqueControlCenterState.Countdown_seconds_left, 16)]() {
222 return fixmul(d_rand() - 16384, 3 * F1_0 / 16 + (F1_0 * (16 - fc)) / 32);
223 };
224 fix disturb_x = get_base_disturbance(), disturb_z = get_base_disturbance();
225 // At Trainee, decrease rocking of ship by 4x.
226 if (GameUniqueState.Difficulty_level == Difficulty_level_type::_0)
227 {
228 disturb_x /= 4;
229 disturb_z /= 4;
230 }
231 rotvel.x += disturb_x;
232 rotvel.z += disturb_z;
233 }
234 // Hook in the rumble sound effect here.
235
236 old_time = LevelUniqueControlCenterState.Countdown_timer;
237 LevelUniqueControlCenterState.Countdown_timer -= FrameTime;
238 const auto Countdown_timer = LevelUniqueControlCenterState.Countdown_timer;
239 const auto Countdown_seconds_left = LevelUniqueControlCenterState.Countdown_seconds_left = f2i(Countdown_timer + F1_0*7/8);
240
241 if (old_time > COUNTDOWN_VOICE_TIME && Countdown_timer <= COUNTDOWN_VOICE_TIME)
242 {
243 digi_play_sample( sound_effect::SOUND_COUNTDOWN_13_SECS, F3_0 );
244 }
245 if (f2i(old_time + F1_0 * 7 / 8) != Countdown_seconds_left)
246 {
247 if (Countdown_seconds_left >= 0 && Countdown_seconds_left < 10)
248 digi_play_sample(static_cast<sound_effect>(static_cast<unsigned>(sound_effect::SOUND_COUNTDOWN_0_SECS) + Countdown_seconds_left), F3_0);
249 if (Countdown_seconds_left == LevelUniqueControlCenterState.Total_countdown_time - 1)
250 digi_play_sample( sound_effect::SOUND_COUNTDOWN_29_SECS, F3_0 );
251 }
252
253 if (Countdown_timer > 0) {
254 fix size,old_size;
255 const auto Total_countdown_time = LevelUniqueControlCenterState.Total_countdown_time;
256 size = (i2f(Total_countdown_time) - Countdown_timer) / fl2f(0.65);
257 old_size = (i2f(Total_countdown_time) - old_time) / fl2f(0.65);
258 if (size != old_size && Countdown_seconds_left < Total_countdown_time - 5)
259 { // Every 2 seconds!
260 //@@if (Dead_controlcen_object_num != -1) {
261 //@@ vms_vector vp; //,v,c;
262 //@@ compute_segment_center(&vp, &Segments[Objects[Dead_controlcen_object_num].segnum]);
263 //@@ object_create_explosion( Objects[Dead_controlcen_object_num].segnum, &vp, size*10, VCLIP_SMALL_EXPLOSION);
264 //@@}
265
266 digi_play_sample( sound_effect::SOUND_CONTROL_CENTER_WARNING_SIREN, F3_0 );
267 }
268 } else {
269 int flash_value;
270
271 if (old_time > 0)
272 digi_play_sample( sound_effect::SOUND_MINE_BLEW_UP, F1_0 );
273
274 flash_value = f2i(-Countdown_timer * (64 / 4)); // 4 seconds to total whiteness
275 PALETTE_FLASH_SET(flash_value,flash_value,flash_value);
276
277 if (PaletteBlueAdd > 64 ) {
278 gr_set_default_canvas();
279 gr_clear_canvas(*grd_curcanv, BM_XRGB(31,31,31)); //make screen all white to match palette effect
280 reset_palette_add(); //restore palette for death message
281 //controlcen->MaxCapacity = Fuelcen_max_amount;
282 //gauge_message( "Control Center Reset" );
283 return DoPlayerDead(); //kill_player();
284 }
285 }
286
287 return window_event_result::handled;
288 }
289
290 }
291
292 // -----------------------------------------------------------------------------
293 // Called when control center gets destroyed.
294 // This code is common to whether control center is implicitly imbedded in a boss,
295 // or is an object of its own.
296 // if objp == NULL that means the boss was the control center and don't set Dead_controlcen_object_num
297 void do_controlcen_destroyed_stuff(const imobjidx_t objp)
298 {
299 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
300 int i;
301
302 #if DXX_BUILD_DESCENT == 2
303 if ((Game_mode & GM_MULTI_ROBOTS) && LevelUniqueControlCenterState.Control_center_destroyed)
304 return; // Don't allow resetting if control center and boss on same level
305 #endif
306
307 auto &Walls = LevelUniqueWallSubsystemState.Walls;
308 auto &vmwallptr = Walls.vmptr;
309 // Must toggle walls whether it is a boss or control center.
310 for (i=0;i<ControlCenterTriggers.num_links;i++)
311 wall_toggle(vmwallptr, vmsegptridx(ControlCenterTriggers.seg[i]), ControlCenterTriggers.side[i]);
312
313 // And start the countdown stuff.
314 LevelUniqueControlCenterState.Control_center_destroyed = 1;
315
316 const auto Difficulty_level = GameUniqueState.Difficulty_level;
317 int Total_countdown_time;
318 #if DXX_BUILD_DESCENT == 2
319 // If a secret level, delete secret.sgc to indicate that we can't return to our secret level.
320 if (Current_level_num < 0)
321 PHYSFS_delete(SECRETC_FILENAME);
322
323 const auto Base_control_center_explosion_time = LevelSharedControlCenterState.Base_control_center_explosion_time;
324 if (Base_control_center_explosion_time != DEFAULT_CONTROL_CENTER_EXPLOSION_TIME)
325 Total_countdown_time = Base_control_center_explosion_time + Base_control_center_explosion_time * (NDL - underlying_value(Difficulty_level) - 1) / 2;
326 else if (!EMULATING_D1)
327 Total_countdown_time = D2_Alan_pavlish_reactor_times[Difficulty_level];
328 else
329 #endif
330 Total_countdown_time = D1_Alan_pavlish_reactor_times[Difficulty_level];
331
332 LevelUniqueControlCenterState.Total_countdown_time = Total_countdown_time;
333 LevelUniqueControlCenterState.Countdown_timer = i2f(Total_countdown_time);
334
335 if (!LevelUniqueControlCenterState.Control_center_present || objp==object_none)
336 return;
337
338 LevelUniqueControlCenterState.Dead_controlcen_object_num = objp;
339 }
340
341 // -----------------------------------------------------------------------------
342 //do whatever this thing does in a frame
343 void do_controlcen_frame(const d_robot_info_array &Robot_info, const vmobjptridx_t obj)
344 {
345 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
346 auto &Objects = LevelUniqueObjectState.Objects;
347 auto &vmobjptr = Objects.vmptr;
348
349 // If a boss level, then Control_center_present will be 0.
350 if (!LevelUniqueControlCenterState.Control_center_present)
351 return;
352
353 #ifndef NDEBUG
354 if (cheats.robotfiringsuspended || (Game_suspended & SUSP_ROBOTS))
355 return;
356 #else
357 if (cheats.robotfiringsuspended)
358 return;
359 #endif
360
361 auto &plrobj = get_local_plrobj();
362 if (!(LevelUniqueControlCenterState.Control_center_been_hit || player_is_visible(LevelUniqueControlCenterState.Control_center_player_been_seen)))
363 {
364 if (!(d_tick_count % 8)) { // Do every so often...
365 // This is a hack. Since the control center is not processed by
366 // ai_do_frame, it doesn't know to deal with cloaked dudes. It
367 // seems to work in single-player mode because it is actually using
368 // the value of Believed_player_position that was set by the last
369 // person to go through ai_do_frame. But since a no-robots game
370 // never goes through ai_do_frame, I'm making it so the control
371 // center can spot cloaked dudes.
372
373 if (Game_mode & GM_MULTI)
374 Believed_player_pos = plrobj.pos;
375
376 // Hack for special control centers which are isolated and not reachable because the
377 // real control center is inside the boss.
378 const shared_segment &objseg = vcsegptr(obj->segnum);
379 auto &children = objseg.children;
380 if (std::none_of(children.begin(), children.end(), IS_CHILD))
381 return;
382
383 const auto &&[dist_to_player, vec_to_player] = vm_vec_normalize_quick_with_magnitude(vm_vec_build_sub(ConsoleObject->pos, obj->pos));
384 if (dist_to_player < F1_0*200) {
385 LevelUniqueControlCenterState.Control_center_player_been_seen = player_is_visible_from_object(Robot_info, obj, obj->pos, 0, vec_to_player);
386 LevelUniqueControlCenterState.Frametime_until_next_fire = 0;
387 }
388 }
389
390 return;
391 }
392
393 #if DXX_BUILD_DESCENT == 2
394 // Periodically, make the reactor fall asleep if player not visible.
395 if (!EMULATING_D1 && (LevelUniqueControlCenterState.Control_center_been_hit || player_is_visible(LevelUniqueControlCenterState.Control_center_player_been_seen)))
396 {
397 if (LevelUniqueControlCenterState.Last_time_cc_vis_check + F1_0 * 5 < GameTime64 || LevelUniqueControlCenterState.Last_time_cc_vis_check > GameTime64)
398 {
399 LevelUniqueControlCenterState.Last_time_cc_vis_check = {GameTime64};
400 const auto &&[dist_to_player, vec_to_player] = vm_vec_normalize_quick_with_magnitude(vm_vec_build_sub(ConsoleObject->pos, obj->pos));
401 if (dist_to_player < F1_0*120) {
402 LevelUniqueControlCenterState.Control_center_player_been_seen = player_is_visible_from_object(Robot_info, obj, obj->pos, 0, vec_to_player);
403 if (!player_is_visible(LevelUniqueControlCenterState.Control_center_player_been_seen))
404 LevelUniqueControlCenterState.Control_center_been_hit = 0;
405 }
406 }
407
408 }
409 #endif
410
411 constexpr fix Relative_frametime_cease_fire = F1_0 * 2;
412 auto &Frametime_since_player_died = LevelUniqueControlCenterState.Frametime_since_player_died;
413 if (Player_dead_state != player_dead_state::no)
414 {
415 if (Frametime_since_player_died <= Relative_frametime_cease_fire)
416 Frametime_since_player_died += FrameTime;
417 }
418 else
419 Frametime_since_player_died = 0;
420
421 if (LevelUniqueControlCenterState.Frametime_until_next_fire < 0 && !(Frametime_since_player_died > Relative_frametime_cease_fire))
422 {
423 auto &player_info = plrobj.ctype.player_info;
424 const auto &player_pos = (player_info.powerup_flags & PLAYER_FLAGS_CLOAKED) ? Believed_player_pos : ConsoleObject->pos;
425 if (const auto opt_best_gun{calc_best_gun(get_reactor_definition(get_reactor_id(obj)).n_guns, obj, player_pos)})
426 {
427 const auto best_gun_num{*opt_best_gun};
428 fix delta_fire_time;
429
430 auto &&[dist_to_player, vec_to_goal] = vm_vec_normalize_quick_with_magnitude(vm_vec_build_sub(player_pos, obj->ctype.reactor_info.gun_pos[best_gun_num]));
431
432 if (dist_to_player > F1_0*300)
433 {
434 LevelUniqueControlCenterState.Control_center_been_hit = 0;
435 LevelUniqueControlCenterState.Control_center_player_been_seen = player_visibility_state::no_line_of_sight;
436 return;
437 }
438
439 #if DXX_USE_MULTIPLAYER
440 if (Game_mode & GM_MULTI)
441 multi_send_controlcen_fire(vec_to_goal, best_gun_num, obj);
442 #endif
443 Laser_create_new_easy(Robot_info, vec_to_goal, obj->ctype.reactor_info.gun_pos[best_gun_num], obj, weapon_id_type::CONTROLCEN_WEAPON_NUM, weapon_sound_flag::audible);
444
445 int count{0};
446 #if DXX_BUILD_DESCENT == 1
447 const unsigned scale_divisor = 4;
448 if (d_rand() < 32767/4)
449 #elif DXX_BUILD_DESCENT == 2
450 const unsigned scale_divisor = 6;
451 int rand_prob;
452 // some of time, based on level, fire another thing, not directly at player, so it might hit him if he's constantly moving.
453 rand_prob = F1_0/(abs(Current_level_num)/4+2);
454 while ((d_rand() > rand_prob) && (count < 4))
455 #endif
456 {
457 vm_vec_scale_add2(vec_to_goal, make_random_vector(), F1_0/scale_divisor);
458 vm_vec_normalize_quick(vec_to_goal);
459 #if DXX_USE_MULTIPLAYER
460 if (Game_mode & GM_MULTI)
461 multi_send_controlcen_fire(vec_to_goal, best_gun_num, obj);
462 #endif
463 Laser_create_new_easy(Robot_info, vec_to_goal, obj->ctype.reactor_info.gun_pos[best_gun_num], obj, weapon_id_type::CONTROLCEN_WEAPON_NUM, count == 0 ? weapon_sound_flag::audible : weapon_sound_flag::silent);
464 count++;
465 }
466
467 const auto Difficulty_level = GameUniqueState.Difficulty_level;
468 delta_fire_time = (NDL - underlying_value(Difficulty_level)) * F1_0/4;
469 #if DXX_BUILD_DESCENT == 2
470 if (Difficulty_level == Difficulty_level_type::_0)
471 delta_fire_time += F1_0/2;
472 #endif
473
474 if (Game_mode & GM_MULTI) // slow down rate of fire in multi player
475 delta_fire_time *= 2;
476
477 LevelUniqueControlCenterState.Frametime_until_next_fire = delta_fire_time;
478
479 }
480 } else
481 LevelUniqueControlCenterState.Frametime_until_next_fire -= FrameTime;
482 }
483
484 // -----------------------------------------------------------------------------
485 // This must be called at the start of each level.
486 // If this level contains a boss and mode != multiplayer, don't do control center stuff. (Ghost out control center object.)
487 // If this level contains a boss and mode == multiplayer, do control center stuff.
488 void init_controlcen_for_level(const d_robot_info_array &Robot_info)
489 {
490 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
491 object *cntrlcen_objp{nullptr};
492 const object *boss_objp{nullptr};
493
494 auto &Objects = LevelUniqueObjectState.Objects;
495 for (auto &obj : Objects.vmptr)
496 {
497 if (obj.type == OBJ_CNTRLCEN)
498 {
499 if (cntrlcen_objp == nullptr)
500 cntrlcen_objp = &obj;
501 }
502 else if (obj.type == OBJ_ROBOT && Robot_info[get_robot_id(obj)].boss_flag != boss_robot_id::None)
503 {
504 if (boss_objp == nullptr)
505 boss_objp = &obj;
506 }
507 }
508
509 if (boss_objp != nullptr && !((Game_mode & GM_MULTI) && !(Game_mode & GM_MULTI_ROBOTS)))
510 {
511 /* If boss is present ... */
512 if (cntrlcen_objp != nullptr)
513 {
514 /* If a control center is also present, then remove the control
515 * center.
516 *
517 * Stock levels with a boss also define a control center. In
518 * anarchy games, the boss is removed and the control center kept,
519 * so that players can destroy the control center to advance to the
520 * next level. In anarchy-with-robots games and non-anarchy games,
521 * the boss is kept, and this block removes the control center, so
522 * that players must destroy the boss to advance to the next level.
523 */
524 cntrlcen_objp->type = OBJ_GHOST;
525 cntrlcen_objp->control_source = object::control_type::None;
526 cntrlcen_objp->render_type = render_type::RT_NONE;
527 LevelUniqueControlCenterState.Control_center_present = 0;
528 }
529 }
530 else if (cntrlcen_objp != nullptr)
531 {
532 // Compute all gun positions.
533 calc_controlcen_gun_point(*cntrlcen_objp);
534 LevelUniqueControlCenterState.Control_center_present = 1;
535
536 #if DXX_BUILD_DESCENT == 1
537 const unsigned secret_level_shield_multiplier = 100;
538 #elif DXX_BUILD_DESCENT == 2
539 const unsigned secret_level_shield_multiplier = 150;
540 const auto Reactor_strength = LevelSharedControlCenterState.Reactor_strength;
541 if (Reactor_strength != -1)
542 cntrlcen_objp->shields = i2f(Reactor_strength);
543 else
544 #endif
545 { //use old defaults
546 // Boost control center strength at higher levels.
547 if (Current_level_num >= 0)
548 cntrlcen_objp->shields = F1_0*200 + (F1_0*200/4) * Current_level_num;
549 else
550 cntrlcen_objp->shields = F1_0*200 - Current_level_num*F1_0*secret_level_shield_multiplier;
551 }
552 }
553
554 // Say the control center has not yet been hit.
555 LevelUniqueControlCenterState.Control_center_been_hit = 0;
556 LevelUniqueControlCenterState.Control_center_player_been_seen = player_visibility_state::no_line_of_sight;
557 LevelUniqueControlCenterState.Frametime_until_next_fire = 0;
558 LevelUniqueControlCenterState.Dead_controlcen_object_num = object_none;
559 }
560
561 #if DXX_BUILD_DESCENT == 2
562 void special_reactor_stuff()
563 {
564 auto &LevelUniqueControlCenterState = LevelUniqueObjectState.ControlCenterState;
565 if (LevelUniqueControlCenterState.Control_center_destroyed) {
566 const auto Base_control_center_explosion_time = LevelSharedControlCenterState.Base_control_center_explosion_time;
567 LevelUniqueControlCenterState.Countdown_timer += i2f(Base_control_center_explosion_time + (NDL - 1 - underlying_value(GameUniqueState.Difficulty_level)) * Base_control_center_explosion_time / (NDL - 1));
568 LevelUniqueControlCenterState.Total_countdown_time = f2i(LevelUniqueControlCenterState.Countdown_timer) + 2; // Will prevent "Self destruct sequence activated" message from replaying.
569 }
570 }
571
572 /*
573 * reads n reactor structs from a PHYSFS_File
574 */
575 void reactor_read_n(const NamedPHYSFS_File fp, std::ranges::subrange<reactor *> r)
576 {
577 range_for (auto &i, r)
578 {
579 i.model_num = build_polygon_model_index_from_untrusted(PHYSFSX_readInt(fp));
580 const std::size_t n_guns{PHYSFSX_readULE32(fp)};
581 i.n_guns = std::min(n_guns, MAX_CONTROLCEN_GUNS);
582 range_for (auto &j, i.gun_points)
583 PHYSFSX_readVector(fp, j);
584 range_for (auto &j, i.gun_dirs)
585 PHYSFSX_readVector(fp, j);
586 }
587 }
588 #endif
589 }
590
591 namespace dcx {
592
593 DEFINE_SERIAL_UDT_TO_MESSAGE(v1_control_center_triggers, cct, (cct.num_links, cct.seg, cct.side));
594 ASSERT_SERIAL_UDT_MESSAGE_SIZE(v1_control_center_triggers, 42);
595
596 v1_control_center_triggers::v1_control_center_triggers(const NamedPHYSFS_File fp)
597 {
598 PHYSFSX_serialize_read(fp, *this);
599 }
600
601 v1_control_center_triggers::v1_control_center_triggers(const control_center_triggers &cct) :
602 num_links(cct.num_links), seg(cct.seg)
603 {
604 for (auto &&[w, r] : zip(side, cct.side))
605 w = underlying_value(r);
606 }
607
608 /*
609 * reads n control_center_triggers structs from a PHYSFS_File and swaps if specified
610 */
611 control_center_triggers control_center_triggers_read(const NamedPHYSFS_File fp)
612 {
613 const v1_control_center_triggers v1cct{fp};
614 const std::size_t num_links{v1cct.num_links};
615 control_center_triggers cct{};
616 if (unlikely(!num_links))
617 return cct;
618 /* num_links is derived from level data, which may be invalid.
619 */
620 constexpr std::size_t maximum_allowed_links{std::size(v1cct.seg)};
621 const auto clamped_num_links{std::min(num_links, maximum_allowed_links)};
622 if (unlikely(clamped_num_links != num_links))
623 LevelError("too many control center triggers (found %" DXX_PRI_size_type ", max %" DXX_PRI_size_type "); ignoring excess triggers.", num_links, maximum_allowed_links);
624 const auto &&cct_input_range = zip(partial_range(v1cct.seg, clamped_num_links), v1cct.side);
625 const auto &&cct_output_range = zip(cct.seg, cct.side);
626 auto oi = cct_output_range.begin();
627 auto ii = cct_input_range.begin();
628 const auto ie = cct_input_range.end();
629 uint8_t valid_num_links{0};
630 const auto segment_count = Segments.get_count();
631 for (; ii != ie; ++ii)
632 {
633 const auto &&[iseg, iside] = *ii;
634 const auto si = build_sidenum_from_untrusted(iside);
635 if (!si)
636 {
637 LevelError("invalid segment side %u in control center trigger; ignoring.", iside);
638 continue;
639 }
640 /* Descent 2: Vertigo level 10 specifies an invalid control center trigger.
641 * seg[0] is 0x257, but the level only has 0x1ae segments defined.
642 * Attempting to access segment 0x257 is undefined behavior, and will crash
643 * in a memory-poisoned build. Guard against that behavior by cleaning the
644 * structure at load time.
645 */
646 if (iseg >= segment_count)
647 {
648 /* When playing Vertigo, only log at verbose level, since users
649 * cannot fix the level.
650 *
651 * For any other mission, log at urgent level, so that level
652 * authors will fix their level.
653 */
654 LevelErrorV(!strcmp(Current_mission->briefing_text_filename, "d2x.txb") ? CON_VERBOSE : CON_URGENT, "invalid segment index %u in control center trigger; ignoring.", iseg);
655 continue;
656 }
657 auto &&[oseg, oside] = *oi;
658 ++ oi;
659 oseg = iseg;
660 oside = si.value();
661 ++ valid_num_links;
662 }
663 cct.num_links = valid_num_links;
664 return cct;
665 }
666
667 void control_center_triggers_write(const control_center_triggers &cct, PHYSFS_File *fp)
668 {
669 const v1_control_center_triggers v1cct{cct};
670 PHYSFSX_serialize_write(fp, v1cct);
671 }
672
673 }