Skip to content

Commit

Permalink
Merge branch '2024-10-19-LobbyArt' of https://github.com/dvir001/fron…
Browse files Browse the repository at this point in the history
…tier-station-14 into 2024-10-19-LobbyArt
  • Loading branch information
dvir001 committed Oct 18, 2024
2 parents 082f501 + 6758066 commit dd46a8e
Show file tree
Hide file tree
Showing 40 changed files with 948 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Content.Client/Entry/EntryPoint.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
using Robust.Shared.Prototypes;
using Robust.Shared.Replays;
using Robust.Shared.Timing;
using Content.Client._NF.Emp.Overlays; // Frontier

namespace Content.Client.Entry
{
Expand Down Expand Up @@ -154,6 +155,7 @@ public override void PostInit()

_overlayManager.AddOverlay(new SingularityOverlay());
_overlayManager.AddOverlay(new RadiationPulseOverlay());
_overlayManager.AddOverlay(new EmpBlastOverlay()); // Frontier
_chatManager.Initialize();
_clientPreferencesManager.Initialize();
_euiManager.Initialize();
Expand Down
145 changes: 145 additions & 0 deletions Content.Client/_NF/Emp/Overlays/EmpBlastOverlay.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
using System.Numerics;
using Content.Shared._NF.Emp.Components;
using Robust.Client.GameObjects;
using Robust.Client.Graphics;
using Robust.Shared.Enums;
using Robust.Shared.Graphics;
using Robust.Shared.Map;
using Robust.Shared.Prototypes;
using Robust.Shared.Timing;

namespace Content.Client._NF.Emp.Overlays
{
public sealed class EmpBlastOverlay : Overlay
{
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IGameTiming _gameTiming = default!;
private TransformSystem? _transform;

private const float PvsDist = 25.0f;

public override OverlaySpace Space => OverlaySpace.WorldSpace;
public override bool RequestScreenTexture => true;

private readonly ShaderInstance _baseShader;
private readonly Dictionary<EntityUid, (ShaderInstance shd, EmpShaderInstance instance)> _blasts = new();

public EmpBlastOverlay()
{
IoCManager.InjectDependencies(this);
_baseShader = _prototypeManager.Index<ShaderPrototype>("Emp").Instance().Duplicate();
}

protected override bool BeforeDraw(in OverlayDrawArgs args)
{
EmpQuery(args.Viewport.Eye);
return _blasts.Count > 0;
}

protected override void Draw(in OverlayDrawArgs args)
{
if (ScreenTexture == null)
return;

var worldHandle = args.WorldHandle;
var viewport = args.Viewport;

foreach ((var shd, var instance) in _blasts.Values)
{
if (instance.CurrentMapCoords.MapId != args.MapId)
continue;

// To be clear, this needs to use "inside-viewport" pixels.
// In other words, specifically NOT IViewportControl.WorldToScreen (which uses outer coordinates).
var tempCoords = viewport.WorldToLocal(instance.CurrentMapCoords.Position);
tempCoords.Y = viewport.Size.Y - tempCoords.Y;
shd?.SetParameter("renderScale", viewport.RenderScale);
shd?.SetParameter("positionInput", tempCoords);
shd?.SetParameter("range", instance.Range);
var life = (_gameTiming.RealTime - instance.Start).TotalSeconds / instance.Duration;
shd?.SetParameter("life", (float)life);

// There's probably a very good reason not to do this.
// Oh well!
shd?.SetParameter("SCREEN_TEXTURE", viewport.RenderTarget.Texture);

worldHandle.UseShader(shd);
worldHandle.DrawRect(Box2.CenteredAround(instance.CurrentMapCoords.Position, new Vector2(instance.Range, instance.Range) * 2f), Color.White);
}

worldHandle.UseShader(null);
}

//Queries all blasts on the map and either adds or removes them from the list of rendered blasts based on whether they should be drawn (in range? on the same z-level/map? blast entity still exists?)
private void EmpQuery(IEye? currentEye)
{
_transform ??= _entityManager.System<TransformSystem>();

if (currentEye == null)
{
_blasts.Clear();
return;
}

var currentEyeLoc = currentEye.Position;

var blasts = _entityManager.EntityQueryEnumerator<EmpBlastComponent>();
//Add all blasts that are not added yet but qualify
while (blasts.MoveNext(out var blastEntity, out var blast))
{
if (!_blasts.ContainsKey(blastEntity) && BlastQualifies(blastEntity, currentEyeLoc, blast))
{
_blasts.Add(
blastEntity,
(
_baseShader.Duplicate(),
new EmpShaderInstance(
_transform.GetMapCoordinates(blastEntity),
blast.VisualRange,
blast.StartTime,
blast.VisualDuration
)
)
);
}
}

var activeShaderIds = _blasts.Keys;
foreach (var blastEntity in activeShaderIds) //Remove all blasts that are added and no longer qualify
{
if (_entityManager.EntityExists(blastEntity) &&
_entityManager.TryGetComponent(blastEntity, out EmpBlastComponent? blast) &&
BlastQualifies(blastEntity, currentEyeLoc, blast))
{
var shaderInstance = _blasts[blastEntity];
shaderInstance.instance.CurrentMapCoords = _transform.GetMapCoordinates(blastEntity);
shaderInstance.instance.Range = blast.VisualRange;
}
else
{
_blasts[blastEntity].shd.Dispose();
_blasts.Remove(blastEntity);
}
}

}

private bool BlastQualifies(EntityUid blastEntity, MapCoordinates currentEyeLoc, EmpBlastComponent blast)
{
var transformComponent = _entityManager.GetComponent<TransformComponent>(blastEntity);
var transformSystem = _entityManager.System<SharedTransformSystem>();
return transformComponent.MapID == currentEyeLoc.MapId
&& transformSystem.InRange(transformComponent.Coordinates, transformSystem.ToCoordinates(transformComponent.ParentUid, currentEyeLoc), PvsDist + blast.VisualRange);
}

private sealed record EmpShaderInstance(MapCoordinates CurrentMapCoords, float Range, TimeSpan Start, float Duration)
{
public MapCoordinates CurrentMapCoords = CurrentMapCoords;
public float Range = Range;
public TimeSpan Start = Start;
public float Duration = Duration;
};
}
}

18 changes: 16 additions & 2 deletions Content.Server/Emp/EmpSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@
using Content.Shared.Tiles; // Frontier
using Robust.Server.GameObjects;
using Robust.Shared.Map;
using Content.Shared._NF.Emp.Components; // Frontier
using Robust.Server.GameStates; // Frontier: EMP Blast PVS
using Robust.Shared.Configuration; // Frontier: EMP Blast PVS
using Robust.Shared; // Frontier: EMP Blast PVS

namespace Content.Server.Emp;

public sealed class EmpSystem : SharedEmpSystem
{
[Dependency] private readonly EntityLookupSystem _lookup = default!;
[Dependency] private readonly TransformSystem _transform = default!;
[Dependency] private readonly PvsOverrideSystem _pvs = default!; // Frontier: EMP Blast PVS
[Dependency] private readonly IConfigurationManager _cfg = default!; // Frontier: EMP Blast PVS

public const string EmpPulseEffectPrototype = "EffectEmpPulse";
public const string EmpPulseEffectPrototype = "EffectEmpBlast"; // Frontier: EffectEmpPulse

public override void Initialize()
{
Expand Down Expand Up @@ -54,7 +60,15 @@ public void EmpPulse(MapCoordinates coordinates, float range, float energyConsum

TryEmpEffects(uid, energyConsumption, duration);
}
Spawn(EmpPulseEffectPrototype, coordinates);

var empBlast = Spawn(EmpPulseEffectPrototype, coordinates); // Frontier: Added visual effect
EnsureComp<EmpBlastComponent>(empBlast, out var empBlastComp); // Frontier
empBlastComp.VisualRange = range; // Frontier

if (range > _cfg.GetCVar(CVars.NetMaxUpdateRange)) // Frontier
_pvs.AddGlobalOverride(empBlast); // Frontier

Dirty(empBlast, empBlastComp); // Frontier
}

/// <summary>
Expand Down
32 changes: 29 additions & 3 deletions Content.Server/_NF/Bank/BankSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -208,11 +208,11 @@ public bool TryBankDeposit(ICommonSession session, PlayerPreferences prefs, Huma
}

/// <summary>
/// Attempts to add money to a character's bank account. This should always be used instead of attempting to modify the bankaccountcomponent directly
/// Retrieves a character's balance via its in-game entity, if it has one.
/// </summary>
/// <param name="ent">The UID that the bank account is connected to, typically the player controlled mob</param>
/// <param name="balance">The amount of spesos to add into the bank account</param>
/// <returns>true if the transaction was successful, false if it was not</returns>
/// <param name="balance">When successful, contains the account balance in spesos. Otherwise, set to 0.</param>
/// <returns>true if the account was successfully queried.</returns>
public bool TryGetBalance(EntityUid ent, out int balance)
{
if (!_playerManager.TryGetSessionByEntity(ent, out var session) ||
Expand All @@ -234,6 +234,32 @@ public bool TryGetBalance(EntityUid ent, out int balance)
return true;
}

/// <summary>
/// Retrieves a character's balance via a player's session.
/// </summary>
/// <param name="session">The session of the player character to query.</param>
/// <param name="balance">When successful, contains the account balance in spesos. Otherwise, set to 0.</param>
/// <returns>true if the account was successfully queried.</returns>
public bool TryGetBalance(ICommonSession session, out int balance)
{
if (!_prefsManager.TryGetCachedPreferences(session.UserId, out var prefs))
{
_log.Info($"{session.UserId} has no cached prefs");
balance = 0;
return false;
}

if (prefs.SelectedCharacter is not HumanoidCharacterProfile profile)
{
_log.Info($"{session.UserId} has the wrong prefs type");
balance = 0;
return false;
}

balance = profile.BankBalance;
return true;
}

/// <summary>
/// Update the bank balance to the character's current account balance.
/// </summary>
Expand Down
Loading

0 comments on commit dd46a8e

Please sign in to comment.