Move refinery and harvester logic to Mods.Common;
Remove unused interface IAcceptOreDockAction
This commit is contained in:
80
OpenRA.Mods.Common/Activities/DeliverResources.cs
Normal file
80
OpenRA.Mods.Common/Activities/DeliverResources.cs
Normal file
@@ -0,0 +1,80 @@
|
||||
#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.Drawing;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class DeliverResources : Activity
|
||||
{
|
||||
const int NextChooseTime = 100;
|
||||
bool isDocking;
|
||||
int chosenTicks;
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (NextActivity != null)
|
||||
return NextActivity;
|
||||
|
||||
var movement = self.Trait<IMove>();
|
||||
var harv = self.Trait<Harvester>();
|
||||
|
||||
// Find the nearest best refinery if not explicitly ordered to a specific refinery:
|
||||
if (harv.OwnerLinkedProc == null || !harv.OwnerLinkedProc.IsInWorld)
|
||||
{
|
||||
// Maybe we lost the owner-linked refinery:
|
||||
harv.OwnerLinkedProc = null;
|
||||
if (self.World.WorldTick - chosenTicks > NextChooseTime)
|
||||
{
|
||||
harv.ChooseNewProc(self, null);
|
||||
chosenTicks = self.World.WorldTick;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
harv.LinkProc(self, harv.OwnerLinkedProc);
|
||||
}
|
||||
|
||||
if (harv.LinkedProc == null || !harv.LinkedProc.IsInWorld)
|
||||
harv.ChooseNewProc(self, null);
|
||||
|
||||
if (harv.LinkedProc == null) // no procs exist; check again in 1s.
|
||||
return Util.SequenceActivities(new Wait(25), this);
|
||||
|
||||
var proc = harv.LinkedProc;
|
||||
var iao = proc.Trait<IAcceptResources>();
|
||||
|
||||
self.SetTargetLine(Target.FromActor(proc), Color.Green, false);
|
||||
if (self.Location != proc.Location + iao.DeliveryOffset)
|
||||
{
|
||||
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
|
||||
var next = new DeliverResources();
|
||||
foreach (var n in notify)
|
||||
n.MovingToRefinery(self, proc.Location + iao.DeliveryOffset, next);
|
||||
|
||||
return Util.SequenceActivities(movement.MoveTo(proc.Location + iao.DeliveryOffset, 0), this);
|
||||
}
|
||||
|
||||
if (!isDocking)
|
||||
{
|
||||
isDocking = true;
|
||||
iao.OnDock(self, this);
|
||||
}
|
||||
|
||||
return Util.SequenceActivities(new Wait(10), this);
|
||||
}
|
||||
|
||||
// Cannot be cancelled
|
||||
public override void Cancel(Actor self) { }
|
||||
}
|
||||
}
|
||||
177
OpenRA.Mods.Common/Activities/FindResources.cs
Normal file
177
OpenRA.Mods.Common/Activities/FindResources.cs
Normal file
@@ -0,0 +1,177 @@
|
||||
#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.Collections.Generic;
|
||||
using System.Drawing;
|
||||
using System.Linq;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class FindResources : Activity
|
||||
{
|
||||
CPos? avoidCell;
|
||||
|
||||
public FindResources()
|
||||
{
|
||||
}
|
||||
|
||||
public FindResources(CPos avoidCell)
|
||||
{
|
||||
this.avoidCell = avoidCell;
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
if (IsCanceled || NextActivity != null) return NextActivity;
|
||||
|
||||
var harv = self.Trait<Harvester>();
|
||||
|
||||
if (harv.IsFull)
|
||||
return Util.SequenceActivities(new DeliverResources(), NextActivity);
|
||||
|
||||
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
|
||||
var mobile = self.Trait<Mobile>();
|
||||
var mobileInfo = self.Info.Traits.Get<MobileInfo>();
|
||||
var resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
||||
var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
|
||||
|
||||
// Determine where to search from and how far to search:
|
||||
var searchFromLoc = harv.LastOrderLocation ?? (harv.LastLinkedProc ?? harv.LinkedProc ?? self).Location;
|
||||
var searchRadius = harv.LastOrderLocation.HasValue ? harvInfo.SearchFromOrderRadius : harvInfo.SearchFromProcRadius;
|
||||
var searchRadiusSquared = searchRadius * searchRadius;
|
||||
|
||||
// Find harvestable resources nearby:
|
||||
var path = self.World.WorldActor.Trait<PathFinder>().FindPath(
|
||||
PathSearch.Search(self.World, mobileInfo, self, true)
|
||||
.WithHeuristic(loc =>
|
||||
{
|
||||
// Avoid this cell:
|
||||
if (avoidCell.HasValue && loc == avoidCell.Value) return 1;
|
||||
|
||||
// Don't harvest out of range:
|
||||
var distSquared = (loc - searchFromLoc).LengthSquared;
|
||||
if (distSquared > searchRadiusSquared)
|
||||
return int.MaxValue;
|
||||
|
||||
// Get the resource at this location:
|
||||
var resType = resLayer.GetResource(loc);
|
||||
|
||||
if (resType == null) return 1;
|
||||
|
||||
// Can the harvester collect this kind of resource?
|
||||
if (!harvInfo.Resources.Contains(resType.Info.Name)) return 1;
|
||||
|
||||
if (territory != null)
|
||||
{
|
||||
// Another harvester has claimed this resource:
|
||||
ResourceClaim claim;
|
||||
if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) return 1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
})
|
||||
.FromPoint(self.Location));
|
||||
|
||||
if (path.Count == 0)
|
||||
{
|
||||
if (!harv.IsEmpty)
|
||||
return new DeliverResources();
|
||||
else
|
||||
{
|
||||
// Get out of the way if we are:
|
||||
harv.UnblockRefinery(self);
|
||||
var randFrames = 125 + self.World.SharedRandom.Next(-35, 35);
|
||||
if (NextActivity != null)
|
||||
return Util.SequenceActivities(NextActivity, new Wait(randFrames), new FindResources());
|
||||
else
|
||||
return Util.SequenceActivities(new Wait(randFrames), new FindResources());
|
||||
}
|
||||
}
|
||||
|
||||
// Attempt to claim a resource as ours:
|
||||
if (territory != null)
|
||||
{
|
||||
if (!territory.ClaimResource(self, path[0]))
|
||||
return Util.SequenceActivities(new Wait(25), new FindResources());
|
||||
}
|
||||
|
||||
// If not given a direct order, assume ordered to the first resource location we find:
|
||||
if (harv.LastOrderLocation == null)
|
||||
harv.LastOrderLocation = path[0];
|
||||
|
||||
self.SetTargetLine(Target.FromCell(self.World, path[0]), Color.Red, false);
|
||||
|
||||
var notify = self.TraitsImplementing<INotifyHarvesterAction>();
|
||||
var next = new FindResources();
|
||||
foreach (var n in notify)
|
||||
n.MovingToResources(self, path[0], next);
|
||||
|
||||
return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), new FindResources());
|
||||
}
|
||||
|
||||
public override IEnumerable<Target> GetTargets(Actor self)
|
||||
{
|
||||
yield return Target.FromCell(self.World, self.Location);
|
||||
}
|
||||
}
|
||||
|
||||
public class HarvestResource : Activity
|
||||
{
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
var territory = self.World.WorldActor.TraitOrDefault<ResourceClaimLayer>();
|
||||
if (IsCanceled)
|
||||
{
|
||||
if (territory != null)
|
||||
territory.UnclaimByActor(self);
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
var harv = self.Trait<Harvester>();
|
||||
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
|
||||
harv.LastHarvestedCell = self.Location;
|
||||
|
||||
if (harv.IsFull)
|
||||
{
|
||||
if (territory != null)
|
||||
territory.UnclaimByActor(self);
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
// Turn to one of the harvestable facings
|
||||
if (harvInfo.HarvestFacings != 0)
|
||||
{
|
||||
var facing = self.Trait<IFacing>().Facing;
|
||||
var desired = Util.QuantizeFacing(facing, harvInfo.HarvestFacings) * (256 / harvInfo.HarvestFacings);
|
||||
if (desired != facing)
|
||||
return Util.SequenceActivities(new Turn(self, desired), this);
|
||||
}
|
||||
|
||||
var resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
||||
var resource = resLayer.Harvest(self.Location);
|
||||
if (resource == null)
|
||||
{
|
||||
if (territory != null)
|
||||
territory.UnclaimByActor(self);
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
harv.AcceptResource(resource);
|
||||
|
||||
foreach (var t in self.TraitsImplementing<INotifyHarvesterAction>())
|
||||
t.Harvested(self, resource);
|
||||
|
||||
return Util.SequenceActivities(new Wait(harvInfo.LoadTicksPerBale), this);
|
||||
}
|
||||
}
|
||||
}
|
||||
104
OpenRA.Mods.Common/Activities/HarvesterDockSequence.cs
Normal file
104
OpenRA.Mods.Common/Activities/HarvesterDockSequence.cs
Normal file
@@ -0,0 +1,104 @@
|
||||
#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 System.Collections.Generic;
|
||||
using OpenRA.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
using OpenRA.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class HarvesterDockSequence : Activity
|
||||
{
|
||||
protected enum State { Wait, Turn, Dock, Loop, Undock, Complete }
|
||||
|
||||
protected readonly Actor Refinery;
|
||||
protected readonly Harvester Harv;
|
||||
protected readonly int DockAngle;
|
||||
protected readonly bool IsDragRequired;
|
||||
protected readonly WVec DragOffset;
|
||||
protected readonly int DragLength;
|
||||
protected readonly WPos StartDrag;
|
||||
protected readonly WPos EndDrag;
|
||||
|
||||
protected State dockingState;
|
||||
|
||||
public HarvesterDockSequence(Actor self, Actor refinery, int dockAngle, bool isDragRequired, WVec dragOffset, int dragLength)
|
||||
{
|
||||
dockingState = State.Turn;
|
||||
Refinery = refinery;
|
||||
DockAngle = dockAngle;
|
||||
IsDragRequired = isDragRequired;
|
||||
DragOffset = dragOffset;
|
||||
DragLength = dragLength;
|
||||
Harv = self.Trait<Harvester>();
|
||||
StartDrag = self.CenterPosition;
|
||||
EndDrag = refinery.CenterPosition + DragOffset;
|
||||
}
|
||||
|
||||
public override Activity Tick(Actor self)
|
||||
{
|
||||
switch (dockingState)
|
||||
{
|
||||
case State.Wait:
|
||||
return this;
|
||||
case State.Turn:
|
||||
dockingState = State.Dock;
|
||||
if (IsDragRequired)
|
||||
return Util.SequenceActivities(new Turn(self, DockAngle), new Drag(self, StartDrag, EndDrag, DragLength), this);
|
||||
return Util.SequenceActivities(new Turn(self, DockAngle), this);
|
||||
case State.Dock:
|
||||
if (Refinery.IsInWorld && !Refinery.IsDead)
|
||||
foreach (var nd in Refinery.TraitsImplementing<INotifyDocking>())
|
||||
nd.Docked(Refinery, self);
|
||||
return OnStateDock(self);
|
||||
case State.Loop:
|
||||
if (!Refinery.IsInWorld || Refinery.IsDead || Harv.TickUnload(self, Refinery))
|
||||
dockingState = State.Undock;
|
||||
return this;
|
||||
case State.Undock:
|
||||
return OnStateUndock(self);
|
||||
case State.Complete:
|
||||
if (Refinery.IsInWorld && !Refinery.IsDead)
|
||||
foreach (var nd in Refinery.TraitsImplementing<INotifyDocking>())
|
||||
nd.Undocked(Refinery, self);
|
||||
Harv.LastLinkedProc = Harv.LinkedProc;
|
||||
Harv.LinkProc(self, null);
|
||||
if (IsDragRequired)
|
||||
return Util.SequenceActivities(new Drag(self, EndDrag, StartDrag, DragLength), NextActivity);
|
||||
return NextActivity;
|
||||
}
|
||||
|
||||
throw new InvalidOperationException("Invalid harvester dock state");
|
||||
}
|
||||
|
||||
public override void Cancel(Actor self)
|
||||
{
|
||||
dockingState = State.Undock;
|
||||
base.Cancel(self);
|
||||
}
|
||||
|
||||
public override IEnumerable<Target> GetTargets(Actor self)
|
||||
{
|
||||
yield return Target.FromActor(Refinery);
|
||||
}
|
||||
|
||||
public virtual Activity OnStateDock(Actor self)
|
||||
{
|
||||
throw new NotImplementedException("Base class HarvesterDockSequence does not implement method OnStateDock!");
|
||||
}
|
||||
|
||||
public virtual Activity OnStateUndock(Actor self)
|
||||
{
|
||||
throw new NotImplementedException("Base class HarvesterDockSequence does not implement method OnStateUndock!");
|
||||
}
|
||||
}
|
||||
}
|
||||
40
OpenRA.Mods.Common/Activities/SpriteHarvesterDockSequence.cs
Normal file
40
OpenRA.Mods.Common/Activities/SpriteHarvesterDockSequence.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
#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.Activities;
|
||||
using OpenRA.Mods.Common.Traits;
|
||||
|
||||
namespace OpenRA.Mods.Common.Activities
|
||||
{
|
||||
public class SpriteHarvesterDockSequence : HarvesterDockSequence
|
||||
{
|
||||
readonly RenderUnit ru;
|
||||
|
||||
public SpriteHarvesterDockSequence(Actor self, Actor refinery, int dockAngle, bool isDragRequired, WVec dragOffset, int dragLength)
|
||||
: base(self, refinery, dockAngle, isDragRequired, dragOffset, dragLength)
|
||||
{
|
||||
ru = self.Trait<RenderUnit>();
|
||||
}
|
||||
|
||||
public override Activity OnStateDock(Actor self)
|
||||
{
|
||||
ru.PlayCustomAnimation(self, "dock", () => ru.PlayCustomAnimRepeating(self, "dock-loop"));
|
||||
dockingState = State.Loop;
|
||||
return this;
|
||||
}
|
||||
|
||||
public override Activity OnStateUndock(Actor self)
|
||||
{
|
||||
ru.PlayCustomAnimBackwards(self, "dock", () => dockingState = State.Complete);
|
||||
dockingState = State.Wait;
|
||||
return this;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user