Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions C7Engine/C7GameData/AIData/TileKnowledge.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Collections.Generic;
using System.Linq;
using System;
using System.Diagnostics;

namespace C7GameData {
public class TileKnowledge {
Expand All @@ -25,6 +26,21 @@ public void AddTilesToKnown(Tile unitLocation, bool recomputeActiveTiles = true)
knownTiles.Add(unitLocation);
borderTiles.Remove(unitLocation);

// Crude benchmarking tool for GetTilesVisibleToUnit, which can be
// a hot function when profiling.
// {
// Stopwatch stopwatch = new Stopwatch();
// stopwatch.Start();

// for (int i = 0; i < 10000; ++i) {
// foreach (Tile t in GetTilesVisibleToUnit(unitLocation)) {
// knownTiles.Add(t);
// }
// }

// System.Console.WriteLine($"10k runs took: {stopwatch.ElapsedMilliseconds} milliseconds");
// }

foreach (Tile t in GetTilesVisibleToUnit(unitLocation)) {
knownTiles.Add(t);
borderTiles.Remove(t);
Expand All @@ -44,18 +60,20 @@ public void AddTilesToKnown(Tile unitLocation, bool recomputeActiveTiles = true)
}
}

private HashSet<Tile> GetTilesVisibleToUnit(Tile unitLocation) {
HashSet<Tile> result = new();
private List<Tile> GetTilesVisibleToUnit(Tile unitLocation) {
// Space for current tile, 8 inner ring tiles, 12 outer ring tiles
List<Tile> result = new(21);
result.Add(unitLocation);
int unitHeight = unitLocation.overlayTerrainType.height;

foreach (TileDirection a in TileDirection.GetValues(typeof(TileDirection))) {
Tile innerRingNeighbor = unitLocation.neighbors[a];
foreach (var (innerTileDirection, innerRingNeighbor) in unitLocation.neighbors) {
if (innerRingNeighbor == Tile.NONE) {
continue;
}
result.Add(innerRingNeighbor);
int innerHeight = innerRingNeighbor.overlayTerrainType.height;

foreach (TileDirection b in TileDirection.GetValues(typeof(TileDirection))) {
foreach (var (outerTileDirection, outerRingNeighbor) in innerRingNeighbor.neighbors) {
// Say we have the following. We are standing on the XX tile
// and need to figure out which tiles we can see. We can see
// the hill directly to our SW, because we're next to it.
Expand All @@ -71,16 +89,13 @@ private HashSet<Tile> GetTilesVisibleToUnit(Tile unitLocation) {
// < .. >< Hill >< gg >
// < Hill >< gg >
// < Hill >
if (((int)a) % 2 == 0 && ((int)b) % 2 == 0) {
if (((int)innerTileDirection) % 2 == 0 && ((int)outerTileDirection) % 2 == 0) {
continue;
}

Tile outerRingNeighbor = innerRingNeighbor.neighbors[b];
if (outerRingNeighbor == Tile.NONE) {
continue;
}
int unitHeight = unitLocation.overlayTerrainType.height;
int innerHeight = innerRingNeighbor.overlayTerrainType.height;
int outerHeight = outerRingNeighbor.overlayTerrainType.height;

// Tiles with a height of at least 2 are visible from 2 tiles
Expand Down
Loading