Re-organize all projects (#4166)

This commit is contained in:
DrSmugleaf
2021-06-09 22:19:39 +02:00
committed by GitHub
parent 9f50e4061b
commit ff1a2d97ea
1773 changed files with 5258 additions and 5508 deletions

View File

@@ -0,0 +1,187 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Content.Server.Players;
using Content.Server.Visible;
using Content.Server.Warps;
using Content.Shared.Examine;
using Content.Shared.Ghost;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
using Robust.Shared.Log;
using Robust.Shared.Network;
using Robust.Shared.Players;
using Robust.Shared.Timing;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
#nullable enable
namespace Content.Server.Ghost.Components
{
[RegisterComponent]
public class GhostComponent : SharedGhostComponent, IExamine
{
private bool _canReturnToBody = true;
private TimeSpan _timeOfDeath = TimeSpan.Zero;
[Dependency] private readonly IPlayerManager _playerManager = default!;
[Dependency] private readonly IGameTiming _gameTimer = default!;
[ViewVariables(VVAccess.ReadWrite)]
public bool CanReturnToBody
{
get => _canReturnToBody;
set
{
_canReturnToBody = value;
Dirty();
}
}
/// <inheritdoc />
protected override void Startup()
{
base.Startup();
// Allow this entity to be seen by other ghosts.
Owner.EnsureComponent<VisibilityComponent>().Layer |= (int) VisibilityFlags.Ghost;
// Allows this entity to see other ghosts.
Owner.EnsureComponent<EyeComponent>().VisibilityMask |= (uint) VisibilityFlags.Ghost;
_timeOfDeath = _gameTimer.RealTime;
}
/// <inheritdoc />
protected override void Shutdown()
{
//Perf: If the entity is deleting itself, no reason to change these back.
if(Owner.LifeStage < EntityLifeStage.Terminating)
{
// Entity can't be seen by ghosts anymore.
if (Owner.TryGetComponent<VisibilityComponent>(out var visComp))
visComp.Layer &= ~(int) VisibilityFlags.Ghost;
// Entity can't see ghosts anymore.
if (Owner.TryGetComponent<EyeComponent>(out var eyeComp))
eyeComp.VisibilityMask &= ~(uint) VisibilityFlags.Ghost;
}
base.Shutdown();
}
public override ComponentState GetComponentState(ICommonSession player) => new GhostComponentState(CanReturnToBody);
public override void HandleNetworkMessage(ComponentMessage message, INetChannel netChannel, ICommonSession? session = null!)
{
base.HandleNetworkMessage(message, netChannel, session);
switch (message)
{
case ReturnToBodyComponentMessage:
{
if (!Owner.TryGetComponent(out ActorComponent? actor) ||
!CanReturnToBody)
{
break;
}
if (netChannel == actor.PlayerSession.ConnectedClient)
{
var o = actor.PlayerSession.ContentData()!.Mind;
o?.UnVisit();
}
break;
}
case GhostWarpToLocationRequestMessage warp:
{
if (session?.AttachedEntity != Owner)
{
break;
}
foreach (var warpPoint in FindWaypoints())
{
if (warp.Name == warpPoint.Location)
{
Owner.Transform.Coordinates = warpPoint.Owner.Transform.Coordinates;
break;
}
}
Logger.Warning($"User {session.Name} tried to warp to an invalid warp: {warp.Name}");
break;
}
case GhostWarpToTargetRequestMessage target:
{
if (session?.AttachedEntity != Owner)
{
break;
}
if (!Owner.TryGetComponent(out ActorComponent? actor))
{
break;
}
if (!Owner.EntityManager.TryGetEntity(target.Target, out var entity))
{
Logger.Warning($"User {session.Name} tried to warp to an invalid entity id: {target.Target}");
break;
}
if (!_playerManager.TryGetSessionByChannel(actor.PlayerSession.ConnectedClient, out var player) ||
player.AttachedEntity != entity)
{
break;
}
Owner.Transform.Coordinates = entity.Transform.Coordinates;
break;
}
case GhostRequestPlayerNameData _:
var playerNames = new Dictionary<EntityUid, string>();
foreach (var names in _playerManager.GetAllPlayers())
{
if (names.AttachedEntity != null && names.UserId != netChannel.UserId)
{
playerNames.Add(names.AttachedEntity.Uid,names.AttachedEntity.Name);
}
}
SendNetworkMessage(new GhostReplyPlayerNameData(playerNames));
break;
case GhostRequestWarpPointData _:
var warpPoints = FindWaypoints();
var warpName = new List<string>();
foreach (var point in warpPoints)
{
if (point.Location == null)
{
continue;
}
warpName.Add(point.Location);
}
SendNetworkMessage(new GhostReplyWarpPointData(warpName));
break;
}
}
private List<WarpPointComponent> FindWaypoints()
{
var comp = IoCManager.Resolve<IComponentManager>();
return comp.EntityQuery<WarpPointComponent>(true).ToList();
}
public void Examine(FormattedMessage message, bool inDetailsRange)
{
var timeSinceDeath = _gameTimer.RealTime.Subtract(_timeOfDeath);
//If we've been dead for longer than 1 minute use minutes, otherwise use seconds. Ignore the improper plurals.
var deathTimeInfo = timeSinceDeath.Minutes > 0 ? Loc.GetString($"{timeSinceDeath.Minutes} minutes ago") : Loc.GetString($"{timeSinceDeath.Seconds} seconds ago");
message.AddMarkup(Loc.GetString("Died [color=yellow]{0}[/color].", deathTimeInfo));
}
}
}

View File

@@ -0,0 +1,30 @@
#nullable enable
using Content.Server.GameTicking;
using Content.Server.Mind.Components;
using Content.Shared.Movement.Components;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Players;
using Robust.Shared.Serialization.Manager.Attributes;
namespace Content.Server.Ghost.Components
{
[RegisterComponent]
[ComponentReference(typeof(IGhostOnMove))]
public class GhostOnMoveComponent : Component, IRelayMoveInput, IGhostOnMove
{
public override string Name => "GhostOnMove";
[Dependency] private readonly IGameTicker _gameTicker = default!;
[DataField("canReturn")] public bool CanReturn { get; set; } = true;
void IRelayMoveInput.MoveInputPressed(ICommonSession session)
{
// Let's not ghost if our mind is visiting...
if (Owner.HasComponent<VisitingMindComponent>()) return;
if (!Owner.TryGetComponent(out MindComponent? mind) || !mind.HasMind || mind.Mind!.IsVisitingEntity) return;
_gameTicker.OnGhostAttempt(mind.Mind!, CanReturn);
}
}
}

View File

@@ -0,0 +1,41 @@
#nullable enable
using Content.Server.Administration;
using Content.Server.GameTicking;
using Content.Server.Players;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.IoC;
namespace Content.Server.Ghost
{
[AnyCommand]
public class Ghost : IConsoleCommand
{
public string Command => "ghost";
public string Description => "Give up on life and become a ghost.";
public string Help => "ghost";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var player = shell.Player as IPlayerSession;
if (player == null)
{
shell?.WriteLine("You have no session, you can't ghost.");
return;
}
var mind = player.ContentData()?.Mind;
if (mind == null)
{
shell?.WriteLine("You have no Mind, you can't ghost.");
return;
}
if (!IoCManager.Resolve<IGameTicker>().OnGhostAttempt(mind, true))
{
shell?.WriteLine("You can't ghost right now.");
return;
}
}
}
}

