Files
tbd-station-14/Content.Shared/Decals/SharedDecalSystem.cs
mirrorcult 31769edf5f Decal Placer + add new decals for mapping (#6548)
* abomination

* okay its less unabashedly garbage now

* other UI changes

* its britney bitch

* proper greyscale full/half/quarter tiles

* misc cleanup

* rsi

* Add a palette system. It's Kara's problem now.

* oops

* a

* Departmental palette alpha tweaks

* oopy

* so true

* Update Content.Shared/Decals/ColorPalettePrototype.cs

Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>

* fixes for that

* neutral light color and new warning lines

* dirt

* checkerboards

* oop

Co-authored-by: moonheart08 <moonheart08@users.noreply.github.com>
Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com>
2022-02-08 14:54:41 -06:00

182 lines
6.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using Robust.Shared;
using Robust.Shared.Configuration;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Map;
using Robust.Shared.Maths;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization;
namespace Content.Shared.Decals
{
public abstract class SharedDecalSystem : EntitySystem
{
[Dependency] protected readonly IPrototypeManager PrototypeManager = default!;
[Dependency] private readonly IConfigurationManager _configurationManager = default!;
[Dependency] protected readonly IMapManager MapManager = default!;
protected readonly Dictionary<GridId, Dictionary<uint, Vector2i>> ChunkIndex = new();
public const int ChunkSize = 32;
public static Vector2i GetChunkIndices(Vector2 coordinates) => new ((int) Math.Floor(coordinates.X / ChunkSize), (int) Math.Floor(coordinates.Y / ChunkSize));
private float _viewSize;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GridInitializeEvent>(OnGridInitialize);
_configurationManager.OnValueChanged(CVars.NetMaxUpdateRange, OnPvsRangeChanged, true);
}
public override void Shutdown()
{
base.Shutdown();
_configurationManager.UnsubValueChanged(CVars.NetMaxUpdateRange, OnPvsRangeChanged);
}
private void OnPvsRangeChanged(float obj)
{
_viewSize = obj * 2f;
}
private void OnGridInitialize(GridInitializeEvent msg)
{
var comp = EntityManager.EnsureComponent<DecalGridComponent>(MapManager.GetGrid(msg.GridId).GridEntityId);
ChunkIndex[msg.GridId] = new();
foreach (var (indices, decals) in comp.ChunkCollection.ChunkCollection)
{
foreach (var uid in decals.Keys)
{
ChunkIndex[msg.GridId][uid] = indices;
}
}
}
protected DecalGridComponent.DecalGridChunkCollection DecalGridChunkCollection(GridId gridId) => EntityManager
.GetComponent<DecalGridComponent>(MapManager.GetGrid(gridId).GridEntityId).ChunkCollection;
protected Dictionary<Vector2i, Dictionary<uint, Decal>> ChunkCollection(GridId gridId) => DecalGridChunkCollection(gridId).ChunkCollection;
protected virtual void DirtyChunk(GridId id, Vector2i chunkIndices) {}
protected bool RemoveDecalInternal(GridId gridId, uint uid)
{
if (!RemoveDecalHook(gridId, uid)) return false;
if (!ChunkIndex.TryGetValue(gridId, out var values) || !values.TryGetValue(uid, out var indices))
{
return false;
}
var chunkCollection = ChunkCollection(gridId);
if (!chunkCollection.TryGetValue(indices, out var chunk) || !chunk.Remove(uid))
{
return false;
}
if (chunkCollection[indices].Count == 0)
chunkCollection.Remove(indices);
ChunkIndex[gridId]?.Remove(uid);
DirtyChunk(gridId, indices);
return true;
}
protected virtual bool RemoveDecalHook(GridId gridId, uint uid) => true;
private (Box2 view, MapId mapId) CalcViewBounds(in EntityUid euid)
{
var xform = EntityManager.GetComponent<TransformComponent>(euid);
var view = Box2.UnitCentered.Scale(_viewSize).Translated(xform.WorldPosition);
var map = xform.MapID;
return (view, map);
}
protected Dictionary<GridId, HashSet<Vector2i>> GetChunksForViewers(HashSet<EntityUid> viewers)
{
var chunks = new Dictionary<GridId, HashSet<Vector2i>>();
foreach (var viewerUid in viewers)
{
var (bounds, mapId) = CalcViewBounds(viewerUid);
MapManager.FindGridsIntersectingEnumerator(mapId, bounds, out var gridsEnumerator, true);
while(gridsEnumerator.MoveNext(out var grid))
{
if(!chunks.ContainsKey(grid.Index))
chunks[grid.Index] = new();
var enumerator = new ChunkIndicesEnumerator(grid.InvWorldMatrix.TransformBox(bounds), ChunkSize);
while (enumerator.MoveNext(out var indices))
{
chunks[grid.Index].Add(indices.Value);
}
}
}
return chunks;
}
}
internal struct ChunkIndicesEnumerator
{
private Vector2i _chunkLB;
private Vector2i _chunkRT;
private int _xIndex;
private int _yIndex;
internal ChunkIndicesEnumerator(Box2 localAABB, int chunkSize)
{
_chunkLB = new Vector2i((int)Math.Floor(localAABB.Left / chunkSize), (int)Math.Floor(localAABB.Bottom / chunkSize));
_chunkRT = new Vector2i((int)Math.Floor(localAABB.Right / chunkSize), (int)Math.Floor(localAABB.Top / chunkSize));
_xIndex = _chunkLB.X;
_yIndex = _chunkLB.Y;
}
public bool MoveNext([NotNullWhen(true)] out Vector2i? indices)
{
if (_yIndex > _chunkRT.Y)
{
_yIndex = _chunkLB.Y;
_xIndex += 1;
}
indices = new Vector2i(_xIndex, _yIndex);
_yIndex += 1;
return _xIndex <= _chunkRT.X;
}
}
/// <summary>
/// Sent by clients to request that a decal is placed on the server.
/// </summary>
[Serializable, NetSerializable]
public sealed class RequestDecalPlacementEvent : EntityEventArgs
{
public Decal Decal;
public EntityCoordinates Coordinates;
public RequestDecalPlacementEvent(Decal decal, EntityCoordinates coordinates)
{
Decal = decal;
Coordinates = coordinates;
}
}
[Serializable, NetSerializable]
public sealed class RequestDecalRemovalEvent : EntityEventArgs
{
public EntityCoordinates Coordinates;
public RequestDecalRemovalEvent(EntityCoordinates coordinates)
{
Coordinates = coordinates;
}
}
}