- simplify ownership check in DisplayRallyPoint with IsOwnedByPlayerOrMutualAlly

- fix rally point path not following moving targets via OnUpdate
-combine set/unset-rallypoint loops in Commands.js
- remove GUI-side entity splitting in unit_actions.js
This commit is contained in:
mehmed-faheim-arslan 2026-04-25 22:12:55 +01:00 committed by mehmed-faheim-arslan
parent d40fb8bb2a
commit 418ec65d5d
3 changed files with 45 additions and 65 deletions

View file

@ -1123,19 +1123,9 @@ var g_UnitActions =
if (action.position)
position = action.position;
const ownEntities = selection.filter(ent => {
const entState = GetEntityState(ent);
return !entState || entState.player === g_ViewedPlayer;
});
const alliedEntities = selection.filter(ent => {
const entState = GetEntityState(ent);
return entState && entState.player !== g_ViewedPlayer;
});
Engine.PostNetworkCommand({
"type": "set-rallypoint",
"entities": ownEntities,
"alliedEntities": alliedEntities,
"alliedEntities": selection,
"x": position.x,
"z": position.z,
"data": action.data,
@ -1363,19 +1353,9 @@ var g_UnitActions =
{
"execute": function(position, action, selection, queued, pushFront)
{
const ownEntities = selection.filter(ent => {
const entState = GetEntityState(ent);
return !entState || entState.player === g_ViewedPlayer;
});
const alliedEntities = selection.filter(ent => {
const entState = GetEntityState(ent);
return entState && entState.player !== g_ViewedPlayer;
});
Engine.PostNetworkCommand({
"type": "unset-rallypoint",
"entities": ownEntities,
"alliedEntities": alliedEntities
"alliedEntities": selection
});
// Remove displayed rally point

View file

@ -397,11 +397,7 @@ GuiInterface.prototype.GetEntityState = function(player, ent)
const cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
if (cmpRallyPoint)
{
const entOwner = cmpOwnership ? cmpOwnership.GetOwner() : INVALID_PLAYER;
const rallyPositions = cmpRallyPoint.GetPositions(player !== entOwner ? player : undefined);
ret.rallyPoint = { "position": rallyPositions[0] }; // undefined or {x,z} object
}
ret.rallyPoint = { "position": cmpRallyPoint.GetPositions(player)[0] }; // undefined or {x,z} object
const cmpGarrisonHolder = Engine.QueryInterface(ent, IID_GarrisonHolder);
if (cmpGarrisonHolder)
@ -1039,6 +1035,16 @@ GuiInterface.prototype.GetNonGaiaEntities = function()
return Engine.QueryInterface(SYSTEM_ENTITY, IID_RangeManager).GetNonGaiaEntities();
};
/**
* @param {number} entity - The entityID to verify.
* @param {number} player - The playerID to check against.
* @return {boolean}.
*/
function IsOwnedByPlayerOrMutualAlly(entity, player)
{
return IsOwnedByPlayer(player, entity) || IsOwnedByMutualAllyOfPlayer(player, entity);
}
/**
* Displays the rally points of a given list of entities (carried in cmd.entities).
*
@ -1050,10 +1056,8 @@ GuiInterface.prototype.GetNonGaiaEntities = function()
*/
GuiInterface.prototype.DisplayRallyPoint = function(player, cmd)
{
const cmpPlayer = QueryPlayerIDInterface(player);
// If there are some rally points already displayed, first hide them.
for (const ent of this.entsRallyPointsDisplayed)
for (const { ent } of this.entsRallyPointsDisplayed)
{
const cmpRallyPointRenderer = Engine.QueryInterface(ent, IID_RallyPointRenderer);
if (cmpRallyPointRenderer)
@ -1075,12 +1079,7 @@ GuiInterface.prototype.DisplayRallyPoint = function(player, cmd)
if (!cmpRallyPoint)
continue;
// Verify the owner.
const cmpOwnership = Engine.QueryInterface(ent, IID_Ownership);
const entOwner = cmpOwnership ? cmpOwnership.GetOwner() : INVALID_PLAYER;
const isOwnBuilding = entOwner == player || cmpPlayer && cmpPlayer.CanControlAllUnits();
const isAlliedBuilding = !isOwnBuilding && IsOwnedByMutualAllyOfPlayer(player, ent);
if (!isOwnBuilding && !isAlliedBuilding)
if (!IsOwnedByPlayerOrMutualAlly(ent, player))
continue;
// If the command was passed an explicit position, use that and
@ -1090,7 +1089,7 @@ GuiInterface.prototype.DisplayRallyPoint = function(player, cmd)
pos = cmd;
else
// may return undefined if no rally point is set.
pos = cmpRallyPoint.GetPositions(isOwnBuilding ? undefined : player)[0];
pos = cmpRallyPoint.GetPositions(player)[0];
if (pos)
{
@ -1106,7 +1105,7 @@ GuiInterface.prototype.DisplayRallyPoint = function(player, cmd)
else if (!cmpRallyPointRenderer.IsSet())
{
// Rebuild the renderer when not set (when reading saved game or in case of building update).
const positions = cmpRallyPoint.GetPositions(isOwnBuilding ? undefined : player);
const positions = cmpRallyPoint.GetPositions(player);
for (const posi of positions)
cmpRallyPointRenderer.AddPosition(new Vector2D(posi.x, posi.z));
}
@ -1114,11 +1113,34 @@ GuiInterface.prototype.DisplayRallyPoint = function(player, cmd)
cmpRallyPointRenderer.SetDisplayed(true);
// Remember which entities have their rally points displayed so we can hide them again.
this.entsRallyPointsDisplayed.push(ent);
this.entsRallyPointsDisplayed.push({ ent, player });
}
}
};
GuiInterface.prototype.OnUpdate = function()
{
for (const { ent, player } of this.entsRallyPointsDisplayed)
{
const cmpRallyPointRenderer = Engine.QueryInterface(ent, IID_RallyPointRenderer);
if (!cmpRallyPointRenderer)
continue;
const cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
if (!cmpRallyPoint)
continue;
const positions = cmpRallyPoint.GetPositions(player);
if (!positions.length)
continue;
// Update renderer positions so the path follows moving targets.
cmpRallyPointRenderer.SetPosition(new Vector2D(positions[0].x, positions[0].z));
for (let i = 1; i < positions.length; i++)
cmpRallyPointRenderer.AddPosition(new Vector2D(positions[i].x, positions[i].z));
}
};
GuiInterface.prototype.AddTargetMarker = function(player, cmd)
{
const ent = Engine.AddLocalEntity(cmd.template);

View file

@ -434,25 +434,12 @@ var g_Commands = {
"set-rallypoint": function(player, cmd, data)
{
for (const ent of data.entities || [])
{
var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
if (!cmpRallyPoint)
continue;
if (!cmd.queued)
cmpRallyPoint.Unset();
cmpRallyPoint.AddPosition(cmd.x, cmd.z);
cmpRallyPoint.AddData(clone(cmd.data));
}
for (const ent of (cmd.alliedEntities || []))
{
if (!IsOwnedByMutualAllyOfPlayer(player, ent))
if (!IsOwnedByPlayerOrMutualAlly(ent, player))
continue;
var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
const cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
if (!cmpRallyPoint)
continue;
@ -466,21 +453,12 @@ var g_Commands = {
"unset-rallypoint": function(player, cmd, data)
{
for (const ent of data.entities || [])
{
var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
if (!cmpRallyPoint)
continue;
cmpRallyPoint.Unset();
}
for (const ent of (cmd.alliedEntities || []))
{
if (!IsOwnedByMutualAllyOfPlayer(player, ent))
if (!IsOwnedByPlayerOrMutualAlly(ent, player))
continue;
var cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
const cmpRallyPoint = Engine.QueryInterface(ent, IID_RallyPoint);
if (!cmpRallyPoint)
continue;