From e07b4df6ba6ea96e074f1b41e729986c684a03d9 Mon Sep 17 00:00:00 2001 From: phosit Date: Mon, 3 Feb 2025 13:06:19 +0100 Subject: [PATCH] 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. --- .../public/simulation/ai/petra/baseManager.js | 26 ++++++++++++------- .../simulation/ai/petra/basesManager.js | 3 +-- .../mods/public/simulation/ai/petra/worker.js | 21 ++++++++------- 3 files changed, 29 insertions(+), 21 deletions(-) diff --git a/binaries/data/mods/public/simulation/ai/petra/baseManager.js b/binaries/data/mods/public/simulation/ai/petra/baseManager.js index b1150d0d38..ebad42345f 100644 --- a/binaries/data/mods/public/simulation/ai/petra/baseManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/baseManager.js @@ -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 }; }; diff --git a/binaries/data/mods/public/simulation/ai/petra/basesManager.js b/binaries/data/mods/public/simulation/ai/petra/basesManager.js index e4a6f32b4a..b87c3afa6a 100644 --- a/binaries/data/mods/public/simulation/ai/petra/basesManager.js +++ b/binaries/data/mods/public/simulation/ai/petra/basesManager.js @@ -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) { diff --git a/binaries/data/mods/public/simulation/ai/petra/worker.js b/binaries/data/mods/public/simulation/ai/petra/worker.js index 00a7ee7cb6..596e323295 100644 --- a/binaries/data/mods/public/simulation/ai/petra/worker.js +++ b/binaries/data/mods/public/simulation/ai/petra/worker.js @@ -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;