ore is now in openra.game

This commit is contained in:
Chris Forbes
2009-11-04 22:51:17 +13:00
parent 7acd327896
commit 66fd532db8
10 changed files with 304 additions and 150 deletions

View File

@@ -23,33 +23,6 @@ namespace OpenRa.FileFormats
public readonly TileReference[ , ] MapTiles = new TileReference[ 128, 128 ];
public readonly List<TreeReference> Trees = new List<TreeReference>();
public static bool[] overlayIsFence =
{
true, true, true, true, true,
false, false, false, false,
false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, true, true,
};
public static bool[] overlayIsOre =
{
false, false, false, false, false,
true, true, true, true,
false, false, false, false,
false, false, false, false, false, false, false,
false, false, false, false, false,
};
public static bool[] overlayIsGems =
{
false, false, false, false, false,
false, false, false, false,
true, true, true, true,
false, false, false, false, false, false, false,
false, false, false, false, false,
};
static string Truncate( string s, int maxLength )
{
return s.Length <= maxLength ? s : s.Substring(0,maxLength );
@@ -78,118 +51,6 @@ namespace OpenRa.FileFormats
UnpackTileData(ReadPackedSection(file.GetSection("MapPack")));
UnpackOverlayData(ReadPackedSection(file.GetSection("OverlayPack")));
ReadTrees(file);
InitOreDensity();
}
IEnumerable<int2> AdjacentTiles(int2 p)
{
for (var u = -1; u < 2; u++)
for (var v = -1; v < 2; v++)
yield return new int2(u, v) + p;
}
byte GetOreDensity(int i, int j)
{
// perf fix. it's ugly, i know :(
int sum = 0;
for( var u = -1 ; u < 2 ; u++ )
for( var v = -1 ; v < 2 ; v++ )
if( ContainsOre( i + u, j + v ) )
++sum;
sum = sum * 3 / 2;
if( sum > 11 )
return 11;
return (byte)sum;
}
byte GetGemDensity(int i, int j)
{
return (byte)Math.Min(2, (AdjacentTiles(new int2(i, j)).Sum(
p => ContainsGem(p.X, p.Y) ? 1 : 0) / 3));
}
void InitOreDensity()
{
for( int j = 0; j < 128; j++ )
for (int i = 0; i < 128; i++)
{
if (ContainsOre(i,j)) MapTiles[i, j].density = GetOreDensity(i, j);
if (ContainsGem(i,j)) MapTiles[i,j].density = GetGemDensity(i,j);
}
}
bool HasOverlay(int i, int j)
{
return MapTiles[i, j].overlay < overlayIsOre.Length;
}
bool ContainsOre(int i, int j)
{
return HasOverlay(i,j) && overlayIsOre[MapTiles[i,j].overlay];
}
bool ContainsGem(int i, int j)
{
return HasOverlay(i, j) && overlayIsGems[MapTiles[i, j].overlay];
}
public bool ContainsResource(int2 p) { return ContainsGem(p.X,p.Y) || ContainsOre(p.X, p.Y); }
const float oreRate = .02f;
const float gemRate = .01f;
public bool Harvest(int2 p, out bool isGems) /* harvests one unit if possible */
{
isGems = ContainsGem(p.X, p.Y);
if (MapTiles[p.X, p.Y].density == 0) return false;
if (--MapTiles[p.X, p.Y].density == 0)
MapTiles[p.X, p.Y].overlay = 0xff;
return true;
}
byte ore = 5;
byte ChooseOre()
{
if (++ore > 8) ore = 5;
return ore;
}
public void GrowOre( Func<int2, bool> canSpreadIntoCell, Random r ) /* todo: deal with ore pits */
{
/* phase 1: grow into neighboring regions */
var newOverlay = new byte[128, 128];
var mini = this.XOffset;
var minj = this.YOffset;
var maxi = this.XOffset + Width;
var maxj = this.YOffset + Height;
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
{
newOverlay[i,j] = 0xff;
if (!HasOverlay(i, j) && r.NextDouble() < oreRate && GetOreDensity(i, j) > 0 && canSpreadIntoCell(new int2(i, j))
)
newOverlay[i, j] = ChooseOre();
}
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
if (newOverlay[i, j] != 0xff)
MapTiles[i, j].overlay = newOverlay[i, j];
/* phase 2: increase density of existing areas */
var newDensity = new byte[128, 128];
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
if (ContainsOre(i, j)) newDensity[i,j] = GetOreDensity(i, j);
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
if (MapTiles[i, j].density < newDensity[i, j])
++MapTiles[i, j].density;
}
static MemoryStream ReadPackedSection(IniSection mapPackSection)