]> git.lizzy.rs Git - dragonfireclient.git/commitdiff
Move serveractiveobject & unitsao
authorLoic Blot <loic.blot@unix-experience.fr>
Fri, 10 Apr 2020 19:25:42 +0000 (21:25 +0200)
committerLoïc Blot <nerzhul@users.noreply.github.com>
Sat, 11 Apr 2020 14:07:17 +0000 (16:07 +0200)
Move serverobject.{cpp,h} to server/serveractiveobject.{cpp,h}
Move UnitSAO class to dedicated files

22 files changed:
build/android/jni/Android.mk
src/CMakeLists.txt
src/collision.cpp
src/content_sao.cpp
src/content_sao.h
src/emerge.cpp
src/environment.cpp
src/inventorymanager.cpp
src/mapgen/mapgen_v6.cpp
src/script/common/c_content.cpp
src/script/cpp_api/s_base.cpp
src/script/lua_api/l_object.cpp
src/server.cpp
src/server/CMakeLists.txt
src/server/activeobjectmgr.h
src/server/serveractiveobject.cpp [new file with mode: 0644]
src/server/serveractiveobject.h [new file with mode: 0644]
src/server/unit_sao.cpp [new file with mode: 0644]
src/server/unit_sao.h [new file with mode: 0644]
src/serverobject.cpp [deleted file]
src/serverobject.h [deleted file]
util/travis/clang-format-whitelist.txt

index a2f32440affac338dc83cec41992e79f6180ef92..32a16c174141efc351e68a503e3fb679af5f983b 100644 (file)
@@ -259,7 +259,7 @@ LOCAL_SRC_FILES := \
                jni/src/serverenvironment.cpp             \
                jni/src/serverlist.cpp                    \
                jni/src/server/mods.cpp                   \
-               jni/src/serverobject.cpp                  \
+               jni/src/server/serveractiveobject.cpp     \
                jni/src/settings.cpp                      \
                jni/src/staticobject.cpp                  \
                jni/src/tileanimation.cpp                 \
