Skip to content

Commit 64ff966

Browse files
committedMar 3, 2015
Send Player HP when setHP (or a setHP caller) is called instead of looping and testing the state change.
1 parent 056e8f7 commit 64ff966

File tree

6 files changed

+54
-42
lines changed

6 files changed

+54
-42
lines changed
 

‎src/content_sao.cpp

+5-15
Original file line numberDiff line numberDiff line change
@@ -718,7 +718,6 @@ PlayerSAO::PlayerSAO(ServerEnvironment *env_, Player *player_, u16 peer_id_,
718718
// public
719719
m_moved(false),
720720
m_inventory_not_sent(false),
721-
m_hp_not_sent(false),
722721
m_breath_not_sent(false),
723722
m_wielded_item_not_sent(false),
724723
m_physics_override_speed(1),
@@ -1081,27 +1080,18 @@ void PlayerSAO::setHP(s16 hp)
10811080
{
10821081
s16 oldhp = m_player->hp;
10831082

1084-
if(hp < 0)
1083+
if (hp < 0)
10851084
hp = 0;
1086-
else if(hp > PLAYER_MAX_HP)
1085+
else if (hp > PLAYER_MAX_HP)
10871086
hp = PLAYER_MAX_HP;
10881087

1089-
if(hp < oldhp && g_settings->getBool("enable_damage") == false)
1090-
{
1091-
m_hp_not_sent = true; // fix wrong prediction on client
1092-
return;
1093-
}
1094-
10951088
m_player->hp = hp;
10961089

1097-
if(hp != oldhp) {
1098-
m_hp_not_sent = true;
1099-
if(oldhp > hp)
1100-
m_damage += oldhp - hp;
1101-
}
1090+
if (oldhp > hp)
1091+
m_damage += (oldhp - hp);
11021092

11031093
// Update properties on death
1104-
if((hp == 0) != (oldhp == 0))
1094+
if ((hp == 0) != (oldhp == 0))
11051095
m_properties_sent = false;
11061096
}
11071097

‎src/content_sao.h

-1
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,6 @@ class PlayerSAO : public ServerActiveObject
318318
// Some flags used by Server
319319
bool m_moved;
320320
bool m_inventory_not_sent;
321-
bool m_hp_not_sent;
322321
bool m_breath_not_sent;
323322
bool m_wielded_item_not_sent;
324323

‎src/network/packethandlers/server.cpp

+19-8
Original file line numberDiff line numberDiff line change
@@ -893,12 +893,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt)
893893
<< std::endl;
894894

895895
playersao->setHP(playersao->getHP() - damage);
896-
897-
if (playersao->getHP() == 0 && playersao->m_hp_not_sent)
898-
DiePlayer(pkt->getPeerId());
899-
900-
if (playersao->m_hp_not_sent)
901-
SendPlayerHP(pkt->getPeerId());
896+
SendPlayerHPOrDie(playersao->getPeerID(), playersao->getHP() == 0);
902897
}
903898
}
904899

@@ -1048,8 +1043,8 @@ void Server::handleCommand_Respawn(NetworkPacket* pkt)
10481043

10491044
RespawnPlayer(pkt->getPeerId());
10501045

1051-
actionstream<<player->getName()<<" respawns at "
1052-
<<PP(player->getPosition()/BS)<<std::endl;
1046+
actionstream << player->getName() << " respawns at "
1047+
<< PP(player->getPosition()/BS) << std::endl;
10531048

10541049
// ActiveObject is added to environment in AsyncRunStep after
10551050
// the previous addition has been succesfully removed
@@ -1234,8 +1229,24 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
12341229
).normalize();
12351230
float time_from_last_punch =
12361231
playersao->resetTimeFromLastPunch();
1232+
1233+
s16 src_original_hp = pointed_object->getHP();
1234+
s16 dst_origin_hp = playersao->getHP();
1235+
12371236
pointed_object->punch(dir, &toolcap, playersao,
12381237
time_from_last_punch);
1238+
1239+
// If the object is a player and its HP changed
1240+
if (src_original_hp != pointed_object->getHP() &&
1241+
pointed_object->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
1242+
SendPlayerHPOrDie(((PlayerSAO*)pointed_object)->getPeerID(),
1243+
pointed_object->getHP() == 0);
1244+
}
1245+
1246+
// If the puncher is a player and its HP changed
1247+
if (dst_origin_hp != playersao->getHP()) {
1248+
SendPlayerHPOrDie(playersao->getPeerID(), playersao->getHP() == 0);
1249+
}
12391250
}
12401251

12411252
} // action == 0

