Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Drop content_sao.{cpp,h}
Move LuaEntitySAO to a new dedicated file
Drop TestSAO (useless object)
Drop the old static startup initialized SAO factory, which was pretty useless.
This factory was using a std::map for 2 elements, now just use a simple condition owned by ServerEnvironment, which will be lightweight, that will also drop a one time useful test on each LuaEntitySAO creation. This should reduce server load on massive SAO creation
  • Loading branch information
nerzhul committed Apr 11, 2020
1 parent 894a34a commit 5146086
Show file tree
Hide file tree
Showing 25 changed files with 125 additions and 294 deletions.
4 changes: 3 additions & 1 deletion build/android/jni/Android.mk
Expand Up @@ -161,7 +161,6 @@ LOCAL_SRC_FILES := \
jni/src/content/mods.cpp \
jni/src/content_nodemeta.cpp \
jni/src/content/packages.cpp \
jni/src/content_sao.cpp \
jni/src/content/subgames.cpp \
jni/src/convert_json.cpp \
jni/src/craftdef.cpp \
Expand Down Expand Up @@ -258,8 +257,11 @@ LOCAL_SRC_FILES := \
jni/src/server.cpp \
jni/src/serverenvironment.cpp \
jni/src/serverlist.cpp \
jni/src/server/luaentity_sao.cpp \
jni/src/server/mods.cpp \
jni/src/server/player_sao.cpp \
jni/src/server/serveractiveobject.cpp \
jni/src/server/unit_sao.cpp \
jni/src/settings.cpp \
jni/src/staticobject.cpp \
jni/src/tileanimation.cpp \
Expand Down
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Expand Up @@ -375,7 +375,6 @@ set(common_SRCS
collision.cpp
content_mapnode.cpp
content_nodemeta.cpp
content_sao.cpp
convert_json.cpp
craftdef.cpp
debug.cpp
Expand Down
2 changes: 1 addition & 1 deletion src/clientiface.cpp
Expand Up @@ -19,7 +19,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include <sstream>
#include "clientiface.h"
#include "content_sao.h"
#include "network/connection.h"
#include "network/serveropcodes.h"
#include "remoteplayer.h"
Expand All @@ -28,6 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "serverenvironment.h"
#include "map.h"
#include "emerge.h"
#include "server/luaentity_sao.h"
#include "server/player_sao.h"
#include "log.h"
#include "util/srp.h"
Expand Down
1 change: 0 additions & 1 deletion src/mapgen/mapgen.cpp
Expand Up @@ -28,7 +28,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "mapnode.h"
#include "map.h"
#include "content_sao.h"
#include "nodedef.h"
#include "emerge.h"
#include "voxelalgorithms.h"
Expand Down
1 change: 0 additions & 1 deletion src/mapgen/mapgen_carpathian.cpp
Expand Up @@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "mapnode.h"
#include "map.h"
#include "content_sao.h"
#include "nodedef.h"
#include "voxelalgorithms.h"
//#include "profiler.h" // For TimeTaker
Expand Down
1 change: 0 additions & 1 deletion src/mapgen/mapgen_flat.cpp
Expand Up @@ -25,7 +25,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "mapnode.h"
#include "map.h"
#include "content_sao.h"
#include "nodedef.h"
#include "voxelalgorithms.h"
//#include "profiler.h" // For TimeTaker
Expand Down
1 change: 0 additions & 1 deletion src/mapgen/mapgen_fractal.cpp
Expand Up @@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "mapnode.h"
#include "map.h"
#include "content_sao.h"
#include "nodedef.h"
#include "voxelalgorithms.h"
//#include "profiler.h" // For TimeTaker
Expand Down
1 change: 0 additions & 1 deletion src/mapgen/mapgen_v5.cpp
Expand Up @@ -25,7 +25,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "mapnode.h"
#include "map.h"
#include "content_sao.h"
#include "nodedef.h"
#include "voxelalgorithms.h"
//#include "profiler.h" // For TimeTaker
Expand Down
1 change: 0 additions & 1 deletion src/mapgen/mapgen_v6.cpp
Expand Up @@ -27,7 +27,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "mapnode.h"
#include "map.h"
#include "content_sao.h"
#include "nodedef.h"
#include "voxelalgorithms.h"
//#include "profiler.h" // For TimeTaker
Expand Down
1 change: 0 additions & 1 deletion src/mapgen/mapgen_v7.cpp
Expand Up @@ -26,7 +26,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "mapblock.h"
#include "mapnode.h"
#include "map.h"
#include "content_sao.h"
#include "nodedef.h"
#include "voxelalgorithms.h"
//#include "profiler.h" // For TimeTaker
Expand Down
1 change: 0 additions & 1 deletion src/script/cpp_api/s_base.cpp
Expand Up @@ -43,7 +43,6 @@ extern "C" {
#include <cstdio>
#include <cstdarg>
#include "script/common/c_content.h"
#include "content_sao.h"
#include <sstream>


Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_env.cpp
Expand Up @@ -33,12 +33,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "nodedef.h"
#include "daynightratio.h"
#include "util/pointedthing.h"
#include "content_sao.h"
#include "mapgen/treegen.h"
#include "emerge.h"
#include "pathfinder.h"
#include "face_position_cache.h"
#include "remoteplayer.h"
#include "server/luaentity_sao.h"
#include "server/player_sao.h"
#ifndef SERVER
#include "client/client.h"
Expand Down
1 change: 0 additions & 1 deletion src/script/lua_api/l_item.cpp
Expand Up @@ -25,7 +25,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "itemdef.h"
#include "nodedef.h"
#include "server.h"
#include "content_sao.h"
#include "inventory.h"
#include "log.h"

Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_object.cpp
Expand Up @@ -27,11 +27,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "common/c_content.h"
#include "log.h"
#include "tool.h"
#include "content_sao.h"
#include "remoteplayer.h"
#include "server.h"
#include "hud.h"
#include "scripting_server.h"
#include "server/luaentity_sao.h"
#include "server/player_sao.h"

/*
Expand Down
1 change: 1 addition & 0 deletions src/server/CMakeLists.txt
@@ -1,5 +1,6 @@
set(server_SRCS
${CMAKE_CURRENT_SOURCE_DIR}/activeobjectmgr.cpp
${CMAKE_CURRENT_SOURCE_DIR}/luaentity_sao.cpp
${CMAKE_CURRENT_SOURCE_DIR}/mods.cpp
${CMAKE_CURRENT_SOURCE_DIR}/player_sao.cpp
${CMAKE_CURRENT_SOURCE_DIR}/serveractiveobject.cpp
Expand Down
192 changes: 46 additions & 146 deletions src/content_sao.cpp → src/server/luaentity_sao.cpp
@@ -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 @@ -17,114 +18,65 @@ with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/

#include "content_sao.h"
#include "util/serialize.h"
#include "luaentity_sao.h"
#include "collision.h"
#include "environment.h"
#include "tool.h" // For ToolCapabilities
#include "gamedef.h"
#include "nodedef.h"
#include "remoteplayer.h"
#include "server.h"
#include "constants.h"
#include "player_sao.h"
#include "scripting_server.h"
#include "server/player_sao.h"
#include "settings.h"
#include <algorithm>
#include <cmath>

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

/*
TestSAO
*/
#include "server.h"
#include "serverenvironment.h"

class TestSAO : public ServerActiveObject
LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &data)
: UnitSAO(env, pos)
{
public:
TestSAO(ServerEnvironment *env, v3f pos):
ServerActiveObject(env, pos),
m_timer1(0),
m_age(0)
{
ServerActiveObject::registerType(getType(), create);
}
ActiveObjectType getType() const
{ return ACTIVEOBJECT_TYPE_TEST; }
std::string name;
std::string state;
u16 hp = 1;
v3f velocity;
v3f rotation;

static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
const std::string &data)
{
return new TestSAO(env, pos);
}
while (!data.empty()) { // breakable, run for one iteration
std::istringstream is(data, std::ios::binary);
// 'version' does not allow to incrementally extend the parameter list thus
// we need another variable to build on top of 'version=1'. Ugly hack but works™
u8 version2 = 0;
u8 version = readU8(is);

void step(float dtime, bool send_recommended)
{
m_age += dtime;
if(m_age > 10)
{
m_pending_removal = true;
return;
}
name = deSerializeString(is);
state = deSerializeLongString(is);

m_base_position.Y += dtime * BS * 2;
if(m_base_position.Y > 8*BS)
m_base_position.Y = 2*BS;
if (version < 1)
break;

if (!send_recommended)
return;
hp = readU16(is);
velocity = readV3F1000(is);
// yaw must be yaw to be backwards-compatible
rotation.Y = readF1000(is);

m_timer1 -= dtime;
if(m_timer1 < 0.0)
{
m_timer1 += 0.125;
if (is.good()) // EOF for old formats
version2 = readU8(is);

std::string data;
if (version2 < 1) // PROTOCOL_VERSION < 37
break;

data += itos(0); // 0 = position
data += " ";
data += itos(m_base_position.X);
data += " ";
data += itos(m_base_position.Y);
data += " ";
data += itos(m_base_position.Z);
// version2 >= 1
rotation.X = readF1000(is);
rotation.Z = readF1000(is);

ActiveObjectMessage aom(getId(), false, data);
m_messages_out.push(aom);
}
// if (version2 < 2)
// break;
// <read new values>
break;
}
// create object
infostream << "LuaEntitySAO::create(name=\"" << name << "\" state=\""
<< state << "\")" << std::endl;

bool getCollisionBox(aabb3f *toset) const { return false; }

virtual bool getSelectionBox(aabb3f *toset) const { return false; }

bool collideWithObjects() const { return false; }

private:
float m_timer1;
float m_age;
};

// Prototype (registers item for deserialization)
TestSAO proto_TestSAO(NULL, v3f(0,0,0));

/*
LuaEntitySAO
*/

// Prototype (registers item for deserialization)
LuaEntitySAO proto_LuaEntitySAO(NULL, v3f(0,0,0), "_prototype", "");

LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos,
const std::string &name, const std::string &state):
UnitSAO(env, pos),
m_init_name(name),
m_init_state(state)
{
// Only register type if no environment supplied
if(env == NULL){
ServerActiveObject::registerType(getType(), create);
return;
}
m_init_name = name;
m_init_state = state;
m_hp = hp;
m_velocity = velocity;
m_rotation = rotation;
}

