1
0
mirror of https://github.com/veden/Rampant.git synced 2025-01-18 02:58:32 +02:00
Rampant/libs/SquadDefense.lua

135 lines
4.9 KiB
Lua
Raw Normal View History

2019-02-15 20:17:30 -08: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-14 17:00:18 -07:00
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
2016-10-14 17:00:18 -07:00
local BASE_PHEROMONE = constants.BASE_PHEROMONE
2019-02-10 22:14:17 -08:00
local PLAYER_PHEROMONE_MULTIPLER = constants.PLAYER_PHEROMONE_MULTIPLER
local SQUAD_RETREATING = constants.SQUAD_RETREATING
2018-02-11 19:21:28 -08:00
local INTERVAL_RETREAT = constants.INTERVAL_RETREAT
2017-04-21 16:14:04 -07:00
2017-11-20 23:27:03 -08:00
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
2017-05-11 21:50:06 -07:00
-- imported functions
local mRandom = math.random
local addSquadToChunk = chunkPropetyUtils.addSquadToChunk
2018-01-13 21:48:21 -08:00
local calculateKamikazeThreshold = unitGroupUtils.calculateKamikazeThreshold
2017-06-07 17:57:24 -07:00
local positionFromDirectionAndChunk = mapUtils.positionFromDirectionAndChunk
local getNeighborChunks = mapUtils.getNeighborChunks
2019-03-09 15:16:35 -08:00
local findNearbyRetreatingSquad = unitGroupUtils.findNearbyRetreatingSquad
local addMovementPenalty = movementUtils.addMovementPenalty
local createSquad = unitGroupUtils.createSquad
local membersToSquad = unitGroupUtils.membersToSquad
2017-11-20 23:27:03 -08: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-20 23:27:03 -08:00
-- module code
2018-01-13 21:48:21 -08:00
local function scoreRetreatLocation(map, neighborChunk)
2019-03-08 22:37:29 -08:00
return (-neighborChunk[BASE_PHEROMONE] +
neighborChunk[MOVEMENT_PHEROMONE] +
-(neighborChunk[PLAYER_PHEROMONE] * PLAYER_PHEROMONE_MULTIPLER) +
-(getPlayerBaseGenerator(map, neighborChunk) * 1000))
end
2019-03-09 19:57:58 -08:00
function aiDefense.retreatUnits(chunk, position, squad, map, surface, natives, tick, radius, artilleryBlast)
if (tick - getRetreatTick(map, chunk) > INTERVAL_RETREAT) and
2019-03-09 19:57:58 -08:00
((getEnemyStructureCount(map, chunk) == 0) or artilleryBlast)
then
2016-11-03 16:51:35 -07:00
local performRetreat = false
2017-05-27 21:50:37 -07:00
local enemiesToSquad = nil
2017-05-27 21:50:37 -07:00
if not squad then
enemiesToSquad = surface.find_enemy_units(position, radius)
performRetreat = #enemiesToSquad > 0
if (mRandom() < calculateKamikazeThreshold(#enemiesToSquad, natives)) then
2018-02-11 19:21:28 -08:00
setRetreatTick(map, chunk, tick)
2019-03-07 19:40:55 -08:00
return
end
2018-02-06 23:57:41 -08:00
elseif squad.group and squad.group.valid and (squad.status ~= SQUAD_RETREATING) and not squad.kamikaze then
2019-03-07 19:40:55 -08:00
performRetreat = #squad.group.members > 3
2016-11-03 16:51:35 -07:00
end
2016-11-03 16:51:35 -07:00
if performRetreat then
2018-01-13 21:48:21 -08:00
setRetreatTick(map, chunk, tick)
2017-06-10 17:59:06 -07:00
local exitPath,exitDirection = scoreNeighborsForRetreat(chunk,
2018-01-13 21:48:21 -08:00
getNeighborChunks(map, chunk.x, chunk.y),
2017-11-20 23:27:03 -08:00
scoreRetreatLocation,
2018-01-13 21:48:21 -08:00
map)
2017-11-20 23:27:03 -08: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 01:38:20 -07:00
end
2016-11-03 16:51:35 -07: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-09 15:16:35 -08:00
local newSquad = findNearbyRetreatingSquad(map, exitPath)
2017-08-08 01:19:51 -07:00
if not newSquad then
2019-03-06 22:12:39 -08:00
newSquad = createSquad(retreatPosition, surface)
natives.squads[#natives.squads+1] = newSquad
2016-11-03 16:51:35 -07:00
end
2017-08-08 01:19:51 -07:00
if newSquad then
2019-03-06 22:12:39 -08:00
newSquad.status = SQUAD_RETREATING
2019-03-07 19:40:55 -08:00
newSquad.cycles = 4
2019-03-06 22:12:39 -08:00
2018-01-13 21:48:21 -08:00
local cmd = map.retreatCommand
cmd.group = newSquad.group
2017-08-08 01:19:51 -07:00
if enemiesToSquad then
2018-02-11 19:21:28 -08:00
membersToSquad(cmd, enemiesToSquad, artilleryBlast)
2019-03-08 22:37:29 -08:00
else
2018-01-13 21:48:21 -08:00
membersToSquad(cmd, squad.group.members, true)
2017-08-08 01:19:51 -07:00
if squad.rabid then
newSquad.rabid = true
end
end
2019-03-07 19:40:55 -08: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-13 21:48:21 -08:00
addSquadToChunk(map, chunk, newSquad)
2019-03-08 22:23:00 -08:00
addMovementPenalty(newSquad, chunk)
2016-11-03 16:51:35 -07:00
end
end
end
end
end
2019-02-15 20:17:30 -08:00
aiDefenseG = aiDefense
return aiDefense