Skip to content

Commit

Permalink
Move PlayerSettings class to client code
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Mar 6, 2024
1 parent c524c52 commit badd427
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 58 deletions.
50 changes: 50 additions & 0 deletions src/client/localplayer.cpp
Expand Up @@ -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
*/
Expand All @@ -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)
Expand Down
30 changes: 26 additions & 4 deletions src/client/localplayer.h
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -161,6 +179,8 @@ class LocalPlayer : public Player

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);
Expand Down Expand Up @@ -211,5 +231,7 @@ class LocalPlayer : public Player

GenericCAO *m_cao = nullptr;
Client *m_client;

PlayerSettings m_player_settings;
Lighting m_lighting;
};
27 changes: 0 additions & 27 deletions src/player.cpp
Expand Up @@ -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();
}

Expand Down Expand Up @@ -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();
}
33 changes: 6 additions & 27 deletions src/player.h
Expand Up @@ -146,24 +146,6 @@ struct PlayerPhysicsOverride
};
};

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;
Expand All @@ -185,16 +167,13 @@ class Player
{}

// in BS-space
v3f getSpeed() const
inline void setSpeed(v3f speed)
{
return m_speed;
m_speed = speed;
}

// in BS-space
void setSpeed(v3f speed)
{
m_speed = speed;
}
v3f getSpeed() const { return m_speed; }

const char *getName() const { return m_name; }

Expand Down Expand Up @@ -235,9 +214,8 @@ class Player

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;
Expand Down Expand Up @@ -273,10 +251,11 @@ class Player
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;
};

0 comments on commit badd427

Please sign in to comment.