Generalize WormManager into ActorSpawnManager.

Added support of multiple actors, conditions and types.
This commit is contained in:
Voidwalker
2017-12-28 12:18:29 +00:00
committed by reaperrr
parent e69cf4fd5c
commit 711bad91a3
15 changed files with 178 additions and 144 deletions

View File

@@ -552,6 +552,8 @@
<Compile Include="Traits\World\TerrainGeometryOverlay.cs" /> <Compile Include="Traits\World\TerrainGeometryOverlay.cs" />
<Compile Include="Traits\World\WarheadDebugOverlay.cs" /> <Compile Include="Traits\World\WarheadDebugOverlay.cs" />
<Compile Include="Traits\World\WeatherOverlay.cs" /> <Compile Include="Traits\World\WeatherOverlay.cs" />
<Compile Include="Traits\World\ActorSpawnManager.cs" />
<Compile Include="Traits\ActorSpawner.cs" />
<Compile Include="UtilityCommands\CheckYaml.cs" /> <Compile Include="UtilityCommands\CheckYaml.cs" />
<Compile Include="UtilityCommands\ConvertPngToShpCommand.cs" /> <Compile Include="UtilityCommands\ConvertPngToShpCommand.cs" />
<Compile Include="UtilityCommands\ConvertSpriteToPngCommand.cs" /> <Compile Include="UtilityCommands\ConvertSpriteToPngCommand.cs" />

View File

@@ -0,0 +1,34 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits
{
[Desc("An actor with this trait indicates a valid spawn point for actors of ActorSpawnManager.")]
public class ActorSpawnerInfo : ConditionalTraitInfo
{
[Desc("Type of ActorSpawner with which it connects.")]
public readonly HashSet<string> Types = new HashSet<string>() { };
public override object Create(ActorInitializer init) { return new ActorSpawner(this); }
}
public class ActorSpawner : ConditionalTrait<ActorSpawnerInfo>
{
public ActorSpawner(ActorSpawnerInfo info)
: base(info) { }
public HashSet<string> Types { get { return Info.Types; } }
}
}

View File

@@ -0,0 +1,116 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Primitives;
using OpenRA.Traits;
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>
{
[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;
[FieldLoader.Require]
[ActorReference]
[Desc("Name of the actor that will be randomly picked to spawn.")]
public readonly string[] Actors = { };
public readonly string Owner = "Creeps";
[Desc("Type of ActorSpawner with which it connects.")]
public readonly HashSet<string> Types = new HashSet<string>() { };
public override object Create(ActorInitializer init) { return new ActorSpawnManager(init.Self, this); }
}
public class ActorSpawnManager : ConditionalTrait<ActorSpawnManagerInfo>, ITick, INotifyCreated
{
readonly ActorSpawnManagerInfo info;
TraitPair<ActorSpawner>[] spawnPointActors;
bool enabled;
int spawnCountdown;
int actorsPresent;
public ActorSpawnManager(Actor self, ActorSpawnManagerInfo info) : base(info)
{
this.info = info;
}
void INotifyCreated.Created(Actor self)
{
self.World.AddFrameEndTask(w =>
{
spawnPointActors = w.ActorsWithTrait<ActorSpawner>()
.Where(x => info.Types.Overlaps(x.Trait.Types) || !x.Trait.Types.Any())
.ToArray();
enabled = self.Trait<MapCreeps>().Enabled && spawnPointActors.Any();
});
}
void ITick.Tick(Actor self)
{
if (IsTraitDisabled || !enabled)
return;
if (info.Maximum < 1 || actorsPresent >= info.Maximum)
return;
if (--spawnCountdown > 0 && actorsPresent >= info.Minimum)
return;
spawnCountdown = info.SpawnInterval;
do
{
// Always spawn at least one actor, plus
// however many needed to reach the minimum.
SpawnActor(self);
} while (actorsPresent < info.Minimum);
}
WPos SpawnActor(Actor self)
{
var spawnPoint = GetRandomSpawnPoint(self.World.SharedRandom);
self.World.AddFrameEndTask(w => w.CreateActor(info.Actors.Random(self.World.SharedRandom), new TypeDictionary
{
new OwnerInit(w.Players.First(x => x.PlayerName == info.Owner)),
new LocationInit(spawnPoint.Location)
}));
actorsPresent++;
return spawnPoint.CenterPosition;
}
Actor GetRandomSpawnPoint(Support.MersenneTwister random)
{
return spawnPointActors.Random(random).Actor;
}
public void DecreaseActorCount()
{
actorsPresent--;
}
}
}

