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

247 lines
7.8 KiB
Lua
Raw Normal View History

local unitGroupUtils = {}
-- imports
local mapUtils = require("MapUtils")
2016-08-07 05:38:47 +02:00
local constants = require("Constants")
-- constants
2016-10-15 02:00:18 +02:00
local MOVEMENT_PHEROMONE_GENERATOR_AMOUNT = constants.MOVEMENT_PHEROMONE_GENERATOR_AMOUNT
local GROUP_STATE_FINISHED = defines.group_state.finished
2017-01-20 07:58:36 +02:00
local GROUP_STATE_ATTACKING_TARGET = defines.group_state.attacking_target
local GROUP_STATE_ATTACKING_DISTRACTION = defines.group_state.attacking_distraction
local SQUAD_RETREATING = constants.SQUAD_RETREATING
local SQUAD_GUARDING = constants.SQUAD_GUARDING
2017-01-20 07:58:36 +02:00
local GROUP_MERGE_DISTANCE = constants.GROUP_MERGE_DISTANCE
local NO_RETREAT_BASE_PERCENT = constants.NO_RETREAT_BASE_PERCENT
local NO_RETREAT_EVOLUTION_BONUS_MAX = constants.NO_RETREAT_EVOLUTION_BONUS_MAX
local NO_RETREAT_SQUAD_SIZE_BONUS_MAX = constants.NO_RETREAT_SQUAD_SIZE_BONUS_MAX
local AI_MAX_POINTS = constants.AI_MAX_POINTS
local AI_UNIT_REFUND = constants.AI_UNIT_REFUND
2017-04-16 08:04:22 +02:00
local AI_MAX_BITER_GROUP_SIZE = constants.AI_MAX_BITER_GROUP_SIZE
-- imported functions
2017-01-20 07:58:36 +02:00
local mLog = math.log10
local tableRemove = table.remove
local tableInsert = table.insert
local euclideanDistanceNamed = mapUtils.euclideanDistanceNamed
-- module code
2017-01-20 07:58:36 +02:00
2016-08-08 03:35:36 +02:00
function unitGroupUtils.findNearBySquad(natives, position, distance, filter)
local squads = natives.squads
2016-08-26 00:20:06 +02:00
for i=1,#squads do
local squad = squads[i]
2016-08-07 05:38:47 +02:00
local unitGroup = squad.group
2017-04-22 01:14:04 +02:00
if unitGroup.valid and ((filter == nil) or (filter ~= nil and filter[squad.status])) then
if (euclideanDistanceNamed(unitGroup.position, position) <= distance) then
return squad
end
end
end
end
2016-08-07 05:38:47 +02:00
function unitGroupUtils.createSquad(position, surface, natives)
local unitGroup = surface.create_unit_group({position=position})
local squad = { group = unitGroup,
status = SQUAD_GUARDING,
penalties = {},
rabid = false,
frenzy = false,
2016-11-04 01:51:35 +02:00
kamikaze = false,
frenzyPosition = {x = 0,
y = 0},
cycles = 0 }
2016-08-07 05:38:47 +02:00
natives.squads[#natives.squads+1] = squad
return squad
end
function unitGroupUtils.membersToSquad(squad, members, overwriteGroup)
2016-08-08 03:35:36 +02:00
if (members ~= nil) then
local cmd = { type = defines.command.group,
group = squad.group,
distraction = defines.distraction.none }
for i=1,#members do
2016-08-07 05:38:47 +02:00
local member = members[i]
2016-08-26 00:20:06 +02:00
if member.valid and (overwriteGroup or (not overwriteGroup and (member.unit_group == nil))) then
member.set_command(cmd)
2016-08-07 05:38:47 +02:00
end
end
end
end
function unitGroupUtils.convertUnitGroupToSquad(natives, unitGroup)
local returnSquad
if (unitGroup ~= nil) then
local squads = natives.squads
2016-08-26 00:20:06 +02:00
for i=1,#squads do
2016-08-07 05:38:47 +02:00
local squad = squads[i]
2016-08-08 03:35:36 +02:00
if (squad.group == unitGroup) then
return squad
2016-08-07 05:38:47 +02:00
end
end
returnSquad = { group = unitGroup,
status = SQUAD_GUARDING,
penalties = {},
rabid = false,
frenzy = false,
2016-11-04 01:51:35 +02:00
kamikaze = false,
frenzyPosition = {x = 0,
y = 0},
cycles = 0 }
squads[#squads+1] = returnSquad
2016-08-07 05:38:47 +02:00
end
return returnSquad
end
function unitGroupUtils.addSquadMovementPenalty(squad, chunkX, chunkY)
local penalties = squad.penalties
2016-08-26 00:20:06 +02:00
for i=1,#penalties do
local penalty = penalties[i]
if (penalty.x == chunkX) and (penalty.y == chunkY) then
2016-10-15 02:00:18 +02:00
penalty.v = penalty.v + MOVEMENT_PHEROMONE_GENERATOR_AMOUNT
return
end
end
if (#penalties == 10) then
tableRemove(penalties, 10)
end
2016-10-15 02:00:18 +02:00
tableInsert(penalties, 1, { v = MOVEMENT_PHEROMONE_GENERATOR_AMOUNT,
x = chunkX,
y = chunkY })
end
function unitGroupUtils.lookupSquadMovementPenalty(squad, chunkX, chunkY)
local penalties = squad.penalties
2016-08-26 00:20:06 +02:00
for i=1,#penalties do
local penalty = penalties[i]
if (penalty.x == chunkX) and (penalty.y == chunkY) then
return penalty.v
end
end
return 0
end
2016-08-07 05:38:47 +02:00
2017-05-06 11:03:28 +02:00
function unitGroupUtils.calculateKamikazeThreshold(squad, natives, evolution_factor)
local maxSize = natives.attackWaveMaxSize
2017-01-20 07:58:36 +02:00
local kamikazeThreshold = NO_RETREAT_BASE_PERCENT + (evolution_factor * NO_RETREAT_EVOLUTION_BONUS_MAX)
2017-05-06 11:03:28 +02:00
local squadSizeBonus = mLog((#squad.group.members / maxSize) + 0.1) + 1
2017-01-20 07:58:36 +02:00
return kamikazeThreshold + (NO_RETREAT_SQUAD_SIZE_BONUS_MAX * squadSizeBonus)
end
2017-04-22 01:14:04 +02:00
local function isAttacking(group)
local state = group.state
return (state == GROUP_STATE_ATTACKING_TARGET) or (state == GROUP_STATE_ATTACKING_DISTRACTION)
2017-01-20 07:58:36 +02:00
end
function unitGroupUtils.cleanSquads(natives, evolution_factor)
2016-08-09 04:18:19 +02:00
local squads = natives.squads
2017-04-22 01:14:04 +02:00
local squadCount = #squads
local weight = AI_UNIT_REFUND * evolution_factor
local cleanSquads = {}
2017-04-22 01:14:04 +02:00
for i=1,squadCount do
local squad = squads[i]
local group = squad.group
if group.valid then
local memberCount = #group.members
if (memberCount == 0) then
group.destroy()
elseif (memberCount > AI_MAX_BITER_GROUP_SIZE) then
local members = group.members
for x=1,memberCount do
members[x].destroy()
natives.points = natives.points + weight
end
if (natives.points > (AI_MAX_POINTS * 3)) then
natives.points = (AI_MAX_POINTS * 3)
end
2017-04-22 01:14:04 +02:00
group.destroy()
else
local status = squad.status
local squadPosition = group.position
local cycles = squad.cycles
if (status == SQUAD_RETREATING) and (cycles == 0) then
squad.status = SQUAD_GUARDING
squad.frenzy = true
squad.frenzyPosition.x = squadPosition.x
squad.frenzyPosition.y = squadPosition.y
elseif (group.state == GROUP_STATE_FINISHED) then
squad.status = SQUAD_GUARDING
elseif (cycles > 0) then
squad.cycles = cycles - 1
end
cleanSquads[#cleanSquads+1] = squad
2017-01-20 07:58:36 +02:00
end
end
2016-08-09 04:18:19 +02:00
end
natives.squads = cleanSquads
end
2017-01-20 07:58:36 +02:00
function unitGroupUtils.regroupSquads(natives, evolution_factor)
2017-04-22 01:14:04 +02:00
local groupThreshold = AI_MAX_BITER_GROUP_SIZE * 0.75
local squads = natives.squads
local squadCount = #squads
2017-04-22 01:14:04 +02:00
for i=1,squadCount do
2017-01-20 07:58:36 +02:00
local squad = squads[i]
2017-04-22 01:14:04 +02:00
local group = squad.group
if group.valid and not isAttacking(group) then
2017-04-22 01:14:04 +02:00
local status = squad.status
local memberCount = #group.members
if (memberCount < groupThreshold) then
local squadPosition = group.position
2017-04-22 01:14:04 +02:00
local mergedSquads = false
for x=i+1,squadCount do
local mergeSquad = squads[x]
local mergeGroup = mergeSquad.group
if mergeGroup.valid and (mergeSquad.status == status) and not isAttacking(mergeGroup) and (euclideanDistanceNamed(squadPosition, mergeGroup.position) < GROUP_MERGE_DISTANCE) then
local mergeMembers = mergeGroup.members
local mergeCount = #mergeMembers
if ((mergeCount + memberCount) < AI_MAX_BITER_GROUP_SIZE) then
2017-04-22 01:14:04 +02:00
for memberIndex=1, mergeCount do
group.add_member(mergeMembers[memberIndex])
end
if mergeSquad.kamikaze then
squad.kamikaze = true
end
mergedSquads = true
mergeGroup.destroy()
end
memberCount = memberCount + mergeCount
if (memberCount > groupThreshold) then
break
end
end
2017-04-22 01:14:04 +02:00
end
if mergedSquads and not squad.kamikaze then
local kamikazeThreshold = unitGroupUtils.calculateKamikazeThreshold(squad, natives, evolution_factor)
if (math.random() < kamikazeThreshold) then
squad.kamikaze = true
end
2017-04-22 01:14:04 +02:00
end
end
end
end
end
return unitGroupUtils