UserFunction; SobGroup_Split - HWRM/KarosGraveyard GitHub Wiki
Splits a given group
into a table of subgroups. The size of the subgroups is optionally given by granularity
.
This is very useful for many situations!
Makes use of SobGroup_FillShipsByIndexRange
.
--- _Splits_ a given `group` into a table of subgroups. The size of the subgroups is given by `granularity`.
---
---@param group string
---@param granularity? integer
---@return string[]
function SobGroup_Split(group, granularity)
granularity = granularity or 1;
if (SobGroup_Count(group) <= granularity) then -- fast return
local subgroup = SobGroup_Clone(group);
return { [1] = subgroup };
end
local subgroups = {};
for i = 0, SobGroup_Count(group), granularity do
local subgroup = SobGroup_Fresh(group.. "-subgroup-" .. i);
SobGroup_FillShipsByIndexRange(subgroup, group, i, granularity);
subgroups[i + 1] = subgroup;
end
return subgroups;
end
📚 See:
Sobgroup_Fresh
andSobGroup_Clone
Here we can tell individual ships to look around themselves to a given search radius, and attack enemy ships in that radius. This is not possible without some kind of sobgroup splitting function, outside of ideal scenarios where you can be sure only one ship satisfies some selection predicate i.e 'ships of type hgn_mothership
etc.
local this_player_idx = 0;
local original_group = SobGroup_Clone("Player_Ships" .. this_player_idx);
local subgroups = SobGroup_Split(original_group); -- granularity = 1, so subgroups should only host one ship each
local close_distance = 10000;
for _, subgroup in subgroups do
local close_enemies = SobGroup_Fresh();
for player_index = -1, 7 do -- all possible player indices
local same_player = player_index == this_player_index;
local allied = AreAllied(player_index, this_player_index) == 1;
if (not same_player and not allied) then -- except for this player & their allies since we won't attack those...
local close_player_ships = SobGroup_Fresh();
-- put into `close_player_ships` any ships belonging to `player_index` which are within `close_distance` to `subgroup`:
Player_FillProximitySobGroup(close_player_ships, player_index, subgroup, close_distance);
-- then add the contents of `close_player_ships` to the larger `close_enemies` group:
SobGroup_SobGroupAdd(close_enemies, close_player_ships);
end
end
-- finally tell ships in `subgroup` belonging to `this_player_idx` to attack ships in `close_enemies`
SobGroup_Attack(this_player_idx, subgroup, close_enemies);
end
Param | Type | Description |
---|---|---|
group |
string |
The name of the new group which will be copied to. |
granularity |
number? |
Integer indicating the amount of ships per subgroup. Defaults to 1 . |
table<integer, string>
The keys are whatever index the SobGroup_FillShipsByIndexRange
happened to give, and the values are the subgroups.