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

148 lines
5.7 KiB
Lua
Raw Normal View History

local squadAttack = {}
-- imports
local constants = require("Constants")
local mapUtils = require("MapUtils")
local unitGroupUtils = require("UnitGroupUtils")
local playerUtils = require("PlayerUtils")
local movementUtils = require("MovementUtils")
local mathUtils = require("MathUtils")
local chunkPropertyUtils = require("ChunkPropertyUtils")
-- constants
2017-05-14 00:32:16 +02:00
local PLAYER_PHEROMONE = constants.PLAYER_PHEROMONE
2016-10-15 02:00:18 +02:00
local MOVEMENT_PHEROMONE = constants.MOVEMENT_PHEROMONE
local BASE_PHEROMONE = constants.BASE_PHEROMONE
local SQUAD_RAIDING = constants.SQUAD_RAIDING
local SQUAD_GUARDING = constants.SQUAD_GUARDING
2017-06-11 02:59:06 +02:00
local PLAYER_PHEROMONE_MULTIPLER = constants.PLAYER_PHEROMONE_MULTIPLER
2017-05-27 02:58:33 +02:00
local DEFINES_GROUP_FINISHED = defines.group_state.finished
local DEFINES_GROUP_GATHERING = defines.group_state.gathering
local DEFINES_GROUP_MOVING = defines.group_state.moving
local DEFINES_GROUP_ATTACKING_DISTRACTION = defines.group_state.attacking_distraction
local DEFINES_GROUP_ATTACKING_TARGET = defines.group_state.attacking_target
local DEFINES_DISTRACTION_BY_ENEMY = defines.distraction.by_enemy
local DEFINES_DISTRACTION_BY_ANYTHING = defines.distraction.by_anything
2017-11-21 09:27:03 +02:00
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
-- imported functions
local mRandom = math.random
local findMovementPosition = movementUtils.findMovementPosition
2017-06-08 02:57:24 +02:00
local getNeighborChunks = mapUtils.getNeighborChunks
local addSquadToChunk = chunkPropertyUtils.addSquadToChunk
2018-01-14 07:48:21 +02:00
local getChunkByXY = mapUtils.getChunkByXY
local positionToChunkXY = mapUtils.positionToChunkXY
local addMovementPenalty = movementUtils.addMovementPenalty
local lookupMovementPenalty = movementUtils.lookupMovementPenalty
2017-01-20 07:58:36 +02:00
local calculateKamikazeThreshold = unitGroupUtils.calculateKamikazeThreshold
2016-10-31 05:24:14 +02:00
local positionFromDirectionAndChunk = mapUtils.positionFromDirectionAndChunk
local euclideanDistanceNamed = mathUtils.euclideanDistanceNamed
local playersWithinProximityToPosition = playerUtils.playersWithinProximityToPosition
local getPlayerBaseGenerator = chunkPropertyUtils.getPlayerBaseGenerator
2017-11-21 09:27:03 +02:00
local scoreNeighborsForAttack = movementUtils.scoreNeighborsForAttack
-- module code
2017-06-11 02:59:06 +02:00
local function scoreAttackLocation(squad, neighborChunk)
2017-06-24 20:41:57 +02:00
local damage = (2*neighborChunk[MOVEMENT_PHEROMONE]) + neighborChunk[BASE_PHEROMONE] + (neighborChunk[PLAYER_PHEROMONE] * PLAYER_PHEROMONE_MULTIPLER)
2017-12-21 05:50:36 +02:00
return damage - lookupMovementPenalty(squad, neighborChunk)
end
2018-01-14 07:48:21 +02:00
function squadAttack.squadsAttack(map, surface, natives)
2016-08-26 00:20:06 +02:00
local squads = natives.squads
2018-01-14 07:48:21 +02:00
local attackPosition = map.position
local attackCmd = map.attackAreaCommand
2016-08-26 00:20:06 +02:00
for i=1,#squads do
local squad = squads[i]
local group = squad.group
if group.valid and (squad.status == SQUAD_RAIDING) then
local groupState = group.state
2017-05-27 02:58:33 +02:00
if (groupState == DEFINES_GROUP_FINISHED) or (groupState == DEFINES_GROUP_GATHERING) or ((groupState == DEFINES_GROUP_MOVING) and (squad.cycles == 0)) then
local groupPosition = group.position
2018-01-14 07:48:21 +02:00
local x, y = positionToChunkXY(groupPosition)
local chunk = getChunkByXY(map, x, y)
local attackChunk, attackDirection = scoreNeighborsForAttack(chunk,
getNeighborChunks(map, x, y),
scoreAttackLocation,
squad)
2017-11-21 09:27:03 +02:00
if (chunk ~= SENTINEL_IMPASSABLE_CHUNK) then
2018-01-14 07:48:21 +02:00
addSquadToChunk(map, chunk, squad)
2017-12-21 05:50:36 +02:00
addMovementPenalty(natives, squad, chunk)
2018-01-14 07:48:21 +02:00
elseif (attackChunk ~= SENTINEL_IMPASSABLE_CHUNK) then
addSquadToChunk(map, attackChunk, squad)
addMovementPenalty(natives, squad, attackChunk)
end
if group.valid and (attackChunk ~= SENTINEL_IMPASSABLE_CHUNK) then
local playerBaseGenerator = getPlayerBaseGenerator(map, attackChunk)
if (playerBaseGenerator == 0) or ((groupState == DEFINES_GROUP_FINISHED) or (groupState == DEFINES_GROUP_GATHERING)) then
squad.cycles = ((#squad.group.members > 80) and 6) or 4
local moreFrenzy = not squad.rabid and squad.frenzy and (euclideanDistanceNamed(groupPosition, squad.frenzyPosition) < 100)
squad.frenzy = moreFrenzy
if squad.rabid or squad.frenzy then
attackCmd.distraction = DEFINES_DISTRACTION_BY_ANYTHING
else
attackCmd.distraction = DEFINES_DISTRACTION_BY_ENEMY
end
local position = findMovementPosition(surface, positionFromDirectionAndChunk(attackDirection, groupPosition, attackPosition, 1.35))
if position then
attackPosition.x = position.x
attackPosition.y = position.y
group.set_command(attackCmd)
group.start_moving()
else
addMovementPenalty(natives, squad, attackChunk)
end
2018-01-14 07:48:21 +02:00
elseif not squad.frenzy and not squad.rabid and
((groupState == DEFINES_GROUP_ATTACKING_DISTRACTION) or (groupState == DEFINES_GROUP_ATTACKING_TARGET) or
(playerBaseGenerator ~= 0)) then
squad.frenzy = true
squad.frenzyPosition.x = groupPosition.x
squad.frenzyPosition.y = groupPosition.y
2016-11-04 01:51:35 +02:00
end
end
end
end
end
end
function squadAttack.squadsBeginAttack(natives, players)
2016-08-26 00:20:06 +02:00
local squads = natives.squads
for i=1,#squads do
local squad = squads[i]
2017-06-01 03:46:53 +02:00
local group = squad.group
if (squad.status == SQUAD_GUARDING) and group.valid then
local kamikazeThreshold = calculateKamikazeThreshold(#squad.group.members, natives)
local groupPosition = group.position
2017-12-21 05:50:36 +02:00
if playersWithinProximityToPosition(players, groupPosition, 100) then
squad.frenzy = true
2017-06-01 03:46:53 +02:00
squad.frenzyPosition.x = groupPosition.x
squad.frenzyPosition.y = groupPosition.y
end
2017-05-14 00:32:16 +02:00
2017-12-21 05:50:36 +02:00
squad.kamikaze = mRandom() < kamikazeThreshold
squad.status = SQUAD_RAIDING
2016-10-15 02:00:18 +02:00
end
end
end
return squadAttack