revamp ore growth for massive simplification - pure SeedsResource-based

This commit is contained in:
Chris Forbes
2010-05-01 15:27:42 +12:00
parent affd853a96
commit 63b150f3fd
4 changed files with 34 additions and 71 deletions

View File

@@ -20,14 +20,15 @@
using System; using System;
using System.Linq; using System.Linq;
using System.Collections.Generic;
namespace OpenRA.Traits namespace OpenRA.Traits
{ {
class SeedsResourceInfo : ITraitInfo class SeedsResourceInfo : ITraitInfo
{ {
public readonly float Chance = .05f; public readonly int Interval = 75;
public readonly int Interval = 5;
public readonly string ResourceType = "Ore"; public readonly string ResourceType = "Ore";
public readonly int MaxRange = 100;
public object Create(Actor self) { return new SeedsResource(); } public object Create(Actor self) { return new SeedsResource(); }
} }
@@ -50,14 +51,34 @@ namespace OpenRA.Traits
var resLayer = self.World.WorldActor.traits.Get<ResourceLayer>(); var resLayer = self.World.WorldActor.traits.Get<ResourceLayer>();
for (var j = -1; j < 2; j++) var cell = RandomWalk(self.Location, self.World.SharedRandom)
for (var i = -1; i < 2; i++) .Take(info.MaxRange)
if (self.World.SharedRandom.NextDouble() < info.Chance) .SkipWhile(p => resLayer.GetResource(p) == resourceType && resLayer.IsFull(p.X, p.Y))
if (self.World.IsCellBuildable(self.Location + new int2(i, j), false)) .Cast<int2?>().FirstOrDefault();
resLayer.AddResource(resourceType, self.Location.X + i, self.Location.Y + j, 1);
if (cell != null &&
(resLayer.GetResource(cell.Value) == resourceType || resLayer.GetResource(cell.Value) == null) &&
self.World.IsCellBuildable(cell.Value, false))
resLayer.AddResource(resourceType, cell.Value.X, cell.Value.Y, 1);
ticks = info.Interval; ticks = info.Interval;
} }
} }
static IEnumerable<int2> RandomWalk(int2 p, Thirdparty.Random r)
{
for (; ; )
{
var dx = r.Next(-1, 2);
var dy = r.Next(-1, 2);
if (dx == 0 && dy == 0)
continue;
p.X += dx;
p.Y += dy;
yield return p;
}
}
} }
} }

View File

