Skip to content

Commit

Permalink
feat: ActorSpawnManager.SpawnInterval supports 1 or 2 values
Browse files Browse the repository at this point in the history
Providing 2 values creates a range from which a value is randomly selected.
  • Loading branch information
phrohdoh committed Oct 31, 2020
1 parent 7b75a78 commit c89661f
Showing 1 changed file with 19 additions and 4 deletions.
23 changes: 19 additions & 4 deletions OpenRA.Mods.Common/Traits/World/ActorSpawnManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,21 @@
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
using OpenRA.Mods.Common;

namespace OpenRA.Mods.Common.Traits
{
[Desc("Controls the spawning of specified actor types. Attach this to the world actor.")]
public class ActorSpawnManagerInfo : ConditionalTraitInfo, Requires<MapCreepsInfo>
public class ActorSpawnManagerInfo : ConditionalTraitInfo, Requires<MapCreepsInfo>, IRulesetLoaded
{
[Desc("Minimum number of actors.")]
public readonly int Minimum = 0;

[Desc("Maximum number of actors.")]
public readonly int Maximum = 4;

[Desc("Time (in ticks) between actor spawn.")]
public readonly int SpawnInterval = 6000;
[Desc("Time (in ticks) between actor spawn. Supports 1 or 2 values.\nIf 2 values are provided they are used as a range from which a value is randomly selected.")]
public readonly int[] SpawnInterval = { 6000 };

[FieldLoader.Require]
[ActorReference]
Expand All @@ -38,6 +39,20 @@ public class ActorSpawnManagerInfo : ConditionalTraitInfo, Requires<MapCreepsInf
[Desc("Type of ActorSpawner with which it connects.")]
public readonly HashSet<string> Types = new HashSet<string>() { };

public override void RulesetLoaded(Ruleset rules, ActorInfo ai)
{
base.RulesetLoaded(rules, ai);

if (SpawnInterval.Length == 0 || SpawnInterval.Length > 2)
throw new YamlException("{0}.{1} must be either 1 or 2 values".F(nameof(ActorSpawnManager), nameof(SpawnInterval)));

if (SpawnInterval.Length == 2 && SpawnInterval[0] >= SpawnInterval[1])
throw new YamlException("{0}.{1}'s first value must be less than the second value".F(nameof(ActorSpawnManager), nameof(SpawnInterval)));

if (SpawnInterval.Any(it => it < 0))
throw new YamlException("{0}.{1}'s value(s) must not be less than 0".F(nameof(ActorSpawnManager), nameof(SpawnInterval)));
}

public override object Create(ActorInitializer init) { return new ActorSpawnManager(init.Self, this); }
}

Expand Down Expand Up @@ -77,7 +92,7 @@ void ITick.Tick(Actor self)
if (spawnPoint == null)
return;

spawnCountdown = info.SpawnInterval;
spawnCountdown = Util.RandomDelay(self.World, info.SpawnInterval);

do
{
Expand Down

0 comments on commit c89661f

Please sign in to comment.