1
0
mirror of https://github.com/veden/Rampant.git synced 2025-03-17 20:58:35 +02:00

FACTO-249: Moved universe global into all library files

This commit is contained in:
Aaron Veden 2023-03-11 12:53:06 -08:00
parent 749f6cbb90
commit e189001880
No known key found for this signature in database
GPG Key ID: FF5990B1C6DD3F84
20 changed files with 1551 additions and 1479 deletions

View File

@ -52,6 +52,7 @@ Version: 3.2.0
- Fixed bases being able to mutate to the same factions
- Fixed players disconnecting on multiplayer could leave residual player pheromone that never dissipated
- Fixed /rampantSetAIState command could error if not provide correct parameters or didn't display a message if parameters were missing
- Fixed the pheromone processing could skip the inner most chunks or the outer most chunks of a map
Optimizations:
- Moved most constants out of global
- Removed new enemy variations setting

File diff suppressed because it is too large Load Diff

View File

@ -21,6 +21,10 @@ local aiAttackWave = {}
-- imports
local Universe
--
local constants = require("Constants")
local mapUtils = require("MapUtils")
local chunkPropertyUtils = require("ChunkPropertyUtils")
@ -80,29 +84,29 @@ local mCeil = math.ceil
-- module code
local function settlerWaveScaling(universe)
return mCeil(gaussianRandomRangeRG(universe.settlerWaveSize,
universe.settlerWaveDeviation,
universe.expansionMinSize,
universe.expansionMaxSize,
universe.random))
local function settlerWaveScaling()
return mCeil(gaussianRandomRangeRG(Universe.settlerWaveSize,
Universe.settlerWaveDeviation,
Universe.expansionMinSize,
Universe.expansionMaxSize,
Universe.random))
end
local function attackWaveScaling(universe)
return mCeil(gaussianRandomRangeRG(universe.attackWaveSize,
universe.attackWaveDeviation,
local function attackWaveScaling()
return mCeil(gaussianRandomRangeRG(Universe.attackWaveSize,
Universe.attackWaveDeviation,
1,
universe.attackWaveUpperBound,
universe.random))
Universe.attackWaveUpperBound,
Universe.random))
end
local function attackWaveValidCandidate(chunk, map, base)
local isValid = getNestActiveness(map, chunk)
local function attackWaveValidCandidate(chunk, base)
local isValid = getNestActiveness(chunk)
if (base.stateAI == BASE_AI_STATE_RAIDING) or
(base.stateAI == BASE_AI_STATE_SIEGE) or
(base.stateAI == BASE_AI_STATE_ONSLAUGHT)
then
isValid = isValid + getRaidNestActiveness(map, chunk)
isValid = isValid + getRaidNestActiveness(chunk)
end
return (isValid > 0)
end
@ -121,19 +125,19 @@ end
local function validSiegeSettlerLocation(map, neighborChunk)
return (getPassable(map, neighborChunk) == CHUNK_ALL_DIRECTIONS) and
(getNestCount(map, neighborChunk) == 0)
(getNestCount(neighborChunk) == 0)
end
local function validSettlerLocation(map, chunk, neighborChunk)
local chunkResource = chunk[RESOURCE_PHEROMONE]
return (getPassable(map, neighborChunk) == CHUNK_ALL_DIRECTIONS) and
(getNestCount(map, neighborChunk) == 0) and
(getNestCount(neighborChunk) == 0) and
(neighborChunk[RESOURCE_PHEROMONE] >= chunkResource)
end
local function validUnitGroupLocation(map, neighborChunk)
return getPassable(map, neighborChunk) == CHUNK_ALL_DIRECTIONS and
(getNestCount(map, neighborChunk) == 0)
(getNestCount(neighborChunk) == 0)
end
local function visitPattern(o, cX, cY, distance)
@ -176,17 +180,17 @@ local function visitPattern(o, cX, cY, distance)
end
function aiAttackWave.rallyUnits(chunk, map, tick, base)
if ((tick - getRallyTick(map, chunk) > COOLDOWN_RALLY) and (base.unitPoints >= AI_VENGENCE_SQUAD_COST)) then
if ((tick - getRallyTick(chunk) > COOLDOWN_RALLY) and (base.unitPoints >= AI_VENGENCE_SQUAD_COST)) then
setRallyTick(map, chunk, tick)
local cX = chunk.x
local cY = chunk.y
local startX, endX, stepX, startY, endY, stepY = visitPattern(tick % 4, cX, cY, RALLY_CRY_DISTANCE)
local vengenceQueue = map.universe.vengenceQueue
local vengenceQueue = Universe.vengenceQueue
for x=startX, endX, stepX do
for y=startY, endY, stepY do
if (x ~= cX) and (y ~= cY) then
local rallyChunk = getChunkByXY(map, x, y)
if (rallyChunk ~= -1) and (getNestCount(map, rallyChunk) > 0) then
if (rallyChunk ~= -1) and (getNestCount(rallyChunk) > 0) then
local pack = vengenceQueue[rallyChunk.id]
if not pack then
pack = {
@ -207,11 +211,10 @@ function aiAttackWave.rallyUnits(chunk, map, tick, base)
end
function aiAttackWave.formSettlers(map, chunk, base)
local universe = map.universe
if (universe.builderCount < universe.AI_MAX_BUILDER_COUNT)
if (Universe.builderCount < Universe.AI_MAX_BUILDER_COUNT)
and (base.sentExpansionGroups < base.maxExpansionGroups)
and ((base.unitPoints - AI_SETTLER_COST) > 0)
and (map.random() < universe.formSquadThreshold)
and (Universe.random() < Universe.formSquadThreshold)
then
local surface = map.surface
local squadPath, squadDirection
@ -239,23 +242,23 @@ function aiAttackWave.formSettlers(map, chunk, base)
if squadPosition then
local squad = createSquad(squadPosition, map, nil, true, base)
local scaledWaveSize = settlerWaveScaling(universe)
universe.formGroupCommand.group = squad.group
universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(universe.formCommand)
local scaledWaveSize = settlerWaveScaling()
Universe.formGroupCommand.group = squad.group
Universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(Universe.formCommand)
if (foundUnits > 0) then
base.sentExpansionGroups = base.sentExpansionGroups + 1
squad.base = base
local kamikazeThreshold = calculateKamikazeSettlerThreshold(foundUnits, universe)
local kamikazeThreshold = calculateKamikazeSettlerThreshold(foundUnits)
if base.stateAI == BASE_AI_STATE_SIEGE then
kamikazeThreshold = kamikazeThreshold * 2.5
end
squad.kamikaze = map.random() < kamikazeThreshold
squad.kamikaze = Universe.random() < kamikazeThreshold
universe.builderCount = universe.builderCount + 1
Universe.builderCount = Universe.builderCount + 1
modifyBaseUnitPoints(base, -AI_SETTLER_COST, "Settler", squadPosition.x, squadPosition.y)
universe.groupNumberToSquad[squad.groupNumber] = squad
Universe.groupNumberToSquad[squad.groupNumber] = squad
else
if (squad.group.valid) then
squad.group.destroy()
@ -267,10 +270,9 @@ function aiAttackWave.formSettlers(map, chunk, base)
end
function aiAttackWave.formVengenceSquad(map, chunk, base)
local universe = map.universe
if (universe.squadCount < universe.AI_MAX_SQUAD_COUNT)
if (Universe.squadCount < Universe.AI_MAX_SQUAD_COUNT)
and ((base.unitPoints - AI_VENGENCE_SQUAD_COST) > 0)
and (map.random() < universe.formSquadThreshold)
and (Universe.random() < Universe.formSquadThreshold)
then
if (chunk[BASE_PHEROMONE] < 0.0001) or (chunk[PLAYER_PHEROMONE] < 0.0001) then
return
@ -292,17 +294,17 @@ function aiAttackWave.formVengenceSquad(map, chunk, base)
if squadPosition then
local squad = createSquad(squadPosition, map, nil, false, base)
squad.rabid = map.random() < 0.03
squad.rabid = Universe.random() < 0.03
local scaledWaveSize = attackWaveScaling(universe)
universe.formGroupCommand.group = squad.group
universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(universe.formCommand)
local scaledWaveSize = attackWaveScaling()
Universe.formGroupCommand.group = squad.group
Universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(Universe.formCommand)
if (foundUnits > 0) then
squad.base = base
squad.kamikaze = map.random() < calculateKamikazeSquadThreshold(foundUnits, universe)
universe.groupNumberToSquad[squad.groupNumber] = squad
universe.squadCount = universe.squadCount + 1
squad.kamikaze = Universe.random() < calculateKamikazeSquadThreshold(foundUnits)
Universe.groupNumberToSquad[squad.groupNumber] = squad
Universe.squadCount = Universe.squadCount + 1
modifyBaseUnitPoints(base, -AI_VENGENCE_SQUAD_COST, "Vengence", squadPosition.x, squadPosition.y)
else
if (squad.group.valid) then
@ -315,11 +317,10 @@ function aiAttackWave.formVengenceSquad(map, chunk, base)
end
function aiAttackWave.formVengenceSettler(map, chunk, base)
local universe = map.universe
if (universe.builderCount < universe.AI_MAX_BUILDER_COUNT)
if (Universe.builderCount < Universe.AI_MAX_BUILDER_COUNT)
and (base.sentExpansionGroups < base.maxExpansionGroups)
and ((base.unitPoints - AI_VENGENCE_SQUAD_COST) > 0)
and (map.random() < universe.formSquadThreshold)
and (Universe.random() < Universe.formSquadThreshold)
then
local surface = map.surface
local squadPath, squadDirection = scoreNeighborsForFormation(getNeighborChunks(map, chunk.x, chunk.y),
@ -337,19 +338,19 @@ function aiAttackWave.formVengenceSettler(map, chunk, base)
if squadPosition then
local squad = createSquad(squadPosition, map, nil, true, base)
squad.rabid = map.random() < 0.03
squad.rabid = Universe.random() < 0.03
local scaledWaveSize = settlerWaveScaling(universe)
universe.formGroupCommand.group = squad.group
universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(universe.formCommand)
local scaledWaveSize = settlerWaveScaling()
Universe.formGroupCommand.group = squad.group
Universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(Universe.formCommand)
if (foundUnits > 0) then
base.sentExpansionGroups = base.sentExpansionGroups + 1
squad.base = base
squad.kamikaze = map.random() < calculateKamikazeSettlerThreshold(foundUnits, universe)
universe.groupNumberToSquad[squad.groupNumber] = squad
universe.builderCount = universe.builderCount + 1
squad.kamikaze = Universe.random() < calculateKamikazeSettlerThreshold(foundUnits)
Universe.groupNumberToSquad[squad.groupNumber] = squad
Universe.builderCount = Universe.builderCount + 1
modifyBaseUnitPoints(base, -AI_VENGENCE_SQUAD_COST, "Vengence Settlers", squadPosition.x, squadPosition.y)
else
if (squad.group.valid) then
@ -362,11 +363,10 @@ function aiAttackWave.formVengenceSettler(map, chunk, base)
end
function aiAttackWave.formSquads(map, chunk, base)
local universe = map.universe
if (universe.squadCount < universe.AI_MAX_SQUAD_COUNT)
and attackWaveValidCandidate(chunk, map, base)
if (Universe.squadCount < Universe.AI_MAX_SQUAD_COUNT)
and attackWaveValidCandidate(chunk, base)
and ((base.unitPoints - AI_SQUAD_COST) > 0)
and (map.random() < universe.formSquadThreshold)
and (Universe.random() < Universe.formSquadThreshold)
then
if (chunk[BASE_PHEROMONE] < 0.0001) or (chunk[PLAYER_PHEROMONE] < 0.0001) then
return
@ -388,17 +388,17 @@ function aiAttackWave.formSquads(map, chunk, base)
if squadPosition then
local squad = createSquad(squadPosition, map, nil, false, base)
squad.rabid = map.random() < 0.03
squad.rabid = Universe.random() < 0.03
local scaledWaveSize = attackWaveScaling(universe)
universe.formGroupCommand.group = squad.group
universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(universe.formCommand)
local scaledWaveSize = attackWaveScaling()
Universe.formGroupCommand.group = squad.group
Universe.formCommand.unit_count = scaledWaveSize
local foundUnits = surface.set_multi_command(Universe.formCommand)
if (foundUnits > 0) then
squad.base = base
squad.kamikaze = map.random() < calculateKamikazeSquadThreshold(foundUnits, universe)
universe.squadCount = universe.squadCount + 1
universe.groupNumberToSquad[squad.groupNumber] = squad
squad.kamikaze = Universe.random() < calculateKamikazeSquadThreshold(foundUnits)
Universe.squadCount = Universe.squadCount + 1
Universe.groupNumberToSquad[squad.groupNumber] = squad
if (base.stateAI == BASE_AI_STATE_AGGRESSIVE) then
base.sentAggressiveGroups = base.sentAggressiveGroups + 1
end
@ -413,6 +413,9 @@ function aiAttackWave.formSquads(map, chunk, base)
end
end
function aiAttackWave.init(universe)
Universe = universe
end
aiAttackWaveG = aiAttackWave
return aiAttackWave

View File

@ -19,6 +19,10 @@ if aiPlanningG then
end
local aiPlanning = {}
--
local universe
-- imports
local constants = require("Constants")
@ -100,7 +104,7 @@ local function getTimeStringFromTick(tick)
return days .. "d " .. hours .. "h " .. minutes .. "m " .. seconds .. "s"
end
function aiPlanning.planning(universe, evolutionLevel)
function aiPlanning.planning(evolutionLevel)
universe.evolutionLevel = evolutionLevel
local maxPoints = mMax(AI_MAX_POINTS * evolutionLevel, MINIMUM_AI_POINTS)
universe.maxPoints = maxPoints
@ -140,7 +144,7 @@ function aiPlanning.planning(universe, evolutionLevel)
universe.kamikazeThreshold = NO_RETREAT_BASE_PERCENT + (evolutionLevel * NO_RETREAT_EVOLUTION_BONUS_MAX)
end
local function processBase(universe, base, tick)
local function processBase(base, tick)
base.maxAggressiveGroups = mCeil(base.activeNests / ACTIVE_NESTS_PER_AGGRESSIVE_GROUPS)
base.maxExpansionGroups = mCeil((base.activeNests + base.activeRaidNests) / ALL_NESTS_PER_EXPANSION_GROUPS)
@ -204,7 +208,7 @@ local function processBase(universe, base, tick)
deathThreshold = universe.adaptationModifier * deathThreshold
if ((base.deathEvents > deathThreshold) and (universe.random() > 0.95)) then
if (base.mutations < universe.MAX_BASE_MUTATIONS) then
if upgradeBaseBasedOnDamage(universe, base) then
if upgradeBaseBasedOnDamage(base) then
base.mutations = base.mutations + 1
end
elseif (base.mutations == universe.MAX_BASE_MUTATIONS) then
@ -345,8 +349,6 @@ local function temperamentPlanner(base, evolutionLevel)
delta = delta + val
end
local universe = base.universe
delta = delta * universe.temperamentRateModifier
base.temperamentScore = mMin(TEMPERAMENT_RANGE_MAX, mMax(TEMPERAMENT_RANGE_MIN, currentTemperament + delta))
base.temperament = ((base.temperamentScore + TEMPERAMENT_RANGE_MAX) * TEMPERAMENT_DIVIDER)
@ -370,7 +372,7 @@ local function temperamentPlanner(base, evolutionLevel)
end
end
local function processState(universe, base, tick)
local function processState(base, tick)
if (base.stateAITick > tick) or not universe.awake then
if (not universe.awake) and (tick >= universe.initialPeaceTime) then
@ -562,7 +564,7 @@ local function processState(universe, base, tick)
end
function aiPlanning.processBaseAIs(universe, tick)
function aiPlanning.processBaseAIs(tick)
local baseId = universe.processBaseAIIterator
local base
if not baseId then
@ -579,10 +581,14 @@ function aiPlanning.processBaseAIs(universe, tick)
return
end
temperamentPlanner(base, universe.evolutionLevel)
processState(universe, base, tick)
processBase(universe, base, tick)
processState(base, tick)
processBase(base, tick)
end
end
function aiPlanning.init(universeGlobal)
universe = universeGlobal
end
aiPlanningG = aiPlanning
return aiPlanning

View File

@ -19,6 +19,10 @@ if (aiPredicatesG) then
end
local aiPredicates = {}
--
local universe
-- imports
local constants = require("Constants")
@ -36,7 +40,6 @@ local BASE_AI_STATE_ONSLAUGHT = constants.BASE_AI_STATE_ONSLAUGHT
-- module code
function aiPredicates.canAttack(map, base)
local universe = map.universe
local isAggressive = ((base.stateAI == BASE_AI_STATE_AGGRESSIVE)
and (base.sentAggressiveGroups < base.maxAggressiveGroups))
local isRaiding = (base.stateAI == BASE_AI_STATE_RAIDING)
@ -65,7 +68,6 @@ function aiPredicates.canMigrate(map, base)
if badAIState then
return false
end
local universe = map.universe
if not universe.expansion then
return false
end
@ -81,5 +83,9 @@ function aiPredicates.canMigrate(map, base)
return true
end
function aiPredicates.init(universeGlobal)
universe = universeGlobal
end
aiPredicatesG = aiPredicates
return aiPredicates

View File

@ -14,10 +14,14 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if baseUtilsG then
return baseUtilsG
if BaseUtilsG then
return BaseUtilsG
end
local baseUtils = {}
local BaseUtils = {}
--
local Universe
-- imports
@ -83,13 +87,13 @@ local next = next
-- module code
local function evoToTier(universe, evolutionFactor, maxSkips)
local function evoToTier(evolutionFactor, maxSkips)
local v
local skipsRemaining = maxSkips
for i=TIERS,1,-1 do
if EVO_TO_TIER_MAPPING[i] <= evolutionFactor then
v = i
if (skipsRemaining == 0) or (universe.random() <= 0.75) then
if (skipsRemaining == 0) or (Universe.random() <= 0.75) then
break
end
skipsRemaining = skipsRemaining - 1
@ -98,8 +102,8 @@ local function evoToTier(universe, evolutionFactor, maxSkips)
return v
end
local function findBaseMutation(universe, targetEvolution, excludeFactions)
local tier = evoToTier(universe, targetEvolution or universe.evolutionLevel, 2)
local function findBaseMutation(targetEvolution, excludeFactions)
local tier = evoToTier(targetEvolution or Universe.evolutionLevel, 2)
local availableAlignments
local alignments = EVOLUTION_TABLE_ALIGNMENT[tier]
@ -114,7 +118,7 @@ local function findBaseMutation(universe, targetEvolution, excludeFactions)
availableAlignments = alignments
end
local roll = universe.random()
local roll = Universe.random()
for i=1,#availableAlignments do
local alignment = availableAlignments[i]
@ -132,7 +136,7 @@ local function initialEntityUpgrade(baseAlignment, tier, maxTier, map, useHiveTy
local useTier
local tierRoll = map.random()
local tierRoll = Universe.random()
if (tierRoll < 0.4) then
useTier = maxTier
elseif (tierRoll < 0.7) then
@ -154,7 +158,7 @@ local function initialEntityUpgrade(baseAlignment, tier, maxTier, map, useHiveTy
for ui=1,#upgrades do
local upgrade = upgrades[ui]
if upgrade[3] == useHiveType then
entity = upgrade[2][map.random(#upgrade[2])]
entity = upgrade[2][Universe.random(#upgrade[2])]
break
end
end
@ -163,7 +167,7 @@ local function initialEntityUpgrade(baseAlignment, tier, maxTier, map, useHiveTy
for ui=1,#upgrades do
local upgrade = upgrades[ui]
if upgrade[3] == entityType then
entity = upgrade[2][map.random(#upgrade[2])]
entity = upgrade[2][Universe.random(#upgrade[2])]
end
end
if not entity then
@ -173,7 +177,7 @@ local function initialEntityUpgrade(baseAlignment, tier, maxTier, map, useHiveTy
for ui=1,#upgrades do
local upgrade = upgrades[ui]
if upgrade[3] == mappedType then
return upgrade[2][map.random(#upgrade[2])]
return upgrade[2][Universe.random(#upgrade[2])]
end
end
end
@ -197,15 +201,15 @@ local function entityUpgrade(baseAlignment, tier, maxTier, originalEntity, map)
for i=1, #mapTypes do
local upgrade = factionLookup[mapTypes[i]]
if upgrade and (#upgrade > 0) then
entity = upgrade[map.random(#upgrade)]
if map.random() < 0.55 then
entity = upgrade[Universe.random(#upgrade)]
if Universe.random() < 0.55 then
return entity
end
end
end
elseif (#upgrades > 0) then
entity = upgrades[map.random(#upgrades)]
if map.random() < 0.55 then
entity = upgrades[Universe.random(#upgrades)]
if Universe.random() < 0.55 then
return entity
end
end
@ -213,15 +217,14 @@ local function entityUpgrade(baseAlignment, tier, maxTier, originalEntity, map)
return entity
end
function baseUtils.findEntityUpgrade(baseAlignment, currentEvo, evoIndex, originalEntity, map, evolve)
local universe = map.universe
function BaseUtils.findEntityUpgrade(baseAlignment, currentEvo, evoIndex, originalEntity, map, evolve)
local adjCurrentEvo = mMax(
((baseAlignment ~= ENEMY_ALIGNMENT_LOOKUP[originalEntity.name]) and 0) or currentEvo,
0
)
local tier = evoToTier(universe, adjCurrentEvo, 5)
local maxTier = evoToTier(universe, evoIndex, 4)
local tier = evoToTier(adjCurrentEvo, 5)
local maxTier = evoToTier(evoIndex, 4)
if (tier > maxTier) then
maxTier = tier
@ -232,13 +235,13 @@ function baseUtils.findEntityUpgrade(baseAlignment, currentEvo, evoIndex, origin
local entityName = originalEntity.name
local entityType = BUILDING_HIVE_TYPE_LOOKUP[entityName]
if not entityType then
if map.random() < 0.5 then
if Universe.random() < 0.5 then
entityType = "biter-spawner"
else
entityType = "spitter-spawner"
end
end
local roll = map.random()
local roll = Universe.random()
local makeHive = (chunk ~= -1) and
(
(entityType == "biter-spawner") or (entityType == "spitter-spawner")
@ -262,23 +265,23 @@ function baseUtils.findEntityUpgrade(baseAlignment, currentEvo, evoIndex, origin
end
-- local
function baseUtils.findBaseInitialAlignment(universe, evoIndex, excludeFactions)
function BaseUtils.findBaseInitialAlignment(evoIndex, excludeFactions)
local dev = evoIndex * 0.15
local evoTop = gaussianRandomRangeRG(evoIndex - (evoIndex * 0.075), dev, 0, evoIndex, universe.random)
local evoTop = gaussianRandomRangeRG(evoIndex - (evoIndex * 0.075), dev, 0, evoIndex, Universe.random)
local result
if universe.random() < 0.05 then
result = {findBaseMutation(universe, evoTop, excludeFactions), findBaseMutation(universe, evoTop, excludeFactions)}
if Universe.random() < 0.05 then
result = {findBaseMutation(evoTop, excludeFactions), findBaseMutation(evoTop, excludeFactions)}
else
result = {findBaseMutation(universe, evoTop, excludeFactions)}
result = {findBaseMutation(evoTop, excludeFactions)}
end
return result
end
function baseUtils.recycleBases(universe)
local bases = universe.bases
local id = universe.recycleBaseIterator
function BaseUtils.recycleBases()
local bases = Universe.bases
local id = Universe.recycleBaseIterator
local base
if not id then
id, base = next(bases, nil)
@ -286,24 +289,24 @@ function baseUtils.recycleBases(universe)
base = bases[id]
end
if not id then
universe.recycleBaseIterator = nil
Universe.recycleBaseIterator = nil
else
universe.recycleBaseIterator = next(bases, id)
Universe.recycleBaseIterator = next(bases, id)
local map = base.map
if (base.chunkCount == 0) or not map.surface.valid then
bases[id] = nil
if universe.processBaseAIIterator == id then
universe.processBaseAIIterator = nil
if Universe.processBaseAIIterator == id then
Universe.processBaseAIIterator = nil
end
if map.surface.valid then
map.universe.bases[id] = nil
Universe.bases[id] = nil
end
end
end
end
function baseUtils.queueUpgrade(entity, base, disPos, evolve, register, timeDelay)
base.universe.pendingUpgrades[entity.unit_number] = {
function BaseUtils.queueUpgrade(entity, base, disPos, evolve, register, timeDelay)
Universe.pendingUpgrades[entity.unit_number] = {
["position"] = disPos,
["register"] = register,
["evolve"] = evolve,
@ -313,7 +316,7 @@ function baseUtils.queueUpgrade(entity, base, disPos, evolve, register, timeDela
}
end
local function pickMutationFromDamageType(universe, damageType, roll, base)
local function pickMutationFromDamageType(damageType, roll, base)
local baseAlignment = base.alignment
local damageFactions = FACTIONS_BY_DAMAGE_TYPE[damageType]
@ -321,7 +324,7 @@ local function pickMutationFromDamageType(universe, damageType, roll, base)
local mutated = false
if damageFactions and (#damageFactions > 0) then
mutation = damageFactions[universe.random(#damageFactions)]
mutation = damageFactions[Universe.random(#damageFactions)]
if not isMember(mutation, base.alignmentHistory) then
if baseAlignment[2] then
if (baseAlignment[1] ~= mutation) and (baseAlignment[2] ~= mutation) then
@ -349,7 +352,7 @@ local function pickMutationFromDamageType(universe, damageType, roll, base)
end
end
else
mutation = findBaseMutation(universe)
mutation = findBaseMutation()
if not isMember(mutation, base.alignmentHistory) then
if baseAlignment[2] then
if (baseAlignment[1] ~= mutation) and (baseAlignment[2] ~= mutation) then
@ -377,7 +380,7 @@ local function pickMutationFromDamageType(universe, damageType, roll, base)
end
end
end
if mutated and universe.printBaseAdaptation then
if mutated and Universe.printBaseAdaptation then
if baseAlignment[2] then
game.print({"description.rampant--adaptation2DebugMessage",
base.id,
@ -388,7 +391,7 @@ local function pickMutationFromDamageType(universe, damageType, roll, base)
base.x,
base.y,
base.mutations,
universe.MAX_BASE_MUTATIONS})
Universe.MAX_BASE_MUTATIONS})
else
game.print({"description.rampant--adaptation1DebugMessage",
base.id,
@ -398,18 +401,18 @@ local function pickMutationFromDamageType(universe, damageType, roll, base)
base.x,
base.y,
base.mutations,
universe.MAX_BASE_MUTATIONS})
Universe.MAX_BASE_MUTATIONS})
end
end
local alignmentCount = table_size(base.alignmentHistory)
while (alignmentCount > universe.MAX_BASE_ALIGNMENT_HISTORY) do
while (alignmentCount > Universe.MAX_BASE_ALIGNMENT_HISTORY) do
tableRemove(base.alignmentHistory, 1)
alignmentCount = alignmentCount - 1
end
return mutated
end
function baseUtils.upgradeBaseBasedOnDamage(universe, base)
function BaseUtils.upgradeBaseBasedOnDamage(base)
local total = 0
@ -420,7 +423,7 @@ function baseUtils.upgradeBaseBasedOnDamage(universe, base)
base.damagedBy["RandomMutation"] = mutationAmount
total = total + mutationAmount
local pickedDamage
local roll = universe.random()
local roll = Universe.random()
for damageTypeName,amount in pairs(base.damagedBy) do
base.damagedBy[damageTypeName] = amount / total
end
@ -432,68 +435,66 @@ function baseUtils.upgradeBaseBasedOnDamage(universe, base)
end
end
return pickMutationFromDamageType(universe, pickedDamage, roll, base)
return pickMutationFromDamageType(pickedDamage, roll, base)
end
function baseUtils.processBaseMutation(chunk, map, base)
function BaseUtils.processBaseMutation(chunk, map, base)
if not base.alignment[1] or
(base.stateGeneration ~= BASE_GENERATION_STATE_ACTIVE) or
(map.random() >= 0.30)
(Universe.random() >= 0.30)
then
return
end
if (base.points >= MINIMUM_BUILDING_COST) then
local surface = map.surface
local universe = map.universe
setPositionXYInQuery(universe.pbFilteredEntitiesPointQueryLimited,
chunk.x + (CHUNK_SIZE * map.random()),
chunk.y + (CHUNK_SIZE * map.random()))
setPositionXYInQuery(Universe.pbFilteredEntitiesPointQueryLimited,
chunk.x + (CHUNK_SIZE * Universe.random()),
chunk.y + (CHUNK_SIZE * Universe.random()))
local entities = surface.find_entities_filtered(universe.pbFilteredEntitiesPointQueryLimited)
local entities = surface.find_entities_filtered(Universe.pbFilteredEntitiesPointQueryLimited)
if #entities ~= 0 then
local entity = entities[1]
local cost = (COST_LOOKUP[entity.name] or MAGIC_MAXIMUM_NUMBER)
if (base.points >= cost) then
local position = entity.position
baseUtils.modifyBaseSpecialPoints(base, -cost, "Scheduling Entity upgrade", position.x, position.y)
baseUtils.queueUpgrade(entity, base, nil, false, true)
BaseUtils.modifyBaseSpecialPoints(base, -cost, "Scheduling Entity upgrade", position.x, position.y)
BaseUtils.queueUpgrade(entity, base, nil, false, true)
end
end
end
end
function baseUtils.createBase(map, chunk, tick)
function BaseUtils.createBase(map, chunk, tick)
local x = chunk.x
local y = chunk.y
local distance = euclideanDistancePoints(x, y, 0, 0)
local meanLevel = mFloor(distance * 0.005)
local universe = map.universe
local distanceIndex = mMin(1, distance * BASE_DISTANCE_TO_EVO_INDEX)
local evoIndex = mMax(distanceIndex, universe.evolutionLevel)
local evoIndex = mMax(distanceIndex, Universe.evolutionLevel)
local alignment =
(universe.NEW_ENEMIES and baseUtils.findBaseInitialAlignment(universe, evoIndex))
(Universe.NEW_ENEMIES and BaseUtils.findBaseInitialAlignment(evoIndex))
or {"neutral"}
local baseLevel = gaussianRandomRangeRG(meanLevel,
meanLevel * 0.3,
meanLevel * 0.50,
meanLevel * 1.50,
universe.random)
Universe.random)
local baseDistanceThreshold = gaussianRandomRangeRG(BASE_DISTANCE_THRESHOLD,
BASE_DISTANCE_THRESHOLD * 0.2,
BASE_DISTANCE_THRESHOLD * 0.75,
BASE_DISTANCE_THRESHOLD * 1.50,
universe.random)
Universe.random)
local distanceThreshold = (baseLevel * BASE_DISTANCE_LEVEL_BONUS) + baseDistanceThreshold
local base = {
x = x,
y = y,
distanceThreshold = distanceThreshold * universe.baseDistanceModifier,
distanceThreshold = distanceThreshold * Universe.baseDistanceModifier,
tick = tick,
alignment = alignment,
alignmentHistory = {},
@ -526,19 +527,18 @@ function baseUtils.createBase(map, chunk, tick)
resourceChunkCount = 0,
temperament = 0.5,
temperamentScore = 0,
universe = universe,
map = map,
id = universe.baseId
id = Universe.baseId
}
universe.baseId = universe.baseId + 1
Universe.baseId = Universe.baseId + 1
map.bases[base.id] = base
universe.bases[base.id] = base
Universe.bases[base.id] = base
return base
end
function baseUtils.modifyBaseUnitPoints(base, points, tag, x, y)
function BaseUtils.modifyBaseUnitPoints(base, points, tag, x, y)
if points > 0 and base.stateAI == BASE_AI_STATE_PEACEFUL then
return
@ -550,17 +550,16 @@ function baseUtils.modifyBaseUnitPoints(base, points, tag, x, y)
base.unitPoints = base.unitPoints + points
local universe = base.universe
local overflowMessage = ""
if base.unitPoints > universe.maxOverflowPoints then
base.unitPoints = universe.maxOverflowPoints
if base.unitPoints > Universe.maxOverflowPoints then
base.unitPoints = Universe.maxOverflowPoints
overflowMessage = " [Point cap reached]"
end
local printPointChange = ""
if points > 0 and universe.aiPointsPrintGainsToChat then
if points > 0 and Universe.aiPointsPrintGainsToChat then
printPointChange = "+" .. string.format("%.2f", points)
elseif points < 0 and universe.aiPointsPrintSpendingToChat then
elseif points < 0 and Universe.aiPointsPrintSpendingToChat then
printPointChange = string.format("%.2f", points)
end
@ -573,7 +572,7 @@ function baseUtils.modifyBaseUnitPoints(base, points, tag, x, y)
end
end
function baseUtils.modifyBaseSpecialPoints(base, points, tag, x, y)
function BaseUtils.modifyBaseSpecialPoints(base, points, tag, x, y)
if points > 0 and base.stateAI == BASE_AI_STATE_PEACEFUL then
return
@ -585,17 +584,16 @@ function baseUtils.modifyBaseSpecialPoints(base, points, tag, x, y)
base.points = base.points + points
local universe = base.universe
local overflowMessage = ""
if base.points > universe.maxOverflowPoints then
base.points = universe.maxOverflowPoints
if base.points > Universe.maxOverflowPoints then
base.points = Universe.maxOverflowPoints
overflowMessage = " [Point cap reached]"
end
local printPointChange = ""
if points > 0 and universe.aiPointsPrintGainsToChat then
if points > 0 and Universe.aiPointsPrintGainsToChat then
printPointChange = "+" .. string.format("%.2f", points)
elseif points < 0 and universe.aiPointsPrintSpendingToChat then
elseif points < 0 and Universe.aiPointsPrintSpendingToChat then
printPointChange = string.format("%.2f", points)
end
@ -608,5 +606,9 @@ function baseUtils.modifyBaseSpecialPoints(base, points, tag, x, y)
end
end
baseUtilsG = baseUtils
return baseUtils
function BaseUtils.init(universe)
Universe = universe
end
BaseUtilsG = BaseUtils
return BaseUtils

View File

@ -14,41 +14,45 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if (chunkProcessorG) then
return chunkProcessorG
if (ChunkProcessorG) then
return ChunkProcessorG
end
local chunkProcessor = {}
local ChunkProcessor = {}
--
local Universe
-- imports
local chunkUtils = require("ChunkUtils")
local queryUtils = require("QueryUtils")
local mapUtils = require("MapUtils")
local mathUtils = require("MathUtils")
local constants = require("Constants")
local baseUtils = require("BaseUtils")
local ChunkUtils = require("ChunkUtils")
local QueryUtils = require("QueryUtils")
local MapUtils = require("MapUtils")
local MathUtils = require("MathUtils")
local Constants = require("Constants")
local BaseUtils = require("BaseUtils")
-- constants
-- Constants
local PROXY_ENTITY_LOOKUP = constants.PROXY_ENTITY_LOOKUP
local BASE_DISTANCE_TO_EVO_INDEX = constants.BASE_DISTANCE_TO_EVO_INDEX
local PROXY_ENTITY_LOOKUP = Constants.PROXY_ENTITY_LOOKUP
local BASE_DISTANCE_TO_EVO_INDEX = Constants.BASE_DISTANCE_TO_EVO_INDEX
local BUILDING_SPACE_LOOKUP = constants.BUILDING_SPACE_LOOKUP
local BUILDING_SPACE_LOOKUP = Constants.BUILDING_SPACE_LOOKUP
-- imported functions
local findInsertionPoint = mapUtils.findInsertionPoint
local removeChunkFromMap = mapUtils.removeChunkFromMap
local setPositionInQuery = queryUtils.setPositionInQuery
local registerEnemyBaseStructure = chunkUtils.registerEnemyBaseStructure
local unregisterEnemyBaseStructure = chunkUtils.unregisterEnemyBaseStructure
local euclideanDistancePoints = mathUtils.euclideanDistancePoints
local findInsertionPoint = MapUtils.findInsertionPoint
local removeChunkFromMap = MapUtils.removeChunkFromMap
local setPositionInQuery = QueryUtils.setPositionInQuery
local registerEnemyBaseStructure = ChunkUtils.registerEnemyBaseStructure
local unregisterEnemyBaseStructure = ChunkUtils.unregisterEnemyBaseStructure
local euclideanDistancePoints = MathUtils.euclideanDistancePoints
local findEntityUpgrade = baseUtils.findEntityUpgrade
local findEntityUpgrade = BaseUtils.findEntityUpgrade
local createChunk = chunkUtils.createChunk
local initialScan = chunkUtils.initialScan
local chunkPassScan = chunkUtils.chunkPassScan
local createChunk = ChunkUtils.createChunk
local initialScan = ChunkUtils.initialScan
local chunkPassScan = ChunkUtils.chunkPassScan
local mMin = math.min
local mMax = math.max
@ -59,9 +63,9 @@ local tInsert = table.insert
-- module code
function chunkProcessor.processPendingChunks(universe, tick, flush)
local pendingChunks = universe.pendingChunks
local eventId = universe.chunkProcessorIterator
function ChunkProcessor.processPendingChunks(tick, flush)
local pendingChunks = Universe.pendingChunks
local eventId = Universe.chunkProcessorIterator
local event
if not eventId then
@ -76,22 +80,22 @@ function chunkProcessor.processPendingChunks(universe, tick, flush)
end
for _=1,endCount do
if not eventId then
universe.chunkProcessorIterator = nil
Universe.chunkProcessorIterator = nil
if (table_size(pendingChunks) == 0) then
-- this is needed as the next command remembers the max length a table has been
universe.pendingChunks = {}
Universe.pendingChunks = {}
end
break
else
if not flush and (event.tick > tick) then
universe.chunkProcessorIterator = eventId
Universe.chunkProcessorIterator = eventId
return
end
local newEventId, newEvent = next(pendingChunks, eventId)
pendingChunks[eventId] = nil
local map = event.map
if not map.surface.valid then
universe.chunkProcessorIterator = newEventId
Universe.chunkProcessorIterator = newEventId
return
end
@ -112,7 +116,7 @@ function chunkProcessor.processPendingChunks(universe, tick, flush)
else
local initialChunk = createChunk(map, x, y)
map[x][y] = initialChunk
universe.chunkIdToChunk[initialChunk.id] = initialChunk
Universe.chunkIdToChunk[initialChunk.id] = initialChunk
local chunk = initialScan(initialChunk, map, tick)
if (chunk ~= -1) then
tInsert(
@ -122,7 +126,7 @@ function chunkProcessor.processPendingChunks(universe, tick, flush)
)
else
map[x][y] = nil
universe.chunkIdToChunk[initialChunk.id] = nil
Universe.chunkIdToChunk[initialChunk.id] = nil
end
end
@ -130,30 +134,30 @@ function chunkProcessor.processPendingChunks(universe, tick, flush)
event = newEvent
end
end
universe.chunkProcessorIterator = eventId
Universe.chunkProcessorIterator = eventId
end
function chunkProcessor.processPendingUpgrades(universe, tick)
local entityId = universe.pendingUpgradeIterator
function ChunkProcessor.processPendingUpgrades(tick)
local entityId = Universe.pendingUpgradeIterator
local entityData
if not entityId then
entityId, entityData = next(universe.pendingUpgrades, nil)
entityId, entityData = next(Universe.pendingUpgrades, nil)
else
entityData = universe.pendingUpgrades[entityId]
entityData = Universe.pendingUpgrades[entityId]
end
if not entityId then
universe.pendingUpgradeIterator = nil
if table_size(universe.pendingUpgrades) == 0 then
universe.pendingUpgrades = {}
Universe.pendingUpgradeIterator = nil
if table_size(Universe.pendingUpgrades) == 0 then
Universe.pendingUpgrades = {}
end
else
local entity = entityData.entity
if entity.valid then
universe.pendingUpgradeIterator = next(universe.pendingUpgrades, entityId)
Universe.pendingUpgradeIterator = next(Universe.pendingUpgrades, entityId)
if entityData.delayTLL and tick < entityData.delayTLL then
return
end
universe.pendingUpgrades[entityId] = nil
Universe.pendingUpgrades[entityId] = nil
local base = entityData.base
local map = base.map
local baseAlignment = base.alignment
@ -173,7 +177,7 @@ function chunkProcessor.processPendingUpgrades(universe, tick)
local currentEvo = entity.prototype.build_base_evolution_requirement or 0
local distance = mMin(1, euclideanDistancePoints(position.x, position.y, 0, 0) * BASE_DISTANCE_TO_EVO_INDEX)
local evoIndex = mMax(distance, universe.evolutionLevel)
local evoIndex = mMax(distance, Universe.evolutionLevel)
local name = findEntityUpgrade(pickedBaseAlignment,
currentEvo,
@ -191,7 +195,7 @@ function chunkProcessor.processPendingUpgrades(universe, tick)
end
local surface = entity.surface
local query = universe.ppuUpgradeEntityQuery
local query = Universe.ppuUpgradeEntityQuery
query.name = name
unregisterEnemyBaseStructure(map, entity, nil, true)
@ -211,7 +215,7 @@ function chunkProcessor.processPendingUpgrades(universe, tick)
if entityData.register then
registerEnemyBaseStructure(map, createdEntity, base, true)
end
if not entityData.evolve and universe.printBaseUpgrades then
if not entityData.evolve and Universe.printBaseUpgrades then
surface.print("["..base.id.."]:"..surface.name.." Upgrading ".. entityName .. " to " .. name .. " [gps=".. position.x ..",".. position.y .."]")
end
if remote.interfaces["kr-creep"] then
@ -219,31 +223,31 @@ function chunkProcessor.processPendingUpgrades(universe, tick)
end
end
else
universe.pendingUpgradeIterator = next(universe.pendingUpgrades, entityId)
universe.pendingUpgrades[entityId] = nil
Universe.pendingUpgradeIterator = next(Universe.pendingUpgrades, entityId)
Universe.pendingUpgrades[entityId] = nil
end
end
end
function chunkProcessor.processScanChunks(universe)
local chunkId = universe.chunkToPassScanIterator
function ChunkProcessor.processScanChunks()
local chunkId = Universe.chunkToPassScanIterator
local chunkPack
if not chunkId then
chunkId, chunkPack = next(universe.chunkToPassScan, nil)
chunkId, chunkPack = next(Universe.chunkToPassScan, nil)
else
chunkPack = universe.chunkToPassScan[chunkId]
chunkPack = Universe.chunkToPassScan[chunkId]
end
if not chunkId then
universe.chunkToPassScanIterator = nil
if (table_size(universe.chunkToPassScan) == 0) then
Universe.chunkToPassScanIterator = nil
if (table_size(Universe.chunkToPassScan) == 0) then
-- this is needed as the next command remembers the max length a table has been
universe.chunkToPassScan = {}
Universe.chunkToPassScan = {}
end
else
universe.chunkToPassScanIterator = next(universe.chunkToPassScan, chunkId)
universe.chunkToPassScan[chunkId] = nil
Universe.chunkToPassScanIterator = next(Universe.chunkToPassScan, chunkId)
Universe.chunkToPassScan[chunkId] = nil
local map = chunkPack.map
if not map.surface.valid then
return
@ -256,5 +260,9 @@ function chunkProcessor.processScanChunks(universe)
end
end
chunkProcessorG = chunkProcessor
return chunkProcessor
function ChunkProcessor.init(universe)
Universe = universe
end
ChunkProcessorG = ChunkProcessor
return ChunkProcessor

View File

@ -14,56 +14,63 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if chunkPropertyUtilsG then
return chunkPropertyUtilsG
if ChunkPropertyUtilsG then
return ChunkPropertyUtilsG
end
local chunkPropertyUtils = {}
local ChunkPropertyUtils = {}
local constants = require("Constants")
local mathUtils = require("MathUtils")
--
-- constants
local Universe
local PLAYER_GENERATOR_PERSISTANCE = constants.PLAYER_GENERATOR_PERSISTANCE
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
--
local COOLDOWN_DRAIN = constants.COOLDOWN_DRAIN
local Constants = require("Constants")
local MathUtils = require("MathUtils")
local RAIDING_MINIMUM_BASE_THRESHOLD = constants.RAIDING_MINIMUM_BASE_THRESHOLD
-- Constants
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local PLAYER_GENERATOR_PERSISTANCE = Constants.PLAYER_GENERATOR_PERSISTANCE
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = Constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
local MOVEMENT_GENERATOR_PERSISTANCE = constants.MOVEMENT_GENERATOR_PERSISTANCE
local CHUNK_ALL_DIRECTIONS = constants.CHUNK_ALL_DIRECTIONS
local COOLDOWN_DRAIN = Constants.COOLDOWN_DRAIN
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
local RAIDING_MINIMUM_BASE_THRESHOLD = Constants.RAIDING_MINIMUM_BASE_THRESHOLD
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
local MOVEMENT_GENERATOR_PERSISTANCE = Constants.MOVEMENT_GENERATOR_PERSISTANCE
local CHUNK_ALL_DIRECTIONS = Constants.CHUNK_ALL_DIRECTIONS
local MAGIC_MAXIMUM_NUMBER = Constants.MAGIC_MAXIMUM_NUMBER
-- imported functions
local manhattenDistancePoints = mathUtils.manhattenDistancePoints
local manhattenDistancePoints = MathUtils.manhattenDistancePoints
local tableSize = table_size
local mMin = math.min
-- module code
function chunkPropertyUtils.getTurretCount(map, chunk)
function ChunkPropertyUtils.getTurretCount(map, chunk)
return map.chunkToTurrets[chunk.id] or 0
end
function chunkPropertyUtils.getTrapCount(map, chunk)
function ChunkPropertyUtils.getTrapCount(map, chunk)
return map.chunkToTraps[chunk.id] or 0
end
function chunkPropertyUtils.getUtilityCount(map, chunk)
function ChunkPropertyUtils.getUtilityCount(map, chunk)
return map.chunkToUtilities[chunk.id] or 0
end
function chunkPropertyUtils.getHiveCount(map, chunk)
function ChunkPropertyUtils.getHiveCount(map, chunk)
return map.chunkToHives[chunk.id] or 0
end
function chunkPropertyUtils.addTurretCount(map, chunk, unitNumber)
function ChunkPropertyUtils.addTurretCount(map, chunk, unitNumber)
map.activeSurface = true
if not map.chunkToTurretIds[chunk.id] then
map.chunkToTurretIds[chunk.id] = {}
@ -76,7 +83,7 @@ function chunkPropertyUtils.addTurretCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.removeTurretCount(map, chunk, unitNumber)
function ChunkPropertyUtils.removeTurretCount(map, chunk, unitNumber)
if map.chunkToTurretIds[chunk.id] and map.chunkToTurretIds[chunk.id][unitNumber] then
map.chunkToTurretIds[chunk.id][unitNumber] = nil
map.chunkToTurrets[chunk.id] = map.chunkToTurrets[chunk.id] - 1
@ -89,7 +96,7 @@ function chunkPropertyUtils.removeTurretCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.addTrapCount(map, chunk, unitNumber)
function ChunkPropertyUtils.addTrapCount(map, chunk, unitNumber)
map.activeSurface = true
if not map.chunkToTrapIds[chunk.id] then
map.chunkToTrapIds[chunk.id] = {}
@ -102,7 +109,7 @@ function chunkPropertyUtils.addTrapCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.removeTrapCount(map, chunk, unitNumber)
function ChunkPropertyUtils.removeTrapCount(map, chunk, unitNumber)
if map.chunkToTrapIds[chunk.id] and map.chunkToTrapIds[chunk.id][unitNumber] then
map.chunkToTrapIds[chunk.id][unitNumber] = nil
map.chunkToTraps[chunk.id] = map.chunkToTraps[chunk.id] - 1
@ -115,7 +122,7 @@ function chunkPropertyUtils.removeTrapCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.addUtilitiesCount(map, chunk, unitNumber)
function ChunkPropertyUtils.addUtilitiesCount(map, chunk, unitNumber)
map.activeSurface = true
if not map.chunkToUtilityIds[chunk.id] then
map.chunkToUtilityIds[chunk.id] = {}
@ -128,7 +135,7 @@ function chunkPropertyUtils.addUtilitiesCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.removeUtilitiesCount(map, chunk, unitNumber)
function ChunkPropertyUtils.removeUtilitiesCount(map, chunk, unitNumber)
if map.chunkToUtilityIds[chunk.id] and map.chunkToUtilityIds[chunk.id][unitNumber] then
map.chunkToUtilityIds[chunk.id][unitNumber] = nil
map.chunkToUtilities[chunk.id] = map.chunkToUtilities[chunk.id] - 1
@ -141,7 +148,7 @@ function chunkPropertyUtils.removeUtilitiesCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.addHiveCount(map, chunk, unitNumber)
function ChunkPropertyUtils.addHiveCount(map, chunk, unitNumber)
map.activeSurface = true
if not map.chunkToHiveIds[chunk.id] then
map.chunkToHiveIds[chunk.id] = {}
@ -154,7 +161,7 @@ function chunkPropertyUtils.addHiveCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.removeHiveCount(map, chunk, unitNumber)
function ChunkPropertyUtils.removeHiveCount(map, chunk, unitNumber)
if map.chunkToHiveIds[chunk.id] and map.chunkToHiveIds[chunk.id][unitNumber] then
map.chunkToHiveIds[chunk.id][unitNumber] = nil
map.chunkToHives[chunk.id] = map.chunkToHives[chunk.id] - 1
@ -167,7 +174,7 @@ function chunkPropertyUtils.removeHiveCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.addNestCount(map, chunk, unitNumber)
function ChunkPropertyUtils.addNestCount(map, chunk, unitNumber)
map.activeSurface = true
local chunkId = chunk.id
if not map.chunkToNestIds[chunkId] then
@ -175,7 +182,7 @@ function chunkPropertyUtils.addNestCount(map, chunk, unitNumber)
end
if not map.chunkToNestIds[chunkId][unitNumber] then
map.chunkToNestIds[chunkId][unitNumber] = true
local cToN = map.universe.chunkToNests
local cToN = Universe.chunkToNests
local pack = cToN[chunkId]
if not pack then
cToN[chunkId] = {
@ -189,20 +196,20 @@ function chunkPropertyUtils.addNestCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.removeNestCount(map, chunk, unitNumber)
function ChunkPropertyUtils.removeNestCount(map, chunk, unitNumber)
local chunkId = chunk.id
if map.chunkToNestIds[chunkId] and map.chunkToNestIds[chunkId][unitNumber] then
map.chunkToNestIds[chunkId][unitNumber] = nil
local cToN = map.universe.chunkToNests
local cToN = Universe.chunkToNests
cToN[chunkId].v = cToN[chunkId].v - 1
if cToN[chunkId].v == 0 then
map.chunkToNestIds[chunkId] = nil
cToN[chunkId] = nil
if (map.universe.processMigrationIterator == chunkId) then
map.universe.processMigrationIterator = nil
if (Universe.processMigrationIterator == chunkId) then
Universe.processMigrationIterator = nil
end
if (map.universe.processNestIterator == chunkId) then
map.universe.processNestIterator = nil
if (Universe.processNestIterator == chunkId) then
Universe.processNestIterator = nil
end
end
return true
@ -210,49 +217,49 @@ function chunkPropertyUtils.removeNestCount(map, chunk, unitNumber)
return false
end
function chunkPropertyUtils.getNestCount(map, chunk)
local nestPack = map.universe.chunkToNests[chunk.id]
function ChunkPropertyUtils.getNestCount(chunk)
local nestPack = Universe.chunkToNests[chunk.id]
if not nestPack then
return 0
end
return nestPack.v
end
function chunkPropertyUtils.addBaseResourceChunk(base, chunk)
if chunkPropertyUtils.getResourceGenerator(base.map, chunk) > 0 then
function ChunkPropertyUtils.addBaseResourceChunk(base, chunk)
if ChunkPropertyUtils.getResourceGenerator(base.map, chunk) > 0 then
base.resourceChunkCount = base.resourceChunkCount + 1
base.resourceChunks[chunk.id] = true
end
end
function chunkPropertyUtils.removeBaseResourceChunk(base, chunk)
function ChunkPropertyUtils.removeBaseResourceChunk(base, chunk)
if base.resourceChunks[chunk.id] then
base.resourceChunkCount = base.resourceChunkCount - 1
base.resourceChunks[chunk.id] = nil
end
end
function chunkPropertyUtils.getChunkBase(map, chunk)
function ChunkPropertyUtils.getChunkBase(map, chunk)
return map.chunkToBase[chunk.id]
end
function chunkPropertyUtils.removeChunkBase(map, chunk, base)
function ChunkPropertyUtils.removeChunkBase(map, chunk, base)
if map.chunkToBase[chunk.id] then
base.chunkCount = base.chunkCount - 1
map.chunkToBase[chunk.id] = nil
end
end
function chunkPropertyUtils.setChunkBase(map, chunk, base)
function ChunkPropertyUtils.setChunkBase(map, chunk, base)
if not map.chunkToBase[chunk.id] then
base.chunkCount = base.chunkCount + 1
map.chunkToBase[chunk.id] = base
end
end
function chunkPropertyUtils.getEnemyStructureCount(map, chunk)
function ChunkPropertyUtils.getEnemyStructureCount(map, chunk)
local nests = 0
local nestPack = map.universe.chunkToNests[chunk.id]
local nestPack = Universe.chunkToNests[chunk.id]
if nestPack then
nests = nestPack.v
end
@ -260,13 +267,13 @@ function chunkPropertyUtils.getEnemyStructureCount(map, chunk)
(map.chunkToUtilities[chunk.id] or 0) + (map.chunkToHives[chunk.id] or 0)
end
function chunkPropertyUtils.setDrainPylons(map, entity1, entity2)
function ChunkPropertyUtils.setDrainPylons(map, entity1, entity2)
local pair = {entity1, entity2}
map.drainPylons[entity1.unit_number] = pair
map.drainPylons[entity2.unit_number] = pair
end
function chunkPropertyUtils.removeDrainPylons(map, unitNumber)
function ChunkPropertyUtils.removeDrainPylons(map, unitNumber)
local pair = map.drainPylons[unitNumber]
if pair then
local target = pair[1]
@ -287,74 +294,74 @@ function chunkPropertyUtils.removeDrainPylons(map, unitNumber)
end
end
function chunkPropertyUtils.getDrainPylonPair(map, unitNumber)
function ChunkPropertyUtils.getDrainPylonPair(map, unitNumber)
return map.drainPylons[unitNumber]
end
function chunkPropertyUtils.isDrained(map, chunk, tick)
local pack = map.universe.chunkToDrained[chunk.id]
function ChunkPropertyUtils.isDrained(chunk, tick)
local pack = Universe.chunkToDrained[chunk.id]
if not pack then
return false
end
return (tick - pack.tick) < COOLDOWN_DRAIN
end
function chunkPropertyUtils.setDrainedTick(map, chunk, tick)
function ChunkPropertyUtils.setDrainedTick(map, chunk, tick)
local chunkId = chunk.id
local pack = map.universe.chunkToDrained[chunkId]
local pack = Universe.chunkToDrained[chunkId]
if not pack then
pack = {
map = map,
tick = 0
}
map.universe.chunkToDrained[chunkId] = pack
Universe.chunkToDrained[chunkId] = pack
end
pack.tick = tick
end
function chunkPropertyUtils.getRetreatTick(map, chunk)
local pack = map.universe.chunkToRetreats[chunk.id]
function ChunkPropertyUtils.getRetreatTick(chunk)
local pack = Universe.chunkToRetreats[chunk.id]
if not pack then
return 0
end
return pack.tick
end
function chunkPropertyUtils.getRallyTick(map, chunk)
local pack = map.universe.chunkToRallys[chunk.id]
function ChunkPropertyUtils.getRallyTick(chunk)
local pack = Universe.chunkToRallys[chunk.id]
if not pack then
return 0
end
return pack.tick
end
function chunkPropertyUtils.setRallyTick(map, chunk, tick)
function ChunkPropertyUtils.setRallyTick(map, chunk, tick)
local chunkId = chunk.id
local pack = map.universe.chunkToRallys[chunkId]
local pack = Universe.chunkToRallys[chunkId]
if not pack then
pack = {
map = map,
tick = tick
}
map.universe.chunkToRallys[chunkId] = pack
Universe.chunkToRallys[chunkId] = pack
end
pack.tick = tick
end
function chunkPropertyUtils.setRetreatTick(map, chunk, tick)
function ChunkPropertyUtils.setRetreatTick(map, chunk, tick)
local chunkId = chunk.id
local pack = map.universe.chunkToRetreats[chunkId]
local pack = Universe.chunkToRetreats[chunkId]
if not pack then
pack = {
map = map,
tick = tick
}
map.universe.chunkToRetreats[chunkId] = pack
Universe.chunkToRetreats[chunkId] = pack
end
pack.tick = tick
end
function chunkPropertyUtils.setResourceGenerator(map, chunk, resourceGenerator)
function ChunkPropertyUtils.setResourceGenerator(map, chunk, resourceGenerator)
if (resourceGenerator <= 0) then
map.chunkToResource[chunk.id] = nil
else
@ -362,80 +369,78 @@ function chunkPropertyUtils.setResourceGenerator(map, chunk, resourceGenerator)
end
end
function chunkPropertyUtils.getResourceGenerator(map, chunk)
function ChunkPropertyUtils.getResourceGenerator(map, chunk)
return map.chunkToResource[chunk.id] or 0
end
function chunkPropertyUtils.addResourceGenerator(map, chunk, delta)
function ChunkPropertyUtils.addResourceGenerator(map, chunk, delta)
map.chunkToResource[chunk.id] = (map.chunkToResource[chunk.id] or 0) + delta
end
function chunkPropertyUtils.getPassable(map, chunk)
function ChunkPropertyUtils.getPassable(map, chunk)
return map.chunkToPassable[chunk.id] or CHUNK_ALL_DIRECTIONS
end
function chunkPropertyUtils.getRaidNestActiveness(map, chunk)
local activeness = map.universe.chunkToActiveRaidNest[chunk.id]
function ChunkPropertyUtils.getRaidNestActiveness(chunk)
local activeness = Universe.chunkToActiveRaidNest[chunk.id]
if not activeness then
return 0
end
return activeness.v or 0
end
function chunkPropertyUtils.setRaidNestActiveness(map, chunk, value, base)
local universe = map.universe
function ChunkPropertyUtils.setRaidNestActiveness(map, chunk, value, base)
if (value <= 0) then
if universe.chunkToActiveRaidNest[chunk.id] then
if Universe.chunkToActiveRaidNest[chunk.id] then
base.activeRaidNests = base.activeRaidNests - 1
end
if (universe.processActiveRaidSpawnerIterator == chunk.id) then
universe.processActiveRaidSpawnerIterator = nil
if (Universe.processActiveRaidSpawnerIterator == chunk.id) then
Universe.processActiveRaidSpawnerIterator = nil
end
universe.chunkToActiveRaidNest[chunk.id] = nil
Universe.chunkToActiveRaidNest[chunk.id] = nil
else
if not universe.chunkToActiveRaidNest[chunk.id] then
if not Universe.chunkToActiveRaidNest[chunk.id] then
base.activeRaidNests = base.activeRaidNests + 1
universe.chunkToActiveRaidNest[chunk.id] = {
Universe.chunkToActiveRaidNest[chunk.id] = {
map = map,
v = 0
}
end
universe.chunkToActiveRaidNest[chunk.id].v = value
Universe.chunkToActiveRaidNest[chunk.id].v = value
end
end
function chunkPropertyUtils.getNestActiveness(map, chunk)
local activeness = map.universe.chunkToActiveNest[chunk.id]
function ChunkPropertyUtils.getNestActiveness(chunk)
local activeness = Universe.chunkToActiveNest[chunk.id]
if not activeness then
return 0
end
return activeness.v or 0
end
function chunkPropertyUtils.setNestActiveness(map, chunk, value, base)
local universe = map.universe
function ChunkPropertyUtils.setNestActiveness(map, chunk, value, base)
if (value <= 0) then
if universe.chunkToActiveNest[chunk.id] then
if Universe.chunkToActiveNest[chunk.id] then
base.activeNests = base.activeNests - 1
end
if (universe.processActiveSpawnerIterator == chunk.id) then
universe.processActiveSpawnerIterator = nil
if (Universe.processActiveSpawnerIterator == chunk.id) then
Universe.processActiveSpawnerIterator = nil
end
universe.chunkToActiveNest[chunk.id] = nil
Universe.chunkToActiveNest[chunk.id] = nil
else
if not universe.chunkToActiveNest[chunk.id] then
if not Universe.chunkToActiveNest[chunk.id] then
base.activeNests = base.activeNests + 1
universe.chunkToActiveNest[chunk.id] = {
Universe.chunkToActiveNest[chunk.id] = {
map = map,
v = 0
}
end
universe.chunkToActiveNest[chunk.id].v = value
Universe.chunkToActiveNest[chunk.id].v = value
end
end
function chunkPropertyUtils.setPassable(map, chunk, value)
function ChunkPropertyUtils.setPassable(map, chunk, value)
if (value == CHUNK_ALL_DIRECTIONS) then
map.chunkToPassable[chunk.id] = nil
else
@ -443,11 +448,11 @@ function chunkPropertyUtils.setPassable(map, chunk, value)
end
end
function chunkPropertyUtils.getPathRating(map, chunk)
function ChunkPropertyUtils.getPathRating(map, chunk)
return map.chunkToPathRating[chunk.id] or 1
end
function chunkPropertyUtils.setPathRating(map, chunk, value)
function ChunkPropertyUtils.setPathRating(map, chunk, value)
if (value == 1) then
map.chunkToPathRating[chunk.id] = nil
else
@ -455,11 +460,11 @@ function chunkPropertyUtils.setPathRating(map, chunk, value)
end
end
function chunkPropertyUtils.getDeathGeneratorRating(map, chunk)
function ChunkPropertyUtils.getDeathGeneratorRating(map, chunk)
return 1 + (map.chunkToDeathGenerator[chunk.id] or 0)
end
function chunkPropertyUtils.getCombinedDeathGeneratorRating(map, chunk)
function ChunkPropertyUtils.getCombinedDeathGeneratorRating(map, chunk)
local amount = 1 + ((map.chunkToDeathGenerator[chunk.id] or 0) + (map.chunkToPermanentDeathGenerator[chunk.id] or 0))
if (amount > 1) then
return 1
@ -470,15 +475,15 @@ function chunkPropertyUtils.getCombinedDeathGeneratorRating(map, chunk)
end
end
function chunkPropertyUtils.getDeathGenerator(map, chunk)
function ChunkPropertyUtils.getDeathGenerator(map, chunk)
return map.chunkToDeathGenerator[chunk.id] or 0
end
function chunkPropertyUtils.getPermanentDeathGeneratorRating(map, chunk)
function ChunkPropertyUtils.getPermanentDeathGeneratorRating(map, chunk)
return 1 + (map.chunkToPermanentDeathGenerator[chunk.id] or 0)
end
function chunkPropertyUtils.getCombinedDeathGenerator(map, chunk)
function ChunkPropertyUtils.getCombinedDeathGenerator(map, chunk)
local amount = (map.chunkToDeathGenerator[chunk.id] or 0) + (map.chunkToPermanentDeathGenerator[chunk.id] or 0)
if (amount > 1) then
return 1
@ -489,7 +494,7 @@ function chunkPropertyUtils.getCombinedDeathGenerator(map, chunk)
end
end
function chunkPropertyUtils.addPermanentDeathGenerator(map, chunk, amount)
function ChunkPropertyUtils.addPermanentDeathGenerator(map, chunk, amount)
local adjustedAmount = (amount * 0.25) + (map.chunkToPermanentDeathGenerator[chunk.id] or 0)
if (adjustedAmount > 0.75) then
map.chunkToPermanentDeathGenerator[chunk.id] = 0.75
@ -500,7 +505,7 @@ function chunkPropertyUtils.addPermanentDeathGenerator(map, chunk, amount)
end
end
function chunkPropertyUtils.addDeathGenerator(map, chunk, value)
function ChunkPropertyUtils.addDeathGenerator(map, chunk, value)
local currentAmount = (map.chunkToDeathGenerator[chunk.id] or 0) + value
if (currentAmount > 1) then
map.chunkToDeathGenerator[chunk.id] = 1
@ -511,7 +516,7 @@ function chunkPropertyUtils.addDeathGenerator(map, chunk, value)
end
end
function chunkPropertyUtils.setDeathGenerator(map, chunk, value)
function ChunkPropertyUtils.setDeathGenerator(map, chunk, value)
if (value > 1) then
map.chunkToDeathGenerator[chunk.id] = 1
elseif (value < -1) then
@ -521,8 +526,8 @@ function chunkPropertyUtils.setDeathGenerator(map, chunk, value)
end
end
function chunkPropertyUtils.addVictoryGenerator(map, chunk, value)
local cToV = map.universe.chunkToVictory
function ChunkPropertyUtils.addVictoryGenerator(map, chunk, value)
local cToV = Universe.chunkToVictory
local chunkId = chunk.id
if not cToV[chunkId] then
cToV[chunkId] = {
@ -533,7 +538,7 @@ function chunkPropertyUtils.addVictoryGenerator(map, chunk, value)
cToV[chunkId].v = cToV[chunkId].v + value
end
function chunkPropertyUtils.decayDeathGenerator(map, chunk)
function ChunkPropertyUtils.decayDeathGenerator(map, chunk)
local gen = map.chunkToDeathGenerator[chunk.id]
if gen then
local v = gen * MOVEMENT_GENERATOR_PERSISTANCE
@ -545,7 +550,7 @@ function chunkPropertyUtils.decayDeathGenerator(map, chunk)
end
end
function chunkPropertyUtils.decayPlayerGenerator(map, chunk)
function ChunkPropertyUtils.decayPlayerGenerator(map, chunk)
local gen = map.chunkToPlayerGenerator[chunk.id]
if gen then
local v = gen * PLAYER_GENERATOR_PERSISTANCE
@ -557,7 +562,7 @@ function chunkPropertyUtils.decayPlayerGenerator(map, chunk)
end
end
function chunkPropertyUtils.addPlayerGenerator(map, chunk, playerMaxGenerator)
function ChunkPropertyUtils.addPlayerGenerator(map, chunk, playerMaxGenerator)
local value = map.chunkToPlayerGenerator[chunk.id]
if value then
map.chunkToPlayerGenerator[chunk.id] = mMin(
@ -569,19 +574,19 @@ function chunkPropertyUtils.addPlayerGenerator(map, chunk, playerMaxGenerator)
end
end
function chunkPropertyUtils.getPlayerGenerator(map, chunk)
function ChunkPropertyUtils.getPlayerGenerator(map, chunk)
return map.chunkToPlayerGenerator[chunk.id] or 0
end
function chunkPropertyUtils.getPlayerBaseGenerator(map, chunk)
function ChunkPropertyUtils.getPlayerBaseGenerator(map, chunk)
return map.chunkToPlayerBase[chunk.id] or 0
end
function chunkPropertyUtils.addSquadToChunk(map, chunk, squad)
function ChunkPropertyUtils.addSquadToChunk(map, chunk, squad)
local chunkToSquad = map.chunkToSquad
if (chunk ~= -1) and ((squad.chunk == -1) or (squad.chunk.id ~= chunk.id)) then
chunkPropertyUtils.removeSquadFromChunk(map, squad)
ChunkPropertyUtils.removeSquadFromChunk(map, squad)
local squads = chunkToSquad[chunk.id]
if not squads then
squads = {}
@ -592,25 +597,25 @@ function chunkPropertyUtils.addSquadToChunk(map, chunk, squad)
end
end
function chunkPropertyUtils.removeSquadFromChunk(map, squad)
function ChunkPropertyUtils.removeSquadFromChunk(map, squad)
local chunkToSquad = map.chunkToSquad
local chunk = squad.chunk
if (chunk ~= -1) then
local squads = chunkToSquad[chunk.id]
if squads then
squads[squad.groupNumber] = nil
if (table_size(squads) == 0) then
if (tableSize(squads) == 0) then
chunkToSquad[chunk.id] = nil
end
end
end
end
function chunkPropertyUtils.getSquadsOnChunk(map, chunk)
function ChunkPropertyUtils.getSquadsOnChunk(map, chunk)
return map.chunkToSquad[chunk.id] or map.emptySquadsOnChunk
end
function chunkPropertyUtils.setPlayerBaseGenerator(map, chunk, playerGenerator)
function ChunkPropertyUtils.setPlayerBaseGenerator(map, chunk, playerGenerator)
if (playerGenerator <= 0) then
map.chunkToPlayerBase[chunk.id] = nil
return 0
@ -620,7 +625,7 @@ function chunkPropertyUtils.setPlayerBaseGenerator(map, chunk, playerGenerator)
end
end
function chunkPropertyUtils.addPlayerBaseGenerator(map, chunk, playerGenerator)
function ChunkPropertyUtils.addPlayerBaseGenerator(map, chunk, playerGenerator)
local amount = (map.chunkToPlayerBase[chunk.id] or 0) + playerGenerator
if amount <= 0 then
map.chunkToPlayerBase[chunk.id] = nil
@ -631,11 +636,11 @@ function chunkPropertyUtils.addPlayerBaseGenerator(map, chunk, playerGenerator)
end
end
function chunkPropertyUtils.findNearbyBase(map, chunk)
function ChunkPropertyUtils.findNearbyBase(map, chunk)
local x = chunk.x
local y = chunk.y
local foundBase = chunkPropertyUtils.getChunkBase(map, chunk)
local foundBase = ChunkPropertyUtils.getChunkBase(map, chunk)
if foundBase then
return foundBase
end
@ -652,7 +657,7 @@ function chunkPropertyUtils.findNearbyBase(map, chunk)
return foundBase
end
function chunkPropertyUtils.findNearbyBaseByPosition(map, x, y)
function ChunkPropertyUtils.findNearbyBaseByPosition(map, x, y)
local foundBase
local closest = MAGIC_MAXIMUM_NUMBER
@ -667,47 +672,46 @@ function chunkPropertyUtils.findNearbyBaseByPosition(map, x, y)
return foundBase
end
function chunkPropertyUtils.processNestActiveness(map, chunk)
local nests = chunkPropertyUtils.getNestCount(map, chunk)
local base = chunkPropertyUtils.findNearbyBase(map, chunk)
function ChunkPropertyUtils.processNestActiveness(map, chunk)
local nests = ChunkPropertyUtils.getNestCount(chunk)
local base = ChunkPropertyUtils.findNearbyBase(map, chunk)
if (nests > 0) then
local surface = map.surface
local activeness = chunkPropertyUtils.getNestActiveness(map, chunk)
local universe = map.universe
local raidActiveness = chunkPropertyUtils.getRaidNestActiveness(map, chunk)
if universe.attackUsePlayer and (chunk[PLAYER_PHEROMONE] > universe.attackPlayerThreshold) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
local activeness = ChunkPropertyUtils.getNestActiveness(chunk)
local raidActiveness = ChunkPropertyUtils.getRaidNestActiveness(chunk)
if Universe.attackUsePlayer and (chunk[PLAYER_PHEROMONE] > Universe.attackPlayerThreshold) then
ChunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
elseif (chunk[BASE_PHEROMONE] > 0) then
if (surface.get_pollution(chunk) > 0) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
ChunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
else
local x = chunk.x
local y = chunk.y
local position = {x=0,y=0}
local pollutionThreshold = universe.pollutionDiffuseMinimum
local pollutionThreshold = Universe.pollutionDiffuseMinimum
position.x = x + 32
position.y = y
if (surface.get_pollution(position) > pollutionThreshold) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
ChunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
else
position.x = x - 32
if (surface.get_pollution(position) > pollutionThreshold) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
ChunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
else
position.x = x
position.y = y - 32
if (surface.get_pollution(position) > pollutionThreshold) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
ChunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
else
position.y = y + 32
if (surface.get_pollution(position) > pollutionThreshold) then
chunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
ChunkPropertyUtils.setNestActiveness(map, chunk, mMin(activeness + 5, 20), base)
else
chunkPropertyUtils.setNestActiveness(map, chunk, activeness - 2, base)
ChunkPropertyUtils.setNestActiveness(map, chunk, activeness - 2, base)
if (chunk[BASE_PHEROMONE] > RAIDING_MINIMUM_BASE_THRESHOLD) then
chunkPropertyUtils.setRaidNestActiveness(map, chunk, mMin(raidActiveness + 3, 20), base)
ChunkPropertyUtils.setRaidNestActiveness(map, chunk, mMin(raidActiveness + 3, 20), base)
else
chunkPropertyUtils.setRaidNestActiveness(map, chunk, raidActiveness - 1, base)
ChunkPropertyUtils.setRaidNestActiveness(map, chunk, raidActiveness - 1, base)
end
end
end
@ -715,15 +719,18 @@ function chunkPropertyUtils.processNestActiveness(map, chunk)
end
end
else
chunkPropertyUtils.setNestActiveness(map, chunk, activeness - 5, base)
chunkPropertyUtils.setRaidNestActiveness(map, chunk, raidActiveness - 5, base)
ChunkPropertyUtils.setNestActiveness(map, chunk, activeness - 5, base)
ChunkPropertyUtils.setRaidNestActiveness(map, chunk, raidActiveness - 5, base)
end
elseif base then
chunkPropertyUtils.setNestActiveness(map, chunk, 0, base)
chunkPropertyUtils.setRaidNestActiveness(map, chunk, 0, base)
ChunkPropertyUtils.setNestActiveness(map, chunk, 0, base)
ChunkPropertyUtils.setRaidNestActiveness(map, chunk, 0, base)
end
end
function ChunkPropertyUtils.init(universe)
Universe = universe
end
chunkPropertyUtilsG = chunkPropertyUtils
return chunkPropertyUtils
ChunkPropertyUtilsG = ChunkPropertyUtils
return ChunkPropertyUtils

View File

@ -14,104 +14,109 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if chunkUtilsG then
return chunkUtilsG
if ChunkUtilsG then
return ChunkUtilsG
end
local chunkUtils = {}
local ChunkUtils = {}
--
local Universe
local ChunkOverlapArray
-- imports
local baseUtils = require("BaseUtils")
local constants = require("Constants")
local mapUtils = require("MapUtils")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local mathUtils = require("MathUtils")
local queryUtils = require("QueryUtils")
local BaseUtils = require("BaseUtils")
local Constants = require("Constants")
local MapUtils = require("MapUtils")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
local MathUtils = require("MathUtils")
local QueryUtils = require("QueryUtils")
-- constants
-- 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 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
local DEFINES_WIRE_TYPE_GREEN = defines.wire_type.green
local BASE_AI_STATE_ONSLAUGHT = constants.BASE_AI_STATE_ONSLAUGHT
local BASE_AI_STATE_ONSLAUGHT = Constants.BASE_AI_STATE_ONSLAUGHT
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
local ENEMY_PHEROMONE = constants.ENEMY_PHEROMONE
local BUILDING_PHEROMONES = constants.BUILDING_PHEROMONES
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
local RESOURCE_PHEROMONE = Constants.RESOURCE_PHEROMONE
local ENEMY_PHEROMONE = Constants.ENEMY_PHEROMONE
local BUILDING_PHEROMONES = Constants.BUILDING_PHEROMONES
local CHUNK_SIZE = constants.CHUNK_SIZE
local CHUNK_SIZE_DIVIDER = constants.CHUNK_SIZE_DIVIDER
local CHUNK_SIZE = Constants.CHUNK_SIZE
local CHUNK_SIZE_DIVIDER = Constants.CHUNK_SIZE_DIVIDER
local CHUNK_NORTH_SOUTH = constants.CHUNK_NORTH_SOUTH
local CHUNK_EAST_WEST = constants.CHUNK_EAST_WEST
local CHUNK_NORTH_SOUTH = Constants.CHUNK_NORTH_SOUTH
local CHUNK_EAST_WEST = Constants.CHUNK_EAST_WEST
local CHUNK_ALL_DIRECTIONS = constants.CHUNK_ALL_DIRECTIONS
local CHUNK_IMPASSABLE = constants.CHUNK_IMPASSABLE
local CHUNK_ALL_DIRECTIONS = Constants.CHUNK_ALL_DIRECTIONS
local CHUNK_IMPASSABLE = Constants.CHUNK_IMPASSABLE
local RESOURCE_NORMALIZER = constants.RESOURCE_NORMALIZER
local RESOURCE_NORMALIZER = Constants.RESOURCE_NORMALIZER
local CHUNK_TICK = constants.CHUNK_TICK
local CHUNK_TICK = Constants.CHUNK_TICK
local GENERATOR_PHEROMONE_LEVEL_1 = constants.GENERATOR_PHEROMONE_LEVEL_1
local GENERATOR_PHEROMONE_LEVEL_3 = constants.GENERATOR_PHEROMONE_LEVEL_3
local GENERATOR_PHEROMONE_LEVEL_5 = constants.GENERATOR_PHEROMONE_LEVEL_5
local GENERATOR_PHEROMONE_LEVEL_6 = constants.GENERATOR_PHEROMONE_LEVEL_6
local GENERATOR_PHEROMONE_LEVEL_1 = Constants.GENERATOR_PHEROMONE_LEVEL_1
local GENERATOR_PHEROMONE_LEVEL_3 = Constants.GENERATOR_PHEROMONE_LEVEL_3
local GENERATOR_PHEROMONE_LEVEL_5 = Constants.GENERATOR_PHEROMONE_LEVEL_5
local GENERATOR_PHEROMONE_LEVEL_6 = Constants.GENERATOR_PHEROMONE_LEVEL_6
-- imported functions
local removeBaseResourceChunk = chunkPropertyUtils.removeBaseResourceChunk
local addBaseResourceChunk = chunkPropertyUtils.addBaseResourceChunk
local removeBaseResourceChunk = ChunkPropertyUtils.removeBaseResourceChunk
local addBaseResourceChunk = ChunkPropertyUtils.addBaseResourceChunk
local setAreaInQueryChunkSize = queryUtils.setAreaInQueryChunkSize
local setAreaXInQuery = queryUtils.setAreaXInQuery
local setAreaYInQuery = queryUtils.setAreaYInQuery
local setAreaInQueryChunkSize = QueryUtils.setAreaInQueryChunkSize
local setAreaXInQuery = QueryUtils.setAreaXInQuery
local setAreaYInQuery = QueryUtils.setAreaYInQuery
local setPlayerBaseGenerator = chunkPropertyUtils.setPlayerBaseGenerator
local addPlayerBaseGenerator = chunkPropertyUtils.addPlayerBaseGenerator
local setResourceGenerator = chunkPropertyUtils.setResourceGenerator
local addResourceGenerator = chunkPropertyUtils.addResourceGenerator
local setPlayerBaseGenerator = ChunkPropertyUtils.setPlayerBaseGenerator
local addPlayerBaseGenerator = ChunkPropertyUtils.addPlayerBaseGenerator
local setResourceGenerator = ChunkPropertyUtils.setResourceGenerator
local addResourceGenerator = ChunkPropertyUtils.addResourceGenerator
local addNestCount = chunkPropertyUtils.addNestCount
local removeNestCount = chunkPropertyUtils.removeNestCount
local addHiveCount = chunkPropertyUtils.addHiveCount
local removeHiveCount = chunkPropertyUtils.removeHiveCount
local addTrapCount = chunkPropertyUtils.addTrapCount
local removeTrapCount = chunkPropertyUtils.removeTrapCount
local addTurretCount = chunkPropertyUtils.addTurretCount
local removeTurretCount = chunkPropertyUtils.removeTurretCount
local addUtilityCount = chunkPropertyUtils.addUtilityCount
local removeUtilityCount = chunkPropertyUtils.removeUtilityCount
local addNestCount = ChunkPropertyUtils.addNestCount
local removeNestCount = ChunkPropertyUtils.removeNestCount
local addHiveCount = ChunkPropertyUtils.addHiveCount
local removeHiveCount = ChunkPropertyUtils.removeHiveCount
local addTrapCount = ChunkPropertyUtils.addTrapCount
local removeTrapCount = ChunkPropertyUtils.removeTrapCount
local addTurretCount = ChunkPropertyUtils.addTurretCount
local removeTurretCount = ChunkPropertyUtils.removeTurretCount
local addUtilityCount = ChunkPropertyUtils.addUtilityCount
local removeUtilityCount = ChunkPropertyUtils.removeUtilityCount
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
local setRaidNestActiveness = chunkPropertyUtils.setRaidNestActiveness
local setNestActiveness = chunkPropertyUtils.setNestActiveness
local getChunkById = mapUtils.getChunkById
local getPlayerBaseGenerator = ChunkPropertyUtils.getPlayerBaseGenerator
local setRaidNestActiveness = ChunkPropertyUtils.setRaidNestActiveness
local setNestActiveness = ChunkPropertyUtils.setNestActiveness
local getChunkById = MapUtils.getChunkById
local processNestActiveness = chunkPropertyUtils.processNestActiveness
local processNestActiveness = ChunkPropertyUtils.processNestActiveness
local removeChunkBase = chunkPropertyUtils.removeChunkBase
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
local removeChunkBase = ChunkPropertyUtils.removeChunkBase
local getEnemyStructureCount = ChunkPropertyUtils.getEnemyStructureCount
local findNearbyBase = chunkPropertyUtils.findNearbyBase
local findNearbyBase = ChunkPropertyUtils.findNearbyBase
local queueUpgrade = baseUtils.queueUpgrade
local createBase = baseUtils.createBase
local modifyBaseUnitPoints = baseUtils.modifyBaseUnitPoints
local queueUpgrade = BaseUtils.queueUpgrade
local createBase = BaseUtils.createBase
local modifyBaseUnitPoints = BaseUtils.modifyBaseUnitPoints
local euclideanDistancePoints = mathUtils.euclideanDistancePoints
local euclideanDistancePoints = MathUtils.euclideanDistancePoints
local getChunkBase = chunkPropertyUtils.getChunkBase
local setChunkBase = chunkPropertyUtils.setChunkBase
local setPassable = chunkPropertyUtils.setPassable
local setPathRating = chunkPropertyUtils.setPathRating
local getChunkBase = ChunkPropertyUtils.getChunkBase
local setChunkBase = ChunkPropertyUtils.setChunkBase
local setPassable = ChunkPropertyUtils.setPassable
local setPathRating = ChunkPropertyUtils.setPathRating
local getChunkByXY = mapUtils.getChunkByXY
local getChunkByXY = MapUtils.getChunkByXY
local mMin = math.min
local mMax = math.max
@ -121,12 +126,11 @@ local mFloor = math.floor
local function getEntityOverlapChunks(map, entity)
local boundingBox = entity.prototype.collision_box or entity.prototype.selection_box
local overlapArray = map.universe.chunkOverlapArray
overlapArray[1] = -1 --LeftTop
overlapArray[2] = -1 --RightTop
overlapArray[3] = -1 --LeftBottom
overlapArray[4] = -1 --RightBottom
ChunkOverlapArray[1] = -1 --LeftTop
ChunkOverlapArray[2] = -1 --RightTop
ChunkOverlapArray[3] = -1 --LeftBottom
ChunkOverlapArray[4] = -1 --RightBottom
if boundingBox then
local center = entity.position
@ -143,18 +147,18 @@ local function getEntityOverlapChunks(map, entity)
local rightTopChunkX = mFloor((center.x + bottomXOffset) * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
local leftBottomChunkY = mFloor((center.y + bottomYOffset) * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
overlapArray[1] = getChunkByXY(map, leftTopChunkX, leftTopChunkY) -- LeftTop
ChunkOverlapArray[1] = getChunkByXY(map, leftTopChunkX, leftTopChunkY) -- LeftTop
if (leftTopChunkX ~= rightTopChunkX) then
overlapArray[2] = getChunkByXY(map, rightTopChunkX, leftTopChunkY) -- RightTop
ChunkOverlapArray[2] = getChunkByXY(map, rightTopChunkX, leftTopChunkY) -- RightTop
end
if (leftTopChunkY ~= leftBottomChunkY) then
overlapArray[3] = getChunkByXY(map, leftTopChunkX, leftBottomChunkY) -- LeftBottom
ChunkOverlapArray[3] = getChunkByXY(map, leftTopChunkX, leftBottomChunkY) -- LeftBottom
end
if (leftTopChunkX ~= rightTopChunkX) and (leftTopChunkY ~= leftBottomChunkY) then
overlapArray[4] = getChunkByXY(map, rightTopChunkX, leftBottomChunkY) -- RightBottom
ChunkOverlapArray[4] = getChunkByXY(map, rightTopChunkX, leftBottomChunkY) -- RightBottom
end
end
return overlapArray
return ChunkOverlapArray
end
local function scanPaths(chunk, map)
@ -164,9 +168,8 @@ local function scanPaths(chunk, map)
local x = chunk.x
local y = chunk.y
local universe = map.universe
local filteredEntitiesCliffQuery = universe.spFilteredEntitiesCliffQuery
local filteredTilesPathQuery = universe.spFilteredTilesPathQuery
local filteredEntitiesCliffQuery = Universe.spFilteredEntitiesCliffQuery
local filteredTilesPathQuery = Universe.spFilteredTilesPathQuery
local count_entities_filtered = surface.count_entities_filtered
local count_tiles_filtered = surface.count_tiles_filtered
@ -209,23 +212,21 @@ end
local function scorePlayerBuildings(map, chunk)
local surface = map.surface
local universe = map.universe
setAreaInQueryChunkSize(universe.spbHasPlayerStructuresQuery, chunk)
if surface.count_entities_filtered(universe.spbHasPlayerStructuresQuery) > 0 then
return (surface.count_entities_filtered(universe.spbFilteredEntitiesPlayerQueryLowest) * GENERATOR_PHEROMONE_LEVEL_1) +
(surface.count_entities_filtered(universe.spbFilteredEntitiesPlayerQueryLow) * GENERATOR_PHEROMONE_LEVEL_3) +
(surface.count_entities_filtered(universe.spbFilteredEntitiesPlayerQueryHigh) * GENERATOR_PHEROMONE_LEVEL_5) +
(surface.count_entities_filtered(universe.spbFilteredEntitiesPlayerQueryHighest) * GENERATOR_PHEROMONE_LEVEL_6)
setAreaInQueryChunkSize(Universe.spbHasPlayerStructuresQuery, chunk)
if surface.count_entities_filtered(Universe.spbHasPlayerStructuresQuery) > 0 then
return (surface.count_entities_filtered(Universe.spbFilteredEntitiesPlayerQueryLowest) * GENERATOR_PHEROMONE_LEVEL_1) +
(surface.count_entities_filtered(Universe.spbFilteredEntitiesPlayerQueryLow) * GENERATOR_PHEROMONE_LEVEL_3) +
(surface.count_entities_filtered(Universe.spbFilteredEntitiesPlayerQueryHigh) * GENERATOR_PHEROMONE_LEVEL_5) +
(surface.count_entities_filtered(Universe.spbFilteredEntitiesPlayerQueryHighest) * GENERATOR_PHEROMONE_LEVEL_6)
end
return 0
end
function chunkUtils.initialScan(chunk, map, tick)
function ChunkUtils.initialScan(chunk, map, tick)
local surface = map.surface
local universe = map.universe
setAreaInQueryChunkSize(universe.isFilteredTilesQuery, chunk)
setAreaInQueryChunkSize(Universe.isFilteredTilesQuery, chunk)
local pass = scanPaths(chunk, map)
local enemyBuildings = surface.find_entities_filtered(universe.isFilteredEntitiesEnemyStructureQuery)
local enemyBuildings = surface.find_entities_filtered(Universe.isFilteredEntitiesEnemyStructureQuery)
local playerObjects = scorePlayerBuildings(map, chunk)
if (pass ~= CHUNK_IMPASSABLE) or (#enemyBuildings > 0) or (playerObjects > 0) then
@ -235,16 +236,16 @@ function chunkUtils.initialScan(chunk, map, tick)
if (pass ~= CHUNK_IMPASSABLE) then
local neutralObjects = mMax(0,
mMin(1 - (surface.count_entities_filtered(universe.isFilteredEntitiesChunkNeutral) * 0.005),
mMin(1 - (surface.count_entities_filtered(Universe.isFilteredEntitiesChunkNeutral) * 0.005),
1) * 0.20)
setPassable(map, chunk, pass)
local waterTiles = (1 - (surface.count_tiles_filtered(universe.isFilteredTilesQuery) * 0.0009765625)) * 0.80
local waterTiles = (1 - (surface.count_tiles_filtered(Universe.isFilteredTilesQuery) * 0.0009765625)) * 0.80
setPathRating(map, chunk, waterTiles + neutralObjects)
setPlayerBaseGenerator(map, chunk, playerObjects)
local resources = surface.count_entities_filtered(universe.isCountResourcesQuery) * RESOURCE_NORMALIZER
local resources = surface.count_entities_filtered(Universe.isCountResourcesQuery) * RESOURCE_NORMALIZER
setResourceGenerator(map, chunk, resources)
local counts = map.chunkScanCounts
@ -258,8 +259,8 @@ function chunkUtils.initialScan(chunk, map, tick)
base = createBase(map, chunk, tick)
end
if universe.NEW_ENEMIES then
local unitList = surface.find_entities_filtered(universe.isFilteredEntitiesUnitQuery)
if Universe.NEW_ENEMIES then
local unitList = surface.find_entities_filtered(Universe.isFilteredEntitiesUnitQuery)
for i=1,#unitList do
local unit = unitList[i]
if (unit.valid) then
@ -269,7 +270,7 @@ function chunkUtils.initialScan(chunk, map, tick)
for i = 1, #enemyBuildings do
local enemyBuilding = enemyBuildings[i]
chunkUtils.registerEnemyBaseStructure(map, enemyBuilding, base)
ChunkUtils.registerEnemyBaseStructure(map, enemyBuilding, base)
local entityName = enemyBuilding.name
local isVanilla = VANILLA_ENTITY_TYPE_LOOKUP[entityName]
if isVanilla or (not isVanilla and not BUILDING_HIVE_TYPE_LOOKUP[entityName]) then
@ -283,7 +284,7 @@ function chunkUtils.initialScan(chunk, map, tick)
else
for i=1,#enemyBuildings do
local building = enemyBuildings[i]
chunkUtils.registerEnemyBaseStructure(map, building, base)
ChunkUtils.registerEnemyBaseStructure(map, building, base)
end
end
end
@ -295,19 +296,18 @@ function chunkUtils.initialScan(chunk, map, tick)
return -1
end
function chunkUtils.chunkPassScan(chunk, map)
function ChunkUtils.chunkPassScan(chunk, map)
local surface = map.surface
local universe = map.universe
setAreaInQueryChunkSize(universe.cpsFilteredTilesQuery, chunk)
setAreaInQueryChunkSize(Universe.cpsFilteredTilesQuery, chunk)
local pass = scanPaths(chunk, map)
local enemyCount = surface.count_entities_filtered(universe.cpsFilteredEnemyAnyFound)
local enemyCount = surface.count_entities_filtered(Universe.cpsFilteredEnemyAnyFound)
local playerObjects = getPlayerBaseGenerator(map, chunk)
if (pass ~= CHUNK_IMPASSABLE) or (enemyCount > 0) or (playerObjects > 0) then
local neutralObjects = mMax(0,
mMin(1 - (surface.count_entities_filtered(universe.cpsFilteredEntitiesChunkNeutral) * 0.005),
mMin(1 - (surface.count_entities_filtered(Universe.cpsFilteredEntitiesChunkNeutral) * 0.005),
1) * 0.20)
local waterTiles = (1 - (surface.count_tiles_filtered(universe.cpsFilteredTilesQuery) * 0.0009765625)) * 0.80
local waterTiles = (1 - (surface.count_tiles_filtered(Universe.cpsFilteredTilesQuery) * 0.0009765625)) * 0.80
if ((playerObjects > 0) or (enemyCount > 0)) and (pass == CHUNK_IMPASSABLE) then
pass = CHUNK_ALL_DIRECTIONS
@ -322,28 +322,26 @@ function chunkUtils.chunkPassScan(chunk, map)
return -1
end
function chunkUtils.mapScanPlayerChunk(chunk, map)
function ChunkUtils.mapScanPlayerChunk(chunk, map)
local playerObjects = scorePlayerBuildings(map, chunk)
setPlayerBaseGenerator(map, chunk, playerObjects)
end
function chunkUtils.mapScanResourceChunk(chunk, map)
local universe = map.universe
setAreaInQueryChunkSize(universe.msrcCountResourcesQuery, chunk)
function ChunkUtils.mapScanResourceChunk(chunk, map)
setAreaInQueryChunkSize(Universe.msrcCountResourcesQuery, chunk)
local surface = map.surface
local resources = surface.count_entities_filtered(universe.msrcCountResourcesQuery) * RESOURCE_NORMALIZER
local resources = surface.count_entities_filtered(Universe.msrcCountResourcesQuery) * RESOURCE_NORMALIZER
setResourceGenerator(map, chunk, resources)
local waterTiles = (1 - (surface.count_tiles_filtered(universe.msrcFilteredTilesQuery) * 0.0009765625)) * 0.80
local waterTiles = (1 - (surface.count_tiles_filtered(Universe.msrcFilteredTilesQuery) * 0.0009765625)) * 0.80
local neutralObjects = mMax(0,
mMin(1 - (surface.count_entities_filtered(universe.msrcFilteredEntitiesChunkNeutral) * 0.005),
mMin(1 - (surface.count_entities_filtered(Universe.msrcFilteredEntitiesChunkNeutral) * 0.005),
1) * 0.20)
setPathRating(map, chunk, waterTiles + neutralObjects)
end
function chunkUtils.mapScanEnemyChunk(chunk, map, tick)
local universe = map.universe
setAreaInQueryChunkSize(universe.msecFilteredEntitiesEnemyStructureQuery, chunk)
local buildings = map.surface.find_entities_filtered(universe.msecFilteredEntitiesEnemyStructureQuery)
function ChunkUtils.mapScanEnemyChunk(chunk, map, tick)
setAreaInQueryChunkSize(Universe.msecFilteredEntitiesEnemyStructureQuery, chunk)
local buildings = map.surface.find_entities_filtered(Universe.msecFilteredEntitiesEnemyStructureQuery)
local counts = map.chunkScanCounts
for i=1,#HIVE_BUILDINGS_TYPES do
counts[HIVE_BUILDINGS_TYPES[i]] = 0
@ -356,16 +354,16 @@ function chunkUtils.mapScanEnemyChunk(chunk, map, tick)
for i=1,#buildings do
local building = buildings[i]
chunkUtils.registerEnemyBaseStructure(map, building, base)
ChunkUtils.registerEnemyBaseStructure(map, building, base)
end
end
end
function chunkUtils.addBasesToAllEnemyStructures(universe, tick)
for chunkId, chunkPack in pairs(universe.chunkToNests) do
function ChunkUtils.addBasesToAllEnemyStructures(tick)
for chunkId, chunkPack in pairs(Universe.chunkToNests) do
local map = chunkPack.map
if map.surface.valid then
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local base = findNearbyBase(map, chunk)
if not base then
base = createBase(map, chunk, tick)
@ -373,10 +371,10 @@ function chunkUtils.addBasesToAllEnemyStructures(universe, tick)
setChunkBase(map, chunk, base)
end
end
for _, map in pairs(universe.maps) do
for _, map in pairs(Universe.maps) do
if map.surface.valid then
for chunkId in pairs(map.chunkToTurrets) do
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local base = findNearbyBase(map, chunk)
if not base then
base = createBase(map, chunk, tick)
@ -384,7 +382,7 @@ function chunkUtils.addBasesToAllEnemyStructures(universe, tick)
setChunkBase(map, chunk, base)
end
for chunkId in pairs(map.chunkToHives) do
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local base = findNearbyBase(map, chunk)
if not base then
base = createBase(map, chunk, tick)
@ -392,7 +390,7 @@ function chunkUtils.addBasesToAllEnemyStructures(universe, tick)
setChunkBase(map, chunk, base)
end
for chunkId in pairs(map.chunkToUtilities) do
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local base = findNearbyBase(map, chunk)
if not base then
base = createBase(map, chunk, tick)
@ -400,7 +398,7 @@ function chunkUtils.addBasesToAllEnemyStructures(universe, tick)
setChunkBase(map, chunk, base)
end
for chunkId in pairs(map.chunkToTraps) do
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local base = findNearbyBase(map, chunk)
if not base then
base = createBase(map, chunk, tick)
@ -411,13 +409,13 @@ function chunkUtils.addBasesToAllEnemyStructures(universe, tick)
end
end
function chunkUtils.entityForPassScan(map, entity)
function ChunkUtils.entityForPassScan(map, entity)
local overlapArray = getEntityOverlapChunks(map, entity)
for i=1,#overlapArray do
local chunk = overlapArray[i]
if (chunk ~= -1) and not map.universe.chunkToPassScan[chunk.id] then
map.universe.chunkToPassScan[chunk.id] = {
if (chunk ~= -1) and not map.Universe.chunkToPassScan[chunk.id] then
map.Universe.chunkToPassScan[chunk.id] = {
map = map,
chunk = chunk
}
@ -425,18 +423,18 @@ function chunkUtils.entityForPassScan(map, entity)
end
end
local function newChunkId(universe)
local id = universe.chunkId
universe.chunkId = universe.chunkId + 1
local function newChunkId()
local id = Universe.chunkId
Universe.chunkId = Universe.chunkId + 1
return id
end
function chunkUtils.createChunk(map, topX, topY)
function ChunkUtils.createChunk(map, topX, topY)
local chunk = {
x = topX,
y = topY,
dOrigin = euclideanDistancePoints(topX, topY, 0, 0),
id = newChunkId(map.universe)
id = newChunkId()
}
chunk[BASE_PHEROMONE] = 0
chunk[PLAYER_PHEROMONE] = 0
@ -447,7 +445,7 @@ function chunkUtils.createChunk(map, topX, topY)
return chunk
end
function chunkUtils.colorChunk(chunk, surface, color, ttl)
function ChunkUtils.colorChunk(chunk, surface, color, ttl)
local lx = math.floor(chunk.x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
local ly = math.floor(chunk.y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
@ -464,7 +462,7 @@ function chunkUtils.colorChunk(chunk, surface, color, ttl)
})
end
function chunkUtils.colorXY(x, y, surface, color)
function ChunkUtils.colorXY(x, y, surface, color)
local lx = math.floor(x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
local ly = math.floor(y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
@ -481,7 +479,7 @@ function chunkUtils.colorXY(x, y, surface, color)
})
end
function chunkUtils.registerEnemyBaseStructure(map, entity, base, skipCount)
function ChunkUtils.registerEnemyBaseStructure(map, entity, base, skipCount)
local entityType = entity.type
local addFunc
@ -525,7 +523,7 @@ function chunkUtils.registerEnemyBaseStructure(map, entity, base, skipCount)
end
end
function chunkUtils.unregisterEnemyBaseStructure(map, entity, damageTypeName, skipCount)
function ChunkUtils.unregisterEnemyBaseStructure(map, entity, damageTypeName, skipCount)
local entityType = entity.type
local removeFunc
@ -580,7 +578,7 @@ function chunkUtils.unregisterEnemyBaseStructure(map, entity, damageTypeName, sk
end
end
function chunkUtils.accountPlayerEntity(entity, map, addObject, base)
function ChunkUtils.accountPlayerEntity(entity, map, addObject, base)
if (BUILDING_PHEROMONES[entity.type] ~= nil) and (entity.force.name ~= "enemy") then
local entityValue = BUILDING_PHEROMONES[entity.type]
local overlapArray = getEntityOverlapChunks(map, entity)
@ -612,7 +610,7 @@ function chunkUtils.accountPlayerEntity(entity, map, addObject, base)
return entity
end
function chunkUtils.unregisterResource(entity, map)
function ChunkUtils.unregisterResource(entity, map)
if entity.prototype.infinite_resource then
return
end
@ -626,7 +624,7 @@ function chunkUtils.unregisterResource(entity, map)
end
end
function chunkUtils.registerResource(entity, map)
function ChunkUtils.registerResource(entity, map)
local overlapArray = getEntityOverlapChunks(map, entity)
for i=1,#overlapArray do
@ -637,7 +635,7 @@ function chunkUtils.registerResource(entity, map)
end
end
function chunkUtils.makeImmortalEntity(surface, entity)
function ChunkUtils.makeImmortalEntity(surface, entity)
local repairPosition = entity.position
local repairName = entity.name
local repairForce = entity.force
@ -673,5 +671,10 @@ function chunkUtils.makeImmortalEntity(surface, entity)
newEntity.destructible = false
end
chunkUtilsG = chunkUtils
return chunkUtils
function ChunkUtils.init(universe)
Universe = universe
ChunkOverlapArray = universe.chunkOverlapArray
end
ChunkUtilsG = ChunkUtils
return ChunkUtils

View File

@ -14,93 +14,93 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if mapProcessorG then
return mapProcessorG
if MapProcessorG then
return MapProcessorG
end
local mapProcessor = {}
local MapProcessor = {}
--
local Universe
-- imports
local queryUtils = require("QueryUtils")
local pheromoneUtils = require("PheromoneUtils")
local aiAttackWave = require("AIAttackWave")
local aiPredicates = require("AIPredicates")
local constants = require("Constants")
local mapUtils = require("MapUtils")
local playerUtils = require("PlayerUtils")
local chunkUtils = require("ChunkUtils")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local baseUtils = require("BaseUtils")
local QueryUtils = require("QueryUtils")
local PheromoneUtils = require("PheromoneUtils")
local AiAttackWave = require("AIAttackWave")
local AiPredicates = require("AIPredicates")
local Constants = require("Constants")
local MapUtils = require("MapUtils")
local PlayerUtils = require("PlayerUtils")
local ChunkUtils = require("ChunkUtils")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
local BaseUtils = require("BaseUtils")
-- constants
-- Constants
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
local DURATION_ACTIVE_NEST = constants.DURATION_ACTIVE_NEST
local PLAYER_PHEROMONE_GENERATOR_AMOUNT = Constants.PLAYER_PHEROMONE_GENERATOR_AMOUNT
local DURATION_ACTIVE_NEST = Constants.DURATION_ACTIVE_NEST
local PROCESS_QUEUE_SIZE = constants.PROCESS_QUEUE_SIZE
local RESOURCE_QUEUE_SIZE = constants.RESOURCE_QUEUE_SIZE
local ENEMY_QUEUE_SIZE = constants.ENEMY_QUEUE_SIZE
local PLAYER_QUEUE_SIZE = constants.PLAYER_QUEUE_SIZE
local PROCESS_QUEUE_SIZE = Constants.PROCESS_QUEUE_SIZE
local RESOURCE_QUEUE_SIZE = Constants.RESOURCE_QUEUE_SIZE
local ENEMY_QUEUE_SIZE = Constants.ENEMY_QUEUE_SIZE
local PLAYER_QUEUE_SIZE = Constants.PLAYER_QUEUE_SIZE
local CLEANUP_QUEUE_SIZE = constants.CLEANUP_QUEUE_SIZE
local CLEANUP_QUEUE_SIZE = Constants.CLEANUP_QUEUE_SIZE
local PROCESS_PLAYER_BOUND = constants.PROCESS_PLAYER_BOUND
local CHUNK_TICK = constants.CHUNK_TICK
local PROCESS_PLAYER_BOUND = Constants.PROCESS_PLAYER_BOUND
local PROCESS_STATIC_QUEUE_SIZE = constants.PROCESS_STATIC_QUEUE_SIZE
local AI_VENGENCE_SQUAD_COST = Constants.AI_VENGENCE_SQUAD_COST
local AI_VENGENCE_SQUAD_COST = constants.AI_VENGENCE_SQUAD_COST
local BASE_AI_STATE_AGGRESSIVE = Constants.BASE_AI_STATE_AGGRESSIVE
local BASE_AI_STATE_SIEGE = Constants.BASE_AI_STATE_SIEGE
local BASE_AI_STATE_PEACEFUL = Constants.BASE_AI_STATE_PEACEFUL
local BASE_AI_STATE_MIGRATING = Constants.BASE_AI_STATE_MIGRATING
local BASE_AI_STATE_AGGRESSIVE = constants.BASE_AI_STATE_AGGRESSIVE
local BASE_AI_STATE_SIEGE = constants.BASE_AI_STATE_SIEGE
local BASE_AI_STATE_PEACEFUL = constants.BASE_AI_STATE_PEACEFUL
local BASE_AI_STATE_MIGRATING = constants.BASE_AI_STATE_MIGRATING
local COOLDOWN_DRAIN = constants.COOLDOWN_DRAIN
local COOLDOWN_RALLY = constants.COOLDOWN_RALLY
local COOLDOWN_RETREAT = constants.COOLDOWN_RETREAT
local COOLDOWN_DRAIN = Constants.COOLDOWN_DRAIN
local COOLDOWN_RALLY = Constants.COOLDOWN_RALLY
local COOLDOWN_RETREAT = Constants.COOLDOWN_RETREAT
-- imported functions
local setPositionInQuery = queryUtils.setPositionInQuery
local setPositionInQuery = QueryUtils.setPositionInQuery
local addPlayerGenerator = chunkPropertyUtils.addPlayerGenerator
local findNearbyBase = chunkPropertyUtils.findNearbyBase
local addPlayerGenerator = ChunkPropertyUtils.addPlayerGenerator
local findNearbyBase = ChunkPropertyUtils.findNearbyBase
local removeChunkToNest = mapUtils.removeChunkToNest
local removeChunkToNest = MapUtils.removeChunkToNest
local processStaticPheromone = pheromoneUtils.processStaticPheromone
local processPheromone = pheromoneUtils.processPheromone
local processPheromone = PheromoneUtils.processPheromone
local getCombinedDeathGeneratorRating = chunkPropertyUtils.getCombinedDeathGeneratorRating
local processBaseMutation = baseUtils.processBaseMutation
local getCombinedDeathGeneratorRating = ChunkPropertyUtils.getCombinedDeathGeneratorRating
local processBaseMutation = BaseUtils.processBaseMutation
local processNestActiveness = chunkPropertyUtils.processNestActiveness
local getChunkBase = chunkPropertyUtils.getChunkBase
local processNestActiveness = ChunkPropertyUtils.processNestActiveness
local getChunkBase = ChunkPropertyUtils.getChunkBase
local formSquads = aiAttackWave.formSquads
local formVengenceSquad = aiAttackWave.formVengenceSquad
local formVengenceSettler = aiAttackWave.formVengenceSettler
local formSettlers = aiAttackWave.formSettlers
local formSquads = AiAttackWave.formSquads
local formVengenceSquad = AiAttackWave.formVengenceSquad
local formVengenceSettler = AiAttackWave.formVengenceSettler
local formSettlers = AiAttackWave.formSettlers
local getChunkByPosition = mapUtils.getChunkByPosition
local getChunkByXY = mapUtils.getChunkByXY
local getChunkById = mapUtils.getChunkById
local getChunkByPosition = MapUtils.getChunkByPosition
local getChunkByXY = MapUtils.getChunkByXY
local getChunkById = MapUtils.getChunkById
local validPlayer = playerUtils.validPlayer
local validPlayer = PlayerUtils.validPlayer
local mapScanEnemyChunk = chunkUtils.mapScanEnemyChunk
local mapScanPlayerChunk = chunkUtils.mapScanPlayerChunk
local mapScanResourceChunk = chunkUtils.mapScanResourceChunk
local mapScanEnemyChunk = ChunkUtils.mapScanEnemyChunk
local mapScanPlayerChunk = ChunkUtils.mapScanPlayerChunk
local mapScanResourceChunk = ChunkUtils.mapScanResourceChunk
local getNestCount = chunkPropertyUtils.getNestCount
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
local getNestActiveness = chunkPropertyUtils.getNestActiveness
local getNestCount = ChunkPropertyUtils.getNestCount
local getEnemyStructureCount = ChunkPropertyUtils.getEnemyStructureCount
local getNestActiveness = ChunkPropertyUtils.getNestActiveness
local getRaidNestActiveness = chunkPropertyUtils.getRaidNestActiveness
local getRaidNestActiveness = ChunkPropertyUtils.getRaidNestActiveness
local canAttack = aiPredicates.canAttack
local canMigrate = aiPredicates.canMigrate
local canAttack = AiPredicates.canAttack
local canMigrate = AiPredicates.canMigrate
local tableSize = table_size
@ -118,7 +118,7 @@ local next = next
In theory, this might be fine as smaller bases have less surface to attack and need to have
pheromone dissipate at a faster rate.
--]]
function mapProcessor.processMap(map, tick)
function MapProcessor.processMap(map, tick)
local processQueue = map.processQueue
local processQueueLength = #processQueue
@ -148,7 +148,7 @@ function mapProcessor.processMap(map, tick)
map.processIndex = endIndex - 1
end
end
map.universe.processedChunks = map.universe.processedChunks + ((startIndex - endIndex) * step)
Universe.processedChunks = Universe.processedChunks + ((startIndex - endIndex) * step)
for x=startIndex,endIndex,step do
processPheromone(map, processQueue[x], tick)
@ -156,11 +156,11 @@ function mapProcessor.processMap(map, tick)
end
local function queueNestSpawners(map, chunk, tick)
local processActiveNest = map.universe.processActiveNest
local processActiveNest = Universe.processActiveNest
local chunkId = chunk.id
if not processActiveNest[chunkId] then
if (getNestActiveness(map, chunk) > 0) or (getRaidNestActiveness(map, chunk) > 0) then
if (getNestActiveness(chunk) > 0) or (getRaidNestActiveness(chunk) > 0) then
processActiveNest[chunkId] = {
map = map,
chunk = chunk,
@ -176,7 +176,7 @@ end
vs
the slower passive version processing the entire map in multiple passes.
--]]
function mapProcessor.processPlayers(players, universe, tick)
function MapProcessor.processPlayers(players, tick)
-- put down player pheromone for player hunters
-- randomize player order to ensure a single player isn't singled out
-- not looping everyone because the cost is high enough already in multiplayer
@ -186,7 +186,7 @@ function mapProcessor.processPlayers(players, universe, tick)
local player = players[i]
if validPlayer(player) then
local char = player.character
local map = universe.maps[char.surface.index]
local map = Universe.maps[char.surface.index]
if map then
local playerChunk = getChunkByPosition(map, char.position)
@ -198,10 +198,10 @@ function mapProcessor.processPlayers(players, universe, tick)
end
if (#players > 0) then
local player = players[universe.random(#players)]
local player = players[Universe.random(#players)]
if validPlayer(player) then
local char = player.character
local map = universe.maps[char.surface.index]
local map = Universe.maps[char.surface.index]
if map then
local playerChunk = getChunkByPosition(map, char.position)
@ -214,7 +214,7 @@ function mapProcessor.processPlayers(players, universe, tick)
local vengence = allowingAttacks and
(base.unitPoints >= AI_VENGENCE_SQUAD_COST) and
((getEnemyStructureCount(map, playerChunk) > 0) or
(getCombinedDeathGeneratorRating(map, playerChunk) < universe.retreatThreshold))
(getCombinedDeathGeneratorRating(map, playerChunk) < Universe.retreatThreshold))
for x=playerChunk.x - PROCESS_PLAYER_BOUND, playerChunk.x + PROCESS_PLAYER_BOUND, 32 do
for y=playerChunk.y - PROCESS_PLAYER_BOUND, playerChunk.y + PROCESS_PLAYER_BOUND, 32 do
@ -223,19 +223,19 @@ function mapProcessor.processPlayers(players, universe, tick)
if (chunk ~= -1) then
processPheromone(map, chunk, tick, true)
if (getNestCount(map, chunk) > 0) then
if (getNestCount(chunk) > 0) then
processNestActiveness(map, chunk)
queueNestSpawners(map, chunk, tick)
if vengence then
local pack = universe.vengenceQueue[chunk.id]
local pack = Universe.vengenceQueue[chunk.id]
if not pack then
pack = {
v = 0,
map = map,
base = base
}
universe.vengenceQueue[chunk.id] = pack
Universe.vengenceQueue[chunk.id] = pack
end
pack.v = pack.v + 1
end
@ -249,8 +249,8 @@ function mapProcessor.processPlayers(players, universe, tick)
end
end
local function processCleanUp(universe, chunks, iterator, tick, duration)
local chunkId = universe[iterator]
local function processCleanUp(chunks, iterator, tick, duration)
local chunkId = Universe[iterator]
local chunkPack
if not chunkId then
chunkId, chunkPack = next(chunks, nil)
@ -258,33 +258,33 @@ local function processCleanUp(universe, chunks, iterator, tick, duration)
chunkPack = chunks[chunkId]
end
if not chunkId then
universe[iterator] = nil
Universe[iterator] = nil
else
universe[iterator] = next(chunks, chunkId)
Universe[iterator] = next(chunks, chunkId)
if (tick - chunkPack.tick) > duration then
chunks[chunkId] = nil
end
end
end
function mapProcessor.cleanUpMapTables(universe, tick)
local retreats = universe.chunkToRetreats
local rallys = universe.chunkToRallys
local drained = universe.chunkToDrained
function MapProcessor.cleanUpMapTables(tick)
local retreats = Universe.chunkToRetreats
local rallys = Universe.chunkToRallys
local drained = Universe.chunkToDrained
for _=1,CLEANUP_QUEUE_SIZE do
processCleanUp(universe, retreats, "chunkToRetreatIterator", tick, COOLDOWN_RETREAT)
processCleanUp(retreats, "chunkToRetreatIterator", tick, COOLDOWN_RETREAT)
processCleanUp(universe, rallys, "chunkToRallyIterator", tick, COOLDOWN_RALLY)
processCleanUp(rallys, "chunkToRallyIterator", tick, COOLDOWN_RALLY)
processCleanUp(universe, drained, "chunkToDrainedIterator", tick, COOLDOWN_DRAIN)
processCleanUp(drained, "chunkToDrainedIterator", tick, COOLDOWN_DRAIN)
end
end
--[[
Passive scan to find entities that have been generated outside the factorio event system
--]]
function mapProcessor.scanPlayerMap(map, tick)
function MapProcessor.scanPlayerMap(map, tick)
if (map.nextProcessMap == tick) or (map.nextPlayerScan == tick) or
(map.nextEnemyScan == tick) or (map.nextChunkProcess == tick)
then
@ -312,7 +312,7 @@ function mapProcessor.scanPlayerMap(map, tick)
end
end
function mapProcessor.scanEnemyMap(map, tick)
function MapProcessor.scanEnemyMap(map, tick)
if (map.nextProcessMap == tick) or (map.nextPlayerScan == tick) or (map.nextChunkProcess == tick) then
return
end
@ -339,7 +339,7 @@ function mapProcessor.scanEnemyMap(map, tick)
end
end
function mapProcessor.scanResourceMap(map, tick)
function MapProcessor.scanResourceMap(map, tick)
if (map.nextProcessMap == tick) or (map.nextPlayerScan == tick) or
(map.nextEnemyScan == tick) or (map.nextChunkProcess == tick)
then
@ -367,9 +367,9 @@ function mapProcessor.scanResourceMap(map, tick)
end
end
function mapProcessor.processActiveNests(universe, tick)
local processActiveNest = universe.processActiveNest
local chunkId = universe.processActiveNestIterator
function MapProcessor.processActiveNests(tick)
local processActiveNest = Universe.processActiveNest
local chunkId = Universe.processActiveNestIterator
local chunkPack
if not chunkId then
chunkId, chunkPack = next(processActiveNest, nil)
@ -377,9 +377,9 @@ function mapProcessor.processActiveNests(universe, tick)
chunkPack = processActiveNest[chunkId]
end
if not chunkId then
universe.processActiveNestIterator = nil
Universe.processActiveNestIterator = nil
else
universe.processActiveNestIterator = next(processActiveNest, chunkId)
Universe.processActiveNestIterator = next(processActiveNest, chunkId)
if chunkPack.tick < tick then
local map = chunkPack.map
if not map.surface.valid then
@ -388,7 +388,7 @@ function mapProcessor.processActiveNests(universe, tick)
end
local chunk = chunkPack.chunk
processNestActiveness(map, chunk)
if (getNestActiveness(map, chunk) == 0) and (getRaidNestActiveness(map, chunk) == 0) then
if (getNestActiveness(chunk) == 0) and (getRaidNestActiveness(chunk) == 0) then
processActiveNest[chunkId] = nil
else
chunkPack.tick = tick + DURATION_ACTIVE_NEST
@ -397,9 +397,9 @@ function mapProcessor.processActiveNests(universe, tick)
end
end
function mapProcessor.processVengence(universe)
local vengenceQueue = universe.vengenceQueue
local chunkId = universe.deployVengenceIterator
function MapProcessor.processVengence()
local vengenceQueue = Universe.vengenceQueue
local chunkId = Universe.deployVengenceIterator
local vengencePack
if not chunkId then
chunkId, vengencePack = next(vengenceQueue, nil)
@ -407,20 +407,20 @@ function mapProcessor.processVengence(universe)
vengencePack = vengenceQueue[chunkId]
end
if not chunkId then
universe.deployVengenceIterator = nil
Universe.deployVengenceIterator = nil
if (tableSize(vengenceQueue) == 0) then
universe.vengenceQueue = {}
Universe.vengenceQueue = {}
end
else
universe.deployVengenceIterator = next(vengenceQueue, chunkId)
Universe.deployVengenceIterator = next(vengenceQueue, chunkId)
vengenceQueue[chunkId] = nil
local map = vengencePack.map
if not map.surface.valid then
return
end
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local base = vengencePack.base
if canMigrate(map, base) and (universe.random() < 0.075) then
if canMigrate(map, base) and (Universe.random() < 0.075) then
formVengenceSettler(map, chunk, base)
else
formVengenceSquad(map, chunk, base)
@ -428,28 +428,28 @@ function mapProcessor.processVengence(universe)
end
end
function mapProcessor.processNests(universe, tick)
local chunkId = universe.processNestIterator
function MapProcessor.processNests(tick)
local chunkId = Universe.processNestIterator
local chunkPack
if not chunkId then
chunkId,chunkPack = next(universe.chunkToNests, nil)
chunkId,chunkPack = next(Universe.chunkToNests, nil)
else
chunkPack = universe.chunkToNests[chunkId]
chunkPack = Universe.chunkToNests[chunkId]
end
if not chunkId then
universe.processNestIterator = nil
Universe.processNestIterator = nil
else
universe.processNestIterator = next(universe.chunkToNests, chunkId)
Universe.processNestIterator = next(Universe.chunkToNests, chunkId)
local map = chunkPack.map
if not map.surface.valid then
removeChunkToNest(universe, chunkId)
removeChunkToNest(chunkId)
return
end
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
processNestActiveness(map, chunk)
queueNestSpawners(map, chunk, tick)
if universe.NEW_ENEMIES then
if Universe.NEW_ENEMIES then
processBaseMutation(chunk,
map,
getChunkBase(map, chunk))
@ -457,8 +457,8 @@ function mapProcessor.processNests(universe, tick)
end
end
local function processSpawnersBody(universe, iterator, chunks)
local chunkId = universe[iterator]
local function processSpawnersBody(iterator, chunks)
local chunkId = Universe[iterator]
local chunkPack
if not chunkId then
chunkId,chunkPack = next(chunks, nil)
@ -466,19 +466,19 @@ local function processSpawnersBody(universe, iterator, chunks)
chunkPack = chunks[chunkId]
end
if not chunkId then
universe[iterator] = nil
Universe[iterator] = nil
else
universe[iterator] = next(chunks, chunkId)
Universe[iterator] = next(chunks, chunkId)
local map = chunkPack.map
if not map.surface.valid then
if (iterator == "processMigrationIterator") then
removeChunkToNest(universe, chunkId)
removeChunkToNest(chunkId)
else
chunks[chunkId] = nil
end
return
end
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local base = findNearbyBase(map, chunk)
if base.stateAI == BASE_AI_STATE_PEACEFUL then
return
@ -508,34 +508,35 @@ local function processSpawnersBody(universe, iterator, chunks)
end
end
function mapProcessor.processAttackWaves(universe)
processSpawnersBody(universe,
"processActiveSpawnerIterator",
universe.chunkToActiveNest)
processSpawnersBody(universe,
"processActiveRaidSpawnerIterator",
universe.chunkToActiveRaidNest)
processSpawnersBody(universe,
"processMigrationIterator",
universe.chunkToNests)
function MapProcessor.processAttackWaves()
processSpawnersBody("processActiveSpawnerIterator",
Universe.chunkToActiveNest)
processSpawnersBody("processActiveRaidSpawnerIterator",
Universe.chunkToActiveRaidNest)
processSpawnersBody("processMigrationIterator",
Universe.chunkToNests)
end
function mapProcessor.processClouds(universe, tick)
local len = universe.settlePurpleCloud.len
local builderPack = universe.settlePurpleCloud[len]
function MapProcessor.processClouds(tick)
local len = Universe.settlePurpleCloud.len
local builderPack = Universe.settlePurpleCloud[len]
if builderPack and (builderPack.tick <= tick) then
universe.settlePurpleCloud[len] = nil
universe.settlePurpleCloud.len = len - 1
Universe.settlePurpleCloud[len] = nil
Universe.settlePurpleCloud.len = len - 1
local map = builderPack.map
if builderPack.group.valid and map.surface.valid then
setPositionInQuery(
universe.obaCreateBuildCloudQuery,
Universe.obaCreateBuildCloudQuery,
builderPack.position
)
map.surface.create_entity(universe.obaCreateBuildCloudQuery)
map.surface.create_entity(Universe.obaCreateBuildCloudQuery)
end
end
end
mapProcessorG = mapProcessor
return mapProcessor
function MapProcessor.init(universe)
Universe = universe
end
MapProcessorG = MapProcessor
return MapProcessor

View File

@ -14,37 +14,42 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if mapUtilsG then
return mapUtilsG
if MapUtilsG then
return MapUtilsG
end
local mapUtils = {}
local MapUtils = {}
--
local Universe
local NeighborChunks
-- imports
local constants = require("Constants")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local Constants = require("Constants")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
-- Constants
local CHUNK_NORTH_SOUTH = constants.CHUNK_NORTH_SOUTH
local CHUNK_EAST_WEST = constants.CHUNK_EAST_WEST
local CHUNK_IMPASSABLE = constants.CHUNK_IMPASSABLE
local CHUNK_ALL_DIRECTIONS = constants.CHUNK_ALL_DIRECTIONS
local CHUNK_NORTH_SOUTH = Constants.CHUNK_NORTH_SOUTH
local CHUNK_EAST_WEST = Constants.CHUNK_EAST_WEST
local CHUNK_IMPASSABLE = Constants.CHUNK_IMPASSABLE
local CHUNK_ALL_DIRECTIONS = Constants.CHUNK_ALL_DIRECTIONS
local CHUNK_SIZE = constants.CHUNK_SIZE
local CHUNK_SIZE = Constants.CHUNK_SIZE
local CHUNK_SIZE_DIVIDER = constants.CHUNK_SIZE_DIVIDER
local CHUNK_SIZE_DIVIDER = Constants.CHUNK_SIZE_DIVIDER
-- imported functions
local mFloor = math.floor
local getPassable = chunkPropertyUtils.getPassable
local getPassable = ChunkPropertyUtils.getPassable
local tRemove = table.remove
local mCeil = math.ceil
-- module code
function mapUtils.getChunkByXY(map, x, y)
function MapUtils.getChunkByXY(map, x, y)
local chunkX = map[x]
if chunkX then
return chunkX[y] or -1
@ -52,7 +57,7 @@ function mapUtils.getChunkByXY(map, x, y)
return -1
end
function mapUtils.getChunkByPosition(map, position)
function MapUtils.getChunkByPosition(map, position)
local chunkX = map[mFloor(position.x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE]
if chunkX then
local chunkY = mFloor(position.y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
@ -61,50 +66,50 @@ function mapUtils.getChunkByPosition(map, position)
return -1
end
function mapUtils.getChunkById(map, chunkId)
return map.universe.chunkIdToChunk[chunkId] or -1
function MapUtils.getChunkById(chunkId)
return Universe.chunkIdToChunk[chunkId] or -1
end
function mapUtils.positionToChunkXY(position)
function MapUtils.positionToChunkXY(position)
local chunkX = mFloor(position.x * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
local chunkY = mFloor(position.y * CHUNK_SIZE_DIVIDER) * CHUNK_SIZE
return chunkX, chunkY
end
function mapUtils.queueGeneratedChunk(universe, event)
local map = universe.maps[event.surface.index]
function MapUtils.queueGeneratedChunk(event)
local map = Universe.maps[event.surface.index]
if not map then
return
end
event.tick = (event.tick or game.tick) + 20
event.id = universe.eventId
event.id = Universe.eventId
event.map = map
universe.pendingChunks[event.id] = event
universe.eventId = universe.eventId + 1
Universe.pendingChunks[event.id] = event
Universe.eventId = Universe.eventId + 1
end
function mapUtils.nextMap(universe)
local mapIterator = universe.mapIterator
function MapUtils.nextMap()
local mapIterator = Universe.mapIterator
repeat
local map
universe.mapIterator, map = next(universe.maps, universe.mapIterator)
Universe.mapIterator, map = next(Universe.maps, Universe.mapIterator)
if map and map.activeSurface then
return map
end
until mapIterator == universe.mapIterator
until mapIterator == Universe.mapIterator
end
function mapUtils.removeChunkToNest(universe, chunkId)
universe.chunkToNests[chunkId] = nil
if (chunkId == universe.processNestIterator) then
universe.processNestIterator = nil
function MapUtils.removeChunkToNest(chunkId)
Universe.chunkToNests[chunkId] = nil
if (chunkId == Universe.processNestIterator) then
Universe.processNestIterator = nil
end
if (chunkId == universe.processMigrationIterator) then
universe.processMigrationIterator = nil
if (chunkId == Universe.processMigrationIterator) then
Universe.processMigrationIterator = nil
end
end
function mapUtils.findInsertionPoint(processQueue, chunk)
function MapUtils.findInsertionPoint(processQueue, chunk)
local low = 1
local high = #processQueue
local pivot
@ -120,8 +125,8 @@ function mapUtils.findInsertionPoint(processQueue, chunk)
return low
end
function mapUtils.removeProcessQueueChunk(processQueue, chunk)
local insertionPoint = mapUtils.findInsertionPoint(processQueue, chunk)
function MapUtils.removeProcessQueueChunk(processQueue, chunk)
local insertionPoint = MapUtils.findInsertionPoint(processQueue, chunk)
if insertionPoint > #processQueue then
insertionPoint = insertionPoint - 1
end
@ -136,24 +141,23 @@ function mapUtils.removeProcessQueueChunk(processQueue, chunk)
end
end
function mapUtils.removeChunkFromMap(map, chunk)
function MapUtils.removeChunkFromMap(map, chunk)
local chunkId = chunk.id
local x = chunk.x
local y = chunk.y
mapUtils.removeProcessQueueChunk(map.processQueue, chunk)
local universe = map.universe
MapUtils.removeProcessQueueChunk(map.processQueue, chunk)
map[x][y] = nil
universe.chunkIdToChunk[chunkId] = nil
universe.chunkToActiveNest[chunkId] = nil
universe.chunkToActiveRaidNest[chunkId] = nil
universe.chunkToDrained[chunkId] = nil
universe.chunkToRetreats[chunkId] = nil
universe.chunkToRallys[chunkId] = nil
universe.chunkToPassScan[chunkId] = nil
universe.chunkToNests[chunkId] = nil
universe.vengenceQueue[chunkId] = nil
universe.processActiveNest[chunkId] = nil
universe.chunkToVictory[chunkId] = nil
Universe.chunkIdToChunk[chunkId] = nil
Universe.chunkToActiveNest[chunkId] = nil
Universe.chunkToActiveRaidNest[chunkId] = nil
Universe.chunkToDrained[chunkId] = nil
Universe.chunkToRetreats[chunkId] = nil
Universe.chunkToRallys[chunkId] = nil
Universe.chunkToPassScan[chunkId] = nil
Universe.chunkToNests[chunkId] = nil
Universe.vengenceQueue[chunkId] = nil
Universe.processActiveNest[chunkId] = nil
Universe.chunkToVictory[chunkId] = nil
local base = map.chunkToBase[chunkId]
if base then
base.chunkCount = base.chunkCount - 1
@ -176,38 +180,38 @@ function mapUtils.removeChunkFromMap(map, chunk)
map.chunkToPathRating[chunkId] = nil
map.chunkToDeathGenerator[chunkId] = nil
if universe.processActiveNestIterator == chunkId then
universe.processActiveNestIterator = nil
if Universe.processActiveNestIterator == chunkId then
Universe.processActiveNestIterator = nil
end
if universe.victoryScentIterator == chunkId then
universe.victoryScentIterator = nil
if Universe.victoryScentIterator == chunkId then
Universe.victoryScentIterator = nil
end
if universe.processNestIterator == chunkId then
universe.processNestIterator = nil
if Universe.processNestIterator == chunkId then
Universe.processNestIterator = nil
end
if universe.chunkToDrainedIterator == chunkId then
universe.chunkToDrainedIterator = nil
if Universe.chunkToDrainedIterator == chunkId then
Universe.chunkToDrainedIterator = nil
end
if universe.chunkToRetreatIterator == chunkId then
universe.chunkToRetreatIterator = nil
if Universe.chunkToRetreatIterator == chunkId then
Universe.chunkToRetreatIterator = nil
end
if universe.chunkToRallyIterator == chunkId then
universe.chunkToRallyIterator = nil
if Universe.chunkToRallyIterator == chunkId then
Universe.chunkToRallyIterator = nil
end
if universe.chunkToPassScanIterator == chunkId then
universe.chunkToPassScanIterator = nil
if Universe.chunkToPassScanIterator == chunkId then
Universe.chunkToPassScanIterator = nil
end
if universe.processActiveSpawnerIterator == chunkId then
universe.processActiveSpawnerIterator = nil
if Universe.processActiveSpawnerIterator == chunkId then
Universe.processActiveSpawnerIterator = nil
end
if universe.processActiveRaidSpawnerIterator == chunkId then
universe.processActiveRaidSpawnerIterator = nil
if Universe.processActiveRaidSpawnerIterator == chunkId then
Universe.processActiveRaidSpawnerIterator = nil
end
if universe.processMigrationIterator == chunkId then
universe.processMigrationIterator = nil
if Universe.processMigrationIterator == chunkId then
Universe.processMigrationIterator = nil
end
if universe.deployVengenceIterator == chunkId then
universe.deployVengenceIterator = nil
if Universe.deployVengenceIterator == chunkId then
Universe.deployVengenceIterator = nil
end
end
@ -218,41 +222,40 @@ end
/|\
6 7 8
]]--
function mapUtils.getNeighborChunks(map, x, y)
local neighbors = map.universe.neighbors
function MapUtils.getNeighborChunks(map, x, y)
local chunkYRow1 = y - CHUNK_SIZE
local chunkYRow3 = y + CHUNK_SIZE
local xChunks = map[x-CHUNK_SIZE]
if xChunks then
neighbors[1] = xChunks[chunkYRow1] or -1
neighbors[4] = xChunks[y] or -1
neighbors[6] = xChunks[chunkYRow3] or -1
NeighborChunks[1] = xChunks[chunkYRow1] or -1
NeighborChunks[4] = xChunks[y] or -1
NeighborChunks[6] = xChunks[chunkYRow3] or -1
else
neighbors[1] = -1
neighbors[4] = -1
neighbors[6] = -1
NeighborChunks[1] = -1
NeighborChunks[4] = -1
NeighborChunks[6] = -1
end
xChunks = map[x+CHUNK_SIZE]
if xChunks then
neighbors[3] = xChunks[chunkYRow1] or -1
neighbors[5] = xChunks[y] or -1
neighbors[8] = xChunks[chunkYRow3] or -1
NeighborChunks[3] = xChunks[chunkYRow1] or -1
NeighborChunks[5] = xChunks[y] or -1
NeighborChunks[8] = xChunks[chunkYRow3] or -1
else
neighbors[3] = -1
neighbors[5] = -1
neighbors[8] = -1
NeighborChunks[3] = -1
NeighborChunks[5] = -1
NeighborChunks[8] = -1
end
xChunks = map[x]
if xChunks then
neighbors[2] = xChunks[chunkYRow1] or -1
neighbors[7] = xChunks[chunkYRow3] or -1
NeighborChunks[2] = xChunks[chunkYRow1] or -1
NeighborChunks[7] = xChunks[chunkYRow3] or -1
else
neighbors[2] = -1
neighbors[7] = -1
NeighborChunks[2] = -1
NeighborChunks[7] = -1
end
return neighbors
return NeighborChunks
end
@ -263,7 +266,7 @@ end
/|\
6 7 8
]]--
function mapUtils.canMoveChunkDirection(map, direction, startChunk, endChunk)
function MapUtils.canMoveChunkDirection(map, direction, startChunk, endChunk)
local canMove = false
local startPassable = getPassable(map, startChunk)
local endPassable = getPassable(map, endChunk)
@ -293,8 +296,8 @@ function mapUtils.canMoveChunkDirection(map, direction, startChunk, endChunk)
return canMove
end
function mapUtils.getCardinalChunks(map, x, y)
local neighbors = map.universe.cardinalNeighbors
function MapUtils.getCardinalChunks(map, x, y)
local neighbors = Universe.cardinalNeighbors
local xChunks = map[x]
if xChunks then
neighbors[1] = xChunks[y-CHUNK_SIZE] or -1
@ -320,7 +323,7 @@ function mapUtils.getCardinalChunks(map, x, y)
return neighbors
end
function mapUtils.positionFromDirectionAndChunk(direction, startPosition, scaling)
function MapUtils.positionFromDirectionAndChunk(direction, startPosition, scaling)
local endPosition = {}
if (direction == 1) then
endPosition.x = startPosition.x - CHUNK_SIZE * (scaling - 0.1)
@ -350,7 +353,7 @@ function mapUtils.positionFromDirectionAndChunk(direction, startPosition, scalin
return endPosition
end
function mapUtils.positionFromDirectionAndFlat(direction, startPosition, multipler)
function MapUtils.positionFromDirectionAndFlat(direction, startPosition, multipler)
local lx = startPosition.x
local ly = startPosition.y
if not multipler then
@ -383,5 +386,10 @@ function mapUtils.positionFromDirectionAndFlat(direction, startPosition, multipl
}
end
mapUtilsG = mapUtils
return mapUtils
function MapUtils.init(universe)
Universe = universe
NeighborChunks = universe.neighbors
end
MapUtilsG = MapUtils
return MapUtils

View File

@ -14,10 +14,10 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if mathUtilsG then
return mathUtilsG
if MathUtilsG then
return MathUtilsG
end
local mathUtils = {}
local MathUtils = {}
-- imports
@ -35,26 +35,26 @@ local mAbs = math.abs
-- module code
function mathUtils.roundToFloor(number, multiple)
function MathUtils.roundToFloor(number, multiple)
return mFloor(number / multiple) * multiple
end
function mathUtils.roundToNearest(number, multiple)
function MathUtils.roundToNearest(number, multiple)
local num = number + (multiple * 0.5)
return num - (num % multiple)
end
function mathUtils.randomTickEvent(rg, tick, low, high)
return tick + mathUtils.randomTickDuration(rg, low, high)
function MathUtils.randomTickEvent(rg, tick, low, high)
return tick + MathUtils.randomTickDuration(rg, low, high)
end
function mathUtils.randomTickDuration(rg, low, high)
function MathUtils.randomTickDuration(rg, low, high)
local range = high - low
local minutesToTick = (range * rg()) + low
return mathUtils.roundToNearest(TICKS_A_MINUTE * minutesToTick, 1)
return MathUtils.roundToNearest(TICKS_A_MINUTE * minutesToTick, 1)
end
function mathUtils.distort(xorRandom, num, stdDev, min, max)
function MathUtils.distort(xorRandom, num, stdDev, min, max)
local amin = min or num * 0.70
local amax = max or num * 1.30
local sd = stdDev or 0.17
@ -63,14 +63,14 @@ function mathUtils.distort(xorRandom, num, stdDev, min, max)
amin = amax
amax = t
end
return mathUtils.roundToNearest(mathUtils.gaussianRandomRangeRG(num, num * sd, amin, amax, xorRandom), 0.01)
return MathUtils.roundToNearest(MathUtils.gaussianRandomRangeRG(num, num * sd, amin, amax, xorRandom), 0.01)
end
function mathUtils.linearInterpolation(percent, min, max)
function MathUtils.linearInterpolation(percent, min, max)
return ((max - min) * percent) + min
end
function mathUtils.xorRandom(state)
function MathUtils.xorRandom(state)
local xor = bit32.bxor
local lshift = bit32.lshift
local rshift = bit32.rshift
@ -88,7 +88,7 @@ end
--[[
Used for gaussian random numbers
--]]
function mathUtils.gaussianRandomRG(mean, std_dev, rg)
function MathUtils.gaussianRandomRG(mean, std_dev, rg)
-- marsagliaPolarMethod
local iid1
local iid2
@ -104,7 +104,7 @@ function mathUtils.gaussianRandomRG(mean, std_dev, rg)
return mean + (v * std_dev)
end
function mathUtils.gaussianRandomRangeRG(mean, std_dev, min, max, rg)
function MathUtils.gaussianRandomRangeRG(mean, std_dev, min, max, rg)
local r
if (min >= max) then
return min
@ -125,35 +125,35 @@ function mathUtils.gaussianRandomRangeRG(mean, std_dev, min, max, rg)
return r
end
function mathUtils.euclideanDistanceNamed(p1, p2)
function MathUtils.euclideanDistanceNamed(p1, p2)
local xs = p1.x - p2.x
local ys = p1.y - p2.y
return ((xs * xs) + (ys * ys)) ^ 0.5
end
function mathUtils.euclideanDistancePoints(x1, y1, x2, y2)
function MathUtils.euclideanDistancePoints(x1, y1, x2, y2)
local xs = x1 - x2
local ys = y1 - y2
return ((xs * xs) + (ys * ys)) ^ 0.5
end
function mathUtils.manhattenDistancePoints(x1, y1, x2, y2)
function MathUtils.manhattenDistancePoints(x1, y1, x2, y2)
return mAbs((x1 - x2) + (y1 - y2))
end
function mathUtils.euclideanDistanceArray(p1, p2)
function MathUtils.euclideanDistanceArray(p1, p2)
local xs = p1[1] - p2[1]
local ys = p1[2] - p2[2]
return ((xs * xs) + (ys * ys)) ^ 0.5
end
function mathUtils.distortPosition(rg, position, size)
local xDistort = mathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
local yDistort = mathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
function MathUtils.distortPosition(rg, position, size)
local xDistort = MathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
local yDistort = MathUtils.gaussianRandomRangeRG(1, 0.5, 0, 2, rg) - 1
position.x = position.x + (xDistort * size)
position.y = position.y + (yDistort * size)
return position
end
mathUtilsG = mathUtils
return mathUtils
MathUtilsG = MathUtils
return MathUtils

View File

@ -14,57 +14,61 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if movementUtilsG then
return movementUtilsG
if MovementUtilsG then
return MovementUtilsG
end
local movementUtils = {}
local MovementUtils = {}
--
local Universe
-- imports
local constants = require("Constants")
local mapUtils = require("MapUtils")
local mathUtils = require("MathUtils")
local unitGroupUtils = require("UnitGroupUtils")
local Constants = require("Constants")
local MapUtils = require("MapUtils")
local MathUtils = require("MathUtils")
local UnitGroupUtils = require("UnitGroupUtils")
-- constants
-- Constants
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
local MAGIC_MAXIMUM_NUMBER = Constants.MAGIC_MAXIMUM_NUMBER
local SQUAD_SETTLING = constants.SQUAD_SETTLING
local SQUAD_SETTLING = Constants.SQUAD_SETTLING
-- imported functions
local calculateSettlerMaxDistance = unitGroupUtils.calculateSettlerMaxDistance
local calculateSettlerMaxDistance = UnitGroupUtils.calculateSettlerMaxDistance
local canMoveChunkDirection = mapUtils.canMoveChunkDirection
local getNeighborChunks = mapUtils.getNeighborChunks
local canMoveChunkDirection = MapUtils.canMoveChunkDirection
local getNeighborChunks = MapUtils.getNeighborChunks
local tableRemove = table.remove
local tableInsert = table.insert
local distortPosition = mathUtils.distortPosition
local distortPosition = MathUtils.distortPosition
-- module code
function movementUtils.findMovementPosition(surface, position)
function MovementUtils.findMovementPosition(surface, position)
local pos = position
pos = surface.find_non_colliding_position("behemoth-biter", pos, 10, 2, false)
return pos
end
function movementUtils.findMovementPositionEntity(entityName, surface, position)
function MovementUtils.findMovementPositionEntity(entityName, surface, position)
local pos = position
pos = surface.find_non_colliding_position(entityName, pos, 5, 4, true)
return pos
end
function movementUtils.findMovementPositionDistort(surface, position)
function MovementUtils.findMovementPositionDistort(surface, position)
local pos = position
pos = surface.find_non_colliding_position("behemoth-biter", pos, 10, 2, false)
return distortPosition(pos, 8)
end
function movementUtils.addMovementPenalty(squad, chunk)
function MovementUtils.addMovementPenalty(squad, chunk)
if (chunk == -1) then
return
end
@ -75,13 +79,12 @@ function movementUtils.addMovementPenalty(squad, chunk)
if (penalty.c.id == chunk.id) then
penalty.v = penalty.v + 1
if penalty.v >= 15 then
local universe = squad.map.universe
if universe.enabledMigration and
(universe.builderCount < universe.AI_MAX_BUILDER_COUNT) then
if Universe.enabledMigration and
(Universe.builderCount < Universe.AI_MAX_BUILDER_COUNT) then
squad.settler = true
squad.originPosition.x = squad.group.position.x
squad.originPosition.y = squad.group.position.y
squad.maxDistance = calculateSettlerMaxDistance(universe)
squad.maxDistance = calculateSettlerMaxDistance()
squad.status = SQUAD_SETTLING
else
@ -103,7 +106,7 @@ end
--[[
Expects all neighbors adjacent to a chunk
--]]
function movementUtils.scoreNeighborsForAttack(map, chunk, neighborDirectionChunks, scoreFunction)
function MovementUtils.scoreNeighborsForAttack(map, chunk, neighborDirectionChunks, scoreFunction)
local highestChunk = -1
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
@ -149,7 +152,7 @@ end
--[[
Expects all neighbors adjacent to a chunk
--]]
function movementUtils.scoreNeighborsForSettling(map, chunk, neighborDirectionChunks, scoreFunction)
function MovementUtils.scoreNeighborsForSettling(map, chunk, neighborDirectionChunks, scoreFunction)
local highestChunk = -1
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection = 0
@ -198,7 +201,7 @@ end
--[[
Expects all neighbors adjacent to a chunk
--]]
function movementUtils.scoreNeighborsForResource(chunk, neighborDirectionChunks, validFunction, scoreFunction, map)
function MovementUtils.scoreNeighborsForResource(chunk, neighborDirectionChunks, validFunction, scoreFunction, map)
local highestChunk = -1
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
@ -227,7 +230,7 @@ end
--[[
Expects all neighbors adjacent to a chunk
--]]
function movementUtils.scoreNeighborsForRetreat(chunk, neighborDirectionChunks, scoreFunction, map)
function MovementUtils.scoreNeighborsForRetreat(chunk, neighborDirectionChunks, scoreFunction, map)
local highestChunk = -1
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
@ -278,7 +281,7 @@ end
--[[
Expects all neighbors adjacent to a chunk
--]]
function movementUtils.scoreNeighborsForFormation(neighborChunks, validFunction, scoreFunction, map)
function MovementUtils.scoreNeighborsForFormation(neighborChunks, validFunction, scoreFunction, map)
local highestChunk = -1
local highestScore = -MAGIC_MAXIMUM_NUMBER
local highestDirection
@ -297,5 +300,9 @@ function movementUtils.scoreNeighborsForFormation(neighborChunks, validFunction,
return highestChunk, highestDirection
end
movementUtilsG = movementUtils
return movementUtils
function MovementUtils.init(universe)
Universe = universe
end
MovementUtilsG = MovementUtils
return MovementUtils

View File

@ -14,80 +14,84 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if pheromoneUtilsG then
return pheromoneUtilsG
if PheromoneUtilsG then
return PheromoneUtilsG
end
local pheromoneUtils = {}
local PheromoneUtils = {}
--
local Universe
-- imports
local mathUtils = require("MathUtils")
local mapUtils = require("MapUtils")
local constants = require("Constants")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local MathUtils = require("MathUtils")
local MapUtils = require("MapUtils")
local Constants = require("Constants")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
-- Constants
local CHUNK_TICK = constants.CHUNK_TICK
local CHUNK_TICK = Constants.CHUNK_TICK
local ENEMY_PHEROMONE_MULTIPLER = constants.ENEMY_PHEROMONE_MULTIPLER
local VICTORY_SCENT_MULTIPLER = constants.VICTORY_SCENT_MULTIPLER
local VICTORY_SCENT_BOUND = constants.VICTORY_SCENT_BOUND
local ENEMY_PHEROMONE_MULTIPLER = Constants.ENEMY_PHEROMONE_MULTIPLER
local VICTORY_SCENT_MULTIPLER = Constants.VICTORY_SCENT_MULTIPLER
local VICTORY_SCENT_BOUND = Constants.VICTORY_SCENT_BOUND
local MAGIC_MAXIMUM_NUMBER = constants.MAGIC_MAXIMUM_NUMBER
local MAGIC_MAXIMUM_NUMBER = Constants.MAGIC_MAXIMUM_NUMBER
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
local ENEMY_PHEROMONE = constants.ENEMY_PHEROMONE
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
local RESOURCE_PHEROMONE = Constants.RESOURCE_PHEROMONE
local ENEMY_PHEROMONE = Constants.ENEMY_PHEROMONE
local VICTORY_SCENT = constants.VICTORY_SCENT
local VICTORY_SCENT = Constants.VICTORY_SCENT
local DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.DEATH_PHEROMONE_GENERATOR_AMOUNT
local TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT
local DEATH_PHEROMONE_GENERATOR_AMOUNT = Constants.DEATH_PHEROMONE_GENERATOR_AMOUNT
local TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT = Constants.TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT
-- imported functions
local decayPlayerGenerator = chunkPropertyUtils.decayPlayerGenerator
local addVictoryGenerator = chunkPropertyUtils.addVictoryGenerator
local getCombinedDeathGenerator = chunkPropertyUtils.getCombinedDeathGenerator
local getCombinedDeathGeneratorRating = chunkPropertyUtils.getCombinedDeathGeneratorRating
local setDeathGenerator = chunkPropertyUtils.setDeathGenerator
local decayPlayerGenerator = ChunkPropertyUtils.decayPlayerGenerator
local addVictoryGenerator = ChunkPropertyUtils.addVictoryGenerator
local getCombinedDeathGenerator = ChunkPropertyUtils.getCombinedDeathGenerator
local getCombinedDeathGeneratorRating = ChunkPropertyUtils.getCombinedDeathGeneratorRating
local setDeathGenerator = ChunkPropertyUtils.setDeathGenerator
local getPlayerGenerator = chunkPropertyUtils.getPlayerGenerator
local getPlayerGenerator = ChunkPropertyUtils.getPlayerGenerator
local addPermanentDeathGenerator = chunkPropertyUtils.addPermanentDeathGenerator
local addPermanentDeathGenerator = ChunkPropertyUtils.addPermanentDeathGenerator
local canMoveChunkDirection = mapUtils.canMoveChunkDirection
local getNeighborChunks = mapUtils.getNeighborChunks
local getChunkById = mapUtils.getChunkById
local canMoveChunkDirection = MapUtils.canMoveChunkDirection
local getNeighborChunks = MapUtils.getNeighborChunks
local getChunkById = MapUtils.getChunkById
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
local getPathRating = chunkPropertyUtils.getPathRating
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
local getResourceGenerator = chunkPropertyUtils.getResourceGenerator
local addDeathGenerator = chunkPropertyUtils.addDeathGenerator
local getEnemyStructureCount = ChunkPropertyUtils.getEnemyStructureCount
local getPathRating = ChunkPropertyUtils.getPathRating
local getPlayerBaseGenerator = ChunkPropertyUtils.getPlayerBaseGenerator
local getResourceGenerator = ChunkPropertyUtils.getResourceGenerator
local addDeathGenerator = ChunkPropertyUtils.addDeathGenerator
local decayDeathGenerator = chunkPropertyUtils.decayDeathGenerator
local decayDeathGenerator = ChunkPropertyUtils.decayDeathGenerator
local linearInterpolation = mathUtils.linearInterpolation
local linearInterpolation = MathUtils.linearInterpolation
local getChunkByXY = mapUtils.getChunkByXY
local getChunkByXY = MapUtils.getChunkByXY
local next = next
local mMax = math.max
-- module code
function pheromoneUtils.victoryScent(map, chunk, entityType)
function PheromoneUtils.victoryScent(map, chunk, entityType)
local value = VICTORY_SCENT[entityType]
if value then
addVictoryGenerator(map, chunk, value)
end
end
function pheromoneUtils.disperseVictoryScent(universe)
local chunkId = universe.victoryScentIterator
local chunkToVictory = universe.chunkToVictory
function PheromoneUtils.disperseVictoryScent()
local chunkId = Universe.victoryScentIterator
local chunkToVictory = Universe.chunkToVictory
local pheromonePack
if not chunkId then
chunkId, pheromonePack = next(chunkToVictory, nil)
@ -95,15 +99,15 @@ function pheromoneUtils.disperseVictoryScent(universe)
pheromonePack = chunkToVictory[chunkId]
end
if not chunkId then
universe.victoryScentIterator = nil
Universe.victoryScentIterator = nil
else
universe.victoryScentIterator = next(chunkToVictory, chunkId)
Universe.victoryScentIterator = next(chunkToVictory, chunkId)
chunkToVictory[chunkId] = nil
local map = pheromonePack.map
if not map.surface.valid then
return
end
local chunk = getChunkById(map, chunkId)
local chunk = getChunkById(chunkId)
local chunkX = chunk.x
local chunkY = chunk.y
local i = 1
@ -121,7 +125,7 @@ function pheromoneUtils.disperseVictoryScent(universe)
end
end
function pheromoneUtils.deathScent(map, chunk, structure)
function PheromoneUtils.deathScent(map, chunk, structure)
local amount = -DEATH_PHEROMONE_GENERATOR_AMOUNT
if structure then
amount = -TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT
@ -130,7 +134,7 @@ function pheromoneUtils.deathScent(map, chunk, structure)
addPermanentDeathGenerator(map, chunk, amount)
end
function pheromoneUtils.processPheromone(map, chunk, tick, player)
function PheromoneUtils.processPheromone(map, chunk, tick, player)
if chunk[CHUNK_TICK] > tick then
return
end
@ -203,5 +207,9 @@ function pheromoneUtils.processPheromone(map, chunk, tick, player)
chunk[RESOURCE_PHEROMONE] = chunkDeathRating * chunkResource * 0.9
end
pheromoneUtilsG = pheromoneUtils
return pheromoneUtils
function PheromoneUtils.init(universe)
Universe = universe
end
PheromoneUtilsG = PheromoneUtils
return PheromoneUtils

View File

@ -14,34 +14,34 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if queryUtilsG then
return queryUtilsG
if QueryUtilsG then
return QueryUtilsG
end
local queryUtils = {}
local QueryUtils = {}
local constants = require("Constants")
local Constants = require("Constants")
local CHUNK_SIZE = constants.CHUNK_SIZE
local CHUNK_SIZE = Constants.CHUNK_SIZE
function queryUtils.setPositionInQuery(query, position)
function QueryUtils.setPositionInQuery(query, position)
local point = query.position
point[1] = position.x
point[2] = position.y
end
function queryUtils.setPositionInCommand(cmd, position)
function QueryUtils.setPositionInCommand(cmd, position)
local point = cmd.destination
point[1] = position.x
point[2] = position.y
end
function queryUtils.setPositionXYInQuery(query, x, y)
function QueryUtils.setPositionXYInQuery(query, x, y)
local point = query.position
point[1] = x
point[2] = y
end
function queryUtils.setAreaInQuery(query, topLeftPosition, size)
function QueryUtils.setAreaInQuery(query, topLeftPosition, size)
local area = query.area
area[1][1] = topLeftPosition.x
area[1][2] = topLeftPosition.y
@ -49,7 +49,7 @@ function queryUtils.setAreaInQuery(query, topLeftPosition, size)
area[2][2] = topLeftPosition.y + size
end
function queryUtils.setAreaInQueryChunkSize(query, topLeftPosition)
function QueryUtils.setAreaInQueryChunkSize(query, topLeftPosition)
local area = query.area
area[1][1] = topLeftPosition.x
area[1][2] = topLeftPosition.y
@ -57,7 +57,7 @@ function queryUtils.setAreaInQueryChunkSize(query, topLeftPosition)
area[2][2] = topLeftPosition.y + CHUNK_SIZE
end
function queryUtils.setPointAreaInQuery(query, position, size)
function QueryUtils.setPointAreaInQuery(query, position, size)
local area = query.area
area[1][1] = position.x - size
area[1][2] = position.y - size
@ -65,17 +65,17 @@ function queryUtils.setPointAreaInQuery(query, position, size)
area[2][2] = position.y + size
end
function queryUtils.setAreaYInQuery(query, y1, y2)
function QueryUtils.setAreaYInQuery(query, y1, y2)
local area = query.area
area[1][2] = y1
area[2][2] = y2
end
function queryUtils.setAreaXInQuery(query, x1, x2)
function QueryUtils.setAreaXInQuery(query, x1, x2)
local area = query.area
area[1][1] = x1
area[2][1] = x2
end
queryUtilsG = queryUtils
return queryUtils
QueryUtilsG = QueryUtils
return QueryUtils

View File

@ -14,42 +14,46 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if (squadAttackG) then
return squadAttackG
if SquadAttackG then
return SquadAttackG
end
local squadAttack = {}
local SquadAttack = {}
--
local Universe
-- imports
local constants = require("Constants")
local mapUtils = require("MapUtils")
local movementUtils = require("MovementUtils")
local mathUtils = require("MathUtils")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local queryUtils = require("QueryUtils")
local Constants = require("Constants")
local MapUtils = require("MapUtils")
local MovementUtils = require("MovementUtils")
local MathUtils = require("MathUtils")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
local QueryUtils = require("QueryUtils")
-- constants
-- Constants
local PLAYER_PHEROMONE_GENERATOR_THRESHOLD = constants.PLAYER_PHEROMONE_GENERATOR_THRESHOLD
local COMMAND_TIMEOUT = constants.COMMAND_TIMEOUT
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local ENEMY_PHEROMONE = constants.ENEMY_PHEROMONE
local RESOURCE_PHEROMONE = constants.RESOURCE_PHEROMONE
local PLAYER_PHEROMONE_GENERATOR_THRESHOLD = Constants.PLAYER_PHEROMONE_GENERATOR_THRESHOLD
local COMMAND_TIMEOUT = Constants.COMMAND_TIMEOUT
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
local ENEMY_PHEROMONE = Constants.ENEMY_PHEROMONE
local RESOURCE_PHEROMONE = Constants.RESOURCE_PHEROMONE
local FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT
local FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT = Constants.FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT
local SQUAD_BUILDING = constants.SQUAD_BUILDING
local SQUAD_BUILDING = Constants.SQUAD_BUILDING
local SQUAD_RAIDING = constants.SQUAD_RAIDING
local SQUAD_SETTLING = constants.SQUAD_SETTLING
local SQUAD_GUARDING = constants.SQUAD_GUARDING
local SQUAD_RETREATING = constants.SQUAD_RETREATING
local SQUAD_RAIDING = Constants.SQUAD_RAIDING
local SQUAD_SETTLING = Constants.SQUAD_SETTLING
local SQUAD_GUARDING = Constants.SQUAD_GUARDING
local SQUAD_RETREATING = Constants.SQUAD_RETREATING
local BASE_AI_STATE_SIEGE = constants.BASE_AI_STATE_SIEGE
local BASE_AI_STATE_AGGRESSIVE = constants.BASE_AI_STATE_AGGRESSIVE
local BASE_AI_STATE_SIEGE = Constants.BASE_AI_STATE_SIEGE
local BASE_AI_STATE_AGGRESSIVE = Constants.BASE_AI_STATE_AGGRESSIVE
local PLAYER_PHEROMONE_MULTIPLER = constants.PLAYER_PHEROMONE_MULTIPLER
local PLAYER_PHEROMONE_MULTIPLER = Constants.PLAYER_PHEROMONE_MULTIPLER
local DEFINES_DISTRACTION_NONE = defines.distraction.none
local DEFINES_DISTRACTION_BY_ENEMY = defines.distraction.by_enemy
@ -57,33 +61,33 @@ local DEFINES_DISTRACTION_BY_ANYTHING = defines.distraction.by_anything
-- imported functions
local getPlayerGenerator = chunkPropertyUtils.getPlayerGenerator
local setPositionInCommand = queryUtils.setPositionInCommand
local getPlayerGenerator = ChunkPropertyUtils.getPlayerGenerator
local setPositionInCommand = QueryUtils.setPositionInCommand
local euclideanDistancePoints = mathUtils.euclideanDistancePoints
local euclideanDistancePoints = MathUtils.euclideanDistancePoints
local findMovementPosition = movementUtils.findMovementPosition
local findMovementPosition = MovementUtils.findMovementPosition
local removeSquadFromChunk = chunkPropertyUtils.removeSquadFromChunk
local addDeathGenerator = chunkPropertyUtils.addDeathGenerator
local removeSquadFromChunk = ChunkPropertyUtils.removeSquadFromChunk
local addDeathGenerator = ChunkPropertyUtils.addDeathGenerator
local getHiveCount = chunkPropertyUtils.getHiveCount
local getNestCount = chunkPropertyUtils.getNestCount
local getHiveCount = ChunkPropertyUtils.getHiveCount
local getNestCount = ChunkPropertyUtils.getNestCount
local getNeighborChunks = mapUtils.getNeighborChunks
local addSquadToChunk = chunkPropertyUtils.addSquadToChunk
local getChunkByXY = mapUtils.getChunkByXY
local positionToChunkXY = mapUtils.positionToChunkXY
local addMovementPenalty = movementUtils.addMovementPenalty
local positionFromDirectionAndFlat = mapUtils.positionFromDirectionAndFlat
local getNeighborChunks = MapUtils.getNeighborChunks
local addSquadToChunk = ChunkPropertyUtils.addSquadToChunk
local getChunkByXY = MapUtils.getChunkByXY
local positionToChunkXY = MapUtils.positionToChunkXY
local addMovementPenalty = MovementUtils.addMovementPenalty
local positionFromDirectionAndFlat = MapUtils.positionFromDirectionAndFlat
local euclideanDistanceNamed = mathUtils.euclideanDistanceNamed
local euclideanDistanceNamed = MathUtils.euclideanDistanceNamed
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
local getResourceGenerator = chunkPropertyUtils.getResourceGenerator
local getPlayerBaseGenerator = ChunkPropertyUtils.getPlayerBaseGenerator
local getResourceGenerator = ChunkPropertyUtils.getResourceGenerator
local scoreNeighborsForAttack = movementUtils.scoreNeighborsForAttack
local scoreNeighborsForSettling = movementUtils.scoreNeighborsForSettling
local scoreNeighborsForAttack = MovementUtils.scoreNeighborsForAttack
local scoreNeighborsForSettling = MovementUtils.scoreNeighborsForSettling
-- module code
local function scoreResourceLocation(map, neighborChunk)
@ -107,7 +111,6 @@ local function scoreAttackLocation(map, neighborChunk)
end
local function settleMove(map, squad)
local universe = map.universe
local group = squad.group
local targetPosition = {x=0,y=0}
@ -141,7 +144,7 @@ local function settleMove(map, squad)
(
(distance >= squad.maxDistance) or
(
(getResourceGenerator(map, chunk) ~= 0) and (getNestCount(map, chunk) == 0) and (getHiveCount(map, chunk) == 0)
(getResourceGenerator(map, chunk) ~= 0) and (getNestCount(chunk) == 0) and (getHiveCount(map, chunk) == 0)
)
)
then
@ -151,7 +154,7 @@ local function settleMove(map, squad)
position = groupPosition
end
cmd = universe.settleCommand
cmd = Universe.settleCommand
if squad.kamikaze then
cmd.distraction = DEFINES_DISTRACTION_NONE
else
@ -173,11 +176,11 @@ local function settleMove(map, squad)
scoreFunction)
if (attackChunk == -1) then
cmd = universe.wanderCommand
cmd = Universe.wanderCommand
group.set_command(cmd)
return
elseif (attackDirection ~= 0) then
local attackPlayerThreshold = universe.attackPlayerThreshold
local attackPlayerThreshold = Universe.attackPlayerThreshold
if (nextAttackChunk ~= -1) then
if (getPlayerBaseGenerator(map, nextAttackChunk) == 0)
@ -216,7 +219,7 @@ local function settleMove(map, squad)
addDeathGenerator(map, attackChunk, -FIVE_DEATH_PHEROMONE_GENERATOR_AMOUNT)
end
else
cmd = universe.wanderCommand
cmd = Universe.wanderCommand
group.set_command(cmd)
return
end
@ -225,7 +228,7 @@ local function settleMove(map, squad)
((getPlayerBaseGenerator(map, nextAttackChunk) ~= 0)
or (getPlayerGenerator(map, nextAttackChunk) >= PLAYER_PHEROMONE_GENERATOR_THRESHOLD))
then
cmd = universe.settleCommand
cmd = Universe.settleCommand
squad.status = SQUAD_BUILDING
if squad.kamikaze then
cmd.distraction = DEFINES_DISTRACTION_NONE
@ -235,7 +238,7 @@ local function settleMove(map, squad)
elseif (getPlayerBaseGenerator(map, attackChunk) ~= 0) or
(attackChunk[PLAYER_PHEROMONE] >= attackPlayerThreshold)
then
cmd = universe.attackCommand
cmd = Universe.attackCommand
if not squad.rabid then
squad.frenzy = true
@ -243,7 +246,7 @@ local function settleMove(map, squad)
squad.frenzyPosition.y = groupPosition.y
end
else
cmd = universe.moveCommand
cmd = Universe.moveCommand
if squad.rabid or squad.kamikaze then
cmd.distraction = DEFINES_DISTRACTION_NONE
else
@ -251,7 +254,7 @@ local function settleMove(map, squad)
end
end
else
cmd = universe.settleCommand
cmd = Universe.settleCommand
targetPosition.x = groupPosition.x
targetPosition.y = groupPosition.y
@ -271,7 +274,6 @@ local function settleMove(map, squad)
end
local function attackMove(map, squad)
local universe = map.universe
local targetPosition = {0,0}
local group = squad.group
@ -301,7 +303,7 @@ local function attackMove(map, squad)
attackScorer)
local cmd
if (attackChunk == -1) then
cmd = universe.wanderCommand
cmd = Universe.wanderCommand
group.set_command(cmd)
return
end
@ -329,7 +331,7 @@ local function attackMove(map, squad)
end
if not position then
cmd = universe.wanderCommand
cmd = Universe.wanderCommand
group.set_command(cmd)
return
else
@ -343,9 +345,9 @@ local function attackMove(map, squad)
end
if (getPlayerBaseGenerator(map, attackChunk) ~= 0) and
(attackChunk[PLAYER_PHEROMONE] >= universe.attackPlayerThreshold)
(attackChunk[PLAYER_PHEROMONE] >= Universe.attackPlayerThreshold)
then
cmd = universe.attackCommand
cmd = Universe.attackCommand
if not squad.rabid then
squad.frenzy = true
@ -353,7 +355,7 @@ local function attackMove(map, squad)
squad.frenzyPosition.y = groupPosition.y
end
else
cmd = universe.moveCommand
cmd = Universe.moveCommand
if squad.rabid or squad.frenzy then
cmd.distraction = DEFINES_DISTRACTION_BY_ANYTHING
else
@ -367,22 +369,21 @@ end
local function buildMove(map, squad)
local group = squad.group
local universe = map.universe
local groupPosition = group.position
local newGroupPosition = findMovementPosition(map.surface, groupPosition)
if not newGroupPosition then
setPositionInCommand(universe.settleCommand, groupPosition)
setPositionInCommand(Universe.settleCommand, groupPosition)
else
setPositionInCommand(universe.settleCommand, newGroupPosition)
setPositionInCommand(Universe.settleCommand, newGroupPosition)
end
group.set_command(universe.compoundSettleCommand)
group.set_command(Universe.compoundSettleCommand)
end
function squadAttack.cleanSquads(universe, tick)
local squads = universe.groupNumberToSquad
local groupId = universe.squadIterator
function SquadAttack.cleanSquads(tick)
local squads = Universe.groupNumberToSquad
local groupId = Universe.squadIterator
local squad
if not groupId then
groupId, squad = next(squads, groupId)
@ -390,13 +391,13 @@ function squadAttack.cleanSquads(universe, tick)
squad = squads[groupId]
end
if not groupId then
universe.squadIterator = nil
Universe.squadIterator = nil
if (table_size(squads) == 0) then
-- this is needed as the next command remembers the max length a table has been
universe.groupNumberToSquad = {}
Universe.groupNumberToSquad = {}
end
else
universe.squadIterator = next(squads, groupId)
Universe.squadIterator = next(squads, groupId)
local group = squad.group
if not group.valid then
if squad.chunk ~= -1 then
@ -404,14 +405,14 @@ function squadAttack.cleanSquads(universe, tick)
end
removeSquadFromChunk(squad.map, squad)
if squad.settlers then
universe.builderCount = universe.builderCount - 1
if universe.builderCount < 0 then
universe.builderCount = 0
Universe.builderCount = Universe.builderCount - 1
if Universe.builderCount < 0 then
Universe.builderCount = 0
end
else
universe.squadCount = universe.squadCount - 1
if universe.squadCount < 0 then
universe.squadCount = 0
Universe.squadCount = Universe.squadCount - 1
if Universe.squadCount < 0 then
Universe.squadCount = 0
end
if squad.type == BASE_AI_STATE_AGGRESSIVE then
local base = squad.base
@ -424,13 +425,13 @@ function squadAttack.cleanSquads(universe, tick)
squads[groupId] = nil
elseif (group.state == 4) then
squad.wanders = 0
squadAttack.squadDispatch(squad.map, squad, tick)
SquadAttack.squadDispatch(squad.map, squad, tick)
elseif (squad.commandTick and (squad.commandTick < tick)) then
if squad.wanders > 5 then
squad.group.destroy()
else
squad.wanders = squad.wanders + 1
local cmd = universe.wander2Command
local cmd = Universe.wander2Command
squad.commandTick = tick + COMMAND_TIMEOUT
group.set_command(cmd)
group.start_moving()
@ -439,7 +440,7 @@ function squadAttack.cleanSquads(universe, tick)
end
end
function squadAttack.squadDispatch(map, squad, tick)
function SquadAttack.squadDispatch(map, squad, tick)
local group = squad.group
if group and group.valid then
local status = squad.status
@ -475,5 +476,9 @@ function squadAttack.squadDispatch(map, squad, tick)
end
end
squadAttackG = squadAttack
return squadAttack
function SquadAttack.init(universe)
Universe = universe
end
SquadAttackG = SquadAttack
return SquadAttack

View File

@ -14,47 +14,51 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if aiDefenseG then
return aiDefenseG
if AiDefenseG then
return AiDefenseG
end
local aiDefense = {}
local AiDefense = {}
--
local Universe
-- imports
local constants = require("Constants")
local mapUtils = require("MapUtils")
local unitGroupUtils = require("UnitGroupUtils")
local movementUtils = require("MovementUtils")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local Constants = require("Constants")
local MapUtils = require("MapUtils")
local UnitGroupUtils = require("UnitGroupUtils")
local MovementUtils = require("MovementUtils")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
-- Constants
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local PLAYER_PHEROMONE = Constants.PLAYER_PHEROMONE
local BASE_PHEROMONE = Constants.BASE_PHEROMONE
local PLAYER_PHEROMONE_MULTIPLER = constants.PLAYER_PHEROMONE_MULTIPLER
local PLAYER_PHEROMONE_MULTIPLER = Constants.PLAYER_PHEROMONE_MULTIPLER
local SQUAD_RETREATING = constants.SQUAD_RETREATING
local SQUAD_RETREATING = Constants.SQUAD_RETREATING
local COOLDOWN_RETREAT = constants.COOLDOWN_RETREAT
local COOLDOWN_RETREAT = Constants.COOLDOWN_RETREAT
-- imported functions
local findNearbyBase = chunkPropertyUtils.findNearbyBase
local findNearbyBase = ChunkPropertyUtils.findNearbyBase
local addSquadToChunk = chunkPropertyUtils.addSquadToChunk
local addSquadToChunk = ChunkPropertyUtils.addSquadToChunk
local positionFromDirectionAndFlat = mapUtils.positionFromDirectionAndFlat
local getNeighborChunks = mapUtils.getNeighborChunks
local findNearbyRetreatingSquad = unitGroupUtils.findNearbyRetreatingSquad
local createSquad = unitGroupUtils.createSquad
local scoreNeighborsForRetreat = movementUtils.scoreNeighborsForRetreat
local findMovementPosition = movementUtils.findMovementPosition
local positionFromDirectionAndFlat = MapUtils.positionFromDirectionAndFlat
local getNeighborChunks = MapUtils.getNeighborChunks
local findNearbyRetreatingSquad = UnitGroupUtils.findNearbyRetreatingSquad
local createSquad = UnitGroupUtils.createSquad
local scoreNeighborsForRetreat = MovementUtils.scoreNeighborsForRetreat
local findMovementPosition = MovementUtils.findMovementPosition
local getRetreatTick = chunkPropertyUtils.getRetreatTick
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
local setRetreatTick = chunkPropertyUtils.setRetreatTick
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
local getRetreatTick = ChunkPropertyUtils.getRetreatTick
local getPlayerBaseGenerator = ChunkPropertyUtils.getPlayerBaseGenerator
local setRetreatTick = ChunkPropertyUtils.setRetreatTick
local getEnemyStructureCount = ChunkPropertyUtils.getEnemyStructureCount
-- module code
@ -64,8 +68,8 @@ local function scoreRetreatLocation(map, neighborChunk)
-(getPlayerBaseGenerator(map, neighborChunk) * 1000))
end
function aiDefense.retreatUnits(chunk, cause, map, tick, radius)
if (tick - getRetreatTick(map, chunk) > COOLDOWN_RETREAT) and (getEnemyStructureCount(map, chunk) == 0) then
function AiDefense.retreatUnits(chunk, cause, map, tick, radius)
if (tick - getRetreatTick(chunk) > COOLDOWN_RETREAT) and (getEnemyStructureCount(map, chunk) == 0) then
setRetreatTick(map, chunk, tick)
local exitPath,exitDirection,
@ -75,7 +79,6 @@ function aiDefense.retreatUnits(chunk, cause, map, tick, radius)
chunk.y),
scoreRetreatLocation,
map)
local universe = map.universe
local position = {
x = chunk.x + 16,
y = chunk.y + 16
@ -117,7 +120,7 @@ function aiDefense.retreatUnits(chunk, cause, map, tick, radius)
local created = false
if not newSquad then
if (universe.squadCount < universe.AI_MAX_SQUAD_COUNT) then
if (Universe.squadCount < Universe.AI_MAX_SQUAD_COUNT) then
created = true
local base = findNearbyBase(map, chunk)
if not base then
@ -129,12 +132,12 @@ function aiDefense.retreatUnits(chunk, cause, map, tick, radius)
end
end
universe.fleeCommand.from = cause
universe.retreatCommand.group = newSquad.group
Universe.fleeCommand.from = cause
Universe.retreatCommand.group = newSquad.group
universe.formRetreatCommand.unit_search_distance = radius
Universe.formRetreatCommand.unit_search_distance = radius
local foundUnits = surface.set_multi_command(universe.formRetreatCommand)
local foundUnits = surface.set_multi_command(Universe.formRetreatCommand)
if (foundUnits == 0) then
if created then
@ -144,8 +147,8 @@ function aiDefense.retreatUnits(chunk, cause, map, tick, radius)
end
if created then
universe.groupNumberToSquad[newSquad.groupNumber] = newSquad
universe.squadCount = universe.squadCount + 1
Universe.groupNumberToSquad[newSquad.groupNumber] = newSquad
Universe.squadCount = Universe.squadCount + 1
end
newSquad.status = SQUAD_RETREATING
@ -159,5 +162,9 @@ function aiDefense.retreatUnits(chunk, cause, map, tick, radius)
end
end
aiDefenseG = aiDefense
return aiDefense
function AiDefense.init(universe)
Universe = universe
end
AiDefenseG = AiDefense
return AiDefense

View File

@ -14,35 +14,39 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
if unitGroupUtilsG then
return unitGroupUtilsG
if UnitGroupUtilsG then
return UnitGroupUtilsG
end
local unitGroupUtils = {}
local UnitGroupUtils = {}
--
local Universe
-- imports
local mapUtils = require("MapUtils")
local constants = require("Constants")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local mathUtils = require("MathUtils")
local MapUtils = require("MapUtils")
local Constants = require("Constants")
local ChunkPropertyUtils = require("ChunkPropertyUtils")
local MathUtils = require("MathUtils")
-- constants
-- Constants
local MINIMUM_EXPANSION_DISTANCE = constants.MINIMUM_EXPANSION_DISTANCE
local SQUAD_RETREATING = constants.SQUAD_RETREATING
local SQUAD_GUARDING = constants.SQUAD_GUARDING
local MINIMUM_EXPANSION_DISTANCE = Constants.MINIMUM_EXPANSION_DISTANCE
local SQUAD_RETREATING = Constants.SQUAD_RETREATING
local SQUAD_GUARDING = Constants.SQUAD_GUARDING
-- imported functions
local gaussianRandomRangeRG = mathUtils.gaussianRandomRangeRG
local gaussianRandomRangeRG = MathUtils.gaussianRandomRangeRG
local getSquadsOnChunk = chunkPropertyUtils.getSquadsOnChunk
local getSquadsOnChunk = ChunkPropertyUtils.getSquadsOnChunk
local getNeighborChunks = mapUtils.getNeighborChunks
local getNeighborChunks = MapUtils.getNeighborChunks
-- module code
function unitGroupUtils.findNearbyRetreatingSquad(map, chunk)
function UnitGroupUtils.findNearbyRetreatingSquad(map, chunk)
for _,squad in pairs(getSquadsOnChunk(map, chunk)) do
local unitGroup = squad.group
@ -67,7 +71,7 @@ function unitGroupUtils.findNearbyRetreatingSquad(map, chunk)
return nil
end
function unitGroupUtils.findNearbySquad(map, chunk)
function UnitGroupUtils.findNearbySquad(map, chunk)
for _,squad in pairs(getSquadsOnChunk(map, chunk)) do
local unitGroup = squad.group
@ -93,28 +97,28 @@ function unitGroupUtils.findNearbySquad(map, chunk)
return nil
end
function unitGroupUtils.calculateSettlerMaxDistance(universe)
function UnitGroupUtils.calculateSettlerMaxDistance()
local targetDistance
local distanceRoll = universe.random()
local distanceRoll = Universe.random()
if distanceRoll < 0.05 then
return 0
elseif distanceRoll < 0.30 then
targetDistance = universe.expansionLowTargetDistance
targetDistance = Universe.expansionLowTargetDistance
elseif distanceRoll < 0.70 then
targetDistance = universe.expansionMediumTargetDistance
targetDistance = Universe.expansionMediumTargetDistance
elseif distanceRoll < 0.95 then
targetDistance = universe.expansionHighTargetDistance
targetDistance = Universe.expansionHighTargetDistance
else
return universe.expansionMaxDistance
return Universe.expansionMaxDistance
end
return gaussianRandomRangeRG(targetDistance,
universe.expansionDistanceDeviation,
Universe.expansionDistanceDeviation,
MINIMUM_EXPANSION_DISTANCE,
universe.expansionMaxDistance,
universe.random)
Universe.expansionMaxDistance,
Universe.random)
end
function unitGroupUtils.createSquad(position, map, group, settlers, base)
function UnitGroupUtils.createSquad(position, map, group, settlers, base)
local unitGroup = group or map.surface.create_unit_group({position=position})
local squad = {
@ -140,7 +144,7 @@ function unitGroupUtils.createSquad(position, map, group, settlers, base)
}
if settlers then
squad.maxDistance = unitGroupUtils.calculateSettlerMaxDistance(map.universe)
squad.maxDistance = UnitGroupUtils.calculateSettlerMaxDistance()
end
if position then
@ -154,15 +158,19 @@ function unitGroupUtils.createSquad(position, map, group, settlers, base)
return squad
end
function unitGroupUtils.calculateKamikazeSquadThreshold(memberCount, universe)
local threshold = (memberCount / universe.attackWaveMaxSize) * 0.2 + (universe.evolutionLevel * 0.2)
function UnitGroupUtils.calculateKamikazeSquadThreshold(memberCount)
local threshold = (memberCount / Universe.attackWaveMaxSize) * 0.2 + (Universe.evolutionLevel * 0.2)
return threshold
end
function unitGroupUtils.calculateKamikazeSettlerThreshold(memberCount, universe)
local threshold = (memberCount / universe.expansionMaxSize) * 0.2 + (universe.evolutionLevel * 0.2)
function UnitGroupUtils.calculateKamikazeSettlerThreshold(memberCount)
local threshold = (memberCount / Universe.expansionMaxSize) * 0.2 + (Universe.evolutionLevel * 0.2)
return threshold
end
unitGroupUtilsG = unitGroupUtils
return unitGroupUtils
function UnitGroupUtils.init(universe)
Universe = universe
end
UnitGroupUtilsG = UnitGroupUtils
return UnitGroupUtils

View File

@ -14,7 +14,7 @@
-- along with this program. If not, see <https://www.gnu.org/licenses/>.
local upgrade = {}
local Upgrade = {}
-- imports
@ -23,6 +23,10 @@ local chunkProcessor = require("libs/ChunkProcessor")
local chunkPropertyUtils = require("libs/ChunkPropertyUtils")
local mapUtils = require("libs/MapUtils")
--
local Universe
-- constants
local MINIMUM_EXPANSION_DISTANCE = constants.MINIMUM_EXPANSION_DISTANCE
@ -60,7 +64,7 @@ local processPendingChunks = chunkProcessor.processPendingChunks
-- module code
function upgrade.isExcludedSurface(surfaceName)
function Upgrade.isExcludedSurface(surfaceName)
return
(surfaceName == "aai-signals") or
(surfaceName == "RTStasisRealm") or
@ -414,97 +418,170 @@ local function addCommandSet(queriesAndCommands)
}
end
function upgrade.excludeSurface(universe)
for mapId,map in pairs(universe.maps) do
local toBeRemoved = not map.surface.valid or upgrade.isExcludedSurface(map.surface.name) or universe.excludedSurfaces[map.surface.name]
function Upgrade.excludeSurface()
for mapId,map in pairs(Universe.maps) do
local toBeRemoved = not map.surface.valid or Upgrade.isExcludedSurface(map.surface.name) or Universe.excludedSurfaces[map.surface.name]
if toBeRemoved then
if universe.mapIterator == mapId then
universe.mapIterator, universe.activeMap = next(universe.maps, universe.mapIterator)
if Universe.mapIterator == mapId then
Universe.mapIterator, Universe.activeMap = next(Universe.maps, Universe.mapIterator)
end
if universe.processMapAIIterator == mapId then
universe.processMapAIIterator = nil
if Universe.processMapAIIterator == mapId then
Universe.processMapAIIterator = nil
end
universe.maps[mapId] = nil
Universe.maps[mapId] = nil
end
end
end
function upgrade.setCommandForces(universe, npcForces, enemyForces)
for force in pairs(universe.playerForces) do
universe.playerForces[force] = nil
function Upgrade.setCommandForces(npcForces, enemyForces)
for force in pairs(Universe.playerForces) do
Universe.playerForces[force] = nil
end
for force in pairs(universe.npcForces) do
universe.npcForces[force] = nil
for force in pairs(Universe.npcForces) do
Universe.npcForces[force] = nil
end
for force in pairs(universe.enemyForces) do
universe.enemyForces[force] = nil
for force in pairs(Universe.enemyForces) do
Universe.enemyForces[force] = nil
end
for force in pairs(universe.nonPlayerForces) do
universe.nonPlayerForces[force] = nil
for force in pairs(Universe.nonPlayerForces) do
Universe.nonPlayerForces[force] = nil
end
for _,force in pairs(game.forces) do
if not npcForces[force.name] and not enemyForces[force.name] then
universe.playerForces[#universe.playerForces+1] = force.name
Universe.playerForces[#Universe.playerForces+1] = force.name
end
end
for force in pairs(enemyForces) do
universe.enemyForces[#universe.enemyForces+1] = force
universe.nonPlayerForces[#universe.nonPlayerForces+1] = force
Universe.enemyForces[#Universe.enemyForces+1] = force
Universe.nonPlayerForces[#Universe.nonPlayerForces+1] = force
end
for force in pairs(npcForces) do
universe.npcForces[#universe.npcForces+1] = force
universe.nonPlayerForces[#universe.nonPlayerForces+1] = force
Universe.npcForces[#Universe.npcForces+1] = force
Universe.nonPlayerForces[#Universe.nonPlayerForces+1] = force
end
end
function upgrade.attempt(universe)
local starting = global.version
if not global.version or global.version < 302 then
global.version = 302
if not universe then
universe = {}
global.universe = universe
function Upgrade.addUniverseProperties()
if not global.universePropertyVersion then
for key in pairs(global) do
if key ~= "universe" then
global[key] = nil
end
end
for key in pairs(Universe) do
Universe[key] = nil
end
global.universePropertyVersion = 0
end
if global.universePropertyVersion < 1 then
global.universePropertyVersion = 1
Universe.safeEntities = {}
Universe.aiPointsScaler = settings.global["rampant--aiPointsScaler"].value
Universe.aiPointsPrintGainsToChat = settings.global["rampant--aiPointsPrintGainsToChat"].value
Universe.aiPointsPrintSpendingToChat = settings.global["rampant--aiPointsPrintSpendingToChat"].value
Universe.aiNocturnalMode = settings.global["rampant--permanentNocturnal"].value
Universe.retreatThreshold = 0
Universe.rallyThreshold = 0
Universe.formSquadThreshold = 0
Universe.attackWaveSize = 0
Universe.attackWaveDeviation = 0
Universe.attackWaveUpperBound = 0
Universe.unitRefundAmount = 0
Universe.regroupIndex = 1
Universe.kamikazeThreshold = 0
Universe.attackWaveLowerBound = 1
Universe.settlerCooldown = 0
Universe.settlerWaveDeviation = 0
Universe.settlerWaveSize = 0
Universe.enabledMigration = Universe.expansion and settings.global["rampant--enableMigration"].value
Universe.peacefulAIToggle = settings.global["rampant--peacefulAIToggle"].value
Universe.printAIStateChanges = settings.global["rampant--printAIStateChanges"].value
Universe.debugTemperament = settings.global["rampant--debugTemperament"].value
Universe.eventId = 0
Universe.chunkId = 0
Universe.maps = {}
Universe.chunkIdToChunk = {}
Universe.groupNumberToSquad = {}
Universe.chunkToVictory = {}
Universe.pendingChunks = {}
Universe.processActiveNest = {}
Universe.processActiveNestIterator = nil
Universe.deployVengenceIterator = nil
Universe.pendingUpgradeIterator = nil
Universe.victoryScentIterator = nil
Universe.squadIterator = nil
Universe.processMapAIIterator = nil
Universe.processNestIterator = nil
Universe.vengenceQueue = {}
Universe.activeMap = nil
Universe.mapIterator = nil
Universe.builderCount = 0
Universe.squadCount = 0
Universe.chunkToNests = {}
Universe.processActiveSpawnerIterator = nil
Universe.processActiveRaidSpawnerIterator = nil
Universe.processMigrationIterator = nil
Universe.chunkToDrainedIterator = nil
Universe.chunkToActiveNest = {}
Universe.chunkToActiveRaidNest = {}
Universe.chunkToDrained = {}
Universe.chunkToRetreatIterator = nil
Universe.chunkToRetreats = {}
Universe.chunkToRallyIterator = nil
Universe.chunkToRallys = {}
Universe.chunkToPassScan = {}
Universe.chunkToPassScanIterator = nil
Universe.baseId = 0
Universe.awake = false
Universe.recycleBaseIterator = nil
Universe.maxPoints = 0
Universe.maxOverflowPoints = 0
addCommandSet(Universe)
Universe.bases = {}
Universe.processBaseAIIterator = nil
for _,map in pairs(Universe.maps) do
local processQueue = map.processQueue
for i=1,#processQueue do
local chunk = processQueue[i]
chunk[CHUNK_TICK] = chunk[ENEMY_PHEROMONE]
chunk[ENEMY_PHEROMONE] = 0
end
end
Universe.excludedSurfaces = {}
Universe.pendingUpgrades = {}
Universe.settlePurpleCloud = {}
Universe.settlePurpleCloud.len = 0
end
end
function Upgrade.attempt()
if not global.gameVersion then
global.gameVersion = 1
game.forces.enemy.kill_all_units()
universe.safeEntities = {}
universe.aiPointsScaler = settings.global["rampant--aiPointsScaler"].value
universe.aiPointsPrintGainsToChat = settings.global["rampant--aiPointsPrintGainsToChat"].value
universe.aiPointsPrintSpendingToChat = settings.global["rampant--aiPointsPrintSpendingToChat"].value
universe.aiNocturnalMode = settings.global["rampant--permanentNocturnal"].value
universe.mapIterator = nil
universe.retreatThreshold = 0
universe.rallyThreshold = 0
universe.formSquadThreshold = 0
universe.attackWaveSize = 0
universe.attackWaveDeviation = 0
universe.attackWaveUpperBound = 0
universe.unitRefundAmount = 0
universe.regroupIndex = 1
game.map_settings.path_finder.min_steps_to_check_path_find_termination =
constants.PATH_FINDER_MIN_STEPS_TO_CHECK_PATH
universe.kamikazeThreshold = 0
universe.attackWaveLowerBound = 1
universe.expansionMaxDistanceDerivation = nil
universe.settlerCooldown = 0
universe.settlerWaveDeviation = 0
universe.settlerWaveSize = 0
universe.enabledMigration = universe.expansion and settings.global["rampant--enableMigration"].value
universe.peacefulAIToggle = settings.global["rampant--peacefulAIToggle"].value
universe.printAIStateChanges = settings.global["rampant--printAIStateChanges"].value
universe.debugTemperament = settings.global["rampant--debugTemperament"].value
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
@ -517,60 +594,9 @@ function upgrade.attempt(universe)
game.map_settings.unit_group.tick_tolerance_when_member_arrives = 60
game.forces.enemy.ai_controllable = true
universe.evolutionLevel = game.forces.enemy.evolution_factor
global.pendingChunks = nil -- removes old pendingChunks
global.natives = nil -- removes old natives
global.map = nil -- removes old map
Universe.evolutionLevel = game.forces.enemy.evolution_factor
universe.eventId = 0
universe.chunkId = 0
universe.randomGenerator = nil
game.forces.enemy.kill_all_units()
universe.maps = {}
universe.chunkIdToChunk = {}
universe.groupNumberToSquad = {}
universe.chunkToVictory = {}
universe.pendingChunks = {}
universe.processActiveNest = {}
universe.processActiveNestIterator = nil
universe.deployVengenceIterator = nil
universe.pendingUpgradeIterator = nil
universe.victoryScentIterator = nil
universe.squadIterator = nil
universe.processMapAIIterator = nil
universe.processNestIterator = nil
universe.vengenceQueue = {}
universe.activeMap = nil
universe.mapIterator = nil
universe.builderCount = 0
universe.squadCount = 0
universe.chunkToNests = {}
universe.processActiveSpawnerIterator = nil
universe.processActiveRaidSpawnerIterator = nil
universe.processMigrationIterator = nil
universe.chunkToDrainedIterator = nil
universe.chunkToActiveNest = {}
universe.chunkToActiveRaidNest = {}
universe.chunkToDrained = {}
universe.chunkToRetreatIterator = nil
universe.chunkToRetreats = {}
universe.chunkToRallyIterator = nil
universe.chunkToRallys = {}
universe.chunkToPassScan = {}
universe.chunkToPassScanIterator = nil
universe.baseId = 0
universe.awake = false
universe.recycleBaseIterator = nil
universe.maxPoints = 0
universe.maxOverflowPoints = 0
addCommandSet(universe)
universe.bases = {}
for _,map in pairs(universe.maps) do
for _,map in pairs(Universe.maps) do
if (map.surface.valid) then
local entities = map.surface.find_entities_filtered({type="land-mine"})
for i=1,#entities do
@ -582,73 +608,26 @@ function upgrade.attempt(universe)
end
end
upgrade.excludeSurface(universe)
Universe.random = game.create_random_generator()
universe.processBaseAIIterator = nil
end
if global.version < 304 then
global.version = 304
Upgrade.excludeSurface()
universe.random = game.create_random_generator()
for _,map in pairs(universe.maps) do
map.random = universe.random
local processQueue = map.processQueue
for i=1,#processQueue do
local chunk = processQueue[i]
chunk[CHUNK_TICK] = chunk[ENEMY_PHEROMONE]
chunk[ENEMY_PHEROMONE] = 0
end
end
end
if global.version < 305 then
global.version = 305
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
end
if global.version < 307 then
global.version = 307
local minDiffuse = game.map_settings.pollution.min_to_diffuse
universe.pollutionDiffuseMinimum = minDiffuse * 0.75
Universe.pollutionDiffuseMinimum = minDiffuse * 0.75
universe.expansion = game.map_settings.enemy_expansion.enabled
universe.expansionMaxDistance = game.map_settings.enemy_expansion.max_expansion_distance * CHUNK_SIZE
universe.expansionMinTime = game.map_settings.enemy_expansion.min_expansion_cooldown / TICKS_A_MINUTE
universe.expansionMaxTime = game.map_settings.enemy_expansion.max_expansion_cooldown / TICKS_A_MINUTE
universe.expansionMinSize = game.map_settings.enemy_expansion.settler_group_min_size
universe.expansionMaxSize = game.map_settings.enemy_expansion.settler_group_max_size
Universe.expansion = game.map_settings.enemy_expansion.enabled
Universe.expansionMaxDistance = game.map_settings.enemy_expansion.max_expansion_distance * CHUNK_SIZE
Universe.expansionMinTime = game.map_settings.enemy_expansion.min_expansion_cooldown / TICKS_A_MINUTE
Universe.expansionMaxTime = game.map_settings.enemy_expansion.max_expansion_cooldown / TICKS_A_MINUTE
Universe.expansionMinSize = game.map_settings.enemy_expansion.settler_group_min_size
Universe.expansionMaxSize = game.map_settings.enemy_expansion.settler_group_max_size
universe.expansionLowTargetDistance = (universe.expansionMaxDistance + MINIMUM_EXPANSION_DISTANCE) * 0.33
universe.expansionMediumTargetDistance = (universe.expansionMaxDistance + MINIMUM_EXPANSION_DISTANCE) * 0.50
universe.expansionHighTargetDistance = (universe.expansionMaxDistance + MINIMUM_EXPANSION_DISTANCE) * 0.75
universe.expansionDistanceDeviation = universe.expansionMediumTargetDistance * 0.33
Universe.expansionLowTargetDistance = (Universe.expansionMaxDistance + MINIMUM_EXPANSION_DISTANCE) * 0.33
Universe.expansionMediumTargetDistance = (Universe.expansionMaxDistance + MINIMUM_EXPANSION_DISTANCE) * 0.50
Universe.expansionHighTargetDistance = (Universe.expansionMaxDistance + MINIMUM_EXPANSION_DISTANCE) * 0.75
Universe.expansionDistanceDeviation = Universe.expansionMediumTargetDistance * 0.33
universe.pendingUpgrades = {}
universe.settlePurpleCloud = {}
universe.settlePurpleCloud.len = 0
for _,base in pairs(universe.bases) do
base.maxExpansionGroups = 0
base.sentExpansionGroups = 0
base.resetExpensionGroupsTick = 0
base.alignmentHistory = {}
base.resourceChunks = {}
base.resourceChunkCount = 0
end
for _,map in pairs(universe.maps) do
for _,map in pairs(Universe.maps) do
if (map.surface.valid) then
for _, chunk in pairs(map.processQueue) do
if getResourceGenerator(map, chunk) > 0 then
@ -663,16 +642,12 @@ function upgrade.attempt(universe)
map.chunkToPermanentDeathGenerator = {}
end
end
game.print("Rampant - Version 3.2.0")
end
return (starting ~= global.version) and global.version
end
function upgrade.prepMap(universe, surface)
function Upgrade.prepMap(surface)
local surfaceName = surface.name
if upgrade.isExcludedSurface(surfaceName) then
if Upgrade.isExcludedSurface(surfaceName) then
return
end
@ -680,12 +655,12 @@ function upgrade.prepMap(universe, surface)
local surfaceIndex = surface.index
if not universe.maps then
universe.maps = {}
if not Universe.maps then
Universe.maps = {}
end
local map = {}
universe.maps[surfaceIndex] = map
Universe.maps[surfaceIndex] = map
map.activatedMap = false
@ -729,35 +704,35 @@ function upgrade.prepMap(universe, surface)
map.emptySquadsOnChunk = {}
map.surface = surface
map.universe = universe
map.bases = {}
map.squads = nil
map.pendingAttack = nil
map.building = nil
map.random = universe.random
-- queue all current chunks that wont be generated during play
local tick = game.tick
for chunk in surface.get_chunks() do
if surface.is_chunk_generated(chunk) then
queueGeneratedChunk(universe,
{
surface = surface,
tick = tick,
area = {
left_top = {
x = chunk.x * 32,
y = chunk.y * 32
}
}
queueGeneratedChunk({
surface = surface,
tick = tick,
area = {
left_top = {
x = chunk.x * 32,
y = chunk.y * 32
}
}
}
)
end
end
processPendingChunks(universe, tick, true)
processPendingChunks(tick, true)
end
return upgrade
function Upgrade.init(universe)
Universe = universe
end
return Upgrade

View File

@ -24,7 +24,6 @@
(string->path "COPYING")
(string->path "tests.lua")
(string->path "changelog.txt")
(string->path "Upgrade.lua")
(string->path "settings.lua")
(string->path "README.md")
(string->path "thumbnail.png")
@ -67,7 +66,6 @@
(copyFile "data-final-fixes.lua" modFolder)
(copyFile "settings.lua" modFolder)
(copyFile "changelog.txt" modFolder)
(copyFile "Upgrade.lua" modFolder)
(copyFile "tests.lua" modFolder)
(copyFile "thumbnail.png" modFolder)
(copyDirectory "libs" modFolder)