Skip to content

Commit

Permalink
ServerEnv: Clean up object lifecycle handling (#6414)
Browse files Browse the repository at this point in the history
* ServerEnv: Clean up object lifecycle handling
  • Loading branch information
sfan5 authored and nerzhul committed Sep 15, 2017
1 parent edbc533 commit 04839f2
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 213 deletions.
20 changes: 8 additions & 12 deletions src/content_sao.cpp
Expand Up @@ -59,7 +59,7 @@ class TestSAO : public ServerActiveObject
m_age += dtime;
if(m_age > 10)
{
m_removed = true;
m_pending_removal = true;
return;
}

Expand Down Expand Up @@ -397,12 +397,11 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
{
ServerMap *map = dynamic_cast<ServerMap *>(&m_env->getMap());
assert(map);
if (!m_pending_deactivation &&
if (!m_pending_removal &&
map->saoPositionOverLimit(m_base_position)) {
infostream << "Remove SAO " << m_id << "(" << m_init_name
infostream << "Removing SAO " << m_id << "(" << m_init_name
<< "), outside of limits" << std::endl;
m_pending_deactivation = true;
m_removed = true;
m_pending_removal = true;
return;
}
}
Expand Down Expand Up @@ -550,9 +549,9 @@ int LuaEntitySAO::punch(v3f dir,
ServerActiveObject *puncher,
float time_from_last_punch)
{
if (!m_registered){
if (!m_registered) {
// Delete unknown LuaEntities when punched
m_removed = true;
m_pending_removal = true;
return 0;
}

Expand Down Expand Up @@ -596,12 +595,10 @@ int LuaEntitySAO::punch(v3f dir,
}

if (getHP() == 0) {
m_removed = true;
m_pending_removal = true;
m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
}



return result.wear;
}

Expand Down Expand Up @@ -1353,11 +1350,10 @@ void PlayerSAO::setWieldIndex(int i)
}
}

// Erase the peer id and make the object for removal
void PlayerSAO::disconnected()
{
m_peer_id = 0;
m_removed = true;
m_pending_removal = true;
}

void PlayerSAO::unlinkPlayerSessionAndSave()
Expand Down
8 changes: 4 additions & 4 deletions src/network/serverpackethandler.cpp
Expand Up @@ -1124,8 +1124,8 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
playersao->noCheatDigStart(p_under);
}
else if (pointed.type == POINTEDTHING_OBJECT) {
// Skip if object has been removed
if (pointed_object->m_removed)
// Skip if object can't be interacted with anymore
if (pointed_object->isGone())
return;

actionstream<<player->getName()<<" punches object "
Expand Down Expand Up @@ -1283,8 +1283,8 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
if (pointed.type == POINTEDTHING_OBJECT) {
// Right click object

// Skip if object has been removed
if (pointed_object->m_removed)
// Skip if object can't be interacted with anymore
if (pointed_object->isGone())
return;

actionstream << player->getName() << " right-clicks object "
Expand Down
4 changes: 4 additions & 0 deletions src/script/cpp_api/s_base.cpp
Expand Up @@ -343,6 +343,10 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
ObjectRef::create(L, cobj);
} else {
push_objectRef(L, cobj->getId());
if (cobj->isGone())
warningstream << "ScriptApiBase::objectrefGetOrCreate(): "
<< "Pushing ObjectRef to removed/deactivated object"
<< ", this is probably a bug." << std::endl;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/script/lua_api/l_env.cpp
Expand Up @@ -642,7 +642,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
std::vector<u16>::const_iterator iter = ids.begin();
for(u32 i = 0; iter != ids.end(); ++iter) {
ServerActiveObject *obj = env->getActiveObject(*iter);
if (!obj->m_removed) {
if (!obj->isGone()) {
// Insert object reference into table
script->objectrefGetOrCreate(L, obj);
lua_rawseti(L, -2, ++i);
Expand Down
9 changes: 4 additions & 5 deletions src/script/lua_api/l_object.cpp
Expand Up @@ -140,15 +140,14 @@ int ObjectRef::l_remove(lua_State *L)
return 0;

const std::unordered_set<int> &child_ids = co->getAttachmentChildIds();
std::unordered_set<int>::const_iterator it;
for (it = child_ids.begin(); it != child_ids.end(); ++it) {
for (int child_id : child_ids) {
// Child can be NULL if it was deleted earlier
if (ServerActiveObject *child = env->getActiveObject(*it))
if (ServerActiveObject *child = env->getActiveObject(child_id))
child->setAttachment(0, "", v3f(0, 0, 0), v3f(0, 0, 0));
}

verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
co->m_removed = true;
verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
co->m_pending_removal = true;
return 0;
}

Expand Down

0 comments on commit 04839f2

Please sign in to comment.