Shift Actor.Health onto a trait.

Known regressions:
 - cnc only
 - health bar colors
 - can't repair buildings
This commit is contained in:
Paul Chote
2010-07-30 00:01:59 +12:00
parent 1e08dc6301
commit 6fba888d45
56 changed files with 530 additions and 303 deletions

View File

@@ -22,7 +22,7 @@ namespace OpenRA.Mods.RA.Activities
public IActivity Tick(Actor self)
{
if (target == null || target.IsDead) return NextActivity;
if (target == null || target.IsDead()) return NextActivity;
target.World.AddFrameEndTask(w =>
{

View File

@@ -24,10 +24,10 @@ namespace OpenRA.Mods.RA.Activities
}
public IActivity Tick(Actor self)
{
if (target == null || target.IsDead) return NextActivity;
{
if (target == null || target.IsDead()) return NextActivity;
self.World.AddFrameEndTask(w => w.Add(new DelayedAction(25 * 2,
() => target.InflictDamage(self, target.Health, null))));
() => target.Kill(self))));
return NextActivity;
}

View File

@@ -20,7 +20,7 @@ namespace OpenRA.Mods.RA.Activities
public IActivity Tick(Actor self)
{
if (target == null || target.IsDead) return NextActivity;
if (target == null || target.IsDead()) return NextActivity;
if (target.Owner == self.Owner) return NextActivity;
foreach (var t in target.traits.WithInterface<IAcceptSpy>())

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.RA.Activities
public IActivity Tick(Actor self)
{
if (Structure != null && Structure.IsDead)
if (Structure != null && Structure.IsDead())
{
Structure = null;
isCanceled = true;

View File

@@ -48,7 +48,7 @@ namespace OpenRA.Mods.RA.Activities
.SetPosition(self, Util.CellContaining(target.CenterLocation));
if (target.IsActor)
target.Actor.InflictDamage(self, target.Actor.Health, null); // kill it
target.Actor.Kill(self);
return NextActivity;
}

View File

@@ -28,11 +28,13 @@ namespace OpenRA.Mods.RA.Activities
if (isCanceled) return NextActivity;
if (remainingTicks == 0)
{
var health = self.traits.GetOrDefault<Health>();
if (health == null) return NextActivity;
var unitCost = self.Info.Traits.Get<ValuedInfo>().Cost;
var hp = self.Info.Traits.Get<OwnedActorInfo>().HP;
var costPerHp = (host.Info.Traits.Get<RepairsUnitsInfo>().URepairPercent * unitCost) / hp;
var hpToRepair = Math.Min(host.Info.Traits.Get<RepairsUnitsInfo>().URepairStep, hp - self.Health);
var costPerHp = (host.Info.Traits.Get<RepairsUnitsInfo>().URepairPercent * unitCost) / health.MaxHP;
var hpToRepair = Math.Min(host.Info.Traits.Get<RepairsUnitsInfo>().URepairStep, health.MaxHP - health.HP);
var cost = (int)Math.Ceiling(costPerHp * hpToRepair);
if (!self.Owner.PlayerActor.traits.Get<PlayerResources>().TakeCash(cost))
{
@@ -41,7 +43,7 @@ namespace OpenRA.Mods.RA.Activities
}
self.InflictDamage(self, -hpToRepair, null);
if (self.Health == hp)
if (health.MaxHP == health.HP)
return NextActivity;
if (host != null)

View File

@@ -22,11 +22,12 @@ namespace OpenRA.Mods.RA.Activities
public IActivity Tick(Actor self)
{
if (target == null || target.IsDead) return NextActivity;
if (target.Health == target.GetMaxHP())
if (target == null || target.IsDead()) return NextActivity;
var health = target.traits.Get<Health>();
if (health.HP == health.MaxHP)
return NextActivity;
target.InflictDamage(self, -target.GetMaxHP(), null);
target.InflictDamage(self, -health.MaxHP, null);
self.World.AddFrameEndTask(w => w.Remove(self));
return NextActivity;

View File

@@ -47,8 +47,12 @@ namespace OpenRA.Mods.RA.Activities
Sound.PlayToPlayer(self.Owner, s, self.CenterLocation);
var a = w.CreateActor(actor, self.Location + offset, self.Owner);
a.Health = GetHealthToTransfer(self, a, transferPercentage);
var health = a.traits.GetOrDefault<Health>();
if (health != null)
{
health.TransferHPFromActor(a, self, transferPercentage);
}
if (selected)
w.Selection.Add(w, a);
});
@@ -56,14 +60,5 @@ namespace OpenRA.Mods.RA.Activities
}
public void Cancel(Actor self) { isCanceled = true; NextActivity = null; }
public static int GetHealthToTransfer(Actor from, Actor to, bool transferPercentage)
{
var oldHP = from.GetMaxHP();
var newHP = to.GetMaxHP();
return (transferPercentage)
? (int)((float)from.Health / oldHP * newHP)
: Math.Min(from.Health, newHP);
}
}
}

