Allow desired entity ID to be specified in addEntity

This commit is contained in:
Kae 2023-07-29 00:52:56 +10:00
parent b17b935c10
commit 35fc2679de
7 changed files with 20 additions and 15 deletions

View File

@ -31,13 +31,15 @@ Maybe<EntityId> EntityMap::maybeReserveEntityId(EntityId entityId) {
if (m_spatialMap.size() >= (size_t)(m_endIdSpace - m_beginIdSpace))
throw EntityMapException("No more entity id space in EntityMap::reserveEntityId");
if (m_spatialMap.contains(entityId))
if (entityId == NullEntityId || m_spatialMap.contains(entityId))
return {};
else
return entityId;
}
EntityId EntityMap::reserveEntityId(EntityId entityId) {
if (entityId == NullEntityId)
return reserveEntityId();
if (auto reserved = maybeReserveEntityId(entityId))
return *reserved;

View File

@ -479,16 +479,19 @@ void UniverseClient::stopLua() {
bool UniverseClient::reloadPlayer(Json const& data, Uuid const& uuid) {
auto player = mainPlayer();
auto world = worldClient();
bool inWorld = player->inWorld();
EntityId entityId = player->entityId();
bool playerInWorld = player->inWorld();
auto world = as<WorldClient>(player->world());
EntityId entityId = (playerInWorld || !world->inWorld())
? player->entityId()
: connectionEntitySpace(world->connection()).first;
if (m_playerReloadPreCallback)
m_playerReloadPreCallback();
if (inWorld)
world->removeEntity(entityId, false);
else {
if (playerInWorld) {
world->removeEntity(player->entityId(), false);
} else {
m_respawning = false;
m_respawnTimer.reset();
}
@ -505,7 +508,7 @@ bool UniverseClient::reloadPlayer(Json const& data, Uuid const& uuid) {
exception = std::current_exception();
}
world->addEntity(player);
world->addEntity(player, entityId);
CelestialCoordinate coordinate = m_systemWorldClient->location();
player->universeMap()->addMappedCoordinate(coordinate);

View File

@ -1203,7 +1203,7 @@ EntityPtr WorldClient::entity(EntityId entityId) const {
return m_entityMap->entity(entityId);
}
void WorldClient::addEntity(EntityPtr const& entity) {
void WorldClient::addEntity(EntityPtr const& entity, EntityId entityId) {
if (!entity)
return;
@ -1211,7 +1211,7 @@ void WorldClient::addEntity(EntityPtr const& entity) {
return;
if (entity->clientEntityMode() != ClientEntityMode::ClientSlaveOnly) {
entity->init(this, m_entityMap->reserveEntityId(), EntityMode::Master);
entity->init(this, m_entityMap->reserveEntityId(entityId), EntityMode::Master);
m_entityMap->addEntity(entity);
} else {
auto entityFactory = Root::singleton().entityFactory();

View File

@ -54,7 +54,7 @@ public:
TileModificationList validTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) const override;
TileModificationList applyTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) override;
EntityPtr entity(EntityId entityId) const override;
void addEntity(EntityPtr const& entity) override;
void addEntity(EntityPtr const& entity, EntityId entityId = NullEntityId) override;
EntityPtr closestEntity(Vec2F const& center, float radius, EntityFilter selector = EntityFilter()) const override;
void forAllEntities(EntityCallback entityCallback) const override;
void forEachEntity(RectF const& boundBox, EntityCallback callback) const override;

View File

@ -713,11 +713,11 @@ EntityPtr WorldServer::entity(EntityId entityId) const {
return m_entityMap->entity(entityId);
}
void WorldServer::addEntity(EntityPtr const& entity) {
void WorldServer::addEntity(EntityPtr const& entity, EntityId entityId) {
if (!entity)
return;
entity->init(this, m_entityMap->reserveEntityId(), EntityMode::Master);
entity->init(this, m_entityMap->reserveEntityId(entityId), EntityMode::Master);
m_entityMap->addEntity(entity);
if (auto tileEntity = as<TileEntity>(entity))

View File

@ -135,7 +135,7 @@ public:
TileModificationList validTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) const override;
TileModificationList applyTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) override;
EntityPtr entity(EntityId entityId) const override;
void addEntity(EntityPtr const& entity) override;
void addEntity(EntityPtr const& entity, EntityId entityId = NullEntityId) override;
EntityPtr closestEntity(Vec2F const& center, float radius, EntityFilter selector = EntityFilter()) const override;
void forAllEntities(EntityCallback entityCallback) const override;
void forEachEntity(RectF const& boundBox, EntityCallback callback) const override;

View File

@ -55,7 +55,7 @@ public:
// passed in pointer directly and initialize it, and entity will have a valid
// id in this world and be ready for use. This is always the case on the
// server, but not *always* the case on the client.
virtual void addEntity(EntityPtr const& entity) = 0;
virtual void addEntity(EntityPtr const& entity, EntityId entityId = NullEntityId) = 0;
virtual EntityPtr closestEntity(Vec2F const& center, float radius, EntityFilter selector = {}) const = 0;