1
1
forked from 0ad/0ad

petra: make some use of mobile dropsites

This was SVN commit r21758.
This commit is contained in:
mimo 2018-04-22 17:00:32 +00:00
parent d5978468d5
commit fe177fb5bf
3 changed files with 29 additions and 10 deletions

View File

@ -48,10 +48,12 @@ m.BaseManager.prototype.init = function(gameState, state)
this.units = gameState.getOwnUnits().filter(API3.Filters.byMetadata(PlayerID, "base", this.ID));
this.workers = this.units.filter(API3.Filters.byMetadata(PlayerID, "role", "worker"));
this.buildings = gameState.getOwnStructures().filter(API3.Filters.byMetadata(PlayerID, "base", this.ID));
this.mobileDropsites = this.units.filter(API3.Filters.isDropsite());
this.units.registerUpdates();
this.workers.registerUpdates();
this.buildings.registerUpdates();
this.mobileDropsites.registerUpdates();
// array of entity IDs, with each being
this.dropsites = {};
@ -983,6 +985,8 @@ m.BaseManager.prototype.update = function(gameState, queues, events)
this.reassignIdleWorkers(gameState);
for (let ent of this.workers.values())
this.workerObject.update(gameState, ent);
for (let ent of this.mobileDropsites.values())
this.workerObject.moveToGatherer(gameState, ent, false);
}
return false;
}
@ -1031,6 +1035,8 @@ m.BaseManager.prototype.update = function(gameState, queues, events)
this.reassignIdleWorkers(gameState);
for (let ent of this.workers.values())
this.workerObject.update(gameState, ent);
for (let ent of this.mobileDropsites.values())
this.workerObject.moveToGatherer(gameState, ent, false);
return true;
}
@ -1070,6 +1076,8 @@ m.BaseManager.prototype.update = function(gameState, queues, events)
// check if workers can find something useful to do
for (let ent of this.workers.values())
this.workerObject.update(gameState, ent);
for (let ent of this.mobileDropsites.values())
this.workerObject.moveToGatherer(gameState, ent, false);
Engine.ProfileStop();
return true;

View File

@ -1926,7 +1926,7 @@ m.HQ.prototype.constructTrainingBuildings = function(gameState, queues)
numBarracks -= numRanges;
let stableTemplate = this.canBuild(gameState, "structures/{civ}_stables") ? "structures/{civ}_stables" :
this.canBuild(gameState, "structures/{civ}_stable") ? "structures/{civ}_stable" : undefined;
this.canBuild(gameState, "structures/{civ}_stable") ? "structures/{civ}_stable" : undefined;
let numStables = gameState.getOwnEntitiesByClass("Stables", true).length;
if (this.getAccountedPopulation(gameState) > this.Config.Military.popForBarracks1 ||

View File

@ -337,7 +337,7 @@ m.Worker.prototype.update = function(gameState, ent)
ent.setMetadata(PlayerID, "target-foundation", undefined);
// If worker elephant, move away to avoid being trapped in between constructions
if (ent.hasClass("Elephant"))
this.moveAway(gameState);
this.moveToGatherer(gameState, ent, true);
else if (this.baseID != gameState.ai.HQ.baseManagers[0].ID)
{
// reassign it to something useful
@ -1003,20 +1003,29 @@ m.Worker.prototype.buildAnyField = function(gameState, baseID)
};
/**
* Workers elephant should move away from the buildings they've built to avoid being trapped in between constructions
* For the time being, we move towards the nearest gatherer (providing him a dropsite)
* Workers elephant should move away from the buildings they've built to avoid being trapped in between constructions.
* For the time being, we move towards the nearest gatherer (providing him a dropsite).
* BaseManager does also use that function to deal with its mobile dropsites.
*/
m.Worker.prototype.moveAway = function(gameState)
m.Worker.prototype.moveToGatherer = function(gameState, ent, forced)
{
let pos = ent.position();
if (!pos || ent.getMetadata(PlayerID, "target-foundation") !== undefined)
return;
if (!forced && gameState.ai.elapsedTime < (ent.getMetadata(PlayerID, "nextMoveToGatherer") || 5))
return;
let gatherers = this.base.workersBySubrole(gameState, "gatherer");
let pos = this.ent.position();
let dist = Math.min();
let destination = pos;
let destination;
let access = m.getLandAccess(gameState, ent);
let types = ent.resourceDropsiteTypes();
for (let gatherer of gatherers.values())
{
if (!gatherer.position() || gatherer.getMetadata(PlayerID, "transport") !== undefined)
let gathererType = gatherer.getMetadata(PlayerID, "gather-type");
if (!gathererType || types.indexOf(gathererType) == -1)
continue;
if (gatherer.isIdle())
if (!gatherer.position() || gatherer.getMetadata(PlayerID, "transport") !== undefined ||
m.getLandAccess(gameState, gatherer) != access || gatherer.isIdle())
continue;
let distance = API3.SquareVectorDistance(pos, gatherer.position());
if (distance > dist)
@ -1024,7 +1033,9 @@ m.Worker.prototype.moveAway = function(gameState)
dist = distance;
destination = gatherer.position();
}
this.ent.move(destination[0], destination[1]);
ent.setMetadata(PlayerID, "nextMoveToGatherer", gameState.ai.elapsedTime + (destination ? 12 : 5));
if (destination && dist > 10)
ent.move(destination[0], destination[1]);
};
/**