Make HitShape mandatory for damaging actors and refactor warheads.

* Adds support for linking Armor traits to HitShapes.
* Adds spread support to TargetDamageWarhead
* Removes ring-damage support from HealthPercentageDamage
* Removes IsValidAgainst check from DoImpact(Actor victim...) overload
  and instead lets warheads perform the check beforehand
  (to avoid HitShape look-ups on invalid targets).
* Reduces duplication and improves readability of Warhead implementations
This commit is contained in:
reaperrr
2018-10-25 21:15:34 +00:00
committed by reaperrr
parent e2050dfdc9
commit f18ce8cfda
8 changed files with 123 additions and 89 deletions

View File

@@ -924,6 +924,7 @@
<Compile Include="UpdateRules\Rules\20180923\RenameEditorTilesetFilter.cs" /> <Compile Include="UpdateRules\Rules\20180923\RenameEditorTilesetFilter.cs" />
<Compile Include="UpdateRules\Rules\20180923\MergeRearmAndRepairAnimation.cs" /> <Compile Include="UpdateRules\Rules\20180923\MergeRearmAndRepairAnimation.cs" />
<Compile Include="UpdateRules\Rules\20180923\LowPowerSlowdownToModifier.cs" /> <Compile Include="UpdateRules\Rules\20180923\LowPowerSlowdownToModifier.cs" />
<Compile Include="UpdateRules\Rules\20180923\RemoveHealthPercentageRing.cs" />
<Compile Include="Traits\Player\PlayerResources.cs" /> <Compile Include="Traits\Player\PlayerResources.cs" />
<Compile Include="UtilityCommands\DumpSequenceSheetsCommand.cs" /> <Compile Include="UtilityCommands\DumpSequenceSheetsCommand.cs" />
<Compile Include="Traits\Render\WithBuildingRepairDecoration.cs" /> <Compile Include="Traits\Render\WithBuildingRepairDecoration.cs" />

View File

