diff --git a/.luacheckrc b/.luacheckrc index 5187397..d080c7a 100644 --- a/.luacheckrc +++ b/.luacheckrc @@ -1,4 +1,5 @@ globals = { + "mods", "game", "util", "data", diff --git a/Upgrade.lua b/Upgrade.lua index 954e1c8..bc0a843 100644 --- a/Upgrade.lua +++ b/Upgrade.lua @@ -485,8 +485,6 @@ function upgrade.attempt(universe) game.map_settings.path_finder.min_steps_to_check_path_find_termination = constants.PATH_FINDER_MIN_STEPS_TO_CHECK_PATH - universe.evolutionTableAlignment = {} - universe.kamikazeThreshold = 0 universe.attackWaveLowerBound = 1 @@ -512,8 +510,6 @@ function upgrade.attempt(universe) universe.printAIStateChanges = settings.global["rampant--printAIStateChanges"].value universe.debugTemperament = settings.global["rampant--debugTemperament"].value - universe.enemyAlignmentLookup = {} - game.map_settings.unit_group.min_group_radius = constants.UNIT_GROUP_MAX_RADIUS * 0.5 game.map_settings.unit_group.max_group_radius = constants.UNIT_GROUP_MAX_RADIUS @@ -576,19 +572,10 @@ function upgrade.attempt(universe) universe.maxPoints = 0 universe.maxOverflowPoints = 0 - universe.proxyEntityLookup = {} - universe.vanillaEntityLookups = {} - addCommandSet(universe) - local evoToTierMapping = {} - universe.evoToTierMapping = evoToTierMapping universe.bases = {} - for i=1,10 do - evoToTierMapping[#evoToTierMapping+1] = (((i - 1) * 0.1) ^ 0.5) - 0.05 - end - for _,map in pairs(universe.maps) do if (map.surface.valid) then local entities = map.surface.find_entities_filtered({type="land-mine"}) @@ -605,11 +592,6 @@ function upgrade.attempt(universe) universe.processBaseAIIterator = nil end - if global.version < 303 then - global.version = 303 - - universe.entitySkipCountLookup = {} - end if global.version < 304 then global.version = 304 @@ -630,6 +612,18 @@ function upgrade.attempt(universe) universe.excludedSurfaces = {} + universe.evolutionTableAlignment = nil + universe.buildingSpaceLookup = nil + universe.enemyAlignmentLookup = nil + universe.upgradeLookup = nil + universe.buildingEvolveLookup = nil + universe.costLookup = nil + universe.buildingHiveTypeLookup = nil + universe.proxyEntityLookup = nil + universe.vanillaEntityTypeLookup = nil + universe.entitySkipCountLookup = nil + universe.evoToTierMapping = nil + game.print("Rampant - Version 3.1.3") end diff --git a/changelog.txt b/changelog.txt index edfba4e..b56d54f 100644 --- a/changelog.txt +++ b/changelog.txt @@ -5,6 +5,8 @@ Version: 3.1.3 Bugfixes: - Removed layer-13 from projectiles - script_raised_built now looks for enemy faction and registers as needed + Optimizations: + - Moved most constants out of global --------------------------------------------------------------------------------------------------- Version: 3.1.2 diff --git a/control.lua b/control.lua index 3d48029..69b73e3 100644 --- a/control.lua +++ b/control.lua @@ -38,6 +38,9 @@ local queryUtils = require("libs/QueryUtils") -- constants +local ENTITY_SKIP_COUNT_LOOKUP = constants.ENTITY_SKIP_COUNT_LOOKUP +local BUILDING_HIVE_TYPE_LOOKUP = constants.BUILDING_HIVE_TYPE_LOOKUP + local TICKS_A_MINUTE = constants.TICKS_A_MINUTE local COMMAND_TIMEOUT = constants.COMMAND_TIMEOUT @@ -293,15 +296,7 @@ local function onConfigChanged() universe["NEW_ENEMIES"] = settings.startup["rampant--newEnemies"].value if universe.NEW_ENEMIES then - rebuildNativeTables(universe, universe.random) - else - universe.buildingHiveTypeLookup = {} - universe.buildingHiveTypeLookup["biter-spawner"] = "biter-spawner" - universe.buildingHiveTypeLookup["spitter-spawner"] = "spitter-spawner" - universe.buildingHiveTypeLookup["small-worm-turret"] = "turret" - universe.buildingHiveTypeLookup["medium-worm-turret"] = "turret" - universe.buildingHiveTypeLookup["big-worm-turret"] = "turret" - universe.buildingHiveTypeLookup["behemoth-worm-turret"] = "turret" + rebuildNativeTables(universe) end -- not a completed implementation needs if checks to use all forces @@ -384,7 +379,7 @@ local function onBuild(event) local entity = event.created_entity or event.entity if entity.valid then local entityForceName = entity.force.name - if entityForceName == "enemy" and universe.buildingHiveTypeLookup[entity.name] then + if entityForceName == "enemy" and BUILDING_HIVE_TYPE_LOOKUP[entity.name] then onEnemyBaseBuild(event) else local map = universe.maps[entity.surface.index] @@ -465,7 +460,7 @@ local function onDeath(event) end local artilleryBlast = (cause and ((cause.type == "artillery-wagon") or (cause.type == "artillery-turret"))) - if (entityType == "unit") and not universe.entitySkipCountLookup[entity.name] then + if (entityType == "unit") and not ENTITY_SKIP_COUNT_LOOKUP[entity.name] then if base then base.lostEnemyUnits = base.lostEnemyUnits + 1 if damageTypeName then @@ -491,7 +486,7 @@ local function onDeath(event) tick, (artilleryBlast and RETREAT_SPAWNER_GRAB_RADIUS) or RETREAT_GRAB_RADIUS) end - elseif universe.buildingHiveTypeLookup[entity.name] or + elseif BUILDING_HIVE_TYPE_LOOKUP[entity.name] or (entityType == "unit-spawner") or (entityType == "turret") then @@ -679,7 +674,7 @@ local function onEntitySpawned(entity, tick) if not map then return end - if universe.buildingHiveTypeLookup[entity.name] then + if BUILDING_HIVE_TYPE_LOOKUP[entity.name] then map.activeSurface = true local disPos = distortPosition(universe.random, entity.position, 8) diff --git a/libs/AIPlanning.lua b/libs/AIPlanning.lua index 3f22a6a..efa7493 100644 --- a/libs/AIPlanning.lua +++ b/libs/AIPlanning.lua @@ -52,8 +52,6 @@ local RETREAT_MOVEMENT_PHEROMONE_LEVEL_MIN = constants.RETREAT_MOVEMENT_PHEROMON local RETREAT_MOVEMENT_PHEROMONE_LEVEL_MAX = constants.RETREAT_MOVEMENT_PHEROMONE_LEVEL_MAX local MINIMUM_AI_POINTS = constants.MINIMUM_AI_POINTS -local AGGRESSIVE_CAN_ATTACK_WAIT_MAX_DURATION = constants.AGGRESSIVE_CAN_ATTACK_WAIT_MAX_DURATION -local AGGRESSIVE_CAN_ATTACK_WAIT_MIN_DURATION = constants.AGGRESSIVE_CAN_ATTACK_WAIT_MIN_DURATION local ACTIVE_NESTS_PER_AGGRESSIVE_GROUPS = constants.ACTIVE_NESTS_PER_AGGRESSIVE_GROUPS local ALL_NESTS_PER_SIEGE_GROUPS = constants.ALL_NESTS_PER_SIEGE_GROUPS diff --git a/libs/BaseUtils.lua b/libs/BaseUtils.lua index f75785d..92cf937 100644 --- a/libs/BaseUtils.lua +++ b/libs/BaseUtils.lua @@ -29,6 +29,17 @@ local queryUtils = require("QueryUtils") -- constants +local EVO_TO_TIER_MAPPING = constants.EVO_TO_TIER_MAPPING +local PROXY_ENTITY_LOOKUP = constants.PROXY_ENTITY_LOOKUP +local BUILDING_HIVE_TYPE_LOOKUP = constants.BUILDING_HIVE_TYPE_LOOKUP +local COST_LOOKUP = constants.COST_LOOKUP +local UPGRADE_LOOKUP = constants.UPGRADE_LOOKUP + +local ENEMY_ALIGNMENT_LOOKUP = constants.ENEMY_ALIGNMENT_LOOKUP + +local EVOLUTION_TABLE_ALIGNMENT = constants.EVOLUTION_TABLE_ALIGNMENT +local BUILDING_EVOLVE_LOOKUP = constants.BUILDING_EVOLVE_LOOKUP + local MINIMUM_BUILDING_COST = constants.MINIMUM_BUILDING_COST local FACTION_MUTATION_MAPPING = constants.FACTION_MUTATION_MAPPING @@ -38,10 +49,6 @@ local FACTIONS_BY_DAMAGE_TYPE = constants.FACTIONS_BY_DAMAGE_TYPE local BASE_GENERATION_STATE_ACTIVE = constants.BASE_GENERATION_STATE_ACTIVE -local FACTION_SET = constants.FACTION_SET - -local HIVE_BUILDINGS_COST = constants.HIVE_BUILDINGS_COST - local BASE_DISTANCE_THRESHOLD = constants.BASE_DISTANCE_THRESHOLD local BASE_DISTANCE_LEVEL_BONUS = constants.BASE_DISTANCE_LEVEL_BONUS local BASE_DISTANCE_TO_EVO_INDEX = constants.BASE_DISTANCE_TO_EVO_INDEX @@ -60,13 +67,10 @@ local getChunkByPosition = mapUtils.getChunkByPosition local gaussianRandomRangeRG = mathUtils.gaussianRandomRangeRG -local linearInterpolation = mathUtils.linearInterpolation - local mFloor = math.floor local mMin = math.min local mMax = math.max -local distort = mathUtils.distort local getResourceGenerator = chunkPropertyUtils.getResourceGenerator @@ -78,7 +82,7 @@ local function evoToTier(universe, evolutionFactor, maxSkips) local v local skipsRemaining = maxSkips for i=10,1,-1 do - if universe.evoToTierMapping[i] <= evolutionFactor then + if EVO_TO_TIER_MAPPING[i] <= evolutionFactor then v = i if (skipsRemaining == 0) or (universe.random() <= 0.75) then break @@ -91,7 +95,7 @@ end local function findBaseMutation(universe, targetEvolution) local tier = evoToTier(universe, targetEvolution or universe.evolutionLevel, 2) - local alignments = universe.evolutionTableAlignment[tier] + local alignments = EVOLUTION_TABLE_ALIGNMENT[tier] local roll = universe.random() for i=1,#alignments do @@ -107,7 +111,6 @@ local function findBaseMutation(universe, targetEvolution) end local function initialEntityUpgrade(baseAlignment, tier, maxTier, map, useHiveType, entityType) - local evolutionTable = map.universe.buildingEvolveLookup local entity local useTier @@ -123,7 +126,7 @@ local function initialEntityUpgrade(baseAlignment, tier, maxTier, map, useHiveTy useTier = mMax(maxTier - 3, tier) end - local alignmentTable = evolutionTable[baseAlignment] + local alignmentTable = BUILDING_EVOLVE_LOOKUP[baseAlignment] local upgrades = alignmentTable[useTier] if upgrades then @@ -162,15 +165,12 @@ local function initialEntityUpgrade(baseAlignment, tier, maxTier, map, useHiveTy end local function entityUpgrade(baseAlignment, tier, maxTier, originalEntity, map) - local universe = map.universe - local buildingHiveTypeLookup = universe.buildingHiveTypeLookup - local evolutionTable = universe.upgradeLookup local entity - local hiveType = buildingHiveTypeLookup[originalEntity.name] + local hiveType = BUILDING_HIVE_TYPE_LOOKUP[originalEntity.name] for t=maxTier,tier,-1 do - local factionLookup = evolutionTable[baseAlignment][t] + local factionLookup = UPGRADE_LOOKUP[baseAlignment][t] local upgrades = factionLookup[hiveType] if not upgrades then local mapTypes = FACTION_MUTATION_MAPPING[hiveType] @@ -196,7 +196,7 @@ end local function findEntityUpgrade(baseAlignment, currentEvo, evoIndex, originalEntity, map, evolve) local universe = map.universe local adjCurrentEvo = mMax( - ((baseAlignment ~= universe.enemyAlignmentLookup[originalEntity.name]) and 0) or currentEvo, + ((baseAlignment ~= ENEMY_ALIGNMENT_LOOKUP[originalEntity.name]) and 0) or currentEvo, 0 ) @@ -210,7 +210,7 @@ local function findEntityUpgrade(baseAlignment, currentEvo, evoIndex, originalEn if evolve then local chunk = getChunkByPosition(map, originalEntity.position) local entityName = originalEntity.name - local entityType = map.universe.buildingHiveTypeLookup[entityName] + local entityType = BUILDING_HIVE_TYPE_LOOKUP[entityName] if not entityType then if map.random() < 0.5 then entityType = "biter-spawner" @@ -227,7 +227,7 @@ local function findEntityUpgrade(baseAlignment, currentEvo, evoIndex, originalEn ( ( (roll <= 0.01) and - not map.universe.proxyEntityLookup[entityName] + not PROXY_ENTITY_LOOKUP[entityName] ) or ( @@ -321,7 +321,7 @@ function baseUtils.upgradeEntity(entity, base, map, disPos, evolve, register, ti return spawnerName end if entity.valid then - if map.universe.proxyEntityLookup[entity.name] then + if PROXY_ENTITY_LOOKUP[entity.name] then entity.destroy() end end @@ -437,7 +437,7 @@ function baseUtils.processBaseMutation(chunk, map, base) local entities = surface.find_entities_filtered(universe.pbFilteredEntitiesPointQueryLimited) if #entities ~= 0 then local entity = entities[1] - local cost = (universe.costLookup[entity.name] or MAGIC_MAXIMUM_NUMBER) + local cost = (COST_LOOKUP[entity.name] or MAGIC_MAXIMUM_NUMBER) if (base.points >= cost) then local newEntity = baseUtils.upgradeEntity(entity, base, map) if newEntity then @@ -520,180 +520,14 @@ function baseUtils.createBase(map, chunk, tick) return base end -local function isMember(lst, x) - for _,l in pairs(lst) do - if l == x then - return true - end - end - return false -end - -function baseUtils.rebuildNativeTables(universe, rg) - local alignmentSet = {} - universe.evolutionTableAlignment = alignmentSet - local buildingSpaceLookup = {} - universe.buildingSpaceLookup = buildingSpaceLookup - local enemyAlignmentLookup = {} - universe.enemyAlignmentLookup = enemyAlignmentLookup - local upgradeLookup = {} - universe.upgradeLookup = upgradeLookup - local buildingEvolveLookup = {} - universe.buildingEvolveLookup = buildingEvolveLookup - local costLookup = {} - universe.costLookup = costLookup - local buildingHiveTypeLookup = {} - universe.buildingHiveTypeLookup = buildingHiveTypeLookup - local proxyEntityLookup = {} - universe.proxyEntityLookup = proxyEntityLookup - local vanillaEntityLookup = {} - universe.vanillaEntityTypeLookup = vanillaEntityLookup - local entitySkipCountLookup = {} - universe.entitySkipCountLookup = entitySkipCountLookup - - buildingHiveTypeLookup["biter-spawner"] = "biter-spawner" - buildingHiveTypeLookup["spitter-spawner"] = "spitter-spawner" - buildingHiveTypeLookup["small-worm-turret"] = "turret" - buildingHiveTypeLookup["medium-worm-turret"] = "turret" - buildingHiveTypeLookup["big-worm-turret"] = "turret" - buildingHiveTypeLookup["behemoth-worm-turret"] = "turret" - - vanillaEntityLookup["biter-spawner"] = true - vanillaEntityLookup["spitter-spawner"] = true - vanillaEntityLookup["small-worm-turret"] = true - vanillaEntityLookup["medium-worm-turret"] = true - vanillaEntityLookup["big-worm-turret"] = true - vanillaEntityLookup["behemoth-worm-turret"] = true - - for i=1,#FACTION_SET do - local faction = FACTION_SET[i] - - local factionUpgradeLookup = {} - upgradeLookup[faction.type] = factionUpgradeLookup - local factionBuildingPicker = {} - buildingEvolveLookup[faction.type] = factionBuildingPicker - - for t=1,10 do - local alignments = alignmentSet[t] - if not alignments then - alignments = {} - alignmentSet[t] = alignments - end - - --[[ - alignments table is a table that is used for selecting what factions are available - to pick given an evolution level. - - evolutionTable is a table that given a faction allows the selection of a building - type based on the propabilities given. Once the the building type is selected given - a faction, then the evolution decides what level of building to select - --]] - local factionAcceptRate = faction.acceptRate - - local low = factionAcceptRate[1] - local high = factionAcceptRate[2] - if (low <= t) and (t <= high) then - alignments[#alignments+1] = { - distort(rg, - linearInterpolation((t - low) / (high - low), factionAcceptRate[3], factionAcceptRate[4])), - faction.type - } - end - - local tieredUpgradeBuildingSet = factionUpgradeLookup[t] - if not tieredUpgradeBuildingSet then - tieredUpgradeBuildingSet = {} - factionUpgradeLookup[t] = tieredUpgradeBuildingSet - end - - local tieredBuildingPickerSet = factionBuildingPicker[t] - if not tieredBuildingPickerSet then - tieredBuildingPickerSet = {} - factionBuildingPicker[t] = tieredBuildingPickerSet - end - - for b=1,#faction.buildings do - local building = faction.buildings[b] - - local buildingSet = tieredUpgradeBuildingSet[building.type] - if not buildingSet then - buildingSet = {} - tieredUpgradeBuildingSet[building.type] = buildingSet - end - - local variationSet = {} - for v=1,universe.ENEMY_VARIATIONS do - local entry = faction.type .. "-" .. building.name .. "-v" .. v .. "-t" .. t .. "-rampant" - enemyAlignmentLookup[entry] = faction.type - local proxyEntity = "entity-proxy-" .. building.type .. "-t" .. t .. "-rampant" - proxyEntityLookup[proxyEntity] = true - buildingSpaceLookup[entry] = proxyEntity - costLookup[entry] = HIVE_BUILDINGS_COST[building.type] - buildingHiveTypeLookup[entry] = building.type - if not buildingHiveTypeLookup[proxyEntity] then - buildingHiveTypeLookup[proxyEntity] = building.type - end - variationSet[#variationSet+1] = entry - for _,unit in pairs(faction.units) do - if isMember(unit.attributes, "skipKillCount") then - local name = faction.type .. "-" .. unit.name .. "-v" .. v .. "-t" .. t .. "-rampant" - universe.entitySkipCountLookup[name] = true - end - end - end - - local buildingAcceptRate = building.acceptRate - - local buildingLow = buildingAcceptRate[1] - local buildingHigh = buildingAcceptRate[2] - if (buildingLow <= t) and (t <= buildingHigh) then - for vi=1,#variationSet do - local variation = variationSet[vi] - buildingSet[#buildingSet+1] = variation - end - tieredBuildingPickerSet[#tieredBuildingPickerSet+1] = { - distort(rg, - linearInterpolation((t - buildingLow) / (buildingHigh - buildingLow), - buildingAcceptRate[3], - buildingAcceptRate[4])), - variationSet, - building.type - } - end - end - end - end - - for t=1,10 do - local alignments = alignmentSet[t] - local totalAlignment = 0 - for i=1,#alignments do - totalAlignment = totalAlignment + alignments[i][1] - end - for i=1,#alignments do - alignments[i][1] = alignments[i][1] / totalAlignment - end - - for fi=1,#FACTION_SET do - local faction = FACTION_SET[fi] - local factionBuildingSet = buildingEvolveLookup[faction.type][t] - local totalBuildingSet = 0 - for i=1,#factionBuildingSet do - totalBuildingSet = totalBuildingSet + factionBuildingSet[i][1] - end - for i=1,#factionBuildingSet do - factionBuildingSet[i][1] = factionBuildingSet[i][1] / totalBuildingSet - end - end - end - +function baseUtils.rebuildNativeTables(universe) local evoIndex = evoToTier(universe, universe.evolutionLevel, 2) if universe.bases then for _,base in pairs(universe.bases) do for x=1,2 do local alignment = base.alignment[x] - if alignment and not universe.buildingEvolveLookup[alignment] then + if alignment and not BUILDING_EVOLVE_LOOKUP[alignment] then base.alignment = findBaseInitialAlignment(universe, evoIndex) break elseif not alignment and (x == 1) then diff --git a/libs/ChunkProcessor.lua b/libs/ChunkProcessor.lua index 65267b8..2703389 100644 --- a/libs/ChunkProcessor.lua +++ b/libs/ChunkProcessor.lua @@ -24,7 +24,7 @@ local chunkProcessor = {} local chunkUtils = require("ChunkUtils") local queryUtils = require("QueryUtils") local mapUtils = require("MapUtils") --- local constants = require("Constants") +local constants = require("Constants") -- constants @@ -32,6 +32,8 @@ local mapUtils = require("MapUtils") -- local HALF_CHUNK_SIZE = constants.HALF_CHUNK_SIZE -- local QUARTER_CHUNK_SIZE = constants.QUARTER_CHUNK_SIZE +local BUILDING_SPACE_LOOKUP = constants.BUILDING_SPACE_LOOKUP + -- imported functions local findInsertionPoint = mapUtils.findInsertionPoint @@ -152,7 +154,7 @@ function chunkProcessor.processPendingUpgrades(universe, tick) query.name = entityData.name unregisterEnemyBaseStructure(entityData.map, entity, nil, true) entity.destroy() - local foundPosition = surface.find_non_colliding_position(universe.buildingSpaceLookup[entityData.name], + local foundPosition = surface.find_non_colliding_position(BUILDING_SPACE_LOOKUP[entityData.name], position, 2, 1, diff --git a/libs/ChunkUtils.lua b/libs/ChunkUtils.lua index dafe061..bbca1b0 100644 --- a/libs/ChunkUtils.lua +++ b/libs/ChunkUtils.lua @@ -30,6 +30,8 @@ local queryUtils = require("QueryUtils") -- constants +local VANILLA_ENTITY_TYPE_LOOKUP = constants.VANILLA_ENTITY_TYPE_LOOKUP +local BUILDING_HIVE_TYPE_LOOKUP = constants.BUILDING_HIVE_TYPE_LOOKUP local HIVE_BUILDINGS_TYPES = constants.HIVE_BUILDINGS_TYPES local DEFINES_WIRE_TYPE_RED = defines.wire_type.red @@ -243,8 +245,6 @@ function chunkUtils.initialScan(chunk, map, tick) local resources = surface.count_entities_filtered(universe.isCountResourcesQuery) * RESOURCE_NORMALIZER setResourceGenerator(map, chunk, resources) - local vanillaEntityTypeLookup = universe.vanillaEntityTypeLookup - local buildingHiveTypeLookup = universe.buildingHiveTypeLookup local counts = map.chunkScanCounts for i=1,#HIVE_BUILDINGS_TYPES do counts[HIVE_BUILDINGS_TYPES[i]] = 0 @@ -269,8 +269,8 @@ function chunkUtils.initialScan(chunk, map, tick) local enemyBuilding = enemyBuildings[i] chunkUtils.registerEnemyBaseStructure(map, enemyBuilding, base) local entityName = enemyBuilding.name - local isVanilla = vanillaEntityTypeLookup[entityName] - if isVanilla or (not isVanilla and not buildingHiveTypeLookup[entityName]) then + local isVanilla = VANILLA_ENTITY_TYPE_LOOKUP[entityName] + if isVanilla or (not isVanilla and not BUILDING_HIVE_TYPE_LOOKUP[entityName]) then upgradeEntity(enemyBuilding, base, map, nil, true) end end @@ -484,9 +484,7 @@ function chunkUtils.registerEnemyBaseStructure(map, entity, base, skipCount) local entityType = entity.type local addFunc - local universe = map.universe - local hiveTypeLookup = universe.buildingHiveTypeLookup - local hiveType = hiveTypeLookup[entity.name] + local hiveType = BUILDING_HIVE_TYPE_LOOKUP[entity.name] if (hiveType == "spitter-spawner") or (hiveType == "biter-spawner") then addFunc = addNestCount elseif (hiveType == "turret") then @@ -529,8 +527,7 @@ function chunkUtils.unregisterEnemyBaseStructure(map, entity, damageTypeName, sk local entityType = entity.type local removeFunc - local hiveTypeLookup = map.universe.buildingHiveTypeLookup - local hiveType = hiveTypeLookup[entity.name] + local hiveType = BUILDING_HIVE_TYPE_LOOKUP[entity.name] if (hiveType == "spitter-spawner") or (hiveType == "biter-spawner") then removeFunc = removeNestCount elseif (hiveType == "turret") then diff --git a/libs/Constants.lua b/libs/Constants.lua index 32d656b..80d2dd3 100644 --- a/libs/Constants.lua +++ b/libs/Constants.lua @@ -19,6 +19,8 @@ if constantsG then end local constants = {} +local mathUtils = require("MathUtils") + -- versions constants.VERSION_5 = 5 @@ -1665,5 +1667,180 @@ constants.MAX_HIVE_TTL = 2485 constants.MIN_HIVE_TTL = 890 constants.DEV_HIVE_TTL = 150 +local rg = mathUtils.xorRandom(settings.startup["rampant--enemySeed"].value) + +local alignmentSet = {} +constants.EVOLUTION_TABLE_ALIGNMENT = alignmentSet -- evolutionTableAlignment +local buildingSpaceLookup = {} +constants.BUILDING_SPACE_LOOKUP = buildingSpaceLookup -- buildingSpaceLookup +local enemyAlignmentLookup = {} +constants.ENEMY_ALIGNMENT_LOOKUP = enemyAlignmentLookup -- enemyAlignmentLookup +local upgradeLookup = {} +constants.UPGRADE_LOOKUP = upgradeLookup --upgradeLookup +local buildingEvolveLookup = {} +constants.BUILDING_EVOLVE_LOOKUP = buildingEvolveLookup --buildingEvolveLookup +local costLookup = {} +constants.COST_LOOKUP = costLookup --costLookup +local buildingHiveTypeLookup = {} +constants.BUILDING_HIVE_TYPE_LOOKUP = buildingHiveTypeLookup --buildingHiveTypeLookup +local proxyEntityLookup = {} +constants.PROXY_ENTITY_LOOKUP = proxyEntityLookup --proxyEntityLookup +local vanillaEntityLookup = {} +constants.VANILLA_ENTITY_TYPE_LOOKUP = vanillaEntityLookup --vanillaEntityTypeLookup +local entitySkipCountLookup = {} +constants.ENTITY_SKIP_COUNT_LOOKUP = entitySkipCountLookup --entitySkipCountLookup + +buildingHiveTypeLookup["biter-spawner"] = "biter-spawner" +buildingHiveTypeLookup["spitter-spawner"] = "spitter-spawner" +buildingHiveTypeLookup["small-worm-turret"] = "turret" +buildingHiveTypeLookup["medium-worm-turret"] = "turret" +buildingHiveTypeLookup["big-worm-turret"] = "turret" +buildingHiveTypeLookup["behemoth-worm-turret"] = "turret" + +vanillaEntityLookup["biter-spawner"] = true +vanillaEntityLookup["spitter-spawner"] = true +vanillaEntityLookup["small-worm-turret"] = true +vanillaEntityLookup["medium-worm-turret"] = true +vanillaEntityLookup["big-worm-turret"] = true +vanillaEntityLookup["behemoth-worm-turret"] = true + +local function isMember(lst, x) + for _,l in pairs(lst) do + if l == x then + return true + end + end + return false +end + +for i=1,#constants.FACTION_SET do + local faction = constants.FACTION_SET[i] + + local factionUpgradeLookup = {} + upgradeLookup[faction.type] = factionUpgradeLookup + local factionBuildingPicker = {} + buildingEvolveLookup[faction.type] = factionBuildingPicker + + for t=1,10 do + local alignments = alignmentSet[t] + if not alignments then + alignments = {} + alignmentSet[t] = alignments + end + + --[[ + alignments table is a table that is used for selecting what factions are available + to pick given an evolution level. + + evolutionTable is a table that given a faction allows the selection of a building + type based on the propabilities given. Once the the building type is selected given + a faction, then the evolution decides what level of building to select + --]] + local factionAcceptRate = faction.acceptRate + + local low = factionAcceptRate[1] + local high = factionAcceptRate[2] + if (low <= t) and (t <= high) then + alignments[#alignments+1] = { + mathUtils.distort(rg, + mathUtils.linearInterpolation((t - low) / (high - low), factionAcceptRate[3], factionAcceptRate[4])), + faction.type + } + end + + local tieredUpgradeBuildingSet = factionUpgradeLookup[t] + if not tieredUpgradeBuildingSet then + tieredUpgradeBuildingSet = {} + factionUpgradeLookup[t] = tieredUpgradeBuildingSet + end + + local tieredBuildingPickerSet = factionBuildingPicker[t] + if not tieredBuildingPickerSet then + tieredBuildingPickerSet = {} + factionBuildingPicker[t] = tieredBuildingPickerSet + end + + for b=1,#faction.buildings do + local building = faction.buildings[b] + + local buildingSet = tieredUpgradeBuildingSet[building.type] + if not buildingSet then + buildingSet = {} + tieredUpgradeBuildingSet[building.type] = buildingSet + end + + local variationSet = {} + for v=1,settings.startup["rampant--newEnemyVariations"].value do + local entry = faction.type .. "-" .. building.name .. "-v" .. v .. "-t" .. t .. "-rampant" + enemyAlignmentLookup[entry] = faction.type + local proxyEntity = "entity-proxy-" .. building.type .. "-t" .. t .. "-rampant" + proxyEntityLookup[proxyEntity] = true + buildingSpaceLookup[entry] = proxyEntity + costLookup[entry] = constants.HIVE_BUILDINGS_COST[building.type] + buildingHiveTypeLookup[entry] = building.type + if not buildingHiveTypeLookup[proxyEntity] then + buildingHiveTypeLookup[proxyEntity] = building.type + end + variationSet[#variationSet+1] = entry + for _,unit in pairs(faction.units) do + if isMember(unit.attributes, "skipKillCount") then + local name = faction.type .. "-" .. unit.name .. "-v" .. v .. "-t" .. t .. "-rampant" + constants.ENTITY_SKIP_COUNT_LOOKUP[name] = true + end + end + end + + local buildingAcceptRate = building.acceptRate + + local buildingLow = buildingAcceptRate[1] + local buildingHigh = buildingAcceptRate[2] + if (buildingLow <= t) and (t <= buildingHigh) then + for vi=1,#variationSet do + local variation = variationSet[vi] + buildingSet[#buildingSet+1] = variation + end + tieredBuildingPickerSet[#tieredBuildingPickerSet+1] = { + mathUtils.distort(rg, + mathUtils.linearInterpolation((t - buildingLow) / (buildingHigh - buildingLow), + buildingAcceptRate[3], + buildingAcceptRate[4])), + variationSet, + building.type + } + end + end + end +end + +for t=1,10 do + local alignments = alignmentSet[t] + local totalAlignment = 0 + for i=1,#alignments do + totalAlignment = totalAlignment + alignments[i][1] + end + for i=1,#alignments do + alignments[i][1] = alignments[i][1] / totalAlignment + end + + for fi=1,#constants.FACTION_SET do + local faction = constants.FACTION_SET[fi] + local factionBuildingSet = buildingEvolveLookup[faction.type][t] + local totalBuildingSet = 0 + for i=1,#factionBuildingSet do + totalBuildingSet = totalBuildingSet + factionBuildingSet[i][1] + end + for i=1,#factionBuildingSet do + factionBuildingSet[i][1] = factionBuildingSet[i][1] / totalBuildingSet + end + end +end + +local evoToTierMapping = {} +constants.EVO_TO_TIER_MAPPING = evoToTierMapping --evoToTierMapping + +for i=1,10 do + evoToTierMapping[#evoToTierMapping+1] = (((i - 1) * 0.1) ^ 0.5) - 0.05 +end + constantsG = constants return constants diff --git a/libs/MathUtils.lua b/libs/MathUtils.lua index 8fe29f7..a63545b 100644 --- a/libs/MathUtils.lua +++ b/libs/MathUtils.lua @@ -21,11 +21,9 @@ local mathUtils = {} -- imports -local constants = require("Constants") - -- constants -local TICKS_A_MINUTE = constants.TICKS_A_MINUTE +local TICKS_A_MINUTE = 60 * 60 -- imported functions diff --git a/libs/SquadDefense.lua b/libs/SquadDefense.lua index 1062f16..132db29 100644 --- a/libs/SquadDefense.lua +++ b/libs/SquadDefense.lua @@ -26,7 +26,6 @@ local mapUtils = require("MapUtils") local unitGroupUtils = require("UnitGroupUtils") local movementUtils = require("MovementUtils") local chunkPropertyUtils = require("ChunkPropertyUtils") -local baseUtils = require("BaseUtils") -- constants diff --git a/tests.lua b/tests.lua index fc194b1..0a7e492 100644 --- a/tests.lua +++ b/tests.lua @@ -355,7 +355,6 @@ function tests.scanEnemy() local map = global.universe.maps[game.player.surface.index] local chunk = mapUtils.getChunkByPosition(map, game.player.character.position) local universe = map.universe - local buildingHiveTypeLookup = universe.buildingHiveTypeLookup local query = universe.filteredEntitiesEnemyStructureQuery queryUtils.setAreaInQuery(query, chunk, constants.CHUNK_SIZE) local buildings = map.surface.find_entities_filtered(query) @@ -365,7 +364,7 @@ function tests.scanEnemy() end for i=1,#buildings do local building = buildings[i] - local hiveType = buildingHiveTypeLookup[building.name] or + local hiveType = constants.BUILDING_HIVE_TYPE_LOOKUP[building.name] or (((building.type == "turret") and "turret") or "biter-spawner") counts[hiveType] = counts[hiveType] + 1 end