From 1bf1825320cd7d6e22fdef51d6a4621b0d044799 Mon Sep 17 00:00:00 2001 From: Dagothur Date: Wed, 30 Nov 2022 18:50:36 -0500 Subject: [PATCH 1/3] Centralize base points manipulation - Centralized base points manipulation and chat messaging - Bases can no longer spend points that exceed the overflow limit --- changelog.txt | 2 ++ control.lua | 30 +++++++----------------------- libs/AIAttackWave.lua | 33 +++++++++------------------------ libs/AIPlanning.lua | 5 +++-- libs/BaseUtils.lua | 30 ++++++++++++++++++++++++++++++ libs/ChunkUtils.lua | 16 +++++----------- 6 files changed, 56 insertions(+), 60 deletions(-) diff --git a/changelog.txt b/changelog.txt index f48f676..32d9fff 100644 --- a/changelog.txt +++ b/changelog.txt @@ -12,11 +12,13 @@ Version: 3.2.0 - Fixed worm range scaler setting not adjusting prepareRange so worms could shoot without being out of the ground. (Thanks Garrotte) - When a faction is removed during play alignment table will default to the neutral faction to prevent alignment table being nil - Fixed settler groups not respecting expansion map setting for min and max time + - Bases can no longer spend points that exceed the overflow limit Optimizations: - Moved most constants out of global - Removed new enemy variations setting - Dropped number of enemy levels see during the game from 10 to 6, new tiers will now appear at 0, 0.25, 0.5, 0.75, 0.85, 0.925 - Moved neturalObject scan chunk to after a chunk is determined to be passable + - Centralized base points manipulation and chat messaging --------------------------------------------------------------------------------------------------- Version: 3.1.2 diff --git a/control.lua b/control.lua index d60d444..6c6c4a6 100644 --- a/control.lua +++ b/control.lua @@ -152,6 +152,8 @@ local cleanSquads = squadAttack.cleanSquads local upgradeEntity = baseUtils.upgradeEntity local rebuildNativeTables = baseUtils.rebuildNativeTables +local modifyBasePoints = baseUtils.modifyBasePoints + local tRemove = table.remove local sFind = string.find @@ -177,11 +179,7 @@ local function onIonCannonFired(event) if base then base.ionCannonBlasts = base.ionCannonBlasts + 1 rallyUnits(chunk, map, event.tick, base) - base.unitPoints = base.unitPoints + 4000 - if universe.aiPointsPrintGainsToChat then - game.print(map.surface.name .. ": Points: +" .. 4000 .. ". [Ion Cannon] Total: " .. - string.format("%.2f", base.unitPoints)) - end + modifyBasePoints(base, 4000, "Ion Cannon") end end end @@ -467,10 +465,7 @@ local function onDeath(event) base.damagedBy[damageTypeName] = (base.damagedBy[damageTypeName] or 0) + 0.01 base.deathEvents = base.deathEvents + 1 end - base.unitPoints = base.unitPoints - UNIT_DEATH_POINT_COST - if universe.aiPointsPrintSpendingToChat then - game.print(map.surface.name .. ": Points: -" .. UNIT_DEATH_POINT_COST .. ". [Unit Lost] Total: " .. string.format("%.2f", base.unitPoints)) - end + modifyBasePoints(base, UNIT_DEATH_POINT_COST*-1.0, "Unit Lost") if (universe.random() < universe.rallyThreshold) and not surface.peaceful_mode then rallyUnits(chunk, map, tick, base) end @@ -492,17 +487,9 @@ local function onDeath(event) then if base then if (entityType == "unit-spawner") then - base.unitPoints = base.unitPoints + RECOVER_NEST_COST - if universe.aiPointsPrintGainsToChat then - game.print(map.surface.name .. ": Points: +" .. RECOVER_NEST_COST .. - ". [Nest Lost] Total: " .. string.format("%.2f", base.unitPoints)) - end + modifyBasePoints(base, RECOVER_NEST_COST, "Nest Lost") elseif (entityType == "turret") then - base.unitPoints = base.unitPoints + RECOVER_WORM_COST - if universe.aiPointsPrintGainsToChat then - game.print(map.surface.name .. ": Points: +" .. RECOVER_WORM_COST .. - ". [Worm Lost] Total: " .. string.format("%.2f", base.unitPoints)) - end + modifyBasePoints(base, RECOVER_WORM_COST, "Worm Lost") end rallyUnits(chunk, map, tick, base) if artilleryBlast then @@ -659,10 +646,7 @@ local function onRocketLaunch(event) local base = findNearbyBase(map, chunk) if base then base.rocketLaunched = base.rocketLaunched + 1 - base.unitPoints = base.unitPoints + 5000 - if universe.aiPointsPrintGainsToChat then - game.print(map.surface.name .. ": Points: +" .. 5000 .. ". [Rocket Launch] Total: " .. string.format("%.2f", base.unitPoints)) - end + modifyBasePoints(base, 5000, "Rocket Launch") end end end diff --git a/libs/AIAttackWave.lua b/libs/AIAttackWave.lua index 420b3fa..9e81ba5 100644 --- a/libs/AIAttackWave.lua +++ b/libs/AIAttackWave.lua @@ -27,6 +27,7 @@ local chunkPropertyUtils = require("ChunkPropertyUtils") local unitGroupUtils = require("UnitGroupUtils") local movementUtils = require("MovementUtils") local mathUtils = require("MathUtils") +local baseUtils = require("libs/BaseUtils") -- constants @@ -76,6 +77,8 @@ local getDeathGeneratorRating = chunkPropertyUtils.getDeathGeneratorRating local mCeil = math.ceil +local modifyBasePoints = baseUtils.modifyBasePoints + -- module code local function settlerWaveScaling(universe) @@ -256,12 +259,7 @@ function aiAttackWave.formSettlers(map, chunk, base) squad.kamikaze = map.random() < kamikazeThreshold universe.builderCount = universe.builderCount + 1 - base.unitPoints = base.unitPoints - AI_SETTLER_COST - if universe.aiPointsPrintSpendingToChat then - game.print(map.surface.name .. ": Points: -" .. AI_SETTLER_COST .. ". [Settler] Total: " .. - string.format("%.2f", base.unitPoints) .. " [gps=" .. squadPosition.x .. "," .. - squadPosition.y .. "]") - end + modifyBasePoints(base, AI_SETTLER_COST*-1.0, "Settler", squadPosition.x, squadPosition.y) universe.groupNumberToSquad[squad.groupNumber] = squad else if (squad.group.valid) then @@ -306,11 +304,7 @@ function aiAttackWave.formVengenceSquad(map, chunk, base) squad.kamikaze = map.random() < calculateKamikazeSquadThreshold(foundUnits, universe) universe.groupNumberToSquad[squad.groupNumber] = squad universe.squadCount = universe.squadCount + 1 - base.unitPoints = base.unitPoints - AI_VENGENCE_SQUAD_COST - if universe.aiPointsPrintSpendingToChat then - game.print(map.surface.name .. ": Points: -" .. AI_VENGENCE_SQUAD_COST .. ". [Vengence] Total: " .. - string.format("%.2f", base.unitPoints) .. " [gps=" .. squadPosition.x .. "," .. squadPosition.y .. "]") - end + modifyBasePoints(base, AI_VENGENCE_SQUAD_COST*-1.0, "Vengence", squadPosition.x, squadPosition.y) else if (squad.group.valid) then squad.group.destroy() @@ -356,12 +350,8 @@ function aiAttackWave.formVengenceSettler(map, chunk, base) squad.base = base squad.kamikaze = map.random() < calculateKamikazeSettlerThreshold(foundUnits, universe) universe.groupNumberToSquad[squad.groupNumber] = squad - universe.builderCount = universe.builderCount + 1 - base.unitPoints = base.unitPoints - AI_VENGENCE_SQUAD_COST - if universe.aiPointsPrintSpendingToChat then - game.print(map.surface.name .. ": Points: -" .. AI_VENGENCE_SQUAD_COST .. ". [Vengence Settlers] Total: " .. - string.format("%.2f", base.unitPoints) .. " [gps=" .. squadPosition.x .. "," .. squadPosition.y .. "]") - end + universe.builderCount = universe.builderCount + 1 + modifyBasePoints(base, AI_VENGENCE_SQUAD_COST*-1.0, "Vengence Settlers", squadPosition.x, squadPosition.y) else if (squad.group.valid) then squad.group.destroy() @@ -404,17 +394,12 @@ function aiAttackWave.formSquads(map, chunk, base) if (foundUnits > 0) then squad.base = base squad.kamikaze = map.random() < calculateKamikazeSquadThreshold(foundUnits, universe) - base.unitPoints = base.unitPoints - AI_SQUAD_COST universe.squadCount = universe.squadCount + 1 universe.groupNumberToSquad[squad.groupNumber] = squad if (base.stateAI == BASE_AI_STATE_AGGRESSIVE) then base.sentAggressiveGroups = base.sentAggressiveGroups + 1 - end - if universe.aiPointsPrintSpendingToChat then - game.print(map.surface.name .. ": Points: -" .. AI_SQUAD_COST .. ". [Squad] Total: " .. - string.format("%.2f", base.unitPoints) .. " [gps=" .. squadPosition.x .. "," .. - squadPosition.y .. "]") - end + end + modifyBasePoints(base, AI_SQUAD_COST*-1.0, "Squad", squadPosition.x, squadPosition.y) else if (squad.group.valid) then squad.group.destroy() diff --git a/libs/AIPlanning.lua b/libs/AIPlanning.lua index 3e4d383..4c904dd 100644 --- a/libs/AIPlanning.lua +++ b/libs/AIPlanning.lua @@ -73,6 +73,7 @@ local randomTickEvent = mathUtils.randomTickEvent local randomTickDuration = mathUtils.randomTickDuration local upgradeBaseBasedOnDamage = baseUtils.upgradeBaseBasedOnDamage +local modifyBasePoints = baseUtils.modifyBasePoints local linearInterpolation = mathUtils.linearInterpolation @@ -175,9 +176,9 @@ local function processBase(universe, base, tick) end if (currentPoints < universe.maxPoints) then - base.unitPoints = currentPoints + points + modifyBasePoints(base, points, "Logic Cycle") elseif currentPoints > universe.maxOverflowPoints then - base.unitPoints = universe.maxOverflowPoints + --modifyBasePoints(base, (base.unitPoints - universe.maxOverflowPoints)*-1.0, "Logic Cycle - Point Cap Reached") end if (base.points < universe.maxPoints) then diff --git a/libs/BaseUtils.lua b/libs/BaseUtils.lua index 6c6d812..7dc061f 100644 --- a/libs/BaseUtils.lua +++ b/libs/BaseUtils.lua @@ -546,5 +546,35 @@ function baseUtils.rebuildNativeTables(universe) end end +function baseUtils.modifyBasePoints(base, points, tag, x, y) + tag = tag or "" + x = x or nil + y = y or nil + + base.unitPoints = base.unitPoints + points + + local universe = base.universe + local overflowMessage = "" + if base.unitPoints > universe.maxOverflowPoints then + base.unitPoints = universe.maxOverflowPoints + overflowMessage = " [Point cap reached]" + end + + local printPointChange = "" + if points > 0 and universe.aiPointsPrintGainsToChat then + printPointChange = "+" .. string.format("%.2f", points) + elseif points < 0 and universe.aiPointsPrintSpendingToChat then + printPointChange = string.format("%.2f", points) + end + + if printPointChange ~= "" then + local gps = "" + if x ~= nil then + gps = " [gps=" .. x .. "," .. y .. "]" + end + game.print("[" .. base.id .. "]:" .. base.map.surface.name .. " " .. printPointChange .. " [" .. tag .. "] Total:" .. string.format("%.2f", base.unitPoints) .. overflowMessage .. gps) + end +end + baseUtilsG = baseUtils return baseUtils diff --git a/libs/ChunkUtils.lua b/libs/ChunkUtils.lua index d8e38b4..79ec3af 100644 --- a/libs/ChunkUtils.lua +++ b/libs/ChunkUtils.lua @@ -98,9 +98,10 @@ local removeChunkBase = chunkPropertyUtils.removeChunkBase local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount local findNearbyBase = chunkPropertyUtils.findNearbyBase -local createBase = baseUtils.createBase +local createBase = baseUtils.createBase local upgradeEntity = baseUtils.upgradeEntity +local modifyBasePoints = baseUtils.modifyBasePoints local euclideanDistancePoints = mathUtils.euclideanDistancePoints @@ -590,17 +591,10 @@ function chunkUtils.accountPlayerEntity(entity, map, addObject, base) pointValue = 0 end base.destroyPlayerBuildings = base.destroyPlayerBuildings + 1 - if (base.stateAI == BASE_AI_STATE_ONSLAUGHT) then - base.unitPoints = base.unitPoints + pointValue - if universe.aiPointsPrintGainsToChat then - game.print(map.surface.name .. ": Points: +" .. math.floor(pointValue) .. ". [Structure Kill] Total: " .. string.format("%.2f", base.unitPoints)) - end - else - base.unitPoints = base.unitPoints + (pointValue * 0.12) - if universe.aiPointsPrintGainsToChat then - game.print(map.surface.name .. ": Points: +" .. math.floor(pointValue) .. ". [Structure Kill] Total: " .. string.format("%.2f", base.unitPoints)) - end + if (base.stateAI ~= BASE_AI_STATE_ONSLAUGHT) then + pointValue = pointValue * 0.12 end + modifyBasePoints(base, pointValue, "Structure Kill") end entityValue = -entityValue end From 765716797890bbd22098e8715bcf2e5c5650f27d Mon Sep 17 00:00:00 2001 From: Dagothur Date: Fri, 2 Dec 2022 14:10:42 -0500 Subject: [PATCH 2/3] Batch unit death point deductions Base point deductions for unit losses are now batched in 20 to reduce chat spam when using the print AI points to chat options --- Upgrade.lua | 1 + changelog.txt | 1 + control.lua | 5 ++++- libs/BaseUtils.lua | 1 + 4 files changed, 7 insertions(+), 1 deletion(-) diff --git a/Upgrade.lua b/Upgrade.lua index 11f3ede..881e89d 100644 --- a/Upgrade.lua +++ b/Upgrade.lua @@ -636,6 +636,7 @@ function upgrade.attempt(universe) base.maxExpansionGroups = 0 base.sentExpansionGroups = 0 base.resetExpensionGroupsTick = 0 + base.totalLostEnemyUnits = 0 end game.print("Rampant - Version 3.2.0") diff --git a/changelog.txt b/changelog.txt index 32d9fff..bf41f15 100644 --- a/changelog.txt +++ b/changelog.txt @@ -19,6 +19,7 @@ Version: 3.2.0 - Dropped number of enemy levels see during the game from 10 to 6, new tiers will now appear at 0, 0.25, 0.5, 0.75, 0.85, 0.925 - Moved neturalObject scan chunk to after a chunk is determined to be passable - Centralized base points manipulation and chat messaging + - Base point deductions for unit losses are now batched in 20 to reduce chat spam when using the print AI points to chat options --------------------------------------------------------------------------------------------------- Version: 3.1.2 diff --git a/control.lua b/control.lua index 6c6c4a6..6ed1f38 100644 --- a/control.lua +++ b/control.lua @@ -461,11 +461,14 @@ local function onDeath(event) if (entityType == "unit") and not ENTITY_SKIP_COUNT_LOOKUP[entity.name] then if base then base.lostEnemyUnits = base.lostEnemyUnits + 1 + base.totalLostEnemyUnits = base.totalLostEnemyUnits + 1 if damageTypeName then base.damagedBy[damageTypeName] = (base.damagedBy[damageTypeName] or 0) + 0.01 base.deathEvents = base.deathEvents + 1 end - modifyBasePoints(base, UNIT_DEATH_POINT_COST*-1.0, "Unit Lost") + if base.totalLostEnemyUnits % 20 == 0 then + modifyBasePoints(base, 20*UNIT_DEATH_POINT_COST*-1.0, "20 Units Lost") + end if (universe.random() < universe.rallyThreshold) and not surface.peaceful_mode then rallyUnits(chunk, map, tick, base) end diff --git a/libs/BaseUtils.lua b/libs/BaseUtils.lua index 7dc061f..991c7c4 100644 --- a/libs/BaseUtils.lua +++ b/libs/BaseUtils.lua @@ -508,6 +508,7 @@ function baseUtils.createBase(map, chunk, tick) activeNests = 0, destroyPlayerBuildings = 0, lostEnemyUnits = 0, + totalLostEnemyUnits = 0, lostEnemyBuilding = 0, rocketLaunched = 0, builtEnemyBuilding = 0, From de19f8df3e97286ea4b94b3172ad1c9f14eef798 Mon Sep 17 00:00:00 2001 From: Dagothur Date: Fri, 2 Dec 2022 14:19:14 -0500 Subject: [PATCH 3/3] Bases no longer gain points in AI_STATE_PEACEFUL --- changelog.txt | 1 + libs/BaseUtils.lua | 5 +++++ 2 files changed, 6 insertions(+) diff --git a/changelog.txt b/changelog.txt index bf41f15..90b944c 100644 --- a/changelog.txt +++ b/changelog.txt @@ -6,6 +6,7 @@ Version: 3.2.0 - Added minimum evolution of 20% before faction adaptation will happen - Increased the death thresholds to 17000 <50%, 34000 <70%, 60000 <90%, 100000 for adaptation - Changed spawner pollution check to use 75% of the chunk pollution diffuse value + - Bases no longer gain points in AI_STATE_PEACEFUL Bugfixes: - Removed layer-13 from projectiles - script_raised_built now looks for enemy faction and registers as needed diff --git a/libs/BaseUtils.lua b/libs/BaseUtils.lua index 991c7c4..7d5fa0b 100644 --- a/libs/BaseUtils.lua +++ b/libs/BaseUtils.lua @@ -548,6 +548,11 @@ function baseUtils.rebuildNativeTables(universe) end function baseUtils.modifyBasePoints(base, points, tag, x, y) + + if points > 0 and base.stateAI == BASE_AI_STATE_PEACEFUL then + return + end + tag = tag or "" x = x or nil y = y or nil