Skip to content

Commit

Permalink
Makes the singularity engine actually work stably. (#4068)
Browse files Browse the repository at this point in the history
* Add GNU Octave script for tuning singularity engine. startsingularityengine is now properly tuned & sets up radiation collectors. FTLize RadiationCollectorComponent.

* Fix bugs with radiation collectors producing infinite power.

* Ensure singularities don't instantly annihilate other singularities (causing new singularities to instantly dissolve)

Technically found by a "bug" where a singularity generator would make multiple singularities, but this renders that bug harmless.

* Tune singularity shield emitters to hopefully randomly fail less, and add an Octave script for looking into that

* Fix singularity shader

* Map in an unfinished PA into Saltern

* Correct PA particles being counted twice by singularity calculations, add singulo food component

* Hopefully stop "level 1 singulo stuck in a corner" issues by freezing it when it goes to level 1 from any other level

* Apply suggestions on 'jazz' PR
  • Loading branch information
20kdc authored May 28, 2021
1 parent 3ba0c01 commit a3d9562
Show file tree
Hide file tree
Showing 20 changed files with 422 additions and 151 deletions.
Original file line number Diff line number Diff line change
@@ -1,32 +1,75 @@
using Content.Shared.GameObjects.Components.Singularity;
using Robust.Shared.GameObjects;
using Robust.Shared.ViewVariables;

namespace Content.Client.GameObjects.Components.Singularity
{
[RegisterComponent]
[ComponentReference(typeof(IClientSingularityInstance))]
class ClientSingularityComponent : SharedSingularityComponent, IClientSingularityInstance
{
public int Level
[ViewVariables]
public int Level { get; set; }

//I am lazy
[ViewVariables]
public float Intensity
{
get
{
return _level;
switch (Level)
{
case 0:
return 0.0f;
case 1:
return 2.7f;
case 2:
return 14.4f;
case 3:
return 47.2f;
case 4:
return 180.0f;
case 5:
return 600.0f;
case 6:
return 800.0f;
}
return -1.0f;
}
set
}
[ViewVariables]
public float Falloff
{
get
{
_level = value;
switch (Level)
{
case 0:
return 9999f;
case 1:
return 6.4f;
case 2:
return 7.0f;
case 3:
return 8.0f;
case 4:
return 10.0f;
case 5:
return 12.0f;
case 6:
return 12.0f;
}
return -1.0f;
}
}
private int _level;

public override void HandleComponentState(ComponentState? curState, ComponentState? nextState)
{
if (curState is not SingularityComponentState state)
{
return;
}
_level = state.Level;
Level = state.Level;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ namespace Content.Client.GameObjects.Components.Singularity
{
interface IClientSingularityInstance
{
public int Level { get; set; }
public float Intensity { get; }
public float Falloff { get; }
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Robust.Shared.GameObjects;
using Robust.Shared.ViewVariables;

namespace Content.Client.GameObjects.Components.Singularity
{
Expand All @@ -8,12 +9,9 @@ namespace Content.Client.GameObjects.Components.Singularity
public class ToySingularityComponent : Component, IClientSingularityInstance
{
public override string Name => "ToySingularity";
public int Level {
get {
return 1;
}
set {
}
}
[ViewVariables(VVAccess.ReadWrite)]
public float Falloff { get; set; } = 2.0f;
[ViewVariables(VVAccess.ReadWrite)]
public float Intensity { get; set; } = 0.25f;
}
}
97 changes: 34 additions & 63 deletions Content.Client/Graphics/Overlays/SingularityOverlay.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using Robust.Client.Graphics;
using System.Linq;
using System;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Content.Client.GameObjects.Components.Singularity;
Expand All @@ -17,7 +18,6 @@ public class SingularityOverlay : Overlay
[Dependency] private readonly IComponentManager _componentManager = default!;
[Dependency] private readonly IEntityManager _entityManager = default!;
[Dependency] private readonly IPrototypeManager _prototypeManager = default!;
[Dependency] private readonly IEyeManager _eyeManager = default!;
[Dependency] private readonly IClyde _displayManager = default!;

public override OverlaySpace Space => OverlaySpace.WorldSpace;
Expand All @@ -40,22 +40,28 @@ public override bool OverwriteTargetFrameBuffer()

protected override void Draw(in OverlayDrawArgs args)
{
SingularityQuery();
SingularityQuery(args.Viewport.Eye);

var viewportWB = args.WorldBounds;
// This is a blatant cheat.
// The correct way of doing this would be if the singularity shader performed the matrix transforms.
// I don't need to explain why I'm not doing that.
var resolution = Math.Max(0.125f, Math.Min(args.Viewport.RenderScale.X, args.Viewport.RenderScale.Y));
foreach (SingularityShaderInstance instance in _singularities.Values)
{
var tempCoords = _eyeManager.WorldToScreen(instance.CurrentMapCoords);
tempCoords.Y = _displayManager.ScreenSize.Y - tempCoords.Y;
// 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(instance.CurrentMapCoords);
tempCoords.Y = args.Viewport.Size.Y - tempCoords.Y;
_shader?.SetParameter("positionInput", tempCoords);
if (ScreenTexture != null)
_shader?.SetParameter("SCREEN_TEXTURE", ScreenTexture);
_shader?.SetParameter("intensity", LevelToIntensity(instance.Level));
_shader?.SetParameter("falloff", LevelToFalloff(instance.Level));
_shader?.SetParameter("intensity", instance.Intensity / resolution);
_shader?.SetParameter("falloff", instance.Falloff / resolution);

var worldHandle = args.WorldHandle;
worldHandle.UseShader(_shader);
var viewport = _eyeManager.GetWorldViewport();
worldHandle.DrawRect(viewport, Color.White);
worldHandle.DrawRect(viewportWB, Color.White);
}

}
Expand All @@ -64,19 +70,24 @@ protected override void Draw(in OverlayDrawArgs args)

//Queries all singulos on the map and either adds or removes them from the list of rendered singulos based on whether they should be drawn (in range? on the same z-level/map? singulo entity still exists?)
private float _maxDist = 15.0f;
private void SingularityQuery()
private void SingularityQuery(IEye? currentEye)
{
var currentEyeLoc = _eyeManager.CurrentEye.Position;
var currentMap = _eyeManager.CurrentMap; //TODO: support multiple viewports once it is added
if (currentEye == null)
{
_singularities.Clear();
return;
}
var currentEyeLoc = currentEye.Position;
var currentMap = currentEye.Position.MapId;

var singuloComponents = _componentManager.EntityQuery<IClientSingularityInstance>();
foreach (var singuloInterface in singuloComponents) //Add all singulos that are not added yet but qualify
{
var singuloComponent = (Component)singuloInterface;
var singuloEntity = singuloComponent.Owner;
if (!_singularities.Keys.Contains(singuloEntity.Uid) && singuloEntity.Transform.MapID == currentMap && singuloEntity.Transform.Coordinates.InRange(_entityManager, EntityCoordinates.FromMap(_entityManager, singuloEntity.Transform.ParentUid, currentEyeLoc), _maxDist))
if (!_singularities.Keys.Contains(singuloEntity.Uid) && SinguloQualifies(singuloEntity, currentEyeLoc))
{
_singularities.Add(singuloEntity.Uid, new SingularityShaderInstance(singuloEntity.Transform.MapPosition.Position, singuloInterface.Level));
_singularities.Add(singuloEntity.Uid, new SingularityShaderInstance(singuloEntity.Transform.MapPosition.Position, singuloInterface.Intensity, singuloInterface.Falloff));
}
}

Expand All @@ -85,7 +96,7 @@ private void SingularityQuery()
{
if (_entityManager.TryGetEntity(activeSinguloUid, out IEntity? singuloEntity))
{
if (singuloEntity.Transform.MapID != currentMap || !singuloEntity.Transform.Coordinates.InRange(_entityManager, EntityCoordinates.FromMap(_entityManager, singuloEntity.Transform.ParentUid, currentEyeLoc), _maxDist))
if (!SinguloQualifies(singuloEntity, currentEyeLoc))
{
_singularities.Remove(activeSinguloUid);
}
Expand All @@ -99,7 +110,8 @@ private void SingularityQuery()
{
var shaderInstance = _singularities[activeSinguloUid];
shaderInstance.CurrentMapCoords = singuloEntity.Transform.MapPosition.Position;
shaderInstance.Level = singuloInterface.Level;
shaderInstance.Intensity = singuloInterface.Intensity;
shaderInstance.Falloff = singuloInterface.Falloff;
}
}

Expand All @@ -112,62 +124,21 @@ private void SingularityQuery()

}




//I am lazy
private float LevelToIntensity(int level)
{
switch (level)
{
case 0:
return 0.0f;
case 1:
return 2.7f;
case 2:
return 14.4f;
case 3:
return 47.2f;
case 4:
return 180.0f;
case 5:
return 600.0f;
case 6:
return 800.0f;

}
return -1.0f;
}
private float LevelToFalloff(int level)
private bool SinguloQualifies(IEntity singuloEntity, MapCoordinates currentEyeLoc)
{
switch (level)
{
case 0:
return 9999f;
case 1:
return 6.4f;
case 2:
return 7.0f;
case 3:
return 8.0f;
case 4:
return 10.0f;
case 5:
return 12.0f;
case 6:
return 12.0f;
}
return -1.0f;
return singuloEntity.Transform.MapID == currentEyeLoc.MapId && singuloEntity.Transform.Coordinates.InRange(_entityManager, EntityCoordinates.FromMap(_entityManager, singuloEntity.Transform.ParentUid, currentEyeLoc), _maxDist);
}

private sealed class SingularityShaderInstance
{
public Vector2 CurrentMapCoords;
public int Level;
public SingularityShaderInstance(Vector2 mapCoords, int level)
public float Intensity;
public float Falloff;
public SingularityShaderInstance(Vector2 mapCoords, float intensity, float falloff)
{
CurrentMapCoords = mapCoords;
Level = level;
Intensity = intensity;
Falloff = falloff;
}
}
}
Expand Down
1 change: 1 addition & 0 deletions Content.Client/IgnoredComponents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ public static class IgnoredComponents
"GlassBeaker",
"SliceableFood",
"DamageOtherOnHit",
"SinguloFood",
"DamageOnLand",
"SmokeSolutionAreaEffect",
"FoamSolutionAreaEffect",
Expand Down
7 changes: 6 additions & 1 deletion Content.Server/Commands/StartSingularityEngineCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using Content.Server.Administration;
using Content.Server.GameObjects.Components.Singularity;
using Content.Server.GameObjects.Components.PA;
using Content.Server.GameObjects.Components.Power.PowerNetComponents;
using Content.Shared.Administration;
using Content.Shared.GameObjects.Components;
using Robust.Shared.Console;
Expand Down Expand Up @@ -30,11 +31,15 @@ public void Execute(IConsoleShell shell, string argStr, string[] args)
{
ent.GetComponent<EmitterComponent>().SwitchOn();
}
foreach (var ent in entityManager.GetEntities(new TypeEntityQuery(typeof(RadiationCollectorComponent))))
{
ent.GetComponent<RadiationCollectorComponent>().Collecting = true;
}
foreach (var ent in entityManager.GetEntities(new TypeEntityQuery(typeof(ParticleAcceleratorControlBoxComponent))))
{
var pacb = ent.GetComponent<ParticleAcceleratorControlBoxComponent>();
pacb.RescanParts();
pacb.SetStrength(ParticleAcceleratorPowerState.Level1);
pacb.SetStrength(ParticleAcceleratorPowerState.Level0);
pacb.SwitchOn();
}
shell.WriteLine("Done!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,21 +20,7 @@ public class ParticleProjectileComponent : Component, IStartCollide
private ParticleAcceleratorPowerState _state;
void IStartCollide.CollideWith(Fixture ourFixture, Fixture otherFixture, in Manifold manifold)
{
if (otherFixture.Body.Owner.TryGetComponent<ServerSingularityComponent>(out var singularityComponent))
{
var multiplier = _state switch
{
ParticleAcceleratorPowerState.Standby => 0,
ParticleAcceleratorPowerState.Level0 => 1,
ParticleAcceleratorPowerState.Level1 => 3,
ParticleAcceleratorPowerState.Level2 => 6,
ParticleAcceleratorPowerState.Level3 => 10,
_ => 0
};
singularityComponent.Energy += 10 * multiplier;
Owner.QueueDelete();
}
else if (otherFixture.Body.Owner.TryGetComponent<SingularityGeneratorComponent>(out var singularityGeneratorComponent))
if (otherFixture.Body.Owner.TryGetComponent<SingularityGeneratorComponent>(out var singularityGeneratorComponent))
{
singularityGeneratorComponent.Power += _state switch
{
Expand Down Expand Up @@ -67,6 +53,22 @@ public void Fire(ParticleAcceleratorPowerState state, Angle angle, IEntity firer
}
projectileComponent.IgnoreEntity(firer);

if (!Owner.TryGetComponent<SinguloFoodComponent>(out var singuloFoodComponent))
{
Logger.Error("ParticleProjectile tried firing, but it was spawned without a SinguloFoodComponent");
return;
}
var multiplier = _state switch
{
ParticleAcceleratorPowerState.Standby => 0,
ParticleAcceleratorPowerState.Level0 => 1,
ParticleAcceleratorPowerState.Level1 => 3,
ParticleAcceleratorPowerState.Level2 => 6,
ParticleAcceleratorPowerState.Level3 => 10,
_ => 0
};
singuloFoodComponent.Energy = 10 * multiplier;

var suffix = state switch
{
ParticleAcceleratorPowerState.Level0 => "0",
Expand Down
Loading

0 comments on commit a3d9562

Please sign in to comment.