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")
|
2016-08-07 05:38:47 +02:00
|
|
|
local constants = require("Constants")
|
2018-01-15 09:41:55 +02:00
|
|
|
local chunkPropertyUtils = require("ChunkPropertyUtils")
|
2020-02-02 21:30:50 +02:00
|
|
|
local chunkUtils = require("ChunkUtils")
|
2019-04-08 07:22:02 +02:00
|
|
|
local movementUtils = require("MovementUtils")
|
2016-08-05 06:47:51 +02:00
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- constants
|
|
|
|
|
2020-05-23 23:29:56 +02:00
|
|
|
local TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT = constants.TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT
|
2019-04-08 07:22:02 +02:00
|
|
|
|
2020-05-20 04:37:16 +02:00
|
|
|
local DIVISOR_DEATH_TRAIL_TABLE = constants.DIVISOR_DEATH_TRAIL_TABLE
|
2017-06-01 04:48:59 +02:00
|
|
|
local SQUAD_QUEUE_SIZE = constants.SQUAD_QUEUE_SIZE
|
|
|
|
|
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
|
2018-01-14 07:48:21 +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
|
|
|
|
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
|
|
|
|
|
2019-04-08 07:22:02 +02:00
|
|
|
local findMovementPosition = movementUtils.findMovementPosition
|
2020-05-17 07:06:55 +02:00
|
|
|
local removeSquadFromChunk = chunkPropertyUtils.removeSquadFromChunk
|
2020-05-20 04:37:16 +02:00
|
|
|
local addDeathGenerator = chunkPropertyUtils.addDeathGenerator
|
|
|
|
local getDeathGenerator = chunkPropertyUtils.getDeathGenerator
|
|
|
|
|
|
|
|
local next = next
|
|
|
|
local table_size = table_size
|
2019-04-08 07:22:02 +02:00
|
|
|
|
2017-01-20 07:58:36 +02:00
|
|
|
local mLog = math.log10
|
|
|
|
|
2017-06-01 04:48:59 +02:00
|
|
|
local mMin = math.min
|
|
|
|
|
2018-01-15 09:41:55 +02:00
|
|
|
local getSquadsOnChunk = chunkPropertyUtils.getSquadsOnChunk
|
2018-01-14 07:48:21 +02:00
|
|
|
|
|
|
|
local getNeighborChunks = mapUtils.getNeighborChunks
|
|
|
|
|
2016-08-20 04:52:27 +02:00
|
|
|
-- module code
|
2017-01-20 07:58:36 +02:00
|
|
|
|
2019-03-10 01:16:35 +02:00
|
|
|
function unitGroupUtils.findNearbyRetreatingSquad(map, chunk)
|
2019-03-09 02:42:20 +02:00
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, chunk)) do
|
2019-10-14 07:49:52 +02:00
|
|
|
local unitGroup = squad.group
|
2020-05-24 05:47:14 +02:00
|
|
|
if (squad.status == SQUAD_RETREATING) and unitGroup and unitGroup.valid 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]
|
2020-05-15 22:51:38 +02:00
|
|
|
if neighbor ~= -1 then
|
2020-05-24 05:47:14 +02:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, neighbor)) do
|
2019-03-09 02:42:20 +02:00
|
|
|
local unitGroup = squad.group
|
2020-05-24 05:47:14 +02:00
|
|
|
if (squad.status == SQUAD_RETREATING) and unitGroup and unitGroup.valid then
|
2019-03-09 02:42:20 +02:00
|
|
|
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
|
|
|
|
2020-05-24 05:47:14 +02:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, chunk)) do
|
2019-03-09 02:42:20 +02:00
|
|
|
local unitGroup = squad.group
|
2019-10-20 22:45:43 +02:00
|
|
|
if unitGroup and unitGroup.valid then
|
|
|
|
return squad
|
2019-03-09 02:42:20 +02:00
|
|
|
end
|
2018-02-01 06:01:28 +02:00
|
|
|
end
|
2019-10-20 22:45:43 +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]
|
2020-05-15 22:51:38 +02:00
|
|
|
if neighbor ~= -1 then
|
2020-05-24 05:47:14 +02:00
|
|
|
for _,squad in pairs(getSquadsOnChunk(map, neighbor)) do
|
2019-03-09 02:42:20 +02:00
|
|
|
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,
|
|
|
|
rabid = false,
|
2020-05-24 06:17:18 +02:00
|
|
|
penalties = {},
|
2019-03-09 02:42:20 +02:00
|
|
|
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},
|
|
|
|
maxDistance = 0,
|
2020-05-17 07:06:55 +02:00
|
|
|
groupNumber = unitGroup.group_number,
|
2019-03-09 08:23:00 +02:00
|
|
|
originPosition = {x = 0,
|
|
|
|
y = 0},
|
2020-05-15 22:51:38 +02:00
|
|
|
chunk = -1
|
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
|
|
|
|
|
2020-05-17 07:06:55 +02:00
|
|
|
function unitGroupUtils.cleanSquads(natives, iterator)
|
|
|
|
local squads = natives.groupNumberToSquad
|
|
|
|
local map = natives.map
|
|
|
|
|
|
|
|
local k, squad = next(squads, iterator)
|
2020-05-20 04:37:16 +02: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-24 05:47:14 +02:00
|
|
|
addDeathGenerator(map, squad.chunk, TEN_DEATH_PHEROMONE_GENERATOR_AMOUNT)
|
|
|
|
removeSquadFromChunk(map, squad)
|
2020-05-17 07:06:55 +02:00
|
|
|
if (map.regroupIterator == k) then
|
|
|
|
map.regroupIterator = nil
|
|
|
|
end
|
2020-05-22 21:43:44 +02:00
|
|
|
if squad.settlers then
|
|
|
|
natives.builderCount = natives.builderCount - 1
|
2020-05-23 23:29:56 +02:00
|
|
|
else
|
|
|
|
natives.squadCount = natives.squadCount - 1
|
2020-05-22 21:43:44 +02:00
|
|
|
end
|
2020-05-25 01:41:12 +02:00
|
|
|
local nextK
|
2020-05-17 07:06:55 +02:00
|
|
|
nextK,squad = next(squads, k)
|
|
|
|
squads[k] = nil
|
|
|
|
k = nextK
|
|
|
|
end
|
|
|
|
end
|
2020-05-20 04:37:16 +02:00
|
|
|
map.squadIterator = k
|
2020-05-17 07:06:55 +02:00
|
|
|
end
|
|
|
|
|
2018-01-15 02:14:38 +02:00
|
|
|
function unitGroupUtils.calculateKamikazeThreshold(memberCount, natives)
|
2020-05-24 05:47:14 +02:00
|
|
|
local threshold = (memberCount / natives.attackWaveMaxSize) * 0.2 + (natives.evolutionLevel * 0.2)
|
|
|
|
return threshold
|
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
|