@@ -108,12 +108,12 @@ namespace OpenRA.Traits
return content[p.X,p.Y].type.GetCost(umt); return content[p.X,p.Y].type.GetCost(umt);
} }
public Sprite[] ChooseContent(ResourceType t) Sprite[] ChooseContent(ResourceType t)
{ {
return t.info.Sprites[world.SharedRandom.Next(t.info.Sprites.Length)]; return t.info.Sprites[world.SharedRandom.Next(t.info.Sprites.Length)];
} }
public int GetAdjacentCellsWith(ResourceType t, int i, int j) int GetAdjacentCellsWith(ResourceType t, int i, int j)
{ {
int sum = 0; int sum = 0;
for (var u = -1; u < 2; u++) for (var u = -1; u < 2; u++)
@@ -123,7 +123,7 @@ namespace OpenRA.Traits
return sum; return sum;
} }
public int GetIdealDensity(int x, int y) int GetIdealDensity(int x, int y)
{ {
return (GetAdjacentCellsWith(content[x, y].type, x, y) * return (GetAdjacentCellsWith(content[x, y].type, x, y) *
(content[x, y].image.Length - 1)) / 9; (content[x, y].image.Length - 1)) / 9;
@@ -146,6 +146,8 @@ namespace OpenRA.Traits
content[i, j].density + n); content[i, j].density + n);
} }
public bool IsFull(int i, int j) { return content[i, j].density == content[i, j].image.Length - 1; }
public ResourceType Harvest(int2 p) public ResourceType Harvest(int2 p)
{ {
var type = content[p.X,p.Y].type; var type = content[p.X,p.Y].type;
@@ -166,43 +168,6 @@ namespace OpenRA.Traits
content[p.X, p.Y].density = 0; content[p.X, p.Y].density = 0;
} }
public void Grow(ResourceType t)
{
var map = world.Map;
var newDensity = new byte[map.MapSize.X, map.MapSize.Y];
for (int i = map.TopLeft.X; i < map.BottomRight.X; i++)
for (int j = map.TopLeft.Y; j < map.BottomRight.Y; j++)
if (content[i, j].type == t)
newDensity[i, j] = (byte)GetIdealDensity(i, j);
for (int i = map.TopLeft.X; i < map.BottomRight.X; i++)
for (int j = map.TopLeft.Y; j < map.BottomRight.Y; j++)
if (content[i, j].type == t && content[i, j].density < newDensity[i, j])
++content[i, j].density;
}
public void Spread(ResourceType t)
{
var map = world.Map;
var growMask = new bool[map.MapSize.X, map.MapSize.Y];
for (int i = map.TopLeft.X; i < map.BottomRight.X; i++)
for (int j = map.TopLeft.Y; j < map.BottomRight.Y; j++)
if (content[i,j].type == null
&& GetAdjacentCellsWith(t, i,j ) > 0
&& world.IsCellBuildable(new int2(i, j), false))
growMask[i, j] = true;
for (int i = map.TopLeft.X; i < map.BottomRight.X; i++)
for (int j = map.TopLeft.Y; j < map.BottomRight.Y; j++)
if (growMask[i, j])
{
content[i, j].type = t;
content[i, j].image = ChooseContent(t);
content[i, j].density = 0;
}
}
public ResourceType GetResource(int2 p) { return content[p.X, p.Y].type; } public ResourceType GetResource(int2 p) { return content[p.X, p.Y].type; }
public struct CellContents public struct CellContents

View File

@@ -33,8 +33,6 @@ namespace OpenRA.Traits
public readonly int ValuePerUnit = 0; public readonly int ValuePerUnit = 0;
public readonly string Name = null; public readonly string Name = null;
public readonly float GrowthInterval = 0;
public readonly float SpreadInterval = 0;
public readonly string MovementTerrainType = null; public readonly string MovementTerrainType = null;
public readonly string PathingTerrainType = null; public readonly string PathingTerrainType = null;
@@ -43,10 +41,8 @@ namespace OpenRA.Traits
public object Create(Actor self) { return new ResourceType(this); } public object Create(Actor self) { return new ResourceType(this); }
} }
public class ResourceType : ITick public class ResourceType
{ {
int growthTicks;
int spreadTicks;
public ResourceTypeInfo info; public ResourceTypeInfo info;
float[] movementSpeed = new float[4]; float[] movementSpeed = new float[4];
float[] pathCost = new float[4]; float[] pathCost = new float[4];
@@ -73,22 +69,5 @@ namespace OpenRA.Traits
{ {
return pathCost[(int)umt]; return pathCost[(int)umt];
} }
public void Tick(Actor self)
{
if (info.GrowthInterval != 0 && --growthTicks <= 0)
{
growthTicks = (int)(info.GrowthInterval * 25 * 60);
self.World.WorldActor.traits.Get<ResourceLayer>().Grow(this);
self.World.Minimap.InvalidateOre();
}
if (info.SpreadInterval != 0 && --spreadTicks <= 0)
{
spreadTicks = (int)(info.SpreadInterval * 25 * 60);
self.World.WorldActor.traits.Get<ResourceLayer>().Spread(this);
self.World.Minimap.InvalidateOre();
}
}
} }
} }

View File

@@ -236,8 +236,6 @@ World:
SpriteNames: gold01,gold02,gold03,gold04 SpriteNames: gold01,gold02,gold03,gold04
ValuePerUnit: 25 ValuePerUnit: 25
Name: Ore Name: Ore
GrowthInterval: 1.2
SpreadInterval: 2.0
ResourceType@gem: ResourceType@gem:
ResourceType: 2 ResourceType: 2
MovementTerrainType: Ore MovementTerrainType: Ore