@@ -11,6 +11,9 @@
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
// Type tag for armor type bits
public class ArmorType { }
[Desc("Used to define weapon efficiency modifiers with different percentages per Type.")] [Desc("Used to define weapon efficiency modifiers with different percentages per Type.")]
public class ArmorInfo : ConditionalTraitInfo public class ArmorInfo : ConditionalTraitInfo
{ {

View File

@@ -12,6 +12,7 @@
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.Mods.Common.HitShapes; using OpenRA.Mods.Common.HitShapes;
using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
@@ -25,6 +26,10 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Create a targetable position at the center of each occupied cell. Stacks with TargetableOffsets.")] [Desc("Create a targetable position at the center of each occupied cell. Stacks with TargetableOffsets.")]
public readonly bool UseTargetableCellsOffsets = false; public readonly bool UseTargetableCellsOffsets = false;
[Desc("Defines which Armor types apply when the actor receives damage to this HitShape.",
"If none specified, all armor types the actor has are valid.")]
public readonly BitSet<ArmorType> ArmorTypes = default(BitSet<ArmorType>);
[FieldLoader.LoadUsing("LoadShape")] [FieldLoader.LoadUsing("LoadShape")]
public readonly IHitShape Type; public readonly IHitShape Type;

View File

@@ -0,0 +1,45 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
namespace OpenRA.Mods.Common.UpdateRules.Rules
{
public class RemoveHealthPercentageRing : UpdateRule
{
public override string Name { get { return "Remove ring support from HealthPercentageDamage warheads' Spread"; } }
public override string Description
{
get
{
return "Setting a second value in this warheads' Spread to define a 'ring' is no longer supported.";
}
}
public override IEnumerable<string> UpdateWeaponNode(ModData modData, MiniYamlNode weaponNode)
{
foreach (var node in weaponNode.ChildrenMatching("Warhead"))
{
if (node.NodeValue<string>() == "HealthPercentageDamage")
{
foreach (var spreadNode in node.ChildrenMatching("Spread"))
{
var oldValue = spreadNode.NodeValue<string[]>();
if (oldValue.Length > 1)
spreadNode.ReplaceValue(oldValue[0]);
}
}
}
yield break;
}
}
}

View File

@@ -30,20 +30,25 @@ namespace OpenRA.Mods.Common.Warheads
public override bool IsValidAgainst(Actor victim, Actor firedBy) public override bool IsValidAgainst(Actor victim, Actor firedBy)
{ {
// Cannot be damaged without a Health trait
if (!victim.Info.HasTraitInfo<HealthInfo>())
return false;
if (Damage < 0 && victim.GetDamageState() == DamageState.Undamaged) if (Damage < 0 && victim.GetDamageState() == DamageState.Undamaged)
return false; return false;
return base.IsValidAgainst(victim, firedBy); return base.IsValidAgainst(victim, firedBy);
} }
public int DamageVersus(Actor victim) public int DamageVersus(Actor victim, HitShapeInfo shapeInfo)
{ {
// If no Versus values are defined, DamageVersus would return 100 anyway, so we might as well do that early. // If no Versus values are defined, DamageVersus would return 100 anyway, so we might as well do that early.
if (Versus.Count == 0) if (Versus.Count == 0)
return 100; return 100;
var armor = victim.TraitsImplementing<Armor>() var armor = victim.TraitsImplementing<Armor>()
.Where(a => !a.IsTraitDisabled && a.Info.Type != null && Versus.ContainsKey(a.Info.Type)) .Where(a => !a.IsTraitDisabled && a.Info.Type != null && Versus.ContainsKey(a.Info.Type) &&
(shapeInfo.ArmorTypes == default(BitSet<ArmorType>) || shapeInfo.ArmorTypes.Contains(a.Info.Type)))
.Select(a => Versus[a.Info.Type]); .Select(a => Versus[a.Info.Type]);
return Util.ApplyPercentageModifiers(100, armor); return Util.ApplyPercentageModifiers(100, armor);
@@ -51,22 +56,28 @@ namespace OpenRA.Mods.Common.Warheads
public override void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers) public override void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers)
{ {
// Used by traits that damage a single actor, rather than a position // Used by traits or warheads that damage a single actor, rather than a position
if (target.Type == TargetType.Actor) if (target.Type == TargetType.Actor)
DoImpact(target.Actor, firedBy, damageModifiers); {
var victim = target.Actor;
if (!IsValidAgainst(victim, firedBy))
return;
var closestActiveShape = victim.TraitsImplementing<HitShape>().Where(Exts.IsTraitEnabled)
.MinByOrDefault(t => t.Info.Type.DistanceFromEdge(victim.CenterPosition, victim));
// Cannot be damaged without an active HitShape
if (closestActiveShape == null)
return;
var damage = Util.ApplyPercentageModifiers(Damage, damageModifiers.Append(DamageVersus(victim, closestActiveShape.Info)));
victim.InflictDamage(firedBy, new Damage(damage, DamageTypes));
}
else if (target.Type != TargetType.Invalid) else if (target.Type != TargetType.Invalid)
DoImpact(target.CenterPosition, firedBy, damageModifiers); DoImpact(target.CenterPosition, firedBy, damageModifiers);
} }
public abstract void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers); public abstract void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers);
public virtual void DoImpact(Actor victim, Actor firedBy, IEnumerable<int> damageModifiers)
{
if (!IsValidAgainst(victim, firedBy))
return;
var damage = Util.ApplyPercentageModifiers(Damage, damageModifiers.Append(DamageVersus(victim)));
victim.InflictDamage(firedBy, new Damage(damage, DamageTypes));
}
} }
} }

View File

