Skip to content

Commit

Permalink
Stuff required for Cash Bounty Generals power.
Browse files Browse the repository at this point in the history
  • Loading branch information
MustaphaTR committed Oct 10, 2017
1 parent 133a1c2 commit ee0617a
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 15 deletions.
1 change: 1 addition & 0 deletions OpenRA.Mods.Common/OpenRA.Mods.Common.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -500,6 +500,7 @@
<Compile Include="Traits\SupportPowers\SupportPower.cs" />
<Compile Include="Traits\SupportPowers\SupportPowerManager.cs" />
<Compile Include="Traits\SupportPowers\SpawnActorPower.cs" />
<Compile Include="Traits\TakesBounty.cs" />
<Compile Include="Traits\Targetable.cs" />
<Compile Include="Traits\ThrowsParticle.cs" />
<Compile Include="Traits\Tooltip.cs" />
Expand Down
33 changes: 18 additions & 15 deletions OpenRA.Mods.Common/Traits/GivesBounty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#endregion

using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Warheads;
using OpenRA.Traits;
Expand All @@ -19,12 +20,9 @@ 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("Type of bounty. Used for targerting along with 'TakesBounty' trait on actors.")]
public readonly string Type = "Bounty";

[Desc("Stance the attacking player needs to receive the bounty.")]
public readonly Stance ValidStances = Stance.Neutral | Stance.Enemy;

Expand Down Expand Up @@ -56,32 +54,32 @@ void INotifyCreated.Created(Actor self)
}

// Returns 100's as 1, so as to keep accuracy for longer.
int GetMultiplier()
int GetMultiplier(TakesBounty activeAttackerTakesBounty)
{
if (gainsExp == null)
return 100;

var slevel = gainsExp.Level;
return (slevel > 0) ? slevel * info.LevelMod : 100;
return (slevel > 0) ? slevel * activeAttackerTakesBounty.Info.LevelMod : 100;
}

int GetBountyValue(Actor self)
int GetBountyValue(Actor self, TakesBounty activeAttackerTakesBounty)
{
// Divide by 10000 because of GetMultiplier and info.Percentage.
return self.GetSellValue() * GetMultiplier() * info.Percentage / 10000;
return self.GetSellValue() * GetMultiplier(activeAttackerTakesBounty) * activeAttackerTakesBounty.Info.Percentage / 10000;
}

int GetDisplayedBountyValue(Actor self)
int GetDisplayedBountyValue(Actor self, TakesBounty activeAttackerTakesBounty)
{
var bounty = GetBountyValue(self);
var bounty = GetBountyValue(self, activeAttackerTakesBounty);
if (cargo == null)
return bounty;

foreach (var a in cargo.Passengers)
{
var givesBounty = a.TraitOrDefault<GivesBounty>();
if (givesBounty != null)
bounty += givesBounty.GetDisplayedBountyValue(a);
bounty += givesBounty.GetDisplayedBountyValue(a, activeAttackerTakesBounty);
}

return bounty;
Expand All @@ -92,17 +90,22 @@ void INotifyKilled.Killed(Actor self, AttackInfo e)
if (e.Attacker == null || e.Attacker.Disposed)
return;

var attackerTakesBounty = e.Attacker.TraitsImplementing<TakesBounty>().ToArray();
var activeAttackerTakesBounty = attackerTakesBounty.FirstOrDefault(tb => !tb.IsTraitDisabled && tb.Info.ValidTypes.Contains(info.Type));
if (activeAttackerTakesBounty == null)
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 displayedBounty = GetDisplayedBountyValue(self);
var displayedBounty = GetDisplayedBountyValue(self, activeAttackerTakesBounty);
if (info.ShowBounty && self.IsInWorld && displayedBounty > 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(displayedBounty), 30)));

e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(GetBountyValue(self));
e.Attacker.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(GetBountyValue(self, activeAttackerTakesBounty));
}
}
}
37 changes: 37 additions & 0 deletions OpenRA.Mods.Common/Traits/TakesBounty.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#region Copyright & License Information
/*
* Copyright 2007-2017 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.Traits;

namespace OpenRA.Mods.Common.Traits
{
[Desc("This actor takes bounty from actors with Gives Bounty trait with same Type.")]
public class TakesBountyInfo : ConditionalTraitInfo
{
[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("Accepted `Gives Bounty` types. Leave empty to accept all types.")]
public readonly HashSet<string> ValidTypes = new HashSet<string>() { "Bounty" };

public override object Create(ActorInitializer init) { return new TakesBounty(this); }
}

public class TakesBounty : ConditionalTrait<TakesBountyInfo>
{
public TakesBounty(TakesBountyInfo info)
: base(info) { }
}
}

0 comments on commit ee0617a

Please sign in to comment.