Rework harvester resource claiming:

* Maintains lists of claims, and only restricts
   reservations for friendly units.
 * Removes OnNotifyResourceClaimLost; it's not
   clear whether that is still useful, and it
   prevents future necessary cleanups.
 * Moves other code without changing behaviour.

This fixed stale claims from dead units and enemy
claims from preventing otherwise valid harvest
activities.
This commit is contained in:
Paul Chote
2017-07-15 12:36:41 +00:00
committed by reaperrr
parent 9097837e6d
commit afd8b9ab86
9 changed files with 79 additions and 192 deletions

View File

@@ -10,6 +10,7 @@
#endregion
using System.Collections.Generic;
using System.Linq;
using OpenRA.Graphics;
using OpenRA.Traits;
@@ -18,112 +19,55 @@ namespace OpenRA.Mods.Common.Traits
[Desc("Allows harvesters to coordinate their operations. Attach this to the world actor.")]
public sealed class ResourceClaimLayerInfo : TraitInfo<ResourceClaimLayer> { }
public sealed class ResourceClaimLayer : IWorldLoaded
public sealed class ResourceClaimLayer
{
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.Disposed) return;
if (!claim.Claimer.IsInWorld) return;
if (claim.Claimer.IsDead) return;
claim.Claimer.Trait<INotifyResourceClaimLost>().OnNotifyResourceClaimLost(claim.Claimer, claim, claimer);
}
}
public void WorldLoaded(World w, WorldRenderer wr)
{
// 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);
}
readonly Dictionary<CPos, List<Actor>> claimByCell = new Dictionary<CPos, List<Actor>>(32);
readonly Dictionary<Actor, CPos> claimByActor = new Dictionary<Actor, CPos>(32);
/// <summary>
/// Attempt to claim the resource at the cell for the given actor.
/// Attempt to reserve the resource in a cell for the given actor.
/// </summary>
/// <param name="claimer"></param>
/// <param name="cell"></param>
/// <returns></returns>
public bool ClaimResource(Actor claimer, CPos cell)
public bool TryClaimCell(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;
var claimers = claimByCell.GetOrAdd(cell);
// 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;
// Clean up any stale claims
claimers.RemoveAll(a => a.IsDead);
// If an enemy/neutral claimed this, don't respect that claim:
}
// Prevent harvesters from the player or their allies fighting over the same cell
if (claimers.Any(c => c != claimer && claimer.Owner.IsAlliedWith(c.Owner)))
return false;
// Either nobody else claims this point or an enemy/neutral claims it:
MakeClaim(claimer, cell);
// Remove the actor's last claim, if it has one
CPos lastClaim;
if (claimByActor.TryGetValue(claimer, out lastClaim))
claimByCell.GetOrAdd(lastClaim).Remove(claimer);
claimers.Add(claimer);
claimByActor[claimer] = cell;
return true;
}
/// <summary>
/// Release the last resource claim made on this cell.
/// Returns false if the cell is already reserved by an allied actor.
/// </summary>
/// <param name="cell"></param>
public void UnclaimByCell(CPos cell, Actor claimer)
public bool CanClaimCell(Actor claimer, CPos cell)
{
ResourceClaim claim;
if (claimByCell.TryGetValue(cell, out claim))
Unclaim(claim, claimer);
return !claimByCell.GetOrAdd(cell)
.Any(c => c != claimer && !c.IsDead && claimer.Owner.IsAlliedWith(c.Owner));
}
/// <summary>
/// Release the last resource claim made by this actor.
/// </summary>
/// <param name="claimer"></param>
public void UnclaimByActor(Actor claimer)
public void RemoveClaim(Actor claimer)
{
ResourceClaim claim;
if (claimByActor.TryGetValue(claimer, out claim))
Unclaim(claim, claimer);
}
CPos lastClaim;
if (claimByActor.TryGetValue(claimer, out lastClaim))
claimByCell.GetOrAdd(lastClaim).Remove(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;
claimByActor.Remove(claimer);
}
}
}