Cast to long to avoid overflow when multiplying by the health (part 2)
This commit is contained in:
@@ -115,7 +115,9 @@ namespace OpenRA.Mods.Common.Traits
|
||||
|
||||
// The cost is the same regardless of the amount of people repairing
|
||||
var hpToRepair = Math.Min(Info.RepairStep, health.MaxHP - health.HP);
|
||||
var cost = Math.Max(1, (hpToRepair * Info.RepairPercent * buildingValue) / (health.MaxHP * 100));
|
||||
|
||||
// Cast to long to avoid overflow when multiplying by the health
|
||||
var cost = Math.Max(1, (int)(((long)hpToRepair * Info.RepairPercent * buildingValue) / (health.MaxHP * 100L)));
|
||||
|
||||
// TakeCash will return false if the player can't pay, and will stop him from contributing this Tick
|
||||
var activePlayers = Repairers.Count(player => player.PlayerActor.Trait<PlayerResources>().TakeCash(cost, true));
|
||||
|
||||
@@ -69,7 +69,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
if (totalTiles == 0)
|
||||
return;
|
||||
|
||||
damageThreshold = (Info.DamageThreshold * health.MaxHP + (100 - Info.DamageThreshold) * safeTiles * health.MaxHP / totalTiles) / 100;
|
||||
// Cast to long to avoid overflow when multiplying by the health
|
||||
damageThreshold = (int)((Info.DamageThreshold * (long)health.MaxHP + (100 - Info.DamageThreshold) * safeTiles * (long)health.MaxHP / totalTiles) / 100);
|
||||
|
||||
// Actors start with maximum damage applied
|
||||
var delta = health.HP - damageThreshold;
|
||||
|
||||
@@ -43,7 +43,8 @@ namespace OpenRA.Mods.Common.Traits
|
||||
Info = info;
|
||||
MaxHP = info.HP > 0 ? info.HP : 1;
|
||||
|
||||
hp = init.Contains<HealthInit>() ? init.Get<HealthInit, int>() * MaxHP / 100 : MaxHP;
|
||||
// Cast to long to avoid overflow when multiplying by the health
|
||||
hp = init.Contains<HealthInit>() ? (int)(init.Get<HealthInit, int>() * (long)MaxHP / 100) : MaxHP;
|
||||
|
||||
DisplayHP = hp;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user