Antag Datums. Oh also minds. (#100)

AKA: Minds and there's a stupid basic roles framework.
This commit is contained in:
Pieter-Jan Briers
2018-08-21 00:59:04 +02:00
committed by Acruid
parent e8c4dc9a34
commit cd4442b81e
9 changed files with 523 additions and 8 deletions

View File

@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Content.Server.Players;
using SS14.Server.Interfaces.Console;
using SS14.Server.Interfaces.Player;
using SS14.Shared.Interfaces.Reflection;
using SS14.Shared.IoC;
using SS14.Shared.Network;
namespace Content.Server.Mobs
{
public class MindInfoCommand : IClientCommand
{
public string Command => "mindinfo";
public string Description => "Lists info for the mind of a specific player.";
public string Help => "mindinfo <session ID>";
public void Execute(IConsoleShell shell, IPlayerSession player, string[] args)
{
if (args.Length != 1)
{
shell.SendText(player, "Expected exactly 1 argument.");
return;
}
var mgr = IoCManager.Resolve<IPlayerManager>();
if (mgr.TryGetPlayerData(new NetSessionId(args[0]), out var data))
{
var mind = data.ContentData().Mind;
var builder = new StringBuilder();
builder.AppendFormat("player: {0}, mob: {1}\nroles: ", mind.SessionId, mind.CurrentMob?.Owner?.Uid);
foreach (var role in mind.AllRoles)
{
builder.AppendFormat("{0} ", role.Name);
}
shell.SendText(player, builder.ToString());
}
else
{
shell.SendText(player, "Can't find that mind");
}
}
}
public class AddRoleCommand : IClientCommand
{
public string Command => "addrole";
public string Description => "Adds a role to a player's mind.";
public string Help => "addrole <session ID> <Role Type>\nThat role type is the actual C# type name.";
public void Execute(IConsoleShell shell, IPlayerSession player, string[] args)
{
if (args.Length != 2)
{
shell.SendText(player, "Expected exactly 2 arguments.");
return;
}
var mgr = IoCManager.Resolve<IPlayerManager>();
if (mgr.TryGetPlayerData(new NetSessionId(args[0]), out var data))
{
var mind = data.ContentData().Mind;
var refl = IoCManager.Resolve<IReflectionManager>();
var type = refl.LooseGetType(args[1]);
mind.AddRole(type);
}
else
{
shell.SendText(player, "Can't find that mind");
}
}
}
public class RemoveRoleCommand : IClientCommand
{
public string Command => "rmrole";
public string Description => "Removes a role from a player's mind.";
public string Help => "rmrole <session ID> <Role Type>\nThat role type is the actual C# type name.";
public void Execute(IConsoleShell shell, IPlayerSession player, string[] args)
{
if (args.Length != 2)
{
shell.SendText(player, "Expected exactly 2 arguments.");
return;
}
var mgr = IoCManager.Resolve<IPlayerManager>();
if (mgr.TryGetPlayerData(new NetSessionId(args[0]), out var data))
{
var mind = data.ContentData().Mind;
var refl = IoCManager.Resolve<IReflectionManager>();
var type = refl.LooseGetType(args[1]);
mind.RemoveRole(type);
}
else
{
shell.SendText(player, "Can't find that mind");
}
}
}
}

199
Content.Server/Mobs/Mind.cs Normal file
View File

@@ -0,0 +1,199 @@
using System;
using System.Collections.Generic;
using Content.Server.GameObjects.Components.Mobs;
using SS14.Server.Interfaces.Player;
using SS14.Shared.Interfaces.GameObjects;
using SS14.Shared.IoC;
using SS14.Shared.Network;
namespace Content.Server.Mobs
{
/// <summary>
/// A mind represents the IC "mind" of a player. Stores roles currently.
/// </summary>
/// <remarks>
/// Think of it like this: if a player is supposed to have their memories,
/// their mind follows along.
///
/// Things such as respawning do not follow, because you're a new character.
/// Getting borged, cloned, turned into a catbeast, etc... will keep it following you.
/// </remarks>
public sealed class Mind
{
private readonly Dictionary<Type, Role> _roles = new Dictionary<Type, Role>();
/// <summary>
/// Creates the new mind attached to a specific player session.
/// </summary>
/// <param name="sessionId">The session ID of the owning player.</param>
public Mind(NetSessionId sessionId)
{
SessionId = sessionId;
}
// TODO: This session should be able to be changed, probably.
/// <summary>
/// The session ID of the player owning this mind.
/// </summary>
public NetSessionId SessionId { get; }
/// <summary>
/// The component currently owned by this mind.
/// Can be null.
/// </summary>
public MindComponent CurrentMob { get; private set; }
/// <summary>
/// The entity currently owned by this mind.
/// Can be null.
/// </summary>
public IEntity CurrentEntity => CurrentMob?.Owner;
/// <summary>
/// An enumerable over all the roles this mind has.
/// </summary>
public IEnumerable<Role> AllRoles => _roles.Values;
/// <summary>
/// The session of the player owning this mind.
/// Can be null, in which case the player is currently not logged in.
/// </summary>
public IPlayerSession Session
{
get
{
var playerMgr = IoCManager.Resolve<IPlayerManager>();
playerMgr.TryGetSessionById(SessionId, out var ret);
return ret;
}
}
/// <summary>
/// Gives this mind a new role.
/// </summary>
/// <typeparam name="T">The type of the role to give.</typeparam>
/// <returns>The instance of the role.</returns>
/// <exception cref="ArgumentException">
/// Thrown if we already have a role with this type.
/// </exception>
public T AddRole<T>() where T : Role
{
return (T)AddRole(typeof(T));
}
/// <summary>
/// Gives this mind a new role.
/// </summary>
/// <param name="t">The type of the role to give.</param>
/// <returns>The instance of the role.</returns>
/// <exception cref="ArgumentException">
/// Thrown if we already have a role with this type.
/// </exception>
public Role AddRole(Type t)
{
if (_roles.ContainsKey(t))
{
throw new ArgumentException($"We already have this role: {t}");
}
var role = (Role)Activator.CreateInstance(t, this);
_roles[t] = role;
role.Greet();
return role;
}
/// <summary>
/// Removes a role from this mind.
/// </summary>
/// <typeparam name="T">The type of the role to remove.</typeparam>
/// <exception cref="ArgumentException">
/// Thrown if we do not have this role.
/// </exception>
public void RemoveRole<T>() where T : Role
{
RemoveRole(typeof(T));
}
/// <summary>
/// Removes a role from this mind.
/// </summary>
/// <param name="t">The type of the role to remove.</param>
/// <exception cref="ArgumentException">
/// Thrown if we do not have this role.
/// </exception>
public void RemoveRole(Type t)
{
if (!_roles.ContainsKey(t))
{
throw new ArgumentException($"We do not have this role: {t}");
}
// This can definitely get more complex removal hooks later,
// when we need it.
_roles.Remove(t);
}
/// <summary>
/// Gets a role of a certain type.
/// </summary>
/// <typeparam name="T">The type of the role to get.</typeparam>
/// <returns>The role's instance.</returns>
/// <exception cref="KeyNotFoundException">
/// Thrown if we do not have a role of this type.
/// </exception>
public T GetRole<T>() where T : Role
{
return (T)_roles[typeof(T)];
}
/// <summary>
/// Gets a role of a certain type.
/// </summary>
/// <param name="t">The type of the role to get.</param>
/// <returns>The role's instance.</returns>
/// <exception cref="KeyNotFoundException">
/// Thrown if we do not have a role of this type.
/// </exception>
public Role GetRole(Type t)
{
return _roles[t];
}
/// <summary>
/// Transfer this mind's control over to a new entity.
/// </summary>
/// <param name="entity">
/// The entity to control.
/// Can be null, in which case it will simply detach the mind from any entity.
/// </param>
/// <exception cref="ArgumentException">
/// Thrown if <paramref name="entity"/> is already owned by another mind.
/// </exception>
public void TransferTo(IEntity entity)
{
MindComponent component = null;
if (entity != null)
{
if (!entity.TryGetComponent(out component))
{
component = entity.AddComponent<MindComponent>();
}
else if (component.HasMind)
{
// TODO: Kick them out, maybe?
throw new ArgumentException("That entity already has a mind.", nameof(entity));
}
}
CurrentMob?.InternalEjectMind();
CurrentMob = component;
CurrentMob?.InternalAssignMind(this);
// Player is CURRENTLY connected.
if (Session != null && CurrentMob != null)
{
Session.AttachToEntity(entity);
}
}
}
}

View File

@@ -0,0 +1,36 @@
// Hey look,
// Antag Datums.
namespace Content.Server.Mobs
{
/// <summary>
/// The Role is a basic building block for,
/// well, IC roles.
/// This can be anything and is not necessarily limited to antagonists.
/// </summary>
public abstract class Role
{
/// <summary>
/// The mind owning this role instance.
/// </summary>
public Mind Mind { get; }
/// <summary>
/// A friendly name for this role type.
/// </summary>
public abstract string Name { get; }
protected Role(Mind mind)
{
Mind = mind;
}
/// <summary>
/// Called when a mind (player) first gets this role, to greet them.
/// </summary>
public virtual void Greet()
{
}
}
}

View File

@@ -0,0 +1,24 @@
using SS14.Server.Interfaces.Chat;
using SS14.Shared.Console;
using SS14.Shared.IoC;
namespace Content.Server.Mobs.Roles
{
public sealed class Traitor : Role
{
public Traitor(Mind mind) : base(mind)
{
}
public override string Name => "Traitor";
public override void Greet()
{
base.Greet();
var chat = IoCManager.Resolve<IChatManager>();
chat.DispatchMessage(Mind.Session.ConnectedClient, ChatChannel.Server,
"You're a traitor. Go fuck something up. Or something. I don't care to be honest.");
}
}
}