pastebin light

pastebin is a collaborative debugging tool allowing you to share and modify code snippets while chatting on IRC, IM or a message board.

pastebin light //misacek

Posted by openttd on Thu 8 Dec 2011 11:54:12 CET
download | new post

  1. import("graph.aystar", "AyStar", 6);
  2.  
  3. class Honza extends AIController {
  4. aystar = null;
  5. sources = [];
  6. goals = [];
  7.  
  8. function Start();
  9. constructor() {
  10. this.aystar = AyStar(this,
  11. this._cost, this._estimate,
  12. this._neighbours, this._directions);
  13. }
  14. }
  15.  
  16. function Honza::_cost(/* Honza */ self,
  17. /* AyStar.Path */ path,
  18. /* tileindex */ newTile,
  19. /* int */ direction) {
  20. //AISign.BuildSign(newTile, "E: ");
  21. local ret;
  22. if (path != null){
  23. ret = path.GetLength();
  24. }else{
  25. ret=1;
  26. }
  27.  
  28. return ret;
  29. }
  30.  
  31. function Honza::_neighbours(/* Honza */ self,
  32. /* AyStar.Path */ path,
  33. /* tileindex */ node) {
  34. local offsets = [AIMap.GetMapSizeX(),
  35. -AIMap.GetMapSizeX(),
  36. -1,
  37. 1];
  38. local newTile;
  39. local result = [];
  40.  
  41. foreach (offset in offsets) {
  42. newTile = node + offset;
  43. if(!AITile.IsCoastTile(newTile)&&(AITile.IsBuildable(newTile))){
  44. result.push([newTile, 1]);}
  45. }
  46. return result;
  47. }
  48.  
  49. function Honza::_estimate(self,
  50. /* tileindex */ tile,
  51. /* int */ direction,
  52. /* array[tileindex, direction] */ goalNodes) {
  53. local ret = AIMap.DistanceSquare(tile,goalNodes[0][0]);
  54. AISign.BuildSign(tile, "Estimate:" + ret);
  55. return ret;
  56. }
  57.  
  58. function Honza::_directions(self, tile,
  59. existingDirection, newDirection) {
  60. return false;
  61. }
  62.  
  63.  
  64. function Honza::Start() {
  65. this.sources = [];
  66. this.sources.push([43280, AIRail.RAILTRACK_NE_SW]);
  67. this.goals = [];
  68. this.goals.push([61458, 61458-1]);
  69.  
  70. local coalCargo = null;
  71. foreach (cidx, cargo in AICargoList()){
  72. if(AICargo.GetCargoLabel(cidx) == "COAL"){
  73. coalCargo = cidx;
  74. break;
  75. }
  76. }
  77. if(coalCargo == null){
  78. return;
  79. }
  80.  
  81. local powerPlants = AIIndustryList_CargoAccepting(coalCargo);
  82. local coalMines = AIIndustryList_CargoProducing(coalCargo);
  83.  
  84. local powerPlant = powerPlants.Begin();
  85. local coalMine = coalMines.Begin();
  86.  
  87. local cmTile = AIIndustry.GetLocation(coalMine)-AIMap.GetMapSizeX();
  88. local ppTile = AIIndustry.GetLocation(powerPlant)-AIMap.GetMapSizeX();
  89.  
  90. AISign.BuildSign(cmTile, "Elektrarna");
  91. AISign.BuildSign(ppTile, "Dul");
  92.  
  93. AIRail.SetCurrentRailType(AIRailTypeList().Begin())
  94. AIRail.BuildRailStation(cmTile-4,AIRail.RAILTRACK_NE_SW,1,4,AIStation.STATION_NEW);
  95. AIRail.BuildRailStation(ppTile-4,AIRail.RAILTRACK_NE_SW,1,4,AIStation.STATION_NEW);
  96.  
  97. AILog.Info(AIError.GetLastErrorString());
  98.  
  99. local ignoredTiles = [];
  100. local path = false;
  101. this.aystar.InitializePath(this.sources,
  102. this.goals, ignoredTiles);
  103. while (!path) {
  104. AILog.Info("hledam cestu");
  105. path = this.aystar.FindPath(-1);
  106. }
  107. if (path) {
  108. local sL = AISignList();
  109. foreach (idx, dSign in sL){AISign.RemoveSign(idx);}
  110.  
  111. local current = path.GetParent();
  112. while (current){
  113. AISign.BuildSign(current.GetTile(), "E: ");
  114. current = current.GetParent();}
  115. }
  116.  
  117.  
  118. while (true) {
  119. AILog.Info("I am a very new AI with a ticker called MyNewAI and I am at tick " + this.GetTick());
  120. this.Sleep(50);
  121. }
  122. }
  123.  

Syntax highlighting: