Files
OpenRA/OpenRA.Mods.Common/Warheads/SpreadDamageWarhead.cs
Alexis Hunt 08ad7d7f4e Refactor handling of hit radii in projectiles.
penev discovered that the RulesetLoaded functions of projectiles were
never being called, meaning that their blocking calculations were not
properly accounting for actors with large hitboxes.

The best fix for this is to change FindActorsOnLine to always account
for the largest actor's hit radius, rather than forcing callers to pass
the largest radius. Per the comment in Util.cs, as a result, move this
computation to ActorMap. I decided to simplify by not making a separate
calculation for actors that block projectiles only; this may cause a
small performance degradation as the search space is a bit larger.

Similarly to this, I've removed the ability to specify a search radius
manually. Because this is only a search radius, setting a value smaller
than the largest eligible actor makes no sense; that would lead to
completely inconsistent blocking. Setting a larger value, on the other
hand, would make no difference.

CreateEffectWarhead was the only place in core code any of these search
radii were set, and that's because 0 was a mysterious magic value that
made the warhead incapable of hitting actors. I replaced it with a
boolean flag that more clearly indicates the actual behaviour.

Fixes #14151.
2018-02-21 23:26:41 +01:00

91 lines
2.8 KiB
C#

#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;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
{
public class SpreadDamageWarhead : DamageWarhead, IRulesetLoaded<WeaponInfo>
{
[Desc("Range between falloff steps.")]
public readonly WDist Spread = new WDist(43);
[Desc("Damage percentage at each range step")]
public readonly int[] Falloff = { 100, 37, 14, 5, 0 };
[Desc("Ranges at which each Falloff step is defined. Overrides Spread.")]
public WDist[] Range = null;
void IRulesetLoaded<WeaponInfo>.RulesetLoaded(Ruleset rules, WeaponInfo info)
{
if (Range != null)
{
if (Range.Length != 1 && Range.Length != Falloff.Length)
throw new YamlException("Number of range values must be 1 or equal to the number of Falloff values.");
for (var i = 0; i < Range.Length - 1; i++)
if (Range[i] > Range[i + 1])
throw new YamlException("Range values must be specified in an increasing order.");
}
else
Range = Exts.MakeArray(Falloff.Length, i => i * Spread);
}
public override void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers)
{
var world = firedBy.World;
var debugVis = world.WorldActor.TraitOrDefault<DebugVisualizations>();
if (debugVis != null && debugVis.CombatGeometry)
world.WorldActor.Trait<WarheadDebugOverlay>().AddImpact(pos, Range, DebugOverlayColor);
var hitActors = world.FindActorsOnCircle(pos, Range[Range.Length - 1]);
foreach (var victim in hitActors)
{
// Cannot be damaged without a Health trait
var healthInfo = victim.Info.TraitInfoOrDefault<HealthInfo>();
if (healthInfo == null)
continue;
// Cannot be damaged without an active HitShape
var activeShapes = victim.TraitsImplementing<HitShape>().Where(Exts.IsTraitEnabled);
if (!activeShapes.Any())
continue;
var distance = activeShapes.Min(t => t.Info.Type.DistanceFromEdge(pos, victim));
var localModifiers = damageModifiers.Append(GetDamageFalloff(distance.Length));
DoImpact(victim, firedBy, localModifiers);
}
}
int GetDamageFalloff(int distance)
{
var inner = Range[0].Length;
for (var i = 1; i < Range.Length; i++)
{
var outer = Range[i].Length;
if (outer > distance)
return int2.Lerp(Falloff[i - 1], Falloff[i], distance - inner, outer - inner);
inner = outer;
}
return 0;
}
}
}