Add WormAttack notification, fix worm min count and spawn interval

Enhance usage of WormManager Minimum and Maximum properties, add escape radius to AttackSwallow
This commit is contained in:
penev92
2014-12-03 06:29:49 +02:00
parent b4d1a605da
commit f57625f9e6
8 changed files with 89 additions and 54 deletions

View File

@@ -21,17 +21,19 @@ namespace OpenRA.Mods.D2k
[Desc("The number of ticks it takes to get in place under the target to attack.")] [Desc("The number of ticks it takes to get in place under the target to attack.")]
public int AttackTime = 30; public int AttackTime = 30;
public readonly string WormAttackNotification = "WormAttack";
public override object Create(ActorInitializer init) { return new AttackSwallow(init.self, this); } public override object Create(ActorInitializer init) { return new AttackSwallow(init.self, this); }
} }
class AttackSwallow : AttackFrontal class AttackSwallow : AttackFrontal
{ {
public readonly AttackSwallowInfo AttackSwallowInfo; new public readonly AttackSwallowInfo Info;
public AttackSwallow(Actor self, AttackSwallowInfo attackSwallowInfo) public AttackSwallow(Actor self, AttackSwallowInfo info)
: base(self, attackSwallowInfo) : base(self, info)
{ {
AttackSwallowInfo = attackSwallowInfo; Info = info;
} }
public override void DoAttack(Actor self, Target target) public override void DoAttack(Actor self, Target target)

View File

@@ -8,11 +8,12 @@
*/ */
#endregion #endregion
using System; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Mods.Common;
using OpenRA.Mods.Common.Traits;
using OpenRA.Mods.RA.Move; using OpenRA.Mods.RA.Move;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.D2k namespace OpenRA.Mods.D2k
@@ -21,9 +22,13 @@ namespace OpenRA.Mods.D2k
class SwallowActor : Activity class SwallowActor : Activity
{ {
const int NearEnough = 1;
readonly CPos location;
readonly Target target; readonly Target target;
readonly WeaponInfo weapon; readonly WeaponInfo weapon;
readonly RenderUnit renderUnit; readonly RenderUnit renderUnit;
readonly RadarPings radarPings;
readonly AttackSwallow swallow; readonly AttackSwallow swallow;
readonly IPositionable positionable; readonly IPositionable positionable;
@@ -34,18 +39,23 @@ namespace OpenRA.Mods.D2k
{ {
this.target = target; this.target = target;
this.weapon = weapon; this.weapon = weapon;
positionable = self.TraitOrDefault<Mobile>(); positionable = self.Trait<Mobile>();
swallow = self.TraitOrDefault<AttackSwallow>(); swallow = self.Trait<AttackSwallow>();
renderUnit = self.TraitOrDefault<RenderUnit>(); renderUnit = self.Trait<RenderUnit>();
countdown = swallow.AttackSwallowInfo.AttackTime; radarPings = self.World.WorldActor.TraitOrDefault<RadarPings>();
countdown = swallow.Info.AttackTime;
renderUnit.DefaultAnimation.ReplaceAnim("burrowed"); renderUnit.DefaultAnimation.ReplaceAnim("burrowed");
stance = AttackState.Burrowed; stance = AttackState.Burrowed;
location = target.Actor.Location;
} }
bool WormAttack(Actor worm) bool WormAttack(Actor worm)
{ {
var targetLocation = target.Actor.Location; var targetLocation = target.Actor.Location;
// The target has moved too far away
if ((location - targetLocation).Length > NearEnough)
return false;
var lunch = worm.World.ActorMap.GetUnitsAt(targetLocation) var lunch = worm.World.ActorMap.GetUnitsAt(targetLocation)
.Where(t => !t.Equals(worm) && weapon.IsValidAgainst(t, worm)); .Where(t => !t.Equals(worm) && weapon.IsValidAgainst(t, worm));
@@ -54,11 +64,17 @@ namespace OpenRA.Mods.D2k
stance = AttackState.EmergingAboveGround; stance = AttackState.EmergingAboveGround;
lunch.Do(t => t.World.AddFrameEndTask(_ => { t.World.Remove(t); t.Kill(t); })); // Dispose of the evidence (we don't want husks) foreach (var actor in lunch)
actor.World.AddFrameEndTask(_ => actor.Destroy());
positionable.SetPosition(worm, targetLocation); positionable.SetPosition(worm, targetLocation);
PlayAttackAnimation(worm); PlayAttackAnimation(worm);
var attackPosition = worm.CenterPosition;
var affectedPlayers = lunch.Select(x => x.Owner).Distinct();
foreach (var affectedPlayer in affectedPlayers)
NotifyPlayer(affectedPlayer, attackPosition);
return true; return true;
} }
@@ -68,6 +84,12 @@ namespace OpenRA.Mods.D2k
renderUnit.PlayCustomAnim(self, "mouth"); renderUnit.PlayCustomAnim(self, "mouth");
} }
void NotifyPlayer(Player player, WPos location)
{
Sound.PlayNotification(player.World.Map.Rules, player, "Speech", swallow.Info.WormAttackNotification, player.Country.Race);
radarPings.Add(() => true, location, Color.Red, 50);
}
public override Activity Tick(Actor self) public override Activity Tick(Actor self)
{ {
if (countdown > 0) if (countdown > 0)
@@ -78,17 +100,17 @@ namespace OpenRA.Mods.D2k
if (stance == AttackState.ReturningUnderground) // Wait for the worm to get back underground if (stance == AttackState.ReturningUnderground) // Wait for the worm to get back underground
{ {
if (self.World.SharedRandom.Next()%2 == 0) // There is a 50-50 chance that the worm would just go away if (self.World.SharedRandom.Next() % 2 == 0) // There is a 50-50 chance that the worm would just go away
{ {
self.CancelActivity(); self.CancelActivity();
self.World.AddFrameEndTask(w => w.Remove(self)); self.World.AddFrameEndTask(w => w.Remove(self));
var wormManager = self.World.WorldActor.TraitOrDefault<WormManager>(); var wormManager = self.World.WorldActor.TraitOrDefault<WormManager>();
if (wormManager != null) if (wormManager != null)
wormManager.DecreaseWorms(); wormManager.DecreaseWorms();
} }
else else
{ {
renderUnit.DefaultAnimation.ReplaceAnim("idle"); renderUnit.DefaultAnimation.ReplaceAnim("idle");
} }
return NextActivity; return NextActivity;
} }
@@ -96,14 +118,17 @@ namespace OpenRA.Mods.D2k
if (stance == AttackState.Burrowed) // Wait for the worm to get in position if (stance == AttackState.Burrowed) // Wait for the worm to get in position
{ {
// This is so that the worm cancels an attack against a target that has reached solid rock // This is so that the worm cancels an attack against a target that has reached solid rock
if (positionable == null || !positionable.CanEnterCell(target.Actor.Location, null, false)) if (!positionable.CanEnterCell(target.Actor.Location, null, false))
return NextActivity; return NextActivity;
var success = WormAttack(self); var success = WormAttack(self);
if (!success) if (!success)
{
renderUnit.DefaultAnimation.ReplaceAnim("idle");
return NextActivity; return NextActivity;
}
countdown = swallow.AttackSwallowInfo.ReturnTime; countdown = swallow.Info.ReturnTime;
stance = AttackState.ReturningUnderground; stance = AttackState.ReturningUnderground;
} }

View File

@@ -9,9 +9,10 @@
#endregion #endregion
using System; using System;
using System.Collections.Generic;
using System.Drawing; using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.Mods.Common; using OpenRA.Mods.Common.Traits;
using OpenRA.Primitives; using OpenRA.Primitives;
using OpenRA.Traits; using OpenRA.Traits;
@@ -21,33 +22,34 @@ namespace OpenRA.Mods.D2k
class WormManagerInfo : ITraitInfo class WormManagerInfo : ITraitInfo
{ {
[Desc("Minimum number of worms")] [Desc("Minimum number of worms")]
public readonly int Minimum = 1; public readonly int Minimum = 2;
[Desc("Maximum number of worms")] [Desc("Maximum number of worms")]
public readonly int Maximum = 8; public readonly int Maximum = 4;
[Desc("Average time (seconds) between worm spawn")] [Desc("Average time (seconds) between worm spawn")]
public readonly int SpawnInterval = 180; public readonly int SpawnInterval = 120;
public readonly string WormSignNotification = "WormSign"; public readonly string WormSignNotification = "WormSign";
public readonly string WormSignature = "sandworm"; public readonly string WormSignature = "sandworm";
public readonly string WormOwnerPlayer = "Creeps"; public readonly string WormOwnerPlayer = "Creeps";
public object Create (ActorInitializer init) { return new WormManager(this, init.self); } public object Create(ActorInitializer init) { return new WormManager(this, init.self); }
} }
class WormManager : ITick class WormManager : ITick
{ {
int countdown; int countdown;
int wormsPresent; int wormsPresent;
RadarPings radarPings;
readonly WormManagerInfo info; readonly WormManagerInfo info;
readonly Lazy<Actor[]> spawnPoints; readonly Lazy<Actor[]> spawnPoints;
readonly Lazy<RadarPings> radarPings;
public WormManager(WormManagerInfo info, Actor self) public WormManager(WormManagerInfo info, Actor self)
{ {
this.info = info; this.info = info;
radarPings = Exts.Lazy(() => self.World.WorldActor.Trait<RadarPings>());
spawnPoints = Exts.Lazy(() => self.World.ActorsWithTrait<WormSpawner>().Select(x => x.Actor).ToArray()); spawnPoints = Exts.Lazy(() => self.World.ActorsWithTrait<WormSpawner>().Select(x => x.Actor).ToArray());
} }
@@ -59,31 +61,40 @@ namespace OpenRA.Mods.D2k
if (!spawnPoints.Value.Any()) if (!spawnPoints.Value.Any())
return; return;
if (--countdown > 0) // Apparantly someone doesn't want worms or the maximum number of worms has been reached
if (info.Maximum < 1 || wormsPresent >= info.Maximum)
return;
if (--countdown > 0 && wormsPresent >= info.Minimum)
return; return;
countdown = info.SpawnInterval * 25; countdown = info.SpawnInterval * 25;
if (wormsPresent < info.Maximum)
SpawnWorm(self); var wormLocations = new List<WPos>();
wormLocations.Add(SpawnWorm(self));
while (wormsPresent < info.Minimum)
wormLocations.Add(SpawnWorm(self));
AnnounceWormSign(self, wormLocations);
} }
void SpawnWorm (Actor self) WPos SpawnWorm(Actor self)
{ {
var spawnPosition = GetRandomSpawnPosition(self); var spawnPoint = GetRandomSpawnPoint(self);
var spawnLocation = self.World.Map.CellContaining(spawnPosition);
self.World.AddFrameEndTask(w => w.CreateActor(info.WormSignature, new TypeDictionary self.World.AddFrameEndTask(w => w.CreateActor(info.WormSignature, new TypeDictionary
{ {
new OwnerInit(w.Players.First(x => x.PlayerName == info.WormOwnerPlayer)), new OwnerInit(w.Players.First(x => x.PlayerName == info.WormOwnerPlayer)),
new LocationInit(spawnLocation) new LocationInit(spawnPoint.Location)
})); }));
wormsPresent++; wormsPresent++;
AnnounceWormSign(self, spawnPosition); return spawnPoint.CenterPosition;
} }
WPos GetRandomSpawnPosition(Actor self) Actor GetRandomSpawnPoint(Actor self)
{ {
return spawnPoints.Value.Random(self.World.SharedRandom).CenterPosition; return spawnPoints.Value.Random(self.World.SharedRandom);
} }
public void DecreaseWorms() public void DecreaseWorms()
@@ -91,22 +102,17 @@ namespace OpenRA.Mods.D2k
wormsPresent--; wormsPresent--;
} }
void AnnounceWormSign(Actor self, WPos wormSpawnPosition) void AnnounceWormSign(Actor self, IEnumerable<WPos> wormLocations)
{ {
if (self.World.LocalPlayer == null) if (self.World.LocalPlayer != null)
Sound.PlayNotification(self.World.Map.Rules, self.World.LocalPlayer, "Speech", info.WormSignNotification, self.World.LocalPlayer.Country.Race);
if (radarPings.Value == null)
return; return;
Sound.PlayNotification(self.World.Map.Rules, self.World.LocalPlayer, "Speech", info.WormSignNotification, self.World.LocalPlayer.Country.Race);
if (radarPings == null) foreach (var wormLocation in wormLocations)
{ radarPings.Value.Add(() => true, wormLocation, Color.Red, 50);
if (self.World.WorldActor == null)
return;
radarPings = self.World.WorldActor.TraitOrDefault<RadarPings>();
}
radarPings.Add(() => true, wormSpawnPosition, Color.Red, 50);
} }
} }

