Drop content_sao.{cpp,h}
authorLoic Blot <loic.blot@unix-experience.fr>
Sat, 11 Apr 2020 09:22:15 +0000 (11:22 +0200)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sat, 11 Apr 2020 14:07:17 +0000 (16:07 +0200)
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

27 files changed:
build/android/jni/Android.mk
src/CMakeLists.txt
src/clientiface.cpp
src/content_sao.cpp [deleted file]
src/content_sao.h [deleted file]
src/mapgen/mapgen.cpp
src/mapgen/mapgen_carpathian.cpp
src/mapgen/mapgen_flat.cpp
src/mapgen/mapgen_fractal.cpp
src/mapgen/mapgen_v5.cpp
src/mapgen/mapgen_v6.cpp
src/mapgen/mapgen_v7.cpp
src/script/cpp_api/s_base.cpp
src/script/lua_api/l_env.cpp
src/script/lua_api/l_item.cpp
src/script/lua_api/l_object.cpp
src/server/CMakeLists.txt
src/server/luaentity_sao.cpp [new file with mode: 0644]
src/server/luaentity_sao.h [new file with mode: 0644]
src/server/player_sao.h
src/server/serveractiveobject.cpp
src/server/serveractiveobject.h
src/serverenvironment.cpp
src/serverenvironment.h
src/staticobject.cpp
src/unittest/test_player.cpp
util/travis/clang-format-whitelist.txt

