Use class to wrap Primary_weapon+Delayed_primary

This commit is contained in:
Kp 2016-08-06 19:55:23 +00:00
parent 37ec5e25d4
commit 1774676571
5 changed files with 61 additions and 19 deletions

View file

@ -132,11 +132,40 @@ extern const array<uint8_t, MAX_SECONDARY_WEAPONS> Secondary_weapon_to_gun_num;
namespace dcx {
extern unsigned N_weapon_types;
template <typename T>
class player_selected_weapon
{
T active, delayed;
public:
operator T() const
{
return get_active();
}
T get_active() const
{
return active;
}
T get_delayed() const
{
return delayed;
}
player_selected_weapon &operator=(T v)
{
active = v;
set_delayed(v);
return *this;
}
void set_delayed(T v)
{
delayed = v;
}
};
}
void do_primary_weapon_select(uint_fast32_t weapon_num);
void do_secondary_weapon_select(uint_fast32_t weapon_num);
extern primary_weapon_index_t Primary_weapon;
extern player_selected_weapon<primary_weapon_index_t> Primary_weapon;
extern sbyte Secondary_weapon;
void auto_select_primary_weapon();

View file

@ -2028,7 +2028,6 @@ int do_laser_firing(vobjptridx_t objp, int weapon_num, int level, int flags, int
#endif
default:
Int3(); // Contact Yuan: Unknown Primary weapon type, setting to 0.
Primary_weapon = primary_weapon_index_t::LASER_INDEX;
}
// Set values to be recognized during comunication phase, if we are the

View file

