-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
EmitInfantryOnSell.cs
84 lines (68 loc) · 2.58 KB
/
EmitInfantryOnSell.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#region Copyright & License Information
/*
* Copyright 2007-2015 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. For more information,
* see COPYING.
*/
#endregion
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawn new actors when sold.")]
public class EmitInfantryOnSellInfo : ITraitInfo
{
public readonly float ValuePercent = 40;
public readonly float MinHpPercent = 30;
[ActorReference]
[Desc("Be sure to use lowercase. Default value is \"e1\".")]
public readonly string[] ActorTypes = { "e1" };
[Desc("Spawns actors only if the selling player's faction is in this list. " +
"Leave empty to allow all factions by default.")]
public readonly string[] Factions = { };
public object Create(ActorInitializer init) { return new EmitInfantryOnSell(init.Self, this); }
}
public class EmitInfantryOnSell : INotifySold
{
readonly EmitInfantryOnSellInfo info;
readonly bool correctFaction;
public EmitInfantryOnSell(Actor self, EmitInfantryOnSellInfo info)
{
this.info = info;
var factionsList = info.Factions;
correctFaction = factionsList.Length == 0 || factionsList.Contains(self.Owner.Faction.InternalName);
}
public void Selling(Actor self) { }
void Emit(Actor self)
{
if (!correctFaction)
return;
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 health = self.TraitOrDefault<Health>();
var dudesValue = info.ValuePercent * cost;
if (health != null)
dudesValue = dudesValue * health.HP / health.MaxHP;
dudesValue /= 100;
var eligibleLocations = FootprintUtils.Tiles(self).ToList();
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].Traits.Get<ValuedInfo>().Cost }).ToList();
while (eligibleLocations.Count > 0 && actorTypes.Any(a => a.Cost <= dudesValue))
{
var at = actorTypes.Where(a => a.Cost <= dudesValue).Random(self.World.SharedRandom);
var loc = eligibleLocations.Random(self.World.SharedRandom);
eligibleLocations.Remove(loc);
dudesValue -= at.Cost;
self.World.AddFrameEndTask(w => w.CreateActor(at.Name, new TypeDictionary
{
new LocationInit(loc),
new OwnerInit(self.Owner),
}));
}
}
public void Sold(Actor self) { Emit(self); }
}
}