2019-02-16 06:17:30 +02:00
|
|
|
if unitGroupUtilsG then
|
|
|
|
return unitGroupUtilsG
|
|
|
|
end
|
2016-08-05 06:47:51 +02:00
|
|
|
local unitGroupUtils = {}
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- imports
|
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
local mapUtils = require("MapUtils")
|
2019-03-09 02:42:20 +02:00
|
|
|
-- local mathUtils = require("MathUtils")
|
2016-08-07 05:38:47 +02:00
|
|
|
local constants = require("Constants")
|
2018-01-15 09:41:55 +02:00
|
|
|
local chunkPropertyUtils = require("ChunkPropertyUtils")
|
2016-08-05 06:47:51 +02:00
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- constants
|
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
-- local HALF_CHUNK_SIZE = constants.HALF_CHUNK_SIZE
|
2018-01-14 07:48:21 +02:00
|
|
|
|
2017-06-01 04:48:59 +02:00
|
|
|
local SQUAD_QUEUE_SIZE = constants.SQUAD_QUEUE_SIZE
|
|
|
|
|
2019-03-07 08:12:39 +02:00
|
|
|
-- local DEFINES_GROUP_STATE_FINISHED = defines.group_state.finished
|
2017-05-27 02:58:33 +02:00
|
|
|
local DEFINES_GROUP_STATE_ATTACKING_TARGET = defines.group_state.attacking_target
|
|
|
|
local DEFINES_GROUP_STATE_ATTACKING_DISTRACTION = defines.group_state.attacking_distraction
|
2016-08-18 07:55:08 +02:00
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
local SQUAD_RETREATING = constants.SQUAD_RETREATING
|
|
|
|
local SQUAD_GUARDING = constants.SQUAD_GUARDING
|
2019-03-07 08:12:39 +02:00
|
|
|
-- local SQUAD_SETTLING = constants.SQUAD_SETTLING
|
|
|
|
-- local SQUAD_BUILDING = constants.SQUAD_BUILDING
|
|
|
|
-- local GROUP_MERGE_DISTANCE = constants.GROUP_MERGE_DISTANCE
|
2017-01-20 07:58:36 +02:00
|
|
|
|
2019-03-07 08:12:39 +02:00
|
|
|
-- local RETREAT_FILTER = constants.RETREAT_FILTER
|
2018-01-14 07:48:21 +02:00
|
|
|
|
2017-01-20 07:58:36 +02:00
|
|
|
local NO_RETREAT_SQUAD_SIZE_BONUS_MAX = constants.NO_RETREAT_SQUAD_SIZE_BONUS_MAX
|
|
|
|
|
2017-06-01 03:46:53 +02:00
|
|
|
local AI_MAX_OVERFLOW_POINTS = constants.AI_MAX_OVERFLOW_POINTS
|
2017-05-24 08:46:23 +02:00
|
|
|
|
2017-04-16 08:04:22 +02:00
|
|
|
local AI_MAX_BITER_GROUP_SIZE = constants.AI_MAX_BITER_GROUP_SIZE
|
2017-06-01 09:03:07 +02:00
|
|
|
local AI_SQUAD_MERGE_THRESHOLD = constants.AI_SQUAD_MERGE_THRESHOLD
|
2017-04-16 08:04:22 +02:00
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
local SENTINEL_IMPASSABLE_CHUNK = constants.SENTINEL_IMPASSABLE_CHUNK
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- imported functions
|
|
|
|
|
2019-03-07 08:12:39 +02:00
|
|
|
local tRemove = table.remove
|
|
|
|
|
2017-07-01 06:36:23 +02:00
|
|
|
local mRandom = math.random
|
|
|
|
|
2017-01-20 07:58:36 +02:00
|
|
|
local mLog = math.log10
|
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
-- local removeSquadFromChunk = chunkPropertyUtils.removeSquadFromChunk
|
2019-03-07 08:12:39 +02:00
|
|
|
|
2017-06-01 04:48:59 +02:00
|
|
|
local mMin = math.min
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
local getSquadsOnChunk = chunkPropertyUtils.getSquadsOnChunk
|
2019-03-07 08:12:39 +02:00
|
|
|
-- local removeSquadFromChunk = chunkPropertyUtils.removeSquadFromChunk
|
2018-01-14 07:48:21 +02:00
|
|
|
|
|
|
|
local getNeighborChunks = mapUtils.getNeighborChunks
|
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
-- local euclideanDistanceNamed = mathUtils.euclideanDistanceNamed
|
2016-08-20 04:52:27 +02:00
|
|
|
|
|
|
|
-- module code
|
2017-01-20 07:58:36 +02:00
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
function unitGroupUtils.findNearbySquadFiltered(map, chunk)
|
|
|
|
|
|
|
|
local squads = getSquadsOnChunk(map, chunk)
|
|
|
|
for i=1,#squads do
|
|
|
|
local squad = squads[i]
|
2018-01-14 07:48:21 +02:00
|
|
|
local unitGroup = squad.group
|
2019-03-06 08:18:03 +02:00
|
|
|
if unitGroup and unitGroup.valid and (squad.status == SQUAD_RETREATING) then
|
2019-03-09 02:42:20 +02:00
|
|
|
return squad
|
|
|
|
end
|
2018-01-14 07:48:21 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
local neighbors = getNeighborChunks(map, chunk.x, chunk.y)
|
|
|
|
|
|
|
|
for i=1,#neighbors do
|
2019-03-09 02:42:20 +02:00
|
|
|
local neighbor = neighbors[i]
|
|
|
|
if neighbor ~= SENTINEL_IMPASSABLE_CHUNK then
|
|
|
|
squads = getSquadsOnChunk(map, neighbor)
|
|
|
|
for squadIndex=1,#squads do
|
|
|
|
local squad = squads[squadIndex]
|
|
|
|
local unitGroup = squad.group
|
|
|
|
if unitGroup and unitGroup.valid and (squad.status == SQUAD_RETREATING) then
|
|
|
|
return squad
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
2018-02-01 06:01:28 +02:00
|
|
|
return nil
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
function unitGroupUtils.findNearbySquad(map, chunk)
|
2018-02-01 06:01:28 +02:00
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
local squads = getSquadsOnChunk(map, chunk)
|
|
|
|
for i=1,#squads do
|
|
|
|
local squad = squads[i]
|
|
|
|
local unitGroup = squad.group
|
|
|
|
if unitGroup and unitGroup.valid then
|
|
|
|
return squad
|
|
|
|
end
|
2018-02-01 06:01:28 +02:00
|
|
|
end
|
2019-03-09 02:42:20 +02:00
|
|
|
|
2018-02-01 06:01:28 +02:00
|
|
|
|
|
|
|
local neighbors = getNeighborChunks(map, chunk.x, chunk.y)
|
2018-01-14 07:48:21 +02:00
|
|
|
|
2018-02-01 06:01:28 +02:00
|
|
|
for i=1,#neighbors do
|
2019-03-09 02:42:20 +02:00
|
|
|
local neighbor = neighbors[i]
|
|
|
|
if neighbor ~= SENTINEL_IMPASSABLE_CHUNK then
|
|
|
|
squads = getSquadsOnChunk(map, neighbor)
|
|
|
|
for squadIndex=1,#squads do
|
|
|
|
local squad = squads[squadIndex]
|
|
|
|
local unitGroup = squad.group
|
|
|
|
if unitGroup and unitGroup.valid then
|
|
|
|
return squad
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-02-01 06:01:28 +02:00
|
|
|
end
|
2019-02-11 08:14:17 +02:00
|
|
|
|
2018-02-01 06:01:28 +02:00
|
|
|
return nil
|
|
|
|
end
|
2018-01-14 07:48:21 +02:00
|
|
|
|
2019-03-07 08:12:39 +02:00
|
|
|
function unitGroupUtils.createSquad(position, surface, group, settlers)
|
2018-01-15 02:14:38 +02:00
|
|
|
local unitGroup = group or surface.create_unit_group({position=position})
|
2019-02-11 08:14:17 +02:00
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
local squad = {
|
2019-03-09 02:42:20 +02:00
|
|
|
group = unitGroup,
|
|
|
|
status = SQUAD_GUARDING,
|
|
|
|
penalties = {},
|
|
|
|
rabid = false,
|
|
|
|
frenzy = false,
|
2019-02-16 20:45:42 +02:00
|
|
|
settlers = settlers or false,
|
2019-03-09 02:42:20 +02:00
|
|
|
kamikaze = false,
|
|
|
|
frenzyPosition = {x = 0,
|
|
|
|
y = 0},
|
|
|
|
cycles = 0,
|
|
|
|
maxDistance = 0,
|
2019-02-16 20:45:42 +02:00
|
|
|
attackScoreFunction = 1,
|
2019-03-09 08:23:00 +02:00
|
|
|
originPosition = {x = 0,
|
|
|
|
y = 0},
|
2019-03-09 02:42:20 +02:00
|
|
|
chunk = nil
|
2018-01-14 07:48:21 +02:00
|
|
|
}
|
2019-03-07 08:12:39 +02:00
|
|
|
|
2019-03-09 08:23:00 +02:00
|
|
|
if position then
|
|
|
|
squad.originPosition.x = position.x
|
|
|
|
squad.originPosition.y = position.y
|
|
|
|
elseif group then
|
|
|
|
squad.originPosition.x = group.position.x
|
|
|
|
squad.originPosition.y = group.position.y
|
|
|
|
end
|
|
|
|
|
2016-08-07 05:38:47 +02:00
|
|
|
return squad
|
|
|
|
end
|
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
function unitGroupUtils.membersToSquad(cmd, members, overwriteGroup)
|
2016-08-08 03:35:36 +02:00
|
|
|
if (members ~= nil) then
|
2019-03-09 02:42:20 +02:00
|
|
|
for i=1,#members do
|
2016-08-07 05:38:47 +02:00
|
|
|
local member = members[i]
|
2017-05-28 06:50:37 +02:00
|
|
|
if member.valid and (overwriteGroup or (not overwriteGroup and not member.unit_group)) then
|
2019-03-09 02:42:20 +02:00
|
|
|
member.set_command(cmd)
|
2016-08-07 05:38:47 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
function unitGroupUtils.convertUnitGroupToSquad(natives, unitGroup)
|
2017-06-01 03:46:53 +02:00
|
|
|
if not unitGroup then
|
2019-03-09 02:42:20 +02:00
|
|
|
return nil
|
2016-08-07 05:38:47 +02:00
|
|
|
end
|
2017-06-01 03:46:53 +02:00
|
|
|
local squads = natives.squads
|
|
|
|
for i=1,#squads do
|
2019-03-09 02:42:20 +02:00
|
|
|
local squad = squads[i]
|
|
|
|
if (squad.group == unitGroup) then
|
|
|
|
return squad
|
|
|
|
end
|
2017-06-01 03:46:53 +02:00
|
|
|
end
|
2019-03-07 08:12:39 +02:00
|
|
|
local squad = unitGroupUtils.createSquad(nil,nil,unitGroup)
|
2018-01-15 02:14:38 +02:00
|
|
|
squad.kamikaze = mRandom() < unitGroupUtils.calculateKamikazeThreshold(#unitGroup.members, natives)
|
|
|
|
return squad
|
2016-08-07 05:38:47 +02:00
|
|
|
end
|
|
|
|
|
2018-01-15 02:14:38 +02:00
|
|
|
function unitGroupUtils.calculateKamikazeThreshold(memberCount, natives)
|
|
|
|
local squadSizeBonus = mLog((memberCount / natives.attackWaveMaxSize) + 0.1) + 1
|
2017-06-01 03:46:53 +02:00
|
|
|
return natives.kamikazeThreshold + (NO_RETREAT_SQUAD_SIZE_BONUS_MAX * squadSizeBonus)
|
2017-01-20 07:58:36 +02:00
|
|
|
end
|
|
|
|
|
2017-06-11 02:59:06 +02:00
|
|
|
function unitGroupUtils.recycleBiters(natives, biters)
|
|
|
|
local unitCount = #biters
|
|
|
|
for i=1,unitCount do
|
2019-03-09 02:42:20 +02:00
|
|
|
biters[i].destroy()
|
2017-06-11 02:59:06 +02:00
|
|
|
end
|
|
|
|
natives.points = natives.points + (unitCount * natives.unitRefundAmount)
|
|
|
|
|
|
|
|
if (natives.points > AI_MAX_OVERFLOW_POINTS) then
|
2019-03-09 02:42:20 +02:00
|
|
|
natives.points = AI_MAX_OVERFLOW_POINTS
|
2018-01-14 07:48:21 +02:00
|
|
|
end
|
|
|
|
end
|
2017-05-24 08:46:23 +02:00
|
|
|
|
2019-03-07 08:12:39 +02:00
|
|
|
function unitGroupUtils.cleanBuilders(natives)
|
|
|
|
local squads = natives.building
|
|
|
|
local squadCount = #squads
|
|
|
|
|
|
|
|
local startIndex = natives.cleanBuildingIndex
|
|
|
|
|
|
|
|
local maxSquadIndex = mMin(startIndex + SQUAD_QUEUE_SIZE, squadCount)
|
|
|
|
for i=maxSquadIndex,startIndex,-1 do
|
2019-03-09 02:42:20 +02:00
|
|
|
local squad = squads[i]
|
|
|
|
local group = squad.group
|
|
|
|
if not (group and group.valid) then
|
|
|
|
tRemove(squads, i)
|
2019-03-07 08:12:39 +02:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
if (maxSquadIndex >= squadCount) then
|
2019-03-07 08:12:39 +02:00
|
|
|
natives.cleanBuildingIndex = 1
|
|
|
|
else
|
|
|
|
natives.cleanBuildingIndex = maxSquadIndex + 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
local function isAttacking(group)
|
|
|
|
local state = group.state
|
|
|
|
return (state == DEFINES_GROUP_STATE_ATTACKING_TARGET) or (state == DEFINES_GROUP_STATE_ATTACKING_DISTRACTION)
|
|
|
|
end
|
2019-03-07 08:12:39 +02:00
|
|
|
|
2018-01-14 07:48:21 +02:00
|
|
|
function unitGroupUtils.regroupSquads(natives, map)
|
2017-05-24 08:46:23 +02:00
|
|
|
local squads = natives.squads
|
|
|
|
local squadCount = #squads
|
2017-06-01 04:48:59 +02:00
|
|
|
|
|
|
|
local startIndex = natives.regroupIndex
|
2018-01-14 07:48:21 +02:00
|
|
|
|
2017-06-01 04:48:59 +02:00
|
|
|
local maxSquadIndex = mMin(startIndex + SQUAD_QUEUE_SIZE, squadCount)
|
|
|
|
for i=startIndex,maxSquadIndex do
|
2019-03-09 02:42:20 +02:00
|
|
|
local squad = squads[i]
|
|
|
|
local group = squad.group
|
|
|
|
if group and group.valid and not isAttacking(group) then
|
|
|
|
local memberCount = #group.members
|
|
|
|
if (memberCount < AI_SQUAD_MERGE_THRESHOLD) then
|
|
|
|
local status = squad.status
|
|
|
|
local chunk = squad.chunk
|
|
|
|
|
|
|
|
if chunk then
|
|
|
|
local chunkSquads = getSquadsOnChunk(map, chunk)
|
|
|
|
for p=1,#chunkSquads do
|
|
|
|
local mergeSquad = chunkSquads[p]
|
|
|
|
if (mergeSquad ~= squad) then
|
|
|
|
local mergeGroup = mergeSquad.group
|
|
|
|
if mergeGroup and
|
|
|
|
mergeGroup.valid and
|
|
|
|
(mergeSquad.status == status) and
|
|
|
|
not isAttacking(mergeGroup)
|
|
|
|
then
|
|
|
|
local mergeMembers = mergeGroup.members
|
|
|
|
local mergeCount = #mergeMembers
|
|
|
|
if ((mergeCount + memberCount) < AI_MAX_BITER_GROUP_SIZE) then
|
|
|
|
for memberIndex=1, mergeCount do
|
|
|
|
group.add_member(mergeMembers[memberIndex])
|
|
|
|
end
|
|
|
|
mergeGroup.destroy()
|
|
|
|
end
|
|
|
|
squad.status = SQUAD_GUARDING
|
|
|
|
memberCount = memberCount + mergeCount
|
|
|
|
if (memberCount > AI_SQUAD_MERGE_THRESHOLD) then
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2019-02-11 08:14:17 +02:00
|
|
|
end
|
2019-03-07 08:12:39 +02:00
|
|
|
end
|
|
|
|
end
|
2017-06-01 04:48:59 +02:00
|
|
|
end
|
2019-02-11 08:14:17 +02:00
|
|
|
|
2019-03-09 02:42:20 +02:00
|
|
|
if (maxSquadIndex >= squadCount) then
|
2019-03-07 08:12:39 +02:00
|
|
|
natives.regroupIndex = 1
|
2017-06-01 04:48:59 +02:00
|
|
|
else
|
2019-03-07 08:12:39 +02:00
|
|
|
natives.regroupIndex = maxSquadIndex + 1
|
2017-06-01 04:48:59 +02:00
|
|
|
end
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
|
|
|
|
2019-02-16 06:17:30 +02:00
|
|
|
unitGroupUtilsG = unitGroupUtils
|
2016-08-30 06:08:22 +02:00
|
|
|
return unitGroupUtils
|