Skip to content

Commit 04839f2

Browse files
sfan5nerzhul
authored andcommittedSep 15, 2017
ServerEnv: Clean up object lifecycle handling (#6414)
* ServerEnv: Clean up object lifecycle handling
1 parent edbc533 commit 04839f2

File tree

8 files changed

+175
-213
lines changed

8 files changed

+175
-213
lines changed
 

‎src/content_sao.cpp

+8-12
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class TestSAO : public ServerActiveObject
5959
m_age += dtime;
6060
if(m_age > 10)
6161
{
62-
m_removed = true;
62+
m_pending_removal = true;
6363
return;
6464
}
6565

@@ -397,12 +397,11 @@ void LuaEntitySAO::step(float dtime, bool send_recommended)
397397
{
398398
ServerMap *map = dynamic_cast<ServerMap *>(&m_env->getMap());
399399
assert(map);
400-
if (!m_pending_deactivation &&
400+
if (!m_pending_removal &&
401401
map->saoPositionOverLimit(m_base_position)) {
402-
infostream << "Remove SAO " << m_id << "(" << m_init_name
402+
infostream << "Removing SAO " << m_id << "(" << m_init_name
403403
<< "), outside of limits" << std::endl;
404-
m_pending_deactivation = true;
405-
m_removed = true;
404+
m_pending_removal = true;
406405
return;
407406
}
408407
}
@@ -550,9 +549,9 @@ int LuaEntitySAO::punch(v3f dir,
550549
ServerActiveObject *puncher,
551550
float time_from_last_punch)
552551
{
553-
if (!m_registered){
552+
if (!m_registered) {
554553
// Delete unknown LuaEntities when punched
555-
m_removed = true;
554+
m_pending_removal = true;
556555
return 0;
557556
}
558557

@@ -596,12 +595,10 @@ int LuaEntitySAO::punch(v3f dir,
596595
}
597596

598597
if (getHP() == 0) {
599-
m_removed = true;
598+
m_pending_removal = true;
600599
m_env->getScriptIface()->luaentity_on_death(m_id, puncher);
601600
}
602601

603-
604-
605602
return result.wear;
606603
}
607604

@@ -1353,11 +1350,10 @@ void PlayerSAO::setWieldIndex(int i)
13531350
}
13541351
}
13551352

1356-
// Erase the peer id and make the object for removal
13571353
void PlayerSAO::disconnected()
13581354
{
13591355
m_peer_id = 0;
1360-
m_removed = true;
1356+
m_pending_removal = true;
13611357
}
13621358

13631359
void PlayerSAO::unlinkPlayerSessionAndSave()

‎src/network/serverpackethandler.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -1124,8 +1124,8 @@ void Server::handleCommand_Interact(NetworkPacket* pkt)
11241124
playersao->noCheatDigStart(p_under);
11251125
}
11261126
else if (pointed.type == POINTEDTHING_OBJECT) {
1127-
// Skip if object has been removed
1128-
if (pointed_object->m_removed)
1127+
// Skip if object can't be interacted with anymore
1128+
if (pointed_object->isGone())
11291129
return;
11301130

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

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

12901290
actionstream << player->getName() << " right-clicks object "

‎src/script/cpp_api/s_base.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,10 @@ void ScriptApiBase::objectrefGetOrCreate(lua_State *L,
343343
ObjectRef::create(L, cobj);
344344
} else {
345345
push_objectRef(L, cobj->getId());
346+
if (cobj->isGone())
347+
warningstream << "ScriptApiBase::objectrefGetOrCreate(): "
348+
<< "Pushing ObjectRef to removed/deactivated object"
349+
<< ", this is probably a bug." << std::endl;
346350
}
347351
}
348352

