Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

INotifyCenterPositionChanged.CenterPositionChanged pass previous pos and layer #21287

Open
anvilvapre opened this issue Jan 7, 2024 · 1 comment

Comments

@anvilvapre
Copy link
Contributor

Idea/Wishlist, Bug

Proposal to allow Shroud - possibly other listeners - to know what the past value of an actor center position was - when it is notified that the center position of an actor changed. If the position was updated - yet the value remained the same - then the Shroud may not require any (expensive) updates.

Or as an alternative do not call SetCenterPosition if the value did not change. Or as an alternative - only notify listeners of actual changes.

Activities call Mobile.SetCenterPosition often/contineously without the position and layer having actually changed.

This may be required - to notify players of updates without changes - or more likely a bug.

At the end of the SetCenterPosition function all listeners are notified that the center position changed. The AffectsShroud traits is one of the listeners which calls UpdateShroudCells which is a quite expensive function as it needs to look up the projected cells of the actor.

This function is called for all actors for each activity for each logic tick. Called often.

Calls to CenterPosition where pos and layer do not change. Last column is WorldTick. This is just the start of a bot game. Hence mostly harvesters. But on the shell map you also see it for other type of actors.

==== Mobile.SetCenterPosition , Pos: 18944,95744,0, FromCell: 18,93, ToCell: 18,93, CellLayer: 0, harv 415, 1171
==== Mobile.SetCenterPosition , Pos: 92672,36352,0, FromCell: 90,35, ToCell: 90,35, CellLayer: 0, harv 421, 1172
==== Mobile.SetCenterPosition , Pos: 22016,89600,0, FromCell: 21,87, ToCell: 21,87, CellLayer: 0, apc 292, 1174
==== Mobile.SetCenterPosition , Pos: 105984,53760,0, FromCell: 103,52, ToCell: 103,52, CellLayer: 0, harv 429, 1176
==== Mobile.SetCenterPosition , Pos: 24789,89344,0, FromCell: 24,87, ToCell: 24,87, CellLayer: 0, e3 291, 1177
==== Mobile.SetCenterPosition , Pos: 104960,18944,0, FromCell: 102,18, ToCell: 102,18, CellLayer: 0, harv 420, 1177
==== Mobile.SetCenterPosition , Pos: 24064,90624,0, FromCell: 23,88, ToCell: 23,88, CellLayer: 0, e1 287, 1180
==== Mobile.SetCenterPosition , Pos: 105984,82432,0, FromCell: 103,80, ToCell: 103,80, CellLayer: 0, harv 427, 1181
==== Mobile.SetCenterPosition , Pos: 29184,45568,0, FromCell: 28,44, ToCell: 28,44, CellLayer: 0, harv 417, 1185
==== Mobile.SetCenterPosition , Pos: 105984,18944,0, FromCell: 103,18, ToCell: 103,18, CellLayer: 0, harv 420, 1191
==== Mobile.SetCenterPosition , Pos: 107008,53760,0, FromCell: 104,52, ToCell: 104,52, CellLayer: 0, harv 429, 1192
==== Mobile.SetCenterPosition , Pos: 91648,36352,0, FromCell: 89,35, ToCell: 89,35, CellLayer: 0, harv 421, 1196
==== Mobile.SetCenterPosition , Pos: 107008,82432,0, FromCell: 104,80, ToCell: 104,80, CellLayer: 0, harv 427, 1197
==== Mobile.SetCenterPosition , Pos: 34304,13824,0, FromCell: 33,13, ToCell: 33,13, CellLayer: 0, harv 413, 1198
==== Mobile.SetCenterPosition , Pos: 106496,18944,0, FromCell: 104,18, ToCell: 104,18, CellLayer: 0, harv 420, 1198
==== Mobile.SetCenterPosition , Pos: 14848,26112,0, FromCell: 14,25, ToCell: 14,25, CellLayer: 0, harv 423, 1199
==== Mobile.SetCenterPosition , Pos: 14848,83456,0, FromCell: 14,81, ToCell: 14,81, CellLayer: 0, harv 425, 1202
==== Mobile.SetCenterPosition , Pos: 23765,90368,0, FromCell: 23,88, ToCell: 23,88, CellLayer: 0, e3 291, 1204
==== Mobile.SetCenterPosition , Pos: 16896,94720,0, FromCell: 16,92, ToCell: 16,92, CellLayer: 0, harv 415, 1204
==== Mobile.SetCenterPosition , Pos: 30208,44544,0, FromCell: 29,43, ToCell: 29,43, CellLayer: 0, harv 417, 1205
==== Mobile.SetCenterPosition , Pos: 107008,18944,0, FromCell: 104,18, ToCell: 104,18, CellLayer: 0, harv 420, 1206
==== Mobile.SetCenterPosition , Pos: 23040,89600,0, FromCell: 22,87, ToCell: 22,87, CellLayer: 0, e1 287, 1207
==== Mobile.SetCenterPosition , Pos: 108032,18944,0, FromCell: 105,18, ToCell: 105,18, CellLayer: 0, harv 420, 1222
==== Mobile.SetCenterPosition , Pos: 31232,43520,0, FromCell: 30,42, ToCell: 30,42, CellLayer: 0, harv 417, 1228
==== Mobile.SetCenterPosition , Pos: 90624,36352,0, FromCell: 88,35, ToCell: 88,35, CellLayer: 0, harv 421, 1233
==== Mobile.SetCenterPosition , Pos: 108032,53760,0, FromCell: 105,52, ToCell: 105,52, CellLayer: 0, harv 429, 1233
==== Mobile.SetCenterPosition , Pos: 14848,93696,0, FromCell: 14,91, ToCell: 14,91, CellLayer: 0, harv 415, 1240
==== Mobile.SetCenterPosition , Pos: 107008,83456,0, FromCell: 104,81, ToCell: 104,81, CellLayer: 0, harv 427, 1251
==== Mobile.SetCenterPosition , Pos: 13824,26112,0, FromCell: 13,25, ToCell: 13,25, CellLayer: 0, harv 423, 1257
==== Mobile.SetCenterPosition , Pos: 109056,18944,0, FromCell: 106,18, ToCell: 106,18, CellLayer: 0, harv 420, 1258
==== Mobile.SetCenterPosition , Pos: 13824,83456,0, FromCell: 13,81, ToCell: 13,81, CellLayer: 0, harv 425, 1260
==== Mobile.SetCenterPosition , Pos: 34304,12800,0, FromCell: 33,12, ToCell: 33,12, CellLayer: 0, harv 413, 1269
==== Mobile.SetCenterPosition , Pos: 89600,36352,0, FromCell: 87,35, ToCell: 87,35, CellLayer: 0, harv 421, 1274
==== Mobile.SetCenterPosition , Pos: 13824,93696,0, FromCell: 13,91, ToCell: 13,91, CellLayer: 0, harv 415, 1276

