1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-03 22:52:20 +02:00
Rampant/libs/SquadDefense.lua

121 lines
4.3 KiB
Lua
Raw Normal View History

2019-02-16 06:17:30 +02:00
if aiDefenseG then
return aiDefenseG
end
local aiDefense = {}
-- imports
local constants = require("Constants")
local mapUtils = require("MapUtils")
local unitGroupUtils = require("UnitGroupUtils")
local movementUtils = require("MovementUtils")
local chunkPropetyUtils = require("ChunkPropertyUtils")
-- constants
2018-01-14 07:48:21 +02:00
-- local RETREAT_SPAWNER_GRAB_RADIUS = constants.RETREAT_SPAWNER_GRAB_RADIUS
2016-10-15 02:00:18 +02:00
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
2016-10-15 02:00:18 +02:00
local BASE_PHEROMONE = constants.BASE_PHEROMONE
2019-02-11 08:14:17 +02:00
local PLAYER_PHEROMONE_MULTIPLER = constants.PLAYER_PHEROMONE_MULTIPLER
local SQUAD_RETREATING = constants.SQUAD_RETREATING
2018-02-12 05:21:28 +02:00
local INTERVAL_RETREAT = constants.INTERVAL_RETREAT
2017-04-22 01:14:04 +02:00
2017-11-21 09:27:03 +02:00
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
2017-05-12 06:50:06 +02:00
-- imported functions
local mRandom = math.random
local addSquadToChunk = chunkPropetyUtils.addSquadToChunk
2018-01-14 07:48:21 +02:00
local calculateKamikazeThreshold = unitGroupUtils.calculateKamikazeThreshold
2017-06-08 02:57:24 +02:00
local positionFromDirectionAndChunk = mapUtils.positionFromDirectionAndChunk
local getNeighborChunks = mapUtils.getNeighborChunks
2018-01-14 07:48:21 +02:00
local findNearbySquadFiltered = unitGroupUtils.findNearbySquadFiltered
local addMovementPenalty = movementUtils.addMovementPenalty
local createSquad = unitGroupUtils.createSquad
local membersToSquad = unitGroupUtils.membersToSquad
2017-11-21 09:27:03 +02:00
local scoreNeighborsForRetreat = movementUtils.scoreNeighborsForRetreat
local findMovementPosition = movementUtils.findMovementPosition
local getRetreatTick = chunkPropetyUtils.getRetreatTick
local getPlayerBaseGenerator = chunkPropetyUtils.getPlayerBaseGenerator
local setRetreatTick = chunkPropetyUtils.setRetreatTick
local getEnemyStructureCount = chunkPropetyUtils.getEnemyStructureCount
2017-11-21 09:27:03 +02:00
-- module code
2018-01-14 07:48:21 +02:00
local function scoreRetreatLocation(map, neighborChunk)
2019-02-11 08:14:17 +02:00
return -(neighborChunk[BASE_PHEROMONE] + neighborChunk[MOVEMENT_PHEROMONE] + -(neighborChunk[PLAYER_PHEROMONE] * PLAYER_PHEROMONE_MULTIPLER) + -(getPlayerBaseGenerator(map, neighborChunk)))
end
2018-02-12 05:21:28 +02:00
function aiDefense.retreatUnits(chunk, position, squad, map, surface, natives, tick, radius, artilleryBlast, force)
if (tick - getRetreatTick(map, chunk) > INTERVAL_RETREAT) and ((getEnemyStructureCount(map, chunk) == 0) or artilleryBlast or force) then
2016-11-04 01:51:35 +02:00
local performRetreat = false
2017-05-28 06:50:37 +02:00
local enemiesToSquad = nil
2017-05-28 06:50:37 +02:00
if not squad then
enemiesToSquad = surface.find_enemy_units(position, radius)
performRetreat = #enemiesToSquad > 0
if (mRandom() < calculateKamikazeThreshold(#enemiesToSquad, natives)) then
2018-02-12 05:21:28 +02:00
setRetreatTick(map, chunk, tick)
performRetreat = false
end
2018-02-07 09:57:41 +02:00
elseif squad.group and squad.group.valid and (squad.status ~= SQUAD_RETREATING) and not squad.kamikaze then
performRetreat = #squad.group.members > 1
2016-11-04 01:51:35 +02:00
end
2016-11-04 01:51:35 +02:00
if performRetreat then
2018-01-14 07:48:21 +02:00
setRetreatTick(map, chunk, tick)
2017-06-11 02:59:06 +02:00
local exitPath,exitDirection = scoreNeighborsForRetreat(chunk,
2018-01-14 07:48:21 +02:00
getNeighborChunks(map, chunk.x, chunk.y),
2017-11-21 09:27:03 +02:00
scoreRetreatLocation,
2018-01-14 07:48:21 +02:00
map)
2017-11-21 09:27:03 +02:00
if (exitPath ~= SENTINEL_IMPASSABLE_CHUNK) then
local retreatPosition = findMovementPosition(surface,
2018-01-14 07:48:21 +02:00
positionFromDirectionAndChunk(exitDirection, position, map.position, 0.98),
false)
if not retreatPosition then
return
2017-06-10 10:38:20 +02:00
end
2016-11-04 01:51:35 +02:00
-- in order for units in a group attacking to retreat, we have to create a new group and give the command to join
-- to each unit, this is the only way I have found to have snappy mid battle retreats even after 0.14.4
2018-01-14 07:48:21 +02:00
local newSquad = findNearbySquadFiltered(map, exitPath, retreatPosition)
2017-08-08 10:19:51 +02:00
if not newSquad then
2016-11-04 01:51:35 +02:00
newSquad = createSquad(retreatPosition, surface, natives)
newSquad.status = SQUAD_RETREATING
newSquad.cycles = 4
end
2017-08-08 10:19:51 +02:00
if newSquad then
2018-01-14 07:48:21 +02:00
local cmd = map.retreatCommand
cmd.group = newSquad.group
2017-08-08 10:19:51 +02:00
if enemiesToSquad then
2018-02-12 05:21:28 +02:00
membersToSquad(cmd, enemiesToSquad, artilleryBlast)
2017-08-08 10:19:51 +02:00
else
2018-01-14 07:48:21 +02:00
membersToSquad(cmd, squad.group.members, true)
2017-08-08 10:19:51 +02:00
newSquad.penalties = squad.penalties
if squad.rabid then
newSquad.rabid = true
end
end
2018-01-14 07:48:21 +02:00
addSquadToChunk(map, chunk, newSquad)
2017-12-21 05:50:36 +02:00
addMovementPenalty(natives, newSquad, chunk)
2016-11-04 01:51:35 +02:00
end
end
end
end
end
2019-02-16 06:17:30 +02:00
aiDefenseG = aiDefense
return aiDefense