52 changes: 22 additions & 30 deletions src/content_sao.h → src/server/luaentity_sao.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Copyright (C) 2013-2020 Minetest core developers & community
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
Expand All @@ -19,38 +20,32 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#pragma once

#include "network/networkprotocol.h"
#include "util/numeric.h"
#include "server/unit_sao.h"
#include "itemgroup.h"
#include "constants.h"

/*
LuaEntitySAO needs some internals exposed.
*/
#include "unit_sao.h"

class LuaEntitySAO : public UnitSAO
{
public:
LuaEntitySAO(ServerEnvironment *env, v3f pos,
const std::string &name, const std::string &state);
LuaEntitySAO() = delete;
// Used by the environment to load SAO
LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &data);
// Used by the Lua API
LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &name,
const std::string &state) :
UnitSAO(env, pos),
m_init_name(name), m_init_state(state)
{
}
~LuaEntitySAO();
ActiveObjectType getType() const
{ return ACTIVEOBJECT_TYPE_LUAENTITY; }
ActiveObjectType getSendType() const
{ return ACTIVEOBJECT_TYPE_GENERIC; }
ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_LUAENTITY; }
ActiveObjectType getSendType() const { return ACTIVEOBJECT_TYPE_GENERIC; }
virtual void addedToEnvironment(u32 dtime_s);
static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
const std::string &data);
void step(float dtime, bool send_recommended);
std::string getClientInitializationData(u16 protocol_version);
bool isStaticAllowed() const
{ return m_prop.static_save; }
bool isStaticAllowed() const { return m_prop.static_save; }
void getStaticData(std::string *result) const;
u16 punch(v3f dir,
const ToolCapabilities *toolcap = nullptr,
ServerActiveObject *puncher = nullptr,
float time_from_last_punch = 1000000.0f);
u16 punch(v3f dir, const ToolCapabilities *toolcap = nullptr,
ServerActiveObject *puncher = nullptr,
float time_from_last_punch = 1000000.0f);
void rightClick(ServerActiveObject *clicker);
void setPos(const v3f &pos);
void moveTo(v3f pos, bool continuous);
Expand All @@ -61,10 +56,7 @@ class LuaEntitySAO : public UnitSAO

/* LuaEntitySAO-specific */
void setVelocity(v3f velocity);
void addVelocity(v3f velocity)
{
m_velocity += velocity;
}
void addVelocity(v3f velocity) { m_velocity += velocity; }
v3f getVelocity();
void setAcceleration(v3f acceleration);
v3f getAcceleration();
Expand All @@ -77,12 +69,13 @@ class LuaEntitySAO : public UnitSAO
bool getCollisionBox(aabb3f *toset) const;
bool getSelectionBox(aabb3f *toset) const;
bool collideWithObjects() const;

private:
std::string getPropertyPacket();
void sendPosition(bool do_interpolate, bool is_movement_end);
std::string generateSetTextureModCommand() const;
static std::string generateSetSpriteCommand(v2s16 p, u16 num_frames, f32 framelength,
bool select_horiz_by_yawpitch);
static std::string generateSetSpriteCommand(v2s16 p, u16 num_frames,
f32 framelength, bool select_horiz_by_yawpitch);

std::string m_init_name;
std::string m_init_state;
Expand All @@ -98,4 +91,3 @@ class LuaEntitySAO : public UnitSAO
float m_last_sent_move_precision = 0.0f;
std::string m_current_texture_modifier = "";
};

83 changes: 29 additions & 54 deletions src/server/player_sao.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

/*
Minetest
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
Expand All @@ -19,6 +18,8 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#pragma once

#include "constants.h"
#include "network/networkprotocol.h"
#include "unit_sao.h"
Expand All @@ -32,33 +33,31 @@ class LagPool
{
float m_pool = 15.0f;
float m_max = 15.0f;

public:
LagPool() = default;

void setMax(float new_max)
{
m_max = new_max;
if(m_pool > new_max)
if (m_pool > new_max)
m_pool = new_max;
}

void add(float dtime)
{
m_pool -= dtime;
if(m_pool < 0)
if (m_pool < 0)
m_pool = 0;
}

void empty()
{
m_pool = m_max;
}
void empty() { m_pool = m_max; }

bool grab(float dtime)
{
if(dtime <= 0)
if (dtime <= 0)
return true;
if(m_pool + dtime > m_max)
if (m_pool + dtime > m_max)
return false;
m_pool += dtime;
return true;
Expand All @@ -73,10 +72,8 @@ class PlayerSAO : public UnitSAO
PlayerSAO(ServerEnvironment *env_, RemotePlayer *player_, session_t peer_id_,
bool is_singleplayer);

ActiveObjectType getType() const
{ return ACTIVEOBJECT_TYPE_PLAYER; }
ActiveObjectType getSendType() const
{ return ACTIVEOBJECT_TYPE_GENERIC; }
ActiveObjectType getType() const { return ACTIVEOBJECT_TYPE_PLAYER; }
ActiveObjectType getSendType() const { return ACTIVEOBJECT_TYPE_GENERIC; }
std::string getDescription();

/*
Expand Down Expand Up @@ -111,10 +108,8 @@ class PlayerSAO : public UnitSAO
Interaction interface
*/

