Skip to content

Commit

Permalink
No damage effects on hp_max change (minetest#11846)
Browse files Browse the repository at this point in the history
  • Loading branch information
appgurueu committed Jun 11, 2022
1 parent 3eafcab commit f4a53f7
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 11 deletions.
1 change: 1 addition & 0 deletions src/client/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,7 @@ void Client::step(float dtime)
ClientEvent *event = new ClientEvent();
event->type = CE_PLAYER_DAMAGE;
event->player_damage.amount = damage;
event->player_damage.effect = true;
m_client_event_queue.push(event);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/client/clientevent.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ struct ClientEvent
struct
{
u16 amount;
bool effect;
} player_damage;
struct
{
Expand Down
3 changes: 3 additions & 0 deletions src/client/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2605,6 +2605,9 @@ void Game::handleClientEvent_PlayerDamage(ClientEvent *event, CameraOrientation
if (client->modsLoaded())
client->getScript()->on_damage_taken(event->player_damage.amount);

if (!event->player_damage.effect)
return;

// Damage flash and hurt tilt are not used at death
if (client->getHP() > 0) {
LocalPlayer *player = client->getEnv().getLocalPlayer();
Expand Down
5 changes: 5 additions & 0 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -570,6 +570,10 @@ void Client::handleCommand_HP(NetworkPacket *pkt)

u16 hp;
*pkt >> hp;
bool damage_effect = true;
try {
*pkt >> damage_effect;
} catch (PacketError &e) {};

player->hp = hp;

Expand All @@ -581,6 +585,7 @@ void Client::handleCommand_HP(NetworkPacket *pkt)
ClientEvent *event = new ClientEvent();
event->type = CE_PLAYER_DAMAGE;
event->player_damage.amount = oldhp - hp;
event->player_damage.effect = damage_effect;
m_client_event_queue.push(event);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/script/common/c_content.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ void read_object_properties(lua_State *L, int index,
prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);

if (prop->hp_max < sao->getHP()) {
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP);
PlayerHPChangeReason reason(PlayerHPChangeReason::SET_HP_MAX);
sao->setHP(prop->hp_max, reason);
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
SendInventory(playersao, false);

// Send HP
SendPlayerHP(playersao);
SendPlayerHP(playersao, false);

// Send death screen
if (playersao->isDead())
Expand Down Expand Up @@ -1374,7 +1374,7 @@ void Server::SendMovement(session_t peer_id)
void Server::HandlePlayerHPChange(PlayerSAO *playersao, const PlayerHPChangeReason &reason)
{
m_script->player_event(playersao, "health_changed");
SendPlayerHP(playersao);
SendPlayerHP(playersao, reason.type != PlayerHPChangeReason::SET_HP_MAX);

// Send to other clients
playersao->sendPunchCommand();
Expand All @@ -1383,15 +1383,15 @@ void Server::HandlePlayerHPChange(PlayerSAO *playersao, const PlayerHPChangeReas
HandlePlayerDeath(playersao, reason);
}

void Server::SendPlayerHP(PlayerSAO *playersao)
void Server::SendPlayerHP(PlayerSAO *playersao, bool effect)
{
SendHP(playersao->getPeerID(), playersao->getHP());
SendHP(playersao->getPeerID(), playersao->getHP(), effect);
}

void Server::SendHP(session_t peer_id, u16 hp)
void Server::SendHP(session_t peer_id, u16 hp, bool effect)
{
NetworkPacket pkt(TOCLIENT_HP, 1, peer_id);
pkt << hp;
NetworkPacket pkt(TOCLIENT_HP, 3, peer_id);
pkt << hp << effect;
Send(&pkt);
}

Expand Down
4 changes: 2 additions & 2 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
void printToConsoleOnly(const std::string &text);

void HandlePlayerHPChange(PlayerSAO *sao, const PlayerHPChangeReason &reason);
void SendPlayerHP(PlayerSAO *sao);
void SendPlayerHP(PlayerSAO *sao, bool effect);
void SendPlayerBreath(PlayerSAO *sao);
void SendInventory(PlayerSAO *playerSAO, bool incremental);
void SendMovePlayer(session_t peer_id);
Expand Down Expand Up @@ -439,7 +439,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
void init();

void SendMovement(session_t peer_id);
void SendHP(session_t peer_id, u16 hp);
void SendHP(session_t peer_id, u16 hp, bool effect);
void SendBreath(session_t peer_id, u16 breath);
void SendAccessDenied(session_t peer_id, AccessDeniedCode reason,
const std::string &custom_reason, bool reconnect = false);
Expand Down
2 changes: 1 addition & 1 deletion src/server/player_sao.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ void PlayerSAO::setHP(s32 target_hp, const PlayerHPChangeReason &reason, bool fr
m_hp = hp;
m_env->getGameDef()->HandlePlayerHPChange(this, reason);
} else if (from_client)
m_env->getGameDef()->SendPlayerHP(this);
m_env->getGameDef()->SendPlayerHP(this, true);
}

void PlayerSAO::setBreath(const u16 breath, bool send)
Expand Down
2 changes: 2 additions & 0 deletions src/server/player_sao.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,7 @@ struct PlayerHPChangeReason
enum Type : u8
{
SET_HP,
SET_HP_MAX, // internal type to allow distinguishing hp reset and damage (for effects)
PLAYER_PUNCH,
FALL,
NODE_DAMAGE,
Expand Down Expand Up @@ -277,6 +278,7 @@ struct PlayerHPChangeReason
{
switch (type) {
case PlayerHPChangeReason::SET_HP:
case PlayerHPChangeReason::SET_HP_MAX:
return "set_hp";
case PlayerHPChangeReason::PLAYER_PUNCH:
return "punch";
Expand Down

0 comments on commit f4a53f7

Please sign in to comment.