Skip to content

Commit ca306cf

Browse files
committed
perf: optimize getActors functions for the case of 1 distance
Instead of iterating over all the actors on the battlefield, we only iterate over the adjacent 6 tiles. Getting adjacent actors is a very widely used thing in mods and occurs inside often repeating functions e.g. onUpdate so it is good to optimize it.
1 parent 5a72fb8 commit ca306cf

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

msu/hooks/entity/tactical/tactical_entity_manager.nut

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
throw ::MSU.Exception.InvalidValue(_tile);
6666
}
6767

68+
if (_distance == 1)
69+
{
70+
local ret = this.getAdjacentActors(_tile);
71+
if (!_atDistance && _tile.IsOccupiedByActor)
72+
ret.push(_tile.getEntity());
73+
return ret.filter(@(_, _a) _a.isAlliedWith(_faction));
74+
}
75+
6876
return this.getActorsByFunction(function(_actor) {
6977
if (!_actor.isAlliedWith(_faction)) return false;
7078
if (_tile != null)
@@ -85,6 +93,14 @@
8593
throw ::MSU.Exception.InvalidValue(_tile);
8694
}
8795

96+
if (_distance == 1)
97+
{
98+
local ret = this.getAdjacentActors(_tile);
99+
if (!_atDistance && _tile.IsOccupiedByActor)
100+
ret.push(_tile.getEntity());
101+
return ret.filter(@(_, _a) !_a.isAlliedWith(_faction));
102+
}
103+
88104
return this.getActorsByFunction(function(_actor) {
89105
if (_actor.isAlliedWith(_faction)) return false;
90106
if (_tile != null)
@@ -108,6 +124,14 @@
108124
::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map.");
109125
throw ::MSU.Exception.InvalidValue(_tile);
110126
}
127+
128+
if (_distance == 1)
129+
{
130+
local ret = this.getAdjacentActors(_tile);
131+
if (!_atDistance && _tile.IsOccupiedByActor)
132+
ret.push(_tile.getEntity());
133+
return ret.filter(@(_, _a) _a.getFaction() == _faction);
134+
}
111135

112136
local actors = this.getInstancesOfFaction(_faction);
113137
local ret = [];
@@ -129,6 +153,14 @@
129153
throw ::MSU.Exception.InvalidValue(_tile);
130154
}
131155

156+
if (_distance == 1)
157+
{
158+
local ret = this.getAdjacentActors(_tile);
159+
if (!_atDistance && _tile.IsOccupiedByActor)
160+
ret.push(_tile.getEntity());
161+
return ret.filter(@(_, _a) _a.isAlliedWith(_faction) && _a.getFaction() != _faction);
162+
}
163+
132164
return this.getActorsByFunction(function(_actor) {
133165
if (!_actor.isAlliedWith(_faction) || _actor.getFaction() == _faction) return false;
134166
if (_tile != null)

0 commit comments

Comments
 (0)