View File

@@ -14,7 +14,6 @@ using System.Drawing;
using System.Linq; using System.Linq;
using OpenRA.GameRules; using OpenRA.GameRules;
using OpenRA.Mods.RA.Buildings; using OpenRA.Mods.RA.Buildings;
using OpenRA.Mods.RA.Move;
using OpenRA.Traits; using OpenRA.Traits;
namespace OpenRA.Mods.RA namespace OpenRA.Mods.RA
@@ -39,6 +38,7 @@ namespace OpenRA.Mods.RA
public IEnumerable<Armament> Armaments { get { return GetArmaments(); } } public IEnumerable<Armament> Armaments { get { return GetArmaments(); } }
protected Lazy<IFacing> facing; protected Lazy<IFacing> facing;
protected Lazy<Building> building; protected Lazy<Building> building;
protected Lazy<IPositionable> positionable;
protected Func<IEnumerable<Armament>> GetArmaments; protected Func<IEnumerable<Armament>> GetArmaments;
readonly Actor self; readonly Actor self;
@@ -56,6 +56,7 @@ namespace OpenRA.Mods.RA
facing = Exts.Lazy(() => self.TraitOrDefault<IFacing>()); facing = Exts.Lazy(() => self.TraitOrDefault<IFacing>());
building = Exts.Lazy(() => self.TraitOrDefault<Building>()); building = Exts.Lazy(() => self.TraitOrDefault<Building>());
positionable = Exts.Lazy(() => self.Trait<IPositionable>());
} }
protected virtual bool CanAttack(Actor self, Target target) protected virtual bool CanAttack(Actor self, Target target)
@@ -146,8 +147,7 @@ namespace OpenRA.Mods.RA
{ {
if (Info.AttackRequiresEnteringCell) if (Info.AttackRequiresEnteringCell)
{ {
var positionable = self.TraitOrDefault<IPositionable>(); if (!positionable.Value.CanEnterCell(t.Actor.Location, null, false))
if (positionable == null || !positionable.CanEnterCell(t.Actor.Location, null, false))
return false; return false;
} }

View File

@@ -458,7 +458,7 @@ VICE:
MuzzleSplitFacings: 8 MuzzleSplitFacings: 8
AttackFrontal: AttackFrontal:
AttackWander: AttackWander:
WanderMoveRadius: 4 WanderMoveRadius: 2
RenderUnit: RenderUnit:
WithMuzzleFlash: WithMuzzleFlash:
SplitFacings: true SplitFacings: true

Binary file not shown.

View File

@@ -121,6 +121,8 @@ Rules:
-MPStartLocations: -MPStartLocations:
ResourceType@Spice: ResourceType@Spice:
ValuePerUnit: 0 ValuePerUnit: 0
WormManager:
Minimum: 1
Sequences: Sequences:

View File

@@ -583,7 +583,7 @@ DOGGIE:
Weapon: FiendShard Weapon: FiendShard
AttackFrontal: AttackFrontal:
AttackWander: AttackWander:
WanderMoveRadius: 4 WanderMoveRadius: 2
VISSML: VISSML:
Inherits: ^Infantry Inherits: ^Infantry