View File

@@ -0,0 +1,49 @@
using Content.Server.Ghost.Components;
using Content.Server.Mind.Components;
using JetBrains.Annotations;
using Robust.Shared.GameObjects;
namespace Content.Server.Ghost
{
[UsedImplicitly]
public class GhostSystem : EntitySystem
{
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<GhostComponent, MindRemovedMessage>(OnMindRemovedMessage);
SubscribeLocalEvent<GhostComponent, MindUnvisitedMessage>(OnMindUnvisitedMessage);
}
public override void Shutdown()
{
base.Shutdown();
UnsubscribeLocalEvent<GhostComponent, MindRemovedMessage>(OnMindRemovedMessage);
UnsubscribeLocalEvent<GhostComponent, MindUnvisitedMessage>(OnMindUnvisitedMessage);
}
private void OnMindRemovedMessage(EntityUid uid, GhostComponent component, MindRemovedMessage args)
{
DeleteEntity(uid);
}
private void OnMindUnvisitedMessage(EntityUid uid, GhostComponent component, MindUnvisitedMessage args)
{
DeleteEntity(uid);
}
private void DeleteEntity(EntityUid uid)
{
if (!EntityManager.TryGetEntity(uid, out var entity)
|| entity.Deleted == true
|| entity.LifeStage == EntityLifeStage.Terminating)
return;
if (entity.TryGetComponent<MindComponent>(out var mind))
mind.GhostOnShutdown = false;
entity.Delete();
}
}
}

