Make SpawnActorsOnSell conditional
This commit is contained in:
committed by
Paul Chote
parent
ea2794b417
commit
82f6c2b862
@@ -17,7 +17,7 @@ using OpenRA.Traits;
|
|||||||
namespace OpenRA.Mods.Common.Traits
|
namespace OpenRA.Mods.Common.Traits
|
||||||
{
|
{
|
||||||
[Desc("Spawn new actors when sold.")]
|
[Desc("Spawn new actors when sold.")]
|
||||||
public class SpawnActorsOnSellInfo : ITraitInfo
|
public class SpawnActorsOnSellInfo : ConditionalTraitInfo
|
||||||
{
|
{
|
||||||
public readonly int ValuePercent = 40;
|
public readonly int ValuePercent = 40;
|
||||||
public readonly int MinHpPercent = 30;
|
public readonly int MinHpPercent = 30;
|
||||||
@@ -30,17 +30,16 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
"Leave empty to allow all factions by default.")]
|
"Leave empty to allow all factions by default.")]
|
||||||
public readonly HashSet<string> Factions = new HashSet<string>();
|
public readonly HashSet<string> Factions = new HashSet<string>();
|
||||||
|
|
||||||
public object Create(ActorInitializer init) { return new SpawnActorsOnSell(init.Self, this); }
|
public override object Create(ActorInitializer init) { return new SpawnActorsOnSell(init.Self, this); }
|
||||||
}
|
}
|
||||||
|
|
||||||
public class SpawnActorsOnSell : INotifySold
|
public class SpawnActorsOnSell : ConditionalTrait<SpawnActorsOnSellInfo>, INotifySold
|
||||||
{
|
{
|
||||||
readonly SpawnActorsOnSellInfo info;
|
|
||||||
readonly bool correctFaction;
|
readonly bool correctFaction;
|
||||||
|
|
||||||
public SpawnActorsOnSell(Actor self, SpawnActorsOnSellInfo info)
|
public SpawnActorsOnSell(Actor self, SpawnActorsOnSellInfo info)
|
||||||
|
: base(info)
|
||||||
{
|
{
|
||||||
this.info = info;
|
|
||||||
var factionsList = info.Factions;
|
var factionsList = info.Factions;
|
||||||
correctFaction = factionsList.Count == 0 || factionsList.Contains(self.Owner.Faction.InternalName);
|
correctFaction = factionsList.Count == 0 || factionsList.Contains(self.Owner.Faction.InternalName);
|
||||||
}
|
}
|
||||||
@@ -49,7 +48,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
|
|
||||||
void Emit(Actor self)
|
void Emit(Actor self)
|
||||||
{
|
{
|
||||||
if (!correctFaction)
|
if (IsTraitDisabled || !correctFaction)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
var csv = self.Info.TraitInfoOrDefault<CustomSellValueInfo>();
|
var csv = self.Info.TraitInfoOrDefault<CustomSellValueInfo>();
|
||||||
@@ -57,11 +56,11 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
var cost = csv != null ? csv.Value : (valued != null ? valued.Cost : 0);
|
var cost = csv != null ? csv.Value : (valued != null ? valued.Cost : 0);
|
||||||
|
|
||||||
var health = self.TraitOrDefault<Health>();
|
var health = self.TraitOrDefault<Health>();
|
||||||
var dudesValue = info.ValuePercent * cost / 100;
|
var dudesValue = Info.ValuePercent * cost / 100;
|
||||||
if (health != null)
|
if (health != null)
|
||||||
{
|
{
|
||||||
// Cast to long to avoid overflow when multiplying by the health
|
// Cast to long to avoid overflow when multiplying by the health
|
||||||
if (100L * health.HP >= info.MinHpPercent * (long)health.MaxHP)
|
if (100L * health.HP >= Info.MinHpPercent * (long)health.MaxHP)
|
||||||
dudesValue = (int)((long)health.HP * dudesValue / health.MaxHP);
|
dudesValue = (int)((long)health.HP * dudesValue / health.MaxHP);
|
||||||
else
|
else
|
||||||
dudesValue = 0;
|
dudesValue = 0;
|
||||||
@@ -70,7 +69,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
var buildingInfo = self.Info.TraitInfoOrDefault<BuildingInfo>();
|
var buildingInfo = self.Info.TraitInfoOrDefault<BuildingInfo>();
|
||||||
|
|
||||||
var eligibleLocations = buildingInfo != null ? buildingInfo.Tiles(self.Location).ToList() : new List<CPos>();
|
var eligibleLocations = buildingInfo != null ? buildingInfo.Tiles(self.Location).ToList() : new List<CPos>();
|
||||||
var actorTypes = info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].TraitInfo<ValuedInfo>().Cost }).ToList();
|
var actorTypes = Info.ActorTypes.Select(a => new { Name = a, Cost = self.World.Map.Rules.Actors[a].TraitInfo<ValuedInfo>().Cost }).ToList();
|
||||||
|
|
||||||
while (eligibleLocations.Count > 0 && actorTypes.Any(a => a.Cost <= dudesValue))
|
while (eligibleLocations.Count > 0 && actorTypes.Any(a => a.Cost <= dudesValue))
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user