View File

@@ -24,7 +24,13 @@ namespace OpenRA.Mods.RA.Activities
w.Remove(self);
var mcv = w.CreateActor("mcv", self.Location + new int2(1, 1), self.Owner);
mcv.Health = TransformIntoActor.GetHealthToTransfer(self, mcv, true);
var health = mcv.traits.GetOrDefault<Health>();
if (health != null)
{
health.TransferHPFromActor(mcv, self, true);
}
mcv.traits.Get<Unit>().Facing = 96;
if (selected)

View File

@@ -218,10 +218,16 @@ namespace OpenRA.Mods.RA
{
// we can never "heal ground"; that makes no sense.
if (!target.IsActor) return null;
// unless forced, only heal allies.
if (self.Owner.Stances[underCursor.Owner] != Stance.Ally && !forceFire) return null;
// we can only heal actors with health
var health = underCursor.traits.GetOrDefault<Health>();
if (health == null) return null;
// don't allow healing of fully-healed stuff!
if (underCursor.Health >= underCursor.GetMaxHP()) return null;
if (health.HP >= health.MaxHP) return null;
}
else
{

View File

@@ -37,8 +37,9 @@ namespace OpenRA.Mods.RA
return true; // he's dead.
if ((attack.target.CenterLocation - self.Location).LengthSquared > range * range + 2)
return true; // wandered off faster than we could follow
if (attack.target.IsActor && attack.target.Actor.Health
== attack.target.Actor.Info.Traits.Get<OwnedActorInfo>().HP)
var health = attack.target.Actor.traits.GetOrDefault<Health>();
if (attack.target.IsActor && health.HP == health.MaxHP)
return true; // fully healed
return false;
@@ -59,7 +60,7 @@ namespace OpenRA.Mods.RA
return inRange
.Where(a => a != self && self.Owner.Stances[ a.Owner ] == Stance.Ally)
.Where(a => Combat.HasAnyValidWeapons(self, Target.FromActor(a)))
.Where(a => a.Health < a.Info.Traits.Get<OwnedActorInfo>().HP)
.Where(a => a.traits.Contains<Health>() && a.traits.Get<Health>().HPFraction < 1f)
.OrderBy(a => (a.Location - self.Location).LengthSquared)
.FirstOrDefault();
}

View File

