Cast to long to avoid overflow when multiplying by the health

This commit is contained in:
Arular101
2018-01-04 00:23:40 +01:00
committed by reaperrr
parent 32b0170785
commit 30acee38c9
9 changed files with 38 additions and 19 deletions

View File

@@ -160,7 +160,12 @@ namespace OpenRA.Mods.Common.AI
case DecisionMetric.Health:
var health = a.TraitOrDefault<Health>();
return (health != null) ? (health.HP / health.MaxHP) * Attractiveness : 0;
if (health == null)
return 0;
// Cast to long to avoid overflow when multiplying by the health
return (int)((long)health.HP * Attractiveness / health.MaxHP);
default:
return Attractiveness;

View File

@@ -34,9 +34,12 @@ namespace OpenRA.Mods.Common.Activities
public override Activity Tick(Actor self)
{
var cost = self.GetSellValue();
var sellValue = self.GetSellValue();
var refund = (cost * sellableInfo.RefundPercent * (health == null ? 1 : health.HP)) / (100 * (health == null ? 1 : health.MaxHP));
// Cast to long to avoid overflow when multiplying by the health
var hp = health != null ? (long)health.HP : 1L;
var maxHP = health != null ? (long)health.MaxHP : 1L;
var refund = (int)((sellValue * sellableInfo.RefundPercent * hp) / (100 * maxHP));
playerResources.GiveCash(refund);
foreach (var ns in self.TraitsImplementing<INotifySold>())

View File

@@ -122,7 +122,8 @@ namespace OpenRA.Mods.Common.Activities
var health = self.TraitOrDefault<Health>();
if (health != null)
{
var newHP = ForceHealthPercentage > 0 ? ForceHealthPercentage : (health.HP * 100) / health.MaxHP;
// Cast to long to avoid overflow when multiplying by the health
var newHP = ForceHealthPercentage > 0 ? ForceHealthPercentage : (int)(health.HP * 100L / health.MaxHP);
init.Add(new HealthInit(newHP));
}

View File

@@ -109,7 +109,9 @@ namespace OpenRA.Mods.Common.Traits
}
var health = target.Trait<Health>();
var lowEnoughHealth = health.HP <= c.Info.CaptureThreshold * health.MaxHP / 100;
// Cast to long to avoid overflow when multiplying by the health
var lowEnoughHealth = health.HP <= (int)(c.Info.CaptureThreshold * (long)health.MaxHP / 100);
cursor = !capturesInfo.Sabotage || lowEnoughHealth || target.Owner.NonCombatant
? capturesInfo.EnterCursor : capturesInfo.SabotageCursor;
@@ -129,7 +131,9 @@ namespace OpenRA.Mods.Common.Traits
}
var health = target.Info.TraitInfoOrDefault<HealthInfo>();
var lowEnoughHealth = target.HP <= c.CaptureThreshold * health.HP / 100;
// Cast to long to avoid overflow when multiplying by the health
var lowEnoughHealth = target.HP <= (int)(c.CaptureThreshold * (long)health.HP / 100);
cursor = !capturesInfo.Sabotage || lowEnoughHealth || target.Owner.NonCombatant
? capturesInfo.EnterCursor : capturesInfo.SabotageCursor;

View File

@@ -60,8 +60,9 @@ namespace OpenRA.Mods.Common.Traits
var dudesValue = info.ValuePercent * cost / 100;
if (health != null)
{
if (100 * health.HP >= info.MinHpPercent * health.MaxHP)
dudesValue = health.HP * dudesValue / health.MaxHP;
// Cast to long to avoid overflow when multiplying by the health
if (100L * health.HP >= info.MinHpPercent * (long)health.MaxHP)
dudesValue = (int)((long)health.HP * dudesValue / health.MaxHP);
else
dudesValue = 0;
}

View File

@@ -134,7 +134,8 @@ namespace OpenRA.Mods.Common.Traits
if (Info.DamageThreshold == 0)
return;
if (health.HP * 100 < Info.DamageThreshold * health.MaxHP)
// Cast to long to avoid overflow when multiplying by the health
if (health.HP * 100L < Info.DamageThreshold * (long)health.MaxHP)
self.World.AddFrameEndTask(w => self.Kill(e.Attacker));
}
}

View File

@@ -32,7 +32,8 @@ namespace OpenRA.Mods.Common.Traits
int IPowerModifier.GetPowerModifier()
{
return 100 * health.HP / health.MaxHP;
// Cast to long to avoid overflow when multiplying by the health
return (int)(100L * health.HP / health.MaxHP);
}
void INotifyDamage.Damaged(Actor self, AttackInfo e) { power.UpdateActor(self); }

View File

@@ -55,7 +55,8 @@ namespace OpenRA.Mods.Common.Traits
if (self.IsDead || IsTraitDisabled)
return;
if (health.HP >= Info.HealIfBelow * health.MaxHP / 100)
// Cast to long to avoid overflow when multiplying by the health
if (health.HP >= Info.HealIfBelow * (long)health.MaxHP / 100)
return;
if (damageTicks > 0)
@@ -67,7 +68,9 @@ namespace OpenRA.Mods.Common.Traits
if (--ticks <= 0)
{
ticks = Info.Delay;
self.InflictDamage(self, new Damage(-(Info.Step + Info.PercentageStep * health.MaxHP / 100), Info.DamageTypes));
// Cast to long to avoid overflow when multiplying by the health
self.InflictDamage(self, new Damage((int)(-(Info.Step + Info.PercentageStep * (long)health.MaxHP / 100)), Info.DamageTypes));
}
}

View File

@@ -94,14 +94,14 @@ namespace OpenRA.Mods.Common.Traits
{
get
{
var sellValue = self.GetSellValue() * info.RefundPercent / 100;
if (health.Value != null)
{
sellValue *= health.Value.HP;
sellValue /= health.Value.MaxHP;
}
var sellValue = self.GetSellValue();
return "Refund: $" + sellValue;
// Cast to long to avoid overflow when multiplying by the health
var hp = health != null ? (long)health.Value.HP : 1L;
var maxHP = health != null ? (long)health.Value.MaxHP : 1L;
var refund = (int)((sellValue * info.RefundPercent * hp) / (100 * maxHP));
return "Refund: $" + refund;
}
}
}