OpenTTD AI which builds a road network between all towns it can reach.

route_planner.nut 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*--------------------------------------------------------------------
  2. | |
  3. | RoutePlanner |
  4. | |
  5. --------------------------------------------------------------------*/
  6. class RoutePlanner
  7. {
  8. town_list = null;
  9. town_list2 = null;
  10. town_acc_list = null;
  11. state = 0;
  12. cargo_id = 0;
  13. date_town_acc_update = 0;
  14. constructor(pass_cargo_id) {
  15. this.cargo_id = pass_cargo_id;
  16. this.town_list = null;
  17. this.town_list2 = null;
  18. this.town_acc_list = null;
  19. this.state = 0;
  20. date_town_acc_update = 0;
  21. }
  22. function FindUnusedTowns(agressive);
  23. function InitTownList();
  24. function UpdateTownAcceptanceList();
  25. }
  26. /*------------------------------------------------------------------*/
  27. function RoutePlanner::InitTownList()
  28. {
  29. this.town_list2 = AITownList();
  30. this.town_list = AIList();
  31. this.town_list.AddList(this.town_list2);
  32. town_list.Valuate(AITown.GetPopulation);
  33. this.town_acc_list = AIList();
  34. UpdateTownAcceptanceList();
  35. }
  36. /*------------------------------------------------------------------*/
  37. function RoutePlanner::UpdateTownAcceptanceList()
  38. {
  39. if (AIDate.GetCurrentDate() - date_town_acc_update > 60) {
  40. town_acc_list.Clear();
  41. foreach ( twn, v in town_list){
  42. local acc = TownManager.EstimateAcceptance(twn, cargo_id);
  43. town_acc_list.AddItem(twn, acc);
  44. AIController.Sleep(1);
  45. }
  46. date_town_acc_update = AIDate.GetCurrentDate();
  47. }
  48. }
  49. /*------------------------------------------------------------------*/
  50. function RoutePlanner::FindUnusedTowns(agressive)
  51. {
  52. local town = 0;
  53. local town2 = 0;
  54. local town_it = AITown();
  55. local found = false;
  56. // AILog.Info("Find unused towns agressive: " + agressive);
  57. switch (state) {
  58. case 0:
  59. AILog.Info("Find unused towns, state 0");
  60. UpdateTownAcceptanceList();
  61. if (town_acc_list.Count()){
  62. for (town_it = town_acc_list.Begin(); !town_acc_list.IsEnd();
  63. town_it = town_acc_list.Next()){
  64. AILog.Info("acceptance of " + AITown.GetName(town_it) + " = " + town_acc_list.GetValue(town_it));
  65. local tl2 = AITileList();
  66. /* On agressive setting, build wherever you can to make money,
  67. * On not-agressive setting, keep away from the towns where a player already built a station
  68. */
  69. if (!agressive){
  70. tl2.AddRectangle(AITown.GetLocation(town_it) + AIMap.GetTileIndex(-8, -8),
  71. AITown.GetLocation(town_it) + AIMap.GetTileIndex(8, 8));
  72. tl2.Valuate(AIRoad.IsRoadStationTile);
  73. tl2.KeepValue(1);
  74. }
  75. if (!tl2.Count()){
  76. town = town_it;
  77. found = true;
  78. town_acc_list.RemoveItem(town);
  79. break;
  80. }
  81. }
  82. if (found) {
  83. found = false;
  84. // AILog.Info("Find unused towns, find second town");
  85. town_list2.Clear();
  86. town_list2.AddList(town_list);
  87. town_list2.Valuate(AITown.GetDistanceManhattanToTile, AITown.GetLocation(town));
  88. town_list2.KeepBetweenValue(70,140);
  89. if (!town_list2.Count()){
  90. town_list2.AddList(town_list);
  91. }
  92. local town_list3 = AIList();
  93. foreach ( twn, v in town_list2){
  94. local acc = TownManager.EstimateAcceptance(twn, cargo_id);
  95. town_list3.AddItem(twn, acc);
  96. AIController.Sleep(1);
  97. }
  98. if (town_list3.Count()) {
  99. for (town_it = town_list3.Begin(); !town_list3.IsEnd();
  100. town_it = town_list3.Next()){
  101. if (town_it != town) {
  102. local tl2 = AITileList();
  103. /* On agressive setting, build wherever you can to make money,
  104. * On not-agressive setting, keep away from the towns where a player already built a station
  105. */
  106. if (!agressive){
  107. tl2.AddRectangle( AITown.GetLocation(town_it) + AIMap.GetTileIndex(-8, -8),
  108. AITown.GetLocation(town_it) + AIMap.GetTileIndex(8, 8));
  109. tl2.Valuate(AIRoad.IsRoadStationTile);
  110. tl2.KeepValue(1);
  111. }
  112. if (!tl2.Count()){
  113. town2 = town_it;
  114. found = true;
  115. break;
  116. }
  117. else {
  118. found = false;
  119. AILog.Info("Find unused towns, find second town: not found");
  120. }
  121. }
  122. }
  123. }
  124. }
  125. }
  126. else {
  127. state = 1;
  128. return null;
  129. }
  130. break;
  131. case 1:
  132. break;
  133. }
  134. if (found) {
  135. return [ town, town2 ];
  136. }
  137. else {
  138. return null;
  139. }
  140. }