decal system & crayons (#5183)

Co-authored-by: Paul <ritter.paul1+git@googlemail.com>
This commit is contained in:
Paul Ritter
2021-12-03 15:35:57 +01:00
committed by GitHub
parent 46a63d82b0
commit 219d91c6da
22 changed files with 2100 additions and 220 deletions

View File

@@ -1,11 +1,13 @@
using System.Linq;
using Content.Server.Cleanable;
using Content.Server.Coordinates.Helpers;
using Content.Shared.Chemistry;
using Content.Server.Decals;
using Content.Shared.Chemistry.Reaction;
using Content.Shared.Chemistry.Reagent;
using Content.Shared.FixedPoint;
using Robust.Shared.GameObjects;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Chemistry.TileReactions
@@ -35,6 +37,12 @@ namespace Content.Server.Chemistry.TileReactions
}
}
var decalSystem = EntitySystem.Get<DecalSystem>();
foreach (var uid in decalSystem.GetDecalsInRange(tile.GridIndex, tile.GridIndices+new Vector2(0.5f, 0.5f), validDelegate: x => x.Cleanable))
{
decalSystem.RemoveDecal(tile.GridIndex, uid);
}
return amount;
}
}

View File

@@ -2,9 +2,10 @@ using System.Linq;
using System.Threading.Tasks;
using Content.Server.Administration.Logs;
using Content.Server.UserInterface;
using Content.Shared.Administration.Logs;
using Content.Shared.Audio;
using Content.Shared.Crayon;
using Content.Server.Decals;
using Content.Shared.Decals;
using Content.Shared.Database;
using Content.Shared.Interaction;
using Content.Shared.Interaction.Helpers;
@@ -60,11 +61,8 @@ namespace Content.Server.Crayon
Charges = Capacity;
// Get the first one from the catalog and set it as default
var decals = _prototypeManager.EnumeratePrototypes<CrayonDecalPrototype>().FirstOrDefault();
if (decals != null)
{
SelectedState = decals.Decals.First();
}
var decal = _prototypeManager.EnumeratePrototypes<DecalPrototype>().FirstOrDefault(x => x.Tags.Contains("crayon"));
SelectedState = decal?.ID ?? string.Empty;
Dirty();
}
@@ -74,14 +72,10 @@ namespace Content.Server.Crayon
{
case CrayonSelectMessage msg:
// Check if the selected state is valid
var crayonDecals = _prototypeManager.EnumeratePrototypes<CrayonDecalPrototype>().FirstOrDefault();
if (crayonDecals != null)
if (_prototypeManager.TryIndex<DecalPrototype>(msg.State, out var prototype) && prototype.Tags.Contains("crayon"))
{
if (crayonDecals.Decals.Contains(msg.State))
{
SelectedState = msg.State;
Dirty();
}
SelectedState = msg.State;
Dirty();
}
break;
default:
@@ -131,12 +125,8 @@ namespace Content.Server.Crayon
return true;
}
var entity = Owner.EntityManager.SpawnEntity("CrayonDecal", eventArgs.ClickLocation);
if (entity.TryGetComponent(out AppearanceComponent? appearance))
{
appearance.SetData(CrayonVisuals.State, SelectedState);
appearance.SetData(CrayonVisuals.Color, _color);
}
if(!EntitySystem.Get<DecalSystem>().TryAddDecal(SelectedState, eventArgs.ClickLocation.Offset(new Vector2(-0.5f,-0.5f)), out _, Color.FromName(_color), cleanable: true))
return false;
if (_useSound != null)
SoundSystem.Play(Filter.Pvs(Owner), _useSound.GetSound(), Owner, AudioHelpers.WithVariation(0.125f));

View File

