From 41417c5ad28499a84f0a0c8da055eba2751b9320 Mon Sep 17 00:00:00 2001 From: Oliver Brakmann Date: Sun, 19 Jun 2016 22:25:54 +0200 Subject: [PATCH] Add a DeathTypes filter to GivesBounty also: * Updates the documentation * Adds an option to disable the floating text --- OpenRA.Mods.Common/Traits/GivesBounty.cs | 47 ++++++++++++++++++------ 1 file changed, 35 insertions(+), 12 deletions(-) diff --git a/OpenRA.Mods.Common/Traits/GivesBounty.cs b/OpenRA.Mods.Common/Traits/GivesBounty.cs index 5bbef12749..4b11926664 100644 --- a/OpenRA.Mods.Common/Traits/GivesBounty.cs +++ b/OpenRA.Mods.Common/Traits/GivesBounty.cs @@ -9,29 +9,48 @@ */ #endregion +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 { - [Desc("You get money for playing this actor.")] - class GivesBountyInfo : TraitInfo + [Desc("When killed, this actor causes the attacking player to receive money.")] + class GivesBountyInfo : ITraitInfo { - [Desc("Calculated by Cost or CustomSellValue so they have to be set to avoid crashes.")] + [Desc("Percentage of the killed actor's Cost or CustomSellValue to be given.")] public readonly int Percentage = 10; - [Desc("Higher ranked units give higher bounties.")] + + [Desc("Scale bounty based on the veterancy of the killed unit. The value is given in percent.")] public readonly int LevelMod = 125; - [Desc("Destroying creeps and enemies is rewarded.")] + + [Desc("Stance the attacking player needs to receive the bounty.")] public readonly Stance[] Stances = { Stance.Neutral, Stance.Enemy }; + + [Desc("Whether to show a floating text announcing the won bounty.")] + public readonly bool ShowBounty = true; + + [Desc("DeathTypes for which a bounty should be granted.", + "Use an empty list (the default) to allow all DeathTypes.")] + public readonly HashSet DeathTypes = new HashSet(); + + public object Create(ActorInitializer init) { return new GivesBounty(init.Self, this); } } class GivesBounty : INotifyKilled { - static int GetMultiplier(Actor self) + readonly GivesBountyInfo info; + + public GivesBounty(Actor self, GivesBountyInfo info) + { + this.info = info; + } + + int GetMultiplier(Actor self) { // returns 100's as 1, so as to keep accuracy for longer. - var info = self.Info.TraitInfo(); var gainsExp = self.TraitOrDefault(); if (gainsExp == null) return 100; @@ -40,20 +59,24 @@ namespace OpenRA.Mods.Common.Traits return (slevel > 0) ? slevel * info.LevelMod : 100; } - public void Killed(Actor self, AttackInfo e) + void INotifyKilled.Killed(Actor self, AttackInfo e) { - var info = self.Info.TraitInfo(); + if (e.Attacker == null || e.Attacker.Disposed) + return; - if (e.Attacker == null || e.Attacker.Disposed) return; + if (!info.Stances.Contains(e.Attacker.Owner.Stances[self.Owner])) + return; - if (!info.Stances.Contains(e.Attacker.Owner.Stances[self.Owner])) return; + var warhead = e.Warhead as DamageWarhead; + if (info.DeathTypes.Count > 0 && warhead != null && !warhead.DamageTypes.Overlaps(info.DeathTypes)) + return; var cost = self.GetSellValue(); // 2 hundreds because of GetMultiplier and info.Percentage. var bounty = cost * GetMultiplier(self) * info.Percentage / 10000; - if (bounty > 0 && e.Attacker.Owner.IsAlliedWith(self.World.RenderPlayer)) + if (info.ShowBounty && bounty > 0 && e.Attacker.Owner.IsAlliedWith(self.World.RenderPlayer)) e.Attacker.World.AddFrameEndTask(w => w.Add(new FloatingText(self.CenterPosition, e.Attacker.Owner.Color.RGB, FloatingText.FormatCashTick(bounty), 30))); e.Attacker.Owner.PlayerActor.Trait().GiveCash(bounty);