From f133410977286178e002c90b9e314951fe0ab32d Mon Sep 17 00:00:00 2001 From: Chris Forbes Date: Wed, 3 Mar 2010 20:54:06 +1300 Subject: [PATCH] ore growth moved into ResourceLayer --- OpenRA.Game/Ore.cs | 18 ------------ OpenRA.Game/Traits/World/OreGrowth.cs | 23 ++++++++++----- OpenRA.Game/Traits/World/ResourceLayer.cs | 34 +++++++++++++++++++++-- 3 files changed, 48 insertions(+), 27 deletions(-) diff --git a/OpenRA.Game/Ore.cs b/OpenRA.Game/Ore.cs index 97d119e1ae..01ee46eb3b 100644 --- a/OpenRA.Game/Ore.cs +++ b/OpenRA.Game/Ore.cs @@ -61,24 +61,6 @@ namespace OpenRA map.MapTiles[i, j].overlay = newOverlay[i, j]; } - public static void GrowOre(this World world) - { - 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 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 (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; - } - static byte GetOreDensity(this Map map, int i, int j) { int sum = 0; diff --git a/OpenRA.Game/Traits/World/OreGrowth.cs b/OpenRA.Game/Traits/World/OreGrowth.cs index 66889d51ba..9b30291083 100644 --- a/OpenRA.Game/Traits/World/OreGrowth.cs +++ b/OpenRA.Game/Traits/World/OreGrowth.cs @@ -18,6 +18,8 @@ */ #endregion +using System.Linq; + namespace OpenRA.Traits { class OreGrowthInfo : ITraitInfo @@ -39,14 +41,21 @@ namespace OpenRA.Traits if (--remainingTicks <= 0) { var info = self.Info.Traits.Get(); - - if (info.Spreads) - Ore.SpreadOre(self.World, - self.World.SharedRandom, - info.Chance); - if (info.Grows) - Ore.GrowOre(self.World); + // HACK HACK: we should push "grows" down to the resource. + var oreResource = self.World.WorldActor.Info.Traits.WithInterface() + .FirstOrDefault(r => r.Name == "Ore"); + + if (oreResource != null) + { + if (info.Spreads) + Ore.SpreadOre(self.World, + self.World.SharedRandom, + info.Chance); + + if (info.Grows) + self.World.WorldActor.traits.Get().Grow(oreResource); + } self.World.Minimap.InvalidateOre(); remainingTicks = (int)(info.Interval * 60 * 25); diff --git a/OpenRA.Game/Traits/World/ResourceLayer.cs b/OpenRA.Game/Traits/World/ResourceLayer.cs index 328b62ce63..6049fbb46b 100644 --- a/OpenRA.Game/Traits/World/ResourceLayer.cs +++ b/OpenRA.Game/Traits/World/ResourceLayer.cs @@ -83,8 +83,7 @@ namespace OpenRA.Traits for (int y = map.YOffset; y < map.YOffset + map.Height; y++) for (int x = map.XOffset; x < map.XOffset + map.Width; x++) if (content[x, y].type != null) - content[x, y].density = (GetAdjacentCellsWith(content[x, y].type, x, y) * - content[x, y].image.Length) / 9; + content[x, y].density = GetIdealDensity(x, y); } public Sprite[] ChooseContent(ResourceTypeInfo info) @@ -102,6 +101,12 @@ namespace OpenRA.Traits return sum; } + public int GetIdealDensity(int x, int y) + { + return (GetAdjacentCellsWith(content[x, y].type, x, y) * + content[x, y].image.Length) / 9; + } + public void AddResource(ResourceTypeInfo info, int i, int j, int n) { if (content[i, j].type == null) @@ -129,6 +134,31 @@ namespace OpenRA.Traits return type; } + public void Destroy(int2 p) + { + content[p.X, p.Y].type = null; + content[p.X, p.Y].image = null; + content[p.X, p.Y].density = 0; + } + + public void Grow(ResourceTypeInfo info) + { + var map = w.Map; + var mini = map.XOffset; var maxi = map.XOffset + map.Width; + var minj = map.YOffset; var maxj = map.YOffset + map.Height; + + var newDensity = new byte[128, 128]; + for (int j = minj; j < maxj; j++) + for (int i = mini; i < maxi; i++) + if (content[i, j].type == info) + newDensity[i, j] = (byte)GetIdealDensity(i, j); + + for (int j = minj; j < maxj; j++) + for (int i = mini; i < maxi; i++) + if (content[i, j].type == info && content[i, j].density < newDensity[i, j]) + ++content[i, j].density; + } + public struct CellContents { public ResourceTypeInfo type;