@@ -0,0 +1,118 @@
using System;
using System.Collections.Generic;
using Content.Server.Administration;
using Content.Shared.Administration;
using Content.Shared.Decals;
using Content.Shared.Maps;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
namespace Content.Server.Decals.Commands
{
[AdminCommand(AdminFlags.Mapping)]
public sealed class AddDecalCommand : IConsoleCommand
{
public string Command => "adddecal";
public string Description => "Creates a decal on the map";
public string Help => $"{Command} <id> <x position> <y position> <gridId> [angle=<angle> zIndex=<zIndex> color=<color>]";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 4 || args.Length > 7)
{
shell.WriteError($"Received invalid amount of arguments arguments. Expected 4 to 7, got {args.Length}.\nUsage: {Help}");
return;
}
if (!IoCManager.Resolve<IPrototypeManager>().HasIndex<DecalPrototype>(args[0]))
{
shell.WriteError($"Cannot find decalprototype '{args[0]}'.");
}
if (!float.TryParse(args[1], out var x))
{
shell.WriteError($"Failed parsing x-coordinate '{args[1]}'.");
return;
}
if (!float.TryParse(args[2], out var y))
{
shell.WriteError($"Failed parsing y-coordinate'{args[2]}'.");
return;
}
var mapManager = IoCManager.Resolve<IMapManager>();
if (!int.TryParse(args[3], out var gridIdRaw) || !mapManager.TryGetGrid(new GridId(gridIdRaw), out var grid))
{
shell.WriteError($"Failed parsing gridId '{args[3]}'.");
return;
}
var coordinates = new EntityCoordinates(grid.GridEntityId, new Vector2(x, y));
if (grid.GetTileRef(coordinates).IsSpace())
{
shell.WriteError($"Cannot create decal on space tile at {coordinates}.");
return;
}
Color? color = null;
var zIndex = 0;
Angle? rotation = null;
if (args.Length > 4)
{
for (int i = 4; i < args.Length; i++)
{
var rawValue = args[i].Split('=');
if (rawValue.Length != 2)
{
shell.WriteError($"Failed parsing parameter: '{args[i]}'");
return;
}
switch (rawValue[0])
{
case "angle":
if (!double.TryParse(rawValue[1], out var degrees))
{
shell.WriteError($"Failed parsing angle '{rawValue[1]}'.");
return;
}
rotation = Angle.FromDegrees(degrees);
break;
case "zIndex":
if (!int.TryParse(rawValue[1], out zIndex))
{
shell.WriteError($"Failed parsing zIndex '{rawValue[1]}'.");
return;
}
break;
case "color":
if (!Color.TryFromName(rawValue[1], out var colorRaw))
{
shell.WriteError($"Failed parsing color '{rawValue[1]}'.");
return;
}
color = colorRaw;
break;
default:
shell.WriteError($"Unknown parameter key '{rawValue[0]}'.");
return;
}
}
}
if(EntitySystem.Get<DecalSystem>().TryAddDecal(args[0], coordinates, out var uid, color, rotation, zIndex))
{
shell.WriteLine($"Successfully created decal {uid}.");
}
else
{
shell.WriteError($"Failed adding decal.");
}
}
}
}

View File