View File

@@ -1635,6 +1635,21 @@ namespace OpenRA.Mods.Common.UtilityCommands
} }
} }
if (engineVersion < 20180225)
{
if (node.Key == "WormSpawner")
RenameNodeKey(node, "ActorSpawner");
if (node.Key == "WormManager")
{
RenameNodeKey(node, "ActorSpawnManager");
var wormSignature = node.Value.Nodes.FirstOrDefault(n => n.Key == "WormSignature");
if (wormSignature != null)
wormSignature.Key = "Actors";
}
}
UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1); UpgradeActorRules(modData, engineVersion, ref node.Value.Nodes, node, depth + 1);
} }

View File

@@ -80,9 +80,7 @@
<Compile Include="Traits\World\FogPaletteFromR8.cs" /> <Compile Include="Traits\World\FogPaletteFromR8.cs" />
<Compile Include="Traits\World\PaletteFromR8.cs" /> <Compile Include="Traits\World\PaletteFromR8.cs" />
<Compile Include="Traits\World\PaletteFromScaledPalette.cs" /> <Compile Include="Traits\World\PaletteFromScaledPalette.cs" />
<Compile Include="Traits\World\WormManager.cs" />
<Compile Include="Traits\AttractsWorms.cs" /> <Compile Include="Traits\AttractsWorms.cs" />
<Compile Include="Traits\WormSpawner.cs" />
<Compile Include="Warheads\ChangeOwnerWarhead.cs" /> <Compile Include="Warheads\ChangeOwnerWarhead.cs" />
<Compile Include="UtilityCommands\D2kMapImporter.cs" /> <Compile Include="UtilityCommands\D2kMapImporter.cs" />
<Compile Include="UtilityCommands\ImportD2kMapCommand.cs" /> <Compile Include="UtilityCommands\ImportD2kMapCommand.cs" />

View File

@@ -37,7 +37,7 @@ namespace OpenRA.Mods.D2k.Traits
{ {
public readonly SandwormInfo WormInfo; public readonly SandwormInfo WormInfo;
readonly WormManager manager; readonly ActorSpawnManager manager;
readonly Mobile mobile; readonly Mobile mobile;
readonly AttackBase attackTrait; readonly AttackBase attackTrait;
@@ -54,7 +54,7 @@ namespace OpenRA.Mods.D2k.Traits
WormInfo = info; WormInfo = info;
mobile = self.Trait<Mobile>(); mobile = self.Trait<Mobile>();
attackTrait = self.Trait<AttackBase>(); attackTrait = self.Trait<AttackBase>();
manager = self.World.WorldActor.Trait<WormManager>(); manager = self.World.WorldActor.Trait<ActorSpawnManager>();
} }
public override void DoAction(Actor self, CPos targetCell) public override void DoAction(Actor self, CPos targetCell)
@@ -140,7 +140,7 @@ namespace OpenRA.Mods.D2k.Traits
if (disposed) if (disposed)
return; return;
manager.DecreaseWormCount(); manager.DecreaseActorCount();
disposed = true; disposed = true;
} }
} }

View File

