Skip to content

Commit a0c8c05

Browse files
committedNov 14, 2017
PlayerSAO damage: Update to cope with variable player heights
Nearby codestyle cleanup.
1 parent 41bc0ef commit a0c8c05

File tree

1 file changed

+45
-37
lines changed

1 file changed

+45
-37
lines changed
 

‎src/content_sao.cpp

+45-37
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
2828
#include "server.h"
2929
#include "scripting_server.h"
3030
#include "genericobject.h"
31+
#include <algorithm>
3132

3233
std::map<u16, ServerActiveObject::Factory> ServerActiveObject::m_types;
3334

@@ -919,9 +920,9 @@ void PlayerSAO::getStaticData(std::string * result) const
919920

920921
void PlayerSAO::step(float dtime, bool send_recommended)
921922
{
922-
if (m_drowning_interval.step(dtime, 2.0)) {
923-
// get head position
924-
v3s16 p = floatToInt(m_base_position + v3f(0, BS * 1.6, 0), BS);
923+
if (m_drowning_interval.step(dtime, 2.0f)) {
924+
// Get nose/mouth position, approximate with eye position
925+
v3s16 p = floatToInt(getEyePosition(), BS);
925926
MapNode n = m_env->getMap().getNodeNoEx(p);
926927
const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
927928
// If node generates drown
@@ -937,32 +938,37 @@ void PlayerSAO::step(float dtime, bool send_recommended)
937938
}
938939
}
939940

940-
if (m_breathing_interval.step(dtime, 0.5)) {
941-
// get head position
942-
v3s16 p = floatToInt(m_base_position + v3f(0, BS * 1.6, 0), BS);
941+
if (m_breathing_interval.step(dtime, 0.5f)) {
942+
// Get nose/mouth position, approximate with eye position
943+
v3s16 p = floatToInt(getEyePosition(), BS);
943944
MapNode n = m_env->getMap().getNodeNoEx(p);
944945
const ContentFeatures &c = m_env->getGameDef()->ndef()->get(n);
945-
// If player is alive & no drowning, breath
946+
// If player is alive & no drowning, breathe
946947
if (m_hp > 0 && m_breath < m_prop.breath_max && c.drowning == 0)
947948
setBreath(m_breath + 1);
948949
}
949950

950-
if (m_node_hurt_interval.step(dtime, 1.0)) {
951-
// Feet, middle and head
952-
v3s16 p1 = floatToInt(m_base_position + v3f(0, BS*0.1, 0), BS);
953-
MapNode n1 = m_env->getMap().getNodeNoEx(p1);
954-
v3s16 p2 = floatToInt(m_base_position + v3f(0, BS*0.8, 0), BS);
955-
MapNode n2 = m_env->getMap().getNodeNoEx(p2);
956-
v3s16 p3 = floatToInt(m_base_position + v3f(0, BS*1.6, 0), BS);
957-
MapNode n3 = m_env->getMap().getNodeNoEx(p3);
958-
951+
if (m_node_hurt_interval.step(dtime, 1.0f)) {
959952
u32 damage_per_second = 0;
960-
damage_per_second = MYMAX(damage_per_second,
961-
m_env->getGameDef()->ndef()->get(n1).damage_per_second);
962-
damage_per_second = MYMAX(damage_per_second,
963-
m_env->getGameDef()->ndef()->get(n2).damage_per_second);
964-
damage_per_second = MYMAX(damage_per_second,
965-
m_env->getGameDef()->ndef()->get(n3).damage_per_second);
953+
// Lowest and highest damage points are 0.1 within collisionbox
954+
float dam_top = m_prop.collisionbox.MaxEdge.Y - 0.1f;
955+
956+
// Sequence of damage points, starting 0.1 above feet and progressing
957+
// upwards in 1 node intervals, stopping below top damage point.
958+
for (float dam_height = 0.1f; dam_height < dam_top; dam_height++) {
959+
v3s16 p = floatToInt(m_base_position +
960+
v3f(0.0f, dam_height * BS, 0.0f), BS);
961+
MapNode n = m_env->getMap().getNodeNoEx(p);
962+
damage_per_second = std::max(damage_per_second,
963+
m_env->getGameDef()->ndef()->get(n).damage_per_second);
964+
}
965+
966+
// Top damage point
967+
v3s16 ptop = floatToInt(m_base_position +
968+
v3f(0.0f, dam_top * BS, 0.0f), BS);
969+
MapNode ntop = m_env->getMap().getNodeNoEx(ptop);
970+
damage_per_second = std::max(damage_per_second,
971+
m_env->getGameDef()->ndef()->get(ntop).damage_per_second);
966972

967973
if (damage_per_second != 0 && m_hp > 0) {
968974
s16 newhp = ((s32) damage_per_second > m_hp ? 0 : m_hp - damage_per_second);
@@ -980,21 +986,20 @@ void PlayerSAO::step(float dtime, bool send_recommended)
980986
}
981987

982988
// If attached, check that our parent is still there. If it isn't, detach.
983-
if(m_attachment_parent_id && !isAttached())
984-
{
989+
if (m_attachment_parent_id && !isAttached()) {
985990
m_attachment_parent_id = 0;
986991
m_attachment_bone = "";
987-
m_attachment_position = v3f(0,0,0);
988-
m_attachment_rotation = v3f(0,0,0);
992+
m_attachment_position = v3f(0.0f, 0.0f, 0.0f);
993+
m_attachment_rotation = v3f(0.0f, 0.0f, 0.0f);
989994
setBasePosition(m_last_good_position);
990995
m_env->getGameDef()->SendMovePlayer(m_peer_id);
991996
}
992997

993998
//dstream<<"PlayerSAO::step: dtime: "<<dtime<<std::endl;
994999

9951000
// Set lag pool maximums based on estimated lag
996-
const float LAG_POOL_MIN = 5.0;
997-
float lag_pool_max = m_env->getMaxLagEstimate() * 2.0;
1001+
const float LAG_POOL_MIN = 5.0f;
1002+
float lag_pool_max = m_env->getMaxLagEstimate() * 2.0f;
9981003
if(lag_pool_max < LAG_POOL_MIN)
9991004
lag_pool_max = LAG_POOL_MIN;
10001005
m_dig_pool.setMax(lag_pool_max);
@@ -1007,8 +1012,10 @@ void PlayerSAO::step(float dtime, bool send_recommended)
10071012
m_time_from_last_punch += dtime;
10081013
m_nocheat_dig_time += dtime;
10091014

1010-
// Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
1011-
// If the object gets detached this comes into effect automatically from the last known origin
1015+
// Each frame, parent position is copied if the object is attached,
1016+
// otherwise it's calculated normally.
1017+
// If the object gets detached this comes into effect automatically from
1018+
// the last known origin.
10121019
if (isAttached()) {
10131020
v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
10141021
m_last_good_position = pos;
@@ -1018,20 +1025,21 @@ void PlayerSAO::step(float dtime, bool send_recommended)
10181025
if (!send_recommended)
10191026
return;
10201027

1021-
// If the object is attached client-side, don't waste bandwidth sending its position to clients
1022-
if(m_position_not_sent && !isAttached())
1023-
{
1028+
// If the object is attached client-side, don't waste bandwidth sending its
1029+
// position to clients.
1030+
if (m_position_not_sent && !isAttached()) {
10241031
m_position_not_sent = false;
10251032
float update_interval = m_env->getSendRecommendedInterval();
10261033
v3f pos;
1027-
if(isAttached()) // Just in case we ever do send attachment position too
1034+
if (isAttached()) // Just in case we ever do send attachment position too
10281035
pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
10291036
else
10301037
pos = m_base_position;
1038+
10311039
std::string str = gob_cmd_update_position(
10321040
pos,
1033-
v3f(0,0,0),
1034-
v3f(0,0,0),
1041+
v3f(0.0f, 0.0f, 0.0f),
1042+
v3f(0.0f, 0.0f, 0.0f),
10351043
m_yaw,
10361044
true,
10371045
false,
@@ -1083,7 +1091,7 @@ void PlayerSAO::step(float dtime, bool send_recommended)
10831091
}
10841092
}
10851093

1086-
if (!m_attachment_sent){
1094+
if (!m_attachment_sent) {
10871095
m_attachment_sent = true;
10881096
std::string str = gob_cmd_update_attachment(m_attachment_parent_id,
10891097
m_attachment_bone, m_attachment_position, m_attachment_rotation);

0 commit comments

Comments
 (0)
Please sign in to comment.