Files
EarthborneTrailerblazer/ET/Web/Services/GameSimulationService.cs
T
2026-06-12 17:27:40 -04:00

366 lines
13 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
namespace Web.Services;
public class RegionState
{
public string Name { get; set; } = "";
public string Slug { get; set; } = "";
public string Terrain { get; set; } = "";
public int X { get; set; }
public int Y { get; set; }
public List<string> Connections { get; set; } = new();
public int PredatorMeeples { get; set; }
public int PreyMeeples { get; set; }
public int FloraMeeples { get; set; }
public RegionState Clone()
{
return new RegionState
{
Name = Name,
Slug = Slug,
Terrain = Terrain,
X = X,
Y = Y,
Connections = new List<string>(Connections),
PredatorMeeples = PredatorMeeples,
PreyMeeples = PreyMeeples,
FloraMeeples = FloraMeeples
};
}
}
public class TerrainActivation
{
public string MeepleType { get; set; } = "";
public string RegionType { get; set; } = "";
}
public class TurnEvent
{
public int TurnNumber { get; set; }
public List<TerrainActivation> Activations { get; set; } = new();
public List<string> Details { get; set; } = new();
}
public class SimulationData
{
public List<RegionState> Regions { get; set; } = new();
public List<RegionState> InitialRegions { get; set; } = new();
public List<TurnEvent> Events { get; set; } = new();
public List<List<RegionState>> Timeline { get; set; } = new();
public int CurrentTurn { get; set; }
public bool IsInitialized { get; set; }
}
public class GameSimulationService
{
private readonly Random _rng = new();
public SimulationData Data { get; private set; } = new();
public void RunSimulation()
{
Data = new SimulationData();
var regions = CreateRegions();
Data.Regions = regions;
AddInitialFlora();
AddRandomMeepleCombos();
Data.InitialRegions = Data.Regions.Select(r => r.Clone()).ToList();
GenerateAndApplyEvents();
Data.IsInitialized = true;
}
public void RandomizeEvents()
{
RunSimulation();
}
public List<RegionState> GetCurrentState()
{
return Data.Timeline[Data.CurrentTurn].Select(r => r.Clone()).ToList();
}
public TurnEvent? GetCurrentEvent()
{
return Data.CurrentTurn > 0
? Data.Events.ElementAtOrDefault(Data.CurrentTurn - 1)
: null;
}
public List<RegionState> GetPreviousState()
{
var prev = Data.CurrentTurn > 0 ? Data.CurrentTurn - 1 : 0;
return Data.Timeline[prev].Select(r => r.Clone()).ToList();
}
public (int predators, int prey, int flora)[] GetPopulationHistory()
{
return Data.Timeline.Select(t => (
t.Sum(r => r.PredatorMeeples),
t.Sum(r => r.PreyMeeples),
t.Sum(r => r.FloraMeeples)
)).ToArray();
}
private void GenerateAndApplyEvents()
{
Data.Timeline.Add(Data.Regions.Select(r => r.Clone()).ToList());
for (int turn = 1; turn <= 20; turn++)
{
var turnEvent = GenerateTurnEvent(turn);
ApplyTurnEvent(turnEvent);
Data.Events.Add(turnEvent);
Data.Timeline.Add(Data.Regions.Select(r => r.Clone()).ToList());
}
Data.CurrentTurn = 0;
}
private TurnEvent GenerateTurnEvent(int turnNumber)
{
string[] meepleTypes = { "Predator", "Prey", "Flora" };
string[] regionTypes = { "Grass", "Forest", "Mountain", "Water", "Wasteland" };
var numTypes = _rng.Next(1, 4);
var selectedTypes = regionTypes.OrderBy(_ => _rng.Next()).Take(numTypes).ToList();
var activations = selectedTypes.Select(rt => new TerrainActivation
{
RegionType = rt,
MeepleType = meepleTypes[_rng.Next(3)]
}).ToList();
return new TurnEvent
{
TurnNumber = turnNumber,
Activations = activations,
Details = new List<string>()
};
}
private void ApplyTurnEvent(TurnEvent turnEvent)
{
var details = new List<string>();
foreach (var activation in turnEvent.Activations)
{
foreach (var region in Data.Regions.Where(r => r.Terrain == activation.RegionType))
{
switch (activation.MeepleType)
{
case "Flora":
region.FloraMeeples++;
details.Add($"{region.Name}: +1 Flora");
break;
case "Predator":
ApplyPredatorActivation(region, details);
break;
case "Prey":
ApplyPreyActivation(region, details);
break;
}
}
}
turnEvent.Details = details;
}
private void ApplyPredatorActivation(RegionState region, List<string> details)
{
if (region.PredatorMeeples <= 0)
return;
int total = region.PredatorMeeples;
int canEat = Math.Min(total, region.PreyMeeples);
int mustTravel = total - canEat;
if (canEat > 0)
{
region.PreyMeeples -= canEat;
region.PredatorMeeples += canEat;
details.Add($"{region.Name}: {canEat} predators ate prey, +{canEat} new predators");
}
if (mustTravel > 0)
{
var target = FindBestTravelTarget(region, r => r.PreyMeeples);
if (target != null)
{
region.PredatorMeeples -= mustTravel;
target.PredatorMeeples += mustTravel;
details.Add($"{region.Name}: {mustTravel} predators traveled to {target.Name}");
}
}
}
private void ApplyPreyActivation(RegionState region, List<string> details)
{
if (region.PreyMeeples <= 0)
return;
int total = region.PreyMeeples;
int canEat = Math.Min(total, region.FloraMeeples);
int mustTravel = total - canEat;
if (canEat > 0)
{
region.FloraMeeples -= canEat;
region.PreyMeeples += canEat;
details.Add($"{region.Name}: {canEat} prey ate flora, +{canEat} new prey");
}
if (mustTravel > 0)
{
var target = FindBestTravelTarget(region, r => r.FloraMeeples);
if (target != null)
{
region.PreyMeeples -= mustTravel;
target.PreyMeeples += mustTravel;
details.Add($"{region.Name}: {mustTravel} prey traveled to {target.Name}");
}
}
}
private RegionState? FindBestTravelTarget(RegionState region, Func<RegionState, int> resourceCount)
{
var direct = region.Connections
.Select(name => Data.Regions.FirstOrDefault(r => r.Name == name))
.OfType<RegionState>()
.Where(r => resourceCount(r) > 0)
.ToList();
if (direct.Count > 0)
{
return direct.OrderByDescending(r => resourceCount(r)).First();
}
// BFS to find nearest region with resource, return first step along path
var visited = new HashSet<string> { region.Name };
var prev = new Dictionary<string, string>();
var queue = new Queue<string>();
foreach (var conn in region.Connections)
{
if (!visited.Contains(conn))
{
visited.Add(conn);
prev[conn] = region.Name;
queue.Enqueue(conn);
}
}
while (queue.Count > 0)
{
var currentName = queue.Dequeue();
var current = Data.Regions.FirstOrDefault(r => r.Name == currentName);
if (current == null) continue;
if (resourceCount(current) > 0)
{
// Walk back to find the first step from the origin region
var step = currentName;
while (prev[step] != region.Name)
step = prev[step];
return Data.Regions.FirstOrDefault(r => r.Name == step);
}
foreach (var conn in current.Connections)
{
if (!visited.Contains(conn))
{
visited.Add(conn);
prev[conn] = currentName;
queue.Enqueue(conn);
}
}
}
return null;
}
private void AddInitialFlora()
{
var excluded = new HashSet<string>
{
"Grass 1", "Grass 2", "Wasteland 1",
"Mountain 1", "Mountain 2", "Mountain 3", "Mountain 4", "Mountain 5"
};
foreach (var region in Data.Regions)
{
if (!excluded.Contains(region.Name))
{
region.FloraMeeples = 1;
}
}
}
private void AddRandomMeepleCombos()
{
var combos = new List<(int pred, int prey, int flora)>();
for (int total = 1; total <= 3; total++)
{
for (int p = 0; p <= total; p++)
{
for (int r = 0; r <= total - p; r++)
{
int f = total - p - r;
combos.Add((p, r, f));
}
}
}
var excluded = new HashSet<string>
{
"Grass 1", "Grass 2", "Wasteland 1",
"Mountain 1", "Mountain 2", "Mountain 3", "Mountain 4", "Mountain 5"
};
var shuffled = combos.OrderBy(_ => _rng.Next()).ToList();
for (int i = 0; i < Data.Regions.Count; i++)
{
if (!excluded.Contains(Data.Regions[i].Name))
{
var (p, r, f) = shuffled[i % shuffled.Count];
Data.Regions[i].PredatorMeeples = p;
Data.Regions[i].PreyMeeples = r;
Data.Regions[i].FloraMeeples += f;
}
}
}
private static List<RegionState> CreateRegions()
{
return new List<RegionState>
{
new() { Name = "Grass 1", Slug = "grass-1", Terrain = "Grass", X = 15, Y = 260, Connections = new() { "Mountain 1", "Water 2", "Forest 1" } },
new() { Name = "Grass 2", Slug = "grass-2", Terrain = "Grass", X = 150, Y = 400, Connections = new() { "Forest 1", "Forest 2" } },
new() { Name = "Grass 3", Slug = "grass-3", Terrain = "Grass", X = 300, Y = 230, Connections = new() { "Water 2", "Mountain 2", "Mountain 3" } },
new() { Name = "Grass 4", Slug = "grass-4", Terrain = "Grass", X = 510, Y = 30, Connections = new() { "Mountain 4", "Wasteland 1" } },
new() { Name = "Grass 5", Slug = "grass-5", Terrain = "Grass", X = 550, Y = 290, Connections = new() { "Mountain 5", "Water 5", "Wasteland 1", "Forest 4" } },
new() { Name = "Forest 1", Slug = "forest-1", Terrain = "Forest", X = 60, Y = 330, Connections = new() { "Grass 1", "Grass 2", "Water 1", "Forest 2" } },
new() { Name = "Forest 2", Slug = "forest-2", Terrain = "Forest", X = 250, Y = 370, Connections = new() { "Grass 2", "Water 2", "Forest 1" } },
new() { Name = "Forest 3", Slug = "forest-3", Terrain = "Forest", X = 150, Y = 125, Connections = new() { "Water 2", "Water 3" } },
new() { Name = "Forest 4", Slug = "forest-4", Terrain = "Forest", X = 420, Y = 100, Connections = new() { "Grass 4", "Wasteland 1", "Grass 5" } },
new() { Name = "Forest 5", Slug = "forest-5", Terrain = "Forest", X = 470, Y = 410, Connections = new() { "Water 5" } },
new() { Name = "Mountain 1", Slug = "mountain-1", Terrain = "Mountain", X = 20, Y = 120, Connections = new() { "Grass 1", "Water 3", "Forest 3" } },
new() { Name = "Mountain 2", Slug = "mountain-2", Terrain = "Mountain", X = 260, Y = 110, Connections = new() { "Water 3", "Forest 3" } },
new() { Name = "Mountain 3", Slug = "mountain-3", Terrain = "Mountain", X = 380, Y = 180, Connections = new() { "Mountain 5", "Forest 4", "Grass 3" } },
new() { Name = "Mountain 4", Slug = "mountain-4", Terrain = "Mountain", X = 370, Y = 30, Connections = new() { "Forest 4", "Grass 4" } },
new() { Name = "Mountain 5", Slug = "mountain-5", Terrain = "Mountain", X = 430, Y = 330, Connections = new() { "Grass 5", "Mountain 3" } },
new() { Name = "Water 1", Slug = "water-1", Terrain = "Water", X = 30, Y = 410, Connections = new() { "Forest 1" } },
new() { Name = "Water 2", Slug = "water-2", Terrain = "Water", X = 140, Y = 260, Connections = new() { "Forest 1", "Forest 2", "Grass 1", "Forest 3", "Grass 3" } },
new() { Name = "Water 3", Slug = "water-3", Terrain = "Water", X = 50, Y = 50, Connections = new() { "Mountain 1", "Mountain 2", "Forest 3" } },
new() { Name = "Water 4", Slug = "water-4", Terrain = "Water", X = 640, Y = 110, Connections = new() { "Grass 4", "Wasteland 1" } },
new() { Name = "Water 5", Slug = "water-5", Terrain = "Water", X = 600, Y = 400, Connections = new() { "Grass 5", "Forest 5" } },
new() { Name = "Wasteland 1", Slug = "wasteland-1", Terrain = "Wasteland", X = 560, Y = 120, Connections = new() { "Forest 4", "Grass 4", "Grass 5", "Water 4" } }
};
}
}