forked from ReikaKalseki/Geothermal
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontrol.lua
366 lines (339 loc) · 11.5 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
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
require "config"
require "constants"
require "__DragonIndustries__.arrays"
require "__DragonIndustries__.world"
local SET_VERSION = 2
local function getPatchSize(tile)
local low = 2
local high = 6
if string.find(tile, "volcanic", 1, true) then
high = 2--32
low = high
end
if string.find(tile, "snow", 1, true) then
low = 5
high = 10
end
if string.find(tile, "desert", 1, true) then
low = 4
high = 6
end
return low, high
end
local function calculateSpawnSet(set)
local lavatiles = game.tile_prototypes["volcanic-orange-heat-1"]
local snowtiles = game.tile_prototypes["frozen-snow-0"]
local set = {}
for name,tile in pairs(game.tile_prototypes) do
if string.find(name, "volcanic", 1, true) then
local heat = tonumber(string.sub(name, -1)) -- 1-4, 4 is hotter & brighter
--game.print(name .. " > " .. heat)
local f2 = 36*0.36*((heat/4)^2)--0.4*((heat/4)^2)--0.25*heat/4
set[name] = {rate = f2, radius = 4}--16}
end
if Config.geothermalSpawnRules == "volcanic-and-snow" or Config.geothermalSpawnRules == "volcanic-snow-and-red-desert" then
if string.find(name, "frozen-snow", 1, true) then
set[name] = {rate = 0.055, radius = 9} --was 0.03, then 0.045
end
end
if Config.geothermalSpawnRules == "volcanic-snow-and-red-desert" then
if string.find(name, "red-desert", 1, true) then
set[name] = {rate = 0.04, radius = 8} --was 0.006, then 0.02
end
end
end
if Config.geothermalSpawnRules == "everywhere" or getTableSize(set) == 0 then
for name,tile in pairs(game.tile_prototypes) do
set[name] = {rate = 0.012, radius = 6} --0.0015 is too rare to appear enough; as was 0.006 and 0.009
end
end
for k,v in pairs(set) do
if type(v) == "number" then
local low,high = getPatchSize(k)
set[k] = {rate = v, count_min = low, count_max = high}
--game.print(k)
elseif type(v) == "table" and not v[count_min] then
local low,high = getPatchSize(k)
v.count_min = low
v.count_max = high
end
end
--game.print(serpent.block(set))
set.version = SET_VERSION
return set
end
local function getSpawnSet()
local geo = global.geo
if not geo then
geo = {}
global.geo = geo
end
if geo.set and (geo.set.version == nil or geo.set.version < SET_VERSION) then
geo.set = nil
end
if not geo.set then
geo.set = calculateSpawnSet(geo)
end
return geo.set
end
local function isNonZeroTile(surface, x, y)
local set = getSpawnSet()
for i = -1,1 do
for k = -1,1 do
local tile = surface.get_tile(x+i, y+k)
--game.print(tile.name .. " > " .. serpent.block(set[tile.name]))
if set[tile.name] and set[tile.name].rate > 0 then
return true
end
end
end
return false
end
function canPlaceAt(surface, x, y)
return surface.can_place_entity{name = "geothermal", position = {x, y}} and not isWaterEdge(surface, x, y) and isNonZeroTile(surface, x, y)
end
local function getTileColor(surface, x, y)
if not Config.geothermalColor then return nil end
local tile = surface.get_tile(x, y)
local clr = nil
if string.find(tile.name, "volcanic", 1, true) then
clr = string.sub(tile.name, string.len("volcanic")+2, -2-string.len("heat")-2)
end
--game.print(tile.name .. " > " .. (clr and clr or "nil"))
return clr
end
local function getPrevailingColor(surface, x, y)
if not Config.geothermalColor then return nil end
local clrs = {}
--game.print(clr)
for dx = x-4,x+4 do
for dy = y-4,y+4 do
local clr = getTileColor(surface, dx, dy)
if clr then
clrs[clr] = clrs[clr] and clrs[clr]+1 or 1
end
end
end
return getHighestTableKey(clrs)
end
function createResource(surface, dx, dy)
if canPlaceAt(surface, dx, dy) then
local color = getPrevailingColor(surface, dx, dy)
local clr = (color and color ~= "red" and color ~= "orange") and ("-" .. color) or ""
local entity = "geothermal" .. clr;
if (game.entity_prototypes[entity] == nil) then
clr = ""
end
surface.create_entity{name = entity, position = {x = dx, y = dy}, force = game.forces.neutral, amount = 1000}
surface.create_entity{name = "geothermal-light" .. clr, position = {x = dx+0.5, y = dy+0.5}, force = game.forces.neutral}
end
end
function cantorCombine(a, b)
--a = (a+1024)%16384
--b = b%16384
local k1 = a*2
local k2 = b*2
if a < 0 then
k1 = a*-2-1
end
if b < 0 then
k2 = b*-2-1
end
return 0.5*(k1 + k2)*(k1 + k2 + 1) + k2
end
function createSeed(surface, x, y) --Used by Minecraft MapGen
local seed = surface.map_gen_settings.seed
if Config.seedMixin ~= 0 then
seed = bit32.band(cantorCombine(seed, Config.seedMixin), 2147483647)
end
return bit32.band(cantorCombine(seed, cantorCombine(x, y)), 2147483647)
end
local function getSpawnData(surface, x, y)
local set = getSpawnSet()
local ret = 0
local sizes = {0, 0}
local radius = 0
c = 0
for i = -1,1 do
for k = -1,1 do
local tile = surface.get_tile(x+i, y+k)
--game.print(tile.name .. " > " .. serpent.block(set[tile.name]))
if set[tile.name] then
ret = ret+set[tile.name].rate
sizes[1] = math.max(sizes[1], set[tile.name].count_min)
sizes[2] = math.max(sizes[2], set[tile.name].count_max)
radius = math.max(radius, set[tile.name].radius)
c = c+1
--if ret > 0 then game.print(tile.name) end
--surface.set_tiles({{name = "water", position = {x+i, y+k}}})
end
end
end
return (c > 0 and ret/c or 0), sizes, radius
end
local function trySpawnPatchAt(rand, surface, x, y, df, counts, radius)
--game.print("Genning Chunk at " .. x .. ", " .. y)
x = x-4+rand(0, 8)
y = y-4+rand(0, 8)
local count = rand(counts[1], counts[2])
count = math.max(1, math.ceil(df*count*Config.size))
--game.print("Chunk at " .. x .. ", " .. y .. " attempting " .. count)
for i = 1, count do
local r = math.floor(radius*Config.size+0.5)
local dx = x-r+rand(0, r*2)
local dy = y-r+rand(0, r*2)
createResource(surface, dx, dy)
end
end
local function controlChunk(surface, area)
local rand = game.create_random_generator()
local x = (area.left_top.x+area.right_bottom.x)/2
local y = (area.left_top.y+area.right_bottom.y)/2
local dd = math.sqrt(x*x+y*y)
if dd < 300 then
return
end
local df = math.min(1, (dd-300)/400)
local seed = createSeed(surface, x, y)
rand.re_seed(seed)
for dx = area.left_top.x,area.right_bottom.x,2 do
for dy = area.left_top.y,area.right_bottom.y,2 do
if df >= 1 or math.random() < df then
local f0,counts,radius = getSpawnData(surface, dx, dy)
--if f0 > 0 then game.print(f0) end
local f = f0*math.min(10, 1+(dd/1000))
f = f*Config.frequency*PATCH_RATE_FACTOR*0.003 -- *0.003 because of 0.17 algo change
--if counts[1] > 0 then
-- game.print("For area " .. dx .. " , " .. dy .. " got " .. f0 .. ">" .. f .. " and " .. serpent.block(counts))
--end
local f1 = rand(0, 2147483647)/2147483647
local shouldGen = f1 < f
--game.print("Chunk at " .. x .. ", " .. y .. " with chance " .. f .. " / " .. f1)
if shouldGen then
--game.print("Genning patch at " .. dx .. " , " .. dy)
trySpawnPatchAt(rand, surface, dx, dy, df, counts, radius)
end
end
end
end
end
script.on_event(defines.events.on_chunk_generated, function(event)
controlChunk(event.surface, event.area)
end)
script.on_event(defines.events.on_tick, function(event)
if not ranTick and Config.retrogenDistance >= 0 then
local surface = game.surfaces["nauvis"]
for chunk in surface.get_chunks() do
local x = chunk.x
local y = chunk.y
if surface.is_chunk_generated({x, y}) then
local area = {
left_top = {
x = x*CHUNK_SIZE,
y = y*CHUNK_SIZE
},
right_bottom = {
x = (x+1)*CHUNK_SIZE,
y = (y+1)*CHUNK_SIZE
}
}
local dx = x*CHUNK_SIZE+CHUNK_SIZE/2
local dy = y*CHUNK_SIZE+CHUNK_SIZE/2
local dist = math.sqrt(dx*dx+dy*dy)
if dist >= Config.retrogenDistance then
controlChunk(surface, area)
end
end
end
ranTick = true
for name,force in pairs(game.forces) do
force.rechart()
end
--game.print("Ran load code")
end
if event.tick%60 == 0 then
for name,force in pairs(game.forces) do
for _,train in pairs(force.get_trains()) do
for __,car in pairs(train.fluid_wagons) do
if car.prototype.name ~= "insulated-wagon" then
--[[
for name,amt in pairs(car.get_fluid_contents()) do
if string.find(name, "geothermal-water", 1, true) then
car.remove_fluid{name=name, amount=100}
end
end
--]]
for i = 1,#car.fluidbox do
local box = car.fluidbox[i]
if box and string.find(box.name, "geothermal-water", 1, true) then
if box.temperature > 95 then
box.name = "cooling-geothermal-water"
else
box.name = "water"
end
box.temperature = box.temperature-5
car.fluidbox[i] = {name = box.name, temperature = box.temperature, amount = box.amount}
end
end
end
end
end
end
end
--local pos=game.players[1].position
--for k,v in pairs(game.surfaces.nauvis.find_entities_filtered{area={{pos.x-1,pos.y-1},{pos.x+1,pos.y+1}}, type="resource"}) do v.destroy() end
end)
--[[
Good AlienBiomes test map: (0.15)
>>>AAAPABUAAAADAwgAAAAEAAAAY29hbAMDAwoAAABjb3BwZXItb3Jl
AwMDCQAAAGNydWRlLW9pbAMDAwoAAABlbmVteS1iYXNlAwMDCAAAAGl
yb24tb3JlAwMDBQAAAHN0b25lAwMDBgAAAHN1bGZ1cgMDAwsAAAB1cm
FuaXVtLW9yZQMDA0FmC0KAhB4AgIQeAAMBAQEBAAAAAAAA0D8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