Enhance simulation performance and accuracy by optimizing physical amount calculations and refining arithmetic operations

This commit is contained in:
Kirill
2026-10-01 20:30:53 +03:00
parent befb377774
commit f51eea6c0f
14 changed files with 155 additions and 55 deletions
+22 -17
View File
@@ -10,10 +10,11 @@ static func is_space_civilization(id: String, rules: Dictionary) -> bool:
# Preserve any existing ethics. New populations start equally distributed;
# reductions (including blasts) are proportional and never leave negative counts.
static func reconcile(world, target: Dictionary, rules: Dictionary) -> void:
static func reconcile(world, target: Dictionary, rules: Dictionary, physical: Dictionary = {}) -> void:
if (not target.has("ethics")):
target.ethics = {}
var physical: Dictionary = world.ledger.physical_amounts(target)
if physical.is_empty():
physical = world.ledger.physical_amounts(target)
for id in rules:
var count: Variant = physical.get(id, 0)
var current: Dictionary = target.ethics.get(id, {})
@@ -40,8 +41,9 @@ static func detach_space_civilizations(target: Dictionary, rules: Dictionary) ->
# A shared budget per stellar family, including stars bound into structures.
# Reservations affect capacity, not the physical stock or its production.
static func stellar_budget(world, target: Dictionary, rules: Dictionary) -> Dictionary:
var physical: Dictionary = world.ledger.physical_amounts(target)
static func stellar_budget(world, target: Dictionary, rules: Dictionary, physical: Dictionary = {}) -> Dictionary:
if physical.is_empty():
physical = world.ledger.physical_amounts(target)
var capacity: Dictionary = {}
for id in physical:
var entity = world.catalog.entities[id]
@@ -62,7 +64,7 @@ static func stellar_budget(world, target: Dictionary, rules: Dictionary) -> Dict
var free: Dictionary = {}
for home in capacity:
free[home] = E.maximum(0, E.subtract(capacity[home], requested.get(home, 0)))
return {"capacity": capacity, "requested": requested, "free": free, "populations": populations, "habitats": habitat_budget(world, target, rules)}
return {"capacity": capacity, "requested": requested, "free": free, "populations": populations, "habitats": habitat_budget(world, target, rules, physical)}
# Account for the source's existing reservation when changing stages. A failed
# transition retains its population at the source, rather than losing objects.
@@ -105,8 +107,9 @@ static func admit_transition(budget: Dictionary, rules: Dictionary, source: Stri
# advanced populations priority and lets a demotion cascade in the same cycle.
static func enforce_capacity(world, target: Dictionary, rules: Dictionary, year: Variant) -> void:
detach_space_civilizations(target, rules)
reconcile(world, target, rules)
var budget: Dictionary = stellar_budget(world, target, rules)
var physical: Dictionary = world.ledger.physical_amounts(target)
reconcile(world, target, rules, physical)
var budget: Dictionary = stellar_budget(world, target, rules, physical)
var remaining: Dictionary = budget.capacity.duplicate()
var remaining_homes: Dictionary = budget.habitats.capacity.duplicate()
var stages: Array = []
@@ -141,10 +144,10 @@ static func enforce_capacity(world, target: Dictionary, rules: Dictionary, year:
target.ethics[previous][ethic] = E.add(target.ethics[previous].get(ethic, 0), removed[ethic])
if E.eq(previous, "C1.9"):
var stars: Dictionary = {}
var physical: Dictionary = world.ledger.physical_amounts(target)
for star in physical:
if (E.eq(world.catalog.entities[star].event_group, "DwStar") and E.gt(physical[star], 0)):
stars[star] = physical[star]
var current_physical: Dictionary = world.ledger.physical_amounts(target)
for star in current_physical:
if (E.eq(world.catalog.entities[star].event_group, "DwStar") and E.gt(current_physical[star], 0)):
stars[star] = current_physical[star]
var weights: Dictionary = QuantityMath.normalize_distribution(stars)
# Worlds are aggregates: a star may host several inhabited planets.
var assigned: Variant = 0
@@ -158,13 +161,15 @@ static func enforce_capacity(world, target: Dictionary, rules: Dictionary, year:
target.binds[owner] = {}
target.binds[owner][previous] = E.add(target.binds[owner].get(previous, 0), amount)
target.amount[previous] = E.subtract(target.amount[previous], amount)
reconcile(world, target, rules)
assign_homes(world, target, rules)
physical = world.ledger.physical_amounts(target)
reconcile(world, target, rules, physical)
assign_homes(world, target, rules, physical)
# Unity's structure chain: direct Claim resets the required structure count;
# intervening stages require ten times more units of the preceding structure.
static func habitat_budget(world, target: Dictionary, rules: Dictionary) -> Dictionary:
var physical: Dictionary = world.ledger.physical_amounts(target)
static func habitat_budget(world, target: Dictionary, rules: Dictionary, physical: Dictionary = {}) -> Dictionary:
if physical.is_empty():
physical = world.ledger.physical_amounts(target)
var structures: Array = []
var capacity: Dictionary = {}
for id in world.catalog.entities:
@@ -198,8 +203,8 @@ static func habitat_budget(world, target: Dictionary, rules: Dictionary) -> Dict
requested[req.id] = E.add(requested.get(req.id, 0), E.multiply(physical.get(id, 0), req.units))
return {"capacity": capacity, "requested": requested, "requirements": requirements, "structures": structures, "anchors": anchors}
static func assign_homes(world, target: Dictionary, rules: Dictionary) -> void:
var budget: Dictionary = habitat_budget(world, target, rules)
static func assign_homes(world, target: Dictionary, rules: Dictionary, physical: Dictionary = {}) -> void:
var budget: Dictionary = habitat_budget(world, target, rules, physical)
var used: Dictionary = {}
target.civilization_homes = {}
var stages: Array = budget.anchors.keys()