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

nws.nut 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. class NWS
  2. {
  3. p = null;
  4. wait = null;
  5. constructor (v)
  6. {
  7. p = v;
  8. wait = 0;
  9. }
  10. function Init ()
  11. {
  12. GSLog.Info ("--------------------------------------");
  13. GSLog.Info ("New World Subsized v0.1 - 20.11.2016");
  14. GSLog.Info ("Based on New World Disorder");
  15. GSLog.Info ("--------------------------------------");
  16. }
  17. function OnLoop ()
  18. {
  19. wait--;
  20. if (wait > 0)
  21. return;
  22. local bdate = GSDate.GetCurrentDate();
  23. GSLog.Info ("--------------------------------------");
  24. GSLog.Info ("Calculating...");
  25. local townList = GSTownList();
  26. townList.Valuate(function(townId)
  27. {
  28. //GSLog.Info("Town "+GSTown.GetName(townId));
  29. local acceptedList = GSCargoList();
  30. acceptedList.Valuate(function(cargoId, townLoc, radius){
  31. //GSLog.Info("-cargo "+GSCargo.GetCargoLabel(cargoId) +" "+GSTile.GetCargoAcceptance(townLoc, cargoId, 1, 1, radius));
  32. return GSTile.GetCargoAcceptance(townLoc, cargoId, 1, 1, radius);
  33. }, GSTown.GetLocation(townId), GSStation.GetCoverageRadius(GSStation.STATION_TRAIN));
  34. acceptedList.KeepAboveValue(0);
  35. //local acceptedList = GSCargoList_StationAccepting(stationId);
  36. local dist = 0;
  37. for (local acceptedId = acceptedList.Begin(); !acceptedList.IsEnd(); acceptedId = acceptedList.Next()) {
  38. if (GSCargo.HasCargoClass(acceptedId, GSCargo.CC_MAIL))
  39. {
  40. acceptedId = acceptedList.Next();
  41. continue;
  42. }
  43. //GSLog.Info("-accepted "+GSCargo.GetCargoLabel(acceptedId));
  44. local industryId = NWS.GetNearestIndustry(GSTown.GetLocation(townId), acceptedId);
  45. dist = dist + GSIndustry.GetDistanceManhattanToTile(industryId, GSTown.GetLocation(townId));
  46. //GSLog.Info("Dist "+dist+" from " + GSTown.GetName(townId) + " to " + GSIndustry.GetName(industryId));
  47. local industryNeedsList = GSList();
  48. industryNeedsList.AddList(GSCargoList_IndustryAccepting(industryId));
  49. for (local industryNeedsCargoId = industryNeedsList.Begin(); !industryNeedsList.IsEnd(); industryNeedsCargoId = industryNeedsList.Next()) {
  50. if (GSCargo.HasCargoClass(industryNeedsCargoId, GSCargo.CC_PASSENGERS))
  51. continue;
  52. local industry2Id = NWS.GetNearestIndustry(GSIndustry.GetLocation(industryId), industryNeedsCargoId);
  53. dist = dist + GSIndustry.GetDistanceManhattanToTile( industryId, GSIndustry.GetLocation(industry2Id));
  54. //GSLog.Info("+Dist "+dist+" from " + GSIndustry.GetName(industryId) + " to " + GSIndustry.GetName(industry2Id));
  55. }
  56. }
  57. return dist;
  58. });
  59. townList.KeepBelowValue(1000);
  60. //townList.Sort(GSList.SORT_BY_VALUE, GSList.SORT_ASCENDING);
  61. for (local stationId = townList.Begin(); !townList.IsEnd(); stationId = townList.Next()) {
  62. local dist = townList.GetValue(stationId);
  63. GSLog.Warning("Would build from " +
  64. GSTown.GetName(stationId) + " dist " + dist
  65. );
  66. /*local acceptedList = GSCargoList_StationAccepting(stationId);
  67. local dist = 0;
  68. for (local acceptedId = acceptedList.Begin(); !acceptedList.IsEnd(); acceptedId = acceptedList.Next())
  69. {
  70. if (GSCargo.HasCargoClass(acceptedId, GSCargo.CC_MAIL))
  71. {
  72. acceptedId = acceptedList.Next();
  73. continue;
  74. }
  75. local industryId = CountryRoads.GetNearestIndustry(GSStation.GetLocation(stationId), acceptedId);
  76. //GSLog.Info(CountryRoads.TimeStamp() + "Dist "+dist+" from " + GSStation.GetName(stationId) + " to " + GSIndustry.GetName(industryId));
  77. local industryNeedsList = GSList();
  78. industryNeedsList.AddList(GSCargoList_IndustryAccepting(industryId));
  79. for (local industryNeedsCargoId = industryNeedsList.Begin(); !industryNeedsList.IsEnd(); industryNeedsCargoId = industryNeedsList.Next()) {
  80. if (GSCargo.HasCargoClass(industryNeedsCargoId, GSCargo.CC_PASSENGERS))
  81. continue;
  82. local industry2Id = CountryRoads.GetNearestIndustry(GSIndustry.GetLocation(industryId), industryNeedsCargoId);
  83. //GSLog.Info(CountryRoads.TimeStamp() + "+Dist "+dist+" from " + GSIndustry.GetName(industryId) + " to " + GSIndustry.GetName(industry2Id));
  84. industryNeedsCargoId = industryNeedsList.Next();
  85. }
  86. acceptedId = acceptedList.Next();
  87. }*/
  88. }
  89. //GSLog.Info ("OK!");
  90. GSLog.Info ("--------------------------------------");
  91. wait = Config.UpdateInterval - (GSDate.GetCurrentDate() - bdate);
  92. if (wait < 0)
  93. GSLog.Warning("WARNING: Can't keep up with UpdateInterval by " + (-wait) + " days!");
  94. }
  95. function OnEvent (e)
  96. {
  97. switch (e.GetEventType ())
  98. {
  99. case GSEvent.ET_TOWN_FOUNDED:
  100. towns.push (Town (this, GSEventTownFounded.Convert (e).GetTownID ()));
  101. break;
  102. }
  103. }
  104. function GetNearestIndustry( tile, cargoType )
  105. {
  106. local industryList = GSList();
  107. industryList.AddList(GSIndustryList_CargoProducing(cargoType));
  108. industryList.Valuate(GSIndustry.GetDistanceManhattanToTile, tile);
  109. return industryList.Begin();
  110. }
  111. }