Skip to content

Commit

Permalink
client.images test
Browse files Browse the repository at this point in the history
  • Loading branch information
amylizzle committed Dec 24, 2023
1 parent 9a6e98f commit fbc20c1
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 52 deletions.
51 changes: 20 additions & 31 deletions OpenDreamClient/Rendering/ClientImagesSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@

namespace OpenDreamClient.Rendering;
sealed class ClientImagesSystem : SharedClientImagesSystem {
private readonly Dictionary<Vector3, List<int>> TurfClientImages = new();
private readonly Dictionary<EntityUid, List<int>> AMClientImages = new();
private readonly Dictionary<Vector3, List<NetEntity>> TurfClientImages = new();
private readonly Dictionary<EntityUid, List<NetEntity>> AMClientImages = new();
private readonly Dictionary<int, DreamIcon> _idToIcon = new();
[Dependency] private readonly ClientAppearanceSystem _clientAppearanceSystem = default!;
[Dependency] private IEntityManager _entityManager = default!;

public override void Initialize() {
Expand All @@ -21,42 +20,28 @@ public override void Shutdown() {
_idToIcon.Clear();
}

public bool TryGetClientImages(EntityUid entity, Vector3? tileCoords, [NotNullWhen(true)] out List<DreamIcon>? result){
result = null;
List<int>? resultIDs;
public bool TryGetClientImages(EntityUid entity, Vector3? tileCoords, [NotNullWhen(true)] out List<NetEntity>? result){
if(entity == EntityUid.Invalid && tileCoords is not null) {
if(!TurfClientImages.TryGetValue(tileCoords.Value, out resultIDs))
if(!TurfClientImages.TryGetValue(tileCoords.Value, out result))
return false;
} else {
if(!AMClientImages.TryGetValue(entity, out resultIDs))
if(!AMClientImages.TryGetValue(entity, out result))
return false;
}
result = new List<DreamIcon>();
foreach(int distinctID in resultIDs)
if(_idToIcon.TryGetValue(distinctID, out DreamIcon? icon))
result.Add(icon);
return result.Count > 0;
}

private void OnAddClientImage(AddClientImageEvent e) {
EntityUid ent = _entityManager.GetEntity(e.AttachedEntity);
if(ent == EntityUid.Invalid) {
if(!TurfClientImages.TryGetValue(e.TurfCoords, out var iconList))
iconList = new List<int>();
if(!_idToIcon.ContainsKey(e.ImageAppearance)){
DreamIcon icon = new DreamIcon(e.ImageAppearance);
_idToIcon[e.ImageAppearance] = icon;
}
iconList.Add(e.ImageAppearance);
iconList = new List<NetEntity>();
iconList.Add(e.ImageEntity);
TurfClientImages[e.TurfCoords] = iconList;
} else {
if(!AMClientImages.TryGetValue(ent, out var iconList))
iconList = new List<int>();
if(!_idToIcon.ContainsKey(e.ImageAppearance)){
DreamIcon icon = new DreamIcon(e.ImageAppearance);
_idToIcon[e.ImageAppearance] = icon;
}
iconList.Add(e.ImageAppearance);
iconList = new List<NetEntity>();
iconList.Add(e.ImageEntity);
AMClientImages[ent] = iconList;
}

Expand All @@ -65,17 +50,21 @@ private void OnAddClientImage(AddClientImageEvent e) {
private void OnRemoveClientImage(RemoveClientImageEvent e) {
EntityUid ent = _entityManager.GetEntity(e.AttachedEntity);
if(ent == EntityUid.Invalid) {
if(!TurfClientImages.TryGetValue(e.TurfCoords, out var iconList))
return;
iconList.Remove(e.ImageAppearance);
if(!TurfClientImages.TryGetValue(e.TurfCoords, out var iconList))
return;
iconList.Remove(e.ImageEntity);
if(iconList.Count == 0)
TurfClientImages.Remove(e.TurfCoords);
else
TurfClientImages[e.TurfCoords] = iconList;
_idToIcon.Remove(e.ImageAppearance);
} else {
if(!AMClientImages.TryGetValue(ent, out var iconList))
return;
iconList.Remove(e.ImageAppearance);
AMClientImages[ent] = iconList;
_idToIcon.Remove(e.ImageAppearance);
iconList.Remove(e.ImageEntity);
if(iconList.Count == 0)
AMClientImages.Remove(ent);
else
AMClientImages[ent] = iconList;
}
}
}
15 changes: 9 additions & 6 deletions OpenDreamClient/Rendering/DreamViewOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -304,14 +304,17 @@ private void ProcessIconComponents(DreamIcon icon, Vector2 position, EntityUid u
//client images act as either an overlay or replace the main icon
//notably they cannot be applied to overlays, so don't check for them if this is an under/overlay
//note also that we use turfCoords and not current.Position because we want world-coordinates, not screen coordinates. This is only used for turfs.
if(parentIcon == null && _clientImagesSystem.TryGetClientImages(current.Uid, turfCoords, out List<DreamIcon>? attachedClientImages)){
foreach(DreamIcon CI in attachedClientImages){
if(CI.Appearance == null)
if(parentIcon == null && _clientImagesSystem.TryGetClientImages(current.Uid, turfCoords, out List<NetEntity>? attachedClientImages)){
foreach(NetEntity CINetEntity in attachedClientImages){
EntityUid imageEntity = _entityManager.GetEntity(CINetEntity);
if (!_spriteQuery.TryGetComponent(imageEntity, out var sprite))
continue;
if(CI.Appearance.Override)
current.MainIcon = CI;
if(sprite.Icon.Appearance == null)
continue;
if(sprite.Icon.Appearance.Override)
current.MainIcon = sprite.Icon;
else
ProcessIconComponents(CI, current.Position, uid, isScreen, ref tieBreaker, result, current, false);
ProcessIconComponents(sprite.Icon, current.Position, uid, isScreen, ref tieBreaker, result, current, false);
}
}

Expand Down
18 changes: 9 additions & 9 deletions OpenDreamRuntime/Rendering/ServerClientImagesSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,31 @@
using OpenDreamShared.Rendering;
using OpenDreamRuntime.Objects;
using Vector3 = Robust.Shared.Maths.Vector3;
using Robust.Server.GameStates;

namespace OpenDreamRuntime.Rendering;
public sealed class ServerClientImagesSystem : SharedClientImagesSystem {
[Dependency] private readonly ServerAppearanceSystem _serverAppearanceSystem = default!;
[Dependency] private readonly AtomManager _atomManager = default!;

private readonly PvsOverrideSystem _pvsOverrideSystem = default!;
public void AddImageObject(DreamConnection connection, DreamObjectImage imageObject) {
DreamObject? loc = imageObject.GetAttachedLoc();
if(loc == null)
return;

EntityUid locEntity = EntityUid.Invalid;
Vector3 turfCoords = Vector3.Zero;
int locAppearanceID = 0;

int imageAppearanceID = _serverAppearanceSystem.AddAppearance(imageObject.Appearance!);

if(loc is DreamObjectMovable movable)
locEntity = movable.Entity;
else if(loc is DreamObjectTurf turf)
turfCoords = new Vector3(turf.X, turf.Y, turf.Z);

NetEntity ent = GetNetEntity(locEntity);
RaiseNetworkEvent(new AddClientImageEvent(ent, turfCoords, imageAppearanceID), connection.Session.ConnectedClient);
EntityUid imageObjectEntity = imageObject.GetEntity();
if (imageObjectEntity != EntityUid.Invalid)
_pvsOverrideSystem.AddGlobalOverride(imageObjectEntity);
NetEntity imageObjectNetEntity = GetNetEntity(imageObjectEntity);
RaiseNetworkEvent(new AddClientImageEvent(ent, turfCoords, imageObjectNetEntity), connection.Session!.Channel);
}

public void RemoveImageObject(DreamConnection connection, DreamObjectImage imageObject) {
Expand All @@ -36,15 +37,14 @@ public void RemoveImageObject(DreamConnection connection, DreamObjectImage image
EntityUid locEntity = EntityUid.Invalid;
Vector3 turfCoords = Vector3.Zero;

int imageAppearanceID = _serverAppearanceSystem.AddAppearance(imageObject.Appearance!);

if(loc is DreamObjectMovable)
locEntity = ((DreamObjectMovable)loc).Entity;
else if(loc is DreamObjectTurf turf)
turfCoords = new Vector3(turf.X, turf.Y, turf.Z);


NetEntity ent = GetNetEntity(locEntity);
RaiseNetworkEvent(new RemoveClientImageEvent(ent, turfCoords, imageAppearanceID), connection.Session.ConnectedClient);
NetEntity imageObjectNetEntity = GetNetEntity(imageObject.GetEntity());
RaiseNetworkEvent(new RemoveClientImageEvent(ent, turfCoords, imageObjectNetEntity), connection.Session!.Channel);
}
}
12 changes: 6 additions & 6 deletions OpenDreamShared/Rendering/SharedClientImagesSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ public class SharedClientImagesSystem : EntitySystem {
public sealed class AddClientImageEvent : EntityEventArgs {
public Vector3 TurfCoords;
public NetEntity AttachedEntity; //if this is NetEntity.Invalid (ie, a turf) use the TurfCoords instead
public int ImageAppearance;
public NetEntity ImageEntity;

public AddClientImageEvent(NetEntity attachedEntity, Vector3 turfCoords, int imageAppearance) {
public AddClientImageEvent(NetEntity attachedEntity, Vector3 turfCoords, NetEntity imageEntity) {
TurfCoords = turfCoords;
ImageAppearance = imageAppearance;
ImageEntity = imageEntity;
AttachedEntity = attachedEntity;
}
}
Expand All @@ -25,11 +25,11 @@ public AddClientImageEvent(NetEntity attachedEntity, Vector3 turfCoords, int ima
public sealed class RemoveClientImageEvent : EntityEventArgs {
public Vector3 TurfCoords;
public NetEntity AttachedEntity; //if this is NetEntity.Invalid (ie, a turf) use the TurfCoords instead
public int ImageAppearance;
public NetEntity ImageEntity;

public RemoveClientImageEvent(NetEntity attachedEntity, Vector3 turfCoords, int imageAppearance) {
public RemoveClientImageEvent(NetEntity attachedEntity, Vector3 turfCoords, NetEntity imageEntity) {
TurfCoords = turfCoords;
ImageAppearance = imageAppearance;
ImageEntity = imageEntity;
AttachedEntity = attachedEntity;
}
}
Expand Down

0 comments on commit fbc20c1

Please sign in to comment.