forked from bahrmichael/factorio-tycoon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontrol.lua
More file actions
186 lines (159 loc) · 5.82 KB
/
Copy pathcontrol.lua
File metadata and controls
186 lines (159 loc) · 5.82 KB
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
local CityPlanning = require("city-planner")
local RecipeCalculator = require("recipe-calculator")
local GuiEventHandler = require("gui-event-handler")
local ShortcutHandler = require("shortcut-handler")
local ResearchHandler = require("research-event-handler")
local OnPlayerEventHandler = require("player-event-handler")
local OnChunkChartedHandler = require("chunk-charted-handler")
local OnConstructionHandler = require("construction-event-handler")
local SurfaceEventHandler = require("surface-event-handler")
local Passengers = require("passengers")
local ChatMessages = require("chat-messages")
local Consumption = require("consumption")
local City = require("city")
local Queue = require("queue")
local PrimaryIndustries = require("primary-industries")
local UsedBottlesStore = require("used-bottles-store")
local FloorUpgradesQueue = require("floor-upgrades-queue")
local Constants = require("constants")
--- TICK HANDLERS
local ONE_SECOND = 60;
local FIVE_SECONDS = 5 * ONE_SECOND;
local THIRTY_SECONDS = 30 * ONE_SECOND;
local ONE_MINUTE = 60 * ONE_SECOND;
script.on_nth_tick(ONE_SECOND, function()
CityPlanning.build_initial_city()
PrimaryIndustries.spawn_initial_industry()
City.start_house_construction()
end)
local function handle_passengers()
for _, city in ipairs(global.tycoon_cities or {}) do
Passengers.clearPassengers(city)
Passengers.spawnPassengers(city)
end
end
local function expand_roads()
for _, city in ipairs(global.tycoon_cities or {}) do
if Queue.count(city.buildingLocationQueue) < #city.grid then
local coordinates = City.growAtRandomRoadEnd(city)
if coordinates ~= nil then
City.updatepossibleBuildingLocations(city, coordinates)
end
end
end
end
script.on_nth_tick(FIVE_SECONDS, function()
expand_roads()
City.complete_house_construction()
handle_passengers()
FloorUpgradesQueue.process()
end)
local function add_more_cities()
if #(global.tycoon_cities or {}) > 0 then
local global_citizen_count = 0
for _, city in pairs(global.tycoon_cities) do
for _, n in pairs(city.citizens) do
global_citizen_count = global_citizen_count + n
end
end
local threshold = #(global.tycoon_cities or {}) * Constants.NEW_CITY_THRESHOLD
if global_citizen_count > threshold then
CityPlanning.addMoreCities(false)
end
end
end
script.on_nth_tick(THIRTY_SECONDS, function()
City.construct_priority_buildings()
-- todo: implement me later
-- rediscover_unused_fields()
add_more_cities()
CityPlanning.tag_cities()
end)
local function display_intro_messages()
ChatMessages.show_info_messages()
end
local function consume_resources()
for _, city in ipairs(global.tycoon_cities or {}) do
Consumption.consumeBasicNeeds(city)
Consumption.consumeAdditionalNeeds(city)
Consumption.update_construction_timers_all(city)
end
end
script.on_nth_tick(ONE_MINUTE, function()
for _, city in pairs(global.tycoon_cities or {}) do
UsedBottlesStore.return_used_bottles(city)
end
consume_resources()
City.construct_gardens()
display_intro_messages()
end)
--- EVENT HANDLERS
script.on_event({
defines.events.on_built_entity,
defines.events.on_robot_built_entity
}, function(event)
OnConstructionHandler.on_built(event)
end)
script.on_event({
defines.events.on_player_mined_entity,
defines.events.on_robot_mined_entity,
-- Register entities with script.register_on_entity_destroyed(entity) so that this event fires.
defines.events.on_entity_destroyed,
}, function(event)
OnConstructionHandler.on_removed(event)
end)
script.on_event(defines.events.on_chunk_generated, function (event)
OnChunkChartedHandler.on_chunk_generated(event)
end)
script.on_event(defines.events.on_chunk_charted, function (event)
OnChunkChartedHandler.on_chunk_charted(event)
end)
script.on_event(defines.events.on_chunk_deleted, function (event)
OnChunkChartedHandler.on_chunk_deleted(event)
end)
script.on_event(defines.events.on_player_cursor_stack_changed, function(event)
OnPlayerEventHandler.on_player_cursor_stack_changed(event)
end)
script.on_event(defines.events.on_research_finished, function(event)
ResearchHandler.on_research_finished(event)
end)
script.on_event({defines.events.on_lua_shortcut, "tycoon-cities-overview"}, function(event)
ShortcutHandler.on_shortcut(event)
end)
script.on_init(function()
global.tycoon_global_generator = game.create_random_generator()
global.tycoon_city_buildings = {}
end)
script.on_event(defines.events.on_gui_opened, function (event)
GuiEventHandler.on_gui_opened(event)
end)
script.on_event(defines.events.on_gui_text_changed, function(event)
GuiEventHandler.on_gui_text_changed(event)
end)
script.on_event(defines.events.on_gui_click, function(event)
GuiEventHandler.on_gui_click(event)
end)
script.on_event(defines.events.on_gui_checked_state_changed, function(event)
GuiEventHandler.on_gui_checked_state_changed(event)
end)
-- surface events
script.on_event(defines.events.on_surface_cleared, function (event)
SurfaceEventHandler.on_surface_cleared(event)
end)
script.on_event(defines.events.on_surface_created, function (event)
SurfaceEventHandler.on_surface_created(event)
end)
script.on_event(defines.events.on_surface_deleted, function (event)
SurfaceEventHandler.on_surface_deleted(event)
end)
script.on_event(defines.events.on_surface_imported, function (event)
SurfaceEventHandler.on_surface_imported(event)
end)
script.on_event(defines.events.on_surface_renamed, function (event)
SurfaceEventHandler.on_surface_renamed(event)
end)
--- REMOTE INTERFACES
remote.add_interface("tycoon", {
spawn_city = CityPlanning.addCity,
calculate_recipes = RecipeCalculator.calculateAllRecipes
})