-
-
Save Milek7/2d25ff9f01f69f1dc73dfda43e50d4cc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require ("config.nut"); | |
class NWD | |
{ | |
p = null; | |
towns = null; | |
wait = null; | |
constructor (v) | |
{ | |
p = v; | |
towns = []; | |
wait = 1; | |
} | |
function Init () | |
{ | |
GSLog.Info ("--------------------------------------"); | |
GSLog.Info ("New World Disorder v0.1.5 - 22.07.2016"); | |
GSLog.Info ("Design by LaChupacabra"); | |
GSLog.Info ("Programming by Milek7"); | |
GSLog.Info ("--------------------------------------"); | |
GSLog.Info ("Initialization..."); | |
local list = GSTownList (); | |
for (local v = list.Begin (); !list.IsEnd (); v = list.Next ()) | |
towns.push (Town (v)); | |
GSLog.Info ("OK!"); | |
GSLog.Info ("--------------------------------------"); | |
} | |
function AssignStation (station) | |
{ | |
foreach (v in towns) | |
if (GSStation.GetDistanceManhattanToTile (station, GSTown.GetLocation (v.t)) < Config.StationDistance || | |
GSStation.IsWithinTownInfluence (station, v.t)) | |
v.stations.push(station); | |
} | |
function AssignIndustry (industry) | |
{ | |
foreach (v in towns) | |
if (GSIndustry.GetDistanceManhattanToTile (industry, GSTown.GetLocation (v.t)) < Config.IndustryDistance || | |
GSTile.IsWithinTownInfluence(GSIndustry.GetLocation (industry), v.t)) | |
v.industries.push(industry); | |
} | |
function OnLoop () | |
{ | |
wait--; | |
if (wait) | |
return; | |
wait = 31; | |
GSLog.Info ("--------------------------------------"); | |
GSLog.Info ("Calculating..."); | |
foreach (v in towns) | |
v.ComputeGrowth (); | |
GSLog.Info ("OK!"); | |
GSLog.Info ("--------------------------------------"); | |
} | |
function OnEvent (e) | |
{ | |
switch (e.GetEventType ()) | |
{ | |
case GSEvent.ET_STATION_FIRST_VEHICLE: | |
AssignStation (GSEventStationFirstVehicle.Convert (e).GetStationID ()); | |
break; | |
case GSEvent.ET_INDUSTRY_OPEN: | |
AssignIndustry (GSEventIndustryOpen.Convert (e).GetIndustryID ()); | |
break; | |
case GSEvent.ET_TOWN_FOUNDED: | |
towns.push (Town (GSEventTownFounded.Convert (e).GetTownID ())); | |
break; | |
} | |
} | |
} | |
class Town | |
{ | |
t = null; | |
industries = null; | |
stations = null; | |
lasthouses = null; | |
lastvalues = null; | |
constructor (v) | |
{ | |
t = v; | |
lasthouses = GSTown.GetHouseCount (t); | |
RebuildStationsList (); | |
RebuildIndustriesList (); | |
GSTown.SetGrowthRate (t, 0); | |
} | |
function RebuildStationsList () | |
{ | |
stations = []; | |
local list = GSStationList (GSStation.STATION_ANY); | |
for (local v = list.Begin (); !list.IsEnd (); v = list.Next ()) | |
{ | |
local distance = GSStation.GetDistanceManhattanToTile (v, GSTown.GetLocation (t)); | |
if (distance < Config.StationDistance || GSStation.IsWithinTownInfluence (v, t)) | |
stations.push (v); | |
} | |
} | |
function RebuildIndustriesList () | |
{ | |
industries = []; | |
local list = GSIndustryList (); | |
for (local v = list.Begin (); !list.IsEnd (); v = list.Next ()) | |
{ | |
local distance = GSIndustry.GetDistanceManhattanToTile (v, GSTown.GetLocation (t)); | |
if (distance < Config.IndustryDistance || GSTile.IsWithinTownInfluence(GSIndustry.GetLocation (v), t)) | |
industries.push (v); | |
} | |
} | |
function GetCategorizedCargo (station, provider, cargo) | |
{ | |
local t = {}; | |
t.intercity <- 0; | |
t.innercity <- 0; | |
local c = provider (station, cargo); | |
for (local x = c.Begin (); !c.IsEnd (); x = c.Next ()) | |
{ | |
if (station == x) | |
continue; | |
local inner = false; | |
foreach (z in stations) | |
if (z == x) | |
{ | |
inner = true; | |
break; | |
} | |
if (inner) | |
t.innercity += c.GetValue (x); | |
else | |
t.intercity += c.GetValue (x); | |
} | |
return t; | |
} | |
function ComputeGrowth () | |
{ | |
if (GSTown.GetHouseCount (t) > lasthouses + 10) | |
{ | |
RebuildStationsList(); | |
lasthouses = GSTown.GetHouseCount (t); | |
} | |
local values = | |
{ | |
industry = 0 | |
trade = 0 | |
build = 0 | |
transport = 0 | |
transport_in = 0 | |
transport_out = 0 | |
}; | |
foreach (k, v in stations) | |
{ | |
if (!GSStation.IsValidStation (v)) | |
{ | |
stations.remove (k); | |
continue; | |
} | |
local c; | |
foreach (h, j in Config.TransportCargo) | |
{ | |
//incoming | |
c = GetCategorizedCargo (v, GSStationList_CargoPlannedByFrom, h); | |
values.transport_in += c.intercity * j; | |
//outcoming | |
c = GetCategorizedCargo (v, GSStationList_CargoPlannedByVia, h); | |
values.transport += c.innercity * j; | |
values.transport_out += c.intercity * j; | |
} | |
} | |
foreach (k, v in industries) | |
{ | |
if (!GSIndustry.IsValidIndustry (v)) | |
{ | |
industries.remove (k); | |
continue; | |
} | |
local type = GSIndustry.GetIndustryType (v); | |
local produced = GSIndustryType.GetProducedCargo (type); | |
local accepted = GSIndustryType.GetAcceptedCargo (type); | |
for (local c = produced.Begin (); !produced.IsEnd (); c = produced.Next ()) | |
{ | |
local m; | |
m = 1; | |
if (c in Config.IndustryMultipier) | |
m = Config.IndustryMultipier[c]; | |
values.industry += GSIndustry.GetLastMonthProduction (v, c) * m; | |
} | |
for (local c = accepted.Begin (); !accepted.IsEnd (); c = accepted.Next ()) | |
{ | |
local m, a; | |
m = 0; | |
if (c in Config.TradeCargoMultipier) | |
m = Config.TradeCargoMultipier[c]; | |
a = MonitorIndustry (c, v) * m; | |
GSLog.Info("trade: " + GSIndustry.GetName(v) + ": " + c.tostring() + ", m:" + m.tostring() + ", v:" + a.tostring()); | |
values.trade += a * m; | |
m = 0; | |
if (c in Config.BuildCargoMultipier) | |
m = Config.BuildCargoMultipier[c]; | |
a = MonitorIndustry (c, v); | |
GSLog.Info("build: " + GSIndustry.GetName(v) + ": " + c.tostring() + ", m:" + m.tostring() + ", v:" + a.tostring()); | |
values.build += a * m; | |
} | |
} | |
local population = GSTown.GetPopulation (t); | |
local total_pax = values.transport + values.transport_in + values.transport_out; | |
local produced_pax = 0; | |
foreach (a, b in Config.TransportCargo) | |
{ | |
produced_pax += GSTown.GetLastMonthProduction (t, a) * b; | |
} | |
values.industry = safediv(values.industry, 200.0 / 1000.0 * population); | |
values.trade = safediv(values.trade, 100.0 / 1000.0 * population); | |
values.build = safediv(values.build, 50.0 / 1000.0 * population); | |
values.transport = min(safediv(values.transport, total_pax) / 0.5, 1.0) * safediv(total_pax, produced_pax); | |
values.transport_inter <- safediv(values.transport_in, produced_pax) * 0.5 | |
+ safediv(values.transport_out, produced_pax) * 0.5; | |
foreach (a, b in values) | |
{ | |
if (b > 1.0) | |
values[a] = 1 + sqrt(b - 1.0) * 3.0; | |
if (lastvalues != null) | |
values[a] = lastvalues[a] + (b - lastvalues[a]) / 4.0; | |
} | |
lastvalues = values; | |
local total = (values.industry + values.trade + values.build + values.transport + values.transport_inter) / 5.0; | |
local unemployment = max((1 - total) * 0.4, 0); | |
local growth = (200 - ((total - 0.25) * 1.33 * 200.0)).tointeger(); | |
if (growth > 200) | |
growth = GSTown.TOWN_GROWTH_NONE; | |
if (growth < 1) | |
growth = 1; | |
GSTown.SetGrowthRate (t, growth); | |
GSTown.SetText (t, GSText (GSText.STR_INFO_WINDOW, | |
GetColor (1 - unemployment / 0.4), (unemployment * 100).tointeger(), | |
GetColor (values.transport), (values.transport * 100).tointeger(), | |
GetColor (values.transport_inter), (values.transport_inter * 100).tointeger(), | |
GetColor (100), (100).tointeger(), | |
GetColor (values.industry), (values.industry * 100).tointeger(), | |
GetColor (values.trade), (values.trade * 100).tointeger(), | |
GetColor (values.build), (values.build * 100).tointeger())); | |
} | |
} | |
function max (a, b) | |
{ | |
if (a < b) | |
return b; | |
return a; | |
} | |
function min (a, b) | |
{ | |
if (a > b) | |
return b; | |
return a; | |
} | |
function safediv (a, b) | |
{ | |
if (b == 0) | |
return 1; | |
return a / b; | |
} | |
function GetColor (v) | |
{ | |
if (v >= 0.75) | |
return GSText (GSText.STR_COLOR_OK); | |
if (v >= 0.25) | |
return GSText (GSText.STR_COLOR_MIDDLE); | |
return GSText (GSText.STR_COLOR_BAD); | |
} | |
function MonitorIndustry (cargo, industry) //terrible hack! | |
{ | |
local r = 0; | |
for (local i = GSCompany.COMPANY_FIRST; i <= GSCompany.COMPANY_LAST; i++) | |
if (GSCompany.ResolveCompanyID (i) != GSCompany.COMPANY_INVALID) | |
r += GSCargoMonitor.GetIndustryDeliveryAmount (i, cargo, industry, true); | |
return r; | |
} | |
function Find (v, c) | |
{ | |
foreach (x in v) | |
if (x == c) | |
return true; | |
return false; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment