Enhance sandworm targeting logic by having actors produce noise
This commit is contained in:
@@ -14,9 +14,6 @@ using OpenRA.Activities;
|
||||
using OpenRA.GameRules;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Mods.D2k.Traits;
|
||||
using OpenRA.Mods.RA;
|
||||
using OpenRA.Mods.RA.Activities;
|
||||
using OpenRA.Mods.RA.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.D2k.Activities
|
||||
@@ -29,11 +26,11 @@ namespace OpenRA.Mods.D2k.Activities
|
||||
|
||||
readonly CPos location;
|
||||
readonly Target target;
|
||||
readonly Sandworm sandworm;
|
||||
readonly WeaponInfo weapon;
|
||||
readonly RenderUnit renderUnit;
|
||||
readonly RadarPings radarPings;
|
||||
readonly AttackSwallow swallow;
|
||||
readonly AttackSwallowInfo swallowInfo;
|
||||
readonly IPositionable positionable;
|
||||
|
||||
int countdown;
|
||||
@@ -43,12 +40,12 @@ namespace OpenRA.Mods.D2k.Activities
|
||||
{
|
||||
this.target = target;
|
||||
this.weapon = weapon;
|
||||
sandworm = self.Trait<Sandworm>();
|
||||
positionable = self.Trait<Mobile>();
|
||||
swallow = self.Trait<AttackSwallow>();
|
||||
swallowInfo = (AttackSwallowInfo)swallow.Info;
|
||||
renderUnit = self.Trait<RenderUnit>();
|
||||
radarPings = self.World.WorldActor.TraitOrDefault<RadarPings>();
|
||||
countdown = swallowInfo.AttackTime;
|
||||
countdown = swallow.Info.AttackTime;
|
||||
|
||||
renderUnit.DefaultAnimation.ReplaceAnim("burrowed");
|
||||
stance = AttackState.Burrowed;
|
||||
@@ -70,6 +67,7 @@ namespace OpenRA.Mods.D2k.Activities
|
||||
return false;
|
||||
|
||||
stance = AttackState.EmergingAboveGround;
|
||||
sandworm.IsAttacking = true;
|
||||
|
||||
foreach (var actor in lunch)
|
||||
actor.World.AddFrameEndTask(_ => actor.Destroy());
|
||||
@@ -94,7 +92,7 @@ namespace OpenRA.Mods.D2k.Activities
|
||||
|
||||
void NotifyPlayer(Player player, WPos location)
|
||||
{
|
||||
Sound.PlayNotification(player.World.Map.Rules, player, "Speech", swallowInfo.WormAttackNotification, player.Country.Race);
|
||||
Sound.PlayNotification(player.World.Map.Rules, player, "Speech", swallow.Info.WormAttackNotification, player.Country.Race);
|
||||
radarPings.Add(() => true, location, Color.Red, 50);
|
||||
}
|
||||
|
||||
@@ -109,15 +107,17 @@ namespace OpenRA.Mods.D2k.Activities
|
||||
// Wait for the worm to get back underground
|
||||
if (stance == AttackState.ReturningUnderground)
|
||||
{
|
||||
// There is a 50-50 chance that the worm would just go away
|
||||
if (self.World.SharedRandom.Next() % 2 == 0)
|
||||
sandworm.IsAttacking = false;
|
||||
|
||||
// There is a chance that the worm would just go away after attacking
|
||||
if (self.World.SharedRandom.Next() % 100 <= sandworm.Info.ChanceToDisappear)
|
||||
{
|
||||
self.CancelActivity();
|
||||
self.World.AddFrameEndTask(w => w.Remove(self));
|
||||
|
||||
var wormManager = self.World.WorldActor.TraitOrDefault<WormManager>();
|
||||
if (wormManager != null)
|
||||
wormManager.DecreaseWorms();
|
||||
wormManager.DecreaseWormCount();
|
||||
}
|
||||
else
|
||||
renderUnit.DefaultAnimation.ReplaceAnim("idle");
|
||||
@@ -138,7 +138,7 @@ namespace OpenRA.Mods.D2k.Activities
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
countdown = swallowInfo.ReturnTime;
|
||||
countdown = swallow.Info.ReturnTime;
|
||||
stance = AttackState.ReturningUnderground;
|
||||
}
|
||||
|
||||
|
||||
@@ -81,6 +81,7 @@
|
||||
<Compile Include="Traits\Render\WithDockingOverlay.cs" />
|
||||
<Compile Include="Traits\Render\WithDeliveryOverlay.cs" />
|
||||
<Compile Include="Traits\Render\WithProductionOverlay.cs" />
|
||||
<Compile Include="Traits\Sandworm.cs" />
|
||||
<Compile Include="Traits\TemporaryOwnerManager.cs" />
|
||||
<Compile Include="Traits\ThrowsShrapnel.cs" />
|
||||
<Compile Include="Traits\World\BuildableTerrainLayer.cs" />
|
||||
@@ -90,6 +91,8 @@
|
||||
<Compile Include="Traits\World\PaletteFromR8.cs" />
|
||||
<Compile Include="Traits\World\PaletteFromScaledPalette.cs" />
|
||||
<Compile Include="Traits\World\WormManager.cs" />
|
||||
<Compile Include="Traits\AttractsWorms.cs" />
|
||||
<Compile Include="Traits\WormSpawner.cs" />
|
||||
<Compile Include="Warheads\ChangeOwnerWarhead.cs" />
|
||||
<Compile Include="Widgets\BuildPaletteWidget.cs" />
|
||||
<Compile Include="Widgets\MoneyBinWidget.cs" />
|
||||
|
||||
@@ -30,8 +30,13 @@ namespace OpenRA.Mods.D2k.Traits
|
||||
|
||||
class AttackSwallow : AttackFrontal
|
||||
{
|
||||
public readonly new AttackSwallowInfo Info;
|
||||
|
||||
public AttackSwallow(Actor self, AttackSwallowInfo info)
|
||||
: base(self, info) { }
|
||||
: base(self, info)
|
||||
{
|
||||
Info = info;
|
||||
}
|
||||
|
||||
public override void DoAttack(Actor self, Target target)
|
||||
{
|
||||
|
||||
60
OpenRA.Mods.D2k/Traits/AttractsWorms.cs
Normal file
60
OpenRA.Mods.D2k/Traits/AttractsWorms.cs
Normal file
@@ -0,0 +1,60 @@
|
||||
#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.D2k.Traits
|
||||
{
|
||||
[Desc("This actor makes noise, which causes them to be targeted by actors with the Sandworm trait.")]
|
||||
public class AttractsWormsInfo : ITraitInfo
|
||||
{
|
||||
[Desc("How much noise this actor produces.")]
|
||||
public readonly int Intensity = 0;
|
||||
|
||||
[Desc("Noise percentage at Range step away from the actor.")]
|
||||
public readonly int[] Falloff = { 100, 100, 25, 11, 6, 4, 3, 2, 1, 0 };
|
||||
|
||||
[Desc("Range between falloff steps.")]
|
||||
public readonly WRange Spread = new WRange(3072);
|
||||
|
||||
[Desc("Ranges at which each Falloff step is defined. Overrides Spread.")]
|
||||
public WRange[] Range = null;
|
||||
|
||||
public object Create(ActorInitializer init) { return new AttractsWorms(this); }
|
||||
}
|
||||
|
||||
public class AttractsWorms
|
||||
{
|
||||
public readonly AttractsWormsInfo Info;
|
||||
|
||||
public AttractsWorms(AttractsWormsInfo info)
|
||||
{
|
||||
Info = info;
|
||||
|
||||
if (info.Range == null)
|
||||
info.Range = Exts.MakeArray(info.Falloff.Length, i => i * info.Spread);
|
||||
}
|
||||
|
||||
public int GetNoisePercentageAtDistance(int distance)
|
||||
{
|
||||
var inner = Info.Range[0].Range;
|
||||
for (var i = 1; i < Info.Range.Length; i++)
|
||||
{
|
||||
var outer = Info.Range[i].Range;
|
||||
if (outer > distance)
|
||||
return int2.Lerp(Info.Falloff[i - 1], Info.Falloff[i], distance - inner, outer - inner);
|
||||
|
||||
inner = outer;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
159
OpenRA.Mods.D2k/Traits/Sandworm.cs
Normal file
159
OpenRA.Mods.D2k/Traits/Sandworm.cs
Normal file
@@ -0,0 +1,159 @@
|
||||
#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 OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.D2k.Traits
|
||||
{
|
||||
class SandwormInfo : WandersInfo, Requires<MobileInfo>, Requires<RenderUnitInfo>, Requires<AttackBaseInfo>
|
||||
{
|
||||
[Desc("Time between rescanning for targets (in ticks).")]
|
||||
public readonly int TargetRescanInterval = 32;
|
||||
|
||||
[Desc("The radius in which the worm \"searches\" for targets.")]
|
||||
public readonly WRange MaxSearchRadius = WRange.FromCells(27);
|
||||
|
||||
[Desc("The range at which the worm launches an attack regardless of noise levels.")]
|
||||
public readonly WRange IgnoreNoiseAttackRange = WRange.FromCells(3);
|
||||
|
||||
[Desc("The chance this actor has of disappearing after it attacks (in %).")]
|
||||
public readonly int ChanceToDisappear = 80;
|
||||
|
||||
[Desc("Name of the sequence that is used when the actor is idle or moving (not attacking).")]
|
||||
public readonly string IdleSequence = "idle";
|
||||
|
||||
public override object Create(ActorInitializer init) { return new Sandworm(init.Self, this); }
|
||||
}
|
||||
|
||||
class Sandworm : Wanders, ITick, INotifyKilled
|
||||
{
|
||||
public readonly SandwormInfo Info;
|
||||
|
||||
readonly WormManager manager;
|
||||
readonly Lazy<Mobile> mobile;
|
||||
readonly Lazy<RenderUnit> renderUnit;
|
||||
readonly Lazy<AttackBase> attackTrait;
|
||||
|
||||
public bool IsMovingTowardTarget { get; private set; }
|
||||
|
||||
public bool IsAttacking;
|
||||
|
||||
int targetCountdown;
|
||||
|
||||
public Sandworm(Actor self, SandwormInfo info)
|
||||
: base(self, info)
|
||||
{
|
||||
Info = info;
|
||||
mobile = Exts.Lazy(self.Trait<Mobile>);
|
||||
renderUnit = Exts.Lazy(self.Trait<RenderUnit>);
|
||||
attackTrait = Exts.Lazy(self.Trait<AttackBase>);
|
||||
manager = self.World.WorldActor.Trait<WormManager>();
|
||||
}
|
||||
|
||||
public override void OnBecomingIdle(Actor self)
|
||||
{
|
||||
if (renderUnit.Value.DefaultAnimation.CurrentSequence.Name != Info.IdleSequence)
|
||||
renderUnit.Value.DefaultAnimation.PlayRepeating("idle");
|
||||
|
||||
base.OnBecomingIdle(self);
|
||||
}
|
||||
|
||||
public override void DoAction(Actor self, CPos targetCell)
|
||||
{
|
||||
IsMovingTowardTarget = false;
|
||||
|
||||
RescanForTargets(self);
|
||||
|
||||
if (IsMovingTowardTarget)
|
||||
return;
|
||||
|
||||
self.QueueActivity(mobile.Value.MoveWithinRange(Target.FromCell(self.World, targetCell, SubCell.Any), WRange.FromCells(1)));
|
||||
}
|
||||
|
||||
public void Tick(Actor self)
|
||||
{
|
||||
if (--targetCountdown > 0 || IsAttacking || !self.IsInWorld)
|
||||
return;
|
||||
|
||||
RescanForTargets(self);
|
||||
}
|
||||
|
||||
void RescanForTargets(Actor self)
|
||||
{
|
||||
targetCountdown = Info.TargetRescanInterval;
|
||||
|
||||
var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, Info.MaxSearchRadius);
|
||||
var noiseDirection = WVec.Zero;
|
||||
|
||||
foreach (var actor in actorsInRange)
|
||||
{
|
||||
if (!actor.IsInWorld)
|
||||
continue;
|
||||
|
||||
// TODO: Test if we really want to ignore actors that are on rock
|
||||
if (!mobile.Value.CanEnterCell(actor.Location, null, false))
|
||||
continue;
|
||||
|
||||
var noise = actor.TraitOrDefault<AttractsWorms>();
|
||||
if (noise == null)
|
||||
continue;
|
||||
|
||||
var distance = actor.CenterPosition - self.CenterPosition;
|
||||
var length = distance.Length;
|
||||
|
||||
// Actor is too far to be heard
|
||||
if (noise.Info.Range[noise.Info.Range.Length - 1].Range < length)
|
||||
continue;
|
||||
|
||||
// If close enough, we don't care about other actors
|
||||
if (length <= Info.IgnoreNoiseAttackRange.Range)
|
||||
{
|
||||
self.CancelActivity();
|
||||
attackTrait.Value.ResolveOrder(self, new Order("Attack", actor, true) { TargetActor = actor });
|
||||
return;
|
||||
}
|
||||
|
||||
var direction = 1024 * distance / length;
|
||||
var percentage = noise.GetNoisePercentageAtDistance(length);
|
||||
|
||||
noiseDirection += direction * noise.Info.Intensity * percentage / 100;
|
||||
}
|
||||
|
||||
// No target was found
|
||||
if (noiseDirection == WVec.Zero)
|
||||
return;
|
||||
|
||||
var moveTo = self.World.Map.CellContaining(self.CenterPosition + noiseDirection);
|
||||
|
||||
while (!self.World.Map.Contains(moveTo) || !mobile.Value.CanEnterCell(moveTo, null, false))
|
||||
{
|
||||
noiseDirection /= 2;
|
||||
moveTo = self.World.Map.CellContaining(self.CenterPosition + noiseDirection);
|
||||
}
|
||||
|
||||
// Don't get stuck when the noise is distributed evenly! This will make the worm wander instead of trying to move to where it already is
|
||||
if (moveTo == self.Location)
|
||||
{
|
||||
self.CancelActivity();
|
||||
return;
|
||||
}
|
||||
|
||||
self.QueueActivity(false, mobile.Value.MoveTo(moveTo, 3));
|
||||
IsMovingTowardTarget = true;
|
||||
}
|
||||
|
||||
public void Killed(Actor self, AttackInfo e)
|
||||
{
|
||||
manager.DecreaseWormCount();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -27,31 +27,32 @@ namespace OpenRA.Mods.D2k.Traits
|
||||
[Desc("Maximum number of worms")]
|
||||
public readonly int Maximum = 4;
|
||||
|
||||
[Desc("Average time (seconds) between worm spawn")]
|
||||
public readonly int SpawnInterval = 120;
|
||||
[Desc("Time (in ticks) between worm spawn.")]
|
||||
public readonly int SpawnInterval = 3000;
|
||||
|
||||
[Desc("Name of the actor that will be spawned.")]
|
||||
public readonly string WormSignature = "sandworm";
|
||||
|
||||
public readonly string WormSignNotification = "WormSign";
|
||||
|
||||
public readonly string WormSignature = "sandworm";
|
||||
public readonly string WormOwnerPlayer = "Creeps";
|
||||
|
||||
public object Create(ActorInitializer init) { return new WormManager(this, init.Self); }
|
||||
public object Create(ActorInitializer init) { return new WormManager(init.Self, this); }
|
||||
}
|
||||
|
||||
class WormManager : ITick
|
||||
{
|
||||
readonly WormManagerInfo info;
|
||||
readonly Lazy<Actor[]> spawnPoints;
|
||||
readonly Lazy<Actor[]> spawnPointActors;
|
||||
readonly Lazy<RadarPings> radarPings;
|
||||
|
||||
int countdown;
|
||||
int spawnCountdown;
|
||||
int wormsPresent;
|
||||
|
||||
public WormManager(WormManagerInfo info, Actor self)
|
||||
public WormManager(Actor self, WormManagerInfo info)
|
||||
{
|
||||
this.info = info;
|
||||
radarPings = Exts.Lazy(() => self.World.WorldActor.Trait<RadarPings>());
|
||||
spawnPoints = Exts.Lazy(() => self.World.ActorsWithTrait<WormSpawner>().Select(x => x.Actor).ToArray());
|
||||
spawnPointActors = Exts.Lazy(() => self.World.ActorsWithTrait<WormSpawner>().Select(x => x.Actor).ToArray());
|
||||
}
|
||||
|
||||
public void Tick(Actor self)
|
||||
@@ -59,18 +60,17 @@ namespace OpenRA.Mods.D2k.Traits
|
||||
if (!self.World.LobbyInfo.GlobalSettings.Creeps)
|
||||
return;
|
||||
|
||||
// TODO: It would be even better to stop
|
||||
if (!spawnPoints.Value.Any())
|
||||
if (!spawnPointActors.Value.Any())
|
||||
return;
|
||||
|
||||
// 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)
|
||||
if (--spawnCountdown > 0 && wormsPresent >= info.Minimum)
|
||||
return;
|
||||
|
||||
countdown = info.SpawnInterval * 25;
|
||||
spawnCountdown = info.SpawnInterval;
|
||||
|
||||
var wormLocations = new List<WPos>();
|
||||
|
||||
@@ -100,10 +100,10 @@ namespace OpenRA.Mods.D2k.Traits
|
||||
|
||||
Actor GetRandomSpawnPoint(Actor self)
|
||||
{
|
||||
return spawnPoints.Value.Random(self.World.SharedRandom);
|
||||
return spawnPointActors.Value.Random(self.World.SharedRandom);
|
||||
}
|
||||
|
||||
public void DecreaseWorms()
|
||||
public void DecreaseWormCount()
|
||||
{
|
||||
wormsPresent--;
|
||||
}
|
||||
@@ -120,8 +120,4 @@ namespace OpenRA.Mods.D2k.Traits
|
||||
radarPings.Value.Add(() => true, wormLocation, Color.Red, 50);
|
||||
}
|
||||
}
|
||||
|
||||
[Desc("An actor with this trait indicates a valid spawn point for sandworms.")]
|
||||
class WormSpawnerInfo : TraitInfo<WormSpawner> { }
|
||||
class WormSpawner { }
|
||||
}
|
||||
|
||||
18
OpenRA.Mods.D2k/Traits/WormSpawner.cs
Normal file
18
OpenRA.Mods.D2k/Traits/WormSpawner.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
#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.D2k.Traits
|
||||
{
|
||||
[Desc("An actor with this trait indicates a valid spawn point for sandworms.")]
|
||||
class WormSpawnerInfo : TraitInfo<WormSpawner> { }
|
||||
class WormSpawner { }
|
||||
}
|
||||
Reference in New Issue
Block a user