u16 punch(v3f dir,
const ToolCapabilities *toolcap,
ServerActiveObject *puncher,
float time_from_last_punch);
u16 punch(v3f dir, const ToolCapabilities *toolcap, ServerActiveObject *puncher,
float time_from_last_punch);
void rightClick(ServerActiveObject *clicker) {}
void setHP(s32 hp, const PlayerHPChangeReason &reason);
void setHPRaw(u16 hp) { m_hp = hp; }
Expand Down Expand Up @@ -144,10 +139,7 @@ class PlayerSAO : public UnitSAO

// Cheat prevention

v3f getLastGoodPosition() const
{
return m_last_good_position;
}
v3f getLastGoodPosition() const { return m_last_good_position; }
float resetTimeFromLastPunch()
{
float r = m_time_from_last_punch;
Expand All @@ -159,30 +151,17 @@ class PlayerSAO : public UnitSAO
m_nocheat_dig_pos = p;
m_nocheat_dig_time = 0;
}
v3s16 getNoCheatDigPos()
{
return m_nocheat_dig_pos;
}
float getNoCheatDigTime()
{
return m_nocheat_dig_time;
}
void noCheatDigEnd()
{
m_nocheat_dig_pos = v3s16(32767, 32767, 32767);
}
LagPool& getDigPool()
{
return m_dig_pool;
}
v3s16 getNoCheatDigPos() { return m_nocheat_dig_pos; }
float getNoCheatDigTime() { return m_nocheat_dig_time; }
void noCheatDigEnd() { m_nocheat_dig_pos = v3s16(32767, 32767, 32767); }
LagPool &getDigPool() { return m_dig_pool; }
void setMaxSpeedOverride(const v3f &vel);
// Returns true if cheated
bool checkMovementCheat();

// Other

void updatePrivileges(const std::set<std::string> &privs,
bool is_singleplayer)
void updatePrivileges(const std::set<std::string> &privs, bool is_singleplayer)
{
m_privs = privs;
m_is_singleplayer = is_singleplayer;
Expand Down Expand Up @@ -236,6 +215,7 @@ class PlayerSAO : public UnitSAO
s16 m_wanted_range = 0.0f;

Metadata m_meta;

public:
float m_physics_override_speed = 1.0f;
float m_physics_override_jump = 1.0f;
Expand All @@ -246,9 +226,10 @@ class PlayerSAO : public UnitSAO
bool m_physics_override_sent = false;
};