View File

@@ -0,0 +1,19 @@
#nullable enable
using Content.Shared.Actions.Behaviors;
namespace Content.Server.Ghost
{
/// <summary>
/// Allow ghost to interact with object by boo action
/// </summary>
public interface IGhostBooAffected
{
/// <summary>
/// Invokes when ghost used boo action near entity.
/// Use it to blink lights or make something spooky.
/// </summary>
/// <param name="args">Boo action details</param>
/// <returns>Returns true if object was affected</returns>
bool AffectedByGhostBoo(InstantActionEventArgs args);
}
}

View File

@@ -0,0 +1,6 @@
namespace Content.Server.Ghost
{
public interface IGhostOnMove
{
}
}

View File

@@ -0,0 +1,60 @@
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.ViewVariables;
namespace Content.Server.Ghost.Roles.Components
{
public abstract class GhostRoleComponent : Component
{
[DataField("name")] private string _roleName = "Unknown";
[DataField("description")] private string _roleDescription = "Unknown";
// We do this so updating RoleName and RoleDescription in VV updates the open EUIs.
[ViewVariables(VVAccess.ReadWrite)]
public string RoleName
{
get => _roleName;
set
{
_roleName = value;
EntitySystem.Get<GhostRoleSystem>().UpdateAllEui();
}
}
[ViewVariables(VVAccess.ReadWrite)]
public string RoleDescription
{
get => _roleDescription;
set
{
_roleDescription = value;
EntitySystem.Get<GhostRoleSystem>().UpdateAllEui();
}
}
[ViewVariables(VVAccess.ReadOnly)]
public bool Taken { get; protected set; }
[ViewVariables]
public uint Identifier { get; set; }
public override void Initialize()
{
base.Initialize();
EntitySystem.Get<GhostRoleSystem>().RegisterGhostRole(this);
}
protected override void Shutdown()
{
base.Shutdown();
EntitySystem.Get<GhostRoleSystem>().UnregisterGhostRole(this);
}
public abstract bool Take(IPlayerSession session);
}
}

View File

