Add invulnerable damage flag, godmode and damage flag commands (#1949)
* Add invulnerable damage flag and commands * Add serialization for damage flags * Add godmode command
This commit is contained in:
214
Content.Server/GameObjects/Components/Damage/DamageCommands.cs
Normal file
214
Content.Server/GameObjects/Components/Damage/DamageCommands.cs
Normal file
@@ -0,0 +1,214 @@
|
|||||||
|
#nullable enable
|
||||||
|
using System;
|
||||||
|
using System.Diagnostics.CodeAnalysis;
|
||||||
|
using Content.Server.GameObjects.Components.Atmos;
|
||||||
|
using Content.Shared.GameObjects.Components.Damage;
|
||||||
|
using Robust.Server.Interfaces.Console;
|
||||||
|
using Robust.Server.Interfaces.Player;
|
||||||
|
using Robust.Shared.GameObjects;
|
||||||
|
using Robust.Shared.Interfaces.GameObjects;
|
||||||
|
using Robust.Shared.IoC;
|
||||||
|
|
||||||
|
namespace Content.Server.GameObjects.Components.Damage
|
||||||
|
{
|
||||||
|
public abstract class DamageFlagCommand : IClientCommand
|
||||||
|
{
|
||||||
|
public abstract string Command { get; }
|
||||||
|
public abstract string Description { get; }
|
||||||
|
public abstract string Help { get; }
|
||||||
|
|
||||||
|
public abstract void Execute(IConsoleShell shell, IPlayerSession? player, string[] args);
|
||||||
|
|
||||||
|
public bool TryGetEntity(
|
||||||
|
IConsoleShell shell,
|
||||||
|
IPlayerSession? player,
|
||||||
|
string[] args,
|
||||||
|
bool adding,
|
||||||
|
[NotNullWhen(true)] out IEntity? entity,
|
||||||
|
out DamageFlag flag,
|
||||||
|
[NotNullWhen(true)] out IDamageableComponent? damageable)
|
||||||
|
{
|
||||||
|
entity = null;
|
||||||
|
flag = DamageFlag.None;
|
||||||
|
damageable = null;
|
||||||
|
|
||||||
|
IEntity? parsedEntity;
|
||||||
|
DamageFlag parsedFlag;
|
||||||
|
IDamageableComponent? parsedDamageable;
|
||||||
|
|
||||||
|
switch (args.Length)
|
||||||
|
{
|
||||||
|
case 1:
|
||||||
|
{
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
shell.SendText(player, "An entity needs to be specified when the command isn't used by a player.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.AttachedEntity == null)
|
||||||
|
{
|
||||||
|
shell.SendText(player, "An entity needs to be specified when you aren't attached to an entity.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Enum.TryParse(args[0], true, out parsedFlag))
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"{args[0]} is not a valid damage flag.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
parsedEntity = player.AttachedEntity;
|
||||||
|
flag = parsedFlag;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case 2:
|
||||||
|
{
|
||||||
|
if (!EntityUid.TryParse(args[0], out var id))
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"{args[0]} isn't a valid entity id.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||||
|
if (!entityManager.TryGetEntity(id, out parsedEntity))
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"No entity found with id {id}.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!Enum.TryParse(args[1], true, out parsedFlag))
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"{args[1]} is not a valid damage flag.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
shell.SendText(player, Help);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!parsedEntity.TryGetComponent(out parsedDamageable))
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"Entity {parsedEntity.Name} doesn't have a {nameof(IDamageableComponent)}");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (parsedDamageable.HasFlag(parsedFlag) && adding)
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"Entity {parsedEntity.Name} already has damage flag {parsedFlag}.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
else if (!parsedDamageable.HasFlag(parsedFlag) && !adding)
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"Entity {parsedEntity.Name} doesn't have damage flag {parsedFlag}.");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
entity = parsedEntity;
|
||||||
|
flag = parsedFlag;
|
||||||
|
damageable = parsedDamageable;
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class AddDamageFlagCommand : DamageFlagCommand
|
||||||
|
{
|
||||||
|
public override string Command => "adddamageflag";
|
||||||
|
public override string Description => "Adds a damage flag to your entity or another.";
|
||||||
|
public override string Help => $"Usage: {Command} <flag> / {Command} <entityUid> <flag>";
|
||||||
|
|
||||||
|
public override void Execute(IConsoleShell shell, IPlayerSession? player, string[] args)
|
||||||
|
{
|
||||||
|
if (!TryGetEntity(shell, player, args, true, out var entity, out var flag, out var damageable))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
damageable.AddFlag(flag);
|
||||||
|
shell.SendText(player, $"Added damage flag {flag} to entity {entity.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class RemoveDamageFlagCommand : DamageFlagCommand
|
||||||
|
{
|
||||||
|
public override string Command => "removedamageflag";
|
||||||
|
public override string Description => "Removes a damage flag from your entity or another.";
|
||||||
|
public override string Help => $"Usage: {Command} <flag> / {Command} <entityUid> <flag>";
|
||||||
|
|
||||||
|
public override void Execute(IConsoleShell shell, IPlayerSession? player, string[] args)
|
||||||
|
{
|
||||||
|
if (!TryGetEntity(shell, player, args, false, out var entity, out var flag, out var damageable))
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
damageable.RemoveFlag(flag);
|
||||||
|
shell.SendText(player, $"Removed damage flag {flag} from entity {entity.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public class GodModeCommand : IClientCommand
|
||||||
|
{
|
||||||
|
public string Command => "godmode";
|
||||||
|
public string Description => "Makes your entity or another invulnerable to almost anything. May have irreversible changes.";
|
||||||
|
public string Help => $"Usage: {Command} / {Command} <entityUid>";
|
||||||
|
public void Execute(IConsoleShell shell, IPlayerSession? player, string[] args)
|
||||||
|
{
|
||||||
|
IEntity entity;
|
||||||
|
|
||||||
|
switch (args.Length)
|
||||||
|
{
|
||||||
|
case 0:
|
||||||
|
if (player == null)
|
||||||
|
{
|
||||||
|
shell.SendText(player, "An entity needs to be specified when the command isn't used by a player.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (player.AttachedEntity == null)
|
||||||
|
{
|
||||||
|
shell.SendText(player, "An entity needs to be specified when you aren't attached to an entity.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
entity = player.AttachedEntity;
|
||||||
|
break;
|
||||||
|
case 1:
|
||||||
|
if (!EntityUid.TryParse(args[0], out var id))
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"{args[0]} isn't a valid entity id.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var entityManager = IoCManager.Resolve<IEntityManager>();
|
||||||
|
if (!entityManager.TryGetEntity(id, out var parsedEntity))
|
||||||
|
{
|
||||||
|
shell.SendText(player, $"No entity found with id {id}.");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
entity = parsedEntity;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
shell.SendText(player, Help);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entity.HasComponent<MovedByPressureComponent>())
|
||||||
|
{
|
||||||
|
entity.RemoveComponent<MovedByPressureComponent>();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (entity.TryGetComponent(out IDamageableComponent? damageable))
|
||||||
|
{
|
||||||
|
damageable.AddFlag(DamageFlag.Invulnerable);
|
||||||
|
}
|
||||||
|
|
||||||
|
shell.SendText(player, $"Enabled godmode for entity {entity.Name}");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
13
Content.Shared/GameObjects/Components/Damage/DamageFlag.cs
Normal file
13
Content.Shared/GameObjects/Components/Damage/DamageFlag.cs
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
using System;
|
||||||
|
using Robust.Shared.Serialization;
|
||||||
|
|
||||||
|
namespace Content.Shared.GameObjects.Components.Damage
|
||||||
|
{
|
||||||
|
[Flags]
|
||||||
|
[Serializable, NetSerializable]
|
||||||
|
public enum DamageFlag
|
||||||
|
{
|
||||||
|
None = 0,
|
||||||
|
Invulnerable = 1 << 0
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -27,6 +27,8 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
|
|
||||||
private DamageState _currentDamageState;
|
private DamageState _currentDamageState;
|
||||||
|
|
||||||
|
private DamageFlag _flags;
|
||||||
|
|
||||||
public event Action<HealthChangedEventArgs>? HealthChangedEvent;
|
public event Action<HealthChangedEventArgs>? HealthChangedEvent;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@@ -79,6 +81,8 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
{
|
{
|
||||||
EnterState(value);
|
EnterState(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Dirty();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -88,6 +92,36 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
|
|
||||||
public IReadOnlyDictionary<DamageType, int> DamageTypes => Damage.DamageTypes;
|
public IReadOnlyDictionary<DamageType, int> DamageTypes => Damage.DamageTypes;
|
||||||
|
|
||||||
|
public DamageFlag Flags
|
||||||
|
{
|
||||||
|
get => _flags;
|
||||||
|
private set
|
||||||
|
{
|
||||||
|
if (_flags == value)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
_flags = value;
|
||||||
|
Dirty();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void AddFlag(DamageFlag flag)
|
||||||
|
{
|
||||||
|
Flags |= flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
public bool HasFlag(DamageFlag flag)
|
||||||
|
{
|
||||||
|
return Flags.HasFlag(flag);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void RemoveFlag(DamageFlag flag)
|
||||||
|
{
|
||||||
|
Flags &= ~flag;
|
||||||
|
}
|
||||||
|
|
||||||
public override void ExposeData(ObjectSerializer serializer)
|
public override void ExposeData(ObjectSerializer serializer)
|
||||||
{
|
{
|
||||||
base.ExposeData(serializer);
|
base.ExposeData(serializer);
|
||||||
@@ -104,6 +138,35 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
t => DeadThreshold = t == -1 ? (int?) null : t,
|
t => DeadThreshold = t == -1 ? (int?) null : t,
|
||||||
() => DeadThreshold ?? -1);
|
() => DeadThreshold ?? -1);
|
||||||
|
|
||||||
|
serializer.DataReadWriteFunction(
|
||||||
|
"flags",
|
||||||
|
new List<DamageFlag>(),
|
||||||
|
flags =>
|
||||||
|
{
|
||||||
|
var result = DamageFlag.None;
|
||||||
|
|
||||||
|
foreach (var flag in flags)
|
||||||
|
{
|
||||||
|
result |= flag;
|
||||||
|
}
|
||||||
|
|
||||||
|
Flags = result;
|
||||||
|
},
|
||||||
|
() =>
|
||||||
|
{
|
||||||
|
var writeFlags = new List<DamageFlag>();
|
||||||
|
|
||||||
|
foreach (var flag in (DamageFlag[]) Enum.GetValues(typeof(DamageFlag)))
|
||||||
|
{
|
||||||
|
if ((Flags & flag) == flag)
|
||||||
|
{
|
||||||
|
writeFlags.Add(flag);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return writeFlags;
|
||||||
|
});
|
||||||
|
|
||||||
if (serializer.Reading)
|
if (serializer.Reading)
|
||||||
{
|
{
|
||||||
// Doesn't write to file, TODO?
|
// Doesn't write to file, TODO?
|
||||||
@@ -151,6 +214,11 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
IEntity? source = null,
|
IEntity? source = null,
|
||||||
HealthChangeParams? extraParams = null)
|
HealthChangeParams? extraParams = null)
|
||||||
{
|
{
|
||||||
|
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (Damage.SupportsDamageType(type))
|
if (Damage.SupportsDamageType(type))
|
||||||
{
|
{
|
||||||
var finalDamage = amount;
|
var finalDamage = amount;
|
||||||
@@ -171,6 +239,11 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
IEntity? source = null,
|
IEntity? source = null,
|
||||||
HealthChangeParams? extraParams = null)
|
HealthChangeParams? extraParams = null)
|
||||||
{
|
{
|
||||||
|
if (amount > 0 && HasFlag(DamageFlag.Invulnerable))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (Damage.SupportsDamageClass(@class))
|
if (Damage.SupportsDamageClass(@class))
|
||||||
{
|
{
|
||||||
var types = @class.ToTypes();
|
var types = @class.ToTypes();
|
||||||
@@ -250,6 +323,11 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
public bool SetDamage(DamageType type, int newValue, IEntity? source = null,
|
public bool SetDamage(DamageType type, int newValue, IEntity? source = null,
|
||||||
HealthChangeParams? extraParams = null)
|
HealthChangeParams? extraParams = null)
|
||||||
{
|
{
|
||||||
|
if (newValue >= TotalDamage && HasFlag(DamageFlag.Invulnerable))
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
if (Damage.SupportsDamageType(type))
|
if (Damage.SupportsDamageType(type))
|
||||||
{
|
{
|
||||||
Damage.SetDamageValue(type, newValue);
|
Damage.SetDamageValue(type, newValue);
|
||||||
|
|||||||
@@ -45,6 +45,30 @@ namespace Content.Shared.GameObjects.Components.Damage
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
IReadOnlyDictionary<DamageType, int> DamageTypes { get; }
|
IReadOnlyDictionary<DamageType, int> DamageTypes { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The damage flags on this component.
|
||||||
|
/// </summary>
|
||||||
|
DamageFlag Flags { get; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Adds a flag to this component.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flag">The flag to add.</param>
|
||||||
|
void AddFlag(DamageFlag flag);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Checks whether or not this component has a specific flag.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flag">The flag to check for.</param>
|
||||||
|
/// <returns>True if it has the flag, false otherwise.</returns>
|
||||||
|
bool HasFlag(DamageFlag flag);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Removes a flag from this component.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="flag">The flag to remove.</param>
|
||||||
|
void RemoveFlag(DamageFlag flag);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets the amount of damage of a type.
|
/// Gets the amount of damage of a type.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -102,6 +102,9 @@
|
|||||||
- readyall
|
- readyall
|
||||||
- factions
|
- factions
|
||||||
- signallink
|
- signallink
|
||||||
|
- adddamageflag
|
||||||
|
- removedamageflag
|
||||||
|
- godmode
|
||||||
CanViewVar: true
|
CanViewVar: true
|
||||||
CanAdminPlace: true
|
CanAdminPlace: true
|
||||||
CanAdminMenu: true
|
CanAdminMenu: true
|
||||||
@@ -197,6 +200,9 @@
|
|||||||
- readyall
|
- readyall
|
||||||
- factions
|
- factions
|
||||||
- signallink
|
- signallink
|
||||||
|
- adddamageflag
|
||||||
|
- removedamageflag
|
||||||
|
- godmode
|
||||||
CanViewVar: true
|
CanViewVar: true
|
||||||
CanAdminPlace: true
|
CanAdminPlace: true
|
||||||
CanScript: true
|
CanScript: true
|
||||||
|
|||||||
Reference in New Issue
Block a user