## The balance pass (issue H3). Two modes: ## ## tools/godot.sh --script res://tools/balance.gd -- --run --first=1 --seeds=50 --out=PATH ## plays bot-only matches and appends one JSON line per match to PATH; ## tools/godot.sh --script res://tools/balance.gd -- --report=DIR ## reads every DIR/*.jsonl, writes DIR/report.txt and judges it against the GDD's ## balance targets (BALANCE_* in Tuning). Exits 1 if a target is missed. ## ## tools/balance.sh runs several --run processes side by side, then the report. extends SceneTree func _init() -> void: var run: bool = false var first: int = 1 var count: int = 10 var out: String = "" var report_dir: String = "" for arg: String in OS.get_cmdline_user_args(): if arg == "--run": run = true elif arg.begins_with("--first="): first = arg.trim_prefix("--first=").to_int() elif arg.begins_with("--seeds="): count = arg.trim_prefix("--seeds=").to_int() elif arg.begins_with("--out="): out = arg.trim_prefix("--out=") elif arg.begins_with("--report="): report_dir = arg.trim_prefix("--report=") if run and out != "": quit(_run(first, count, out)) elif report_dir != "": quit(_report(report_dir)) else: printerr("balance: give --run --first=N --seeds=N --out=PATH, or --report=DIR") quit(2) func _run(first: int, count: int, out: String) -> int: for seed_value: int in range(first, first + count): var result: Dictionary = BotBatch.run_match(seed_value) result.erase("slow_ticks") var file: FileAccess = FileAccess.open(out, FileAccess.READ_WRITE if FileAccess.file_exists(out) else FileAccess.WRITE) if file == null: printerr("balance: can't write %s" % out) return 1 file.seek_end() file.store_line(JSON.stringify(result)) file.close() print("balance: seed %d done, %.1f min" % [seed_value, float(result["ticks"]) / Tuning.TICK_RATE_HZ / 60.0]) return 0 func _report(dir_path: String) -> int: var results: Array[Dictionary] = [] var dir: DirAccess = DirAccess.open(dir_path) if dir == null: printerr("balance: no directory %s" % dir_path) return 2 var names: PackedStringArray = dir.get_files() names.sort() for file_name: String in names: if not file_name.ends_with(".jsonl"): continue for line: String in FileAccess.get_file_as_string(dir_path.path_join(file_name)).split("\n", false): var parsed: Variant = JSON.parse_string(line) if parsed is Dictionary: results.append(parsed) var analysis: Dictionary = BalanceReport.analyse(results) var text: String = BalanceReport.format(analysis) var file: FileAccess = FileAccess.open(dir_path.path_join("report.txt"), FileAccess.WRITE) file.store_string(text) file.close() print(text) return 0 if analysis["problems"].is_empty() else 1