Many harvester behavior improvements; summary below.
Implemented Harvester territory marking with a simple resource claim system in ResourceClaimLayer trait added to World. Added customCost for PathSearch to support new Harvester search preferences. Explicit delivery order forces harvester to always deliver to that refinery. Explicit harvest order frees harvester from forced delivery refinery and allows for auto-balancing. Harvesters auto-balance refinery choice such that no more than 3 harvesters are linked to any one refinery at a time. Harvesters try very hard to not block the refinery dock location. Harvesters try to avoid enemy territory when searching for resources. Group-select harvest order intelligently disperses harvesters around the order location. Fixed PathFinder caching to not be a sliding window. This is a correctness issue. Sliding window causes no-route paths to be cached permanently in tight move loops and doesn't allow eventual progress to be made. This may have negative performance implications.
This commit is contained in:
@@ -12,6 +12,7 @@ using System;
|
|||||||
using System.Drawing;
|
using System.Drawing;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using OpenRA.Graphics;
|
using OpenRA.Graphics;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
|
||||||
namespace OpenRA.Traits
|
namespace OpenRA.Traits
|
||||||
{
|
{
|
||||||
@@ -163,6 +164,12 @@ namespace OpenRA.Traits
|
|||||||
}
|
}
|
||||||
|
|
||||||
public ResourceType GetResource(CPos p) { return content[p.X, p.Y].type; }
|
public ResourceType GetResource(CPos p) { return content[p.X, p.Y].type; }
|
||||||
|
public int GetResourceDensity(CPos p) { return content[p.X, p.Y].density; }
|
||||||
|
public int GetMaxResourceDensity(CPos p)
|
||||||
|
{
|
||||||
|
if (content[p.X, p.Y].image == null) return 0;
|
||||||
|
return content[p.X, p.Y].image.Length - 1;
|
||||||
|
}
|
||||||
|
|
||||||
public struct CellContents
|
public struct CellContents
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -18,6 +18,9 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
public class DeliverResources : Activity
|
public class DeliverResources : Activity
|
||||||
{
|
{
|
||||||
bool isDocking;
|
bool isDocking;
|
||||||
|
int chosenTicks;
|
||||||
|
|
||||||
|
const int NextChooseTime = 100;
|
||||||
|
|
||||||
public DeliverResources() { }
|
public DeliverResources() { }
|
||||||
|
|
||||||
@@ -29,6 +32,22 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
var mobile = self.Trait<Mobile>();
|
var mobile = self.Trait<Mobile>();
|
||||||
var harv = self.Trait<Harvester>();
|
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.FrameNumber - chosenTicks > NextChooseTime)
|
||||||
|
{
|
||||||
|
harv.ChooseNewProc(self, null);
|
||||||
|
chosenTicks = self.World.FrameNumber;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
harv.LinkedProc = harv.OwnerLinkedProc;
|
||||||
|
}
|
||||||
|
|
||||||
if (harv.LinkedProc == null || !harv.LinkedProc.IsInWorld)
|
if (harv.LinkedProc == null || !harv.LinkedProc.IsInWorld)
|
||||||
harv.ChooseNewProc(self, null);
|
harv.ChooseNewProc(self, null);
|
||||||
|
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ using System.Linq;
|
|||||||
using OpenRA.Mods.RA.Move;
|
using OpenRA.Mods.RA.Move;
|
||||||
using OpenRA.Mods.RA.Render;
|
using OpenRA.Mods.RA.Render;
|
||||||
using OpenRA.Traits;
|
using OpenRA.Traits;
|
||||||
|
using System;
|
||||||
|
|
||||||
namespace OpenRA.Mods.RA.Activities
|
namespace OpenRA.Mods.RA.Activities
|
||||||
{
|
{
|
||||||
@@ -24,19 +25,85 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
if (IsCanceled || NextActivity != null) return NextActivity;
|
if (IsCanceled || NextActivity != null) return NextActivity;
|
||||||
|
|
||||||
var harv = self.Trait<Harvester>();
|
var harv = self.Trait<Harvester>();
|
||||||
|
|
||||||
if (harv.IsFull)
|
if (harv.IsFull)
|
||||||
return Util.SequenceActivities(new DeliverResources(), NextActivity);
|
return Util.SequenceActivities(new DeliverResources(), NextActivity);
|
||||||
|
|
||||||
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
|
var harvInfo = self.Info.Traits.Get<HarvesterInfo>();
|
||||||
var mobile = self.Trait<Mobile>();
|
var mobile = self.Trait<Mobile>();
|
||||||
var mobileInfo = self.Info.Traits.Get<MobileInfo>();
|
var mobileInfo = self.Info.Traits.Get<MobileInfo>();
|
||||||
var res = self.World.WorldActor.Trait<ResourceLayer>();
|
var resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
||||||
var path = self.World.WorldActor.Trait<PathFinder>().FindPath(PathSearch.Search(self.World, mobileInfo, self.Owner, true)
|
var territory = self.World.WorldActor.Trait<ResourceClaimLayer>();
|
||||||
.WithHeuristic(loc => (res.GetResource(loc) != null && harvInfo.Resources.Contains(res.GetResource(loc).info.Name)) ? 0 : 1)
|
|
||||||
.FromPoint(self.Location));
|
// Find harvestable resources nearby:
|
||||||
|
var path = self.World.WorldActor.Trait<PathFinder>().FindPath(
|
||||||
|
PathSearch.Search(self.World, mobileInfo, self.Owner, true)
|
||||||
|
.WithCustomCost(loc =>
|
||||||
|
{
|
||||||
|
// Avoid enemy territory:
|
||||||
|
int safetycost = (
|
||||||
|
// TODO: calculate weapons ranges of units and factor those in instead of hard-coding 8.
|
||||||
|
from u in self.World.FindUnitsInCircle(loc.ToPPos(), Game.CellSize * 8)
|
||||||
|
where !u.Destroyed
|
||||||
|
where self.Owner.Stances[u.Owner] == Stance.Enemy
|
||||||
|
select Math.Max(0, 64 - (loc - u.Location).LengthSquared)
|
||||||
|
).Sum();
|
||||||
|
|
||||||
|
return safetycost;
|
||||||
|
})
|
||||||
|
.WithHeuristic(loc =>
|
||||||
|
{
|
||||||
|
// Don't harvest out of range:
|
||||||
|
int distSquared = (loc - (harv.LastOrderLocation ?? harv.LinkedProc.Location)).LengthSquared;
|
||||||
|
if (distSquared > (12 * 12))
|
||||||
|
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;
|
||||||
|
|
||||||
|
// Another harvester has claimed this resource:
|
||||||
|
ResourceClaim claim;
|
||||||
|
if (territory.IsClaimedByAnyoneElse(self, loc, out claim)) return 1;
|
||||||
|
|
||||||
|
// Is anyone covering the location already?
|
||||||
|
// NOTE(jsd): This is required to prevent harvester deadlocking.
|
||||||
|
var unitsAtLoc =
|
||||||
|
from u in self.World.FindUnits(loc.ToPPos(), loc.ToPPos() + PVecInt.OneCell)
|
||||||
|
where u != self
|
||||||
|
select u;
|
||||||
|
if (unitsAtLoc.Any()) return 1;
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
})
|
||||||
|
.FromPoint(self.Location)
|
||||||
|
);
|
||||||
|
|
||||||
if (path.Count == 0)
|
if (path.Count == 0)
|
||||||
return NextActivity;
|
{
|
||||||
|
if (!harv.IsEmpty)
|
||||||
|
return new DeliverResources();
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// Get out of the way if we are:
|
||||||
|
harv.UnblockRefinery(self);
|
||||||
|
if (NextActivity != null)
|
||||||
|
return Util.SequenceActivities(NextActivity, new Wait(90), this);
|
||||||
|
else
|
||||||
|
return Util.SequenceActivities(new Wait(90), this);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Attempt to claim a resource as ours:
|
||||||
|
if (!territory.ClaimResource(self, path[0]))
|
||||||
|
return Util.SequenceActivities(new Wait(25), this);
|
||||||
|
|
||||||
|
// 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(path[0]), Color.Red, false);
|
self.SetTargetLine(Target.FromCell(path[0]), Color.Red, false);
|
||||||
return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), this);
|
return Util.SequenceActivities(mobile.MoveTo(path[0], 1), new HarvestResource(), this);
|
||||||
@@ -55,23 +122,38 @@ namespace OpenRA.Mods.RA.Activities
|
|||||||
public override Activity Tick(Actor self)
|
public override Activity Tick(Actor self)
|
||||||
{
|
{
|
||||||
if (isHarvesting) return this;
|
if (isHarvesting) return this;
|
||||||
if (IsCanceled) return NextActivity;
|
|
||||||
|
var territory = self.World.WorldActor.Trait<ResourceClaimLayer>();
|
||||||
|
if (IsCanceled)
|
||||||
|
{
|
||||||
|
territory.UnclaimByActor(self);
|
||||||
|
return NextActivity;
|
||||||
|
}
|
||||||
|
|
||||||
var harv = self.Trait<Harvester>();
|
var harv = self.Trait<Harvester>();
|
||||||
harv.LastHarvestedCell = self.Location;
|
harv.LastHarvestedCell = self.Location;
|
||||||
|
|
||||||
if (harv.IsFull)
|
if (harv.IsFull)
|
||||||
|
{
|
||||||
|
territory.UnclaimByActor(self);
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
|
}
|
||||||
|
|
||||||
|
var resLayer = self.World.WorldActor.Trait<ResourceLayer>();
|
||||||
|
var resource = resLayer.Harvest(self.Location);
|
||||||
|
if (resource == null)
|
||||||
|
{
|
||||||
|
territory.UnclaimByActor(self);
|
||||||
|
return NextActivity;
|
||||||
|
}
|
||||||
|
|
||||||
var renderUnit = self.Trait<RenderUnit>(); /* better have one of these! */
|
var renderUnit = self.Trait<RenderUnit>(); /* better have one of these! */
|
||||||
var resource = self.World.WorldActor.Trait<ResourceLayer>().Harvest(self.Location);
|
|
||||||
if (resource == null)
|
|
||||||
return NextActivity;
|
|
||||||
|
|
||||||
if (renderUnit.anim.CurrentSequence.Name != "harvest")
|
if (renderUnit.anim.CurrentSequence.Name != "harvest")
|
||||||
{
|
{
|
||||||
isHarvesting = true;
|
isHarvesting = true;
|
||||||
renderUnit.PlayCustomAnimation(self, "harvest", () => isHarvesting = false);
|
renderUnit.PlayCustomAnimation(self, "harvest", () => isHarvesting = false);
|
||||||
}
|
}
|
||||||
|
|
||||||
harv.AcceptResource(resource);
|
harv.AcceptResource(resource);
|
||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,8 @@ namespace OpenRA.Mods.RA
|
|||||||
state = State.Wait;
|
state = State.Wait;
|
||||||
return this;
|
return this;
|
||||||
case State.Complete:
|
case State.Complete:
|
||||||
|
harv.LastLinkedProc = harv.LinkedProc;
|
||||||
|
harv.LinkedProc = null;
|
||||||
return NextActivity;
|
return NextActivity;
|
||||||
}
|
}
|
||||||
throw new InvalidOperationException("Invalid harvester dock state");
|
throw new InvalidOperationException("Invalid harvester dock state");
|
||||||
|
|||||||
@@ -30,13 +30,16 @@ namespace OpenRA.Mods.RA
|
|||||||
}
|
}
|
||||||
|
|
||||||
public class Harvester : IIssueOrder, IResolveOrder, IPips,
|
public class Harvester : IIssueOrder, IResolveOrder, IPips,
|
||||||
IExplodeModifier, IOrderVoice, ISpeedModifier, ISync
|
IExplodeModifier, IOrderVoice, ISpeedModifier, ISync, INotifyResourceClaimLost, INotifyIdle
|
||||||
{
|
{
|
||||||
Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
|
Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
|
||||||
|
|
||||||
|
[Sync] public Actor OwnerLinkedProc = null;
|
||||||
|
[Sync] public Actor LastLinkedProc = null;
|
||||||
[Sync] public Actor LinkedProc = null;
|
[Sync] public Actor LinkedProc = null;
|
||||||
[Sync] int currentUnloadTicks;
|
[Sync] int currentUnloadTicks;
|
||||||
public CPos? LastHarvestedCell = null;
|
public CPos? LastHarvestedCell = null;
|
||||||
|
public CPos? LastOrderLocation = null;
|
||||||
[Sync] public int ContentValue { get { return contents.Sum(c => c.Key.ValuePerUnit * c.Value); } }
|
[Sync] public int ContentValue { get { return contents.Sum(c => c.Key.ValuePerUnit * c.Value); } }
|
||||||
readonly HarvesterInfo Info;
|
readonly HarvesterInfo Info;
|
||||||
|
|
||||||
@@ -46,36 +49,52 @@ namespace OpenRA.Mods.RA
|
|||||||
self.QueueActivity(new CallFunc(() => ChooseNewProc(self, null)));
|
self.QueueActivity(new CallFunc(() => ChooseNewProc(self, null)));
|
||||||
}
|
}
|
||||||
|
|
||||||
public void ChooseNewProc(Actor self, Actor ignore) { LinkedProc = ClosestProc(self, ignore); }
|
public void ChooseNewProc(Actor self, Actor ignore)
|
||||||
|
{
|
||||||
|
LinkedProc = ClosestProc(self, ignore);
|
||||||
|
LastLinkedProc = null;
|
||||||
|
}
|
||||||
|
|
||||||
public void ContinueHarvesting(Actor self)
|
public void ContinueHarvesting(Actor self)
|
||||||
{
|
{
|
||||||
if (LastHarvestedCell.HasValue)
|
// Move out of the refinery dock and continue harvesting:
|
||||||
{
|
UnblockRefinery(self);
|
||||||
var mobile = self.Trait<Mobile>();
|
|
||||||
self.QueueActivity( mobile.MoveTo(LastHarvestedCell.Value, 5) );
|
|
||||||
self.SetTargetLine(Target.FromCell(LastHarvestedCell.Value), Color.Red, false);
|
|
||||||
}
|
|
||||||
self.QueueActivity(new FindResources());
|
self.QueueActivity(new FindResources());
|
||||||
}
|
}
|
||||||
|
|
||||||
Actor ClosestProc(Actor self, Actor ignore)
|
Actor ClosestProc(Actor self, Actor ignore)
|
||||||
{
|
{
|
||||||
var refs = self.World.ActorsWithTrait<IAcceptOre>()
|
// Find all refineries and their occupancy count:
|
||||||
.Where(x => x.Actor != ignore && x.Actor.Owner == self.Owner)
|
var refs = (
|
||||||
.ToList();
|
from r in self.World.ActorsWithTrait<IAcceptOre>()
|
||||||
|
where r.Actor != ignore && r.Actor.Owner == self.Owner
|
||||||
|
let linkedHarvs = self.World.ActorsWithTrait<Harvester>().Where(a => a.Trait.LinkedProc == r.Actor).Count()
|
||||||
|
select new { Location = r.Actor.Location + r.Trait.DeliverOffset, Actor = r.Actor, Occupancy = linkedHarvs }
|
||||||
|
).ToDictionary(r => r.Location);
|
||||||
|
|
||||||
|
// Start a search from each refinery's delivery location:
|
||||||
var mi = self.Info.Traits.Get<MobileInfo>();
|
var mi = self.Info.Traits.Get<MobileInfo>();
|
||||||
var path = self.World.WorldActor.Trait<PathFinder>().FindPath(
|
var path = self.World.WorldActor.Trait<PathFinder>().FindPath(
|
||||||
PathSearch.FromPoints(self.World, mi, self.Owner,
|
PathSearch.FromPoints(self.World, mi, self.Owner, refs.Values.Select(r => r.Location), self.Location, false)
|
||||||
refs.Select(r => r.Actor.Location + r.Trait.DeliverOffset),
|
.WithCustomCost((loc) =>
|
||||||
self.Location, false));
|
{
|
||||||
|
if (!refs.ContainsKey(loc)) return 0;
|
||||||
|
|
||||||
|
int occupancy = refs[loc].Occupancy;
|
||||||
|
// 4 harvesters clogs up the refinery's delivery location:
|
||||||
|
if (occupancy >= 3) return int.MaxValue;
|
||||||
|
|
||||||
|
// Prefer refineries with less occupancy (multiplier is to offset distance cost):
|
||||||
|
return occupancy * 12;
|
||||||
|
})
|
||||||
|
);
|
||||||
|
|
||||||
|
// Reverse the found-path to find the refinery location instead of our location:
|
||||||
path.Reverse();
|
path.Reverse();
|
||||||
|
|
||||||
if (path.Count != 0)
|
if (path.Count != 0)
|
||||||
return refs.Where(x => x.Actor.Location + x.Trait.DeliverOffset == path[0])
|
return refs[path[0]].Actor;
|
||||||
.Select(a => a.Actor).FirstOrDefault();
|
|
||||||
else
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,6 +108,43 @@ namespace OpenRA.Mods.RA
|
|||||||
else contents[type.info]++;
|
else contents[type.info]++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void UnblockRefinery(Actor self)
|
||||||
|
{
|
||||||
|
// Check that we're not in a critical location and being useless (refinery drop-off):
|
||||||
|
var lastproc = LastLinkedProc ?? LinkedProc;
|
||||||
|
if (lastproc != null)
|
||||||
|
{
|
||||||
|
var deliveryLoc = lastproc.Location + lastproc.Trait<IAcceptOre>().DeliverOffset;
|
||||||
|
if (self.Location == deliveryLoc)
|
||||||
|
{
|
||||||
|
// Get out of the way:
|
||||||
|
var mobile = self.Trait<Mobile>();
|
||||||
|
var harv = self.Trait<Harvester>();
|
||||||
|
// TODO: would like an interruptible move here
|
||||||
|
var moveTo = harv.LastHarvestedCell ?? (deliveryLoc + new CVec(0, 4));
|
||||||
|
self.QueueActivity(mobile.MoveTo(moveTo, 1));
|
||||||
|
self.SetTargetLine(Target.FromCell(moveTo), Color.Red, false);
|
||||||
|
self.World.WorldActor.Trait<ResourceClaimLayer>().ClaimResource(self, moveTo);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void TickIdle(Actor self)
|
||||||
|
{
|
||||||
|
// Are we not empty? Deliver resources:
|
||||||
|
if (!IsEmpty)
|
||||||
|
{
|
||||||
|
self.QueueActivity(new DeliverResources());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
UnblockRefinery(self);
|
||||||
|
|
||||||
|
// Wait for a bit before becoming idle again:
|
||||||
|
self.QueueActivity(new Wait(10));
|
||||||
|
}
|
||||||
|
|
||||||
// Returns true when unloading is complete
|
// Returns true when unloading is complete
|
||||||
public bool TickUnload(Actor self, Actor proc)
|
public bool TickUnload(Actor self, Actor proc)
|
||||||
{
|
{
|
||||||
@@ -145,23 +201,35 @@ namespace OpenRA.Mods.RA
|
|||||||
{
|
{
|
||||||
if (order.OrderString == "Harvest")
|
if (order.OrderString == "Harvest")
|
||||||
{
|
{
|
||||||
|
// NOTE: An explicit harvest order allows the harvester to decide which refinery to deliver to.
|
||||||
|
OwnerLinkedProc = null;
|
||||||
|
|
||||||
var mobile = self.Trait<Mobile>();
|
var mobile = self.Trait<Mobile>();
|
||||||
self.CancelActivity();
|
self.CancelActivity();
|
||||||
if (order.TargetLocation != CPos.Zero)
|
if (order.TargetLocation != CPos.Zero)
|
||||||
{
|
{
|
||||||
self.QueueActivity(mobile.MoveTo(order.TargetLocation, 0));
|
var loc = order.TargetLocation;
|
||||||
self.SetTargetLine(Target.FromOrder(order), Color.Red);
|
var territory = self.World.WorldActor.Trait<ResourceClaimLayer>();
|
||||||
|
|
||||||
|
// Find the nearest claimable cell to the order location (useful for group-select harvest):
|
||||||
|
loc = mobile.NearestCell(loc, p => mobile.CanEnterCell(p) && territory.ClaimResource(self, p), 1, 6);
|
||||||
|
|
||||||
|
self.QueueActivity(mobile.MoveTo(loc, 0));
|
||||||
|
self.SetTargetLine(Target.FromCell(loc), Color.Red);
|
||||||
|
|
||||||
|
LastOrderLocation = loc;
|
||||||
}
|
}
|
||||||
self.QueueActivity(new FindResources());
|
self.QueueActivity(new FindResources());
|
||||||
}
|
}
|
||||||
else if (order.OrderString == "Deliver")
|
else if (order.OrderString == "Deliver")
|
||||||
{
|
{
|
||||||
|
// NOTE: An explicit deliver order forces the harvester to always deliver to this refinery.
|
||||||
var iao = order.TargetActor.TraitOrDefault<IAcceptOre>();
|
var iao = order.TargetActor.TraitOrDefault<IAcceptOre>();
|
||||||
if (iao == null || !iao.AllowDocking)
|
if (iao == null || !iao.AllowDocking)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (order.TargetActor != LinkedProc)
|
if (order.TargetActor != OwnerLinkedProc)
|
||||||
LinkedProc = order.TargetActor;
|
LinkedProc = OwnerLinkedProc = order.TargetActor;
|
||||||
|
|
||||||
if (IsEmpty)
|
if (IsEmpty)
|
||||||
return;
|
return;
|
||||||
@@ -179,6 +247,15 @@ namespace OpenRA.Mods.RA
|
|||||||
ChooseNewProc(self, proc);
|
ChooseNewProc(self, proc);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void OnNotifyResourceClaimLost(Actor self, ResourceClaim claim, Actor claimer)
|
||||||
|
{
|
||||||
|
if (self == claimer) return;
|
||||||
|
|
||||||
|
// Our claim on a resource was stolen, find more unclaimed resources:
|
||||||
|
self.CancelActivity();
|
||||||
|
self.QueueActivity(new FindResources());
|
||||||
|
}
|
||||||
|
|
||||||
PipType GetPipAt(int i)
|
PipType GetPipAt(int i)
|
||||||
{
|
{
|
||||||
var n = i * Info.Capacity / Info.PipCount;
|
var n = i * Info.Capacity / Info.PipCount;
|
||||||
|
|||||||
@@ -200,13 +200,18 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
}
|
}
|
||||||
|
|
||||||
public CPos NearestMoveableCell(CPos target)
|
public CPos NearestMoveableCell(CPos target)
|
||||||
|
{
|
||||||
|
return NearestMoveableCell(target, 1, 10);
|
||||||
|
}
|
||||||
|
|
||||||
|
public CPos NearestMoveableCell(CPos target, int minRange, int maxRange)
|
||||||
{
|
{
|
||||||
if (CanEnterCell(target))
|
if (CanEnterCell(target))
|
||||||
return target;
|
return target;
|
||||||
|
|
||||||
var searched = new List<CPos>();
|
var searched = new List<CPos>();
|
||||||
// Limit search to a radius of 10 tiles
|
// Limit search to a radius of 10 tiles
|
||||||
for (int r = 1; r < 10; r++)
|
for (int r = minRange; r < maxRange; r++)
|
||||||
foreach (var tile in self.World.FindTilesInCircle(target, r).Except(searched))
|
foreach (var tile in self.World.FindTilesInCircle(target, r).Except(searched))
|
||||||
{
|
{
|
||||||
if (CanEnterCell(tile))
|
if (CanEnterCell(tile))
|
||||||
@@ -219,6 +224,25 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
return target;
|
return target;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public CPos NearestCell(CPos target, Func<CPos, bool> check, int minRange, int maxRange)
|
||||||
|
{
|
||||||
|
if (check(target))
|
||||||
|
return target;
|
||||||
|
|
||||||
|
var searched = new List<CPos>();
|
||||||
|
for (int r = minRange; r < maxRange; r++)
|
||||||
|
foreach (var tile in self.World.FindTilesInCircle(target, r).Except(searched))
|
||||||
|
{
|
||||||
|
if (check(tile))
|
||||||
|
return tile;
|
||||||
|
|
||||||
|
searched.Add(tile);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Couldn't find a cell
|
||||||
|
return target;
|
||||||
|
}
|
||||||
|
|
||||||
void PerformMoveInner(Actor self, CPos targetLocation, bool queued)
|
void PerformMoveInner(Actor self, CPos targetLocation, bool queued)
|
||||||
{
|
{
|
||||||
var currentLocation = NearestMoveableCell(targetLocation);
|
var currentLocation = NearestMoveableCell(targetLocation);
|
||||||
|
|||||||
@@ -233,7 +233,7 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
|
|
||||||
public override void Cancel( Actor self )
|
public override void Cancel( Actor self )
|
||||||
{
|
{
|
||||||
path = new List<CPos>();
|
path = new List<CPos>(0);
|
||||||
base.Cancel(self);
|
base.Cancel(self);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -47,7 +47,8 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
if (cached != null)
|
if (cached != null)
|
||||||
{
|
{
|
||||||
Log.Write("debug", "Actor {0} asked for a path from {1} tick(s) ago", self.ActorID, world.FrameNumber - cached.tick);
|
Log.Write("debug", "Actor {0} asked for a path from {1} tick(s) ago", self.ActorID, world.FrameNumber - cached.tick);
|
||||||
cached.tick = world.FrameNumber;
|
if (world.FrameNumber - cached.tick > MaxPathAge)
|
||||||
|
CachedPaths.Remove(cached);
|
||||||
return new List<CPos>(cached.result);
|
return new List<CPos>(cached.result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
public CellInfo[,] cellInfo;
|
public CellInfo[,] cellInfo;
|
||||||
public PriorityQueue<PathDistance> queue;
|
public PriorityQueue<PathDistance> queue;
|
||||||
public Func<CPos, int> heuristic;
|
public Func<CPos, int> heuristic;
|
||||||
|
Func<CPos, int> customCost;
|
||||||
Func<CPos, bool> customBlock;
|
Func<CPos, bool> customBlock;
|
||||||
public bool checkForBlocked;
|
public bool checkForBlocked;
|
||||||
public Actor ignoreBuilding;
|
public Actor ignoreBuilding;
|
||||||
@@ -35,6 +36,7 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
cellInfo = InitCellInfo();
|
cellInfo = InitCellInfo();
|
||||||
this.mobileInfo = mobileInfo;
|
this.mobileInfo = mobileInfo;
|
||||||
this.owner = owner;
|
this.owner = owner;
|
||||||
|
customCost = null;
|
||||||
queue = new PriorityQueue<PathDistance>();
|
queue = new PriorityQueue<PathDistance>();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -62,6 +64,12 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
return this;
|
return this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public PathSearch WithCustomCost(Func<CPos, int> w)
|
||||||
|
{
|
||||||
|
customCost = w;
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
public PathSearch WithoutLaneBias()
|
public PathSearch WithoutLaneBias()
|
||||||
{
|
{
|
||||||
LaneBias = 0;
|
LaneBias = 0;
|
||||||
@@ -92,6 +100,13 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
if (thisCost == int.MaxValue)
|
if (thisCost == int.MaxValue)
|
||||||
return p.Location;
|
return p.Location;
|
||||||
|
|
||||||
|
if (customCost != null)
|
||||||
|
{
|
||||||
|
int c = customCost(p.Location);
|
||||||
|
if (c == int.MaxValue)
|
||||||
|
return p.Location;
|
||||||
|
}
|
||||||
|
|
||||||
foreach( CVec d in directions )
|
foreach( CVec d in directions )
|
||||||
{
|
{
|
||||||
CPos newHere = p.Location + d;
|
CPos newHere = p.Location + d;
|
||||||
@@ -118,7 +133,12 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
int cellCost = costHere;
|
int cellCost = costHere;
|
||||||
if (d.X * d.Y != 0) cellCost = (cellCost * 34) / 24;
|
if (d.X * d.Y != 0) cellCost = (cellCost * 34) / 24;
|
||||||
|
|
||||||
|
if (customCost != null)
|
||||||
|
cellCost += customCost(newHere);
|
||||||
|
|
||||||
// directional bonuses for smoother flow!
|
// directional bonuses for smoother flow!
|
||||||
|
if (LaneBias != 0)
|
||||||
|
{
|
||||||
var ux = (newHere.X + (inReverse ? 1 : 0) & 1);
|
var ux = (newHere.X + (inReverse ? 1 : 0) & 1);
|
||||||
var uy = (newHere.Y + (inReverse ? 1 : 0) & 1);
|
var uy = (newHere.Y + (inReverse ? 1 : 0) & 1);
|
||||||
|
|
||||||
@@ -126,6 +146,7 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
else if (ux == 1 && d.Y > 0) cellCost += LaneBias;
|
else if (ux == 1 && d.Y > 0) cellCost += LaneBias;
|
||||||
if (uy == 0 && d.X < 0) cellCost += LaneBias;
|
if (uy == 0 && d.X < 0) cellCost += LaneBias;
|
||||||
else if (uy == 1 && d.X > 0) cellCost += LaneBias;
|
else if (uy == 1 && d.X > 0) cellCost += LaneBias;
|
||||||
|
}
|
||||||
|
|
||||||
int newCost = cellInfo[p.Location.X, p.Location.Y].MinCost + cellCost;
|
int newCost = cellInfo[p.Location.X, p.Location.Y].MinCost + cellCost;
|
||||||
|
|
||||||
@@ -136,7 +157,6 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
cellInfo[newHere.X, newHere.Y].MinCost = newCost;
|
cellInfo[newHere.X, newHere.Y].MinCost = newCost;
|
||||||
|
|
||||||
queue.Add(new PathDistance(newCost + est, newHere));
|
queue.Add(new PathDistance(newCost + est, newHere));
|
||||||
|
|
||||||
}
|
}
|
||||||
return p.Location;
|
return p.Location;
|
||||||
}
|
}
|
||||||
@@ -164,16 +184,20 @@ namespace OpenRA.Mods.RA.Move
|
|||||||
|
|
||||||
public static PathSearch Search(World world, MobileInfo mi, Player owner, bool checkForBlocked)
|
public static PathSearch Search(World world, MobileInfo mi, Player owner, bool checkForBlocked)
|
||||||
{
|
{
|
||||||
var search = new PathSearch(world, mi, owner) {
|
var search = new PathSearch(world, mi, owner)
|
||||||
checkForBlocked = checkForBlocked };
|
{
|
||||||
|
checkForBlocked = checkForBlocked
|
||||||
|
};
|
||||||
return search;
|
return search;
|
||||||
}
|
}
|
||||||
|
|
||||||
public static PathSearch FromPoint(World world, MobileInfo mi, Player owner, CPos from, CPos target, bool checkForBlocked)
|
public static PathSearch FromPoint(World world, MobileInfo mi, Player owner, CPos from, CPos target, bool checkForBlocked)
|
||||||
{
|
{
|
||||||
var search = new PathSearch(world, mi, owner) {
|
var search = new PathSearch(world, mi, owner)
|
||||||
|
{
|
||||||
heuristic = DefaultEstimator(target),
|
heuristic = DefaultEstimator(target),
|
||||||
checkForBlocked = checkForBlocked };
|
checkForBlocked = checkForBlocked
|
||||||
|
};
|
||||||
|
|
||||||
search.AddInitialCell(from);
|
search.AddInitialCell(from);
|
||||||
return search;
|
return search;
|
||||||
|
|||||||
@@ -356,6 +356,8 @@
|
|||||||
<Compile Include="Widgets\WorldCommandWidget.cs" />
|
<Compile Include="Widgets\WorldCommandWidget.cs" />
|
||||||
<Compile Include="Widgets\WorldTooltipWidget.cs" />
|
<Compile Include="Widgets\WorldTooltipWidget.cs" />
|
||||||
<Compile Include="World\ChooseBuildTabOnSelect.cs" />
|
<Compile Include="World\ChooseBuildTabOnSelect.cs" />
|
||||||
|
<Compile Include="World\ResourceClaim.cs" />
|
||||||
|
<Compile Include="World\ResourceClaimLayer.cs" />
|
||||||
<Compile Include="World\PlayMusicOnMapLoad.cs" />
|
<Compile Include="World\PlayMusicOnMapLoad.cs" />
|
||||||
<Compile Include="World\SmudgeLayer.cs" />
|
<Compile Include="World\SmudgeLayer.cs" />
|
||||||
<Compile Include="Player\BaseAttackNotifier.cs" />
|
<Compile Include="Player\BaseAttackNotifier.cs" />
|
||||||
|
|||||||
@@ -116,7 +116,6 @@ namespace OpenRA.Mods.RA
|
|||||||
harv.QueueActivity( new CallFunc( () => harv.Trait<Harvester>().ContinueHarvesting(harv) ) );
|
harv.QueueActivity( new CallFunc( () => harv.Trait<Harvester>().ContinueHarvesting(harv) ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
|
public void OnCapture(Actor self, Actor captor, Player oldOwner, Player newOwner)
|
||||||
{
|
{
|
||||||
// Steal any docked harv too
|
// Steal any docked harv too
|
||||||
|
|||||||
@@ -37,4 +37,9 @@ namespace OpenRA.Mods.RA
|
|||||||
{
|
{
|
||||||
IEnumerable<string> ProvidesPrerequisites {get;}
|
IEnumerable<string> ProvidesPrerequisites {get;}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public interface INotifyResourceClaimLost
|
||||||
|
{
|
||||||
|
void OnNotifyResourceClaimLost(Actor self, ResourceClaim claim, Actor claimer);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
15
OpenRA.Mods.RA/World/ResourceClaim.cs
Normal file
15
OpenRA.Mods.RA/World/ResourceClaim.cs
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace OpenRA.Mods.RA
|
||||||
|
{
|
||||||
|
public sealed class ResourceClaim
|
||||||
|
{
|
||||||
|
public readonly Actor Claimer;
|
||||||
|
public CPos Cell;
|
||||||
|
|
||||||
|
public ResourceClaim(Actor claimer, CPos cell) { Claimer = claimer; Cell = cell; }
|
||||||
|
}
|
||||||
|
}
|
||||||
121
OpenRA.Mods.RA/World/ResourceClaimLayer.cs
Normal file
121
OpenRA.Mods.RA/World/ResourceClaimLayer.cs
Normal file
@@ -0,0 +1,121 @@
|
|||||||
|
using System;
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
using OpenRA.Traits;
|
||||||
|
|
||||||
|
namespace OpenRA.Mods.RA
|
||||||
|
{
|
||||||
|
public sealed class ResourceClaimLayerInfo : TraitInfo<ResourceClaimLayer>
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
public sealed class ResourceClaimLayer : IWorldLoaded
|
||||||
|
{
|
||||||
|
Dictionary<CPos, ResourceClaim> claimByCell;
|
||||||
|
Dictionary<Actor, ResourceClaim> claimByActor;
|
||||||
|
|
||||||
|
private void MakeClaim(Actor claimer, CPos cell)
|
||||||
|
{
|
||||||
|
UnclaimByActor(claimer);
|
||||||
|
UnclaimByCell(cell, claimer);
|
||||||
|
claimByActor[claimer] = claimByCell[cell] = new ResourceClaim(claimer, cell);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void Unclaim(ResourceClaim claim, Actor claimer)
|
||||||
|
{
|
||||||
|
if (claimByActor.Remove(claim.Claimer) & claimByCell.Remove(claim.Cell))
|
||||||
|
{
|
||||||
|
if (claim.Claimer.Destroyed) return;
|
||||||
|
if (!claim.Claimer.IsInWorld) return;
|
||||||
|
if (claim.Claimer.IsDead()) return;
|
||||||
|
|
||||||
|
claim.Claimer.Trait<INotifyResourceClaimLost>().OnNotifyResourceClaimLost(claim.Claimer, claim, claimer);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void WorldLoaded(OpenRA.World w)
|
||||||
|
{
|
||||||
|
// NOTE(jsd): 32 seems a sane default initial capacity for the total # of harvesters in a game. Purely a guesstimate.
|
||||||
|
claimByCell = new Dictionary<CPos, ResourceClaim>(32);
|
||||||
|
claimByActor = new Dictionary<Actor, ResourceClaim>(32);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Attempt to claim the resource at the cell for the given actor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="claimer"></param>
|
||||||
|
/// <param name="cell"></param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public bool ClaimResource(Actor claimer, CPos cell)
|
||||||
|
{
|
||||||
|
// Has anyone else claimed this point?
|
||||||
|
ResourceClaim claim;
|
||||||
|
if (claimByCell.TryGetValue(cell, out claim))
|
||||||
|
{
|
||||||
|
// Same claimer:
|
||||||
|
if (claim.Claimer == claimer) return true;
|
||||||
|
|
||||||
|
// This is to prevent in-fighting amongst friendly harvesters:
|
||||||
|
if (claimer.Owner == claim.Claimer.Owner) return false;
|
||||||
|
if (claimer.Owner.Stances[claim.Claimer.Owner] == Stance.Ally) return false;
|
||||||
|
|
||||||
|
// If an enemy/neutral claimed this, don't respect that claim:
|
||||||
|
}
|
||||||
|
|
||||||
|
// Either nobody else claims this point or an enemy/neutral claims it:
|
||||||
|
MakeClaim(claimer, cell);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Release the last resource claim made on this cell.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="cell"></param>
|
||||||
|
public void UnclaimByCell(CPos cell, Actor claimer)
|
||||||
|
{
|
||||||
|
ResourceClaim claim;
|
||||||
|
if (claimByCell.TryGetValue(cell, out claim))
|
||||||
|
Unclaim(claim, claimer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Release the last resource claim made by this actor.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="claimer"></param>
|
||||||
|
public void UnclaimByActor(Actor claimer)
|
||||||
|
{
|
||||||
|
ResourceClaim claim;
|
||||||
|
if (claimByActor.TryGetValue(claimer, out claim))
|
||||||
|
Unclaim(claim, claimer);
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Is the cell location <paramref name="cell"/> claimed for harvesting by any other actor?
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="self"></param>
|
||||||
|
/// <param name="cell"></param>
|
||||||
|
/// <returns>true if already claimed by an ally that isn't <paramref name="self"/>; false otherwise.</returns>
|
||||||
|
public bool IsClaimedByAnyoneElse(Actor self, CPos cell, out ResourceClaim claim)
|
||||||
|
{
|
||||||
|
if (claimByCell.TryGetValue(cell, out claim))
|
||||||
|
{
|
||||||
|
// Same claimer:
|
||||||
|
if (claim.Claimer == self) return false;
|
||||||
|
|
||||||
|
// This is to prevent in-fighting amongst friendly harvesters:
|
||||||
|
if (self.Owner == claim.Claimer.Owner) return true;
|
||||||
|
if (self.Owner.Stances[claim.Claimer.Owner] == Stance.Ally) return true;
|
||||||
|
|
||||||
|
// If an enemy/neutral claimed this, don't respect that claim and fall through:
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// No claim.
|
||||||
|
claim = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -261,6 +261,7 @@ World:
|
|||||||
Race: soviet
|
Race: soviet
|
||||||
BibLayer:
|
BibLayer:
|
||||||
ResourceLayer:
|
ResourceLayer:
|
||||||
|
ResourceClaimLayer:
|
||||||
ResourceType@ore:
|
ResourceType@ore:
|
||||||
ResourceType: 1
|
ResourceType: 1
|
||||||
Palette: terrain
|
Palette: terrain
|
||||||
|
|||||||
Reference in New Issue
Block a user