SharedToolSystem.Tile.cs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. using Content.Shared.Database;
  2. using Content.Shared.Fluids.Components;
  3. using Content.Shared.Interaction;
  4. using Content.Shared.Maps;
  5. using Content.Shared.Physics;
  6. using Content.Shared.Tools.Components;
  7. using Robust.Shared.Map;
  8. using Robust.Shared.Map.Components;
  9. using Robust.Shared.Network;
  10. using Robust.Shared.Utility;
  11. namespace Content.Shared.Tools.Systems;
  12. public abstract partial class SharedToolSystem
  13. {
  14. [Dependency] private readonly INetManager _net = default!;
  15. public void InitializeTile()
  16. {
  17. SubscribeLocalEvent<ToolTileCompatibleComponent, AfterInteractEvent>(OnToolTileAfterInteract);
  18. SubscribeLocalEvent<ToolTileCompatibleComponent, TileToolDoAfterEvent>(OnToolTileComplete);
  19. }
  20. private void OnToolTileAfterInteract(Entity<ToolTileCompatibleComponent> ent, ref AfterInteractEvent args)
  21. {
  22. if (args.Handled || args.Target != null && !HasComp<PuddleComponent>(args.Target))
  23. return;
  24. args.Handled = UseToolOnTile((ent, ent, null), args.User, args.ClickLocation);
  25. }
  26. private void OnToolTileComplete(Entity<ToolTileCompatibleComponent> ent, ref TileToolDoAfterEvent args)
  27. {
  28. var comp = ent.Comp;
  29. if (args.Handled || args.Cancelled)
  30. return;
  31. if (!TryComp<ToolComponent>(ent, out var tool))
  32. return;
  33. var gridUid = GetEntity(args.Grid);
  34. if (!TryComp<MapGridComponent>(gridUid, out var grid))
  35. {
  36. Log.Error("Attempted use tool on a non-existent grid?");
  37. return;
  38. }
  39. var tileRef = _maps.GetTileRef(gridUid, grid, args.GridTile);
  40. var coords = _maps.ToCoordinates(tileRef, grid);
  41. if (comp.RequiresUnobstructed && _turfs.IsTileBlocked(gridUid, tileRef.GridIndices, CollisionGroup.MobMask))
  42. return;
  43. if (!TryDeconstructWithToolQualities(tileRef, tool.Qualities))
  44. return;
  45. AdminLogger.Add(
  46. LogType.LatticeCut,
  47. LogImpact.Medium,
  48. $"{ToPrettyString(args.User):player} used {ToPrettyString(ent)} to edit the tile at {coords}");
  49. args.Handled = true;
  50. }
  51. private bool UseToolOnTile(Entity<ToolTileCompatibleComponent?, ToolComponent?> ent, EntityUid user, EntityCoordinates clickLocation)
  52. {
  53. if (!Resolve(ent, ref ent.Comp1, ref ent.Comp2, false))
  54. return false;
  55. var comp = ent.Comp1!;
  56. var tool = ent.Comp2!;
  57. if (!_mapManager.TryFindGridAt(_transformSystem.ToMapCoordinates(clickLocation), out var gridUid, out var mapGrid))
  58. return false;
  59. var tileRef = _maps.GetTileRef(gridUid, mapGrid, clickLocation);
  60. var tileDef = (ContentTileDefinition) _tileDefManager[tileRef.Tile.TypeId];
  61. if (!tool.Qualities.ContainsAny(tileDef.DeconstructTools))
  62. return false;
  63. if (string.IsNullOrWhiteSpace(tileDef.BaseTurf))
  64. return false;
  65. if (comp.RequiresUnobstructed && _turfs.IsTileBlocked(gridUid, tileRef.GridIndices, CollisionGroup.MobMask))
  66. return false;
  67. var coordinates = _maps.GridTileToLocal(gridUid, mapGrid, tileRef.GridIndices);
  68. if (!InteractionSystem.InRangeUnobstructed(user, coordinates, popup: false))
  69. return false;
  70. var args = new TileToolDoAfterEvent(GetNetEntity(gridUid), tileRef.GridIndices);
  71. UseTool(ent, user, ent, comp.Delay, tool.Qualities, args, out _, toolComponent: tool);
  72. return true;
  73. }
  74. public bool TryDeconstructWithToolQualities(TileRef tileRef, PrototypeFlags<ToolQualityPrototype> withToolQualities)
  75. {
  76. var tileDef = (ContentTileDefinition) _tileDefManager[tileRef.Tile.TypeId];
  77. if (withToolQualities.ContainsAny(tileDef.DeconstructTools))
  78. {
  79. // don't do this on the client or else the tile entity spawn mispredicts and looks horrible
  80. return _net.IsClient || _tiles.DeconstructTile(tileRef);
  81. }
  82. return false;
  83. }
  84. }