Skip to content

Commit

Permalink
Move viewsubscriber to shared + handle eye targets (#5362)
Browse files Browse the repository at this point in the history
* Move viewsubscriber to shared + handle eye targets

Need this stuff for AI.

* Fix dumb

* review
  • Loading branch information
metalgearsloth authored Aug 23, 2024
1 parent 36a5b67 commit 140767c
Show file tree
Hide file tree
Showing 6 changed files with 145 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
using Robust.Shared.GameObjects;

namespace Robust.Client.GameObjects;

public sealed class ViewSubscriberSystem : SharedViewSubscriberSystem;

This file was deleted.

128 changes: 63 additions & 65 deletions Robust.Server/GameObjects/EntitySystems/ViewSubscriberSystem.cs
Original file line number Diff line number Diff line change
@@ -1,89 +1,87 @@
using Robust.Shared.GameObjects;
using Robust.Shared.Player;

namespace Robust.Server.GameObjects
namespace Robust.Server.GameObjects;

/// <summary>
/// Entity System that handles subscribing and unsubscribing to PVS views.
/// </summary>
public sealed class ViewSubscriberSystem : SharedViewSubscriberSystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<ViewSubscriberComponent, ComponentShutdown>(OnViewSubscriberShutdown);
}

/// <summary>
/// Entity System that handles subscribing and unsubscribing to PVS views.
/// Subscribes the session to get PVS updates from the point of view of the specified entity.
/// </summary>
public sealed class ViewSubscriberSystem : EntitySystem
public override void AddViewSubscriber(EntityUid uid, ICommonSession session)
{
public override void Initialize()
{
base.Initialize();

SubscribeLocalEvent<ViewSubscriberComponent, ComponentShutdown>(OnViewSubscriberShutdown);
}

/// <summary>
/// Subscribes the session to get PVS updates from the point of view of the specified entity.
/// </summary>
public void AddViewSubscriber(EntityUid uid, ICommonSession session)
{
// If the entity doesn't have the component, it will be added.
var viewSubscriber = EntityManager.EnsureComponent<ViewSubscriberComponent>(uid);
// If the entity doesn't have the component, it will be added.
var viewSubscriber = EntityManager.EnsureComponent<Shared.GameObjects.ViewSubscriberComponent>(uid);

if (viewSubscriber.SubscribedSessions.Contains(session))
return; // Already subscribed, do nothing else.
if (viewSubscriber.SubscribedSessions.Contains(session))
return; // Already subscribed, do nothing else.

viewSubscriber.SubscribedSessions.Add(session);
session.ViewSubscriptions.Add(uid);
viewSubscriber.SubscribedSessions.Add(session);
session.ViewSubscriptions.Add(uid);

RaiseLocalEvent(uid, new ViewSubscriberAddedEvent(uid, session), true);
}

/// <summary>
/// Unsubscribes the session from getting PVS updates from the point of view of the specified entity.
/// </summary>
public void RemoveViewSubscriber(EntityUid uid, ICommonSession session)
{
if(!EntityManager.TryGetComponent(uid, out ViewSubscriberComponent? viewSubscriber))
return; // Entity didn't have any subscriptions, do nothing.

if (!viewSubscriber.SubscribedSessions.Remove(session))
return; // Session wasn't subscribed, do nothing.

session.ViewSubscriptions.Remove(uid);
RaiseLocalEvent(uid, new ViewSubscriberRemovedEvent(uid, session), true);
}

private void OnViewSubscriberShutdown(EntityUid uid, ViewSubscriberComponent component, ComponentShutdown _)
{
foreach (var session in component.SubscribedSessions)
{
session.ViewSubscriptions.Remove(uid);
}
}
RaiseLocalEvent(uid, new ViewSubscriberAddedEvent(uid, session), true);
}

/// <summary>
/// Raised when a session subscribes to an entity's PVS view.
/// Unsubscribes the session from getting PVS updates from the point of view of the specified entity.
/// </summary>
public sealed class ViewSubscriberAddedEvent : EntityEventArgs
public override void RemoveViewSubscriber(EntityUid uid, ICommonSession session)
{
public EntityUid View { get; }
public ICommonSession Subscriber { get; }
if(!EntityManager.TryGetComponent(uid, out Shared.GameObjects.ViewSubscriberComponent? viewSubscriber))
return; // Entity didn't have any subscriptions, do nothing.

if (!viewSubscriber.SubscribedSessions.Remove(session))
return; // Session wasn't subscribed, do nothing.

public ViewSubscriberAddedEvent(EntityUid view, ICommonSession subscriber)
session.ViewSubscriptions.Remove(uid);
RaiseLocalEvent(uid, new ViewSubscriberRemovedEvent(uid, session), true);
}

private void OnViewSubscriberShutdown(EntityUid uid, ViewSubscriberComponent component, ComponentShutdown _)
{
foreach (var session in component.SubscribedSessions)
{
View = view;
Subscriber = subscriber;
session.ViewSubscriptions.Remove(uid);
}
}
}

