1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-05 22:53:24 +02:00
Rampant/libs/ChunkProcessor.lua

149 lines
3.7 KiB
Lua
Raw Normal View History

2019-02-16 06:17:30 +02:00
if (chunkProcessorG) then
return chunkProcessorG
end
local chunkProcessor = {}
-- imports
local chunkUtils = require("ChunkUtils")
2019-02-11 08:14:17 +02:00
local mathUtils = require("MathUtils")
2017-06-01 03:46:53 +02:00
local constants = require("Constants")
-- constants
local CHUNK_SIZE = constants.CHUNK_SIZE
2019-02-12 03:17:19 +02:00
local MAX_TICKS_BEFORE_SORT_CHUNKS = constants.MAX_TICKS_BEFORE_SORT_CHUNKS
-- imported functions
local createChunk = chunkUtils.createChunk
2019-05-16 07:11:43 +02:00
local mapScanChunk = chunkUtils.mapScanChunk
local initialScan = chunkUtils.initialScan
local chunkPassScan = chunkUtils.chunkPassScan
2019-02-11 08:14:17 +02:00
local euclideanDistanceNamed = mathUtils.euclideanDistanceNamed
local tSort = table.sort
local abs = math.abs
2019-10-20 22:45:43 +02:00
local tRemove = table.remove
-- module code
2019-02-11 08:14:17 +02:00
local origin = {x=0,y=0}
local function sorter(a, b)
local aDistance = euclideanDistanceNamed(a, origin)
local bDistance = euclideanDistanceNamed(b, origin)
if (aDistance == bDistance) then
if (a.x == b.x) then
return (abs(a.y) < abs(b.y))
else
return (abs(a.x) < abs(b.x))
end
end
return (aDistance < bDistance)
end
2019-11-30 02:49:22 +02:00
function chunkProcessor.processPendingChunks(map, surface, pendingStack, tick, rebuilding)
2018-01-14 07:48:21 +02:00
local processQueue = map.processQueue
2018-01-14 07:48:21 +02:00
local area = map.area
2019-02-11 08:14:17 +02:00
local topOffset = area[1]
local bottomOffset = area[2]
2019-11-30 02:49:22 +02:00
2017-08-08 10:19:51 +02:00
for i=#pendingStack, 1, -1 do
2017-06-01 03:46:53 +02:00
local event = pendingStack[i]
pendingStack[i] = nil
2019-10-19 21:13:48 +02:00
local topLeft = event.area.left_top
local x = topLeft.x
local y = topLeft.y
2017-11-21 09:27:03 +02:00
2019-10-19 21:13:48 +02:00
topOffset[1] = x
topOffset[2] = y
bottomOffset[1] = x + CHUNK_SIZE
bottomOffset[2] = y + CHUNK_SIZE
2017-11-21 09:27:03 +02:00
2019-11-30 02:49:22 +02:00
if map[x] and map[x][y] then
2019-05-16 07:11:43 +02:00
mapScanChunk(map[x][y], surface, map)
else
if map[x] == nil then
map[x] = {}
end
local chunk = createChunk(x, y)
2019-11-30 02:49:22 +02:00
chunk = initialScan(chunk, surface, map, tick, rebuilding)
2019-05-16 07:11:43 +02:00
2020-05-15 22:51:38 +02:00
if (chunk ~= -1) then
2019-05-16 07:11:43 +02:00
map[x][y] = chunk
processQueue[#processQueue+1] = chunk
end
2019-11-30 02:49:22 +02:00
end
end
2019-02-11 08:14:17 +02:00
2019-11-30 02:49:22 +02:00
if (#processQueue > map.nextChunkSort) or
(((tick - map.nextChunkSortTick) > MAX_TICKS_BEFORE_SORT_CHUNKS) and ((map.nextChunkSort - 75) ~= #processQueue))
2019-02-20 08:16:43 +02:00
then
2019-11-30 02:49:22 +02:00
map.nextChunkSort = #processQueue + 75
map.nextChunkSortTick = tick
2019-02-11 08:14:17 +02:00
tSort(processQueue, sorter)
end
end
2018-01-14 07:48:21 +02:00
function chunkProcessor.processScanChunks(map, surface)
local area = map.area
2019-11-30 02:49:22 +02:00
local topOffset = area[1]
local bottomOffset = area[2]
2019-10-20 22:45:43 +02:00
local removals = map.chunkRemovals
local chunkCount = 0
2019-10-20 22:45:43 +02:00
local chunkToPassScan = map.chunkToPassScan
2019-11-30 02:49:22 +02:00
2019-10-20 22:45:43 +02:00
for chunk,_ in pairs(chunkToPassScan) do
2019-10-19 21:13:48 +02:00
local x = chunk.x
local y = chunk.y
2019-02-11 08:14:17 +02:00
2019-10-19 21:13:48 +02:00
topOffset[1] = x
topOffset[2] = y
bottomOffset[1] = x + CHUNK_SIZE
bottomOffset[2] = y + CHUNK_SIZE
2018-01-14 07:48:21 +02:00
chunk = chunkPassScan(chunk, surface, map)
2020-05-15 22:51:38 +02:00
if (chunk == -1) then
2019-10-19 21:13:48 +02:00
map[x][y] = nil
2019-10-20 22:45:43 +02:00
chunkCount = chunkCount + 1
removals[chunkCount] = chunk
2019-10-19 21:13:48 +02:00
end
2019-11-30 02:49:22 +02:00
2019-10-20 22:45:43 +02:00
chunkToPassScan[chunk] = nil
end
2019-10-20 22:45:43 +02:00
if (chunkCount > 0) then
2019-03-10 21:28:43 +02:00
local processQueue = map.processQueue
for i=#processQueue,1,-1 do
2019-10-20 22:45:43 +02:00
for ri=chunkCount,1,-1 do
2019-03-10 21:28:43 +02:00
if (removals[ri] == processQueue[i]) then
2019-10-20 22:45:43 +02:00
tRemove(processQueue, i)
-- tRemove(removals, ri)
2019-03-10 21:28:43 +02:00
break
end
end
end
end
end
2019-02-16 06:17:30 +02:00
chunkProcessorG = chunkProcessor
return chunkProcessor