Replace ResourceLayer references with IResourceLayer in traits/warheads.

This commit is contained in:
Paul Chote
2021-03-07 17:31:26 +00:00
committed by reaperrr
parent 5adcbe4c78
commit dcd8eccee4
14 changed files with 72 additions and 80 deletions

View File

@@ -63,7 +63,7 @@ namespace OpenRA.Mods.Common.Traits
IPathFinder pathfinder;
DomainIndex domainIndex;
ResourceLayer resLayer;
IResourceLayer resourceLayer;
ResourceClaimLayer claimLayer;
IBotRequestUnitProduction[] requestUnitProduction;
int scanForIdleHarvestersTicks;
@@ -85,7 +85,7 @@ namespace OpenRA.Mods.Common.Traits
{
pathfinder = world.WorldActor.Trait<IPathFinder>();
domainIndex = world.WorldActor.Trait<DomainIndex>();
resLayer = world.WorldActor.TraitOrDefault<ResourceLayer>();
resourceLayer = world.WorldActor.TraitOrDefault<IResourceLayer>();
claimLayer = world.WorldActor.TraitOrDefault<ResourceClaimLayer>();
// Avoid all AIs scanning for idle harvesters on the same tick, randomize their initial scan delay.
@@ -94,7 +94,7 @@ namespace OpenRA.Mods.Common.Traits
void IBotTick.BotTick(IBot bot)
{
if (resLayer == null || resLayer.IsResourceLayerEmpty)
if (resourceLayer == null || resourceLayer.IsEmpty)
return;
if (--scanForIdleHarvestersTicks > 0)

View File

@@ -87,9 +87,9 @@ namespace OpenRA.Mods.Common.Traits
if (bi.AllowInvalidPlacement)
return true;
var res = world.WorldActor.TraitOrDefault<ResourceLayer>();
var resourceLayer = world.WorldActor.TraitOrDefault<IResourceLayer>();
return bi.Tiles(cell).All(t => world.Map.Contains(t) &&
(bi.AllowPlacementOnResources || res == null || res.GetResourceType(t) == null) &&
(bi.AllowPlacementOnResources || resourceLayer == null || resourceLayer.GetResource(t).Type == null) &&
world.IsCellBuildable(t, ai, bi, toIgnore));
}

View File

@@ -106,7 +106,7 @@ namespace OpenRA.Mods.Common.Traits
public readonly IReadOnlyDictionary<ResourceTypeInfo, int> Contents;
readonly Mobile mobile;
readonly ResourceLayer resLayer;
readonly IResourceLayer resourceLayer;
readonly ResourceClaimLayer claimLayer;
readonly Dictionary<ResourceTypeInfo, int> contents = new Dictionary<ResourceTypeInfo, int>();
int conditionToken = Actor.InvalidConditionToken;
@@ -138,7 +138,7 @@ namespace OpenRA.Mods.Common.Traits
Contents = new ReadOnlyDictionary<ResourceTypeInfo, int>(contents);
mobile = self.Trait<Mobile>();
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
resourceLayer = self.World.WorldActor.Trait<IResourceLayer>();
claimLayer = self.World.WorldActor.Trait<ResourceClaimLayer>();
}
@@ -276,12 +276,12 @@ namespace OpenRA.Mods.Common.Traits
if (cell.Layer != 0)
return false;
var resType = resLayer.GetResourceType(cell);
if (resType == null)
var resourceType = resourceLayer.GetResource(cell).Type;
if (resourceType == null)
return false;
// Can the harvester collect this kind of resource?
return Info.Resources.Contains(resType.Info.Type);
return Info.Resources.Contains(resourceType.Info.Type);
}
IEnumerable<IOrderTargeter> IIssueOrder.Orders

View File

@@ -30,7 +30,7 @@ namespace OpenRA.Mods.Common.Traits
readonly SeedsResourceInfo info;
readonly ResourceType resourceType;
readonly ResourceLayer resLayer;
readonly IResourceLayer resourceLayer;
public SeedsResource(Actor self, SeedsResourceInfo info)
: base(info)
@@ -43,7 +43,7 @@ namespace OpenRA.Mods.Common.Traits
if (resourceType == null)
throw new InvalidOperationException("No such resource type `{0}`".F(info.ResourceType));
resLayer = self.World.WorldActor.Trait<ResourceLayer>();
resourceLayer = self.World.WorldActor.Trait<IResourceLayer>();
}
int ticks;
@@ -64,12 +64,11 @@ namespace OpenRA.Mods.Common.Traits
{
var cell = Util.RandomWalk(self.Location, self.World.SharedRandom)
.Take(info.MaxRange)
.SkipWhile(p => !self.World.Map.Contains(p) ||
(resLayer.GetResourceType(p) == resourceType && resLayer.IsFull(p)))
.SkipWhile(p => resourceLayer.GetResource(p).Type == resourceType && !resourceLayer.CanAddResource(resourceType, p))
.Cast<CPos?>().FirstOrDefault();
if (cell != null && resLayer.CanAddResource(resourceType, cell.Value))
resLayer.AddResource(resourceType, cell.Value);
if (cell != null && resourceLayer.CanAddResource(resourceType, cell.Value))
resourceLayer.AddResource(resourceType, cell.Value);
}
}
}

