Moves some misc traits/activities to Mods.Common, moves some RA traits into the Traits folder
This commit is contained in:
66
OpenRA.Mods.Common/Traits/EmitInfantryOnSell.cs
Normal file
66
OpenRA.Mods.Common/Traits/EmitInfantryOnSell.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
#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.")]
|
||||
class EmitInfantryOnSellInfo : TraitInfo<EmitInfantryOnSell>
|
||||
{
|
||||
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" };
|
||||
}
|
||||
|
||||
class EmitInfantryOnSell : INotifySold
|
||||
{
|
||||
public void Selling(Actor self) { }
|
||||
|
||||
static void Emit(Actor self)
|
||||
{
|
||||
var info = self.Info.Traits.Get<EmitInfantryOnSellInfo>();
|
||||
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 }).ToArray();
|
||||
|
||||
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); }
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user