Move some Building traits and related elements to Mods.Common

This commit is contained in:
reaperrr
2014-12-26 02:34:49 +01:00
parent 2f67a88b93
commit 9dfd369446
44 changed files with 61 additions and 35 deletions

View File

@@ -14,6 +14,7 @@ using System.Linq;
using OpenRA.Effects;
using OpenRA.GameRules;
using OpenRA.Graphics;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;

View File

@@ -10,6 +10,7 @@
using System;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits

View File

@@ -0,0 +1,50 @@
 #region Copyright & License Information
/*
* Copyright 2007-2014 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;
using System.Linq;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
[Desc("Creates a free duplicate of produced units.")]
public class ClonesProducedUnitsInfo : ITraitInfo, Requires<ProductionInfo>, Requires<ExitInfo>
{
[Desc("Uses the \"Cloneable\" trait to determine whether or not we should clone a produced unit.")]
public readonly string[] CloneableTypes = { };
public object Create(ActorInitializer init) { return new ClonesProducedUnits(init.self, this); }
}
public class ClonesProducedUnits : INotifyOtherProduction
{
readonly ClonesProducedUnitsInfo info;
readonly Production production;
public ClonesProducedUnits(Actor self, ClonesProducedUnitsInfo info)
{
this.info = info;
production = self.Trait<Production>();
}
public void UnitProducedByOther(Actor self, Actor producer, Actor produced)
{
// No recursive cloning!
if (producer.Owner != self.Owner || producer.HasTrait<ClonesProducedUnits>())
return;
var ci = produced.Info.Traits.GetOrDefault<CloneableInfo>();
if (ci == null || !info.CloneableTypes.Intersect(ci.Types).Any())
return;
production.Produce(self, produced.Info, self.Owner.Country.Race);
}
}
}

View File

@@ -1,72 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 OpenRA.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
[Desc("Player recives a unit for free once the building is placed. This also works for structures.",
"If you want more than one unit to appear copy this section and assign IDs like FreeActor@2, ...")]
public class FreeActorInfo : ITraitInfo
{
[ActorReference]
[Desc("Name of actor (use HARV if this trait is for refineries)")]
public readonly string Actor = null;
[Desc("What the unit should start doing. Warning: If this is not a harvester", "it will break if you use FindResources.")]
public readonly string InitialActivity = null;
[Desc("Offset relative to structure-center in 2D (e.g. 1, 2)")]
public readonly CVec SpawnOffset = CVec.Zero;
[Desc("Which direction the unit should face.")]
public readonly int Facing = 0;
public object Create( ActorInitializer init ) { return new FreeActor(init, this); }
}
public class FreeActor
{
public FreeActor(ActorInitializer init, FreeActorInfo info)
{
if (init.Contains<FreeActorInit>() && !init.Get<FreeActorInit>().value)
return;
init.self.World.AddFrameEndTask(w =>
{
var a = w.CreateActor(info.Actor, new TypeDictionary
{
new ParentActorInit(init.self),
new LocationInit(init.self.Location + info.SpawnOffset),
new OwnerInit(init.self.Owner),
new FacingInit(info.Facing),
});
if (info.InitialActivity != null)
a.QueueActivity(Game.CreateObject<Activity>(info.InitialActivity));
});
}
}
public class FreeActorInit : IActorInit<bool>
{
[FieldFromYamlKey]
public readonly bool value = true;
public FreeActorInit() { }
public FreeActorInit(bool init) { value = init; }
public bool Value(World world) { return value; }
}
public class ParentActorInit : IActorInit<Actor>
{
public readonly Actor value;
public ParentActorInit(Actor parent) { value = parent; }
public Actor Value(World world) { return value; }
}
}

View File

@@ -13,6 +13,7 @@ using System.Linq;
using OpenRA.Activities;
using OpenRA.Mods.Common.Activities;
using OpenRA.Mods.Common.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.RA.Activities;
using OpenRA.Traits;

View File

@@ -0,0 +1,127 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.RA.Effects;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
[Desc("Building can be repaired by the repair button.")]
public class RepairableBuildingInfo : UpgradableTraitInfo, ITraitInfo, Requires<HealthInfo>
{
public readonly int RepairPercent = 20;
public readonly int RepairInterval = 24;
public readonly int RepairStep = 7;
public readonly int[] RepairBonuses = { 100, 150, 175, 200, 220, 240, 260, 280, 300 };
public readonly bool CancelWhenDisabled = false;
public readonly string IndicatorPalettePrefix = "player";
public object Create(ActorInitializer init) { return new RepairableBuilding(init.self, this); }
}
public class RepairableBuilding : UpgradableTrait<RepairableBuildingInfo>, ITick
{
[Sync]
public int RepairersHash { get { return Repairers.Aggregate(0, (code, player) => code ^ Sync.hash_player(player)); } }
public List<Player> Repairers = new List<Player>();
Health Health;
public bool RepairActive = false;
public RepairableBuilding(Actor self, RepairableBuildingInfo info)
: base (info)
{
Health = self.Trait<Health>();
}
public void RepairBuilding(Actor self, Player player)
{
if (!IsTraitDisabled && self.AppearsFriendlyTo(player.PlayerActor))
{
// If the player won't affect the repair, we won't add him
if (!Repairers.Remove(player) && Repairers.Count < Info.RepairBonuses.Length)
{
Repairers.Add(player);
Sound.PlayNotification(self.World.Map.Rules, player, "Speech", "Repairing", player.Country.Race);
self.World.AddFrameEndTask(w =>
{
if (!self.IsDead)
w.Add(new RepairIndicator(self, Info.IndicatorPalettePrefix));
});
}
}
}
int remainingTicks;
public void Tick(Actor self)
{
if (IsTraitDisabled)
{
if (RepairActive && Info.CancelWhenDisabled)
{
Repairers.Clear();
RepairActive = false;
}
return;
}
if (remainingTicks == 0)
{
Repairers = Repairers.Where(player => player.WinState == WinState.Undefined
&& player.Stances[self.Owner] == Stance.Ally).ToList();
// If after the previous operation there's no repairers left, stop
if (!Repairers.Any()) return;
var buildingValue = self.GetSellValue();
// The cost is the same regardless of the amount of people repairing
var hpToRepair = Math.Min(Info.RepairStep, Health.MaxHP - Health.HP);
var cost = Math.Max(1, (hpToRepair * Info.RepairPercent * buildingValue) / (Health.MaxHP * 100));
// TakeCash will return false if the player can't pay, and will stop him from contributing this Tick
var activePlayers = Repairers.Count(player => player.PlayerActor.Trait<PlayerResources>().TakeCash(cost));
RepairActive = activePlayers > 0;
if (!RepairActive)
{
remainingTicks = 1;
return;
}
// Bonus is applied after finding players who can pay
// activePlayers won't cause IndexOutOfRange because we capped the max amount of players
// to the length of the array
self.InflictDamage(self, -(hpToRepair * Info.RepairBonuses[activePlayers - 1] / 100), null);
if (Health.DamageState == DamageState.Undamaged)
{
Repairers.Clear();
RepairActive = false;
return;
}
remainingTicks = Info.RepairInterval;
}
else
--remainingTicks;
}
}
}

View File

@@ -1,23 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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 OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits
{
public class RepairsUnitsInfo : TraitInfo<RepairsUnits>
{
public readonly int ValuePercentage = 20; // charge 20% of the unit value to fully repair
public readonly int HpPerStep = 10;
public readonly int Interval = 24; // Ticks
}
public class RepairsUnits { }
}

View File

@@ -10,6 +10,7 @@
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Traits;
namespace OpenRA.Mods.RA.Traits