1
0
forked from 0ad/0ad

Added checks to see if a resource is in another players territory and to mark a resource as inaccessible if a unit has spent too long trying to reach it.

This was SVN commit r12074.
This commit is contained in:
Jonathan Waller 2012-07-05 21:14:48 +00:00
parent 574f4db8ab
commit 463e62a514
6 changed files with 45 additions and 18 deletions

View File

@ -116,7 +116,6 @@ EconomyManager.prototype.reassignIdleWorkers = function(gameState) {
if (idleWorkers.length) {
var resourceSupplies;
//var territoryMap = Map.createTerritoryMap(gameState);
idleWorkers.forEach(function(ent) {
// Check that the worker isn't garrisoned

View File

@ -60,7 +60,7 @@ GameState.prototype.getMap = function() {
};
GameState.prototype.getTerritoryMap = function() {
return this.ai.territoryMap;
return Map.createTerritoryMap(this);
};
GameState.prototype.getPopulation = function() {

View File

@ -1,5 +1,6 @@
// TODO: Make this cope with negative cell values
const TERRITORY_PLAYER_MASK = 0x3F;
//TODO: Make this cope with negative cell values
function Map(gameState, originalMap){
// get the map to find out the correct dimensions
var gameMap = gameState.getMap();
@ -25,9 +26,7 @@ Map.prototype.point = function(p){
Map.createObstructionMap = function(gameState, template){
var passabilityMap = gameState.getMap();
var territoryMap = gameState.getTerritoryMap();
const TERRITORY_PLAYER_MASK = 0x3F;
var territoryMap = gameState.ai.territoryMap;
// default values
var placementType = "land";
@ -95,12 +94,13 @@ Map.createObstructionMap = function(gameState, template){
Map.createTerritoryMap = function(gameState) {
var map = gameState.ai.territoryMap;
var obstructionTiles = new Uint16Array(map.data.length);
for ( var i = 0; i < map.data.length; ++i){
obstructionTiles[i] = map.data[i] & 0x7F;
var ret = new Map(gameState, map.data);
ret.getOwner = function(p) {
return this.point(p) & TERRITORY_PLAYER_MASK;
}
return new Map(gameState, obstructionTiles);
return ret;
};
Map.prototype.addInfluence = function(cx, cy, maxDist, strength, type) {

View File

@ -98,8 +98,6 @@ TerrainAnalysis.prototype.countConnected = function(startPoint, maxCount, curCou
function PathFinder(gameState){
this.TerrainAnalysis(gameState);
this.territoryMap = Map.createTerritoryMap(gameState);
}
copyPrototype(PathFinder, TerrainAnalysis);

View File

@ -65,7 +65,7 @@ var Timer = function() {
}
// Check if the alarm has rung 'alarm.repeat' times if so, delete the alarm.
if (alarm.counter >= alarm.repeat) {
if (alarm.counter >= alarm.repeat && alarm.repeat != -1) {
this.clearTimer(id);
}

View File

@ -4,11 +4,17 @@
var Worker = function(ent) {
this.ent = ent;
this.approachCount = 0;
};
Worker.prototype.update = function(gameState) {
var subrole = this.ent.getMetadata("subrole");
if (!this.ent.position()){
// If the worker has no position then no work can be done
return;
}
if (subrole === "gatherer"){
if (!(this.ent.unitAIState().split(".")[1] === "GATHER" && this.ent.unitAIOrderData().type
&& this.getResourceType(this.ent.unitAIOrderData().type) === this.ent.getMetadata("gather-type"))
@ -26,7 +32,28 @@ Worker.prototype.update = function(gameState) {
Engine.ProfileStop();
}
this.startApproachingResourceTime = gameState.getTimeElapsed();
//Engine.PostCommand({"type": "set-shading-color", "entities": [this.ent.id()], "rgb": [10,0,0]});
}else{
// If we haven't reached the resource in 2 minutes twice in a row and none of the resource has been
// gathered then mark it as inaccessible.
if (gameState.getTimeElapsed() - this.startApproachingResourceTime > 120000){
if (this.gatheringFrom){
var ent = gameState.getEntityById(this.gatheringFrom);
if (ent && ent.resourceSupplyAmount() == ent.resourceSupplyMax()){
if (this.approachCount > 0){
ent.setMetadata("inaccessible", true);
this.ent.setMetadata("subrole", "idle");
}
this.approachCount++;
}else{
this.approachCount = 0;
}
this.startApproachingResourceTime = gameState.getTimeElapsed();
}
}
}
}else if(subrole === "builder"){
if (this.ent.unitAIState().split(".")[1] !== "REPAIR"){
@ -161,7 +188,10 @@ Worker.prototype.startGathering = function(gameState){
});
if (nearestSupply) {
if (!gameState.ai.accessibility.isAccessible(nearestSupply.position())){
var pos = nearestSupply.position();
var territoryOwner = gameState.getTerritoryMap().getOwner(pos);
if (!gameState.ai.accessibility.isAccessible(pos) ||
(territoryOwner != gameState.getPlayerID() && territoryOwner != 0)){
nearestSupply.setMetadata("inaccessible", true);
}else{
ent.gather(nearestSupply);