Skip to content

Commit

Permalink
Merge branch 'upstream-merge' into 'arumoon-server'
Browse files Browse the repository at this point in the history
Upstream fix + minor TG Maps update

Closes #10

See merge request Workbench-Team/space-station-14!122
  • Loading branch information
MilenVolf committed Aug 13, 2023
2 parents b2777af + 716cde6 commit 06fa62a
Show file tree
Hide file tree
Showing 28 changed files with 266 additions and 156 deletions.
2 changes: 1 addition & 1 deletion Content.Client/CombatMode/CombatModeIndicatorsOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected override bool BeforeDraw(in OverlayDrawArgs args)
protected override void Draw(in OverlayDrawArgs args)
{
var mouseScreenPosition = _inputManager.MouseScreenPosition;
var mousePosMap = _eye.ScreenToMap(mouseScreenPosition);
var mousePosMap = _eye.PixelToMap(mouseScreenPosition);
if (mousePosMap.MapId != args.MapId)
return;

Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Decals/Overlays/DecalPlacementOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ protected override void Draw(in OverlayDrawArgs args)
return;

var mouseScreenPos = _inputManager.MouseScreenPosition;
var mousePos = _eyeManager.ScreenToMap(mouseScreenPos);
var mousePos = _eyeManager.PixelToMap(mouseScreenPos);

if (mousePos.MapId != args.MapId)
return;
Expand Down
6 changes: 3 additions & 3 deletions Content.Client/DragDrop/DragDropSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ private void StartDrag()
if (TryComp<SpriteComponent>(_draggedEntity, out var draggedSprite))
{
// pop up drag shadow under mouse
var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition);
var mousePos = _eyeManager.PixelToMap(_inputManager.MouseScreenPosition);
_dragShadow = EntityManager.SpawnEntity("dragshadow", mousePos);
var dragSprite = Comp<SpriteComponent>(_dragShadow.Value);
dragSprite.CopyFrom(draggedSprite);
Expand Down Expand Up @@ -405,7 +405,7 @@ private void HighlightTargets()

// find possible targets on screen even if not reachable
// TODO: Duplicated in SpriteSystem and TargetOutlineSystem. Should probably be cached somewhere for a frame?
var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition);
var mousePos = _eyeManager.PixelToMap(_inputManager.MouseScreenPosition);
var expansion = new Vector2(1.5f, 1.5f);

var bounds = new Box2(mousePos.Position - expansion, mousePos.Position + expansion);
Expand Down Expand Up @@ -533,7 +533,7 @@ public override void FrameUpdate(float frameTime)
// Update position every frame to make it smooth.
if (Exists(_dragShadow))
{
var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition);
var mousePos = _eyeManager.PixelToMap(_inputManager.MouseScreenPosition);
Transform(_dragShadow.Value).WorldPosition = mousePos.Position;
}
}
Expand Down
4 changes: 2 additions & 2 deletions Content.Client/Gameplay/GameplayStateBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public class GameplayStateBase : State, IEntityEventSubscriber

EntityUid? uid = null;
if (UserInterfaceManager.CurrentlyHovered is IViewportControl vp && _inputManager.MouseScreenPosition.IsValid)
uid = GetClickedEntity(vp.ScreenToMap(_inputManager.MouseScreenPosition.Position));
uid = GetClickedEntity(vp.PixelToMap(_inputManager.MouseScreenPosition.Position));
else if (UserInterfaceManager.CurrentlyHovered is EntityMenuElement element)
uid = element.Entity;

Expand Down Expand Up @@ -166,7 +166,7 @@ protected virtual void OnKeyBindStateChanged(ViewportBoundKeyEventArgs args)
EntityUid? entityToClick = null;
if (args.Viewport is IViewportControl vp)
{
var mousePosWorld = vp.ScreenToMap(kArgs.PointerLocation.Position);
var mousePosWorld = vp.PixelToMap(kArgs.PointerLocation.Position);
entityToClick = GetClickedEntity(mousePosWorld);

coordinates = _mapManager.TryFindGridAt(mousePosWorld, out _, out var grid) ?
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Maps/GridDraggingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public override void Update(float frameTime)
}

var mouseScreenPos = _inputManager.MouseScreenPosition;
var mousePos = _eyeManager.ScreenToMap(mouseScreenPos);
var mousePos = _eyeManager.PixelToMap(mouseScreenPos);

