From 76dde8d8b60fecb6c675fd48afa4d84818b5d4d8 Mon Sep 17 00:00:00 2001 From: Aaron Veden Date: Fri, 26 Nov 2021 10:31:15 -0800 Subject: [PATCH] corrected typos in chunkProcessing and added one final table sort in upgrade --- Upgrade.lua | 18 +++++++++++++++++- libs/ChunkProcessor.lua | 4 ++-- libs/ChunkUtils.lua | 2 +- 3 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Upgrade.lua b/Upgrade.lua index 4bfc384..b97fe0e 100644 --- a/Upgrade.lua +++ b/Upgrade.lua @@ -29,8 +29,23 @@ local TRIPLE_CHUNK_SIZE = constants.TRIPLE_CHUNK_SIZE local euclideanDistancePoints = mathUtils.euclideanDistancePoints +local mAbs = math.abs +local tSort = table.sort + -- module code +local function sorter(a, b) + if (a.dOrigin == b.dOrigin) then + if (a.x == b.x) then + return (mAbs(a.y) < mAbs(b.y)) + else + return (mAbs(a.x) < mAbs(b.x)) + end + end + + return (a.dOrigin < b.dOrigin) +end + local function addCommandSet(queriesAndCommands) -- preallocating memory to be used in code, making it fast by reducing garbage generated. queriesAndCommands.neighbors = { @@ -429,8 +444,9 @@ function upgrade.attempt(universe) map.pendingUpgrades = {} for i=1,#map.processQueue do local chunk = map.processQueue[i] - map.processQueue[i].dOrgin = euclideanDistancePoints(chunk.x, chunk.y, 0, 0) + map.processQueue[i].dOrigin = euclideanDistancePoints(chunk.x, chunk.y, 0, 0) end + tSort(map.processQueue, sorter) for _,base in pairs(map.bases) do base.mutations = 0 end diff --git a/libs/ChunkProcessor.lua b/libs/ChunkProcessor.lua index b4d12da..23ed144 100644 --- a/libs/ChunkProcessor.lua +++ b/libs/ChunkProcessor.lua @@ -38,9 +38,9 @@ local function findInsertionPoint(processQueue, chunk) while (low <= high) do pivot = mCeil((low + high) * 0.5) local pivotChunk = processQueue[pivot] - if (pivotChunk.dOrgin > chunk.dOrgin) then + if (pivotChunk.dOrigin > chunk.dOrigin) then high = pivot - 1 - elseif (pivotChunk.dOrgin <= chunk.dOrgin) then + elseif (pivotChunk.dOrigin <= chunk.dOrigin) then low = pivot + 1 end end diff --git a/libs/ChunkUtils.lua b/libs/ChunkUtils.lua index a4230b4..9e20469 100644 --- a/libs/ChunkUtils.lua +++ b/libs/ChunkUtils.lua @@ -377,7 +377,7 @@ function chunkUtils.createChunk(topX, topY) local chunk = { x = topX, y = topY, - dOrgin = euclideanDistancePoints(topX, topY, 0, 0) + dOrigin = euclideanDistancePoints(topX, topY, 0, 0) } chunk[BASE_PHEROMONE] = 0 chunk[PLAYER_PHEROMONE] = 0