adityaravishankar/command-and-conquer — Issue #42: line 3717 & 3736: findRefineryInRange(hero) and findTiberiumInRange(hero)
Author: jovoud
Created: 2012-01-27 17:26:17+00:00
State: open

[code]
function findRefineryInRange(hero){
if(!hero){
hero = this;
}
var currentDistance;
var currentRefinery;
for (var i=0; i < game.buildings.length; i++) {
var building = game.buildings[i];
if (building.name == 'refinery' && building.team == hero.team){
var distance = Math.pow(building.x-hero.x,2)+Math.pow(building.y-hero.y,2);
if (!currentDistance || (currentDistance > distance)){
currentRefinery = building;
currentDistance = distance;
}
}
};
return currentRefinery;
}

[/code]

In the current first level this isn't really a problem, but when you play in big maps this can cause your harvester to go to the wrong refinery/tiberium. Why not use the A* pattern here and remember the path?

Let's say this map

00harvester0000000000tiberium10000000
0000000000000000000000000000000000
1111111111111111111111111111110000
000tiberium2000000000000000000000000

in this case the harvester will go to tiberium2 because the function says it's closer, but in fact there is a wall (represented by 1's here) here so the harvester will go all arround (using the A*) the wall to tiberium2. Why not use the A* to search for this as well and then remember the path somehow that it doesn't need to be calculated every gamecycli?