Files
owniverse/autoload/global.gd
T
Kirill 5f3875bc14 Add life rules and implement tests for civilization and life states
- Introduced a new file `life_rules.txt` to define life progression and degradation rules.
- Created `test_civilization_state.gd` to validate the functionality of civilization rules and ethics.
- Added `test_life.gd` to test life evolution, including initialization, batch processing, and state validation.
- Implemented `civilization_state.gd` to manage civilization ethics and space civilization detachment logic.
- Ensured all tests check for expected outcomes and validate state integrity after operations.
2026-09-15 23:05:05 +03:00

142 lines
4.6 KiB
GDScript

extends Node
const SPEED_LIMIT: int = 5
# Write a readable diagnostic copy next to the encrypted save. Never used for loading.
var save_plaintext_copy: bool = true
var Settings: Dictionary = {
"filename": "save-0001",
"sound_volume": 1.0,
"language": "en",
}
# TODO getter setter for locale
var Locales: Dictionary = {}
var Locale = "en"
var OwniverseEntities: Dictionary = {}
var OwniverseItems: Dictionary = {}
var Templates: Dictionary = {}
var LifeRules: Dictionary = {}
var CivilizationRules: Dictionary = {}
var batch_count: float = 40.0 # must be even not odd
func get_localized_item_description(item_id: String, item: String) -> String:
if Locales[Locale].has(item_id):
return Locales[Locale][item_id][item]
return ""
# Russian templates include the class; legacy English keys omit it.
func get_template(id: String, category: String, field: String) -> Dictionary:
var rows: Dictionary = Templates.get(Locale, {})
for key in [id + "." + category + "." + field, id + "." + field]:
if rows.has(key):
return rows[key]
return {}
func render_template(row: Dictionary, variables: Dictionary) -> String:
var result: String = str(row.get("text", ""))
for i in range(5):
var variable: String = str(row.get("value" + str(i), "")).strip_edges()
var value: String = str(variables.get(variable, "—")) if variable != "" else ""
result = result.replace("%value" + str(i) + "%", value)
return result.replace("<br>", "\n").replace("[p]", "\n")
func get_description(item_id: String, items: Array = []) -> String:
var row: Dictionary = Templates.get(Locale, {}).get(item_id, {})
var variables: Dictionary = {}
for i in mini(items.size(), 5):
variables[str(row.get("value" + str(i), ""))] = items[i]
return render_template(row, variables)
func format_number(value: float, digits: int = 0) -> String:
if value == 0: return ""
if value < 10_000:
return "%.*f" % [digits, value]
var exp_value = int(floor(log(abs(value)) / log(10.0)))
var mantissa = value / pow(10.0, exp_value)
return "%.*f" % [digits, mantissa] + "E%+d" % exp_value
func _ready() -> void:
for row: Dictionary in _read_from_csv("res://data/civilization_rules.txt").values():
if not CivilizationRules.has(row.stage):
CivilizationRules[row.stage] = {"home": row.home, "span": float(row.span), "stars": float(row.stars), "ethics": {}}
CivilizationRules[row.stage].ethics[row.ethic] = row
LifeRules = _read_from_csv("res://data/life_rules.txt")
OwniverseEntities = _read_from_csv("res://data/owniverse_entities.txt")
OwniverseItems = _read_from_csv("res://data/owniverse_items.txt")
Locales["en"] = _read_from_csv("res://data/locale.en.txt")
Locales["ru"] = _read_from_csv("res://data/locale.ru.txt")
Templates["en"] = _read_from_csv("res://data/templates.en.txt")
Templates["ru"] = _read_from_csv("res://data/templates.ru.txt")
func _read_from_csv(file_path: String) -> Dictionary:
if not FileAccess.file_exists(file_path):
printerr("File not found: " + file_path)
return {}
var file = FileAccess.open(file_path, FileAccess.READ)
if file == null:
printerr("File can not be opened:" + file_path)
return {}
var headers: PackedStringArray = []
var data: Array = []
if not file.eof_reached():
headers = PackedStringArray()
for val in file.get_csv_line("|"):
headers.append(val.strip_edges())
while not file.eof_reached():
var raw_row = file.get_csv_line("|")
if raw_row.is_empty(): # пропускаем пустые строки
continue
var trimmed_row: PackedStringArray = []
for val in raw_row:
trimmed_row.append(val.strip_edges())
data.append(trimmed_row)
file.close()
var dict_data: Dictionary = {}
for row in data:
if row.size() < 2: continue
if row.size() == headers.size():
var dict: Dictionary = {}
for i in headers.size():
dict[headers[i]] = row[i]
dict_data[row[0]] = dict
return dict_data
func _read_folder_list(folder_name: String) -> void:
var dir := DirAccess.open(folder_name)
if dir == null: printerr("Could not open folder: ", folder_name); return
dir.list_dir_begin()
for file: String in dir.get_files():
print(" → ", file)
func _print_dict(dict_to_print: Dictionary):
for d in dict_to_print:
print(d, " → ", dict_to_print[d])