public void SetCenterPosition(Actor self, WPos pos)
{
CenterPosition = pos;
self.World.UpdateMaps(self, this);
var map = self.World.Map;
SetTerrainRampOrientation(map.TerrainOrientation(map.CellContaining(pos)));
// The first time SetCenterPosition is called is in the constructor before creation, so we need a null check here as well
if (notifyCenterPositionChanged == null)
return;
foreach (var n in notifyCenterPositionChanged)
n.CenterPositionChanged(self, FromCell.Layer, ToCell.Layer);
}

[RequireExplicitImplementation]
public interface INotifyCenterPositionChanged
{
void CenterPositionChanged(Actor self, byte oldLayer, byte newLayer);
}

@abcdefg30
Copy link
Member

AffectsShroud already reduces the amount of updates by caching the center position:

var centerPosition = self.CenterPosition;
var projectedPos = centerPosition - new WVec(0, centerPosition.Z, centerPosition.Z);
var projectedLocation = self.World.Map.CellContaining(projectedPos);
var pos = self.CenterPosition;
var dirty = Info.MoveRecalculationThreshold.Length > 0 && (pos - cachedPos).LengthSquared > Info.MoveRecalculationThreshold.LengthSquared;
if (!dirty && cachedLocation == projectedLocation)
return;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants