forked from credomane/FactorioMods_FactorioMaps
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generateMap.lua
332 lines (271 loc) · 14.3 KB
/
generateMap.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
math.log2 = function(x) return math.log(x) / math.log(2) end
function prettyjson(o, i)
local tab = '\n'..string.rep('\t', i or 0)
if type(o) == 'table' then
local s = o[1] and '[' or '{'
for k,v in pairs(o) do
s = s..tab..(o[1] and '\t' or '\t"'..k..'": ')..json(v, 1+(i or 0))..','
end
return s:sub(1, -2)..(#s>1 and tab..(o[1] and ']' or '}') or '[]')
end
return type(o) ~= 'number' and '"'..tostring(o)..'"' or tostring(o)
end
function json(o, i)
if type(o) == 'table' then
local s = o[1] and '[' or '{'
for k,v in pairs(o) do
s = s .. (o[1] and '' or '"'..k..'":')..json(v, 1+(i or 0))..','
end
return s:sub(1, -2)..(#s>1 and (o[1] and ']' or '}') or '[]')
end
return type(o) ~= 'number' and '"'..tostring(o)..'"' or tostring(o)
end
--[[
x+ = UP, y+ = RIGHT
corners:
2 1
X
4 3
]]--
function adjustBox(entity, box, initBox, corners)
if entity.bounding_box.right_bottom.x < box[1] then
box[1] = math.ceil(entity.bounding_box.right_bottom.x) - 8/32 --8 pixel remains of the lamp, 8 pixels because dont wanna mess with jpg
elseif entity.bounding_box.left_top.x > box[3] then
box[3] = math.floor(entity.bounding_box.left_top.x) + 8/32
end
if entity.bounding_box.right_bottom.y < box[2] then
box[2] = math.ceil(entity.bounding_box.right_bottom.y) - 8/32
elseif entity.bounding_box.left_top.y > box[4] then
box[4] = math.floor(entity.bounding_box.left_top.y) + 8/32
end
if entity.bounding_box.left_top.x > initBox[3] then
if not (entity.bounding_box.left_top.y < initBox[2]) then corners[1] = 1 end
if not (entity.bounding_box.right_bottom.y > initBox[4]) then corners[2] = 1 end
elseif entity.bounding_box.right_bottom.x < initBox[1] then
if not (entity.bounding_box.left_top.y < initBox[2]) then corners[3] = 1 end
if not (entity.bounding_box.right_bottom.y > initBox[4]) then corners[4] = 1 end
end
end
function fm.generateMap(data)
local player = game.players[data.player_index]
local surface = player.surface
local force = player.force
if fm.autorun.mapInfo.maps == nil then
fm.autorun.mapInfo = {
seed = game.default_map_gen_settings.seed,
maps = {}
}
end
-- delete folder (if it already exists)
local basePath = fm.topfolder
local subPath = basePath .. "/Images/" .. fm.autorun.filePath .. "/" .. surface.name .. "/" .. fm.subfolder .. "/"
game.remove_path(subPath)
-- Number of pixels in an image -- CHANGE THIS AND REF.PY WILL NEED TO BE CHANGED
local gridSizes = {256, 512, 1024} -- cant have 2048 anymore. code now relies on it being smaller than one game chunk (32 tiles * 32 pixels)
local gridSize = gridSizes[2]
local tilesPerChunk = 32 --hardcoded
local pixelsPerTile = 32
if fm.autorun.HD == true then
pixelsPerTile = 64 -- HD textures have 64 pixels/tile
end
-- These are the number of tiles per grid section
local gridPixelSize = gridSize / pixelsPerTile
if fm.tilenames == nil then
local blacklist = {
"water",
"dirt",
"grass",
"lab",
"out-of-map",
"desert",
"sand",
"tutorial",
"ghost"
}
local tilenamedict = {}
for _, item in pairs(game.item_prototypes) do
if item.place_as_tile_result ~= nil and tilenamedict[item.place_as_tile_result.result.name] == nil then
for _, keyword in pairs(blacklist) do
if string.match(item.place_as_tile_result.result.name, keyword) then
tilenamedict[item.place_as_tile_result.result.name] = false
goto continue
end
end
tilenamedict[item.place_as_tile_result.result.name] = true
end
::continue::
end
fm.tilenames = {}
for tilename, value in pairs(tilenamedict) do
if value then
fm.tilenames[#fm.tilenames+1] = tilename
end
end
end
local spawn = force.get_spawn_position(surface)
local minX = spawn.x
local minY = spawn.y
local maxX = spawn.x
local maxY = spawn.y
local allGrid = {}
local mapIndex = 0
if fm.autorun.chunkCache then
for mapTick, v in pairs(fm.autorun.chunkCache) do
if tonumber(mapTick) <= fm.autorun.tick and v[surface.name] ~= nil then
for s in v[surface.name]:gmatch("%-?%d+ %-?%d+") do
local gridX, gridY = s:match("(%-?%d+) (%-?%d+)")
gridX = tonumber(gridX)
gridY = tonumber(gridY)
allGrid[s] = {x = gridX, y = gridY}
minX = math.min(minX, gridX)
minY = math.min(minY, gridY)
maxX = math.max(maxX, gridX)
maxY = math.max(maxY, gridY)
end
if tonumber(mapTick) == fm.autorun.tick then
for i, map in pairs(fm.autorun.mapInfo.maps) do
if map.tick == mapTick then
mapIndex = i
break
end
end
end
end
end
end
local buildChunks = {}
local allGridString = ""
if mapIndex == 0 then
for chunk in surface.get_chunks() do
if force.is_chunk_charted(surface, chunk) then
for gridX = (chunk.x) * tilesPerChunk / gridPixelSize, (chunk.x + 1) * tilesPerChunk / gridPixelSize - 1 do
for gridY = (chunk.y) * tilesPerChunk / gridPixelSize, (chunk.y + 1) * tilesPerChunk / gridPixelSize - 1 do
if allGrid[gridX .. " " .. gridY] == nil then
for k = 0, fm.autorun.around_build_range * pixelsPerTile / tilesPerChunk, 1 do
for l = 0, fm.autorun.around_build_range * pixelsPerTile / tilesPerChunk, 1 do
for m = 1, k > 0 and -1 or 1, -2 do
for n = 1, l > 0 and -1 or 1, -2 do
local i = k * m
local j = l * n
local dist = math.pow(i * tilesPerChunk / pixelsPerTile, 2) + math.pow(j * tilesPerChunk / pixelsPerTile, 2)
if dist <= math.pow(fm.autorun.around_build_range + 0.5, 2) then
local x = gridX + i + (tilesPerChunk / gridPixelSize) / 2 - 1
local y = gridY + j + (tilesPerChunk / gridPixelSize) / 2 - 1
local area = {{gridPixelSize * (x-.5), gridPixelSize * (y-.5)}, {gridPixelSize * (x+.5), gridPixelSize * (y+.5)}}
if buildChunks[x .. " " .. y] == nil then
local powerCount = 0
if fm.autorun.smaller_types and #fm.autorun.smaller_types > 0 then
powerCount = surface.count_entities_filtered({ force=force.name, area=area, type=fm.autorun.smaller_types })
end
local excludeCount = powerCount + surface.count_entities_filtered({ force=force.name, area=area, type={"player"} })
if surface.count_entities_filtered({ force=force.name, area=area, limit=excludeCount + 1 }) > excludeCount or surface.count_tiles_filtered({ area=area, limit=excludeCount + 1, name=fm.tilenames }) > 0 then
buildChunks[x .. " " .. y] = 2
elseif powerCount > 0 then
buildChunks[x .. " " .. y] = 1
else
buildChunks[x .. " " .. y] = 0
end
end
if buildChunks[x .. " " .. y] == 2 or (buildChunks[x .. " " .. y] == 1 and dist <= math.pow(fm.autorun.around_smaller_range + 0.5, 2)) then
allGrid[gridX .. " " .. gridY] = {x = gridX, y = gridY}
allGridString = allGridString .. gridX .. " " .. gridY .. "|"
local x = gridX + (tilesPerChunk / gridPixelSize) / 2 - 1
local y = gridY + (tilesPerChunk / gridPixelSize) / 2 - 1
minX = math.min(minX, gridX)
minY = math.min(minY, gridY)
maxX = math.max(maxX, gridX)
maxY = math.max(maxY, gridY)
goto done
end
end
end
end
end
end
end
::done::
end
end
end
end
-- smoothing
local cont = true
while cont do
cont = false
tmp = {}
for _, p in pairs(allGrid) do
for _, o in pairs({ {x=1, y=0}, {x=-1, y=0}, {x=0, y=1}, {x=0, y=-1} }) do
if allGrid[(p.x+o.x) .. " " .. (p.y+o.y)] == nil then
local count = 0
for _, pos in pairs({ {x=p.x+2*o.x, y=p.y+2*o.y}, {x=p.x+o.x+o.y, y=p.y+o.y+o.x}, {x=p.x+o.x-o.y, y=p.y+o.y-o.x} }) do
if allGrid[pos.x .. " " .. pos.y] ~= nil then
count = count + 1
if count >= 2 then
tmp[#tmp + 1] = {x=p.x+o.x, y=p.y+o.y}
cont = true
end
end
end
end
end
end
cont = #tmp > 0
for _, v in pairs(tmp) do
allGrid[v.x .. " " .. v.y] = v
allGridString = allGridString .. v.x .. " " .. v.y .. "|"
end
end
mapIndex = #fm.autorun.mapInfo.maps + 1
fm.autorun.mapInfo.maps[mapIndex] = {
tick = fm.autorun.tick,
path = fm.autorun.filePath,
date = fm.autorun.date,
mods = game.active_mods,
surfaces = {}
}
end
local maxZoom = 20
if fm.autorun.HD == true then
maxZoom = 21
end
local minZoom = (maxZoom - math.max(0, math.ceil(math.min(math.log2(maxX - minX), math.log2(maxY - minY)) + 0.01 - math.log2(2)))) -- Changed from 4 to 2 for more zoomout
if fm.autorun.mapInfo.maps[mapIndex].surfaces[surface.name] == nil then
fm.autorun.mapInfo.maps[mapIndex].surfaces[surface.name] = {
spawn = spawn,
zoom = { min = minZoom, max = maxZoom },
playerPosition = player.position
}
if fm.autorun.chunkCache[fm.autorun.tick] == nil then
fm.autorun.chunkCache[fm.autorun.tick] = {}
end
fm.autorun.chunkCache[fm.autorun.tick][surface.name] = allGridString:sub(1, -2)
game.write_file(basePath .. "/chunkCache.json", prettyjson(fm.autorun.chunkCache), false, data.player_index)
end
fm.autorun.mapInfo.maps[mapIndex].surfaces[surface.name][fm.subfolder] = true
local extension = "bmp"
game.write_file(basePath .. "/mapInfo.json", json(fm.autorun.mapInfo), false, data.player_index)
local cropText = ""
for _, chunk in pairs(allGrid) do
--game.print(chunk)
local positionTable = {(chunk.x + 0.5) * gridPixelSize, (chunk.y + 0.5) * gridPixelSize}
local box = { positionTable[1], positionTable[2], positionTable[1] + gridPixelSize, positionTable[2] + gridPixelSize } -- -X -Y X Y
local initialBox = { box[1], box[2], box[3], box[4] }
local area = {{box[1] - 16, box[2] - 16}, {box[3] + 16, box[4] + 16}}
local corners = {0, 0, 0, 0}
for _, t in pairs(surface.find_entities_filtered{area=area, name="big-electric-pole"}) do
adjustBox(t, box, initialBox, corners)
end
for _, t in pairs(surface.find_entities_filtered{area=area, type="lamp"}) do
local control = t.get_control_behavior()
if t.energy > 1 and (control and not control.disabled) or (not control and surface.darkness > 0.3) then
adjustBox(t, box, initialBox, corners)
end
end
if box[1] < positionTable[1] or box[2] < positionTable[2] or box[3] > positionTable[1] + gridPixelSize or box[4] > positionTable[2] + gridPixelSize then
cropText = cropText .. "\n" .. chunk.x .. " " .. chunk.y .. " " .. (positionTable[1] - box[1])*pixelsPerTile .. " " .. (positionTable[2] - box[2])*pixelsPerTile .. " " .. string.format("%x", corners[1] + 2*corners[2] + 4*corners[3] + 8*corners[4])
end
local pathText = subPath .. maxZoom .. "/" .. chunk.x .. "/" .. chunk.y .. "." .. extension
game.take_screenshot({by_player=player, position = {(box[1] + box[3]) / 2, (box[2] + box[4]) / 2}, resolution = {(box[3] - box[1])*pixelsPerTile, (box[4] - box[2])*pixelsPerTile}, zoom = fm.autorun.HD and 2 or 1, path = pathText, show_entity_info = true})
end
game.write_file(subPath .. "crop.txt", gridSize .. cropText, false, data.player_index)
end