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

152 lines
4.5 KiB
Lua
Raw Normal View History

2019-02-16 06:17:30 +02:00
if unitGroupUtilsG then
return unitGroupUtilsG
end
local unitGroupUtils = {}
-- 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")
local chunkPropertyUtils = require("ChunkPropertyUtils")
local mathUtils = require("MathUtils")
-- constants
local MINIMUM_EXPANSION_DISTANCE = constants.MINIMUM_EXPANSION_DISTANCE
local SQUAD_RETREATING = constants.SQUAD_RETREATING
local SQUAD_GUARDING = constants.SQUAD_GUARDING
2018-01-14 07:48:21 +02:00
-- imported functions
local gaussianRandomRangeRG = mathUtils.gaussianRandomRangeRG
local getSquadsOnChunk = chunkPropertyUtils.getSquadsOnChunk
2018-01-14 07:48:21 +02:00
local getNeighborChunks = mapUtils.getNeighborChunks
-- 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
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
end
return nil
end
2019-03-09 02:42:20 +02:00
function unitGroupUtils.findNearbySquad(map, chunk)
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
end
2019-10-20 22:45:43 +02:00
local neighbors = getNeighborChunks(map, chunk.x, chunk.y)
2018-01-14 07:48:21 +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
end
2019-02-11 08:14:17 +02:00
return nil
end
2018-01-14 07:48:21 +02:00
function unitGroupUtils.calculateSettlerMaxDistance(universe)
local targetDistance
local distanceRoll = universe.random()
if distanceRoll < 0.05 then
return 0
elseif distanceRoll < 0.30 then
targetDistance = universe.expansionLowTargetDistance
elseif distanceRoll < 0.70 then
targetDistance = universe.expansionMediumTargetDistance
elseif distanceRoll < 0.95 then
targetDistance = universe.expansionHighTargetDistance
else
return universe.expansionMaxDistance
end
return gaussianRandomRangeRG(targetDistance,
universe.expansionDistanceDeviation,
MINIMUM_EXPANSION_DISTANCE,
universe.expansionMaxDistance,
universe.random)
end
2021-12-06 05:40:39 +02:00
function unitGroupUtils.createSquad(position, map, group, settlers)
local unitGroup = group or map.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 = {},
2021-04-30 07:24:14 +02:00
base = nil,
2019-03-09 02:42:20 +02:00
frenzy = false,
2021-12-06 05:40:39 +02:00
map = map,
wanders = 0,
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},
commandTick = nil,
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
if settlers then
squad.maxDistance = unitGroupUtils.calculateSettlerMaxDistance(map.universe)
end
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
function unitGroupUtils.calculateKamikazeSquadThreshold(memberCount, universe)
local threshold = (memberCount / universe.attackWaveMaxSize) * 0.2 + (universe.evolutionLevel * 0.2)
2020-05-24 05:47:14 +02:00
return threshold
end
function unitGroupUtils.calculateKamikazeSettlerThreshold(memberCount, universe)
local threshold = (memberCount / universe.expansionMaxSize) * 0.2 + (universe.evolutionLevel * 0.2)
return threshold
end
2019-02-16 06:17:30 +02:00
unitGroupUtilsG = unitGroupUtils
return unitGroupUtils