struct PlayerHPChangeReason {
enum Type : u8 {
struct PlayerHPChangeReason
{
enum Type : u8
{
SET_HP,
PLAYER_PUNCH,
FALL,
Expand All @@ -266,10 +247,7 @@ struct PlayerHPChangeReason {
// For NODE_DAMAGE
std::string node;

inline bool hasLuaReference() const
{
return lua_reference >= 0;
}
inline bool hasLuaReference() const { return lua_reference >= 0; }

bool setTypeFromString(const std::string &typestr)
{
Expand Down Expand Up @@ -311,15 +289,12 @@ struct PlayerHPChangeReason {
}
}

PlayerHPChangeReason(Type type):
type(type)
{}
PlayerHPChangeReason(Type type) : type(type) {}

PlayerHPChangeReason(Type type, ServerActiveObject *object):
PlayerHPChangeReason(Type type, ServerActiveObject *object) :
type(type), object(object)
{}
{
}

PlayerHPChangeReason(Type type, std::string node):
type(type), node(node)
{}
PlayerHPChangeReason(Type type, std::string node) : type(type), node(node) {}
};
33 changes: 0 additions & 33 deletions src/server/serveractiveobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,6 @@ ServerActiveObject::ServerActiveObject(ServerEnvironment *env, v3f pos):
{
}

ServerActiveObject* ServerActiveObject::create(ActiveObjectType type,
ServerEnvironment *env, u16 id, v3f pos,
const std::string &data)
{
// Find factory function
std::map<u16, Factory>::iterator n;
n = m_types.find(type);
if(n == m_types.end()) {
// These are 0.3 entity types, return without error.
if (ACTIVEOBJECT_TYPE_ITEM <= type && type <= ACTIVEOBJECT_TYPE_MOBV2) {
return NULL;
}

// If factory is not found, just return.
warningstream<<"ServerActiveObject: No factory for type="
<<type<<std::endl;
return NULL;
}

Factory f = n->second;
ServerActiveObject *object = (*f)(env, pos, data);
return object;
}

void ServerActiveObject::registerType(u16 type, Factory f)
{
std::map<u16, Factory>::iterator n;
n = m_types.find(type);
if(n != m_types.end())
return;
m_types[type] = f;
}

float ServerActiveObject::getMinimumSavedMovement()
{
return 2.0*BS;
Expand Down
10 changes: 0 additions & 10 deletions src/server/serveractiveobject.h
Original file line number Diff line number Diff line change
Expand Up @@ -244,12 +244,6 @@ class ServerActiveObject : public ActiveObject
virtual void onAttach(int parent_id) {}
virtual void onDetach(int parent_id) {}

// Used for creating objects based on type
typedef ServerActiveObject* (*Factory)
(ServerEnvironment *env, v3f pos,
const std::string &data);
static void registerType(u16 type, Factory f);

ServerEnvironment *m_env;
v3f m_base_position;
std::unordered_set<u32> m_attached_particle_spawners;
Expand All @@ -258,8 +252,4 @@ class ServerActiveObject : public ActiveObject
Queue of messages to be sent to the client
*/
std::queue<ActiveObjectMessage> m_messages_out;

private:
// Used for creating objects based on type
static std::map<u16, Factory> m_types;
};
19 changes: 16 additions & 3 deletions src/serverenvironment.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#if USE_POSTGRESQL
#include "database/database-postgresql.h"
#endif
#include "server/luaentity_sao.h"
#include "server/player_sao.h"

#define LBM_NAME_ALLOWED_CHARS "abcdefghijklmnopqrstuvwxyz0123456789_:"
Expand Down Expand Up @@ -1778,6 +1779,18 @@ static void print_hexdump(std::ostream &o, const std::string &data)
}
}

ServerActiveObject* ServerEnvironment::createSAO(ActiveObjectType type, v3f pos,
const std::string &data)
{
switch (type) {
case ACTIVEOBJECT_TYPE_LUAENTITY:
return new LuaEntitySAO(this, pos, data);
default:
warningstream << "ServerActiveObject: No factory for type=" << type << std::endl;
}
return nullptr;
}

/*
Convert stored objects from blocks near the players to active.
*/
Expand Down Expand Up @@ -1811,10 +1824,10 @@ void ServerEnvironment::activateObjects(MapBlock *block, u32 dtime_s)
std::vector<StaticObject> new_stored;
for (const StaticObject &s_obj : block->m_static_objects.m_stored) {
// Create an active object from the data
ServerActiveObject *obj = ServerActiveObject::create
((ActiveObjectType) s_obj.type, this, 0, s_obj.pos, s_obj.data);
ServerActiveObject *obj = createSAO((ActiveObjectType) s_obj.type, s_obj.pos,
s_obj.data);
// If couldn't create object, store static data back.
if(obj == NULL) {
if (!obj) {
errorstream<<"ServerEnvironment::activateObjects(): "
<<"failed to create active object from static object "
<<"in block "<<PP(s_obj.pos/BS)
Expand Down
2 changes: 2 additions & 0 deletions src/serverenvironment.h
Original file line number Diff line number Diff line change
Expand Up @@ -468,4 +468,6 @@ class ServerEnvironment : public Environment
IntervalLimiter m_particle_management_interval;
std::unordered_map<u32, float> m_particle_spawners;
std::unordered_map<u32, u16> m_particle_spawner_attachments;

ServerActiveObject* createSAO(ActiveObjectType type, v3f pos, const std::string &data);
};
2 changes: 1 addition & 1 deletion src/staticobject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "staticobject.h"
#include "util/serialize.h"
#include "content_sao.h"
#include "server/serveractiveobject.h"

StaticObject::StaticObject(const ServerActiveObject *s_obj, const v3f &pos_):
type(s_obj->getType()),
Expand Down
1 change: 0 additions & 1 deletion src/unittest/test_player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "exceptions.h"
#include "remoteplayer.h"
#include "content_sao.h"
#include "server.h"

class TestPlayer : public TestBase
Expand Down
4 changes: 2 additions & 2 deletions util/travis/clang-format-whitelist.txt
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,6 @@ src/content_mapblock.h
src/content_mapnode.cpp
src/content_nodemeta.cpp
src/content_nodemeta.h
src/content_sao.cpp
src/content_sao.h
src/convert_json.cpp
src/convert_json.h
src/craftdef.cpp
Expand Down Expand Up @@ -408,6 +406,8 @@ src/serverenvironment.h
src/server.h
src/serverlist.cpp
src/serverlist.h
src/server/luaentity_sao.cpp
src/server/player_sao.cpp
src/server/serveractiveobject.cpp
src/server/serveractiveobject.h
src/settings.cpp
Expand Down