Move PlayerSettings class to client code

This commit is contained in:
sfan5 2024-02-29 15:13:52 +01:00
parent c524c52baa
commit badd42789a
4 changed files with 83 additions and 59 deletions

@ -28,6 +28,49 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "client.h"
#include "content_cao.h"
/*
PlayerSettings
*/
const static std::string PlayerSettings_names[] = {
"free_move", "pitch_move", "fast_move", "continuous_forward", "always_fly_fast",
"aux1_descends", "noclip", "autojump"
};
void PlayerSettings::readGlobalSettings()
{
free_move = g_settings->getBool("free_move");
pitch_move = g_settings->getBool("pitch_move");
fast_move = g_settings->getBool("fast_move");
continuous_forward = g_settings->getBool("continuous_forward");
always_fly_fast = g_settings->getBool("always_fly_fast");
aux1_descends = g_settings->getBool("aux1_descends");
noclip = g_settings->getBool("noclip");
autojump = g_settings->getBool("autojump");
}
void PlayerSettings::registerSettingsCallback()
{
for (auto &name : PlayerSettings_names) {
g_settings->registerChangedCallback(name,
&PlayerSettings::settingsChangedCallback, this);
}
}
void PlayerSettings::deregisterSettingsCallback()
{
for (auto &name : PlayerSettings_names) {
g_settings->deregisterChangedCallback(name,
&PlayerSettings::settingsChangedCallback, this);
}
}
void PlayerSettings::settingsChangedCallback(const std::string &name, void *data)
{
((PlayerSettings *)data)->readGlobalSettings();
}
/*
LocalPlayer
*/
@ -36,6 +79,13 @@ LocalPlayer::LocalPlayer(Client *client, const char *name):
Player(name, client->idef()),
m_client(client)
{
m_player_settings.readGlobalSettings();
m_player_settings.registerSettingsCallback();
}
LocalPlayer::~LocalPlayer()
{
m_player_settings.deregisterSettingsCallback();
}
static aabb3f getNodeBoundingBox(const std::vector<aabb3f> &nodeboxes)

@ -24,7 +24,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "constants.h"
#include "settings.h"
#include "lighting.h"
#include <list>
class Client;
class Environment;
@ -39,14 +38,33 @@ enum class LocalPlayerAnimation
NO_ANIM,
WALK_ANIM,
DIG_ANIM,
WD_ANIM
}; // no local animation, walking, digging, both
WD_ANIM // walking + digging
};
struct PlayerSettings
{
bool free_move = false;
bool pitch_move = false;
bool fast_move = false;
bool continuous_forward = false;
bool always_fly_fast = false;
bool aux1_descends = false;
bool noclip = false;
bool autojump = false;
void readGlobalSettings();
void registerSettingsCallback();
void deregisterSettingsCallback();
private:
static void settingsChangedCallback(const std::string &name, void *data);
};
class LocalPlayer : public Player
{
public:
LocalPlayer(Client *client, const char *name);
virtual ~LocalPlayer() = default;
virtual ~LocalPlayer();
// Initialize hp to 0, so that no hearts will be shown if server
// doesn't support health points
@ -161,6 +179,8 @@ public:
inline Lighting& getLighting() { return m_lighting; }
inline PlayerSettings &getPlayerSettings() { return m_player_settings; }
private:
void accelerate(const v3f &target_speed, const f32 max_increase_H,
const f32 max_increase_V, const bool use_pitch);
@ -211,5 +231,7 @@ private:
GenericCAO *m_cao = nullptr;
Client *m_client;
PlayerSettings m_player_settings;
Lighting m_lighting;
};

@ -75,20 +75,10 @@ Player::Player(const char *name, IItemDefManager *idef):
HUD_FLAG_CHAT_VISIBLE;
hud_hotbar_itemcount = HUD_HOTBAR_ITEMCOUNT_DEFAULT;
m_player_settings.readGlobalSettings();
// Register player setting callbacks
for (const std::string &name : m_player_settings.setting_names)
g_settings->registerChangedCallback(name,
&Player::settingsChangedCallback, &m_player_settings);
}
Player::~Player()
{
// m_player_settings becomes invalid, remove callbacks
for (const std::string &name : m_player_settings.setting_names)
g_settings->deregisterChangedCallback(name,
&Player::settingsChangedCallback, &m_player_settings);
clearHud();
}
@ -224,20 +214,3 @@ void PlayerControl::unpackKeysPressed(u32 keypress_bits)
place = keypress_bits & (1 << 8);
zoom = keypress_bits & (1 << 9);
}
void PlayerSettings::readGlobalSettings()
{
free_move = g_settings->getBool("free_move");
pitch_move = g_settings->getBool("pitch_move");
fast_move = g_settings->getBool("fast_move");
continuous_forward = g_settings->getBool("continuous_forward");
always_fly_fast = g_settings->getBool("always_fly_fast");
aux1_descends = g_settings->getBool("aux1_descends");
noclip = g_settings->getBool("noclip");
autojump = g_settings->getBool("autojump");
}
void Player::settingsChangedCallback(const std::string &name, void *data)
{
((PlayerSettings *)data)->readGlobalSettings();
}

@ -146,24 +146,6 @@ public:
};
};
struct PlayerSettings
{
bool free_move = false;
bool pitch_move = false;
bool fast_move = false;
bool continuous_forward = false;
bool always_fly_fast = false;
bool aux1_descends = false;
bool noclip = false;
bool autojump = false;
const std::string setting_names[8] = {
"free_move", "pitch_move", "fast_move", "continuous_forward", "always_fly_fast",
"aux1_descends", "noclip", "autojump"
};
void readGlobalSettings();
};
class Map;
struct CollisionInfo;
struct HudElement;
@ -185,17 +167,14 @@ public:
{}
// in BS-space
v3f getSpeed() const
{
return m_speed;
}
// in BS-space
void setSpeed(v3f speed)
inline void setSpeed(v3f speed)
{
m_speed = speed;
}
// in BS-space
v3f getSpeed() const { return m_speed; }
const char *getName() const { return m_name; }
u32 getFreeHudID()
@ -235,9 +214,8 @@ public:
PlayerControl control;
const PlayerControl& getPlayerControl() { return control; }
PlayerPhysicsOverride physics_override;
PlayerSettings &getPlayerSettings() { return m_player_settings; }
static void settingsChangedCallback(const std::string &name, void *data);
// Returns non-empty `selected` ItemStack. `hand` is a fallback, if specified
ItemStack &getWieldedItem(ItemStack *selected, ItemStack *hand) const;
@ -273,10 +251,11 @@ protected:
PlayerFovSpec m_fov_override_spec = { 0.0f, false, 0.0f };
std::vector<HudElement *> hud;
private:
// Protect some critical areas
// hud for example can be modified by EmergeThread
// and ServerThread
// FIXME: ^ this sounds like nonsense. should be checked.
std::mutex m_mutex;
PlayerSettings m_player_settings;
};