Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 32 additions & 0 deletions SecretAPI/Enums/RoomSafetyFailReason.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
namespace SecretAPI.Enums
{
using System;
using MapGeneration;

/// <summary>
/// Reasons why room safety should fail.
/// </summary>
[Flags]
public enum RoomSafetyFailReason
{
/// <summary>
/// No fail.
/// </summary>
None = 0,

/// <summary>
/// Room safety check will fail if warhead has gone off and this is not part of <see cref="FacilityZone.Surface"/>.
/// </summary>
Warhead = 1,

/// <summary>
/// Room safety check will fail if decontamination has gone off and this is part of <see cref="FacilityZone.LightContainment"/>.
/// </summary>
Decontamination = 2,

/// <summary>
/// Room safety check will fail if the listed room is <see cref="TeslaGate"/>.
/// </summary>
Tesla = 4,
}
}
44 changes: 40 additions & 4 deletions SecretAPI/Extensions/RoomExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,19 @@
using System.Collections.Generic;
using LabApi.Features.Wrappers;
using MapGeneration;
using PlayerRoles.FirstPersonControl;
using PlayerRoles.PlayableScps.Scp106;
using SecretAPI.Enums;
using UnityEngine;

/// <summary>
/// Extensions related to rooms.
/// </summary>
/// TODO: Make TryGetSafeTeleport(Room)
public static class RoomExtensions
{
private static readonly List<RoomName> KnownUnsafeRooms =
[
RoomName.HczTesla, // Instant death
RoomName.EzEvacShelter, // Stuck permanently
RoomName.EzCollapsedTunnel, // Stuck permanently
RoomName.HczWaysideIncinerator, // Death
Expand All @@ -23,19 +26,52 @@ public static class RoomExtensions
/// Gets whether a room is safe to teleport to. Will consider decontamination, warhead, teslas and void rooms.
/// </summary>
/// <param name="room">The room to check.</param>
/// <param name="failReasons">Reasons why .</param>
/// <returns>Whether the room is safe to teleport to.</returns>
public static bool IsSafeToTeleport(this Room room)
public static bool IsSafeToTeleport(this Room room, RoomSafetyFailReason failReasons)
{
if (Warhead.IsDetonated && room.Zone != FacilityZone.Surface)
if (failReasons.HasFlag(RoomSafetyFailReason.Warhead) && Warhead.IsDetonated && room.Zone != FacilityZone.Surface)
return false;

if (Decontamination.IsDecontaminating && room.Zone == FacilityZone.LightContainment)
if (failReasons.HasFlag(RoomSafetyFailReason.Decontamination) && Decontamination.IsDecontaminating && room.Zone == FacilityZone.LightContainment)
return false;

if (failReasons.HasFlag(RoomSafetyFailReason.Tesla) && room.Name == RoomName.HczTesla)
return false;

if (KnownUnsafeRooms.Contains(room.Name))
return false;

return Physics.Raycast(room.Position, Vector3.down, out _, 2);
}

/// <summary>
/// Gets a safe teleport point for a <see cref="Player"/>.
/// </summary>
/// <param name="player">The player to get teleport point for.</param>
/// <param name="zone">The zone to attempt to teleport to player to.</param>
/// <param name="range">The range of which the position is allowed to vary from its "point".</param>
/// <param name="teleportPoint">The teleport point which was found.</param>
/// <returns>Whether a valid position was found.</returns>
public static bool GetSafeTeleportPoint(this Player player, FacilityZone zone, float range, out Vector3 teleportPoint)
{
if (player.RoleBase is not IFpcRole fpcRole)
{
teleportPoint = Vector3.zero;
return false;
}

Pose[] poses = Scp106PocketExitFinder.GetPosesForZone(zone);
if (poses.IsEmpty())
{
teleportPoint = Vector3.zero;
return false;
}

Pose pose = Scp106PocketExitFinder.GetRandomPose(poses);
Vector3 position = SafeLocationFinder.GetSafePosition(pose.position, pose.rotation.eulerAngles, range, fpcRole.FpcModule.CharController);
teleportPoint = position;
return true;
}
}
}