Move Crate, CrateSpawner and all CrateActions to Mods.Common

This commit is contained in:
penev92
2015-01-18 16:40:55 +02:00
parent f1a0f6e2a5
commit 52cc69fd1f
16 changed files with 28 additions and 40 deletions

View File

@@ -247,6 +247,19 @@
<Compile Include="Traits\Cargo.cs" />
<Compile Include="Traits\CashTrickler.cs" />
<Compile Include="Traits\Cloak.cs" />
<Compile Include="Traits\Crates\Crate.cs" />
<Compile Include="Traits\Crates\CrateAction.cs" />
<Compile Include="Traits\Crates\DuplicateUnitCrateAction.cs" />
<Compile Include="Traits\Crates\ExplodeCrateAction.cs" />
<Compile Include="Traits\Crates\GiveCashCrateAction.cs" />
<Compile Include="Traits\Crates\GiveMcvCrateAction.cs" />
<Compile Include="Traits\Crates\GiveUnitCrateAction.cs" />
<Compile Include="Traits\Crates\GrantUpgradeCrateAction.cs" />
<Compile Include="Traits\Crates\HealUnitsCrateAction.cs" />
<Compile Include="Traits\Crates\HideMapCrateAction.cs" />
<Compile Include="Traits\Crates\LevelUpCrateAction.cs" />
<Compile Include="Traits\Crates\RevealMapCrateAction.cs" />
<Compile Include="Traits\Crates\SupportPowerCrateAction.cs" />
<Compile Include="Traits\Crushable.cs" />
<Compile Include="Traits\EmitInfantryOnSell.cs" />
<Compile Include="Traits\ExternalCapturable.cs" />
@@ -388,6 +401,7 @@
<Compile Include="Traits\Valued.cs" />
<Compile Include="Traits\Wanders.cs" />
<Compile Include="Traits\World\BridgeLayer.cs" />
<Compile Include="Traits\World\CrateSpawner.cs" />
<Compile Include="Traits\World\CreateMPPlayers.cs" />
<Compile Include="Traits\World\DomainIndex.cs" />
<Compile Include="Traits\World\LoadWidgetAtGameStart.cs" />

View File

