ore growth -> trait on world

This commit is contained in:
Chris Forbes
2010-02-07 18:09:41 +13:00
parent 1d964d9301
commit 853f9ae6d5
12 changed files with 101 additions and 78 deletions

View File

@@ -356,10 +356,8 @@ namespace OpenRa
while (!PaletteAvailable(newIndex) && newIndex != (int)Game.world.LocalPlayer.PaletteIndex)
newIndex = (newIndex + d) % Player.PlayerColors.Count();
Game.world.Minimap.InvalidateSpawnPoints();
Game.IssueOrder(
Order.Chat("/pal " + newIndex));
}
void CycleRace(bool left)

View File

@@ -46,9 +46,6 @@ namespace OpenRa.GameRules
public readonly float BuildupTime = 0;
public readonly int GemValue = 0;
public readonly int GoldValue = 0;
public readonly float GrowthRate = 0;
public readonly bool OreGrows = true;
public readonly bool OreSpreads = true;
public readonly float OreTruckRate = 0;
public readonly bool SeparateAircraft = true;
public readonly float SurvivorRate = 0;
@@ -90,7 +87,6 @@ namespace OpenRa.GameRules
public readonly bool FineDiffControl = false;
/* OpenRA-specific */
public readonly float OreChance = 0; /* chance of spreading to a particular eligible cell */
public readonly int LowPowerSlowdown = 3; /* build time multiplier */
}
}

View File

@@ -17,7 +17,7 @@ namespace OpenRa.Graphics
SpriteRenderer rgbaRenderer;
LineRenderer lineRenderer;
Sprite sprite, mapOnlySprite, mapSpawnPointSprite;
Bitmap terrain, oreLayer, spawnPointsLayer;
Bitmap terrain, oreLayer;
Rectangle bounds;
Sprite ownedSpawnPoint;
@@ -74,7 +74,6 @@ namespace OpenRa.Graphics
static Color shroudColor;
public void InvalidateOre() { oreLayer = null; }
public void InvalidateSpawnPoints() { spawnPointsLayer = null; }
public static Bitmap RenderTerrainBitmap(Map map, TileSet tileset)
{

View File

@@ -118,6 +118,7 @@
<Compile Include="Support\PerfHistory.cs" />
<Compile Include="Sync.cs" />
<Compile Include="Traits\CrateSpawner.cs" />
<Compile Include="Traits\OreGrowth.cs" />
<Compile Include="Traits\OreRefinery.cs" />
<Compile Include="Traits\Activities\Attack.cs" />
<Compile Include="Traits\Activities\CallFunc.cs" />

View File

@@ -8,14 +8,13 @@ namespace OpenRa
{
public static void AddOre(this Map map, int i, int j)
{
if (Rules.General.OreSpreads)
if (map.ContainsOre(i, j) && map.MapTiles[i, j].density < 12)
map.MapTiles[i, j].density++;
else if (map.MapTiles[i, j].overlay == 0xff)
{
map.MapTiles[i, j].overlay = ChooseOre();
map.MapTiles[i, j].density = 1;
}
if (map.ContainsOre(i, j) && map.MapTiles[i, j].density < 12)
map.MapTiles[i, j].density++;
else if (map.MapTiles[i, j].overlay == 0xff)
{
map.MapTiles[i, j].overlay = ChooseOre();
map.MapTiles[i, j].density = 1;
}
}
public static void DestroyOre(this Map map, int i, int j)
@@ -37,48 +36,49 @@ namespace OpenRa
< double.PositiveInfinity;
}
public static void SpreadOre(this World world, Random r, float chance)
{
var map = world.Map;
var mini = map.XOffset; var maxi = map.XOffset + map.Width;
var minj = map.YOffset; var maxj = map.YOffset + map.Height;
/* phase 1: grow into neighboring regions */
var newOverlay = new byte[128, 128];
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
{
newOverlay[i, j] = 0xff;
if (!map.HasOverlay(i, j)
&& r.NextDouble() < chance
&& map.GetOreDensity(i, j) > 0
&& world.OreCanSpreadInto(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)
map.MapTiles[i, j].overlay = newOverlay[i, j];
}
public static void GrowOre(this World world, Random r)
{
var map = world.Map;
var mini = map.XOffset; var maxi = map.XOffset + map.Width;
var minj = map.YOffset; var maxj = map.YOffset + map.Height;
var chance = Rules.General.OreChance;
/* phase 1: grow into neighboring regions */
if (Rules.General.OreSpreads)
{
var newOverlay = new byte[128, 128];
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
{
newOverlay[i, j] = 0xff;
if (!map.HasOverlay(i, j)
&& r.NextDouble() < chance
&& map.GetOreDensity(i, j) > 0
&& world.OreCanSpreadInto(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)
map.MapTiles[i, j].overlay = newOverlay[i, j];
}
/* phase 2: increase density of existing areas */
if (Rules.General.OreGrows)
{
var newDensity = new byte[128, 128];
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
if (map.ContainsOre(i, j)) newDensity[i, j] = map.GetOreDensity(i, j);
var newDensity = new byte[128, 128];
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
if (map.ContainsOre(i, j)) newDensity[i, j] = map.GetOreDensity(i, j);
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
if (map.MapTiles[i, j].density < newDensity[i, j])
++map.MapTiles[i, j].density;
}
for (int j = minj; j < maxj; j++)
for (int i = mini; i < maxi; i++)
if (map.MapTiles[i, j].density < newDensity[i, j])
++map.MapTiles[i, j].density;
}
public static void InitOreDensity( this Map map )

View File

@@ -0,0 +1,42 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using OpenRa.Graphics;
namespace OpenRa.Traits
{
class OreGrowthInfo : ITraitInfo
{
public readonly float Interval = 1f;
public readonly float Chance = .02f;
public readonly bool Spreads = true;
public readonly bool Grows = true;
public object Create(Actor self) { return new OreGrowth(); }
}
class OreGrowth : ITick
{
int remainingTicks;
public void Tick(Actor self)
{
if (--remainingTicks <= 0)
{
var info = self.Info.Traits.Get<OreGrowthInfo>();
if (info.Spreads)
Ore.SpreadOre(self.World,
Game.SharedRandom,
info.Chance);
if (info.Grows)
Ore.GrowOre(self.World, Game.SharedRandom);
self.World.Minimap.InvalidateOre();
remainingTicks = (int)(info.Interval * 60 * 25);
}
}
}
}

View File

@@ -50,9 +50,6 @@ namespace OpenRa
public readonly WorldRenderer WorldRenderer;
internal readonly Minimap Minimap;
readonly int oreFrequency;
int oreTicks;
public World()
{
Timer.Time( "----World.ctor" );
@@ -66,9 +63,6 @@ namespace OpenRa
WorldRenderer = new WorldRenderer(this, Game.renderer);
Timer.Time("renderer: {0}");
oreFrequency = (int)(Rules.General.GrowthRate * 60 * 25);
oreTicks = oreFrequency;
Map.InitOreDensity();
Timer.Time( "Ore: {0}" );
@@ -124,14 +118,6 @@ namespace OpenRa
public void Tick()
{
if (--oreTicks == 0)
using( new PerfSample( "ore" ) )
{
this.GrowOre( Game.SharedRandom );
Minimap.InvalidateOre();
oreTicks = oreFrequency;
}
foreach (var a in actors) a.Tick();
Queries.WithTraitMultiple<ITick>().Do( x => x.Trait.Tick( x.Actor ) );