index 4b1d6d647b4f8ed1fd4afb1f7a2f7d38afc0c239..faa117d41c09d6324e4fc67fe29af356d23a1034 100644 (file)
@@ -421,7 +421,6 @@ set(common_SRCS
        server.cpp
        serverenvironment.cpp
        serverlist.cpp
-       serverobject.cpp
        settings.cpp
        staticobject.cpp
        terminal_chat_console.cpp
index 0d37ea436d96b44e5f3f8ed913b9f03728ff3f0e..d9fbd3202419cb7fb598abf441aeb7fa32b5543a 100644 (file)
@@ -28,7 +28,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "client/localplayer.h"
 #endif
 #include "serverenvironment.h"
-#include "serverobject.h"
+#include "server/serveractiveobject.h"
 #include "util/timetaker.h"
 #include "profiler.h"
 
index be7674f52c412ce98316e325c85b5212f43494e0..0d387b53a59fdd94b89a029b66746062d1386613 100644 (file)
@@ -106,302 +106,6 @@ class TestSAO : public ServerActiveObject
 // Prototype (registers item for deserialization)
 TestSAO proto_TestSAO(NULL, v3f(0,0,0));
 
-/*
-       UnitSAO
- */
-
-UnitSAO::UnitSAO(ServerEnvironment *env, v3f pos):
-       ServerActiveObject(env, pos)
-{
-       // Initialize something to armor groups
-       m_armor_groups["fleshy"] = 100;
-}
-
-ServerActiveObject *UnitSAO::getParent() const
-{
-       if (!m_attachment_parent_id)
-               return nullptr;
-       // Check if the parent still exists
-       ServerActiveObject *obj = m_env->getActiveObject(m_attachment_parent_id);
-
-       return obj;
-}
-
-void UnitSAO::setArmorGroups(const ItemGroupList &armor_groups)
-{
-       m_armor_groups = armor_groups;
-       m_armor_groups_sent = false;
-}
-
-const ItemGroupList &UnitSAO::getArmorGroups() const
-{
-       return m_armor_groups;
-}
-
-void UnitSAO::setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop)
-{
-       // store these so they can be updated to clients
-       m_animation_range = frame_range;
-       m_animation_speed = frame_speed;
-       m_animation_blend = frame_blend;
-       m_animation_loop = frame_loop;
-       m_animation_sent = false;
-}
-
-void UnitSAO::getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop)
-{
-       *frame_range = m_animation_range;
-       *frame_speed = m_animation_speed;
-       *frame_blend = m_animation_blend;
-       *frame_loop = m_animation_loop;
-}
-
-void UnitSAO::setAnimationSpeed(float frame_speed)
-{
-       m_animation_speed = frame_speed;
-       m_animation_speed_sent = false;
-}
-
-void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotation)
-{
-       // store these so they can be updated to clients
-       m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
-       m_bone_position_sent = false;
-}
-
-void UnitSAO::getBonePosition(const std::string &bone, v3f *position, v3f *rotation)
-{
-       *position = m_bone_position[bone].X;
-       *rotation = m_bone_position[bone].Y;
-}
-
-void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
-{
-       // Attachments need to be handled on both the server and client.
-       // If we just attach on the server, we can only copy the position of the parent. Attachments
-       // are still sent to clients at an interval so players might see them lagging, plus we can't
-       // read and attach to skeletal bones.
-       // If we just attach on the client, the server still sees the child at its original location.
-       // This breaks some things so we also give the server the most accurate representation
-       // even if players only see the client changes.
-
-       int old_parent = m_attachment_parent_id;
-       m_attachment_parent_id = parent_id;
-       m_attachment_bone = bone;
-       m_attachment_position = position;
-       m_attachment_rotation = rotation;
-       m_attachment_sent = false;
-
-       if (parent_id != old_parent) {
-               onDetach(old_parent);
-               onAttach(parent_id);
-       }
-}
-
-void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
-       v3f *rotation) const
-{
-       *parent_id = m_attachment_parent_id;
-       *bone = m_attachment_bone;
-       *position = m_attachment_position;
-       *rotation = m_attachment_rotation;
-}
-
-void UnitSAO::clearChildAttachments()
-{
-       for (int child_id : m_attachment_child_ids) {
-               // Child can be NULL if it was deleted earlier
-               if (ServerActiveObject *child = m_env->getActiveObject(child_id))
-                       child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
-       }
-       m_attachment_child_ids.clear();
-}
-
-void UnitSAO::clearParentAttachment()
-{
-       ServerActiveObject *parent = nullptr;
-       if (m_attachment_parent_id) {
-               parent = m_env->getActiveObject(m_attachment_parent_id);
-               setAttachment(0, "", m_attachment_position, m_attachment_rotation);
-       } else {
-               setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
-       }
-       // Do it
-       if (parent)
-               parent->removeAttachmentChild(m_id);
-}
-
-void UnitSAO::addAttachmentChild(int child_id)
-{
-       m_attachment_child_ids.insert(child_id);
-}
-
-void UnitSAO::removeAttachmentChild(int child_id)
-{
-       m_attachment_child_ids.erase(child_id);
-}
-
-const std::unordered_set<int> &UnitSAO::getAttachmentChildIds() const
-{
-       return m_attachment_child_ids;
-}
-
-void UnitSAO::onAttach(int parent_id)
-{
-       if (!parent_id)
-               return;
-
-       ServerActiveObject *parent = m_env->getActiveObject(parent_id);
-
-       if (!parent || parent->isGone())
-               return; // Do not try to notify soon gone parent
-
-       if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY) {
-               // Call parent's on_attach field
-               m_env->getScriptIface()->luaentity_on_attach_child(parent_id, this);
-       }
-}
-
-void UnitSAO::onDetach(int parent_id)
-{
-       if (!parent_id)
-               return;
-
-       ServerActiveObject *parent = m_env->getActiveObject(parent_id);
-       if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
-               m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
-
-       if (!parent || parent->isGone())
-               return; // Do not try to notify soon gone parent
-
-       if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
-               m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this);
-}
-
-ObjectProperties* UnitSAO::accessObjectProperties()
-{
-       return &m_prop;
-}
-
-void UnitSAO::notifyObjectPropertiesModified()
-{
-       m_properties_sent = false;
-}
-
-std::string UnitSAO::generateUpdateAttachmentCommand() const
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_ATTACH_TO);
-       // parameters
-       writeS16(os, m_attachment_parent_id);
-       os << serializeString(m_attachment_bone);
-       writeV3F32(os, m_attachment_position);
-       writeV3F32(os, m_attachment_rotation);
-       return os.str();
-}
-
-std::string UnitSAO::generateUpdateBonePositionCommand(const std::string &bone,
-       const v3f &position, const v3f &rotation)
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_SET_BONE_POSITION);
-       // parameters
-       os << serializeString(bone);
-       writeV3F32(os, position);
-       writeV3F32(os, rotation);
-       return os.str();
-}
-
-
-std::string UnitSAO::generateUpdateAnimationSpeedCommand() const
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_SET_ANIMATION_SPEED);
-       // parameters
-       writeF32(os, m_animation_speed);
-       return os.str();
-}
-
-std::string UnitSAO::generateUpdateAnimationCommand() const
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_SET_ANIMATION);
-       // parameters
-       writeV2F32(os, m_animation_range);
-       writeF32(os, m_animation_speed);
-       writeF32(os, m_animation_blend);
-       // these are sent inverted so we get true when the server sends nothing
-       writeU8(os, !m_animation_loop);
-       return os.str();
-}
-
-
-std::string UnitSAO::generateUpdateArmorGroupsCommand() const
-{
-       std::ostringstream os(std::ios::binary);
-       writeU8(os, AO_CMD_UPDATE_ARMOR_GROUPS);
-       writeU16(os, m_armor_groups.size());
-       for (const auto &armor_group : m_armor_groups) {
-               os<<serializeString(armor_group.first);
-               writeS16(os, armor_group.second);
-       }
-       return os.str();
-}
-
-
-std::string UnitSAO::generateUpdatePositionCommand(const v3f &position, const v3f &velocity,
-       const v3f &acceleration, const v3f &rotation, bool do_interpolate, bool is_movement_end,
-       f32 update_interval)
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_UPDATE_POSITION);
-       // pos
-       writeV3F32(os, position);
-       // velocity
-       writeV3F32(os, velocity);
-       // acceleration
-       writeV3F32(os, acceleration);
-       // rotation
-       writeV3F32(os, rotation);
-       // do_interpolate
-       writeU8(os, do_interpolate);
-       // is_end_position (for interpolation)
-       writeU8(os, is_movement_end);
-       // update_interval (for interpolation)
-       writeF32(os, update_interval);
-       return os.str();
-}
-
-
-std::string UnitSAO::generateSetPropertiesCommand(const ObjectProperties &prop) const
-{
-       std::ostringstream os(std::ios::binary);
-       writeU8(os, AO_CMD_SET_PROPERTIES);
-       prop.serialize(os);
-       return os.str();
-}
-
-std::string UnitSAO::generatePunchCommand(u16 result_hp) const
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_PUNCHED);
-       // result_hp
-       writeU16(os, result_hp);
-       return os.str();
-}
-
-void UnitSAO::sendPunchCommand()
-{
-       m_messages_out.emplace(getId(), true, generatePunchCommand(getHP()));
-}
-
-
 /*
        LuaEntitySAO
 */
index e0304299a40df7d751bc1c48063d5714de4b233e..32ab922d66c4588008b4779b7a0866d66817ba0f 100644 (file)
@@ -21,100 +21,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 
 #include "network/networkprotocol.h"
 #include "util/numeric.h"
-#include "serverobject.h"
+#include "server/unit_sao.h"
 #include "itemgroup.h"
-#include "object_properties.h"
 #include "constants.h"
 