@@ -0,0 +1,190 @@
#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.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
class CrateInfo : ITraitInfo, IOccupySpaceInfo, Requires<RenderSpritesInfo>
{
[Desc("Length of time (in seconds) until the crate gets removed automatically. " +
"A value of zero disables auto-removal.")]
public readonly int Lifetime = 0;
[Desc("Allowed to land on.")]
public readonly string[] TerrainTypes = { };
[Desc("Define actors that can collect crates by setting this into the Crushes field from the Mobile trait.")]
public readonly string CrushClass = "crate";
public object Create(ActorInitializer init) { return new Crate(init, this); }
}
class Crate : ITick, IPositionable, ICrushable, ISync, INotifyParachuteLanded, INotifyAddedToWorld, INotifyRemovedFromWorld
{
readonly Actor self;
readonly CrateInfo info;
bool collected;
[Sync] int ticks;
[Sync] public CPos Location;
public Crate(ActorInitializer init, CrateInfo info)
{
this.self = init.Self;
this.info = info;
if (init.Contains<LocationInit>())
SetPosition(self, init.Get<LocationInit, CPos>());
}
public void WarnCrush(Actor crusher) { }
public void OnCrush(Actor crusher)
{
if (collected)
return;
var crateActions = self.TraitsImplementing<CrateAction>();
self.Destroy();
collected = true;
if (crateActions.Any())
{
var shares = crateActions.Select(a => Pair.New(a, a.GetSelectionSharesOuter(crusher)));
var totalShares = shares.Sum(a => a.Second);
var n = self.World.SharedRandom.Next(totalShares);
foreach (var s in shares)
{
if (n < s.Second)
{
s.First.Activate(crusher);
return;
}
else
n -= s.Second;
}
}
}
public void OnLanded()
{
// Check whether the crate landed on anything
var landedOn = self.World.ActorMap.GetUnitsAt(self.Location)
.Where(a => a != self);
if (!landedOn.Any())
return;
var collector = landedOn.FirstOrDefault(a =>
{
// Mobile is (currently) the only trait that supports crushing
var mi = a.Info.Traits.GetOrDefault<MobileInfo>();
if (mi == null)
return false;
// Make sure that the actor can collect this crate type
return CrushableBy(mi.Crushes, a.Owner);
});
// Destroy the crate if none of the units in the cell are valid collectors
if (collector != null)
OnCrush(collector);
else
self.Destroy();
}
public void Tick(Actor self)
{
if (info.Lifetime != 0 && ++ticks >= info.Lifetime * 25)
self.Destroy();
}
public CPos TopLeft { get { return Location; } }
public IEnumerable<Pair<CPos, SubCell>> OccupiedCells() { return new[] { Pair.New(Location, SubCell.FullCell) }; }
public WPos CenterPosition { get; private set; }
public void SetPosition(Actor self, WPos pos) { SetPosition(self, self.World.Map.CellContaining(pos)); }
public void SetVisualPosition(Actor self, WPos pos) { SetPosition(self, self.World.Map.CellContaining(pos)); }
public bool IsLeavingCell(CPos location, SubCell subCell = SubCell.Any) { return self.Location == location && ticks + 1 == info.Lifetime * 25; }
public SubCell GetValidSubCell(SubCell preferred = SubCell.Any) { return SubCell.FullCell; }
public SubCell GetAvailableSubCell(CPos cell, SubCell preferredSubCell = SubCell.Any, Actor ignoreActor = null, bool checkTransientActors = true)
{
if (!self.World.Map.Contains(cell))
return SubCell.Invalid;
var type = self.World.Map.GetTerrainInfo(cell).Type;
if (!info.TerrainTypes.Contains(type))
return SubCell.Invalid;
if (self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(cell) != null)
return SubCell.Invalid;
if (!checkTransientActors)
return SubCell.FullCell;
return !self.World.ActorMap.GetUnitsAt(cell)
.Where(x => x != ignoreActor)
.Any() ? SubCell.FullCell : SubCell.Invalid;
}
public bool CanEnterCell(CPos a, Actor ignoreActor = null, bool checkTransientActors = true)
{
return GetAvailableSubCell(a, SubCell.Any, ignoreActor, checkTransientActors) != SubCell.Invalid;
}
public void SetPosition(Actor self, CPos cell, SubCell subCell = SubCell.Any)
{
self.World.ActorMap.RemoveInfluence(self, this);
Location = cell;
CenterPosition = self.World.Map.CenterOfCell(cell);
if (self.IsInWorld)
{
self.World.ActorMap.UpdatePosition(self, this);
self.World.ScreenMap.Update(self);
}
}
public bool CrushableBy(string[] crushClasses, Player owner)
{
return crushClasses.Contains(info.CrushClass);
}
public void AddedToWorld(Actor self)
{
self.World.ActorMap.AddInfluence(self, this);
self.World.ActorMap.AddPosition(self, this);
self.World.ScreenMap.Add(self);
var cs = self.World.WorldActor.TraitOrDefault<CrateSpawner>();
if (cs != null)
cs.IncrementCrates();
}
public void RemovedFromWorld(Actor self)
{
self.World.ActorMap.RemoveInfluence(self, this);
self.World.ActorMap.RemovePosition(self, this);
self.World.ScreenMap.Remove(self);
var cs = self.World.WorldActor.TraitOrDefault<CrateSpawner>();
if (cs != null)
cs.DecrementCrates();
}
}
}

View File