/// <summary>
/// Raised when a session is unsubscribed from an entity's PVS view.
/// Not raised when sessions are unsubscribed due to the component being removed.
/// </summary>
public sealed class ViewSubscriberRemovedEvent : EntityEventArgs
/// <summary>
/// Raised when a session subscribes to an entity's PVS view.
/// </summary>
public sealed class ViewSubscriberAddedEvent : EntityEventArgs
{
public EntityUid View { get; }
public ICommonSession Subscriber { get; }

public ViewSubscriberAddedEvent(EntityUid view, ICommonSession subscriber)
{
public EntityUid View { get; }
public ICommonSession Subscriber { get; }
View = view;
Subscriber = subscriber;
}
}

public ViewSubscriberRemovedEvent(EntityUid view, ICommonSession subscriber)
{
View = view;
Subscriber = subscriber;
}
/// <summary>
/// Raised when a session is unsubscribed from an entity's PVS view.
/// Not raised when sessions are unsubscribed due to the component being removed.
/// </summary>
public sealed class ViewSubscriberRemovedEvent : EntityEventArgs
{
public EntityUid View { get; }
public ICommonSession Subscriber { get; }

public ViewSubscriberRemovedEvent(EntityUid view, ICommonSession subscriber)
{
View = view;
Subscriber = subscriber;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System.Collections.Generic;
using Robust.Shared.Player;

namespace Robust.Shared.GameObjects;

// Not networked because doesn't do anything on client.
[RegisterComponent]
internal sealed partial class ViewSubscriberComponent : Component
{
internal readonly HashSet<ICommonSession> SubscribedSessions = new();
}
48 changes: 48 additions & 0 deletions Robust.Shared/GameObjects/Systems/SharedEyeSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,41 @@
using System.Numerics;
using Robust.Shared.IoC;
using Robust.Shared.Maths;
using Robust.Shared.Player;

namespace Robust.Shared.GameObjects;

public abstract class SharedEyeSystem : EntitySystem
{
[Dependency] private readonly SharedViewSubscriberSystem _views = default!;

public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<EyeComponent, PlayerAttachedEvent>(OnEyePlayerAttached);
SubscribeLocalEvent<EyeComponent, PlayerDetachedEvent>(OnEyePlayerDetached);
}

private void OnEyePlayerAttached(Entity<EyeComponent> ent, ref PlayerAttachedEvent args)
{
var value = ent.Comp.Target;

if (value != null && TryComp(ent.Owner, out ActorComponent? actorComp))
{
_views.AddViewSubscriber(value.Value, actorComp.PlayerSession);
}
}

private void OnEyePlayerDetached(Entity<EyeComponent> ent, ref PlayerDetachedEvent args)
{
var value = ent.Comp.Target;

if (value != null && TryComp(ent.Owner, out ActorComponent? actorComp))
{
_views.RemoveViewSubscriber(value.Value, actorComp.PlayerSession);
}
}

/// <summary>
/// Refreshes all values for IEye with the component.
/// </summary>
Expand Down Expand Up @@ -74,6 +104,10 @@ public void SetRotation(EntityUid uid, Angle rotation, EyeComponent? eyeComponen
eyeComponent.Eye.Rotation = rotation;
}

/// <summary>
/// Sets the eye component as tracking another entity.
/// Will also add the target to view subscribers so they can leave range and still work with PVS.
/// </summary>
public void SetTarget(EntityUid uid, EntityUid? value, EyeComponent? eyeComponent = null)
{
if (!Resolve(uid, ref eyeComponent))
Expand All @@ -82,6 +116,20 @@ public void SetTarget(EntityUid uid, EntityUid? value, EyeComponent? eyeComponen
if (eyeComponent.Target.Equals(value))
return;

// Automatically handle view subs.
if (TryComp(uid, out ActorComponent? actorComp))
{
if (value != null)
{
_views.AddViewSubscriber(value.Value, actorComp.PlayerSession);
}
else
{
// Should never be null here
_views.RemoveViewSubscriber(eyeComponent.Target!.Value, actorComp.PlayerSession);
}
}

eyeComponent.Target = value;
Dirty(uid, eyeComponent);
}
Expand Down
18 changes: 18 additions & 0 deletions Robust.Shared/GameObjects/Systems/SharedViewSubscriberSystem.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Robust.Shared.Player;

namespace Robust.Shared.GameObjects;

public abstract class SharedViewSubscriberSystem : EntitySystem
{
// NOOP on client

/// <summary>
/// Subscribes the session to get PVS updates from the point of view of the specified entity.
/// </summary>
public virtual void AddViewSubscriber(EntityUid uid, ICommonSession session) {}

/// <summary>
/// Unsubscribes the session from getting PVS updates from the point of view of the specified entity.
/// </summary>
public virtual void RemoveViewSubscriber(EntityUid uid, ICommonSession session) {}
}

0 comments on commit 140767c

Please sign in to comment.