1
0
mirror of https://github.com/veden/Rampant.git synced 2024-12-30 21:19:46 +02:00
Rampant/libs/SquadDefense.lua

166 lines
6.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
2020-05-20 04:37:16 +02:00
local COOLDOWN_RETREAT = constants.COOLDOWN_RETREAT
2017-04-22 01:14:04 +02:00
-- imported functions
local mRandom = math.random
local addSquadToChunk = chunkPropetyUtils.addSquadToChunk
2018-01-14 07:48:21 +02:00
local calculateKamikazeThreshold = unitGroupUtils.calculateKamikazeThreshold
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
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
2020-05-20 04:37:16 +02:00
function aiDefense.retreatUnits(chunk, position, group, map, surface, tick, radius)
if (tick - getRetreatTick(map, chunk) > COOLDOWN_RETREAT) and (getEnemyStructureCount(map, chunk) == 0) then
local performRetreat = false
local enemiesToSquad = nil
2020-05-17 07:06:55 +02:00
local squad
2020-05-20 04:37:16 +02:00
local groupMembers
2020-05-17 07:06:55 +02:00
if group and group.valid then
squad = map.natives.groupNumberToSquad[group.group_number]
if not squad then
squad = createSquad(position, surface, group)
2020-05-20 04:37:16 +02:00
groupMembers = group.members
squad.kamikaze = mRandom() < calculateKamikazeThreshold(#groupMembers, natives)
2020-05-17 07:06:55 +02:00
end
end
if not squad then
2020-05-20 04:37:16 +02:00
print("grabbing people", position.x, position.y)
2019-10-20 22:45:43 +02:00
enemiesToSquad = map.enemiesToSquad
local unitCount = 0
local units = surface.find_enemy_units(position, radius)
for i=1,#units do
local unit = units[i]
if not unit.unit_group then
unitCount = unitCount + 1
enemiesToSquad[unitCount] = unit
end
end
enemiesToSquad.len = unitCount
2019-11-30 02:49:22 +02:00
if (mRandom() < calculateKamikazeThreshold(unitCount, map.natives)) then
2020-05-20 04:37:16 +02:00
performRetreat = false
else
performRetreat = unitCount > 6
end
elseif squad.group and squad.group.valid and (squad.status ~= SQUAD_RETREATING) and not squad.kamikaze then
2020-05-20 04:37:16 +02:00
performRetreat = #groupMembers > 6
end
2020-05-20 04:37:16 +02:00
setRetreatTick(map, chunk, tick)
if performRetreat then
2019-10-20 22:45:43 +02:00
local exitPath,exitDirection,nextExitPath,nextExitDirection = scoreNeighborsForRetreat(chunk,
getNeighborChunks(map,
chunk.x,
chunk.y),
scoreRetreatLocation,
map)
2020-05-15 22:51:38 +02:00
if (exitPath ~= -1) then
2019-10-20 22:45:43 +02:00
local targetPosition = map.position
2020-02-02 21:30:50 +02:00
local targetPosition2 = map.position2
2019-10-20 22:45:43 +02:00
positionFromDirectionAndFlat(exitDirection, position, targetPosition)
local retreatPosition = findMovementPosition(surface, targetPosition)
if not retreatPosition then
return
end
2020-05-15 22:51:38 +02:00
if (nextExitPath ~= -1) then
2019-10-20 22:45:43 +02:00
positionFromDirectionAndFlat(nextExitDirection, retreatPosition, targetPosition2)
2020-02-02 21:30:50 +02:00
2019-10-20 22:45:43 +02:00
local retreatPosition2 = findMovementPosition(surface, targetPosition2)
if retreatPosition2 then
retreatPosition.x = retreatPosition2.x
retreatPosition.y = retreatPosition2.y
end
end
-- 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
local newSquad = findNearbyRetreatingSquad(map, exitPath)
2020-02-02 21:30:50 +02:00
if not newSquad then
newSquad = createSquad(retreatPosition, surface)
2020-05-17 07:06:55 +02:00
map.natives.groupNumberToSquad[newSquad.groupNumber] = newSquad
end
2020-02-02 21:30:50 +02:00
if newSquad then
2019-03-07 08:12:39 +02:00
newSquad.status = SQUAD_RETREATING
2020-02-02 21:30:50 +02:00
local cmd = map.retreatCommand
cmd.group = newSquad.group
if enemiesToSquad then
2019-10-20 22:45:43 +02:00
membersToSquad(cmd, enemiesToSquad.len, enemiesToSquad, artilleryBlast)
else
2020-05-20 04:37:16 +02:00
membersToSquad(cmd, #groupMembers, groupMembers, true)
if squad.rabid then
newSquad.rabid = true
end
end
2020-02-02 21:30:50 +02:00
2020-05-15 22:51:38 +02:00
if not newSquad.rabid then
2019-03-08 05:40:55 +02:00
newSquad.frenzy = true
local squadPosition = newSquad.group.position
newSquad.frenzyPosition.x = squadPosition.x
newSquad.frenzyPosition.y = squadPosition.y
end
addMovementPenalty(map, newSquad, chunk)
2020-05-17 00:34:54 +02:00
addSquadToChunk(map, chunk, newSquad)
end
end
end
end
end
2019-02-16 06:17:30 +02:00
aiDefenseG = aiDefense
return aiDefense