‎src/script/lua_api/l_object.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -208,8 +208,26 @@ int ObjectRef::l_punch(lua_State *L)
208208
time_from_last_punch = lua_tonumber(L, 3);
209209
ToolCapabilities toolcap = read_tool_capabilities(L, 4);
210210
dir.normalize();
211+
212+
s16 src_original_hp = co->getHP();
213+
s16 dst_origin_hp = puncher->getHP();
214+
211215
// Do it
212216
co->punch(dir, &toolcap, puncher, time_from_last_punch);
217+
218+
// If the punched is a player, and its HP changed
219+
if (src_original_hp != co->getHP() &&
220+
co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
221+
getServer(L)->SendPlayerHPOrDie(((PlayerSAO*)co)->getPeerID(),
222+
co->getHP() == 0);
223+
}
224+
225+
// If the puncher is a player, and its HP changed
226+
if (dst_origin_hp != puncher->getHP() &&
227+
puncher->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
228+
getServer(L)->SendPlayerHPOrDie(((PlayerSAO*)puncher)->getPeerID(),
229+
puncher->getHP() == 0);
230+
}
213231
return 0;
214232
}
215233

@@ -243,6 +261,9 @@ int ObjectRef::l_set_hp(lua_State *L)
243261
<<" hp="<<hp<<std::endl;*/
244262
// Do it
245263
co->setHP(hp);
264+
if (co->getType() == ACTIVEOBJECT_TYPE_PLAYER) {
265+
getServer(L)->SendPlayerHPOrDie(((PlayerSAO*)co)->getPeerID(), co->getHP() == 0);
266+
}
246267
// Return
247268
return 0;
248269
}

‎src/server.cpp

+8-18
Original file line numberDiff line numberDiff line change
@@ -604,17 +604,6 @@ void Server::AsyncRunStep(bool initial_step)
604604
if(playersao == NULL)
605605
continue;
606606

607-
/*
608-
Handle player HPs (die if hp=0)
609-
*/
610-
if(playersao->m_hp_not_sent && g_settings->getBool("enable_damage"))
611-
{
612-
if(playersao->getHP() == 0)
613-
DiePlayer(*i);
614-
else
615-
SendPlayerHP(*i);
616-
}
617-
618607
/*
619608
Send player breath if changed
620609
*/
@@ -1889,7 +1878,6 @@ void Server::SendPlayerHP(u16 peer_id)
18891878
DSTACK(__FUNCTION_NAME);
18901879
PlayerSAO *playersao = getPlayerSAO(peer_id);
18911880
assert(playersao);
1892-
playersao->m_hp_not_sent = false;
18931881
SendHP(peer_id, playersao->getHP());
18941882
m_script->player_event(playersao,"health_changed");
18951883

@@ -2588,9 +2576,9 @@ void Server::DiePlayer(u16 peer_id)
25882576
PlayerSAO *playersao = getPlayerSAO(peer_id);
25892577
assert(playersao);
25902578

2591-
infostream<<"Server::DiePlayer(): Player "
2592-
<<playersao->getPlayer()->getName()
2593-
<<" dies"<<std::endl;
2579+
infostream << "Server::DiePlayer(): Player "
2580+
<< playersao->getPlayer()->getName()
2581+
<< " dies" << std::endl;
25942582

25952583
playersao->setHP(0);
25962584

@@ -2608,13 +2596,15 @@ void Server::RespawnPlayer(u16 peer_id)
26082596
PlayerSAO *playersao = getPlayerSAO(peer_id);
26092597
assert(playersao);
26102598

2611-
infostream<<"Server::RespawnPlayer(): Player "
2612-
<<playersao->getPlayer()->getName()
2613-
<<" respawns"<<std::endl;
2599+
infostream << "Server::RespawnPlayer(): Player "
2600+
<< playersao->getPlayer()->getName()
2601+
<< " respawns" << std::endl;
26142602

26152603
playersao->setHP(PLAYER_MAX_HP);
26162604
playersao->setBreath(PLAYER_MAX_BREATH);
26172605

2606+
SendPlayerHP(peer_id);
2607+
26182608
bool repositioned = m_script->on_respawnplayer(playersao);
26192609
if(!repositioned){
26202610
v3f pos = findSpawnPos(m_env->getServerMap());

‎src/server.h

+1
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,7 @@ class Server : public con::PeerHandler, public MapEventReceiver,
372372
u8* ser_vers, u16* prot_vers, u8* major, u8* minor, u8* patch,
373373
std::string* vers_string);
374374

375+
void SendPlayerHPOrDie(u16 peer_id, bool die) { die ? DiePlayer(peer_id) : SendPlayerHP(peer_id); }
375376
// Bind address
376377
Address m_bind_addr;
377378

0 commit comments

Comments
 (0)
Please sign in to comment.