index 32a16c174141efc351e68a503e3fb679af5f983b..fdbfba9bc61cf51c0bafc50a4944ccbc24000993 100644 (file)
@@ -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                      \
@@ -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                 \
index faa117d41c09d6324e4fc67fe29af356d23a1034..0ebbd628ce79bb9612e6d573aa1ad3c8237cf7a0 100644 (file)
@@ -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
index 17237f73ee545778e92218108fcca82419f70cb4..4f954342af50ba033904e1ed5373abb40bd774c8 100644 (file)
@@ -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"
@@ -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"
diff --git a/src/content_sao.cpp b/src/content_sao.cpp
deleted file mode 100644 (file)
index 7ec17aa..0000000
+++ /dev/null
@@ -1,681 +0,0 @@
-/*
-Minetest
-Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
-
-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
-the Free Software Foundation; either version 2.1 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Lesser General Public License for more details.
-
-You should have received a copy of the GNU Lesser General Public License along
-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 "collision.h"
-#include "environment.h"
-#include "tool.h" // For ToolCapabilities
-#include "gamedef.h"
-#include "nodedef.h"
-#include "remoteplayer.h"
-#include "server.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
-*/
-
-class TestSAO : public ServerActiveObject
-{
-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; }
-
-       static ServerActiveObject* create(ServerEnvironment *env, v3f pos,
-                       const std::string &data)
-       {
-               return new TestSAO(env, pos);
-       }
-
-       void step(float dtime, bool send_recommended)
-       {
-               m_age += dtime;
-               if(m_age > 10)
-               {
-                       m_pending_removal = true;
-                       return;
-               }
-
-               m_base_position.Y += dtime * BS * 2;
-               if(m_base_position.Y > 8*BS)
-                       m_base_position.Y = 2*BS;
-
-               if (!send_recommended)
-                       return;
-
-               m_timer1 -= dtime;
-               if(m_timer1 < 0.0)
-               {
-                       m_timer1 += 0.125;
-
-                       std::string data;
-
-                       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);
-
-                       ActiveObjectMessage aom(getId(), false, data);
-                       m_messages_out.push(aom);
-               }
-       }
-
-       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;
-       }
-}
-
-LuaEntitySAO::~LuaEntitySAO()
-{
-       if(m_registered){
-               m_env->getScriptIface()->luaentity_Remove(m_id);
-       }
-
-       for (u32 attached_particle_spawner : m_attached_particle_spawners) {
-               m_env->deleteParticleSpawner(attached_particle_spawner, false);
-       }
-}
-
-void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
-{
-       ServerActiveObject::addedToEnvironment(dtime_s);
-
-       // Create entity from name
-       m_registered = m_env->getScriptIface()->
-               luaentity_Add(m_id, m_init_name.c_str());
-
-       if(m_registered){
-               // Get properties
-               m_env->getScriptIface()->
-                       luaentity_GetProperties(m_id, this, &m_prop);
-               // Initialize HP from properties
-               m_hp = m_prop.hp_max;
-               // Activate entity, supplying serialized state
-               m_env->getScriptIface()->
-                       luaentity_Activate(m_id, m_init_state, dtime_s);
-       } else {
-               m_prop.infotext = m_init_name;
-       }
-}
-
-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)
-       {
-               m_properties_sent = true;
-               std::string str = getPropertyPacket();
-               // create message and add to list
-               ActiveObjectMessage aom(getId(), true, str);
-               m_messages_out.push(aom);
-       }
-
-       // If attached, check that our parent is still there. If it isn't, detach.
-       if(m_attachment_parent_id && !isAttached())
-       {
-               m_attachment_parent_id = 0;
-               m_attachment_bone = "";
-               m_attachment_position = v3f(0,0,0);
-               m_attachment_rotation = v3f(0,0,0);
-               sendPosition(false, true);
-       }
-
-       m_last_sent_position_timer += dtime;
-
-       // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
-       // If the object gets detached this comes into effect automatically from the last known origin
-       if(isAttached())
-       {
-               v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
-               m_base_position = pos;
-               m_velocity = v3f(0,0,0);
-               m_acceleration = v3f(0,0,0);
-       }
-       else
-       {
-               if(m_prop.physical){
-                       aabb3f box = m_prop.collisionbox;
-                       box.MinEdge *= BS;
-                       box.MaxEdge *= BS;
-                       collisionMoveResult moveresult;
-                       f32 pos_max_d = BS*0.25; // Distance per iteration
-                       v3f p_pos = m_base_position;
-                       v3f p_velocity = m_velocity;
-                       v3f p_acceleration = m_acceleration;
-                       moveresult = collisionMoveSimple(m_env, m_env->getGameDef(),
-                                       pos_max_d, box, m_prop.stepheight, dtime,
-                                       &p_pos, &p_velocity, p_acceleration,
-                                       this, m_prop.collideWithObjects);
-
-                       // Apply results
-                       m_base_position = p_pos;
-                       m_velocity = p_velocity;
-                       m_acceleration = p_acceleration;
-               } else {
-                       m_base_position += dtime * m_velocity + 0.5 * dtime
-                                       * dtime * m_acceleration;
-                       m_velocity += dtime * m_acceleration;
-               }
-
-               if (m_prop.automatic_face_movement_dir &&
-                               (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
-                       float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
-                               + m_prop.automatic_face_movement_dir_offset;
-                       float max_rotation_per_sec =
-                                       m_prop.automatic_face_movement_max_rotation_per_sec;
-
-                       if (max_rotation_per_sec > 0) {
-                               m_rotation.Y = wrapDegrees_0_360(m_rotation.Y);
-                               wrappedApproachShortest(m_rotation.Y, target_yaw,
-                                       dtime * max_rotation_per_sec, 360.f);
-                       } else {
-                               // Negative values of max_rotation_per_sec mean disabled.
-                               m_rotation.Y = target_yaw;
-                       }
-               }
-       }
-
-       if(m_registered){
-               m_env->getScriptIface()->luaentity_Step(m_id, dtime);
-       }
-
-       if (!send_recommended)
-               return;
-
-       if(!isAttached())
-       {
-               // TODO: force send when acceleration changes enough?
-               float minchange = 0.2*BS;
-               if(m_last_sent_position_timer > 1.0){
-                       minchange = 0.01*BS;
-               } else if(m_last_sent_position_timer > 0.2){
-                       minchange = 0.05*BS;
-               }
-               float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
-               move_d += m_last_sent_move_precision;
-               float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
-               if (move_d > minchange || vel_d > minchange ||
-                               std::fabs(m_rotation.X - m_last_sent_rotation.X) > 1.0f ||
-                               std::fabs(m_rotation.Y - m_last_sent_rotation.Y) > 1.0f ||
-                               std::fabs(m_rotation.Z - m_last_sent_rotation.Z) > 1.0f) {
-
-                       sendPosition(true, false);
-               }
-       }
-
-       if (!m_armor_groups_sent) {
-               m_armor_groups_sent = true;
-               // create message and add to list
-               m_messages_out.emplace(getId(), true, generateUpdateArmorGroupsCommand());
-       }
-
-       if (!m_animation_sent) {
-               m_animation_sent = true;
-               std::string str = generateUpdateAnimationCommand();
-               // create message and add to list
-               ActiveObjectMessage aom(getId(), true, str);
-               m_messages_out.push(aom);
-       }
-
-       if (!m_animation_speed_sent) {
-               m_animation_speed_sent = true;
-               std::string str = generateUpdateAnimationSpeedCommand();
-               // create message and add to list
-               ActiveObjectMessage aom(getId(), true, str);
-               m_messages_out.push(aom);
-       }
-
-       if (!m_bone_position_sent) {
-               m_bone_position_sent = true;
-               for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
-                               ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
-                       std::string str = generateUpdateBonePositionCommand((*ii).first,
-                                       (*ii).second.X, (*ii).second.Y);
-                       // create message and add to list
-                       ActiveObjectMessage aom(getId(), true, str);
-                       m_messages_out.push(aom);
-               }
-       }
-
-       if (!m_attachment_sent) {
-               m_attachment_sent = true;
-               std::string str = generateUpdateAttachmentCommand();
-               // create message and add to list
-               ActiveObjectMessage aom(getId(), true, str);
-               m_messages_out.push(aom);
-       }
-}
-
-std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
-{
-       std::ostringstream os(std::ios::binary);
-
-       // PROTOCOL_VERSION >= 37
-       writeU8(os, 1); // version
-       os << serializeString(""); // name
-       writeU8(os, 0); // is_player
-       writeU16(os, getId()); //id
-       writeV3F32(os, m_base_position);
-       writeV3F32(os, m_rotation);
-       writeU16(os, m_hp);
-
-       std::ostringstream msg_os(std::ios::binary);
-       msg_os << serializeLongString(getPropertyPacket()); // message 1
-       msg_os << serializeLongString(generateUpdateArmorGroupsCommand()); // 2
-       msg_os << serializeLongString(generateUpdateAnimationCommand()); // 3
-       for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
-                       ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
-               msg_os << serializeLongString(generateUpdateBonePositionCommand((*ii).first,
-                               (*ii).second.X, (*ii).second.Y)); // m_bone_position.size
-       }
-       msg_os << serializeLongString(generateUpdateAttachmentCommand()); // 4
-       int message_count = 4 + m_bone_position.size();
-       for (std::unordered_set<int>::const_iterator ii = m_attachment_child_ids.begin();
-                       (ii != m_attachment_child_ids.end()); ++ii) {
-               if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) {
-                       message_count++;
-                       // TODO after a protocol bump: only send the object initialization data
-                       // to older clients (superfluous since this message exists)
-                       msg_os << serializeLongString(obj->generateUpdateInfantCommand(*ii, protocol_version));
-               }
-       }
-
-       msg_os << serializeLongString(generateSetTextureModCommand());
-       message_count++;
-
-       writeU8(os, message_count);
-       os.write(msg_os.str().c_str(), msg_os.str().size());
-
-       // return result
-       return os.str();
-}
-
-void LuaEntitySAO::getStaticData(std::string *result) const
-{
-       verbosestream<<FUNCTION_NAME<<std::endl;
-       std::ostringstream os(std::ios::binary);
-       // version must be 1 to keep backwards-compatibility. See version2
-       writeU8(os, 1);
-       // name
-       os<<serializeString(m_init_name);
-       // state
-       if(m_registered){
-               std::string state = m_env->getScriptIface()->
-                       luaentity_GetStaticdata(m_id);
-               os<<serializeLongString(state);
-       } else {
-               os<<serializeLongString(m_init_state);
-       }
-       writeU16(os, m_hp);
-       writeV3F1000(os, m_velocity);
-       // yaw
-       writeF1000(os, m_rotation.Y);
-
-       // version2. Increase this variable for new values
-       writeU8(os, 1); // PROTOCOL_VERSION >= 37
-
-       writeF1000(os, m_rotation.X);
-       writeF1000(os, m_rotation.Z);
-
-       // <write new values>
-
-       *result = os.str();
-}
-
-u16 LuaEntitySAO::punch(v3f dir,
-               const ToolCapabilities *toolcap,
-               ServerActiveObject *puncher,
-               float time_from_last_punch)
-{
-       if (!m_registered) {
-               // Delete unknown LuaEntities when punched
-               m_pending_removal = true;
-               return 0;
-       }
-
-       FATAL_ERROR_IF(!puncher, "Punch action called without SAO");
-
-       s32 old_hp = getHP();
-       ItemStack selected_item, hand_item;
-       ItemStack tool_item = puncher->getWieldedItem(&selected_item, &hand_item);
-
-       PunchDamageResult result = getPunchDamage(
-                       m_armor_groups,
-                       toolcap,
-                       &tool_item,
-                       time_from_last_punch);
-
-       bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
-                       time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0);
-
-       if (!damage_handled) {
-               if (result.did_punch) {
-                       setHP((s32)getHP() - result.damage,
-                               PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
-
-                       // create message and add to list
-                       sendPunchCommand();
-               }
-       }
-
-       if (getHP() == 0 && !isGone()) {
-               clearParentAttachment();
-               clearChildAttachments();
-               m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
-               m_pending_removal = true;
-       }
-
-       actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
-                       ", hp=" << puncher->getHP() << ") punched " <<
-                       getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
-                       "), damage=" << (old_hp - (s32)getHP()) <<
-                       (damage_handled ? " (handled by Lua)" : "") << std::endl;
-
-       // TODO: give Lua control over wear
-       return result.wear;
-}
-
-void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
-{
-       if (!m_registered)
-               return;
-
-       m_env->getScriptIface()->luaentity_Rightclick(m_id, clicker);
-}
-
-void LuaEntitySAO::setPos(const v3f &pos)
-{
-       if(isAttached())
-               return;
-       m_base_position = pos;
-       sendPosition(false, true);
-}
-
-void LuaEntitySAO::moveTo(v3f pos, bool continuous)
-{
-       if(isAttached())
-               return;
-       m_base_position = pos;
-       if(!continuous)
-               sendPosition(true, true);
-}
-
-float LuaEntitySAO::getMinimumSavedMovement()
-{
-       return 0.1 * BS;
-}
-
-std::string LuaEntitySAO::getDescription()
-{
-       std::ostringstream oss;
-       oss << "LuaEntitySAO \"" << m_init_name << "\" ";
-       auto pos = floatToInt(m_base_position, BS);
-       oss << "at " << PP(pos);
-       return oss.str();
-}
-
-void LuaEntitySAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
-{
-       m_hp = rangelim(hp, 0, U16_MAX);
-}
-
-u16 LuaEntitySAO::getHP() const
-{
-       return m_hp;
-}
-
-void LuaEntitySAO::setVelocity(v3f velocity)
-{
-       m_velocity = velocity;
-}
-
-v3f LuaEntitySAO::getVelocity()
-{
-       return m_velocity;
-}
-
-void LuaEntitySAO::setAcceleration(v3f acceleration)
-{
-       m_acceleration = acceleration;
-}
-
-v3f LuaEntitySAO::getAcceleration()
-{
-       return m_acceleration;
-}
-
-void LuaEntitySAO::setTextureMod(const std::string &mod)
-{
-       m_current_texture_modifier = mod;
-       // create message and add to list
-       m_messages_out.emplace(getId(), true, generateSetTextureModCommand());
-}
-
-std::string LuaEntitySAO::getTextureMod() const
-{
-       return m_current_texture_modifier;
-}
-
-
-std::string LuaEntitySAO::generateSetTextureModCommand() const
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_SET_TEXTURE_MOD);
-       // parameters
-       os << serializeString(m_current_texture_modifier);
-       return os.str();
-}
-
-std::string LuaEntitySAO::generateSetSpriteCommand(v2s16 p, u16 num_frames,
-       f32 framelength, bool select_horiz_by_yawpitch)
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_SET_SPRITE);
-       // parameters
-       writeV2S16(os, p);
-       writeU16(os, num_frames);
-       writeF32(os, framelength);
-       writeU8(os, select_horiz_by_yawpitch);
-       return os.str();
-}
-
-void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
-               bool select_horiz_by_yawpitch)
-{
-       std::string str = generateSetSpriteCommand(
-               p,
-               num_frames,
-               framelength,
-               select_horiz_by_yawpitch
-       );
-       // create message and add to list
-       m_messages_out.emplace(getId(), true, str);
-}
-
-std::string LuaEntitySAO::getName()
-{
-       return m_init_name;
-}
-
-std::string LuaEntitySAO::getPropertyPacket()
-{
-       return generateSetPropertiesCommand(m_prop);
-}
-
-void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
-{
-       // If the object is attached client-side, don't waste bandwidth sending its position to clients
-       if(isAttached())
-               return;
-
-       m_last_sent_move_precision = m_base_position.getDistanceFrom(
-                       m_last_sent_position);
-       m_last_sent_position_timer = 0;
-       m_last_sent_position = m_base_position;
-       m_last_sent_velocity = m_velocity;
-       //m_last_sent_acceleration = m_acceleration;
-       m_last_sent_rotation = m_rotation;
-
-       float update_interval = m_env->getSendRecommendedInterval();
-
-       std::string str = generateUpdatePositionCommand(
-               m_base_position,
-               m_velocity,
-               m_acceleration,
-               m_rotation,
-               do_interpolate,
-               is_movement_end,
-               update_interval
-       );
-       // create message and add to list
-       m_messages_out.emplace(getId(), false, str);
-}
-
-bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const
-{
-       if (m_prop.physical)
-       {
-               //update collision box
-               toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
-               toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
-
-               toset->MinEdge += m_base_position;
-               toset->MaxEdge += m_base_position;
-
-               return true;
-       }
-
-       return false;
-}
-
-bool LuaEntitySAO::getSelectionBox(aabb3f *toset) const
-{
-       if (!m_prop.is_visible || !m_prop.pointable) {
-               return false;
-       }
-
-       toset->MinEdge = m_prop.selectionbox.MinEdge * BS;
-       toset->MaxEdge = m_prop.selectionbox.MaxEdge * BS;
-
-       return true;
-}
-
-bool LuaEntitySAO::collideWithObjects() const
-{
-       return m_prop.collideWithObjects;
-}
diff --git a/src/content_sao.h b/src/content_sao.h
deleted file mode 100644 (file)
index 5387fd1..0000000
+++ /dev/null
@@ -1,101 +0,0 @@
-/*
-Minetest
-Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
-
-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
-the Free Software Foundation; either version 2.1 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU Lesser General Public License for more details.
-
-You should have received a copy of the GNU Lesser General Public License along
-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 "network/networkprotocol.h"
-#include "util/numeric.h"
-#include "server/unit_sao.h"
-#include "itemgroup.h"
-#include "constants.h"
-
-/*
-       LuaEntitySAO needs some internals exposed.
-*/
-
-class LuaEntitySAO : public UnitSAO
-{
-public:
-       LuaEntitySAO(ServerEnvironment *env, v3f pos,
-               const std::string &name, const std::string &state);
-       ~LuaEntitySAO();
-       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; }
-       void getStaticData(std::string *result) const;
-       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);
-       float getMinimumSavedMovement();
-       std::string getDescription();
-       void setHP(s32 hp, const PlayerHPChangeReason &reason);
-       u16 getHP() const;
-
-       /* LuaEntitySAO-specific */
-       void setVelocity(v3f velocity);
-       void addVelocity(v3f velocity)
-       {
-               m_velocity += velocity;
-       }
-       v3f getVelocity();
-       void setAcceleration(v3f acceleration);
-       v3f getAcceleration();
-
-       void setTextureMod(const std::string &mod);
-       std::string getTextureMod() const;
-       void setSprite(v2s16 p, int num_frames, float framelength,
-                       bool select_horiz_by_yawpitch);
-       std::string getName();
-       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);
-
-       std::string m_init_name;
-       std::string m_init_state;
-       bool m_registered = false;
-
-       v3f m_velocity;
-       v3f m_acceleration;
-
-       v3f m_last_sent_position;
-       v3f m_last_sent_velocity;
-       v3f m_last_sent_rotation;
-       float m_last_sent_position_timer = 0.0f;
-       float m_last_sent_move_precision = 0.0f;
-       std::string m_current_texture_modifier = "";
-};
-
index 6c426ba4108284fe7ecb5a42a6f097ff45d750a8..0094608ec6803e945edadd4ea6c196279b46da0d 100644 (file)
@@ -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"
index 0dc1d33be6edd216e434f25c081679f943bab9f5..bd7ae5e7cd918749642730c3dea377150727624a 100644 (file)
@@ -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
index 879435948d2fbd9e94d12ca9b3f13d664bc6c6f9..272964b514758458706e40d13e8ec6204e6706c4 100644 (file)
@@ -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
index 96febb4f4bb4dd8cb654f75271f36c2553c5b8a6..97f77d9472aca1173ddfc192b98e5086a2eda33d 100644 (file)
@@ -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
index 447fe8c5083027229f322cbc9a2b9465cde16e33..3bd7615c473bb9de79a261480776cc419be98635 100644 (file)
@@ -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
index f473f725dbcdf488ad49dba20a3092cfde6508a3..da9ae1428c8d1f7c1d5c9c5ad304bbfe9c8cb479 100644 (file)
@@ -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
index 325c4957a97250f775f119c640e392f25a57c290..82556cc4f5468d4c62a242a5fe9d2a8816da3caa 100644 (file)
@@ -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
index 150baf77e3d5ab6d0f314f8754a67de380a29082..df07206d7dc1d378ba740929fc8dac97395ba1c4 100644 (file)
@@ -43,7 +43,6 @@ extern "C" {
 #include <cstdio>
 #include <cstdarg>
 #include "script/common/c_content.h"
-#include "content_sao.h"
 #include <sstream>
 
 
index 438669feb5060120875a66c4c2ff1653d405661b..40e50e64a81ee8803928bd12fb6662a963d14fc8 100644 (file)
@@ -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"
index ff77cba321504ec43ead0d7a645b9c51ec9b2011..9f12d3ac79a84baedf34b355b486f559d6d170f3 100644 (file)
@@ -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"
 
index 95c96235e9e5a2cf2bf2ec1b75e9c48479e68457..fa34260bf3a717c2f6fc502b60f903f91ddff9f0 100644 (file)
@@ -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"
 
 /*
index 26eaed5ac17e36922ff728d4bb50a8d570c815d4..4d94504f649f504fd4fdd8d376eaa8259cd07a0a 100644 (file)
@@ -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
diff --git a/src/server/luaentity_sao.cpp b/src/server/luaentity_sao.cpp
new file mode 100644 (file)
index 0000000..1259392
--- /dev/null
@@ -0,0 +1,581 @@
+/*
+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
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+with this program; if not, write to the Free Software Foundation, Inc.,
+51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+*/
+
+#include "luaentity_sao.h"
+#include "collision.h"
+#include "constants.h"
+#include "player_sao.h"
+#include "scripting_server.h"
+#include "server.h"
+#include "serverenvironment.h"
+
+LuaEntitySAO::LuaEntitySAO(ServerEnvironment *env, v3f pos, const std::string &data)
+       : UnitSAO(env, pos)
+{
+       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;
+
+       m_init_name = name;
+       m_init_state = state;
+       m_hp = hp;
+       m_velocity = velocity;
+       m_rotation = rotation;
+}
+
+LuaEntitySAO::~LuaEntitySAO()
+{
+       if(m_registered){
+               m_env->getScriptIface()->luaentity_Remove(m_id);
+       }
+
+       for (u32 attached_particle_spawner : m_attached_particle_spawners) {
+               m_env->deleteParticleSpawner(attached_particle_spawner, false);
+       }
+}
+
+void LuaEntitySAO::addedToEnvironment(u32 dtime_s)
+{
+       ServerActiveObject::addedToEnvironment(dtime_s);
+
+       // Create entity from name
+       m_registered = m_env->getScriptIface()->
+               luaentity_Add(m_id, m_init_name.c_str());
+
+       if(m_registered){
+               // Get properties
+               m_env->getScriptIface()->
+                       luaentity_GetProperties(m_id, this, &m_prop);
+               // Initialize HP from properties
+               m_hp = m_prop.hp_max;
+               // Activate entity, supplying serialized state
+               m_env->getScriptIface()->
+                       luaentity_Activate(m_id, m_init_state, dtime_s);
+       } else {
+               m_prop.infotext = m_init_name;
+       }
+}
+
+void LuaEntitySAO::step(float dtime, bool send_recommended)
+{
+       if(!m_properties_sent)
+       {
+               m_properties_sent = true;
+               std::string str = getPropertyPacket();
+               // create message and add to list
+               ActiveObjectMessage aom(getId(), true, str);
+               m_messages_out.push(aom);
+       }
+
+       // If attached, check that our parent is still there. If it isn't, detach.
+       if(m_attachment_parent_id && !isAttached())
+       {
+               m_attachment_parent_id = 0;
+               m_attachment_bone = "";
+               m_attachment_position = v3f(0,0,0);
+               m_attachment_rotation = v3f(0,0,0);
+               sendPosition(false, true);
+       }
+
+       m_last_sent_position_timer += dtime;
+
+       // Each frame, parent position is copied if the object is attached, otherwise it's calculated normally
+       // If the object gets detached this comes into effect automatically from the last known origin
+       if(isAttached())
+       {
+               v3f pos = m_env->getActiveObject(m_attachment_parent_id)->getBasePosition();
+               m_base_position = pos;
+               m_velocity = v3f(0,0,0);
+               m_acceleration = v3f(0,0,0);
+       }
+       else
+       {
+               if(m_prop.physical){
+                       aabb3f box = m_prop.collisionbox;
+                       box.MinEdge *= BS;
+                       box.MaxEdge *= BS;
+                       collisionMoveResult moveresult;
+                       f32 pos_max_d = BS*0.25; // Distance per iteration
+                       v3f p_pos = m_base_position;
+                       v3f p_velocity = m_velocity;
+                       v3f p_acceleration = m_acceleration;
+                       moveresult = collisionMoveSimple(m_env, m_env->getGameDef(),
+                                       pos_max_d, box, m_prop.stepheight, dtime,
+                                       &p_pos, &p_velocity, p_acceleration,
+                                       this, m_prop.collideWithObjects);
+
+                       // Apply results
+                       m_base_position = p_pos;
+                       m_velocity = p_velocity;
+                       m_acceleration = p_acceleration;
+               } else {
+                       m_base_position += dtime * m_velocity + 0.5 * dtime
+                                       * dtime * m_acceleration;
+                       m_velocity += dtime * m_acceleration;
+               }
+
+               if (m_prop.automatic_face_movement_dir &&
+                               (fabs(m_velocity.Z) > 0.001 || fabs(m_velocity.X) > 0.001)) {
+                       float target_yaw = atan2(m_velocity.Z, m_velocity.X) * 180 / M_PI
+                               + m_prop.automatic_face_movement_dir_offset;
+                       float max_rotation_per_sec =
+                                       m_prop.automatic_face_movement_max_rotation_per_sec;
+
+                       if (max_rotation_per_sec > 0) {
+                               m_rotation.Y = wrapDegrees_0_360(m_rotation.Y);
+                               wrappedApproachShortest(m_rotation.Y, target_yaw,
+                                       dtime * max_rotation_per_sec, 360.f);
+                       } else {
+                               // Negative values of max_rotation_per_sec mean disabled.
+                               m_rotation.Y = target_yaw;
+                       }
+               }
+       }
+
+       if(m_registered){
+               m_env->getScriptIface()->luaentity_Step(m_id, dtime);
+       }
+
+       if (!send_recommended)
+               return;
+
+       if(!isAttached())
+       {
+               // TODO: force send when acceleration changes enough?
+               float minchange = 0.2*BS;
+               if(m_last_sent_position_timer > 1.0){
+                       minchange = 0.01*BS;
+               } else if(m_last_sent_position_timer > 0.2){
+                       minchange = 0.05*BS;
+               }
+               float move_d = m_base_position.getDistanceFrom(m_last_sent_position);
+               move_d += m_last_sent_move_precision;
+               float vel_d = m_velocity.getDistanceFrom(m_last_sent_velocity);
+               if (move_d > minchange || vel_d > minchange ||
+                               std::fabs(m_rotation.X - m_last_sent_rotation.X) > 1.0f ||
+                               std::fabs(m_rotation.Y - m_last_sent_rotation.Y) > 1.0f ||
+                               std::fabs(m_rotation.Z - m_last_sent_rotation.Z) > 1.0f) {
+
+                       sendPosition(true, false);
+               }
+       }
+
+       if (!m_armor_groups_sent) {
+               m_armor_groups_sent = true;
+               // create message and add to list
+               m_messages_out.emplace(getId(), true, generateUpdateArmorGroupsCommand());
+       }
+
+       if (!m_animation_sent) {
+               m_animation_sent = true;
+               std::string str = generateUpdateAnimationCommand();
+               // create message and add to list
+               ActiveObjectMessage aom(getId(), true, str);
+               m_messages_out.push(aom);
+       }
+
+       if (!m_animation_speed_sent) {
+               m_animation_speed_sent = true;
+               std::string str = generateUpdateAnimationSpeedCommand();
+               // create message and add to list
+               ActiveObjectMessage aom(getId(), true, str);
+               m_messages_out.push(aom);
+       }
+
+       if (!m_bone_position_sent) {
+               m_bone_position_sent = true;
+               for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
+                               ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii){
+                       std::string str = generateUpdateBonePositionCommand((*ii).first,
+                                       (*ii).second.X, (*ii).second.Y);
+                       // create message and add to list
+                       ActiveObjectMessage aom(getId(), true, str);
+                       m_messages_out.push(aom);
+               }
+       }
+
+       if (!m_attachment_sent) {
+               m_attachment_sent = true;
+               std::string str = generateUpdateAttachmentCommand();
+               // create message and add to list
+               ActiveObjectMessage aom(getId(), true, str);
+               m_messages_out.push(aom);
+       }
+}
+
+std::string LuaEntitySAO::getClientInitializationData(u16 protocol_version)
+{
+       std::ostringstream os(std::ios::binary);
+
+       // PROTOCOL_VERSION >= 37
+       writeU8(os, 1); // version
+       os << serializeString(""); // name
+       writeU8(os, 0); // is_player
+       writeU16(os, getId()); //id
+       writeV3F32(os, m_base_position);
+       writeV3F32(os, m_rotation);
+       writeU16(os, m_hp);
+
+       std::ostringstream msg_os(std::ios::binary);
+       msg_os << serializeLongString(getPropertyPacket()); // message 1
+       msg_os << serializeLongString(generateUpdateArmorGroupsCommand()); // 2
+       msg_os << serializeLongString(generateUpdateAnimationCommand()); // 3
+       for (std::unordered_map<std::string, core::vector2d<v3f>>::const_iterator
+                       ii = m_bone_position.begin(); ii != m_bone_position.end(); ++ii) {
+               msg_os << serializeLongString(generateUpdateBonePositionCommand((*ii).first,
+                               (*ii).second.X, (*ii).second.Y)); // m_bone_position.size
+       }
+       msg_os << serializeLongString(generateUpdateAttachmentCommand()); // 4
+       int message_count = 4 + m_bone_position.size();
+       for (std::unordered_set<int>::const_iterator ii = m_attachment_child_ids.begin();
+                       (ii != m_attachment_child_ids.end()); ++ii) {
+               if (ServerActiveObject *obj = m_env->getActiveObject(*ii)) {
+                       message_count++;
+                       // TODO after a protocol bump: only send the object initialization data
+                       // to older clients (superfluous since this message exists)
+                       msg_os << serializeLongString(obj->generateUpdateInfantCommand(*ii, protocol_version));
+               }
+       }
+
+       msg_os << serializeLongString(generateSetTextureModCommand());
+       message_count++;
+
+       writeU8(os, message_count);
+       os.write(msg_os.str().c_str(), msg_os.str().size());
+
+       // return result
+       return os.str();
+}
+
+void LuaEntitySAO::getStaticData(std::string *result) const
+{
+       verbosestream<<FUNCTION_NAME<<std::endl;
+       std::ostringstream os(std::ios::binary);
+       // version must be 1 to keep backwards-compatibility. See version2
+       writeU8(os, 1);
+       // name
+       os<<serializeString(m_init_name);
+       // state
+       if(m_registered){
+               std::string state = m_env->getScriptIface()->
+                       luaentity_GetStaticdata(m_id);
+               os<<serializeLongString(state);
+       } else {
+               os<<serializeLongString(m_init_state);
+       }
+       writeU16(os, m_hp);
+       writeV3F1000(os, m_velocity);
+       // yaw
+       writeF1000(os, m_rotation.Y);
+
+       // version2. Increase this variable for new values
+       writeU8(os, 1); // PROTOCOL_VERSION >= 37
+
+       writeF1000(os, m_rotation.X);
+       writeF1000(os, m_rotation.Z);
+
+       // <write new values>
+
+       *result = os.str();
+}
+
+u16 LuaEntitySAO::punch(v3f dir,
+               const ToolCapabilities *toolcap,
+               ServerActiveObject *puncher,
+               float time_from_last_punch)
+{
+       if (!m_registered) {
+               // Delete unknown LuaEntities when punched
+               m_pending_removal = true;
+               return 0;
+       }
+
+       FATAL_ERROR_IF(!puncher, "Punch action called without SAO");
+
+       s32 old_hp = getHP();
+       ItemStack selected_item, hand_item;
+       ItemStack tool_item = puncher->getWieldedItem(&selected_item, &hand_item);
+
+       PunchDamageResult result = getPunchDamage(
+                       m_armor_groups,
+                       toolcap,
+                       &tool_item,
+                       time_from_last_punch);
+
+       bool damage_handled = m_env->getScriptIface()->luaentity_Punch(m_id, puncher,
+                       time_from_last_punch, toolcap, dir, result.did_punch ? result.damage : 0);
+
+       if (!damage_handled) {
+               if (result.did_punch) {
+                       setHP((s32)getHP() - result.damage,
+                               PlayerHPChangeReason(PlayerHPChangeReason::PLAYER_PUNCH, puncher));
+
+                       // create message and add to list
+                       sendPunchCommand();
+               }
+       }
+
+       if (getHP() == 0 && !isGone()) {
+               clearParentAttachment();
+               clearChildAttachments();
+               m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
+               m_pending_removal = true;
+       }
+
+       actionstream << puncher->getDescription() << " (id=" << puncher->getId() <<
+                       ", hp=" << puncher->getHP() << ") punched " <<
+                       getDescription() << " (id=" << m_id << ", hp=" << m_hp <<
+                       "), damage=" << (old_hp - (s32)getHP()) <<
+                       (damage_handled ? " (handled by Lua)" : "") << std::endl;
+
+       // TODO: give Lua control over wear
+       return result.wear;
+}
+
+void LuaEntitySAO::rightClick(ServerActiveObject *clicker)
+{
+       if (!m_registered)
+               return;
+
+       m_env->getScriptIface()->luaentity_Rightclick(m_id, clicker);
+}
+
+void LuaEntitySAO::setPos(const v3f &pos)
+{
+       if(isAttached())
+               return;
+       m_base_position = pos;
+       sendPosition(false, true);
+}
+
+void LuaEntitySAO::moveTo(v3f pos, bool continuous)
+{
+       if(isAttached())
+               return;
+       m_base_position = pos;
+       if(!continuous)
+               sendPosition(true, true);
+}
+
+float LuaEntitySAO::getMinimumSavedMovement()
+{
+       return 0.1 * BS;
+}
+
+std::string LuaEntitySAO::getDescription()
+{
+       std::ostringstream oss;
+       oss << "LuaEntitySAO \"" << m_init_name << "\" ";
+       auto pos = floatToInt(m_base_position, BS);
+       oss << "at " << PP(pos);
+       return oss.str();
+}
+
+void LuaEntitySAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
+{
+       m_hp = rangelim(hp, 0, U16_MAX);
+}
+
+u16 LuaEntitySAO::getHP() const
+{
+       return m_hp;
+}
+
+void LuaEntitySAO::setVelocity(v3f velocity)
+{
+       m_velocity = velocity;
+}
+
+v3f LuaEntitySAO::getVelocity()
+{
+       return m_velocity;
+}
+
+void LuaEntitySAO::setAcceleration(v3f acceleration)
+{
+       m_acceleration = acceleration;
+}
+
+v3f LuaEntitySAO::getAcceleration()
+{
+       return m_acceleration;
+}
+
+void LuaEntitySAO::setTextureMod(const std::string &mod)
+{
+       m_current_texture_modifier = mod;
+       // create message and add to list
+       m_messages_out.emplace(getId(), true, generateSetTextureModCommand());
+}
+
+std::string LuaEntitySAO::getTextureMod() const
+{
+       return m_current_texture_modifier;
+}
+
+
+std::string LuaEntitySAO::generateSetTextureModCommand() const
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_SET_TEXTURE_MOD);
+       // parameters
+       os << serializeString(m_current_texture_modifier);
+       return os.str();
+}
+
+std::string LuaEntitySAO::generateSetSpriteCommand(v2s16 p, u16 num_frames,
+       f32 framelength, bool select_horiz_by_yawpitch)
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_SET_SPRITE);
+       // parameters
+       writeV2S16(os, p);
+       writeU16(os, num_frames);
+       writeF32(os, framelength);
+       writeU8(os, select_horiz_by_yawpitch);
+       return os.str();
+}
+
+void LuaEntitySAO::setSprite(v2s16 p, int num_frames, float framelength,
+               bool select_horiz_by_yawpitch)
+{
+       std::string str = generateSetSpriteCommand(
+               p,
+               num_frames,
+               framelength,
+               select_horiz_by_yawpitch
+       );
+       // create message and add to list
+       m_messages_out.emplace(getId(), true, str);
+}
+
+std::string LuaEntitySAO::getName()
+{
+       return m_init_name;
+}
+
+std::string LuaEntitySAO::getPropertyPacket()
+{
+       return generateSetPropertiesCommand(m_prop);
+}
+
+void LuaEntitySAO::sendPosition(bool do_interpolate, bool is_movement_end)
+{
+       // If the object is attached client-side, don't waste bandwidth sending its position to clients
+       if(isAttached())
+               return;
+
+       m_last_sent_move_precision = m_base_position.getDistanceFrom(
+                       m_last_sent_position);
+       m_last_sent_position_timer = 0;
+       m_last_sent_position = m_base_position;
+       m_last_sent_velocity = m_velocity;
+       //m_last_sent_acceleration = m_acceleration;
+       m_last_sent_rotation = m_rotation;
+
+       float update_interval = m_env->getSendRecommendedInterval();
+
+       std::string str = generateUpdatePositionCommand(
+               m_base_position,
+               m_velocity,
+               m_acceleration,
+               m_rotation,
+               do_interpolate,
+               is_movement_end,
+               update_interval
+       );
+       // create message and add to list
+       m_messages_out.emplace(getId(), false, str);
+}
+
+bool LuaEntitySAO::getCollisionBox(aabb3f *toset) const
+{
+       if (m_prop.physical)
+       {
+               //update collision box
+               toset->MinEdge = m_prop.collisionbox.MinEdge * BS;
+               toset->MaxEdge = m_prop.collisionbox.MaxEdge * BS;
+
+               toset->MinEdge += m_base_position;
+               toset->MaxEdge += m_base_position;
+
+               return true;
+       }
+
+       return false;
+}
+
+bool LuaEntitySAO::getSelectionBox(aabb3f *toset) const
+{
+       if (!m_prop.is_visible || !m_prop.pointable) {
+               return false;
+       }
+
+       toset->MinEdge = m_prop.selectionbox.MinEdge * BS;
+       toset->MaxEdge = m_prop.selectionbox.MaxEdge * BS;
+
+       return true;
+}
+
+bool LuaEntitySAO::collideWithObjects() const
+{
+       return m_prop.collideWithObjects;
+}
diff --git a/src/server/luaentity_sao.h b/src/server/luaentity_sao.h
new file mode 100644 (file)
index 0000000..2520c8f
--- /dev/null
@@ -0,0 +1,93 @@
+/*
+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
+the Free Software Foundation; either version 2.1 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License along
+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 "unit_sao.h"
+
+class LuaEntitySAO : public UnitSAO
+{
+public:
+       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; }
+       virtual void addedToEnvironment(u32 dtime_s);
+       void step(float dtime, bool send_recommended);
+       std::string getClientInitializationData(u16 protocol_version);
+       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);
+       void rightClick(ServerActiveObject *clicker);
+       void setPos(const v3f &pos);
+       void moveTo(v3f pos, bool continuous);
+       float getMinimumSavedMovement();
+       std::string getDescription();
+       void setHP(s32 hp, const PlayerHPChangeReason &reason);
+       u16 getHP() const;
+
+       /* LuaEntitySAO-specific */
+       void setVelocity(v3f velocity);
+       void addVelocity(v3f velocity) { m_velocity += velocity; }
+       v3f getVelocity();
+       void setAcceleration(v3f acceleration);
+       v3f getAcceleration();
+
+       void setTextureMod(const std::string &mod);
+       std::string getTextureMod() const;
+       void setSprite(v2s16 p, int num_frames, float framelength,
+                       bool select_horiz_by_yawpitch);
+       std::string getName();
+       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);
+
+       std::string m_init_name;
+       std::string m_init_state;
+       bool m_registered = false;
+
+       v3f m_velocity;
+       v3f m_acceleration;
+
+       v3f m_last_sent_position;
+       v3f m_last_sent_velocity;
+       v3f m_last_sent_rotation;
+       float m_last_sent_position_timer = 0.0f;
+       float m_last_sent_move_precision = 0.0f;
+       std::string m_current_texture_modifier = "";
+};
index ce1cb1677c4c76c768804084f688a90884efafb1..8571bd4f91d01af85f6dcbc274ebe9dbdc49b3f1 100644 (file)
@@ -1,4 +1,3 @@
-
 /*
 Minetest
 Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
@@ -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"
@@ -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;
@@ -73,10 +72,8 @@ public:
        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();
 
        /*
@@ -111,10 +108,8 @@ public:
                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; }
@@ -144,10 +139,7 @@ public:
 
        // 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;
@@ -159,30 +151,17 @@ public:
                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;
@@ -236,6 +215,7 @@ private:
        s16 m_wanted_range = 0.0f;
 
        Metadata m_meta;
+
 public:
        float m_physics_override_speed = 1.0f;
        float m_physics_override_jump = 1.0f;
@@ -246,9 +226,10 @@ public:
        bool m_physics_override_sent = false;
 };
 
-
-struct PlayerHPChangeReason {
-       enum Type : u8 {
+struct PlayerHPChangeReason
+{
+       enum Type : u8
+       {
                SET_HP,
                PLAYER_PUNCH,
                FALL,
@@ -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)
        {
@@ -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) {}
 };
index 3aa78c7d56b69e296abed5276197c88d4fbfc15c..8345ebd477da36a123842d19929b44830d6437db 100644 (file)
@@ -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;
index 2e013a6b63aaee56bb5a3d384f38b46f23fba671..927009aefff889f6e9fa49dd87ddb3e92d76fab5 100644 (file)
@@ -244,12 +244,6 @@ protected:
        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;
@@ -258,8 +252,4 @@ protected:
                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;
 };
index c2ab5c07d60c1a5d7d182b0d858c7e29323cb155..32d10f8c0757d7ebed086b7b7bb51edc688b28b9 100644 (file)
@@ -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_:"
@@ -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.
 */
@@ -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)
index bf10e3ca864a08e09d6f8ce353bce7ef8c9bf74e..e106582492c1a2e9d90b17279478d1c60f813837 100644 (file)
@@ -468,4 +468,6 @@ private:
        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);
 };
index bebca12ecabe0b48e4ae2c2d1e7d33481c592031..5ccb7baf5227893eeb366693eca6c226349dda76 100644 (file)
@@ -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()),
index e2b1cd855e2beb56c859708a172ecf2381452a8c..6990b40163cdaec0d6d22a8cd7bce45010eb8a25 100644 (file)
@@ -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
index 816ec2c59bbcaa560ce077bc5f642bb050c3b048..bb97da7b56a100550a069cb83f341f8c3219f786 100644 (file)
@@ -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
@@ -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