Advertisement
Guest User

Untitled

a guest
Dec 29th, 2011
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. import("pathfinder.road", "RoadPathFinder", 3);
  2.  
  3. class YAMAI extends AIController {
  4. function Start();
  5. }
  6.  
  7. function YAMAI::Start()
  8. {
  9. AILog.Info("Yet Another Mindless AI. Yes - another one.");
  10.  
  11. /* Get a list of all towns on the map. */
  12. local townlist1 = AITownList();
  13. local townlist2 = AITownList();
  14. local townid_a = townlist1.Begin();
  15. local townid_b = townlist2.Begin();
  16. for (local loop_a = 0; loop_a < AITown.GetTownCount; loop_a++) { // error
  17. for (local loop_b = 0; loop_b < AITown.GetTownCount(); loop_b++) {
  18.  
  19. /* Print the names of the towns we'll try to connect. */
  20. AILog.Info("Going to connect " + AITown.GetName(townid_a) + " to " + AITown.GetName(townid_b));
  21.  
  22. /* Tell OpenTTD we want to build normal road (no tram tracks). */
  23. AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);
  24.  
  25. /* Create an instance of the pathfinder. */
  26. local pathfinder = RoadPathFinder();
  27.  
  28. /* Set the cost for making a turn extreme high. */
  29. pathfinder.cost.turn = 5000;
  30.  
  31. /* Give the source and goal tiles to the pathfinder. */
  32. pathfinder.InitializePath([AITown.GetLocation(townid_a)], [AITown.GetLocation(townid_b)]);
  33.  
  34. /* Try to find a path. */
  35. local path = false;
  36. while (path == false) {
  37. path = pathfinder.FindPath(100);
  38. this.Sleep(1);
  39. }
  40.  
  41. if (path == null) {
  42. /* No path was found. */
  43. AILog.Error("pathfinder.FindPath return null");
  44. }
  45.  
  46. /* If a path was found, build a road over it. */
  47. while (path != null) {
  48. local par = path.GetParent();
  49. if (par != null) {
  50. local last_node = path.GetTile();
  51. if (AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) == 1 ) {
  52. if (!AIRoad.BuildRoad(path.GetTile(), par.GetTile())) {
  53. /* An error occured while building a piece of road. TODO: handle it.
  54. * Note that is can also be the case that the road was already build. */
  55. }
  56. } else {
  57. /* Build a bridge or tunnel. */
  58. if (!AIBridge.IsBridgeTile(path.GetTile()) && !AITunnel.IsTunnelTile(path.GetTile())) {
  59. /* If it was a road tile, demolish it first. Do this to work around expended roadbits. */
  60. if (AIRoad.IsRoadTile(path.GetTile())) AITile.DemolishTile(path.GetTile());
  61. if (AITunnel.GetOtherTunnelEnd(path.GetTile()) == par.GetTile()) {
  62. if (!AITunnel.BuildTunnel(AIVehicle.VT_ROAD, path.GetTile())) {
  63. /* An error occured while building a tunnel. TODO: handle it. */
  64. }
  65. } else {
  66. local bridge_list = AIBridgeList_Length(AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) + 1);
  67. bridge_list.Valuate(AIBridge.GetMaxSpeed);
  68. bridge_list.Sort(AIList.SORT_BY_VALUE, false);
  69. if (!AIBridge.BuildBridge(AIVehicle.VT_ROAD, bridge_list.Begin(), path.GetTile(), par.GetTile())) {
  70. /* An error occured while building a bridge. TODO: handle it. */
  71. }
  72. }
  73. }
  74. }
  75. }
  76. path = par;
  77. }
  78. AILog.Info("Done");
  79. townid_b = townlist2.Next();
  80. }
  81. townid_a = townlist1.Next();
  82. }
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement