Merge pull request #11520 from reaperrr/IdleWander

Make civilians wander around when idle
This commit is contained in:
Oliver Brakmann
2016-07-04 11:50:35 +02:00
committed by GitHub
10 changed files with 38 additions and 13 deletions

View File

@@ -273,7 +273,7 @@
<Compile Include="Traits\Attack\AttackOmni.cs" /> <Compile Include="Traits\Attack\AttackOmni.cs" />
<Compile Include="Traits\Attack\AttackSuicides.cs" /> <Compile Include="Traits\Attack\AttackSuicides.cs" />
<Compile Include="Traits\Attack\AttackTurreted.cs" /> <Compile Include="Traits\Attack\AttackTurreted.cs" />
<Compile Include="Traits\Attack\AttackWander.cs" /> <Compile Include="Traits\AttackWander.cs" />
<Compile Include="Traits\AutoTarget.cs" /> <Compile Include="Traits\AutoTarget.cs" />
<Compile Include="Traits\BlocksProjectiles.cs" /> <Compile Include="Traits\BlocksProjectiles.cs" />
<Compile Include="Traits\Buildable.cs" /> <Compile Include="Traits\Buildable.cs" />

View File

@@ -27,7 +27,7 @@ namespace OpenRA.Mods.Common.Traits
public AttackWander(Actor self, AttackWanderInfo info) public AttackWander(Actor self, AttackWanderInfo info)
: base(self, info) : base(self, info)
{ {
attackMove = self.TraitOrDefault<AttackMove>(); attackMove = self.Trait<AttackMove>();
} }
public override void DoAction(Actor self, CPos targetCell) public override void DoAction(Actor self, CPos targetCell)

View File

