Revert "Refactor Damage to use Protoypes (#4262)"

This reverts commit 20bf5739a9.
This commit is contained in:
Silver
2021-08-24 00:50:39 -06:00
committed by Silver
parent 20bf5739a9
commit e708091518
121 changed files with 711 additions and 10237 deletions

View File

@@ -1,129 +0,0 @@
#nullable enable
using System;
using System.Linq;
using Content.Server.Administration;
using Content.Server.Commands.Observer;
using Content.Server.GameObjects.Components.GUI;
using Content.Server.GameObjects.Components.Items.Storage;
using Content.Server.Interfaces.Chat;
using Content.Server.Interfaces.GameObjects;
using Content.Server.Players;
using Content.Server.Utility;
using Content.Shared.Damage;
using Content.Shared.GameObjects.Components.Damage;
using Content.Shared.Interfaces;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.Enums;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Localization;
namespace Content.Server.Commands.Chat
{
[AnyCommand]
internal class SuicideCommand : IConsoleCommand
{
public string Command => "suicide";
public string Description => "Commits suicide";
public string Help => "The suicide command gives you a quick way out of a round while remaining in-character.\n" +
"The method varies, first it will attempt to use the held item in your active hand.\n" +
"If that fails, it will attempt to use an object in the environment.\n" +
"Finally, if neither of the above worked, you will die by biting your tongue.";
private void DealDamage(ISuicideAct suicide, IChatManager chat, IDamageableComponent damageableComponent, IEntity source, IEntity target)
{
var kind = suicide.Suicide(target, chat);
if (kind != SuicideKind.Special)
{
damageableComponent.SetDamage(kind switch
{
SuicideKind.Blunt => damageableComponent.GetDamageType("Blunt"),
SuicideKind.Slash => damageableComponent.GetDamageType("Slash"),
SuicideKind.Piercing => damageableComponent.GetDamageType("Piercing"),
SuicideKind.Heat => damageableComponent.GetDamageType("Heat"),
SuicideKind.Shock => damageableComponent.GetDamageType("Shock"),
SuicideKind.Cold => damageableComponent.GetDamageType("Cold"),
SuicideKind.Poison => damageableComponent.GetDamageType("Poison"),
SuicideKind.Radiation => damageableComponent.GetDamageType("Radiation"),
SuicideKind.Asphyxiation => damageableComponent.GetDamageType("Asphyxiation"),
SuicideKind.Bloodloss => damageableComponent.GetDamageType("Bloodloss"),
_ => damageableComponent.GetDamageType("Blunt")
},
200, source);
}
}
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var player = shell.Player as IPlayerSession;
if (player == null)
{
shell.WriteLine("You cannot run this command from the server.");
return;
}
if (player.Status != SessionStatus.InGame)
return;
var chat = IoCManager.Resolve<IChatManager>();
var owner = player.ContentData()?.Mind?.OwnedComponent?.Owner;
if (owner == null)
{
shell.WriteLine("You don't have a mind!");
return;
}
var dmgComponent = owner.GetComponent<IDamageableComponent>();
//TODO: needs to check if the mob is actually alive
//TODO: maybe set a suicided flag to prevent resurrection?
// Held item suicide
var handsComponent = owner.GetComponent<HandsComponent>();
var itemComponent = handsComponent.GetActiveHand;
if (itemComponent != null)
{
var suicide = itemComponent.Owner.GetAllComponents<ISuicideAct>().FirstOrDefault();
if (suicide != null)
{
DealDamage(suicide, chat, dmgComponent, itemComponent.Owner, owner);
return;
}
}
// Get all entities in range of the suicider
var entities = IoCManager.Resolve<IEntityLookup>().GetEntitiesInRange(owner, 1, true).ToArray();
if (entities.Length > 0)
{
foreach (var entity in entities)
{
if (entity.HasComponent<ItemComponent>())
continue;
var suicide = entity.GetAllComponents<ISuicideAct>().FirstOrDefault();
if (suicide != null)
{
DealDamage(suicide, chat, dmgComponent, entity, owner);
return;
}
}
}
// Default suicide, bite your tongue
var othersMessage = Loc.GetString("{0:theName} is attempting to bite {0:their} own tongue!", owner);
owner.PopupMessageOtherClients(othersMessage);
var selfMessage = Loc.GetString("You attempt to bite your own tongue!");
owner.PopupMessage(selfMessage);
dmgComponent.SetDamage(dmgComponent.GetDamageType("Piercing"), 200, owner);
// Prevent the player from returning to the body. Yes, this is an ugly hack.
var ghost = new Ghost(){CanReturn = false};
ghost.Execute(shell, argStr, Array.Empty<string>());
}
}
}

View File

