2019-02-16 06:17:30 +02:00
|
|
|
if aiDefenseG then
|
|
|
|
return aiDefenseG
|
|
|
|
end
|
2016-08-18 07:55:08 +02:00
|
|
|
local aiDefense = {}
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- imports
|
|
|
|
|
2016-08-18 07:55:08 +02:00
|
|
|
local constants = require("Constants")
|
|
|
|
local mapUtils = require("MapUtils")
|
|
|
|
local unitGroupUtils = require("UnitGroupUtils")
|
2017-06-16 03:30:26 +02:00
|
|
|
local movementUtils = require("MovementUtils")
|
2020-05-24 05:47:14 +02:00
|
|
|
local chunkPropertyUtils = require("ChunkPropertyUtils")
|
2016-08-18 07:55:08 +02:00
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- constants
|
|
|
|
|
|
|
|
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
|
2016-10-15 02:00:18 +02:00
|
|
|
local BASE_PHEROMONE = constants.BASE_PHEROMONE
|
2016-08-20 04:52:27 +02:00
|
|
|
|
2019-02-11 08:14:17 +02:00
|
|
|
local PLAYER_PHEROMONE_MULTIPLER = constants.PLAYER_PHEROMONE_MULTIPLER
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
local SQUAD_RETREATING = constants.SQUAD_RETREATING
|
|
|
|
|
2020-05-20 04:37:16 +02:00
|
|
|
local COOLDOWN_RETREAT = constants.COOLDOWN_RETREAT
|
2017-04-22 01:14:04 +02:00
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- imported functions
|
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
local addSquadToChunk = chunkPropertyUtils.addSquadToChunk
|
2018-01-14 07:48:21 +02:00
|
|
|
|
2019-10-20 22:45:43 +02:00
|
|
|
local positionFromDirectionAndFlat = mapUtils.positionFromDirectionAndFlat
|
2017-06-08 02:57:24 +02:00
|
|
|
local getNeighborChunks = mapUtils.getNeighborChunks
|
2019-03-10 01:16:35 +02:00
|
|
|
local findNearbyRetreatingSquad = unitGroupUtils.findNearbyRetreatingSquad
|
2016-08-20 04:52:27 +02:00
|
|
|
local createSquad = unitGroupUtils.createSquad
|
2017-11-21 09:27:03 +02:00
|
|
|
local scoreNeighborsForRetreat = movementUtils.scoreNeighborsForRetreat
|
2017-06-16 03:30:26 +02:00
|
|
|
local findMovementPosition = movementUtils.findMovementPosition
|
2016-08-20 04:52:27 +02:00
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
local getRetreatTick = chunkPropertyUtils.getRetreatTick
|
|
|
|
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
|
|
|
|
local setRetreatTick = chunkPropertyUtils.setRetreatTick
|
|
|
|
local getDeathGenerator = chunkPropertyUtils.getDeathGenerator
|
|
|
|
local getEnemyStructureCount = chunkPropertyUtils.getEnemyStructureCount
|
2017-11-21 09:27:03 +02:00
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- module code
|
2016-08-18 07:55:08 +02:00
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
local function scoreRetreatLocation(map, neighborChunk)
|
2019-03-09 08:37:29 +02:00
|
|
|
return (-neighborChunk[BASE_PHEROMONE] +
|
2021-02-14 06:49:54 +02:00
|
|
|
-getDeathGenerator(map, neighborChunk) +
|
|
|
|
-(neighborChunk[PLAYER_PHEROMONE] * PLAYER_PHEROMONE_MULTIPLER) +
|
|
|
|
-(getPlayerBaseGenerator(map, neighborChunk) * 1000))
|
2016-08-27 08:44:17 +02:00
|
|
|
end
|
|
|
|
|
2021-02-20 07:41:30 +02:00
|
|
|
function aiDefense.retreatUnits(chunk, cause, map, tick, radius)
|
2020-05-20 04:37:16 +02:00
|
|
|
if (tick - getRetreatTick(map, chunk) > COOLDOWN_RETREAT) and (getEnemyStructureCount(map, chunk) == 0) then
|
2020-05-24 05:47:14 +02:00
|
|
|
|
|
|
|
setRetreatTick(map, chunk, tick)
|
2021-02-14 06:49:54 +02:00
|
|
|
local exitPath,exitDirection,
|
|
|
|
nextExitPath,nextExitDirection = scoreNeighborsForRetreat(chunk,
|
|
|
|
getNeighborChunks(map,
|
|
|
|
chunk.x,
|
|
|
|
chunk.y),
|
|
|
|
scoreRetreatLocation,
|
|
|
|
map)
|
2020-05-24 05:47:14 +02:00
|
|
|
local position = map.position
|
|
|
|
local targetPosition2 = map.position2
|
|
|
|
local retreatPosition
|
|
|
|
position.x = chunk.x + 16
|
|
|
|
position.y = chunk.y + 16
|
2021-02-20 07:41:30 +02:00
|
|
|
local surface = map.surface
|
2020-05-24 05:47:14 +02:00
|
|
|
if (exitPath == -1) then
|
|
|
|
return
|
|
|
|
elseif (nextExitPath ~= -1) then
|
|
|
|
positionFromDirectionAndFlat(exitDirection, position, targetPosition2)
|
|
|
|
positionFromDirectionAndFlat(nextExitDirection, targetPosition2, position)
|
|
|
|
retreatPosition = findMovementPosition(surface, position)
|
|
|
|
exitPath = nextExitPath
|
|
|
|
else
|
|
|
|
positionFromDirectionAndFlat(exitDirection, position, targetPosition2)
|
|
|
|
retreatPosition = findMovementPosition(surface, targetPosition2)
|
2020-05-17 07:06:55 +02:00
|
|
|
end
|
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
if retreatPosition then
|
|
|
|
position.x = retreatPosition.x
|
|
|
|
position.y = retreatPosition.y
|
|
|
|
else
|
|
|
|
return
|
2019-10-14 07:49:52 +02:00
|
|
|
end
|
2020-10-19 05:33:01 +02:00
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
local newSquad = findNearbyRetreatingSquad(map, exitPath)
|
2021-02-20 09:31:36 +02:00
|
|
|
local universe = map.universe
|
2020-05-24 05:47:14 +02:00
|
|
|
local created = false
|
2020-10-19 05:33:01 +02:00
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
if not newSquad then
|
2021-02-20 09:31:36 +02:00
|
|
|
if (universe.squadCount < universe.AI_MAX_SQUAD_COUNT) then
|
2020-05-25 01:41:12 +02:00
|
|
|
created = true
|
2020-10-19 05:33:01 +02:00
|
|
|
newSquad = createSquad(position, surface)
|
2020-05-25 01:41:12 +02:00
|
|
|
else
|
|
|
|
return
|
2020-10-19 05:33:01 +02:00
|
|
|
end
|
2020-05-24 05:47:14 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
map.fleeCommand.from = cause
|
|
|
|
map.retreatCommand.group = newSquad.group
|
2019-10-14 07:49:52 +02:00
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
map.formRetreatCommand.unit_search_distance = radius
|
|
|
|
|
|
|
|
local foundUnits = surface.set_multi_command(map.formRetreatCommand)
|
2020-10-19 05:33:01 +02:00
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
if (foundUnits == 0) then
|
|
|
|
if created then
|
|
|
|
newSquad.group.destroy()
|
2019-10-14 07:49:52 +02:00
|
|
|
end
|
2020-05-24 05:47:14 +02:00
|
|
|
return
|
|
|
|
end
|
|
|
|
|
2020-10-19 05:33:01 +02:00
|
|
|
if created then
|
2021-02-20 09:31:36 +02:00
|
|
|
map.groupNumberToSquad[newSquad.groupNumber] = newSquad
|
|
|
|
universe.squadCount = universe.squadCount + 1
|
2019-10-14 07:49:52 +02:00
|
|
|
end
|
2020-05-24 05:47:14 +02:00
|
|
|
|
|
|
|
newSquad.status = SQUAD_RETREATING
|
|
|
|
|
|
|
|
addSquadToChunk(map, chunk, newSquad)
|
|
|
|
|
|
|
|
newSquad.frenzy = true
|
|
|
|
local squadPosition = newSquad.group.position
|
|
|
|
newSquad.frenzyPosition.x = squadPosition.x
|
|
|
|
newSquad.frenzyPosition.y = squadPosition.y
|
2016-08-18 07:55:08 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-16 06:17:30 +02:00
|
|
|
aiDefenseG = aiDefense
|
2016-08-30 06:08:22 +02:00
|
|
|
return aiDefense
|