@@ -10,45 +10,17 @@
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads namespace OpenRA.Mods.Common.Warheads
{ {
public class HealthPercentageDamageWarhead : DamageWarhead public class HealthPercentageDamageWarhead : TargetDamageWarhead
{ {
[Desc("Size of the area. Damage will be applied to this area.", "If two spreads are defined, the area of effect is a ring, where the second value is the inner radius.")] protected override void InflictDamage(Actor victim, Actor firedBy, HitShapeInfo hitshapeInfo, IEnumerable<int> damageModifiers)
public readonly WDist[] Spread = { new WDist(43) };
public override void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers)
{ {
var world = firedBy.World; var healthInfo = victim.Info.TraitInfo<HealthInfo>();
var damage = Util.ApplyPercentageModifiers(healthInfo.HP, damageModifiers.Append(Damage, DamageVersus(victim, hitshapeInfo)));
var debugVis = world.WorldActor.TraitOrDefault<DebugVisualizations>();
if (debugVis != null && debugVis.CombatGeometry)
world.WorldActor.Trait<WarheadDebugOverlay>().AddImpact(pos, Spread, DebugOverlayColor);
var range = Spread[0];
var hitActors = world.FindActorsInCircle(pos, range);
if (Spread.Length > 1 && Spread[1].Length > 0)
hitActors = hitActors.Except(world.FindActorsInCircle(pos, Spread[1]));
foreach (var victim in hitActors)
DoImpact(victim, firedBy, damageModifiers);
}
public override void DoImpact(Actor victim, Actor firedBy, IEnumerable<int> damageModifiers)
{
if (!IsValidAgainst(victim, firedBy))
return;
var healthInfo = victim.Info.TraitInfoOrDefault<IHealthInfo>();
if (healthInfo == null)
return;
// Damage is measured as a percentage of the target health
var damage = Util.ApplyPercentageModifiers(healthInfo.MaxHP, damageModifiers.Append(Damage, DamageVersus(victim)));
victim.InflictDamage(firedBy, new Damage(damage, DamageTypes)); victim.InflictDamage(firedBy, new Damage(damage, DamageTypes));
} }
} }

View File

@@ -13,6 +13,7 @@ using System.Collections.Generic;
using System.Linq; using System.Linq;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads namespace OpenRA.Mods.Common.Warheads
@@ -45,30 +46,27 @@ namespace OpenRA.Mods.Common.Warheads
public override void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers) public override void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers)
{ {
var world = firedBy.World; var debugVis = firedBy.World.WorldActor.TraitOrDefault<DebugVisualizations>();
var debugVis = world.WorldActor.TraitOrDefault<DebugVisualizations>();
if (debugVis != null && debugVis.CombatGeometry) if (debugVis != null && debugVis.CombatGeometry)
world.WorldActor.Trait<WarheadDebugOverlay>().AddImpact(pos, Range, DebugOverlayColor); firedBy.World.WorldActor.Trait<WarheadDebugOverlay>().AddImpact(pos, Range, DebugOverlayColor);
var hitActors = world.FindActorsOnCircle(pos, Range[Range.Length - 1]); foreach (var victim in firedBy.World.FindActorsOnCircle(pos, Range[Range.Length - 1]))
foreach (var victim in hitActors)
{ {
// Cannot be damaged without a Health trait if (!IsValidAgainst(victim, firedBy))
var healthInfo = victim.Info.TraitInfoOrDefault<IHealthInfo>();
if (healthInfo == null)
continue; continue;
var closestActiveShape = victim.TraitsImplementing<HitShape>()
.Where(Exts.IsTraitEnabled)
.Select(s => Pair.New(s, s.Info.Type.DistanceFromEdge(pos, victim)))
.MinByOrDefault(s => s.Second);
// Cannot be damaged without an active HitShape // Cannot be damaged without an active HitShape
var activeShapes = victim.TraitsImplementing<HitShape>().Where(Exts.IsTraitEnabled); if (closestActiveShape.First == null)
if (!activeShapes.Any())
continue; continue;
var distance = activeShapes.Min(t => t.Info.Type.DistanceFromEdge(pos, victim)); var localModifiers = damageModifiers.Append(GetDamageFalloff(closestActiveShape.Second.Length));
var localModifiers = damageModifiers.Append(GetDamageFalloff(distance.Length)); var damage = Util.ApplyPercentageModifiers(Damage, localModifiers.Append(DamageVersus(victim, closestActiveShape.First.Info)));
victim.InflictDamage(firedBy, new Damage(damage, DamageTypes));
DoImpact(victim, firedBy, localModifiers);
} }
} }

