forked from F1rrel/RenewedVillageGrowth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.nut
520 lines (444 loc) · 16.9 KB
/
main.nut
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
require("version.nut");
require("cargo.nut");
require("industry.nut");
require("town.nut");
require("company.nut");
require("subsidies.nut")
require("story.nut");
require("strings.nut");
// Import SuperLib for GameScript
import("util.superlib", "SuperLib", 40);
Log <- SuperLib.Log;
Helper <- SuperLib.Helper;
// Import ToyLib
import("Library.GSToyLib", "GSToyLib", 1);
import("Library.SCPLib", "SCPLib", 45);
enum Randomization {
NONE = 1,
INDUSTRY_DESC = 2,
INDUSTRY_ASC = 3,
FIXED_1 = 4,
FIXED_2 = 5,
FIXED_3 = 6,
FIXED_5 = 7,
FIXED_7 = 8,
RANGE_1_2 = 9,
RANGE_1_3 = 10,
RANGE_2_3 = 11,
RANGE_3_5 = 12,
RANGE_3_7 = 13,
DESCENDING = 14,
ASCENDING = 15,
};
enum InitError {
NONE,
CARGO_LIST,
INDUSTRY_LIST,
TOWN_NUMBER,
TOWN_GROWTH_RATE,
}
class MainClass extends GSController
{
companies = null;
towns = null;
current_date = null;
current_week = null;
current_month = null;
current_year = null;
gs_init_done = null;
load_saved_data = null;
current_save_version = null;
actual_town_info_mode = null;
toy_lib = null;
story_editor = null;
constructor() {
this.companies = [];
this.towns = [];
this.current_date = 0;
this.current_week = 0;
this.current_month = 0;
this.current_year = 0;
this.gs_init_done = false;
this.current_save_version = SELF_MAJORVERSION; // Ensures compatibility between revisions
this.load_saved_data = false;
this.actual_town_info_mode = 0;
this.toy_lib = null;
this.story_editor = null;
::TownDataTable <- {};
::CompanyDataTable <- {};
::SettingsTable <- {};
}
}
function MainClass::Start()
{
// Wait random number of ticks (less than one day) based on system time to ensure random number seed
local sysdate = GSDate.GetSystemTime() % 70 + 1;
this.Sleep(sysdate);
// Initializing the script
local start_tick = GSController.GetTick();
// Read the openttd.cfg Town Growth Rate setting first.
// If the map is set to disallow town growth at all, this script
// won't do anything further.
if (GSGameSettings.IsValid("town_growth_rate")) {
if (! GSGameSettings.GetValue("town_growth_rate") ) {
GSLog.Error("You must set town growth in advanced setting to something other than None. This script is now exiting!");
this.story_editor = StoryEditor();
this.story_editor.CreateStoryBook([], 0, InitError.TOWN_GROWTH_RATE);
return;
}
}
GSGame.Pause();
Log.Info("Script initialisation...", Log.LVL_INFO);
local init_error = this.Init();
GSGame.Unpause();
local setup_duration = GSController.GetTick() - start_tick;
Log.Info("Game setup done.", Log.LVL_INFO);
Log.Info("Setup took " + setup_duration + " ticks.", Log.LVL_DEBUG);
Log.Info("Happy playing !", Log.LVL_INFO);
// Wait for the game to start
GSController.Sleep(1);
// Create and fill StoryBook. This can't be done before OTTD is ready.
this.story_editor = StoryEditor();
this.story_editor.CreateStoryBook(this.companies, this.towns.len(), init_error);
if (!this.gs_init_done) {
GSLog.Error("Game initialisation failed. This script is now exiting!");
return;
}
// Main loop
local past_system_time = GSDate.GetSystemTime();
while (true) {
local town_info_mode = GSController.GetSetting("town_info_mode");
if (this.actual_town_info_mode != town_info_mode) {
this.actual_town_info_mode = town_info_mode;
foreach (town in this.towns) {
town.UpdateTownText(this.actual_town_info_mode);
}
continue;
}
local system_time = GSDate.GetSystemTime();
if (1 == this.actual_town_info_mode && system_time - past_system_time > 3) {
past_system_time = system_time;
foreach (town in this.towns) {
town.UpdateTownText(this.actual_town_info_mode);
}
}
this.HandleEvents();
this.ManageTowns();
}
}
function MainClass::Init()
{
this.toy_lib = GSToyLib(null); // Init ToyLib;
// Check game settings
GSGameSettings.SetValue("economy.town_growth_rate", 2);
GSGameSettings.SetValue("economy.fund_buildings", 0);
if (!this.load_saved_data) { // Disallow changing these in a running game
::SettingsTable.use_town_sign <- GSController.GetSetting("use_town_sign");
::SettingsTable.randomization <- GSController.GetSetting("cargo_randomization");
::SettingsTable.display_cargo <- GSController.GetSetting("display_cargo");
::SettingsTable.category_min_pop <- [GSController.GetSetting("category_1_min_pop"),
GSController.GetSetting("category_2_min_pop"),
GSController.GetSetting("category_3_min_pop"),
GSController.GetSetting("category_4_min_pop"),
GSController.GetSetting("category_5_min_pop")];
}
// Set current date
this.current_date = this.current_week = GSDate.GetCurrentDate();
this.current_month = GSDate.GetMonth(this.current_date);
this.current_year = GSDate.GetYear(this.current_date);
if (::SettingsTable.randomization == Randomization.INDUSTRY_DESC
|| ::SettingsTable.randomization == Randomization.INDUSTRY_ASC) {
if (!InitIndustryLists())
return InitError.INDUSTRY_LIST;
}
else {
// Initialize cargo lists and variables
if (!InitCargoLists())
return InitError.CARGO_LIST;
}
/* Check whether saved data are in the current save
* format.
*/
if (!this.load_saved_data) {
Helper.ClearAllSigns();
}
// Create company list
Log.Info("Creating company list ...", Log.LVL_INFO);
this.companies = this.CreateCompanyList();
// Create the towns list
Log.Info("Creating town list ... (can take a while on large maps)", Log.LVL_INFO);
this.towns = this.CreateTownList();
if (this.towns.len() > SELF_MAX_TOWNS)
return InitError.TOWN_NUMBER;
// Run industry stabilizer
Log.Info("Prospecting raw industries ... (can take a while on large maps)", Log.LVL_INFO);
ProspectRawIndustry();
// Ending initialization
this.gs_init_done = true;
return InitError.NONE;
}
function MainClass::HandleEvents()
{
while (GSEventController.IsEventWaiting()) {
local event = GSEventController.GetNextEvent();
if (event == null)
return;
switch (event.GetEventType()) {
// On town founding, add a new GoalTown instance
case GSEvent.ET_TOWN_FOUNDED:
event = GSEventTownFounded.Convert(event);
local town_id = event.GetTownID();
if (GSTown.IsValidTown(town_id)) this.UpdateTownList(town_id);
break;
case GSEvent.ET_COMPANY_NEW:
case GSEvent.ET_COMPANY_BANKRUPT:
case GSEvent.ET_COMPANY_MERGER:
Log.Info("A company was created/bankrupt/merged => update company list", Log.LVL_INFO);
this.UpdateCompanyList();
break;
default: break;
}
}
}
function MainClass::Save()
{
Log.Info("Saving data...", Log.LVL_INFO);
local save_table = {};
/* If the script isn't yet initialized, we can't retrieve data
* from GoalTown instances. Thus, simply use the original
* loaded table. Otherwise we build the table with town data.
*/
save_table.company_data_table <- {};
save_table.town_data_table <- {};
if (!this.gs_init_done) {
save_table.town_data_table <- ::TownDataTable;
} else {
// Save permanent settings (allows changing them in scenario editor)
save_table.use_town_sign <- ::SettingsTable.use_town_sign;
save_table.randomization <- ::SettingsTable.randomization;
save_table.display_cargo <- ::SettingsTable.display_cargo;
save_table.category_min_pop <- ::SettingsTable.category_min_pop;
foreach (company in this.companies)
{
save_table.company_data_table[company.id] <- company.SavingCompanyData();
}
local start_opcodes = GSController.GetOpsTillSuspend();
foreach (i, town in this.towns)
{
save_table.town_data_table[town.id] <- town.SavingTownData();
}
Log.Info("Opcodes per saved town = " + ((start_opcodes - GSController.GetOpsTillSuspend()) / this.towns.len()), Log.LVL_DEBUG);
// Also store a savegame version flag
save_table.save_version <- this.current_save_version;
}
return save_table;
}
function MainClass::Load(version, saved_data)
{
Log.Info("Loading data...", Log.LVL_INFO);
// Loading town data. Only load data if the savegame version matches.
if ((saved_data.rawin("save_version") && saved_data.save_version == this.current_save_version)) {
this.load_saved_data = true;
::SettingsTable.use_town_sign <- saved_data.use_town_sign;
::SettingsTable.randomization <- saved_data.randomization;
::SettingsTable.display_cargo <- saved_data.display_cargo;
::SettingsTable.category_min_pop <- saved_data.category_min_pop;
foreach (companyid, company_data in saved_data.company_data_table) {
::CompanyDataTable[companyid] <- company_data;
}
foreach (townid, town_data in saved_data.town_data_table) {
::TownDataTable[townid] <- town_data;
}
}
else {
Log.Info("Save data format doesn't match with current version (saved " + saved_data.save_version + " vs current " + this.current_save_version + "). Resetting.", Log.LVL_INFO);
}
}
function MainClass::UpdateCompanyList()
{
for(local c = GSCompany.COMPANY_FIRST; c <= GSCompany.COMPANY_LAST; c++)
{
local existing = null;
local existing_idx = 0;
foreach(index, company in this.companies)
{
if(company.id == c)
{
existing = company;
existing_idx = index;
break;
}
}
if(GSCompany.ResolveCompanyID(c) == GSCompany.COMPANY_INVALID)
{
if(existing != null) {
existing.RemoveGUIGoals();
this.companies.remove(existing_idx);
}
continue;
}
// If the company can be resolved and exists => do anything
if(existing != null) continue;
// Initialize new company
local company = Company(c, false);
this.companies.append(company);
if (this.story_editor != null)
this.story_editor.CreateNewCompanyStoryBook(company);
}
}
function MainClass::CreateCompanyList()
{
this.companies = [];
// Create company list from saved data
if (this.load_saved_data && ::CompanyDataTable != null) {
foreach (company_id, company_data in ::CompanyDataTable)
{
this.companies.append(Company(company_id, true));
}
}
// Now we can free ::CompanyDataTable
::CompanyDataTable = null;
// Update company list for created/bankrupted/merged
this.UpdateCompanyList();
return companies;
}
/* Make a squirrel array of GoalTown instances (towns_array). For each
* town, an instance of GoalTown is created to store the data related
* to that town.
*/
function MainClass::CreateTownList()
{
// In multiplayer, pause level cannot be changed, so monthly update is forced at start to finish initialization
// In single player, if it is not set, temporarily allow all non-construction actions during pause
local pause_level = GSGameSettings.GetValue("construction.command_pause_level");
if (GSGame.IsMultiplayer() && pause_level < 1)
this.current_month -= 1;
else if (pause_level < 1)
GSGameSettings.SetValue("construction.command_pause_level", 1);
// Create list of cargos/industries near each town
local near_town;
if (::SettingsTable.randomization == Randomization.INDUSTRY_DESC ||
::SettingsTable.randomization == Randomization.INDUSTRY_ASC)
near_town = GetTownsNearbyIndustryPerCategory();
else
near_town = GetTownsNearbyCargoPerCategory();
local towns_list = GSTownList();
local towns_array = [];
local min_transport = GSController.GetSetting("limit_min_transport");
local near_cargo_probability = GSController.GetSetting("near_cargo_probability");
foreach (t, _ in towns_list) {
towns_array.append(GoalTown(t, this.load_saved_data, min_transport, near_town[t], near_cargo_probability));
}
// Reset to previous settings
GSGameSettings.SetValue("construction.command_pause_level", pause_level);
// Now we can free ::TownDataTable
::TownDataTable = null;
return towns_array;
}
/* Function called on town creation. We need to add a now GoalTown
* instance to the this.towns array. The array order doesn't matter,
* since we never use the array index, only its values.
*/
function MainClass::UpdateTownList(town_id)
{
// Create list of cargos/industries near each town
local near_town;
if (::SettingsTable.randomization == Randomization.INDUSTRY_DESC ||
::SettingsTable.randomization == Randomization.INDUSTRY_ASC)
near_town = GetTownsNearbyIndustryPerCategory();
else
near_town = GetTownsNearbyCargoPerCategory();
local min_transport = GSController.GetSetting("limit_min_transport");
local near_cargo_probability = GSController.GetSetting("near_cargo_probability");
this.towns.append(GoalTown(town_id, false, min_transport, near_town[town_id], near_cargo_probability));
Log.Info("New town founded: "+GSTown.GetName(town_id)+" (id: "+town_id+")", Log.LVL_DEBUG);
}
function MainClass::DailyManageTownPopulation()
{
foreach (town in this.towns) {
local new_population = GSTown.GetPopulation(town.id);
if (new_population > town.max_population) {
foreach (company in companies) {
if (company.id == town.contributor) {
company.AddPoints(new_population - town.max_population);
Log.Info(GSTown.GetName(town.id)
+ " increased population by " + (new_population - town.max_population)
+ " which was added to " + GSCompany.GetName(company.id), Log.LVL_DEBUG);
}
}
town.max_population = new_population;
}
}
}
/* Function called periodically (each 74 ticks) to manage
* towns and other stuff.
*/
function MainClass::ManageTowns()
{
// Run the daily functions
local date = GSDate.GetCurrentDate();
local diff_date = date - this.current_date;
if (diff_date == 0) {
return;
} else {
GSToyLib.Check();
this.story_editor.CheckParameters(this.companies);
DailyManageTownPopulation();
this.current_date = date;
}
// Run the monthly functions
local month = GSDate.GetMonth(date);
local diff_month = month - this.current_month;
if (diff_month == 0) {
return;
} else {
local month_tick = GSController.GetTick();
Log.Info("Starting Monthly Updates...", Log.LVL_INFO);
local eternal_love = GSController.GetSetting("eternal_love");
local eternal_love_rating = 0;
switch (eternal_love) {
case(1): // Outstanding
eternal_love_rating = 1000;
break;
case(2): // Good
eternal_love_rating = 400;
break;
case(3): // Poor
eternal_love_rating = 0;
break;
}
local threshold_setting = GSController.GetSetting("town_size_threshold");
local min_transport = GSController.GetSetting("limit_min_transport");
foreach (town in this.towns) {
town.ManageTownLimiting(threshold_setting, min_transport);
town.MonthlyManageTown();
if (this.actual_town_info_mode > 1) {
town.UpdateTownText(this.actual_town_info_mode);
}
if (eternal_love > 0) {
town.EternalLove(eternal_love_rating);
}
}
foreach (company in this.companies) {
company.MonthlyUpdateGUIGoals(this.towns);
}
this.current_month = month;
local month_tick_duration = GSController.GetTick() - month_tick;
Log.Info("Monthly Update took "+month_tick_duration+" ticks.", Log.LVL_DEBUG);
}
// Run the yearly functions - Nothing to do for now, so we leave it out
local year = GSDate.GetYear(date);
local diff_year = year - this.current_year;
if ( diff_year == 0)
return;
else
{
Log.Info("Starting Yearly Updates...", Log.LVL_INFO);
ProspectRawIndustry();
CreateSubsidies(towns, companies);
this.current_year = year;
}
}
function Modulo(num, divisor) {
return (num - divisor * (num / divisor));
}