Advertisement
Guest User

Untitled

a guest
Apr 24th, 2011
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.05 KB | None | 0 0
  1. info.nut
  2. ==================================================================================================
  3. class SampleAI extends AIInfo
  4. {
  5. function GetAuthor() { return "Newbie AI Writer"; }
  6. function GetName() { return "SampleAI"; }
  7. function GetDescription() { return "An example AI by following the wiki tutorial"; }
  8. function GetVersion() { return 1; }
  9. function MinVersionToLoad() { return 1; }
  10. function GetDate() { return "2007-03-17"; }
  11. function GetShortName() { return "SAAI"; }
  12. function CreateInstance() { return "SampleAI"; }
  13. function GetAPIVersion() { return "1.0"; }
  14. }
  15.  
  16. /* Tell the core we are an AI */
  17. RegisterAI(SampleAI());
  18.  
  19. ==================================================================================================
  20. main.nut
  21. ==================================================================================================
  22. import("pathfinder.road", "RoadPathFinder", 3);
  23.  
  24. class SampleAI extends AIController {
  25. }
  26.  
  27. function SampleAI::Start()
  28. {
  29. /* Get a list of all towns on the map. */
  30. local townlist = AITownList();
  31.  
  32. /* Sort the list by population, highest population first. */
  33. townlist.Valuate(AITown.GetPopulation);
  34. townlist.Sort(AIAbstractList.SORT_BY_VALUE, false);
  35.  
  36. /* Pick the two towns with the highest population. */
  37. local townid_a = townlist.Begin();
  38. local townid_b = townlist.Next();
  39.  
  40. /* Print the names of the towns we'll try to connect. */
  41. AILog.Info("Going to connect " + AITown.GetName(townid_a) + " to " + AITown.GetName(townid_b));
  42.  
  43. /* Tell OpenTTD we want to build normal road (no tram tracks). */
  44. AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);
  45.  
  46. /* Create an instance of the pathfinder. */
  47. local pathfinder = RoadPathFinder();
  48.  
  49. /* Give the source and goal tiles to the pathfinder. */
  50. pathfinder.InitializePath([AITown.GetLocation(townid_a)], [AITown.GetLocation(townid_b)]);
  51.  
  52. /* Try to find a path. */
  53. local path = false;
  54. local i = 0;
  55. while (path == false) {
  56. path = pathfinder.FindPath(100);
  57. i++;
  58. if (i >= 100) {
  59. path = null;
  60. break;
  61. }
  62. SampleAI.Sleep(1);
  63. AILog.Info("Zzzz");
  64. }
  65.  
  66. if (path == null) {
  67. /* No path was found. */
  68. AILog.Warning("Ooops: FindPath returned null");
  69. AILog.Warning("TODO: Find another town");
  70. SampleAI.Stop()
  71. }
  72.  
  73. /* If a path was found, build a road over it. */
  74. while (path != null) {
  75. local par = path.GetParent();
  76. if (par != null) {
  77. local last_node = path.GetTile();
  78. if (AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) == 1 ) {
  79. if (!AIRoad.BuildRoad(path.GetTile(), par.GetTile())) {
  80. /* An error occured while building a piece of road. TODO: handle it.
  81. * Note that is can also be the case that the road was already build. */
  82. AILog.Warning("Ooops: BuildRoad returned null");
  83. }
  84. } else {
  85. /* Build a bridge or tunnel. */
  86. if (!AIBridge.IsBridgeTile(path.GetTile()) && !AITunnel.IsTunnelTile(path.GetTile())) {
  87. /* If it was a road tile, demolish it first. Do this to work around expended roadbits. */
  88. if (AIRoad.IsRoadTile(path.GetTile())) AITile.DemolishTile(path.GetTile());
  89. if (AITunnel.GetOtherTunnelEnd(path.GetTile()) == par.GetTile()) {
  90. if (!AITunnel.BuildTunnel(AIVehicle.VT_ROAD, path.GetTile())) {
  91. /* An error occured while building a tunnel. TODO: handle it. */
  92. }
  93. } else {
  94. local bridge_list = AIBridgeList_Length(AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) + 1);
  95. bridge_list.Valuate(AIBridge.GetMaxSpeed);
  96. bridge_list.Sort(AIAbstractList.SORT_BY_VALUE, false);
  97. if (!AIBridge.BuildBridge(AIVehicle.VT_ROAD, bridge_list.Begin(), path.GetTile(), par.GetTile())) {
  98. /* An error occured while building a bridge. TODO: handle it. */
  99. }
  100. }
  101. }
  102. }
  103. }
  104. path = par;
  105. }
  106. AILog.Info("Done");
  107. }
  108.  
  109. function SampleAI::Stop()
  110. {
  111. AILog.Error("Panic!"); // :p
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement