Skip to content

Commit

Permalink
Fixes client overlays not being removed (#6837)
Browse files Browse the repository at this point in the history
* Removes the unused 'RemoveAll' argument and variables in tile update changes code.
Removes RemoveOverlaysOfName() and GetOverlayPosByName(), all overlay removals will now use OverlayType instead.
All remove overlay server functions will now be thread safe.
Separates the update tile message and remove tile message in SpawnSafeThread into two.
Fixes clients not removing certain specific overlays.

* Moves the OverlayType enum  to the TileManagement namespace.
  • Loading branch information
Aranclanos committed Jun 21, 2021
1 parent 1fe5fca commit bbbd2a4
Show file tree
Hide file tree
Showing 30 changed files with 123 additions and 191 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
//using NUnit.Framework.Internal;
using UnityEngine;
using Random = UnityEngine.Random;
using TileManagement;

/// <summary>
/// Changes the tile to a different tile when an item with a particular
Expand Down Expand Up @@ -78,7 +79,7 @@ public override void ServerPerformInteraction(TileApply interaction)
//change tile
interaction.TileChangeManager.UpdateTile(interaction.TargetCellPos, toTile);
//remove overlays
interaction.TileChangeManager.RemoveFloorWallOverlaysOfType(interaction.TargetCellPos, TileChangeManager.OverlayType.Cleanable);
interaction.TileChangeManager.RemoveFloorWallOverlaysOfType(interaction.TargetCellPos, OverlayType.Cleanable);
if (objectsToSpawn != null)
{
objectsToSpawn.SpawnAt(SpawnDestination.At(interaction.WorldPositionTarget));
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using TileManagement;

/// <summary>
/// Deconstruct the tile and spawn its deconstruction object (if defined) and (optionally) additional objects when an item with a particular
Expand Down Expand Up @@ -58,7 +59,7 @@ public override void ServerPerformInteraction(TileApply interaction)
{
interaction.TileChangeManager.RemoveTile(interaction.TargetCellPos, interaction.BasicTile.LayerType);
interaction.TileChangeManager.RemoveFloorWallOverlaysOfType(interaction.TargetCellPos, TileChangeManager.OverlayType.Cleanable);
interaction.TileChangeManager.RemoveFloorWallOverlaysOfType(interaction.TargetCellPos, OverlayType.Cleanable);
//spawn things that need to be spawned
if (interaction.BasicTile.SpawnOnDeconstruct != null &&
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using UnityEngine;
using Random = UnityEngine.Random;
using TileManagement;

namespace Objects.Tables
{
Expand Down Expand Up @@ -61,7 +62,7 @@ public override void ServerPerformInteraction(TileApply interaction)
{
interaction.TileChangeManager.RemoveTile(interaction.TargetCellPos, interaction.BasicTile.LayerType);
interaction.TileChangeManager.RemoveFloorWallOverlaysOfType(interaction.TargetCellPos, TileChangeManager.OverlayType.Cleanable);
interaction.TileChangeManager.RemoveFloorWallOverlaysOfType(interaction.TargetCellPos, OverlayType.Cleanable);
//spawn things that need to be spawned
if (interaction.BasicTile.SpawnOnDeconstruct != null &&
Expand Down
19 changes: 14 additions & 5 deletions UnityProject/Assets/Scripts/Core/Lifecycle/SpawnSafeThread.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ public static class SpawnSafeThread
private static ConcurrentQueue<Tuple<uint, Vector3Int, TileType, string, Matrix4x4, Color, LayerType>> tilesToUpdate
= new ConcurrentQueue<Tuple<uint, Vector3Int, TileType, string, Matrix4x4, Color, LayerType>>();

private static ConcurrentQueue<Tuple<uint, Vector3Int, LayerType>> tilesToRemove
= new ConcurrentQueue<Tuple<uint, Vector3Int, LayerType>>();

public static void Process()
{
if (!prefabsToSpawn.IsEmpty)
Expand Down Expand Up @@ -45,6 +48,15 @@ public static void Process()
tuple.Item7);
}
}

if (!tilesToRemove.IsEmpty)
{
Tuple<uint, Vector3Int, LayerType> tuple;
while (tilesToRemove.TryDequeue(out tuple))
{
RemoveTileMessage.Send(tuple.Item1, tuple.Item2, tuple.Item3);
}
}
}

public static void SpawnPrefab(Vector3 tilePos, GameObject prefabObject, Transform parentTransform = null,
Expand All @@ -62,12 +74,9 @@ public static void Process()
(matrixSyncNetID, position, tileType, tileName, transformMatrix, colour, LayerType.None));
}

public static void RemoveTileMessageSend(uint matrixSyncNetID, Vector3Int cellPosition, LayerType layerType,
TileType tileType = TileType.Effects)
public static void RemoveTileMessageSend(uint matrixSyncNetID, Vector3Int cellPosition, LayerType layerType)
{
tilesToUpdate.Enqueue(
new Tuple<uint, Vector3Int, TileType, string, Matrix4x4, Color, LayerType>
(matrixSyncNetID, cellPosition, tileType, "", Matrix4x4.identity, Color.white, layerType));
tilesToRemove.Enqueue(new Tuple<uint, Vector3Int, LayerType>(matrixSyncNetID, cellPosition, layerType));
}
}

Expand Down
5 changes: 3 additions & 2 deletions UnityProject/Assets/Scripts/Items/Tool/CrayonSprayCan.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using UnityEngine;
using UnityEngine.Serialization;
using Random = UnityEngine.Random;
using TileManagement;

namespace Items.Tool
{
Expand Down Expand Up @@ -195,7 +196,7 @@ private void AddOverlay(PositionalHandApply interaction, Vector3Int cellPos, boo
}

var graffitiAlreadyOnTile = registerItem.TileChangeManager
.GetAllOverlayTiles(cellPos, isWall ? LayerType.Walls : LayerType.Floors, TileChangeManager.OverlayType.Cleanable)
.GetAllOverlayTiles(cellPos, isWall ? LayerType.Walls : LayerType.Floors, OverlayType.Cleanable)
.Where(t => t.IsGraffiti).ToList();

foreach (var graffiti in graffitiAlreadyOnTile)
Expand All @@ -213,7 +214,7 @@ private void AddOverlay(PositionalHandApply interaction, Vector3Int cellPos, boo
{
if (charges > 0 || charges == -1)
{
registerItem.TileChangeManager.RemoveOverlaysOfName(cellPos, isWall ? LayerType.Walls : LayerType.Floors, graffiti.OverlayName);
registerItem.TileChangeManager.RemoveOverlaysOfType(cellPos, isWall ? LayerType.Walls : LayerType.Floors, OverlayType.Cleanable);
registerItem.TileChangeManager.AddOverlay(cellPos, tileToUse, chosenDirection, chosenColour);
}
Expand Down
3 changes: 2 additions & 1 deletion UnityProject/Assets/Scripts/Items/Tool/Pickaxe.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using UnityEngine;
using AddressableReferences;
using Objects.Mining;
using TileManagement;

namespace Items
{
Expand Down Expand Up @@ -49,7 +50,7 @@ void FinishMine()
var tile = interactableTiles.LayerTileAt(interaction.WorldPositionTarget) as BasicTile;
Spawn.ServerPrefab(tile.SpawnOnDeconstruct, interaction.WorldPositionTarget, count: tile.SpawnAmountOnDeconstruct);
interactableTiles.TileChangeManager.RemoveTile(cellPos, LayerType.Walls);
interactableTiles.TileChangeManager.RemoveOverlaysOfType(cellPos, LayerType.Effects, TileChangeManager.OverlayType.Mining);
interactableTiles.TileChangeManager.RemoveOverlaysOfType(cellPos, LayerType.Effects, OverlayType.Mining);
}
}

Expand Down
14 changes: 5 additions & 9 deletions UnityProject/Assets/Scripts/Messages/Server/RemoveTileMessage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ public struct NetMessage : NetworkMessage
{
public Vector3 Position;
public LayerType LayerType;
public bool RemoveAll;
public uint MatrixSyncNetId;
}

Expand All @@ -20,13 +19,11 @@ public struct delayedData
{
public Vector3 Position;
public LayerType LayerType;
public bool RemoveAll;
public uint MatrixSyncNetId;
public delayedData(Vector3 inPosition, LayerType inLayerType, bool inRemoveAll, uint inMatrixSyncNetId)
public delayedData(Vector3 inPosition, LayerType inLayerType, uint inMatrixSyncNetId)
{
Position = inPosition;
LayerType = inLayerType;
RemoveAll = inRemoveAll;
MatrixSyncNetId = inMatrixSyncNetId;
}
}
Expand All @@ -35,12 +32,12 @@ public override void Process(NetMessage msg)
LoadNetworkObject(msg.MatrixSyncNetId);
if (NetworkObject == null)
{
DelayedStuff.Add(new delayedData(msg.Position, msg.LayerType, msg.RemoveAll, msg.MatrixSyncNetId));
DelayedStuff.Add(new delayedData(msg.Position, msg.LayerType, msg.MatrixSyncNetId));
}
else
{
var tileChangerManager = NetworkObject.transform.parent.GetComponent<TileChangeManager>();
tileChangerManager.InternalRemoveTile(msg.Position, msg.LayerType, msg.RemoveAll);
tileChangerManager.InternalRemoveTile(msg.Position, msg.LayerType);
TryDoNotDoneTiles();
}
}
Expand All @@ -54,20 +51,19 @@ public void TryDoNotDoneTiles()
if (NetworkObject != null)
{
var tileChangerManager = NetworkObject.transform.parent.GetComponent<TileChangeManager>();
tileChangerManager.InternalRemoveTile(DelayedStuff[i].Position, DelayedStuff[i].LayerType, DelayedStuff[i].RemoveAll);
tileChangerManager.InternalRemoveTile(DelayedStuff[i].Position, DelayedStuff[i].LayerType);
DelayedStuff.RemoveAt(i);
i--;
}
}
}

public static NetMessage Send(uint matrixSyncNetId, Vector3 position, LayerType layerType, bool removeAll)
public static NetMessage Send(uint matrixSyncNetId, Vector3 position, LayerType layerType)
{
NetMessage msg = new NetMessage
{
Position = position,
LayerType = layerType,
RemoveAll = removeAll,
MatrixSyncNetId = matrixSyncNetId
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,7 @@ public override void Process(NetMessage msg)
else
{
var tileChangerManager = NetworkObject.transform.parent.GetComponent<TileChangeManager>();
if (msg.TileName == "")
{
tileChangerManager.RemoveTile(msg.Position, msg.LayerType);
}
else
{
tileChangerManager.InternalUpdateTile(msg.Position, msg.TileType, msg.TileName, msg.TransformMatrix, msg.Colour);
}

tileChangerManager.InternalUpdateTile(msg.Position, msg.TileType, msg.TileName, msg.TransformMatrix, msg.Colour);
TryDoNotDoneTiles();
}
}
Expand Down
2 changes: 1 addition & 1 deletion UnityProject/Assets/Scripts/NPC/AI/MobExplore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ protected virtual void PerformTargetAction(Vector3Int checkPos)
break;
case Target.missingFloor:
if (IsEmagged == false) interactableTiles.TileChangeManager.UpdateTile(checkPos, TileType.Floor, "Floor");
else interactableTiles.TileChangeManager.RemoveTile(checkPos, LayerType.Floors, true);
else interactableTiles.TileChangeManager.RemoveTile(checkPos, LayerType.Floors);

break;
case Target.injuredPeople:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Linq;
using HealthV2;
using UnityEngine;
using TileManagement;

namespace Systems.Explosions
{
Expand Down Expand Up @@ -120,7 +121,7 @@ public IEnumerator TimedFireEffect(Vector3Int position, float time, TileChangeMa

tileChangeManager.AddOverlay(position, TileType.Effects, "Fire");
yield return WaitFor.Seconds(time);
tileChangeManager.RemoveOverlaysOfName(position, LayerType.Effects, "Fire");
tileChangeManager.RemoveOverlaysOfType(position, LayerType.Effects, OverlayType.Fire);
}


Expand Down
2 changes: 1 addition & 1 deletion UnityProject/Assets/Scripts/Systems/Spells/Spell.cs
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ IEnumerator DespawnAfterDelay()
IEnumerator DespawnAfterDelay()
{
yield return WaitFor.Seconds(SpellData.SummonLifespan);
matrixInfo.TileChangeManager.RemoveTile(localPos, tileToSummon.LayerType, false);
matrixInfo.TileChangeManager.RemoveTile(localPos, tileToSummon.LayerType);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -559,7 +559,7 @@ public bool TryMine(Vector3 worldPosition)
Spawn.ServerPrefab(getTile.SpawnOnDeconstruct, worldPosition,
count: getTile.SpawnAmountOnDeconstruct);
tileChangeManager.RemoveTile(cellPos, LayerType.Walls);
tileChangeManager.RemoveOverlaysOfType(cellPos, LayerType.Effects, TileChangeManager.OverlayType.Mining);
tileChangeManager.RemoveOverlaysOfType(cellPos, LayerType.Effects, OverlayType.Mining);

return true;
}
Expand All @@ -586,11 +586,7 @@ public void CreateAnimatedTile(Vector3 worldPosition, AnimatedOverlayTile animat

yield return WaitFor.Seconds(animationTime);

RemoveOverlay(cellPos, animatedTile);
tileChangeManager.RemoveOverlaysOfType(cellPos, LayerType.Effects, animatedTile.OverlayType);
}

private void RemoveOverlay(Vector3Int cellPos, AnimatedOverlayTile animatedTile)
{
tileChangeManager.RemoveOverlaysOfName(cellPos, LayerType.Effects, animatedTile.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using HealthV2;
using UnityEngine;
using Objects.Construction;
using TileManagement;

/// <summary>
/// Holds and provides functionality for all the MetaDataTiles for a given matrix.
Expand Down Expand Up @@ -236,7 +237,7 @@ public void Clean(Vector3Int worldPosInt, Vector3Int localPosInt, bool makeSlipp
}

//check for any moppable overlays
matrix.TileChangeManager.RemoveFloorWallOverlaysOfType(localPosInt, TileChangeManager.OverlayType.Cleanable);
matrix.TileChangeManager.RemoveFloorWallOverlaysOfType(localPosInt, OverlayType.Cleanable);

if (MatrixManager.IsSpaceAt(worldPosInt, true) == false && makeSlippery)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -938,66 +938,14 @@ public bool HasTile(Vector3Int position, LayerType layerType)
return null;
}

/// <summary>
/// Get position of an overlay by name
/// </summary>
/// <param name="position"></param>
/// <param name="layerType"></param>
/// <param name="overlayName"></param>
/// <returns></returns>
public Vector3Int? GetOverlayPos(Vector3Int position, LayerType layerType, string overlayName)
{
if (layerType == LayerType.Objects)
{
Logger.LogError("Please use get objects instead of get tile");
return null;
}

TileLocation tileLocation = null;
OverlayTile overlayTile = null;
position.z = 1;

if (Layers.TryGetValue(layerType, out var layer))
{
//Go through overlays under the overlay limit. The first overlay checked will be at z = 1.
var count = 0;
while (count < OVERLAY_LIMIT)
{
lock (PresentTiles)
{
PresentTiles[layer].TryGetValue(position, out tileLocation);
}

if (tileLocation != null)
{
overlayTile = tileLocation.Tile as OverlayTile;

if (overlayTile != null && overlayTile.OverlayName == overlayName)
{
return position;
}
}

position.z++;
count++;
}
}
else
{
LogMissingLayer(position, layerType);
}

return null;
}

/// <summary>
/// Gets all positions with a specific overlay type
/// </summary>
/// <param name="position"></param>
/// <param name="layerType"></param>
/// <param name="overlayName"></param>
/// <returns></returns>
public List<Vector3Int> GetOverlayPosByType(Vector3Int position, LayerType layerType, TileChangeManager.OverlayType overlayType)
public List<Vector3Int> GetOverlayPosByType(Vector3Int position, LayerType layerType, OverlayType overlayType)
{
if (layerType == LayerType.Objects)
{
Expand Down Expand Up @@ -1103,7 +1051,7 @@ public List<Vector3Int> GetAllOverlayPos(Vector3Int position, LayerType layerTyp
/// <param name="layerType"></param>
/// <param name="overlayType"></param>
/// <returns></returns>
public List<OverlayTile> GetOverlayTilesByType(Vector3Int position, LayerType layerType, TileChangeManager.OverlayType overlayType)
public List<OverlayTile> GetOverlayTilesByType(Vector3Int position, LayerType layerType, OverlayType overlayType)
{
if (layerType == LayerType.Objects)
{
Expand Down Expand Up @@ -1653,4 +1601,23 @@ public void ClearPreview()

#endregion
}

public enum OverlayType
{
//none is used to say there is no overlay, add new category if you need a new type
None,
Gas,
Damage,
Cleanable,
Fire,
Mining,
KineticAnimation,
Plasma,
NO2,
WaterVapour,
Miasma,
Nitryl,
Tritium,
Freon
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,7 @@ public void RemoveSpecifiedTile(Vector3Int position, LayerTile tile, bool UseSpe
if (TileStore[(Vector2Int) position].Contains(tile))
{
int index = TileStore[(Vector2Int) position].IndexOf(tile);
matrix.TileChangeManager.RemoveTile(new Vector3Int(position.x, position.y, (-index) + 1),
LayerType.Underfloor,
false);
matrix.TileChangeManager.RemoveTile(new Vector3Int(position.x, position.y, (-index) + 1), LayerType.Underfloor);
TileStore[(Vector2Int) position][index] = null;
}
}
Expand Down
Loading

0 comments on commit bbbd2a4

Please sign in to comment.