Add comprehensive civilization evolution tests and enhance civilization state management

- Introduced a new test script for civilization evolution (`test_civilization_evolution.gd`) to validate the evolution mechanics, including population dynamics, ethical transitions, and war impacts.
- Updated `README.md` to include instructions for running the new civilization evolution tests.
- Modified `test_civilization_state.gd` to accommodate changes in civilization rules and ensure proper validation of ethical stages.
- Enhanced `test_descriptions.gd` to check for accurate population counters and state validation.
- Adjusted `test_life.gd` to reflect updates in life version management.
- Improved `civilization_state.gd` to include habitat budget calculations and transition management.
- Added `civilization_evolution.gd` to handle the logic for civilization evolution, including population drift, warfare, and colonization mechanics.
- Updated `owniverse.gd` and `owniverse_entity.gd` to integrate new evolution logic and ensure proper initialization of entities.
This commit is contained in:
Kirill
2026-09-16 19:54:47 +03:00
parent 5ea65a7e9b
commit 26347fca30
14 changed files with 607 additions and 20 deletions
+9
View File
@@ -62,3 +62,12 @@ godot --headless --path Owniverse.GI --script res://tests/test_civilization_stat
```
Тест цивилизаций дополнительно проверяет общий звёздный бюджет, повторное использование резерва при переходе, звёзды в структурах, массовый эквивалент SMBH, блокировку неподдержанного прогресса, каскад деградации и повторное применение ограничений без потерь.
## Полная эволюция цивилизаций
```sh
godot --headless --path Owniverse.GI --script res://tests/test_civilization_evolution.gd
```
Проверяются реальные коэффициенты C0.2, войны и побочный ущерб C2.0, замена планет колониями с сохранением хозяина, дипломатическое влияние, бонус периода от структур и деградация при их отсутствии. Ускоренная модель проходит все стадии C0.2 → C4.4 → Ct. Проверки контролируют сохранение населения, целочисленность этик, валидацию сохранения и детерминированное продолжение после сериализации. Миграция часов цивилизаций использует `life_version = 3`.
+164
View File
@@ -0,0 +1,164 @@
extends SceneTree
const Clock = preload("res://world/owniverse/simulation_time.gd")
var checks: int = 0
var failures: int = 0
func _initialize() -> void:
call_deferred("run")
func check(condition: bool, message: String) -> void:
checks += 1
if not condition:
failures += 1
push_error(message)
func fixture():
var world = load("res://world/owniverse/owniverse.gd").new()
var global: Node = root.get_node("Global")
for id in global.OwniverseEntities:
var entity = load("res://world/owniverse/owniverse_entity.gd").new(global.OwniverseEntities[id])
world.entities[id] = entity
world.state.amount[id] = 0.0
world.state.stats.total_amount[id] = 0.0
if entity.group_as != "":
world.state.amount[entity.group_as] = 0.0
world.state.stats.total_amount[entity.group_as] = 0.0
world.entities["N"].life_product = {}
world.entities["N"].product_per_year_type = "H"
world.entities["N"].product_per_year_amount = 10.0
world.entities["N"].auto = 0.0
world.state.amount.N = 1.0
return world
func dispose(world) -> void:
for entity in world.entities.values():
entity.free()
world.free()
func seed_population(world, id: String, count: float, time: float = 1.0) -> void:
world.state.amount.N = 0.0
world.state.amount[id] = count
world.state.batches[id] = [{"amount": count, "start_year": time, "end_year": time}]
world.initialize_life_batches()
func sum_ethics(world, id: String) -> float:
var result: float = 0.0
for n in world.state.ethics.get(id, {}).values(): result += n
return result
func invariants(world) -> void:
var physical: Dictionary = world._physical_amounts(world.state)
for id in world.state.ethics:
check(sum_ethics(world, id) == physical.get(id, 0.0), "Ethics equal physical population for " + id)
for n in world.state.ethics[id].values():
if n < 0.0 or n != floorf(n): check(false, "Invalid ethic amount")
check(root.get_node("SaveManager").validate_data({"state": world.state}) == "", "Civilization state validates")
func run() -> void:
var global: Node = root.get_node("Global")
var baseline: Dictionary = global.CivilizationRules.duplicate(true)
var world = fixture()
seed_population(world, "C0.2", 60000.0)
world._evolve_batches(world.state, 1.0)
check(world.state.amount["C0.3"] > 0.0, "C0.2 actually progresses")
check(world.state.amount["C0.1"] > 0.0, "C0.2 actually degrades")
check(world.state.civilization_stats.get("wars", 0.0) == 0.0, "Table disables early wars")
check(world.state.amount["C0.2"] + world.state.amount["C0.3"] + world.state.amount["C0.1"] == 60000.0, "Ordinary evolution conserves population")
check(world.state.stats.total_amount["C0.2"] == 0.0, "Survivors do not count as new civilizations")
check(world.state.civilization_stats.worship > 0.0, "Worship derived from surviving ethics")
invariants(world)
var restored = fixture()
restored.state = bytes_to_var(var_to_bytes(world.state))
world._evolve_batches(world.state, 2100.0)
restored._evolve_batches(restored.state, 2100.0)
check(world.state == restored.state, "Save resumes deterministic ethical evolution")
invariants(world)
dispose(restored)
dispose(world)
world = fixture()
world.state.amount.RD = 1e8
seed_population(world, "C2.0", 60000.0)
world.state.amount["C1.9"] = 10000.0
world.initialize_life_batches()
world._evolve_batches(world.state, 1.0)
check(world.state.civilization_stats.wars > 0.0, "Wars execute when enabled")
check(world.state.civilization_stats.collateral > 0.0, "Wars affect previous-stage populations")
check(world.state.amount["C2.1"] > 0.0, "Winners and peaceful progress reach next stage")
invariants(world)
dispose(world)
world = fixture()
world.state.amount.RD = 1.0
world.state.binds.RD = {"HP": 5.0, "GG": 5.0, "AB": 5.0, "RP": 5.0}
seed_population(world, "C1.6", 60000.0)
for row: Dictionary in global.CivilizationRules["C1.6"].ethics.values(): row.degradation = "0"
world._evolve_batches(world.state, 1.0)
var after: Dictionary = world._physical_amounts(world.state)
check(world.state.civilization_stats.colonies == 20.0, "Colonization limited to real targets")
check(after.HP + after.GG + after.AB + after.RP == 0.0, "Colonized objects consumed once")
check(world.state.binds.RD.get("C1.4", 0.0) == 20.0, "Colonies retain target host and use two-stages-earlier level")
var totals: float = 0.0
for id in after:
if world.entities[id].group_as == "Planet": totals += after[id]
check(totals == 60020.0, "Colonization replaces planets, does not create extra worlds")
invariants(world)
dispose(world)
global.CivilizationRules = baseline.duplicate(true)
world = fixture()
world.state.amount.RD = 1e25
world.state.amount.BG = 1e25
world.state.amount.TWD = 1e25
world.state.amount.SNS = 1e25
world.state.amount.MGT = 1e25
world.state.amount.BH25 = 1e25
world.state.amount.SMBH = 1e26
for id in world.entities:
if world.entities[id].claim != "": world.state.amount[id] = 1e12
# Accelerated deterministic fixture traverses every enabled stage, including Ct.
for id in global.CivilizationRules:
if id == "Ct": continue
global.CivilizationRules[id].fights = false
global.CivilizationRules[id].colonizes = false
global.CivilizationRules[id].diplomacy = false
for row: Dictionary in global.CivilizationRules[id].ethics.values():
row.progress = "1"
row.degradation = "0"
world.entities[id].lifespan = 1.0
world.entities[id].lifespan_delta = 1.0
global.CivilizationRules[id].span = 1.0
seed_population(world, "C0.2", 6.0)
world._evolve_batches(world.state, 1000.0)
check(world.state.amount.Ct == 6.0, "Complete C0.2 -> C4.4 -> Ct chain runs")
check(not world.state.batches.has("Ct"), "Transcendence is terminal")
invariants(world)
dispose(world)
global.CivilizationRules = baseline
world = fixture()
world.state.amount.RD = 10000.0
world.state.amount.SCS = 1.0
world.state.amount["C2.4"] = 1.0
world.initialize_life_batches()
var span: float = world.state.batches["C2.4"][0].start_year
check(is_equal_approx(span, 20000.0 / 3.6), "Structure bonus shortens newly registered lifespan")
dispose(world)
var evolution = load("res://world/owniverse/civilization_evolution.gd")
var balanced: Dictionary = {"m": 10000.0, "l": 10000.0, "w": 10000.0, "d": 10000.0, "c": 10000.0, "b": 10000.0}
var neutral: Dictionary = evolution.drift({}, "test", balanced, global.CivilizationRules["C2.0"].ethics)
var diplomatic: Dictionary = evolution.drift({}, "test", balanced, global.CivilizationRules["C2.0"].ethics, true)
check(neutral == balanced, "Ordinary drift preserves balanced ethics")
check(diplomatic.d > diplomatic.w, "Diplomacy uses per-ethic influence")
check(evolution.total(diplomatic) == 60000.0, "Diplomacy conserves population")
world = fixture()
world.state.amount.RD = 1e15
world.state.amount.BG = 1e15
world.state.amount["C3.0"] = 6.0
world.initialize_life_batches()
check(world.state.amount["C3.0"] == 0.0 and world.state.amount["C2.9"] == 6.0, "Missing mandatory structures degrade existing civilizations")
invariants(world)
dispose(world)
print("Civilization evolution: %d checks, %d failures" % [checks, failures])
quit(0 if failures == 0 else 1)
+1
View File
@@ -0,0 +1 @@
uid://u4ujq17gu2l5
+11 -4
View File
@@ -38,9 +38,9 @@ func dispose(world) -> void:
func run() -> void:
var world = fixture()
var global: Node = root.get_node("Global")
check(global.CivilizationRules.size() == 43, "Load all ethical civilization stages")
check(global.CivilizationRules.size() == 44, "Load all ethical civilization stages")
for row: Dictionary in global.CivilizationRules.values():
check(row.ethics.size() == 6, "Every ethical stage has six rules")
check(row.ethics.size() == 6 or row.span == 0.0, "Every ethical stage has six rules")
world.state.amount.RD = 12.0
world.state.amount["C0.2"] = 8.0
world.state.binds.RD = {"C2.0": 12.0, "C1.9": 3.0}
@@ -67,7 +67,10 @@ func run() -> void:
dispose(world)
world = fixture()
world.state.amount.RD = 10.0
world.entities["C1.9"].output_and = {"C2.0": 1.0}
for row: Dictionary in global.CivilizationRules["C1.9"].ethics.values():
row.progress = "1"
row.degradation = "0"
row.colonization = "0"
world.state.binds.RD = {"C1.9": 10.0}
world.state.batches["C1.9"] = [{"amount": 10.0, "start_year": 1.0, "end_year": 1.0}]
world._evolve_batches(world.state, 1.0)
@@ -107,6 +110,7 @@ func run() -> void:
dispose(world)
world = fixture()
world.state.amount.LGW = 20.0
world.state.amount.SMBH = 2.0e22
world.state.amount["C4.2"] = 3.0
world.initialize_life_batches()
@@ -117,7 +121,10 @@ func run() -> void:
world = fixture()
world.state.amount.RD = 2.0
world.entities["C1.9"].output_and = {"C2.0": 1.0}
for row: Dictionary in global.CivilizationRules["C1.9"].ethics.values():
row.progress = "1"
row.degradation = "0"
row.colonization = "0"
world.state.binds.RD = {"C1.9": 5.0}
world.state.batches["C1.9"] = [{"amount": 5.0, "start_year": 1.0, "end_year": 1.0}]
world._evolve_batches(world.state, 1.0)
+8
View File
@@ -62,6 +62,14 @@ func run() -> void:
check(values.amount.contains("#ff6060") and values.amount.contains("−2.0"), "Real-time decrease red")
values = description.variables(world, global, "HP")
check(values.amount == "7", "Planet description includes bound planets")
world.state.amount.EON1 = 2.0
world.state.binds.G.EON2 = 3.0
var saved_state: Dictionary = world.state.duplicate(true)
var counters: Dictionary = world.get_item_amounts()
check(counters.HP == 7.0 and counters.LP == 5.0 and counters.Planet == 12.0, "Planet counters include bound objects and aggregate eons")
check(counters.RD == 2.0, "Star counters retain available quantity")
check(world.state == saved_state, "Reading counters does not mutate simulation")
check(world.get_item_amounts(true).HP == 0.0, "Produced counters remain independent of current population")
values = description.variables(world, global, "RD", 2.0)
check(values.amount.contains("#40cc40") and values.amount.contains("+2.0"), "Increase green")
var stage_script = load("res://stage/stage.gd")
+1 -1
View File
@@ -103,7 +103,7 @@ func run() -> void:
world.state.life_version = 1
world.state.amount["C0.0"] = 40000.0
world.initialize_life_batches()
check(world.state.life_version == 2 and world.state.batches["C0.0"][0].created_from == 123.4, "Previous life saves receive missing civilization cohorts")
check(world.state.life_version == 3 and world.state.batches["C0.0"][0].created_from == 123.4, "Previous life saves receive missing civilization cohorts")
before = world.state.duplicate(true)
world.initialize_life_batches()
check(world.state == before, "Second life migration is idempotent")