Add resource updating methods to IResourceLayer.
This commit is contained in:
@@ -81,14 +81,14 @@ namespace OpenRA.Mods.Common.Activities
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var resource = resLayer.Harvest(self.Location);
|
var resourceType = resLayer.GetResourceType(self.Location);
|
||||||
if (resource == null)
|
if (resourceType == null || resLayer.RemoveResource(resourceType, self.Location) != 1)
|
||||||
return true;
|
return true;
|
||||||
|
|
||||||
harv.AcceptResource(self, resource);
|
harv.AcceptResource(self, resourceType);
|
||||||
|
|
||||||
foreach (var t in notifyHarvesterActions)
|
foreach (var t in notifyHarvesterActions)
|
||||||
t.Harvested(self, resource);
|
t.Harvested(self, resourceType);
|
||||||
|
|
||||||
QueueChild(new Wait(harvInfo.BaleLoadDelay));
|
QueueChild(new Wait(harvInfo.BaleLoadDelay));
|
||||||
return false;
|
return false;
|
||||||
|
|||||||
@@ -68,8 +68,8 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
(resLayer.GetResourceType(p) == resourceType && resLayer.IsFull(p)))
|
(resLayer.GetResourceType(p) == resourceType && resLayer.IsFull(p)))
|
||||||
.Cast<CPos?>().FirstOrDefault();
|
.Cast<CPos?>().FirstOrDefault();
|
||||||
|
|
||||||
if (cell != null && resLayer.CanSpawnResourceAt(resourceType, cell.Value))
|
if (cell != null && resLayer.CanAddResource(resourceType, cell.Value))
|
||||||
resLayer.AddResource(resourceType, cell.Value, 1);
|
resLayer.AddResource(resourceType, cell.Value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,6 +36,10 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
public event Action<CPos, ResourceType> CellChanged;
|
public event Action<CPos, ResourceType> CellChanged;
|
||||||
|
|
||||||
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Tiles[cell]; }
|
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Tiles[cell]; }
|
||||||
|
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.IsVisible(CPos cell) { return Map.Contains(cell); }
|
||||||
|
|
||||||
public EditorResourceLayer(Actor self)
|
public EditorResourceLayer(Actor self)
|
||||||
@@ -73,12 +77,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
var newTerrain = byte.MaxValue;
|
var newTerrain = byte.MaxValue;
|
||||||
if (Resources.TryGetValue(tile.Type, out var type))
|
if (Resources.TryGetValue(tile.Type, out var type))
|
||||||
{
|
{
|
||||||
newTile = new ResourceLayerContents
|
newTile = new ResourceLayerContents(type, CalculateCellDensity(type, cell));
|
||||||
{
|
|
||||||
Type = type,
|
|
||||||
Density = CalculateCellDensity(type, cell)
|
|
||||||
};
|
|
||||||
|
|
||||||
newTerrain = Map.Rules.TerrainInfo.GetTerrainIndex(type.Info.TerrainType);
|
newTerrain = Map.Rules.TerrainInfo.GetTerrainIndex(type.Info.TerrainType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -104,8 +103,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
continue;
|
continue;
|
||||||
|
|
||||||
UpdateNetWorth(neighbouringTile.Type, neighbouringTile.Density, neighbouringTile.Type, density);
|
UpdateNetWorth(neighbouringTile.Type, neighbouringTile.Density, neighbouringTile.Type, density);
|
||||||
neighbouringTile.Density = density;
|
Tiles[neighbouringCell] = new ResourceLayerContents(neighbouringTile.Type, density);
|
||||||
Tiles[neighbouringCell] = neighbouringTile;
|
|
||||||
|
|
||||||
CellChanged?.Invoke(neighbouringCell, type);
|
CellChanged?.Invoke(neighbouringCell, type);
|
||||||
}
|
}
|
||||||
@@ -142,6 +140,74 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
return Math.Max(int2.Lerp(0, type.Info.MaxDensity, adjacent, 9), 1);
|
return Math.Max(int2.Lerp(0, type.Info.MaxDensity, adjacent, 9), 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool AllowResourceAt(ResourceType rt, CPos cell)
|
||||||
|
{
|
||||||
|
var mapResources = Map.Resources;
|
||||||
|
if (!mapResources.Contains(cell))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
if (!rt.Info.AllowedTerrainTypes.Contains(Map.GetTerrainInfo(cell).Type))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
// TODO: Check against actors in the EditorActorLayer
|
||||||
|
return rt.Info.AllowOnRamps || Map.Ramp[cell] == 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool CanAddResource(ResourceType resourceType, CPos cell, int amount = 1)
|
||||||
|
{
|
||||||
|
var resources = Map.Resources;
|
||||||
|
if (!resources.Contains(cell))
|
||||||
|
return false;
|
||||||
|
|
||||||
|
var content = resources[cell];
|
||||||
|
if (content.Type == 0)
|
||||||
|
return amount <= resourceType.Info.MaxDensity && AllowResourceAt(resourceType, cell);
|
||||||
|
|
||||||
|
if (content.Type != resourceType.Info.ResourceType)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return content.Index + amount <= resourceType.Info.MaxDensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
int AddResource(ResourceType resourceType, CPos cell, int amount = 1)
|
||||||
|
{
|
||||||
|
var resources = Map.Resources;
|
||||||
|
if (!resources.Contains(cell))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var content = resources[cell];
|
||||||
|
if (content.Type != 0 && content.Type != resourceType.Info.ResourceType)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var oldDensity = content.Index;
|
||||||
|
var density = (byte)Math.Min(resourceType.Info.MaxDensity, oldDensity + amount);
|
||||||
|
Map.Resources[cell] = new ResourceTile((byte)resourceType.Info.ResourceType, density);
|
||||||
|
|
||||||
|
return density - oldDensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
int RemoveResource(ResourceType resourceType, CPos cell, int amount = 1)
|
||||||
|
{
|
||||||
|
var resources = Map.Resources;
|
||||||
|
if (!resources.Contains(cell))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var content = resources[cell];
|
||||||
|
if (content.Type == 0 || content.Type != resourceType.Info.ResourceType)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var oldDensity = content.Index;
|
||||||
|
var density = (byte)Math.Max(0, oldDensity - amount);
|
||||||
|
resources[cell] = density > 0 ? new ResourceTile((byte)resourceType.Info.ResourceType, density) : default;
|
||||||
|
|
||||||
|
return oldDensity - density;
|
||||||
|
}
|
||||||
|
|
||||||
|
void ClearResources(CPos cell)
|
||||||
|
{
|
||||||
|
Map.Resources[cell] = default;
|
||||||
|
}
|
||||||
|
|
||||||
void INotifyActorDisposing.Disposing(Actor self)
|
void INotifyActorDisposing.Disposing(Actor self)
|
||||||
{
|
{
|
||||||
if (disposed)
|
if (disposed)
|
||||||
|
|||||||
@@ -18,9 +18,15 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
{
|
{
|
||||||
public struct ResourceLayerContents
|
public struct ResourceLayerContents
|
||||||
{
|
{
|
||||||
public static readonly ResourceLayerContents Empty = default(ResourceLayerContents);
|
public static readonly ResourceLayerContents Empty = default;
|
||||||
public ResourceType Type;
|
public readonly ResourceType Type;
|
||||||
public int Density;
|
public readonly int Density;
|
||||||
|
|
||||||
|
public ResourceLayerContents(ResourceType type, int density)
|
||||||
|
{
|
||||||
|
Type = type;
|
||||||
|
Density = density;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface IResourceLayerInfo : ITraitInfoInterface { }
|
public interface IResourceLayerInfo : ITraitInfoInterface { }
|
||||||
@@ -30,6 +36,10 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
{
|
{
|
||||||
event Action<CPos, ResourceType> CellChanged;
|
event Action<CPos, ResourceType> CellChanged;
|
||||||
ResourceLayerContents GetResource(CPos cell);
|
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);
|
bool IsVisible(CPos cell);
|
||||||
}
|
}
|
||||||
@@ -100,10 +110,7 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
// Adjacent includes the current cell, so is always >= 1
|
// Adjacent includes the current cell, so is always >= 1
|
||||||
var adjacent = GetAdjacentCellsWith(type, cell);
|
var adjacent = GetAdjacentCellsWith(type, cell);
|
||||||
var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9);
|
var density = int2.Lerp(0, type.Info.MaxDensity, adjacent, 9);
|
||||||
var temp = Content[cell];
|
Content[cell] = new ResourceLayerContents(Content[cell].Type, Math.Max(density, 1));
|
||||||
temp.Density = Math.Max(density, 1);
|
|
||||||
|
|
||||||
Content[cell] = temp;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -125,40 +132,94 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
return rt.Info.AllowOnRamps || world.Map.Ramp[cell] == 0;
|
return rt.Info.AllowOnRamps || world.Map.Ramp[cell] == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool CanSpawnResourceAt(ResourceType newResourceType, CPos cell)
|
|
||||||
{
|
|
||||||
if (!world.Map.Contains(cell))
|
|
||||||
return false;
|
|
||||||
|
|
||||||
var currentResourceType = GetResourceType(cell);
|
|
||||||
return (currentResourceType == newResourceType && !IsFull(cell))
|
|
||||||
|| (currentResourceType == null && AllowResourceAt(newResourceType, cell));
|
|
||||||
}
|
|
||||||
|
|
||||||
ResourceLayerContents CreateResourceCell(ResourceType t, CPos cell)
|
ResourceLayerContents CreateResourceCell(ResourceType t, CPos cell)
|
||||||
{
|
{
|
||||||
world.Map.CustomTerrain[cell] = world.Map.Rules.TerrainInfo.GetTerrainIndex(t.Info.TerrainType);
|
world.Map.CustomTerrain[cell] = world.Map.Rules.TerrainInfo.GetTerrainIndex(t.Info.TerrainType);
|
||||||
++resCells;
|
++resCells;
|
||||||
|
|
||||||
return new ResourceLayerContents
|
return new ResourceLayerContents(t, 0);
|
||||||
{
|
|
||||||
Type = t
|
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddResource(ResourceType t, CPos p, int n)
|
public bool CanAddResource(ResourceType resourceType, CPos cell, int amount = 1)
|
||||||
{
|
{
|
||||||
var cell = Content[p];
|
if (!world.Map.Contains(cell))
|
||||||
if (cell.Type == null)
|
return false;
|
||||||
cell = CreateResourceCell(t, p);
|
|
||||||
|
|
||||||
if (cell.Type != t)
|
var content = Content[cell];
|
||||||
|
if (content.Type == null)
|
||||||
|
return amount <= resourceType.Info.MaxDensity && AllowResourceAt(resourceType, cell);
|
||||||
|
|
||||||
|
if (content.Type != resourceType)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
return content.Density + amount <= resourceType.Info.MaxDensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int AddResource(ResourceType resourceType, CPos cell, int amount = 1)
|
||||||
|
{
|
||||||
|
if (!Content.Contains(cell))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var content = Content[cell];
|
||||||
|
if (content.Type == null)
|
||||||
|
content = CreateResourceCell(resourceType, cell);
|
||||||
|
|
||||||
|
if (content.Type != resourceType)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var oldDensity = content.Density;
|
||||||
|
var density = Math.Min(content.Type.Info.MaxDensity, oldDensity + amount);
|
||||||
|
Content[cell] = new ResourceLayerContents(content.Type, density);
|
||||||
|
|
||||||
|
CellChanged?.Invoke(cell, content.Type);
|
||||||
|
|
||||||
|
return density - oldDensity;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int RemoveResource(ResourceType resourceType, CPos cell, int amount = 1)
|
||||||
|
{
|
||||||
|
if (!Content.Contains(cell))
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var content = Content[cell];
|
||||||
|
if (content.Type == null || content.Type != resourceType)
|
||||||
|
return 0;
|
||||||
|
|
||||||
|
var oldDensity = content.Density;
|
||||||
|
var density = Math.Max(0, oldDensity - amount);
|
||||||
|
|
||||||
|
if (density == 0)
|
||||||
|
{
|
||||||
|
Content[cell] = ResourceLayerContents.Empty;
|
||||||
|
world.Map.CustomTerrain[cell] = byte.MaxValue;
|
||||||
|
--resCells;
|
||||||
|
|
||||||
|
CellChanged?.Invoke(cell, null);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
Content[cell] = new ResourceLayerContents(content.Type, density);
|
||||||
|
CellChanged?.Invoke(cell, content.Type);
|
||||||
|
}
|
||||||
|
|
||||||
|
return oldDensity - density;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void ClearResources(CPos cell)
|
||||||
|
{
|
||||||
|
if (!Content.Contains(cell))
|
||||||
return;
|
return;
|
||||||
|
|
||||||
cell.Density = Math.Min(cell.Type.Info.MaxDensity, cell.Density + n);
|
// Don't break other users of CustomTerrain if there are no resources
|
||||||
Content[p] = cell;
|
var content = Content[cell];
|
||||||
|
if (content.Type == null)
|
||||||
|
return;
|
||||||
|
|
||||||
CellChanged?.Invoke(p, cell.Type);
|
Content[cell] = ResourceLayerContents.Empty;
|
||||||
|
world.Map.CustomTerrain[cell] = byte.MaxValue;
|
||||||
|
--resCells;
|
||||||
|
|
||||||
|
CellChanged?.Invoke(cell, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool IsFull(CPos cell)
|
public bool IsFull(CPos cell)
|
||||||
@@ -167,47 +228,16 @@ namespace OpenRA.Mods.Common.Traits
|
|||||||
return cellContents.Density == cellContents.Type.Info.MaxDensity;
|
return cellContents.Density == cellContents.Type.Info.MaxDensity;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ResourceType Harvest(CPos cell)
|
|
||||||
{
|
|
||||||
var c = Content[cell];
|
|
||||||
if (c.Type == null)
|
|
||||||
return null;
|
|
||||||
|
|
||||||
if (--c.Density < 0)
|
|
||||||
{
|
|
||||||
Content[cell] = ResourceLayerContents.Empty;
|
|
||||||
world.Map.CustomTerrain[cell] = byte.MaxValue;
|
|
||||||
--resCells;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
Content[cell] = c;
|
|
||||||
|
|
||||||
CellChanged?.Invoke(cell, c.Type);
|
|
||||||
|
|
||||||
return c.Type;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void Destroy(CPos cell)
|
|
||||||
{
|
|
||||||
// Don't break other users of CustomTerrain if there are no resources
|
|
||||||
var c = Content[cell];
|
|
||||||
if (c.Type == null)
|
|
||||||
return;
|
|
||||||
|
|
||||||
--resCells;
|
|
||||||
|
|
||||||
// Clear cell
|
|
||||||
Content[cell] = ResourceLayerContents.Empty;
|
|
||||||
world.Map.CustomTerrain[cell] = byte.MaxValue;
|
|
||||||
|
|
||||||
CellChanged?.Invoke(cell, c.Type);
|
|
||||||
}
|
|
||||||
|
|
||||||
public ResourceType GetResourceType(CPos cell) { return Content[cell].Type; }
|
public ResourceType GetResourceType(CPos cell) { return Content[cell].Type; }
|
||||||
|
|
||||||
public int GetResourceDensity(CPos cell) { return Content[cell].Density; }
|
public int GetResourceDensity(CPos cell) { return Content[cell].Density; }
|
||||||
|
|
||||||
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Content[cell]; }
|
ResourceLayerContents IResourceLayer.GetResource(CPos cell) { return Content[cell]; }
|
||||||
|
|
||||||
|
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.IsVisible(CPos cell) { return !world.FogObscures(cell); }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -59,7 +59,7 @@ namespace OpenRA.Mods.Common.Warheads
|
|||||||
var resLayer = world.WorldActor.Trait<ResourceLayer>();
|
var resLayer = world.WorldActor.Trait<ResourceLayer>();
|
||||||
foreach (var cell in allCells)
|
foreach (var cell in allCells)
|
||||||
{
|
{
|
||||||
if (!resLayer.CanSpawnResourceAt(resourceType, cell))
|
if (!resLayer.CanAddResource(resourceType, cell))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
var splash = world.SharedRandom.Next(1, resourceType.Info.MaxDensity - resLayer.GetResourceDensity(cell));
|
var splash = world.SharedRandom.Next(1, resourceType.Info.MaxDensity - resLayer.GetResourceDensity(cell));
|
||||||
|
|||||||
@@ -41,7 +41,7 @@ namespace OpenRA.Mods.Common.Warheads
|
|||||||
|
|
||||||
// Destroy all resources in the selected tiles
|
// Destroy all resources in the selected tiles
|
||||||
foreach (var cell in allCells)
|
foreach (var cell in allCells)
|
||||||
resLayer.Destroy(cell);
|
resLayer.ClearResources(cell);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user