2019-02-15 20:17:30 -08:00
|
|
|
if unitGroupUtilsG then
|
|
|
|
return unitGroupUtilsG
|
|
|
|
end
|
2016-08-04 21:47:51 -07:00
|
|
|
local unitGroupUtils = {}
|
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
-- imports
|
|
|
|
|
2018-01-13 21:48:21 -08:00
|
|
|
local mapUtils = require("MapUtils")
|
2016-08-06 20:38:47 -07:00
|
|
|
local constants = require("Constants")
|
2018-01-14 23:41:55 -08:00
|
|
|
local chunkPropertyUtils = require("ChunkPropertyUtils")
|
2020-02-02 11:30:50 -08:00
|
|
|
local chunkUtils = require("ChunkUtils")
|
2019-04-07 22:22:02 -07:00
|
|
|
local movementUtils = require("MovementUtils")
|
2016-08-04 21:47:51 -07:00
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
-- constants
|
|
|
|
|
2020-05-23 14:29:56 -07:00
|
|
|
local TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT
|
2019-04-07 22:22:02 -07:00
|
|
|
|
2020-05-19 19:37:16 -07:00
|
|
|
local DIVISOR_DEATH_TRAIL_TABLE = constants.DIVISOR_DEATH_TRAIL_TABLE
|
2017-05-31 19:48:59 -07:00
|
|
|
local SQUAD_QUEUE_SIZE = constants.SQUAD_QUEUE_SIZE
|
|
|
|
|
2017-05-26 17:58:33 -07: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-17 22:55:08 -07:00
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
local SQUAD_RETREATING = constants.SQUAD_RETREATING
|
|
|
|
local SQUAD_GUARDING = constants.SQUAD_GUARDING
|
2018-01-13 21:48:21 -08:00
|
|
|
|
2017-04-15 23:04:22 -07:00
|
|
|
local AI_MAX_BITER_GROUP_SIZE = constants.AI_MAX_BITER_GROUP_SIZE
|
2017-06-01 00:03:07 -07:00
|
|
|
local AI_SQUAD_MERGE_THRESHOLD = constants.AI_SQUAD_MERGE_THRESHOLD
|
2017-04-15 23:04:22 -07:00
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
-- imported functions
|
|
|
|
|
2019-03-06 22:12:39 -08:00
|
|
|
local tRemove = table.remove
|
|
|
|
|
2017-06-30 21:36:23 -07:00
|
|
|
local mRandom = math.random
|
|
|
|
|
2019-04-07 22:22:02 -07:00
|
|
|
local findMovementPosition = movementUtils.findMovementPosition
|
2020-05-16 22:06:55 -07:00
|
|
|
local removeSquadFromChunk = chunkPropertyUtils.removeSquadFromChunk
|
2020-05-19 19:37:16 -07:00
|
|
|
local addDeathGenerator = chunkPropertyUtils.addDeathGenerator
|
|
|
|
local getDeathGenerator = chunkPropertyUtils.getDeathGenerator
|
|
|
|
|
|
|
|
local next = next
|
|
|
|
local table_size = table_size
|
2019-04-07 22:22:02 -07:00
|
|
|
|
2017-01-19 21:58:36 -08:00
|
|
|
local mLog = math.log10
|
|
|
|
|
2017-05-31 19:48:59 -07:00
|
|
|
local mMin = math.min
|
|
|
|
|
2018-01-14 23:41:55 -08:00
|
|
|
local getSquadsOnChunk = chunkPropertyUtils.getSquadsOnChunk
|
2018-01-13 21:48:21 -08:00
|
|
|
|
|
|
|
local getNeighborChunks = mapUtils.getNeighborChunks
|
|
|
|
|
2016-08-19 19:52:27 -07:00
|
|
|
-- module code
|
2017-01-19 21:58:36 -08:00
|
|
|
|
2019-03-09 15:16:35 -08:00
|
|
|
function unitGroupUtils.findNearbyRetreatingSquad(map, chunk)
|
2019-03-08 16:42:20 -08:00
|
|
|
|
2020-05-23 20:47:14 -07:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, chunk)) do
|
2019-10-13 22:49:52 -07:00
|
|
|
local unitGroup = squad.group
|
2020-05-23 20:47:14 -07:00
|
|
|
if (squad.status == SQUAD_RETREATING) and unitGroup and unitGroup.valid then
|
2019-03-08 16:42:20 -08:00
|
|
|
return squad
|
|
|
|
end
|
2018-01-13 21:48:21 -08:00
|
|
|
end
|
|
|
|
|
|
|
|
local neighbors = getNeighborChunks(map, chunk.x, chunk.y)
|
|
|
|
|
|
|
|
for i=1,#neighbors do
|
2019-03-08 16:42:20 -08:00
|
|
|
local neighbor = neighbors[i]
|
2020-05-15 13:51:38 -07:00
|
|
|
if neighbor ~= -1 then
|
2020-05-23 20:47:14 -07:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, neighbor)) do
|
2019-03-08 16:42:20 -08:00
|
|
|
local unitGroup = squad.group
|
2020-05-23 20:47:14 -07:00
|
|
|
if (squad.status == SQUAD_RETREATING) and unitGroup and unitGroup.valid then
|
2019-03-08 16:42:20 -08:00
|
|
|
return squad
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
2018-01-31 20:01:28 -08:00
|
|
|
return nil
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
|
|
|
|
2019-03-08 16:42:20 -08:00
|
|
|
function unitGroupUtils.findNearbySquad(map, chunk)
|
2018-01-31 20:01:28 -08:00
|
|
|
|
2020-05-23 20:47:14 -07:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, chunk)) do
|
2019-03-08 16:42:20 -08:00
|
|
|
local unitGroup = squad.group
|
2019-10-20 13:45:43 -07:00
|
|
|
if unitGroup and unitGroup.valid then
|
|
|
|
return squad
|
2019-03-08 16:42:20 -08:00
|
|
|
end
|
2018-01-31 20:01:28 -08:00
|
|
|
end
|
2019-10-20 13:45:43 -07:00
|
|
|
|
2018-01-31 20:01:28 -08:00
|
|
|
local neighbors = getNeighborChunks(map, chunk.x, chunk.y)
|
2018-01-13 21:48:21 -08:00
|
|
|
|
2018-01-31 20:01:28 -08:00
|
|
|
for i=1,#neighbors do
|
2019-03-08 16:42:20 -08:00
|
|
|
local neighbor = neighbors[i]
|
2020-05-15 13:51:38 -07:00
|
|
|
if neighbor ~= -1 then
|
2020-05-23 20:47:14 -07:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, neighbor)) do
|
2019-03-08 16:42:20 -08:00
|
|
|
local unitGroup = squad.group
|
|
|
|
if unitGroup and unitGroup.valid then
|
|
|
|
return squad
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2018-01-31 20:01:28 -08:00
|
|
|
end
|
2019-02-10 22:14:17 -08:00
|
|
|
|
2018-01-31 20:01:28 -08:00
|
|
|
return nil
|
|
|
|
end
|
2018-01-13 21:48:21 -08:00
|
|
|
|
2019-03-06 22:12:39 -08:00
|
|
|
function unitGroupUtils.createSquad(position, surface, group, settlers)
|
2018-01-14 16:14:38 -08:00
|
|
|
local unitGroup = group or surface.create_unit_group({position=position})
|
2019-02-10 22:14:17 -08:00
|
|
|
|
2018-01-13 21:48:21 -08:00
|
|
|
local squad = {
|
2019-03-08 16:42:20 -08:00
|
|
|
group = unitGroup,
|
|
|
|
status = SQUAD_GUARDING,
|
|
|
|
rabid = false,
|
2020-05-23 21:17:18 -07:00
|
|
|
penalties = {},
|
2019-03-08 16:42:20 -08:00
|
|
|
frenzy = false,
|
2019-02-16 10:45:42 -08:00
|
|
|
settlers = settlers or false,
|
2019-03-08 16:42:20 -08:00
|
|
|
kamikaze = false,
|
|
|
|
frenzyPosition = {x = 0,
|
|
|
|
y = 0},
|
|
|
|
maxDistance = 0,
|
2020-05-16 22:06:55 -07:00
|
|
|
groupNumber = unitGroup.group_number,
|
2019-03-08 22:23:00 -08:00
|
|
|
originPosition = {x = 0,
|
|
|
|
y = 0},
|
2020-05-15 13:51:38 -07:00
|
|
|
chunk = -1
|
2018-01-13 21:48:21 -08:00
|
|
|
}
|
2019-03-06 22:12:39 -08:00
|
|
|
|
2019-03-08 22:23:00 -08: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-06 20:38:47 -07:00
|
|
|
return squad
|
|
|
|
end
|
|
|
|
|
2020-05-16 22:06:55 -07:00
|
|
|
function unitGroupUtils.cleanSquads(natives, iterator)
|
|
|
|
local squads = natives.groupNumberToSquad
|
|
|
|
local map = natives.map
|
|
|
|
|
|
|
|
local k, squad = next(squads, iterator)
|
2020-05-19 19:37:16 -07:00
|
|
|
if not k then
|
|
|
|
if (table_size(squads) == 0) then
|
|
|
|
-- this is needed as the next command remembers the max length a table has been
|
|
|
|
natives.groupNumberToSquad = {}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
local group = squad.group
|
|
|
|
if not group.valid then
|
2020-05-23 20:47:14 -07:00
|
|
|
addDeathGenerator(map, squad.chunk, TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT)
|
|
|
|
removeSquadFromChunk(map, squad)
|
2020-05-16 22:06:55 -07:00
|
|
|
if (map.regroupIterator == k) then
|
|
|
|
map.regroupIterator = nil
|
|
|
|
end
|
2020-05-22 12:43:44 -07:00
|
|
|
if squad.settlers then
|
|
|
|
natives.builderCount = natives.builderCount - 1
|
2020-05-23 14:29:56 -07:00
|
|
|
else
|
|
|
|
natives.squadCount = natives.squadCount - 1
|
2020-05-22 12:43:44 -07:00
|
|
|
end
|
2020-05-24 16:41:12 -07:00
|
|
|
local nextK
|
2020-05-16 22:06:55 -07:00
|
|
|
nextK,squad = next(squads, k)
|
|
|
|
squads[k] = nil
|
|
|
|
k = nextK
|
|
|
|
end
|
|
|
|
end
|
2020-05-19 19:37:16 -07:00
|
|
|
map.squadIterator = k
|
2020-05-16 22:06:55 -07:00
|
|
|
end
|
|
|
|
|
2018-01-14 16:14:38 -08:00
|
|
|
function unitGroupUtils.calculateKamikazeThreshold(memberCount, natives)
|
2020-05-23 20:47:14 -07:00
|
|
|
local threshold = (memberCount / natives.attackWaveMaxSize) * 0.2 + (natives.evolutionLevel * 0.2)
|
|
|
|
return threshold
|
2016-08-04 21:47:51 -07:00
|
|
|
end
|
|
|
|
|
2019-02-15 20:17:30 -08:00
|
|
|
unitGroupUtilsG = unitGroupUtils
|
2016-08-29 21:08:22 -07:00
|
|
|
return unitGroupUtils
|