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

167 lines
4.5 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")
2017-06-01 03:46:53 +02:00
local constants = require("Constants")
-- constants
local CHUNK_SIZE = constants.CHUNK_SIZE
-- imported functions
local mapScanEnemyChunk = chunkUtils.mapScanEnemyChunk
local mapScanPlayerChunk = chunkUtils.mapScanPlayerChunk
local mapScanResourceChunk = chunkUtils.mapScanResourceChunk
local createChunk = chunkUtils.createChunk
local initialScan = chunkUtils.initialScan
local chunkPassScan = chunkUtils.chunkPassScan
2020-05-20 04:37:16 +02:00
local next = next
local table_size = table_size
2019-02-11 08:14:17 +02:00
2019-10-20 22:45:43 +02:00
local tRemove = table.remove
local tInsert = table.insert
local mCeil = math.ceil
2019-10-20 22:45:43 +02:00
-- module code
local function findInsertionPoint(processQueue, chunk)
local low = 1
local high = #processQueue
local pivot
while (low <= high) do
pivot = mCeil((low + high) * 0.5)
local pivotChunk = processQueue[pivot]
if (pivotChunk.dOrigin > chunk.dOrigin) then
high = pivot - 1
elseif (pivotChunk.dOrigin <= chunk.dOrigin) then
low = pivot + 1
2019-02-11 08:14:17 +02:00
end
end
return low
2019-02-11 08:14:17 +02:00
end
2021-02-20 07:41:30 +02:00
function chunkProcessor.processPendingChunks(map, tick, flush)
2018-01-14 07:48:21 +02:00
local processQueue = map.processQueue
2020-05-20 04:37:16 +02:00
local pendingChunks = map.pendingChunks
2021-02-20 09:31:36 +02:00
local area = map.universe.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
local event = map.chunkProcessorIterator
if not event then
event = next(pendingChunks, nil)
end
2021-02-20 07:41:30 +02:00
local endCount = 2
2020-05-20 04:37:16 +02:00
if flush then
endCount = table_size(pendingChunks)
end
2021-02-14 06:49:54 +02:00
for _=1,endCount do
2020-05-20 04:37:16 +02:00
if not event then
map.chunkProcessorIterator = nil
if (table_size(pendingChunks) == 0) then
-- this is needed as the next command remembers the max length a table has been
map.pendingChunks = {}
2019-05-16 07:11:43 +02:00
end
2020-05-20 04:37:16 +02:00
break
else
if (event.tick > tick) then
map.chunkProcessorIterator = event
return
end
2020-05-20 04:37:16 +02:00
local topLeft = event.area.left_top
local x = topLeft.x
local y = topLeft.y
topOffset[1] = x
topOffset[2] = y
bottomOffset[1] = x + CHUNK_SIZE
bottomOffset[2] = y + CHUNK_SIZE
if map[x] and map[x][y] then
local chunk = map[x][y]
2021-02-20 09:31:36 +02:00
mapScanPlayerChunk(chunk, map)
mapScanEnemyChunk(chunk, map)
mapScanResourceChunk(chunk, map)
2020-05-20 04:37:16 +02:00
else
if not map[x] then
2020-05-20 04:37:16 +02:00
map[x] = {}
end
local chunk = createChunk(x, y)
2021-02-20 07:41:30 +02:00
chunk = initialScan(chunk, map, tick)
2020-05-20 04:37:16 +02:00
if (chunk ~= -1) then
map[x][y] = chunk
tInsert(
processQueue,
findInsertionPoint(processQueue, chunk),
chunk
)
2020-05-20 04:37:16 +02:00
end
2019-05-16 07:11:43 +02:00
end
local newEvent = next(pendingChunks, event)
2020-05-20 04:37:16 +02:00
pendingChunks[event] = nil
event = newEvent
2019-11-30 02:49:22 +02:00
end
end
2020-05-20 04:37:16 +02:00
map.chunkProcessorIterator = event
end
2021-02-20 07:41:30 +02:00
function chunkProcessor.processScanChunks(map)
2021-02-20 09:31:36 +02:00
local area = map.universe.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
2021-04-22 05:46:50 +02:00
for preScanChunk in pairs(chunkToPassScan) do
local x = preScanChunk.x
local y = preScanChunk.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
2021-04-22 05:46:50 +02:00
if (chunkPassScan(preScanChunk, map) == -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
2021-04-22 05:46:50 +02:00
removals[chunkCount] = preScanChunk
2019-10-19 21:13:48 +02:00
end
2019-11-30 02:49:22 +02:00
2021-04-22 05:46:50 +02:00
chunkToPassScan[preScanChunk] = 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