# HG changeset patch # User Alys Brooks # Date 2022-03-14 05:44:38 # Node ID da13ec49a1f1d531c6569546b9e53b9b5d2f93b5 # Parent 9b246dc3530b7c4c93cc659f8fd2638ede684b8a Calculate surrounding cells for preserve. diff --git a/isometric-park-fna/Engines/SimulationBridgeEngine.cs b/isometric-park-fna/Engines/SimulationBridgeEngine.cs --- a/isometric-park-fna/Engines/SimulationBridgeEngine.cs +++ b/isometric-park-fna/Engines/SimulationBridgeEngine.cs @@ -70,16 +70,29 @@ decimal new_misc_amount = 0M; // decimal new_upkeep_amount = 0M; - this.simulation.PreserveCells.Clear(); + // this.simulation.PreserveCells.Clear(); + + var preserve_cells = new HashSet(); + var count = 0; foreach (ref readonly var entity in ReadEntities()) { ref readonly var areaComponent = ref GetComponent(entity); foreach (var square in areaComponent.squares) { - this.simulation.PreserveCells.Add(this.simulation.map.cells[(int)square.X][(int)square.Y]); + preserve_cells.Add(this.simulation.map.cells[(int)square.X][(int)square.Y]); } + } - + for (int i = 0; i < this.simulation.PreserveCounts.GetLength(0); i++) { + for (int j = 0; j < this.simulation.PreserveCounts.GetLength(0); j++) { + count = 0; + foreach (var cell in this.simulation.map.iterate_neighbor_cells(i, j)) { + if (preserve_cells.Contains(cell)) { + count++; + } + } + this.simulation.PreserveCounts[i, j] = count; + } } foreach (ref readonly var entity in ReadEntities()) diff --git a/isometric-park-fna/Renderers/AreaRenderer.cs b/isometric-park-fna/Renderers/AreaRenderer.cs --- a/isometric-park-fna/Renderers/AreaRenderer.cs +++ b/isometric-park-fna/Renderers/AreaRenderer.cs @@ -26,7 +26,7 @@ public override void Render() { - var budgetWindow = new BudgetWindow(new Budget { }, this.font, 0, 0); + var budgetWindow = new BudgetWindow(new Budget {}, this.font, 0, 0); foreach (ref readonly var entity in ReadEntities()) { diff --git a/isometric-park-fna/Simulation.cs b/isometric-park-fna/Simulation.cs --- a/isometric-park-fna/Simulation.cs +++ b/isometric-park-fna/Simulation.cs @@ -176,7 +176,8 @@ public CellMap map; - public HashSet PreserveCells; + // public HashSet PreserveCells; + public int[,] PreserveCounts; public int ticksPerAdvance; private float lastAdvance; @@ -261,7 +262,8 @@ this.BridgeEngine = new SimulationBridgeEngine(this); - this.PreserveCells = new HashSet(); + // this.PreserveCells = new HashSet(); + this.PreserveCounts = new int[this.map.MapWidth, this.map.MapHeight]; } private void advanceSimulation() @@ -274,12 +276,17 @@ this.BridgeEngine.addTick(); - foreach (Cell cell in this.map.iterate_cells()) + // foreach (Cell cell in this.map.iterate_cells()) + for(int i = 0; i < this.map.MapWidth; i++) { - if (random.NextDouble() > (this.PreserveCells.Contains(cell) ? PRESERVE_SPONTANEOUS_NEW_TREE_CHANCE : SPONTANEOUS_NEW_TREE_CHANCE )) + for(int j = 0; j < this.map.MapHeight; j++) { - var random_type = random.Next(0, 4); - cell.AddTree(this.DateTime, (TreeType)random_type); + var cell = this.map.cells[i][j]; + if (random.NextDouble() > (this.PreserveCounts[i, j] > 0 ? PRESERVE_SPONTANEOUS_NEW_TREE_CHANCE : SPONTANEOUS_NEW_TREE_CHANCE )) + { + var random_type = random.Next(0, 4); + cell.AddTree(this.DateTime, (TreeType)random_type); + } } } @@ -287,10 +294,10 @@ foreach (var (x, y) in this.map.iterate_cell_locations_with_neighbors(4)) { var neighbor = this.map.iterate_neighbors(x, y).First(); - var cell = this.map.cells[x][y]; - if (random.NextDouble() > (this.PreserveCells.Contains(cell) ? PRESERVE_NEIGHBOR_NEW_TREE_CHANCE : NEIGHBOR_NEW_TREE_CHANCE )) + if (random.NextDouble() > (this.PreserveCounts[x, y] > 0 ? PRESERVE_NEIGHBOR_NEW_TREE_CHANCE : NEIGHBOR_NEW_TREE_CHANCE )) { + var cell = this.map.cells[x][y]; var random_type = random.Next(0, 4); cell.AddTree(this.DateTime, neighbor.Type); new_planted += 1; @@ -298,10 +305,11 @@ } int crowded_out = 0; - foreach (Cell cell in this.map.iterate_cells_with_neighbors(7)) + foreach (var (x, y) in this.map.iterate_cell_locations_with_neighbors(7)) { - if (random.NextDouble() > (this.PreserveCells.Contains(cell) ? PRESERVE_NEIGHBOR_CROWDS_TREE_CHANCE : NEIGHBOR_CROWDS_TREE_CHANCE ) ) + if (random.NextDouble() > (this.PreserveCounts[x, y] > 0 ? PRESERVE_NEIGHBOR_CROWDS_TREE_CHANCE : NEIGHBOR_CROWDS_TREE_CHANCE ) ) { + var cell = this.map.cells[x][y]; cell.MarkTreeDead(); crowded_out += 1; }