5359ec922c
Removes every predetermined construction trigger (shelters-per-capita cap, granary-at-five-shelters milestone). Buildings now emerge from lived need: - Shelters have real bed capacity (3 + modules). A full house turns you away; three nights sleeping outside and a person gathers stone and raises a roof where they actually live. When nobody sleeps outside, nobody builds. Ruined shelters spill their sleepers, so settlements can migrate over time. - The first granary is founded by memory: someone who has truly starved stockpiles food once the land gives again and builds storage where the harvest is. A second granary appears only when the first fills. - Emergent consequence: freeriders compete for beds but never build, so scarce housing squeezes them into the open. The village never decides to punish them; the housing supply does. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
231 lines
7.7 KiB
C#
231 lines
7.7 KiB
C#
using System.Collections.Generic;
|
|
using Godot;
|
|
|
|
namespace WorldSim;
|
|
|
|
/// <summary>
|
|
/// Main loop and time system (SPEC §9).
|
|
/// 1 tick = 1 in-game minute; 1440 ticks = 1 day.
|
|
/// Day/night drives activity bias: gather/build by day, rest/socialize by night.
|
|
/// </summary>
|
|
public partial class GameManager : Node2D
|
|
{
|
|
public static GameManager? Instance { get; private set; }
|
|
|
|
[Export] public int Seed = 12345;
|
|
|
|
// --- Settlements ------------------------------------------------------
|
|
// Expansion phase: multiple towns, each a soul culture with its own
|
|
// centroid, buildings, and granary. NPCs affiliate with a home town
|
|
// but roam freely — the space between towns is where cultures meet.
|
|
|
|
[Export] public int SouthPopulation = 12; // nature souls
|
|
[Export(PropertyHint.Range, "0,15,1")]
|
|
public int SelfishCount = 3; // east souls living in the South
|
|
[Export] public int NorthPopulation = 10; // generational souls
|
|
|
|
public record TownDef(string Name, Vector2 Center, SoulType Soul);
|
|
|
|
public IReadOnlyList<TownDef> Towns => _towns;
|
|
private readonly List<TownDef> _towns = new()
|
|
{
|
|
new("South", new Vector2(128f, 190f), SoulType.Nature),
|
|
new("North", new Vector2(128f, 62f), SoulType.Generational),
|
|
};
|
|
|
|
[Export(PropertyHint.Range, "1,2000,1")]
|
|
public float TicksPerRealSecond = 20f; // sim speed; ~1500 ≈ one day/sec for soaks
|
|
|
|
public World World { get; private set; } = null!;
|
|
public List<Npc> Npcs { get; } = new();
|
|
public List<Building> Buildings { get; } = new();
|
|
|
|
public long TotalTicks { get; private set; }
|
|
public int Day => (int)(TotalTicks / 1440);
|
|
public int MinuteOfDay => (int)(TotalTicks % 1440);
|
|
public bool IsNight => MinuteOfDay < 6 * 60 || MinuteOfDay >= 22 * 60;
|
|
|
|
// --- Seasons: the pressure that makes community structure matter ------
|
|
|
|
[Export] public int DaysPerSeason = 30;
|
|
|
|
public int SeasonIndex => (Day / DaysPerSeason) % 4;
|
|
|
|
public string SeasonName => SeasonIndex switch
|
|
{
|
|
0 => "spring", 1 => "summer", 2 => "autumn", _ => "winter",
|
|
};
|
|
|
|
/// <summary>Food regen multiplier — winter gives almost nothing.</summary>
|
|
public float SeasonFoodMult => SeasonIndex switch
|
|
{
|
|
0 => 1.5f, 1 => 1.0f, 2 => 0.8f, _ => 0.05f,
|
|
};
|
|
|
|
/// <summary>Wood regen multiplier — growth slows in the cold.</summary>
|
|
public float SeasonWoodMult => SeasonIndex switch
|
|
{
|
|
0 => 1.2f, 1 => 1.0f, 2 => 1.0f, _ => 0.4f,
|
|
};
|
|
|
|
private double _tickAccumulator;
|
|
|
|
public override void _Ready()
|
|
{
|
|
Instance = this;
|
|
TotalTicks = 8 * 60; // start at 08:00 — day one begins mid-morning, not asleep
|
|
World = new World(Seed);
|
|
World.Generate();
|
|
SpawnNpcs();
|
|
StatsLogger.Init();
|
|
GD.Print($"[WorldSim] World generated (seed {Seed}), {Npcs.Count} NPCs dropped in.");
|
|
}
|
|
|
|
private void SpawnNpcs()
|
|
{
|
|
// Dropped near their town's seed point with scatter — no scripted
|
|
// layout. Where each community actually forms is the experiment.
|
|
var rng = new System.Random(Seed);
|
|
|
|
void Spawn(string town, Vector2 center, SoulProfile soul, int count, string prefix)
|
|
{
|
|
for (int i = 0; i < count; i++)
|
|
{
|
|
Npcs.Add(new Npc
|
|
{
|
|
Name = $"{prefix}_{(char)('A' + Npcs.Count % 26)}{(Npcs.Count >= 26 ? Npcs.Count.ToString() : "")}",
|
|
HomeTown = town,
|
|
Soul = soul,
|
|
PersonalityModifier = (float)(rng.NextDouble() * 0.4 - 0.2),
|
|
Position = center + new Vector2(rng.Next(-12, 13), rng.Next(-12, 13)),
|
|
});
|
|
}
|
|
}
|
|
|
|
foreach (var town in _towns)
|
|
{
|
|
if (town.Name == "South")
|
|
{
|
|
Spawn(town.Name, town.Center, SoulProfile.NatureSoul(), SouthPopulation, "South");
|
|
for (int i = 0; i < SelfishCount; i++)
|
|
Spawn(town.Name, town.Center, SoulProfile.SelfishSoul(), 1, "South");
|
|
}
|
|
else if (town.Name == "North")
|
|
{
|
|
Spawn(town.Name, town.Center, SoulProfile.GenerationalSoul(), NorthPopulation, "North");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>How many villagers are currently sleeping at this shelter.</summary>
|
|
public int RestingOccupancy(Building b)
|
|
{
|
|
int count = 0;
|
|
foreach (var npc in Npcs)
|
|
if (npc.State == NpcState.Resting && npc.Position.DistanceTo(b.Site) <= 2.5f)
|
|
count++;
|
|
return count;
|
|
}
|
|
|
|
public int TownPopulation(string town)
|
|
{
|
|
int count = 0;
|
|
foreach (var npc in Npcs)
|
|
if (npc.HomeTown == town) count++;
|
|
return count;
|
|
}
|
|
|
|
public Vector2 TownCentroid(string town)
|
|
{
|
|
Vector2 sum = Vector2.Zero;
|
|
int count = 0;
|
|
foreach (var npc in Npcs)
|
|
{
|
|
if (npc.HomeTown != town) continue;
|
|
sum += npc.Position;
|
|
count++;
|
|
}
|
|
if (count == 0)
|
|
{
|
|
foreach (var t in _towns)
|
|
if (t.Name == town) return t.Center;
|
|
return new Vector2(World.GridSize / 2f, World.GridSize / 2f);
|
|
}
|
|
return sum / count;
|
|
}
|
|
|
|
public override void _Process(double delta)
|
|
{
|
|
_tickAccumulator += delta * TicksPerRealSecond;
|
|
while (_tickAccumulator >= 1.0)
|
|
{
|
|
_tickAccumulator -= 1.0;
|
|
Tick();
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Soul-to-soul atmospheric pressure (ported from the Unity POC's
|
|
/// SoulFieldSystem): souls in proximity press on each other's
|
|
/// atmospheres. An encased soul darkens the room; a clear soul makes
|
|
/// it slightly easier for everyone near them to radiate. Both
|
|
/// directions apply; the net effect depends on relative debris.
|
|
/// </summary>
|
|
private void ApplySoulPressure()
|
|
{
|
|
const float radius = 8f;
|
|
const float strength = 0.00004f;
|
|
|
|
for (int i = 0; i < Npcs.Count; i++)
|
|
{
|
|
for (int j = i + 1; j < Npcs.Count; j++)
|
|
{
|
|
var a = Npcs[i];
|
|
var b = Npcs[j];
|
|
float dist = a.Position.DistanceTo(b.Position);
|
|
if (dist > radius) continue;
|
|
|
|
float falloff = 1f - dist / radius;
|
|
|
|
float fromA = Mathf.Lerp(-strength, strength, a.Imprint.Total) * falloff;
|
|
b.Imprint.ExternalPerception = Mathf.Clamp(b.Imprint.ExternalPerception + fromA, 0f, 1f);
|
|
|
|
float fromB = Mathf.Lerp(-strength, strength, b.Imprint.Total) * falloff;
|
|
a.Imprint.ExternalPerception = Mathf.Clamp(a.Imprint.ExternalPerception + fromB, 0f, 1f);
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>Where the community's feet actually are — new communal
|
|
/// buildings are founded here, not at a scripted town center.</summary>
|
|
public Vector2 CommunityCentroid()
|
|
{
|
|
if (Npcs.Count == 0) return new Vector2(World.GridSize / 2f, World.GridSize / 2f);
|
|
Vector2 sum = Vector2.Zero;
|
|
foreach (var npc in Npcs) sum += npc.Position;
|
|
return sum / Npcs.Count;
|
|
}
|
|
|
|
private void Tick()
|
|
{
|
|
TotalTicks++;
|
|
|
|
foreach (var npc in Npcs)
|
|
npc.Tick(World, this);
|
|
|
|
ApplySoulPressure();
|
|
|
|
if (TotalTicks % 1440 == 0)
|
|
{
|
|
World.RegenerateDaily(SeasonFoodMult, SeasonWoodMult);
|
|
foreach (var building in Buildings)
|
|
building.DailyUpkeep();
|
|
foreach (var npc in Npcs)
|
|
npc.FadeEchoes(0.005f);
|
|
StatsLogger.LogDay(this);
|
|
if (Day % 10 == 0)
|
|
GD.Print($"[WorldSim] Day {Day} complete.");
|
|
}
|
|
}
|
|
}
|