@@ -0,0 +1,72 @@
using System;
using Content.Server.Mind.Commands;
using Content.Server.Mind.Components;
using Content.Server.Players;
using JetBrains.Annotations;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Serialization.Manager.Attributes;
using Robust.Shared.Utility;
using Robust.Shared.ViewVariables;
namespace Content.Server.Ghost.Roles.Components
{
/// <summary>
/// Allows a ghost to take this role, spawning a new entity.
/// </summary>
[RegisterComponent, ComponentReference(typeof(GhostRoleComponent))]
public class GhostRoleMobSpawnerComponent : GhostRoleComponent
{
public override string Name => "GhostRoleMobSpawner";
[ViewVariables(VVAccess.ReadWrite)] [DataField("deleteOnSpawn")]
private bool _deleteOnSpawn = true;
[ViewVariables(VVAccess.ReadWrite)] [DataField("makeSentient")]
private bool _makeSentient = true;
[ViewVariables(VVAccess.ReadWrite)] [DataField("availableTakeovers")]
private int _availableTakeovers = 1;
[ViewVariables]
private int _currentTakeovers = 0;
[CanBeNull]
[ViewVariables(VVAccess.ReadWrite)]
[DataField("prototype")]
public string? Prototype { get; private set; }
public override bool Take(IPlayerSession session)
{
if (Taken)
return false;
if(string.IsNullOrEmpty(Prototype))
throw new NullReferenceException("Prototype string cannot be null or empty!");
var mob = Owner.EntityManager.SpawnEntity(Prototype, Owner.Transform.Coordinates);
if(_makeSentient)
MakeSentientCommand.MakeSentient(mob);
mob.EnsureComponent<MindComponent>();
var mind = session.ContentData()?.Mind;
DebugTools.AssertNotNull(mind);
mind!.TransferTo(mob);
if (++_currentTakeovers < _availableTakeovers) return true;
Taken = true;
if (_deleteOnSpawn)
Owner.Delete();
return true;
}
}
}

View File

@@ -0,0 +1,41 @@
using System;
using Content.Server.Mind.Components;
using Content.Server.Players;
using Robust.Server.Player;
using Robust.Shared.GameObjects;
using Robust.Shared.Utility;
namespace Content.Server.Ghost.Roles.Components
{
/// <summary>
/// Allows a ghost to take over the Owner entity.
/// </summary>
[RegisterComponent, ComponentReference(typeof(GhostRoleComponent))]
public class GhostTakeoverAvailableComponent : GhostRoleComponent
{
public override string Name => "GhostTakeoverAvailable";
public override bool Take(IPlayerSession session)
{
if (Taken)
return false;
Taken = true;
var mind = Owner.EnsureComponent<MindComponent>();
if (mind.HasMind)
throw new Exception("MindComponent already has a mind!");
var sessionMind = session.ContentData()?.Mind;
DebugTools.AssertNotNull(sessionMind);
sessionMind!.TransferTo(Owner);
EntitySystem.Get<GhostRoleSystem>().UnregisterGhostRole(this);
return true;
}
}
}

View File

