From 969b5f409838b965908c147ae28cb95e6248f751 Mon Sep 17 00:00:00 2001 From: LordMidas <55047920+LordMidas@users.noreply.github.com> Date: Thu, 11 Apr 2024 00:06:32 -0400 Subject: [PATCH 1/2] feat: add functions to get actors adjacent and within specified range --- .../tactical/tactical_entity_manager.nut | 51 +++++++++++++++++++ 1 file changed, 51 insertions(+) diff --git a/msu/hooks/entity/tactical/tactical_entity_manager.nut b/msu/hooks/entity/tactical/tactical_entity_manager.nut index 38e1a0fa9..c463e3132 100644 --- a/msu/hooks/entity/tactical/tactical_entity_manager.nut +++ b/msu/hooks/entity/tactical/tactical_entity_manager.nut @@ -32,6 +32,57 @@ } return ret; } + + q.getActorsWithinRange <- function( _tile, _max = 1, _min = 1 ) + { + if (_tile.ID == 0) + { + ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); + throw ::MSU.Exception.InvalidValue(_tile); + } + + if (_max < _min) + { + ::logError("_max must be equal to or greater than _min"); + throw ::MSU.Exception.InvalidValue(_max); + } + + if (_max == 1) + { + local ret = this.getAdjacentActors(_tile); + if (_min == 0 && _tile.IsOccupiedByActor) + ret.push(_tile.getEntity()); + return ret; + } + + return this.getActorsByFunction(function(_actor) { + if (!_actor.isPlacedOnMap()) + return false; + local distance = _tile.getDistanceTo(_actor.getTile()); + return distance <= _max && distance >= _min; + }); + } + + q.getAdjacentActors <- function( _tile ) + { + if (_tile.ID == 0) + { + ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); + throw ::MSU.Exception.InvalidValue(_tile); + } + + local ret = []; + for (local i = 0; i < 6; i++) + { + if (!_tile.hasNextTile(i)) + continue; + + local nextTile = _tile.getNextTile(i); + if (nextTile.IsOccupiedByActor) + ret.push(nextTile.getEntity()); + } + return ret; + } q.getAlliedActors <- function( _faction, _tile = null, _distance = null, _atDistance = false ) { From 15e50d92c33fb7862c4e5bea46311a54d5255bba Mon Sep 17 00:00:00 2001 From: LordMidas <55047920+LordMidas@users.noreply.github.com> Date: Wed, 15 May 2024 10:33:51 -0400 Subject: [PATCH 2/2] perf: optimize getActors functions for when distance is 1 --- .../tactical/tactical_entity_manager.nut | 52 ++++++++++++++----- 1 file changed, 40 insertions(+), 12 deletions(-) diff --git a/msu/hooks/entity/tactical/tactical_entity_manager.nut b/msu/hooks/entity/tactical/tactical_entity_manager.nut index c463e3132..a9488e4d0 100644 --- a/msu/hooks/entity/tactical/tactical_entity_manager.nut +++ b/msu/hooks/entity/tactical/tactical_entity_manager.nut @@ -86,10 +86,17 @@ q.getAlliedActors <- function( _faction, _tile = null, _distance = null, _atDistance = false ) { - if (_tile != null && _tile.ID == 0) + if (_tile != null) { - ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); - throw ::MSU.Exception.InvalidValue(_tile); + if (_tile.ID == 0) + { + ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); + throw ::MSU.Exception.InvalidValue(_tile); + } + if (_distance == 1) + { + return this.getActorsWithinRange(_tile, 1, _atDistance ? 1 : 0).filter(@(_, _a) _a.isAlliedWith(_faction)); + } } return this.getActorsByFunction(function(_actor) { @@ -106,10 +113,17 @@ q.getHostileActors <- function( _faction, _tile = null, _distance = null, _atDistance = false ) { - if (_tile != null && _tile.ID == 0) + if (_tile != null) { - ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); - throw ::MSU.Exception.InvalidValue(_tile); + if (_tile.ID == 0) + { + ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); + throw ::MSU.Exception.InvalidValue(_tile); + } + if (_distance == 1) + { + return this.getActorsWithinRange(_tile, 1, _atDistance ? 1 : 0).filter(@(_, _a) !_a.isAlliedWith(_faction)); + } } return this.getActorsByFunction(function(_actor) { @@ -130,10 +144,17 @@ { return clone this.getInstancesOfFaction(_faction); } - else if (_tile.ID == 0) + else { - ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); - throw ::MSU.Exception.InvalidValue(_tile); + if (_tile.ID == 0) + { + ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); + throw ::MSU.Exception.InvalidValue(_tile); + } + if (_distance == 1) + { + return this.getActorsWithinRange(_tile, 1, _atDistance ? 1 : 0).filter(@(_, _a) _a.getFaction() == _faction); + } } local actors = this.getInstancesOfFaction(_faction); @@ -150,10 +171,17 @@ q.getNonFactionAlliedActors <- function( _faction, _tile = null, _distance = null, _atDistance = false ) { - if (_tile != null && _tile.ID == 0) + if (_tile != null) { - ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); - throw ::MSU.Exception.InvalidValue(_tile); + if (_tile.ID == 0) + { + ::logError("The ID of _tile is 0 which means that the actor this tile was fetched from is not placed on map."); + throw ::MSU.Exception.InvalidValue(_tile); + } + if (_distance == 1) + { + return this.getActorsWithinRange(_tile, 1, _atDistance ? 1 : 0).filter(@(_, _a) _a.getFaction() != _faction && _a.isAlliedWith(_faction)); + } } return this.getActorsByFunction(function(_actor) {