@@ -1,228 +0,0 @@
#nullable enable
using System;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Text;
using Content.Server.Administration;
using Content.Shared.Administration;
using Content.Shared.Damage;
using Content.Shared.GameObjects.Components.Damage;
using Robust.Server.Player;
using Robust.Shared.Console;
using Robust.Shared.GameObjects;
using Robust.Shared.IoC;
using Robust.Shared.Prototypes;
namespace Content.Server.Commands
{
[AdminCommand(AdminFlags.Fun)]
class HurtCommand : IConsoleCommand
{
[Dependency]
private readonly IPrototypeManager _prototypeManager = default!;
public string Command => "hurt";
public string Description => "Ouch";
public string Help => $"Usage: {Command} <type/?> <amount> (<entity uid/_>) (<ignoreResistances>)";
private string DamageTypes()
{
var msg = new StringBuilder();
foreach (var damageGroup in _prototypeManager.EnumeratePrototypes<DamageGroupPrototype>())
{
msg.Append($"\n{damageGroup.ID}");
if (damageGroup.DamageTypes.Any())
{
msg.Append(": ");
msg.AppendJoin('|', damageGroup.DamageTypes);
}
}
return $"Damage Types:{msg}";
}
private delegate void Damage(IDamageableComponent damageable, bool ignoreResistances);
private bool TryParseEntity(IConsoleShell shell, IPlayerSession? player, string arg,
[NotNullWhen(true)] out IEntity? entity)
{
entity = null;
if (arg == "_")
{
var playerEntity = player?.AttachedEntity;
if (playerEntity == null)
{
shell.WriteLine($"You must have a player entity to use this command without specifying an entity.\n{Help}");
return false;
}
entity = playerEntity;
return true;
}
if (!EntityUid.TryParse(arg, out var entityUid))
{
shell.WriteLine($"{arg} is not a valid entity uid.\n{Help}");
return false;
}
var entityManager = IoCManager.Resolve<IEntityManager>();
if (!entityManager.TryGetEntity(entityUid, out var parsedEntity))
{
shell.WriteLine($"No entity found with uid {entityUid}");
return false;
}
entity = parsedEntity;
return true;
}
private bool TryParseDamageArgs(
IConsoleShell shell,
IPlayerSession? player,
string[] args,
[NotNullWhen(true)] out Damage? func)
{
if (!int.TryParse(args[1], out var amount))
{
shell.WriteLine($"{args[1]} is not a valid damage integer.");
func = null;
return false;
}
if (_prototypeManager.TryIndex<DamageGroupPrototype>(args[0], out var damageClass))
{
func = (damageable, ignoreResistances) =>
{
if (!damageable.DamageClasses.ContainsKey(damageClass))
{
shell.WriteLine($"Entity {damageable.Owner.Name} with id {damageable.Owner.Uid} can not be damaged with damage class {damageClass}");
return;
}
if (!damageable.ChangeDamage(damageClass, amount, ignoreResistances))
{
shell.WriteLine($"Entity {damageable.Owner.Name} with id {damageable.Owner.Uid} received no damage.");
return;
}
shell.WriteLine($"Damaged entity {damageable.Owner.Name} with id {damageable.Owner.Uid} for {amount} {damageClass} damage{(ignoreResistances ? ", ignoring resistances." : ".")}");
};
return true;
}
// Fall back to DamageType
else if (_prototypeManager.TryIndex<DamageTypePrototype>(args[0], out var damageType))
{
func = (damageable, ignoreResistances) =>
{
if (!damageable.DamageTypes.ContainsKey(damageType))
{
shell.WriteLine($"Entity {damageable.Owner.Name} with id {damageable.Owner.Uid} can not be damaged with damage type {damageType}");
return;
}
if (!damageable.ChangeDamage(damageType, amount, ignoreResistances))
{
shell.WriteLine($"Entity {damageable.Owner.Name} with id {damageable.Owner.Uid} received no damage.");
return;
}
shell.WriteLine($"Damaged entity {damageable.Owner.Name} with id {damageable.Owner.Uid} for {amount} {damageType} damage{(ignoreResistances ? ", ignoring resistances." : ".")}");
};
return true;
}
else
{
shell.WriteLine($"{args[0]} is not a valid damage class or type.");
var types = DamageTypes();
shell.WriteLine(types);
func = null;
return false;
}
}
public void Execute(IConsoleShell shell, string argStr, string[] args)
{
var player = shell.Player as IPlayerSession;
bool ignoreResistances;
IEntity entity;
Damage? damageFunc;
switch (args.Length)
{
// Check if we have enough for the dmg types to show
case var n when n > 0 && (args[0] == "?" || args[0] == "¿"):
var types = DamageTypes();
if (args[0] == "¿")
{
types = types.Replace('e', 'é');
}
shell.WriteLine(types);
return;
// Not enough args
case var n when n < 2:
shell.WriteLine($"Invalid number of arguments ({args.Length}).\n{Help}");
return;
case var n when n >= 2 && n <= 4:
if (!TryParseDamageArgs(shell, player, args, out damageFunc))
{
return;
}
var entityUid = n == 2 ? "_" : args[2];
if (!TryParseEntity(shell, player, entityUid, out var parsedEntity))
{
return;
}
entity = parsedEntity;
if (n == 4)
{
if (!bool.TryParse(args[3], out ignoreResistances))
{
shell.WriteLine($"{args[3]} is not a valid boolean value for ignoreResistances.\n{Help}");
return;
}
}
else
{
ignoreResistances = false;
}
break;
default:
shell.WriteLine($"Invalid amount of arguments ({args.Length}).\n{Help}");
return;
}
if (!entity.TryGetComponent(out IDamageableComponent? damageable))
{
shell.WriteLine($"Entity {entity.Name} with id {entity.Uid} does not have a {nameof(IDamageableComponent)}.");
return;
}
damageFunc(damageable, ignoreResistances);
}
}
}