MEGS - This is the economy gamescript running on Midgard, a public OpenTTD server. It's a heavily changed version of https://openttd-polska.pl/Thread-Inne-Projekt-skrypt-New-World-Disorder

ac.nut 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. class AC
  2. {
  3. watchStations = null;
  4. wait = null;
  5. truckCargo = null;
  6. constructor (v)
  7. {
  8. watchStations = {};
  9. wait = 0;
  10. truckCargo = GSCargoList();
  11. truckCargo.Valuate(GSRoad.GetRoadVehicleTypeForCargo);
  12. truckCargo.KeepValue(GSRoad.ROADVEHTYPE_TRUCK);
  13. }
  14. function Init ()
  15. {
  16. GSLog.Info ("--------------------------------------");
  17. GSLog.Info ("Anti Cheat v0.1 - 04.12.2016");
  18. GSLog.Info ("--------------------------------------");
  19. }
  20. function OnLoop ()
  21. {
  22. wait--;
  23. if (wait > 0)
  24. return;
  25. local bdate = GSDate.GetCurrentDate();
  26. GSLog.Info("Checking...");
  27. local stations = GSStationList(GSStation.STATION_TRAIN);
  28. foreach (stationId, v in stations)
  29. {
  30. local station = getStation(stationId);
  31. station.isTrain = true;
  32. local vehicles = getStationVehicles(stationId, GSVehicle.VT_RAIL);
  33. station.hasTrain = vehicles.Count() > 0;
  34. }
  35. local stations = GSList();
  36. stations.AddList(GSStationList(GSStation.STATION_TRUCK_STOP));
  37. stations.AddList(GSStationList(GSStation.STATION_BUS_STOP));
  38. foreach (stationId, v in stations)
  39. {
  40. local station = getStation(stationId);
  41. station.isRoad = true;
  42. local vehicles = getStationVehicles(stationId, GSVehicle.VT_ROAD);
  43. station.hasRoad = vehicles.Count() > 0;
  44. }
  45. local stations = GSStationList(GSStation.STATION_AIRPORT);
  46. foreach (stationId, v in stations)
  47. {
  48. local station = getStation(stationId);
  49. station.isAir = true;
  50. local vehicles = getStationVehicles(stationId, GSVehicle.VT_AIR);
  51. station.hasAir = vehicles.Count() > 0;
  52. }
  53. local stations = GSStationList(GSStation.STATION_DOCK);
  54. foreach (stationId, v in stations)
  55. {
  56. local station = getStation(stationId);
  57. station.isDock = true;
  58. local vehicles = getStationVehicles(stationId, GSVehicle.VT_WATER);
  59. station.hasDock = vehicles.Count() > 0;
  60. }
  61. foreach (_, station in watchStations) {
  62. /*GSLog.Info("Station " + GSStation.GetName(station.id) + " "
  63. + "Train " + station.isTrain + station.hasTrain + " "
  64. + "Road " + station.isRoad + station.hasRoad + " "
  65. + "Air " + station.isAir + station.hasAir + " "
  66. + "Dock " + station.isDock + station.hasDock
  67. );*/
  68. if (!GSStation.IsValidStation(station.id)) {
  69. watchStations.rawdelete(_);
  70. continue;
  71. }
  72. if (station.isAir && station.hasAir || station.isTrain && station.hasTrain || station.isDock && station.hasDock) {
  73. if (station.isRoad && !station.hasRoad) {
  74. GSLog.Info("- " + station.owner + " " + GSStation.GetName(station.id) + " has stations but no busses for "+station.checks+" months");
  75. if (station.checks >= 6) {
  76. removeRoadStations(station);
  77. watchStations.rawdelete(_);
  78. } else
  79. station.checks++;
  80. }
  81. }
  82. /*if (station.isAir && station.hasAir || station.isRoad && station.hasRoad || station.isDock && station.hasDock) {
  83. if (station.isTrain && !station.hasTrain) {
  84. GSLog.Info("- " + station.owner + " " + GSStation.GetName(station.id) + " has stations but no trains for "+station.checks+" months");
  85. if (station.checks >= 6) {
  86. removeTrainStations(station);
  87. watchStations.rawdelete(_);
  88. } else
  89. station.checks++;
  90. }
  91. }*/
  92. }
  93. GSLog.Info(watchStations.len()+" Stations checked");
  94. wait = Config.UpdateInterval - (GSDate.GetCurrentDate() - bdate);
  95. if (wait < 0)
  96. GSLog.Warning("WARNING: Can't keep up with UpdateInterval by " + (-wait) + " days!");
  97. }
  98. function OnEvent (e)
  99. {
  100. switch (e.GetEventType ())
  101. {
  102. case GSEvent.ET_TOWN_FOUNDED:
  103. towns.push (Town (this, GSEventTownFounded.Convert (e).GetTownID ()));
  104. break;
  105. }
  106. }
  107. function getStation(id) {
  108. if (!(id in watchStations)) {
  109. watchStations[id] <- ACStation(id);
  110. }
  111. return watchStations[id];
  112. }
  113. function getStationVehicles(stationId, vehicleType)
  114. {
  115. local vehicles = GSVehicleList_Station(stationId);
  116. vehicles.Valuate(GSVehicle.GetVehicleType);
  117. vehicles.KeepValue(vehicleType);
  118. return vehicles;
  119. }
  120. function removeRoadStations(station)
  121. {
  122. local location = GSStation.GetLocation(station.id);
  123. local spread = (GSGameSettings.GetValue("station.station_spread")).tointeger();
  124. local stationTiles = GSTileList();
  125. local rect = GSTileList();
  126. rect.AddRectangle(location + GSMap.GetTileIndex(spread*-1, spread*-1), location + GSMap.GetTileIndex(spread, spread));
  127. rect.Valuate(GSStation.GetStationID);
  128. rect.KeepValue(station.id);
  129. rect.Valuate(GSRoad.IsDriveThroughRoadStationTile);
  130. rect.KeepValue(1);
  131. stationTiles.AddList(rect);
  132. rect = GSTileList();
  133. rect.AddRectangle(location + GSMap.GetTileIndex(spread*-1, spread*-1), location + GSMap.GetTileIndex(spread, spread));
  134. rect.Valuate(GSStation.GetStationID);
  135. rect.KeepValue(station.id);
  136. rect.Valuate(GSRoad.IsRoadStationTile);
  137. rect.KeepValue(1);
  138. stationTiles.AddList(rect);
  139. foreach(tile,_ in stationTiles) {
  140. removeRoadStationAsCompany(station.owner, tile);
  141. }
  142. }
  143. function removeTrainStations(station)
  144. {
  145. local location = GSStation.GetLocation(station.id);
  146. local spread = (GSGameSettings.GetValue("station.station_spread")).tointeger();
  147. local rect = GSTileList();
  148. rect.AddRectangle(location + GSMap.GetTileIndex(spread*-1, spread*-1), location + GSMap.GetTileIndex(spread, spread));
  149. local stationTiles = GSTileList();
  150. stationTiles.AddList(rect);
  151. stationTiles.Valuate(GSStation.GetStationID);
  152. stationTiles.KeepValue(station.id);
  153. stationTiles.Valuate(GSRail.IsRailStationTile);
  154. stationTiles.KeepValue(1);
  155. foreach(tile,_ in stationTiles) {
  156. removeRailStationAsCompany(station.owner, tile);
  157. }
  158. }
  159. function removeRoadStationAsCompany(companyId, tile)
  160. {
  161. if (GSCompany.ResolveCompanyID(companyId)!=GSCompany.COMPANY_INVALID)
  162. {
  163. local cm = GSCompanyMode(companyId);
  164. GSRoad.RemoveRoadStation(tile);
  165. }
  166. }
  167. function removeRailStationAsCompany(companyId, tile)
  168. {
  169. if (GSCompany.ResolveCompanyID(companyId)!=GSCompany.COMPANY_INVALID)
  170. {
  171. local cm = GSCompanyMode(companyId);
  172. GSRail.RemoveRailStationTileRectangle(tile, tile, false);
  173. }
  174. }
  175. }
  176. class ACStation
  177. {
  178. id = null;
  179. owner = null;
  180. checks = null;
  181. isTrain = false;
  182. hasTrain= false;
  183. isRoad = false;
  184. hasRoad = false;
  185. isAir = false;
  186. hasAir = false;
  187. isDock = false;
  188. hasDock = false;
  189. constructor (stationId) {
  190. id = stationId;
  191. owner = GSStation.GetOwner(stationId);
  192. checks = 0;
  193. }
  194. }