Advertisement
Guest User

Untitled

a guest
May 2nd, 2011
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.30 KB | None | 0 0
  1. main.nut using 1.1API
  2. ================
  3. import("pathfinder.road", "RoadPathFinder", 3);
  4.  
  5. class Gamma extends AIController {
  6. }
  7.  
  8. function Gamma::Start()
  9. {
  10. /* Globals */
  11. local version = 0.7.1;
  12.  
  13. /* Initialization */
  14. AILog.Info("Gamma " + version + " by Matthew Brades (The idiot known as OTTDmaster to some, and hated by all)");
  15. AILog.Info("Thanks go to Lord Aro, Zuu & probably a few others");
  16. AILog.Info("0.7.1: Reaching for the flyspray");
  17. AILog.Warning("Initiating start procedure.");
  18. /* Get a list of all towns on the map. */
  19. local townlist = AITownList();
  20. AILog.Info("Townlist...Loaded.");
  21.  
  22. /* Sort the list by population, highest population first. */
  23. townlist.Valuate(AITown.GetPopulation);
  24. townlist.Sort(AIList.SORT_BY_VALUE, false);
  25. AILog.Info("Townlist...Sorted.");
  26.  
  27. /* Personal stuff. */
  28. if (!AICompany.SetName("Gamma " + version + "")) {
  29. local i = 2;
  30. while (!AICompany.SetName("Gamma " + version + " #" + i)) {
  31. i = i + 1;
  32. }
  33. }
  34. AILog.Info("Company...Named.");
  35. if (!AICompany.SetPresidentName("ZirconiumX")) {
  36. local i = 2;
  37. while (!AICompany.SetPresidentName("ZirconiumX #" + i)) {
  38. i = i + 1;
  39. }
  40. }
  41. AILog.Info("President..Named.");
  42.  
  43. /* Pick the two towns with the highest population. */
  44. AILog.Info("Townlist...Done.");
  45. AILog.Warning("Start Proc...Done.");
  46. AILog.Info("==========================================");
  47. AILog.Warning("Starting Main Loop...Done.");
  48.  
  49. /* Connect the towns. */
  50. local townid_a = townlist.Begin();
  51. local i = 0;
  52. while (!townlist.IsEnd())
  53. {
  54. i = i + 1;
  55. AILog.Warning("Main Loop...iteration " + i + ".");
  56. local townid_b = townlist.Next();
  57. this.BuildRoute(townid_a, townid_b);
  58. this.BuildRV(townid_a);
  59. this.BuildRV(townid_b);
  60. AILog.Info("Checking for events...")
  61. while (AIEventController.IsEventWaiting()) {
  62. local e = AIEventController.GetNextEvent();
  63. switch (e.GetEventType()) {
  64. case AIEvent.AI_ET_VEHICLE_CRASHED:
  65. local ec = AIEventVehicleCrashed.Convert(e);
  66. local v = ec.GetVehicleID();
  67. AILog.Info("Vehicle (" + v + ") crashed.");
  68. /* Handle the crashed vehicle */
  69. /* TODO: Build extra road vehicle */
  70. break;
  71. case AIEvent.AI_ET_ENGINE_PREVIEW:
  72. local ep = AIEventEnginePreview.Convert(e);
  73. local vn = ep.GetName();
  74. ep.AcceptPreview;
  75. AILog.Info("Preview of " + vn + " accepted.");
  76. break;
  77. case AIEvent.AI_ET_COMPANY_NEW:
  78. local cn = AIEventCompanyNew.Convert(e);
  79. local cid = cn.GetCompanyID();
  80. AILog.Info("Welcome " + AICompany.GetName(cid) + ".");
  81. break;
  82. }
  83. AILog.Info("Main Loop...Done");
  84. }
  85.  
  86. /* Done. */
  87. AILog.Info("Finished.");
  88. AILog.Error("TODO: Standby - and manage events");
  89. }
  90.  
  91. function Gamma::BuildRoute(town_a, town_b)
  92. {
  93. /* Print the names of the towns we'll try to connect. */
  94. AILog.Info(AITown.GetName(town_a) + " to " + AITown.GetName(town_b) + "...Connecting");
  95.  
  96. /* Tell OpenTTD we want to build normal road (no tram tracks). */
  97. AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);
  98. AILog.Info("Roadtype...Set");
  99.  
  100. /* Create an instance of the pathfinder. */
  101. local pathfinder = RoadPathFinder();
  102. AILog.Info("Path...Instanced");
  103.  
  104. /* Set the cost for making a turn extreme high. */
  105. pathfinder.cost.turn = 5000;
  106. AILog.Info("Path...Costed");
  107.  
  108. /* Give the source and goal tiles to the pathfinder. */
  109. pathfinder.InitializePath([AITown.GetLocation(town_a)], [AITown.GetLocation(town_b)]);
  110. AILog.Info("Path...Initialized");
  111.  
  112. /* Try to find a path. */
  113. local path = false;
  114. while (path == false) {
  115. path = pathfinder.FindPath(100);
  116. this.Sleep(1);
  117. }
  118.  
  119. if (path == null) {
  120. /* No path was found. */
  121. AILog.Error("Path...Not Found");
  122. }
  123.  
  124. /* If a path was found, build a road over it. */
  125. AILog.Info("Path...Found");
  126. while (path != null) {
  127. local par = path.GetParent();
  128. if (par != null) {
  129. local last_node = path.GetTile();
  130. if (AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) == 1 ) {
  131. if (!AIRoad.BuildRoad(path.GetTile(), par.GetTile())) {
  132. /* An error occured while building a piece of road. TODO: handle it.
  133. * Note that is can also be the case that the road was already build. */
  134. AILog.Warning("Path...Error during road construction");
  135. }
  136. } else {
  137. /* Build a bridge or tunnel. */
  138. AILog.Warning("Path...Building Tunnel");
  139. if (!AIBridge.IsBridgeTile(path.GetTile()) && !AITunnel.IsTunnelTile(path.GetTile())) {
  140. /* If it was a road tile, demolish it first. Do this to work around expended roadbits. */
  141. if (AIRoad.IsRoadTile(path.GetTile())) AITile.DemolishTile(path.GetTile());
  142. if (AITunnel.GetOtherTunnelEnd(path.GetTile()) == par.GetTile()) {
  143. if (!AITunnel.BuildTunnel(AIVehicle.VT_ROAD, path.GetTile())) {
  144. /* An error occured while building a tunnel. TODO: handle it. */
  145. AILog.Warning("Path...Building Tunnel...Failed");
  146. }
  147. } else {
  148. AILog.Warning("Path...Building Bridge");
  149. local bridge_list = AIBridgeList_Length(AIMap.DistanceManhattan(path.GetTile(), par.GetTile()) + 1);
  150. bridge_list.Valuate(AIBridge.GetMaxSpeed);
  151. bridge_list.Sort(AIList.SORT_BY_VALUE, false);
  152. if (!AIBridge.BuildBridge(AIVehicle.VT_ROAD, bridge_list.Begin(), path.GetTile(), par.GetTile())) {
  153. /* An error occured while building a bridge. TODO: handle it. */
  154.  
  155. switch(AIError.GetLastError()) {
  156. case AIError.ERR_NOT_ENOUGH_CASH:
  157. local l_inter = AICompany.GetLoanInterval;
  158. local l_loan = AICompany.GetLoanAmount;
  159. AICompany.SetLoanAmount([l_loan + l_inter])
  160. break;
  161. default:
  162. AILog.Warning("Path...Building Bridge...Failed");
  163. break;
  164. }
  165.  
  166. }
  167. }
  168. }
  169. }
  170. }
  171. path = par;
  172. }
  173. AILog.Info("Path...Built");
  174. }
  175.  
  176. function Gamma::GetNeighbours4(tile_id)
  177. {
  178. /* Borrowed from TownCars */
  179. local list = AITileList();
  180.  
  181. if(!AIMap.IsValidTile(tile_id))
  182. return list;
  183.  
  184. local tile_x = AIMap.GetTileX(tile_id);
  185. local tile_y = AIMap.GetTileY(tile_id);
  186.  
  187. list.AddTile(GetTileRelative(tile_id, -1, 0));
  188. list.AddTile(GetTileRelative(tile_id, 0, -1));
  189. list.AddTile(GetTileRelative(tile_id, 1, 0));
  190. list.AddTile(GetTileRelative(tile_id, 0, 1));
  191.  
  192. return list;
  193. }
  194.  
  195. function Gamma::Connectable(road_tile, adjacent_tile_to_connect)
  196. {
  197. /* Borrowed from TownCars */
  198. local neighbours = GetNeighbours4(road_tile);
  199.  
  200. neighbours.RemoveTile(adjacent_tile_to_connect);
  201.  
  202. // This function requires that road_tile is connected with at least one other road tile.
  203. neighbours.Valuate(AIRoad.IsRoadTile);
  204. if(Helper.ListValueSum(neighbours) < 1)
  205. return false;
  206.  
  207. for(local neighbour_tile_id = neighbours.Begin(); neighbours.HasNext(); neighbour_tile_id = neighbours.Next())
  208. {
  209. if(AIRoad.IsRoadTile(neighbour_tile_id))
  210. {
  211. local ret = AIRoad.CanBuildConnectedRoadPartsHere(road_tile, neighbour_tile_id, adjacent_tile_to_connect);
  212. if(ret == 0 || ret == -1)
  213. return false;
  214. }
  215. }
  216.  
  217. return true;
  218. }
  219.  
  220. function Gamma::BuildRV(number, town)
  221. {
  222. /* Get locations of town */
  223. local loc = AITown.GetLocation(town);
  224.  
  225. /* We want road - not tramtracks */
  226. AIRoad.SetCurrentRoadType(AIRoad.ROADTYPE_ROAD);
  227.  
  228. /* Build Bus Stop */
  229. local neighbours = GetNeighbours4(tile_id);
  230. neighbours.Valuate(AIRoad.IsRoadTile);
  231. neighbours.KeepValue(1);
  232.  
  233. neighbours.Valuate(CanConnectToRoad, tile_id);
  234. neighbours.KeepValue(1);
  235.  
  236. neighbours.Valuate(Helper.GetMaxHeightOfTile);
  237. neighbours.KeepValue(Helper.GetMaxHeightOfTile(tile_id));
  238.  
  239. /* Return false if none of the neighbours has road */
  240. if(neighbours.Count() == 0 || neighbours.Count() == 1)
  241. {
  242. return false;
  243. }
  244.  
  245. local front = neighbours.Begin();
  246. local depot_front = neighbours.Next();
  247.  
  248. if (!AIRoad.BuildDriveThroughRoadStation(loc, front, ROADVEHTYPE_BUS, AIStation.STATION_JOIN_ADJACENT))
  249. {
  250. if (!AIRoad.BuildRoadStation(loc, front, ROADVEHTYPE_BUS, AIStation.STATION_JOIN_ADJACENT))
  251. {
  252. AILog.Warning("Build Station...Failed");
  253. return false;
  254. }
  255. }
  256. /* Build Depot */
  257. if (!AIRoad.BuildDepot(loc, depot_front))
  258. {
  259. AILog.Warning("Build Depot...Failed");
  260. return false;
  261. }
  262. }
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement