-
Notifications
You must be signed in to change notification settings - Fork 0
/
control.lua
183 lines (150 loc) · 5.08 KB
/
control.lua
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
require("config")
jobs = {}
propagations = {}
-- Make sure that we have a tick available for the tick hook
for k,a_type in pairs(types) do
a_type.tick = math.random(a_type.tick_min, a_type.tick_max)
end
function tick()
for k,a_type in pairs(types) do
a_type.tick = a_type.tick - 1
if a_type.tick <= 0 or a_type.tick == nil then
try_propagate(a_type)
-- Reset tick counter
a_type.tick = math.random(a_type.tick_min, a_type.tick_max)
end
end
handle_jobs()
propagate()
end
function try_propagate(a_type)
local surface = game.surfaces[a_type.surface]
-- Validate the surface exists
if surface ~= nil then
local selected_chunks = {}
for chunk in surface.get_chunks() do
if math.random() <= a_type.selection_chance then
table.insert(selected_chunks, chunk)
end
end
-- If chunks have been selected to propogate the type,
-- create the job
if #selected_chunks > 0 then
local job = {}
job.name = a_type.name
job.chunks = selected_chunks
local propagation = {}
propagation.surface = a_type.surface
propagation.tick_wait = 0
propagation.per_tick = 0
propagation.props = {}
job.propagation = propagation
table.insert(jobs, job)
end
end
end
function propagate()
-- Iterate through all current propagations
for i, propagation in ipairs(propagations) do
propagation.tick_current = propagation.tick_current - 1
-- If we are at the point of propagation
if propagation.tick_current < 1 then
-- Grab the surface
local surface = game.surfaces[propagation.surface]
-- Iterate through the
for i=1,propagation.per_tick do
local prop = table.remove(propagation.props, 1)
-- Propogate if all of the conditions are met
if prop ~= nil and math.random() <= prop.chance and surface.can_place_entity{name=prop.name, position=prop.position} then
surface.create_entity{name=prop.name, position=prop.position}
end
end
propagation.tick_current = propagation.tick_wait
-- Remove it if it has completed
if #propagation.props == 0 then
table.remove(propagations, i)
end
end
end
end
function handle_jobs()
local job = table.remove(jobs,1)
if job ~= nil then
local propagation = job.propagation
-- Grab the surface
local surface = game.surfaces[propagation.surface]
local a_type = types[job.name]
-- Process 10 chunks
for i=1,10 do
local chunk = table.remove(job.chunks, 1)
-- If we have a chunk to process
if chunk ~= nil then
chunk.x = chunk.x * 32
chunk.y = chunk.y * 32
local entities = surface.find_entities_filtered{area={{chunk.x, chunk.y},{chunk.x + 31, chunk.y + 31}}, type=a_type.name}
local entity_count = table.maxn(entities)
-- Validate that entities were found
if entity_count ~= 0 then
local entity = entities[math.random(entity_count)]
local prototype = game.entity_prototypes[entity.name]
local max_place_distance = a_type.max_place_distance
-- User per-entity variation overrides if available
local override = a_type.overrides[entity.name]
local disabled = false
if override ~= nil then
if override.max_place_distance ~= nil then max_place_distance = override.max_place_distance end
if override.disabled ~= nil then disabled = override.disabled end
end
-- Calculate the placement position
local position = {
math.random(max_place_distance * 2) - max_place_distance + entity.position.x,
math.random(max_place_distance * 2) - max_place_distance + entity.position.y
}
-- Grab the per-tile propogation chance
local tile = surface.get_tile(position[1], position[2])
local propagation_chance = 0
-- Make sure that the tile is valid
if tile ~= nil and tile.valid == true then
propogation_chance = a_type.propagation_chance[tile.name]
end
-- Make sure we can handle non-vanilla flooring
if propagation_chance == nil then
propagation_chance = 0.05
end
-- Only propogate when the tile propogation chance has been specified
if disabled == false and propagation_chance > 0 then
local prop = {}
prop.name = entity.name
prop.chance = propagation_chance
prop.position = position
-- Add a propogation point
table.insert(propagation.props, prop)
end
end
else
break
end
end
-- If all chunks have been processed
if #job.chunks == 0 then
-- If we have propogation chances
if #propagation.props > 0 then
propagation.per_tick = math.floor(#propagation.props / (a_type.tick_min * 0.5) +.5)
if propagation.per_tick < 1 then propagation.per_tick = 1 end
propagation.tick_wait = math.floor((a_type.tick_min * 0.5) / #propagation.props) - 1
if propagation.tick_wait < 1 then propagation.tick_wait = 1 end
propagation.tick_current = propagation.tick_wait
-- Add the propogation
table.insert(propagations, propagation)
end
else
-- Reinsert the job
job.propagation = propagation
table.insert(jobs, job)
end
end
end
script.on_event(defines.events.on_tick, tick)
script.on_event(defines.events.on_player_joined_game, function(event)
math.randomseed(game.tick)
end)