Files
OpenRA/OpenRA.Mods.Common/Traits/GivesBounty.cs
Zimmermann Gyula cf8fff2b99 Remove passing the warheads from DamageWarhead to AttackInfo.
Added a Damage class to pass damage value and damage(types) instead.
This removes a great amount of overhead and longterm opens possibilities to have damagetypes without warheads.
2016-07-12 10:09:49 +02:00

84 lines
2.7 KiB
C#

#region Copyright & License Information
/*
* Copyright 2007-2016 The OpenRA Developers (see AUTHORS)
* This file is part of OpenRA, which is free software. It is made
* available to you under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System.Collections.Generic;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("When killed, this actor causes the attacking player to receive money.")]
class GivesBountyInfo : ITraitInfo
{
[Desc("Percentage of the killed actor's Cost or CustomSellValue to be given.")]
public readonly int Percentage = 10;
[Desc("Scale bounty based on the veterancy of the killed unit. The value is given in percent.")]
public readonly int LevelMod = 125;
[Desc("Stance the attacking player needs to receive the bounty.")]
public readonly Stance ValidStances = 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<string> DeathTypes = new HashSet<string>();
public object Create(ActorInitializer init) { return new GivesBounty(init.Self, this); }
}
class GivesBounty : INotifyKilled
{
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 gainsExp = self.TraitOrDefault<GainsExperience>();
if (gainsExp == null)
return 100;
var slevel = gainsExp.Level;
return (slevel > 0) ? slevel * info.LevelMod : 100;
}
void INotifyKilled.Killed(Actor self, AttackInfo e)
{
if (e.Attacker == null || e.Attacker.Disposed)
return;
if (!info.ValidStances.HasStance(e.Attacker.Owner.Stances[self.Owner]))
return;
if (info.DeathTypes.Count > 0 && !e.Damage.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 (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<PlayerResources>().GiveCash(bounty);
}
}
}