@@ -0,0 +1,81 @@
#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.Mods.Common.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class CrateActionInfo : ITraitInfo
{
[Desc("Chance of getting this crate, assuming the collector is compatible.")]
public readonly int SelectionShares = 10;
[Desc("An animation defined in sequence yaml(s) to draw.")]
public readonly string Effect = null;
[Desc("Palette to draw the animation in.")]
public readonly string Palette = "effect";
[Desc("Audio clip to play when the crate is collected.")]
public readonly string Notification = null;
[Desc("The earliest time (in ticks) that this crate action can occur on.")]
public readonly int TimeDelay = 0;
[Desc("Only allow this crate action when the collector has these prerequisites")]
public readonly string[] Prerequisites = { };
[Desc("Actor types that this crate action will not occur for.")]
[ActorReference] public string[] ExcludedActorTypes = { };
public virtual object Create(ActorInitializer init) { return new CrateAction(init.Self, this); }
}
public class CrateAction
{
readonly Actor self;
readonly CrateActionInfo info;
public CrateAction(Actor self, CrateActionInfo info)
{
this.self = self;
this.info = info;
}
public int GetSelectionSharesOuter(Actor collector)
{
if (self.World.WorldTick < info.TimeDelay)
return 0;
if (info.ExcludedActorTypes.Contains(collector.Info.Name))
return 0;
if (info.Prerequisites.Any() && !collector.Owner.PlayerActor.Trait<TechTree>().HasPrerequisites(info.Prerequisites))
return 0;
return GetSelectionShares(collector);
}
public virtual int GetSelectionShares(Actor collector)
{
return info.SelectionShares;
}
public virtual void Activate(Actor collector)
{
Sound.PlayToPlayer(collector.Owner, info.Notification);
if (info.Effect != null)
collector.World.AddFrameEndTask(w => w.Add(new CrateEffect(collector, info.Effect, info.Palette)));
}
}
}

View File