@ -1046,7 +1046,7 @@ void newdemo_record_start_demo()
nd_write_byte(nd_record_v_player_energy = static_cast<int8_t>(f2ir(get_local_player_energy())));
nd_write_byte(nd_record_v_player_shields = static_cast<int8_t>(f2ir(get_local_player_shields())));
nd_write_int(nd_record_v_player_flags = get_local_player_flags().get_player_flags()); // be sure players flags are set
nd_write_byte(static_cast<int8_t>(Primary_weapon));
nd_write_byte(static_cast<int8_t>(static_cast<primary_weapon_index_t>(Primary_weapon)));
nd_write_byte(static_cast<int8_t>(Secondary_weapon));
nd_record_v_start_frame = nd_record_v_frame_number = 0;
#if defined(DXX_BUILD_DESCENT_II)
@ -1318,7 +1318,7 @@ void newdemo_record_player_weapon(int weapon_type, int weapon_num)
nd_write_byte( ND_EVENT_PLAYER_WEAPON );
nd_write_byte(static_cast<int8_t>(nd_record_v_weapon_type = weapon_type));
nd_write_byte(static_cast<int8_t>(nd_record_v_weapon_num = weapon_num));
nd_write_byte(weapon_type ? static_cast<int8_t>(Secondary_weapon) : static_cast<int8_t>(Primary_weapon));
nd_write_byte(weapon_type ? static_cast<int8_t>(Secondary_weapon) : static_cast<int8_t>(static_cast<primary_weapon_index_t>(Primary_weapon)));
}
void newdemo_record_effect_blowup(segnum_t segment, int side, const vms_vector &pnt)
@ -1828,7 +1828,11 @@ static int newdemo_read_demo_start(enum purpose_type purpose)
if (get_local_player_flags() & PLAYER_FLAGS_INVULNERABLE)
get_local_player_invulnerable_time() = GameTime64 - (INVULNERABLE_TIME_MAX / 2);
nd_read_byte(reinterpret_cast<int8_t *>(&Primary_weapon));
{
int8_t v;
nd_read_byte(&v);
Primary_weapon = static_cast<primary_weapon_index_t>(v);
}
nd_read_byte(&Secondary_weapon);
if (purpose == PURPOSE_REWRITE)
{
@ -3295,7 +3299,11 @@ void newdemo_goto_end(int to_rewrite)
}
if (get_local_player_flags() & PLAYER_FLAGS_INVULNERABLE)
get_local_player_invulnerable_time() = GameTime64 - (INVULNERABLE_TIME_MAX / 2);
nd_read_byte(reinterpret_cast<int8_t *>(&Primary_weapon));
{
int8_t v;
nd_read_byte(&v);
Primary_weapon = static_cast<primary_weapon_index_t>(v);
}
nd_read_byte(reinterpret_cast<int8_t *>(&Secondary_weapon));
for (int i = 0; i < MAX_PRIMARY_WEAPONS; i++)
{
@ -3702,7 +3710,7 @@ static void newdemo_write_end()
nd_write_byte((sbyte)(f2ir(get_local_player_energy())));
nd_write_byte((sbyte)(f2ir(get_local_player_shields())));
nd_write_int(get_local_player_flags().get_player_flags()); // be sure players flags are set
nd_write_byte(static_cast<int8_t>(Primary_weapon));
nd_write_byte(static_cast<int8_t>(static_cast<primary_weapon_index_t>(Primary_weapon)));
nd_write_byte(static_cast<int8_t>(Secondary_weapon));
byte_count += 8;

View file

@ -989,7 +989,10 @@ int state_save_all_sub(const char *filename, const char *desc)
state_write_player(fp, get_local_player(), get_local_player_shields(), get_local_plrobj().ctype.player_info);
// Save the current weapon info
PHYSFS_write(fp, &Primary_weapon, sizeof(sbyte), 1);
{
int8_t v = static_cast<int8_t>(static_cast<primary_weapon_index_t>(Primary_weapon));
PHYSFS_write(fp, &v, sizeof(int8_t), 1);
}
PHYSFS_write(fp, &Secondary_weapon, sizeof(sbyte), 1);
// Save the difficulty level
@ -1474,7 +1477,11 @@ int state_restore_all_sub(const char *filename, const secret_restore secret)
get_local_player().objnum = coop_org_objnum;
// Restore the weapon states
PHYSFS_read(fp, &Primary_weapon, sizeof(sbyte), 1);
{
int8_t v;
PHYSFS_read(fp, &v, sizeof(int8_t), 1);
Primary_weapon = static_cast<primary_weapon_index_t>(v);
}
PHYSFS_read(fp, &Secondary_weapon, sizeof(sbyte), 1);
select_primary_weapon(nullptr, Primary_weapon, 0);

View file

@ -117,9 +117,8 @@ weapon_info_array Weapon_info;
namespace dcx {
unsigned N_weapon_types;
}
primary_weapon_index_t Primary_weapon;
player_selected_weapon<primary_weapon_index_t> Primary_weapon;
sbyte Secondary_weapon;
static primary_weapon_index_t Delayed_primary;
static sbyte Delayed_secondary;
// autoselect ordering
@ -438,7 +437,7 @@ void select_primary_weapon(const char *const weapon_name, const uint_fast32_t we
if (wait_for_rearm) digi_play_sample( SOUND_ALREADY_SELECTED, F1_0 );
#endif
}
Delayed_primary = Primary_weapon = static_cast<primary_weapon_index_t>(weapon_num);
Primary_weapon = static_cast<primary_weapon_index_t>(weapon_num);
#if defined(DXX_BUILD_DESCENT_II)
//save flag for whether was super version
Primary_last_was_super[weapon_num % SUPER_WEAPON] = (weapon_num >= SUPER_WEAPON);
@ -544,7 +543,7 @@ void do_primary_weapon_select(uint_fast32_t weapon_num)
#elif defined(DXX_BUILD_DESCENT_II)
has_weapon_result weapon_status;
const auto current = Primary_weapon;
const auto current = Primary_weapon.get_active();
const auto last_was_super = Primary_last_was_super[weapon_num];
const auto has_flag = weapon_status.has_weapon_flag;
@ -690,14 +689,14 @@ void delayed_autoselect()
{
if (!Controls.state.fire_primary)
{
const auto primary_weapon = Primary_weapon;
const auto delayed_primary = Delayed_primary;
const auto primary_weapon = Primary_weapon.get_active();
const auto delayed_primary = Primary_weapon.get_delayed();
if (delayed_primary != primary_weapon)
{
if (player_has_primary_weapon(delayed_primary).has_all())
select_primary_weapon(nullptr, delayed_primary, 1);
else
Delayed_primary = primary_weapon;
Primary_weapon.set_delayed(primary_weapon);
}
}
if (!Controls.state.fire_secondary)
@ -719,14 +718,14 @@ static void maybe_autoselect_primary_weapon(int weapon_index)
const auto want_switch = [weapon_index]{
const auto cutpoint = POrderList(255);
const auto weapon_order = POrderList(weapon_index);
return weapon_order < cutpoint && weapon_order < POrderList(get_mapped_weapon_index(Delayed_primary));
return weapon_order < cutpoint && weapon_order < POrderList(get_mapped_weapon_index(Primary_weapon.get_delayed()));
};
if (Controls.state.fire_primary && PlayerCfg.NoFireAutoselect != FiringAutoselectMode::Immediate)
{
if (PlayerCfg.NoFireAutoselect == FiringAutoselectMode::Delayed)
{
if (want_switch())
Delayed_primary = static_cast<primary_weapon_index_t>(weapon_index);
Primary_weapon.set_delayed(static_cast<primary_weapon_index_t>(weapon_index));
}
}
else if (want_switch())
@ -1270,7 +1269,7 @@ void DropCurrentWeapon ()
const auto Primary_weapon = ::Primary_weapon;
const auto GrantedItems = (Game_mode & GM_MULTI) ? Netgame.SpawnGrantedItems : 0;
auto weapon_name = PRIMARY_WEAPON_NAMES(Primary_weapon);
if (Primary_weapon==0)
if (Primary_weapon == primary_weapon_index_t::LASER_INDEX)
{
if ((player_info.powerup_flags & PLAYER_FLAGS_QUAD_LASERS) && !GrantedItems.has_quad_laser())
{