mirror of
https://github.com/corvax-team/ss14-wl.git
synced 2026-02-14 19:29:57 +01:00
Signed-off-by: Prole <172158352+Prole0@users.noreply.github.com> Co-authored-by: PJBot <pieterjan.briers+bot@gmail.com> Co-authored-by: ScarKy0 <106310278+ScarKy0@users.noreply.github.com> Co-authored-by: Samuka-C <47865393+Samuka-C@users.noreply.github.com> Co-authored-by: ArtisticRoomba <145879011+ArtisticRoomba@users.noreply.github.com> Co-authored-by: Partmedia <kevinz5000@gmail.com> Co-authored-by: slarticodefast <161409025+slarticodefast@users.noreply.github.com> Co-authored-by: themias <89101928+themias@users.noreply.github.com> Co-authored-by: Victor Shen <71985089+Vexerot@users.noreply.github.com> Co-authored-by: Ed <96445749+TheShuEd@users.noreply.github.com> Co-authored-by: Milon <milonpl.git@proton.me> Co-authored-by: Kirus59 <145689588+Kirus59@users.noreply.github.com> Co-authored-by: Nemanja <98561806+EmoGarbage404@users.noreply.github.com> Co-authored-by: Stomf <5dorkydorks@gmail.com> Co-authored-by: drakewill-CRL <46307022+drakewill-CRL@users.noreply.github.com> Co-authored-by: PraxisMapper <praxismapper@gmail.com> Co-authored-by: EmoGarbage404 <retron404@gmail.com> Co-authored-by: lzk <124214523+lzk228@users.noreply.github.com> Co-authored-by: Princess Cheeseballs <66055347+Pronana@users.noreply.github.com> Co-authored-by: IProduceWidgets <107586145+IProduceWidgets@users.noreply.github.com> Co-authored-by: TytosB <54259736+TytosB@users.noreply.github.com> Co-authored-by: abadaba695 <spacestation13thingy@gmail.com> Co-authored-by: kosticia <kosticia46@gmail.com> Co-authored-by: Thinbug <101073555+Thinbug0@users.noreply.github.com> Co-authored-by: pathetic meowmeow <uhhadd@gmail.com> Co-authored-by: SlamBamActionman <83650252+SlamBamActionman@users.noreply.github.com> Co-authored-by: Boaz1111 <149967078+Boaz1111@users.noreply.github.com> Co-authored-by: ActiveMammmoth <140334666+ActiveMammmoth@users.noreply.github.com> Co-authored-by: Myra <vasilis@pikachu.systems> Co-authored-by: Whatstone <166147148+whatston3@users.noreply.github.com> Co-authored-by: Pieter-Jan Briers <pieterjan.briers+git@gmail.com> Co-authored-by: K-Dynamic <20566341+K-Dynamic@users.noreply.github.com> Co-authored-by: Gentleman-Bird <dcgreen406@gmail.com> Co-authored-by: metalgearsloth <31366439+metalgearsloth@users.noreply.github.com> Co-authored-by: BIGZi0348 <svalker0348@gmail.com> Co-authored-by: LaCumbiaDelCoronavirus <90893484+LaCumbiaDelCoronavirus@users.noreply.github.com> Co-authored-by: imatsoup <93290208+imatsoup@users.noreply.github.com> Co-authored-by: Matthew Herber <32679887+happyrobot33@users.noreply.github.com> Co-authored-by: Ertanic <36124833+Ertanic@users.noreply.github.com> Co-authored-by: MissKay1994 <15877268+MissKay1994@users.noreply.github.com> Co-authored-by: Errant <35878406+Errant-4@users.noreply.github.com> Co-authored-by: eoineoineoin <helloworld@eoinrul.es> Co-authored-by: Tiniest Shark <head.rebel@yahoo.com> Co-authored-by: nikitosych <boriszyn@gmail.com> Co-authored-by: Tayrtahn <tayrtahn@gmail.com> Co-authored-by: Perry Fraser <perryprog@users.noreply.github.com> Co-authored-by: YoungThug <ramialanbagy@gmail.com> Co-authored-by: beck-thompson <107373427+beck-thompson@users.noreply.github.com> Co-authored-by: metalgearsloth <comedian_vs_clown@hotmail.com> Co-authored-by: Southbridge <7013162+southbridge-fur@users.noreply.github.com> Co-authored-by: Vladislav Suchkov <20380250+murolem@users.noreply.github.com> Co-authored-by: Prole <172158352+Prole0@users.noreply.github.com> Co-authored-by: Unkn0wn_Gh0st <shadowstalkermll@gmail.com> Co-authored-by: 3nderall <101940324+3nderall@users.noreply.github.com> Co-authored-by: Radezolid <snappednexus@gmail.com> Co-authored-by: J <billsmith116@gmail.com> Co-authored-by: Ghagliiarghii <68826635+Ghagliiarghii@users.noreply.github.com> Co-authored-by: chromiumboy <50505512+chromiumboy@users.noreply.github.com> Co-authored-by: youtissoum <51883137+youtissoum@users.noreply.github.com> Co-authored-by: Minemoder5000 <minemoder50000@gmail.com> Co-authored-by: Spanky <scott@wearejacob.com> Co-authored-by: Spessmann <156740760+Spessmann@users.noreply.github.com> Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Co-authored-by: brainfood1183 <113240905+brainfood1183@users.noreply.github.com> Co-authored-by: Deerstop <edainturner@gmail.com> Co-authored-by: B_Kirill <153602297+B-Kirill@users.noreply.github.com> Co-authored-by: archee1 <archee3@hotmail.co.uk> Co-authored-by: Cojoke <83733158+Cojoke-dot@users.noreply.github.com> Co-authored-by: Quantum-cross <7065792+Quantum-cross@users.noreply.github.com> Co-authored-by: poklj <compgeek223@gmail.com> Co-authored-by: Krunklehorn <42424291+Krunklehorn@users.noreply.github.com> Co-authored-by: OnyxTheBrave <131422822+OnyxTheBrave@users.noreply.github.com> Co-authored-by: UpAndLeaves <92269094+Alpha-Two@users.noreply.github.com> Co-authored-by: Flareguy <78941145+Flareguy@users.noreply.github.com> Co-authored-by: Zalycon <84675130+Zalycon@users.noreply.github.com> Co-authored-by: deltanedas <39013340+deltanedas@users.noreply.github.com> Co-authored-by: Verm <32827189+Vermidia@users.noreply.github.com> Co-authored-by: nikthechampiongr <32041239+nikthechampiongr@users.noreply.github.com> Co-authored-by: ScarKy0 <scarky0@onet.eu> Co-authored-by: Dmitry <57028746+dimm00n@users.noreply.github.com>
435 lines
16 KiB
C#
435 lines
16 KiB
C#
using System.Diagnostics.CodeAnalysis;
|
|
using System.Linq;
|
|
using Content.Client.Popups;
|
|
using Content.Shared.Construction;
|
|
using Content.Shared.Construction.Prototypes;
|
|
using Content.Shared.Examine;
|
|
using Content.Shared.Input;
|
|
using Content.Shared.Wall;
|
|
using JetBrains.Annotations;
|
|
using Robust.Client.GameObjects;
|
|
using Robust.Client.Player;
|
|
using Robust.Shared.Input;
|
|
using Robust.Shared.Input.Binding;
|
|
using Robust.Shared.Map;
|
|
using Robust.Shared.Player;
|
|
using Robust.Shared.Prototypes;
|
|
using Robust.Shared.Utility;
|
|
|
|
namespace Content.Client.Construction
|
|
{
|
|
/// <summary>
|
|
/// The client-side implementation of the construction system, which is used for constructing entities in game.
|
|
/// </summary>
|
|
[UsedImplicitly]
|
|
public sealed class ConstructionSystem : SharedConstructionSystem
|
|
{
|
|
[Dependency] private readonly IPlayerManager _playerManager = default!;
|
|
[Dependency] private readonly ExamineSystemShared _examineSystem = default!;
|
|
[Dependency] private readonly SharedTransformSystem _transformSystem = default!;
|
|
[Dependency] private readonly PopupSystem _popupSystem = default!;
|
|
|
|
private readonly Dictionary<int, EntityUid> _ghosts = new();
|
|
private readonly Dictionary<string, ConstructionGuide> _guideCache = new();
|
|
|
|
private readonly Dictionary<string, string> _recipesMetadataCache = [];
|
|
|
|
public bool CraftingEnabled { get; private set; }
|
|
|
|
/// <inheritdoc />
|
|
public override void Initialize()
|
|
{
|
|
base.Initialize();
|
|
|
|
WarmupRecipesCache();
|
|
|
|
UpdatesOutsidePrediction = true;
|
|
SubscribeLocalEvent<LocalPlayerAttachedEvent>(HandlePlayerAttached);
|
|
SubscribeNetworkEvent<AckStructureConstructionMessage>(HandleAckStructure);
|
|
SubscribeNetworkEvent<ResponseConstructionGuide>(OnConstructionGuideReceived);
|
|
|
|
CommandBinds.Builder
|
|
.Bind(ContentKeyFunctions.OpenCraftingMenu,
|
|
new PointerInputCmdHandler(HandleOpenCraftingMenu, outsidePrediction: true))
|
|
.Bind(EngineKeyFunctions.Use,
|
|
new PointerInputCmdHandler(HandleUse, outsidePrediction: true))
|
|
.Bind(ContentKeyFunctions.EditorFlipObject,
|
|
new PointerInputCmdHandler(HandleFlip, outsidePrediction: true))
|
|
.Register<ConstructionSystem>();
|
|
|
|
SubscribeLocalEvent<ConstructionGhostComponent, ExaminedEvent>(HandleConstructionGhostExamined);
|
|
SubscribeLocalEvent<ConstructionGhostComponent, ComponentShutdown>(HandleGhostComponentShutdown);
|
|
}
|
|
|
|
private void HandleGhostComponentShutdown(EntityUid uid, ConstructionGhostComponent component, ComponentShutdown args)
|
|
{
|
|
ClearGhost(component.GhostId);
|
|
}
|
|
|
|
public bool TryGetRecipePrototype(string constructionProtoId, [NotNullWhen(true)] out string? targetProtoId)
|
|
{
|
|
if (_recipesMetadataCache.TryGetValue(constructionProtoId, out targetProtoId))
|
|
return true;
|
|
|
|
targetProtoId = null;
|
|
return false;
|
|
}
|
|
|
|
private void WarmupRecipesCache()
|
|
{
|
|
foreach (var constructionProto in PrototypeManager.EnumeratePrototypes<ConstructionPrototype>())
|
|
{
|
|
if (!PrototypeManager.TryIndex(constructionProto.Graph, out var graphProto))
|
|
continue;
|
|
|
|
if (constructionProto.TargetNode is not { } targetNodeId)
|
|
continue;
|
|
|
|
if (!graphProto.Nodes.TryGetValue(targetNodeId, out var targetNode))
|
|
continue;
|
|
|
|
// Recursion is for wimps.
|
|
var stack = new Stack<ConstructionGraphNode>();
|
|
stack.Push(targetNode);
|
|
|
|
do
|
|
{
|
|
var node = stack.Pop();
|
|
|
|
// I never realized if this uid affects anything...
|
|
// EntityUid? userUid = args.SenderSession.State.ControlledEntity.HasValue
|
|
// ? GetEntity(args.SenderSession.State.ControlledEntity.Value)
|
|
// : null;
|
|
|
|
// We try to get the id of the target prototype, if it fails, we try going through the edges.
|
|
if (node.Entity.GetId(null, null, new(EntityManager)) is not { } entityId)
|
|
{
|
|
// If the stack is not empty, there is a high probability that the loop will go to infinity.
|
|
if (stack.Count == 0)
|
|
{
|
|
foreach (var edge in node.Edges)
|
|
{
|
|
if (graphProto.Nodes.TryGetValue(edge.Target, out var graphNode))
|
|
stack.Push(graphNode);
|
|
}
|
|
}
|
|
|
|
continue;
|
|
}
|
|
|
|
// If we got the id of the prototype, we exit the “recursion” by clearing the stack.
|
|
stack.Clear();
|
|
|
|
if (!PrototypeManager.TryIndex(constructionProto.ID, out ConstructionPrototype? recipe))
|
|
continue;
|
|
|
|
if (!PrototypeManager.TryIndex(entityId, out var proto))
|
|
continue;
|
|
|
|
var name = recipe.SetName.HasValue ? Loc.GetString(recipe.SetName) : proto.Name;
|
|
var desc = recipe.SetDescription.HasValue ? Loc.GetString(recipe.SetDescription) : proto.Description;
|
|
|
|
recipe.Name = name;
|
|
recipe.Description = desc;
|
|
|
|
_recipesMetadataCache.Add(constructionProto.ID, entityId);
|
|
} while (stack.Count > 0);
|
|
}
|
|
}
|
|
|
|
private void OnConstructionGuideReceived(ResponseConstructionGuide ev)
|
|
{
|
|
_guideCache[ev.ConstructionId] = ev.Guide;
|
|
ConstructionGuideAvailable?.Invoke(this, ev.ConstructionId);
|
|
}
|
|
|
|
/// <inheritdoc />
|
|
public override void Shutdown()
|
|
{
|
|
base.Shutdown();
|
|
|
|
CommandBinds.Unregister<ConstructionSystem>();
|
|
}
|
|
|
|
public ConstructionGuide? GetGuide(ConstructionPrototype prototype)
|
|
{
|
|
if (_guideCache.TryGetValue(prototype.ID, out var guide))
|
|
return guide;
|
|
|
|
RaiseNetworkEvent(new RequestConstructionGuide(prototype.ID));
|
|
return null;
|
|
}
|
|
|
|
private void HandleConstructionGhostExamined(EntityUid uid, ConstructionGhostComponent component, ExaminedEvent args)
|
|
{
|
|
if (component.Prototype?.Name is null)
|
|
return;
|
|
|
|
using (args.PushGroup(nameof(ConstructionGhostComponent)))
|
|
{
|
|
args.PushMarkup(Loc.GetString(
|
|
"construction-ghost-examine-message",
|
|
("name", component.Prototype.Name)));
|
|
|
|
if (!PrototypeManager.TryIndex(component.Prototype.Graph, out var graph))
|
|
return;
|
|
|
|
var startNode = graph.Nodes[component.Prototype.StartNode];
|
|
|
|
if (!graph.TryPath(component.Prototype.StartNode, component.Prototype.TargetNode, out var path) ||
|
|
!startNode.TryGetEdge(path[0].Name, out var edge))
|
|
{
|
|
return;
|
|
}
|
|
|
|
foreach (var step in edge.Steps)
|
|
{
|
|
step.DoExamine(args);
|
|
}
|
|
}
|
|
}
|
|
|
|
public event EventHandler<CraftingAvailabilityChangedArgs>? CraftingAvailabilityChanged;
|
|
public event EventHandler<string>? ConstructionGuideAvailable;
|
|
public event EventHandler? ToggleCraftingWindow;
|
|
public event EventHandler? FlipConstructionPrototype;
|
|
|
|
private void HandleAckStructure(AckStructureConstructionMessage msg)
|
|
{
|
|
// We get sent a NetEntity but it actually corresponds to our local Entity.
|
|
ClearGhost(msg.GhostId);
|
|
}
|
|
|
|
private void HandlePlayerAttached(LocalPlayerAttachedEvent msg)
|
|
{
|
|
var available = IsCraftingAvailable(msg.Entity);
|
|
UpdateCraftingAvailability(available);
|
|
}
|
|
|
|
private bool HandleOpenCraftingMenu(in PointerInputCmdHandler.PointerInputCmdArgs args)
|
|
{
|
|
if (args.State == BoundKeyState.Down)
|
|
ToggleCraftingWindow?.Invoke(this, EventArgs.Empty);
|
|
return true;
|
|
}
|
|
|
|
private bool HandleFlip(in PointerInputCmdHandler.PointerInputCmdArgs args)
|
|
{
|
|
if (args.State == BoundKeyState.Down)
|
|
FlipConstructionPrototype?.Invoke(this, EventArgs.Empty);
|
|
return true;
|
|
}
|
|
|
|
private void UpdateCraftingAvailability(bool available)
|
|
{
|
|
if (CraftingEnabled == available)
|
|
return;
|
|
|
|
CraftingAvailabilityChanged?.Invoke(this, new CraftingAvailabilityChangedArgs(available));
|
|
CraftingEnabled = available;
|
|
}
|
|
|
|
private static bool IsCraftingAvailable(EntityUid? entity)
|
|
{
|
|
if (entity == default)
|
|
return false;
|
|
|
|
// TODO: Decide if entity can craft, using capabilities or something
|
|
return true;
|
|
}
|
|
|
|
private bool HandleUse(in PointerInputCmdHandler.PointerInputCmdArgs args)
|
|
{
|
|
if (!args.EntityUid.IsValid() || !IsClientSide(args.EntityUid))
|
|
return false;
|
|
|
|
if (!HasComp<ConstructionGhostComponent>(args.EntityUid))
|
|
return false;
|
|
|
|
TryStartConstruction(args.EntityUid);
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Creates a construction ghost at the given location.
|
|
/// </summary>
|
|
public void SpawnGhost(ConstructionPrototype prototype, EntityCoordinates loc, Direction dir)
|
|
=> TrySpawnGhost(prototype, loc, dir, out _);
|
|
|
|
/// <summary>
|
|
/// Creates a construction ghost at the given location.
|
|
/// </summary>
|
|
public bool TrySpawnGhost(
|
|
ConstructionPrototype prototype,
|
|
EntityCoordinates loc,
|
|
Direction dir,
|
|
[NotNullWhen(true)] out EntityUid? ghost)
|
|
{
|
|
ghost = null;
|
|
if (_playerManager.LocalEntity is not { } user ||
|
|
!user.IsValid())
|
|
{
|
|
return false;
|
|
}
|
|
|
|
if (!TryGetRecipePrototype(prototype.ID, out var targetProtoId) || !PrototypeManager.TryIndex(targetProtoId, out EntityPrototype? targetProto))
|
|
return false;
|
|
|
|
if (GhostPresent(loc))
|
|
return false;
|
|
|
|
var predicate = GetPredicate(prototype.CanBuildInImpassable, _transformSystem.ToMapCoordinates(loc));
|
|
if (!_examineSystem.InRangeUnOccluded(user, loc, 20f, predicate: predicate))
|
|
return false;
|
|
|
|
if (!CheckConstructionConditions(prototype, loc, dir, user, showPopup: true))
|
|
return false;
|
|
|
|
ghost = EntityManager.SpawnEntity("constructionghost", loc);
|
|
var comp = EntityManager.GetComponent<ConstructionGhostComponent>(ghost.Value);
|
|
comp.Prototype = prototype;
|
|
comp.GhostId = ghost.GetHashCode();
|
|
EntityManager.GetComponent<TransformComponent>(ghost.Value).LocalRotation = dir.ToAngle();
|
|
_ghosts.Add(comp.GhostId, ghost.Value);
|
|
|
|
var sprite = EntityManager.GetComponent<SpriteComponent>(ghost.Value);
|
|
sprite.Color = new Color(48, 255, 48, 128);
|
|
|
|
if (targetProto.TryGetComponent(out IconComponent? icon, EntityManager.ComponentFactory))
|
|
{
|
|
sprite.AddBlankLayer(0);
|
|
sprite.LayerSetSprite(0, icon.Icon);
|
|
sprite.LayerSetShader(0, "unshaded");
|
|
sprite.LayerSetVisible(0, true);
|
|
}
|
|
else if (targetProto.Components.TryGetValue("Sprite", out _))
|
|
{
|
|
var dummy = EntityManager.SpawnEntity(targetProtoId, MapCoordinates.Nullspace);
|
|
var targetSprite = EntityManager.EnsureComponent<SpriteComponent>(dummy);
|
|
EntityManager.System<AppearanceSystem>().OnChangeData(dummy, targetSprite);
|
|
|
|
for (var i = 0; i < targetSprite.AllLayers.Count(); i++)
|
|
{
|
|
if (!targetSprite[i].Visible || !targetSprite[i].RsiState.IsValid)
|
|
continue;
|
|
|
|
var rsi = targetSprite[i].Rsi ?? targetSprite.BaseRSI;
|
|
if (rsi is null || !rsi.TryGetState(targetSprite[i].RsiState, out var state) ||
|
|
state.StateId.Name is null)
|
|
continue;
|
|
|
|
sprite.AddBlankLayer(i);
|
|
sprite.LayerSetSprite(i, new SpriteSpecifier.Rsi(rsi.Path, state.StateId.Name));
|
|
sprite.LayerSetShader(i, "unshaded");
|
|
sprite.LayerSetVisible(i, true);
|
|
}
|
|
|
|
EntityManager.DeleteEntity(dummy);
|
|
}
|
|
else
|
|
return false;
|
|
|
|
if (prototype.CanBuildInImpassable)
|
|
EnsureComp<WallMountComponent>(ghost.Value).Arc = new(Math.Tau);
|
|
|
|
return true;
|
|
}
|
|
|
|
private bool CheckConstructionConditions(ConstructionPrototype prototype, EntityCoordinates loc, Direction dir,
|
|
EntityUid user, bool showPopup = false)
|
|
{
|
|
foreach (var condition in prototype.Conditions)
|
|
{
|
|
if (!condition.Condition(user, loc, dir))
|
|
{
|
|
if (showPopup)
|
|
{
|
|
var message = condition.GenerateGuideEntry()?.Localization;
|
|
if (message != null)
|
|
{
|
|
// Show the reason to the user:
|
|
_popupSystem.PopupCoordinates(Loc.GetString(message), loc);
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
/// <summary>
|
|
/// Checks if any construction ghosts are present at the given position
|
|
/// </summary>
|
|
private bool GhostPresent(EntityCoordinates loc)
|
|
{
|
|
foreach (var ghost in _ghosts)
|
|
{
|
|
if (EntityManager.GetComponent<TransformComponent>(ghost.Value).Coordinates.Equals(loc))
|
|
return true;
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public void TryStartConstruction(EntityUid ghostId, ConstructionGhostComponent? ghostComp = null)
|
|
{
|
|
if (!Resolve(ghostId, ref ghostComp))
|
|
return;
|
|
|
|
if (ghostComp.Prototype == null)
|
|
{
|
|
throw new ArgumentException($"Can't start construction for a ghost with no prototype. Ghost id: {ghostId}");
|
|
}
|
|
|
|
var transform = EntityManager.GetComponent<TransformComponent>(ghostId);
|
|
var msg = new TryStartStructureConstructionMessage(GetNetCoordinates(transform.Coordinates), ghostComp.Prototype.ID, transform.LocalRotation, ghostId.GetHashCode());
|
|
RaiseNetworkEvent(msg);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Starts constructing an item underneath the attached entity.
|
|
/// </summary>
|
|
public void TryStartItemConstruction(string prototypeName)
|
|
{
|
|
RaiseNetworkEvent(new TryStartItemConstructionMessage(prototypeName));
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes a construction ghost entity with the given ID.
|
|
/// </summary>
|
|
public void ClearGhost(int ghostId)
|
|
{
|
|
if (!_ghosts.TryGetValue(ghostId, out var ghost))
|
|
return;
|
|
|
|
EntityManager.QueueDeleteEntity(ghost);
|
|
_ghosts.Remove(ghostId);
|
|
}
|
|
|
|
/// <summary>
|
|
/// Removes all construction ghosts.
|
|
/// </summary>
|
|
public void ClearAllGhosts()
|
|
{
|
|
foreach (var ghost in _ghosts.Values)
|
|
{
|
|
EntityManager.QueueDeleteEntity(ghost);
|
|
}
|
|
|
|
_ghosts.Clear();
|
|
}
|
|
}
|
|
|
|
public sealed class CraftingAvailabilityChangedArgs : EventArgs
|
|
{
|
|
public bool Available { get; }
|
|
|
|
public CraftingAvailabilityChangedArgs(bool available)
|
|
{
|
|
Available = available;
|
|
}
|
|
}
|
|
}
|