@@ -1,113 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("Controls the spawning of sandworms. Attach this to the world actor.")]
class WormManagerInfo : ITraitInfo, Requires<MapCreepsInfo>
{
[Desc("Minimum number of worms")]
public readonly int Minimum = 0;
[Desc("Maximum number of worms")]
public readonly int Maximum = 4;
[Desc("Time (in ticks) between worm spawn.")]
public readonly int SpawnInterval = 6000;
[Desc("Name of the actor that will be spawned.")]
public readonly string WormSignature = "sandworm";
public readonly string WormSignNotification = "WormSign";
public readonly string WormOwnerPlayer = "Creeps";
public object Create(ActorInitializer init) { return new WormManager(init.Self, this); }
}
class WormManager : ITick, INotifyCreated
{
readonly WormManagerInfo info;
readonly Lazy<Actor[]> spawnPointActors;
bool enabled;
int spawnCountdown;
int wormsPresent;
public WormManager(Actor self, WormManagerInfo info)
{
this.info = info;
spawnPointActors = Exts.Lazy(() => self.World.ActorsHavingTrait<WormSpawner>().ToArray());
}
void INotifyCreated.Created(Actor self)
{
enabled = self.Trait<MapCreeps>().Enabled;
}
void ITick.Tick(Actor self)
{
if (!enabled)
return;
if (!spawnPointActors.Value.Any())
return;
// Apparently someone doesn't want worms or the maximum number of worms has been reached
if (info.Maximum < 1 || wormsPresent >= info.Maximum)
return;
if (--spawnCountdown > 0 && wormsPresent >= info.Minimum)
return;
spawnCountdown = info.SpawnInterval;
var wormLocations = new List<WPos>();
do
{
// Always spawn at least one worm, plus however many
// more we need to reach the defined minimum count.
wormLocations.Add(SpawnWorm(self));
} while (wormsPresent < info.Minimum);
}
WPos SpawnWorm(Actor self)
{
var spawnPoint = GetRandomSpawnPoint(self);
self.World.AddFrameEndTask(w => w.CreateActor(info.WormSignature, new TypeDictionary
{
new OwnerInit(w.Players.First(x => x.PlayerName == info.WormOwnerPlayer)),
new LocationInit(spawnPoint.Location)
}));
wormsPresent++;
return spawnPoint.CenterPosition;
}
Actor GetRandomSpawnPoint(Actor self)
{
return spawnPointActors.Value.Random(self.World.SharedRandom);
}
public void DecreaseWormCount()
{
wormsPresent--;
}
}
}

View File

@@ -1,19 +0,0 @@
#region Copyright & License Information
/*
* Copyright 2007-2018 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, either version 3 of
* the License, or (at your option) any later version. For more
* information, see COPYING.
*/
#endregion
using OpenRA.Traits;
namespace OpenRA.Mods.D2k.Traits
{
[Desc("An actor with this trait indicates a valid spawn point for sandworms.")]
class WormSpawnerInfo : TraitInfo<WormSpawner> { }
class WormSpawner { }
}

View File

@@ -1,4 +1,4 @@
World: World:
WormManager: ActorSpawnManager:
Minimum: 1 Minimum: 1
Maximum: 2 Maximum: 2

View File

@@ -1,4 +1,4 @@
World: World:
WormManager: ActorSpawnManager:
Minimum: 3 Minimum: 3
Maximum: 6 Maximum: 6

View File

@@ -1,4 +1,4 @@
World: World:
WormManager: ActorSpawnManager:
Minimum: 1 Minimum: 1
Maximum: 2 Maximum: 2

View File

@@ -8,7 +8,7 @@ World:
-MPStartLocations: -MPStartLocations:
ResourceType@Spice: ResourceType@Spice:
ValuePerUnit: 0 ValuePerUnit: 0
WormManager: ActorSpawnManager:
Minimum: 1 Minimum: 1
Maximum: 3 Maximum: 3
MusicPlaylist: MusicPlaylist:

View File

@@ -16,7 +16,7 @@ World:
-MPStartLocations: -MPStartLocations:
ObjectivesPanel: ObjectivesPanel:
PanelName: MISSION_OBJECTIVES PanelName: MISSION_OBJECTIVES
WormManager: ActorSpawnManager:
Minimum: 1 Minimum: 1
Maximum: 1 Maximum: 1
MapCreeps: MapCreeps:

View File

@@ -204,7 +204,7 @@ wormspawner:
WithSpriteBody: WithSpriteBody:
BodyOrientation: BodyOrientation:
QuantizedFacings: 1 QuantizedFacings: 1
WormSpawner: ActorSpawner:
EditorTilesetFilter: EditorTilesetFilter:
Categories: System Categories: System

View File

@@ -72,7 +72,8 @@ World:
BuildingInfluence: BuildingInfluence:
ProductionQueueFromSelection: ProductionQueueFromSelection:
ProductionPaletteWidget: PRODUCTION_PALETTE ProductionPaletteWidget: PRODUCTION_PALETTE
WormManager: ActorSpawnManager:
Actors: sandworm
CrateSpawner: CrateSpawner:
Minimum: 0 Minimum: 0
Maximum: 2 Maximum: 2