View File

@@ -10,50 +10,49 @@
#endregion #endregion
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits; using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads namespace OpenRA.Mods.Common.Warheads
{ {
public class TargetDamageWarhead : DamageWarhead public class TargetDamageWarhead : DamageWarhead
{ {
public override void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers) [Desc("Damage will be applied to actors in this area. A value of zero means only targeted actor will be damaged.")]
{ public readonly WDist Spread = WDist.Zero;
// Damages a single actor, rather than a position. Only support by InstantHit for now.
// TODO: Add support for 'area of damage'
if (target.Type == TargetType.Actor)
DoImpact(target.Actor, firedBy, damageModifiers);
}
public override void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers) public override void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers)
{ {
// For now this only displays debug overlay if (Spread == WDist.Zero)
// TODO: Add support for 'area of effect' / multiple targets
var world = firedBy.World;
var debugOverlayRange = new[] { WDist.Zero, new WDist(128) };
var debugVis = world.WorldActor.TraitOrDefault<DebugVisualizations>();
if (debugVis != null && debugVis.CombatGeometry)
world.WorldActor.Trait<WarheadDebugOverlay>().AddImpact(pos, debugOverlayRange, DebugOverlayColor);
}
public override void DoImpact(Actor victim, Actor firedBy, IEnumerable<int> damageModifiers)
{
if (!IsValidAgainst(victim, firedBy))
return; return;
var damage = Util.ApplyPercentageModifiers(Damage, damageModifiers.Append(DamageVersus(victim))); var debugVis = firedBy.World.WorldActor.TraitOrDefault<DebugVisualizations>();
victim.InflictDamage(firedBy, new Damage(damage, DamageTypes)); if (debugVis != null && debugVis.CombatGeometry)
firedBy.World.WorldActor.Trait<WarheadDebugOverlay>().AddImpact(pos, new[] { WDist.Zero, Spread }, DebugOverlayColor);
var world = firedBy.World; foreach (var victim in firedBy.World.FindActorsOnCircle(pos, Spread))
if (world.LocalPlayer != null)
{ {
var debugOverlayRange = new[] { WDist.Zero, new WDist(128) }; if (!IsValidAgainst(victim, firedBy))
continue;
var debugVis = world.WorldActor.TraitOrDefault<DebugVisualizations>(); var closestActiveShape = victim.TraitsImplementing<HitShape>()
if (debugVis != null && debugVis.CombatGeometry) .Where(Exts.IsTraitEnabled)
world.WorldActor.Trait<WarheadDebugOverlay>().AddImpact(victim.CenterPosition, debugOverlayRange, DebugOverlayColor); .Select(s => Pair.New(s, s.Info.Type.DistanceFromEdge(pos, victim)))
.MinByOrDefault(s => s.Second);
// Cannot be damaged without an active HitShape or if HitShape is outside Spread
if (closestActiveShape.First == null || closestActiveShape.Second > Spread)
continue;
InflictDamage(victim, firedBy, closestActiveShape.First.Info, damageModifiers);
} }
} }
protected virtual void InflictDamage(Actor victim, Actor firedBy, HitShapeInfo hitshapeInfo, IEnumerable<int> damageModifiers)
{
var damage = Util.ApplyPercentageModifiers(Damage, damageModifiers.Append(DamageVersus(victim, hitshapeInfo)));
victim.InflictDamage(firedBy, new Damage(damage, DamageTypes));
}
} }
} }