1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-10 00:28:31 +02:00
Rampant/libs/SquadDefense.lua

135 lines
4.9 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
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
2019-03-10 01:16:35 +02:00
local findNearbyRetreatingSquad = unitGroupUtils.findNearbyRetreatingSquad
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-03-09 08:37:29 +02:00
return (-neighborChunk[BASE_PHEROMONE] +
neighborChunk[MOVEMENT_PHEROMONE] +
-(neighborChunk[PLAYER_PHEROMONE] * PLAYER_PHEROMONE_MULTIPLER) +
-(getPlayerBaseGenerator(map, neighborChunk) * 1000))
end
2019-03-10 05:57:58 +02:00
function aiDefense.retreatUnits(chunk, position, squad, map, surface, natives, tick, radius, artilleryBlast)
if (tick - getRetreatTick(map, chunk) > INTERVAL_RETREAT) and
2019-03-10 05:57:58 +02:00
((getEnemyStructureCount(map, chunk) == 0) or artilleryBlast)
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)
2019-03-08 05:40:55 +02:00
return
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
2019-03-08 05:40:55 +02:00
performRetreat = #squad.group.members > 3
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,
positionFromDirectionAndChunk(exitDirection,
position,
map.position,
0.98))
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
2019-03-10 01:16:35 +02:00
local newSquad = findNearbyRetreatingSquad(map, exitPath)
2017-08-08 10:19:51 +02:00
if not newSquad then
2019-03-07 08:12:39 +02:00
newSquad = createSquad(retreatPosition, surface)
natives.squads[#natives.squads+1] = newSquad
2016-11-04 01:51:35 +02:00
end
2017-08-08 10:19:51 +02:00
if newSquad then
2019-03-07 08:12:39 +02:00
newSquad.status = SQUAD_RETREATING
2019-03-08 05:40:55 +02:00
newSquad.cycles = 4
2019-03-07 08:12:39 +02:00
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)
2019-03-09 08:37:29 +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
if squad.rabid then
newSquad.rabid = true
end
end
2019-03-08 05:40:55 +02:00
if not newSquad.rapid then
newSquad.frenzy = true
local squadPosition = newSquad.group.position
newSquad.frenzyPosition.x = squadPosition.x
newSquad.frenzyPosition.y = squadPosition.y
end
2018-01-14 07:48:21 +02:00
addSquadToChunk(map, chunk, newSquad)
2019-03-09 08:23:00 +02:00
addMovementPenalty(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