@@ -0,0 +1,174 @@
using System.Collections.Generic;
using Content.Server.Administration;
using Content.Server.EUI;
using Content.Server.Ghost.Components;
using Content.Server.Ghost.Roles.Components;
using Content.Server.Ghost.Roles.UI;
using Content.Shared.GameTicking;
using Content.Shared.Ghost.Roles;
using JetBrains.Annotations;
using Robust.Server.GameObjects;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.ViewVariables;
namespace Content.Server.Ghost.Roles
{
[UsedImplicitly]
public class GhostRoleSystem : EntitySystem, IResettingEntitySystem
{
[Dependency] private readonly EuiManager _euiManager = default!;
private uint _nextRoleIdentifier = 0;
private readonly Dictionary<uint, GhostRoleComponent> _ghostRoles = new();
private readonly Dictionary<IPlayerSession, GhostRolesEui> _openUis = new();
private readonly Dictionary<IPlayerSession, MakeGhostRoleEui> _openMakeGhostRoleUis = new();
[ViewVariables]
public IReadOnlyCollection<GhostRoleComponent> GhostRoles => _ghostRoles.Values;
public override void Initialize()
{
base.Initialize();
SubscribeLocalEvent<PlayerAttachedEvent>(OnPlayerAttached);
}
public override void Shutdown()
{
base.Shutdown();
UnsubscribeLocalEvent<PlayerAttachedEvent>();
}
private uint GetNextRoleIdentifier()
{
return unchecked(_nextRoleIdentifier++);
}
public void OpenEui(IPlayerSession session)
{
if (session.AttachedEntity == null || !session.AttachedEntity.HasComponent<GhostComponent>())
return;
if(_openUis.ContainsKey(session))
CloseEui(session);
var eui = _openUis[session] = new GhostRolesEui();
_euiManager.OpenEui(eui, session);
eui.StateDirty();
}
public void OpenMakeGhostRoleEui(IPlayerSession session, EntityUid uid)
{
if (session.AttachedEntity == null)
return;
if (_openMakeGhostRoleUis.ContainsKey(session))
CloseEui(session);
var eui = _openMakeGhostRoleUis[session] = new MakeGhostRoleEui(uid);
_euiManager.OpenEui(eui, session);
eui.StateDirty();
}
public void CloseEui(IPlayerSession session)
{
if (!_openUis.ContainsKey(session)) return;
_openUis.Remove(session, out var eui);
eui?.Close();
}
public void CloseMakeGhostRoleEui(IPlayerSession session)
{
if (_openMakeGhostRoleUis.Remove(session, out var eui))
{
eui?.Close();
}
}
public void UpdateAllEui()
{
foreach (var eui in _openUis.Values)
{
eui.StateDirty();
}
}
public void RegisterGhostRole(GhostRoleComponent role)
{
if (_ghostRoles.ContainsValue(role)) return;
_ghostRoles[role.Identifier = GetNextRoleIdentifier()] = role;
UpdateAllEui();
}
public void UnregisterGhostRole(GhostRoleComponent role)
{
if (!_ghostRoles.ContainsKey(role.Identifier) || _ghostRoles[role.Identifier] != role) return;
_ghostRoles.Remove(role.Identifier);
UpdateAllEui();
}
public void Takeover(IPlayerSession player, uint identifier)
{
if (!_ghostRoles.TryGetValue(identifier, out var role)) return;
if (!role.Take(player)) return;
CloseEui(player);
}
public GhostRoleInfo[] GetGhostRolesInfo()
{
var roles = new GhostRoleInfo[_ghostRoles.Count];
var i = 0;
foreach (var (id, role) in _ghostRoles)
{
roles[i] = new GhostRoleInfo(){Identifier = id, Name = role.RoleName, Description = role.RoleDescription};
i++;
}
return roles;
}
private void OnPlayerAttached(PlayerAttachedEvent message)
{
// Close the session of any player that has a ghost roles window open and isn't a ghost anymore.
if (!_openUis.ContainsKey(message.Player)) return;
if (message.Entity.HasComponent<GhostComponent>()) return;
CloseEui(message.Player);
}
public void Reset()
{
foreach (var session in _openUis.Keys)
{
CloseEui(session);
}
_openUis.Clear();
_ghostRoles.Clear();
_nextRoleIdentifier = 0;
}
}
[AnyCommand]
public class GhostRoles : IConsoleCommand
{
public string Command => "ghostroles";
public string Description => "Opens the ghost role request window.";
public string Help => $"{Command}";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if(shell.Player != null)
EntitySystem.Get<GhostRoleSystem>().OpenEui((IPlayerSession)shell.Player);
else
shell.WriteLine("You can only open the ghost roles UI on a client.");
}
}
}

View File

@@ -0,0 +1,63 @@
#nullable enable
using Content.Server.Administration;
using Content.Server.Ghost.Roles.Components;
using Content.Server.Mind.Components;
using Content.Shared.Administration;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
namespace Content.Server.Ghost.Roles
{
[AdminCommand(AdminFlags.Fun)]
public class MakeGhostRoleCommand : IConsoleCommand
{
public string Command => "makeghostrole";
public string Description => "Turns an entity into a ghost role.";
public string Help => $"Usage: {Command} <entity uid> <name> <description>";
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
if (args.Length != 3)
{
shell.WriteLine($"Invalid amount of arguments.\n{Help}");
return;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!EntityUid.TryParse(args[0], out var uid))
{
shell.WriteLine($"{args[0]} is not a valid entity uid.");
return;
}
if (!entityManager.TryGetEntity(uid, out var entity))
{
shell.WriteLine($"No entity found with uid {uid}");
return;
}
if (entity.TryGetComponent(out MindComponent? mind) &&
mind.HasMind)
{
shell.WriteLine($"Entity {entity.Name} with id {uid} already has a mind.");
return;
}
var name = args[1];
var description = args[2];
if (entity.EnsureComponent(out GhostTakeoverAvailableComponent takeOver))
{
shell.WriteLine($"Entity {entity.Name} with id {uid} already has a {nameof(GhostTakeoverAvailableComponent)}");
return;
}
takeOver.RoleName = name;
takeOver.RoleDescription = description;
shell.WriteLine($"Made entity {entity.Name} a ghost role.");
}
}
}

