Portals & hand teleporter (#13266)

* basic system with portals & linked ents

* hand tele sprites, no impl

* hand tele and teleportation works

* fancy it up

* oog

* special case projectiles

* predict portal-to-portal teleportation

* this stuff

* check nullspace

* sloth

* give to rd instead

* i guess this can probably happen

* docs
This commit is contained in:
Kara 2023-01-02 19:58:25 -06:00 committed by GitHub
parent a21a4711b6
commit c821ca71aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
23 changed files with 592 additions and 3 deletions

View File

@ -0,0 +1,57 @@
using Content.Shared.Interaction.Events;
using Content.Shared.Teleportation.Components;
using Content.Shared.Teleportation.Systems;
using Robust.Server.GameObjects;
namespace Content.Server.Teleportation;
/// <summary>
/// This handles creating portals from a hand teleporter.
/// </summary>
public sealed class HandTeleporterSystem : EntitySystem
{
[Dependency] private readonly LinkedEntitySystem _link = default!;
[Dependency] private readonly AudioSystem _audio = default!;
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<HandTeleporterComponent, UseInHandEvent>(OnUseInHand);
}
private void OnUseInHand(EntityUid uid, HandTeleporterComponent component, UseInHandEvent args)
{
if (Deleted(component.FirstPortal))
component.FirstPortal = null;
if (Deleted(component.SecondPortal))
component.SecondPortal = null;
// Create the first portal.
if (component.FirstPortal == null && component.SecondPortal == null)
{
var timeout = EnsureComp<PortalTimeoutComponent>(args.User);
timeout.EnteredPortal = null;
component.FirstPortal = Spawn(component.FirstPortalPrototype, Transform(args.User).Coordinates);
_audio.PlayPvs(component.NewPortalSound, uid);
}
else if (component.SecondPortal == null)
{
var timeout = EnsureComp<PortalTimeoutComponent>(args.User);
timeout.EnteredPortal = null;
component.SecondPortal = Spawn(component.SecondPortalPrototype, Transform(args.User).Coordinates);
_link.TryLink(component.FirstPortal!.Value, component.SecondPortal.Value, true);
_audio.PlayPvs(component.NewPortalSound, uid);
}
else
{
// Clear both portals
QueueDel(component.FirstPortal!.Value);
QueueDel(component.SecondPortal!.Value);
component.FirstPortal = null;
component.SecondPortal = null;
_audio.PlayPvs(component.ClearPortalsSound, uid);
}
}
}

View File

