Serialize dropsiteSupplies

The assignement has to be done for new dropsite but must not always be
done as it sends a command.

Serialized data shouldn't be too complicated. Because of that the `ent`
property is removed.
This commit is contained in:
phosit 2025-02-03 13:06:19 +01:00
parent 6facc9ee17
commit e07b4df6ba
No known key found for this signature in database
GPG key ID: C9430B600671C268
3 changed files with 29 additions and 21 deletions

View file

@ -111,7 +111,7 @@ BaseManager.prototype.assignEntity = function(gameState, ent)
this.workers.updateEnt(ent);
this.buildings.updateEnt(ent);
if (ent.resourceDropsiteTypes() && !ent.hasClass("Unit"))
this.assignResourceToDropsite(gameState, ent);
this.assignResourceToDropsite(gameState, ent, false);
};
BaseManager.prototype.setAnchor = function(gameState, anchorEntity)
@ -171,7 +171,7 @@ BaseManager.prototype.setAnchorlessEntity = function(gameState, ent)
* Assign the resources around the dropsites of this basis in three areas according to distance, and sort them in each area.
* Moving resources (animals) and buildable resources (fields) are treated elsewhere.
*/
BaseManager.prototype.assignResourceToDropsite = function(gameState, dropsite)
BaseManager.prototype.assignResourceToDropsite = function(gameState, dropsite, deserialized)
{
if (this.dropsites[dropsite.id()])
{
@ -214,11 +214,11 @@ BaseManager.prototype.assignResourceToDropsite = function(gameState, dropsite)
if (dist < maxDistResourceSquare)
{
if (dist < maxDistResourceSquare/16) // distmax/4
nearby.push({ "dropsite": dropsiteId, "id": supply.id(), "ent": supply, "dist": dist });
nearby.push({ "dropsite": dropsiteId, "id": supply.id(), "dist": dist });
else if (dist < maxDistResourceSquare/4) // distmax/2
medium.push({ "dropsite": dropsiteId, "id": supply.id(), "ent": supply, "dist": dist });
medium.push({ "dropsite": dropsiteId, "id": supply.id(), "dist": dist });
else
faraway.push({ "dropsite": dropsiteId, "id": supply.id(), "ent": supply, "dist": dist });
faraway.push({ "dropsite": dropsiteId, "id": supply.id(), "dist": dist });
}
});
@ -231,18 +231,21 @@ BaseManager.prototype.assignResourceToDropsite = function(gameState, dropsite)
if (debug)
{
faraway.forEach(function(res){
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [res.ent.id()], "rgb": [2,0,0]});
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [res.id], "rgb": [2,0,0]});
});
medium.forEach(function(res){
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [res.ent.id()], "rgb": [0,2,0]});
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [res.id], "rgb": [0,2,0]});
});
nearby.forEach(function(res){
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [res.ent.id()], "rgb": [0,0,2]});
Engine.PostCommand(PlayerID,{"type": "set-shading-color", "entities": [res.id], "rgb": [0,0,2]});
});
}
*/
}
if (deserialized)
return;
// Allows all allies to use this dropsite except if base anchor to be sure to keep
// a minimum of resources for this base
Engine.PostCommand(PlayerID, {
@ -418,7 +421,9 @@ BaseManager.prototype.getResourceLevel = function(gameState, type, distances = [
if (check[supply.id]) // avoid double counting as same resource can appear several time
continue;
check[supply.id] = true;
count += supply.ent.resourceSupplyAmount();
const supplyEntity = gameState.getEntityById(supply.id);
if (supplyEntity)
count += supplyEntity.resourceSupplyAmount();
}
return count;
};
@ -1208,7 +1213,8 @@ BaseManager.prototype.Serialize = function()
"gatherers": this.gatherers,
"neededDefenders": this.neededDefenders,
"territoryIndices": this.territoryIndices,
"timeNextIdleCheck": this.timeNextIdleCheck
"timeNextIdleCheck": this.timeNextIdleCheck,
"dropsiteSupplies": this.dropsiteSupplies
};
};

View file

@ -64,7 +64,6 @@ BasesManager.prototype.postinit = function(gameState)
if (baseID === undefined)
continue;
const base = this.getBaseByID(baseID);
base.assignResourceToDropsite(gameState, ent);
}
};
@ -239,7 +238,7 @@ BasesManager.prototype.checkEvents = function(gameState, events)
const base = this.getBaseByID(ent.getMetadata(PlayerID, "base"));
base.buildings.updateEnt(ent);
if (ent.resourceDropsiteTypes())
base.assignResourceToDropsite(gameState, ent);
base.assignResourceToDropsite(gameState, ent, false);
if (ent.getMetadata(PlayerID, "baseAnchor") === true)
{

View file

@ -488,32 +488,35 @@ Worker.prototype.startGathering = function(gameState)
const gatherRates = ent.resourceGatherRates();
for (let i = 0; i < supplies.length; ++i)
{
const entity = gameState.getEntityById(supplies[i].id);
// exhausted resource, remove it from this list
if (!supplies[i].ent || !gameState.getEntityById(supplies[i].id))
if (!entity)
{
supplies.splice(i--, 1);
continue;
}
if (isSupplyFull(gameState, supplies[i].ent))
if (isSupplyFull(gameState, entity))
continue;
const inaccessibleTime = supplies[i].ent.getMetadata(PlayerID, "inaccessibleTime");
const inaccessibleTime = entity.getMetadata(PlayerID, "inaccessibleTime");
if (inaccessibleTime && gameState.ai.elapsedTime < inaccessibleTime)
continue;
const supplyType = supplies[i].ent.get("ResourceSupply/Type");
const supplyType = entity.get("ResourceSupply/Type");
if (!gatherRates[supplyType])
continue;
// check if available resource is worth one additionnal gatherer (except for farms)
const nbGatherers = supplies[i].ent.resourceSupplyNumGatherers() + worker.base.GetTCGatherer(supplies[i].id);
if (supplies[i].ent.resourceSupplyType().specific != "grain" && nbGatherers > 0 &&
supplies[i].ent.resourceSupplyAmount()/(1+nbGatherers) < 30)
const nbGatherers = entity.resourceSupplyNumGatherers() + worker.base.GetTCGatherer(supplies[i].id);
if (entity.resourceSupplyType().specific != "grain" && nbGatherers > 0 &&
entity.resourceSupplyAmount()/(1+nbGatherers) < 30)
{
continue;
}
// not in ennemy territory
const territoryOwner = gameState.ai.HQ.territoryMap.getOwner(supplies[i].ent.position());
const territoryOwner = gameState.ai.HQ.territoryMap.getOwner(entity.position());
if (territoryOwner != 0 && !gameState.isPlayerAlly(territoryOwner)) // player is its own ally
continue;
worker.base.AddTCGatherer(supplies[i].id);
ent.setMetadata(PlayerID, "supply", supplies[i].id);
ret = supplies[i].ent;
ret = entity;
break;
}
return ret;