if (_dragging == null)
{
Expand Down
4 changes: 2 additions & 2 deletions Content.Client/NPC/PathfindingSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ protected override void Draw(in OverlayDrawArgs args)
private void DrawScreen(OverlayDrawArgs args, DrawingHandleScreen screenHandle)
{
var mousePos = _inputManager.MouseScreenPosition;
var mouseWorldPos = _eyeManager.ScreenToMap(mousePos);
var mouseWorldPos = _eyeManager.PixelToMap(mousePos);
var aabb = new Box2(mouseWorldPos.Position - SharedPathfindingSystem.ChunkSizeVec, mouseWorldPos.Position + SharedPathfindingSystem.ChunkSizeVec);
var xformQuery = _entManager.GetEntityQuery<TransformComponent>();

Expand Down Expand Up @@ -324,7 +324,7 @@ private void DrawScreen(OverlayDrawArgs args, DrawingHandleScreen screenHandle)
private void DrawWorld(OverlayDrawArgs args, DrawingHandleWorld worldHandle)
{
var mousePos = _inputManager.MouseScreenPosition;
var mouseWorldPos = _eyeManager.ScreenToMap(mousePos);
var mouseWorldPos = _eyeManager.PixelToMap(mousePos);
var aabb = new Box2(mouseWorldPos.Position - Vector2.One / 4f, mouseWorldPos.Position + Vector2.One / 4f);
var xformQuery = _entManager.GetEntityQuery<TransformComponent>();

Expand Down
2 changes: 1 addition & 1 deletion Content.Client/NodeContainer/NodeVisualizationOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ private void DrawScreen(in OverlayDrawArgs args)
var mousePos = _inputManager.MouseScreenPosition.Position;
_mouseWorldPos = args
.ViewportControl!
.ScreenToMap(new Vector2(mousePos.X, mousePos.Y))
.PixelToMap(mousePos)
.Position;

if (_hovered == null)
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Outline/InteractionOutlineSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public override void FrameUpdate(float frameTime)
if (_uiManager.CurrentlyHovered is IViewportControl vp
&& _inputManager.MouseScreenPosition.IsValid)
{
var mousePosWorld = vp.ScreenToMap(_inputManager.MouseScreenPosition.Position);
var mousePosWorld = vp.PixelToMap(_inputManager.MouseScreenPosition.Position);
entityToClick = screen.GetClickedEntity(mousePosWorld);

if (vp is ScalingViewport svp)
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Outline/TargetOutlineSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private void HighlightTargets()

// find possible targets on screen
// TODO: Duplicated in SpriteSystem and DragDropSystem. Should probably be cached somewhere for a frame?
var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition).Position;
var mousePos = _eyeManager.PixelToMap(_inputManager.MouseScreenPosition).Position;
var bounds = new Box2(mousePos - LookupVector, mousePos + LookupVector);
var pvsEntities = _lookup.GetEntitiesIntersecting(_eyeManager.CurrentMap, bounds, LookupFlags.Approximate | LookupFlags.Static);
var spriteQuery = GetEntityQuery<SpriteComponent>();
Expand Down
63 changes: 57 additions & 6 deletions Content.Client/Singularity/SingularityOverlay.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
using System.Numerics;
using Content.Shared.Singularity.Components;
using Robust.Client.Graphics;
using Robust.Client.UserInterface.CustomControls;
using Robust.Shared.Enums;
using Robust.Shared.Prototypes;
using System.Numerics;

namespace Content.Client.Singularity
{
public sealed class SingularityOverlay : Overlay
public sealed class SingularityOverlay : Overlay, IEntityEventSubscriber
{
[Dependency] private readonly IEntityManager _entMan = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
private SharedTransformSystem? _xformSystem = null;

/// <summary>
/// Maximum number of distortions that can be shown on screen at a time.
Expand All @@ -29,6 +31,8 @@ public SingularityOverlay()
IoCManager.InjectDependencies(this);
_shader = _prototypeManager.Index<ShaderPrototype>("Singularity").Instance().Duplicate();
_shader.SetParameter("maxDistance", MaxDistance * EyeManager.PixelsPerMeter);
_entMan.EventBus.SubscribeEvent<PixelToMapEvent>(EventSource.Local, this, OnProjectFromScreenToMap);
ZIndex = 101; // Should be drawn after the placement overlay so admins placing items near the singularity can tell where they're going.
}

private readonly Vector2[] _positions = new Vector2[MaxCount];
Expand All @@ -40,14 +44,17 @@ protected override bool BeforeDraw(in OverlayDrawArgs args)
{
if (args.Viewport.Eye == null)
return false;
if (_xformSystem is null && !_entMan.TrySystem(out _xformSystem))
return false;

_count = 0;
foreach (var (distortion, xform) in _entMan.EntityQuery<SingularityDistortionComponent, TransformComponent>())
var query = _entMan.EntityQueryEnumerator<SingularityDistortionComponent, TransformComponent>();
while (query.MoveNext(out var uid, out var distortion, out var xform))
{
if (xform.MapID != args.MapId)
continue;

var mapPos = xform.WorldPosition;
var mapPos = _xformSystem.GetWorldPosition(uid);

// is the distortion in range?
if ((mapPos - args.WorldAABB.ClosestPoint(mapPos)).LengthSquared() > MaxDistance * MaxDistance)
Expand All @@ -56,7 +63,7 @@ protected override bool BeforeDraw(in OverlayDrawArgs args)
// To be clear, this needs to use "inside-viewport" pixels.
// In other words, specifically NOT IViewportControl.WorldToScreen (which uses outer coordinates).
var tempCoords = args.Viewport.WorldToLocal(mapPos);
tempCoords.Y = args.Viewport.Size.Y - tempCoords.Y;
tempCoords.Y = args.Viewport.Size.Y - tempCoords.Y; // Local space to fragment space.

_positions[_count] = tempCoords;
_intensities[_count] = distortion.Intensity;
Expand Down Expand Up @@ -87,6 +94,50 @@ protected override void Draw(in OverlayDrawArgs args)
worldHandle.DrawRect(args.WorldAABB, Color.White);
worldHandle.UseShader(null);
}

/// <summary>
/// Repeats the transformation applied by the shader in <see cref="Resources/Textures/Shaders/singularity.swsl"/>
/// </summary>
private void OnProjectFromScreenToMap(ref PixelToMapEvent args)
{ // Mostly copypasta from the singularity shader.
var maxDistance = MaxDistance * EyeManager.PixelsPerMeter;
var finalCoords = args.VisiblePosition;

for (var i = 0; i < MaxCount && i < _count; i++)
{
// An explanation of pain:
// The shader used by the singularity to create the neat distortion effect occurs in _fragment space_
// All of these calculations are done in _local space_.
// The only difference between the two is that in fragment space 'Y' is measured in pixels from the bottom of the viewport...
// and in local space 'Y' is measured in pixels from the top of the viewport.
// As a minor optimization the locations of the singularities are transformed into fragment space in BeforeDraw so the shader doesn't need to.
// We need to undo that here or this will transform the cursor position as if the singularities were mirrored vertically relative to the center of the viewport.
var localPosition = _positions[i];
localPosition.Y = args.Viewport.Size.Y - localPosition.Y;
var delta = args.VisiblePosition - localPosition;
var distance = (delta / args.Viewport.RenderScale).Length();

var deformation = _intensities[i] / MathF.Pow(distance, _falloffPowers[i]);

// ensure deformation goes to zero at max distance
// avoids long-range single-pixel shifts that are noticeable when leaving PVS.

if (distance >= maxDistance)
deformation = 0.0f;
else
deformation *= 1.0f - MathF.Pow(distance / maxDistance, 4.0f);

if (deformation > 0.8)
deformation = MathF.Pow(deformation, 0.3f);

finalCoords -= delta * deformation;
}

finalCoords.X -= MathF.Floor(finalCoords.X / (args.Viewport.Size.X * 2)) * args.Viewport.Size.X * 2; // Manually handle the wrapping reflection behaviour used by the viewport texture.
finalCoords.Y -= MathF.Floor(finalCoords.Y / (args.Viewport.Size.Y * 2)) * args.Viewport.Size.Y * 2;
finalCoords.X = (finalCoords.X >= args.Viewport.Size.X) ? ((args.Viewport.Size.X * 2) - finalCoords.X) : finalCoords.X;
finalCoords.Y = (finalCoords.Y >= args.Viewport.Size.Y) ? ((args.Viewport.Size.Y * 2) - finalCoords.Y) : finalCoords.Y;
args.VisiblePosition = finalCoords;
}
}
}

2 changes: 1 addition & 1 deletion Content.Client/Tabletop/TabletopSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public override void FrameUpdate(float frameTime)
}

// Map mouse position to EntityCoordinates
var coords = _viewport.ScreenToMap(_inputManager.MouseScreenPosition.Position);
var coords = _viewport.PixelToMap(_inputManager.MouseScreenPosition.Position);

// Clamp coordinates to viewport
var clampedCoords = ClampPositionToViewport(coords, _viewport);
Expand Down
21 changes: 20 additions & 1 deletion Content.Client/Viewport/ScalingViewport.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ namespace Content.Client.Viewport
public sealed class ScalingViewport : Control, IViewportControl
{
[Dependency] private readonly IClyde _clyde = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IInputManager _inputManager = default!;

// Internal viewport creation is deferred.
Expand Down Expand Up @@ -252,8 +253,26 @@ public MapCoordinates ScreenToMap(Vector2 coords)
EnsureViewportCreated();

var matrix = Matrix3.Invert(GetLocalToScreenMatrix());
coords = matrix.Transform(coords);

return _viewport!.LocalToWorld(matrix.Transform(coords));
return _viewport!.LocalToWorld(coords);
}

/// <inheritdoc/>
public MapCoordinates PixelToMap(Vector2 coords)
{
if (_eye == null)
return default;

EnsureViewportCreated();

var matrix = Matrix3.Invert(GetLocalToScreenMatrix());
coords = matrix.Transform(coords);

var ev = new PixelToMapEvent(coords, this, _viewport!);
_entityManager.EventBus.RaiseEvent(EventSource.Local, ref ev);

return _viewport!.LocalToWorld(ev.VisiblePosition);
}

public Vector2 WorldToScreen(Vector2 map)
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Weapons/Melee/MeleeArcOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected override void Draw(in OverlayDrawArgs args)
return;

var mousePos = _inputManager.MouseScreenPosition;
var mapPos = _eyeManager.ScreenToMap(mousePos);
var mapPos = _eyeManager.PixelToMap(mousePos);

if (mapPos.MapId != args.MapId)
return;
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Weapons/Melee/MeleeWeaponSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ public override void Update(float frameTime)
return;
}

var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition);
var mousePos = _eyeManager.PixelToMap(_inputManager.MouseScreenPosition);

