Second implementation of Sandworms

Fix tabs in AttackSwallow.cs
This commit is contained in:
penev92
2014-11-02 14:17:52 +02:00
parent 5da5bce405
commit 1057f8ff3b
18 changed files with 500 additions and 34 deletions

View File

@@ -0,0 +1,54 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.RA;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k
{
// TODO: This is a copy of AttackLeap. Maybe combine them in AttackMelee trait when the code is finalized?
[Desc("Sandworms use this attack model.")]
class AttackSwallowInfo : AttackFrontalInfo, Requires<SandwormInfo>
{
public override object Create(ActorInitializer init) { return new AttackSwallow(init.self, this); }
}
class AttackSwallow : AttackFrontal
{
readonly Sandworm sandworm;
public AttackSwallow(Actor self, AttackSwallowInfo attackSwallowInfo)
: base(self, attackSwallowInfo)
{
sandworm = self.Trait<Sandworm>();
}
public override void DoAttack(Actor self, Target target)
{
// TODO: Worm should ignore Fremen as targets unless they are firing/being fired upon (even moving fremen do not attract worms)
if (target.Type != TargetType.Actor || !CanAttack(self, target) || !sandworm.CanAttackAtLocation(self, target.Actor.Location))
// this is so that the worm does not launch an attack against a target that has reached solid rock
{
self.CancelActivity();
return;
}
var a = ChooseArmamentForTarget(target);
if (a == null)
return;
if (!target.IsInRange(self.CenterPosition, a.Weapon.Range))
return;
self.CancelActivity();
self.QueueActivity(new SwallowActor(self, target.Actor, a.Weapon));
}
}
}

View File

@@ -68,6 +68,11 @@
</Reference>
</ItemGroup>
<ItemGroup>
<Compile Include="AttackSwallow.cs" />
<Compile Include="Sandworm.cs" />
<Compile Include="SwallowActor.cs" />
<Compile Include="ThrowsShrapnel.cs" />
<Compile Include="DamagedWithoutFoundation.cs" />
<Compile Include="SpriteLoaders\R8Loader.cs" />
<Compile Include="Traits\Buildings\DamagedWithoutFoundation.cs" />
<Compile Include="Traits\Render\WithBuildingPlacedOverlay.cs" />
@@ -89,6 +94,13 @@
<Compile Include="AutoCarryall\AutoCarryall.cs" />
<Compile Include="AutoCarryall\Carryable.cs" />
<Compile Include="AutoCarryall\CarryUnit.cs" />
<Compile Include="World\ChooseBuildTabOnSelect.cs" />
<Compile Include="World\PaletteFromR8.cs" />
<Compile Include="World\FogPaletteFromR8.cs" />
<Compile Include="World\D2kResourceLayer.cs" />
<Compile Include="World\PaletteFromScaledPalette.cs" />
<Compile Include="WormManager.cs" />
<Compile Include="WormSpawner.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<PropertyGroup>

View File

@@ -0,0 +1,70 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.RA;
using OpenRA.Mods.RA.Move;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k
{
class SandwormInfo : Requires<RenderUnitInfo>, Requires<MobileInfo>, IOccupySpaceInfo
{
readonly public int WanderMoveRadius = 20;
readonly public string WormSignNotification = "WormSign";
public object Create(ActorInitializer init) { return new Sandworm(this); }
}
class Sandworm : INotifyIdle
{
int ticksIdle;
int effectiveMoveRadius;
readonly int maxMoveRadius;
public Sandworm(SandwormInfo info)
{
maxMoveRadius = info.WanderMoveRadius;
effectiveMoveRadius = info.WanderMoveRadius;
// TODO: Someone familiar with how the sounds work should fix this:
// TODO: This should not be here. It should be same as "Enemy unit sighted".
//Sound.PlayNotification(self.Owner, "Speech", info.WormSignNotification, self.Owner.Country.Race);
}
// TODO: This copies AttackWander and builds on top of it. AttackWander should be revised.
public void TickIdle(Actor self)
{
var globalOffset = new WVec(0, -1024 * effectiveMoveRadius, 0).Rotate(WRot.FromFacing(self.World.SharedRandom.Next(255)));
var offset = new CVec(globalOffset.X/1024, globalOffset.Y/1024);
var targetlocation = self.Location + offset;
if (!self.World.Map.Bounds.Contains(targetlocation.X, targetlocation.Y))
{
// If MoveRadius is too big there might not be a valid cell to order the attack to (if actor is on a small island and can't leave)
if (++ticksIdle % 10 == 0) // completely random number
{
effectiveMoveRadius--;
}
return; // We'll be back the next tick; better to sit idle for a few seconds than prolongue this tick indefinitely with a loop
}
self.World.IssueOrder(new Order("AttackMove", self, false) { TargetLocation = targetlocation });
ticksIdle = 0;
effectiveMoveRadius = maxMoveRadius;
}
public bool CanAttackAtLocation(Actor self, CPos targetLocation)
{
return self.Trait<Mobile>().MovementSpeedForCell(self, targetLocation) != 0;
}
}
}

View File

