Debian Codegraph @

similar/main/weapon.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 * Functions for weapons...
23 *
24 */
25
26 #include <stdexcept>
27 #include <stdlib.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <type_traits>
31
32 #include "hudmsg.h"
33 #include "game.h"
34 #include "laser.h"
35 #include "weapon.h"
36 #include "player.h"
37 #include "dxxerror.h"
38 #include "sounds.h"
39 #include "text.h"
40 #include "powerup.h"
41 #include "newdemo.h"
42 #include "multi.h"
43 #include "object.h"
44 #include "segment.h"
45 #include "newmenu.h"
46 #include "playsave.h"
47 #include "physfs-serial.h"
48 #include "vclip.h"
49 #include "compiler-range_for.h"
50 #include "d_enumerate.h"
51 #include "d_levelstate.h"
52 #include "partial_range.h"
53
54 // Note, only Vulcan cannon requires ammo.
55 // NOTE: Now Vulcan and Gauss require ammo. -5/3/95 Yuan
56 //ubyte Default_primary_ammo_level[MAX_PRIMARY_WEAPONS] = {255, 0, 255, 255, 255};
57 //ubyte Default_secondary_ammo_level[MAX_SECONDARY_WEAPONS] = {3, 0, 0, 0, 0};
58
59 // Convert primary weapons to indices in Weapon_info array.
60 #if DXX_BUILD_DESCENT == 1
61 namespace dsx {
62 const enumerated_array<weapon_id_type, MAX_PRIMARY_WEAPONS, primary_weapon_index_t> Primary_weapon_to_weapon_info{{
63 {
64 weapon_id_type::LASER_ID,
65 weapon_id_type::VULCAN_ID,
66 weapon_id_type::CHEAP_SPREADFIRE_ID,
67 weapon_id_type::PLASMA_ID,
68 weapon_id_type::FUSION_ID
69 }
70 }};
71 const enumerated_array<weapon_id_type, MAX_SECONDARY_WEAPONS, secondary_weapon_index_t> Secondary_weapon_to_weapon_info{{
72 {
73 weapon_id_type::CONCUSSION_ID,
74 weapon_id_type::HOMING_ID,
75 weapon_id_type::PROXIMITY_ID,
76 weapon_id_type::SMART_ID,
77 weapon_id_type::MEGA_ID
78 }
79 }};
80
81 }
82 #elif DXX_BUILD_DESCENT == 2
83 #include "fvi.h"
84
85 namespace dsx {
86 const enumerated_array<weapon_id_type, MAX_PRIMARY_WEAPONS, primary_weapon_index_t> Primary_weapon_to_weapon_info{{
87 {
88 weapon_id_type::LASER_ID,
89 weapon_id_type::VULCAN_ID,
90 weapon_id_type::SPREADFIRE_ID,
91 weapon_id_type::PLASMA_ID,
92 weapon_id_type::FUSION_ID,
93 weapon_id_type::SUPER_LASER_ID,
94 weapon_id_type::GAUSS_ID,
95 weapon_id_type::HELIX_ID,
96 weapon_id_type::PHOENIX_ID,
97 weapon_id_type::OMEGA_ID
98 }
99 }};
100 const enumerated_array<weapon_id_type, MAX_SECONDARY_WEAPONS, secondary_weapon_index_t> Secondary_weapon_to_weapon_info{{
101 {
102 weapon_id_type::CONCUSSION_ID,
103 weapon_id_type::HOMING_ID,
104 weapon_id_type::PROXIMITY_ID,
105 weapon_id_type::SMART_ID,
106 weapon_id_type::MEGA_ID,
107 weapon_id_type::FLASH_ID,
108 weapon_id_type::GUIDEDMISS_ID,
109 weapon_id_type::SUPERPROX_ID,
110 weapon_id_type::MERCURY_ID,
111 weapon_id_type::EARTHSHAKER_ID
112 }
113 }};
114
115 namespace {
116
117 /*
118 * On entry:
119 * - base_weapon must be the non-super version of a weapon.
120 */
121 template <typename T>
122 static T get_super_weapon_from_base_weapon(const T base_weapon)
123 {
124 return static_cast<T>(static_cast<unsigned>(base_weapon) + SUPER_WEAPON);
125 }
126
127 /*
128 * On entry:
129 * - current_weapon may be any valid weapon index, whether regular or
130 * super.
131 * - base_weapon must be the non-super version of current_weapon. If
132 * current_weapon is the regular version, then base_weapon ==
133 * current_weapon. If current_weapon is the super version, then
134 * base_weapon + SUPER_WEAPON == current_weapon.
135 */
136 template <typename T>
137 static T get_alternate_weapon(const T current_weapon, const T base_weapon)
138 {
139 const auto b = static_cast<unsigned>(base_weapon);
140 const auto c = static_cast<unsigned>(current_weapon);
141 const auto s = static_cast<unsigned>(get_super_weapon_from_base_weapon(base_weapon));
142 /* If current_weapon == base_weapon, then this expression simplifies
143 * to (base_weapon + SUPER_WEAPON) and produces the super form of
144 * base_weapon.
145 *
146 * If current_weapon == base_weapon+SUPER_WEAPON, then this
147 * expression simplifies to (base_weapon), and produces the
148 * non-super form of base_weapon.
149 */
150 return static_cast<T>(b + s - c);
151 }
152
153 }
154
155 }
156 #endif
157
158 namespace dsx {
159
160 //for each Secondary weapon, which gun it fires out of
161 const enumerated_array<player_gun_number, MAX_SECONDARY_WEAPONS, secondary_weapon_index_t> Secondary_weapon_to_gun_num{{{
162 player_gun_number::_4,
163 player_gun_number::_4,
164 player_gun_number::_7,
165 player_gun_number::_7,
166 player_gun_number::_7,
167 #if DXX_BUILD_DESCENT == 2
168 player_gun_number::_4,
169 player_gun_number::_4,
170 player_gun_number::_7,
171 player_gun_number::_4,
172 player_gun_number::_7
173 #endif
174 }}};
175
176 const enumerated_array<uint8_t, MAX_SECONDARY_WEAPONS, secondary_weapon_index_t> Secondary_ammo_max{{
177 {
178 20, 10, 10, 5, 5,
179 #if DXX_BUILD_DESCENT == 2
180 20, 20, 15, 10, 10
181 #endif
182 }
183 }};
184
185 //for each primary weapon, what kind of powerup gives weapon
186 const enumerated_array<powerup_type_t, MAX_PRIMARY_WEAPONS, primary_weapon_index_t> Primary_weapon_to_powerup{{
187 {
188 powerup_type_t::POW_LASER,
189 powerup_type_t::POW_VULCAN_WEAPON,
190 powerup_type_t::POW_SPREADFIRE_WEAPON,
191 powerup_type_t::POW_PLASMA_WEAPON,
192 powerup_type_t::POW_FUSION_WEAPON,
193 #if DXX_BUILD_DESCENT == 2
194 powerup_type_t::POW_LASER,
195 powerup_type_t::POW_GAUSS_WEAPON,
196 powerup_type_t::POW_HELIX_WEAPON,
197 powerup_type_t::POW_PHOENIX_WEAPON,
198 powerup_type_t::POW_OMEGA_WEAPON,
199 #endif
200 }
201 }};
202
203 //for each Secondary weapon, what kind of powerup gives weapon
204 const enumerated_array<powerup_type_t, MAX_SECONDARY_WEAPONS, secondary_weapon_index_t> Secondary_weapon_to_powerup{{
205 {
206 powerup_type_t::POW_MISSILE_1,
207 powerup_type_t::POW_HOMING_AMMO_1,
208 powerup_type_t::POW_PROXIMITY_WEAPON,
209 powerup_type_t::POW_SMARTBOMB_WEAPON,
210 powerup_type_t::POW_MEGA_WEAPON,
211 #if DXX_BUILD_DESCENT == 2
212 powerup_type_t::POW_SMISSILE1_1,
213 powerup_type_t::POW_GUIDED_MISSILE_1,
214 powerup_type_t::POW_SMART_MINE,
215 powerup_type_t::POW_MERCURY_MISSILE_1,
216 powerup_type_t::POW_EARTHSHAKER_MISSILE,
217 #endif
218 }
219 }};
220
221 weapon_info_array Weapon_info;
222 }
223 namespace dcx {
224 unsigned N_weapon_types;
225
226 namespace {
227
228 template <typename cycle_weapon_state>
229 struct weapon_reorder_menu_items
230 {
231 std::array<newmenu_item, cycle_weapon_state::max_weapons + 1> menu_items;
232 weapon_reorder_menu_items();
233 };
234
235 template <typename cycle_weapon_state>
236 struct weapon_reorder_menu : weapon_reorder_menu_items<cycle_weapon_state>, reorder_newmenu
237 {
238 using weapon_reorder_menu_items<cycle_weapon_state>::menu_items;
239 weapon_reorder_menu(grs_canvas &src) :
240 reorder_newmenu(menu_title{cycle_weapon_state::reorder_title}, menu_subtitle{"Shift+Up/Down arrow to move item"}, menu_filename{nullptr}, tiny_mode_flag::normal, tab_processing_flag::ignore, adjusted_citem::create(menu_items, 0), src)
241 {
242 }
243 virtual window_event_result event_handler(const d_event &event) override;
244 };
245
246 template <typename cycle_weapon_state>
247 weapon_reorder_menu_items<cycle_weapon_state>::weapon_reorder_menu_items()
248 {
249 for (auto &&[i, mi] : enumerate(menu_items))
250 {
251 const auto o = cycle_weapon_state::get_weapon_by_order_slot(i);
252 mi.value = underlying_value(o);
253 nm_set_item_menu(mi, cycle_weapon_state::get_weapon_name(o));
254 }
255 }
256
257 template <typename cycle_weapon_state>
258 window_event_result weapon_reorder_menu<cycle_weapon_state>::event_handler(const d_event &event)
259 {
260 switch(event.type)
261 {
262 case event_type::key_command:
263 event_key_command(event);
264 break;
265 case event_type::window_close:
266 for (auto &&[i, mi] : enumerate(menu_items))
267 cycle_weapon_state::get_weapon_by_order_slot(i) = static_cast<typename cycle_weapon_state::weapon_index_type>(mi.value);
268 break;
269 default:
270 break;
271 }
272 return newmenu::event_handler(event);
273 }
274
275 }
276
277 static void check_enum(auto && /* accessor */, polygon_model_index &pmi)
278 {
279 pmi = build_polygon_model_index_from_untrusted(underlying_value(pmi));
280 }
281
282 }
283
284 // autoselect ordering
285
286 namespace dsx {
287 namespace {
288 #if DXX_BUILD_DESCENT == 1
289 constexpr std::array<primary_weapon_index_t, MAX_PRIMARY_WEAPONS + 1> DefaultPrimaryOrder{{
290 primary_weapon_index_t::FUSION_INDEX, primary_weapon_index_t::PLASMA_INDEX, primary_weapon_index_t::SPREADFIRE_INDEX, primary_weapon_index_t::VULCAN_INDEX, primary_weapon_index_t::LASER_INDEX, primary_weapon_index_t{255}
291 }};
292 constexpr std::array<secondary_weapon_index_t, MAX_SECONDARY_WEAPONS + 1> DefaultSecondaryOrder{{
293 secondary_weapon_index_t::MEGA_INDEX, secondary_weapon_index_t::SMART_INDEX, secondary_weapon_index_t::HOMING_INDEX, secondary_weapon_index_t::CONCUSSION_INDEX, secondary_weapon_index_t{255}, secondary_weapon_index_t::PROXIMITY_INDEX
294 }};
295 #elif DXX_BUILD_DESCENT == 2
296 constexpr std::array<primary_weapon_index_t, MAX_PRIMARY_WEAPONS + 1> DefaultPrimaryOrder={{
297 primary_weapon_index_t::OMEGA_INDEX, primary_weapon_index_t::PHOENIX_INDEX, primary_weapon_index_t::HELIX_INDEX, primary_weapon_index_t::GAUSS_INDEX, primary_weapon_index_t::SUPER_LASER_INDEX, primary_weapon_index_t::FUSION_INDEX, primary_weapon_index_t::PLASMA_INDEX, primary_weapon_index_t::SPREADFIRE_INDEX, primary_weapon_index_t::VULCAN_INDEX, primary_weapon_index_t::LASER_INDEX, primary_weapon_index_t{255}
298 }};
299 constexpr std::array<secondary_weapon_index_t, MAX_SECONDARY_WEAPONS + 1> DefaultSecondaryOrder={{
300 secondary_weapon_index_t::SMISSILE5_INDEX, secondary_weapon_index_t::SMISSILE4_INDEX, secondary_weapon_index_t::MEGA_INDEX, secondary_weapon_index_t::SMART_INDEX, secondary_weapon_index_t::HOMING_INDEX, secondary_weapon_index_t::SMISSILE1_INDEX, secondary_weapon_index_t::CONCUSSION_INDEX, secondary_weapon_index_t{255}, secondary_weapon_index_t::SMART_MINE_INDEX, secondary_weapon_index_t::GUIDED_INDEX, secondary_weapon_index_t::PROXIMITY_INDEX
301 }};
302 #endif
303
304 static primary_weapon_index_t get_mapped_weapon_index(const player_info &player_info, const primary_weapon_index_t weapon_index)
305 {
306 #if DXX_BUILD_DESCENT == 1
307 (void)player_info;
308 #elif DXX_BUILD_DESCENT == 2
309 if (weapon_index == primary_weapon_index_t::LASER_INDEX && player_info.laser_level > MAX_LASER_LEVEL)
310 return primary_weapon_index_t::SUPER_LASER_INDEX;
311 #endif
312 return weapon_index;
313 }
314 }
315 }
316
317 // ; (0) Laser Level 1
318 // ; (1) Laser Level 2
319 // ; (2) Laser Level 3
320 // ; (3) Laser Level 4
321 // ; (4) Unknown Use
322 // ; (5) Josh Blobs
323 // ; (6) Unknown Use
324 // ; (7) Unknown Use
325 // ; (8) ---------- Concussion Missile ----------
326 // ; (9) ---------- Flare ----------
327 // ; (10) ---------- Blue laser that blue guy shoots -----------
328 // ; (11) ---------- Vulcan Cannon ----------
329 // ; (12) ---------- Spreadfire Cannon ----------
330 // ; (13) ---------- Plasma Cannon ----------
331 // ; (14) ---------- Fusion Cannon ----------
332 // ; (15) ---------- Homing Missile ----------
333 // ; (16) ---------- Proximity Bomb ----------
334 // ; (17) ---------- Smart Missile ----------
335 // ; (18) ---------- Mega Missile ----------
336 // ; (19) ---------- Children of the PLAYER'S Smart Missile ----------
337 // ; (20) ---------- Bad Guy Spreadfire Laser ----------
338 // ; (21) ---------- SuperMech Homing Missile ----------
339 // ; (22) ---------- Regular Mech's missile -----------
340 // ; (23) ---------- Silent Spreadfire Laser ----------
341 // ; (24) ---------- Red laser that baby spiders shoot -----------
342 // ; (25) ---------- Green laser that rifleman shoots -----------
343 // ; (26) ---------- Plasma gun that 'plasguy' fires ------------
344 // ; (27) ---------- Blobs fired by Red Spiders -----------
345 // ; (28) ---------- Final Boss's Mega Missile ----------
346 // ; (29) ---------- Children of the ROBOT'S Smart Missile ----------
347 // ; (30) Laser Level 5
348 // ; (31) Laser Level 6
349 // ; (32) ---------- Super Vulcan Cannon ----------
350 // ; (33) ---------- Super Spreadfire Cannon ----------
351 // ; (34) ---------- Super Plasma Cannon ----------
352 // ; (35) ---------- Super Fusion Cannon ----------
353
354 // ------------------------------------------------------------------------------------
355 // Return:
356 // Bits set:
357 // has_primary_weapon_result::weapon
358 // has_primary_weapon_result::energy
359 // has_primary_weapon_result::ammo
360 // See weapon.h for bit values
361 namespace dsx {
362 has_primary_weapon_result player_has_primary_weapon(const player_info &player_info, primary_weapon_index_t weapon_num)
363 {
364 // Hack! If energy goes negative, you can't fire a weapon that doesn't require energy.
365 // But energy should not go negative (but it does), so find out why it does!
366 auto &energy = player_info.energy;
367
368 const auto weapon_index = Primary_weapon_to_weapon_info[weapon_num];
369
370 if (!(player_info.primary_weapon_flags & HAS_PRIMARY_FLAG(weapon_num)))
371 {
372 return has_primary_weapon_result{};
373 }
374
375 // Special case: Gauss cannon uses vulcan ammo.
376 if (weapon_index_uses_vulcan_ammo(weapon_num)) {
377 if (Weapon_info[weapon_index].ammo_usage <= player_info.vulcan_ammo)
378 return has_primary_weapon_result::weapon | has_primary_weapon_result::ammo | has_primary_weapon_result::energy;
379 return has_primary_weapon_result::weapon | has_primary_weapon_result::energy;
380 }
381 /* reject_unusable_primary_weapon_select checks
382 * has_primary_weapon_result::ammo for all weapons. Report ammo as present
383 * for energy weapons to prevent a spurious "You don't have ammo for the"
384 * error message. */
385 auto return_value = has_primary_weapon_result::weapon | has_primary_weapon_result::ammo;
386
387 #if DXX_BUILD_DESCENT == 1
388 //added on 1/21/99 by Victor Rachels... yet another hack
389 //fusion has 0 energy usage, HAS_ENERGY_FLAG was always true
390 if(weapon_num == primary_weapon_index_t::FUSION_INDEX)
391 {
392 if (energy >= F1_0*2)
393 return_value |= has_primary_weapon_result::energy;
394 }
395 #elif DXX_BUILD_DESCENT == 2
396 if (weapon_num == primary_weapon_index_t::OMEGA_INDEX) { // Hack: Make sure player has energy to omega
397 if (energy > 0 || player_info.Omega_charge)
398 return_value |= has_primary_weapon_result::energy;
399 }
400 #endif
401 else
402 {
403 const auto energy_usage = Weapon_info[weapon_index].energy_usage;
404 /* The test for `energy_usage <= 0` should not be needed.
405 * However, a Parallax comment suggests that players
406 * sometimes get negative energy. Use this test in
407 * preference to coercing negative player energy to zero.
408 */
409 if (energy_usage <= 0 || energy_usage <= energy)
410 return_value |= has_primary_weapon_result::energy;
411 }
412 return return_value;
413 }
414
415 has_secondary_weapon_result player_has_secondary_weapon(const player_info &player_info, const secondary_weapon_index_t weapon_num)
416 {
417 const auto secondary_ammo = player_info.secondary_ammo[weapon_num];
418 const auto weapon_index = Secondary_weapon_to_weapon_info[weapon_num];
419 if (secondary_ammo && Weapon_info[weapon_index].ammo_usage <= secondary_ammo)
420 return has_secondary_weapon_result::present;
421 return has_secondary_weapon_result::absent;
422 }
423
424 void InitWeaponOrdering ()
425 {
426 // short routine to setup default weapon priorities for new pilots
427 PlayerCfg.PrimaryOrder = DefaultPrimaryOrder;
428 PlayerCfg.SecondaryOrder = DefaultSecondaryOrder;
429 }
430
431 namespace {
432
433 static uint_fast32_t POrderList(primary_weapon_index_t num);
434 static uint_fast32_t SOrderList(secondary_weapon_index_t num);
435
436 class cycle_weapon_state
437 {
438 public:
439 static constexpr char DXX_WEAPON_TEXT_NEVER_AUTOSELECT[] = "--- Never autoselect below ---";
440 [[noreturn]]
441 dxx_compiler_attribute_cold
442 static void report_runtime_error(const char *);
443 };
444
445 class cycle_primary_state : public cycle_weapon_state
446 {
447 player_info &pl_info;
448 public:
449 using weapon_index_type = primary_weapon_index_t;
450 static constexpr std::integral_constant<weapon_index_type, weapon_index_type{255}> cycle_never_autoselect_below{};
451 cycle_primary_state(player_info &p) :
452 pl_info(p)
453 {
454 }
455 static constexpr std::integral_constant<uint_fast32_t, MAX_PRIMARY_WEAPONS> max_weapons{};
456 static constexpr char reorder_title[] = "Reorder Primary";
457 static constexpr char error_weapon_list_corrupt[] = "primary weapon list corrupt";
458 static uint_fast32_t get_cycle_position(primary_weapon_index_t i)
459 {
460 return POrderList(i);
461 }
462 static player_config::primary_weapon_order::reference get_weapon_by_order_slot(player_config::primary_weapon_order::size_type cur_order_slot)
463 {
464 return PlayerCfg.PrimaryOrder[cur_order_slot];
465 }
466 bool maybe_select_weapon_by_order_slot(uint_fast32_t cur_order_slot) const
467 {
468 return maybe_select_weapon_by_type(get_weapon_by_order_slot(cur_order_slot));
469 }
470 bool maybe_select_weapon_by_type(const weapon_index_type desired_weapon_idx) const
471 {
472 weapon_index_type desired_weapon = static_cast<weapon_index_type>(desired_weapon_idx);
473 #if DXX_BUILD_DESCENT == 2
474 // some remapping for SUPER LASER which is not an actual weapon type at all
475 if (desired_weapon == primary_weapon_index_t::LASER_INDEX)
476 {
477 if (pl_info.laser_level > MAX_LASER_LEVEL)
478 return false;
479 }
480 else if (desired_weapon == primary_weapon_index_t::SUPER_LASER_INDEX)
481 {
482 if (pl_info.laser_level <= MAX_LASER_LEVEL)
483 return false;
484 else
485 desired_weapon = primary_weapon_index_t::LASER_INDEX;
486 }
487 #endif
488 if (!has_all(player_has_primary_weapon(pl_info, desired_weapon)))
489 return false;
490 select_primary_weapon(pl_info, PRIMARY_WEAPON_NAMES(desired_weapon), desired_weapon, 1);
491 return true;
492 }
493 void abandon_auto_select()
494 {
495 {
496 const auto &&m = TXT_NO_PRIMARY;
497 HUD_init_message_literal(HM_DEFAULT, {m, strlen(m)});
498 }
499 if (pl_info.Primary_weapon == primary_weapon_index_t::LASER_INDEX)
500 return;
501 select_primary_weapon(pl_info, nullptr, primary_weapon_index_t::LASER_INDEX, 1);
502 }
503 static const char *get_weapon_name(const primary_weapon_index_t i)
504 {
505 return i == cycle_never_autoselect_below ? DXX_WEAPON_TEXT_NEVER_AUTOSELECT : PRIMARY_WEAPON_NAMES(i);
506 }
507 };
508
509 class cycle_secondary_state : public cycle_weapon_state
510 {
511 player_info &pl_info;
512 public:
513 using weapon_index_type = secondary_weapon_index_t;
514 static constexpr std::integral_constant<weapon_index_type, weapon_index_type{255}> cycle_never_autoselect_below{};
515 cycle_secondary_state(player_info &p) :
516 pl_info(p)
517 {
518 }
519 static constexpr std::integral_constant<uint_fast32_t, MAX_SECONDARY_WEAPONS> max_weapons{};
520 static constexpr char reorder_title[] = "Reorder Secondary";
521 static constexpr char error_weapon_list_corrupt[] = "secondary weapon list corrupt";
522 static uint_fast32_t get_cycle_position(secondary_weapon_index_t i)
523 {
524 return SOrderList(i);
525 }
526 static player_config::secondary_weapon_order::reference get_weapon_by_order_slot(player_config::secondary_weapon_order::size_type cur_order_slot)
527 {
528 return PlayerCfg.SecondaryOrder[cur_order_slot];
529 }
530 bool maybe_select_weapon_by_order_slot(uint_fast32_t cur_order_slot)
531 {
532 return maybe_select_weapon_by_type(get_weapon_by_order_slot(cur_order_slot));
533 }
534 bool maybe_select_weapon_by_type(const weapon_index_type desired_weapon_idx)
535 {
536 const weapon_index_type desired_weapon = static_cast<weapon_index_type>(desired_weapon_idx);
537 if (!has_all(player_has_secondary_weapon(pl_info, desired_weapon)))
538 return false;
539 select_secondary_weapon(pl_info, SECONDARY_WEAPON_NAMES(desired_weapon), desired_weapon, 1);
540 return true;
541 }
542 static void abandon_auto_select()
543 {
544 HUD_init_message_literal(HM_DEFAULT, "No secondary weapons available!");
545 }
546 static const char *get_weapon_name(const secondary_weapon_index_t i)
547 {
548 return i == cycle_never_autoselect_below ? DXX_WEAPON_TEXT_NEVER_AUTOSELECT : SECONDARY_WEAPON_NAMES(i);
549 }
550 };
551
552 void cycle_weapon_state::report_runtime_error(const char *const p)
553 {
554 throw std::runtime_error(p);
555 }
556
557 template <typename T>
558 void CycleWeapon(T t, const typename T::weapon_index_type effective_weapon)
559 {
560 auto cur_order_slot = t.get_cycle_position(effective_weapon);
561 const auto autoselect_order_slot = t.get_cycle_position(t.cycle_never_autoselect_below);
562 const auto use_restricted_autoselect =
563 (cur_order_slot < autoselect_order_slot) &&
564 (1 < autoselect_order_slot) &&
565 PlayerCfg.CycleAutoselectOnly;
566 for (uint_fast32_t loop = t.max_weapons + 1; loop--;)
567 {
568 cur_order_slot++; // next slot
569 if (cur_order_slot >= t.max_weapons + 1) // loop if necessary
570 cur_order_slot = 0;
571 if (cur_order_slot == autoselect_order_slot) // what to to with non-autoselect weapons?
572 {
573 if (use_restricted_autoselect)
574 {
575 cur_order_slot = 0; // loop over or ...
576 }
577 else
578 {
579 continue; // continue?
580 }
581 }
582 if (t.maybe_select_weapon_by_order_slot(cur_order_slot)) // now that is the weapon next to our current one
583 // select the weapon if we have it
584 return;
585 }
586 }
587
588 }
589
590 void CyclePrimary(player_info &player_info)
591 {
592 CycleWeapon(cycle_primary_state(player_info), get_mapped_weapon_index(player_info, player_info.Primary_weapon));
593 }
594
595 void CycleSecondary(player_info &player_info)
596 {
597 CycleWeapon(cycle_secondary_state(player_info), player_info.Secondary_weapon);
598 }
599
600 #if DXX_BUILD_DESCENT == 2
601 namespace {
602 static inline void set_weapon_last_was_super(uint8_t &last, const uint8_t mask, const bool is_super)
603 {
604 if (is_super)
605 last |= mask;
606 else
607 last &= ~mask;
608 }
609
610 static inline void set_weapon_last_was_super(uint8_t &last, const auto eweapon_num)
611 {
612 const bool is_super = is_super_weapon(eweapon_num);
613 const auto weapon_num = underlying_value(eweapon_num);
614 set_weapon_last_was_super(last, 1 << (is_super ? weapon_num - SUPER_WEAPON : weapon_num), is_super);
615 }
616 }
617 #endif
618
619 void set_primary_weapon(player_info &player_info, const primary_weapon_index_t weapon_num)
620 {
621 if (Newdemo_state == ND_STATE_RECORDING)
622 newdemo_record_player_weapon(weapon_num);
623 player_info.Fusion_charge=0;
624 player_info.Next_laser_fire_time = 0;
625 player_info.Primary_weapon = weapon_num;
626 #if DXX_BUILD_DESCENT == 2
627 //save flag for whether was super version
628 auto &Primary_last_was_super = player_info.Primary_last_was_super;
629 set_weapon_last_was_super(Primary_last_was_super, weapon_num);
630 #endif
631 }
632
633 // ------------------------------------------------------------------------------------
634 //if message flag set, print message saying selected
635 void select_primary_weapon(player_info &player_info, const char *const weapon_name, const primary_weapon_index_t weapon_num, const int wait_for_rearm)
636 {
637 if (Newdemo_state==ND_STATE_RECORDING )
638 newdemo_record_player_weapon(weapon_num);
639
640 {
641 auto &Primary_weapon = player_info.Primary_weapon;
642 if (Primary_weapon != weapon_num) {
643 Primary_weapon = weapon_num;
644 #ifndef FUSION_KEEPS_CHARGE
645 //added 8/6/98 by Victor Rachels to fix fusion charge bug
646 player_info.Fusion_charge=0;
647 //end edit - Victor Rachels
648 #endif
649 auto &Next_laser_fire_time = player_info.Next_laser_fire_time;
650 if (wait_for_rearm)
651 {
652 multi_digi_play_sample_once(SOUND_GOOD_SELECTION_PRIMARY, F1_0);
653 Next_laser_fire_time = GameTime64 + REARM_TIME;
654 }
655 else
656 Next_laser_fire_time = 0;
657 }
658 else
659 {
660 #if DXX_BUILD_DESCENT == 1
661 if (wait_for_rearm)
662 /*
663 * In Descent 1, requesting a weapon that is already
664 * armed is pointless, so play a warning sound.
665 *
666 * In Descent 2, the player may be trying to toggle
667 * between the base and super forms of a weapon, so do
668 * not generate a warning sound in that case.
669 */
670 digi_play_sample(sound_effect::SOUND_ALREADY_SELECTED, F1_0);
671 #endif
672 }
673 #if DXX_BUILD_DESCENT == 2
674 //save flag for whether was super version
675 set_weapon_last_was_super(player_info.Primary_last_was_super, weapon_num);
676 #endif
677 }
678 if (weapon_name)
679 {
680 #if DXX_BUILD_DESCENT == 2
681 if (weapon_num == primary_weapon_index_t::LASER_INDEX)
682 HUD_init_message(HM_DEFAULT, "%s Level %u %s", weapon_name, static_cast<unsigned>(player_info.laser_level) + 1, TXT_SELECTED);
683 else
684 #endif
685 HUD_init_message(HM_DEFAULT, "%s %s", weapon_name, TXT_SELECTED);
686 }
687
688 }
689
690 void set_secondary_weapon_to_concussion(player_info &player_info)
691 {
692 const auto weapon_num = secondary_weapon_index_t::CONCUSSION_INDEX;
693 if (Newdemo_state == ND_STATE_RECORDING)
694 newdemo_record_player_weapon(weapon_num);
695
696 player_info.Next_missile_fire_time = 0;
697 Global_missile_firing_count = 0;
698 player_info.Secondary_weapon = weapon_num;
699 #if DXX_BUILD_DESCENT == 2
700 //save flag for whether was super version
701 set_weapon_last_was_super(player_info.Secondary_last_was_super, weapon_num);
702 #endif
703 }
704
705 void select_secondary_weapon(player_info &player_info, const char *const weapon_name, const secondary_weapon_index_t weapon_num, const int wait_for_rearm)
706 {
707 if (Newdemo_state==ND_STATE_RECORDING )
708 newdemo_record_player_weapon(weapon_num);
709
710 {
711 auto &Secondary_weapon = player_info.Secondary_weapon;
712 if (Secondary_weapon != weapon_num) {
713 auto &Next_missile_fire_time = player_info.Next_missile_fire_time;
714 if (wait_for_rearm)
715 {
716 multi_digi_play_sample_once(SOUND_GOOD_SELECTION_SECONDARY, F1_0);
717 Next_missile_fire_time = GameTime64 + REARM_TIME;
718 }
719 else
720 Next_missile_fire_time = 0;
721 Global_missile_firing_count = 0;
722 } else {
723 if (wait_for_rearm)
724 {
725 digi_play_sample_once( sound_effect::SOUND_ALREADY_SELECTED, F1_0 );
726 }
727
728 }
729 Secondary_weapon = weapon_num;
730 #if DXX_BUILD_DESCENT == 2
731 //save flag for whether was super version
732 set_weapon_last_was_super(player_info.Secondary_last_was_super, weapon_num);
733 #endif
734 }
735 if (weapon_name)
736 {
737 HUD_init_message(HM_DEFAULT, "%s %s", weapon_name, TXT_SELECTED);
738 }
739 }
740
741 #if DXX_BUILD_DESCENT == 1
742 namespace {
743 static bool reject_shareware_weapon_select_impl(const uint_fast32_t weapon_num, const char *const weapon_name)
744 {
745 // do special hud msg. for picking registered weapon in shareware version.
746 if (PCSharePig)
747 if (weapon_num >= NUM_SHAREWARE_WEAPONS) {
748 HUD_init_message(HM_DEFAULT, "%s %s!", weapon_name,TXT_NOT_IN_SHAREWARE);
749 return true;
750 }
751 return false;
752 }
753
754 template <typename T>
755 requires(std::is_same<T, primary_weapon_index_t>::value || std::is_same<T, secondary_weapon_index_t>::value)
756 static bool reject_shareware_weapon_select(const T weapon_num, const char *const weapon_name)
757 {
758 return reject_shareware_weapon_select_impl(underlying_value(weapon_num), weapon_name);
759 }
760
761 static bool reject_unusable_primary_weapon_select(const player_info &player_info, const primary_weapon_index_t weapon_num, const char *const weapon_name)
762 {
763 const auto weapon_status = player_has_primary_weapon(player_info, weapon_num);
764 const char *prefix;
765 if (!has_weapon(weapon_status))
766 prefix = TXT_DONT_HAVE;
767 else if ((weapon_status & has_primary_weapon_result::ammo) == has_primary_weapon_result{})
768 prefix = TXT_DONT_HAVE_AMMO;
769 else
770 return false;
771 HUD_init_message(HM_DEFAULT, "%s %s!", prefix, weapon_name);
772 return true;
773 }
774
775 static bool reject_unusable_secondary_weapon_select(const player_info &player_info, const secondary_weapon_index_t weapon_num, const char *const weapon_name)
776 {
777 const auto weapon_status = player_has_secondary_weapon(player_info, weapon_num);
778 if (has_all(weapon_status))
779 return false;
780 HUD_init_message(HM_DEFAULT, "%s %s%s", TXT_HAVE_NO, weapon_name, TXT_SX);
781 return true;
782 }
783 }
784 #endif
785
786 // ------------------------------------------------------------------------------------
787 // Select a weapon, primary or secondary.
788 void do_primary_weapon_select(player_info &player_info, primary_weapon_index_t weapon_num)
789 {
790 #if DXX_BUILD_DESCENT == 1
791 //added on 10/9/98 by Victor Rachels to add laser cycle
792 //end this section addition - Victor Rachels
793 const auto weapon_name = PRIMARY_WEAPON_NAMES(weapon_num);
794 if (reject_shareware_weapon_select(weapon_num, weapon_name) || reject_unusable_primary_weapon_select(player_info, weapon_num, weapon_name))
795 {
796 digi_play_sample(sound_effect::SOUND_BAD_SELECTION, F1_0);
797 return;
798 }
799 #elif DXX_BUILD_DESCENT == 2
800 has_primary_weapon_result weapon_status;
801
802 auto &Primary_weapon = player_info.Primary_weapon;
803 const auto current = Primary_weapon.get_active();
804 const auto last_was_super = player_info.Primary_last_was_super & HAS_PRIMARY_FLAG(weapon_num);
805
806 if (current == weapon_num || current == static_cast<primary_weapon_index_t>(underlying_value(weapon_num) + SUPER_WEAPON))
807 {
808 //already have this selected, so toggle to other of normal/super version
809 weapon_num = get_alternate_weapon(current, weapon_num);
810 weapon_status = player_has_primary_weapon(player_info, weapon_num);
811 }
812 else {
813 const auto weapon_num_save{weapon_num};
814
815 //go to last-select version of requested missile
816
817 if (last_was_super)
818 weapon_num = get_super_weapon_from_base_weapon(weapon_num);
819
820 weapon_status = player_has_primary_weapon(player_info, weapon_num);
821
822 //if don't have last-selected, try other version
823
824 if (!has_weapon(weapon_status))
825 {
826 weapon_num = get_alternate_weapon(weapon_num, weapon_num_save);
827 weapon_status = player_has_primary_weapon(player_info, weapon_num);
828 if (!has_weapon(weapon_status))
829 weapon_num = get_alternate_weapon(weapon_num, weapon_num_save);
830 }
831 }
832
833 //if we don't have the weapon we're switching to, give error & bail
834 const auto weapon_name = PRIMARY_WEAPON_NAMES(weapon_num);
835 if (!has_weapon(weapon_status))
836 {
837 {
838 if (weapon_num == primary_weapon_index_t::SUPER_LASER_INDEX)
839 return; //no such thing as super laser, so no error
840 HUD_init_message(HM_DEFAULT, "%s %s!", TXT_DONT_HAVE, weapon_name);
841 }
842 digi_play_sample( sound_effect::SOUND_BAD_SELECTION, F1_0 );
843 return;
844 }
845
846 //now actually select the weapon
847 #endif
848 select_primary_weapon(player_info, weapon_name, weapon_num, 1);
849 }
850
851 void do_secondary_weapon_select(player_info &player_info, secondary_weapon_index_t weapon_num)
852 {
853 #if DXX_BUILD_DESCENT == 1
854 //added on 10/9/98 by Victor Rachels to add laser cycle
855 //end this section addition - Victor Rachels
856 // do special hud msg. for picking registered weapon in shareware version.
857 const auto weapon_name = SECONDARY_WEAPON_NAMES(weapon_num);
858 if (reject_shareware_weapon_select(weapon_num, weapon_name) || reject_unusable_secondary_weapon_select(player_info, weapon_num, weapon_name))
859 {
860 digi_play_sample(sound_effect::SOUND_BAD_SELECTION, F1_0);
861 return;
862 }
863 #elif DXX_BUILD_DESCENT == 2
864 has_secondary_weapon_result weapon_status;
865
866 const auto current = player_info.Secondary_weapon.get_active();
867 const auto last_was_super = player_info.Secondary_last_was_super & HAS_SECONDARY_FLAG(weapon_num);
868
869 if (current == weapon_num || current == static_cast<secondary_weapon_index_t>(underlying_value(weapon_num) + SUPER_WEAPON))
870 {
871 //already have this selected, so toggle to other of normal/super version
872 weapon_num = get_alternate_weapon(current, weapon_num);
873 weapon_status = player_has_secondary_weapon(player_info, weapon_num);
874 }
875 else {
876 const auto weapon_num_save{weapon_num};
877
878 //go to last-select version of requested missile
879
880 if (last_was_super)
881 weapon_num = get_super_weapon_from_base_weapon(weapon_num);
882
883 weapon_status = player_has_secondary_weapon(player_info, weapon_num);
884
885 //if don't have last-selected, try other version
886
887 if (weapon_status == has_secondary_weapon_result::absent)
888 {
889 weapon_num = get_alternate_weapon(weapon_num, weapon_num_save);
890 weapon_status = player_has_secondary_weapon(player_info, weapon_num);
891 if (weapon_status == has_secondary_weapon_result::absent)
892 weapon_num = get_alternate_weapon(weapon_num, weapon_num_save);
893 }
894 }
895
896 //if we don't have the weapon we're switching to, give error & bail
897 const auto weapon_name = SECONDARY_WEAPON_NAMES(weapon_num);
898 if (weapon_status == has_secondary_weapon_result::absent)
899 {
900 HUD_init_message(HM_DEFAULT, "%s %s%s", TXT_HAVE_NO, weapon_name, TXT_SX);
901 digi_play_sample( sound_effect::SOUND_BAD_SELECTION, F1_0 );
902 return;
903 }
904
905 //now actually select the weapon
906 #endif
907 select_secondary_weapon(player_info, weapon_name, weapon_num, 1);
908 }
909 }
910
911 namespace {
912
913 void auto_select_weapon(auto t)
914 {
915 for (uint_fast32_t cur_order_slot = 0; cur_order_slot != t.max_weapons + 1; ++cur_order_slot)
916 {
917 const auto weapon_type = t.get_weapon_by_order_slot(cur_order_slot);
918 if (underlying_value(weapon_type) >= t.max_weapons)
919 {
920 t.abandon_auto_select();
921 return;
922 }
923 if (t.maybe_select_weapon_by_type(weapon_type))
924 return;
925 }
926 }
927
928 }
929
930 namespace dsx {
931
932 // ----------------------------------------------------------------------------------------
933 // Automatically select next best weapon if unable to fire current weapon.
934 // Weapon type: 0==primary, 1==secondary
935 void auto_select_primary_weapon(player_info &player_info)
936 {
937 if (!has_all(player_has_primary_weapon(player_info, player_info.Primary_weapon)))
938 auto_select_weapon(cycle_primary_state(player_info));
939 }
940
941 void auto_select_secondary_weapon(player_info &player_info)
942 {
943 if (!has_all(player_has_secondary_weapon(player_info, player_info.Secondary_weapon)))
944 auto_select_weapon(cycle_secondary_state(player_info));
945 }
946
947 void delayed_autoselect(player_info &player_info, const control_info &Controls)
948 {
949 if (!Controls.state.fire_primary)
950 {
951 auto &Primary_weapon = player_info.Primary_weapon;
952 const auto primary_weapon = Primary_weapon.get_active();
953 const auto delayed_primary = Primary_weapon.get_delayed();
954 if (delayed_primary != primary_weapon)
955 {
956 if (has_all(player_has_primary_weapon(player_info, delayed_primary)))
957 select_primary_weapon(player_info, nullptr, delayed_primary, 1);
958 else
959 Primary_weapon.set_delayed(primary_weapon);
960 }
961 }
962 if (!Controls.state.fire_secondary)
963 {
964 auto &Secondary_weapon = player_info.Secondary_weapon;
965 const auto secondary_weapon = Secondary_weapon.get_active();
966 const auto delayed_secondary = Secondary_weapon.get_delayed();
967 if (delayed_secondary != secondary_weapon)
968 {
969 if (has_all(player_has_secondary_weapon(player_info, delayed_secondary)))
970 select_secondary_weapon(player_info, nullptr, delayed_secondary, 1);
971 else
972 Secondary_weapon.set_delayed(secondary_weapon);
973 }
974 }
975 }
976
977 namespace {
978
979 static void maybe_autoselect_primary_weapon(player_info &player_info, primary_weapon_index_t weapon_index, const control_info &Controls)
980 {
981 const auto want_switch = [weapon_index, &player_info]{
982 const auto cutpoint = POrderList(cycle_primary_state::cycle_never_autoselect_below);
983 const auto weapon_order = POrderList(weapon_index);
984 return weapon_order < cutpoint && weapon_order < POrderList(get_mapped_weapon_index(player_info, player_info.Primary_weapon.get_delayed()));
985 };
986 if (Controls.state.fire_primary && PlayerCfg.NoFireAutoselect != FiringAutoselectMode::Immediate)
987 {
988 if (PlayerCfg.NoFireAutoselect == FiringAutoselectMode::Delayed)
989 {
990 if (want_switch())
991 player_info.Primary_weapon.set_delayed(weapon_index);
992 }
993 }
994 else if (want_switch())
995 select_primary_weapon(player_info, nullptr, weapon_index, 1);
996 }
997
998 }
999
1000 // ---------------------------------------------------------------------
1001 //called when one of these weapons is picked up
1002 //when you pick up a secondary, you always get the weapon & ammo for it
1003 // Returns true if powerup picked up, else returns false.
1004 int pick_up_secondary(player_info &player_info, const secondary_weapon_index_t weapon_index, const int count, const control_info &Controls)
1005 {
1006 int num_picked_up;
1007 const auto max = PLAYER_MAX_AMMO(player_info.powerup_flags, Secondary_ammo_max[weapon_index]);
1008 auto &secondary_ammo = player_info.secondary_ammo;
1009 if (secondary_ammo[weapon_index] >= max)
1010 {
1011 HUD_init_message(HM_DEFAULT|HM_REDUNDANT|HM_MAYDUPL, "%s %i %ss!", TXT_ALREADY_HAVE, secondary_ammo[weapon_index], SECONDARY_WEAPON_NAMES(weapon_index));
1012 return 0;
1013 }
1014
1015 secondary_ammo[weapon_index] += count;
1016
1017 num_picked_up = count;
1018 if (secondary_ammo[weapon_index] > max)
1019 {
1020 num_picked_up = count - (secondary_ammo[weapon_index] - max);
1021 secondary_ammo[weapon_index] = max;
1022 }
1023
1024 if (secondary_ammo[weapon_index] == count) // only autoselect if player didn't have any
1025 {
1026 const auto weapon_order = SOrderList(weapon_index);
1027 auto &Secondary_weapon = player_info.Secondary_weapon;
1028 const auto want_switch = [weapon_order, &secondary_ammo, &Secondary_weapon]{
1029 return weapon_order < SOrderList(cycle_secondary_state::cycle_never_autoselect_below) && (
1030 secondary_ammo[Secondary_weapon.get_delayed()] == 0 ||
1031 weapon_order < SOrderList(Secondary_weapon.get_delayed())
1032 );
1033 };
1034 if (Controls.state.fire_secondary && PlayerCfg.NoFireAutoselect != FiringAutoselectMode::Immediate)
1035 {
1036 if (PlayerCfg.NoFireAutoselect == FiringAutoselectMode::Delayed)
1037 {
1038 if (want_switch())
1039 Secondary_weapon.set_delayed(weapon_index);
1040 }
1041 }
1042 else if (want_switch())
1043 select_secondary_weapon(player_info, nullptr, weapon_index, 1);
1044 #if DXX_BUILD_DESCENT == 2
1045 //if it's a proxbomb or smart mine,
1046 //we want to do a mini-auto-selection that applies to the drop bomb key
1047
1048 if (weapon_index_is_player_bomb(weapon_index) &&
1049 !weapon_index_is_player_bomb(player_info.Secondary_weapon))
1050 {
1051 const auto mask = HAS_SECONDARY_FLAG(secondary_weapon_index_t::PROXIMITY_INDEX);
1052 if (weapon_order < SOrderList((player_info.Secondary_last_was_super & mask) ? secondary_weapon_index_t::SMART_MINE_INDEX : secondary_weapon_index_t::PROXIMITY_INDEX))
1053 set_weapon_last_was_super(player_info.Secondary_last_was_super, mask, weapon_index == secondary_weapon_index_t::SMART_MINE_INDEX);
1054 }
1055 #endif
1056 }
1057
1058 //note: flash for all but concussion was 7,14,21
1059 if (num_picked_up>1) {
1060 PALETTE_FLASH_ADD(15,15,15);
1061 HUD_init_message(HM_DEFAULT, "%d %s%s",num_picked_up,SECONDARY_WEAPON_NAMES(weapon_index), TXT_SX);
1062 }
1063 else {
1064 PALETTE_FLASH_ADD(10,10,10);
1065 HUD_init_message(HM_DEFAULT, "%s!",SECONDARY_WEAPON_NAMES(weapon_index));
1066 }
1067
1068 return 1;
1069 }
1070 }
1071
1072 namespace {
1073
1074 template <typename cycle_weapon_state>
1075 static void ReorderWeapon()
1076 {
1077 auto menu = window_create<weapon_reorder_menu<cycle_weapon_state>>(grd_curscreen->sc_canvas);
1078 (void)menu;
1079 }
1080
1081 }
1082
1083 namespace dsx {
1084
1085 void ReorderPrimary()
1086 {
1087 ReorderWeapon<cycle_primary_state>();
1088 }
1089
1090 void ReorderSecondary()
1091 {
1092 ReorderWeapon<cycle_secondary_state>();
1093 }
1094
1095 }
1096
1097 namespace {
1098
1099 template <typename T>
1100 static uint_fast32_t search_weapon_order_list(const typename T::weapon_index_type goal)
1101 {
1102 for (uint_fast32_t i = 0; i != T::max_weapons + 1; ++i)
1103 if (T::get_weapon_by_order_slot(i) == goal)
1104 return i;
1105 T::report_runtime_error(T::error_weapon_list_corrupt);
1106 }
1107
1108 }
1109
1110 namespace dsx {
1111
1112 namespace {
1113
1114 uint_fast32_t POrderList (primary_weapon_index_t num)
1115 {
1116 return search_weapon_order_list<cycle_primary_state>(num);
1117 }
1118
1119 uint_fast32_t SOrderList (secondary_weapon_index_t num)
1120 {
1121 return search_weapon_order_list<cycle_secondary_state>(num);
1122 }
1123
1124 }
1125
1126 //called when a primary weapon is picked up
1127 //returns true if actually picked up
1128 int pick_up_primary(player_info &player_info, const primary_weapon_index_t weapon_index)
1129 {
1130 ushort flag = HAS_PRIMARY_FLAG(weapon_index);
1131
1132 if (weapon_index != primary_weapon_index_t::LASER_INDEX &&
1133 (player_info.primary_weapon_flags & flag))
1134 { //already have
1135 HUD_init_message(HM_DEFAULT|HM_REDUNDANT|HM_MAYDUPL, "%s %s!", TXT_ALREADY_HAVE_THE, PRIMARY_WEAPON_NAMES(weapon_index));
1136 return 0;
1137 }
1138
1139 player_info.primary_weapon_flags |= flag;
1140
1141 maybe_autoselect_primary_weapon(player_info, weapon_index, Controls);
1142
1143 PALETTE_FLASH_ADD(7,14,21);
1144
1145 if (weapon_index != primary_weapon_index_t::LASER_INDEX)
1146 HUD_init_message(HM_DEFAULT, "%s!",PRIMARY_WEAPON_NAMES(weapon_index));
1147
1148 return 1;
1149 }
1150
1151 #if DXX_BUILD_DESCENT == 2
1152 void check_to_use_primary_super_laser(player_info &player_info)
1153 {
1154 if (!(player_info.primary_weapon_flags & HAS_SUPER_LASER_FLAG))
1155 {
1156 const auto weapon_index = primary_weapon_index_t::SUPER_LASER_INDEX;
1157 const auto pwi = POrderList(weapon_index);
1158 if (pwi < POrderList(cycle_primary_state::cycle_never_autoselect_below) &&
1159 pwi < POrderList(player_info.Primary_weapon))
1160 {
1161 select_primary_weapon(player_info, nullptr, primary_weapon_index_t::LASER_INDEX, 1);
1162 }
1163 }
1164 PALETTE_FLASH_ADD(7,14,21);
1165 }
1166 #endif
1167
1168 namespace {
1169
1170 static void maybe_autoselect_vulcan_weapon(player_info &player_info)
1171 {
1172 #if DXX_BUILD_DESCENT == 1
1173 const auto weapon_flag_mask{HAS_VULCAN_FLAG};
1174 #elif DXX_BUILD_DESCENT == 2
1175 const auto weapon_flag_mask = HAS_VULCAN_FLAG | HAS_GAUSS_FLAG;
1176 #endif
1177 const auto primary_weapon_flags = player_info.primary_weapon_flags;
1178 if (!(primary_weapon_flags & weapon_flag_mask))
1179 return;
1180 const auto cutpoint = POrderList(cycle_primary_state::cycle_never_autoselect_below);
1181 auto weapon_index = primary_weapon_index_t::VULCAN_INDEX;
1182 #if DXX_BUILD_DESCENT == 1
1183 const auto weapon_order_vulcan = POrderList(primary_weapon_index_t::VULCAN_INDEX);
1184 const auto better{weapon_order_vulcan};
1185 #elif DXX_BUILD_DESCENT == 2
1186 /* If a weapon is missing, pretend its auto-select priority is equal
1187 * to cutpoint. Priority at or worse than cutpoint is never
1188 * auto-selected.
1189 */
1190 const auto weapon_order_vulcan = (primary_weapon_flags & HAS_VULCAN_FLAG)
1191 ? POrderList(primary_weapon_index_t::VULCAN_INDEX)
1192 : cutpoint;
1193 const auto weapon_order_gauss = (primary_weapon_flags & HAS_GAUSS_FLAG)
1194 ? POrderList(primary_weapon_index_t::GAUSS_INDEX)
1195 : cutpoint;
1196 /* Set better to whichever vulcan-based weapon is higher priority.
1197 * The chosen weapon might still be worse than cutpoint.
1198 */
1199 const auto better = (weapon_order_vulcan < weapon_order_gauss)
1200 ? weapon_order_vulcan
1201 : (weapon_index = primary_weapon_index_t::GAUSS_INDEX, weapon_order_gauss);
1202 #endif
1203 if (better >= cutpoint)
1204 /* Preferred weapon is not auto-selectable */
1205 return;
1206 if (better >= POrderList(get_mapped_weapon_index(player_info, player_info.Primary_weapon)))
1207 /* Preferred weapon is not as desirable as the current weapon */
1208 return;
1209 maybe_autoselect_primary_weapon(player_info, weapon_index, Controls);
1210 }
1211
1212 }
1213
1214 //called when ammo (for the vulcan cannon) is picked up
1215 // Returns the amount picked up
1216 int pick_up_vulcan_ammo(player_info &player_info, uint_fast32_t ammo_count, const bool change_weapon)
1217 {
1218 const auto max = PLAYER_MAX_AMMO(player_info.powerup_flags, VULCAN_AMMO_MAX);
1219 auto &plr_vulcan_ammo = player_info.vulcan_ammo;
1220 const auto old_ammo{plr_vulcan_ammo};
1221 if (old_ammo >= max)
1222 return 0;
1223
1224 const auto amount_can_add = max - old_ammo;
1225 /* If the amount available will not exceed maximum, then add
1226 * everything and report the entire amount as used.
1227 * If the amount available would exceed maximum, set player's count
1228 * to maximum and report as used the delta between maximum and the
1229 * player's previous count (`old_ammo`).
1230 */
1231 const auto used = (ammo_count < amount_can_add)
1232 ? (plr_vulcan_ammo += ammo_count, ammo_count)
1233 : (plr_vulcan_ammo = max, amount_can_add);
1234
1235 if (change_weapon &&
1236 plr_vulcan_ammo &&
1237 !old_ammo)
1238 maybe_autoselect_vulcan_weapon(player_info);
1239 return used; //return amount used
1240 }
1241
1242 #if DXX_BUILD_DESCENT == 2
1243 // Homing weapons cheat
1244
1245 void weapons_homing_all()
1246 {
1247 auto &Objects = LevelUniqueObjectState.Objects;
1248 auto &vmobjptr = Objects.vmptr;
1249 for(auto &obj : vmobjptr)
1250 if (obj.type == OBJ_WEAPON)
1251 obj.ctype.laser_info.track_goal = object_none;
1252 for (auto &w : Weapon_info)
1253 w.homing_flag |= 2;
1254 }
1255
1256 void weapons_homing_all_reset()
1257 {
1258 range_for (auto &w, Weapon_info)
1259 w.homing_flag &= ~2;
1260 }
1261
1262 #define SMEGA_SHAKE_TIME (F1_0*2)
1263
1264 // Call this to initialize for a new level.
1265 // Sets all super mega missile detonation times to 0 which means there aren't any.
1266 void init_smega_detonates()
1267 {
1268 LevelUniqueSeismicState.Earthshaker_detonate_times = {};
1269 }
1270
1271 namespace {
1272
1273 static void start_seismic_sound()
1274 {
1275 if (LevelUniqueSeismicState.Next_seismic_sound_time)
1276 return;
1277 LevelUniqueSeismicState.Next_seismic_sound_time = GameTime64 + d_rand()/2;
1278 digi_play_sample_looping(sound_effect::SOUND_SEISMIC_DISTURBANCE_START, F1_0, -1, -1);
1279 }
1280
1281 static void apply_seismic_effect(const int entry_fc)
1282 {
1283 const auto fc = std::clamp(entry_fc, 1, 16);
1284 LevelUniqueSeismicState.Seismic_tremor_volume += fc;
1285
1286 if (!d_tick_step)
1287 return;
1288 const auto get_base_disturbance = [fc]() {
1289 return fixmul(d_rand() - 16384, 3 * F1_0 / 16 + (F1_0 * (16 - fc)) / 32);
1290 };
1291 const fix disturb_x = get_base_disturbance();
1292 const fix disturb_z = get_base_disturbance();
1293
1294 {
1295 auto &rotvel = ConsoleObject->mtype.phys_info.rotvel;
1296 rotvel.x += disturb_x;
1297 rotvel.z += disturb_z;
1298 }
1299
1300 // Shake the buddy!
1301 auto &BuddyState = LevelUniqueObjectState.BuddyState;
1302 const auto Buddy_objnum = BuddyState.Buddy_objnum;
1303 if (Buddy_objnum != object_none) {
1304 auto &objp = *LevelUniqueObjectState.Objects.vmptr(Buddy_objnum);
1305 auto &rotvel = objp.mtype.phys_info.rotvel;
1306 rotvel.x += disturb_x * 4;
1307 rotvel.z += disturb_z * 4;
1308 }
1309 // Shake a guided missile!
1310 LevelUniqueSeismicState.Seismic_tremor_magnitude += disturb_x;
1311 }
1312
1313 }
1314
1315 // If a smega missile been detonated, rock the mine!
1316 // This should be called every frame.
1317 // Maybe this should affect all robots, being called when they get their physics done.
1318 void rock_the_mine_frame(void)
1319 {
1320 range_for (auto &i, LevelUniqueSeismicState.Earthshaker_detonate_times)
1321 {
1322 if (i != 0) {
1323 fix delta_time = GameTime64 - i;
1324 start_seismic_sound();
1325 if (delta_time < SMEGA_SHAKE_TIME) {
1326
1327 // Control center destroyed, rock the player's ship.
1328 // -- fc = abs(delta_time - SMEGA_SHAKE_TIME/2);
1329 // Changed 10/23/95 to make decreasing for super mega missile.
1330 const int fc = (SMEGA_SHAKE_TIME - delta_time) / 2;
1331 apply_seismic_effect(fc / (SMEGA_SHAKE_TIME / 32));
1332 } else
1333 i = 0;
1334 }
1335 }
1336
1337 // Hook in the rumble sound effect here.
1338 }
1339
1340 void init_seismic_disturbances()
1341 {
1342 LevelUniqueSeismicState.Seismic_disturbance_end_time = 0;
1343 }
1344
1345 namespace {
1346
1347 // Return true if time to start a seismic disturbance.
1348 static bool seismic_disturbance_active()
1349 {
1350 const auto level_shake_duration = LevelSharedSeismicState.Level_shake_duration;
1351 if (level_shake_duration < 1)
1352 return false;
1353
1354 if (LevelUniqueSeismicState.Seismic_disturbance_end_time && LevelUniqueSeismicState.Seismic_disturbance_end_time < GameTime64)
1355 return true;
1356
1357 bool rval;
1358 rval = (2 * fixmul(d_rand(), LevelSharedSeismicState.Level_shake_frequency)) < FrameTime;
1359
1360 if (rval) {
1361 LevelUniqueSeismicState.Seismic_disturbance_end_time = GameTime64 + level_shake_duration;
1362 start_seismic_sound();
1363 if (Game_mode & GM_MULTI)
1364 multi_send_seismic(level_shake_duration);
1365 }
1366 return rval;
1367 }
1368
1369 static void seismic_disturbance_frame(void)
1370 {
1371 if (LevelSharedSeismicState.Level_shake_frequency) {
1372 if (seismic_disturbance_active()) {
1373 fix delta_time = static_cast<fix>(GameTime64 - LevelUniqueSeismicState.Seismic_disturbance_end_time);
1374 const int fc = abs(delta_time - LevelSharedSeismicState.Level_shake_duration / 2);
1375 apply_seismic_effect(fc / (F1_0 / 16));
1376 }
1377 }
1378 }
1379
1380 }
1381
1382 // Call this when a smega detonates to start the process of rocking the mine.
1383 void smega_rock_stuff(void)
1384 {
1385 auto least = &LevelUniqueSeismicState.Earthshaker_detonate_times[0];
1386 range_for (auto &i, LevelUniqueSeismicState.Earthshaker_detonate_times)
1387 {
1388 if (i + SMEGA_SHAKE_TIME < GameTime64)
1389 i = 0;
1390 if (*least > i)
1391 least = &i;
1392 }
1393 *least = {GameTime64};
1394 }
1395
1396 namespace {
1397
1398 static uint8_t Super_mines_yes = 1;
1399
1400 static bool immediate_detonate_smart_mine(const vcobjptridx_t smart_mine, const vcobjptridx_t target)
1401 {
1402 if (smart_mine->segnum == target->segnum)
1403 return true;
1404 // Object which is close enough to detonate smart mine is not in same segment as smart mine.
1405 // Need to do a more expensive check to make sure there isn't an obstruction.
1406 if (likely((d_tick_count ^ (static_cast<vcobjptridx_t::integral_type>(smart_mine) + static_cast<vcobjptridx_t::integral_type>(target))) % 4))
1407 // Maybe next frame
1408 return false;
1409 fvi_info hit_data;
1410 auto fate = find_vector_intersection(fvi_query{
1411 smart_mine->pos,
1412 target->pos,
1413 fvi_query::unused_ignore_obj_list,
1414 fvi_query::unused_LevelUniqueObjectState,
1415 fvi_query::unused_Robot_info,
1416 0,
1417 smart_mine,
1418 }, smart_mine->segnum, 0, hit_data);
1419 return fate != fvi_hit_type::Wall;
1420 }
1421
1422 }
1423
1424 [[nodiscard]]
1425 static constexpr vm_distance_squared operator-(const vm_distance_squared a, const vm_distance_squared b)
1426 {
1427 return vm_distance_squared{underlying_value(a) - underlying_value(b)};
1428 }
1429
1430 // Call this once/frame to process all super mines in the level.
1431 void process_super_mines_frame(void)
1432 {
1433 auto &Objects = LevelUniqueObjectState.Objects;
1434 auto &vmobjptridx = Objects.vmptridx;
1435 int start, add;
1436
1437 // If we don't know of there being any super mines in the level, just
1438 // check every 8th object each frame.
1439 if (Super_mines_yes == 0) {
1440 start = d_tick_count & 7;
1441 add = 8;
1442 } else {
1443 start = 0;
1444 add = 1;
1445 }
1446
1447 Super_mines_yes = 0;
1448
1449 for (objnum_t i=start; i<=Highest_object_index; i+=add) {
1450 const auto io = vmobjptridx(i);
1451 if (likely(io->type != OBJ_WEAPON || get_weapon_id(io) != weapon_id_type::SUPERPROX_ID))
1452 continue;
1453 Super_mines_yes = 1;
1454 if (unlikely(io->lifeleft + F1_0*2 >= Weapon_info[weapon_id_type::SUPERPROX_ID].lifetime))
1455 continue;
1456 const auto parent_num = io->ctype.laser_info.parent_num;
1457 const auto &bombpos = io->pos;
1458 range_for (const auto &&jo, vmobjptridx)
1459 {
1460 if (unlikely(jo == parent_num))
1461 continue;
1462 if (jo->type != OBJ_PLAYER && jo->type != OBJ_ROBOT)
1463 continue;
1464 const auto dist_squared = vm_vec_dist2(bombpos, jo->pos);
1465 const vm_distance distance_threshold{F1_0 * 20};
1466 const auto distance_threshold_squared = distance_threshold * distance_threshold;
1467 if (likely(distance_threshold_squared < dist_squared))
1468 /* Cheap check, some false negatives */
1469 continue;
1470 const fix64 j_size{jo->size};
1471 if (dist_squared - vm_distance_squared{j_size * j_size} >= distance_threshold_squared)
1472 /* Accurate check */
1473 continue;
1474 if (immediate_detonate_smart_mine(io, jo))
1475 io->lifeleft = 1;
1476 }
1477 }
1478 }
1479 #endif
1480
1481 #define SPIT_SPEED 20
1482
1483 //this function is for when the player intentionally drops a powerup
1484 //this function is based on drop_powerup()
1485 imobjptridx_t spit_powerup(d_level_unique_object_state &LevelUniqueObjectState, const d_level_shared_segment_state &LevelSharedSegmentState, d_level_unique_segment_state &LevelUniqueSegmentState, const d_vclip_array &Vclip, const object_base &spitter, const powerup_type_t id, const unsigned seed)
1486 {
1487 d_srand(seed);
1488
1489 auto new_velocity{vm_vec_scale_add(spitter.mtype.phys_info.velocity, spitter.orient.fvec, i2f(SPIT_SPEED))};
1490
1491 new_velocity.x += (d_rand() - 16384) * SPIT_SPEED * 2;
1492 new_velocity.y += (d_rand() - 16384) * SPIT_SPEED * 2;
1493 new_velocity.z += (d_rand() - 16384) * SPIT_SPEED * 2;
1494
1495 // Give keys zero velocity so they can be tracked better in multi
1496
1497 if ((Game_mode & GM_MULTI) && id >= powerup_type_t::POW_KEY_BLUE && id <= powerup_type_t::POW_KEY_GOLD)
1498 new_velocity = {};
1499
1500 //there's a piece of code which lets the player pick up a powerup if
1501 //the distance between him and the powerup is less than 2 time their
1502 //combined radii. So we need to create powerups pretty far out from
1503 //the player.
1504
1505 const auto new_pos{vm_vec_scale_add(spitter.pos, spitter.orient.fvec, spitter.size)};
1506
1507 if (Game_mode & GM_MULTI)
1508 {
1509 if (Net_create_loc >= MAX_NET_CREATE_OBJECTS)
1510 {
1511 return object_none;
1512 }
1513 }
1514
1515 const auto &&objp = obj_create(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, OBJ_POWERUP, underlying_value(id), vmsegptridx(spitter.segnum), new_pos, &vmd_identity_matrix, Powerup_info[id].size, object::control_type::powerup, object::movement_type::physics, render_type::RT_POWERUP);
1516
1517 if (objp == object_none)
1518 return objp;
1519 auto &obj = *objp;
1520 obj.mtype.phys_info.velocity = new_velocity;
1521 obj.mtype.phys_info.drag = 512; //1024;
1522 obj.mtype.phys_info.mass = F1_0;
1523
1524 obj.mtype.phys_info.flags = PF_BOUNCE;
1525
1526 obj.rtype.vclip_info.vclip_num = Powerup_info[get_powerup_id(obj)].vclip_num;
1527 obj.rtype.vclip_info.frametime = Vclip[obj.rtype.vclip_info.vclip_num].frame_time;
1528 obj.rtype.vclip_info.framenum = 0;
1529
1530 if (&spitter == ConsoleObject)
1531 obj.ctype.powerup_info.flags |= PF_SPAT_BY_PLAYER;
1532
1533 switch (id)
1534 {
1535 case powerup_type_t::POW_MISSILE_1:
1536 case powerup_type_t::POW_MISSILE_4:
1537 case powerup_type_t::POW_SHIELD_BOOST:
1538 case powerup_type_t::POW_ENERGY:
1539 obj.lifeleft = (d_rand() + F1_0*3) * 64; // Lives for 3 to 3.5 binary minutes (a binary minute is 64 seconds)
1540 if (Game_mode & GM_MULTI)
1541 obj.lifeleft /= 2;
1542 break;
1543 default:
1544 break;
1545 }
1546 return objp;
1547 }
1548
1549 void DropCurrentWeapon (player_info &player_info)
1550 {
1551 auto &Objects{LevelUniqueObjectState.Objects};
1552 if (LevelUniqueObjectState.num_objects >= Objects.size())
1553 return;
1554
1555 powerup_type_t drop_type;
1556 const auto &Primary_weapon{player_info.Primary_weapon};
1557 const auto GrantedItems{(Game_mode & GM_MULTI) ? Netgame.SpawnGrantedItems : netgrant_flag::None};
1558 auto weapon_name{PRIMARY_WEAPON_NAMES(Primary_weapon)};
1559 if (Primary_weapon == primary_weapon_index_t::LASER_INDEX)
1560 {
1561 if ((player_info.powerup_flags & PLAYER_FLAGS_QUAD_LASERS) && !GrantedItems.has_quad_laser())
1562 {
1563 /* Sorry, no message. Need to fall through in case player
1564 * wanted to drop a laser powerup.
1565 */
1566 drop_type = powerup_type_t::POW_QUAD_FIRE;
1567 weapon_name = TXT_QUAD_LASERS;
1568 }
1569 else if (player_info.laser_level == laser_level::_1)
1570 {
1571 HUD_init_message_literal(HM_DEFAULT, "You cannot drop your base weapon!");
1572 return;
1573 }
1574 #if DXX_BUILD_DESCENT == 2
1575 else if (player_info.laser_level > MAX_LASER_LEVEL)
1576 {
1577 /* Disallow dropping any super lasers until someone requests
1578 * it.
1579 */
1580 HUD_init_message_literal(HM_DEFAULT, "You cannot drop super lasers!");
1581 return;
1582 }
1583 #endif
1584 else if (player_info.laser_level <= map_granted_flags_to_laser_level(GrantedItems))
1585 {
1586 HUD_init_message_literal(HM_DEFAULT, "You cannot drop granted lasers!");
1587 return;
1588 }
1589 else
1590 drop_type = powerup_type_t::POW_LASER;
1591 }
1592 else
1593 {
1594 if (HAS_PRIMARY_FLAG(Primary_weapon) & map_granted_flags_to_primary_weapon_flags(GrantedItems))
1595 {
1596 HUD_init_message(HM_DEFAULT, "You cannot drop granted %s!", weapon_name);
1597 return;
1598 }
1599 drop_type = Primary_weapon_to_powerup[Primary_weapon];
1600 }
1601
1602 const auto seed{d_rand()};
1603 const auto objnum{spit_powerup(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, Vclip, *ConsoleObject, drop_type, seed)};
1604 if (objnum == object_none)
1605 {
1606 HUD_init_message(HM_DEFAULT, "Failed to drop %s!", weapon_name);
1607 return;
1608 }
1609
1610 HUD_init_message(HM_DEFAULT, "%s dropped!", weapon_name);
1611 #if DXX_BUILD_DESCENT == 2
1612 digi_play_sample (sound_effect::SOUND_DROP_WEAPON,F1_0);
1613 #endif
1614
1615 if (weapon_index_uses_vulcan_ammo(Primary_weapon)) {
1616
1617 //if it's one of these, drop some ammo with the weapon
1618 auto &plr_vulcan_ammo{player_info.vulcan_ammo};
1619 auto ammo{plr_vulcan_ammo};
1620 #if DXX_BUILD_DESCENT == 2
1621 const auto HAS_VULCAN_AND_GAUSS_FLAGS{HAS_VULCAN_FLAG | HAS_GAUSS_FLAG};
1622 if ((player_info.primary_weapon_flags & HAS_VULCAN_AND_GAUSS_FLAGS) == HAS_VULCAN_AND_GAUSS_FLAGS)
1623 ammo /= 2; //if both vulcan & gauss, drop half
1624 #endif
1625
1626 plr_vulcan_ammo -= ammo;
1627 objnum->ctype.powerup_info.count = ammo;
1628 }
1629 #if DXX_BUILD_DESCENT == 2
1630 else if (Primary_weapon == primary_weapon_index_t::OMEGA_INDEX) {
1631
1632 //dropped weapon has current energy
1633 objnum->ctype.powerup_info.count = player_info.Omega_charge;
1634 }
1635 #endif
1636
1637 if (Game_mode & GM_MULTI)
1638 multi_send_drop_weapon(objnum,seed);
1639
1640 if (Primary_weapon == primary_weapon_index_t::LASER_INDEX)
1641 {
1642 if (drop_type == powerup_type_t::POW_QUAD_FIRE)
1643 player_info.powerup_flags &= ~PLAYER_FLAGS_QUAD_LASERS;
1644 else
1645 -- player_info.laser_level;
1646 }
1647 else
1648 player_info.primary_weapon_flags &= ~HAS_PRIMARY_FLAG(Primary_weapon);
1649 auto_select_primary_weapon(player_info);
1650 }
1651
1652 void DropSecondaryWeapon (player_info &player_info)
1653 {
1654 auto &Objects = LevelUniqueObjectState.Objects;
1655 int seed;
1656 ushort sub_ammo{0};
1657
1658 if (LevelUniqueObjectState.num_objects >= Objects.size())
1659 return;
1660
1661 auto &Secondary_weapon = player_info.Secondary_weapon;
1662 auto &secondary_ammo = player_info.secondary_ammo[Secondary_weapon];
1663 if (secondary_ammo == 0)
1664 {
1665 HUD_init_message(HM_DEFAULT, "Cannot drop %s: you have none to drop!", SECONDARY_WEAPON_NAMES(Secondary_weapon));
1666 return;
1667 }
1668
1669 auto weapon_drop_id = Secondary_weapon_to_powerup[Secondary_weapon];
1670
1671 // see if we drop single or 4-pack
1672 switch (weapon_drop_id)
1673 {
1674 case powerup_type_t::POW_MISSILE_1:
1675 case powerup_type_t::POW_HOMING_AMMO_1:
1676 #if DXX_BUILD_DESCENT == 2
1677 case powerup_type_t::POW_SMISSILE1_1:
1678 case powerup_type_t::POW_GUIDED_MISSILE_1:
1679 case powerup_type_t::POW_MERCURY_MISSILE_1:
1680 #endif
1681 if (secondary_ammo % 4)
1682 {
1683 sub_ammo = 1;
1684 }
1685 else
1686 {
1687 sub_ammo = 4;
1688 //4-pack always is next index
1689 weapon_drop_id = static_cast<powerup_type_t>(1 + static_cast<uint_fast32_t>(weapon_drop_id));
1690 }
1691 break;
1692 case powerup_type_t::POW_PROXIMITY_WEAPON:
1693 #if DXX_BUILD_DESCENT == 2
1694 case powerup_type_t::POW_SMART_MINE:
1695 #endif
1696 if (secondary_ammo < 4)
1697 {
1698 HUD_init_message(HM_DEFAULT, "Cannot drop %s: You need at least 4 to drop, but have only %u!", SECONDARY_WEAPON_NAMES(Secondary_weapon), secondary_ammo);
1699 return;
1700 }
1701 else
1702 {
1703 sub_ammo = 4;
1704 }
1705 break;
1706 case powerup_type_t::POW_SMARTBOMB_WEAPON:
1707 case powerup_type_t::POW_MEGA_WEAPON:
1708 #if DXX_BUILD_DESCENT == 2
1709 case powerup_type_t::POW_EARTHSHAKER_MISSILE:
1710 #endif
1711 sub_ammo = 1;
1712 break;
1713 case powerup_type_t::POW_EXTRA_LIFE:
1714 case powerup_type_t::POW_ENERGY:
1715 case powerup_type_t::POW_SHIELD_BOOST:
1716 case powerup_type_t::POW_LASER:
1717 case powerup_type_t::POW_KEY_BLUE:
1718 case powerup_type_t::POW_KEY_RED:
1719 case powerup_type_t::POW_KEY_GOLD:
1720 case powerup_type_t::POW_MISSILE_4:
1721 case powerup_type_t::POW_QUAD_FIRE:
1722 case powerup_type_t::POW_VULCAN_WEAPON:
1723 case powerup_type_t::POW_SPREADFIRE_WEAPON:
1724 case powerup_type_t::POW_PLASMA_WEAPON:
1725 case powerup_type_t::POW_FUSION_WEAPON:
1726 case powerup_type_t::POW_HOMING_AMMO_4:
1727 case powerup_type_t::POW_VULCAN_AMMO:
1728 case powerup_type_t::POW_CLOAK:
1729 case powerup_type_t::POW_TURBO:
1730 case powerup_type_t::POW_INVULNERABILITY:
1731 case powerup_type_t::POW_MEGAWOW:
1732 #if DXX_BUILD_DESCENT == 2
1733 case powerup_type_t::POW_GAUSS_WEAPON:
1734 case powerup_type_t::POW_HELIX_WEAPON:
1735 case powerup_type_t::POW_PHOENIX_WEAPON:
1736 case powerup_type_t::POW_OMEGA_WEAPON:
1737 case powerup_type_t::POW_SUPER_LASER:
1738 case powerup_type_t::POW_FULL_MAP:
1739 case powerup_type_t::POW_CONVERTER:
1740 case powerup_type_t::POW_AMMO_RACK:
1741 case powerup_type_t::POW_AFTERBURNER:
1742 case powerup_type_t::POW_HEADLIGHT:
1743 case powerup_type_t::POW_SMISSILE1_4:
1744 case powerup_type_t::POW_GUIDED_MISSILE_4:
1745 case powerup_type_t::POW_MERCURY_MISSILE_4:
1746 case powerup_type_t::POW_FLAG_BLUE:
1747 case powerup_type_t::POW_FLAG_RED:
1748 case powerup_type_t::POW_HOARD_ORB:
1749 #endif
1750 break;
1751 }
1752
1753 seed = d_rand();
1754
1755 auto objnum = spit_powerup(LevelUniqueObjectState, LevelSharedSegmentState, LevelUniqueSegmentState, Vclip, *ConsoleObject, weapon_drop_id, seed);
1756
1757 const auto weapon_name = SECONDARY_WEAPON_NAMES(Secondary_weapon);
1758 if (objnum == object_none)
1759 {
1760 HUD_init_message(HM_DEFAULT, "Failed to drop %s%s!", weapon_name, sub_ammo > 1 ? "s" : "");
1761 return;
1762 }
1763 #if DXX_BUILD_DESCENT == 2
1764 digi_play_sample(sound_effect::SOUND_DROP_WEAPON, F1_0);
1765 #endif
1766
1767 if (Game_mode & GM_MULTI)
1768 multi_send_drop_weapon(objnum,seed);
1769
1770 secondary_ammo -= sub_ammo;
1771 HUD_init_message(HM_DEFAULT, "Dropped %s%s, leaving %u on board!", weapon_name, sub_ammo > 1 ? "s" : "", secondary_ammo);
1772
1773 if (secondary_ammo == 0)
1774 {
1775 auto_select_secondary_weapon(player_info);
1776 }
1777 }
1778
1779 #if DXX_BUILD_DESCENT == 2
1780 // ---------------------------------------------------------------------------------------
1781 // Do seismic disturbance stuff including the looping sounds with changing volume.
1782 void do_seismic_stuff(void)
1783 {
1784 const auto stv_save = std::exchange(LevelUniqueSeismicState.Seismic_tremor_volume, 0);
1785 LevelUniqueSeismicState.Seismic_tremor_magnitude = 0;
1786
1787 rock_the_mine_frame();
1788 seismic_disturbance_frame();
1789
1790 if (stv_save != 0) {
1791 const auto Seismic_tremor_volume = LevelUniqueSeismicState.Seismic_tremor_volume;
1792 if (Seismic_tremor_volume == 0)
1793 {
1794 digi_stop_looping_sound();
1795 LevelUniqueSeismicState.Next_seismic_sound_time = 0;
1796 }
1797 else if (GameTime64 > LevelUniqueSeismicState.Next_seismic_sound_time)
1798 {
1799 int volume;
1800
1801 volume = Seismic_tremor_volume * 2048;
1802 if (volume > F1_0)
1803 volume = F1_0;
1804 digi_change_looping_volume(volume);
1805 LevelUniqueSeismicState.Next_seismic_sound_time = GameTime64 + d_rand()/4 + 8192;
1806 }
1807 }
1808
1809 }
1810 #endif
1811
1812 }
1813
1814 namespace serial {
1815
1816 template <typename T>
1817 class is_cxx_array<enumerated_array<T, NDL, Difficulty_level_type>> : public is_cxx_array<std::array<T, NDL>>
1818 {
1819 };
1820
1821 }
1822
1823 #if DXX_BUILD_DESCENT == 1
1824 DEFINE_SERIAL_UDT_TO_MESSAGE(dsx::weapon_info, w, (w.render, w.model_num, w.model_num_inner, w.persistent, w.flash_vclip, w.flash_sound, serial::pad<1>(), w.robot_hit_vclip, w.robot_hit_sound, serial::pad<1>(), w.wall_hit_vclip, w.wall_hit_sound, serial::pad<1>(), w.fire_count, w.ammo_usage, w.weapon_vclip, w.destroyable, w.matter, w.bounce, w.homing_flag, serial::pad<3>(), w.energy_usage, w.fire_wait, w.bitmap, w.blob_size, w.flash_size, w.impact_size, w.strength, w.speed, w.mass, w.drag, w.thrust, w.po_len_to_width_ratio, w.light, w.lifetime, w.damage_radius, w.picture));
1825 ASSERT_SERIAL_UDT_MESSAGE_SIZE(dsx::weapon_info, 115);
1826 #elif DXX_BUILD_DESCENT == 2
1827 namespace {
1828
1829 struct v2_weapon_info : weapon_info
1830 {
1831 };
1832
1833 }
1834
1835 void postprocess_udt(auto && /* accessor */, v2_weapon_info &w)
1836 {
1837 w.children = weapon_id_type::unspecified;
1838 w.multi_damage_scale = F1_0;
1839 w.hires_picture = w.picture;
1840 }
1841
1842 DEFINE_SERIAL_UDT_TO_MESSAGE(v2_weapon_info, w, (w.render, w.persistent, w.model_num, serial::pad<1>(), w.model_num_inner, serial::pad<1>(), w.flash_vclip, w.robot_hit_vclip, w.flash_sound, serial::pad<1>(), w.wall_hit_vclip, w.fire_count, w.robot_hit_sound, serial::pad<1>(), w.ammo_usage, w.weapon_vclip, w.wall_hit_sound, serial::pad<1>(), w.destroyable, w.matter, w.bounce, w.homing_flag, w.speedvar, w.flags, w.flash, w.afterburner_size, w.energy_usage, w.fire_wait, w.bitmap, w.blob_size, w.flash_size, w.impact_size, w.strength, w.speed, w.mass, w.drag, w.thrust, w.po_len_to_width_ratio, w.light, w.lifetime, w.damage_radius, w.picture));
1843 DEFINE_SERIAL_UDT_TO_MESSAGE(weapon_info, w, (w.render, w.persistent, w.model_num, serial::pad<1>(), w.model_num_inner, serial::pad<1>(), w.flash_vclip, w.robot_hit_vclip, w.flash_sound, serial::pad<1>(), w.wall_hit_vclip, w.fire_count, w.robot_hit_sound, serial::pad<1>(), w.ammo_usage, w.weapon_vclip, w.wall_hit_sound, serial::pad<1>(), w.destroyable, w.matter, w.bounce, w.homing_flag, w.speedvar, w.flags, w.flash, w.afterburner_size, w.children, w.energy_usage, w.fire_wait, w.multi_damage_scale, w.bitmap, w.blob_size, w.flash_size, w.impact_size, w.strength, w.speed, w.mass, w.drag, w.thrust, w.po_len_to_width_ratio, w.light, w.lifetime, w.damage_radius, w.picture, w.hires_picture));
1844 ASSERT_SERIAL_UDT_MESSAGE_SIZE(weapon_info, 125);
1845 ASSERT_SERIAL_UDT_MESSAGE_SIZE(v2_weapon_info, 118);
1846 #endif
1847
1848 #if 0
1849 void weapon_info_write(PHYSFS_File *fp, const weapon_info &w)
1850 {
1851 PHYSFSX_serialize_write(fp, w);
1852 }
1853 #endif
1854
1855 /*
1856 * reads n weapon_info structs from a PHYSFS_File
1857 */
1858 namespace dsx {
1859
1860 void weapon_info_read_n(weapon_info_array &wi, std::size_t count, const NamedPHYSFS_File fp,
1861 #if DXX_BUILD_DESCENT == 2
1862 const pig_hamfile_version file_version,
1863 #endif
1864 std::size_t offset)
1865 {
1866 auto r = partial_range(wi, offset, count);
1867 #if DXX_BUILD_DESCENT == 1
1868 #elif DXX_BUILD_DESCENT == 2
1869 if (file_version < pig_hamfile_version::_3)
1870 {
1871 range_for (auto &w, r)
1872 PHYSFSX_serialize_read(fp, static_cast<v2_weapon_info &>(w));
1873 /* Set the type of children correctly when using old
1874 * datafiles. In earlier descent versions this was simply
1875 * hard-coded in create_smart_children().
1876 */
1877 wi[weapon_id_type::SMART_ID].children = weapon_id_type::PLAYER_SMART_HOMING_ID;
1878 wi[weapon_id_type::SUPERPROX_ID].children = weapon_id_type::SMART_MINE_HOMING_ID;
1879 return;
1880 }
1881 #endif
1882 range_for (auto &w, r)
1883 {
1884 PHYSFSX_serialize_read(fp, w);
1885 }
1886 }
1887 }