Reduce usage of entityObj in BasesManager

Some functions in the `BaseManager` expect a complete entity but only
the ID is used. When an entity is destroied the entity isn't available
anymore.
This commit is contained in:
phosit 2025-09-13 18:54:55 +02:00
parent 7b50e32b28
commit 4f6b01ccb8
No known key found for this signature in database
GPG key ID: C9430B600671C268
2 changed files with 23 additions and 18 deletions

View file

@ -135,7 +135,7 @@ BaseManager.prototype.setAnchor = function(gameState, anchorEntity)
};
/* we lost our anchor. Let's reassign our units and buildings */
BaseManager.prototype.anchorLost = function(gameState, ent)
BaseManager.prototype.anchorLost = function(gameState)
{
this.anchor = undefined;
this.anchorId = undefined;
@ -255,25 +255,27 @@ BaseManager.prototype.assignResourceToDropsite = function(gameState, dropsite, d
});
};
BaseManager.prototype.removeFromAssignedDropsite = function(ent)
BaseManager.prototype.removeFromAssignedDropsite = function(entityID)
{
for (const type in this.dropsiteSupplies)
for (const proxim in this.dropsiteSupplies[type])
{
const resourcesList = this.dropsiteSupplies[type][proxim];
for (let i = 0; i < resourcesList.length; ++i)
if (resourcesList[i].id === ent.id())
{
if (resourcesList[i].id === entityID)
resourcesList.splice(i--, 1);
}
}
};
// completely remove the dropsite resources from our list.
BaseManager.prototype.removeDropsite = function(gameState, ent)
BaseManager.prototype.removeDropsite = function(gameState, entityID)
{
if (!ent.id())
if (!entityID)
return;
const removeSupply = function(entId, supply)
const removeSupply = function(supply)
{
for (let i = 0; i < supply.length; ++i)
{
@ -281,19 +283,19 @@ BaseManager.prototype.removeDropsite = function(gameState, ent)
if (!supply[i].ent || !gameState.getEntityById(supply[i].id))
supply.splice(i--, 1);
// resource assigned to the removed dropsite, remove it
else if (supply[i].dropsite == entId)
else if (supply[i].dropsite == entityID)
supply.splice(i--, 1);
}
};
for (const type in this.dropsiteSupplies)
{
removeSupply(ent.id(), this.dropsiteSupplies[type].nearby);
removeSupply(ent.id(), this.dropsiteSupplies[type].medium);
removeSupply(ent.id(), this.dropsiteSupplies[type].faraway);
removeSupply(this.dropsiteSupplies[type].nearby);
removeSupply(this.dropsiteSupplies[type].medium);
removeSupply(this.dropsiteSupplies[type].faraway);
}
this.dropsites[ent.id()] = undefined;
this.dropsites[entityID] = undefined;
};
/**
@ -1075,7 +1077,7 @@ BaseManager.prototype.update = function(gameState, queues, events)
continue;
}
if (ent.resourceDropsiteTypes())
this.removeDropsite(gameState, ent);
this.removeDropsite(gameState, ent.id());
bestBase.assignEntity(gameState, ent);
}
}
@ -1125,7 +1127,7 @@ BaseManager.prototype.update = function(gameState, queues, events)
for (const ent of this.buildings.values())
{
if (ent.resourceDropsiteTypes())
this.removeDropsite(gameState, ent);
this.removeDropsite(gameState, ent.id());
reassignedBase.assignEntity(gameState, ent);
}
return false;

View file

@ -140,7 +140,10 @@ BasesManager.prototype.checkEvents = function(gameState, events)
{
const ent = evt.entityObj;
if (evt?.metadata?.[PlayerID]?.assignedResource)
this.getBaseByID(evt.metadata[PlayerID].base).removeFromAssignedDropsite(ent);
{
this.getBaseByID(evt.metadata[PlayerID].base)
.removeFromAssignedDropsite(evt.entity);
}
if (ent.owner() != PlayerID)
continue;
// A new base foundation was created and destroyed on the same (AI) turn
@ -148,9 +151,9 @@ BasesManager.prototype.checkEvents = function(gameState, events)
continue;
const base = this.getBaseByID(evt.metadata[PlayerID].base);
if (ent.resourceDropsiteTypes() && ent.hasClass("Structure"))
base.removeDropsite(gameState, ent);
base.removeDropsite(gameState, evt.entity);
if (evt.metadata[PlayerID].baseAnchor && evt.metadata[PlayerID].baseAnchor === true)
base.anchorLost(gameState, ent);
base.anchorLost(gameState);
}
}
@ -257,9 +260,9 @@ BasesManager.prototype.checkEvents = function(gameState, events)
continue;
const base = this.getBaseByID(ent.getMetadata(PlayerID, "base"));
if (ent.resourceDropsiteTypes() && ent.hasClass("Structure"))
base.removeDropsite(gameState, ent);
base.removeDropsite(gameState, ent.id());
if (ent.getMetadata(PlayerID, "baseAnchor") === true)
base.anchorLost(gameState, ent);
base.anchorLost(gameState);
}
if (evt.to != PlayerID)