LuaEntitySAO::~LuaEntitySAO()
Expand Down Expand Up @@ -160,58 +112,6 @@ void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
}
}

ServerActiveObject* LuaEntitySAO::create(ServerEnvironment *env, v3f pos,
const std::string &data)
{
std::string name;
std::string state;
u16 hp = 1;
v3f velocity;
v3f rotation;

while (!data.empty()) { // breakable, run for one iteration
std::istringstream is(data, std::ios::binary);
// 'version' does not allow to incrementally extend the parameter list thus
// we need another variable to build on top of 'version=1'. Ugly hack but works™
u8 version2 = 0;
u8 version = readU8(is);

name = deSerializeString(is);
state = deSerializeLongString(is);

if (version < 1)
break;

hp = readU16(is);
velocity = readV3F1000(is);
// yaw must be yaw to be backwards-compatible
rotation.Y = readF1000(is);

if (is.good()) // EOF for old formats
version2 = readU8(is);

if (version2 < 1) // PROTOCOL_VERSION < 37
break;

// version2 >= 1
rotation.X = readF1000(is);
rotation.Z = readF1000(is);

// if (version2 < 2)
// break;
// <read new values>
break;
}
// create object
infostream << "LuaEntitySAO::create(name=\"" << name << "\" state=\""
<< state << "\")" << std::endl;
LuaEntitySAO *sao = new LuaEntitySAO(env, pos, name, state);
sao->m_hp = hp;
sao->m_velocity = velocity;
sao->m_rotation = rotation;
return sao;
}

void LuaEntitySAO::step(float dtime, bool send_recommended)
{
if(!m_properties_sent)
Expand Down

0 comments on commit 5146086

Please sign in to comment.