2016-08-05 06:47:51 +02:00
|
|
|
local unitGroupUtils = {}
|
|
|
|
|
|
|
|
local utils = require("Utils")
|
2016-08-07 05:38:47 +02:00
|
|
|
local constants = require("Constants")
|
2016-08-05 06:47:51 +02:00
|
|
|
|
2016-08-18 07:55:08 +02:00
|
|
|
local groupingCommand = {type=defines.command.group,
|
|
|
|
group=1,
|
|
|
|
distraction=0}
|
|
|
|
|
2016-08-08 03:35:36 +02:00
|
|
|
function unitGroupUtils.findNearBySquad(natives, position, distance, filter)
|
2016-08-07 05:38:47 +02:00
|
|
|
local getDistance = utils.euclideanDistanceNamed
|
2016-08-05 06:47:51 +02:00
|
|
|
local squads = natives.squads
|
|
|
|
local i = 1
|
|
|
|
while (i <= #squads) do
|
|
|
|
local squad = squads[i]
|
2016-08-07 05:38:47 +02:00
|
|
|
local unitGroup = squad.group
|
2016-08-08 03:35:36 +02:00
|
|
|
if (unitGroup ~= nil) and unitGroup.valid and ((filter == nil) or (filter ~= nil and filter[squad.status])) then
|
2016-08-07 05:38:47 +02:00
|
|
|
if (getDistance(unitGroup.position, position) <= distance) then
|
2016-08-05 06:47:51 +02:00
|
|
|
return squad
|
|
|
|
end
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
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 = constants.SQUAD_GUARDING,
|
2016-08-18 07:55:08 +02:00
|
|
|
cycles = 0 }
|
2016-08-07 05:38:47 +02:00
|
|
|
natives.squads[#natives.squads+1] = squad
|
|
|
|
return squad
|
|
|
|
end
|
|
|
|
|
2016-08-18 07:55:08 +02:00
|
|
|
function unitGroupUtils.membersToSquad(squad, members, overwriteGroup, distraction)
|
2016-08-08 03:35:36 +02:00
|
|
|
if (members ~= nil) then
|
2016-08-07 05:38:47 +02:00
|
|
|
local group = squad.group
|
|
|
|
for i=1,#members do
|
|
|
|
local member = members[i]
|
|
|
|
if (member ~= nil) and member.valid and (overwriteGroup or (not overwriteGroup and (member.unit_group == nil))) then
|
2016-08-18 07:55:08 +02:00
|
|
|
groupingCommand.group = group
|
|
|
|
groupingCommand.distraction = distraction
|
|
|
|
member.set_command(groupingCommand)
|
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
|
|
|
|
local addUnitGroup = true
|
|
|
|
local i = 1
|
|
|
|
while (i <= #squads) and addUnitGroup do
|
|
|
|
local squad = squads[i]
|
2016-08-08 03:35:36 +02:00
|
|
|
if (squad.group == unitGroup) then
|
2016-08-07 05:38:47 +02:00
|
|
|
addUnitGroup = false
|
|
|
|
returnSquad = squad
|
|
|
|
end
|
|
|
|
i = i + 1
|
|
|
|
end
|
|
|
|
if addUnitGroup then
|
|
|
|
returnSquad = { group = unitGroup,
|
|
|
|
status = constants.SQUAD_GUARDING,
|
2016-08-18 07:55:08 +02:00
|
|
|
cycles = 0 }
|
2016-08-07 05:38:47 +02:00
|
|
|
squads[#squads+1] = returnSquad
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
return returnSquad
|
|
|
|
end
|
|
|
|
|
2016-08-18 07:55:08 +02:00
|
|
|
-- function unitGroupUtils.setSquadCommand(squad, command, state, cycles)
|
|
|
|
-- local group = squad.group
|
|
|
|
-- if (group ~= nil) and group.valid then
|
|
|
|
-- squad.status = state
|
|
|
|
-- squad.cycles = cycles
|
|
|
|
-- group.set_command(command)
|
2016-08-07 05:38:47 +02:00
|
|
|
-- group.start_moving()
|
2016-08-18 07:55:08 +02:00
|
|
|
-- end
|
|
|
|
-- end
|
2016-08-07 05:38:47 +02:00
|
|
|
|
|
|
|
function unitGroupUtils.regroupSquads(natives)
|
|
|
|
local SQUAD_RETREATING = constants.SQUAD_RETREATING
|
|
|
|
local SQUAD_GUARDING = constants.SQUAD_GUARDING
|
2016-08-09 04:18:19 +02:00
|
|
|
local findDistance = utils.euclideanDistanceNamed
|
|
|
|
local mergeSquadMembers = unitGroupUtils.membersToSquad
|
|
|
|
|
|
|
|
local squads = natives.squads
|
|
|
|
for i=1,#squads do
|
|
|
|
local squad = squads[i]
|
|
|
|
if squad.group.valid then
|
|
|
|
local squadPosition = squad.group.position
|
|
|
|
for x=i+1, #squads do
|
|
|
|
local mergeSquad = squads[x]
|
|
|
|
local mergeGroup = mergeSquad.group
|
2016-08-18 07:55:08 +02:00
|
|
|
if mergeGroup.valid and (mergeSquad.status == squad.status) and (findDistance(squadPosition, mergeGroup.position) < constants.HALF_CHUNK_SIZE) then
|
2016-08-09 04:18:19 +02:00
|
|
|
mergeSquadMembers(squad, mergeGroup.members, true)
|
|
|
|
mergeGroup.destroy()
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-08-07 05:38:47 +02:00
|
|
|
|
2016-08-09 04:18:19 +02:00
|
|
|
local squads = natives.squads
|
|
|
|
for i=#squads,1,-1 do
|
|
|
|
local squad = squads[i]
|
2016-08-07 05:38:47 +02:00
|
|
|
if (squad.group == nil) then
|
2016-08-09 04:18:19 +02:00
|
|
|
table.remove(squads, i)
|
2016-08-07 05:38:47 +02:00
|
|
|
elseif not squad.group.valid then
|
2016-08-09 04:18:19 +02:00
|
|
|
table.remove(squads, i)
|
2016-08-07 05:38:47 +02:00
|
|
|
elseif (#squad.group.members == 0) then
|
|
|
|
squad.group.destroy()
|
2016-08-09 04:18:19 +02:00
|
|
|
table.remove(squads, i)
|
|
|
|
else
|
|
|
|
if (squad.status == SQUAD_RETREATING) and (squad.cycles == 0) then
|
2016-08-19 00:14:40 +02:00
|
|
|
squad.cX = nil
|
|
|
|
squad.cY = nil
|
2016-08-09 04:18:19 +02:00
|
|
|
squad.status = SQUAD_GUARDING
|
|
|
|
elseif (squad.cycles > 0) then
|
|
|
|
squad.cycles = squad.cycles - 1
|
|
|
|
end
|
2016-08-07 05:38:47 +02:00
|
|
|
end
|
|
|
|
end
|
2016-08-05 06:47:51 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
return unitGroupUtils
|