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)) if (m_spatialMap.size() >= (size_t)(m_endIdSpace - m_beginIdSpace))
throw EntityMapException("No more entity id space in EntityMap::reserveEntityId"); throw EntityMapException("No more entity id space in EntityMap::reserveEntityId");
if (m_spatialMap.contains(entityId)) if (entityId == NullEntityId || m_spatialMap.contains(entityId))
return {}; return {};
else else
return entityId; return entityId;
} }
EntityId EntityMap::reserveEntityId(EntityId entityId) { EntityId EntityMap::reserveEntityId(EntityId entityId) {
if (entityId == NullEntityId)
return reserveEntityId();
if (auto reserved = maybeReserveEntityId(entityId)) if (auto reserved = maybeReserveEntityId(entityId))
return *reserved; return *reserved;

View File

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

View File

@ -1203,7 +1203,7 @@ EntityPtr WorldClient::entity(EntityId entityId) const {
return m_entityMap->entity(entityId); return m_entityMap->entity(entityId);
} }
void WorldClient::addEntity(EntityPtr const& entity) { void WorldClient::addEntity(EntityPtr const& entity, EntityId entityId) {
if (!entity) if (!entity)
return; return;
@ -1211,7 +1211,7 @@ void WorldClient::addEntity(EntityPtr const& entity) {
return; return;
if (entity->clientEntityMode() != ClientEntityMode::ClientSlaveOnly) { 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); m_entityMap->addEntity(entity);
} else { } else {
auto entityFactory = Root::singleton().entityFactory(); auto entityFactory = Root::singleton().entityFactory();

View File

@ -54,7 +54,7 @@ public:
TileModificationList validTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) const override; TileModificationList validTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) const override;
TileModificationList applyTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) override; TileModificationList applyTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) override;
EntityPtr entity(EntityId entityId) const 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; EntityPtr closestEntity(Vec2F const& center, float radius, EntityFilter selector = EntityFilter()) const override;
void forAllEntities(EntityCallback entityCallback) const override; void forAllEntities(EntityCallback entityCallback) const override;
void forEachEntity(RectF const& boundBox, EntityCallback callback) 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); return m_entityMap->entity(entityId);
} }
void WorldServer::addEntity(EntityPtr const& entity) { void WorldServer::addEntity(EntityPtr const& entity, EntityId entityId) {
if (!entity) if (!entity)
return; return;
entity->init(this, m_entityMap->reserveEntityId(), EntityMode::Master); entity->init(this, m_entityMap->reserveEntityId(entityId), EntityMode::Master);
m_entityMap->addEntity(entity); m_entityMap->addEntity(entity);
if (auto tileEntity = as<TileEntity>(entity)) if (auto tileEntity = as<TileEntity>(entity))

View File

@ -135,7 +135,7 @@ public:
TileModificationList validTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) const override; TileModificationList validTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) const override;
TileModificationList applyTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) override; TileModificationList applyTileModifications(TileModificationList const& modificationList, bool allowEntityOverlap) override;
EntityPtr entity(EntityId entityId) const 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; EntityPtr closestEntity(Vec2F const& center, float radius, EntityFilter selector = EntityFilter()) const override;
void forAllEntities(EntityCallback entityCallback) const override; void forAllEntities(EntityCallback entityCallback) const override;
void forEachEntity(RectF const& boundBox, EntityCallback callback) 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 // 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 // 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. // 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; virtual EntityPtr closestEntity(Vec2F const& center, float radius, EntityFilter selector = {}) const = 0;