‎src/script/lua_api/l_env.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -642,7 +642,7 @@ int ModApiEnvMod::l_get_objects_inside_radius(lua_State *L)
642642
std::vector<u16>::const_iterator iter = ids.begin();
643643
for(u32 i = 0; iter != ids.end(); ++iter) {
644644
ServerActiveObject *obj = env->getActiveObject(*iter);
645-
if (!obj->m_removed) {
645+
if (!obj->isGone()) {
646646
// Insert object reference into table
647647
script->objectrefGetOrCreate(L, obj);
648648
lua_rawseti(L, -2, ++i);

‎src/script/lua_api/l_object.cpp

+4-5
Original file line numberDiff line numberDiff line change
@@ -140,15 +140,14 @@ int ObjectRef::l_remove(lua_State *L)
140140
return 0;
141141

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

150-
verbosestream<<"ObjectRef::l_remove(): id="<<co->getId()<<std::endl;
151-
co->m_removed = true;
149+
verbosestream << "ObjectRef::l_remove(): id=" << co->getId() << std::endl;
150+
co->m_pending_removal = true;
152151
return 0;
153152
}
154153

‎src/serverenvironment.cpp

+130-180
Large diffs are not rendered by default.

‎src/serverenvironment.h

+10-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ class PlayerDatabase;
3535
class PlayerSAO;
3636
class ServerEnvironment;
3737
class ActiveBlockModifier;
38+
struct StaticObject;
3839
class ServerActiveObject;
3940
class Server;
4041
class ServerScripting;
@@ -368,7 +369,7 @@ class ServerEnvironment : public Environment
368369
u16 addActiveObjectRaw(ServerActiveObject *object, bool set_changed, u32 dtime_s);
369370

370371
/*
371-
Remove all objects that satisfy (m_removed && m_known_by_count==0)
372+
Remove all objects that satisfy (isGone() && m_known_by_count==0)
372373
*/
373374
void removeRemovedObjects();
374375

@@ -388,6 +389,14 @@ class ServerEnvironment : public Environment
388389
*/
389390
void deactivateFarObjects(bool force_delete);
390391

392+
/*
393+
A few helpers used by the three above methods
394+
*/
395+
void deleteStaticFromBlock(
396+
ServerActiveObject *obj, u16 id, u32 mod_reason, bool no_emerge);
397+
bool saveStaticToBlock(v3s16 blockpos, u16 store_id,
398+
ServerActiveObject *obj, const StaticObject &s_obj, u32 mod_reason);
399+
391400
/*
392401
Member variables
393402
*/

‎src/serverobject.h

+14-10
Original file line numberDiff line numberDiff line change
@@ -212,23 +212,27 @@ class ServerActiveObject : public ActiveObject
212212
it anymore.
213213
- Removal is delayed to preserve the id for the time during which
214214
it could be confused to some other object by some client.
215-
- This is set to true by the step() method when the object wants
216-
to be deleted.
217-
- This can be set to true by anything else too.
215+
- This is usually set to true by the step() method when the object wants
216+
to be deleted but can be set by anything else too.
218217
*/
219-
bool m_removed = false;
218+
bool m_pending_removal = false;
220219

221220
/*
222-
This is set to true when an object should be removed from the active
223-
object list but couldn't be removed because the id has to be
224-
reserved for some client.
221+
Same purpose as m_pending_removal but for deactivation.
222+
deactvation = save static data in block, remove active object
225223
226-
The environment checks this periodically. If this is true and also
227-
m_known_by_count is true, object is deleted from the active object
228-
list.
224+
If this is set alongside with m_pending_removal, removal takes
225+
priority.
229226
*/
230227
bool m_pending_deactivation = false;
231228

229+
/*
230+
A getter that unifies the above to answer the question:
231+
"Can the environment still interact with this object?"
232+
*/
233+
inline bool isGone() const
234+
{ return m_pending_removal || m_pending_deactivation; }
235+
232236
/*
233237
Whether the object's static data has been stored to a block
234238
*/

0 commit comments

Comments
 (0)
Please sign in to comment.