@@ -0,0 +1,162 @@
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
namespace Content.Server.Decals;
[AdminCommand(AdminFlags.Mapping)]
public class EditDecalCommand : IConsoleCommand
{
public string Command => "editdecal";
public string Description => "Edits a decal.";
public string Help => $@"{Command} <gridId> <uid> <mode>\n
Possible modes are:\n
- position <x position> <y position>\n
- color <color>\n
- id <id>\n
- rotation <degrees>\n
- zindex <zIndex>\n
- clean <cleanable>
";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length < 4)
{
shell.WriteError("Expected at least 5 arguments.");
return;
}
if (!int.TryParse(args[0], out var gridIdRaw))
{
shell.WriteError($"Failed parsing gridId '{args[3]}'.");
return;
}
if (!uint.TryParse(args[1], out var uid))
{
shell.WriteError($"Failed parsing uid '{args[1]}'.");
return;
}
var gridId = new GridId(gridIdRaw);
if (!IoCManager.Resolve<IMapManager>().GridExists(gridId))
{
shell.WriteError($"No grid with gridId {gridId} exists.");
return;
}
var decalSystem = EntitySystem.Get<DecalSystem>();
switch (args[2].ToLower())
{
case "position":
if(args.Length != 5)
{
shell.WriteError("Expected 6 arguments.");
return;
}
if (!float.TryParse(args[3], out var x) || !float.TryParse(args[4], out var y))
{
shell.WriteError("Failed parsing position.");
return;
}
if (!decalSystem.SetDecalPosition(gridId, uid, gridId, new Vector2(x, y)))
{
shell.WriteError("Failed changing decalposition.");
}
break;
case "color":
if(args.Length != 4)
{
shell.WriteError("Expected 5 arguments.");
return;
}
if (!Color.TryFromName(args[3], out var color))
{
shell.WriteError("Failed parsing color.");
return;
}
if (!decalSystem.SetDecalColor(gridId, uid, color))
{
shell.WriteError("Failed changing decal color.");
}
break;
case "id":
if(args.Length != 4)
{
shell.WriteError("Expected 5 arguments.");
return;
}
if (!decalSystem.SetDecalId(gridId, uid, args[3]))
{
shell.WriteError("Failed changing decal id.");
}
break;
case "rotation":
if(args.Length != 4)
{
shell.WriteError("Expected 5 arguments.");
return;
}
if (!double.TryParse(args[3], out var degrees))
{
shell.WriteError("Failed parsing degrees.");
return;
}
if (!decalSystem.SetDecalRotation(gridId, uid, Angle.FromDegrees(degrees)))
{
shell.WriteError("Failed changing decal rotation.");
}
break;
case "zindex":
if(args.Length != 4)
{
shell.WriteError("Expected 5 arguments.");
return;
}
if (!int.TryParse(args[3], out var zIndex))
{
shell.WriteError("Failed parsing zIndex.");
return;
}
if (!decalSystem.SetDecalZIndex(gridId, uid, zIndex))
{
shell.WriteError("Failed changing decal zIndex.");
}
break;
case "clean":
if(args.Length != 4)
{
shell.WriteError("Expected 5 arguments.");
return;
}
if (!bool.TryParse(args[3], out var cleanable))
{
shell.WriteError("Failed parsing cleanable.");
return;
}
if (!decalSystem.SetDecalCleanable(gridId, uid, cleanable))
{
shell.WriteError("Failed changing decal cleanable flag.");
}
break;
default:
shell.WriteError("Invalid mode.");
return;
}
}
}

View File

@@ -0,0 +1,46 @@
using Content.Server.Administration;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
namespace Content.Server.Decals.Commands
{
[AdminCommand(AdminFlags.Mapping)]
public class RemoveDecalCommand : IConsoleCommand
{
public string Command => "rmdecal";
public string Description => "removes a decal";
public string Help => $"{Command} <uid> <gridId>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 2)
{
shell.WriteError($"Unexpected number of arguments.\nExpected two: {Help}");
return;
}
if (!uint.TryParse(args[0], out var uid))
{
shell.WriteError($"Failed parsing uid.");
return;
}
if (!int.TryParse(args[1], out var rawGridId) ||
!IoCManager.Resolve<IMapManager>().GridExists(new GridId(rawGridId)))
{
shell.WriteError("Failed parsing gridId.");
}
var decalSystem = EntitySystem.Get<DecalSystem>();
if (decalSystem.RemoveDecal(new GridId(rawGridId), uid))
{
shell.WriteLine($"Successfully removed decal {uid}.");
return;
}
shell.WriteError($"Failed trying to remove decal {uid}.");
}
}
}

View File