View File

@@ -35,12 +35,13 @@ namespace OpenRA.Mods.Common.Traits
public event Action<CPos, ResourceType> CellChanged;
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Tiles[cell]; }
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Tiles.Contains(cell) ? Tiles[cell] : default; }
bool IResourceLayer.CanAddResource(ResourceType resourceType, CPos cell, int amount) { return CanAddResource(resourceType, cell, amount); }
int IResourceLayer.AddResource(ResourceType resourceType, CPos cell, int amount) { return AddResource(resourceType, cell, amount); }
int IResourceLayer.RemoveResource(ResourceType resourceType, CPos cell, int amount) { return RemoveResource(resourceType, cell, amount); }
void IResourceLayer.ClearResources(CPos cell) { ClearResources(cell); }
bool IResourceLayer.IsVisible(CPos cell) { return Map.Contains(cell); }
bool IResourceLayer.IsEmpty => false;
public EditorResourceLayer(Actor self)
{

View File

@@ -29,22 +29,7 @@ namespace OpenRA.Mods.Common.Traits
}
}
public interface IResourceLayerInfo : ITraitInfoInterface { }
[RequireExplicitImplementation]
public interface IResourceLayer
{
event Action<CPos, ResourceType> CellChanged;
ResourceLayerContents GetResource(CPos cell);
bool CanAddResource(ResourceType resourceType, CPos cell, int amount = 1);
int AddResource(ResourceType resourceType, CPos cell, int amount = 1);
int RemoveResource(ResourceType resourceType, CPos cell, int amount = 1);
void ClearResources(CPos cell);
bool IsVisible(CPos cell);
}
[Desc("Attach this to the world actor.", "Order of the layers defines the Z sorting.")]
[Desc("Attach this to the world actor.")]
public class ResourceLayerInfo : TraitInfo, IResourceLayerInfo, Requires<ResourceTypeInfo>, Requires<BuildingInfluenceInfo>
{
public override object Create(ActorInitializer init) { return new ResourceLayer(init.Self); }
@@ -57,8 +42,6 @@ namespace OpenRA.Mods.Common.Traits
protected readonly CellLayer<ResourceLayerContents> Content;
public bool IsResourceLayerEmpty => resCells < 1;
int resCells;
public event Action<CPos, ResourceType> CellChanged;
@@ -103,7 +86,7 @@ namespace OpenRA.Mods.Common.Traits
foreach (var cell in w.Map.AllCells)
{
var type = GetResourceType(cell);
var type = Content[cell].Type;
if (type != null)
{
// Set initial density based on the number of neighboring resources
@@ -140,7 +123,7 @@ namespace OpenRA.Mods.Common.Traits
return new ResourceLayerContents(t, 0);
}
public bool CanAddResource(ResourceType resourceType, CPos cell, int amount = 1)
bool CanAddResource(ResourceType resourceType, CPos cell, int amount = 1)
{
if (!world.Map.Contains(cell))
return false;
@@ -155,7 +138,7 @@ namespace OpenRA.Mods.Common.Traits
return content.Density + amount <= resourceType.Info.MaxDensity;
}
public int AddResource(ResourceType resourceType, CPos cell, int amount = 1)
int AddResource(ResourceType resourceType, CPos cell, int amount = 1)
{
if (!Content.Contains(cell))
return 0;
@@ -176,7 +159,7 @@ namespace OpenRA.Mods.Common.Traits
return density - oldDensity;
}
public int RemoveResource(ResourceType resourceType, CPos cell, int amount = 1)
int RemoveResource(ResourceType resourceType, CPos cell, int amount = 1)
{
if (!Content.Contains(cell))
return 0;
@@ -205,7 +188,7 @@ namespace OpenRA.Mods.Common.Traits
return oldDensity - density;
}
public void ClearResources(CPos cell)
void ClearResources(CPos cell)
{
if (!Content.Contains(cell))
return;
@@ -222,22 +205,12 @@ namespace OpenRA.Mods.Common.Traits
CellChanged?.Invoke(cell, null);
}
public bool IsFull(CPos cell)
{
var cellContents = Content[cell];
return cellContents.Density == cellContents.Type.Info.MaxDensity;
}
public ResourceType GetResourceType(CPos cell) { return Content[cell].Type; }
public int GetResourceDensity(CPos cell) { return Content[cell].Density; }
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Content[cell]; }
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Content.Contains(cell) ? Content[cell] : default; }
bool IResourceLayer.CanAddResource(ResourceType resourceType, CPos cell, int amount) { return CanAddResource(resourceType, cell, amount); }
int IResourceLayer.AddResource(ResourceType resourceType, CPos cell, int amount) { return AddResource(resourceType, cell, amount); }
int IResourceLayer.RemoveResource(ResourceType resourceType, CPos cell, int amount) { return RemoveResource(resourceType, cell, amount); }
void IResourceLayer.ClearResources(CPos cell) { ClearResources(cell); }
bool IResourceLayer.IsVisible(CPos cell) { return !world.FogObscures(cell); }
bool IResourceLayer.IsEmpty => resCells < 1;
}
}