View File

@@ -0,0 +1,61 @@
#nullable enable
using Content.Server.Mind.Components;
using Content.Shared.Verbs;
using Robust.Server.Console;
using Robust.Server.GameObjects;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Server.Ghost.Roles
{
[GlobalVerb]
public class MakeGhostRoleVerb : GlobalVerb
{
public override bool RequireInteractionRange => false;
public override bool BlockedByContainers => false;
public override void GetData(IEntity user, IEntity target, VerbData data)
{
data.Visibility = VerbVisibility.Invisible;
var groupController = IoCManager.Resolve<IConGroupController>();
if (target.TryGetComponent(out MindComponent? mind) &&
mind.HasMind)
{
return;
}
if (!user.TryGetComponent(out ActorComponent? actor) ||
!groupController.CanCommand(actor.PlayerSession, "makeghostrole"))
{
return;
}
data.Visibility = VerbVisibility.Visible;
data.Text = Loc.GetString("Make Ghost Role");
data.CategoryData = VerbCategories.Debug;
}
public override void Activate(IEntity user, IEntity target)
{
var groupController = IoCManager.Resolve<IConGroupController>();
if (target.TryGetComponent(out MindComponent? mind) &&
mind.HasMind)
{
return;
}
if (!user.TryGetComponent(out ActorComponent? actor) ||
!groupController.CanCommand(actor.PlayerSession, "makeghostrole"))
{
return;
}
var ghostRoleSystem = EntitySystem.Get<GhostRoleSystem>();
ghostRoleSystem.OpenMakeGhostRoleEui(actor.PlayerSession, target.Uid);
}
}
}

View File

@@ -0,0 +1,38 @@
using Content.Server.EUI;
using Content.Shared.Eui;
using Content.Shared.Ghost.Roles;
using Robust.Shared.GameObjects;
namespace Content.Server.Ghost.Roles.UI
{
public class GhostRolesEui : BaseEui
{
public override GhostRolesEuiState GetNewState()
{
return new(EntitySystem.Get<GhostRoleSystem>().GetGhostRolesInfo());
}
public override void HandleMessage(EuiMessageBase msg)
{
base.HandleMessage(msg);
switch (msg)
{
case GhostRoleTakeoverRequestMessage req:
EntitySystem.Get<GhostRoleSystem>().Takeover(Player, req.Identifier);
break;
case GhostRoleWindowCloseMessage _:
Closed();
break;
}
}
public override void Closed()
{
base.Closed();
EntitySystem.Get<GhostRoleSystem>().CloseEui(Player);
}
}
}

View File

@@ -0,0 +1,41 @@
using Content.Server.EUI;
using Content.Shared.Eui;
using Content.Shared.Ghost.Roles;
using Robust.Shared.GameObjects;
namespace Content.Server.Ghost.Roles.UI
{
public class MakeGhostRoleEui : BaseEui
{
public MakeGhostRoleEui(EntityUid entityUid)
{
EntityUid = entityUid;
}
public EntityUid EntityUid { get; }
public override EuiStateBase GetNewState()
{
return new MakeGhostRoleEuiState(EntityUid);
}
public override void HandleMessage(EuiMessageBase msg)
{
base.HandleMessage(msg);
switch (msg)
{
case MakeGhostRoleWindowClosedMessage _:
Closed();
break;
}
}
public override void Closed()
{
base.Closed();
EntitySystem.Get<GhostRoleSystem>().CloseMakeGhostRoleEui(Player);
}
}
}