Files
tbd-station-14/Content.Server/GameObjects/Components/Singularity/ContainmentFieldGeneratorComponent.cs
metalgearsloth 3e64fd56a1 Physics (#3452)
* Content side new physics structure

* BroadPhase outline done

* But we need to fix WorldAABB

* Fix static pvs AABB

* Fix import

* Rando fixes

* B is for balloon

* Change human mob hitbox to circle

* Decent movement

* Start adding friction to player controller

I think it's the best way to go about it to keep other objects somewhat consistent for physics.

* This baby can fit so many physics bugs in it.

* Slight mob mover optimisations.

* Player mover kinda works okay.

* Beginnings of testbed

* More testbed

* Circlestack bed

* Namespaces

* BB fixes

* Pull WorldAABB

* Joint pulling

* Semi-decent movement I guess.

* Pulling better

* Bullet controller + old movement

* im too dumb for this shit

* Use kinematic mob controller again

It's probably for the best TBH

* Stashed shitcode

* Remove SlipController

* In which movement code is entirely refactored

* Singularity fix

* Fix ApplyLinearImpulse

* MoveRelay fix

* Fix door collisions

* Disable subfloor collisions

Saves on broadphase a fair bit

* Re-implement ClimbController

* Zumzum's pressure

* Laggy item throwing

* Minor atmos change

* Some caching

* Optimise controllers

* Optimise CollideWith to hell and back

* Re-do throwing and tile friction

* Landing too

* Optimise controllers

* Move CCVars and other stuff swept is beautiful

* Cleanup a bunch of controllers

* Fix shooting and high pressure movement controller

* Flashing improvements

* Stuff and things

* Combat collisions

* Combat mode collisions

* Pulling distance joint again

* Cleanup physics interfaces

* More like scuffedularity

* Shit's fucked

* Haha tests go green

* Bigmoneycrab

Co-authored-by: Metal Gear Sloth <metalgearsloth@gmail.com>
2021-03-01 03:11:29 +11:00

211 lines
7.9 KiB
C#

#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using Content.Server.GameObjects.Components.Projectiles;
using Content.Server.Utility;
using Content.Shared.Physics;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Log;
using Robust.Shared.Maths;
using Robust.Shared.Physics;
using Robust.Shared.Physics.Broadphase;
using Robust.Shared.ViewVariables;
using Robust.Server.GameObjects;
namespace Content.Server.GameObjects.Components.Singularity
{
[RegisterComponent]
public class ContainmentFieldGeneratorComponent : Component, ICollideBehavior
{
[Dependency] private readonly IPhysicsManager _physicsManager = null!;
public override string Name => "ContainmentFieldGenerator";
private int _powerBuffer;
[ViewVariables]
public int PowerBuffer
{
get => _powerBuffer;
set => _powerBuffer = Math.Clamp(value, 0, 6);
}
public void ReceivePower(int power)
{
var totalPower = power + PowerBuffer;
var powerPerConnection = totalPower / 2;
var newBuffer = totalPower % 2;
TryPowerConnection(ref _connection1, ref newBuffer, powerPerConnection);
TryPowerConnection(ref _connection2, ref newBuffer, powerPerConnection);
PowerBuffer = newBuffer;
}
private void TryPowerConnection(ref Tuple<Direction, ContainmentFieldConnection>? connectionProperty, ref int powerBuffer, int powerPerConnection)
{
if (connectionProperty != null)
{
connectionProperty.Item2.SharedEnergyPool += powerPerConnection;
}
else
{
if (TryGenerateFieldConnection(ref connectionProperty))
{
connectionProperty.Item2.SharedEnergyPool += powerPerConnection;
}
else
{
powerBuffer += powerPerConnection;
}
}
}
[ComponentDependency] private readonly PhysicsComponent? _collidableComponent = default;
[ComponentDependency] private readonly PointLightComponent? _pointLightComponent = default;
private Tuple<Direction, ContainmentFieldConnection>? _connection1;
private Tuple<Direction, ContainmentFieldConnection>? _connection2;
public bool CanRepell(IEntity toRepell) => _connection1?.Item2?.CanRepell(toRepell) == true ||
_connection2?.Item2?.CanRepell(toRepell) == true;
public override void HandleMessage(ComponentMessage message, IComponent? component)
{
base.HandleMessage(message, component);
switch (message)
{
case AnchoredChangedMessage:
OnAnchoredChanged();
break;
}
}
private void OnAnchoredChanged()
{
if(_collidableComponent?.Anchored == true)
{
Owner.SnapToGrid();
}
else
{
_connection1?.Item2.Dispose();
_connection2?.Item2.Dispose();
}
}
private bool IsConnectedWith(ContainmentFieldGeneratorComponent comp)
{
return comp == this || _connection1?.Item2.Generator1 == comp || _connection1?.Item2.Generator2 == comp ||
_connection2?.Item2.Generator1 == comp || _connection2?.Item2.Generator2 == comp;
}
public bool HasFreeConnections()
{
return _connection1 == null || _connection2 == null;
}
private bool TryGenerateFieldConnection([NotNullWhen(true)] ref Tuple<Direction, ContainmentFieldConnection>? propertyFieldTuple)
{
if (propertyFieldTuple != null) return false;
if(_collidableComponent?.Anchored == false) return false;
foreach (var direction in new[] {Direction.North, Direction.East, Direction.South, Direction.West})
{
if (_connection1?.Item1 == direction || _connection2?.Item1 == direction) continue;
var dirVec = direction.ToVec();
var ray = new CollisionRay(Owner.Transform.WorldPosition, dirVec, (int) CollisionGroup.MobMask);
var rawRayCastResults = EntitySystem.Get<SharedBroadPhaseSystem>().IntersectRay(Owner.Transform.MapID, ray, 4.5f, Owner, false);
var rayCastResults = rawRayCastResults as RayCastResults[] ?? rawRayCastResults.ToArray();
if(!rayCastResults.Any()) continue;
RayCastResults? closestResult = null;
var smallestDist = 4.5f;
foreach (var res in rayCastResults)
{
if (res.Distance > smallestDist) continue;
smallestDist = res.Distance;
closestResult = res;
}
if(closestResult == null) continue;
var ent = closestResult.Value.HitEntity;
if (!ent.TryGetComponent<ContainmentFieldGeneratorComponent>(out var fieldGeneratorComponent) ||
fieldGeneratorComponent.Owner == Owner ||
!fieldGeneratorComponent.HasFreeConnections() ||
IsConnectedWith(fieldGeneratorComponent) ||
!ent.TryGetComponent<PhysicsComponent>(out var collidableComponent) ||
!collidableComponent.Anchored)
{
continue;
}
var connection = new ContainmentFieldConnection(this, fieldGeneratorComponent);
propertyFieldTuple = new Tuple<Direction, ContainmentFieldConnection>(direction, connection);
if (fieldGeneratorComponent._connection1 == null)
{
fieldGeneratorComponent._connection1 = new Tuple<Direction, ContainmentFieldConnection>(direction.GetOpposite(), connection);
}
else if (fieldGeneratorComponent._connection2 == null)
{
fieldGeneratorComponent._connection2 = new Tuple<Direction, ContainmentFieldConnection>(direction.GetOpposite(), connection);
}
else
{
Logger.Error("When trying to connect two Containmentfieldgenerators, the second one already had two connection but the check didn't catch it");
}
UpdateConnectionLights();
return true;
}
return false;
}
public void RemoveConnection(ContainmentFieldConnection? connection)
{
if (_connection1?.Item2 == connection)
{
_connection1 = null;
UpdateConnectionLights();
}
else if (_connection2?.Item2 == connection)
{
_connection2 = null;
UpdateConnectionLights();
}
else if(connection != null)
{
Logger.Error("RemoveConnection called on Containmentfieldgenerator with a connection that can't be found in its connections.");
}
}
public void CollideWith(IPhysBody ourBody, IPhysBody otherBody)
{
if (otherBody.Entity.HasComponent<EmitterBoltComponent>())
{
ReceivePower(4);
}
}
public void UpdateConnectionLights()
{
if (_pointLightComponent != null)
{
bool hasAnyConnection = (_connection1 != null) || (_connection2 != null);
_pointLightComponent.Enabled = hasAnyConnection;
}
}
public override void OnRemove()
{
_connection1?.Item2.Dispose();
_connection2?.Item2.Dispose();
base.OnRemove();
}
}
}