15 changes: 1 addition & 14 deletions src/genericobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,11 @@ std::string gob_cmd_set_sprite(
return os.str();
}

std::string gob_cmd_punched(s16 damage, s16 result_hp)
std::string gob_cmd_punched(u16 result_hp)
{
std::ostringstream os(std::ios::binary);
// command
writeU8(os, GENERIC_CMD_PUNCHED);
// damage
writeS16(os, damage);
// result_hp
writeS16(os, result_hp);
return os.str();
Expand Down Expand Up @@ -184,17 +182,6 @@ std::string gob_cmd_update_attachment(int parent_id, const std::string &bone,
return os.str();
}

std::string gob_cmd_update_nametag_attributes(video::SColor color)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deprecated, see content_sao.cpp

{
std::ostringstream os(std::ios::binary);
// command
writeU8(os, GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES);
// parameters
writeU8(os, 1); // version for forward compatibility
writeARGB8(os, color);
return os.str();
}

std::string gob_cmd_update_infant(u16 id, u8 type,
const std::string &client_initialization_data)
{
Expand Down
7 changes: 2 additions & 5 deletions src/genericobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ enum GenericCMD {
GENERIC_CMD_SET_BONE_POSITION,
GENERIC_CMD_ATTACH_TO,
GENERIC_CMD_SET_PHYSICS_OVERRIDE,
GENERIC_CMD_UPDATE_NAMETAG_ATTRIBUTES,
GENERIC_CMD_SPAWN_INFANT,
GENERIC_CMD_SPAWN_INFANT = 11,
GENERIC_CMD_SET_ANIMATION_SPEED
};

Expand All @@ -63,7 +62,7 @@ std::string gob_cmd_set_sprite(
bool select_horiz_by_yawpitch
);

std::string gob_cmd_punched(s16 damage, s16 result_hp);
std::string gob_cmd_punched(u16 result_hp);

std::string gob_cmd_update_armor_groups(const ItemGroupList &armor_groups);

Expand All @@ -81,7 +80,5 @@ std::string gob_cmd_update_bone_position(const std::string &bone, v3f position,
std::string gob_cmd_update_attachment(int parent_id, const std::string &bone,
v3f position, v3f rotation);

std::string gob_cmd_update_nametag_attributes(video::SColor color);

std::string gob_cmd_update_infant(u16 id, u8 type,
const std::string &client_initialization_data);
2 changes: 1 addition & 1 deletion src/network/clientopcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ const ToClientCommandHandler toClientCommandTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_ACTIVE_OBJECT_MESSAGES", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_ActiveObjectMessages }, // 0x32
{ "TOCLIENT_HP", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_HP }, // 0x33
{ "TOCLIENT_MOVE_PLAYER", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_MovePlayer }, // 0x34
{ "TOCLIENT_ACCESS_DENIED_LEGACY", TOCLIENT_STATE_NOT_CONNECTED, &Client::handleCommand_AccessDenied }, // 0x35
null_command_handler,
null_command_handler,
{ "TOCLIENT_DEATHSCREEN", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_DeathScreen }, // 0x37
{ "TOCLIENT_MEDIA", TOCLIENT_STATE_CONNECTED, &Client::handleCommand_Media }, // 0x38
Expand Down
68 changes: 23 additions & 45 deletions src/network/clientpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -927,21 +927,19 @@ void Client::handleCommand_SpawnParticle(NetworkPacket* pkt)
float size = readF32(is);
bool collisiondetection = readU8(is);
std::string texture = deSerializeLongString(is);
bool vertical = readU8(is);
bool collision_removal = readU8(is);
bool object_collision = false;

bool vertical = false;
bool collision_removal = false;
TileAnimationParams animation;
animation.type = TAT_NONE;
u8 glow = 0;
bool object_collision = false;
animation.deSerialize(is, m_proto_ver);

u8 glow = readU8(is);
try {
vertical = readU8(is);
collision_removal = readU8(is);
animation.deSerialize(is, m_proto_ver);
glow = readU8(is);
object_collision = readU8(is);
} catch (...) {}


ClientEvent *event = new ClientEvent();
event->type = CE_SPAWN_PARTICLE;
event->spawn_particle.pos = new v3f (pos);
Expand All @@ -964,46 +962,37 @@ void Client::handleCommand_AddParticleSpawner(NetworkPacket* pkt)
{
u16 amount;
float spawntime;
v3f minpos;
v3f maxpos;
v3f minvel;
v3f maxvel;
v3f minacc;
v3f maxacc;
float minexptime;
float maxexptime;
float minsize;
float maxsize;
v3f minpos, maxpos;
v3f minvel, maxvel;
v3f minacc, maxacc;
float minexptime, maxexptime;
float minsize, maxsize;
bool collisiondetection;
u32 server_id;
bool vertical;
bool collision_removal;
u16 attached_id;
u8 glow;
bool object_collision;

*pkt >> amount >> spawntime >> minpos >> maxpos >> minvel >> maxvel
>> minacc >> maxacc >> minexptime >> maxexptime >> minsize
>> maxsize >> collisiondetection;

std::string texture = pkt->readLongString();

*pkt >> server_id;
*pkt >> server_id >> vertical >> collision_removal >> attached_id;

bool vertical = false;
bool collision_removal = false;
u16 attached_id = 0;
TileAnimationParams animation;
animation.type = TAT_NONE;
u8 glow = 0;
bool object_collision = false;
try {
*pkt >> vertical;
*pkt >> collision_removal;
*pkt >> attached_id;

{
// This is horrible but required (why are there two ways to deserialize pkts?)
std::string datastring(pkt->getRemainingString(), pkt->getRemainingBytes());
std::istringstream is(datastring, std::ios_base::binary);
animation.deSerialize(is, m_proto_ver);

glow = readU8(is);
object_collision = readU8(is);
} catch (...) {}
}

u32 client_id = m_particle_manager.getSpawnerId();
m_particles_server_to_client[server_id] = client_id;
Expand Down Expand Up @@ -1075,15 +1064,7 @@ void Client::handleCommand_HudAdd(NetworkPacket* pkt)
v2s32 size;

*pkt >> server_id >> type >> pos >> name >> scale >> text >> number >> item
>> dir >> align >> offset;
try {
*pkt >> world_pos;
}
catch(SerializationError &e) {};

try {
*pkt >> size;
} catch(SerializationError &e) {};
>> dir >> align >> offset >> world_pos >> size;

ClientEvent *event = new ClientEvent();
event->type = CE_HUDADD;
Expand Down Expand Up @@ -1236,10 +1217,7 @@ void Client::handleCommand_HudSetSky(NetworkPacket* pkt)
for (size_t i = 0; i < count; i++)
params->push_back(deSerializeString(is));

bool clouds = true;
try {
clouds = readU8(is);
} catch (...) {}
bool clouds = readU8(is);

ClientEvent *event = new ClientEvent();
event->type = CE_SET_SKY;
Expand Down
8 changes: 2 additions & 6 deletions src/network/networkprotocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,7 @@ enum ToClientCommand
f1000 player yaw
*/

TOCLIENT_ACCESS_DENIED_LEGACY = 0x35,
/*
u16 reason_length
wstring reason
*/
TOCLIENT_ACCESS_DENIED_LEGACY = 0x35, // Obsolete

TOCLIENT_PLAYERITEM = 0x36, // Obsolete

Expand Down Expand Up @@ -771,7 +767,7 @@ enum ToServerCommand

TOSERVER_DAMAGE = 0x35,
/*
u8 amount
u16 amount
*/

TOSERVER_PASSWORD_LEGACY = 0x36, // Obsolete
Expand Down
2 changes: 1 addition & 1 deletion src/network/serveropcodes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ const ClientCommandFactory clientCommandFactoryTable[TOCLIENT_NUM_MSG_TYPES] =
{ "TOCLIENT_ACTIVE_OBJECT_MESSAGES", 0, true }, // 0x32 Special packet, sent by 0 (rel) and 1 (unrel) channel
{ "TOCLIENT_HP", 0, true }, // 0x33
{ "TOCLIENT_MOVE_PLAYER", 0, true }, // 0x34
{ "TOCLIENT_ACCESS_DENIED_LEGACY", 0, true }, // 0x35
null_command_factory, // 0x35
null_command_factory, // 0x36
{ "TOCLIENT_DEATHSCREEN", 0, true }, // 0x37
{ "TOCLIENT_MEDIA", 2, true }, // 0x38
Expand Down
4 changes: 2 additions & 2 deletions src/network/serverpackethandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -776,7 +776,7 @@ void Server::handleCommand_ChatMessage(NetworkPacket* pkt)

void Server::handleCommand_Damage(NetworkPacket* pkt)
{
u8 damage;
u16 damage;

*pkt >> damage;

Expand Down Expand Up @@ -812,7 +812,7 @@ void Server::handleCommand_Damage(NetworkPacket* pkt)
<< std::endl;

PlayerHPChangeReason reason(PlayerHPChangeReason::FALL);
playersao->setHP(playersao->getHP() - damage, reason);
playersao->setHP((s32)playersao->getHP() - damage, reason);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

subtraction of signed from unsigned

SendPlayerHPOrDie(playersao, reason);
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/object_properties.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ std::string ObjectProperties::dump()
void ObjectProperties::serialize(std::ostream &os) const
{
writeU8(os, 4); // PROTOCOL_VERSION >= 37
writeS16(os, hp_max);
writeU16(os, hp_max);
writeU8(os, physical);
writeF32(os, weight);
writeV3F32(os, collisionbox.MinEdge);
Expand Down Expand Up @@ -126,7 +126,7 @@ void ObjectProperties::deSerialize(std::istream &is)
if (version != 4)
throw SerializationError("unsupported ObjectProperties version");

hp_max = readS16(is);
hp_max = readU16(is);
physical = readU8(is);
weight = readF32(is);
collisionbox.MinEdge = readV3F32(is);
Expand Down
2 changes: 1 addition & 1 deletion src/object_properties.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

struct ObjectProperties
{
s16 hp_max = 1;
u16 hp_max = 1;
u16 breath_max = 0;
bool physical = false;
bool collideWithObjects = true;
Expand Down
4 changes: 2 additions & 2 deletions src/remoteplayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,

if (sao) {
try {
sao->setHPRaw(args.getS32("hp"));
sao->setHPRaw(args.getU16("hp"));
} catch(SettingNotFoundException &e) {
sao->setHPRaw(PLAYER_MAX_HP_DEFAULT);
}
Expand All @@ -115,7 +115,7 @@ void RemotePlayer::deSerialize(std::istream &is, const std::string &playername,
} catch (SettingNotFoundException &e) {}

try {
sao->setBreath(args.getS32("breath"), false);
sao->setBreath(args.getU16("breath"), false);
} catch (SettingNotFoundException &e) {}

try {
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 @@ -191,7 +191,7 @@ void read_object_properties(lua_State *L, int index,

int hp_max = 0;
if (getintfield(L, -1, "hp_max", hp_max))
prop->hp_max = (s16)rangelim(hp_max, 0, S16_MAX);
prop->hp_max = (u16)rangelim(hp_max, 0, U16_MAX);

getintfield(L, -1, "breath_max", prop->breath_max);
getboolfield(L, -1, "physical", prop->physical);
Expand Down
18 changes: 0 additions & 18 deletions src/script/cpp_api/s_entity.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,24 +169,6 @@ void ScriptApiEntity::luaentity_GetProperties(u16 id,
// Set default values that differ from ObjectProperties defaults
prop->hp_max = 10;

/* Read stuff */
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is read by read_object_properties(L, -1, prop, getServer()->idef());


prop->hp_max = getintfield_default(L, -1, "hp_max", 10);

getboolfield(L, -1, "physical", prop->physical);
getboolfield(L, -1, "collide_with_objects", prop->collideWithObjects);

getfloatfield(L, -1, "weight", prop->weight);

lua_getfield(L, -1, "collisionbox");
if (lua_istable(L, -1))
prop->collisionbox = read_aabb3f(L, -1, 1.0);
lua_pop(L, 1);

getstringfield(L, -1, "visual", prop->visual);

getstringfield(L, -1, "mesh", prop->mesh);

// Deprecated: read object properties directly
read_object_properties(L, -1, prop, getServer()->idef());

Expand Down
4 changes: 2 additions & 2 deletions src/script/cpp_api/s_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ bool ScriptApiPlayer::on_punchplayer(ServerActiveObject *player,
return readParam<bool>(L, -1);
}

s16 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
s16 hp_change, const PlayerHPChangeReason &reason)
s32 ScriptApiPlayer::on_player_hpchange(ServerActiveObject *player,
s32 hp_change, const PlayerHPChangeReason &reason)
{
SCRIPTAPI_PRECHECKHEADER

Expand Down
2 changes: 1 addition & 1 deletion src/script/cpp_api/s_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class ScriptApiPlayer : virtual public ScriptApiBase
bool on_punchplayer(ServerActiveObject *player, ServerActiveObject *hitter,
float time_from_last_punch, const ToolCapabilities *toolcap,
v3f dir, s16 damage);
s16 on_player_hpchange(ServerActiveObject *player, s16 hp_change,
s32 on_player_hpchange(ServerActiveObject *player, s32 hp_change,
const PlayerHPChangeReason &reason);
void on_playerReceiveFields(ServerActiveObject *player,
const std::string &formname, const StringMap &fields);
Expand Down
4 changes: 3 additions & 1 deletion src/script/lua_api/l_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,9 @@ int ModApiServer::l_kick_player(lua_State *L)
lua_pushboolean(L, false); // No such player
return 1;
}
getServer(L)->DenyAccess_Legacy(player->getPeerId(), utf8_to_wide(message));

getServer(L)->DenyAccess(player->getPeerId(),
SERVER_ACCESSDENIED_CUSTOM_STRING, message);
lua_pushboolean(L, true);
return 1;
}
Expand Down
80 changes: 21 additions & 59 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1010,8 +1010,9 @@ void Server::Receive()
<< e.what() << std::endl;
} catch (const ClientStateError &e) {
errorstream << "ProcessData: peer=" << peer_id << e.what() << std::endl;
DenyAccess_Legacy(peer_id, L"Your client sent something server didn't expect."
L"Try reconnecting or updating your client");
DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING,
"Your client sent something server didn't expect."
"Try reconnecting or updating your client");
} catch (const con::PeerNotFoundException &e) {
// Do nothing
}
Expand Down Expand Up @@ -1041,13 +1042,15 @@ PlayerSAO* Server::StageTwoClientInit(session_t peer_id)
if (player && player->getPeerId() != PEER_ID_INEXISTENT) {
actionstream << "Server: Failed to emerge player \"" << playername
<< "\" (player allocated to an another client)" << std::endl;
DenyAccess_Legacy(peer_id, L"Another client is connected with this "
L"name. If your client closed unexpectedly, try again in "
L"a minute.");
DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING,
"Another client is connected with this name. "
"If your client closed unexpectedly, try again in "
"a minute.");
} else {
errorstream << "Server: " << playername << ": Failed to emerge player"
<< std::endl;
DenyAccess_Legacy(peer_id, L"Could not allocate player.");
DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING,
"Could not allocate player.");
}
return NULL;
}
Expand Down Expand Up @@ -1119,9 +1122,9 @@ void Server::ProcessData(NetworkPacket *pkt)
infostream << "Server: A banned client tried to connect from "
<< addr_s << "; banned name was "
<< ban_name << std::endl;
// This actually doesn't seem to transfer to the client
DenyAccess_Legacy(peer_id, L"Your ip is banned. Banned name was "
+ utf8_to_wide(ban_name));

DenyAccess(peer_id, SERVER_ACCESSDENIED_CUSTOM_STRING,
"Your IP is banned. Banned name was " + ban_name);
return;
}
}
Expand Down Expand Up @@ -1468,13 +1471,6 @@ void Server::SendAccessDenied(session_t peer_id, AccessDeniedCode reason,
Send(&pkt);
}

void Server::SendAccessDenied_Legacy(session_t peer_id,const std::wstring &reason)
{
NetworkPacket pkt(TOCLIENT_ACCESS_DENIED_LEGACY, 0, peer_id);
pkt << reason;
Send(&pkt);
}

void Server::SendDeathscreen(session_t peer_id, bool set_camera_point_target,
v3f camera_point_target)
{
Expand Down Expand Up @@ -1828,7 +1824,7 @@ void Server::SendPlayerHP(session_t peer_id)
m_script->player_event(playersao,"health_changed");

// Send to other clients
std::string str = gob_cmd_punched(playersao->readDamage(), playersao->getHP());
std::string str = gob_cmd_punched(playersao->getHP());
ActiveObjectMessage aom(playersao->getId(), true, str);
playersao->m_messages_out.push(aom);
}
Expand Down Expand Up @@ -2018,14 +2014,9 @@ s32 Server::playSound(const SimpleSoundSpec &spec,
<< (u8) params.type << pos << params.object
<< params.loop << params.fade << params.pitch;

// Backwards compability
bool play_sound = gain > 0;

for (const u16 dst_client : dst_clients) {
if (play_sound || m_clients.getProtocolVersion(dst_client) >= 32) {
psound.clients.insert(dst_client);
m_clients.send(dst_client, 0, &pkt, true);
}
psound.clients.insert(dst_client);
m_clients.send(dst_client, 0, &pkt, true);
}
return id;
}
Expand Down Expand Up @@ -2064,36 +2055,16 @@ void Server::fadeSound(s32 handle, float step, float gain)
NetworkPacket pkt(TOCLIENT_FADE_SOUND, 4);
pkt << handle << step << gain;

// Backwards compability
bool play_sound = gain > 0;
ServerPlayingSound compat_psound = psound;
compat_psound.clients.clear();

NetworkPacket compat_pkt(TOCLIENT_STOP_SOUND, 4);
compat_pkt << handle;

for (std::unordered_set<u16>::iterator it = psound.clients.begin();
it != psound.clients.end();) {
if (m_clients.getProtocolVersion(*it) >= 32) {
// Send as reliable
m_clients.send(*it, 0, &pkt, true);
++it;
} else {
compat_psound.clients.insert(*it);
// Stop old sound
m_clients.send(*it, 0, &compat_pkt, true);
psound.clients.erase(it++);
}

for (session_t client_id : psound.clients) {
// Send as reliable
m_clients.send(client_id, 0, &pkt, true);
}

// Remove sound reference
if (!play_sound || psound.clients.empty())
m_playing_sounds.erase(i);

if (play_sound && !compat_psound.clients.empty()) {
// Play new sound volume on older clients
playSound(compat_psound.spec, compat_psound.params);
}
}

void Server::sendRemoveNode(v3s16 p, std::unordered_set<u16> *far_players,
Expand Down Expand Up @@ -2663,15 +2634,6 @@ void Server::DenyAccess(session_t peer_id, AccessDeniedCode reason,
DisconnectPeer(peer_id);
}

// 13/03/15: remove this function when protocol version 25 will become
// the minimum version for MT users, maybe in 1 year
void Server::DenyAccess_Legacy(session_t peer_id, const std::wstring &reason)
{
SendAccessDenied_Legacy(peer_id, reason);
m_clients.event(peer_id, CSE_SetDenied);
DisconnectPeer(peer_id);
}

void Server::DisconnectPeer(session_t peer_id)
{
m_modchannel_mgr->leaveAllChannels(peer_id);
Expand Down Expand Up @@ -2850,8 +2812,8 @@ std::wstring Server::handleChat(const std::string &name, const std::wstring &wna
return ws.str();
}
case RPLAYER_CHATRESULT_KICK:
DenyAccess_Legacy(player->getPeerId(),
L"You have been kicked due to message flooding.");
DenyAccess(player->getPeerId(), SERVER_ACCESSDENIED_CUSTOM_STRING,
"You have been kicked due to message flooding.");
return L"";
case RPLAYER_CHATRESULT_OK:
break;
Expand Down
1 change: 0 additions & 1 deletion src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ class Server : public con::PeerHandler, public MapEventReceiver,
void SendBreath(session_t peer_id, u16 breath);
void SendAccessDenied(session_t peer_id, AccessDeniedCode reason,
const std::string &custom_reason, bool reconnect = false);
void SendAccessDenied_Legacy(session_t peer_id, const std::wstring &reason);
void SendDeathscreen(session_t peer_id, bool set_camera_point_target,
v3f camera_point_target);
void SendItemDef(session_t peer_id, IItemDefManager *itemdef, u16 protocol_version);
Expand Down
6 changes: 2 additions & 4 deletions src/serverobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,8 @@ class ServerActiveObject : public ActiveObject
{ return 0; }
virtual void rightClick(ServerActiveObject *clicker)
{}
virtual void setHP(s16 hp, const PlayerHPChangeReason &reason)
{}
virtual s16 getHP() const
{ return 0; }
virtual void setHP(s32 hp, const PlayerHPChangeReason &reason) {}
virtual u16 getHP() const { return 0; }

virtual void setArmorGroups(const ItemGroupList &armor_groups)
{}
Expand Down