Merge pull request #6218 from pchote/percentmods

Change attribute modifiers to use integers.
This commit is contained in:
Matthias Mailänder
2014-08-16 17:28:37 +02:00
19 changed files with 121 additions and 66 deletions

View File

@@ -100,16 +100,21 @@ namespace OpenRA.Traits
public void InflictDamage(Actor self, Actor attacker, int damage, DamageWarhead warhead, bool ignoreModifiers)
{
if (IsDead) return; /* overkill! don't count extra hits as more kills! */
// Overkill! don't count extra hits as more kills!
if (IsDead)
return;
var oldState = this.DamageState;
/* apply the damage modifiers, if we have any. */
var modifier = self.TraitsImplementing<IDamageModifier>()
.Concat(self.Owner.PlayerActor.TraitsImplementing<IDamageModifier>())
.Select(t => t.GetDamageModifier(attacker, warhead)).Product();
if (!ignoreModifiers)
damage = damage > 0 ? (int)(damage * modifier) : damage;
// Apply any damage modifiers
if (!ignoreModifiers && damage > 0)
{
var modifiers = self.TraitsImplementing<IDamageModifier>()
.Concat(self.Owner.PlayerActor.TraitsImplementing<IDamageModifier>())
.Select(t => t.GetDamageModifier(attacker, warhead));
damage = Util.ApplyPercentageModifiers(damage, modifiers);
}
hp = Exts.Clamp(hp - damage, 0, MaxHP);

View File

@@ -167,9 +167,9 @@ namespace OpenRA.Traits
}
public interface IRenderModifier { IEnumerable<IRenderable> ModifyRender(Actor self, WorldRenderer wr, IEnumerable<IRenderable> r); }
public interface IDamageModifier { float GetDamageModifier(Actor attacker, DamageWarhead warhead); }
public interface ISpeedModifier { decimal GetSpeedModifier(); }
public interface IFirepowerModifier { float GetFirepowerModifier(); }
public interface IDamageModifier { int GetDamageModifier(Actor attacker, DamageWarhead warhead); }
public interface ISpeedModifier { int GetSpeedModifier(); }
public interface IFirepowerModifier { int GetFirepowerModifier(); }
public interface ILoadsPalettes { void LoadPalettes(WorldRenderer wr); }
public interface IPaletteModifier { void AdjustPalette(IReadOnlyDictionary<string, MutablePalette> b); }
public interface IPips { IEnumerable<PipType> GetPips(Actor self); }

View File

@@ -138,5 +138,15 @@ namespace OpenRA.Traits
var cells = target.Positions.Select(p => w.Map.CellContaining(p)).Distinct();
return ExpandFootprint(cells, true);
}
public static int ApplyPercentageModifiers(int number, IEnumerable<int> percentages)
{
// See the comments of PR#6079 for a faster algorithm if this becomes a performance bottleneck
var a = (decimal)number;
foreach (var p in percentages)
a *= p / 100m;
return (int)a;
}
}
}