Skip to content

Commit

Permalink
Add SharedPopupSystem.PopupPredicted (space-wizards#25811)
Browse files Browse the repository at this point in the history
* Added SharedPopupSystem.PopupPredicted

* Documentation improvement
  • Loading branch information
Tayrtahn committed Mar 6, 2024
1 parent 72b0889 commit 0738829
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 5 deletions.
8 changes: 7 additions & 1 deletion Content.Client/Popups/PopupSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ public override void PopupEntity(string? message, EntityUid uid, ICommonSession
PopupEntity(message, uid, type);
}

public override void PopupEntity(string? message, EntityUid uid, Filter filter, bool recordReplay, PopupType type=PopupType.Small)
public override void PopupEntity(string? message, EntityUid uid, Filter filter, bool recordReplay, PopupType type = PopupType.Small)
{
if (!filter.Recipients.Contains(_playerManager.LocalSession))
return;
Expand All @@ -170,6 +170,12 @@ public override void PopupEntity(string? message, EntityUid uid, PopupType type
PopupMessage(message, type, transform.Coordinates, uid, true);
}

public override void PopupPredicted(string? message, EntityUid uid, EntityUid? recipient, PopupType type = PopupType.Small)
{
if (recipient != null && _timing.IsFirstTimePredicted)
PopupEntity(message, uid, recipient.Value, type);
}

#endregion

#region Network Event Handlers
Expand Down
24 changes: 21 additions & 3 deletions Content.Server/Popups/PopupSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override void PopupCursor(string? message, PopupType type = PopupType.Sma
// No local user.
}

public override void PopupCursor(string? message, ICommonSession recipient, PopupType type=PopupType.Small)
public override void PopupCursor(string? message, ICommonSession recipient, PopupType type = PopupType.Small)
{
if (message == null)
return;
Expand Down Expand Up @@ -75,11 +75,11 @@ public override void PopupEntity(string? message, EntityUid uid, PopupType type
if (message == null)
return;

var filter = Filter.Empty().AddPlayersByPvs(uid, entityManager:EntityManager, playerMan: _player, cfgMan: _cfg);
var filter = Filter.Empty().AddPlayersByPvs(uid, entityManager: EntityManager, playerMan: _player, cfgMan: _cfg);
RaiseNetworkEvent(new PopupEntityEvent(message, type, GetNetEntity(uid)), filter);
}

public override void PopupEntity(string? message, EntityUid uid, EntityUid recipient, PopupType type=PopupType.Small)
public override void PopupEntity(string? message, EntityUid uid, EntityUid recipient, PopupType type = PopupType.Small)
{
if (message == null)
return;
Expand Down Expand Up @@ -108,5 +108,23 @@ public override void PopupEntity(string? message, EntityUid uid, Filter filter,

RaiseNetworkEvent(new PopupEntityEvent(message, type, GetNetEntity(uid)), filter, recordReplay);
}

public override void PopupPredicted(string? message, EntityUid uid, EntityUid? recipient, PopupType type = PopupType.Small)
{
if (message == null)
return;

if (recipient != null)
{
// Don't send to recipient, since they predicted it locally
var filter = Filter.PvsExcept(recipient.Value, entityManager: EntityManager);
RaiseNetworkEvent(new PopupEntityEvent(message, type, GetNetEntity(uid)), filter);
}
else
{
// With no recipient, send to everyone (in PVS range)
RaiseNetworkEvent(new PopupEntityEvent(message, type, GetNetEntity(uid)));
}
}
}
}
9 changes: 8 additions & 1 deletion Content.Shared/Popups/SharedPopupSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,17 @@ public abstract class SharedPopupSystem : EntitySystem
public abstract void PopupEntity(string? message, EntityUid uid, Filter filter, bool recordReplay, PopupType type = PopupType.Small);

/// <summary>
/// Variant of <see cref="PopupEnity(string, EntityUid, EntityUid, PopupType)"/> that only runs on the client, outside of prediction.
/// Variant of <see cref="PopupEntity(string, EntityUid, EntityUid, PopupType)"/> that only runs on the client, outside of prediction.
/// Useful for shared code that is always ran by both sides to avoid duplicate popups.
/// </summary>
public abstract void PopupClient(string? message, EntityUid uid, EntityUid recipient, PopupType type = PopupType.Small);

/// <summary>
/// Variant of <see cref="PopupEntity(string, EntityUid, EntityUid, PopupType)"/> for use with prediction. The local client will show
/// the popup to the recipient, and the server will show it to every other player in PVS range. If recipient is null, the local client
/// will do nothing and the server will show the message to every player in PVS range.
/// </summary>
public abstract void PopupPredicted(string? message, EntityUid uid, EntityUid? recipient, PopupType type = PopupType.Small);
}

/// <summary>
Expand Down

0 comments on commit 0738829

Please sign in to comment.