mirror of
https://github.com/minetest/minetest.git
synced 2024-11-26 17:43:45 +01:00
Rework object attachment handling to fix bugs (#14825)
This commit is contained in:
parent
a0e33ba9ea
commit
85e717fcd1
@ -5100,12 +5100,15 @@ Callbacks:
|
|||||||
to the object (not necessarily an actual rightclick)
|
to the object (not necessarily an actual rightclick)
|
||||||
* `clicker`: an `ObjectRef` (may or may not be a player)
|
* `clicker`: an `ObjectRef` (may or may not be a player)
|
||||||
* `on_attach_child(self, child)`
|
* `on_attach_child(self, child)`
|
||||||
* `child`: an `ObjectRef` of the child that attaches
|
* Called after another object is attached to this object.
|
||||||
|
* `child`: an `ObjectRef` of the child
|
||||||
* `on_detach_child(self, child)`
|
* `on_detach_child(self, child)`
|
||||||
* `child`: an `ObjectRef` of the child that detaches
|
* Called after another object has detached from this object.
|
||||||
|
* `child`: an `ObjectRef` of the child
|
||||||
* `on_detach(self, parent)`
|
* `on_detach(self, parent)`
|
||||||
* `parent`: an `ObjectRef` (can be `nil`) from where it got detached
|
* Called after detaching from another object.
|
||||||
* This happens before the parent object is removed from the world
|
* `parent`: an `ObjectRef` from where it got detached
|
||||||
|
* Note: this is also called before removal from the world.
|
||||||
* `get_staticdata(self)`
|
* `get_staticdata(self)`
|
||||||
* Should return a string that will be passed to `on_activate` when the
|
* Should return a string that will be passed to `on_activate` when the
|
||||||
object is instantiated the next time.
|
object is instantiated the next time.
|
||||||
|
@ -40,12 +40,36 @@ core.register_entity("unittests:callbacks", {
|
|||||||
end,
|
end,
|
||||||
on_attach_child = function(self, child)
|
on_attach_child = function(self, child)
|
||||||
insert_log("on_attach_child(%s)", objref_str(self, child))
|
insert_log("on_attach_child(%s)", objref_str(self, child))
|
||||||
|
assert(child:get_attach() == self.object)
|
||||||
|
local ok = false
|
||||||
|
for _, obj in ipairs(self.object:get_children()) do
|
||||||
|
if obj == child then
|
||||||
|
ok = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(ok, "Child not found in get_children")
|
||||||
end,
|
end,
|
||||||
on_detach_child = function(self, child)
|
on_detach_child = function(self, child)
|
||||||
insert_log("on_detach_child(%s)", objref_str(self, child))
|
insert_log("on_detach_child(%s)", objref_str(self, child))
|
||||||
|
assert(child:get_attach() == nil)
|
||||||
|
local ok = true
|
||||||
|
for _, obj in ipairs(self.object:get_children()) do
|
||||||
|
if obj == child then
|
||||||
|
ok = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(ok, "Former child found in get_children")
|
||||||
end,
|
end,
|
||||||
on_detach = function(self, parent)
|
on_detach = function(self, parent)
|
||||||
insert_log("on_detach(%s)", objref_str(self, parent))
|
insert_log("on_detach(%s)", objref_str(self, parent))
|
||||||
|
assert(self.object:get_attach() == nil)
|
||||||
|
local ok = true
|
||||||
|
for _, obj in ipairs(parent:get_children()) do
|
||||||
|
if obj == self.object then
|
||||||
|
ok = false
|
||||||
|
end
|
||||||
|
end
|
||||||
|
assert(ok, "Former child found in get_children")
|
||||||
end,
|
end,
|
||||||
get_staticdata = function(self)
|
get_staticdata = function(self)
|
||||||
assert(false)
|
assert(false)
|
||||||
@ -118,19 +142,25 @@ local function test_entity_attach(player, pos)
|
|||||||
-- attach player to entity
|
-- attach player to entity
|
||||||
player:set_attach(obj)
|
player:set_attach(obj)
|
||||||
check_log({"on_attach_child(player)"})
|
check_log({"on_attach_child(player)"})
|
||||||
|
assert(player:get_attach() == obj)
|
||||||
player:set_detach()
|
player:set_detach()
|
||||||
check_log({"on_detach_child(player)"})
|
check_log({"on_detach_child(player)"})
|
||||||
|
assert(player:get_attach() == nil)
|
||||||
|
|
||||||
-- attach entity to player
|
-- attach entity to player
|
||||||
obj:set_attach(player)
|
obj:set_attach(player)
|
||||||
check_log({})
|
check_log({})
|
||||||
|
assert(obj:get_attach() == player)
|
||||||
obj:set_detach()
|
obj:set_detach()
|
||||||
check_log({"on_detach(player)"})
|
check_log({"on_detach(player)"})
|
||||||
|
assert(obj:get_attach() == nil)
|
||||||
|
|
||||||
obj:remove()
|
obj:remove()
|
||||||
end
|
end
|
||||||
unittests.register("test_entity_attach", test_entity_attach, {player=true, map=true})
|
unittests.register("test_entity_attach", test_entity_attach, {player=true, map=true})
|
||||||
|
|
||||||
|
---------
|
||||||
|
|
||||||
core.register_entity("unittests:dummy", {
|
core.register_entity("unittests:dummy", {
|
||||||
initial_properties = {
|
initial_properties = {
|
||||||
hp_max = 1,
|
hp_max = 1,
|
||||||
|
@ -152,17 +152,19 @@ typedef std::unordered_map<std::string, BoneOverride> BoneOverrideMap;
|
|||||||
class ActiveObject
|
class ActiveObject
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
ActiveObject(u16 id):
|
typedef u16 object_t;
|
||||||
|
|
||||||
|
ActiveObject(object_t id):
|
||||||
m_id(id)
|
m_id(id)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
u16 getId() const
|
object_t getId() const
|
||||||
{
|
{
|
||||||
return m_id;
|
return m_id;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setId(u16 id)
|
void setId(object_t id)
|
||||||
{
|
{
|
||||||
m_id = id;
|
m_id = id;
|
||||||
}
|
}
|
||||||
@ -193,14 +195,22 @@ public:
|
|||||||
virtual bool collideWithObjects() const = 0;
|
virtual bool collideWithObjects() const = 0;
|
||||||
|
|
||||||
|
|
||||||
virtual void setAttachment(int parent_id, const std::string &bone, v3f position,
|
virtual void setAttachment(object_t parent_id, const std::string &bone, v3f position,
|
||||||
v3f rotation, bool force_visible) {}
|
v3f rotation, bool force_visible) {}
|
||||||
virtual void getAttachment(int *parent_id, std::string *bone, v3f *position,
|
virtual void getAttachment(object_t *parent_id, std::string *bone, v3f *position,
|
||||||
v3f *rotation, bool *force_visible) const {}
|
v3f *rotation, bool *force_visible) const {}
|
||||||
|
// Detach all children
|
||||||
virtual void clearChildAttachments() {}
|
virtual void clearChildAttachments() {}
|
||||||
virtual void clearParentAttachment() {}
|
// Detach from parent
|
||||||
virtual void addAttachmentChild(int child_id) {}
|
virtual void clearParentAttachment()
|
||||||
virtual void removeAttachmentChild(int child_id) {}
|
{
|
||||||
|
setAttachment(0, "", v3f(), v3f(), false);
|
||||||
|
}
|
||||||
|
|
||||||
|
// To be be called from setAttachment() and descendants, but not manually!
|
||||||
|
virtual void addAttachmentChild(object_t child_id) {}
|
||||||
|
virtual void removeAttachmentChild(object_t child_id) {}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
u16 m_id; // 0 is invalid, "no id"
|
object_t m_id; // 0 is invalid, "no id"
|
||||||
};
|
};
|
||||||
|
@ -368,7 +368,7 @@ void ClientEnvironment::addActiveObject(u16 id, u8 type,
|
|||||||
void ClientEnvironment::removeActiveObject(u16 id)
|
void ClientEnvironment::removeActiveObject(u16 id)
|
||||||
{
|
{
|
||||||
// Get current attachment childs to detach them visually
|
// Get current attachment childs to detach them visually
|
||||||
std::unordered_set<int> attachment_childs;
|
std::unordered_set<ClientActiveObject::object_t> attachment_childs;
|
||||||
if (auto *obj = getActiveObject(id))
|
if (auto *obj = getActiveObject(id))
|
||||||
attachment_childs = obj->getAttachmentChildIds();
|
attachment_childs = obj->getAttachmentChildIds();
|
||||||
|
|
||||||
|
@ -57,8 +57,8 @@ public:
|
|||||||
virtual bool isLocalPlayer() const { return false; }
|
virtual bool isLocalPlayer() const { return false; }
|
||||||
|
|
||||||
virtual ClientActiveObject *getParent() const { return nullptr; };
|
virtual ClientActiveObject *getParent() const { return nullptr; };
|
||||||
virtual const std::unordered_set<int> &getAttachmentChildIds() const
|
virtual const std::unordered_set<object_t> &getAttachmentChildIds() const
|
||||||
{ static std::unordered_set<int> rv; return rv; }
|
{ static std::unordered_set<object_t> rv; return rv; }
|
||||||
virtual void updateAttachments() {};
|
virtual void updateAttachments() {};
|
||||||
|
|
||||||
virtual bool doShowSelectionBox() { return true; }
|
virtual bool doShowSelectionBox() { return true; }
|
||||||
|
@ -465,7 +465,7 @@ scene::IAnimatedMeshSceneNode *GenericCAO::getAnimatedMeshSceneNode() const
|
|||||||
|
|
||||||
void GenericCAO::setChildrenVisible(bool toset)
|
void GenericCAO::setChildrenVisible(bool toset)
|
||||||
{
|
{
|
||||||
for (u16 cao_id : m_attachment_child_ids) {
|
for (object_t cao_id : m_attachment_child_ids) {
|
||||||
GenericCAO *obj = m_env->getGenericCAO(cao_id);
|
GenericCAO *obj = m_env->getGenericCAO(cao_id);
|
||||||
if (obj) {
|
if (obj) {
|
||||||
// Check if the entity is forced to appear in first person.
|
// Check if the entity is forced to appear in first person.
|
||||||
@ -474,10 +474,10 @@ void GenericCAO::setChildrenVisible(bool toset)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCAO::setAttachment(int parent_id, const std::string &bone,
|
void GenericCAO::setAttachment(object_t parent_id, const std::string &bone,
|
||||||
v3f position, v3f rotation, bool force_visible)
|
v3f position, v3f rotation, bool force_visible)
|
||||||
{
|
{
|
||||||
int old_parent = m_attachment_parent_id;
|
const auto old_parent = m_attachment_parent_id;
|
||||||
m_attachment_parent_id = parent_id;
|
m_attachment_parent_id = parent_id;
|
||||||
m_attachment_bone = bone;
|
m_attachment_bone = bone;
|
||||||
m_attachment_position = position;
|
m_attachment_position = position;
|
||||||
@ -509,7 +509,7 @@ void GenericCAO::setAttachment(int parent_id, const std::string &bone,
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
|
void GenericCAO::getAttachment(object_t *parent_id, std::string *bone, v3f *position,
|
||||||
v3f *rotation, bool *force_visible) const
|
v3f *rotation, bool *force_visible) const
|
||||||
{
|
{
|
||||||
*parent_id = m_attachment_parent_id;
|
*parent_id = m_attachment_parent_id;
|
||||||
@ -523,29 +523,21 @@ void GenericCAO::clearChildAttachments()
|
|||||||
{
|
{
|
||||||
// Cannot use for-loop here: setAttachment() modifies 'm_attachment_child_ids'!
|
// Cannot use for-loop here: setAttachment() modifies 'm_attachment_child_ids'!
|
||||||
while (!m_attachment_child_ids.empty()) {
|
while (!m_attachment_child_ids.empty()) {
|
||||||
int child_id = *m_attachment_child_ids.begin();
|
const auto child_id = *m_attachment_child_ids.begin();
|
||||||
|
|
||||||
if (ClientActiveObject *child = m_env->getActiveObject(child_id))
|
if (auto *child = m_env->getActiveObject(child_id))
|
||||||
child->setAttachment(0, "", v3f(), v3f(), false);
|
child->clearParentAttachment();
|
||||||
|
else
|
||||||
removeAttachmentChild(child_id);
|
removeAttachmentChild(child_id);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCAO::clearParentAttachment()
|
void GenericCAO::addAttachmentChild(object_t child_id)
|
||||||
{
|
|
||||||
if (m_attachment_parent_id)
|
|
||||||
setAttachment(0, "", m_attachment_position, m_attachment_rotation, false);
|
|
||||||
else
|
|
||||||
setAttachment(0, "", v3f(), v3f(), false);
|
|
||||||
}
|
|
||||||
|
|
||||||
void GenericCAO::addAttachmentChild(int child_id)
|
|
||||||
{
|
{
|
||||||
m_attachment_child_ids.insert(child_id);
|
m_attachment_child_ids.insert(child_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void GenericCAO::removeAttachmentChild(int child_id)
|
void GenericCAO::removeAttachmentChild(object_t child_id)
|
||||||
{
|
{
|
||||||
m_attachment_child_ids.erase(child_id);
|
m_attachment_child_ids.erase(child_id);
|
||||||
}
|
}
|
||||||
|
@ -106,8 +106,8 @@ private:
|
|||||||
// stores position and rotation for each bone name
|
// stores position and rotation for each bone name
|
||||||
BoneOverrideMap m_bone_override;
|
BoneOverrideMap m_bone_override;
|
||||||
|
|
||||||
int m_attachment_parent_id = 0;
|
object_t m_attachment_parent_id = 0;
|
||||||
std::unordered_set<int> m_attachment_child_ids;
|
std::unordered_set<object_t> m_attachment_child_ids;
|
||||||
std::string m_attachment_bone = "";
|
std::string m_attachment_bone = "";
|
||||||
v3f m_attachment_position;
|
v3f m_attachment_position;
|
||||||
v3f m_attachment_rotation;
|
v3f m_attachment_rotation;
|
||||||
@ -226,16 +226,15 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
void setChildrenVisible(bool toset);
|
void setChildrenVisible(bool toset);
|
||||||
void setAttachment(int parent_id, const std::string &bone, v3f position,
|
void setAttachment(object_t parent_id, const std::string &bone, v3f position,
|
||||||
v3f rotation, bool force_visible) override;
|
v3f rotation, bool force_visible) override;
|
||||||
void getAttachment(int *parent_id, std::string *bone, v3f *position,
|
void getAttachment(object_t *parent_id, std::string *bone, v3f *position,
|
||||||
v3f *rotation, bool *force_visible) const override;
|
v3f *rotation, bool *force_visible) const override;
|
||||||
void clearChildAttachments() override;
|
void clearChildAttachments() override;
|
||||||
void clearParentAttachment() override;
|
void addAttachmentChild(object_t child_id) override;
|
||||||
void addAttachmentChild(int child_id) override;
|
void removeAttachmentChild(object_t child_id) override;
|
||||||
void removeAttachmentChild(int child_id) override;
|
|
||||||
ClientActiveObject *getParent() const override;
|
ClientActiveObject *getParent() const override;
|
||||||
const std::unordered_set<int> &getAttachmentChildIds() const override
|
const std::unordered_set<object_t> &getAttachmentChildIds() const override
|
||||||
{ return m_attachment_child_ids; }
|
{ return m_attachment_child_ids; }
|
||||||
void updateAttachments() override;
|
void updateAttachments() override;
|
||||||
|
|
||||||
|
@ -37,11 +37,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "server/serverinventorymgr.h"
|
#include "server/serverinventorymgr.h"
|
||||||
#include "server/unit_sao.h"
|
#include "server/unit_sao.h"
|
||||||
|
|
||||||
|
using object_t = ServerActiveObject::object_t;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
ObjectRef
|
ObjectRef
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
|
ServerActiveObject* ObjectRef::getobject(ObjectRef *ref)
|
||||||
{
|
{
|
||||||
ServerActiveObject *sao = ref->m_object;
|
ServerActiveObject *sao = ref->m_object;
|
||||||
@ -99,9 +100,6 @@ int ObjectRef::l_remove(lua_State *L)
|
|||||||
if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
|
if (sao->getType() == ACTIVEOBJECT_TYPE_PLAYER)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
sao->clearChildAttachments();
|
|
||||||
sao->clearParentAttachment();
|
|
||||||
|
|
||||||
verbosestream << "ObjectRef::l_remove(): id=" << sao->getId() << std::endl;
|
verbosestream << "ObjectRef::l_remove(): id=" << sao->getId() << std::endl;
|
||||||
sao->markForRemoval();
|
sao->markForRemoval();
|
||||||
return 0;
|
return 0;
|
||||||
@ -724,25 +722,17 @@ int ObjectRef::l_set_attach(lua_State *L)
|
|||||||
if (sao == parent)
|
if (sao == parent)
|
||||||
throw LuaError("ObjectRef::set_attach: attaching object to itself is not allowed.");
|
throw LuaError("ObjectRef::set_attach: attaching object to itself is not allowed.");
|
||||||
|
|
||||||
int parent_id;
|
|
||||||
std::string bone;
|
std::string bone;
|
||||||
v3f position;
|
v3f position;
|
||||||
v3f rotation;
|
v3f rotation;
|
||||||
bool force_visible;
|
bool force_visible;
|
||||||
|
|
||||||
sao->getAttachment(&parent_id, &bone, &position, &rotation, &force_visible);
|
|
||||||
if (parent_id) {
|
|
||||||
ServerActiveObject *old_parent = env->getActiveObject(parent_id);
|
|
||||||
old_parent->removeAttachmentChild(sao->getId());
|
|
||||||
}
|
|
||||||
|
|
||||||
bone = readParam<std::string>(L, 3, "");
|
bone = readParam<std::string>(L, 3, "");
|
||||||
position = readParam<v3f>(L, 4, v3f(0, 0, 0));
|
position = readParam<v3f>(L, 4, v3f(0, 0, 0));
|
||||||
rotation = readParam<v3f>(L, 5, v3f(0, 0, 0));
|
rotation = readParam<v3f>(L, 5, v3f(0, 0, 0));
|
||||||
force_visible = readParam<bool>(L, 6, false);
|
force_visible = readParam<bool>(L, 6, false);
|
||||||
|
|
||||||
sao->setAttachment(parent->getId(), bone, position, rotation, force_visible);
|
sao->setAttachment(parent->getId(), bone, position, rotation, force_visible);
|
||||||
parent->addAttachmentChild(sao->getId());
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -755,7 +745,7 @@ int ObjectRef::l_get_attach(lua_State *L)
|
|||||||
if (sao == nullptr)
|
if (sao == nullptr)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
int parent_id;
|
object_t parent_id;
|
||||||
std::string bone;
|
std::string bone;
|
||||||
v3f position;
|
v3f position;
|
||||||
v3f rotation;
|
v3f rotation;
|
||||||
@ -783,11 +773,11 @@ int ObjectRef::l_get_children(lua_State *L)
|
|||||||
if (sao == nullptr)
|
if (sao == nullptr)
|
||||||
return 0;
|
return 0;
|
||||||
|
|
||||||
const std::unordered_set<int> child_ids = sao->getAttachmentChildIds();
|
const auto &child_ids = sao->getAttachmentChildIds();
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
lua_createtable(L, child_ids.size(), 0);
|
lua_createtable(L, child_ids.size(), 0);
|
||||||
for (const int id : child_ids) {
|
for (const object_t id : child_ids) {
|
||||||
ServerActiveObject *child = env->getActiveObject(id);
|
ServerActiveObject *child = env->getActiveObject(id);
|
||||||
getScriptApiBase(L)->objectrefGetOrCreate(L, child);
|
getScriptApiBase(L)->objectrefGetOrCreate(L, child);
|
||||||
lua_rawseti(L, -2, ++i);
|
lua_rawseti(L, -2, ++i);
|
||||||
|
@ -2926,9 +2926,6 @@ void Server::DeleteClient(session_t peer_id, ClientDeletionReason reason)
|
|||||||
PlayerSAO *playersao = player->getPlayerSAO();
|
PlayerSAO *playersao = player->getPlayerSAO();
|
||||||
assert(playersao);
|
assert(playersao);
|
||||||
|
|
||||||
playersao->clearChildAttachments();
|
|
||||||
playersao->clearParentAttachment();
|
|
||||||
|
|
||||||
// inform connected clients
|
// inform connected clients
|
||||||
const std::string &player_name = player->getName();
|
const std::string &player_name = player->getName();
|
||||||
NetworkPacket notice(TOCLIENT_UPDATE_PLAYER_LIST, 0, PEER_ID_INEXISTENT);
|
NetworkPacket notice(TOCLIENT_UPDATE_PLAYER_LIST, 0, PEER_ID_INEXISTENT);
|
||||||
|
@ -85,23 +85,13 @@ void RemoteClient::ResendBlockIfOnWire(v3s16 p)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
LuaEntitySAO *getAttachedObject(PlayerSAO *sao, ServerEnvironment *env)
|
static LuaEntitySAO *getAttachedObject(PlayerSAO *sao, ServerEnvironment *env)
|
||||||
{
|
{
|
||||||
if (!sao->isAttached())
|
ServerActiveObject *ao = sao;
|
||||||
return nullptr;
|
while (ao->getParent())
|
||||||
|
ao = ao->getParent();
|
||||||
|
|
||||||
int id;
|
return ao == sao ? nullptr : dynamic_cast<LuaEntitySAO*>(ao);
|
||||||
std::string bone;
|
|
||||||
v3f dummy;
|
|
||||||
bool force_visible;
|
|
||||||
sao->getAttachment(&id, &bone, &dummy, &dummy, &force_visible);
|
|
||||||
ServerActiveObject *ao = env->getActiveObject(id);
|
|
||||||
while (id && ao) {
|
|
||||||
ao->getAttachment(&id, &bone, &dummy, &dummy, &force_visible);
|
|
||||||
if (id)
|
|
||||||
ao = env->getActiveObject(id);
|
|
||||||
}
|
|
||||||
return dynamic_cast<LuaEntitySAO *>(ao);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void RemoteClient::GetNextBlocks (
|
void RemoteClient::GetNextBlocks (
|
||||||
|
@ -147,7 +147,7 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If attached, check that our parent is still there. If it isn't, detach.
|
// If attached, check that our parent is still there. If it isn't, detach.
|
||||||
if (m_attachment_parent_id && !isAttached()) {
|
if (m_attachment_parent_id && !getParent()) {
|
||||||
// This is handled when objects are removed from the map
|
// This is handled when objects are removed from the map
|
||||||
warningstream << "LuaEntitySAO::step() " << m_init_name << " at " << m_last_sent_position << ", id=" << m_id <<
|
warningstream << "LuaEntitySAO::step() " << m_init_name << " at " << m_last_sent_position << ", id=" << m_id <<
|
||||||
" is attached to nonexistent parent. This is a bug." << std::endl;
|
" is attached to nonexistent parent. This is a bug." << std::endl;
|
||||||
@ -415,8 +415,6 @@ void LuaEntitySAO::setHP(s32 hp, const PlayerHPChangeReason &reason)
|
|||||||
sendPunchCommand();
|
sendPunchCommand();
|
||||||
|
|
||||||
if (m_hp == 0 && !isGone()) {
|
if (m_hp == 0 && !isGone()) {
|
||||||
clearParentAttachment();
|
|
||||||
clearChildAttachments();
|
|
||||||
if (m_registered) {
|
if (m_registered) {
|
||||||
ServerActiveObject *killer = nullptr;
|
ServerActiveObject *killer = nullptr;
|
||||||
if (reason.type == PlayerHPChangeReason::PLAYER_PUNCH)
|
if (reason.type == PlayerHPChangeReason::PLAYER_PUNCH)
|
||||||
|
@ -81,8 +81,14 @@ public:
|
|||||||
|
|
||||||
protected:
|
protected:
|
||||||
void dispatchScriptDeactivate(bool removal);
|
void dispatchScriptDeactivate(bool removal);
|
||||||
virtual void onMarkedForDeactivation() { dispatchScriptDeactivate(false); }
|
virtual void onMarkedForDeactivation() {
|
||||||
virtual void onMarkedForRemoval() { dispatchScriptDeactivate(true); }
|
UnitSAO::onMarkedForDeactivation();
|
||||||
|
dispatchScriptDeactivate(false);
|
||||||
|
}
|
||||||
|
virtual void onMarkedForRemoval() {
|
||||||
|
UnitSAO::onMarkedForRemoval();
|
||||||
|
dispatchScriptDeactivate(true);
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::string getPropertyPacket();
|
std::string getPropertyPacket();
|
||||||
|
@ -233,13 +233,12 @@ void PlayerSAO::step(float dtime, bool send_recommended)
|
|||||||
}
|
}
|
||||||
|
|
||||||
// If attached, check that our parent is still there. If it isn't, detach.
|
// If attached, check that our parent is still there. If it isn't, detach.
|
||||||
if (m_attachment_parent_id && !isAttached()) {
|
if (m_attachment_parent_id && !getParent()) {
|
||||||
// This is handled when objects are removed from the map
|
// This is handled when objects are removed from the map
|
||||||
warningstream << "PlayerSAO::step() id=" << m_id <<
|
warningstream << "PlayerSAO::step() id=" << m_id <<
|
||||||
" is attached to nonexistent parent. This is a bug." << std::endl;
|
" is attached to nonexistent parent. This is a bug." << std::endl;
|
||||||
clearParentAttachment();
|
clearParentAttachment();
|
||||||
setBasePosition(m_last_good_position);
|
setPos(m_last_good_position);
|
||||||
m_env->getGameDef()->SendMovePlayer(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//dstream<<"PlayerSAO::step: dtime: "<<dtime<<std::endl;
|
//dstream<<"PlayerSAO::step: dtime: "<<dtime<<std::endl;
|
||||||
|
@ -171,8 +171,8 @@ public:
|
|||||||
{ BoneOverride props; return props; }
|
{ BoneOverride props; return props; }
|
||||||
virtual const BoneOverrideMap &getBoneOverrides() const
|
virtual const BoneOverrideMap &getBoneOverrides() const
|
||||||
{ static BoneOverrideMap rv; return rv; }
|
{ static BoneOverrideMap rv; return rv; }
|
||||||
virtual const std::unordered_set<int> &getAttachmentChildIds() const
|
virtual const std::unordered_set<object_t> &getAttachmentChildIds() const
|
||||||
{ static std::unordered_set<int> rv; return rv; }
|
{ static std::unordered_set<object_t> rv; return rv; }
|
||||||
virtual ServerActiveObject *getParent() const { return nullptr; }
|
virtual ServerActiveObject *getParent() const { return nullptr; }
|
||||||
virtual ObjectProperties *accessObjectProperties()
|
virtual ObjectProperties *accessObjectProperties()
|
||||||
{ return NULL; }
|
{ return NULL; }
|
||||||
@ -240,8 +240,8 @@ protected:
|
|||||||
virtual void onMarkedForDeactivation() {}
|
virtual void onMarkedForDeactivation() {}
|
||||||
virtual void onMarkedForRemoval() {}
|
virtual void onMarkedForRemoval() {}
|
||||||
|
|
||||||
virtual void onAttach(int parent_id) {}
|
virtual void onAttach(object_t parent_id) {}
|
||||||
virtual void onDetach(int parent_id) {}
|
virtual void onDetach(object_t parent_id) {}
|
||||||
|
|
||||||
ServerEnvironment *m_env;
|
ServerEnvironment *m_env;
|
||||||
v3f m_base_position;
|
v3f m_base_position;
|
||||||
|
@ -130,50 +130,92 @@ void UnitSAO::sendOutdatedData()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnitSAO::setAttachment(int parent_id, const std::string &bone, v3f position,
|
void UnitSAO::setAttachment(const object_t new_parent, const std::string &bone, v3f position,
|
||||||
v3f rotation, bool force_visible)
|
v3f rotation, bool force_visible)
|
||||||
{
|
{
|
||||||
auto *obj = parent_id ? m_env->getActiveObject(parent_id) : nullptr;
|
const auto call_count = ++m_attachment_call_counter;
|
||||||
if (obj) {
|
|
||||||
// Do checks to avoid circular references
|
const auto check_nesting = [&] (const char *descr) -> bool {
|
||||||
// The chain of wanted parent must not refer or contain "this"
|
// The counter is never decremented, so if it differs that means
|
||||||
for (obj = obj->getParent(); obj; obj = obj->getParent()) {
|
// a nested call to setAttachment() has happened.
|
||||||
if (obj == this) {
|
if (m_attachment_call_counter == call_count)
|
||||||
warningstream << "Mod bug: Attempted to attach object " << m_id << " to parent "
|
return false;
|
||||||
<< parent_id << " but former is an (in)direct parent of latter." << std::endl;
|
verbosestream << "UnitSAO::setAttachment() id=" << m_id <<
|
||||||
return;
|
" nested call detected (" << descr << ")." << std::endl;
|
||||||
|
return true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Do checks to avoid circular references
|
||||||
|
{
|
||||||
|
auto *obj = new_parent ? m_env->getActiveObject(new_parent) : nullptr;
|
||||||
|
if (obj == this) {
|
||||||
|
assert(false);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
bool problem = false;
|
||||||
|
if (obj) {
|
||||||
|
// The chain of wanted parent must not refer or contain "this"
|
||||||
|
for (obj = obj->getParent(); obj; obj = obj->getParent()) {
|
||||||
|
if (obj == this) {
|
||||||
|
problem = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (problem) {
|
||||||
|
warningstream << "Mod bug: Attempted to attach object " << m_id << " to parent "
|
||||||
|
<< new_parent << " but former is an (in)direct parent of latter." << std::endl;
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Attachments need to be handled on both the server and client.
|
// Detach first
|
||||||
// If we just attach on the server, we can only copy the position of the parent.
|
// Note: make sure to apply data changes before running callbacks.
|
||||||
// Attachments are still sent to clients at an interval so players might see them
|
const auto old_parent = m_attachment_parent_id;
|
||||||
// lagging, plus we can't read and attach to skeletal bones. If we just attach on
|
m_attachment_parent_id = 0;
|
||||||
// the client, the server still sees the child at its original location. This
|
m_attachment_sent = false;
|
||||||
// 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;
|
if (old_parent && old_parent != new_parent) {
|
||||||
m_attachment_parent_id = parent_id;
|
auto *parent = m_env->getActiveObject(old_parent);
|
||||||
|
if (parent) {
|
||||||
|
onDetach(parent);
|
||||||
|
} else {
|
||||||
|
warningstream << "UnitSAO::setAttachment() id=" << m_id <<
|
||||||
|
" is attached to nonexistent parent. This is a bug." << std::endl;
|
||||||
|
// we can pretend it never happened
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// The detach callbacks might call to setAttachment() again.
|
if (check_nesting("onDetach")) {
|
||||||
// Ensure the attachment params are applied after this callback is run.
|
// Don't touch anything after the other call has completed.
|
||||||
if (parent_id != old_parent)
|
return;
|
||||||
onDetach(old_parent);
|
}
|
||||||
|
|
||||||
m_attachment_parent_id = parent_id;
|
if (isGone())
|
||||||
|
return;
|
||||||
|
|
||||||
|
// Now attach to new parent
|
||||||
|
m_attachment_parent_id = new_parent;
|
||||||
m_attachment_bone = bone;
|
m_attachment_bone = bone;
|
||||||
m_attachment_position = position;
|
m_attachment_position = position;
|
||||||
m_attachment_rotation = rotation;
|
m_attachment_rotation = rotation;
|
||||||
m_force_visible = force_visible;
|
m_force_visible = force_visible;
|
||||||
m_attachment_sent = false;
|
|
||||||
|
|
||||||
if (parent_id != old_parent)
|
if (new_parent && old_parent != new_parent) {
|
||||||
onAttach(parent_id);
|
auto *parent = m_env->getActiveObject(new_parent);
|
||||||
|
if (parent) {
|
||||||
|
onAttach(parent);
|
||||||
|
} else {
|
||||||
|
warningstream << "UnitSAO::setAttachment() id=" << m_id <<
|
||||||
|
" tried to attach to nonexistent parent. This is a bug." << std::endl;
|
||||||
|
m_attachment_parent_id = 0; // detach
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
check_nesting("onAttach");
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
|
void UnitSAO::getAttachment(object_t *parent_id, std::string *bone, v3f *position,
|
||||||
v3f *rotation, bool *force_visible) const
|
v3f *rotation, bool *force_visible) const
|
||||||
{
|
{
|
||||||
*parent_id = m_attachment_parent_id;
|
*parent_id = m_attachment_parent_id;
|
||||||
@ -183,79 +225,70 @@ void UnitSAO::getAttachment(int *parent_id, std::string *bone, v3f *position,
|
|||||||
*force_visible = m_force_visible;
|
*force_visible = m_force_visible;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void UnitSAO::clearAnyAttachments()
|
||||||
|
{
|
||||||
|
// This is called before this SAO is marked for removal/deletion and unlinks
|
||||||
|
// any parent or child relationships.
|
||||||
|
// This is done at this point and not in ~UnitSAO() so attachments to
|
||||||
|
// "phantom objects" don't stay around while we're waiting to be actually deleted.
|
||||||
|
// (which can take several server steps)
|
||||||
|
clearParentAttachment();
|
||||||
|
clearChildAttachments();
|
||||||
|
}
|
||||||
|
|
||||||
void UnitSAO::clearChildAttachments()
|
void UnitSAO::clearChildAttachments()
|
||||||
{
|
{
|
||||||
// Cannot use for-loop here: setAttachment() modifies 'm_attachment_child_ids'!
|
// Cannot use for-loop here: setAttachment() modifies 'm_attachment_child_ids'!
|
||||||
while (!m_attachment_child_ids.empty()) {
|
while (!m_attachment_child_ids.empty()) {
|
||||||
int child_id = *m_attachment_child_ids.begin();
|
const auto child_id = *m_attachment_child_ids.begin();
|
||||||
|
|
||||||
// Child can be NULL if it was deleted earlier
|
if (auto *child = m_env->getActiveObject(child_id)) {
|
||||||
if (ServerActiveObject *child = m_env->getActiveObject(child_id))
|
child->clearParentAttachment();
|
||||||
child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0), false);
|
} else {
|
||||||
|
// should not happen but we need to handle it to prevent an infinite loop
|
||||||
removeAttachmentChild(child_id);
|
removeAttachmentChild(child_id);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnitSAO::clearParentAttachment()
|
void UnitSAO::addAttachmentChild(object_t child_id)
|
||||||
{
|
|
||||||
ServerActiveObject *parent = nullptr;
|
|
||||||
if (m_attachment_parent_id) {
|
|
||||||
parent = m_env->getActiveObject(m_attachment_parent_id);
|
|
||||||
setAttachment(0, "", m_attachment_position, m_attachment_rotation, false);
|
|
||||||
} else {
|
|
||||||
setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0), false);
|
|
||||||
}
|
|
||||||
// Do it
|
|
||||||
if (parent)
|
|
||||||
parent->removeAttachmentChild(m_id);
|
|
||||||
}
|
|
||||||
|
|
||||||
void UnitSAO::addAttachmentChild(int child_id)
|
|
||||||
{
|
{
|
||||||
m_attachment_child_ids.insert(child_id);
|
m_attachment_child_ids.insert(child_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
void UnitSAO::removeAttachmentChild(int child_id)
|
void UnitSAO::removeAttachmentChild(object_t child_id)
|
||||||
{
|
{
|
||||||
m_attachment_child_ids.erase(child_id);
|
m_attachment_child_ids.erase(child_id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const std::unordered_set<int> &UnitSAO::getAttachmentChildIds() const
|
void UnitSAO::onAttach(ServerActiveObject *parent)
|
||||||
{
|
{
|
||||||
return m_attachment_child_ids;
|
assert(parent);
|
||||||
}
|
|
||||||
|
|
||||||
void UnitSAO::onAttach(int parent_id)
|
parent->addAttachmentChild(m_id);
|
||||||
{
|
|
||||||
if (!parent_id)
|
|
||||||
return;
|
|
||||||
|
|
||||||
ServerActiveObject *parent = m_env->getActiveObject(parent_id);
|
// Do not try to notify soon gone parent
|
||||||
|
if (!parent->isGone()) {
|
||||||
if (!parent || parent->isGone())
|
if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
|
||||||
return; // Do not try to notify soon gone parent
|
m_env->getScriptIface()->luaentity_on_attach_child(parent->getId(), this);
|
||||||
|
|
||||||
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)
|
void UnitSAO::onDetach(ServerActiveObject *parent)
|
||||||
{
|
{
|
||||||
if (!parent_id)
|
assert(parent);
|
||||||
return;
|
|
||||||
|
parent->removeAttachmentChild(m_id);
|
||||||
|
|
||||||
ServerActiveObject *parent = m_env->getActiveObject(parent_id);
|
|
||||||
if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
|
if (getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
|
||||||
m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
|
m_env->getScriptIface()->luaentity_on_detach(m_id, parent);
|
||||||
|
|
||||||
if (!parent || parent->isGone())
|
// callback could affect the parent
|
||||||
return; // Do not try to notify soon gone parent
|
if (parent->isGone())
|
||||||
|
return;
|
||||||
|
|
||||||
if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
|
if (parent->getType() == ACTIVEOBJECT_TYPE_LUAENTITY)
|
||||||
m_env->getScriptIface()->luaentity_on_detach_child(parent_id, this);
|
m_env->getScriptIface()->luaentity_on_detach_child(parent->getId(), this);
|
||||||
}
|
}
|
||||||
|
|
||||||
ObjectProperties *UnitSAO::accessObjectProperties()
|
ObjectProperties *UnitSAO::accessObjectProperties()
|
||||||
|
@ -77,16 +77,17 @@ public:
|
|||||||
|
|
||||||
// Attachments
|
// Attachments
|
||||||
ServerActiveObject *getParent() const;
|
ServerActiveObject *getParent() const;
|
||||||
inline bool isAttached() const { return getParent(); }
|
inline bool isAttached() const { return m_attachment_parent_id != 0; }
|
||||||
void setAttachment(int parent_id, const std::string &bone, v3f position,
|
void setAttachment(object_t parent_id, const std::string &bone, v3f position,
|
||||||
v3f rotation, bool force_visible);
|
v3f rotation, bool force_visible);
|
||||||
void getAttachment(int *parent_id, std::string *bone, v3f *position,
|
void getAttachment(object_t *parent_id, std::string *bone, v3f *position,
|
||||||
v3f *rotation, bool *force_visible) const;
|
v3f *rotation, bool *force_visible) const;
|
||||||
void clearChildAttachments();
|
void clearChildAttachments() override;
|
||||||
void clearParentAttachment();
|
void addAttachmentChild(object_t child_id) override;
|
||||||
void addAttachmentChild(int child_id);
|
void removeAttachmentChild(object_t child_id) override;
|
||||||
void removeAttachmentChild(int child_id);
|
const std::unordered_set<object_t> &getAttachmentChildIds() const {
|
||||||
const std::unordered_set<int> &getAttachmentChildIds() const;
|
return m_attachment_child_ids;
|
||||||
|
}
|
||||||
|
|
||||||
// Object properties
|
// Object properties
|
||||||
ObjectProperties *accessObjectProperties();
|
ObjectProperties *accessObjectProperties();
|
||||||
@ -121,14 +122,28 @@ protected:
|
|||||||
// Stores position and rotation for each bone name
|
// Stores position and rotation for each bone name
|
||||||
std::unordered_map<std::string, BoneOverride> m_bone_override;
|
std::unordered_map<std::string, BoneOverride> m_bone_override;
|
||||||
|
|
||||||
int m_attachment_parent_id = 0;
|
object_t m_attachment_parent_id = 0;
|
||||||
|
|
||||||
|
void clearAnyAttachments();
|
||||||
|
virtual void onMarkedForDeactivation() {
|
||||||
|
ServerActiveObject::onMarkedForDeactivation();
|
||||||
|
clearAnyAttachments();
|
||||||
|
}
|
||||||
|
virtual void onMarkedForRemoval() {
|
||||||
|
ServerActiveObject::onMarkedForRemoval();
|
||||||
|
clearAnyAttachments();
|
||||||
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void onAttach(int parent_id);
|
void onAttach(ServerActiveObject *parent);
|
||||||
void onDetach(int parent_id);
|
void onDetach(ServerActiveObject *parent);
|
||||||
|
|
||||||
std::string generatePunchCommand(u16 result_hp) const;
|
std::string generatePunchCommand(u16 result_hp) const;
|
||||||
|
|
||||||
|
// Used to detect nested calls to setAttachments(), which can happen due to
|
||||||
|
// Lua callbacks
|
||||||
|
u8 m_attachment_call_counter = 0;
|
||||||
|
|
||||||
// Armor groups
|
// Armor groups
|
||||||
bool m_armor_groups_sent = false;
|
bool m_armor_groups_sent = false;
|
||||||
|
|
||||||
@ -144,7 +159,7 @@ private:
|
|||||||
bool m_bone_override_sent = false;
|
bool m_bone_override_sent = false;
|
||||||
|
|
||||||
// Attachments
|
// Attachments
|
||||||
std::unordered_set<int> m_attachment_child_ids;
|
std::unordered_set<object_t> m_attachment_child_ids;
|
||||||
std::string m_attachment_bone = "";
|
std::string m_attachment_bone = "";
|
||||||
v3f m_attachment_position;
|
v3f m_attachment_position;
|
||||||
v3f m_attachment_rotation;
|
v3f m_attachment_rotation;
|
||||||
|
@ -439,6 +439,17 @@ inline u32 npot2(u32 orig) {
|
|||||||
return orig + 1;
|
return orig + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Distance between two values in a wrapped (circular) system
|
||||||
|
template<typename T>
|
||||||
|
inline unsigned wrappedDifference(T a, T b, const T maximum)
|
||||||
|
{
|
||||||
|
if (a > b)
|
||||||
|
std::swap(a, b);
|
||||||
|
// now b >= a
|
||||||
|
unsigned s = b - a, l = static_cast<unsigned>(maximum - b) + a + 1;
|
||||||
|
return std::min(s, l);
|
||||||
|
}
|
||||||
|
|
||||||
// Gradual steps towards the target value in a wrapped (circular) system
|
// Gradual steps towards the target value in a wrapped (circular) system
|
||||||
// using the shorter of both ways
|
// using the shorter of both ways
|
||||||
template<typename T>
|
template<typename T>
|
||||||
|
Loading…
Reference in New Issue
Block a user