@@ -0,0 +1,326 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Content.Shared.Decals;
using Content.Shared.Maps;
using Robust.Server.Player;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Player;
namespace Content.Server.Decals
{
public class DecalSystem : SharedDecalSystem
{
[Dependency] private readonly IPlayerManager _playerManager = default!;
private readonly Dictionary<GridId, HashSet<Vector2i>> _dirtyChunks = new();
private readonly Dictionary<IPlayerSession, Dictionary<GridId, HashSet<Vector2i>>> _previousSentChunks = new();
public override void Initialize()
{
base.Initialize();
_playerManager.PlayerStatusChanged += OnPlayerStatusChanged;
MapManager.TileChanged += OnTileChanged;
}
public override void Shutdown()
{
base.Shutdown();
_playerManager.PlayerStatusChanged -= OnPlayerStatusChanged;
MapManager.TileChanged -= OnTileChanged;
}
private void OnTileChanged(object? sender, TileChangedEventArgs e)
{
if (!e.NewTile.IsSpace())
return;
var chunkCollection = ChunkCollection(e.NewTile.GridIndex);
var indices = GetChunkIndices(e.NewTile.GridIndices);
var toDelete = new HashSet<uint>();
if (chunkCollection.TryGetValue(indices, out var chunk))
{
foreach (var (uid, decal) in chunk)
{
if (new Vector2((int) Math.Floor(decal.Coordinates.X), (int) Math.Floor(decal.Coordinates.Y)) ==
e.NewTile.GridIndices)
{
toDelete.Add(uid);
}
}
}
if (toDelete.Count == 0) return;
foreach (var uid in toDelete)
{
RemoveDecalInternal(e.NewTile.GridIndex, uid);
}
DirtyChunk(e.NewTile.GridIndex, indices);
}
private void OnPlayerStatusChanged(object? sender, SessionStatusEventArgs e)
{
switch (e.NewStatus)
{
case SessionStatus.InGame:
_previousSentChunks[e.Session] = new();
break;
case SessionStatus.Disconnected:
_previousSentChunks.Remove(e.Session);
break;
}
}
protected override void DirtyChunk(GridId id, Vector2i chunkIndices)
{
if(!_dirtyChunks.ContainsKey(id))
_dirtyChunks[id] = new HashSet<Vector2i>();
_dirtyChunks[id].Add(chunkIndices);
}
public bool TryAddDecal(string id, EntityCoordinates coordinates, [NotNullWhen(true)] out uint? uid, Color? color = null, Angle? rotation = null, int zIndex = 0, bool cleanable = false)
{
uid = 0;
if (!PrototypeManager.HasIndex<DecalPrototype>(id))
return false;
var gridId = coordinates.GetGridId(EntityManager);
if (MapManager.GetGrid(gridId).GetTileRef(coordinates).IsSpace())
return false;
rotation ??= Angle.Zero;
var decal = new Decal(coordinates.Position, id, color, rotation.Value, zIndex, cleanable);
var chunkCollection = DecalGridChunkCollection(gridId);
uid = chunkCollection.NextUid++;
var chunkIndices = GetChunkIndices(decal.Coordinates);
if(!chunkCollection.ChunkCollection.ContainsKey(chunkIndices))
chunkCollection.ChunkCollection[chunkIndices] = new();
chunkCollection.ChunkCollection[chunkIndices][uid.Value] = decal;
ChunkIndex[gridId][uid.Value] = chunkIndices;
DirtyChunk(gridId, chunkIndices);
return true;
}
public bool RemoveDecal(GridId gridId, uint uid) => RemoveDecalInternal(gridId, uid);
public HashSet<uint> GetDecalsInRange(GridId gridId, Vector2 position, float distance = 0.75f, Func<Decal, bool>? validDelegate = null)
{
var uids = new HashSet<uint>();
var chunkCollection = ChunkCollection(gridId);
var chunkIndices = GetChunkIndices(position);
if (!chunkCollection.TryGetValue(chunkIndices, out var chunk))
return uids;
foreach (var (uid, decal) in chunk)
{
if ((position - decal.Coordinates-new Vector2(0.5f, 0.5f)).Length > distance)
continue;
if (validDelegate == null || validDelegate(decal))
{
uids.Add(uid);
}
}
return uids;
}
public bool SetDecalPosition(GridId gridId, uint uid, EntityCoordinates coordinates)
{
return SetDecalPosition(gridId, uid, coordinates.GetGridId(EntityManager), coordinates.Position);
}
public bool SetDecalPosition(GridId gridId, uint uid, GridId newGridId, Vector2 position)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
DirtyChunk(gridId, indices);
var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid];
if (newGridId == gridId)
{
chunkCollection[indices][uid] = decal.WithCoordinates(position);
return true;
}
RemoveDecalInternal(gridId, uid);
var newChunkCollection = ChunkCollection(newGridId);
var chunkIndices = GetChunkIndices(position);
if(!newChunkCollection.ContainsKey(chunkIndices))
newChunkCollection[chunkIndices] = new();
newChunkCollection[chunkIndices][uid] = decal.WithCoordinates(position);
ChunkIndex[newGridId][uid] = chunkIndices;
DirtyChunk(newGridId, chunkIndices);
return true;
}
public bool SetDecalColor(GridId gridId, uint uid, Color? color)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid];
chunkCollection[indices][uid] = decal.WithColor(color);
DirtyChunk(gridId, indices);
return true;
}
public bool SetDecalId(GridId gridId, uint uid, string id)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
if (!PrototypeManager.HasIndex<DecalPrototype>(id))
throw new ArgumentOutOfRangeException($"Tried to set decal id to invalid prototypeid: {id}");
var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid];
chunkCollection[indices][uid] = decal.WithId(id);
DirtyChunk(gridId, indices);
return true;
}
public bool SetDecalRotation(GridId gridId, uint uid, Angle angle)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid];
chunkCollection[indices][uid] = decal.WithRotation(angle);
DirtyChunk(gridId, indices);
return true;
}
public bool SetDecalZIndex(GridId gridId, uint uid, int zIndex)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid];
chunkCollection[indices][uid] = decal.WithZIndex(zIndex);
DirtyChunk(gridId, indices);
return true;
}
public bool SetDecalCleanable(GridId gridId, uint uid, bool cleanable)
{
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
var decal = chunkCollection[indices][uid];
chunkCollection[indices][uid] = decal.WithCleanable(cleanable);
DirtyChunk(gridId, indices);
return true;
}
public override void Update(float frameTime)
{
base.Update(frameTime);
foreach (var session in Filter.Broadcast().Recipients)
{
if(session is not IPlayerSession playerSession || playerSession.Status != SessionStatus.InGame)
continue;
var chunks = GetChunksForSession(playerSession);
var updatedChunks = new Dictionary<GridId, HashSet<Vector2i>>();
foreach (var (gridId, gridChunks) in chunks)
{
var newChunks = new HashSet<Vector2i>(gridChunks);
if (_previousSentChunks[playerSession].TryGetValue(gridId, out var previousChunks))
{
newChunks.ExceptWith(previousChunks);
}
if (_dirtyChunks.TryGetValue(gridId, out var dirtyChunks))
{
gridChunks.IntersectWith(dirtyChunks);
newChunks.UnionWith(gridChunks);
}
if (newChunks.Count == 0)
continue;
updatedChunks[gridId] = newChunks;
}
if(updatedChunks.Count == 0)
continue;
_previousSentChunks[playerSession] = chunks;
//send all gridChunks to client
SendChunkUpdates(playerSession, updatedChunks);
}
_dirtyChunks.Clear();
}
private void SendChunkUpdates(IPlayerSession session, Dictionary<GridId, HashSet<Vector2i>> updatedChunks)
{
var updatedDecals = new Dictionary<GridId, Dictionary<Vector2i, Dictionary<uint, Decal>>>();
foreach (var (gridId, chunks) in updatedChunks)
{
var gridChunks = new Dictionary<Vector2i, Dictionary<uint, Decal>>();
foreach (var indices in chunks)
{
gridChunks.Add(indices,
ChunkCollection(gridId).TryGetValue(indices, out var chunk)
? chunk
: new Dictionary<uint, Decal>());
}
updatedDecals[gridId] = gridChunks;
}
RaiseNetworkEvent(new DecalChunkUpdateEvent{Data = updatedDecals}, Filter.SinglePlayer(session));
}
private HashSet<EntityUid> GetSessionViewers(IPlayerSession session)
{
var viewers = new HashSet<EntityUid>();
if (session.Status != SessionStatus.InGame || session.AttachedEntityUid is null)
return viewers;
viewers.Add(session.AttachedEntityUid.Value);
foreach (var uid in session.ViewSubscriptions)
{
viewers.Add(uid);
}
return viewers;
}
private Dictionary<GridId, HashSet<Vector2i>> GetChunksForSession(IPlayerSession session)
{
return GetChunksForViewers(GetSessionViewers(session));
}
}
}