if (mousePos.MapId == MapId.Nullspace)
{
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Weapons/Misc/TetherGunSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public override void Update(float frameTime)
}

var mousePos = _input.MouseScreenPosition;
var mouseWorldPos = _eyeManager.ScreenToMap(mousePos);
var mouseWorldPos = _eyeManager.PixelToMap(mousePos);

if (mouseWorldPos.MapId == MapId.Nullspace)
return;
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Weapons/Ranged/GunSpreadOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ protected override void Draw(in OverlayDrawArgs args)
return;

var mouseScreenPos = _input.MouseScreenPosition;
var mousePos = _eye.ScreenToMap(mouseScreenPos);
var mousePos = _eye.PixelToMap(mouseScreenPos);

if (mapPos.MapId != mousePos.MapId)
return;
Expand Down
2 changes: 1 addition & 1 deletion Content.Client/Weapons/Ranged/Systems/GunSystem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ public override void Update(float frameTime)
if (gun.NextFire > Timing.CurTime)
return;

var mousePos = _eyeManager.ScreenToMap(_inputManager.MouseScreenPosition);
var mousePos = _eyeManager.PixelToMap(_inputManager.MouseScreenPosition);

if (mousePos.MapId == MapId.Nullspace)
{
Expand Down
43 changes: 22 additions & 21 deletions Content.Server/Objectives/Conditions/KillRandomPersonCondition.cs
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
using System.Linq;
using Content.Server.Mind.Components;
using Content.Server.Objectives.Interfaces;
using Content.Shared.Humanoid;
using Content.Shared.Mobs.Components;
using JetBrains.Annotations;
using Robust.Shared.Random;

namespace Content.Server.Objectives.Conditions
namespace Content.Server.Objectives.Conditions;

[DataDefinition]
public sealed class KillRandomPersonCondition : KillPersonCondition
{
[UsedImplicitly]
[DataDefinition]
public sealed class KillRandomPersonCondition : KillPersonCondition
public override IObjectiveCondition GetAssigned(Mind.Mind mind)
{
public override IObjectiveCondition GetAssigned(Mind.Mind mind)
var allHumans = new List<Mind.Mind>();
var query = EntityManager.EntityQuery<MindContainerComponent, HumanoidAppearanceComponent>(true);
foreach (var (mc, _) in query)
{
var allHumans = EntityManager.EntityQuery<MindContainerComponent>(true).Where(mc =>
{
var entity = mc.Mind?.OwnedEntity;
if (entity == default)
return false;
var entity = mc.Mind?.OwnedEntity;
if (entity == default)
continue;

return EntityManager.TryGetComponent(entity, out MobStateComponent? mobState) &&
MobStateSystem.IsAlive(entity.Value, mobState) &&
mc.Mind != mind;
}).Select(mc => mc.Mind).ToList();
if (EntityManager.TryGetComponent(entity, out MobStateComponent? mobState) &&
MobStateSystem.IsAlive(entity.Value, mobState) &&
mc.Mind != mind && mc.Mind != null)
{
allHumans.Add(mc.Mind);
}
}

if (allHumans.Count == 0)
return new DieCondition(); // I guess I'll die
if (allHumans.Count == 0)
return new DieCondition(); // I guess I'll die

return new KillRandomPersonCondition {Target = IoCManager.Resolve<IRobustRandom>().Pick(allHumans)};
}
return new KillRandomPersonCondition {Target = IoCManager.Resolve<IRobustRandom>().Pick(allHumans)};
}
}
Loading

0 comments on commit 06fa62a

Please sign in to comment.