@@ -15,7 +15,7 @@ using OpenRA.Traits;
namespace OpenRA.Mods.Common.Traits namespace OpenRA.Mods.Common.Traits
{ {
[Desc("Wanders around aimlessly while idle.")] [Desc("Wanders around aimlessly while idle.")]
public abstract class WandersInfo : ITraitInfo public class WandersInfo : UpgradableTraitInfo, Requires<IMoveInfo>
{ {
public readonly int WanderMoveRadius = 1; public readonly int WanderMoveRadius = 1;
@@ -28,13 +28,14 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Maximum amount of ticks the actor will sit idly before starting to wander.")] [Desc("Maximum amount of ticks the actor will sit idly before starting to wander.")]
public readonly int MaxMoveDelay = 0; public readonly int MaxMoveDelay = 0;
public abstract object Create(ActorInitializer init); public override object Create(ActorInitializer init) { return new Wanders(init.Self, this); }
} }
public class Wanders : INotifyIdle, INotifyBecomingIdle public class Wanders : UpgradableTrait<WandersInfo>, INotifyCreated, INotifyIdle, INotifyBecomingIdle
{ {
readonly Actor self; readonly Actor self;
readonly WandersInfo info; readonly WandersInfo info;
IResolveOrder move;
int countdown; int countdown;
int ticksIdle; int ticksIdle;
@@ -42,12 +43,18 @@ namespace OpenRA.Mods.Common.Traits
bool firstTick = true; bool firstTick = true;
public Wanders(Actor self, WandersInfo info) public Wanders(Actor self, WandersInfo info)
: base(info)
{ {
this.self = self; this.self = self;
this.info = info; this.info = info;
effectiveMoveRadius = info.WanderMoveRadius; effectiveMoveRadius = info.WanderMoveRadius;
} }
void INotifyCreated.Created(Actor self)
{
move = self.Trait<IMove>() as IResolveOrder;
}
public virtual void OnBecomingIdle(Actor self) public virtual void OnBecomingIdle(Actor self)
{ {
countdown = self.World.SharedRandom.Next(info.MinMoveDelay, info.MaxMoveDelay); countdown = self.World.SharedRandom.Next(info.MinMoveDelay, info.MaxMoveDelay);
@@ -55,9 +62,13 @@ namespace OpenRA.Mods.Common.Traits
public virtual void TickIdle(Actor self) public virtual void TickIdle(Actor self)
{ {
// The countdown has not have been set at this point, so don't check yet if (IsTraitDisabled)
return;
// OnBecomingIdle has not been called yet at this point, so set the initial countdown here
if (firstTick) if (firstTick)
{ {
countdown = self.World.SharedRandom.Next(info.MinMoveDelay, info.MaxMoveDelay);
firstTick = false; firstTick = false;
return; return;
} }
@@ -92,7 +103,7 @@ namespace OpenRA.Mods.Common.Traits
public virtual void DoAction(Actor self, CPos targetCell) public virtual void DoAction(Actor self, CPos targetCell)
{ {
throw new NotImplementedException("Base class Wanders does not implement method DoAction!"); move.ResolveOrder(self, new Order("Move", self, false) { TargetLocation = targetCell });
} }
} }
} }

View File

@@ -152,7 +152,7 @@ namespace OpenRA.Mods.D2k.Activities
sandworm.IsAttacking = false; sandworm.IsAttacking = false;
// There is a chance that the worm would just go away after attacking // There is a chance that the worm would just go away after attacking
if (self.World.SharedRandom.Next(100) <= sandworm.Info.ChanceToDisappear) if (self.World.SharedRandom.Next(100) <= sandworm.WormInfo.ChanceToDisappear)
{ {
self.CancelActivity(); self.CancelActivity();
self.World.AddFrameEndTask(w => self.Dispose()); self.World.AddFrameEndTask(w => self.Dispose());

View File

@@ -35,7 +35,7 @@ namespace OpenRA.Mods.D2k.Traits
class Sandworm : Wanders, ITick, INotifyActorDisposing class Sandworm : Wanders, ITick, INotifyActorDisposing
{ {
public readonly SandwormInfo Info; public readonly SandwormInfo WormInfo;
readonly WormManager manager; readonly WormManager manager;
readonly Mobile mobile; readonly Mobile mobile;
@@ -50,7 +50,7 @@ namespace OpenRA.Mods.D2k.Traits
public Sandworm(Actor self, SandwormInfo info) public Sandworm(Actor self, SandwormInfo info)
: base(self, info) : base(self, info)
{ {
Info = 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<WormManager>();
@@ -78,10 +78,10 @@ namespace OpenRA.Mods.D2k.Traits
void RescanForTargets(Actor self) void RescanForTargets(Actor self)
{ {
targetCountdown = Info.TargetRescanInterval; targetCountdown = WormInfo.TargetRescanInterval;
// If close enough, we don't care about other actors. // If close enough, we don't care about other actors.
var target = self.World.FindActorsInCircle(self.CenterPosition, Info.IgnoreNoiseAttackRange) var target = self.World.FindActorsInCircle(self.CenterPosition, WormInfo.IgnoreNoiseAttackRange)
.FirstOrDefault(x => attackTrait.HasAnyValidWeapons(Target.FromActor(x))); .FirstOrDefault(x => attackTrait.HasAnyValidWeapons(Target.FromActor(x)));
if (target != null) if (target != null)
{ {
@@ -98,7 +98,7 @@ namespace OpenRA.Mods.D2k.Traits
return mobile.CanEnterCell(a.Location, null, false); return mobile.CanEnterCell(a.Location, null, false);
}; };
var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, Info.MaxSearchRadius) var actorsInRange = self.World.FindActorsInCircle(self.CenterPosition, WormInfo.MaxSearchRadius)
.Where(isValidTarget).SelectMany(a => a.TraitsImplementing<AttractsWorms>()); .Where(isValidTarget).SelectMany(a => a.TraitsImplementing<AttractsWorms>());
var noiseDirection = actorsInRange.Aggregate(WVec.Zero, (a, b) => a + b.AttractionAtPosition(self.CenterPosition)); var noiseDirection = actorsInRange.Aggregate(WVec.Zero, (a, b) => a + b.AttractionAtPosition(self.CenterPosition));

View File

@@ -503,6 +503,7 @@ C10:
DELPHI: DELPHI:
Inherits: ^CivInfantry Inherits: ^CivInfantry
Inherits@armed: ^ArmedCivilian Inherits@armed: ^ArmedCivilian
-Wanders:
Tooltip: Tooltip:
Name: Agent Delphi Name: Agent Delphi
@@ -513,6 +514,7 @@ CHAN:
MOEBIUS: MOEBIUS:
Inherits: ^CivInfantry Inherits: ^CivInfantry
-Wanders:
Voiced: Voiced:
VoiceSet: MoebiusVoice VoiceSet: MoebiusVoice
Tooltip: Tooltip:

View File

@@ -301,6 +301,9 @@
CrushSound: squish2.aud CrushSound: squish2.aud
Voiced: Voiced:
VoiceSet: CivilianMaleVoice VoiceSet: CivilianMaleVoice
Wanders:
MinMoveDelay: 150
MaxMoveDelay: 750
^ArmedCivilian: ^ArmedCivilian:
Armament: Armament:

View File

@@ -290,6 +290,9 @@
ScaredyCat: ScaredyCat:
Voiced: Voiced:
VoiceSet: CivilianMaleVoice VoiceSet: CivilianMaleVoice
Wanders:
MinMoveDelay: 150
MaxMoveDelay: 750
^ArmedCivilian: ^ArmedCivilian:
Armament: Armament:

View File

@@ -362,6 +362,7 @@ MECH:
EINSTEIN: EINSTEIN:
Inherits: ^CivInfantry Inherits: ^CivInfantry
-Wanders:
Tooltip: Tooltip:
Name: Prof. Einstein Name: Prof. Einstein
Mobile: Mobile:
@@ -371,6 +372,7 @@ EINSTEIN:
DELPHI: DELPHI:
Inherits: ^CivInfantry Inherits: ^CivInfantry
-Wanders:
Tooltip: Tooltip:
Name: Agent Delphi Name: Agent Delphi
Mobile: Mobile:
@@ -388,6 +390,7 @@ CHAN:
GNRL: GNRL:
Inherits@1: ^CivInfantry Inherits@1: ^CivInfantry
Inherits@2: ^ArmedCivilian Inherits@2: ^ArmedCivilian
-Wanders:
Tooltip: Tooltip:
Name: General Name: General
Selectable: Selectable:

View File

@@ -403,6 +403,9 @@
Tooltip: Tooltip:
Name: Civilian Name: Civilian
ScaredyCat: ScaredyCat:
Wanders:
MinMoveDelay: 150
MaxMoveDelay: 750
^Vehicle: ^Vehicle:
Inherits@1: ^GainsExperience Inherits@1: ^GainsExperience