-class UnitSAO: public ServerActiveObject
-{
-public:
-       UnitSAO(ServerEnvironment *env, v3f pos);
-       virtual ~UnitSAO() = default;
-
-       void setRotation(v3f rotation) { m_rotation = rotation; }
-       const v3f &getRotation() const { return m_rotation; }
-       v3f getRadRotation() { return m_rotation * core::DEGTORAD; }
-
-       // Deprecated
-       f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }
-
-       u16 getHP() const { return m_hp; }
-       // Use a function, if isDead can be defined by other conditions
-       bool isDead() const { return m_hp == 0; }
-
-       inline bool isAttached() const
-       { return getParent(); }
-
-       inline bool isImmortal() const
-       { return itemgroup_get(getArmorGroups(), "immortal"); }
-
-       void setArmorGroups(const ItemGroupList &armor_groups);
-       const ItemGroupList &getArmorGroups() const;
-       void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
-       void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
-       void setAnimationSpeed(float frame_speed);
-       void setBonePosition(const std::string &bone, v3f position, v3f rotation);
-       void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
-       void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
-       void getAttachment(int *parent_id, std::string *bone, v3f *position,
-                       v3f *rotation) const;
-       void clearChildAttachments();
-       void clearParentAttachment();
-       void addAttachmentChild(int child_id);
-       void removeAttachmentChild(int child_id);
-       const std::unordered_set<int> &getAttachmentChildIds() const;
-       ServerActiveObject *getParent() const;
-       ObjectProperties* accessObjectProperties();
-       void notifyObjectPropertiesModified();
-
-       std::string generateUpdateAttachmentCommand() const;
-       std::string generateUpdateAnimationSpeedCommand() const;
-       std::string generateUpdateAnimationCommand() const;
-       std::string generateUpdateArmorGroupsCommand() const;
-       static std::string generateUpdatePositionCommand(const v3f &position, const v3f &velocity,
-                       const v3f &acceleration, const v3f &rotation, bool do_interpolate,
-                       bool is_movement_end, f32 update_interval);
-       std::string generateSetPropertiesCommand(const ObjectProperties &prop) const;
-       void sendPunchCommand();
-       static std::string generateUpdateBonePositionCommand(const std::string &bone,
-               const v3f &position, const v3f &rotation);
-
-protected:
-       u16 m_hp = 1;
-
-       v3f m_rotation;
-
-       bool m_properties_sent = true;
-       ObjectProperties m_prop;
-
-       ItemGroupList m_armor_groups;
-       bool m_armor_groups_sent = false;
-
-       v2f m_animation_range;
-       float m_animation_speed = 0.0f;
-       float m_animation_blend = 0.0f;
-       bool m_animation_loop = true;
-       bool m_animation_sent = false;
-       bool m_animation_speed_sent = false;
-
-       // Stores position and rotation for each bone name
-       std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
-       bool m_bone_position_sent = false;
-
-       int m_attachment_parent_id = 0;
-       std::unordered_set<int> m_attachment_child_ids;
-       std::string m_attachment_bone = "";
-       v3f m_attachment_position;
-       v3f m_attachment_rotation;
-       bool m_attachment_sent = false;
-private:
-       void onAttach(int parent_id);
-       void onDetach(int parent_id);
-
-       std::string generatePunchCommand(u16 result_hp) const;
-};
-
 /*
        LuaEntitySAO needs some internals exposed.
 */
index 4835c3fad7e13dc573fc101c23e63a9104a508d5..fe885447c5480460f26754bd73f5f5e03d88c5fc 100644 (file)
@@ -42,7 +42,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "profiler.h"
 #include "scripting_server.h"
 #include "server.h"
-#include "serverobject.h"
 #include "settings.h"
 #include "voxel.h"
 
index c997be3ff75ed5d6a6254fa8be0554748025163e..6751f39e434f40a99682597c8b4b554787425b8f 100644 (file)
@@ -21,7 +21,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "environment.h"
 #include "collision.h"
 #include "raycast.h"
-#include "serverobject.h"
 #include "scripting_server.h"
 #include "server.h"
 #include "daynightratio.h"
index 5a24f95a448fb6fbafc273e183acaeb7fd4cb81f..b6f4649014ae6c4635e4d3878cb0f7771df3d72e 100644 (file)
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "log.h"
 #include "serverenvironment.h"
 #include "scripting_server.h"
-#include "serverobject.h"
+#include "server/serveractiveobject.h"
 #include "settings.h"
 #include "craftdef.h"
 #include "rollback_interface.h"
index 8a863c044206fa93d477b64d639f5f6971efaca5..f473f725dbcdf488ad49dba20a3092cfde6508a3 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 "serverobject.h"
 #include "content_sao.h"
 #include "nodedef.h"
 #include "voxelalgorithms.h"
index accbb1a875465a14aff9d11e81da2543d34061fc..60f12052fde5eee425ed63d340ac4add56772eec 100644 (file)
@@ -29,7 +29,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "server.h"
 #include "log.h"
 #include "tool.h"
-#include "serverobject.h"
+#include "server/serveractiveobject.h"
 #include "porting.h"
 #include "mapgen/mg_schematic.h"
 #include "noise.h"
index f234a15d4565e2b02f8fb7c974fcc8468fcafa32..16c20eeae2671dafbd80c82824542f7c7ba8c497 100644 (file)
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "cpp_api/s_security.h"
 #include "lua_api/l_object.h"
 #include "common/c_converter.h"
-#include "serverobject.h"
+#include "server/serveractiveobject.h"
 #include "filesys.h"
 #include "content/mods.h"
 #include "porting.h"
index 5cd978d73cbb31417c44dccde1ff7a1c906d1f70..1ea144a1cd606268350b5e35297d7b023e077b56 100644 (file)
@@ -27,7 +27,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "common/c_content.h"
 #include "log.h"
 #include "tool.h"
-#include "serverobject.h"
+#include "server/serveractiveobject.h"
 #include "content_sao.h"
 #include "remoteplayer.h"
 #include "server.h"
index 062fe07985cbdf90d021655bab2b28286e8eed3e..529466f6b23cf6025a0056cf661c2ce23b100e0c 100644 (file)
@@ -34,7 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include "version.h"
 #include "filesys.h"
 #include "mapblock.h"
-#include "serverobject.h"
+#include "server/serveractiveobject.h"
 #include "settings.h"
 #include "profiler.h"
 #include "log.h"
index e964c69ff9d8e4dc06760a1242b803c155854337..9fa5ed9fac94c2002f69a640ee172db1268282d2 100644 (file)
@@ -1,4 +1,6 @@
 set(server_SRCS
        ${CMAKE_CURRENT_SOURCE_DIR}/activeobjectmgr.cpp
        ${CMAKE_CURRENT_SOURCE_DIR}/mods.cpp
+       ${CMAKE_CURRENT_SOURCE_DIR}/serveractiveobject.cpp
+       ${CMAKE_CURRENT_SOURCE_DIR}/unit_sao.cpp
        PARENT_SCOPE)
index a502ac6edccaaf2852a602d73da4ab605300f2e2..5fea1bea67623fdd71c2190d5609a37375e90f99 100644 (file)
@@ -22,7 +22,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #include <functional>
 #include <vector>
 #include "../activeobjectmgr.h"