@ -0,0 +1,36 @@
using Content.Shared.Audio;
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
using Robust.Shared.Prototypes;
using Robust.Shared.Serialization.TypeSerializers.Implementations.Custom.Prototype;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Creates portals. If two are created, both are linked together--otherwise the first teleports randomly.
/// Using it with both portals active deactivates both.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class HandTeleporterComponent : Component
{
[ViewVariables, DataField("firstPortal")]
public EntityUid? FirstPortal = null;
[ViewVariables, DataField("secondPortal")]
public EntityUid? SecondPortal = null;
[DataField("firstPortalPrototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
public string FirstPortalPrototype = "PortalRed";
[DataField("secondPortalPrototype", customTypeSerializer:typeof(PrototypeIdSerializer<EntityPrototype>))]
public string SecondPortalPrototype = "PortalBlue";
[DataField("newPortalSound")]
public SoundSpecifier NewPortalSound = new SoundPathSpecifier("/Audio/Machines/high_tech_confirm.ogg")
{
Params = AudioParams.Default.WithVolume(-2f)
};
[DataField("clearPortalsSound")]
public SoundSpecifier ClearPortalsSound = new SoundPathSpecifier("/Audio/Machines/button.ogg");
}

View File

@ -0,0 +1,42 @@
using Content.Shared.Teleportation.Systems;
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Represents an entity which is linked to other entities (perhaps portals), and which can be walked through/
/// thrown into to teleport an entity.
/// </summary>
[RegisterComponent, Access(typeof(LinkedEntitySystem)), NetworkedComponent]
public sealed class LinkedEntityComponent : Component
{
/// <summary>
/// The entities that this entity is linked to.
/// </summary>
[DataField("linkedEntities")]
public HashSet<EntityUid> LinkedEntities = new();
/// <summary>
/// Should this entity be deleted if all of its links are removed?
/// </summary>
[DataField("deleteOnEmptyLinks")]
public bool DeleteOnEmptyLinks = false;
}
[Serializable, NetSerializable]
public sealed class LinkedEntityComponentState : ComponentState
{
public HashSet<EntityUid> LinkedEntities;
public LinkedEntityComponentState(HashSet<EntityUid> linkedEntities)
{
LinkedEntities = linkedEntities;
}
}
[Serializable, NetSerializable]
public enum LinkedEntityVisuals : byte
{
HasAnyLinks
}

View File

@ -0,0 +1,31 @@
using Robust.Shared.Audio;
using Robust.Shared.GameStates;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Marks an entity as being a 'portal' which teleports entities sent through it to linked entities.
/// Relies on <see cref="LinkedEntityComponent"/> being set up.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class PortalComponent : Component
{
/// <summary>
/// Sound played on arriving to this portal, centered on the destination.
/// The arrival sound of the entered portal will play if the destination is not a portal.
/// </summary>
[DataField("arrivalSound")]
public SoundSpecifier ArrivalSound = new SoundPathSpecifier("/Audio/Effects/teleport_arrival.ogg");
/// <summary>
/// Sound played on departing from this portal, centered on the original portal.
/// </summary>
[DataField("departureSound")]
public SoundSpecifier DepartureSound = new SoundPathSpecifier("/Audio/Effects/teleport_departure.ogg");
/// <summary>
/// If no portals are linked, the subject will be teleported a random distance at maximum this far away.
/// </summary>
[DataField("maxRandomRadius")]
public float MaxRandomRadius = 10.0f;
}

View File

@ -0,0 +1,29 @@
using Robust.Shared.GameStates;
using Robust.Shared.Serialization;
namespace Content.Shared.Teleportation.Components;
/// <summary>
/// Attached to an entity after portal transit to mark that they should not immediately be portaled back
/// at the end destination.
/// </summary>
[RegisterComponent, NetworkedComponent]
public sealed class PortalTimeoutComponent : Component
{
/// <summary>
/// The portal that was entered. Null if coming from a hand teleporter, etc.
/// </summary>
[ViewVariables, DataField("enteredPortal")]
public EntityUid? EnteredPortal = null;
}
[Serializable, NetSerializable]
public sealed class PortalTimeoutComponentState : ComponentState
{
public EntityUid? EnteredPortal;
public PortalTimeoutComponentState(EntityUid? enteredPortal)
{
EnteredPortal = enteredPortal;
}
}

View File

@ -0,0 +1,115 @@
using System.Linq;
using Content.Shared.Teleportation.Components;
using Robust.Shared.GameStates;
namespace Content.Shared.Teleportation.Systems;
/// <summary>
/// Handles symmetrically linking two entities together, and removing links properly.
/// This does not do anything on its own (outside of deleting entities that have 0 links, if that option is true)
/// Systems can do whatever they please with the linked entities, such as <see cref="PortalSystem"/>.
/// </summary>
public sealed class LinkedEntitySystem : EntitySystem
{
[Dependency] private readonly SharedAppearanceSystem _appearance = default!;
/// <inheritdoc/>
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<LinkedEntityComponent, ComponentShutdown>(OnLinkShutdown);
SubscribeLocalEvent<LinkedEntityComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<LinkedEntityComponent, ComponentHandleState>(OnHandleState);
}
private void OnGetState(EntityUid uid, LinkedEntityComponent component, ref ComponentGetState args)
{
args.State = new LinkedEntityComponentState(component.LinkedEntities);
}
private void OnHandleState(EntityUid uid, LinkedEntityComponent component, ref ComponentHandleState args)
{
if (args.Current is LinkedEntityComponentState state)
component.LinkedEntities = state.LinkedEntities;
}
private void OnLinkShutdown(EntityUid uid, LinkedEntityComponent component, ComponentShutdown args)
{
// Remove any links to this entity when deleted.
foreach (var ent in component.LinkedEntities.ToArray())
{
if (LifeStage(ent) < EntityLifeStage.Terminating && TryComp<LinkedEntityComponent>(ent, out var link))
{
TryUnlink(uid, ent, component, link);
}
}
}
#region Public API
/// <summary>
/// Links two entities together. Does not require the existence of <see cref="LinkedEntityComponent"/> on either
/// already. Linking is symmetrical, so order doesn't matter.
/// </summary>
/// <param name="first">The first entity to link</param>
/// <param name="second">The second entity to link</param>
/// <param name="deleteOnEmptyLinks">Whether both entities should now delete once their links are removed</param>
/// <returns>Whether linking was successful (e.g. they weren't already linked)</returns>
public bool TryLink(EntityUid first, EntityUid second, bool deleteOnEmptyLinks=false)
{
var firstLink = EnsureComp<LinkedEntityComponent>(first);
var secondLink = EnsureComp<LinkedEntityComponent>(second);
firstLink.DeleteOnEmptyLinks = deleteOnEmptyLinks;
secondLink.DeleteOnEmptyLinks = deleteOnEmptyLinks;
_appearance.SetData(first, LinkedEntityVisuals.HasAnyLinks, true);
_appearance.SetData(second, LinkedEntityVisuals.HasAnyLinks, true);
Dirty(firstLink);
Dirty(secondLink);
return firstLink.LinkedEntities.Add(second)
&& secondLink.LinkedEntities.Add(first);
}
/// <summary>
/// Unlinks two entities. Deletes either entity if <see cref="LinkedEntityComponent.DeleteOnEmptyLinks"/>
/// was true and its links are now empty. Symmetrical, so order doesn't matter.
/// </summary>
/// <param name="first">The first entity to unlink</param>
/// <param name="second">The second entity to unlink</param>
/// <param name="firstLink">Resolve comp</param>
/// <param name="secondLink">Resolve comp</param>
/// <returns>Whether unlinking was successful (e.g. they both were actually linked to one another)</returns>
public bool TryUnlink(EntityUid first, EntityUid second,
LinkedEntityComponent? firstLink=null, LinkedEntityComponent? secondLink=null)
{
if (!Resolve(first, ref firstLink))
return false;
if (!Resolve(second, ref secondLink))
return false;
var success = firstLink.LinkedEntities.Remove(second)
&& secondLink.LinkedEntities.Remove(first);
_appearance.SetData(first, LinkedEntityVisuals.HasAnyLinks, firstLink.LinkedEntities.Any());
_appearance.SetData(second, LinkedEntityVisuals.HasAnyLinks, secondLink.LinkedEntities.Any());
Dirty(firstLink);
Dirty(secondLink);
if (firstLink.LinkedEntities.Count == 0 && firstLink.DeleteOnEmptyLinks)
QueueDel(first);
if (secondLink.LinkedEntities.Count == 0 && secondLink.DeleteOnEmptyLinks)
QueueDel(second);
return success;
}
#endregion
}

View File

@ -0,0 +1,146 @@
using System.Linq;
using Content.Shared.Projectiles;
using Content.Shared.Teleportation.Components;
using Robust.Shared.GameStates;
using Robust.Shared.Map;
using Robust.Shared.Network;
using Robust.Shared.Physics.Dynamics;
using Robust.Shared.Physics.Events;
using Robust.Shared.Player;
using Robust.Shared.Random;
namespace Content.Shared.Teleportation.Systems;
/// <summary>
/// This handles teleporting entities through portals, and creating new linked portals.
/// </summary>
public sealed class PortalSystem : EntitySystem
{
[Dependency] private readonly IRobustRandom _random = default!;
[Dependency] private readonly INetManager _netMan = default!;
[Dependency] private readonly SharedAudioSystem _audio = default!;
private const string PortalFixture = "portalFixture";
private const string ProjectileFixture = "projectile";
/// <inheritdoc/>
public override void Initialize()
{
SubscribeLocalEvent<PortalComponent, StartCollideEvent>(OnCollide);
SubscribeLocalEvent<PortalComponent, EndCollideEvent>(OnEndCollide);
SubscribeLocalEvent<PortalTimeoutComponent, ComponentGetState>(OnGetState);
SubscribeLocalEvent<PortalTimeoutComponent, ComponentHandleState>(OnHandleState);
}
private void OnGetState(EntityUid uid, PortalTimeoutComponent component, ref ComponentGetState args)
{
args.State = new PortalTimeoutComponentState(component.EnteredPortal);
}
private void OnHandleState(EntityUid uid, PortalTimeoutComponent component, ref ComponentHandleState args)
{
if (args.Current is PortalTimeoutComponentState state)
component.EnteredPortal = state.EnteredPortal;
}
private bool ShouldCollide(Fixture our, Fixture other)
{
// most non-hard fixtures shouldn't pass through portals, but projectiles are non-hard as well
// and they should still pass through
return our.ID == PortalFixture && (other.Hard || other.ID == ProjectileFixture);
}
private void OnCollide(EntityUid uid, PortalComponent component, ref StartCollideEvent args)
{
if (!ShouldCollide(args.OurFixture, args.OtherFixture))
return;
var subject = args.OtherFixture.Body.Owner;
// best not.
if (Transform(subject).Anchored)
return;
// if they came from another portal, just return and wait for them to exit the portal
if (HasComp<PortalTimeoutComponent>(subject))
{
return;
}
if (TryComp<LinkedEntityComponent>(uid, out var link))
{
if (!link.LinkedEntities.Any())
return;
// client can't predict outside of simple portal-to-portal interactions due to randomness involved
// --also can't predict if the target doesn't exist on the client / is outside of PVS
if (_netMan.IsClient)
{
var first = link.LinkedEntities.First();
var exists = Exists(first);
if (link.LinkedEntities.Count != 1 || !exists || (exists && Transform(first).MapID == MapId.Nullspace))
return;
}
// pick a target and teleport there
var target = _random.Pick(link.LinkedEntities);
if (HasComp<PortalComponent>(target))
{
// if target is a portal, signal that they shouldn't be immediately portaled back
var timeout = EnsureComp<PortalTimeoutComponent>(subject);
timeout.EnteredPortal = uid;
Dirty(timeout);
}
TeleportEntity(uid, subject, Transform(target).Coordinates, target);
return;
}
if (_netMan.IsClient)
return;
// no linked entity--teleport randomly
var randVector = _random.NextVector2(component.MaxRandomRadius);
var newCoords = Transform(uid).Coordinates.Offset(randVector);
TeleportEntity(uid, subject, newCoords);
}
private void OnEndCollide(EntityUid uid, PortalComponent component, ref EndCollideEvent args)
{
if (!ShouldCollide(args.OurFixture, args.OtherFixture))
return;
var subject = args.OtherFixture.Body.Owner;
// if they came from (not us), remove the timeout
if (TryComp<PortalTimeoutComponent>(subject, out var timeout) && timeout.EnteredPortal != uid)
{
RemComp<PortalTimeoutComponent>(subject);
}
}
private void TeleportEntity(EntityUid portal, EntityUid subject, EntityCoordinates target, EntityUid? targetEntity=null,
PortalComponent? portalComponent = null)
{
if (!Resolve(portal, ref portalComponent))
return;
var arrivalSound = CompOrNull<PortalComponent>(targetEntity)?.ArrivalSound ?? portalComponent.ArrivalSound;
var departureSound = portalComponent.DepartureSound;
// Some special cased stuff: projectiles should stop ignoring shooter when they enter a portal, to avoid
// stacking 500 bullets in between 2 portals and instakilling people--you'll just hit yourself instead
// (as expected)
if (TryComp<ProjectileComponent>(subject, out var projectile))
{
projectile.IgnoreShooter = false;
}
Transform(subject).Coordinates = target;
_audio.PlayPredicted(departureSound, portal, subject);
_audio.PlayPredicted(arrivalSound, subject, subject);
}
}

View File

@ -2,3 +2,7 @@
license: "CC-BY-NC-SA-3.0"
copyright: "Amateur foley and audio editing by Bright0."
source: "https://github.com/Bright0"
- files: ["teleport_arrival.ogg", "teleport_departure.ogg"]
license: "CC-BY-SA-3.0"
copyright: "tgstation"
source: "https://github.com/tgstation/tgstation/commit/906fb0682bab6a0975b45036001c54f021f58ae7"

View File

@ -152,6 +152,7 @@
- id: ClothingNeckCloakRd
- id: ClothingHeadsetMedicalScience
- id: ClothingOuterHardsuitRd
- id: HandTeleporter
- id: PlushieSlime
prob: 0.1
- id: DoorRemoteResearch

View File

@ -0,0 +1,51 @@
- type: entity
id: BasePortal
abstract: true
name: bluespace portal
description: Transports you to a linked destination!
components:
- type: InteractionOutline
- type: Clickable
- type: Physics
bodyType: Static
- type: Sprite
sprite: /Textures/Effects/portal.rsi
- type: Fixtures
fixtures:
- id: portalFixture
shape:
!type:PhysShapeAabb
bounds: "-0.25,-0.48,0.25,0.48"
mask:
- FullTileMask
layer:
- WallLayer
hard: false
- type: Portal
- type: entity
id: PortalRed
parent: BasePortal
description: This one looks more like a redspace portal.
components:
- type: Sprite
layers:
- state: portal-red
- type: PointLight
netsync: false
color: OrangeRed
radius: 3
energy: 3
- type: entity
id: PortalBlue
parent: BasePortal
components:
- type: Sprite
layers:
- state: portal-blue
- type: PointLight
netsync: false
color: SkyBlue
radius: 3
energy: 3

View File

@ -0,0 +1,11 @@
- type: entity
id: HandTeleporter
parent: BaseItem
name: hand teleporter
description: "A Nanotrasen signature item--only the finest bluespace tech. Instructions: Use once to create a portal which teleports at random. Use again to link it to a portal at your current location. Use again to clear all portals."
components:
- type: Sprite
sprite: /Textures/Objects/Devices/hand_teleporter.rsi
layers:
- state: icon
- type: HandTeleporter

View File

@ -20,6 +20,7 @@
CorgiMeatStealObjective: 1
CaptainGunStealObjective: 0.5
CaptainJetpackStealObjective: 0.5
HandTeleporterStealObjective: 0.5
- type: weightedRandom
id: TraitorObjectiveGroupKill

View File

@ -82,6 +82,22 @@
prototype: ClothingOuterHardsuitRd
owner: job-name-rd
- type: objective
id: HandTeleporterStealObjective
issuer: syndicate
difficultyOverride: 2.75
requirements:
- !type:TraitorRequirement {}
- !type:IncompatibleConditionsRequirement
conditions:
- DieCondition
- !type:NotRoleRequirement
roleId: ResearchDirector
conditions:
- !type:StealCondition
prototype: HandTeleporter
owner: job-name-rd
- type: objective
id: NukeDiskStealObjective
issuer: syndicate

View File

@ -8,7 +8,7 @@
"copyright": "https://github.com/discordia-space/CEV-Eris/raw/237d8f7894617007d75c71d5d9feb4354c78debd/icons/obj/stationobjs.dmi",
"states": [
{
"name": "portal-pending",
"name": "portal-blue",
"delays": [[
0.1,
0.1,
@ -27,7 +27,26 @@
]]
},
{
"name": "portal-unconnected",
"name": "portal-red",
"delays": [[
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1,
0.1
]]
},
{
"name": "portal",
"delays": [[
0.1,
0.1,
@ -46,4 +65,4 @@
]]
}
]
}
}

View File

Before

Width:  |  Height:  |  Size: 1.6 KiB

After

Width:  |  Height:  |  Size: 1.6 KiB

View File

Before

Width:  |  Height:  |  Size: 1.7 KiB

After

Width:  |  Height:  |  Size: 1.7 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 710 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 306 B

Binary file not shown.

After

Width:  |  Height:  |  Size: 316 B

View File

@ -0,0 +1,30 @@
{
"version": 1,
"license": "CC-BY-SA-3.0",
"copyright": "tgstation at 43d3fb991641d0dc73b6ce81cdf4f3f09d6b70a0",
"size": {
"x": 32,
"y": 32
},
"states": [
{
"name": "icon",
"delays": [
[
0.1,
0.1,
0.1,
0.1
]
]
},
{
"name": "inhand-left",
"directions": 4
},
{
"name": "inhand-right",
"directions": 4
}
]
}