Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Consistent HP and damage types (#8167)
Remove deprecated HUDs and chat message handling.
Remove unused m_damage variable (compat break).
HP: s32 for setter/calculations, u16 for getter.
  • Loading branch information
SmallJoker authored and paramat committed Feb 10, 2019
1 parent ba5a9f2 commit ffb17f1
Show file tree
Hide file tree
Showing 25 changed files with 67 additions and 136 deletions.
17 changes: 3 additions & 14 deletions src/client/client.cpp
Expand Up @@ -437,7 +437,7 @@ void Client::step(float dtime)
ClientEnvEvent envEvent = m_env.getClientEnvEvent();

if (envEvent.type == CEE_PLAYER_DAMAGE) {
u8 damage = envEvent.player_damage.amount;
u16 damage = envEvent.player_damage.amount;

if (envEvent.player_damage.send_to_server)
sendDamage(damage);
Expand Down Expand Up @@ -1213,9 +1213,9 @@ void Client::sendChangePassword(const std::string &oldpassword,
}


void Client::sendDamage(u8 damage)
void Client::sendDamage(u16 damage)
{
NetworkPacket pkt(TOSERVER_DAMAGE, sizeof(u8));
NetworkPacket pkt(TOSERVER_DAMAGE, sizeof(u16));
pkt << damage;
Send(&pkt);
}
Expand Down Expand Up @@ -1515,17 +1515,6 @@ void Client::typeChatMessage(const std::wstring &message)

// Send to others
sendChatMessage(message);

// Show locally
if (message[0] != L'/') {
// compatibility code
if (m_proto_ver < 29) {
LocalPlayer *player = m_env.getLocalPlayer();
assert(player);
std::wstring name = narrow_to_wide(player->getName());
pushToChatQueue(new ChatMessage(CHATMESSAGE_TYPE_NORMAL, message, name));
}
}
}

void Client::addUpdateMeshTask(v3s16 p, bool ack_to_server, bool urgent)
Expand Down
6 changes: 3 additions & 3 deletions src/client/client.h
Expand Up @@ -243,7 +243,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
void clearOutChatQueue();
void sendChangePassword(const std::string &oldpassword,
const std::string &newpassword);
void sendDamage(u8 damage);
void sendDamage(u16 damage);
void sendRespawn();
void sendReady();

Expand Down Expand Up @@ -342,7 +342,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
bool mediaReceived()
{ return !m_media_downloader; }

u8 getProtoVersion()
u16 getProtoVersion()
{ return m_proto_ver; }

bool connectedToServer();
Expand Down Expand Up @@ -504,7 +504,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
// and aren't accurate. We simply just don't know, because
// the server didn't send the version back then.
// If 0, server init hasn't been received yet.
u8 m_proto_ver = 0;
u16 m_proto_ver = 0;

u16 m_playeritem = 0;
bool m_inventory_updated = false;
Expand Down
4 changes: 2 additions & 2 deletions src/client/clientenvironment.cpp
Expand Up @@ -228,7 +228,7 @@ void ClientEnvironment::step(float dtime)
float speed = pre_factor * speed_diff.getLength();
if (speed > tolerance && !player_immortal) {
f32 damage_f = (speed - tolerance) / BS * post_factor;
u8 damage = (u8)MYMIN(damage_f + 0.5, 255);
u16 damage = (u16)MYMIN(damage_f + 0.5, U16_MAX);
if (damage != 0) {
damageLocalPlayer(damage, true);
m_client->getEventManager()->put(
Expand Down Expand Up @@ -419,7 +419,7 @@ void ClientEnvironment::processActiveObjectMessage(u16 id, const std::string &da
Callbacks for activeobjects
*/

void ClientEnvironment::damageLocalPlayer(u8 damage, bool handle_hp)
void ClientEnvironment::damageLocalPlayer(u16 damage, bool handle_hp)
{
LocalPlayer *lplayer = getLocalPlayer();
assert(lplayer);
Expand Down
4 changes: 2 additions & 2 deletions src/client/clientenvironment.h
Expand Up @@ -53,7 +53,7 @@ struct ClientEnvEvent
//struct{
//} none;
struct{
u8 amount;
u16 amount;
bool send_to_server;
} player_damage;
};
Expand Down Expand Up @@ -115,7 +115,7 @@ class ClientEnvironment : public Environment
Callbacks for activeobjects
*/

void damageLocalPlayer(u8 damage, bool handle_hp=true);
void damageLocalPlayer(u16 damage, bool handle_hp=true);

/*
Client likes to call these
Expand Down
7 changes: 3 additions & 4 deletions src/client/content_cao.cpp
Expand Up @@ -371,7 +371,7 @@ void GenericCAO::processInitData(const std::string &data)
m_id = readU16(is);
m_position = readV3F32(is);
m_rotation = readV3F32(is);
m_hp = readS16(is);
m_hp = readU16(is);
const u8 num_messages = readU8(is);

for (int i = 0; i < num_messages; i++) {
Expand Down Expand Up @@ -1508,11 +1508,10 @@ void GenericCAO::processMessage(const std::string &data)

updateAttachments();
} else if (cmd == GENERIC_CMD_PUNCHED) {
/*s16 damage =*/ readS16(is);
s16 result_hp = readS16(is);
u16 result_hp = readU16(is);

// Use this instead of the send damage to not interfere with prediction
s16 damage = m_hp - result_hp;
s32 damage = (s32)m_hp - (s32)result_hp;

m_hp = result_hp;

Expand Down
2 changes: 1 addition & 1 deletion src/client/content_cao.h
Expand Up @@ -88,7 +88,7 @@ class GenericCAO : public ClientActiveObject
v3f m_velocity;
v3f m_acceleration;
v3f m_rotation;
s16 m_hp = 1;
u16 m_hp = 1;
SmoothTranslator<v3f> pos_translator;
SmoothTranslatorWrappedv3f rot_translator;
// Spritesheet/animation stuff
Expand Down
19 changes: 0 additions & 19 deletions src/client/hud.cpp
Expand Up @@ -475,25 +475,6 @@ void Hud::drawHotbar(u16 playeritem) {
hotbar_itemcount / 2, mainlist, playeritem + 1, 0);
}
}

//////////////////////////// compatibility code to be removed //////////////
// this is ugly as hell but there's no other way to keep compatibility to
// old servers
if ((player->hud_flags & HUD_FLAG_HEALTHBAR_VISIBLE)) {
drawStatbar(v2s32(floor(0.5 * (float)m_screensize.X + 0.5),
floor(1 * (float) m_screensize.Y + 0.5)),
HUD_CORNER_UPPER, 0, "heart.png",
player->hp, v2s32((-10*24)-25,-(48+24+10)), v2s32(24,24));
}

if ((player->hud_flags & HUD_FLAG_BREATHBAR_VISIBLE) &&
(player->getBreath() < 11)) {
drawStatbar(v2s32(floor(0.5 * (float)m_screensize.X + 0.5),
floor(1 * (float) m_screensize.Y + 0.5)),
HUD_CORNER_UPPER, 0, "bubble.png",
player->getBreath(), v2s32(25,-(48+24+10)), v2s32(24,24));
}
////////////////////////////////////////////////////////////////////////////
}


Expand Down
57 changes: 20 additions & 37 deletions src/content_sao.cpp
Expand Up @@ -347,7 +347,7 @@ ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
{
std::string name;
std::string state;
s16 hp = 1;
u16 hp = 1;
v3f velocity;
v3f rotation;

Expand All @@ -364,7 +364,7 @@ ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
if (version < 1)
break;

hp = readS16(is);
hp = readU16(is);
velocity = readV3F1000(is);
// yaw must be yaw to be backwards-compatible
rotation.Y = readF1000(is);
Expand Down Expand Up @@ -548,10 +548,10 @@ std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
writeU8(os, 1); // version
os << serializeString(""); // name
writeU8(os, 0); // is_player
writeS16(os, getId()); //id
writeU16(os, getId()); //id
writeV3F32(os, m_base_position);
writeV3F32(os, m_rotation);
writeS16(os, m_hp);
writeU16(os, m_hp);

std::ostringstream msg_os(std::ios::binary);
msg_os << serializeLongString(getPropertyPacket()); // message 1
Expand Down Expand Up @@ -601,7 +601,7 @@ void LuaEntitySAO::getStaticData(std::string *result) const
} else {
os<<serializeLongString(m_init_state);
}
writeS16(os, m_hp);
writeU16(os, m_hp);
writeV3F1000(os, m_velocity);
// yaw
writeF1000(os, m_rotation.Y);
Expand Down Expand Up @@ -646,7 +646,7 @@ int LuaEntitySAO::punch(v3f dir,

if (!damage_handled) {
if (result.did_punch) {
setHP(getHP() - result.damage,
setHP((s32)getHP() - result.damage,
PlayerHPChangeReason(PlayerHPChangeReason::SET_HP));

if (result.damage > 0) {
Expand All @@ -657,7 +657,7 @@ int LuaEntitySAO::punch(v3f dir,
<< " hp, health now " << getHP() << " hp" << std::endl;
}

std::string str = gob_cmd_punched(result.damage, getHP());
std::string str = gob_cmd_punched(getHP());
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push(aom);
Expand Down Expand Up @@ -715,14 +715,12 @@ std::string LuaEntitySAO::getDescription()
return os.str();
}

void LuaEntitySAO::setHP(s16 hp, const PlayerHPChangeReason &reason)
void LuaEntitySAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
{
if (hp < 0)
hp = 0;
m_hp = hp;
m_hp = rangelim(hp, 0, U16_MAX);
}

s16 LuaEntitySAO::getHP() const
u16 LuaEntitySAO::getHP() const
{
return m_hp;
}
Expand Down Expand Up @@ -949,7 +947,7 @@ std::string PlayerSAO::getClientInitializationData(u16 protocol_version)
writeS16(os, getId()); // id
writeV3F32(os, m_base_position);
writeV3F32(os, m_rotation);
writeS16(os, getHP());
writeU16(os, getHP());

std::ostringstream msg_os(std::ios::binary);
msg_os << serializeLongString(getPropertyPacket()); // message 1
Expand Down Expand Up @@ -1044,7 +1042,7 @@ void PlayerSAO::step(float dtime, bool send_recommended)
m_env->getGameDef()->ndef()->get(ntop).damage_per_second);

if (damage_per_second != 0 && m_hp > 0) {
s16 newhp = ((s32) damage_per_second > m_hp ? 0 : m_hp - damage_per_second);
s32 newhp = (s32)m_hp - (s32)damage_per_second;
PlayerHPChangeReason reason(PlayerHPChangeReason::NODE_DAMAGE);
setHP(newhp, reason);
m_env->getGameDef()->SendPlayerHPOrDie(this, reason);
Expand Down Expand Up @@ -1272,7 +1270,7 @@ int PlayerSAO::punch(v3f dir,
// No effect if PvP disabled
if (!g_settings->getBool("enable_pvp")) {
if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
std::string str = gob_cmd_punched(0, getHP());
std::string str = gob_cmd_punched(getHP());
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push(aom);
Expand All @@ -1295,11 +1293,11 @@ int PlayerSAO::punch(v3f dir,
hitparams.hp);

if (!damage_handled) {
setHP(getHP() - hitparams.hp,
setHP((s32)getHP() - (s32)hitparams.hp,
PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
} else { // override client prediction
if (puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
std::string str = gob_cmd_punched(0, getHP());
std::string str = gob_cmd_punched(getHP());
// create message and add to list
ActiveObjectMessage aom(getId(), true, str);
m_messages_out.push(aom);
Expand All @@ -1319,36 +1317,21 @@ int PlayerSAO::punch(v3f dir,
return hitparams.wear;
}

s16 PlayerSAO::readDamage()
{
s16 damage = m_damage;
m_damage = 0;
return damage;
}

void PlayerSAO::setHP(s16 hp, const PlayerHPChangeReason &reason)
void PlayerSAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
{
s16 oldhp = m_hp;
s32 oldhp = m_hp;

s16 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp, reason);
s32 hp_change = m_env->getScriptIface()->on_player_hpchange(this, hp - oldhp, reason);
if (hp_change == 0)
return;
hp = oldhp + hp_change;

if (hp < 0)
hp = 0;
else if (hp > m_prop.hp_max)
hp = m_prop.hp_max;
hp = rangelim(oldhp + hp_change, 0, m_prop.hp_max);

if (hp < oldhp && !g_settings->getBool("enable_damage")) {
if (hp < oldhp && !g_settings->getBool("enable_damage"))
return;
}

m_hp = hp;

if (oldhp > hp)
m_damage += (oldhp - hp);

// Update properties on death
if ((hp == 0) != (oldhp == 0))
m_properties_sent = false;
Expand Down
13 changes: 6 additions & 7 deletions src/content_sao.h
Expand Up @@ -39,7 +39,7 @@ class UnitSAO: public ServerActiveObject
// Deprecated
f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }

s16 getHP() const { return m_hp; }
u16 getHP() const { return m_hp; }
// Use a function, if isDead can be defined by other conditions
bool isDead() const { return m_hp == 0; }

Expand All @@ -64,7 +64,7 @@ class UnitSAO: public ServerActiveObject
ObjectProperties* accessObjectProperties();
void notifyObjectPropertiesModified();
protected:
s16 m_hp = -1;
u16 m_hp = 1;

v3f m_rotation;

Expand Down Expand Up @@ -127,8 +127,8 @@ class LuaEntitySAO : public UnitSAO
void moveTo(v3f pos, bool continuous);
float getMinimumSavedMovement();
std::string getDescription();
void setHP(s16 hp, const PlayerHPChangeReason &reason);
s16 getHP() const;
void setHP(s32 hp, const PlayerHPChangeReason &reason);
u16 getHP() const;
/* LuaEntitySAO-specific */
void setVelocity(v3f velocity);
void addVelocity(v3f velocity)
Expand Down Expand Up @@ -258,8 +258,8 @@ class PlayerSAO : public UnitSAO
ServerActiveObject *puncher,
float time_from_last_punch);
void rightClick(ServerActiveObject *clicker) {}
void setHP(s16 hp, const PlayerHPChangeReason &reason);
void setHPRaw(s16 hp) { m_hp = hp; }
void setHP(s32 hp, const PlayerHPChangeReason &reason);
void setHPRaw(u16 hp) { m_hp = hp; }
s16 readDamage();
u16 getBreath() const { return m_breath; }
void setBreath(const u16 breath, bool send = true);
Expand Down Expand Up @@ -351,7 +351,6 @@ class PlayerSAO : public UnitSAO
RemotePlayer *m_player = nullptr;
session_t m_peer_id = 0;
Inventory *m_inventory = nullptr;
s16 m_damage = 0;

// Cheat prevention
LagPool m_dig_pool;
Expand Down
4 changes: 2 additions & 2 deletions src/database/database-files.cpp
Expand Up @@ -44,11 +44,11 @@ void PlayerDatabaseFiles::serialize(std::ostringstream &os, RemotePlayer *player
args.set("name", player->getName());

sanity_check(player->getPlayerSAO());
args.setS32("hp", player->getPlayerSAO()->getHP());
args.setU16("hp", player->getPlayerSAO()->getHP());
args.setV3F("position", player->getPlayerSAO()->getBasePosition());
args.setFloat("pitch", player->getPlayerSAO()->getLookPitch());
args.setFloat("yaw", player->getPlayerSAO()->getRotation().Y);
args.setS32("breath", player->getPlayerSAO()->getBreath());
args.setU16("breath", player->getPlayerSAO()->getBreath());

std::string extended_attrs;
player->serializeExtraAttributes(extended_attrs);
Expand Down
2 changes: 1 addition & 1 deletion src/database/database-postgresql.cpp
Expand Up @@ -553,7 +553,7 @@ bool PlayerDatabasePostgreSQL::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
pg_to_float(results, 0, 3),
pg_to_float(results, 0, 4))
);
sao->setHPRaw((s16) pg_to_int(results, 0, 5));
sao->setHPRaw((u16) pg_to_int(results, 0, 5));
sao->setBreath((u16) pg_to_int(results, 0, 6), false);

PQclear(results);
Expand Down
2 changes: 1 addition & 1 deletion src/database/database-sqlite3.cpp
Expand Up @@ -546,7 +546,7 @@ bool PlayerDatabaseSQLite3::loadPlayer(RemotePlayer *player, PlayerSAO *sao)
sao->setLookPitch(sqlite_to_float(m_stmt_player_load, 0));
sao->setPlayerYaw(sqlite_to_float(m_stmt_player_load, 1));
sao->setBasePosition(sqlite_to_v3f(m_stmt_player_load, 2));
sao->setHPRaw((s16) MYMIN(sqlite_to_int(m_stmt_player_load, 5), S16_MAX));
sao->setHPRaw((u16) MYMIN(sqlite_to_int(m_stmt_player_load, 5), U16_MAX));
sao->setBreath((u16) MYMIN(sqlite_to_int(m_stmt_player_load, 6), U16_MAX), false);
sqlite3_reset(m_stmt_player_load);

Expand Down

0 comments on commit ffb17f1

Please sign in to comment.