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

173 lines
4.6 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 mapScanEnemyChunk = chunkUtils.mapScanEnemyChunk
local mapScanPlayerChunk = chunkUtils.mapScanPlayerChunk
local mapScanResourceChunk = chunkUtils.mapScanResourceChunk
local createChunk = chunkUtils.createChunk
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
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
-- 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
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
2020-05-20 04:37:16 +02:00
local event = next(pendingChunks, map.chunkProcessorIterator)
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
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 map[x] == nil then
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
processQueue[#processQueue+1] = chunk
end
2019-05-16 07:11:43 +02:00
end
2020-05-20 04:37:16 +02:00
local newEvent,_ = next(pendingChunks, event)
pendingChunks[event] = nil
event = newEvent
2019-11-30 02:49:22 +02:00
end
2020-05-21 03:03:32 +02:00
end
2020-05-20 04:37:16 +02:00
map.chunkProcessorIterator = event
2019-02-11 08:14:17 +02:00
2019-11-30 02:49:22 +02:00
if (#processQueue > map.nextChunkSort) or
2020-05-20 04:37:16 +02:00
(((tick - map.nextChunkSortTick) > MAX_TICKS_BEFORE_SORT_CHUNKS) and
((map.nextChunkSort - 150) < #processQueue))
2019-02-20 08:16:43 +02:00
then
2020-05-20 04:37:16 +02:00
map.nextChunkSort = #processQueue + 150
2019-11-30 02:49:22 +02:00
map.nextChunkSortTick = tick
2019-02-11 08:14:17 +02:00
tSort(processQueue, sorter)
end
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