From 537a8a03e9aae8a8bbe1e3d29e2f585e37e78858 Mon Sep 17 00:00:00 2001 From: Aaron Veden Date: Tue, 5 Apr 2022 18:37:20 -0700 Subject: [PATCH] FACTO-89: Fixed on_unit_group_finished_gathering invalid chunk/base checks --- changelog.txt | 2 +- control.lua | 24 +++++++++++++++++++----- libs/ChunkPropertyUtils.lua | 15 +++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/changelog.txt b/changelog.txt index df8e7e6..df0f950 100644 --- a/changelog.txt +++ b/changelog.txt @@ -1,7 +1,7 @@ --------------------------------------------------------------------------------------------------- Version: 3.0.0 Notes: - - Will reset maps of in progress games + - Will reset maps of in progress games. This means that all enemy units will be removed and new enemy faction mutations will be reset. Improvement: - Modified death pheromone squad path scoring - AI is now per regional base diff --git a/control.lua b/control.lua index 9253f12..3b22d3a 100644 --- a/control.lua +++ b/control.lua @@ -112,6 +112,7 @@ local victoryScent = pheromoneUtils.victoryScent local createSquad = unitGroupUtils.createSquad local createBase = baseUtils.createBase +local findNearbyBaseByPosition = chunkPropertyUtils.findNearbyBaseByPosition local findNearbyBase = chunkPropertyUtils.findNearbyBase local processActiveNests = mapProcessor.processActiveNests @@ -747,11 +748,14 @@ local function onUnitGroupCreated(event) return end map.activeSurface = true - local chunk = getChunkByPosition(map, group.position) + local position = group.position + local chunk = getChunkByPosition(map, position) + local base if (chunk == -1) then - return + base = findNearbyBaseByPosition(map, position.x, position.y) + else + base = findNearbyBase(map, chunk) end - local base = findNearbyBase(map, chunk) if not base then group.destroy() return @@ -830,8 +834,18 @@ local function onGroupFinishedGathering(event) end end else - local chunk = getChunkByPosition(map, group.position) - local base = findNearbyBase(map, chunk) + local position = group.position + local chunk = getChunkByPosition(map, position) + local base + if chunk == -1 then + base = findNearbyBaseByPosition(map, position.x, position.y) + else + base = findNearbyBase(map, chunk) + end + if not base then + group.destroy() + return + end local settler = canMigrate(map, base) and (universe.builderCount < universe.AI_MAX_BUILDER_COUNT) and (universe.random() < 0.25) diff --git a/libs/ChunkPropertyUtils.lua b/libs/ChunkPropertyUtils.lua index a767b73..8985681 100644 --- a/libs/ChunkPropertyUtils.lua +++ b/libs/ChunkPropertyUtils.lua @@ -594,6 +594,21 @@ function chunkPropertyUtils.findNearbyBase(map, chunk) return foundBase end +function chunkPropertyUtils.findNearbyBaseByPosition(map, x, y) + local foundBase + + local closest = MAGIC_MAXIMUM_NUMBER + for _, base in pairs(map.bases) do + local distance = manhattenDistancePoints(base.x, base.y, x, y) + if (distance <= base.distanceThreshold) and (distance < closest) then + closest = distance + foundBase = base + end + end + + return foundBase +end + function chunkPropertyUtils.processNestActiveness(map, chunk) local nests = chunkPropertyUtils.getNestCount(map, chunk) local base = chunkPropertyUtils.findNearbyBase(map, chunk)