Merge pull request #8635 from penev92/bleed_warheads

Move Warheads from the engine to Mods.Common
This commit is contained in:
reaperrr
2015-07-07 18:32:45 +02:00
24 changed files with 80 additions and 64 deletions

View File

@@ -12,8 +12,8 @@ using System;
using System.Collections.Generic;
using System.Linq;
using AI.Fuzzy.Library;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.AI
@@ -191,13 +191,13 @@ namespace OpenRA.Mods.Common.AI
protected float RelativePower(IEnumerable<Actor> own, IEnumerable<Actor> enemy)
{
return RelativeValue(own, enemy, 100, SumOfValues<AttackBase>, (Actor a) =>
return RelativeValue(own, enemy, 100, SumOfValues<AttackBase>, a =>
{
var sumOfDamage = 0;
var arms = a.TraitsImplementing<Armament>();
foreach (var arm in arms)
{
var warhead = arm.Weapon.Warheads.Select(w => w as DamageWarhead).FirstOrDefault(w => w != null);
var warhead = arm.Weapon.Warheads.OfType<DamageWarhead>().FirstOrDefault();
if (warhead != null)
sumOfDamage += warhead.Damage;
}

View File

@@ -10,8 +10,8 @@
using System;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Lint

View File

@@ -539,11 +539,13 @@
<Compile Include="UtilityCommands\UpgradeRules.cs" />
<Compile Include="Warheads\CreateEffectWarhead.cs" />
<Compile Include="Warheads\CreateResourceWarhead.cs" />
<Compile Include="Warheads\DamageWarhead.cs" />
<Compile Include="Warheads\DestroyResourceWarhead.cs" />
<Compile Include="Warheads\GrantUpgradeWarhead.cs" />
<Compile Include="Warheads\HealthPercentageDamageWarhead.cs" />
<Compile Include="Warheads\LeaveSmudgeWarhead.cs" />
<Compile Include="Warheads\SpreadDamageWarhead.cs" />
<Compile Include="Warheads\Warhead.cs" />
<Compile Include="Widgets\ActorPreviewWidget.cs" />
<Compile Include="Widgets\ColorMixerWidget.cs" />
<Compile Include="Widgets\ColorPreviewManagerWidget.cs" />

View File

@@ -13,7 +13,7 @@ using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using OpenRA.Activities;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -101,8 +101,11 @@ namespace OpenRA.Mods.Common.Traits
{
get
{
if (IsTraitDisabled)
yield break;
var armament = Armaments.FirstOrDefault(a => a.Weapon.Warheads.Any(w => (w is DamageWarhead)));
if (armament == null || IsTraitDisabled)
if (armament == null)
yield break;
var negativeDamage = (armament.Weapon.Warheads.FirstOrDefault(w => (w is DamageWarhead)) as DamageWarhead).Damage < 0;

View File

@@ -9,6 +9,7 @@
#endregion
using System.Linq;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -40,7 +41,8 @@ namespace OpenRA.Mods.Common.Traits
if (self.World.SharedRandom.Next(100) > info.Chance)
return;
if (info.DeathType != null && e.Warhead != null && !info.DeathType.Intersect(e.Warhead.DamageTypes).Any())
var warhead = e.Warhead as DamageWarhead;
if (info.DeathType != null && warhead != null && !info.DeathType.Intersect(warhead.DamageTypes).Any())
return;
var weaponName = ChooseWeaponForExplosion(self);

View File

@@ -10,7 +10,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -64,7 +64,8 @@ namespace OpenRA.Mods.Common.Traits
public void Damaged(Actor self, AttackInfo e)
{
if (e.Damage <= 0 || e.Warhead == null || !e.Warhead.DamageTypes.Any(x => info.DamageTriggers.Contains(x)))
var warhead = e.Warhead as DamageWarhead;
if (e.Damage <= 0 || warhead == null || !warhead.DamageTypes.Any(x => info.DamageTriggers.Contains(x)))
return;
if (!IsProne)
@@ -81,12 +82,16 @@ namespace OpenRA.Mods.Common.Traits
localOffset = WVec.Zero;
}
public int GetDamageModifier(Actor attacker, DamageWarhead warhead)
public int GetDamageModifier(Actor attacker, IWarhead warhead)
{
if (!IsProne)
return 100;
var modifierPercentages = info.DamageModifiers.Where(x => warhead.DamageTypes.Contains(x.Key)).Select(x => x.Value);
var damageWh = warhead as DamageWarhead;
if (damageWh == null)
return 100;
var modifierPercentages = info.DamageModifiers.Where(x => damageWh.DamageTypes.Contains(x.Key)).Select(x => x.Value);
return Util.ApplyPercentageModifiers(100, modifierPercentages);
}

View File

@@ -8,10 +8,9 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -42,6 +41,8 @@ namespace OpenRA.Mods.Common.Traits
public class TerrainModifiesDamage : IDamageModifier
{
const int FullDamage = 100;
public readonly TerrainModifiesDamageInfo Info;
readonly Actor self;
@@ -52,11 +53,11 @@ namespace OpenRA.Mods.Common.Traits
this.self = self;
}
public int GetDamageModifier(Actor attacker, DamageWarhead warhead)
public int GetDamageModifier(Actor attacker, IWarhead warhead)
{
var percent = 100;
if (attacker.Owner.IsAlliedWith(self.Owner) && (warhead != null && warhead.Damage < 0) && !Info.ModifyHealing)
return percent;
var damageWh = warhead as DamageWarhead;
if (attacker.Owner.IsAlliedWith(self.Owner) && (damageWh != null && damageWh.Damage < 0) && !Info.ModifyHealing)
return FullDamage;
var world = self.World;
var map = world.Map;
@@ -67,7 +68,7 @@ namespace OpenRA.Mods.Common.Traits
var terrainType = tileSet[tileSet.GetTerrainIndex(tiles[pos])].Type;
if (!Info.TerrainModifier.ContainsKey(terrainType))
return percent;
return FullDamage;
return Info.TerrainModifier[terrainType];
}

View File

@@ -8,7 +8,6 @@
*/
#endregion
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -18,6 +17,6 @@ namespace OpenRA.Mods.Common.Traits
class Invulnerable : IDamageModifier
{
public int GetDamageModifier(Actor attacker, DamageWarhead warhead) { return 0; }
public int GetDamageModifier(Actor attacker, IWarhead warhead) { return 0; }
}
}

View File

@@ -12,6 +12,7 @@ using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -72,16 +73,17 @@ namespace OpenRA.Mods.Common.Traits
{
// Killed by some non-standard means. This includes being crushed
// by a vehicle (Actors with Crushable trait will spawn CrushedSequence instead).
if (e.Warhead == null)
if (e.Warhead == null || !(e.Warhead is DamageWarhead))
return;
var sequence = Info.DeathSequence;
if (Info.UseDeathTypeSuffix)
{
var damageType = e.Warhead.DamageTypes.Intersect(Info.DeathTypes.Keys).FirstOrDefault();
var warhead = e.Warhead as DamageWarhead;
var damageType = warhead.DamageTypes.Intersect(Info.DeathTypes.Keys).FirstOrDefault();
if (damageType == null)
throw new Exception("Actor type `{0}` does not define a death animation for weapon with damage types `{1}`!"
.F(self.Info.Name, string.Join(", ", e.Warhead.DamageTypes)));
.F(self.Info.Name, string.Join(", ", warhead.DamageTypes)));
sequence += Info.DeathTypes[damageType];
}

View File

@@ -9,6 +9,7 @@
#endregion
using System.Linq;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
@@ -37,11 +38,13 @@ namespace OpenRA.Mods.Common.Traits
public void Killed(Actor self, AttackInfo e)
{
var warhead = e.Warhead as DamageWarhead;
// Killed by some non-standard means
if (e.Warhead == null)
if (warhead == null)
return;
if (info.DeathTypes.Intersect(e.Warhead.DamageTypes).Any())
if (info.DeathTypes.Intersect(warhead.DamageTypes).Any())
self.PlayVoiceLocal(info.Voice, info.VolumeMultiplier);
}
}

View File

@@ -99,7 +99,7 @@ namespace OpenRA.Mods.Common.Traits
inaccuracyLevel = newLevel.Clamp(0, info.InaccuracyModifier.Length);
}
public int GetDamageModifier(Actor attacker, DamageWarhead warhead)
public int GetDamageModifier(Actor attacker, IWarhead warhead)
{
return damageLevel > 0 ? info.DamageModifier[damageLevel - 1] : 100;
}

View File

@@ -12,8 +12,8 @@ using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using OpenRA.GameRules;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.UtilityCommands

View File

@@ -0,0 +1,77 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
{
public abstract class DamageWarhead : Warhead
{
[Desc("How much (raw) damage to deal.")]
public readonly int Damage = 0;
[Desc("Types of damage that this warhead causes. Leave empty for no damage.")]
public readonly string[] DamageTypes = new string[0];
[FieldLoader.LoadUsing("LoadVersus")]
[Desc("Damage percentage versus each armortype. 0% = can't target.")]
public readonly Dictionary<string, int> Versus;
public static object LoadVersus(MiniYaml yaml)
{
var nd = yaml.ToDictionary();
return nd.ContainsKey("Versus")
? nd["Versus"].ToDictionary(my => FieldLoader.GetValue<int>("(value)", my.Value))
: new Dictionary<string, int>();
}
public int DamageVersus(ActorInfo victim)
{
var armor = victim.Traits.GetOrDefault<ArmorInfo>();
if (armor != null && armor.Type != null)
{
int versus;
if (Versus.TryGetValue(armor.Type, out versus))
return versus;
}
return 100;
}
// TODO: This can be removed after the legacy and redundant 0% = not targetable
// assumption has been removed from the yaml definitions
public override bool CanTargetActor(ActorInfo victim, Actor firedBy)
{
var health = victim.Traits.GetOrDefault<HealthInfo>();
if (health == null)
return false;
return DamageVersus(victim) > 0;
}
public override void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers)
{
// Used by traits that damage a single actor, rather than a position
if (target.Type == TargetType.Actor)
DoImpact(target.Actor, firedBy, damageModifiers);
else if (target.Type != TargetType.Invalid)
DoImpact(target.CenterPosition, firedBy, damageModifiers);
}
public abstract void DoImpact(WPos pos, Actor firedBy, IEnumerable<int> damageModifiers);
public virtual void DoImpact(Actor victim, Actor firedBy, IEnumerable<int> damageModifiers)
{
var damage = Util.ApplyPercentageModifiers(Damage, damageModifiers.Append(DamageVersus(victim.Info)));
victim.InflictDamage(firedBy, damage, this);
}
}
}

View File

@@ -8,11 +8,8 @@
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Effects;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads

View File

@@ -10,9 +10,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Effects;
using OpenRA.GameRules;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads

View File

@@ -0,0 +1,94 @@
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Warheads
{
[Desc("Base warhead class. This can be used to derive other warheads from.")]
public abstract class Warhead : IWarhead
{
[Desc("What types of targets are affected.")]
public readonly string[] ValidTargets = { "Ground", "Water" };
[Desc("What types of targets are unaffected.", "Overrules ValidTargets.")]
public readonly string[] InvalidTargets = { };
[Desc("What diplomatic stances are affected.")]
public readonly Stance ValidStances = Stance.Ally | Stance.Neutral | Stance.Enemy;
[Desc("Can this warhead affect the actor that fired it.")]
public readonly bool AffectsParent = false;
[Desc("Delay in ticks before applying the warhead effect.", "0 = instant (old model).")]
public readonly int Delay = 0;
int IWarhead.Delay { get { return Delay; } }
HashSet<string> validTargetSet;
HashSet<string> invalidTargetSet;
public bool IsValidTarget(IEnumerable<string> targetTypes)
{
if (validTargetSet == null)
validTargetSet = new HashSet<string>(ValidTargets);
if (invalidTargetSet == null)
invalidTargetSet = new HashSet<string>(InvalidTargets);
return validTargetSet.Overlaps(targetTypes) && !invalidTargetSet.Overlaps(targetTypes);
}
/// <summary>Applies the warhead's effect against the target.</summary>
public abstract void DoImpact(Target target, Actor firedBy, IEnumerable<int> damageModifiers);
// TODO: This can be removed after the legacy and redundant 0% = not targetable
// assumption has been removed from the yaml definitions
public virtual bool CanTargetActor(ActorInfo victim, Actor firedBy) { return false; }
/// <summary>Checks if the warhead is valid against (can do something to) the actor.</summary>
public bool IsValidAgainst(Actor victim, Actor firedBy)
{
if (!CanTargetActor(victim.Info, firedBy))
return false;
if (!AffectsParent && victim == firedBy)
return false;
var stance = firedBy.Owner.Stances[victim.Owner];
if (!ValidStances.HasFlag(stance))
return false;
// A target type is valid if it is in the valid targets list, and not in the invalid targets list.
var targetable = victim.TraitOrDefault<ITargetable>();
if (targetable == null || !IsValidTarget(targetable.TargetTypes))
return false;
return true;
}
/// <summary>Checks if the warhead is valid against (can do something to) the frozen actor.</summary>
public bool IsValidAgainst(FrozenActor victim, Actor firedBy)
{
if (!CanTargetActor(victim.Info, firedBy))
return false;
// AffectsParent checks do not make sense for FrozenActors, so skip to stance checks
var stance = firedBy.Owner.Stances[victim.Owner];
if (!ValidStances.HasFlag(stance))
return false;
// A target type is valid if it is in the valid targets list, and not in the invalid targets list.
var targetable = victim.Info.Traits.GetOrDefault<ITargetableInfo>();
if (targetable == null || !IsValidTarget(targetable.GetTargetTypes()))
return false;
return true;
}
}
}