@@ -66,8 +66,6 @@ namespace OpenRA.Mods.RA
{
if (!self.IsIdle) return;
if (!e.Attacker.Info.Traits.Contains<OwnedActorInfo>()) return;
// not a lot we can do about things we can't hurt... although maybe we should automatically run away?
if (!Combat.HasAnyValidWeapons(self, Target.FromActor(e.Attacker))) return;

View File

@@ -17,7 +17,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class BridgeInfo : ITraitInfo
class BridgeInfo : ITraitInfo, ITraitPrerequisite<HealthInfo>
{
public readonly bool Long = false;
@@ -75,11 +75,13 @@ namespace OpenRA.Mods.RA
BridgeInfo Info;
public string Type;
Bridge northNeighbour, southNeighbour;
Health Health;
public Bridge(Actor self, BridgeInfo info)
{
this.self = self;
self.RemoveOnDeath = false;
Health = self.traits.Get<Health>();
Health.RemoveOnDeath = false;
this.Info = info;
this.Type = self.Info.Name;
}
@@ -88,9 +90,9 @@ namespace OpenRA.Mods.RA
{
currentTemplate = template;
if (template == Info.DamagedTemplate)
self.Health = (int)(self.World.Defaults.ConditionYellow*self.GetMaxHP());
Health.InflictDamage(self, self, Health.MaxHP/2, null);
else if (template != Info.Template)
self.Health = 0;
Health.InflictDamage(self, self, Health.MaxHP, null);
// Create a new cache to store the tile data
if (cachedTileset != self.World.Map.Tileset)
@@ -141,7 +143,7 @@ namespace OpenRA.Mods.RA
bool IsIntact(Bridge b)
{
return b != null && b.self.IsInWorld && b.self.Health > 0;
return b != null && b.self.IsInWorld && !b.self.IsDead();
}
void KillUnitsOnBridge()
@@ -151,23 +153,21 @@ namespace OpenRA.Mods.RA
foreach (var c in TileSprites[currentTemplate].Keys)
foreach (var a in uim.GetUnitsAt(c))
if (!a.traits.Get<IMove>().CanEnterCell(c))
a.InflictDamage(self, a.Health, null);
a.Kill(self);
}
bool dead = false;
void UpdateState()
{
var ds = self.GetDamageState();
{
// If this is a long bridge next to a destroyed shore piece, we need die to give clean edges to the break
if (Info.Long && ds != DamageState.Dead &&
if (Info.Long && Health.DamageState != DamageState.Dead &&
((southNeighbour != null && Info.ShorePieces.Contains(southNeighbour.Type) && !IsIntact(southNeighbour)) ||
(northNeighbour != null && Info.ShorePieces.Contains(northNeighbour.Type) && !IsIntact(northNeighbour))))
{
self.Health = 0;
ds = DamageState.Dead;
self.Kill(self); // this changes the damagestate
}
var ds = Health.DamageState;
currentTemplate = (ds == DamageState.Half && Info.DamagedTemplate > 0) ? Info.DamagedTemplate :
(ds == DamageState.Dead && Info.DestroyedTemplate > 0) ? Info.DestroyedTemplate : Info.Template;

View File

@@ -94,8 +94,7 @@ namespace OpenRA.Mods.RA
foreach (var t in world.FindTilesInCircle(targetTile, warhead.Size[0]))
foreach (var unit in world.FindUnits(Game.CellSize * t, Game.CellSize * (t + new float2(1,1))))
unit.InflictDamage(args.firedBy,
(int)(warhead.Damage * warhead.EffectivenessAgainst(
unit.Info.Traits.Get<OwnedActorInfo>().Armor)), warhead);
(int)(warhead.Damage * warhead.EffectivenessAgainst(unit)), warhead);
} break;
}
}
@@ -154,7 +153,7 @@ namespace OpenRA.Mods.RA
var distance = (int)Math.Max(0, (target.CenterLocation - args.dest).Length - radius);
var falloff = (float)GetDamageFalloff(distance / warhead.Spread);
var rawDamage = (float)(warhead.Damage * modifier * falloff);
var multiplier = (float)warhead.EffectivenessAgainst(target.Info.Traits.Get<OwnedActorInfo>().Armor);
var multiplier = (float)warhead.EffectivenessAgainst(target);
return (float)(rawDamage * multiplier);
}
@@ -171,8 +170,7 @@ namespace OpenRA.Mods.RA
if (targetable == null || !weapon.ValidTargets.Intersect(targetable.TargetTypes).Any())
return false;
var ownedInfo = target.Actor.Info.Traits.GetOrDefault<OwnedActorInfo>();
if (weapon.Warheads.All( w => w.EffectivenessAgainst(ownedInfo.Armor) <= 0))
if (weapon.Warheads.All( w => w.EffectivenessAgainst(target.Actor) <= 0))
return false;
return true;

View File

@@ -47,7 +47,7 @@ namespace OpenRA.Mods.RA
Game.Debug("{0} is defeated.".F(self.Owner.PlayerName));
foreach (var a in self.World.Queries.OwnedBy[self.Owner])
a.InflictDamage(a, a.Health, null);
a.Kill(a);
self.Owner.Shroud.Disabled = true;

View File

@@ -45,7 +45,7 @@ namespace OpenRA.Mods.RA
Sound.Play(report + ".aud", self.CenterLocation);
// Remove from world
self.Health = 0;
self.Kill(self);
detonatedBy.Owner.Kills++;
self.Owner.Deaths++;
w.Remove(self);

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.RA.Effects
public void Tick( World world )
{
if (a.IsDead || b.GetDamageModifier(null) > 0)
if (a.IsDead() || b.GetDamageModifier(null) > 0)
world.AddFrameEndTask(w => w.Remove(this));
}

View File