@@ -0,0 +1,110 @@
#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;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Creates duplicates of the actor that collects the crate.")]
class DuplicateUnitCrateActionInfo : CrateActionInfo
{
[Desc("The maximum number of duplicates to make.")]
public readonly int MaxAmount = 2;
[Desc("The minimum number of duplicates to make. Overrules MaxDuplicatesWorth.")]
public readonly int MinAmount = 1;
[Desc("The maximum total value allowed for the duplicates.", "Duplication stops if the total worth will exceed this number.", "-1 = no limit")]
public readonly int MaxDuplicateValue = -1;
[Desc("The maximum radius (in cells) that duplicates can be spawned.")]
public readonly int MaxRadius = 4;
[Desc("The list of unit target types we are allowed to duplicate.")]
public readonly string[] ValidTargets = { "Ground", "Water" };
[Desc("Which races this crate action can occur for.")]
public readonly string[] ValidRaces = { };
[Desc("Is the new duplicates given to a specific owner, regardless of whom collected it?")]
public readonly string Owner = null;
public override object Create(ActorInitializer init) { return new DuplicateUnitCrateAction(init.Self, this); }
}
class DuplicateUnitCrateAction : CrateAction
{
readonly DuplicateUnitCrateActionInfo info;
public DuplicateUnitCrateAction(Actor self, DuplicateUnitCrateActionInfo info)
: base(self, info)
{
this.info = info;
}
public bool CanGiveTo(Actor collector)
{
if (info.ValidRaces.Any() && !info.ValidRaces.Contains(collector.Owner.Country.Race))
return false;
var targetable = collector.Info.Traits.GetOrDefault<ITargetableInfo>();
if (targetable == null || !info.ValidTargets.Intersect(targetable.GetTargetTypes()).Any())
return false;
var positionable = collector.TraitOrDefault<IPositionable>();
if (positionable == null)
return false;
return collector.World.Map.FindTilesInCircle(collector.Location, info.MaxRadius)
.Any(c => positionable.CanEnterCell(c));
}
public override int GetSelectionShares(Actor collector)
{
if (!CanGiveTo(collector))
return 0;
return base.GetSelectionShares(collector);
}
public override void Activate(Actor collector)
{
var positionable = collector.Trait<IPositionable>();
var candidateCells = collector.World.Map.FindTilesInCircle(collector.Location, info.MaxRadius)
.Where(c => positionable.CanEnterCell(c)).Shuffle(collector.World.SharedRandom)
.ToArray();
var duplicates = Math.Min(candidateCells.Length, info.MaxAmount);
// Restrict duplicate count to a maximum value
if (info.MaxDuplicateValue > 0)
{
var vi = collector.Info.Traits.GetOrDefault<ValuedInfo>();
if (vi != null && vi.Cost > 0)
duplicates = Math.Min(duplicates, info.MaxDuplicateValue / vi.Cost);
}
for (var i = 0; i < duplicates; i++)
{
var cell = candidateCells[i]; // Avoid modified closure bug
collector.World.AddFrameEndTask(w => w.CreateActor(collector.Info.Name, new TypeDictionary
{
new LocationInit(cell),
new OwnerInit(info.Owner ?? collector.Owner.InternalName)
}));
}
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,43 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Fires a weapon at the location when collected.")]
class ExplodeCrateActionInfo : CrateActionInfo
{
[Desc("The weapon to fire upon collection.")]
[WeaponReference] public string Weapon = null;
public override object Create(ActorInitializer init) { return new ExplodeCrateAction(init.Self, this); }
}
class ExplodeCrateAction : CrateAction
{
readonly ExplodeCrateActionInfo info;
public ExplodeCrateAction(Actor self, ExplodeCrateActionInfo info)
: base(self, info)
{
this.info = info;
}
public override void Activate(Actor collector)
{
var weapon = collector.World.Map.Rules.Weapons[info.Weapon.ToLowerInvariant()];
weapon.Impact(Target.FromPos(collector.CenterPosition), collector, Enumerable.Empty<int>());
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,50 @@
#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 OpenRA.Mods.Common.Effects;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Gives cash to the collector.")]
class GiveCashCrateActionInfo : CrateActionInfo
{
[Desc("Amount of cash to give.")]
public int Amount = 2000;
[Desc("Should the collected amount be displayed as a cash tick?")]
public bool UseCashTick = false;
public override object Create(ActorInitializer init) { return new GiveCashCrateAction(init.Self, this); }
}
class GiveCashCrateAction : CrateAction
{
readonly GiveCashCrateActionInfo info;
public GiveCashCrateAction(Actor self, GiveCashCrateActionInfo info)
: base(self, info)
{
this.info = info;
}
public override void Activate(Actor collector)
{
collector.World.AddFrameEndTask(w =>
{
collector.Owner.PlayerActor.Trait<PlayerResources>().GiveCash(info.Amount);
if (info.UseCashTick)
w.Add(new FloatingText(collector.CenterPosition, collector.Owner.Color.RGB, FloatingText.FormatCashTick(info.Amount), 30));
});
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,45 @@
#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;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawns units when collected.", "Adjust selection shares when player has no base.")]
class GiveMcvCrateActionInfo : GiveUnitCrateActionInfo
{
[Desc("The selection shares to use if the collector has no base.")]
public int NoBaseSelectionShares = 1000;
public override object Create(ActorInitializer init) { return new GiveMcvCrateAction(init.Self, this); }
}
class GiveMcvCrateAction : GiveUnitCrateAction
{
readonly GiveMcvCrateActionInfo info;
public GiveMcvCrateAction(Actor self, GiveMcvCrateActionInfo info)
: base(self, info)
{
this.info = info;
}
public override int GetSelectionShares(Actor collector)
{
// There's some other really good reason why we shouldn't give this.
if (!CanGiveTo(collector))
return 0;
var hasBase = collector.World.ActorsWithTrait<BaseBuilding>()
.Any(a => a.Actor.Owner == collector.Owner);
return hasBase ? info.SelectionShares : info.NoBaseSelectionShares;
}
}
}

View File

@@ -0,0 +1,113 @@
#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.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Spawns units when collected.")]
class GiveUnitCrateActionInfo : CrateActionInfo
{
[Desc("The list of units to spawn.")]
[ActorReference]
public readonly string[] Units = { };
[Desc("Races that are allowed to trigger this action")]
public readonly string[] ValidRaces = { };
[Desc("Override the owner of the newly spawned unit: e.g. Creeps or Neutral")]
public readonly string Owner = null;
public override object Create(ActorInitializer init) { return new GiveUnitCrateAction(init.Self, this); }
}
class GiveUnitCrateAction : CrateAction
{
readonly Actor self;
readonly GiveUnitCrateActionInfo info;
readonly List<CPos> usedCells = new List<CPos>();
public GiveUnitCrateAction(Actor self, GiveUnitCrateActionInfo info)
: base(self, info)
{
this.self = self;
this.info = info;
if (!info.Units.Any())
throw new YamlException("A GiveUnitCrateAction does not specify any units to give. This might be because the yaml is referring to 'Unit' rather than 'Units'.");
}
public bool CanGiveTo(Actor collector)
{
if (info.ValidRaces.Any() && !info.ValidRaces.Contains(collector.Owner.Country.Race))
return false;
foreach (string unit in info.Units)
{
// avoid dumping tanks in the sea, and ships on dry land.
if (!GetSuitableCells(collector.Location, unit).Any())
return false;
}
return true;
}
public override int GetSelectionShares(Actor collector)
{
if (!CanGiveTo(collector))
return 0;
return base.GetSelectionShares(collector);
}
public override void Activate(Actor collector)
{
foreach (var u in info.Units)
{
var unit = u; // avoiding access to modified closure
var location = ChooseEmptyCellNear(collector, unit);
if (location != null)
{
usedCells.Add(location.Value);
collector.World.AddFrameEndTask(
w => w.CreateActor(unit, new TypeDictionary
{
new LocationInit(location.Value),
new OwnerInit(info.Owner ?? collector.Owner.InternalName)
}));
}
}
base.Activate(collector);
}
IEnumerable<CPos> GetSuitableCells(CPos near, string unitName)
{
var mi = self.World.Map.Rules.Actors[unitName].Traits.Get<MobileInfo>();
for (var i = -1; i < 2; i++)
for (var j = -1; j < 2; j++)
if (mi.CanEnterCell(self.World, self, near + new CVec(i, j)))
yield return near + new CVec(i, j);
}
CPos? ChooseEmptyCellNear(Actor a, string unit)
{
var possibleCells = GetSuitableCells(a.Location, unit).Where(c => !usedCells.Contains(c)).ToArray();
if (possibleCells.Length == 0)
return null;
return possibleCells.Random(self.World.SharedRandom);
}
}
}

View File

@@ -0,0 +1,88 @@
#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;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Grants an upgrade to the collector.")]
public class GrantUpgradeCrateActionInfo : CrateActionInfo
{
[Desc("The upgrades to apply.")]
public readonly string[] Upgrades = { };
[Desc("Duration of the upgrade (in ticks). Set to 0 for a permanent upgrade.")]
public readonly int Duration = 0;
[Desc("The range to search for extra collectors in.", "Extra collectors will also be granted the crate action.")]
public readonly WRange Range = new WRange(3);
[Desc("The maximum number of extra collectors to grant the crate action to.", "-1 = no limit")]
public readonly int MaxExtraCollectors = 4;
public override object Create(ActorInitializer init) { return new GrantUpgradeCrateAction(init.Self, this); }
}
public class GrantUpgradeCrateAction : CrateAction
{
readonly Actor self;
readonly GrantUpgradeCrateActionInfo info;
public GrantUpgradeCrateAction(Actor self, GrantUpgradeCrateActionInfo info)
: base(self, info)
{
this.self = self;
this.info = info;
}
bool AcceptsUpgrade(Actor a)
{
var um = a.TraitOrDefault<UpgradeManager>();
return um != null && info.Upgrades.Any(u => um.AcceptsUpgrade(a, u));
}
public override int GetSelectionShares(Actor collector)
{
return AcceptsUpgrade(collector) ? info.SelectionShares : 0;
}
public override void Activate(Actor collector)
{
var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, info.Range)
.Where(a => a != self && a != collector && a.Owner == collector.Owner && AcceptsUpgrade(a));
if (info.MaxExtraCollectors > -1)
actorsInRange = actorsInRange.Take(info.MaxExtraCollectors);
collector.World.AddFrameEndTask(w =>
{
foreach (var a in actorsInRange.Append(collector))
{
if (!a.IsInWorld || a.IsDead)
continue;
var um = a.TraitOrDefault<UpgradeManager>();
foreach (var u in info.Upgrades)
{
if (!um.AcceptsUpgrade(a, u))
continue;
if (info.Duration > 0)
um.GrantTimedUpgrade(a, u, info.Duration);
else
um.GrantUpgrade(a, u, this);
}
}
});
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,39 @@
#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.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Heals all actors that belong to the owner of the collector.")]
class HealUnitsCrateActionInfo : CrateActionInfo
{
public override object Create(ActorInitializer init) { return new HealUnitsCrateAction(init.Self, this); }
}
class HealUnitsCrateAction : CrateAction
{
public HealUnitsCrateAction(Actor self, HealUnitsCrateActionInfo info)
: base(self, info) { }
public override void Activate(Actor collector)
{
foreach (var unit in collector.World.Actors.Where(a => a.Owner == collector.Owner))
{
var health = unit.TraitOrDefault<Health>();
if (health != null && !health.IsDead)
health.InflictDamage(unit, unit, -(health.MaxHP - health.HP), null, true);
}
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,41 @@
#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
namespace OpenRA.Mods.Common.Traits
{
[Desc("Hides the entire map in shroud.")]
class HideMapCrateActionInfo : CrateActionInfo
{
public override object Create(ActorInitializer init) { return new HideMapCrateAction(init.Self, this); }
}
class HideMapCrateAction : CrateAction
{
public HideMapCrateAction(Actor self, HideMapCrateActionInfo info)
: base(self, info) { }
public override int GetSelectionShares(Actor collector)
{
// don't ever hide the map for people who have GPS.
if (collector.Owner.Shroud.HasFogVisibility())
return 0;
return base.GetSelectionShares(collector);
}
public override void Activate(Actor collector)
{
if (collector.Owner == collector.World.LocalPlayer)
collector.Owner.Shroud.ResetExploration();
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,83 @@
#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;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Gives experience levels to the collector.")]
class LevelUpCrateActionInfo : CrateActionInfo
{
[Desc("Number of experience levels to give.")]
public readonly int Levels = 1;
[Desc("The range to search for extra collectors in.", "Extra collectors will also be granted the crate action.")]
public readonly WRange Range = new WRange(3);
[Desc("The maximum number of extra collectors to grant the crate action to.")]
public readonly int MaxExtraCollectors = 4;
public override object Create(ActorInitializer init) { return new LevelUpCrateAction(init.Self, this); }
}
class LevelUpCrateAction : CrateAction
{
readonly Actor self;
readonly LevelUpCrateActionInfo info;
public LevelUpCrateAction(Actor self, LevelUpCrateActionInfo info)
: base(self, info)
{
this.self = self;
this.info = info;
}
public override int GetSelectionShares(Actor collector)
{
var ge = collector.TraitOrDefault<GainsExperience>();
return ge != null && ge.CanGainLevel ? info.SelectionShares : 0;
}
public override void Activate(Actor collector)
{
var inRange = self.World.FindActorsInCircle(self.CenterPosition, info.Range).Where(a =>
{
// Don't upgrade the same unit twice
if (a == collector)
return false;
// Only upgrade the collecting player's units
// TODO: Also apply to allied units?
if (a.Owner != collector.Owner)
return false;
// Ignore units that can't level up
var ge = a.TraitOrDefault<GainsExperience>();
return ge != null && ge.CanGainLevel;
});
if (info.MaxExtraCollectors > -1)
inRange = inRange.Take(info.MaxExtraCollectors);
foreach (var actor in inRange.Append(collector))
{
var recipient = actor; // loop variable in closure hazard
recipient.World.AddFrameEndTask(w =>
{
var gainsExperience = recipient.TraitOrDefault<GainsExperience>();
if (gainsExperience != null)
gainsExperience.GiveLevels(((LevelUpCrateActionInfo)info).Levels);
});
}
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,51 @@
#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 OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Reveals the entire map.")]
class RevealMapCrateActionInfo : CrateActionInfo
{
[Desc("Should the map also be revealed for the allies of the collector's owner.")]
public readonly bool IncludeAllies = false;
public override object Create(ActorInitializer init) { return new RevealMapCrateAction(init.Self, this); }
}
class RevealMapCrateAction : CrateAction
{
readonly RevealMapCrateActionInfo info;
public RevealMapCrateAction(Actor self, RevealMapCrateActionInfo info)
: base(self, info)
{
this.info = info;
}
bool ShouldReveal(Player collectingPlayer)
{
if (info.IncludeAllies)
return collectingPlayer.World.LocalPlayer != null &&
collectingPlayer.Stances[collectingPlayer.World.LocalPlayer] == Stance.Ally;
return collectingPlayer == collectingPlayer.World.LocalPlayer;
}
public override void Activate(Actor collector)
{
if (ShouldReveal(collector.Owner))
collector.Owner.Shroud.ExploreAll(collector.World);
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,46 @@
#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 OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("Gives a supportpower to the collector.")]
class SupportPowerCrateActionInfo : CrateActionInfo
{
[Desc("Which proxy actor, which grants the support power, to spawn.")]
[ActorReference] public readonly string Proxy = null;
public override object Create(ActorInitializer init) { return new SupportPowerCrateAction(init.Self, this); }
}
class SupportPowerCrateAction : CrateAction
{
SupportPowerCrateActionInfo info;
public SupportPowerCrateAction(Actor self, SupportPowerCrateActionInfo info)
: base(self, info)
{
this.info = info;
}
// The free unit crate requires same race and the actor needs to be mobile.
// We want neither of these properties for crate power proxies.
public override void Activate(Actor collector)
{
collector.World.AddFrameEndTask(w => w.CreateActor(info.Proxy, new TypeDictionary
{
new OwnerInit(collector.Owner)
}));
base.Activate(collector);
}
}
}

View File

@@ -0,0 +1,184 @@
#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;
using System.Linq;
using OpenRA.Mods.Common.Activities;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
public class CrateSpawnerInfo : ITraitInfo
{
[Desc("Minimum number of crates")]
public readonly int Minimum = 1;
[Desc("Maximum number of crates")]
public readonly int Maximum = 255;
[Desc("Average time (seconds) between crate spawn")]
public readonly int SpawnInterval = 180;
[Desc("Which terrain types can we drop on?")]
public readonly string[] ValidGround = { "Clear", "Rough", "Road", "Ore", "Beach" };
[Desc("Which terrain types count as water?")]
public readonly string[] ValidWater = { "Water" };
[Desc("Chance of generating a water crate instead of a land crate")]
public readonly float WaterChance = .2f;
[ActorReference]
[Desc("Crate actors to drop")]
public readonly string[] CrateActors = { "crate" };
[Desc("Chance of each crate actor spawning")]
public readonly int[] CrateActorShares = { 10 };
[ActorReference]
[Desc("If a DeliveryAircraft: is specified, then this actor will deliver crates")]
public readonly string DeliveryAircraft = null;
[Desc("Number of facings that the delivery aircraft may approach from.")]
public readonly int QuantizedFacings = 32;
[Desc("Spawn and remove the plane this far outside the map.")]
public readonly WRange Cordon = new WRange(5120);
public object Create(ActorInitializer init) { return new CrateSpawner(this, init.Self); }
}
public class CrateSpawner : ITick
{
int crates = 0;
int ticks = 0;
CrateSpawnerInfo info;
Actor self;
public CrateSpawner(CrateSpawnerInfo info, Actor self)
{
this.info = info;
this.self = self;
}
public void Tick(Actor self)
{
if (!self.World.LobbyInfo.GlobalSettings.Crates)
return;
if (--ticks <= 0)
{
ticks = info.SpawnInterval * 25;
var toSpawn = Math.Max(0, info.Minimum - crates)
+ (crates < info.Maximum ? 1 : 0);
for (var n = 0; n < toSpawn; n++)
SpawnCrate(self);
}
}
void SpawnCrate(Actor self)
{
var threshold = 100;
var inWater = self.World.SharedRandom.NextFloat() < info.WaterChance;
var pp = ChooseDropCell(self, inWater, threshold);
if (pp == null)
return;
var p = pp.Value;
var crateActor = ChooseCrateActor();
self.World.AddFrameEndTask(w =>
{
if (info.DeliveryAircraft != null)
{
var crate = w.CreateActor(false, crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner) });
var dropFacing = Util.QuantizeFacing(self.World.SharedRandom.Next(256), info.QuantizedFacings) * (256 / info.QuantizedFacings);
var delta = new WVec(0, -1024, 0).Rotate(WRot.FromFacing(dropFacing));
var altitude = self.World.Map.Rules.Actors[info.DeliveryAircraft].Traits.Get<PlaneInfo>().CruiseAltitude.Range;
var target = self.World.Map.CenterOfCell(p) + new WVec(0, 0, altitude);
var startEdge = target - (self.World.Map.DistanceToEdge(target, -delta) + info.Cordon).Range * delta / 1024;
var finishEdge = target + (self.World.Map.DistanceToEdge(target, delta) + info.Cordon).Range * delta / 1024;
var plane = w.CreateActor(info.DeliveryAircraft, new TypeDictionary
{
new CenterPositionInit(startEdge + new WVec(0, 0, altitude)),
new OwnerInit(self.Owner),
new FacingInit(dropFacing),
});
var drop = plane.Trait<ParaDrop>();
drop.SetLZ(p, true);
plane.Trait<Cargo>().Load(plane, crate);
plane.CancelActivity();
plane.QueueActivity(new Fly(plane, Target.FromPos(finishEdge)));
plane.QueueActivity(new RemoveSelf());
}
else
{
w.CreateActor(crateActor, new TypeDictionary { new OwnerInit(w.WorldActor.Owner), new LocationInit(p) });
}
});
}
CPos? ChooseDropCell(Actor self, bool inWater, int maxTries)
{
for (var n = 0; n < maxTries; n++)
{
var p = self.World.Map.ChooseRandomCell(self.World.SharedRandom);
// Is this valid terrain?
var terrainType = self.World.Map.GetTerrainInfo(p).Type;
if (!(inWater ? info.ValidWater : info.ValidGround).Contains(terrainType))
continue;
// Don't drop on any actors
if (self.World.WorldActor.Trait<BuildingInfluence>().GetBuildingAt(p) != null
|| self.World.ActorMap.GetUnitsAt(p).Any())
continue;
return p;
}
return null;
}
string ChooseCrateActor()
{
var crateShares = info.CrateActorShares;
var n = self.World.SharedRandom.Next(crateShares.Sum());
var cumulativeShares = 0;
for (var i = 0; i < crateShares.Length; i++)
{
cumulativeShares += crateShares[i];
if (n <= cumulativeShares)
return info.CrateActors[i];
}
return null;
}
public void IncrementCrates()
{
crates++;
}
public void DecrementCrates()
{
crates--;
}
}
}