Public Access
83 lines
4.7 KiB
Python
83 lines
4.7 KiB
Python
#!/usr/bin/env python3
|
|
"""Exercise the bundled-baseline Java policy without a server or test world."""
|
|
import argparse
|
|
import json
|
|
from pathlib import Path
|
|
import subprocess
|
|
import tempfile
|
|
from generate_block_baseline import generate
|
|
|
|
ROOT = Path(__file__).resolve().parents[1]
|
|
JAVA = r"""
|
|
import java.util.Map;
|
|
import net.dnorberg.rivencraft.shared.MaterialBaselinePolicy;
|
|
|
|
public class BaselineDefaultsChecks {
|
|
private static int checks;
|
|
private static void equal(double actual, double expected, String label) {
|
|
checks++;
|
|
if (Math.abs(actual - expected) > 1e-9) throw new AssertionError(label + ": " + actual + " != " + expected);
|
|
}
|
|
public static void main(String[] args) {
|
|
var policy = MaterialBaselinePolicy.load("/assets/rivencraft/instability/default_block_baseline.json");
|
|
double iron = Double.parseDouble(args[0]), coal = Double.parseDouble(args[1]);
|
|
equal(policy.average("minecraft:coal_ore", 0, 500, false, 0), coal, "Void coal default");
|
|
equal(policy.average("minecraft:coal_ore", 25, 500, false, 0), .05, "Measured vanilla wins");
|
|
equal(policy.average("test:tin_ore", 0, 500, true, 1000), 2, "Measured iron for missing modded ore");
|
|
equal(policy.average("test:tin_ore", 0, 500, true, 0), iron, "Void uses bundled iron");
|
|
equal(policy.average("create:zinc_ore", 0, 500, true, 0), iron, "Bundled modded entry cannot replace iron rule");
|
|
equal(policy.average("test:tin_ore", 5, 500, true, 0), .01, "Measured modded ore wins");
|
|
equal(policy.average("test:tin_ore", 0, 1000, true, 2000), 2, "Chunk-count normalization");
|
|
equal(policy.average("test:tin_block", 0, 500, false, 1000), 0, "No iron fallback for storage");
|
|
equal(policy.average("test:fluid", 0, 500, false, 1000), 0, "No iron fallback for fluid");
|
|
equal(policy.average("minecraft:diamond_block", 0, 500, false, 0), 0, "Absent default remains zero");
|
|
equal(policy.average("minecraft:coal_ore", 1, 3, false, 0), 1.0 / 3, "No integer rounding");
|
|
try {
|
|
policy.average("minecraft:coal_ore", 0, 0, false, 0);
|
|
throw new AssertionError("Empty profile accepted");
|
|
} catch (IllegalArgumentException expected) { checks++; }
|
|
try {
|
|
new MaterialBaselinePolicy(Map.of("minecraft:iron_ore", Double.NaN));
|
|
throw new AssertionError("NaN accepted");
|
|
} catch (IllegalArgumentException expected) { checks++; }
|
|
try {
|
|
new MaterialBaselinePolicy(Map.of("minecraft:coal_ore", 1.0));
|
|
throw new AssertionError("Missing iron accepted");
|
|
} catch (IllegalArgumentException expected) { checks++; }
|
|
System.out.println("BASELINE_DEFAULTS_PASS version=" + args[2] + " checks=" + checks);
|
|
}
|
|
}
|
|
"""
|
|
|
|
def main(source_dir):
|
|
gson = next((Path.home() / ".gradle/caches/modules-2/files-2.1/com.google.code.gson/gson").rglob("gson-*.jar"))
|
|
output = ROOT / "build/verification/baseline115/defaults.txt"
|
|
output.parent.mkdir(parents=True, exist_ok=True)
|
|
lines = []
|
|
with tempfile.TemporaryDirectory(prefix="rivencraft-baseline-policy-") as temp:
|
|
work = Path(temp)
|
|
fixture = work / "BaselineDefaultsChecks.java"
|
|
fixture.write_text(JAVA)
|
|
subprocess.run(["javac", "-cp", str(gson), "-d", temp,
|
|
str(ROOT / "shared/src/main/java/net/dnorberg/rivencraft/shared/MaterialBaselinePolicy.java"),
|
|
str(fixture)], check=True)
|
|
for version, resources, iron, coal in [
|
|
("1.21.1", ROOT / "src/main/resources", 89.96656534954407, 170.09625126646404),
|
|
("26.3", ROOT / "ports/mc263/src/main/resources", 76.5361328125, 95.7587890625),
|
|
]:
|
|
bundled = json.loads((resources / "assets/rivencraft/instability/default_block_baseline.json").read_text())
|
|
if source_dir:
|
|
expected = generate(source_dir / ("baseline-" + version + ".json"), version)
|
|
assert bundled == expected, "Bundled averages differ from source chunk means: " + version
|
|
lines.append("SOURCE_AVERAGES_PASS version=" + version + " chunks=" + str(bundled["source_sample_count"]))
|
|
result = subprocess.run(["java", "-cp", ":".join(map(str, [work, gson, resources])),
|
|
"BaselineDefaultsChecks", str(iron), str(coal), version], check=True, capture_output=True, text=True)
|
|
lines.append(result.stdout.strip())
|
|
output.write_text("\n".join(lines) + "\n")
|
|
print("\n".join(lines))
|
|
|
|
if __name__ == "__main__":
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
parser.add_argument("--source-dir", type=Path, help="Optionally verify each bundled mean against the original JSON exports")
|
|
main(parser.parse_args().source_dir)
|