@@ -34,8 +34,9 @@ namespace OpenRA.Mods.RA
var csv = self.Info.Traits.GetOrDefault<CustomSellValueInfo>();
var valued = self.Info.Traits.GetOrDefault<ValuedInfo>();
var cost = csv != null ? csv.Value : (valued != null ? valued.Cost : 0);
var hp = self.Info.Traits.Get<OwnedActorInfo>().HP;
var hpFraction = Math.Max(info.MinHpFraction, hp / self.GetMaxHP());
var health = self.traits.GetOrDefault<Health>();
var hpFraction = (health == null) ? 1f : health.HPFraction;
var dudesValue = (int)(hpFraction * info.ValueFraction * cost);
var eligibleLocations = Footprint.Tiles(self).ToList();
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = Rules.Info[a].Traits.Get<ValuedInfo>().Cost }).ToArray();

View File

@@ -39,19 +39,25 @@ namespace OpenRA.Mods.RA
{
if (order.OrderString != "EngineerRepair") return null;
if (order.TargetActor == null) return null;
return (order.TargetActor.Health == order.TargetActor.GetMaxHP()) ? "goldwrench-blocked" : "goldwrench";
var health = order.TargetActor.traits.GetOrDefault<Health>();
if (health == null) return null;
return (health.HP == health.MaxHP) ? "goldwrench-blocked" : "goldwrench";
}
public string VoicePhraseForOrder(Actor self, Order order)
{
var health = order.TargetActor.traits.GetOrDefault<Health>();
if (health == null) return null;
return (order.OrderString == "EngineerRepair"
&& order.TargetActor.Health < order.TargetActor.GetMaxHP()) ? "Attack" : null;
&& health.HP < health.MaxHP) ? "Attack" : null;
}
public void ResolveOrder(Actor self, Order order)
{
if (order.OrderString == "EngineerRepair" && order.TargetActor.Health < order.TargetActor.GetMaxHP())
var health = order.TargetActor.traits.GetOrDefault<Health>();
if (health == null) return;
if (order.OrderString == "EngineerRepair" && health.HP < health.MaxHP)
{
if (self.Owner == self.World.LocalPlayer)
self.World.AddFrameEndTask(w =>

View File

@@ -25,7 +25,7 @@ namespace OpenRA.Mods.RA
{
public void Damaged(Actor self, AttackInfo e)
{
if (self.IsDead)
if (e.DamageState == DamageState.Dead)
{
var weapon = ChooseWeaponForExplosion(self);
if (weapon != null)

View File

@@ -164,7 +164,7 @@ namespace OpenRA.Mods.RA
public void Damaged(Actor self, AttackInfo e)
{
if (self.IsDead)
if (e.DamageState == DamageState.Dead)
if (LinkedProc != null)
LinkedProc.traits.WithInterface<IAcceptOre>().FirstOrDefault().UnlinkHarvester(LinkedProc,self);
}

View File

@@ -115,7 +115,7 @@ namespace OpenRA.Mods.RA
public void Tick(World world)
{
if (minelayer.IsDead || !minelayer.IsInWorld)
if (minelayer.IsDead() || !minelayer.IsInWorld)
world.CancelInputMode();
}

View File

@@ -34,8 +34,9 @@ namespace OpenRA.Mods.RA.Orders
&& a.traits.Contains<Selectable>()).FirstOrDefault();
var building = underCursor != null ? underCursor.Info.Traits.Get<BuildingInfo>() : null;
if (building != null && building.Repairable && underCursor.Health < building.HP)
var health = underCursor != null ? underCursor.traits.GetOrDefault<Health>() : null;
if (building != null && building.Repairable && health != null && health.HPFraction < 1f)
yield return new Order("Repair", underCursor);
}
}

View File

@@ -92,7 +92,7 @@ namespace OpenRA.Mods.RA
public void Damaged (Actor self, AttackInfo e)
{
if (self.IsDead)
if (e.DamageState == DamageState.Dead)
foreach (var harv in LinkedHarv)
harv.traits.Get<Harvester> ().UnlinkProc(harv, self);
}

View File

@@ -31,37 +31,13 @@ namespace OpenRA.Mods.RA.Render
anim.PlayFetchIndex(seqName, () => adjacentWalls);
}
enum ExtendedDamageState { Normal, ThreeQuarter, Half, Quarter, Dead };
ExtendedDamageState GetExtendedState( Actor self, int damage )
{
var effectiveHealth = self.Health + damage;
if (effectiveHealth <= 0)
return ExtendedDamageState.Dead;
if (effectiveHealth < self.GetMaxHP() * self.World.Defaults.ConditionRed)
return ExtendedDamageState.Quarter;
if (effectiveHealth < self.GetMaxHP() * self.World.Defaults.ConditionYellow)
return ExtendedDamageState.Half;
if (effectiveHealth < self.GetMaxHP() * 0.75)
return ExtendedDamageState.ThreeQuarter;
return ExtendedDamageState.Normal;
}
public override void Damaged(Actor self, AttackInfo e)
{
var oldState = GetExtendedState(self, e.Damage);
var newState = GetExtendedState(self, 0);
var numStates = self.Info.Traits.Get<RenderBuildingWallInfo>().DamageStates;
if (oldState == newState) return;
if (!e.ExtendedDamageStateChanged) return;
switch (newState)
switch (e.ExtendedDamageState)
{
case ExtendedDamageState.Normal:
seqName = "idle";

View File

@@ -17,10 +17,21 @@ using System.Drawing;
namespace OpenRA.Mods.RA
{
class RepairableInfo : TraitInfo<Repairable> { public readonly string[] RepairBuildings = { "fix" }; }
class RepairableInfo : ITraitInfo, ITraitPrerequisite<HealthInfo>
{
public readonly string[] RepairBuildings = { "fix" };
public virtual object Create(ActorInitializer init) { return new Repairable(init.self); }
}
class Repairable : IIssueOrder, IResolveOrder, IOrderCursor, IOrderVoice
{
Health Health;
public Repairable(Actor self)
{
Health = self.traits.Get<Health>();
}
public Order IssueOrder(Actor self, int2 xy, MouseInput mi, Actor underCursor)
{
if (mi.Button != MouseButton.Right) return null;
@@ -36,7 +47,7 @@ namespace OpenRA.Mods.RA
bool CanRepair(Actor self)
{
var li = self.traits.GetOrDefault<LimitedAmmo>();
return (self.Health < self.GetMaxHP() || (li != null && !li.FullAmmo()) );
return (Health.HPFraction < 1f || (li != null && !li.FullAmmo()) );
}
public string CursorForOrder(Actor self, Order order)

View File

@@ -16,7 +16,7 @@ using System.Drawing;
namespace OpenRA.Mods.RA
{
class RepairableNearInfo : TraitInfo<RepairableNear>
class RepairableNearInfo : TraitInfo<RepairableNear>, ITraitPrerequisite<HealthInfo>
{
[ActorReference]
public readonly string[] Buildings = { "spen", "syrd" };
@@ -28,10 +28,10 @@ namespace OpenRA.Mods.RA
{
if (mi.Button != MouseButton.Right) return null;
if (underCursor == null) return null;
if (underCursor.Owner == self.Owner &&
self.Info.Traits.Get<RepairableNearInfo>().Buildings.Contains( underCursor.Info.Name ) &&
self.Health < self.GetMaxHP())
self.traits.Get<Health>().HPFraction < 1f)
return new Order("Enter", self, underCursor);
return null;

View File

@@ -26,7 +26,7 @@ namespace OpenRA.Mods.RA
if (reservedFor == null)
return; /* nothing to do */
if (reservedFor.IsDead) reservedFor = null; /* not likely to arrive now. */
if (reservedFor.IsDead()) reservedFor = null; /* not likely to arrive now. */
}
public IDisposable Reserve(Actor forActor)

View File

@@ -12,7 +12,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.RA
{
class SelfHealingInfo : TraitInfo<SelfHealing>
class SelfHealingInfo : TraitInfo<SelfHealing>, ITraitPrerequisite<HealthInfo>
{
public readonly int Step = 5;
public readonly int Ticks = 5;
@@ -27,8 +27,7 @@ namespace OpenRA.Mods.RA
public void Tick(Actor self)
{
var info = self.Info.Traits.Get<SelfHealingInfo>();
if ((float)self.Health / self.GetMaxHP() >= info.HealIfBelow)
if (self.traits.Get<Health>().HPFraction >= info.HealIfBelow)
return;
if (--ticks <= 0)

View File

@@ -49,7 +49,7 @@ namespace OpenRA.Mods.RA
public void Damaged(Actor self, AttackInfo e)
{
if (self.IsDead && Player.GetSiloFullness() > 0)
if (self.IsDead() && Player.GetSiloFullness() > 0)
Player.TakeOre(Stored(self)); // Lose the stored ore
}

View File

@@ -33,6 +33,6 @@ namespace OpenRA.Mods.RA
}
public IEnumerable<string> CrushClasses { get { return info.CrushClasses; } }
public void OnCrush(Actor crusher) { self.InflictDamage(crusher, self.Health, null); }
public void OnCrush(Actor crusher) { self.Kill(crusher); }
}
}