@@ -0,0 +1,136 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.GameRules;
using OpenRA.Mods.RA.Move;
using OpenRA.Mods.RA.Render;
using OpenRA.Traits;
namespace OpenRA.Mods.D2k
{
public enum AttackState { Burrowed, EmergingAboveGround, ReturningUndergrown }
class SwallowActor : Activity
{
readonly Actor target;
readonly Mobile mobile;
readonly Sandworm sandworm;
readonly WeaponInfo weapon;
int countdown;
AttackState stance = AttackState.Burrowed;
// TODO: random numbers to make it look ok
[Desc("The number of ticks it takes to return underground.")]
const int ReturnTime = 60;
[Desc("The number of ticks it takes to get in place under the target to attack.")]
const int AttackTime = 30;
public SwallowActor(Actor self, Actor target, WeaponInfo weapon)
{
if (!target.HasTrait<Mobile>())
throw new InvalidOperationException("SwallowActor requires a target actor with the Mobile trait");
this.target = target;
this.weapon = weapon;
mobile = self.TraitOrDefault<Mobile>();
sandworm = self.TraitOrDefault<Sandworm>();
countdown = AttackTime;
}
bool WormAttack(Actor worm)
{
var targetLocation = target.Location;
var lunch = worm.World.ActorMap.GetUnitsAt(targetLocation)
.Except(new[] { worm })
.Where(t => weapon.IsValidAgainst(t, worm));
if (!lunch.Any())
return false;
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)
mobile.SetPosition(worm, targetLocation);
PlayAttackAnimation(worm);
return true;
}
public bool PlayAttackAnimation(Actor self)
{
var renderUnit = self.Trait<RenderUnit>();
renderUnit.PlayCustomAnim(self, "sand");
renderUnit.PlayCustomAnim(self, "mouth");
// TODO: Someone familiar with how the sounds work should fix this:
//Sound.PlayNotification(self.Owner, "Speech", "WormAttack", self.Owner.Country.Race);
return true;
}
public override Activity Tick(Actor self)
{
if (countdown > 0)
{
countdown--;
return this;
}
if (stance == AttackState.ReturningUndergrown) // wait for the worm to get back underground
{
#region DisappearToMapEdge
// More random numbers used for min and max bounds
var rand = self.World.SharedRandom.Next(200, 400);
if (rand % 2 == 0) // there is a 50-50 chance that the worm would just go away
{
self.CancelActivity();
//self.World.WorldActor.QueueActivity(new DisappearToMapEdge(self, rand));
self.World.AddFrameEndTask(w => w.Remove(self));
var wormManager = self.World.WorldActor.TraitOrDefault<WormManager>();
if (wormManager != null)
wormManager.DecreaseWorms();
}
#endregion
// TODO: if the worm did not disappear, make the animation reappear here
return NextActivity;
}
if (stance == AttackState.Burrowed) // wait for the worm to get in position
{
// TODO: make the worm animation (currenty the lightning) disappear here
// this is so that the worm cancels an attack against a target that has reached solid rock
if (sandworm == null || !sandworm.CanAttackAtLocation(self, target.Location))
{
return NextActivity;
}
var success = WormAttack(self);
if (!success)
{
return NextActivity;
}
countdown = ReturnTime;
stance = AttackState.ReturningUndergrown;
}
return this;
}
}
}

View File

@@ -0,0 +1,87 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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.D2k
{
[Desc("Controls the spawning of sandworms. Attach this to the world actor.")]
class WormManagerInfo : ITraitInfo
{
[Desc("Minimum number of worms")]
public readonly int Minimum = 1;
[Desc("Maximum number of worms")]
public readonly int Maximum = 5;
[Desc("Average time (seconds) between crate spawn")]
public readonly int SpawnInterval = 180;
public readonly string WormSignature = "sandworm";
public readonly string WormOwnerPlayer = "Creeps";
public object Create (ActorInitializer init) { return new WormManager(this, init.self); }
}
class WormManager : ITick
{
int countdown;
int wormsPresent;
readonly WormManagerInfo info;
readonly Lazy<Actor[]> spawnPoints;
public WormManager(WormManagerInfo info, Actor self)
{
this.info = info;
spawnPoints = Exts.Lazy(() => self.World.ActorsWithTrait<WormSpawner>().Select(x => x.Actor).ToArray());
}
public void Tick(Actor self)
{
// TODO: Add a lobby option to disable worms just like crates
if (--countdown > 0)
return;
countdown = info.SpawnInterval * 25;
if (wormsPresent < info.Maximum)
SpawnWorm(self);
}
private void SpawnWorm (Actor self)
{
var spawnLocation = GetRandomSpawnPosition(self);
self.World.AddFrameEndTask(w =>
w.CreateActor(info.WormSignature, new TypeDictionary
{
new OwnerInit(w.Players.First(x => x.PlayerName == info.WormOwnerPlayer)),
new LocationInit(spawnLocation)
}));
wormsPresent++;
}
private CPos GetRandomSpawnPosition(Actor self)
{
// TODO: This is here only for testing, while the maps don't have valid spawn points
if (!spawnPoints.Value.Any())
return self.World.Map.ChooseRandomEdgeCell(self.World.SharedRandom);
return spawnPoints.Value[self.World.SharedRandom.Next(0, spawnPoints.Value.Count() - 1)].Location;
}
public void DecreaseWorms()
{
wormsPresent--;
}
}
}

View File

@@ -0,0 +1,24 @@
#region Copyright & License Information
/*
* Copyright 2007-2014 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
{
[Desc("An actor with this trait indicates a valid spawn point for sandworms.")]
class WormSpawnerInfo : ITraitInfo
{
public object Create(ActorInitializer init) { return new WormSpawner(); }
}
class WormSpawner
{
}
}