-#include "serverobject.h"
+#include "serveractiveobject.h"
 
 namespace server
 {
diff --git a/src/server/serveractiveobject.cpp b/src/server/serveractiveobject.cpp
new file mode 100644 (file)
index 0000000..3aa78c7
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+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 "serveractiveobject.h"
+#include <fstream>
+#include "inventory.h"
+#include "constants.h" // BS
+#include "log.h"
+
+ServerActiveObject::ServerActiveObject(ServerEnvironment *env, v3f pos):
+       ActiveObject(0),
+       m_env(env),
+       m_base_position(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;
+}
+
+ItemStack ServerActiveObject::getWieldedItem(ItemStack *selected, ItemStack *hand) const
+{
+       *selected = ItemStack();
+       if (hand)
+               *hand = ItemStack();
+
+       return ItemStack();
+}
+
+bool ServerActiveObject::setWieldedItem(const ItemStack &item)
+{
+       return false;
+}
+
+std::string ServerActiveObject::generateUpdateInfantCommand(u16 infant_id, u16 protocol_version)
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_SPAWN_INFANT);
+       // parameters
+       writeU16(os, infant_id);
+       writeU8(os, getSendType());
+       os << serializeLongString(getClientInitializationData(protocol_version));
+       return os.str();
+}
+
+std::string ServerActiveObject::generateUpdateNametagAttributesCommand(const video::SColor &color) const
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_UPDATE_NAMETAG_ATTRIBUTES);
+       // parameters
+       writeU8(os, 1); // version for forward compatibility
+       writeARGB8(os, color);
+       return os.str();
+}
+
+void ServerActiveObject::dumpAOMessagesToQueue(std::queue<ActiveObjectMessage> &queue)
+{
+       while (!m_messages_out.empty()) {
+               queue.push(m_messages_out.front());
+               m_messages_out.pop();
+       }
+}
\ No newline at end of file
diff --git a/src/server/serveractiveobject.h b/src/server/serveractiveobject.h
new file mode 100644 (file)
index 0000000..2e013a6
--- /dev/null
@@ -0,0 +1,265 @@
+/*
+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 <unordered_set>
+#include "irrlichttypes_bloated.h"
+#include "activeobject.h"
+#include "inventorymanager.h"
+#include "itemgroup.h"
+#include "util/container.h"
+
+/*
+
+Some planning
+-------------
+
+* Server environment adds an active object, which gets the id 1
+* The active object list is scanned for each client once in a while,
+  and it finds out what objects have been added that are not known
+  by the client yet. This scan is initiated by the Server class and
+  the result ends up directly to the server.
+* A network packet is created with the info and sent to the client.
+* Environment converts objects to static data and static data to
+  objects, based on how close players are to them.
+
+*/
+
+class ServerEnvironment;
+struct ItemStack;
+struct ToolCapabilities;
+struct ObjectProperties;
+struct PlayerHPChangeReason;
+
+class ServerActiveObject : public ActiveObject
+{
+public:
+       /*
+               NOTE: m_env can be NULL, but step() isn't called if it is.
+               Prototypes are used that way.
+       */
+       ServerActiveObject(ServerEnvironment *env, v3f pos);
+       virtual ~ServerActiveObject() = default;
+
+       virtual ActiveObjectType getSendType() const
+       { return getType(); }
+
+       // Called after id has been set and has been inserted in environment
+       virtual void addedToEnvironment(u32 dtime_s){};
+       // Called before removing from environment
+       virtual void removingFromEnvironment(){};
+       // Returns true if object's deletion is the job of the
+       // environment
+       virtual bool environmentDeletes() const
+       { return true; }
+
+       // Create a certain type of ServerActiveObject
+       static ServerActiveObject* create(ActiveObjectType type,
+                       ServerEnvironment *env, u16 id, v3f pos,
+                       const std::string &data);
+
+       /*
+               Some simple getters/setters
+       */
+       v3f getBasePosition() const { return m_base_position; }
+       void setBasePosition(v3f pos){ m_base_position = pos; }
+       ServerEnvironment* getEnv(){ return m_env; }
+
+       /*
+               Some more dynamic interface
+       */
+
+       virtual void setPos(const v3f &pos)
+               { setBasePosition(pos); }
+       // continuous: if true, object does not stop immediately at pos
+       virtual void moveTo(v3f pos, bool continuous)
+               { setBasePosition(pos); }
+       // If object has moved less than this and data has not changed,
+       // saving to disk may be omitted
+       virtual float getMinimumSavedMovement();
+
+       virtual std::string getDescription(){return "SAO";}
+
+       /*
+               Step object in time.
+               Messages added to messages are sent to client over network.
+
+               send_recommended:
+                       True at around 5-10 times a second, same for all objects.
+                       This is used to let objects send most of the data at the
+                       same time so that the data can be combined in a single
+                       packet.
+       */
+       virtual void step(float dtime, bool send_recommended){}
+
+       /*
+               The return value of this is passed to the client-side object
+               when it is created
+       */
+       virtual std::string getClientInitializationData(u16 protocol_version) {return "";}
+
+       /*
+               The return value of this is passed to the server-side object
+               when it is created (converted from static to active - actually
+               the data is the static form)
+       */
+       virtual void getStaticData(std::string *result) const
+       {
+               assert(isStaticAllowed());
+               *result = "";
+       }
+       /*
+               Return false in here to never save and instead remove object
+               on unload. getStaticData() will not be called in that case.
+       */
+       virtual bool isStaticAllowed() const
+       {return true;}
+
+       // Returns tool wear
+       virtual u16 punch(v3f dir,
+                       const ToolCapabilities *toolcap = nullptr,
+                       ServerActiveObject *puncher = nullptr,
+                       float time_from_last_punch = 1000000.0f)
+       { return 0; }
+       virtual void rightClick(ServerActiveObject *clicker)
+       {}
+       virtual void setHP(s32 hp, const PlayerHPChangeReason &reason)
+       {}
+       virtual u16 getHP() const
+       { return 0; }
+
+       virtual void setArmorGroups(const ItemGroupList &armor_groups)
+       {}
+       virtual const ItemGroupList &getArmorGroups() const
+       { static ItemGroupList rv; return rv; }
+       virtual void setPhysicsOverride(float physics_override_speed, float physics_override_jump, float physics_override_gravity)
+       {}
+       virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
+       {}
+       virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
+       {}
+       virtual void setAnimationSpeed(float frame_speed)
+       {}
+       virtual void setBonePosition(const std::string &bone, v3f position, v3f rotation)
+       {}
+       virtual void getBonePosition(const std::string &bone, v3f *position, v3f *lotation)
+       {}
+       virtual const std::unordered_set<int> &getAttachmentChildIds() const
+       { static std::unordered_set<int> rv; return rv; }
+       virtual ServerActiveObject *getParent() const { return nullptr; }
+       virtual ObjectProperties* accessObjectProperties()
+       { return NULL; }
+       virtual void notifyObjectPropertiesModified()
+       {}
+
+       // Inventory and wielded item
+       virtual Inventory *getInventory() const
+       { return NULL; }
+       virtual InventoryLocation getInventoryLocation() const
+       { return InventoryLocation(); }
+       virtual void setInventoryModified()
+       {}
+       virtual std::string getWieldList() const
+       { return ""; }
+       virtual u16 getWieldIndex() const
+       { return 0; }
+       virtual ItemStack getWieldedItem(ItemStack *selected,
+                       ItemStack *hand = nullptr) const;
+       virtual bool setWieldedItem(const ItemStack &item);
+       inline void attachParticleSpawner(u32 id)
+       {
+               m_attached_particle_spawners.insert(id);
+       }
+       inline void detachParticleSpawner(u32 id)
+       {
+               m_attached_particle_spawners.erase(id);
+       }
+
+       std::string generateUpdateInfantCommand(u16 infant_id, u16 protocol_version);
+       std::string generateUpdateNametagAttributesCommand(const video::SColor &color) const;
+
+       void dumpAOMessagesToQueue(std::queue<ActiveObjectMessage> &queue);
+
+       /*
+               Number of players which know about this object. Object won't be
+               deleted until this is 0 to keep the id preserved for the right
+               object.
+       */
+       u16 m_known_by_count = 0;
+
+       /*
+               - Whether this object is to be removed when nobody knows about
+                 it anymore.
+               - Removal is delayed to preserve the id for the time during which
+                 it could be confused to some other object by some client.
+               - This is usually set to true by the step() method when the object wants
+                 to be deleted but can be set by anything else too.
+       */
+       bool m_pending_removal = false;
+
+       /*
+               Same purpose as m_pending_removal but for deactivation.
+               deactvation = save static data in block, remove active object
+
+               If this is set alongside with m_pending_removal, removal takes
+               priority.
+       */
+       bool m_pending_deactivation = false;
+
+       /*
+               A getter that unifies the above to answer the question:
+               "Can the environment still interact with this object?"
+       */
+       inline bool isGone() const
+       { return m_pending_removal || m_pending_deactivation; }
+
+       /*
+               Whether the object's static data has been stored to a block
+       */
+       bool m_static_exists = false;
+       /*
+               The block from which the object was loaded from, and in which
+               a copy of the static data resides.
+       */
+       v3s16 m_static_block = v3s16(1337,1337,1337);
+
+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;
+
+       /*
+               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;
+};
diff --git a/src/server/unit_sao.cpp b/src/server/unit_sao.cpp
new file mode 100644 (file)
index 0000000..66be675
--- /dev/null
@@ -0,0 +1,318 @@
+/*
+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 "unit_sao.h"
+#include "scripting_server.h"
+#include "serverenvironment.h"
+
+/*
+       UnitSAO
+ */
+
+UnitSAO::UnitSAO(ServerEnvironment *env, v3f pos):
+       ServerActiveObject(env, pos)
+{
+       // Initialize something to armor groups
+       m_armor_groups["fleshy"] = 100;
+}
+
+ServerActiveObject *UnitSAO::getParent() const
+{
+       if (!m_attachment_parent_id)
+               return nullptr;
+       // Check if the parent still exists
+       ServerActiveObject *obj = m_env->getActiveObject(m_attachment_parent_id);
+
+       return obj;
+}
+
+void UnitSAO::setArmorGroups(const ItemGroupList &armor_groups)
+{
+       m_armor_groups = armor_groups;
+       m_armor_groups_sent = false;
+}
+
+const ItemGroupList &UnitSAO::getArmorGroups() const
+{
+       return m_armor_groups;
+}
+
+void UnitSAO::setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop)
+{
+       // store these so they can be updated to clients
+       m_animation_range = frame_range;
+       m_animation_speed = frame_speed;
+       m_animation_blend = frame_blend;
+       m_animation_loop = frame_loop;
+       m_animation_sent = false;
+}
+
+void UnitSAO::getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop)
+{
+       *frame_range = m_animation_range;
+       *frame_speed = m_animation_speed;
+       *frame_blend = m_animation_blend;
+       *frame_loop = m_animation_loop;
+}
+
+void UnitSAO::setAnimationSpeed(float frame_speed)
+{
+       m_animation_speed = frame_speed;
+       m_animation_speed_sent = false;
+}
+
+void UnitSAO::setBonePosition(const std::string &bone, v3f position, v3f rotation)
+{
+       // store these so they can be updated to clients
+       m_bone_position[bone] = core::vector2d<v3f>(position, rotation);
+       m_bone_position_sent = false;
+}
+
+void UnitSAO::getBonePosition(const std::string &bone, v3f *position, v3f *rotation)
+{
+       *position = m_bone_position[bone].X;
+       *rotation = m_bone_position[bone].Y;
+}
+
+void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation)
+{
+       // Attachments need to be handled on both the server and client.
+       // If we just attach on the server, we can only copy the position of the parent. Attachments
+       // are still sent to clients at an interval so players might see them lagging, plus we can't
+       // read and attach to skeletal bones.
+       // If we just attach on the client, the server still sees the child at its original location.
+       // This breaks some things so we also give the server the most accurate representation
+       // even if players only see the client changes.
+
+       int old_parent = m_attachment_parent_id;
+       m_attachment_parent_id = parent_id;
+       m_attachment_bone = bone;
+       m_attachment_position = position;
+       m_attachment_rotation = rotation;
+       m_attachment_sent = false;
+
+       if (parent_id != old_parent) {
+               onDetach(old_parent);
+               onAttach(parent_id);
+       }
+}
+
+void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
+       v3f *rotation) const
+{
+       *parent_id = m_attachment_parent_id;
+       *bone = m_attachment_bone;
+       *position = m_attachment_position;
+       *rotation = m_attachment_rotation;
+}
+
+void UnitSAO::clearChildAttachments()
+{
+       for (int child_id : m_attachment_child_ids) {
+               // Child can be NULL if it was deleted earlier
+               if (ServerActiveObject *child = m_env->getActiveObject(child_id))
+                       child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
+       }
+       m_attachment_child_ids.clear();
+}
+
+void UnitSAO::clearParentAttachment()
+{
+       ServerActiveObject *parent = nullptr;
+       if (m_attachment_parent_id) {
+               parent = m_env->getActiveObject(m_attachment_parent_id);
+               setAttachment(0, "", m_attachment_position, m_attachment_rotation);
+       } else {
+               setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
+       }
+       // Do it
+       if (parent)
+               parent->removeAttachmentChild(m_id);
+}
+
+void UnitSAO::addAttachmentChild(int child_id)
+{
+       m_attachment_child_ids.insert(child_id);
+}
+
+void UnitSAO::removeAttachmentChild(int child_id)
+{
+       m_attachment_child_ids.erase(child_id);
+}
+
+const std::unordered_set<int> &UnitSAO::getAttachmentChildIds() const
+{
+       return m_attachment_child_ids;
+}
+
+void UnitSAO::onAttach(int parent_id)
+{
+       if (!parent_id)
+               return;
+
+       ServerActiveObject *parent = m_env->getActiveObject(parent_id);
+
+       if (!parent || parent->isGone())
+               return; // Do not try to notify soon gone parent
+
+       if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY) {
+               // Call parent's on_attach field
+               m_env->getScriptIface()->luaentity_on_attach_child(parent_id, this);
+       }
+}
+
+void UnitSAO::onDetach(int parent_id)
+{
+       if (!parent_id)
+               return;
+
+       ServerActiveObject *parent = m_env->getActiveObject(parent_id);
+       if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
+               m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
+
+       if (!parent || parent->isGone())
+               return; // Do not try to notify soon gone parent
+
+       if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
+               m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this);
+}
+
+ObjectProperties* UnitSAO::accessObjectProperties()
+{
+       return &m_prop;
+}
+
+void UnitSAO::notifyObjectPropertiesModified()
+{
+       m_properties_sent = false;
+}
+
+std::string UnitSAO::generateUpdateAttachmentCommand() const
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_ATTACH_TO);
+       // parameters
+       writeS16(os, m_attachment_parent_id);
+       os << serializeString(m_attachment_bone);
+       writeV3F32(os, m_attachment_position);
+       writeV3F32(os, m_attachment_rotation);
+       return os.str();
+}
+
+std::string UnitSAO::generateUpdateBonePositionCommand(const std::string &bone,
+       const v3f &position, const v3f &rotation)
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_SET_BONE_POSITION);
+       // parameters
+       os << serializeString(bone);
+       writeV3F32(os, position);
+       writeV3F32(os, rotation);
+       return os.str();
+}
+
+
+std::string UnitSAO::generateUpdateAnimationSpeedCommand() const
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_SET_ANIMATION_SPEED);
+       // parameters
+       writeF32(os, m_animation_speed);
+       return os.str();
+}
+
+std::string UnitSAO::generateUpdateAnimationCommand() const
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_SET_ANIMATION);
+       // parameters
+       writeV2F32(os, m_animation_range);
+       writeF32(os, m_animation_speed);
+       writeF32(os, m_animation_blend);
+       // these are sent inverted so we get true when the server sends nothing
+       writeU8(os, !m_animation_loop);
+       return os.str();
+}
+
+
+std::string UnitSAO::generateUpdateArmorGroupsCommand() const
+{
+       std::ostringstream os(std::ios::binary);
+       writeU8(os, AO_CMD_UPDATE_ARMOR_GROUPS);
+       writeU16(os, m_armor_groups.size());
+       for (const auto &armor_group : m_armor_groups) {
+               os<<serializeString(armor_group.first);
+               writeS16(os, armor_group.second);
+       }
+       return os.str();
+}
+
+
+std::string UnitSAO::generateUpdatePositionCommand(const v3f &position, const v3f &velocity,
+       const v3f &acceleration, const v3f &rotation, bool do_interpolate, bool is_movement_end,
+       f32 update_interval)
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_UPDATE_POSITION);
+       // pos
+       writeV3F32(os, position);
+       // velocity
+       writeV3F32(os, velocity);
+       // acceleration
+       writeV3F32(os, acceleration);
+       // rotation
+       writeV3F32(os, rotation);
+       // do_interpolate
+       writeU8(os, do_interpolate);
+       // is_end_position (for interpolation)
+       writeU8(os, is_movement_end);
+       // update_interval (for interpolation)
+       writeF32(os, update_interval);
+       return os.str();
+}
+
+
+std::string UnitSAO::generateSetPropertiesCommand(const ObjectProperties &prop) const
+{
+       std::ostringstream os(std::ios::binary);
+       writeU8(os, AO_CMD_SET_PROPERTIES);
+       prop.serialize(os);
+       return os.str();
+}
+
+std::string UnitSAO::generatePunchCommand(u16 result_hp) const
+{
+       std::ostringstream os(std::ios::binary);
+       // command
+       writeU8(os, AO_CMD_PUNCHED);
+       // result_hp
+       writeU16(os, result_hp);
+       return os.str();
+}
+
+void UnitSAO::sendPunchCommand()
+{
+       m_messages_out.emplace(getId(), true, generatePunchCommand(getHP()));
+}
diff --git a/src/server/unit_sao.h b/src/server/unit_sao.h
new file mode 100644 (file)
index 0000000..d0e20fb
--- /dev/null
@@ -0,0 +1,113 @@
+/*
+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 "object_properties.h"
+#include "serveractiveobject.h"
+
+class UnitSAO: public ServerActiveObject
+{
+public:
+       UnitSAO(ServerEnvironment *env, v3f pos);
+       virtual ~UnitSAO() = default;
+
+       void setRotation(v3f rotation) { m_rotation = rotation; }
+       const v3f &getRotation() const { return m_rotation; }
+       v3f getRadRotation() { return m_rotation * core::DEGTORAD; }
+
+       // Deprecated
+       f32 getRadYawDep() const { return (m_rotation.Y + 90.) * core::DEGTORAD; }
+
+       u16 getHP() const { return m_hp; }
+       // Use a function, if isDead can be defined by other conditions
+       bool isDead() const { return m_hp == 0; }
+
+       inline bool isAttached() const
+       { return getParent(); }
+
+       inline bool isImmortal() const
+       { return itemgroup_get(getArmorGroups(), "immortal"); }
+
+       void setArmorGroups(const ItemGroupList &armor_groups);
+       const ItemGroupList &getArmorGroups() const;
+       void setAnimation(v2f frame_range, float frame_speed, float frame_blend, bool frame_loop);
+       void getAnimation(v2f *frame_range, float *frame_speed, float *frame_blend, bool *frame_loop);
+       void setAnimationSpeed(float frame_speed);
+       void setBonePosition(const std::string &bone, v3f position, v3f rotation);
+       void getBonePosition(const std::string &bone, v3f *position, v3f *rotation);
+       void setAttachment(int parent_id, const std::string &bone, v3f position, v3f rotation);
+       void getAttachment(int *parent_id, std::string *bone, v3f *position,
+                       v3f *rotation) const;
+       void clearChildAttachments();
+       void clearParentAttachment();
+       void addAttachmentChild(int child_id);
+       void removeAttachmentChild(int child_id);
+       const std::unordered_set<int> &getAttachmentChildIds() const;
+       ServerActiveObject *getParent() const;
+       ObjectProperties* accessObjectProperties();
+       void notifyObjectPropertiesModified();
+
+       std::string generateUpdateAttachmentCommand() const;
+       std::string generateUpdateAnimationSpeedCommand() const;
+       std::string generateUpdateAnimationCommand() const;
+       std::string generateUpdateArmorGroupsCommand() const;
+       static std::string generateUpdatePositionCommand(const v3f &position, const v3f &velocity,
+                       const v3f &acceleration, const v3f &rotation, bool do_interpolate,
+                       bool is_movement_end, f32 update_interval);
+       std::string generateSetPropertiesCommand(const ObjectProperties &prop) const;
+       void sendPunchCommand();
+       static std::string generateUpdateBonePositionCommand(const std::string &bone,
+               const v3f &position, const v3f &rotation);
+
+protected:
+       u16 m_hp = 1;
+
+       v3f m_rotation;
+
+       bool m_properties_sent = true;
+       ObjectProperties m_prop;
+
+       ItemGroupList m_armor_groups;
+       bool m_armor_groups_sent = false;
+
+       v2f m_animation_range;
+       float m_animation_speed = 0.0f;
+       float m_animation_blend = 0.0f;
+       bool m_animation_loop = true;
+       bool m_animation_sent = false;
+       bool m_animation_speed_sent = false;
+
+       // Stores position and rotation for each bone name
+       std::unordered_map<std::string, core::vector2d<v3f>> m_bone_position;
+       bool m_bone_position_sent = false;
+
+       int m_attachment_parent_id = 0;
+       std::unordered_set<int> m_attachment_child_ids;
+       std::string m_attachment_bone = "";
+       v3f m_attachment_position;
+       v3f m_attachment_rotation;
+       bool m_attachment_sent = false;
+private:
+       void onAttach(int parent_id);
+       void onDetach(int parent_id);
+
+       std::string generatePunchCommand(u16 result_hp) const;
+};
diff --git a/src/serverobject.cpp b/src/serverobject.cpp
deleted file mode 100644 (file)
index 119a41b..0000000
+++ /dev/null
@@ -1,114 +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 "serverobject.h"
-#include <fstream>
-#include "inventory.h"
-#include "constants.h" // BS
-#include "log.h"
-
-ServerActiveObject::ServerActiveObject(ServerEnvironment *env, v3f pos):
-       ActiveObject(0),
-       m_env(env),
-       m_base_position(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;
-}
-
-ItemStack ServerActiveObject::getWieldedItem(ItemStack *selected, ItemStack *hand) const
-{
-       *selected = ItemStack();
-       if (hand)
-               *hand = ItemStack();
-
-       return ItemStack();
-}
-
-bool ServerActiveObject::setWieldedItem(const ItemStack &item)
-{
-       return false;
-}
-
-std::string ServerActiveObject::generateUpdateInfantCommand(u16 infant_id, u16 protocol_version)
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_SPAWN_INFANT);
-       // parameters
-       writeU16(os, infant_id);
-       writeU8(os, getSendType());
-       os << serializeLongString(getClientInitializationData(protocol_version));
-       return os.str();
-}
-
-std::string ServerActiveObject::generateUpdateNametagAttributesCommand(const video::SColor &color) const
-{
-       std::ostringstream os(std::ios::binary);
-       // command
-       writeU8(os, AO_CMD_UPDATE_NAMETAG_ATTRIBUTES);
-       // parameters
-       writeU8(os, 1); // version for forward compatibility
-       writeARGB8(os, color);
-       return os.str();
-}
-
-void ServerActiveObject::dumpAOMessagesToQueue(std::queue<ActiveObjectMessage> &queue)
-{
-       while (!m_messages_out.empty()) {
-               queue.push(m_messages_out.front());
-               m_messages_out.pop();
-       }
-}
\ No newline at end of file
diff --git a/src/serverobject.h b/src/serverobject.h
deleted file mode 100644 (file)
index 2e013a6..0000000
+++ /dev/null
@@ -1,265 +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 <unordered_set>
-#include "irrlichttypes_bloated.h"
-#include "activeobject.h"
-#include "inventorymanager.h"
-#include "itemgroup.h"
-#include "util/container.h"
-
-/*
-
-Some planning
--------------
-
-* Server environment adds an active object, which gets the id 1
-* The active object list is scanned for each client once in a while,
-  and it finds out what objects have been added that are not known
-  by the client yet. This scan is initiated by the Server class and
-  the result ends up directly to the server.
-* A network packet is created with the info and sent to the client.
-* Environment converts objects to static data and static data to
-  objects, based on how close players are to them.
-
-*/
-
-class ServerEnvironment;
-struct ItemStack;
-struct ToolCapabilities;
-struct ObjectProperties;
-struct PlayerHPChangeReason;
-
-class ServerActiveObject : public ActiveObject
-{
-public:
-       /*
-               NOTE: m_env can be NULL, but step() isn't called if it is.
-               Prototypes are used that way.
-       */
-       ServerActiveObject(ServerEnvironment *env, v3f pos);
-       virtual ~ServerActiveObject() = default;
-
-       virtual ActiveObjectType getSendType() const
-       { return getType(); }
-
-       // Called after id has been set and has been inserted in environment
-       virtual void addedToEnvironment(u32 dtime_s){};
-       // Called before removing from environment
-       virtual void removingFromEnvironment(){};
-       // Returns true if object's deletion is the job of the
-       // environment
-       virtual bool environmentDeletes() const
-       { return true; }
-
-       // Create a certain type of ServerActiveObject
-       static ServerActiveObject* create(ActiveObjectType type,
-                       ServerEnvironment *env, u16 id, v3f pos,
-                       const std::string &data);
-
-       /*
-               Some simple getters/setters
-       */
-       v3f getBasePosition() const { return m_base_position; }
-       void setBasePosition(v3f pos){ m_base_position = pos; }
-       ServerEnvironment* getEnv(){ return m_env; }
-
-       /*
-               Some more dynamic interface
-       */
-
-       virtual void setPos(const v3f &pos)
-               { setBasePosition(pos); }
-       // continuous: if true, object does not stop immediately at pos
-       virtual void moveTo(v3f pos, bool continuous)
-               { setBasePosition(pos); }
-       // If object has moved less than this and data has not changed,
-       // saving to disk may be omitted
-       virtual float getMinimumSavedMovement();
-
-       virtual std::string getDescription(){return "SAO";}
-
-       /*
-               Step object in time.
-               Messages added to messages are sent to client over network.
-
-               send_recommended:
-                       True at around 5-10 times a second, same for all objects.
-                       This is used to let objects send most of the data at the
-                       same time so that the data can be combined in a single
-                       packet.
-       */
-       virtual void step(float dtime, bool send_recommended){}
-
-       /*
-               The return value of this is passed to the client-side object
-               when it is created
-       */
-       virtual std::string getClientInitializationData(u16 protocol_version) {return "";}
-
-       /*
-               The return value of this is passed to the server-side object
-               when it is created (converted from static to active - actually
-               the data is the static form)
-       */
-       virtual void getStaticData(std::string *result) const
-       {
-               assert(isStaticAllowed());
-               *result = "";
-       }
-       /*
-               Return false in here to never save and instead remove object
-               on unload. getStaticData() will not be called in that case.
-       */
-       virtual bool isStaticAllowed() const
-       {return true;}
-
-       // Returns tool wear
-       virtual u16 punch(v3f dir,
-                       const ToolCapabilities *toolcap = nullptr,
-                       ServerActiveObject *puncher = nullptr,
-                       float time_from_last_punch = 1000000.0f)
-       { return 0; }
-       virtual void rightClick(ServerActiveObject *clicker)
-       {}
-       virtual void setHP(s32 hp, const PlayerHPChangeReason &reason)
-       {}
-       virtual u16 getHP() const
-       { return 0; }
-
-       virtual void setArmorGroups(const ItemGroupList &armor_groups)
-       {}
-       virtual const ItemGroupList &getArmorGroups() const
-       { static ItemGroupList rv; return rv; }
-       virtual void setPhysicsOverride(float physics_override_speed, float physics_override_jump, float physics_override_gravity)
-       {}
-       virtual void setAnimation(v2f frames, float frame_speed, float frame_blend, bool frame_loop)
-       {}
-       virtual void getAnimation(v2f *frames, float *frame_speed, float *frame_blend, bool *frame_loop)
-       {}
-       virtual void setAnimationSpeed(float frame_speed)
-       {}
-       virtual void setBonePosition(const std::string &bone, v3f position, v3f rotation)
-       {}
-       virtual void getBonePosition(const std::string &bone, v3f *position, v3f *lotation)
-       {}
-       virtual const std::unordered_set<int> &getAttachmentChildIds() const
-       { static std::unordered_set<int> rv; return rv; }
-       virtual ServerActiveObject *getParent() const { return nullptr; }
-       virtual ObjectProperties* accessObjectProperties()
-       { return NULL; }
-       virtual void notifyObjectPropertiesModified()
-       {}
-
-       // Inventory and wielded item
-       virtual Inventory *getInventory() const
-       { return NULL; }
-       virtual InventoryLocation getInventoryLocation() const
-       { return InventoryLocation(); }
-       virtual void setInventoryModified()
-       {}
-       virtual std::string getWieldList() const
-       { return ""; }
-       virtual u16 getWieldIndex() const
-       { return 0; }
-       virtual ItemStack getWieldedItem(ItemStack *selected,
-                       ItemStack *hand = nullptr) const;
-       virtual bool setWieldedItem(const ItemStack &item);
-       inline void attachParticleSpawner(u32 id)
-       {
-               m_attached_particle_spawners.insert(id);
-       }
-       inline void detachParticleSpawner(u32 id)
-       {
-               m_attached_particle_spawners.erase(id);
-       }
-
-       std::string generateUpdateInfantCommand(u16 infant_id, u16 protocol_version);
-       std::string generateUpdateNametagAttributesCommand(const video::SColor &color) const;
-
-       void dumpAOMessagesToQueue(std::queue<ActiveObjectMessage> &queue);
-
-       /*
-               Number of players which know about this object. Object won't be
-               deleted until this is 0 to keep the id preserved for the right
-               object.
-       */
-       u16 m_known_by_count = 0;
-
-       /*
-               - Whether this object is to be removed when nobody knows about
-                 it anymore.
-               - Removal is delayed to preserve the id for the time during which
-                 it could be confused to some other object by some client.
-               - This is usually set to true by the step() method when the object wants
-                 to be deleted but can be set by anything else too.
-       */
-       bool m_pending_removal = false;
-
-       /*
-               Same purpose as m_pending_removal but for deactivation.
-               deactvation = save static data in block, remove active object
-
-               If this is set alongside with m_pending_removal, removal takes
-               priority.
-       */
-       bool m_pending_deactivation = false;
-
-       /*
-               A getter that unifies the above to answer the question:
-               "Can the environment still interact with this object?"
-       */
-       inline bool isGone() const
-       { return m_pending_removal || m_pending_deactivation; }
-
-       /*
-               Whether the object's static data has been stored to a block
-       */
-       bool m_static_exists = false;
-       /*
-               The block from which the object was loaded from, and in which
-               a copy of the static data resides.
-       */
-       v3s16 m_static_block = v3s16(1337,1337,1337);
-
-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;
-
-       /*
-               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 7b2fd823605544e2d7e3e2b5d879634443774278..816ec2c59bbcaa560ce077bc5f642bb050c3b048 100644 (file)
@@ -402,16 +402,14 @@ src/script/scripting_server.cpp
 src/script/scripting_server.h
 src/serialization.cpp
 src/serialization.h
-src/serveractiveobjectmap.cpp
-src/serveractiveobjectmap.h
 src/server.cpp
 src/serverenvironment.cpp
 src/serverenvironment.h
 src/server.h
 src/serverlist.cpp
 src/serverlist.h
-src/serverobject.cpp
-src/serverobject.h
+src/server/serveractiveobject.cpp
+src/server/serveractiveobject.h
 src/settings.cpp
 src/settings.h
 src/settings_translation_file.cpp