import("graph.aystar", "AyStar", 6);
class Honza extends AIController {
aystar = null;
sources = [];
goals = [];
function Start();
constructor() {
this.aystar = AyStar(this,
this._cost, this._estimate,
this._neighbours, this._directions);
}
}
function Honza::_cost(/* Honza */ self,
/* AyStar.Path */ path,
/* tileindex */ newTile,
/* int */ direction) {
//AISign.BuildSign(newTile, "E: ");
local ret;
if (path != null){
ret = path.GetLength();
}else{
ret=1;
}
return ret;
}
function Honza::_neighbours(/* Honza */ self,
/* AyStar.Path */ path,
/* tileindex */ node) {
local offsets = [AIMap.GetMapSizeX(),
-AIMap.GetMapSizeX(),
-1,
1];
local newTile;
local result = [];
foreach (offset in offsets) {
newTile = node + offset;
if(!AITile.IsCoastTile(newTile)&&(AITile.IsBuildable(newTile))){
result.push([newTile, 1]);}
}
return result;
}
function Honza::_estimate(self,
/* tileindex */ tile,
/* int */ direction,
/* array[tileindex, direction] */ goalNodes) {
local ret = AIMap.DistanceSquare(tile,goalNodes[0][0]);
AISign.BuildSign(tile, "Estimate:" + ret);
return ret;
}
function Honza::_directions(self, tile,
existingDirection, newDirection) {
return false;
}
function Honza::Start() {
this.sources = [];
this.sources.push([43280, AIRail.RAILTRACK_NE_SW]);
this.goals = [];
this.goals.push([61458, 61458-1]);
local coalCargo = null;
foreach (cidx, cargo in AICargoList()){
if(AICargo.GetCargoLabel(cidx) == "COAL"){
coalCargo = cidx;
break;
}
}
if(coalCargo == null){
return;
}
local powerPlants = AIIndustryList_CargoAccepting(coalCargo);
local coalMines = AIIndustryList_CargoProducing(coalCargo);
local powerPlant = powerPlants.Begin();
local coalMine = coalMines.Begin();
local cmTile = AIIndustry.GetLocation(coalMine)-AIMap.GetMapSizeX();
local ppTile = AIIndustry.GetLocation(powerPlant)-AIMap.GetMapSizeX();
AISign.BuildSign(cmTile, "Elektrarna");
AISign.BuildSign(ppTile, "Dul");
AIRail.SetCurrentRailType(AIRailTypeList().Begin())
AIRail.BuildRailStation(cmTile-4,AIRail.RAILTRACK_NE_SW,1,4,AIStation.STATION_NEW);
AIRail.BuildRailStation(ppTile-4,AIRail.RAILTRACK_NE_SW,1,4,AIStation.STATION_NEW);
AILog.Info(AIError.GetLastErrorString());
local ignoredTiles = [];
local path = false;
this.aystar.InitializePath(this.sources,
this.goals, ignoredTiles);
while (!path) {
AILog.Info("hledam cestu");
path = this.aystar.FindPath(-1);
}
if (path) {
local sL = AISignList();
foreach (idx, dSign in sL){AISign.RemoveSign(idx);}
local current = path.GetParent();
while (current){
AISign.BuildSign(current.GetTile(), "E: ");
current = current.GetParent();}
}
while (true) {
AILog.Info("I am a very new AI with a ticker called MyNewAI and I am at tick " + this.GetTick());
this.Sleep(50);
}
}