diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index bbb8b3b84..5d5e285c5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -83,7 +83,7 @@ jobs: - name: The harnesses this change can affect env: PARITY_HOSTS: '' - run: python3 tools/parity.py --changed ${{ github.event.pull_request.base.sha }} --timeout 1500 + run: python3 tools/parity.py --changed ${{ github.event.pull_request.base.sha }} --timeout 2400 # world_spawn takes ~1480 s on a busy runner - name: Logs of the failed harnesses if: failure() uses: actions/upload-artifact@v4 diff --git a/benchmarks/shared-json-buffer-lifetime-20261017.json b/benchmarks/shared-json-buffer-lifetime-20261017.json new file mode 100644 index 000000000..d618906ed --- /dev/null +++ b/benchmarks/shared-json-buffer-lifetime-20261017.json @@ -0,0 +1,379 @@ +{ + "scope": "Assets + BlockFacts + Behaviours loader only; no full startup or first frame", + "resources": "/srv/bob-data/horizon/.local/horizon-client/26.3", + "repeats": 10, + "ordering": [ + [ + "released", + "retained" + ], + [ + "retained", + "released" + ], + [ + "released", + "retained" + ], + [ + "retained", + "released" + ], + [ + "released", + "retained" + ], + [ + "retained", + "released" + ], + [ + "released", + "retained" + ], + [ + "retained", + "released" + ], + [ + "released", + "retained" + ], + [ + "retained", + "released" + ] + ], + "load_average_before": [ + 8.2421875, + 6.56396484375, + 6.39208984375 + ], + "load_average_after": [ + 7.98095703125, + 6.6455078125, + 6.4248046875 + ], + "executables": { + "released": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-integrated", + "mode": "deferred", + "sha256": "a52d6a7efc7b3198ef0dd8150325f56a3fe5d0c5883fd6d14c4b0c281866ce7c" + }, + "retained": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-integrated", + "mode": "deferred-held", + "sha256": "a52d6a7efc7b3198ef0dd8150325f56a3fe5d0c5883fd6d14c4b0c281866ce7c" + } + }, + "metrics": { + "released": { + "phase_ms": { + "median": 482.591, + "p25": 444.276, + "p75": 586.92, + "min": 430.128, + "max": 736.543, + "samples": [ + 529.244, + 606.146, + 449.23, + 442.624, + 669.108, + 511.519, + 736.543, + 432.616, + 430.128, + 453.663 + ] + }, + "total_ms": { + "median": 987.004, + "p25": 932.885, + "p75": 1134.876, + "min": 914.242, + "max": 1201.759, + "samples": [ + 1055.119, + 1161.462, + 968.629, + 914.242, + 1201.759, + 1005.379, + 1200.01, + 928.249, + 935.775, + 931.921 + ] + }, + "cpu_ms": { + "median": 1004.675, + "p25": 936.465, + "p75": 1116.791, + "min": 908.327, + "max": 1241.168, + "samples": [ + 1074.425, + 1130.913, + 984.832, + 931.205, + 1186.169, + 1024.519, + 1241.168, + 908.327, + 952.245, + 927.15 + ] + }, + "peak_rss_kib": { + "median": 268124.0, + "p25": 267753.0, + "p75": 268455.0, + "min": 267708, + "max": 268700, + "samples": [ + 268268, + 268500, + 267744, + 267724, + 267708, + 267780, + 268700, + 268496, + 268332, + 267980 + ] + }, + "behaviour_json_ms": { + "median": 24.371, + "p25": 22.469, + "p75": 28.486, + "min": 20.662, + "max": 35.297, + "samples": [ + 33.387, + 35.297, + 24.732, + 24.011, + 27.699, + 28.748, + 23.623, + 22.084, + 20.662, + 21.266 + ] + }, + "facts_ms": { + "median": 292.77, + "p25": 270.649, + "p75": 383.193, + "min": 264.691, + "max": 443.291, + "samples": [ + 346.613, + 398.468, + 274.673, + 264.691, + 443.291, + 292.926, + 395.387, + 267.232, + 269.308, + 292.615 + ] + }, + "behaviours_ms": { + "median": 148.635, + "p25": 141.329, + "p75": 181.741, + "min": 136.361, + "max": 307.004, + "samples": [ + 145.009, + 168.982, + 146.535, + 150.736, + 194.604, + 185.994, + 307.004, + 140.102, + 136.785, + 136.361 + ] + }, + "post_rss_kib": { + "median": 268668.0, + "p25": 268246.0, + "p75": 268922.0, + "min": 268048.0, + "max": 269012.0, + "samples": [ + 268668.0, + 268936.0, + 268108.0, + 268088.0, + 268048.0, + 268668.0, + 268880.0, + 269012.0, + 268960.0, + 268660.0 + ] + } + }, + "retained": { + "phase_ms": { + "median": 487.954, + "p25": 471.456, + "p75": 531.586, + "min": 401.516, + "max": 834.154, + "samples": [ + 486.974, + 613.755, + 540.535, + 470.333, + 488.934, + 474.825, + 834.154, + 504.74, + 401.516, + 440.381 + ] + }, + "total_ms": { + "median": 1038.875, + "p25": 989.724, + "p75": 1079.197, + "min": 880.933, + "max": 1746.386, + "samples": [ + 996.407, + 1067.467, + 1082.69, + 1103.131, + 1010.283, + 1068.719, + 1746.386, + 987.496, + 890.46, + 880.933 + ] + }, + "cpu_ms": { + "median": 1035.537, + "p25": 982.276, + "p75": 1082.698, + "min": 908.774, + "max": 1660.068, + "samples": [ + 1011.712, + 1087.688, + 1076.523, + 1084.757, + 998.654, + 1059.363, + 1660.068, + 976.817, + 912.849, + 908.774 + ] + }, + "peak_rss_kib": { + "median": 268150.0, + "p25": 267537.0, + "p75": 268614.0, + "min": 267268, + "max": 268848, + "samples": [ + 268748, + 267968, + 267268, + 268332, + 267540, + 268560, + 267492, + 268632, + 267536, + 268848 + ] + }, + "behaviour_json_ms": { + "median": 23.208, + "p25": 22.305, + "p75": 26.544, + "min": 20.059, + "max": 32.669, + "samples": [ + 32.669, + 22.62, + 22.2, + 27.331, + 25.088, + 27.029, + 23.488, + 20.977, + 22.928, + 20.059 + ] + }, + "facts_ms": { + "median": 304.44, + "p25": 291.562, + "p75": 330.991, + "min": 244.758, + "max": 384.634, + "samples": [ + 307.936, + 374.145, + 336.931, + 300.944, + 290.81, + 293.817, + 384.634, + 313.171, + 244.758, + 279.303 + ] + }, + "behaviours_ms": { + "median": 162.285, + "p25": 143.135, + "p75": 179.31, + "min": 133.83, + "max": 426.031, + "samples": [ + 146.367, + 216.988, + 181.402, + 142.058, + 173.036, + 153.978, + 426.031, + 170.592, + 133.83, + 141.017 + ] + }, + "post_rss_kib": { + "median": 268694.0, + "p25": 268116.0, + "p75": 269056.0, + "min": 267988.0, + "max": 269212.0, + "samples": [ + 269212.0, + 268564.0, + 268096.0, + 268824.0, + 268176.0, + 269032.0, + 267988.0, + 269064.0, + 268044.0, + 269120.0 + ] + } + } + } +} diff --git a/benchmarks/shared-json-decoder-phases-20261017.json b/benchmarks/shared-json-decoder-phases-20261017.json new file mode 100644 index 000000000..8c5880124 --- /dev/null +++ b/benchmarks/shared-json-decoder-phases-20261017.json @@ -0,0 +1,176 @@ +{ + "scope": "Isolated gameplay-facts.json validation/typed decoding; includes JSON file read", + "resources": "/srv/bob-data/horizon/.local/horizon-client/26.3", + "repeats": 10, + "load_averages_before_after": [ + [ + 14.7451171875, + 8.8994140625, + 6.89599609375 + ], + [ + 14.7451171875, + 8.8994140625, + 6.89599609375 + ] + ], + "binary_sha256": "c2be9140c792720499d70c5806b8def0472734264ad72486d01365447e0cc2d7", + "metrics": { + "validate": { + "phase_ms": { + "median": 117.417, + "p25": 114.101, + "p75": 158.855, + "min": 109.199, + "max": 173.408, + "samples": [ + 109.199, + 113.958, + 110.195, + 118.987, + 171.275, + 124.701, + 170.24, + 173.408, + 115.848, + 114.531 + ] + }, + "total_ms": { + "median": 139.933, + "p25": 133.863, + "p75": 193.565, + "min": 129.75, + "max": 276.311, + "samples": [ + 276.311, + 135.448, + 129.75, + 138.452, + 195.127, + 141.413, + 188.88, + 199.802, + 133.335, + 132.094 + ] + }, + "cpu_ms": { + "median": 138.849, + "p25": 134.285, + "p75": 168.881, + "min": 129.089, + "max": 190.82, + "samples": [ + 140.962, + 136.06, + 129.089, + 139.059, + 189.478, + 138.639, + 178.187, + 190.82, + 133.693, + 132.716 + ] + }, + "peak_rss_kib": { + "median": 56654.0, + "p25": 56642.0, + "p75": 56704.0, + "min": 56616, + "max": 56724, + "samples": [ + 56620, + 56656, + 56640, + 56720, + 56616, + 56724, + 56652, + 56720, + 56648, + 56656 + ] + } + }, + "typed": { + "phase_ms": { + "median": 115.873, + "p25": 109.422, + "p75": 129.67, + "min": 100.872, + "max": 183.923, + "samples": [ + 116.965, + 132.071, + 100.872, + 105.098, + 122.468, + 170.777, + 107.997, + 183.923, + 113.699, + 114.781 + ] + }, + "total_ms": { + "median": 133.883, + "p25": 126.939, + "p75": 155.913, + "min": 117.54, + "max": 210.164, + "samples": [ + 135.04, + 159.38, + 117.54, + 122.391, + 145.512, + 189.132, + 125.846, + 210.164, + 130.217, + 132.727 + ] + }, + "cpu_ms": { + "median": 134.375, + "p25": 126.231, + "p75": 154.675, + "min": 118.132, + "max": 204.242, + "samples": [ + 135.633, + 157.961, + 118.132, + 122.77, + 144.816, + 178.293, + 124.755, + 204.242, + 130.661, + 133.117 + ] + }, + "peak_rss_kib": { + "median": 83974.0, + "p25": 82744.0, + "p75": 84490.0, + "min": 82332, + "max": 84596, + "samples": [ + 83596, + 84596, + 82332, + 82424, + 84288, + 82460, + 84532, + 83660, + 84364, + 84536 + ] + } + } + } +} diff --git a/benchmarks/shared-json-five-way-20261017.json b/benchmarks/shared-json-five-way-20261017.json new file mode 100644 index 000000000..61dcb7c3d --- /dev/null +++ b/benchmarks/shared-json-five-way-20261017.json @@ -0,0 +1,453 @@ +{ + "scope": "Assets + BlockFacts + Behaviours loader only; no full startup or first frame", + "resources": "/srv/bob-data/horizon/.local/horizon-client/26.3", + "repeats": 8, + "ordering": [ + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ], + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ], + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ], + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ] + ], + "load_average_before": [ + 10.57958984375, + 8.45947265625, + 6.8134765625 + ], + "load_average_after": [ + 7.158203125, + 7.7998046875, + 6.66455078125 + ], + "executables": { + "baseline": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-e422bd13", + "mode": "baseline", + "sha256": "bf61f2e6c92e001be3c3bc93cf8072cc6af6fd1d7bcfa7273045960948ec89e7" + }, + "a01d47f2": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-a01d47f2", + "mode": "shared", + "sha256": "88be56f8dbd28d0b4abd6c672486ba758df96ba18f68da45b94db14231dd3eea" + }, + "cf837118": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-cf837118", + "mode": "deferred", + "sha256": "f1b044cd9ac2f67c644fb40bd7ecbe38128ec739a8112d57fc36f05fe9036a52" + }, + "cd61e11b": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-cd61e11b", + "mode": "deferred", + "sha256": "fb2d3725b09ee38f85b49901f7053a6fe339b980a2b2dac6930a8ded22c139e8" + }, + "current": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-new", + "mode": "deferred", + "sha256": "84eb1baae8e138f52e300b0b44f1339fca38e70db6e06126e78db776ea3e5cd0" + } + }, + "metrics": { + "baseline": { + "phase_ms": { + "median": 1277.781, + "p25": 1271.23, + "p75": 1296.157, + "min": 1223.341, + "max": 1338.745, + "samples": [ + 1265.934, + 1223.341, + 1274.931, + 1289.177, + 1338.745, + 1317.096, + 1272.995, + 1280.631 + ] + }, + "total_ms": { + "median": 1711.802, + "p25": 1695.745, + "p75": 1754.276, + "min": 1645.69, + "max": 2887.078, + "samples": [ + 2887.078, + 1645.69, + 1690.413, + 1711.459, + 1761.859, + 1751.748, + 1697.522, + 1712.144 + ] + }, + "cpu_ms": { + "median": 1769.996, + "p25": 1748.925, + "p75": 1807.254, + "min": 1705.167, + "max": 1950.463, + "samples": [ + 1950.463, + 1705.167, + 1749.023, + 1768.05, + 1804.543, + 1815.386, + 1771.942, + 1748.632 + ] + }, + "peak_rss_kib": { + "median": 820180.0, + "p25": 820030.0, + "p75": 820502.0, + "min": 819472, + "max": 820884, + "samples": [ + 819472, + 820416, + 820060, + 820096, + 820264, + 820884, + 819940, + 820760 + ] + } + }, + "a01d47f2": { + "phase_ms": { + "median": 430.783, + "p25": 427.542, + "p75": 451.763, + "min": 423.035, + "max": 550.345, + "samples": [ + 491.599, + 423.035, + 428.85, + 427.694, + 432.715, + 427.084, + 438.484, + 550.345 + ] + }, + "total_ms": { + "median": 868.457, + "p25": 863.709, + "p75": 932.066, + "min": 837.75, + "max": 997.779, + "samples": [ + 911.935, + 837.75, + 865.419, + 868.626, + 858.578, + 997.779, + 868.288, + 992.46 + ] + }, + "cpu_ms": { + "median": 897.639, + "p25": 887.661, + "p75": 944.363, + "min": 865.497, + "max": 1001.635, + "samples": [ + 925.321, + 865.497, + 899.505, + 895.774, + 876.929, + 1001.635, + 891.239, + 1001.488 + ] + }, + "peak_rss_kib": { + "median": 351906.0, + "p25": 351505.0, + "p75": 352181.0, + "min": 351176, + "max": 352440, + "samples": [ + 352008, + 352120, + 351352, + 351556, + 351804, + 351176, + 352440, + 352364 + ] + } + }, + "cf837118": { + "phase_ms": { + "median": 408.459, + "p25": 398.987, + "p75": 428.585, + "min": 387.999, + "max": 524.425, + "samples": [ + 433.521, + 387.999, + 412.195, + 397.366, + 404.724, + 399.527, + 426.939, + 524.425 + ] + }, + "total_ms": { + "median": 851.294, + "p25": 823.291, + "p75": 890.55, + "min": 801.356, + "max": 1030.737, + "samples": [ + 892.054, + 801.356, + 840.381, + 818.285, + 862.208, + 824.96, + 890.049, + 1030.737 + ] + }, + "cpu_ms": { + "median": 870.765, + "p25": 850.849, + "p75": 907.922, + "min": 825.718, + "max": 1041.996, + "samples": [ + 906.657, + 825.718, + 866.472, + 843.591, + 875.058, + 853.268, + 911.715, + 1041.996 + ] + }, + "peak_rss_kib": { + "median": 274172.0, + "p25": 273909.0, + "p75": 274438.0, + "min": 273668, + "max": 274964, + "samples": [ + 274372, + 274964, + 273668, + 273972, + 274552, + 274400, + 273840, + 273932 + ] + } + }, + "cd61e11b": { + "phase_ms": { + "median": 419.114, + "p25": 404.191, + "p75": 460.634, + "min": 388.557, + "max": 735.687, + "samples": [ + 457.971, + 418.303, + 388.557, + 405.041, + 419.925, + 401.64, + 468.624, + 735.687 + ] + }, + "total_ms": { + "median": 840.723, + "p25": 835.398, + "p75": 917.995, + "min": 811.43, + "max": 1565.471, + "samples": [ + 906.328, + 837.706, + 811.43, + 835.727, + 843.74, + 834.412, + 952.996, + 1565.471 + ] + }, + "cpu_ms": { + "median": 863.404, + "p25": 858.916, + "p75": 934.2, + "min": 833.955, + "max": 1550.132, + "samples": [ + 916.017, + 864.82, + 833.955, + 859.057, + 861.989, + 858.494, + 988.748, + 1550.132 + ] + }, + "peak_rss_kib": { + "median": 267508.0, + "p25": 267310.0, + "p75": 267890.0, + "min": 267172, + "max": 268412, + "samples": [ + 267304, + 268292, + 267312, + 267756, + 267340, + 267676, + 267172, + 268412 + ] + } + }, + "current": { + "phase_ms": { + "median": 406.779, + "p25": 397.839, + "p75": 413.957, + "min": 385.028, + "max": 552.051, + "samples": [ + 387.136, + 385.028, + 409.317, + 404.242, + 401.407, + 411.451, + 421.476, + 552.051 + ] + }, + "total_ms": { + "median": 819.787, + "p25": 814.211, + "p75": 855.69, + "min": 800.867, + "max": 1128.453, + "samples": [ + 808.685, + 800.867, + 816.053, + 817.907, + 821.667, + 843.26, + 892.982, + 1128.453 + ] + }, + "cpu_ms": { + "median": 847.078, + "p25": 836.598, + "p75": 877.524, + "min": 826.002, + "max": 1134.369, + "samples": [ + 834.665, + 826.002, + 837.243, + 842.86, + 851.296, + 864.892, + 915.419, + 1134.369 + ] + }, + "peak_rss_kib": { + "median": 267956.0, + "p25": 267612.0, + "p75": 268326.0, + "min": 267308, + "max": 268428, + "samples": [ + 268428, + 267308, + 268260, + 267516, + 267652, + 268312, + 268368, + 267644 + ] + } + } + }, + "experiment_note": "Experimental projection variant (reused sounds, Arc-keyed string pools and direct flags); reverted after mixed CPU/RSS evidence. Current in this file is not the final PR version." +} diff --git a/benchmarks/shared-json-five-way-final-20261017.json b/benchmarks/shared-json-five-way-final-20261017.json new file mode 100644 index 000000000..ce92b7b61 --- /dev/null +++ b/benchmarks/shared-json-five-way-final-20261017.json @@ -0,0 +1,582 @@ +{ + "scope": "Assets + BlockFacts + Behaviours loader only; no full startup or first frame", + "resources": "/srv/bob-data/horizon/.local/horizon-client/26.3", + "repeats": 10, + "ordering": [ + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ], + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ], + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ], + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ], + [ + "baseline", + "a01d47f2", + "cf837118", + "cd61e11b", + "current" + ], + [ + "current", + "cd61e11b", + "cf837118", + "a01d47f2", + "baseline" + ] + ], + "load_average_before": [ + 11.4921875, + 7.46875, + 6.69677734375 + ], + "load_average_after": [ + 17.763671875, + 11.39013671875, + 8.20556640625 + ], + "executables": { + "baseline": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-e422bd13", + "mode": "baseline", + "sha256": "bf61f2e6c92e001be3c3bc93cf8072cc6af6fd1d7bcfa7273045960948ec89e7" + }, + "a01d47f2": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-a01d47f2", + "mode": "shared", + "sha256": "88be56f8dbd28d0b4abd6c672486ba758df96ba18f68da45b94db14231dd3eea" + }, + "cf837118": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-cf837118", + "mode": "deferred", + "sha256": "f1b044cd9ac2f67c644fb40bd7ecbe38128ec739a8112d57fc36f05fe9036a52" + }, + "cd61e11b": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-cd61e11b", + "mode": "deferred", + "sha256": "fb2d3725b09ee38f85b49901f7053a6fe339b980a2b2dac6930a8ded22c139e8" + }, + "current": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-integrated", + "mode": "deferred", + "sha256": "a52d6a7efc7b3198ef0dd8150325f56a3fe5d0c5883fd6d14c4b0c281866ce7c" + } + }, + "metrics": { + "baseline": { + "phase_ms": { + "median": 1506.588, + "p25": 1388.314, + "p75": 2119.85, + "min": 1319.319, + "max": 3143.124, + "samples": [ + 3143.124, + 2483.307, + 2124.952, + 2104.544, + 1525.153, + 1396.75, + 1381.771, + 1488.022, + 1385.502, + 1319.319 + ] + }, + "total_ms": { + "median": 1962.122, + "p25": 1858.188, + "p75": 3152.608, + "min": 1804.713, + "max": 4136.912, + "samples": [ + 4136.912, + 3250.285, + 3134.13, + 3158.767, + 2001.229, + 1883.076, + 1849.892, + 1923.016, + 1818.34, + 1804.713 + ] + }, + "cpu_ms": { + "median": 1969.296, + "p25": 1893.348, + "p75": 2694.824, + "min": 1842.049, + "max": 3059.824, + "samples": [ + 3059.824, + 2765.392, + 2698.837, + 2682.786, + 2026.89, + 1911.703, + 1888.282, + 1908.545, + 1859.233, + 1842.049 + ] + }, + "peak_rss_kib": { + "median": 819952.0, + "p25": 819778.0, + "p75": 820164.0, + "min": 818944, + "max": 820304, + "samples": [ + 819956, + 819744, + 818944, + 818984, + 820128, + 820176, + 819948, + 820236, + 819880, + 820304 + ] + } + }, + "a01d47f2": { + "phase_ms": { + "median": 666.765, + "p25": 520.278, + "p75": 1156.022, + "min": 450.677, + "max": 1882.249, + "samples": [ + 1001.208, + 1309.911, + 1207.627, + 1882.249, + 747.233, + 544.933, + 512.06, + 450.677, + 466.596, + 586.298 + ] + }, + "total_ms": { + "median": 1141.968, + "p25": 974.723, + "p75": 2326.553, + "min": 922.496, + "max": 3158.114, + "samples": [ + 2374.699, + 2275.18, + 2343.677, + 3158.114, + 1267.594, + 1006.411, + 964.16, + 922.496, + 936.687, + 1016.342 + ] + }, + "cpu_ms": { + "median": 1106.901, + "p25": 973.629, + "p75": 1525.998, + "min": 929.573, + "max": 1568.573, + "samples": [ + 1545.278, + 1568.573, + 1468.158, + 1563.377, + 1196.236, + 1017.565, + 960.219, + 929.573, + 946.096, + 1013.861 + ] + }, + "peak_rss_kib": { + "median": 351564.0, + "p25": 351148.0, + "p75": 352106.0, + "min": 350416, + "max": 353368, + "samples": [ + 351572, + 351268, + 351108, + 350416, + 351656, + 353368, + 352256, + 351556, + 350964, + 352308 + ] + } + }, + "cf837118": { + "phase_ms": { + "median": 505.124, + "p25": 435.315, + "p75": 1103.87, + "min": 417.056, + "max": 1875.147, + "samples": [ + 1875.147, + 1129.722, + 1066.305, + 1116.391, + 461.019, + 423.38, + 417.056, + 445.353, + 549.229, + 431.969 + ] + }, + "total_ms": { + "median": 1209.375, + "p25": 895.862, + "p75": 2133.144, + "min": 872.434, + "max": 2936.234, + "samples": [ + 2936.234, + 2215.538, + 1885.964, + 2695.131, + 1378.495, + 879.351, + 897.968, + 895.16, + 1040.255, + 872.434 + ] + }, + "cpu_ms": { + "median": 1068.41, + "p25": 896.926, + "p75": 1488.578, + "min": 882.398, + "max": 1570.652, + "samples": [ + 1530.601, + 1520.851, + 1391.76, + 1570.652, + 1116.403, + 900.779, + 894.039, + 895.642, + 1020.417, + 882.398 + ] + }, + "peak_rss_kib": { + "median": 273832.0, + "p25": 273592.0, + "p75": 274131.0, + "min": 272732, + "max": 274976, + "samples": [ + 274080, + 272732, + 273560, + 273468, + 274200, + 274148, + 273728, + 273936, + 274976, + 273688 + ] + } + }, + "cd61e11b": { + "phase_ms": { + "median": 429.481, + "p25": 410.043, + "p75": 1178.091, + "min": 399.576, + "max": 1644.881, + "samples": [ + 1194.624, + 1128.491, + 1644.881, + 1428.78, + 405.544, + 434.62, + 424.341, + 414.829, + 408.447, + 399.576 + ] + }, + "total_ms": { + "median": 902.865, + "p25": 868.889, + "p75": 2443.447, + "min": 827.16, + "max": 2961.261, + "samples": [ + 2682.056, + 2199.278, + 2524.837, + 2961.261, + 884.838, + 885.282, + 848.79, + 863.573, + 920.448, + 827.16 + ] + }, + "cpu_ms": { + "median": 920.917, + "p25": 869.777, + "p75": 1475.629, + "min": 840.036, + "max": 1626.459, + "samples": [ + 1518.201, + 1496.456, + 1413.149, + 1626.459, + 893.762, + 899.049, + 861.782, + 861.417, + 942.785, + 840.036 + ] + }, + "peak_rss_kib": { + "median": 267762.0, + "p25": 267612.0, + "p75": 267966.0, + "min": 266300, + "max": 268636, + "samples": [ + 266672, + 268636, + 266300, + 267596, + 267660, + 268280, + 267968, + 267680, + 267960, + 267844 + ] + } + }, + "current": { + "phase_ms": { + "median": 460.489, + "p25": 429.004, + "p75": 986.935, + "min": 404.103, + "max": 1300.033, + "samples": [ + 992.891, + 1126.098, + 1300.033, + 969.067, + 443.604, + 408.397, + 472.429, + 424.137, + 448.549, + 404.103 + ] + }, + "total_ms": { + "median": 935.623, + "p25": 874.18, + "p75": 2282.679, + "min": 839.642, + "max": 2965.198, + "samples": [ + 2320.566, + 2169.019, + 2965.198, + 2531.591, + 924.347, + 839.642, + 946.899, + 873.617, + 875.87, + 854.328 + ] + }, + "cpu_ms": { + "median": 950.702, + "p25": 892.992, + "p75": 1540.918, + "min": 861.669, + "max": 1586.478, + "samples": [ + 1557.765, + 1503.564, + 1553.369, + 1586.478, + 931.391, + 861.669, + 970.013, + 888.14, + 907.549, + 868.73 + ] + }, + "peak_rss_kib": { + "median": 268010.0, + "p25": 267013.0, + "p75": 268385.0, + "min": 266144, + "max": 268876, + "samples": [ + 266824, + 267580, + 266516, + 266144, + 267976, + 268304, + 268044, + 268480, + 268412, + 268876 + ] + }, + "behaviour_json_ms": { + "median": 34.288, + "p25": 23.085, + "p75": 37.474, + "min": 20.137, + "max": 70.397, + "samples": [ + 37.07, + 70.397, + 37.609, + 38.225, + 34.568, + 21.094, + 23.469, + 22.957, + 34.008, + 20.137 + ] + }, + "facts_ms": { + "median": 272.839, + "p25": 256.553, + "p75": 618.472, + "min": 248.146, + "max": 780.388, + "samples": [ + 625.908, + 693.735, + 780.388, + 596.165, + 272.718, + 249.954, + 272.959, + 264.913, + 253.767, + 248.146 + ] + }, + "behaviours_ms": { + "median": 162.633, + "p25": 133.345, + "p75": 326.117, + "min": 132.828, + "max": 476.9, + "samples": [ + 325.198, + 356.28, + 476.9, + 326.424, + 132.828, + 133.946, + 167.929, + 133.144, + 157.337, + 132.934 + ] + }, + "post_rss_kib": { + "median": 268408.0, + "p25": 267369.0, + "p75": 268861.0, + "min": 267052.0, + "max": 269072.0, + "samples": [ + 267084.0, + 268056.0, + 267052.0, + 267140.0, + 268184.0, + 268632.0, + 268660.0, + 269072.0, + 268928.0, + 268960.0 + ] + } + } + } +} diff --git a/benchmarks/shared-json-historical-20261017.json b/benchmarks/shared-json-historical-20261017.json new file mode 100644 index 000000000..2e0e7538f --- /dev/null +++ b/benchmarks/shared-json-historical-20261017.json @@ -0,0 +1,188 @@ +{ + "scope": "Assets + block facts + behaviours loader only; excludes full startup", + "resources": ".local/horizon-client/26.3", + "repeats": 5, + "comparison": "historical cf837118 release binary", + "reference_binary": "target/release/examples/shared_json_startup_profile-cf837118", + "metrics": { + "baseline": { + "phase_ms": { + "median": 1462.003, + "samples": [ + 1639.999, + 1506.864, + 1429.844, + 1462.003, + 1346.061 + ] + }, + "total_ms": { + "median": 1922.795, + "samples": [ + 3383.516, + 1990.554, + 1912.354, + 1922.795, + 1777.912 + ] + }, + "cpu_ms": { + "median": 1961.459, + "samples": [ + 2419.086, + 2021.457, + 1951.587, + 1961.459, + 1839.984 + ] + }, + "peak_rss_kib": { + "median": 820912, + "samples": [ + 819956, + 821052, + 820912, + 821792, + 820424 + ] + } + }, + "pr607": { + "phase_ms": { + "median": 486.715, + "samples": [ + 460.738, + 442.013, + 623.053, + 486.715, + 550.652 + ] + }, + "total_ms": { + "median": 948.089, + "samples": [ + 923.057, + 935.599, + 1109.64, + 948.089, + 1052.546 + ] + }, + "cpu_ms": { + "median": 968.424, + "samples": [ + 928.489, + 946.932, + 1128.431, + 968.424, + 1079.642 + ] + }, + "peak_rss_kib": { + "median": 351796, + "samples": [ + 351796, + 351844, + 351536, + 351532, + 351988 + ] + } + }, + "cf837118": { + "phase_ms": { + "median": 474.268, + "samples": [ + 443.122, + 474.268, + 520.497, + 433.525, + 532.058 + ] + }, + "total_ms": { + "median": 945.87, + "samples": [ + 934.722, + 945.87, + 1006.539, + 888.201, + 1032.6 + ] + }, + "cpu_ms": { + "median": 953.668, + "samples": [ + 941.957, + 953.668, + 1029.122, + 903.574, + 1065.025 + ] + }, + "peak_rss_kib": { + "median": 274476, + "samples": [ + 273448, + 274844, + 274476, + 274556, + 274252 + ] + } + }, + "current": { + "phase_ms": { + "median": 445.924, + "samples": [ + 472.165, + 427.797, + 419.288, + 445.924, + 524.211 + ] + }, + "total_ms": { + "median": 999.669, + "samples": [ + 999.669, + 892.616, + 1051.427, + 940.595, + 1112.158 + ] + }, + "cpu_ms": { + "median": 1001.24, + "samples": [ + 1001.24, + 887.874, + 1051.732, + 951.495, + 1132.208 + ] + }, + "peak_rss_kib": { + "median": 267604, + "samples": [ + 267716, + 267920, + 267448, + 267604, + 267344 + ] + } + } + }, + "builds": { + "current": "release example built at cf837118 + current uncommitted delta in PR #607 worktree", + "historical": "release example built at exact cf837118 commit, with same Cargo release profile and shared dependency target" + }, + "mode_commits": { + "baseline": "independent loader algorithm at e422bd13, executed in current binary", + "pr607": "shared early construction algorithm at a01d47f2, executed in current binary", + "cf837118": "exact historical binary at cf837118, deferred mode", + "current": "current PR worktree, deferred mode with interned strings" + }, + "limitations": "Loader-only (Assets + BlockFacts + Behaviours), no full client startup or first-frame timing. Same release profile but different compilation contexts for historical binary; hardware activity and cache warmth vary. CPU and elapsed differences between cf837118 and current are mixed and may be noise. Peak RSS is whole-process Linux ru_maxrss." +} diff --git a/benchmarks/shared-json-startup-20261017.json b/benchmarks/shared-json-startup-20261017.json new file mode 100644 index 000000000..92f557b33 --- /dev/null +++ b/benchmarks/shared-json-startup-20261017.json @@ -0,0 +1,93 @@ +{ + "scope": "Assets + block facts + behaviours loader only; excludes full startup", + "resources": ".local/horizon-client/26.3", + "repeats": 5, + "metrics": { + "baseline": { + "phase_ms": { + "median": 4730.402, + "samples": [ + 3258.541, + 6539.764, + 4773.735, + 3980.032, + 4730.402 + ] + }, + "total_ms": { + "median": 6325.026, + "samples": [ + 6596.141, + 7627.893, + 6296.443, + 5435.408, + 6325.026 + ] + }, + "cpu_ms": { + "median": 3199.722, + "samples": [ + 3322.801, + 3199.722, + 3227.438, + 3042.687, + 3144.658 + ] + }, + "peak_rss_kib": { + "median": 820000, + "samples": [ + 818540, + 820400, + 819352, + 820000, + 820128 + ] + } + }, + "shared": { + "phase_ms": { + "median": 1180.588, + "samples": [ + 869.228, + 842.213, + 1180.588, + 1560.087, + 1728.575 + ] + }, + "total_ms": { + "median": 2260.709, + "samples": [ + 1632.489, + 1553.753, + 2260.709, + 2879.728, + 3388.639 + ] + }, + "cpu_ms": { + "median": 1603.393, + "samples": [ + 1568.288, + 1432.106, + 1603.393, + 1604.423, + 1619.02 + ] + }, + "peak_rss_kib": { + "median": 351728, + "samples": [ + 353256, + 353664, + 351728, + 350724, + 351668 + ] + } + } + }, + "build": "cargo build --release -p horizon-client --example shared_json_startup_profile", + "limitations": "Loader-only wall-clock timings under concurrent machine load; not full startup or time to first frame. RSS includes Assets::load." +} diff --git a/benchmarks/shared-json-startup-followup-20261017.json b/benchmarks/shared-json-startup-followup-20261017.json new file mode 100644 index 000000000..c828ac7b4 --- /dev/null +++ b/benchmarks/shared-json-startup-followup-20261017.json @@ -0,0 +1,227 @@ +{ + "scope": "Assets + block facts + behaviours loader only; excludes full startup", + "resources": ".local/horizon-client/26.3", + "repeats": 5, + "metrics": { + "baseline": { + "phase_ms": { + "median": 1996.921, + "samples": [ + 2213.335, + 1453.207, + 1637.901, + 1996.921, + 2010.335 + ], + "range": [ + 1453.207, + 2213.335 + ] + }, + "total_ms": { + "median": 4728.136, + "samples": [ + 5628.975, + 2147.329, + 2162.696, + 4728.136, + 6404.524 + ], + "range": [ + 2147.329, + 6404.524 + ] + }, + "cpu_ms": { + "median": 2514.776, + "samples": [ + 2919.908, + 2088.239, + 2212.892, + 2716.104, + 2514.776 + ], + "range": [ + 2088.239, + 2919.908 + ] + }, + "peak_rss_kib": { + "median": 819824, + "samples": [ + 819824, + 820872, + 820744, + 818980, + 818816 + ] + } + }, + "pr607": { + "phase_ms": { + "median": 586.707, + "samples": [ + 464.424, + 739.67, + 586.707, + 1373.307, + 509.266 + ], + "range": [ + 464.424, + 1373.307 + ] + }, + "total_ms": { + "median": 1128.6, + "samples": [ + 954.73, + 1764.614, + 1128.6, + 4312.528, + 1030.735 + ], + "range": [ + 954.73, + 4312.528 + ] + }, + "cpu_ms": { + "median": 1145.826, + "samples": [ + 981.058, + 1465.79, + 1145.826, + 1736.807, + 1056.992 + ], + "range": [ + 981.058, + 1736.807 + ] + }, + "peak_rss_kib": { + "median": 351900, + "samples": [ + 352164, + 351816, + 351900, + 352348, + 350492 + ] + } + }, + "shared": { + "phase_ms": { + "median": 560.117, + "samples": [ + 522.284, + 1255.75, + 560.117, + 2151.064, + 479.329 + ], + "range": [ + 479.329, + 2151.064 + ] + }, + "total_ms": { + "median": 1101.305, + "samples": [ + 1011.715, + 2565.449, + 1101.305, + 2960.186, + 1006.817 + ], + "range": [ + 1006.817, + 2960.186 + ] + }, + "cpu_ms": { + "median": 1128.214, + "samples": [ + 1035.19, + 1565.383, + 1128.214, + 1494.36, + 1031.198 + ], + "range": [ + 1031.198, + 1565.383 + ] + }, + "peak_rss_kib": { + "median": 351532, + "samples": [ + 351624, + 350540, + 351532, + 351716, + 350076 + ] + } + }, + "deferred": { + "phase_ms": { + "median": 564.046, + "samples": [ + 433.35, + 665.149, + 587.456, + 564.046, + 497.422 + ], + "range": [ + 433.35, + 665.149 + ] + }, + "total_ms": { + "median": 1143.129, + "samples": [ + 885.053, + 1143.129, + 1195.869, + 1074.27, + 1162.103 + ], + "range": [ + 885.053, + 1195.869 + ] + }, + "cpu_ms": { + "median": 1114.261, + "samples": [ + 910.946, + 1128.96, + 1194.638, + 1114.261, + 1073.823 + ], + "range": [ + 910.946, + 1194.638 + ] + }, + "peak_rss_kib": { + "median": 273776, + "samples": [ + 274320, + 273860, + 272548, + 273776, + 272520 + ] + } + } + }, + "baseline_commit": "e422bd13", + "pr607_commit": "a01d47f2", + "measurement": "same optimized binary, isolated processes in alternating forward/reverse four-mode runs", + "limitations": "Includes Assets::load, BlockFacts, Behaviours; excludes complete client startup, pack/mod root resolution, graphics and first frame. Different modes emulate the original independent, PR #607 at a01d47f2, paired and deferred startup sequences in the same release build. Machine was not idle. Elapsed times show high variance; no statistically established gain between PR, paired and deferred modes." +} diff --git a/benchmarks/shared-json-variant-isolation-20261017.json b/benchmarks/shared-json-variant-isolation-20261017.json new file mode 100644 index 000000000..e399c7ba1 --- /dev/null +++ b/benchmarks/shared-json-variant-isolation-20261017.json @@ -0,0 +1,453 @@ +{ + "scope": "Assets + BlockFacts + Behaviours loader only; no full startup or first frame", + "resources": "/srv/bob-data/horizon/.local/horizon-client/26.3", + "repeats": 8, + "ordering": [ + [ + "cf837118", + "cd61e11b", + "arc-key", + "reused-sounds", + "current" + ], + [ + "current", + "reused-sounds", + "arc-key", + "cd61e11b", + "cf837118" + ], + [ + "cf837118", + "cd61e11b", + "arc-key", + "reused-sounds", + "current" + ], + [ + "current", + "reused-sounds", + "arc-key", + "cd61e11b", + "cf837118" + ], + [ + "cf837118", + "cd61e11b", + "arc-key", + "reused-sounds", + "current" + ], + [ + "current", + "reused-sounds", + "arc-key", + "cd61e11b", + "cf837118" + ], + [ + "cf837118", + "cd61e11b", + "arc-key", + "reused-sounds", + "current" + ], + [ + "current", + "reused-sounds", + "arc-key", + "cd61e11b", + "cf837118" + ] + ], + "load_average_before": [ + 10.9873046875, + 8.37646484375, + 6.91015625 + ], + "load_average_after": [ + 8.6962890625, + 8.1591796875, + 6.90087890625 + ], + "executables": { + "cf837118": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-cf837118", + "mode": "deferred", + "sha256": "f1b044cd9ac2f67c644fb40bd7ecbe38128ec739a8112d57fc36f05fe9036a52" + }, + "cd61e11b": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-cd61e11b", + "mode": "deferred", + "sha256": "fb2d3725b09ee38f85b49901f7053a6fe339b980a2b2dac6930a8ded22c139e8" + }, + "arc-key": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-arcpool", + "mode": "deferred", + "sha256": "8cbb53d01afedf5c7a5da0e36c9f8c3569f34def9bc51488fe8699e7f615a131" + }, + "reused-sounds": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-reusesounds", + "mode": "deferred", + "sha256": "f45794bfbdbb3f9b5d34c375d397191ac67984b3ae91268aa3fdd02f2c1b7b98" + }, + "current": { + "path": "/srv/bob-data/horizon/.worktrees/shared-block-json-20261017/target/release/examples/shared_json_startup_profile-new", + "mode": "deferred", + "sha256": "84eb1baae8e138f52e300b0b44f1339fca38e70db6e06126e78db776ea3e5cd0" + } + }, + "metrics": { + "cf837118": { + "phase_ms": { + "median": 424.082, + "p25": 417.508, + "p75": 430.923, + "min": 395.439, + "max": 948.609, + "samples": [ + 948.609, + 425.085, + 425.571, + 417.945, + 395.439, + 416.197, + 423.078, + 446.98 + ] + }, + "total_ms": { + "median": 853.026, + "p25": 839.421, + "p75": 867.731, + "min": 813.144, + "max": 1906.081, + "samples": [ + 1906.081, + 861.641, + 864.233, + 844.411, + 813.144, + 830.783, + 842.3, + 878.226 + ] + }, + "cpu_ms": { + "median": 876.143, + "p25": 862.934, + "p75": 891.4, + "min": 837.769, + "max": 1517.372, + "samples": [ + 1517.372, + 885.13, + 887.888, + 865.404, + 837.769, + 855.526, + 867.157, + 901.937 + ] + }, + "peak_rss_kib": { + "median": 273904.0, + "p25": 273717.0, + "p75": 274142.0, + "min": 273648, + "max": 274836, + "samples": [ + 273648, + 273936, + 273872, + 274836, + 274064, + 274376, + 273740, + 273648 + ] + } + }, + "cd61e11b": { + "phase_ms": { + "median": 422.356, + "p25": 406.294, + "p75": 425.846, + "min": 394.111, + "max": 472.536, + "samples": [ + 425.832, + 420.666, + 472.536, + 425.886, + 400.588, + 394.111, + 424.047, + 408.196 + ] + }, + "total_ms": { + "median": 852.475, + "p25": 828.474, + "p75": 876.413, + "min": 817.3, + "max": 951.587, + "samples": [ + 951.587, + 865.442, + 899.564, + 868.696, + 823.74, + 817.3, + 839.508, + 830.052 + ] + }, + "cpu_ms": { + "median": 875.04, + "p25": 850.012, + "p75": 900.57, + "min": 837.672, + "max": 938.285, + "samples": [ + 938.285, + 888.533, + 926.846, + 891.812, + 845.956, + 837.672, + 861.548, + 851.364 + ] + }, + "peak_rss_kib": { + "median": 268086.0, + "p25": 267719.0, + "p75": 268486.0, + "min": 267444, + "max": 268920, + "samples": [ + 267776, + 268660, + 267752, + 268920, + 268428, + 268396, + 267444, + 267620 + ] + } + }, + "arc-key": { + "phase_ms": { + "median": 421.422, + "p25": 401.781, + "p75": 451.774, + "min": 394.041, + "max": 472.337, + "samples": [ + 446.155, + 472.337, + 468.632, + 420.636, + 400.91, + 394.041, + 422.207, + 402.072 + ] + }, + "total_ms": { + "median": 866.601, + "p25": 822.563, + "p75": 918.038, + "min": 817.092, + "max": 1001.287, + "samples": [ + 1001.287, + 918.457, + 917.898, + 892.666, + 817.092, + 824.225, + 840.536, + 817.576 + ] + }, + "cpu_ms": { + "median": 887.84, + "p25": 844.533, + "p75": 937.331, + "min": 838.687, + "max": 991.212, + "samples": [ + 991.212, + 949.048, + 933.425, + 911.921, + 838.687, + 845.021, + 863.759, + 843.069 + ] + }, + "peak_rss_kib": { + "median": 267728.0, + "p25": 267598.0, + "p75": 268039.0, + "min": 267452, + "max": 268636, + "samples": [ + 268264, + 267616, + 267840, + 267964, + 268636, + 267452, + 267580, + 267604 + ] + } + }, + "reused-sounds": { + "phase_ms": { + "median": 421.683, + "p25": 412.913, + "p75": 439.098, + "min": 395.338, + "max": 487.446, + "samples": [ + 487.446, + 434.111, + 402.815, + 416.279, + 424.524, + 454.058, + 395.338, + 418.842 + ] + }, + "total_ms": { + "median": 879.846, + "p25": 845.021, + "p75": 901.166, + "min": 823.721, + "max": 1013.539, + "samples": [ + 1013.539, + 892.033, + 928.565, + 884.284, + 847.853, + 875.407, + 823.721, + 836.527 + ] + }, + "cpu_ms": { + "median": 894.546, + "p25": 871.499, + "p75": 921.844, + "min": 844.891, + "max": 1028.688, + "samples": [ + 1028.688, + 913.48, + 946.935, + 903.785, + 868.684, + 885.308, + 844.891, + 872.438 + ] + }, + "peak_rss_kib": { + "median": 267960.0, + "p25": 267678.0, + "p75": 268133.0, + "min": 267212, + "max": 268404, + "samples": [ + 267212, + 267888, + 268404, + 267692, + 268096, + 267636, + 268244, + 268032 + ] + } + }, + "current": { + "phase_ms": { + "median": 411.403, + "p25": 406.814, + "p75": 452.869, + "min": 391.768, + "max": 532.402, + "samples": [ + 532.402, + 442.563, + 408.857, + 410.202, + 483.788, + 391.768, + 400.683, + 412.604 + ] + }, + "total_ms": { + "median": 905.017, + "p25": 851.488, + "p75": 937.934, + "min": 818.206, + "max": 1075.445, + "samples": [ + 1075.445, + 878.155, + 837.627, + 955.655, + 932.027, + 818.206, + 856.108, + 931.88 + ] + }, + "cpu_ms": { + "median": 922.9, + "p25": 874.849, + "p75": 956.282, + "min": 839.639, + "max": 1067.693, + "samples": [ + 1067.693, + 900.18, + 862.137, + 970.356, + 951.591, + 839.639, + 879.086, + 945.62 + ] + }, + "peak_rss_kib": { + "median": 268354.0, + "p25": 267889.0, + "p75": 268492.0, + "min": 267056, + "max": 268820, + "samples": [ + 268008, + 268384, + 268564, + 268820, + 267056, + 267532, + 268324, + 268468 + ] + } + } + }, + "experiment_note": "Exploratory variants (Arc-key pool, reused sounds, combined proposal) alongside cd61e11b; none gave a robust consistent improvement and all were reverted." +} diff --git a/crates/horizon-client/examples/gameplay_decode_profile.rs b/crates/horizon-client/examples/gameplay_decode_profile.rs new file mode 100644 index 000000000..60260452f --- /dev/null +++ b/crates/horizon-client/examples/gameplay_decode_profile.rs @@ -0,0 +1,33 @@ +//! Profile the two mandatory passes over gameplay-facts.json independently. +//! Build with `cargo build --release -p horizon-client --example gameplay_decode_profile`. +//! Each invocation runs one phase in a separate process for CPU/RSS measurements. +#[allow(dead_code)] +#[path = "../src/blocks/decode.rs"] +mod decode; + +use std::{hint::black_box, path::Path, time::Instant}; + +// The included private decoder expects the parent crate Result alias. +type Result = std::result::Result>; + +fn main() -> Result<()> { + let mut args = std::env::args().skip(1); + let root = args.next().ok_or("expected resource directory")?; + let mode = args.next().ok_or("expected validate or typed")?; + if mode != "validate" && mode != "typed" { + return Err("expected validate or typed".into()); + } + let total = Instant::now(); + let bytes = std::fs::read(Path::new(&root).join("gameplay-facts.json"))?; + let started = Instant::now(); + if mode == "validate" { + let parsed: decode::Validate = serde_json::from_slice(&bytes)?; + black_box(parsed); + } else { + let parsed: decode::Gameplay<'_> = serde_json::from_slice(&bytes)?; + black_box(parsed); + } + println!("phase_ms={:.3}", started.elapsed().as_secs_f64() * 1000.0); + println!("total_ms={:.3}", total.elapsed().as_secs_f64() * 1000.0); + Ok(()) +} diff --git a/crates/horizon-client/examples/lan_flat.rs b/crates/horizon-client/examples/lan_flat.rs index 63ea9f136..16fc36b95 100644 --- a/crates/horizon-client/examples/lan_flat.rs +++ b/crates/horizon-client/examples/lan_flat.rs @@ -88,6 +88,7 @@ fn main() -> Result<(), Box> { seed: 0, max_players: 8, dedicated: false, + integrated: false, }, ); let mut next_entity = 100u32; diff --git a/crates/horizon-client/examples/mod_port_probe.rs b/crates/horizon-client/examples/mod_port_probe.rs new file mode 100644 index 000000000..c78285bbb --- /dev/null +++ b/crates/horizon-client/examples/mod_port_probe.rs @@ -0,0 +1,271 @@ +//! The Horizon side of a mod port's harness (tools/mod_port_harness.py, +//! docs/modpacks.md): builds the content root for carried-over mods and +//! writes what Horizon makes of every block state, drop, recipe and tag the +//! NeoForge oracle exported. +//! +//! cargo run -p horizon-client --example mod_port_probe -- PREPARED WORK OUT.json [MOD...] +//! +//! WORK is a `horizon-port-pack` work folder: `oracle/facts.json` and +//! `mods//carry/`. The root is built in `WORK/horizon-root`. +use horizon_client::{ + Result, + assets::Assets, + blocks::BlockFacts, + crafting::{Recipe, Recipes}, + items::{ItemStack, Items}, + mod_content::{self, ContentMod}, + stations::StationRecipes, +}; +use horizon_core::{geometry::Axis, random::LegacyRandom}; +use horizon_world::chunk::BlockState; +use serde_json::{Map, Value, json}; +use std::{collections::BTreeMap, fs, path::Path}; + +fn boxes(list: impl IntoIterator) -> Value { + let mut v: Vec<[f64; 6]> = list.into_iter().collect(); + v.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal)); + json!(v) +} + +fn sound(s: &horizon_client::blocks::Sounds) -> Value { + json!({"volume": s.volume, "pitch": s.pitch, "break": &*s.break_sound, "step": &*s.step, + "place": &*s.place, "hit": &*s.hit, "fall": &*s.fall}) +} + +fn tool(name: &str) -> Option { + match name { + "hand" => None, + "silk_touch_pickaxe" => { + let mut pick = ItemStack::new("minecraft:diamond_pickaxe", 1); + pick.enchant("minecraft:silk_touch", 1); + Some(pick) + } + other => Some(ItemStack::new(&format!("minecraft:{other}"), 1)), + } +} + +fn main() -> Result<()> { + let args: Vec = std::env::args().skip(1).collect(); + let [base, work, out, wanted @ ..] = args.as_slice() else { + return Err("usage: mod_port_probe PREPARED WORK OUT.json [MOD...]".into()); + }; + let (base, work) = (Path::new(base), Path::new(work)); + let oracle: Value = serde_json::from_slice(&fs::read(work.join("oracle/facts.json"))?)?; + let mut mods = Vec::new(); + for entry in fs::read_dir(work.join("mods"))?.flatten() { + let id = entry.file_name().to_string_lossy().into_owned(); + let dir = entry.path().join("carry"); + if !(wanted.is_empty() || wanted.contains(&id)) || !dir.is_dir() { + continue; + } + let package = horizon_mods::Package::read(&dir)?; + let content = horizon_mods::Content::read(&dir, &package.manifest)?; + mods.push(ContentMod { + id: package.manifest.id.clone(), + content, + }); + } + mods.sort_by(|a, b| a.id.cmp(&b.id)); + let root = work.join("horizon-root"); + let began = std::time::Instant::now(); + let step = |what: &str| { + let what = what.to_owned(); + move |e: Box| format!("{what}: {e}") + }; + mod_content::build(base, &mods, &root).map_err(step("content root"))?; + let built = began.elapsed().as_secs_f64(); + let assets = Assets::load(&root).map_err(step("assets"))?; + let registry = assets.registry; + let facts = BlockFacts::load(&root, registry).map_err(step("block facts"))?; + let items = Items::load(&root, registry, &facts).map_err(step("items"))?; + let recipes = Recipes::load(&root, &items).map_err(step("recipes"))?; + let stations = StationRecipes::load(&root, &items); + let collisions = + horizon_client::collisions::load(&root, registry).map_err(step("collisions"))?; + let loaded = began.elapsed().as_secs_f64() - built; + let ours = |id: &str| { + id.split_once(':') + .is_some_and(|(ns, _)| mods.iter().any(|m| m.id == ns)) + }; + let tools: Vec = serde_json::from_value(oracle["tools"].clone())?; + + let mut blocks = Map::new(); + for (name, block) in oracle["blocks"].as_object().ok_or("oracle: no blocks")? { + if !ours(name) { + continue; + } + let Some(default) = registry.default_state(name) else { + blocks.insert(name.clone(), Value::Null); + continue; + }; + let mut states = Map::new(); + for (key, o) in block["states"].as_object().into_iter().flatten() { + let properties: BTreeMap = key + .split(',') + .filter(|p| !p.is_empty()) + .filter_map(|p| p.split_once('=')) + .map(|(k, v)| (k.to_owned(), v.to_owned())) + .collect(); + let descriptor = BlockState { + name: name.clone(), + properties, + }; + let Some(id) = registry.id(&descriptor) else { + states.insert(key.clone(), Value::Null); + continue; + }; + let f = facts.get(id); + let collision = collisions + .cached_shapes() + .cached_shape(id) + .ok() + .flatten() + .map(|s| { + boxes(s.boxes().iter().map(|b| { + [ + b.min(Axis::X), + b.min(Axis::Y), + b.min(Axis::Z), + b.max(Axis::X), + b.max(Axis::Y), + b.max(Axis::Z), + ] + })) + }); + let mut row = json!({ + "hardness": f.hardness, + "explosion_resistance": f.explosion_resistance, + "emission": f.emission, + "requires_tool": f.requires_tool, + "friction": f.friction, + "map_color_id": f.map_color, + "can_occlude": f.can_occlude, + "sound": sound(&f.sounds), + "outline": boxes(f.outline.iter().copied()), + "collision": collision, + "classes": f.classes.iter().map(|c| &**c).collect::>(), + }); + if o.get("drops").is_some() { + let mut drops = Map::new(); + for t in &tools { + let mut random = LegacyRandom::new(0); + let held = tool(t); + let mut counts: BTreeMap = BTreeMap::new(); + for s in items.block_drops(registry, id, held.as_ref(), &mut random) { + *counts.entry(s.item.to_string()).or_default() += s.count; + } + drops.insert(t.clone(), json!(counts)); + } + row["drops"] = Value::Object(drops); + } + states.insert(key.clone(), row); + } + let item = facts.get(default).item.to_string(); + blocks.insert( + name.clone(), + json!({"states": states, "item": item, "item_known": items.items.contains_key(&*item)}), + ); + } + + // Vanilla's sounds, by block: the harness maps the oracle's version's + // sound events to this one's through them. + let mut vanilla_sounds = Map::new(); + for name in oracle["blocks"] + .as_object() + .into_iter() + .flatten() + .map(|(k, _)| k) + { + if let Some(id) = name + .starts_with("minecraft:") + .then(|| registry.default_state(name)) + .flatten() + { + vanilla_sounds.insert(name.clone(), sound(&facts.get(id).sounds)); + } + } + + let mut item_rows = Map::new(); + for name in oracle["items"] + .as_object() + .into_iter() + .flatten() + .map(|(k, _)| k) + { + item_rows.insert( + name.clone(), + match items.items.get(name.as_str()) { + Some(i) => json!({"max_stack_size": i.max_stack, "block": i.block.map(|b| b.0)}), + None => Value::Null, + }, + ); + } + + let sorted = |list: &[String]| { + let mut v = list.to_vec(); + v.sort(); + v.dedup(); + v + }; + let mut recipe_rows = Map::new(); + for (name, recipe) in &recipes.crafting { + if !ours(name) { + continue; + } + let row = match recipe { + Recipe::Shaped { cells, result, .. } => json!({ + "result": result.item.as_deref(), "count": result.count, + "ingredients": cells.iter().flatten().map(|i| sorted(&i.1)).collect::>()}), + Recipe::Shapeless { + ingredients, + result, + } => json!({ + "result": result.item.as_deref(), "count": result.count, + "ingredients": ingredients.iter().map(|i| sorted(&i.1)).collect::>()}), + Recipe::Special(_) => json!({"special": true}), + }; + recipe_rows.insert(name.clone(), row); + } + for c in recipes.cooking.iter().filter(|c| ours(&c.id)) { + recipe_rows.insert( + c.id.clone(), + json!({"result": &*c.result.item, "count": c.result.count, + "ingredients": [sorted(&c.ingredient.1)]}), + ); + } + for s in stations.stonecutting.iter().filter(|s| ours(&s.id)) { + recipe_rows.insert( + s.id.clone(), + json!({"result": &*s.result.item, "count": s.result.count, + "ingredients": [sorted(&s.input.1)]}), + ); + } + + let mut block_tags = Map::new(); + for (tag, members) in &items.block_tags { + let mut ids: Vec<&String> = members.iter().filter(|m| ours(m)).collect(); + if !ids.is_empty() { + ids.sort(); + block_tags.insert(tag.clone(), json!(ids)); + } + } + + let report = json!({ + "states": registry.len(), + "build_seconds": built, + "load_seconds": loaded, + "blocks": blocks, + "items": item_rows, + "recipes": recipe_rows, + "block_tags": block_tags, + "vanilla_sounds": vanilla_sounds, + }); + fs::write(out, serde_json::to_vec(&report)?)?; + println!( + "{out}: {} blocks, {} items, {} recipes; root built in {built:.2} s, loaded in {loaded:.2} s", + blocks.len(), + item_rows.len(), + recipe_rows.len() + ); + Ok(()) +} diff --git a/crates/horizon-client/examples/shared_json_startup_profile.rs b/crates/horizon-client/examples/shared_json_startup_profile.rs new file mode 100644 index 000000000..7b7d896b2 --- /dev/null +++ b/crates/horizon-client/examples/shared_json_startup_profile.rs @@ -0,0 +1,74 @@ +//! Profile the fact and behaviour table phase of client startup. +//! Run each mode in a separate process to capture CPU usage and peak RSS. +//! The deferred mode evaluates both phases contiguously for fair loader comparison; +//! actual client startup performs other initialization between these phases. +use horizon_client::{assets::Assets, blocks::BlockFacts}; +use horizon_world::level::Behaviours; +use std::{path::Path, time::Instant}; + +fn resident_kib() -> Option { + let status = std::fs::read_to_string("/proc/self/status").ok()?; + let line = status.lines().find(|line| line.starts_with("VmRSS:"))?; + line.split_whitespace().nth(1)?.parse().ok() +} + +fn main() -> Result<(), Box> { + let mut args = std::env::args().skip(1); + let root = args.next().ok_or("expected resource directory")?; + let mode = args + .next() + .ok_or("expected baseline, pr607, shared, deferred or deferred-held")?; + if !["baseline", "pr607", "shared", "deferred", "deferred-held"].contains(&mode.as_str()) { + return Err("expected baseline, pr607, shared, deferred or deferred-held".into()); + } + let root = Path::new(&root); + let total = Instant::now(); + let assets = Assets::load(root)?; + let started = Instant::now(); + let mut retained_inputs = None; + let mut subphases = None; + let (facts, behaviours) = if mode == "baseline" { + ( + BlockFacts::load(root, assets.registry)?, + Behaviours::load(assets.registry, root)?, + ) + } else if mode == "pr607" { + // Original PR #607 sequence at a01d47f2, retained as a same-build comparator. + let json: serde_json::Value = serde_json::from_reader(std::io::BufReader::new( + std::fs::File::open(root.join("block-behaviours.json"))?, + ))?; + BlockFacts::load_with_behaviours(root, assets.registry, &json)? + } else if mode == "shared" { + BlockFacts::load_pair(root, assets.registry)? + } else { + let parse_started = Instant::now(); + let json: serde_json::Value = serde_json::from_reader(std::io::BufReader::new( + std::fs::File::open(root.join("block-behaviours.json"))?, + ))?; + let parse_ms = parse_started.elapsed().as_secs_f64() * 1000.0; + let facts_started = Instant::now(); + let (facts, gameplay) = BlockFacts::load_with_projection(root, assets.registry, &json)?; + let facts_ms = facts_started.elapsed().as_secs_f64() * 1000.0; + let behaviours_started = Instant::now(); + let behaviours = Behaviours::load_with_gameplay(assets.registry, root, &json, &gameplay)?; + let behaviours_ms = behaviours_started.elapsed().as_secs_f64() * 1000.0; + subphases = Some((parse_ms, facts_ms, behaviours_ms)); + if mode == "deferred-held" { + retained_inputs = Some((json, gameplay)); + } + (facts, behaviours) + }; + println!("phase_ms={:.3}", started.elapsed().as_secs_f64() * 1000.0); + println!("total_ms={:.3}", total.elapsed().as_secs_f64() * 1000.0); + println!("states={}", facts.states.len()); + if let Some((parse, facts, behaviours)) = subphases { + println!("behaviour_json_ms={parse:.3}"); + println!("facts_ms={facts:.3}"); + println!("behaviours_ms={behaviours:.3}"); + } + if let Some(rss) = resident_kib() { + println!("post_rss_kib={rss}"); + } + std::hint::black_box((facts, behaviours, retained_inputs)); + Ok(()) +} diff --git a/crates/horizon-client/examples/translucency_fixture.rs b/crates/horizon-client/examples/translucency_fixture.rs new file mode 100644 index 000000000..11083d6b2 --- /dev/null +++ b/crates/horizon-client/examples/translucency_fixture.rs @@ -0,0 +1,47 @@ +//! Transport for the vanilla quad sorting / viewpoint parity gate. +use horizon_client::gfx::translucency::{SortState, Sorter, quad_center}; +use serde::Deserialize; +use serde_json::json; +use std::{ + fs, + io::{BufWriter, Write}, +}; + +#[derive(Deserialize)] +struct Case { + origin: [i32; 3], + quads: Vec<[[f32; 3]; 4]>, + cameras: Vec<[f64; 3]>, +} + +fn main() -> Result<(), Box> { + let args: Vec<_> = std::env::args().skip(1).collect(); + let [input, output] = args.as_slice() else { + return Err("usage: translucency_fixture INPUT OUTPUT".into()); + }; + let mut out = BufWriter::new(fs::File::create(output)?); + for line in fs::read_to_string(input)?.lines() { + let case: Case = serde_json::from_str(line)?; + let centers: Vec<_> = case.quads.iter().map(|q| quad_center(q[0], q[2])).collect(); + let mut state = SortState::new(centers.clone()); + let mut sorter = Sorter::default(); + let mut cache_sorter = Sorter::default(); + let frames: Vec<_> = case + .cameras + .iter() + .map(|&camera| { + let update = state + .update(camera, case.origin, &mut cache_sorter) + .is_some(); + let local = std::array::from_fn(|i| (camera[i] - f64::from(case.origin[i])) as f32); + json!({"indices": sorter.sort(¢ers, local), "update": update}) + }) + .collect(); + let bits: Vec<_> = centers + .iter() + .map(|c| c.map(|f| f.to_bits() as i32)) + .collect(); + writeln!(out, "{}", json!({"centers": bits, "frames": frames}))?; + } + Ok(()) +} diff --git a/crates/horizon-client/src/assets.rs b/crates/horizon-client/src/assets.rs index 351d57d45..b8551a059 100644 --- a/crates/horizon-client/src/assets.rs +++ b/crates/horizon-client/src/assets.rs @@ -245,7 +245,7 @@ impl Assets { material_states, sprite_size: sprites.size, sprite_minimum_size: sprites.minimum_size, - texture_layers: u32::try_from(sprites.layers.len())?, + texture_layers: u32::try_from(sprites.count())?, pixels: sprites.pixels, model_catalog: models.catalog, model_library, diff --git a/crates/horizon-client/src/banner.rs b/crates/horizon-client/src/banner.rs index d2bc33fba..861294722 100644 --- a/crates/horizon-client/src/banner.rs +++ b/crates/horizon-client/src/banner.rs @@ -199,6 +199,14 @@ pub fn dye_rgb(name: &str) -> u32 { pub fn layers(stack: &ItemStack) -> &[BannerLayer] { stack.data().map_or(&[], |d| &d.banner_patterns) } +/// The ominous banner's component patch hides its pattern tooltip. +pub fn tooltip_layers(stack: &ItemStack) -> &[BannerLayer] { + if stack.data().is_some_and(|d| d.ominous_banner) { + &[] + } else { + layers(stack) + } +} /// `LoomMenu.setupResultSlot`: one banner with the pattern added in the dye colour. pub fn loom_result(banner: &ItemStack, dye: &str, pattern: &str) -> ItemStack { let mut result = ItemStack { @@ -302,6 +310,24 @@ mod tests { } assert!(has_max_layers(&banner)); } + #[test] + fn ominous_banner_hides_tooltip_layers_but_keeps_its_patterns() { + let mut banner = crate::mobs::raider::ominous_banner(); + (banner.damage, banner.extra) = crate::item_data::decode_components( + crate::item_data::encode_components(&banner).as_ref(), + ); + assert_eq!( + layers(&banner).len(), + crate::item_data::OMINOUS_LAYERS.len() + ); + assert!(tooltip_layers(&banner).is_empty()); + banner.data_mut().custom_name = Some("Captain's Standard".into()); + assert!(tooltip_layers(&banner).is_empty()); + banner.data_mut().ominous_banner = false; + assert_eq!(tooltip_layers(&banner), layers(&banner)); + assert!(!tooltip_layers(&banner).is_empty()); + } + #[test] fn layers_round_trip_through_nbt() { let l = vec![ diff --git a/crates/horizon-client/src/block_entity_render.rs b/crates/horizon-client/src/block_entity_render.rs index 9ffd888a1..9c90e8ebf 100644 --- a/crates/horizon-client/src/block_entity_render.rs +++ b/crates/horizon-client/src/block_entity_render.rs @@ -876,7 +876,7 @@ impl BlockEntityRenderer { .as_vec3(); let base = Mat4::from_translation(origin); let packed = scene.world.light(pos[0], pos[1], pos[2]); - self.render_one(scene, *pos, kind, base, packed, lights, out); + self.render_one(scene, *pos, kind, base, packed, lights, false, out); } } /// One block entity at `pos` drawn under `base` with the given packed light @@ -892,12 +892,33 @@ impl BlockEntityRenderer { ) { let kind = self.kind(state); let lights = diffuse_lights(scene.nether); - self.render_one(scene, pos, kind, base, packed, lights, out); + self.render_one(scene, pos, kind, base, packed, lights, false, out); } /// Whether this block state has a block-entity renderer here. pub fn renders(&self, state: u32) -> bool { !matches!(self.kind(state), Kind::None) } + /// ChestRenderer and BannerRenderer submit their animated models for crumbling. + pub fn render_breaking(&self, scene: &Scene, pos: [i32; 3], state: u32, out: &mut Vec) { + if !matches!(self.kind(state), Kind::Banner { .. } | Kind::Chest { .. }) { + return; + } + let origin = (DVec3::new(f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])) + - scene.camera) + .as_vec3(); + let start = out.len(); + self.render_one( + scene, + pos, + self.kind(state), + Mat4::from_translation(origin), + 0xff, + diffuse_lights(scene.nether), + true, + out, + ); + crate::breaking::entity_geometry(&mut out[start..], origin); + } /// `ChestRenderer.extractRenderState`'s openness for the chest of `state` /// at `pos`: `ChestBlock.combine` (`DoubleBlockCombiner.combineWithNeigbour`) /// pairs a double half with its neighbour only when that is the same @@ -970,7 +991,7 @@ impl BlockEntityRenderer { let kind = self.kind(state); if !matches!(kind, Kind::None) { let lights = diffuse_lights(scene.nether); - self.render_one(scene, [i32::MAX; 3], kind, base, packed, lights, out); + self.render_one(scene, [i32::MAX; 3], kind, base, packed, lights, false, out); } } #[allow(clippy::too_many_arguments)] @@ -982,11 +1003,24 @@ impl BlockEntityRenderer { base: Mat4, packed: u8, lights: (Vec3, Vec3), + crumbling: bool, out: &mut Vec, ) { let sprite = |name: &str| scene.sprites.get(name).copied(); let paint = |name: &str, tint: u32, packed: u8| { - sprite(name).map(|s| Paint { + let rect = if crumbling { + Some(Rect { + u0: 0.0, + v0: 0.0, + u1: 1.0, + v1: 1.0, + w: 16, + h: 16, + }) + } else { + sprite(name) + }; + rect.map(|s| Paint { sprite: s, tint, light: vertex_light(packed), @@ -1058,7 +1092,7 @@ impl BlockEntityRenderer { ChestPart::Left => "double_chest_left", ChestPart::Right => "double_chest_right", }; - let (Some(model), Some(_)) = (self.models.get(model), sprite(texture)) else { + let Some(model) = self.models.get(model) else { return; }; let (open, other) = self.chest_openness( @@ -1145,6 +1179,10 @@ impl BlockEntityRenderer { draw_part(main, "root", m, &none, paint, out); draw_part(flag, "root", m, &wave, paint, out); } + // Pattern passes must not multiply the crumbling decal. + if crumbling { + return; + } // BannerRenderer.submitPatterns: the base pattern layer in the dye colour, // then up to 16 pattern layers in order. if let Some(paint) = paint("entity/banner/base", *color, packed) { @@ -1673,6 +1711,138 @@ fn portal_cube(m: Mat4, faces: &[Direction], layer: u32, out: &mut Vec) #[cfg(test)] mod tests { use super::*; + #[test] + #[ignore = "requires prepared models/textures in HORIZON_TEST_RESOURCES"] + fn entity_block_particles_and_breaking_use_exported_models() -> crate::Result<()> { + use crate::{models::catalog::StateModels, sprites::Sprites}; + use horizon_world::{DATA_VERSION, chunk::BlockState, nbt::Tag}; + let root = std::path::PathBuf::from( + std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"), + ); + let names = [ + "white_banner", + "white_wall_banner", + "chest", + "trapped_chest", + "ender_chest", + "copper_chest", + ]; + let mut facts: std::collections::BTreeMap = + serde_json::from_slice(&std::fs::read(root.join("model-states.json"))?)?; + let states = names.map(|name| { + let name = format!("minecraft:{name}"); + let mut first = facts[&name]["states"][0].clone(); + if first.get("type").is_some() { + first["type"] = serde_json::json!("single"); + } + facts.get_mut(&name).unwrap()["states"] = serde_json::json!([first.clone()]); + BlockState { + name, + properties: serde_json::from_value(first).unwrap(), + } + .to_nbt() + }); + let registry = BlockRegistry::from_nbt(&Tag::compound([ + ("DataVersion", Tag::Int(DATA_VERSION)), + ("states", Tag::List(states.into())), + ( + "defaults", + Tag::compound(names.iter().enumerate().map(|(id, name)| { + ( + horizon_world::nbt::JavaString::from(format!("minecraft:{name}").as_str()), + Tag::Int(id as i32), + ) + })), + ), + ]))?; + let mut sprites = Sprites::default(); + let catalog = StateModels::from_facts(&root, ®istry, &names, &mut sprites, &facts)?; + for (id, name) in names.iter().enumerate() { + let models = catalog.collect( + BlockStateId(id as u32), + horizon_core::position::BlockPos::default(), + )?; + assert!(models.iter().all(|(_, model)| model.faces.is_empty())); + let particle = match *name { + "ender_chest" => "minecraft:block/obsidian", + "copper_chest" => "minecraft:block/copper_block", + _ => "minecraft:block/oak_planks", + }; + assert_eq!(models[0].1.particle, Some(sprites.layers[particle])); + } + let all: Value = serde_json::from_slice(&std::fs::read(root.join("entity-models.json"))?)?; + let renderer = BlockEntityRenderer::new( + MODELS + .iter() + .filter_map(|(key, layer)| all.get(*layer).map(|v| (*key, ModelPart::parse(v)))) + .collect(), + ®istry, + ); + let world = World::new(BlockStateId(0), 1); + let base_sprites = [ + "entity/banner/banner_base", + "entity/chest/normal", + "entity/chest/trapped", + "entity/chest/ender", + "entity/chest/copper", + ] + .into_iter() + .map(|name| { + ( + name.to_owned(), + Rect { + u0: 0.0, + v0: 0.0, + u1: 1.0, + v1: 1.0, + w: 16, + h: 16, + }, + ) + }) + .collect(); + let mut scene = Scene { + camera: DVec3::ZERO, + orientation: Quat::IDENTITY, + anim: &|_| BlockAnim::default(), + texts: None, + partial: 0.5, + game_time: 0, + max_y: 320, + world: &world, + sprites: &base_sprites, + nether: false, + lid: &|_| 0.0, + sign: &|_| None, + banner: &|_| Vec::new(), + glyph: &|_| (None, 0.0), + }; + for state in 0..names.len() as u32 { + let mut base = Vec::new(); + renderer.render_block(&scene, [0; 3], state, Mat4::IDENTITY, 0xff, &mut base); + let mut cracks = Vec::new(); + renderer.render_breaking(&scene, [0; 3], state, &mut cracks); + assert!(!cracks.is_empty()); + assert_eq!( + base.iter().map(|v| v.pos).collect::>(), + cracks.iter().map(|v| v.pos).collect::>() + ); + assert!(cracks.iter().all(|v| v.layer == 11 + && v.color == u32::MAX + && v.uv.iter().all(|u| u.is_finite()))); + scene.game_time = 20; + if state >= 2 { + scene.lid = &|_| 1.0; + } + let mut animated = Vec::new(); + renderer.render_breaking(&scene, [0; 3], state, &mut animated); + assert!(cracks.iter().zip(&animated).any(|(a, b)| a.pos != b.pos)); + scene.game_time = 0; + scene.lid = &|_| 0.0; + } + Ok(()) + } + #[test] fn classifies_vanilla_block_names() { let props = |pairs: &'static [(&'static str, &'static str)]| { diff --git a/crates/horizon-client/src/blocks.rs b/crates/horizon-client/src/blocks.rs index 521d389bc..27a36396a 100644 --- a/crates/horizon-client/src/blocks.rs +++ b/crates/horizon-client/src/blocks.rs @@ -2,7 +2,10 @@ //! (`gameplay-facts.json`), indexed by registry state ID. mod decode; use crate::Result; -use horizon_world::registry::{BlockRegistry, BlockStateId}; +use horizon_world::{ + level::{Behaviours, GameplayStateSource}, + registry::{BlockRegistry, BlockStateId}, +}; use serde_json::Value; use std::{ collections::HashMap, @@ -107,12 +110,178 @@ impl StateFacts { self.classes.iter().any(|c| &**c == name) } } +/// Compact, owned behaviour inputs retained until the normal behaviour startup stage. +/// No borrowed gameplay JSON or process-wide cache survives the fact loader. +pub struct GameplayProjection { + flags: u16, + emission: u64, + dampening: u64, + hardness: f64, + push: Arc, + volume: f64, + pitch: f64, + place: Arc, + break_sound: Arc, + fall: Arc, +} + +impl GameplayProjection { + fn from_entry(entry: &decode::Entry<'_>, strings: &mut HashMap>) -> Self { + fn intern(strings: &mut HashMap>, value: &str) -> Arc { + if let Some(existing) = strings.get(value) { + return Arc::clone(existing); + } + let value_arc: Arc = Arc::from(value); + strings.insert(value.to_owned(), Arc::clone(&value_arc)); + value_arc + } + const FLAGS: [&str; 11] = [ + "replaceable", + "random_ticks", + "solid_render", + "liquid", + "block_entity", + "ignited_by_lava", + "can_occlude", + "full_collision", + "skylight_down", + "requires_tool", + "shape_occlusion", + ]; + Self { + flags: FLAGS.iter().enumerate().fold(0, |flags, (i, name)| { + flags | ((u16::from(entry.flag(name))) << i) + }), + emission: entry.unsigned("emission"), + dampening: entry.unsigned("dampening"), + hardness: entry.number("hardness"), + push: intern(strings, entry.text("push")), + volume: entry.sound_number("volume"), + pitch: entry.sound_number("pitch"), + place: intern(strings, entry.sound_id("place")), + break_sound: intern(strings, entry.sound_id("break")), + fall: intern(strings, entry.sound_id("fall")), + } + } +} + +impl GameplayStateSource for GameplayProjection { + fn flag(&self, name: &str) -> bool { + const FLAGS: [&str; 11] = [ + "replaceable", + "random_ticks", + "solid_render", + "liquid", + "block_entity", + "ignited_by_lava", + "can_occlude", + "full_collision", + "skylight_down", + "requires_tool", + "shape_occlusion", + ]; + FLAGS + .iter() + .position(|field| *field == name) + .is_some_and(|index| self.flags & (1 << index) != 0) + } + fn unsigned(&self, name: &str) -> u64 { + match name { + "emission" => self.emission, + "dampening" => self.dampening, + _ => 0, + } + } + fn number(&self, name: &str) -> f64 { + if name == "hardness" { + self.hardness + } else { + 0.0 + } + } + fn text(&self, name: &str) -> &str { + if name == "push" { &self.push } else { "" } + } + fn sound_number(&self, name: &str) -> f64 { + match name { + "volume" => self.volume, + "pitch" => self.pitch, + _ => 1.0, + } + } + fn sound_id(&self, name: &str) -> &str { + match name { + "place" => &self.place, + "break" => &self.break_sound, + "fall" => &self.fall, + _ => "", + } + } +} + pub struct BlockFacts { pub states: Vec, pub air: BlockStateId, } impl BlockFacts { pub fn load(root: &Path, registry: &BlockRegistry) -> Result { + let behaviours: Value = serde_json::from_reader(BufReader::new(File::open( + root.join("block-behaviours.json"), + )?))?; + Self::load_with_behaviour_json(root, registry, &behaviours) + } + + /// Load both tables from one resource root, sharing each JSON decode. + /// No data is cached across calls: changes to prepared packs or mods are read again. + pub fn load_pair(root: &Path, registry: &BlockRegistry) -> Result<(Self, Behaviours)> { + let behaviours: Value = serde_json::from_reader(BufReader::new(File::open( + root.join("block-behaviours.json"), + )?))?; + Self::load_with_behaviours(root, registry, &behaviours) + } + + /// Decode state facts once, retaining only the small owned fields needed later. + pub fn load_with_projection( + root: &Path, + registry: &BlockRegistry, + behaviours: &Value, + ) -> Result<(Self, Vec)> { + Self::load_with_callback(root, registry, behaviours, |entries| { + let mut strings = HashMap::new(); + Ok(entries + .iter() + .map(|entry| GameplayProjection::from_entry(entry, &mut strings)) + .collect()) + }) + } + + pub fn load_with_behaviour_json( + root: &Path, + registry: &BlockRegistry, + behaviours: &Value, + ) -> Result { + Ok(Self::load_with_callback(root, registry, behaviours, |_| Ok(()))?.0) + } + + /// Build both tables from the same behaviour document and gameplay decoder. + pub fn load_with_behaviours( + root: &Path, + registry: &BlockRegistry, + behaviours: &Value, + ) -> Result<(Self, Behaviours)> { + Self::load_with_callback(root, registry, behaviours, |entries| { + Ok(Behaviours::load_with_gameplay( + registry, root, behaviours, entries, + )?) + }) + } + + fn load_with_callback( + root: &Path, + registry: &BlockRegistry, + behaviours: &Value, + callback: impl FnOnce(&[decode::Entry<'_>]) -> Result, + ) -> Result<(Self, T)> { let bytes = fs::read(root.join("gameplay-facts.json"))?; serde_json::from_slice::(&bytes)?; let raw: decode::Gameplay<'_> = serde_json::from_slice(&bytes)?; @@ -125,9 +294,6 @@ impl BlockFacts { return Err("gameplay facts do not match the block registry".into()); } // Class chains and per-state behaviour flags exported from the actual block objects. - let behaviours: Value = serde_json::from_reader(BufReader::new(File::open( - root.join("block-behaviours.json"), - )?))?; let behaviour_states = behaviours["states"] .as_array() .filter(|s| s.len() == registry.len()) @@ -221,13 +387,13 @@ impl BlockFacts { outline, outline_edges, name: intern(&state.name), - emission: entry.emission as u8, - dampening: entry.dampening as u8, + emission: entry.emission.as_f64().unwrap_or(0.0) as u8, + dampening: entry.dampening.as_f64().unwrap_or(0.0) as u8, skylight_down: entry.skylight_down, shape_occlusion: entry.shape_occlusion, can_occlude: entry.can_occlude, solid_render: entry.solid_render, - hardness: entry.hardness as f32, + hardness: entry.hardness.as_f64().unwrap_or(0.0) as f32, requires_tool: entry.requires_tool, replaceable: entry.replaceable, random_ticks: entry.random_ticks, @@ -264,12 +430,16 @@ impl BlockFacts { .ok_or("missing block behaviour class chain")?, }); } - Ok(Self { - states, - air: registry - .default_state("minecraft:air") - .ok_or("missing air")?, - }) + let extra = callback(entries)?; + Ok(( + Self { + states, + air: registry + .default_state("minecraft:air") + .ok_or("missing air")?, + }, + extra, + )) } pub fn get(&self, id: BlockStateId) -> &StateFacts { &self.states[id.0 as usize] @@ -281,3 +451,56 @@ impl BlockFacts { ) } } + +impl GameplayStateSource for decode::Entry<'_> { + fn flag(&self, name: &str) -> bool { + match name { + "replaceable" => self.replaceable, + "random_ticks" => self.random_ticks, + "solid_render" => self.solid_render, + "liquid" => self.liquid, + "block_entity" => self.block_entity, + "ignited_by_lava" => self.ignited_by_lava, + "can_occlude" => self.can_occlude, + "full_collision" => self.full_collision, + "skylight_down" => self.skylight_down, + "requires_tool" => self.requires_tool, + "shape_occlusion" => self.shape_occlusion, + _ => false, + } + } + fn unsigned(&self, name: &str) -> u64 { + match name { + "emission" => self.emission.as_u64().unwrap_or(0), + "dampening" => self.dampening.as_u64().unwrap_or(0), + _ => 0, + } + } + fn number(&self, name: &str) -> f64 { + match name { + "hardness" => self.hardness.as_f64().unwrap_or(0.0), + _ => 0.0, + } + } + fn text(&self, name: &str) -> &str { + match name { + "push" => self.push.as_deref().unwrap_or(""), + _ => "", + } + } + fn sound_number(&self, name: &str) -> f64 { + match name { + "volume" => self.sound.volume, + "pitch" => self.sound.pitch, + _ => 1.0, + } + } + fn sound_id(&self, name: &str) -> &str { + match name { + "place" => self.sound.place.id.as_deref().unwrap_or(""), + "break" => self.sound.r#break.id.as_deref().unwrap_or(""), + "fall" => self.sound.fall.id.as_deref().unwrap_or(""), + _ => "", + } + } +} diff --git a/crates/horizon-client/src/blocks/decode.rs b/crates/horizon-client/src/blocks/decode.rs index 967955f9e..7feb3ac41 100644 --- a/crates/horizon-client/src/blocks/decode.rs +++ b/crates/horizon-client/src/blocks/decode.rs @@ -155,17 +155,20 @@ object!(Sound<'a> { }); object!(Entry<'a> { id: Option = None => value(unsigned), - emission: f64 = 0.0 => value(number), - dampening: f64 = 0.0 => value(number), + emission: Value = Value::Null => typed, + dampening: Value = Value::Null => typed, skylight_down: bool = false => value(flag), shape_occlusion: bool = false => value(flag), can_occlude: bool = false => value(flag), solid_render: bool = false => value(flag), - hardness: f64 = 0.0 => value(number), + hardness: Value = Value::Null => typed, requires_tool: bool = false => value(flag), replaceable: bool = false => value(flag), random_ticks: bool = false => value(flag), block_entity: bool = false => value(flag), + liquid: bool = false => value(flag), + ignited_by_lava: bool = false => value(flag), + push: Option = None => value(text), render_shape: Option = None => value(text), full_collision: bool = false => value(flag), friction: f64 = 0.0 => value(number), diff --git a/crates/horizon-client/src/blocks/decode_tests.rs b/crates/horizon-client/src/blocks/decode_tests.rs index d9d0e14fd..8fdb93f4f 100644 --- a/crates/horizon-client/src/blocks/decode_tests.rs +++ b/crates/horizon-client/src/blocks/decode_tests.rs @@ -89,9 +89,9 @@ fn typed_fields_keep_defaults_duplicates_and_wrong_type_behavior() -> crate::Res assert_eq!(entries[1].id, None); let e = &entries[2]; assert_eq!(e.id, Some(2)); - assert_eq!(e.emission, 4.5); - assert_eq!(e.dampening, 0.0); - assert_eq!(e.hardness.to_bits(), (-0.0f64).to_bits()); + assert_eq!(e.emission.as_f64(), Some(4.5)); + assert_eq!(e.dampening.as_f64().unwrap_or(0.0), 0.0); + assert_eq!(e.hardness.as_f64().unwrap().to_bits(), (-0.0f64).to_bits()); assert!(e.skylight_down && e.outline_offset.unwrap_or(e.offset)); assert_eq!(e.item, None); assert_eq!(e.render_shape, None); diff --git a/crates/horizon-client/src/breaking.rs b/crates/horizon-client/src/breaking.rs index 9dc948ae3..c7ee19206 100644 --- a/crates/horizon-client/src/breaking.rs +++ b/crates/horizon-client/src/breaking.rs @@ -41,6 +41,35 @@ pub fn geometry(model: &BakedModel, origin: glam::Vec3, translucent: bool, out: } } +/// Project a block entity's transformed quads onto the same destroy-stage pass. +pub fn entity_geometry(vertices: &mut [Vertex], origin: glam::Vec3) { + for quad in vertices.as_chunks_mut::<4>().0 { + let positions = [0, 1, 2].map(|i| glam::Vec3::from(quad[i].pos)); + let normal = (positions[1] - positions[0]).cross(positions[2] - positions[0]); + let mut facing = Direction::North; + let mut best = f32::from_bits(1); + for (direction, dot) in [ + (Direction::Down, -normal.y), + (Direction::Up, normal.y), + (Direction::North, -normal.z), + (Direction::South, normal.z), + (Direction::West, -normal.x), + (Direction::East, normal.x), + ] { + if dot > best { + facing = direction; + best = dot; + } + } + for vertex in quad { + vertex.uv = decal_uv((glam::Vec3::from(vertex.pos) - origin).to_array(), facing); + vertex.layer = 11; + vertex.color = 0xffff_ffff; + vertex.light = 240 | (240 << 8); + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -49,6 +78,7 @@ mod tests { fn slab_top() -> BakedModel { BakedModel { ambient_occlusion: true, + particle: None, cube: None, faces: vec![BakedFace { layer: 0, @@ -86,6 +116,24 @@ mod tests { assert_eq!(v.color, 0xffff_ffff); } } + #[test] + fn entity_overlay_uses_block_local_projected_uvs() { + let origin = glam::Vec3::new(12.0, -3.0, 8.0); + let mut expected = Vec::new(); + geometry(&slab_top(), origin, false, &mut expected); + let mut actual = expected.clone(); + for vertex in &mut actual { + vertex.uv = [0.0; 2]; + vertex.layer = 0; + vertex.color = 0; + } + entity_geometry(&mut actual, origin); + assert_eq!( + bytemuck::cast_slice::<_, u8>(&actual), + bytemuck::cast_slice::<_, u8>(&expected) + ); + } + #[test] fn small_faces_use_projected_not_stretched_uvs() { let a = decal_uv([0.0, 0.0, 0.0], Direction::North); diff --git a/crates/horizon-client/src/collisions.rs b/crates/horizon-client/src/collisions.rs index 39fde56b5..d3e00daca 100644 --- a/crates/horizon-client/src/collisions.rs +++ b/crates/horizon-client/src/collisions.rs @@ -28,7 +28,7 @@ use std::{fs, io, path::Path, sync::Arc}; pub fn load<'r>(root: &Path, registry: &'r BlockRegistry) -> Result> { let data = nbt::read_named( &fs::read(root.join("cached-collision.nbt"))?, - Limits::default(), + Limits::tables(), )? .0 .tag; diff --git a/crates/horizon-client/src/containers.rs b/crates/horizon-client/src/containers.rs index 46e73f067..9cdac1463 100644 --- a/crates/horizon-client/src/containers.rs +++ b/crates/horizon-client/src/containers.rs @@ -132,6 +132,8 @@ impl Crafter { pub struct Banner { pub layers: Vec, pub name: Option, + /// Components not consumed by BannerBlockEntity's implicit fields. + pub components: horizon_world::nbt::Compound, } /// `CreakingHeartBlockEntity` (the logic is in `game/creaking.rs`). #[derive(Clone, Debug, Default)] diff --git a/crates/horizon-client/src/debug_chart.rs b/crates/horizon-client/src/debug_chart.rs index d006a8ab6..35fd40bd4 100644 --- a/crates/horizon-client/src/debug_chart.rs +++ b/crates/horizon-client/src/debug_chart.rs @@ -74,6 +74,10 @@ pub struct DebugCharts { pub bandwidth: SampleLogger, /// `MinecraftServer.smoothedTickTimeMillis`. pub smoothed_tick_ms: f32, + /// Connection's smoothed packets per second, sent then received. + pub packet_rates: [f32; 2], + packet_totals: [i64; 2], + packet_tick: u8, previous_tick: Option, bytes_seen: Option, } @@ -113,6 +117,9 @@ impl DebugCharts { self.previous_tick = None; self.bytes_seen = None; self.smoothed_tick_ms = 0.0; + self.packet_rates = [0.0; 2]; + self.packet_totals = [0; 2]; + self.packet_tick = 0; } /// One integrated server tick that started at `started` and ran its /// tick method for `method` (`MinecraftServer.logFullTickTime` and @@ -136,6 +143,17 @@ impl DebugCharts { } self.bytes_seen = Some(total); } + /// `Connection.tickSecond`: sampled on the first tick, then every 20 ticks. + pub fn log_packets(&mut self, totals: [i64; 2]) { + if self.packet_tick == 0 { + for (i, total) in totals.into_iter().enumerate() { + let count = (total - self.packet_totals[i]).max(0) as f32; + self.packet_rates[i] = count + 0.75 * (self.packet_rates[i] - count); + } + self.packet_totals = totals; + } + self.packet_tick = (self.packet_tick + 1) % 20; + } /// The charts along the bottom of the screen (`extractRenderState`): /// frame times left and tick times right, or bandwidth left (remote /// servers only) and ping right. `mspt` is the level's milliseconds @@ -524,6 +542,27 @@ mod tests { assert!(draws.contains(&fill(361, 174, 365, 178, 0xFF0A_141E))); } + #[test] + fn packet_rates_follow_connection_smoothing_and_reset_between_worlds() { + let mut charts = DebugCharts::default(); + charts.log_packets([80, 400]); + assert_eq!(charts.packet_rates, [20.0, 100.0]); + for _ in 1..20 { + charts.log_packets([180, 800]); + } + assert_eq!(charts.packet_rates, [20.0, 100.0]); + charts.log_packets([180, 800]); + assert_eq!(charts.packet_rates, [40.0, 175.0]); + for _ in 0..20 { + charts.log_packets([180, 800]); + } + assert_eq!(charts.packet_rates, [30.0, 131.25]); + charts.reset(); + assert_eq!(charts.packet_rates, [0.0, 0.0]); + charts.log_packets([4, 8]); + assert_eq!(charts.packet_rates, [1.0, 2.0]); + } + #[test] fn ticks_and_bandwidth_log_their_differences() { let mut c = DebugCharts::default(); diff --git a/crates/horizon-client/src/effects.rs b/crates/horizon-client/src/effects.rs index 71a9377ed..2e600333a 100644 --- a/crates/horizon-client/src/effects.rs +++ b/crates/horizon-client/src/effects.rs @@ -68,6 +68,14 @@ impl EffectInstance { } changed } + /// `MobEffectInstance.tickClient`: a client counts its effects down + /// (the server removes them). + pub fn tick_client(&mut self) { + if self.infinite() || self.duration > 0 { + self.tick_down(); + self.downgrade(); + } + } fn tick_down(&mut self) { if let Some(h) = self.hidden.as_mut() { h.tick_down(); @@ -170,6 +178,39 @@ impl PlayerEffects { pub fn clear(&mut self) { self.list.clear(); } + /// The effects of a saved player (`Player.active_effects`: id, + /// duration, amplifier, ambient, visible). + pub fn from_saved(saved: &[(String, i32, i32, bool, bool)]) -> Self { + let mut effects = Self::default(); + for (id, duration, amplifier, ambient, _) in saved { + if let Some(def) = brewing::effect(id) { + let mut e = EffectInstance::new(def.id, *duration, *amplifier); + e.ambient = *ambient; + effects.add(e); + } + } + effects + } + /// `Player.active_effects` for the save. + pub fn saved(&self) -> Vec<(String, i32, i32, bool, bool)> { + self.list + .iter() + .map(|e| { + ( + format!("minecraft:{}", e.id), + e.duration, + e.amplifier, + e.ambient, + e.visible, + ) + }) + .collect() + } + /// ClientboundUpdateMobEffectPacket's flags without blending: ambient, + /// particles visible and the icon shown (always, for these effects). + pub fn flags(e: &EffectInstance) -> i8 { + i8::from(e.ambient) | i8::from(e.visible) << 1 | 4 + } /// `LivingEntity.tickEffects` (server part) with the omens' effect ticks: /// `omen` answers `BadOmenMobEffect` (every tick) and `RaidOmenMobEffect` /// (on its last tick). Returns the actions to apply, in order, and diff --git a/crates/horizon-client/src/enchant.rs b/crates/horizon-client/src/enchant.rs index c62a67a07..86bdbe562 100644 --- a/crates/horizon-client/src/enchant.rs +++ b/crates/horizon-client/src/enchant.rs @@ -185,6 +185,8 @@ impl Enchantments { .into_iter() .filter(|(k, _)| { k.starts_with("enchantment.") + || k.starts_with("attribute.") + || k.starts_with("item.modifiers.") || k.starts_with("effect.") || k.starts_with("item.minecraft.potion") || k.starts_with("item.minecraft.splash_potion") diff --git a/crates/horizon-client/src/entities.rs b/crates/horizon-client/src/entities.rs index a1e18076e..5e6a3336a 100644 --- a/crates/horizon-client/src/entities.rs +++ b/crates/horizon-client/src/entities.rs @@ -301,6 +301,20 @@ pub struct Experience { pub total: i32, } impl Experience { + /// `Player.getBaseExperienceReward`: keep-inventory and spectators drop none. + pub fn death_reward(&self, keep: bool) -> u32 { + if keep { + 0 + } else { + self.level.wrapping_mul(7).clamp(0, 100) as u32 + } + } + /// `ServerPlayer.restoreFrom`: only keep-inventory and spectators retain XP. + pub fn respawn(&mut self, keep: bool) { + if !keep { + *self = Self::default(); + } + } pub fn needed(level: i32) -> i32 { if level >= 30 { 112 + (level - 30) * 9 @@ -540,6 +554,37 @@ impl ArmorStand { } } +#[cfg(test)] +mod experience_tests { + use super::*; + + #[test] + fn death_reward_uses_levels_and_caps_the_recoverable_points() { + for (level, reward) in [(0, 0), (1, 7), (14, 98), (15, 100), (30, 100)] { + let xp = Experience { + level, + progress: 0.75, + total: 10_000, + }; + assert_eq!(xp.death_reward(false), reward); + assert_eq!(xp.death_reward(true), 0); + } + } + + #[test] + fn respawn_clears_all_xp_unless_it_is_kept() { + let mut xp = Experience::default(); + xp.give(155); + let before = (xp.level, xp.progress, xp.total); + xp.respawn(true); + assert_eq!((xp.level, xp.progress, xp.total), before); + xp.respawn(false); + assert_eq!((xp.level, xp.progress, xp.total), (0, 0.0, 0)); + xp.give(7); + assert_eq!((xp.level, xp.progress, xp.total), (1, 0.0, 7)); + } +} + #[cfg(test)] mod fire_environment_tests { use super::*; diff --git a/crates/horizon-client/src/extras.rs b/crates/horizon-client/src/extras.rs index 264456c1a..ee142486c 100644 --- a/crates/horizon-client/src/extras.rs +++ b/crates/horizon-client/src/extras.rs @@ -68,6 +68,7 @@ impl Corner { #[derive(Clone, Debug, PartialEq)] pub struct Extras { + pub fullbright: bool, pub zoom: bool, /// A vanilla key name (`key.keyboard.z`). pub zoom_key: String, @@ -108,6 +109,7 @@ pub struct Extras { impl Default for Extras { fn default() -> Self { Self { + fullbright: false, zoom: false, zoom_key: "key.keyboard.z".into(), zoom_level: 4, @@ -155,6 +157,7 @@ impl Extras { _ => d, }; match key.trim() { + "fullbright" => e.fullbright = flag(e.fullbright), "zoom" => e.zoom = flag(e.zoom), "zoomKey" if value.starts_with("key.") => e.zoom_key = value.to_owned(), "zoomLevel" => { @@ -198,7 +201,8 @@ impl Extras { } pub fn serialize(&self) -> String { format!( - "zoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n", + "fullbright:{}\nzoom:{}\nzoomKey:{}\nzoomLevel:{}\nzoomSmooth:{}\nzoomSensitivity:{}\nfps:{}\nfpsCorner:{}\nfpsLows:{}\nkeystrokes:{}\nkeystrokesCorner:{}\nkeystrokesCps:{}\nminimap:{}\nminimapCorner:{}\nminimapSize:{}\nminimapShape:{}\nminimapZoom:{}\nminimapRotate:{}\nautoReconnect:{}\nmouseTweaks:{}\nmouseTweaksWheel:{}\nsort:{}\nsortKey:{}\n", + bool_text(self.fullbright), bool_text(self.zoom), self.zoom_key, self.zoom_level, @@ -418,6 +422,7 @@ impl Cps { /// The settings the Extras page shows, one per row. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum Item { + Fullbright, Zoom, ZoomKey, ZoomLevel, @@ -445,6 +450,8 @@ pub enum Item { pub fn rows() -> Vec> { use Item::*; vec![ + Err("Lighting"), + Ok(Fullbright), Err("Zoom"), Ok(Zoom), Ok(ZoomKey), @@ -493,6 +500,7 @@ fn next(all: &[T], current: T, back: bool) -> T { impl Item { pub fn caption(self) -> &'static str { match self { + Item::Fullbright => "Fullbright", Item::Zoom => "Zoom", Item::ZoomKey => "Zoom key", Item::ZoomLevel => "Starting zoom", @@ -518,6 +526,7 @@ impl Item { /// What the row says under its caption. pub fn description(self) -> &'static str { match self { + Item::Fullbright => "Show blocks and entities at full light, even in dark areas.", Item::Zoom => "Hold the zoom key to zoom; the wheel changes it while held.", Item::ZoomKey => "Click, then press a key. Escape unbinds it.", Item::ZoomLevel => "The zoom the key starts at.", @@ -556,7 +565,7 @@ impl Item { pub fn cost(self) -> Option { use crate::graphics_style::Cost; match self { - Item::Zoom | Item::Fps | Item::Keystrokes => Some(Cost::None), + Item::Fullbright | Item::Zoom | Item::Fps | Item::Keystrokes => Some(Cost::None), Item::Minimap => Some(Cost::Low), Item::AutoReconnect | Item::MouseTweaks | Item::Sort => Some(Cost::None), _ => None, @@ -565,6 +574,7 @@ impl Item { /// On/off items and their value. pub fn switch(self, e: &Extras) -> Option { Some(match self { + Item::Fullbright => e.fullbright, Item::Zoom => e.zoom, Item::ZoomSmooth => e.zoom_smooth, Item::ZoomSensitivity => e.zoom_sensitivity, @@ -606,6 +616,7 @@ impl Item { /// A click (`back` for the right button). The zoom key binds instead. pub fn cycle(self, e: &mut Extras, back: bool) { match self { + Item::Fullbright => e.fullbright = !e.fullbright, Item::Zoom => e.zoom = !e.zoom, Item::ZoomSmooth => e.zoom_smooth = !e.zoom_smooth, Item::ZoomSensitivity => e.zoom_sensitivity = !e.zoom_sensitivity, @@ -651,11 +662,14 @@ mod tests { #[test] fn everything_is_off_by_default_and_round_trips() { let d = Extras::default(); + assert!(!d.fullbright); assert!(!d.zoom && !d.fps && !d.keystrokes && !d.minimap && !d.auto_reconnect); assert!(!d.mouse_tweaks && !d.sort); assert_eq!(Extras::parse(""), d); assert_eq!(Extras::parse(&d.serialize()), d); let mut e = d.clone(); + Item::Fullbright.cycle(&mut e, false); + assert_eq!(Item::Fullbright.switch(&e), Some(true)); e.zoom = true; e.zoom_key = "key.keyboard.c".into(); e.zoom_level = 6; diff --git a/crates/horizon-client/src/front.rs b/crates/horizon-client/src/front.rs index 7b1771000..5b0a2c5ab 100644 --- a/crates/horizon-client/src/front.rs +++ b/crates/horizon-client/src/front.rs @@ -318,6 +318,7 @@ impl Title { x: f32, y: f32, ) -> Action { + self.remember_direct_address(); if self.skip_opening() { return Action::None; } @@ -472,7 +473,7 @@ impl Title { _ => None, }; if let Some(id) = id { - match screen.click(Some(id), 0.0, 0.0, (0.0, 0.0), &saves) { + match self.multiplayer_click(Some(id), 0.0, 0.0, (0.0, 0.0)) { Outcome::Back => self.mode = Mode::Main, Outcome::Connect(address) => return Action::Connect { address }, Outcome::None => {} @@ -1432,6 +1433,28 @@ mod tests { } } + #[test] + fn horizon_direct_connection_remembers_the_address_after_leaving() { + let atlas = + HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap(); + let saves = + std::env::temp_dir().join(format!("horizon-front-direct-{}", std::process::id())); + let mut title = Title::new(saves.clone(), &saves); + title.last_server = "localhost:25566".into(); + title.mode = Mode::Multiplayer(Screen::list(&saves)); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "localhost:25566"); + *title.focused_field().unwrap().1 = "example.org:25567".into(); + press(&mut title, &atlas, BACK); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "example.org:25567"); + *title.focused_field().unwrap().1 = "localhost:25568".into(); + press(&mut title, &atlas, NAV); + press(&mut title, &atlas, NAV + 2); + press(&mut title, &atlas, DIRECT); + assert_eq!(title.focused_field().unwrap().1, "localhost:25568"); + } + #[test] fn horizon_world_delete_confirms_and_keeps_the_selection_in_place() { let atlas = diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index b640c789d..117b83bc5 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -1087,6 +1087,8 @@ struct Game<'a> { lan: Option, /// Single player's server thread, which `remote` is connected to. integrated: Option, + /// This game is that server thread's (game/integrated.rs). + serving: Option, /// The open world is shared behind an invite code (game/friends.rs). friends: Option, world_options: Option, @@ -1447,9 +1449,13 @@ impl Game<'_> { .iter() .flat_map(|(_, m)| m.faces.iter()) .find(|f| f.quad.facing == horizon_assets::model::Direction::Up) - .or_else(|| models.iter().flat_map(|(_, m)| m.faces.iter()).next())?; - let name = self.pack.layer_names.get(&face.layer)?.clone(); - let tint = if face.quad.tint_index >= 0 { + .or_else(|| models.iter().flat_map(|(_, m)| m.faces.iter()).next()); + // Entity-rendered blocks may have a particle texture without any faces. + let layer = face + .map(|face| face.layer) + .or_else(|| models.iter().find_map(|(_, model)| model.particle))?; + let name = self.pack.layer_names.get(&layer)?.clone(); + let tint = if face.is_some_and(|face| face.quad.tint_index >= 0) { let n = &self.res.facts.get(state).name; if n.contains("leaves") || n.contains("vine") { 0x48B518 @@ -1777,6 +1783,12 @@ impl Game<'_> { }); self.process_level_events() } + fn attack_block(&mut self, pos: [i32; 3]) -> Result<()> { + self.with_level(|b, level, ticks| { + b.attack_block(level, ticks, BlockPos::new(pos[0], pos[1], pos[2])); + }); + self.process_level_events() + } /// Track which chunks are ticking (Ticks needs chunk load/unload). fn sync_tick_chunks(&mut self) { if !self.tick_chunks.synchronize(&self.world.chunks) { @@ -2885,6 +2897,7 @@ impl Game<'_> { return Ok(()); } if self.mining.as_ref().is_none_or(|m| m.pos != hit.pos) { + self.attack_block(hit.pos)?; self.mining = Some(Mining { pos: hit.pos, progress: 0.0, @@ -3091,6 +3104,7 @@ impl Game<'_> { self.eye = 1.62; self.previous_eye = 1.62; self.clock.reset(); + self.capture_loaded_world(); Ok(()) } fn tick(&mut self) -> Result<()> { @@ -3406,11 +3420,8 @@ impl Game<'_> { } if player.health.dead_or_dying() && self.screen != Screen::Dead { // Inventory.dropAll: every stack thrown in a random direction. - let at = [ - player.position.x, - player.position.y + 1.32, - player.position.z, - ]; + let feet = [player.position.x, player.position.y, player.position.z]; + let at = [feet[0], feet[1] + 1.32, feet[2]]; let killer = self.combat_killer_kind(); self.death_message(); // ServerPlayer.die: deaths, and the time-since counters restart. @@ -3426,6 +3437,10 @@ impl Game<'_> { } self.stats.reset("custom", "minecraft:time_since_death"); self.stats.reset("custom", "minecraft:time_since_rest"); + let reward = self + .experience + .death_reward(self.ui.rules.bool("keep_inventory") || self.spectator()); + self.spawn_orbs(feet, reward); if !self.creative && !self.ui.rules.bool("keep_inventory") { // Inventory.dropAll: slots 0..36, armor feet to head (36..39), off hand (40). let mut stacks: Vec = @@ -3634,8 +3649,8 @@ impl Game<'_> { } /// `Minecraft.pause`: a single-player game with a pausing screen open. fn pauses_game(&self) -> bool { - (self.remote.is_none() || self.integrated.is_some()) - && self.lan.is_none() + self.has_singleplayer_server() + && !self.is_published() && self.title.is_none() && (matches!( self.screen, @@ -3648,6 +3663,13 @@ impl Game<'_> { ) || self.book.is_some()) } fn capture(&mut self, capture: bool) { + // MouseHandler.grabMouse never captures an inactive window. + if capture && !self.focused { + return; + } + if !capture { + self.stop_mining(); + } // The real pointer takes over from an agent's. self.agent_pointer = false; let Some(window) = &self.window else { return }; @@ -3663,6 +3685,16 @@ impl Game<'_> { } window.set_cursor_visible(!self.captured); } + /// LevelLoadingScreen.onClose -> Gui.setScreen(null) -> grabMouse. + fn capture_loaded_world(&mut self) { + // Loading may finish after focus was lost or another screen opened. + if self.title.is_none() + && self.screen == Screen::Playing + && !self.remote.as_ref().is_some_and(|r| r.chat_input.is_some()) + { + self.capture(true); + } + } fn draw_item(&mut self, batch: &mut HudBatch, stack: &ItemStack, x: f32, y: f32) { // `GuiGraphics.renderItem`: the resolved item model (tints, foil and // special renderers included), then the decorations. @@ -3840,7 +3872,9 @@ impl Game<'_> { let spectator = self.spectator(); let (wi, hi) = (w as i32, h as i32); // Horizon's pause menu sits over the blurred world alone. - if !self.ui.hide_gui && !self.horizon_pause() { + let horizon_pause = self.horizon_pause(); + let show_hud = !self.ui.hide_gui && !horizon_pause; + if show_hud { self.draw_spyglass(&mut batch); // Hud.extractCrosshair / extractItemHotbar / extractPlayerHealth / // extractVehicleHealth and the experience bar, laid out by hud_layout. @@ -3890,7 +3924,7 @@ impl Game<'_> { let draws = hud_overlays::sleep_overlay(wi, hi, ticks as i32); hud_layout::paint(&mut batch, &self.atlas, &draws); } - if !self.ui.hide_gui { + if show_hud { self.draw_titles(&mut batch); self.draw_scoreboard(&mut batch); self.draw_subtitles(&mut batch); @@ -3966,15 +4000,17 @@ impl Game<'_> { } } let atlas = &self.atlas; - if !self.ui.hide_gui + if show_hud && let Some((text, at)) = &self.message && at.elapsed() < Duration::from_secs(3) { let tw = HudBatch::text_width(atlas, text); batch.text(atlas, text, (w - tw) / 2.0, h - 59.0, u32::MAX, true); } - self.draw_chat(&mut batch); - self.draw_tab_list(&mut batch); + if !horizon_pause { + self.draw_chat(&mut batch); + self.draw_tab_list(&mut batch); + } match self.screen { Screen::Paused if self.pause_menu => self.draw_pause_menu(&mut batch), // PauseScreen(false): only its title, at the top. @@ -4333,14 +4369,7 @@ impl Game<'_> { } else { ("Crafting", 97.0) }; - batch.text( - atlas, - title, - ox + tx, - oy + if self.craft_size == 3 { 6.0 } else { 8.0 }, - grey, - false, - ); + batch.text(atlas, title, ox + tx, oy + 6.0, grey, false); if self.craft_size == 3 { batch.text(atlas, "Inventory", ox + 8.0, oy + 72.0, grey, false); } @@ -5576,6 +5605,20 @@ impl Game<'_> { .block_entity_render .render(&scene, visible, &mut out); } + if let Some(mining) = &self.mining + && mining.progress > 0.0 + && self.atlas.sprites.contains_key(&format!( + "block/destroy_stage_{}", + ((mining.progress * 10.0) as u32).min(9) + )) + { + let state = self + .world + .block(mining.pos[0], mining.pos[1], mining.pos[2]); + self.res + .block_entity_render + .render_breaking(&scene, mining.pos, state.0, &mut out); + } } // Experience orbs (ExperienceOrbRenderer): pulsing billboards from a 4x4 icon sheet. if let Some(r) = self @@ -6637,6 +6680,9 @@ impl Game<'_> { std::env::current_dir()?.join("saves"), &self.options.resources, )); + if let Some(title) = &mut self.title { + title.last_server = self.settings.file.string("lastServer"); + } self.capture(false); Ok(()) } @@ -7642,7 +7688,15 @@ impl Game<'_> { /// The renderer for the window, with the current block sprites, shared /// atlas and world border texture. fn new_renderer(&self, window: Arc) -> Result { - let assets = &self.pack.assets; + self.renderer_for(window, &self.pack, &self.atlas) + } + fn renderer_for( + &self, + window: Arc, + pack: &PackResources, + atlas: &HudAtlas, + ) -> Result { + let assets = &pack.assets; let mut renderer = pollster::block_on(Renderer::new( window, self.options.offscreen, @@ -7652,12 +7706,12 @@ impl Game<'_> { assets.texture_layers, &assets.sprite_mips, &assets.animations, - (&self.atlas.pixels, self.atlas.width, self.atlas.height), - (&self.pack.forcefield.2, self.pack.forcefield.0), - &self.pack.panorama, + (&atlas.pixels, atlas.width, atlas.height), + (&pack.forcefield.2, pack.forcefield.0), + &pack.panorama, + self.renderer.as_ref(), ))?; - renderer.hud.fire_animations = self - .atlas + renderer.hud.fire_animations = atlas .fire_animations .iter() .map(|(rect, animation)| (*rect, animation.clone(), usize::MAX)) @@ -8058,7 +8112,7 @@ impl Game<'_> { self.creative_screen.search_focused.elapsed().as_millis(), ); } - let mut hovered: Option = None; + let mut hovered: Option = None; if tab.inventory { self.draw_player_preview( batch, @@ -8108,7 +8162,7 @@ impl Game<'_> { && self.cursor.1 < y + 16.0 { batch.fill(&self.atlas, x, y, 16.0, 16.0, rgba(255, 255, 255, 128)); - hovered = Some(stack.item.to_string()); + hovered = Some(stack.clone()); } } } @@ -8123,7 +8177,7 @@ impl Game<'_> { && self.cursor.1 >= y && self.cursor.1 < y + 16.0 { - hovered = Some(stack.item.to_string()); + hovered = Some(stack); } } let scrollable = self.creative_screen.rows(&self.pack.names) > 5; @@ -8159,8 +8213,8 @@ impl Game<'_> { } else if self.carried.is_none() && let Some(item) = hovered { - let name = self.pack.names.item(&item); - self.tooltip(batch, &name, self.cursor.0, self.cursor.1); + let lines = self.stack_tooltip(&item); + self.draw_tooltip_lines(batch, &lines, self.cursor.0, self.cursor.1); } self.draw_inventory_effects(batch, (ox, oy), Creative::WIDTH as i32); } @@ -8304,21 +8358,7 @@ impl Game<'_> { self.ender_items = items.clone(); } level.ender_items = self.ender_items.clone(); - level.active_effects = self - .potions - .effects - .list - .iter() - .map(|e| { - ( - format!("minecraft:{}", e.id), - e.duration, - e.amplifier, - e.ambient, - e.visible, - ) - }) - .collect(); + level.active_effects = self.potions.effects.saved(); } fn save_game(&mut self) -> Result<()> { self.save_game_with(true) @@ -8436,12 +8476,23 @@ impl Game<'_> { let debug_lines = horizon_client::gizmos::camera_relative(&lines, at, view); let debug_lines_on_top = horizon_client::gizmos::camera_relative(&on_top, at, view); let debug_crosshair = horizon_client::gizmos::camera_relative(&crosshair.on_top, at, view); - // LevelRenderer.renderHitOutline: the outline shape's edges. + // LevelExtractor.extractBlockOutline: model translucency controls draw order. + let mut outline_after_terrain = false; let outline = if self.screen == Screen::Playing && !self.spectator() { self.pick() .map(|h| { let state = self.world.block(h.pos[0], h.pos[1], h.pos[2]); let f = self.res.facts.get(state); + outline_after_terrain = self + .pack + .assets + .model_catalog + .collect(state, BlockPos::new(h.pos[0], h.pos[1], h.pos[2])) + .is_ok_and(|models| { + models + .iter() + .any(|(_, model)| model.faces.iter().any(|f| f.translucent)) + }); let o = if f.outline_offset { self.pack .context @@ -8591,6 +8642,7 @@ impl Game<'_> { }), dark_disc: self.dark_disc(camera), outline, + outline_after_terrain, debug_lines: &debug_lines, debug_lines_on_top: &debug_lines_on_top, debug_crosshair: &debug_crosshair, @@ -8854,7 +8906,13 @@ impl Game<'_> { } _ => {} }, - // key.swapOffhand: ServerboundPlayerActionPacket SWAP_ITEM_WITH_OFFHAND. + // key.swapOffhand: on a server, ServerboundPlayerActionPacket SWAP_ITEM_WITH_OFFHAND + // (the server swaps the hands and sends both slots back; spectators can't). + KeyCode::KeyF if self.screen == Screen::Playing && self.remote.is_some() => { + if !self.spectator() { + self.remote_swap_hands(); + } + } KeyCode::KeyF if self.screen == Screen::Playing => { std::mem::swap(&mut self.offhand, &mut self.inventory[self.selected]); self.using = None; @@ -9304,6 +9362,7 @@ impl ApplicationHandler for Game<'_> { clicked } }); + self.save_title_options(); if sound { self.audio.play_button_click(); } @@ -9356,6 +9415,21 @@ impl ApplicationHandler for Game<'_> { } WindowEvent::MouseInput { state, button, .. } => { let pressed = state == ElementState::Pressed; + if matches!(self.screen, Screen::Inventory | Screen::Creative) + && let Some(key) = self + .settings + .keys + .container_mouse_key(button, self.creative) + { + if pressed && self.carried.is_none() { + self.inventory_hotkey(key); + } + // Neither edge of a bound button is an ordinary slot click. + return Ok(()); + } + if pressed && self.sort_mouse_press(button) { + return Ok(()); + } match self.screen { Screen::Playing => { if pressed && !self.captured { @@ -9542,6 +9616,7 @@ impl ApplicationHandler for Game<'_> { if let Some(t) = self.title.as_mut() { t.escape(); } + self.save_title_options(); } _ => self.title_edit(event.text.as_deref(), false), } @@ -10078,6 +10153,7 @@ impl<'a> Game<'a> { remote: None, lan: None, integrated: None, + serving: None, world_options: None, options_to_world_options: false, command_block_screen: None, @@ -10139,7 +10215,13 @@ pub fn run() -> Result<()> { horizon_client::spans::enable(options.spikes.is_some()); let vanilla_root = options.resources.clone(); let early_settings = Settings::load(&std::env::current_dir()?.join("options.txt")); - options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs); + let mut pack_error = None; + options.resources = packs::startup_root(&vanilla_root, &early_settings.resource_packs) + .unwrap_or_else(|e| { + log::warn!("Horizon: resource packs not applied: {e}"); + pack_error = Some(e.to_string()); + vanilla_root.clone() + }); options.resources = mods::content_root( &options.resources, &early_settings.mods.enabled, @@ -10151,6 +10233,7 @@ pub fn run() -> Result<()> { // with the game's own resources. Err(e) if options.resources != vanilla_root => { log::warn!("Horizon: resource packs failed to load ({e}); using the default resources"); + pack_error = Some(e.to_string()); options.resources = vanilla_root.clone(); Assets::load(&vanilla_root)? } @@ -10164,7 +10247,11 @@ pub fn run() -> Result<()> { let root = options.resources.clone(); let assets = Arc::new(assets); log::info!("load assets: {:.2}s", started.elapsed().as_secs_f32()); - let facts = BlockFacts::load(&root, assets.registry)?; + let behaviour_json: serde_json::Value = serde_json::from_reader(std::io::BufReader::new( + std::fs::File::open(root.join("block-behaviours.json"))?, + ))?; + let (facts, gameplay_projection) = + BlockFacts::load_with_projection(&root, assets.registry, &behaviour_json)?; let mob_data = horizon_client::mobs::MobData::load(&root, &facts); horizon_client::mobs::trial_spawner::load_registry(&root); log::info!("load facts: {:.2}s", started.elapsed().as_secs_f32()); @@ -10231,7 +10318,12 @@ pub fn run() -> Result<()> { ambient: horizon_client::ambience::AmbientTables::load(&root), nether_environment: Environment::load_dimension(&root, "the_nether", false)?, end_environment: Environment::load_dimension(&root, "the_end", false)?, - behaviours: Behaviours::load(registry_owner.registry, &root)?, + behaviours: Behaviours::load_with_gameplay( + registry_owner.registry, + &root, + &behaviour_json, + &gameplay_projection, + )?, mob_types: MobTypes::load(&root)? .types .into_iter() @@ -10243,6 +10335,10 @@ pub fn run() -> Result<()> { player_arm: horizon_client::mobs::load_model(&root, "minecraft:player#main") .and_then(|m| m.children.into_iter().find(|c| c.name == "right_arm")), }); + // Both inputs are needed only while constructing the behaviour tables. + // Release their owned JSON/Arc buffers before entering the long-lived game loop. + drop(gameplay_projection); + drop(behaviour_json); let mut settings = Settings::load(&std::env::current_dir()?.join("options.txt")); horizon_client::ui_theme::load_preferences(&std::env::current_dir()?); // Scripted and frame-limited runs (the parity harnesses) see vanilla's @@ -10257,6 +10353,10 @@ pub fn run() -> Result<()> { } let mut game = Game::new(&resources, pack, atlas, options, settings, audio)?; game.ui.vanilla_root = vanilla_root; + if let Some(error) = pack_error { + game.open_options_at(true, horizon_client::options_ui::Page::Packs); + game.ui.options.resource_error = Some(error); + } if game.options.benchmark.is_some() { // The same conditions on every machine: no VSync, no frame cap, no // pause or idle limit without focus. In memory only, never saved. @@ -10325,10 +10425,11 @@ pub fn run() -> Result<()> { game.open_world(&folder, None, Some(seed), creative, None)?; } None => { - let title = Title::new( + let mut title = Title::new( std::env::current_dir()?.join("saves"), &game.options.resources, ); + title.last_server = game.settings.file.string("lastServer"); // The launcher's opening, once per session and only in Horizon's style. if horizon_client::ui_theme::horizon() && game.options.frames.is_none() { title diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs index cd39606b3..48b24bbb3 100644 --- a/crates/horizon-client/src/game/agent.rs +++ b/crates/horizon-client/src/game/agent.rs @@ -460,7 +460,10 @@ impl Game<'_> { json!({ "screen": self.screen_label(), "menu": self.title.as_ref().map(|_| self.title_value()), - "world": (self.title.is_none()).then(|| self.level.name.clone()), + "world": (self.title.is_none()).then(|| match &self.integrated { + Some(server) => server.world_name.clone(), + None => self.level.name.clone(), + }), "dimension": self.dim.id(), "gamemode": format!("{:?}", self.mode).to_lowercase(), "time": { "day_time": self.world.day_time, "day": self.world.day_time.div_euclid(24000), "time_of_day": self.world.day_time.rem_euclid(24000) }, diff --git a/crates/horizon-client/src/game/chat.rs b/crates/horizon-client/src/game/chat.rs index 8f6bed0ca..bcf6426e1 100644 --- a/crates/horizon-client/src/game/chat.rs +++ b/crates/horizon-client/src/game/chat.rs @@ -425,6 +425,12 @@ impl Game<'_> { capture.push(text); return; } + // Connected to a server (single player's own included), the chat on + // screen is the connection's: the game's own messages join it. + if self.remote.is_some() { + self.chat_line(text.plain()); + return; + } let focused = self.screen == Screen::Chat; let heard = self.mods.mods.is_running().then(|| text.plain()); self.ui.chat.add( diff --git a/crates/horizon-client/src/game/commands/extra.rs b/crates/horizon-client/src/game/commands/extra.rs index ab41596ee..a90648043 100644 --- a/crates/horizon-client/src/game/commands/extra.rs +++ b/crates/horizon-client/src/game/commands/extra.rs @@ -758,18 +758,25 @@ impl Game<'_> { .and_then(|l| l.local_addr()) .map_or(25564, |a| i32::from(a.port())), }; - if let Some(lan) = &self.lan { - return Err(err( - "commands.publish.alreadyPublished.lan", - &[&lan.settings.port], - )); + let published = if self.serving() { + self.published_port() + } else { + self.lan.as_ref().map(|l| l.settings.port) + }; + if let Some(port) = published { + return Err(err("commands.publish.alreadyPublished.lan", &[&port])); } let settings = super::super::lan::LanSettings { port: port as u16, guest_commands: allow, force_game_mode: false, }; - if self.lan_start(settings).is_err() { + let started = if self.serving() { + self.publish(settings).is_ok() + } else { + self.lan_start(settings).is_ok() + }; + if !started { return Err(err("commands.publish.failed", &[])); } let port_text = port.to_string(); @@ -785,6 +792,13 @@ impl Game<'_> { Ok(port) } pub(super) fn cmd_unpublish(&mut self) -> R { + if self.serving() { + if !self.unpublish() { + return Err(err("commands.unpublish.notPublished", &[])); + } + self.feedback(Text::translate("commands.unpublish.success", &[])); + return Ok(1); + } if self.lan.is_none() { return Err(err("commands.unpublish.notPublished", &[])); } diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index e1169b2c8..cf7510065 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -29,7 +29,7 @@ const HELP: &[&str] = &[ "chat TEXT send a chat line or /command as if typed", "type TEXT type into the open chat box (key enter tap sends)", "chat-log [N] the newest N chat messages (default 10)", - "options [PAGE] open the options (video sound controls keybinds mouse chat accessibility language font packs graphics graphics-advanced)", + "options [PAGE] open the options (video sound controls keybinds mouse chat accessibility language font packs mods graphics graphics-advanced)", "stats [reset] frame-time summary since the last reset", "bench SECONDS reset stats, wait, then reply with the summary", "fly X1 Y1 Z1 X2 Y2 Z2 SECONDS [PITCH [raw]] fly the camera along a line at constant speed, then reply with the frame summary (raw: every frame time too)", @@ -462,8 +462,10 @@ impl Game<'_> { "language" => Page::Language, "font" => Page::Font, "packs" => Page::Packs, + "mods" => Page::Mods, "graphics" => Page::Graphics, "graphics-advanced" => Page::GraphicsAdvanced, + "extras" => Page::Extras, other => return Err(format!("unknown options page {other}").into()), }; if self.screen == Screen::Playing && self.title.is_none() { @@ -475,7 +477,10 @@ impl Game<'_> { } "chat-log" => { let n = words.get(1).map_or(Ok(10), |n| n.parse())?; - Ok(Some(json!({ "chat": self.ui.chat.recent_lines(n) }))) + let lines = self + .remote_chat(n) + .unwrap_or_else(|| self.ui.chat.recent_lines(n)); + Ok(Some(json!({ "chat": lines }))) } "stats" => { let summary = self.frame_stats.summary(); @@ -549,18 +554,20 @@ impl Game<'_> { self.leave_server(None)?; ok() } - "save" => { + "save" | "save-all" => { self.require_world()?; - self.save_game()?; - ok() - } - "save-all" => { - // Like `save-all flush`: also rewrite chunks that were not modified - // (the save round-trip harness compares them with vanilla's files). - self.require_world()?; - let keys: Vec<_> = self.world.chunks.keys().copied().collect(); - self.world.modified.extend(keys); - self.save_game()?; + // `save-all` is like `save-all flush`: it also rewrites chunks + // that were not modified (the save round-trip harness compares + // them with vanilla's files). + let all = words[0] == "save-all"; + // Single player's world is its server thread's. + if let Some(server) = &self.integrated { + server + .call(move |g| g.control_save(all).map_err(|e| e.to_string())) + .ok_or("the integrated server stopped")??; + } else { + self.control_save(all)?; + } ok() } "dragon-hurt" => { @@ -581,6 +588,15 @@ impl Game<'_> { } } + /// `horizon-ctl save` and `save-all`. + fn control_save(&mut self, all: bool) -> Result<()> { + if all { + let keys: Vec<_> = self.world.chunks.keys().copied().collect(); + self.world.modified.extend(keys); + } + self.save_game() + } + fn require_world(&self) -> Result<()> { if self.title.is_some() { return Err("no world open; use: open WORLD [SEED] [creative]".into()); diff --git a/crates/horizon-client/src/game/debug_keys.rs b/crates/horizon-client/src/game/debug_keys.rs index 3aac02ae7..36761a85e 100644 --- a/crates/horizon-client/src/game/debug_keys.rs +++ b/crates/horizon-client/src/game/debug_keys.rs @@ -38,7 +38,7 @@ impl DebugHost for Host<'_, '_> { // the menu as it always has (key_pressed). let g = &mut *self.game; if without_menu && g.screen == Screen::Playing { - g.pause_menu = !(g.remote.is_none() && g.lan.is_none()); + g.pause_menu = !(g.has_singleplayer_server() && !g.is_published()); g.screen = Screen::Paused; g.capture(false); g.mining = None; @@ -194,15 +194,17 @@ impl Game<'_> { } return; }; - let received = r - .session - .counters - .lock() - .unwrap() - .as_ref() - .map(|c| c.bytes_in.load(std::sync::atomic::Ordering::Relaxed)); - if let Some(total) = received { - self.debug_charts.log_bandwidth(total); + let received = r.session.counters.lock().unwrap().as_ref().map(|c| { + let order = std::sync::atomic::Ordering::Relaxed; + [ + c.bytes_in.load(order), + c.sent.load(order), + c.received.load(order), + ] + }); + if let Some([bytes, sent, received]) = received { + self.debug_charts.log_bandwidth(bytes); + self.debug_charts.log_packets([sent, received]); } if shown { use horizon_protocol::packets::{play::Serverbound, status::Ping}; @@ -403,8 +405,9 @@ impl Game<'_> { ], ); } - // tps: DebugEntryTps for the integrated server (it counts no packets). - if self.player.is_some() && self.remote.is_none() { + // tps: DebugEntryTps for local and remote connections. + if self.player.is_some() { + let [tx, rx] = self.debug_charts.packet_rates.map(|n| n.round() as i64); let (target, status) = if self.clock.sprinting() { ("-".to_owned(), " (sprinting)") } else if self.clock.stepping() { @@ -417,10 +420,22 @@ impl Game<'_> { } else { (format!("{:.1}", 1000.0 / self.clock.rate()), "") }; - d.line(format!( - "Integrated server @ {:.1}/{target} ms{status}, 0 tx, 0 rx", - self.debug_charts.smoothed_tick_ms - )); + if self.remote.is_none() || self.integrated.is_some() { + let tick_ms = self + .integrated + .as_ref() + .map_or(self.debug_charts.smoothed_tick_ms, |server| { + server.smoothed_tick_ms() + }); + d.line(format!( + "Integrated server @ {tick_ms:.1}/{target} ms{status}, {tx} tx, {rx} rx", + )); + } else if let Some(remote) = &self.remote { + d.line(format!( + "\"{}\" server{status}, {tx} tx, {rx} rx", + remote.brand + )); + } } let (left, right) = d.columns(&self.debug_chart_lines()); let atlas = &self.atlas; diff --git a/crates/horizon-client/src/game/dedicated.rs b/crates/horizon-client/src/game/dedicated.rs index 6f1ba3fee..d22e486b9 100644 --- a/crates/horizon-client/src/game/dedicated.rs +++ b/crates/horizon-client/src/game/dedicated.rs @@ -481,6 +481,7 @@ impl Game<'_> { seed: self.level.seed, max_players: props.max_players.max(0) as usize, dedicated: true, + integrated: false, }; let settings = LanSettings { port, diff --git a/crates/horizon-client/src/game/extras.rs b/crates/horizon-client/src/game/extras.rs index c6ac6d25e..712796a81 100644 --- a/crates/horizon-client/src/game/extras.rs +++ b/crates/horizon-client/src/game/extras.rs @@ -58,8 +58,12 @@ impl Game<'_> { pub(super) fn extras_scroll(&mut self, steps: f32) -> bool { self.settings.extras.zoom && self.extras.zoom.scroll(steps) } - /// Mouse buttons while playing, for the keystroke overlay. + /// Mouse buttons while playing, for zoom and the keystroke overlay. pub(super) fn extras_mouse(&mut self, button: MouseButton, pressed: bool) { + let e = &self.settings.extras; + if e.zoom && e.zoom_key == keybinds::mouse_name(button) { + self.extras.zoom.key(pressed, false, true, e.zoom_level); + } let i = match button { MouseButton::Left => 0, MouseButton::Right => 1, diff --git a/crates/horizon-client/src/game/hand.rs b/crates/horizon-client/src/game/hand.rs index 76d626716..a932913af 100644 --- a/crates/horizon-client/src/game/hand.rs +++ b/crates/horizon-client/src/game/hand.rs @@ -30,6 +30,18 @@ fn attack_range_contains(range: &serde_json::Value, distance: f64, creative: boo } impl Game<'_> { + /// Opening a screen stops breaking before the next game tick (which may pause). + pub(super) fn stop_mining(&mut self) { + let mut destroying = std::mem::take(&mut self.clicks.destroying); + destroying.stop(&mut GameHands { + game: self, + error: None, + }); + self.clicks.destroying = destroying; + self.attack_clicks = 0; + self.stop_remote_mining(); + self.mining = None; + } /// `LivingEntity.swing` with `getModifiedSwingDuration`. pub(super) fn swing_hand(&mut self, hand: Hand, animation: SwingAnimation) { let effects = &self.potions.effects; @@ -397,7 +409,10 @@ impl HandWorld for GameHands<'_, '_> { fn within_border(&mut self, _: [i32; 3]) -> bool { true } - fn attack_block(&mut self, _: [i32; 3]) {} + fn attack_block(&mut self, pos: [i32; 3]) { + let result = self.game.attack_block(pos); + self.keep(result); + } fn destroy_progress(&mut self, pos: [i32; 3]) -> f32 { self.game.destroy_progress(pos) } diff --git a/crates/horizon-client/src/game/integrated.rs b/crates/horizon-client/src/game/integrated.rs index 6edbb6e40..1ecdbff28 100644 --- a/crates/horizon-client/src/game/integrated.rs +++ b/crates/horizon-client/src/game/integrated.rs @@ -2,6 +2,10 @@ //! its own "Server thread", as vanilla's does, and the window joins it as a //! player through memory, so a world tick never holds up a frame. //! +//! The window asks its server for what only the server knows (Open to LAN, +//! the world's options) with [`Integrated::call`], as vanilla's client calls +//! into its `IntegratedServer`. +//! //! Opt-in for now (`--server-thread` or `HORIZON_SERVER_THREAD=1`): the //! player joins as a network player, and the server side of a network player //! does not do everything single player does yet. Worlds whose player is @@ -15,7 +19,7 @@ use horizon_client::{ Result, audio::Audio, hud::HudAtlas, - lan::{MAX_PLAYERS, NetConfig, hub::HubConfig}, + lan::{Lan, MAX_PLAYERS, NetConfig, hub::HubConfig}, save::Level, settings::Settings, title::WorldOptions, @@ -23,11 +27,12 @@ use horizon_client::{ use horizon_protocol::connection::Pipe; use horizon_protocol::{crypto::offline_uuid, text::translatable}; use std::{ + net::TcpListener, path::Path, sync::{ Arc, - atomic::{AtomicBool, Ordering}, - mpsc::{Sender, channel}, + atomic::{AtomicBool, AtomicU16, AtomicU32, Ordering}, + mpsc::{Receiver, Sender, channel, sync_channel}, }, thread::JoinHandle, time::{Duration, Instant}, @@ -39,18 +44,49 @@ struct Flags { /// `Minecraft.pause`: a single-player pause screen is open. paused: AtomicBool, stop: AtomicBool, + /// `IntegratedServer.getPort`: the port it is open to the LAN on, 0 + /// while it is not. + published: AtomicU16, + smoothed_tick_ms: AtomicU32, } +/// Work the window hands its server thread (`MinecraftServer.execute`). +type Task = Box) + Send>; + /// The window's handle on its server thread. pub(super) struct Integrated { flags: Arc, thread: Option>, + tasks: Sender, + /// The world's name (`level.dat`'s `LevelName`), for the pause menu. + pub world_name: String, } impl Integrated { + pub fn smoothed_tick_ms(&self) -> f32 { + f32::from_bits(self.flags.smoothed_tick_ms.load(Ordering::Relaxed)) + } /// `IntegratedServer.tickServer` skips the world while the game is paused. pub fn set_paused(&self, paused: bool) { self.flags.paused.store(paused, Ordering::Relaxed); } + /// `IntegratedServer.getPort` while `isPublished`. + pub fn published(&self) -> Option { + Some(self.flags.published.load(Ordering::Relaxed)).filter(|&p| p != 0) + } + /// `executeBlocking`: run `f` on the server thread between two ticks and + /// wait for its answer; `None` once the server has stopped. + pub fn call( + &self, + f: impl FnOnce(&mut Game<'_>) -> T + Send + 'static, + ) -> Option { + let (tx, rx) = sync_channel(1); + self.tasks + .send(Box::new(move |g| { + let _ = tx.send(f(g)); + })) + .ok()?; + rx.recv_timeout(Duration::from_secs(10)).ok() + } /// `IntegratedServer.halt(true)`: returns once the world is saved and let go. pub fn stop(mut self) { self.halt(); @@ -68,6 +104,25 @@ impl Drop for Integrated { } } +/// The server thread's side of the window's handle (`Game::serving`). +pub(super) struct Serving { + flags: Arc, + tasks: Receiver, + /// The window's profile. + owner: u128, + /// Open to LAN or closed from inside a player's command, while the hub + /// is out of its place: applied after the tick. + pending: Option, +} +enum Publish { + Open(TcpListener, LanSettings), + Close, +} + +/// What the server thread hands the window once the world is open: its end +/// of the connection and the world's name, or why it failed. +type Ready = std::result::Result<(Pipe, String), String>; + /// The world to open and the player who owns it. struct Launch { folder: String, @@ -75,8 +130,11 @@ struct Launch { seed: Option, creative: bool, create: Option, - /// The window's profile: the only one let in, and the world's player. + /// The window's profile: the only one let in until the world is open to + /// the LAN, and the world's player. owner: u128, + /// Its name, for the LAN's MOTD. + player: String, } /// Whether this world opens on a server thread: asked for, and its player @@ -88,6 +146,19 @@ pub(super) fn wanted(options: &Options, world: &Path) -> bool { } impl Game<'_> { + /// `Minecraft.hasSingleplayerServer`: a world of this window's own, run + /// in the frame or on its server thread. + pub(super) fn has_singleplayer_server(&self) -> bool { + self.remote.is_none() || self.integrated.is_some() + } + /// `IntegratedServer.isPublished`: the world is open to the network. + pub(super) fn is_published(&self) -> bool { + self.lan.is_some() + || self + .integrated + .as_ref() + .is_some_and(|s| s.published().is_some()) + } /// `Minecraft.doWorldLoad`: start the server thread on the world, wait /// until it can be joined, then join it through memory. pub(super) fn open_integrated( @@ -110,6 +181,7 @@ impl Game<'_> { create: create.cloned(), // An offline-mode server names its players by their offline profile. owner: offline_uuid(&player), + player: player.clone(), }; let mut options = self.options.clone(); // A server without a console: no local player, no window, no control socket. @@ -127,6 +199,7 @@ impl Game<'_> { let (res, pack, settings) = (self.res.clone(), self.pack.clone(), self.settings.clone()); let flags = Arc::new(Flags::default()); let (ready_tx, ready) = channel(); + let (tasks, server_tasks) = channel(); let server_flags = flags.clone(); let thread = std::thread::Builder::new() .name("Server thread".into()) @@ -137,7 +210,8 @@ impl Game<'_> { options, settings, launch, - &server_flags, + server_flags, + server_tasks, &ready_tx, ); if let Err(e) = result { @@ -145,12 +219,17 @@ impl Game<'_> { let _ = ready_tx.send(Err(e.to_string())); } })?; - let handle = Integrated { + let mut handle = Integrated { flags, thread: Some(thread), + tasks, + world_name: String::new(), }; let pipe = match ready.recv() { - Ok(Ok(pipe)) => pipe, + Ok(Ok((pipe, world_name))) => { + handle.world_name = world_name; + pipe + } Ok(Err(e)) => return Err(e.into()), Err(_) => return Err("the integrated server stopped".into()), }; @@ -164,8 +243,9 @@ impl Game<'_> { fn run_integrated( &mut self, launch: Launch, - flags: &Flags, - ready: &Sender>, + flags: Arc, + tasks: Receiver, + ready: &Sender, ) -> Result<()> { self.open_world( &launch.folder, @@ -178,7 +258,8 @@ impl Game<'_> { let view_distance = self.settings.render_distance.clamp(2, 32); let config = HubConfig { default_game_mode: self.level.mode(), - guest_commands: true, + // Only the owner until it is opened to the LAN. + guest_commands: false, force_game_mode: false, hardcore: self.level.hardcore, view_distance, @@ -188,23 +269,28 @@ impl Game<'_> { max_players: MAX_PLAYERS, // No player of its own: the hub runs without a host. dedicated: true, + integrated: true, }; let owner = launch.owner; + let published = flags.clone(); let net = NetConfig { - // No network until it is opened to the LAN. + // No network until it is opened to the LAN; the memory connection + // is not compressed (`Connection.isMemoryConnection`). listen: false, - // A memory connection is not compressed (`Connection.isMemoryConnection`). - compression_threshold: -1, - enable_status: false, - announce: false, gate: Some(Arc::new(move |_, uuid, _| { - (uuid != owner).then(|| translatable("multiplayer.disconnect.not_whitelisted", &[])) + (uuid != owner && published.published.load(Ordering::Relaxed) == 0) + .then(|| translatable("multiplayer.disconnect.not_whitelisted", &[])) })), - ..NetConfig::lan(0, self.level.name.clone(), MAX_PLAYERS) + // IntegratedServer.initServer: the MOTD names the owner and the world. + ..NetConfig::lan( + 0, + format!("{} - {}", launch.player, self.level.name), + MAX_PLAYERS, + ) }; let settings = LanSettings { port: 0, - guest_commands: true, + guest_commands: false, force_game_mode: false, }; self.lan_host(settings, net, config, None)?; @@ -223,18 +309,32 @@ impl Game<'_> { }); host.hub.lan.connect_local()? }; - let _ = ready.send(Ok(pipe)); + self.serving = Some(Serving { + flags: flags.clone(), + tasks, + owner, + pending: None, + }); + let _ = ready.send(Ok((pipe, self.level.name.clone()))); self.settle_spawn()?; let mut next = Instant::now(); while !flags.stop.load(Ordering::Relaxed) { + self.run_tasks(); self.update_dedicated_streaming(); self.update_world(); if flags.paused.load(Ordering::Relaxed) { // Only the connections run. self.tick_lan(); } else { + let started = Instant::now(); self.tick()?; + self.debug_charts.log_tick(started, started.elapsed()); + flags.smoothed_tick_ms.store( + self.debug_charts.smoothed_tick_ms.to_bits(), + Ordering::Relaxed, + ); } + self.apply_publish(); let rate = self.clock.rate().max(1.0); next += Duration::from_secs_f64(1.0 / f64::from(rate)); let now = Instant::now(); @@ -250,6 +350,10 @@ impl Game<'_> { /// `MinecraftServer.stopServer`: the players are saved and sent away, /// then the world is saved. fn stop_integrated(&mut self) -> Result<()> { + // IntegratedServer.stopServer: teardownPublishedState. + if let Some(s) = self.serving.take() { + s.flags.published.store(0, Ordering::Relaxed); + } if let Some(mut host) = self.lan.take() { for g in &host.hub.guests { self.save_guest(&host, g); @@ -272,18 +376,191 @@ impl Game<'_> { } } +/// What the world options screen shows of an integrated server. +#[derive(Clone, Copy, Debug)] +pub(super) struct ServerOptions { + pub default_mode: i32, + /// The owner's own game mode. + pub personal_mode: i32, + pub cheats: bool, + pub difficulty: u8, + pub hardcore: bool, + pub difficulty_locked: bool, + pub port: Option, + pub guest_commands: bool, + pub force: bool, +} + +/// The server thread's side. +impl Game<'_> { + /// Whether this game is an integrated server's. + pub(super) fn serving(&self) -> bool { + self.serving.is_some() + } + /// The window's requests, between two ticks. + fn run_tasks(&mut self) { + let tasks: Vec = match &self.serving { + Some(s) => s.tasks.try_iter().collect(), + None => return, + }; + for task in tasks { + task(self); + self.apply_publish(); + } + } + /// `IntegratedServer.getPort` while published. + pub(super) fn published_port(&self) -> Option { + let s = self.serving.as_ref()?; + Some(s.flags.published.load(Ordering::Relaxed)).filter(|&p| p != 0) + } + /// `IntegratedServer.publishServer`: listen on `settings.port` (0 picks + /// one) and announce the world. The socket is bound at once so a busy + /// port fails here; the hub starts listening once it is at hand. + pub(super) fn publish(&mut self, settings: LanSettings) -> std::result::Result { + let Some(s) = self.serving.as_mut() else { + return Err("not an integrated server".into()); + }; + if s.flags.published.load(Ordering::Relaxed) != 0 { + return Err("already published".into()); + } + let listener = Lan::bind(None, settings.port).map_err(|e| e.to_string())?; + let port = listener.local_addr().map_err(|e| e.to_string())?.port(); + log::info!("Published LAN server on port {port}"); + s.flags.published.store(port, Ordering::Relaxed); + s.pending = Some(Publish::Open(listener, LanSettings { port, ..settings })); + self.apply_publish(); + Ok(port) + } + /// `IntegratedServer.unpublishServer`: stop listening and send every + /// player but the owner away. False when it was not published. + pub(super) fn unpublish(&mut self) -> bool { + let Some(s) = self.serving.as_mut() else { + return false; + }; + if s.flags.published.swap(0, Ordering::Relaxed) == 0 { + return false; + } + s.pending = Some(Publish::Close); + self.apply_publish(); + true + } + /// Publish or unpublish for real, once the hub is in its place (a + /// player's `/publish` runs while it is out). + fn apply_publish(&mut self) { + if self.lan.is_none() { + return; + } + let Some(s) = self.serving.as_mut() else { + return; + }; + let (Some(pending), owner) = (s.pending.take(), s.owner) else { + return; + }; + let allow_commands = self.allow_commands(); + let Some(host) = self.lan.as_mut() else { + return; + }; + match pending { + Publish::Open(listener, settings) => { + if let Err(e) = host.hub.lan.listen_on(listener, true) { + log::error!("Horizon: Open to LAN: {e}"); + } + host.settings = settings; + host.hub.config.force_game_mode = settings.force_game_mode; + // IntegratedServer.setGuestCommandAccess, then updatePermissions. + host.hub.config.guest_commands = settings.guest_commands && allow_commands; + host.hub.resend_commands(); + } + Publish::Close => { + host.hub.lan.stop_listening(); + log::info!( + "Unpublishing integrated server (was on port {})", + host.settings.port + ); + let others: Vec = host + .hub + .guests + .iter() + .filter(|g| g.uuid != owner) + .map(|g| g.conn) + .collect(); + for conn in others { + host.hub.kick( + conn, + translatable("multiplayer.disconnect.server_shutdown", &[]), + ); + } + } + } + } + /// The world options screen's state, read on the server. + pub(super) fn server_options(&self) -> ServerOptions { + let owner = self.serving.as_ref().map(|s| s.owner); + let host = self.lan.as_ref(); + let personal_mode = host + .and_then(|h| h.hub.guests.iter().find(|g| Some(g.uuid) == owner)) + .map_or(self.level.mode(), |g| g.game_mode); + let published = self.published_port(); + ServerOptions { + default_mode: self.level.mode(), + personal_mode, + cheats: self.allow_commands(), + difficulty: self.ui.difficulty, + hardcore: self.level.hardcore, + difficulty_locked: self.level.difficulty_locked, + port: published, + guest_commands: published.is_some() && host.is_some_and(|h| h.settings.guest_commands), + force: published.is_none() || host.is_some_and(|h| h.settings.force_game_mode), + } + } + /// `IntegratedServer.setPersonalGameType`: the owner's game mode. + pub(super) fn set_owner_mode(&mut self, mode: i32) { + let Some(owner) = self.serving.as_ref().map(|s| s.owner) else { + return; + }; + if let Some(host) = self.lan.as_mut() + && let Some(conn) = host + .hub + .guests + .iter() + .find(|g| g.uuid == owner) + .map(|g| g.conn) + { + host.hub.set_game_mode(conn, mode); + } + } + /// `IntegratedServer.setWorldAllowCommands`: the owner is an operator + /// while the world allows commands; everyone's level is sent again. + pub(super) fn owner_allow_commands(&mut self, allow: bool) { + let Some(owner) = self.serving.as_ref().map(|s| s.owner) else { + return; + }; + let published = self.published_port().is_some(); + if let Some(host) = self.lan.as_mut() { + host.hub.ops.insert(owner, if allow { 4 } else { 0 }); + if published { + host.hub.config.guest_commands = allow && host.settings.guest_commands; + } + host.hub.resend_commands(); + } + } +} + /// The server thread: a game of its own over the shared resources. +#[allow(clippy::too_many_arguments)] fn serve( res: &Arc, pack: PackResources, options: Options, settings: Settings, launch: Launch, - flags: &Flags, - ready: &Sender>, + flags: Arc, + tasks: Receiver, + ready: &Sender, ) -> Result<()> { - let atlas = HudAtlas::build(&options.resources)?; + // It draws nothing: only the font, which books and chat measure with. + let atlas = HudAtlas::fonts(&options.resources)?; let audio = Audio::silent(&options.resources); let mut server = Game::new(res, pack, atlas, options, settings, audio)?; - server.run_integrated(launch, flags, ready) + server.run_integrated(launch, flags, tasks, ready) } diff --git a/crates/horizon-client/src/game/lan.rs b/crates/horizon-client/src/game/lan.rs index c416c202f..f6fc1e860 100644 --- a/crates/horizon-client/src/game/lan.rs +++ b/crates/horizon-client/src/game/lan.rs @@ -31,7 +31,7 @@ use std::{ static SYNC: OnceLock, String>> = OnceLock::new(); /// The settings of the open world (WorldOptionsScreen's multiplayer section). -#[derive(Clone, Debug)] +#[derive(Clone, Copy, Debug)] pub struct LanSettings { pub port: u16, pub guest_commands: bool, @@ -316,6 +316,15 @@ impl Game<'_> { if words[0] != "lan" { return Ok(None); } + if let Some(server) = &self.integrated + && words.get(1) == Some(&"status") + { + // The hub is the server thread's. + let status = server + .call(|g| g.lan_status()) + .ok_or("the integrated server stopped")?; + return Ok(Some(status)); + } match words.get(1).copied() { Some("open") => { if self.title.is_some() { @@ -337,43 +346,58 @@ impl Game<'_> { self.lan_close(true); Ok(Some(json!({ "ok": true }))) } - Some("status") => Ok(Some(match &self.lan { - Some(h) => json!({ - "open": true, - "host": h.host_name, - "port": h.settings.port, - "guests": h.hub.guests.iter().map(|g| json!({ - "name": g.name, - "position": g.position, - "placed": g.placed, - "loaded": g.loaded, - "game_mode": g.game_mode, - "sneaking": g.sneaking, - "health": g.health, - // FoodData: food level, saturation and exhaustion. - "food": [ - f64::from(g.food), - f64::from(g.saturation), - g.motion.as_ref().map_or(0.0, |m| f64::from(m.health.food.exhaustion)), - ], - "chunks": g.sent_chunks.len(), - "tracked": g.tracker.len(), - // The armour the host draws on this guest, head to feet. - "model_armor": h.models.get(&g.conn).map(|m| { - [&m.extra.head, &m.extra.chest, &m.extra.legs, &m.extra.feet] - .map(|s| s.as_deref()) - }), - })).collect::>(), - }), - None => json!({ "open": false }), - })), + Some("status") => Ok(Some(self.lan_status())), _ => Err("lan open|close|status".into()), } } + /// `horizon-ctl lan status`. + fn lan_status(&self) -> serde_json::Value { + use serde_json::json; + match &self.lan { + Some(h) if !self.serving() || self.published_port().is_some() => json!({ + "open": true, + "host": h.host_name, + "port": h.settings.port, + "guests": h.hub.guests.iter().map(|g| json!({ + "name": g.name, + "position": g.position, + "placed": g.placed, + "loaded": g.loaded, + "game_mode": g.game_mode, + "sneaking": g.sneaking, + "health": g.health, + // FoodData: food level, saturation and exhaustion. + "food": [ + f64::from(g.food), + f64::from(g.saturation), + g.motion.as_ref().map_or(0.0, |m| f64::from(m.health.food.exhaustion)), + ], + "chunks": g.sent_chunks.len(), + "tracked": g.tracker.len(), + // The armour the host draws on this guest, head to feet. + "model_armor": h.models.get(&g.conn).map(|m| { + [&m.extra.head, &m.extra.chest, &m.extra.legs, &m.extra.feet] + .map(|s| s.as_deref()) + }), + })).collect::>(), + }), + _ => json!({ "open": false }), + } + } + pub(super) fn lan_open(&mut self, settings: LanSettings) -> Result<()> { - self.lan_start(settings)?; - let port = self.lan.as_ref().map_or(0, |l| l.settings.port); + let port = if let Some(server) = &self.integrated { + // ShareToLanScreen: IntegratedServer.publishServer. + server + .call(move |g| g.publish(settings)) + .ok_or("the integrated server stopped")?? + } else if self.serving() { + return self.publish(settings).map(drop).map_err(Into::into); + } else { + self.lan_start(settings)?; + self.lan.as_ref().map_or(0, |l| l.settings.port) + }; self.system_message(Text::translate( "menu.multiplayerOptions.publish.started.lan", &[Text::literal(port.to_string())], @@ -408,6 +432,7 @@ impl Game<'_> { seed: self.level.seed, max_players: MAX_PLAYERS, dedicated: false, + integrated: false, }; let net = NetConfig::lan(settings.port, motd, MAX_PLAYERS); self.lan_host(settings, net, config, Some(host_uuid)) @@ -484,6 +509,22 @@ impl Game<'_> { /// IntegratedServer.unpublishServer: the guests are sent away. pub(super) fn lan_close(&mut self, announce: bool) { + if self.integrated.is_some() || self.serving() { + let closed = match &self.integrated { + Some(server) => server.call(|g| g.unpublish()).unwrap_or(false), + None => self.unpublish(), + }; + if closed { + self.friends_lan_closed(); + } + if closed && announce && self.integrated.is_some() { + self.system_message(Text::translate( + "menu.multiplayerOptions.publish.stopped", + &[], + )); + } + return; + } self.friends_lan_closed(); let Some(mut host) = self.lan.take() else { return; @@ -541,16 +582,29 @@ impl Game<'_> { /// New command access and game mode forcing settings of the open world. pub(super) fn lan_apply_settings(&mut self, guest_commands: bool, force: bool) { + if let Some(server) = &self.integrated { + server.call(move |g| g.lan_apply_settings(guest_commands, force)); + return; + } + // An integrated server gives guests commands only where the world + // allows them (IntegratedServer.getCustomPermissionLevel). + let effective = guest_commands && (!self.serving() || self.allow_commands()); if let Some(host) = self.lan.as_mut() { host.settings.guest_commands = guest_commands; host.settings.force_game_mode = force; - host.hub.config.guest_commands = guest_commands; + host.hub.config.guest_commands = effective; host.hub.config.force_game_mode = force; host.hub.resend_commands(); } } pub(super) fn lan_port(&self) -> Option { + if let Some(server) = &self.integrated { + return server.published(); + } + if self.serving() { + return self.published_port(); + } self.lan.as_ref().map(|h| h.settings.port) } @@ -630,6 +684,8 @@ impl Game<'_> { let Some(mut host) = self.lan.take() else { return; }; + // What the tick sends a guest goes to its connection at the end. + let holding = lan::Holding::start(); let state = self.lan_host_state(&host); // A dedicated server runs the hub without a host player. if state.is_some() || host.hub.config.dedicated { @@ -729,6 +785,10 @@ impl Game<'_> { self.lan_swings(&mut host); self.lan_update_streaming(&host); self.lan_update_models(&mut host); + drop(holding); + for g in &host.hub.guests { + g.sender.flush(); + } self.lan = Some(host); } diff --git a/crates/horizon-client/src/game/lan_actions.rs b/crates/horizon-client/src/game/lan_actions.rs index 6bf76f160..af7ff10f8 100644 --- a/crates/horizon-client/src/game/lan_actions.rs +++ b/crates/horizon-client/src/game/lan_actions.rs @@ -13,6 +13,7 @@ use horizon_client::{ items::ItemStack, lan::{ guest::SPECTATOR, + hub::TickCtx, menu::{Rules, Slots}, }, motion::{MotionEvent, MotionWorld, PlayerMotion}, @@ -36,6 +37,11 @@ pub(super) struct GuestPlay { swing: horizon_client::first_person::SwingState, /// The block being dug and the face the client aimed at. digging: Option<([i32; 3], [i32; 3])>, + /// The server tick the digging started (ServerPlayerGameMode.destroyProgressStart). + dig_start: u64, + /// A block the client stopped digging before the server's time was up: it breaks once + /// its progress since `dig_start` is complete (ServerPlayerGameMode.hasDelayedDestroy). + delayed: Option<([i32; 3], u64)>, last_progress: i32, /// Hand movements the client announced and the host animated. swung: bool, @@ -43,6 +49,8 @@ pub(super) struct GuestPlay { pub(super) merchant: Option, /// The inventory menu and the crafting table the guest crafts in. pub(super) crafting: super::lan_crafting::GuestCrafting, + /// What the guest's body was given for its effects and enchantments. + potions: super::potions::BodyPotions, } struct ItemRules<'a>(&'a horizon_client::items::Items); @@ -92,7 +100,11 @@ impl Game<'_> { let mut play = host.play.remove(&conn).unwrap_or_default(); let g = &mut host.hub.guests[i]; // The guest's body. - let mut motion = g.motion.take()?; + let Some(mut motion) = g.motion.take() else { + host.play.insert(conn, play); + return None; + }; + self.potions.swap_player(&mut g.effects, &mut play.potions); motion.position = Vec3::new(g.position[0], g.position[1], g.position[2]); motion.ground.on_ground = g.on_ground; motion.yaw = g.yaw; @@ -188,6 +200,7 @@ impl Game<'_> { } let g = &mut host.hub.guests[i]; std::mem::swap(&mut self.ender_items, &mut g.ender_items); + self.potions.swap_player(&mut g.effects, &mut play.potions); if let Some(key) = host_ender { self.world .block_entities @@ -274,6 +287,14 @@ impl Game<'_> { "container.enderchest", Slots::Ender, ) + } else if let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get(&pos) { + // AbstractFurnaceBlock.openContainer: FurnaceMenu, BlastFurnaceMenu, SmokerMenu. + let (menu, title) = match f.kind.recipe_type() { + "minecraft:blasting" => ("minecraft:blast_furnace", "container.blast_furnace"), + "minecraft:smoking" => ("minecraft:smoker", "container.smoker"), + _ => ("minecraft:furnace", "container.furnace"), + }; + (menu, title, Slots::Block(pos)) } else if let Some((other, right)) = self.chest_partner(pos) { // ChestBlock.combine: the right half's slots come first. let halves = if right { @@ -350,35 +371,41 @@ impl Game<'_> { let face = direction(i32::from(a.direction)); let play = host.play.entry(conn).or_default(); play.digging = Some((a.pos, face)); + play.dig_start = host.hub.ticks; play.last_progress = -1; + // The pause between two blocks is the client's (MultiPlayerGameMode's + // destroyDelay); the server digs from the START it receives. + play.destroy_delay = 0; } 1 => {} - 2 | 3 => { + 3 => { + host.hub.ack(conn, a.sequence); + self.guest_stop_digging(host, conn, a.pos); + } + 2 => { host.hub.ack(conn, a.sequence); let play = host.play.entry(conn).or_default(); play.digging = None; play.mining = None; - if a.action == 2 { - let state = self.world.block(a.pos[0], a.pos[1], a.pos[2]).0 as i32; - host.hub.send_to( - conn, - &C::BlockUpdate(play::BlockUpdate { pos: a.pos, state }), - ); - host.hub.broadcast_near( - [ - f64::from(a.pos[0]), - f64::from(a.pos[1]), - f64::from(a.pos[2]), - ], - 64.0, - &C::BlockDestruction(play::BlockDestruction { - id: host.hub.guests[gi].entity_id, - pos: a.pos, - progress: 255, - }), - Some(conn), - ); - } + let state = self.world.block(a.pos[0], a.pos[1], a.pos[2]).0 as i32; + host.hub.send_to( + conn, + &C::BlockUpdate(play::BlockUpdate { pos: a.pos, state }), + ); + host.hub.broadcast_near( + [ + f64::from(a.pos[0]), + f64::from(a.pos[1]), + f64::from(a.pos[2]), + ], + 64.0, + &C::BlockDestruction(play::BlockDestruction { + id: host.hub.guests[gi].entity_id, + pos: a.pos, + progress: 255, + }), + Some(conn), + ); } 4 | 5 => { if !spectator { @@ -395,10 +422,15 @@ impl Game<'_> { } }); } - 7 => { + // SWAP_ITEM_WITH_OFFHAND: the hands swap and the item in use stops + // (ServerGamePacketListenerImpl.handlePlayerAction), not for spectators. + 7 if !spectator => { let g = &mut host.hub.guests[gi]; let slot = g.selected; std::mem::swap(&mut g.inventory[slot], &mut g.offhand); + if let Some(play) = host.play.get_mut(&conn) { + play.using = None; + } } _ => {} }, @@ -474,6 +506,7 @@ impl Game<'_> { S::ContainerClick(p) if self.lan_merchant_click(host, conn, &p) => {} S::ContainerClick(p) if self.lan_inventory_click(host, conn, &p) => {} S::ContainerClick(p) if self.lan_crafting_click(host, conn, &p) => {} + S::ContainerClick(p) if self.lan_furnace_click(host, conn, &p) => {} S::SelectTrade(p) => self.lan_select_trade(host, conn, p.item), S::ContainerClose(p) if self.lan_merchant_closed(host, conn, Some(p.container)) => { let g = &mut host.hub.guests[gi]; @@ -669,6 +702,31 @@ impl Game<'_> { ); return; } + // An integrated server runs every player's commands at their own + // permission level (`CommandSourceStack.permissions`). + if host.hub.config.integrated { + let Some(level) = host.hub.guest(conn).map(|g| g.permission) else { + return; + }; + let command = command.trim_start_matches('/').to_owned(); + let lines = self.as_guest(host, conn, |g| { + let console = std::mem::replace(&mut g.ui.registries.permission, level); + g.chat_capture = Some(Vec::new()); + g.run_command(&command); + g.ui.registries.permission = console; + g.chat_capture.take().unwrap_or_default() + }); + for line in lines.unwrap_or_default() { + host.hub.send_to( + conn, + &C::SystemChat(play::SystemChat { + content: horizon_client::lan::data::component_nbt(&line.component()), + overlay: false, + }), + ); + } + return; + } // A LAN world follows its Allow Commands; a dedicated server leaves // it to the ops list (the guest's permission level). let allowed = self.options.dedicated.is_some() || self.allow_commands(); @@ -683,9 +741,14 @@ impl Game<'_> { return; } let command = command.trim_start_matches('/').to_owned(); + // The guest's own level (Commands.performCommand with the player's source): an + // operator's on a dedicated server, never the host's. + let permission = host.hub.guest(conn).map_or(0, |g| g.permission); let lines = self.as_guest(host, conn, |g| { g.chat_capture = Some(Vec::new()); + let level = std::mem::replace(&mut g.ui.registries.permission, permission); g.run_command(&command); + g.ui.registries.permission = level; g.chat_capture .take() .unwrap_or_default() @@ -759,7 +822,34 @@ impl Game<'_> { g.saturation = 5.0; g.sent_health = None; g.motion = None; - host.hub.respawn(i, spawn); + let mut xp = Experience { + level: g.xp_level, + progress: g.xp_progress, + total: g.xp_total, + }; + xp.respawn(self.ui.rules.bool("keep_inventory") || g.game_mode == SPECTATOR); + (g.xp_level, g.xp_progress, g.xp_total) = (xp.level, xp.progress, xp.total); + // A new body: nothing given to the old one carries over. + if let Some(play) = host.play.get_mut(&conn) { + play.potions = Default::default(); + } + let ctx = TickCtx { + world: &mut self.world, + host: None, + entities: &[], + next_entity: &mut self.next_entity, + spawn, + difficulty: i32::from(self.ui.difficulty), + difficulty_locked: self.level.difficulty_locked, + clock_rate: if self.ui.rules.bool("advance_time") { + self.ui.clock.rate + } else { + 0.0 + }, + tick_rate: self.clock.rate(), + tick_frozen: self.clock.frozen(), + }; + host.hub.respawn(i, spawn, &ctx); } /// Per tick, for every guest: dig, use items, pick up drops, die. @@ -806,20 +896,29 @@ impl Game<'_> { } }); } + self.guest_delayed_destroy(host, conn); self.guest_server_tick(host, conn); self.guest_dig_feedback(host, conn); self.guest_pickups(host, gi); let g = &mut host.hub.guests[gi]; if g.health <= 0.0 && !g.dead { g.dead = true; + g.last_death = Some(g.position.map(|v| v.floor() as i32)); + let (id, at) = (g.entity_id, g.position); + let reward = Experience { + level: g.xp_level, + progress: g.xp_progress, + total: g.xp_total, + } + .death_reward(self.ui.rules.bool("keep_inventory") || g.game_mode == SPECTATOR); let message = literal(&format!("{} died", g.name)); host.hub.send_to( conn, - &C::PlayerCombatKill(play::PlayerCombatKill { - id: host.hub.guests[gi].entity_id, - message, - }), + &C::PlayerCombatKill(play::PlayerCombatKill { id, message }), ); + // LivingEntity.die: the death animation, the dying player included. + host.hub.entity_event(id, 3, at); + self.spawn_orbs(at, reward); } } } @@ -846,12 +945,17 @@ impl Game<'_> { g.moves_from_ground }; if g.spectator() { + // Spectators' effects still run (LivingEntity.baseTick). + if !g.effects.list.is_empty() { + self.as_guest(host, conn, |game| game.tick_mob_effects()); + } return; } let in_wall = !g.sleeping && self.in_wall(g.position, g.eye_height()); let at = g.position; let sounds = self.as_guest(host, conn, |game| { - let (rules, health) = (game.world.health_rules, &game.motion.health); + let (rules, motion) = (game.world.health_rules, game.motion); + let health = &motion.health; let Some(player) = game.player.as_mut() else { return Vec::new(); }; @@ -889,9 +993,16 @@ impl Game<'_> { let _ = w.tick_environment_damage(); let _ = w.apply_fire_contact(); let _ = w.server_hazards_tick(in_wall); - let _ = w.player.peaceful_regeneration(rules); - let _ = w.player.food_tick(health, rules); - std::mem::take(&mut w.player.events) + let mut events = std::mem::take(&mut w.player.events); + // LivingEntity.baseTick ends with tickEffects. + game.tick_mob_effects(); + let Some(player) = game.player.as_mut() else { + return Vec::new(); + }; + let _ = player.peaceful_regeneration(rules); + let _ = player.food_tick(health, rules); + events.append(&mut player.events); + events .into_iter() .filter_map(|e| match e { MotionEvent::HurtSound(sound) => Some(sound), @@ -919,6 +1030,81 @@ impl Game<'_> { }) } + /// ServerPlayerGameMode.handleBlockBreakAction, STOP_DESTROY_BLOCK: the client finished + /// digging `pos` (and predicted the break). The server breaks it if its own count of the + /// digging since START is at least 70 % done; otherwise it keeps digging alone and breaks + /// it when done (`guest_delayed_destroy`), so the client's broken block is never left + /// standing on the server. + fn guest_stop_digging(&mut self, host: &mut LanHost, conn: u32, pos: [i32; 3]) { + let ticks = host.hub.ticks; + let play = host.play.entry(conn).or_default(); + let dug = play.digging.take().is_some_and(|(p, _)| p == pos); + play.mining = None; + let start = play.dig_start; + let state = self.world.block(pos[0], pos[1], pos[2]); + if !dug || self.res.facts.is_air(state) { + return; + } + let progress = self + .as_guest(host, conn, |g| g.destroy_progress(pos)) + .map_or(0.0, |p| p * (ticks - start + 1) as f32); + if progress >= 0.7 { + self.guest_destroy(host, conn, pos); + } else { + let play = host.play.entry(conn).or_default(); + if play.delayed.is_none() { + play.delayed = Some((pos, start)); + } + } + } + + /// ServerPlayerGameMode.tick's delayed destroy: the block breaks once its progress since + /// the digging started is complete, unless something else removed it first. + fn guest_delayed_destroy(&mut self, host: &mut LanHost, conn: u32) { + let Some((pos, start)) = host.play.get(&conn).and_then(|p| p.delayed) else { + return; + }; + let ticks = host.hub.ticks; + let state = self.world.block(pos[0], pos[1], pos[2]); + let progress = if self.res.facts.is_air(state) { + None + } else { + self.as_guest(host, conn, |g| g.destroy_progress(pos)) + .map(|p| p * (ticks - start + 1) as f32) + }; + match progress { + Some(p) if p < 1.0 => {} + done => { + if let Some(play) = host.play.get_mut(&conn) { + play.delayed = None; + } + if done.is_some() { + self.guest_destroy(host, conn, pos); + } + } + } + } + + /// ServerPlayerGameMode.destroyBlock for a guest, its crack cleared for the others + /// (`destroyBlockProgress(id, pos, -1)`). + fn guest_destroy(&mut self, host: &mut LanHost, conn: u32, pos: [i32; 3]) { + self.as_guest(host, conn, |g| { + let _ = g.break_block(pos); + }); + if let Some(g) = host.hub.guest(conn) { + host.hub.broadcast_near( + [f64::from(pos[0]), f64::from(pos[1]), f64::from(pos[2])], + 64.0, + &C::BlockDestruction(play::BlockDestruction { + id: g.entity_id, + pos, + progress: 255, + }), + Some(conn), + ); + } + } + /// The crack animation other players see. fn guest_dig_feedback(&mut self, host: &mut LanHost, conn: u32) { let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { diff --git a/crates/horizon-client/src/game/lan_crafting.rs b/crates/horizon-client/src/game/lan_crafting.rs index 02db51586..9841fa879 100644 --- a/crates/horizon-client/src/game/lan_crafting.rs +++ b/crates/horizon-client/src/game/lan_crafting.rs @@ -6,8 +6,9 @@ //! closes (`AbstractContainerMenu.clearContainer`). use super::{Game, lan::LanHost}; use horizon_client::{ + containers::BlockEntity, items::ItemStack, - lan::guest::Guest, + lan::{guest::Guest, menu::Slots}, menu::{Env, Input, Kind, Menu}, }; use horizon_protocol::packets::play::{self, Clientbound as C}; @@ -20,6 +21,9 @@ pub(super) struct GuestCrafting { inventory: Option>, /// The open crafting table: its window, state id and menu. table: Option<(i32, i32, Box)>, + /// The open furnace's window and menu (`AbstractFurnaceMenu`); its slots live in + /// the furnace block entity and the hub's container menu keeps the client in sync. + furnace: Option<(i32, Box)>, } /// ServerboundContainerClickPacket's input. @@ -153,6 +157,74 @@ impl Game<'_> { true } + /// A click on the guest's open furnace, blast furnace or smoker: the host's + /// `AbstractFurnaceMenu` (fuel slot, take-only result, quick moves by recipe and + /// fuel) over the block entity's slots. False for other windows. + pub(super) fn lan_furnace_click( + &mut self, + host: &mut LanHost, + conn: u32, + p: &play::ContainerClick, + ) -> bool { + let Some(gi) = host.hub.guests.iter().position(|g| g.conn == conn) else { + return false; + }; + let Some(pos) = host.hub.guests[gi] + .menu + .as_ref() + .filter(|m| p.container != 0 && m.window == p.container) + .and_then(|m| match m.slots { + Slots::Block(pos) => Some(pos), + _ => None, + }) + else { + return false; + }; + let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get(&pos) else { + return false; + }; + let recipe = match f.kind.recipe_type() { + "minecraft:blasting" => "blasting", + "minecraft:smoking" => "smoking", + _ => "smelting", + }; + let items = f.items.clone(); + if host.hub.guests[gi].spectator() { + return true; + } + let mut menu = host + .play + .entry(conn) + .or_default() + .crafting + .furnace + .take() + .filter(|(window, _)| *window == p.container) + .map_or_else(|| Box::new(Menu::new(Kind::Furnace(recipe))), |(_, m)| m); + let changed = self.guest_menu_op(host, gi, &mut menu, |menu, env| { + for (k, s) in items.iter().enumerate() { + menu.set_slot(env, k, s.clone()); + } + let before: Vec<_> = (0..menu.slot_count()) + .map(|s| menu.slot(s).cloned()) + .collect(); + let _ = menu.click(env, i32::from(p.slot), i32::from(p.button), input(p.input)); + (0..menu.slot_count()) + .filter(|&s| menu.slot(s) != before[s].as_ref()) + .collect::>() + }); + if let Some(BlockEntity::Furnace(f)) = self.world.block_entities.get_mut(&pos) { + for k in 0..3 { + f.items[k] = menu.slot(k).cloned(); + } + // AbstractFurnaceBlockEntity.setItem: only a new input restarts cooking. + f.input_clicked(&self.res.recipes, items[0].clone()); + } + host.play.entry(conn).or_default().crafting.furnace = Some((p.container, menu)); + host.hub.inventory_clicked(conn, p, &self.world, &changed); + true + } + /// The guest opened a crafting table (`CraftingTableBlock.getMenuProvider`). pub(super) fn lan_open_crafting(&mut self, host: &mut LanHost, conn: u32) { self.lan_crafting_closed(host, conn, None); diff --git a/crates/horizon-client/src/game/lan_screen.rs b/crates/horizon-client/src/game/lan_screen.rs index 2402a5286..7c4533a16 100644 --- a/crates/horizon-client/src/game/lan_screen.rs +++ b/crates/horizon-client/src/game/lan_screen.rs @@ -7,6 +7,7 @@ use super::{ Game, Screen, game_rules_screen::{Exit, RulesScreen}, + integrated::ServerOptions, lan::LanSettings, }; use horizon_client::{ @@ -50,7 +51,7 @@ enum View { Restrictions, GameRules, } -#[derive(Clone, PartialEq)] +#[derive(Clone, Copy, PartialEq)] struct Snapshot { default_mode: i32, personal_mode: i32, @@ -406,28 +407,32 @@ impl Restrictions { impl Game<'_> { pub(super) fn open_world_options(&mut self) { let hint = horizon_client::lan::available_port(); - let lan = self.lan.is_some(); let mut port = EditBox::new(0.0, 0.0, 150.0, 20.0); port.max_length = 5; - let (guest_commands, force) = self.lan.as_ref().map_or((false, true), |h| { - (h.settings.guest_commands, h.settings.force_game_mode) - }); - if let Some(p) = self.lan_port() { + // The world is the server thread's when there is one. + let state = match &self.integrated { + Some(server) => match server.call(|g| g.server_options()) { + Some(state) => state, + None => return, + }, + None => self.local_server_options(), + }; + if let Some(p) = state.port { port.set_value(&p.to_string()); } let mut options = WorldOptions { - default_mode: self.level.mode(), - personal_mode: self.mode_id(), - cheats: self.allow_commands(), - difficulty: self.ui.difficulty, - lan, + default_mode: state.default_mode, + personal_mode: state.personal_mode, + cheats: state.cheats, + difficulty: state.difficulty, + lan: state.port.is_some(), port, hint, - guest_commands, - force, - hardcore: self.level.hardcore, - difficulty_locked: self.level.difficulty_locked, - locked: self.level.difficulty_locked || self.level.hardcore, + guest_commands: state.guest_commands, + force: state.force, + hardcore: state.hardcore, + difficulty_locked: state.difficulty_locked, + locked: state.difficulty_locked || state.hardcore, scroll: 0.0, view: View::Options, rules: None, @@ -808,7 +813,7 @@ impl Game<'_> { fn apply_and_close_world_options(&mut self, o: WorldOptions) { let wanted = o.snapshot(); - let before = o.initial.clone(); + let before = o.initial; self.world_options = Some(o); self.apply_world_options(&before, &wanted); self.close_world_options(); @@ -891,7 +896,47 @@ impl Game<'_> { } } + /// The world options of a world run in the frame. + fn local_server_options(&self) -> ServerOptions { + let (guest_commands, force) = self.lan.as_ref().map_or((false, true), |h| { + (h.settings.guest_commands, h.settings.force_game_mode) + }); + ServerOptions { + default_mode: self.level.mode(), + personal_mode: self.mode_id(), + cheats: self.allow_commands(), + difficulty: self.ui.difficulty, + hardcore: self.level.hardcore, + difficulty_locked: self.level.difficulty_locked, + port: self.lan_port(), + guest_commands, + force, + } + } + fn apply_world_options(&mut self, before: &Snapshot, wanted: &Snapshot) { + if let Some(server) = &self.integrated { + // The server applies them; the window tells what came of + // Open to LAN (ShareToLanScreen's messages). + let (b, w) = (*before, *wanted); + server.call(move |g| g.apply_world_options(&b, &w)); + if w.lan != b.lan || (w.lan && w.port != b.port) { + match (w.lan, self.lan_port()) { + (true, Some(port)) => self.system_message(Text::translate( + "menu.multiplayerOptions.publish.started.lan", + &[Text::literal(port.to_string())], + )), + (true, None) => { + self.system_message(Text::translate("commands.publish.failed", &[])); + } + (false, _) => self.system_message(Text::translate( + "menu.multiplayerOptions.publish.stopped", + &[], + )), + } + } + return; + } if wanted.difficulty != before.difficulty { let name = DIFFICULTIES[usize::from(wanted.difficulty) & 3]; self.as_console(|g| g.run_command(&format!("difficulty {name}"))); @@ -904,10 +949,15 @@ impl Game<'_> { self.set_default_game_mode(wanted.default_mode); } if wanted.personal_mode != before.personal_mode { - self.set_mode_id(wanted.personal_mode); + if self.serving() { + self.set_owner_mode(wanted.personal_mode); + } else { + self.set_mode_id(wanted.personal_mode); + } } if wanted.cheats != before.cheats { self.set_allow_commands(wanted.cheats); + self.owner_allow_commands(wanted.cheats); } let settings_changed = wanted.guest_commands != before.guest_commands || wanted.force != before.force; diff --git a/crates/horizon-client/src/game/options.rs b/crates/horizon-client/src/game/options.rs index 25eb6eb96..ed0dccb06 100644 --- a/crates/horizon-client/src/game/options.rs +++ b/crates/horizon-client/src/game/options.rs @@ -17,6 +17,17 @@ pub(super) const CONTROL_HELP: &[&str] = &[ ]; impl Game<'_> { + pub(super) fn save_title_options(&mut self) { + if let Some(title) = &self.title + && title.last_server != self.settings.file.string("lastServer") + { + self.settings.file.set( + "lastServer", + horizon_client::options_file::V::Str(title.last_server.clone()), + ); + self.settings.save(&Self::options_path()); + } + } /// `options-get` and `options-set`; None for other commands. pub(super) fn options_control(&mut self, words: &[&str], line: &str) -> Result> { match words[0] { @@ -175,16 +186,11 @@ impl Game<'_> { } /// Mouse buttons pressed while a key is being bound. pub(super) fn options_waiting_for_key(&self) -> bool { - self.ui.options.selected_key.is_some() && self.ui.options.page() == Page::KeyBinds + self.ui.options.selected_key.is_some() } pub(super) fn options_bind_mouse(&mut self, button: MouseButton) -> Result<()> { - if let Some(name) = self.ui.options.selected_key.take() { - self.settings - .keys - .set(name, &horizon_client::keybinds::mouse_name(button)); - self.handle_option_events(vec![Event::Changed])?; - } - Ok(()) + let events = self.ui.options.bind_mouse(&mut self.settings, button); + self.handle_option_events(events) } pub(super) fn handle_option_events(&mut self, events: Vec) -> Result<()> { for e in events { @@ -195,6 +201,7 @@ impl Game<'_> { self.apply_settings(); self.sync_mods(); } + Event::Close if self.ui.options.resource_error.is_some() => {} Event::Close => { self.settings.save(&Self::options_path()); self.screen = if std::mem::take(&mut self.options_to_world_options) { @@ -233,6 +240,7 @@ impl Game<'_> { let s = &self.settings; if let Some(r) = self.renderer.as_mut() { r.gamma = s.gamma; + r.fullbright = s.extras.fullbright; r.set_present_mode(s.vsync, s.horizon.present_mode); r.animated_textures = s.horizon.animated_textures; r.set_mipmap_levels(s.mipmap_levels.max(0) as u32); diff --git a/crates/horizon-client/src/game/packs.rs b/crates/horizon-client/src/game/packs.rs index 4a80bee48..0b95dd2ad 100644 --- a/crates/horizon-client/src/game/packs.rs +++ b/crates/horizon-client/src/game/packs.rs @@ -35,7 +35,7 @@ pub fn open_folder(dir: &Path) { } } /// The resource root to load from, given the vanilla root and the selection. -pub fn startup_root(vanilla: &Path, selected: &[String]) -> PathBuf { +pub fn startup_root(vanilla: &Path, selected: &[String]) -> std::io::Result { let all = packs::discover(vanilla, &packs_dir()); let enabled = packs::enabled(&all, selected); packs::resource_root(vanilla, &enabled, &cache_dir()) @@ -104,9 +104,18 @@ impl Game<'_> { if self.ui.vanilla_root.as_os_str().is_empty() { self.ui.vanilla_root = vanilla.clone(); } - let root = startup_root(&vanilla, &self.settings.resource_packs); - if let Err(e) = self.reload_resources(&root) { + let result = startup_root(&vanilla, &self.settings.resource_packs) + .map_err(Into::into) + .and_then(|root| self.reload_resources(&root)); + if let Err(e) = result { log::warn!("Horizon: resource reload failed: {e}"); + if self.screen != super::Screen::Options { + self.open_options_at( + self.title.is_some(), + horizon_client::options_ui::Page::Packs, + ); + } + self.ui.options.resource_error = Some(e.to_string()); } } fn reload_resources(&mut self, root: &Path) -> horizon_client::Result<()> { @@ -121,12 +130,35 @@ impl Game<'_> { self.pack.context.biomes.clone(), self.pack.context.light.clone(), )?; + // Keep the working GPU resources, but release its exclusive window surface. + if let Some(renderer) = &mut self.renderer { + renderer.release_surface(); + } + let renderer = match self + .window + .clone() + .map(|window| self.renderer_for(window, &pack, &atlas)) + .transpose() + { + Ok(renderer) => renderer, + Err(error) => { + if let Some(renderer) = &mut self.renderer { + renderer.recreate_surface().map_err(|restore| { + format!( + "{error}; could not restore the previous rendering surface: {restore}" + ) + })?; + } + return Err(error); + } + }; // Everything loaded: switch over. self.random_tick_sections.clear(); self.options.resources = root.to_path_buf(); let context = pack.context.clone(); self.pack = pack; self.atlas = atlas; + horizon_client::ui_theme::reset_atlas(); // Villager hats come from the pack's texture metadata (shared data, swapped in place). *self .res @@ -142,9 +174,8 @@ impl Game<'_> { self.pending_uploads.clear(); // New GPU textures: a renderer built from the reloaded resources; the // sections are uploaded again as they are meshed. - if let Some(window) = self.window.clone() { - self.renderer = None; - self.renderer = Some(self.new_renderer(window)?); + if let Some(renderer) = renderer { + self.renderer = Some(renderer); self.apply_ui_settings(); } self.ui.chat.rescale(&self.atlas); diff --git a/crates/horizon-client/src/game/pad.rs b/crates/horizon-client/src/game/pad.rs index cae85bc6c..62d481b11 100644 --- a/crates/horizon-client/src/game/pad.rs +++ b/crates/horizon-client/src/game/pad.rs @@ -354,6 +354,7 @@ impl Game<'_> { } if let Some(title) = self.title.as_mut() { title.escape(); + self.save_title_options(); return Ok(()); } if self.sign_edit.is_some() { diff --git a/crates/horizon-client/src/game/perspective.rs b/crates/horizon-client/src/game/perspective.rs index a66ca487d..bb463547f 100644 --- a/crates/horizon-client/src/game/perspective.rs +++ b/crates/horizon-client/src/game/perspective.rs @@ -427,7 +427,7 @@ impl Game<'_> { } /// `extractEntityInInventoryFollowsMouse`: the player in the box /// `[x0, x1) x [y0, y1)`, `size` pixels per block, turned toward the - /// cursor; drawn as depth-sorted, back-face-culled quads. + /// cursor; drawn with a depth buffer and back-face culling. #[allow(clippy::too_many_arguments)] pub(super) fn draw_player_preview( &self, @@ -495,9 +495,9 @@ impl Game<'_> { let quads = player_preview_quads(verts, gui, fire, &atlas.sprites); let start = batch.vertices.len(); for (_, p, uv, color) in quads { - batch.quad(p, uv, color); + batch.quad_3d(p, uv, color); } - batch.clip_since(start, x0, y0, x1, y1); + batch.depth_since(start, [x0, y0, x1, y1]); } /// The survival inventory's player box (`InventoryScreen`). pub(super) fn draw_inventory_player(&self, batch: &mut HudBatch, ox: f32, oy: f32) { @@ -570,7 +570,7 @@ fn player_preview_quads( gui: glam::Mat4, fire: Option<(f32, f32)>, sprites: &std::collections::HashMap, -) -> Vec<(f32, [[f32; 2]; 4], [[f32; 2]; 4], u32)> { +) -> Vec<(f32, [[f32; 3]; 4], [[f32; 2]; 4], u32)> { let flame_start = verts.len(); if let Some((width, height)) = fire { horizon_client::fire_render::flames( @@ -584,7 +584,7 @@ fn player_preview_quads( &mut verts, ); } - let mut quads: Vec<(f32, [[f32; 2]; 4], [[f32; 2]; 4], u32)> = Vec::new(); + let mut quads: Vec<(f32, [[f32; 3]; 4], [[f32; 2]; 4], u32)> = Vec::new(); for (index, q) in verts.as_chunks::<4>().0.iter().enumerate() { let p: [glam::Vec3; 4] = std::array::from_fn(|i| gui.transform_point3(glam::Vec3::from_array(q[i].pos))); @@ -606,12 +606,12 @@ fn player_preview_quads( let depth = p.iter().map(|v| v.z).sum::() / 4.0; quads.push(( depth, - p.map(|v| [v.x, v.y]), + p.map(|v| v.to_array()), std::array::from_fn(|i| q[i].uv), color, )); } - // Far (low z) first: the viewer is on the +z side. + // Keep far-to-near order for translucent faces; opaque overlap uses depth. quads.sort_by(|a, b| a.0.total_cmp(&b.0)); quads } @@ -619,6 +619,60 @@ fn player_preview_quads( #[cfg(test)] mod preview_tests { use super::*; + #[test] + fn preview_keeps_crossing_faces_depth_and_clips_without_dropping_faces() { + use horizon_client::{ + hud::{Blend, HudBatch}, + mesher::Vertex, + }; + let face = |z: [f32; 4]| { + [[-2.0, -1.0], [-2.0, 1.0], [2.0, 1.0], [2.0, -1.0]] + .into_iter() + .zip(z) + .map(|([x, y], z)| Vertex { + pos: [x, y, z], + uv: [0.0; 2], + layer: 0, + color: u32::MAX, + light: 0, + normal: [0; 4], + }) + }; + // A shield and arm can intersect: neither average depth orders both ends. + let verts = face([1.0, 1.0, -1.0, -1.0]).chain(face([0.0; 4])).collect(); + let quads = player_preview_quads(verts, glam::Mat4::IDENTITY, None, &Default::default()); + assert_eq!(quads.len(), 2); + let mut batch = HudBatch::new([320, 240]); + for (_, p, uv, color) in quads { + batch.quad_3d(p, uv, color); + } + // Its centre is outside this scissor; the visible part must still be drawn. + batch.depth_since(0, [1.0, 0.0, 2.0, 1.0]); + assert_eq!(batch.vertices.len(), 12); + let shield = &batch.vertices[..6]; + let arm = &batch.vertices[6..]; + assert!(shield[0].depth < arm[0].depth); + assert!(shield[2].depth > arm[2].depth); + assert_eq!(batch.runs(), vec![(0..12, Blend::Depth([1, 0, 1, 1]))]); + } + + #[test] + #[ignore = "requires prepared model data in HORIZON_TEST_RESOURCES"] + fn player_render_transform_uses_vanilla_avatar_scale() { + let root = std::path::PathBuf::from( + std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"), + ); + let mut m = Mob::new(1, player_type(&root).unwrap(), Vec3::ZERO, 0.0); + for (id, expected) in [("minecraft:player", 0.9375), ("minecraft:zombie", 1.0)] { + Arc::make_mut(&mut m.kind).id = id.into(); + let transform = + horizon_client::mobs::render::base_transform_at(&m, 1.0, glam::Mat4::IDENTITY); + for axis in [glam::Vec3::X, glam::Vec3::Y, glam::Vec3::Z] { + assert!((transform.transform_vector3(axis).length() - expected).abs() < 1e-6); + } + } + } + #[test] fn burning_inventory_preview_keeps_visible_fullbright_flames() { let rect = |u0| horizon_client::hud::Rect { diff --git a/crates/horizon-client/src/game/potions.rs b/crates/horizon-client/src/game/potions.rs index fbc8556e7..a60926256 100644 --- a/crates/horizon-client/src/game/potions.rs +++ b/crates/horizon-client/src/game/potions.rs @@ -49,6 +49,14 @@ impl Game<'_> { }); } } +/// What a player's body was last given for its effects and enchantments: +/// a guest keeps their own while the host's are in `PotionState`. +#[derive(Default)] +pub(super) struct BodyPotions { + applied: Vec<(&'static str, &'static str, f64, &'static str)>, + protection_for: Option>>, + enchant_modifiers: Vec<(String, String, f64, String)>, +} /// Effects, thrown potions and the attribute modifiers last applied. #[derive(Default)] pub(super) struct PotionState { @@ -63,6 +71,14 @@ pub(super) struct PotionState { } impl PotionState { + /// Trade the player's own state (effects and what their body was given) + /// with a guest's, around `as_guest`. Potions in flight stay. + pub(super) fn swap_player(&mut self, effects: &mut PlayerEffects, body: &mut BodyPotions) { + std::mem::swap(&mut self.effects, effects); + std::mem::swap(&mut self.applied, &mut body.applied); + std::mem::swap(&mut self.protection_for, &mut body.protection_for); + std::mem::swap(&mut self.enchant_modifiers, &mut body.enchant_modifiers); + } /// Thrown potions in flight as (entity type, position, motion), for the /// dispenser parity fixture. pub(super) fn thrown_entities(&self) -> Vec<(&'static str, Vec3, Vec3)> { @@ -274,8 +290,16 @@ impl Game<'_> { } } } - /// `LivingEntity.tickEffects`, attribute modifiers and the status flags. + /// The local player's effects (`tick_mob_effects`), then the potions + /// in flight, which a server without a local player still has. pub(super) fn tick_effects(&mut self) { + self.tick_mob_effects(); + self.tick_thrown_potions(); + } + /// `LivingEntity.tickEffects` (server side) for `self.player`, the local + /// player or a guest acting (`as_guest`), then its attribute modifiers + /// and status flags. + pub(super) fn tick_mob_effects(&mut self) { let Some(player) = self.player.as_ref() else { return; }; @@ -294,6 +318,20 @@ impl Game<'_> { for action in actions { self.apply_effect_action(action); } + self.apply_effect_status(); + } + /// A client's own effects (`LivingEntity.tickEffects` on the client, + /// `MobEffectInstance.tickClient`): they count down, and the server + /// removes them. Their modifiers and flags move the predicted player. + pub(super) fn tick_client_effects(&mut self) { + for e in &mut self.potions.effects.list { + e.tick_client(); + } + self.apply_effect_status(); + } + /// The effects' attribute modifiers, armor protection and status flags + /// on `self.player`. + fn apply_effect_status(&mut self) { let modifiers = self.potions.effects.modifiers(); if modifiers != self.potions.applied && let Some(p) = self.player.as_mut() @@ -346,7 +384,6 @@ impl Game<'_> { status.protection = std::mem::take(&mut p.status.protection); p.status = status; } - self.tick_thrown_potions(); } fn tick_thrown_potions(&mut self) { let mut thrown = std::mem::take(&mut self.potions.thrown); @@ -606,13 +643,7 @@ impl Game<'_> { /// Effects saved in level.dat (`Player.active_effects`). pub(super) fn restore_effects(&mut self) { self.potions = PotionState::default(); - for (id, duration, amplifier, ambient, _) in self.level.active_effects.clone() { - if let Some(def) = brewing::effect(&id) { - let mut e = EffectInstance::new(def.id, duration, amplifier); - e.ambient = ambient; - self.potions.effects.add(e); - } - } + self.potions.effects = PlayerEffects::from_saved(&self.level.active_effects); } pub(super) fn effects_json(&self) -> serde_json::Value { serde_json::json!({ diff --git a/crates/horizon-client/src/game/remote.rs b/crates/horizon-client/src/game/remote.rs index e31e270b8..f2c72c3b5 100644 --- a/crates/horizon-client/src/game/remote.rs +++ b/crates/horizon-client/src/game/remote.rs @@ -68,6 +68,7 @@ struct Lerp { pub(super) struct Remote { pub session: Session, + pub brand: String, builtin: Arc, lang: Arc, registries: Arc, @@ -88,6 +89,11 @@ pub(super) struct Remote { was_sprinting: bool, carried: Option, loaded_sent: bool, + /// BlockStatePredictionHandler: blocks changed ahead of the server (a predicted break), + /// with the sequence of the last action that changed them and the state the server + /// last said they have. Until the server acknowledges that sequence its block updates + /// only replace the remembered state; then the block takes it. + predictions: HashMap<[i32; 3], (i32, i32)>, /// Open container: (window id, menu name). menu: Option<(i32, String)>, /// The open menu's title as the server named it. @@ -153,7 +159,20 @@ fn number_format(f: &play::NumberFormat) -> horizon_client::scoreboard::NumberFo impl Game<'_> { /// Leave the title for a server (ConnectScreen): an empty world fed by the connection. pub(super) fn connect_server(&mut self, address: &str, name: &str) -> Result<()> { - self.join_server(address, name, None) + if let Err(error) = self.join_server(address, name, None) { + log::warn!("Horizon: could not join server: {error}"); + if self.title.is_none() { + self.title = Some(horizon_client::title::Title::new( + std::env::current_dir()?.join("saves"), + &self.options.resources, + )); + } + if let Some(title) = &mut self.title { + title.failed(error); + } + self.capture(false); + } + Ok(()) } /// Join single player's integrated server through memory. pub(super) fn connect_local( @@ -173,7 +192,7 @@ impl Game<'_> { let root = self.options.resources.clone(); let builtin = Builtin::find(&root) .and_then(|p| Builtin::load(&p).ok()) - .ok_or("registries.json not found next to the resources; run the data generator reports (horizon-prepare)")?; + .ok_or("Resources are incomplete: registries.json is missing or invalid. Click \"Prepare again\" in the launcher (or run horizon-prepare), then try again.")?; let lang = Arc::new(horizon_client::net::load_language(&root)); let view = self.settings.render_distance.clamp(2, 32) as i8; if local.is_none() { @@ -238,6 +257,7 @@ impl Game<'_> { self.apply_settings(); self.remote = Some(Remote { session, + brand: "unknown".into(), builtin: Arc::new(builtin), lang, registries: Arc::new(Dynamic::default()), @@ -258,6 +278,7 @@ impl Game<'_> { was_sprinting: false, carried: None, loaded_sent: false, + predictions: HashMap::new(), menu: None, menu_title: None, state_id: 0, @@ -330,6 +351,7 @@ impl Game<'_> { } let saves = std::env::current_dir()?.join("saves"); let mut title = horizon_client::title::Title::new(saves.clone(), &self.options.resources); + title.last_server = self.settings.file.string("lastServer"); if single_player && reason.is_none() { self.title = Some(title); self.capture(false); @@ -366,6 +388,31 @@ impl Game<'_> { .unwrap_or("minecraft:air"), ) } + /// A mob effect's network id as the effect (`brewing::EFFECTS` id). + fn remote_effect_id(&self, effect: i32) -> Option<&'static str> { + let r = self.remote.as_ref()?; + let name = r.builtin.get("minecraft:mob_effect")?.name(effect)?; + horizon_client::brewing::effect(name).map(|d| d.id) + } + /// ClientPacketListener.handleUpdateMobEffect for the player itself + /// (`LivingEntity.forceAddEffect`): the effect replaces any of its kind. + /// Other entities' effects are not shown. + fn remote_effect(&mut self, p: &play::UpdateMobEffect) { + if !self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) { + return; + } + let Some(id) = self.remote_effect_id(p.effect) else { + return; + }; + let mut e = horizon_client::effects::EffectInstance::new(id, p.duration, p.amplifier); + e.ambient = p.flags & 1 != 0; + e.visible = p.flags & 2 != 0; + let list = &mut self.potions.effects.list; + match list.iter_mut().find(|x| x.id == id) { + Some(existing) => *existing = e, + None => list.push(e), + } + } fn item_id(&self, name: &str) -> i32 { let r = self.remote.as_ref().expect("remote"); r.builtin @@ -401,7 +448,7 @@ impl Game<'_> { } Some(stack) } - fn chat_line(&mut self, text: String) { + pub(super) fn chat_line(&mut self, text: String) { log::info!("[chat] {text}"); if let Some(r) = self.remote.as_mut() { r.chat.push((text, Instant::now())); @@ -442,6 +489,11 @@ impl Game<'_> { r.status = format!("Joined as {name}; loading terrain..."); } } + Event::Brand(brand) => { + if let Some(remote) = &mut self.remote { + remote.brand = brand; + } + } Event::Reconfigured(registries) => self.set_registries(registries), Event::DecodeError(e) => { if let Some(r) = self.remote.as_mut() { @@ -495,6 +547,7 @@ impl Game<'_> { r.dimension = dimension; r.game_mode = spawn.game_type; r.loaded_sent = false; + r.predictions.clear(); self.set_mode_id(spawn.game_type); self.dim = if spawn.dimension == "minecraft:the_nether" { Dim::Nether @@ -622,6 +675,42 @@ impl Game<'_> { } } } + /// ClientLevel.setServerVerifiedBlockState: a block the client predicted keeps its + /// predicted state until the server acknowledges the action; the server's state is + /// remembered for then. + fn server_block(&mut self, pos: [i32; 3], state: i32) { + if let Some((_, known)) = self + .remote + .as_mut() + .and_then(|r| r.predictions.get_mut(&pos)) + { + *known = state; + return; + } + self.set_block(pos, state); + } + /// BlockStatePredictionHandler.endPredictionsUpTo (ClientboundBlockChangedAckPacket): the + /// actions up to `sequence` are settled, so the blocks they predicted take the state the + /// server gave them (a break the server refused comes back). + fn end_predictions(&mut self, sequence: i32) { + let Some(r) = self.remote.as_mut() else { + return; + }; + let settled: Vec<_> = r + .predictions + .iter() + .filter(|(_, (s, _))| *s <= sequence) + .map(|(&pos, &(_, state))| (pos, state)) + .collect(); + for (pos, state) in settled { + if let Some(r) = self.remote.as_mut() { + r.predictions.remove(&pos); + } + if self.world.block(pos[0], pos[1], pos[2]).0 as i32 != state { + self.set_block(pos, state); + } + } + } fn set_block(&mut self, pos: [i32; 3], state: i32) { let state = BlockStateId(state.max(0) as u32); if self.world.loaded(pos[0], pos[2]) @@ -732,6 +821,8 @@ impl Game<'_> { 5..=8 => Some(500 + s - 5), 9..=35 => Some(s), 36..=44 => Some(s - 36), + // InventoryMenu.SHIELD_SLOT: the off hand. + 45 => Some(504), _ => None, }, "minecraft:crafting" => match s { @@ -806,6 +897,8 @@ impl Game<'_> { )); } C::Respawn(p) => { + // A new player, without effects: the server sends them again. + self.potions.effects.clear(); self.enter_remote_dimension(&p.spawn, true); self.screen = Screen::Playing; if let Some(r) = self.remote.as_mut() { @@ -865,7 +958,7 @@ impl Game<'_> { chunks_per_tick: 64.0, })); } - C::BlockUpdate(p) => self.set_block(p.pos, p.state), + C::BlockUpdate(p) => self.server_block(p.pos, p.state), C::SectionBlocksUpdate(p) => { let [sx, sy, sz] = p.section; for change in p.changes { @@ -875,10 +968,10 @@ impl Game<'_> { sy * 16 + (local & 15), sz * 16 + (local >> 4 & 15), ]; - self.set_block(pos, (change >> 12) as i32); + self.server_block(pos, (change >> 12) as i32); } } - C::BlockChangedAck(_) => {} + C::BlockChangedAck(p) => self.end_predictions(p.sequence), // ClientLevel.blockEvent: BlockEntity.triggerEvent (chest lids, // shulker boxes, bells) when the block is still the one it names. C::BlockEvent(p) => { @@ -1106,6 +1199,14 @@ impl Game<'_> { } } C::EntityEvent(p) => { + // EntityEvent.PERMISSION_LEVEL_ALL..OWNERS (24..=28) for this + // player: LocalPlayer.setPermissionLevel, which the commands + // the chat offers and checks follow. + if (24..=28).contains(&p.event) + && self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) + { + self.ui.registries.permission = (p.event - 24) as u8; + } if p.event == 1 && let Some(m) = self.mobs.iter_mut().find(|m| m.id == p.id as u32) && m.species() == horizon_client::mobs::Species::Rabbit @@ -1120,6 +1221,14 @@ impl Game<'_> { } } C::Animate(p) if p.action == 0 => {} + C::UpdateMobEffect(p) => self.remote_effect(&p), + C::RemoveMobEffect(p) => { + if self.remote.as_ref().is_some_and(|r| r.entity_id == p.id) + && let Some(id) = self.remote_effect_id(p.effect) + { + self.potions.effects.list.retain(|e| e.id != id); + } + } C::SetHealth(p) => { if let Some(r) = self.remote.as_mut() { r.health = (p.health, p.food, p.saturation); @@ -1231,6 +1340,8 @@ impl Game<'_> { match p.slot { 0..=35 => self.inventory[p.slot as usize] = stack, 36..=39 => self.armor[3 - (p.slot - 36) as usize] = stack, + // Inventory.SLOT_OFFHAND. + 40 => self.offhand = stack, _ => {} } self.remote_synced(); @@ -1789,6 +1900,7 @@ impl Game<'_> { self.tick_block_entity_anims(); self.tick_hand(); self.tick_particles(); + self.tick_client_effects(); let ghast_collisions = self.happy_ghast_player_collisions(); let Some(player) = self.player.as_mut() else { return Ok(()); @@ -1806,6 +1918,7 @@ impl Game<'_> { if let Some(r) = self.remote.as_mut() { r.loaded_sent = true; } + self.capture_loaded_world(); } let stick = self.pad_move(); let Some(player) = self.player.as_mut() else { @@ -2057,6 +2170,17 @@ impl Game<'_> { } } + pub(super) fn stop_remote_mining(&mut self) { + if let Some((pos, _, _)) = self.remote.as_mut().and_then(|r| r.destroying.take()) { + self.send(S::PlayerAction(play::PlayerAction { + action: 2, + pos, + direction: 0, + sequence: 0, + })); + } + } + /// MultiPlayerGameMode.startDestroyBlock / continueDestroyBlock. fn remote_mining(&mut self) { let Some(r) = self.remote.as_mut() else { @@ -2067,14 +2191,7 @@ impl Game<'_> { } let chatting = r.chat_input.is_some(); if !self.left || self.screen != Screen::Playing || chatting { - if let Some((pos, _, _)) = r.destroying.take() { - self.send(S::PlayerAction(play::PlayerAction { - action: 2, - pos, - direction: 0, - sequence: 0, - })); - } + self.stop_remote_mining(); self.mining = None; return; } @@ -2094,7 +2211,7 @@ impl Game<'_> { self.send(S::Punch(Empty)); if self.creative { let seq = self.next_sequence(); - self.predict_break(hit.pos, state); + self.predict_break(hit.pos, state, seq); self.send(S::PlayerAction(play::PlayerAction { action: 0, pos: hit.pos, @@ -2143,7 +2260,7 @@ impl Game<'_> { sequence: seq, })); if progress >= 1.0 { - self.predict_break(hit.pos, state); + self.predict_break(hit.pos, state, seq); if let Some(r) = self.remote.as_mut() { r.destroy_delay = 5; } @@ -2180,7 +2297,7 @@ impl Game<'_> { self.crack_particle(pos, hit.face, state); if done >= 1.0 { let seq = self.next_sequence(); - self.predict_break(pos, state); + self.predict_break(pos, state, seq); self.send(S::PlayerAction(play::PlayerAction { action: 3, pos, @@ -2212,7 +2329,14 @@ impl Game<'_> { } } /// MultiPlayerGameMode.destroyBlock: the predicted break. - fn predict_break(&mut self, pos: [i32; 3], state: BlockStateId) { + fn predict_break(&mut self, pos: [i32; 3], state: BlockStateId, sequence: i32) { + // BlockStatePredictionHandler.retainKnownServerState. + if let Some(r) = self.remote.as_mut() { + r.predictions + .entry(pos) + .and_modify(|p| p.0 = sequence) + .or_insert((sequence, state.0 as i32)); + } let s = self.res.facts.get(state).sounds.clone(); let at = [ f64::from(pos[0]) + 0.5, @@ -2503,6 +2627,17 @@ impl Game<'_> { })); } } + /// Minecraft.handleKeybinds, key.swapOffhand: the server swaps the hands + /// (ServerboundPlayerActionPacket SWAP_ITEM_WITH_OFFHAND at BlockPos.ZERO, facing down) and + /// sends both slots back; nothing is predicted. + pub(super) fn remote_swap_hands(&mut self) { + self.send(S::PlayerAction(play::PlayerAction { + action: 7, + pos: [0; 3], + direction: 0, + sequence: 0, + })); + } /// A container click: the server answers with the authoritative slots. pub(super) fn remote_click(&mut self, right: bool) { let Some(size) = self @@ -2923,3 +3058,20 @@ impl Game<'_> { })) } } + +#[cfg(test)] +mod tests { + use super::Game; + + #[test] + fn every_inventory_menu_slot_is_a_game_slot_and_45_is_the_off_hand() { + // InventoryMenu: result, 2x2 grid, armour, 27 + 9 inventory slots, then the off hand. + assert_eq!(Game::menu_slot("inventory", 45), Some(504)); + let mut seen = std::collections::HashSet::new(); + for slot in 0..46 { + let internal = Game::menu_slot("inventory", slot); + assert!(internal.is_some_and(|i| seen.insert(i)), "slot {slot}"); + } + assert_eq!(Game::menu_slot("inventory", 46), None); + } +} diff --git a/crates/horizon-client/src/game/scoreboard.rs b/crates/horizon-client/src/game/scoreboard.rs index e5a5fe1e6..9fa610cd1 100644 --- a/crates/horizon-client/src/game/scoreboard.rs +++ b/crates/horizon-client/src/game/scoreboard.rs @@ -738,10 +738,7 @@ mod tab_list_tests { properties: Vec::new(), info: ClientInformation::new(8), brand: "test".into(), - sender: PlayerSender { - tx, - pending: Arc::default(), - }, + sender: PlayerSender::new(tx, Arc::default()), latency: Arc::new(AtomicI32::new(42)), address: "127.0.0.1:25565".parse().unwrap(), }, diff --git a/crates/horizon-client/src/game/screens.rs b/crates/horizon-client/src/game/screens.rs index ac4d16274..3c6f90099 100644 --- a/crates/horizon-client/src/game/screens.rs +++ b/crates/horizon-client/src/game/screens.rs @@ -60,17 +60,14 @@ impl Game<'_> { && horizon_client::ui_theme::horizon() } fn pause_labels(&self) -> (String, &'static str) { - let world = if self.remote.is_some() { - "Multiplayer".to_owned() + if let Some(server) = &self.integrated { + return (server.world_name.clone(), "Save and quit"); + } + if self.remote.is_some() { + ("Multiplayer".to_owned(), "Disconnect") } else { - self.level.name.clone() - }; - let quit = if self.remote.is_some() { - "Disconnect" - } else { - "Save and quit" - }; - (world, quit) + (self.level.name.clone(), "Save and quit") + } } pub(super) fn draw_pause_menu(&mut self, batch: &mut HudBatch) { if horizon_client::ui_theme::horizon() { @@ -149,9 +146,6 @@ impl Game<'_> { .find(|b| b.5 && ui::hovered(self.cursor, b.0, b.1, b.2, 20.0)) .map(|b| b.4) }; - if hit.is_some() && !horizon_client::ui_theme::horizon() { - self.audio.play_button_click(); - } match hit { Some(0) => { self.screen = Screen::Playing; @@ -171,6 +165,9 @@ impl Game<'_> { Some(2) => self.close_world()?, _ => {} } + if hit.is_some() && !horizon_client::ui_theme::horizon() { + self.audio.play_button_click(); + } Ok(()) } /// DeathScreen: gradient, the title at double size, the cause and score. @@ -263,8 +260,9 @@ impl Game<'_> { .into_iter() .find(|d| ui::hovered(self.cursor, d.0, d.1, 200.0, 20.0)) .map(|d| d.3); - if hit.is_some() { - self.audio.play_button_click(); + if hit == Some(0) && self.remote.is_none() { + let keep = self.ui.rules.bool("keep_inventory") || self.spectator(); + self.experience.respawn(keep); } match hit { Some(0) if self.remote.is_some() => { @@ -295,6 +293,9 @@ impl Game<'_> { Some(1) => self.close_world()?, _ => {} } + if hit.is_some() { + self.audio.play_button_click(); + } Ok(()) } } diff --git a/crates/horizon-client/src/game/spill.rs b/crates/horizon-client/src/game/spill.rs index ac508dfb8..20dcdf0de 100644 --- a/crates/horizon-client/src/game/spill.rs +++ b/crates/horizon-client/src/game/spill.rs @@ -6,6 +6,7 @@ use super::{Game, GameLevel, Screen, dispenser::new_item_motion, storage::is_shulker_box}; use horizon_client::{ containers::{Banner, BlockEntity}, + item_data, items::ItemStack, }; use horizon_core::position::BlockPos; @@ -15,6 +16,7 @@ use horizon_world::{ LevelEvent, removal::{self, PreRemove}, }, + nbt::{JavaString, Tag}, registry::BlockStateId, }; use std::collections::HashMap; @@ -80,14 +82,37 @@ impl Pending { stack.data_mut().pot_decorations = Some(Box::new(sides)); } if stack.item.ends_with("_banner") - && let Some(Banner { layers, name }) = self.banner.remove(&pos) + && let Some(Banner { + layers, + name, + components, + }) = self.banner.remove(&pos) { - let data = stack.data_mut(); - data.banner_patterns = layers; - if name.is_some() { - data.custom_name = name; + let mut patch = match item_data::encode_components(stack) { + Some(Tag::Compound(c)) => c, + _ => Default::default(), + }; + // The remaining components in the banner loot table's copy_components. + for key in [ + "minecraft:item_name", + "minecraft:rarity", + "minecraft:tooltip_display", + ] { + if let Some(value) = components.get(&JavaString::from(key)) { + patch.insert(key.into(), value.clone()); + } } - stack.tidy(); + patch.insert( + "minecraft:banner_patterns".into(), + horizon_client::banner::layers_to_nbt(&layers), + ); + if let Some(name) = name { + patch.insert( + "minecraft:custom_name".into(), + Tag::String(name.as_str().into()), + ); + } + (stack.damage, stack.extra) = item_data::decode_components(Some(&Tag::Compound(patch))); } } /// Remember a decorated pot's sherds from its saved block entity. @@ -553,3 +578,49 @@ impl Game<'_> { } } } + +#[cfg(test)] +mod tests { + use super::*; + use horizon_client::{block_entity_item, item_data, mobs::raider, save}; + use horizon_world::nbt::Tag; + + #[test] + fn ominous_banner_survives_placement_save_and_drop() { + let original = raider::ominous_banner(); + let Tag::Compound(components) = item_data::encode_components(&original).unwrap() else { + panic!("banner components"); + }; + for block in ["minecraft:white_banner", "minecraft:white_wall_banner"] { + let pos = (1, 64, 2); + let fresh = BlockEntity::for_block(block).unwrap(); + let fresh = save::encode_block_entity(pos, &fresh, block); + let placed = block_entity_item::place(&fresh, &components, false); + let (_, placed) = save::decode_block_entity(&placed).unwrap(); + let saved = save::encode_block_entity(pos, &placed, block); + let (_, loaded) = save::decode_block_entity(&saved).unwrap(); + let BlockEntity::Banner(banner) = &loaded else { + panic!("placed banner"); + }; + assert_eq!(banner.layers.len(), item_data::OMINOUS_LAYERS.len()); + let mut pending = Pending::default(); + pending.record([pos.0, pos.1, pos.2], &loaded, block, None); + let mut dropped = ItemStack::new("minecraft:white_banner", 1); + pending.attach([pos.0, pos.1, pos.2], &mut dropped); + assert!(dropped.data().unwrap().ominous_banner); + assert!(horizon_client::banner::tooltip_layers(&dropped).is_empty()); + assert_eq!( + horizon_client::item_tooltip::name_line(&dropped, &|_| None), + "Ominous Banner" + ); + assert_eq!( + horizon_client::item_tooltip::rarity(&dropped, "common"), + "uncommon" + ); + assert_eq!( + item_data::encode_components(&dropped), + Some(Tag::Compound(components.clone())) + ); + } + } +} diff --git a/crates/horizon-client/src/game/stations.rs b/crates/horizon-client/src/game/stations.rs index f16fa14af..8cd751548 100644 --- a/crates/horizon-client/src/game/stations.rs +++ b/crates/horizon-client/src/game/stations.rs @@ -1860,18 +1860,10 @@ impl Game<'_> { pub(super) fn stack_tooltip(&self, stack: &ItemStack) -> Vec<(String, u32)> { let e: &Enchantments = &self.res.stations.enchantments; let props = e.props(&stack.item); - let mut rarity = props.map_or("common", |p| p.rarity.as_str()); - // The ominous banner's rarity component. - if stack.data().is_some_and(|d| d.ominous_banner) { - rarity = "uncommon"; - } - if !stack.enchantments().is_empty() { - rarity = match rarity { - "common" | "uncommon" => "rare", - "rare" => "epic", - r => r, - }; - } + let rarity = horizon_client::item_tooltip::rarity( + stack, + props.map_or("common", |p| p.rarity.as_str()), + ); let color = match rarity { "uncommon" => rgba(255, 255, 85, 255), "rare" => rgba(85, 255, 255, 255), @@ -1952,7 +1944,7 @@ impl Game<'_> { )); } // BannerPatternLayers.addToTooltip: up to six layer names. - for layer in horizon_client::banner::layers(stack).iter().take(6) { + for layer in horizon_client::banner::tooltip_layers(stack).iter().take(6) { let key = self.res.stations.banners.layer_key(layer); lines.push((self.translate(&key, &key), rgba(170, 170, 170, 255))); } @@ -1977,6 +1969,34 @@ impl Game<'_> { } } } + if let Some(info) = self.res.items.get(&stack.item) { + let base = |name: &str, default| { + self.player + .as_ref() + .and_then(|player| { + let key = player.attributes.key(name)?; + let handle = player.attributes.lookup(Some(&key))?; + player + .attributes + .instance(&handle) + .ok() + .map(|value| value.base_value()) + }) + .unwrap_or(default) + }; + let player_base = [ + base("minecraft:attack_damage", 1.0), + base( + "minecraft:attack_speed", + horizon_client::attack_strength::BASE_ATTACK_SPEED, + ), + ]; + lines.extend(horizon_client::item_tooltip::attribute_lines( + &info.modifiers, + Some(player_base), + &translate, + )); + } if let Some(info) = self.res.items.get(&stack.item) && info.max_damage > 0 && stack.damage > 0 diff --git a/crates/horizon-client/src/game/titles.rs b/crates/horizon-client/src/game/titles.rs index 30abfc34d..459aab503 100644 --- a/crates/horizon-client/src/game/titles.rs +++ b/crates/horizon-client/src/game/titles.rs @@ -6,6 +6,7 @@ use horizon_client::{ hud::HudBatch, hud_layout, hud_overlays::HeldName, + item_tooltip, text::{self, Text}, }; @@ -24,22 +25,14 @@ impl Game<'_> { fn held_name(&self) -> Option { let stack = self.inventory.get(self.selected)?.as_ref()?; let custom = stack.custom_name(); - // ItemStack.getRarity: enchanting raises common and uncommon to rare - // and rare to epic. - let mut rarity = self.item_rarity(&stack.item); - if !stack.enchantments().is_empty() { - rarity = match rarity.as_str() { - "common" | "uncommon" => "rare".into(), - "rare" => "epic".into(), - _ => rarity, - }; - } + let default_rarity = self.item_rarity(&stack.item); + let rarity = item_tooltip::rarity(stack, &default_rarity); Some(HeldName { item: stack.item.to_string(), - hover_name: Text::literal( - custom.map_or_else(|| self.pack.names.item(&stack.item), str::to_owned), - ), - rarity_color: match rarity.as_str() { + hover_name: Text::literal(item_tooltip::name_line(stack, &|key| { + self.pack.names.get(key).map(str::to_owned) + })), + rarity_color: match rarity { "uncommon" => text::YELLOW, "rare" => text::AQUA, "epic" => text::LIGHT_PURPLE, diff --git a/crates/horizon-client/src/game/tweaks.rs b/crates/horizon-client/src/game/tweaks.rs index dc87a13bf..018b6640b 100644 --- a/crates/horizon-client/src/game/tweaks.rs +++ b/crates/horizon-client/src/game/tweaks.rs @@ -28,7 +28,7 @@ use horizon_client::{ menu::Input, stations::hover_name, }; -use winit::keyboard::KeyCode; +use winit::{event::MouseButton, keyboard::KeyCode}; /// A sort button's side in GUI pixels. const SORT_BUTTON: f32 = 9.0; @@ -377,8 +377,14 @@ impl Game<'_> { /// The sort key: sorts the part under the cursor (the container when /// the cursor is over no slot). pub(super) fn sort_key_press(&mut self, key: KeyCode) -> bool { + keybinds::key_name(key).is_some_and(|name| self.sort_input(name)) + } + pub(super) fn sort_mouse_press(&mut self, button: MouseButton) -> bool { + self.sort_input(&keybinds::mouse_name(button)) + } + fn sort_input(&mut self, name: &str) -> bool { let e = &self.settings.extras; - if !e.sort || keybinds::key_code(&e.sort_key) != Some(key) || self.recipe_search_focused() { + if !e.sort || e.sort_key != name || self.recipe_search_focused() { return false; } let buttons = self.sort_buttons(); diff --git a/crates/horizon-client/src/gfx.rs b/crates/horizon-client/src/gfx.rs index 4401ea22d..7391ad8b9 100644 --- a/crates/horizon-client/src/gfx.rs +++ b/crates/horizon-client/src/gfx.rs @@ -64,6 +64,7 @@ mod pack_stages; pub mod panorama; mod resident; mod timing; +pub mod translucency; pub(crate) mod upload; mod world_post; @@ -98,6 +99,8 @@ struct SectionGpu { translucent: u32, /// The solid quads by facing: back-facing groups are skipped. faces: crate::mesher::FaceGroups, + sort: translucency::SortState, + sorted_indices: Option<(u32, u64)>, } /// Line segments the hit outline can hold. const MAX_OUTLINE_EDGES: u64 = 256; @@ -133,6 +136,8 @@ pub struct Frame<'a> { pub dark_disc: bool, /// The hit block outline as line segments (from, to) in world space. pub outline: Vec<[f64; 6]>, + /// `BlockOutlineRenderState.isTranslucent`: draw after translucent terrain. + pub outline_after_terrain: bool, /// `lines` with a colour and pixel width (`gizmos::camera_relative`): /// the fishing line and debug gizmos, depth tested, then gizmos drawn /// over everything. @@ -222,6 +227,7 @@ struct BorderUniform { } pub struct Renderer { instance: wgpu::Instance, + adapter: wgpu::Adapter, /// The window's swapchain; `None` when rendering offscreen (`--offscreen`). surface: Option>, /// The offscreen colour target, sized like `config`. @@ -290,7 +296,13 @@ pub struct Renderer { group: wgpu::BindGroup, globals: wgpu::Buffer, lightmap: wgpu::Texture, - sprite_texture: wgpu::Texture, + /// The block sprites, [`SPRITE_SPLIT`] layers per array (the first is + /// `sprites` in terrain.wgsl, the others `sprites_1`...). + sprite_textures: Vec, + /// A 1-layer array bound where a pack needs fewer arrays. + sprite_unused: wgpu::TextureView, + /// Layers per sprite array ([`SPRITE_SPLIT`] but in tests). + sprite_split: u32, mip_levels: u32, /// The mipmap levels sampled (the Mipmap Levels option), at most `mip_levels - 1`. mipmaps: u32, @@ -311,6 +323,9 @@ pub struct Renderer { depth: wgpu::TextureView, sections: resident::Resident, arena: arena::Arena, + sorted_indices: arena::Arena, + translucent_sorter: translucency::Sorter, + translucency_cursor: usize, /// `DrawIndexedIndirectArgs` for this frame's solid then translucent draws. indirect: wgpu::Buffer, indirect_capacity: u64, @@ -322,8 +337,8 @@ pub struct Renderer { /// `DrawIndexedIndirectArgs` words: index count, instance count, first /// index, base vertex, first instance. draws: Vec<[u32; 5]>, - /// The arena page of each of `draws`. - draw_pages: Vec, + /// Vertex arena page and optional sorted index arena page of each draw. + draw_pages: Vec<(u32, Option)>, /// Block-entity blocks per meshed section (world position, state id). block_entities: HashMap>, /// VisGraph face connectivity per meshed section (cave culling). @@ -348,12 +363,87 @@ pub struct Renderer { pub screenshot_result: Option>, /// Brightness option (lightmap gamma), 0..1. pub gamma: f32, + /// Optional full-light lightmap (Horizon Extras). + pub fullbright: bool, /// Advance animated block textures (Horizon setting; vanilla always does). pub animated_textures: bool, present_modes: Vec, } /// Every layer's mip levels, layer major (`first` is the first layer's /// index into `settings`), made by `MipmapGenerator`. +/// Block sprite layers per texture array: the smallest +/// `max_texture_array_layers` of the GPUs Horizon runs on. Mods add sprites +/// past it into more arrays (terrain.wgsl's `sprite`). +pub(crate) const SPRITE_SPLIT: u32 = 2048; +/// [`SPRITE_SPLIT`], or a smaller split from `HORIZON_SPRITE_SPLIT` (dev +/// only: vanilla's sprites drawn from several arrays must look the same). +fn sprite_split() -> u32 { + std::env::var("HORIZON_SPRITE_SPLIT") + .ok() + .and_then(|v| v.parse().ok()) + .filter(|&n| (1..=SPRITE_SPLIT).contains(&n)) + .unwrap_or(SPRITE_SPLIT) +} +/// Sprite arrays terrain.wgsl binds (`sprites`, `sprites_1`...). +const SPRITE_ARRAYS: u32 = 4; +/// The views of terrain.wgsl's sprite arrays, the unused ones a 1-layer +/// stand-in. +fn sprite_views( + textures: &[wgpu::Texture], + unused: &wgpu::TextureView, + mips: Option, +) -> Vec { + (0..SPRITE_ARRAYS as usize) + .map(|i| match textures.get(i) { + Some(t) => t.create_view(&wgpu::TextureViewDescriptor { + dimension: Some(wgpu::TextureViewDimension::D2Array), + mip_level_count: mips, + ..Default::default() + }), + None => unused.clone(), + }) + .collect() +} +/// terrain.wgsl's group 0. +#[allow(clippy::too_many_arguments)] +fn terrain_group( + device: &wgpu::Device, + label: &str, + layout: &wgpu::BindGroupLayout, + globals: &wgpu::Buffer, + sprites: &[wgpu::TextureView], + sprite_sampler: &wgpu::Sampler, + lightmap: &wgpu::TextureView, + lightmap_sampler: &wgpu::Sampler, +) -> wgpu::BindGroup { + let view = |binding, view| wgpu::BindGroupEntry { + binding, + resource: wgpu::BindingResource::TextureView(view), + }; + device.create_bind_group(&wgpu::BindGroupDescriptor { + label: Some(label), + layout, + entries: &[ + wgpu::BindGroupEntry { + binding: 0, + resource: globals.as_entire_binding(), + }, + view(1, &sprites[0]), + wgpu::BindGroupEntry { + binding: 2, + resource: wgpu::BindingResource::Sampler(sprite_sampler), + }, + view(3, lightmap), + wgpu::BindGroupEntry { + binding: 4, + resource: wgpu::BindingResource::Sampler(lightmap_sampler), + }, + view(5, &sprites[1]), + view(6, &sprites[2]), + view(7, &sprites[3]), + ], + }) +} fn mip_chain( pixels: &[u8], size: u32, @@ -454,8 +544,14 @@ impl Renderer { hud_atlas: (&[u8], u32, u32), forcefield: (&[u8], u32), panorama_faces: &panorama::Faces, + previous: Option<&Self>, ) -> Result { - let instance = wgpu::Instance::new(instance_descriptor()); + // Reload resources on the same device; replacing live Vulkan devices + // can invalidate the driver's swapchain state on the same window. + let instance = previous.map_or_else( + || wgpu::Instance::new(instance_descriptor()), + |renderer| renderer.instance.clone(), + ); // Offscreen rendering needs no surface, so it can use a GPU the // window's display cannot present from (a headless compositor). let surface = if offscreen { @@ -463,15 +559,18 @@ impl Renderer { } else { Some(instance.create_surface(window.clone())?) }; - let adapter = instance - .request_adapter(&wgpu::RequestAdapterOptions { - power_preference: wgpu::PowerPreference::HighPerformance, - compatible_surface: surface.as_ref(), - force_fallback_adapter: false, - ..Default::default() - }) - .await - .map_err(|e| format!("{NO_GPU_DRIVER} ({e})"))?; + let adapter = match previous { + Some(renderer) => renderer.adapter.clone(), + None => instance + .request_adapter(&wgpu::RequestAdapterOptions { + power_preference: wgpu::PowerPreference::HighPerformance, + compatible_surface: surface.as_ref(), + force_fallback_adapter: false, + ..Default::default() + }) + .await + .map_err(|e| format!("{NO_GPU_DRIVER} ({e})"))?, + }; let info = adapter.get_info(); log::info!( "Horizon renderer: {} ({:?}, {:?})", @@ -494,22 +593,36 @@ impl Renderer { ), ); let limits = adapter.limits(); - if limits.max_texture_array_layers < layers { + let split = sprite_split(); + if layers > split * SPRITE_ARRAYS { return Err(format!( - "GPU supports {} texture layers; {layers} required", - limits.max_texture_array_layers + "{layers} block textures; Horizon draws at most {}", + split * SPRITE_ARRAYS ) .into()); } - let (device, queue) = adapter - .request_device(&wgpu::DeviceDescriptor { - label: Some("Horizon device"), - required_features: timing::GpuTimer::features(&adapter) - | (adapter.features() & wgpu::Features::INDIRECT_FIRST_INSTANCE), - required_limits: limits.clone(), - ..Default::default() - }) - .await?; + if limits.max_texture_array_layers < layers.min(split) { + return Err(format!( + "GPU supports {} texture layers; {} required", + limits.max_texture_array_layers, + layers.min(split) + ) + .into()); + } + let (device, queue) = match previous { + Some(renderer) => (renderer.device.clone(), renderer.queue.clone()), + None => { + adapter + .request_device(&wgpu::DeviceDescriptor { + label: Some("Horizon device"), + required_features: timing::GpuTimer::features(&adapter) + | (adapter.features() & wgpu::Features::INDIRECT_FIRST_INSTANCE), + required_limits: limits.clone(), + ..Default::default() + }) + .await? + } + }; let size = window.inner_size(); let (config, present_modes) = if let Some(surface) = &surface { let mut config = surface @@ -528,11 +641,16 @@ impl Renderer { if caps.usages.contains(wgpu::TextureUsages::COPY_SRC) { config.usage |= wgpu::TextureUsages::COPY_SRC; } - config.present_mode = if caps.present_modes.contains(&wgpu::PresentMode::Mailbox) { - wgpu::PresentMode::Mailbox - } else { - wgpu::PresentMode::Fifo - }; + config.present_mode = previous + .map(|renderer| renderer.config.present_mode) + .filter(|mode| caps.present_modes.contains(mode)) + .unwrap_or_else(|| { + if caps.present_modes.contains(&wgpu::PresentMode::Mailbox) { + wgpu::PresentMode::Mailbox + } else { + wgpu::PresentMode::Fifo + } + }); surface.configure(&device, &config); (config, caps.present_modes.clone()) } else { @@ -565,6 +683,19 @@ impl Renderer { count: None, }; let vf = wgpu::ShaderStages::VERTEX_FRAGMENT; + let sprite_array = wgpu::BindingType::Texture { + sample_type: wgpu::TextureSampleType::Float { filterable: true }, + view_dimension: wgpu::TextureViewDimension::D2Array, + multisampled: false, + }; + let sprite_arrays = layers.div_ceil(split).max(1); + if sprite_arrays > 1 { + log::info!("Horizon: {layers} block textures in {sprite_arrays} arrays"); + } + let sprite_constants = [ + ("SPRITE_ARRAYS", f64::from(sprite_arrays)), + ("SPRITE_SPLIT", f64::from(split)), + ]; let bindings = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: Some("Horizon terrain bindings"), entries: &[ @@ -605,6 +736,9 @@ impl Renderer { vf, wgpu::BindingType::Sampler(wgpu::SamplerBindingType::Filtering), ), + entry(5, vf, sprite_array), + entry(6, vf, sprite_array), + entry(7, vf, sprite_array), ], }); let layout = device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor { @@ -677,7 +811,10 @@ impl Renderer { fragment: Some(wgpu::FragmentState { module: &shader, entry_point: Some(fragment), - compilation_options: Default::default(), + compilation_options: wgpu::PipelineCompilationOptions { + constants: &sprite_constants, + ..Default::default() + }, targets: &[Some(wgpu::ColorTargetState { format: config.format.remove_srgb_suffix(), blend, @@ -707,7 +844,7 @@ impl Renderer { &terrain_buffers, tri, Some(wgpu::Face::Back), - false, + true, true, Some(alpha_blend), ); @@ -740,7 +877,7 @@ impl Renderer { &line_buffers, wgpu::PrimitiveTopology::TriangleStrip, None, - false, + true, true, Some(alpha_blend), ); @@ -784,30 +921,62 @@ impl Renderer { usage: wgpu::BufferUsages::UNIFORM | wgpu::BufferUsages::COPY_DST, mapped_at_creation: false, }); - let (mips, mip_levels) = mip_chain(sprite_pixels, sprite_size, sprite_mips, 0, layers); - let sprite_texture = device.create_texture_with_data( - &queue, - &wgpu::TextureDescriptor { - label: Some("Block sprites"), + let layer_bytes = (sprite_size * sprite_size * 4) as usize; + let mut mip_levels = 1; + let sprite_textures: Vec = (0..sprite_arrays) + .map(|array| { + let first = array * split; + let count = layers.saturating_sub(first).clamp(1, split); + let start = (first as usize * layer_bytes).min(sprite_pixels.len()); + let (mips, levels) = mip_chain( + &sprite_pixels[start..], + sprite_size, + sprite_mips, + first, + count, + ); + mip_levels = levels; + device.create_texture_with_data( + &queue, + &wgpu::TextureDescriptor { + label: Some("Block sprites"), + size: wgpu::Extent3d { + width: sprite_size, + height: sprite_size, + depth_or_array_layers: count, + }, + mip_level_count: levels, + sample_count: 1, + dimension: wgpu::TextureDimension::D2, + format: wgpu::TextureFormat::Rgba8Unorm, + usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + view_formats: &[], + }, + wgpu::util::TextureDataOrder::LayerMajor, + &mips, + ) + }) + .collect(); + let sprite_unused = device + .create_texture(&wgpu::TextureDescriptor { + label: Some("Block sprites (unused)"), size: wgpu::Extent3d { - width: sprite_size, - height: sprite_size, - depth_or_array_layers: layers, + width: 1, + height: 1, + depth_or_array_layers: 1, }, - mip_level_count: mip_levels, + mip_level_count: 1, sample_count: 1, dimension: wgpu::TextureDimension::D2, format: wgpu::TextureFormat::Rgba8Unorm, - usage: wgpu::TextureUsages::TEXTURE_BINDING | wgpu::TextureUsages::COPY_DST, + usage: wgpu::TextureUsages::TEXTURE_BINDING, view_formats: &[], - }, - wgpu::util::TextureDataOrder::LayerMajor, - &mips, - ); - let sprite_view = sprite_texture.create_view(&wgpu::TextureViewDescriptor { - dimension: Some(wgpu::TextureViewDimension::D2Array), - ..Default::default() - }); + }) + .create_view(&wgpu::TextureViewDescriptor { + dimension: Some(wgpu::TextureViewDimension::D2Array), + ..Default::default() + }); + let sprite_views = sprite_views(&sprite_textures, &sprite_unused, None); let sprite_sampler = device.create_sampler(&wgpu::SamplerDescriptor { label: Some("Block sprites"), address_mode_u: wgpu::AddressMode::Repeat, @@ -838,58 +1007,26 @@ impl Renderer { min_filter: wgpu::FilterMode::Linear, ..Default::default() }); - let group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("Horizon terrain resources"), - layout: &bindings, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: globals.as_entire_binding(), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView(&sprite_view), - }, - wgpu::BindGroupEntry { - binding: 2, - resource: wgpu::BindingResource::Sampler(&sprite_sampler), - }, - wgpu::BindGroupEntry { - binding: 3, - resource: wgpu::BindingResource::TextureView(&lightmap_view), - }, - wgpu::BindGroupEntry { - binding: 4, - resource: wgpu::BindingResource::Sampler(&lightmap_sampler), - }, - ], - }); - let crosshair_group = device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("Debug crosshair resources"), - layout: &bindings, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: crosshair_globals.as_entire_binding(), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView(&sprite_view), - }, - wgpu::BindGroupEntry { - binding: 2, - resource: wgpu::BindingResource::Sampler(&sprite_sampler), - }, - wgpu::BindGroupEntry { - binding: 3, - resource: wgpu::BindingResource::TextureView(&lightmap_view), - }, - wgpu::BindGroupEntry { - binding: 4, - resource: wgpu::BindingResource::Sampler(&lightmap_sampler), - }, - ], - }); + let group = terrain_group( + &device, + "Horizon terrain resources", + &bindings, + &globals, + &sprite_views, + &sprite_sampler, + &lightmap_view, + &lightmap_sampler, + ); + let crosshair_group = terrain_group( + &device, + "Debug crosshair resources", + &bindings, + &crosshair_globals, + &sprite_views, + &sprite_sampler, + &lightmap_view, + &lightmap_sampler, + ); let index_quads = 1 << 16; let indices = Self::quad_indices(&device, index_quads); let instance_capacity = 4096; @@ -1504,6 +1641,7 @@ impl Renderer { std::mem::size_of::() as u64, 1 << 20, ); + let sorted_indices = arena::Arena::indices(&device, 1 << 16); let indirect = device.create_buffer(&wgpu::BufferDescriptor { label: Some("Section draws"), size: 1024 * 20, @@ -1523,6 +1661,7 @@ impl Renderer { ); Ok(Self { instance, + adapter, surface, offscreen, in_flight: std::collections::VecDeque::new(), @@ -1575,7 +1714,9 @@ impl Renderer { group, globals, lightmap, - sprite_texture, + sprite_textures, + sprite_unused, + sprite_split: split, mip_levels, mipmaps: mip_levels - 1, terrain_layout: bindings, @@ -1594,6 +1735,9 @@ impl Renderer { depth, sections: resident::Resident::default(), arena, + sorted_indices, + translucent_sorter: translucency::Sorter::default(), + translucency_cursor: 0, indirect, indirect_capacity: 1024, multi_draw, @@ -1616,6 +1760,7 @@ impl Renderer { screenshot: None, screenshot_result: None, gamma: 0.5, + fullbright: false, animated_textures: true, present_modes, }) @@ -1656,6 +1801,22 @@ impl Renderer { pub fn is_offscreen(&self) -> bool { self.surface.is_none() } + /// Release the window surface before another renderer uses the same window. + pub fn release_surface(&mut self) { + self.surface = None; + } + /// Reattach a window after surface loss or a failed renderer replacement. + pub fn recreate_surface(&mut self) -> Result<()> { + if self.offscreen.is_some() { + return Ok(()); + } + // Wayland allows only one tearing-control object per window surface. + self.release_surface(); + let surface = self.instance.create_surface(self.window.clone())?; + surface.configure(&self.device, &self.config); + self.surface = Some(surface); + Ok(()) + } fn depth(device: &wgpu::Device, config: &wgpu::SurfaceConfiguration) -> wgpu::TextureView { device .create_texture(&wgpu::TextureDescriptor { @@ -1761,6 +1922,10 @@ impl Renderer { } let vertices = (mesh.solid.len() + mesh.translucent.len()) as u64; let (page, start) = self.arena.alloc(&self.device, &self.queue, vertices); + let sorted_indices = (translucent > 0).then(|| { + self.sorted_indices + .alloc(&self.device, &self.queue, u64::from(translucent) * 6) + }); let stride = std::mem::size_of::() as u64; for (offset, part) in [(0, &mesh.solid), (mesh.solid.len(), &mesh.translucent)] { if !part.is_empty() { @@ -1781,6 +1946,15 @@ impl Renderer { solid, translucent, faces: mesh.faces, + sort: translucency::SortState::new( + mesh.translucent + .as_chunks::<4>() + .0 + .iter() + .map(|q| translucency::quad_center(q[0].pos, q[2].pos)) + .collect(), + ), + sorted_indices, }, ); } @@ -1791,17 +1965,19 @@ impl Renderer { fn draw_sections(&self, pass: &mut wgpu::RenderPass<'_>, first: u32, count: u32) { let (mut i, end) = (first as usize, (first + count) as usize); while i < end { - let page = self.draw_pages[i]; + let (page, index_page) = self.draw_pages[i]; let run = self.draw_pages[i..end] .iter() - .take_while(|&&p| p == page) + .take_while(|&&p| p == (page, index_page)) .count(); pass.set_vertex_buffer(0, self.arena.buffer(page).slice(..)); + let indices = index_page.map_or(&self.indices, |p| self.sorted_indices.buffer(p)); + pass.set_index_buffer(indices.slice(..), wgpu::IndexFormat::Uint32); if self.multi_draw { pass.multi_draw_indexed_indirect(&self.indirect, i as u64 * 20, run as u32); } else { for d in &self.draws[i..i + run] { - pass.draw_indexed(0..d[0], d[3] as i32, d[4]..d[4] + 1); + pass.draw_indexed(d[2]..d[2] + d[0], d[3] as i32, d[4]..d[4] + 1); } } i += run; @@ -1809,6 +1985,10 @@ impl Renderer { } fn release_section(&mut self, key: SectionKey) { if let Some(s) = self.sections.remove(key) { + if let Some((page, start)) = s.sorted_indices { + self.sorted_indices + .release(page, start, u64::from(s.translucent) * 6); + } self.arena .release(s.page, s.start, u64::from(s.solid + s.translucent) * 4); } @@ -1821,6 +2001,8 @@ impl Renderer { self.block_entities.clear(); self.sections.clear(); self.arena.clear(); + self.sorted_indices.clear(); + self.translucency_cursor = 0; self.occlusion.clear(); } pub fn remove(&mut self, key: SectionKey) { @@ -1864,7 +2046,7 @@ impl Renderer { pub fn set_mipmap_levels(&mut self, levels: u32) { let levels = sprite_mipmap_limit( levels, - self.sprite_texture.width(), + self.sprite_textures[0].width(), self.sprite_minimum_size, self.mip_levels, ); @@ -1872,39 +2054,17 @@ impl Renderer { return; } self.mipmaps = levels; - let sprite_view = self - .sprite_texture - .create_view(&wgpu::TextureViewDescriptor { - dimension: Some(wgpu::TextureViewDimension::D2Array), - mip_level_count: Some(levels + 1), - ..Default::default() - }); - self.group = self.device.create_bind_group(&wgpu::BindGroupDescriptor { - label: Some("Horizon terrain resources"), - layout: &self.terrain_layout, - entries: &[ - wgpu::BindGroupEntry { - binding: 0, - resource: self.globals.as_entire_binding(), - }, - wgpu::BindGroupEntry { - binding: 1, - resource: wgpu::BindingResource::TextureView(&sprite_view), - }, - wgpu::BindGroupEntry { - binding: 2, - resource: wgpu::BindingResource::Sampler(&self.sprite_sampler), - }, - wgpu::BindGroupEntry { - binding: 3, - resource: wgpu::BindingResource::TextureView(&self.lightmap_view), - }, - wgpu::BindGroupEntry { - binding: 4, - resource: wgpu::BindingResource::Sampler(&self.lightmap_sampler), - }, - ], - }); + let views = sprite_views(&self.sprite_textures, &self.sprite_unused, Some(levels + 1)); + self.group = terrain_group( + &self.device, + "Horizon terrain resources", + &self.terrain_layout, + &self.globals, + &views, + &self.sprite_sampler, + &self.lightmap_view, + &self.lightmap_sampler, + ); } pub fn view_projection(&self, frame: &Frame) -> Mat4 { self.projection( @@ -1972,6 +2132,9 @@ impl Renderer { pixels[i + 3] = 255; } } + if self.fullbright { + pixels.fill(255); + } // The lightmap changes with the time of day, flicker and effects, // not every frame: upload only what differs from the texture. if pixels == self.lightmap_pixels && self.lightmap_uploaded { @@ -2001,12 +2164,12 @@ impl Renderer { self.uploads.texture( encoder, wgpu::TexelCopyTextureInfo { - texture: &self.sprite_texture, + texture: &self.sprite_textures[(layer / self.sprite_split) as usize], mip_level: level, origin: wgpu::Origin3d { x: 0, y: 0, - z: layer, + z: layer % self.sprite_split, }, aspect: wgpu::TextureAspect::All, }, @@ -2039,9 +2202,7 @@ impl Renderer { return Ok(false); } Some(wgpu::CurrentSurfaceTexture::Lost) => { - let surface = self.instance.create_surface(self.window.clone())?; - surface.configure(&self.device, &self.config); - self.surface = Some(surface); + self.recreate_surface()?; return Ok(false); } Some(wgpu::CurrentSurfaceTexture::Validation) => { @@ -2217,7 +2378,7 @@ impl Renderer { visible.clear(); self.sections.visible( camera, - frame.distance, + (frame.distance / 16.0) as i32, &view, |k| !culling || reachable.contains(k), &mut visible, @@ -2263,19 +2424,51 @@ impl Renderer { s.faces.ranges(eye - Vec3::from(v.origin), |first, quads| { let first = s.start + u64::from(first) * 4; self.draws.push([quads * 6, 1, 0, first as u32, i as u32]); - self.draw_pages.push(s.page); + self.draw_pages.push((s.page, None)); }); } let solid_draws = self.draws.len() as u32; + // LevelRenderer: nearby sections each frame, plus a rotating eighth + // (at least 15) of the visible list. Cached views avoid needless sorts. + let count = visible.len(); + let budget = (count / 8).max(15).min(count); + self.translucency_cursor %= count.max(1); for (i, v) in visible.iter().enumerate().rev() { - let s = self.sections.get(v.slot); - if s.translucent > 0 { + let s = self.sections.get_mut(v.slot); + if let Some((index_page, index_start)) = s.sorted_indices { + let nearby = (v.key.0 - camera_section.0).abs() <= 2 + && (v.key.1 - camera_section.1).abs() <= 2 + && (v.key.2 - camera_section.2).abs() <= 2; + let scheduled = (i + count - self.translucency_cursor) % count < budget; + let origin = [ + v.key.0 * 16, + crate::chunk::MIN_Y + v.key.1 * 16, + v.key.2 * 16, + ]; + if (!s.sort.is_sorted() || nearby || scheduled) + && let Some(indices) = + s.sort + .update(camera.to_array(), origin, &mut self.translucent_sorter) + { + self.uploads.buffer( + &mut encoder, + self.sorted_indices.buffer(index_page), + index_start * 4, + bytemuck::cast_slice(indices), + ); + } let first = s.start + u64::from(s.solid) * 4; - self.draws - .push([s.translucent * 6, 1, 0, first as u32, i as u32]); - self.draw_pages.push(s.page); + self.draws.push([ + s.translucent * 6, + 1, + index_start as u32, + first as u32, + i as u32, + ]); + self.draw_pages.push((s.page, Some(index_page))); } } + self.translucency_cursor += budget; let translucent_draws = self.draws.len() as u32 - solid_draws; if self.multi_draw && !self.draws.is_empty() { if self.draws.len() as u64 > self.indirect_capacity { @@ -2537,7 +2730,7 @@ impl Renderer { pass.draw_indexed(0..additive_quads * 6, (world_quads * 4) as i32, 0..1); pass.set_bind_group(0, &self.group, &[]); } - if outline_edges > 0 { + if outline_edges > 0 && !frame.outline_after_terrain { pass.set_pipeline(&self.lines); pass.set_vertex_buffer(0, self.outline.slice(..)); pass.draw(0..4, 0..outline_edges as u32); @@ -2556,6 +2749,14 @@ impl Renderer { pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); pass.set_vertex_buffer(1, self.instances.slice(..)); self.draw_sections(&mut pass, solid_draws, translucent_draws); + // LevelRenderer.executeClassicTransparency: translucent targets' + // outlines follow terrain, so its blending cannot wash them out. + if outline_edges > 0 && frame.outline_after_terrain { + pass.set_pipeline(&self.lines); + pass.set_vertex_buffer(0, self.outline.slice(..)); + pass.draw(0..4, 0..outline_edges as u32); + } + pass.set_index_buffer(self.indices.slice(..), wgpu::IndexFormat::Uint32); // LevelRenderer: the world border after translucent terrain and weather. if let Some(b) = frame.border.as_ref().filter(|_| !frame.title) { pass.set_pipeline(&self.border_pipeline); @@ -2776,6 +2977,7 @@ impl Renderer { &mut self.uploads, &mut encoder, &view_target, + &self.depth, frame.hud, [self.config.width, self.config.height], ); diff --git a/crates/horizon-client/src/gfx/arena.rs b/crates/horizon-client/src/gfx/arena.rs index 4793dab85..233333e04 100644 --- a/crates/horizon-client/src/gfx/arena.rs +++ b/crates/horizon-client/src/gfx/arena.rs @@ -52,31 +52,47 @@ pub struct Arena { pages: Vec, stride: u64, page_limit: u64, + usage: wgpu::BufferUsages, } impl Arena { pub fn new(device: &wgpu::Device, stride: u64, capacity: u64) -> Self { + Self::with_usage(device, stride, capacity, wgpu::BufferUsages::VERTEX) + } + pub fn indices(device: &wgpu::Device, capacity: u64) -> Self { + Self::with_usage(device, 4, capacity, wgpu::BufferUsages::INDEX) + } + fn with_usage( + device: &wgpu::Device, + stride: u64, + capacity: u64, + usage: wgpu::BufferUsages, + ) -> Self { // 256 MiB pages, or less where the device's buffers are smaller. let bytes = device.limits().max_buffer_size.min(256 << 20); let page_limit = (bytes / stride) / GRAIN * GRAIN; let capacity = capacity.next_multiple_of(GRAIN).min(page_limit); Self { pages: vec![Page { - buffer: Self::create(device, stride, capacity), + buffer: Self::create(device, stride, capacity, usage), capacity, free: FreeList::new(capacity), }], stride, page_limit, + usage, } } - fn create(device: &wgpu::Device, stride: u64, capacity: u64) -> wgpu::Buffer { + fn create( + device: &wgpu::Device, + stride: u64, + capacity: u64, + usage: wgpu::BufferUsages, + ) -> wgpu::Buffer { device.create_buffer(&wgpu::BufferDescriptor { - label: Some("Section vertex arena"), + label: Some("Section buffer arena"), size: capacity * stride, - usage: wgpu::BufferUsages::VERTEX - | wgpu::BufferUsages::COPY_DST - | wgpu::BufferUsages::COPY_SRC, + usage: usage | wgpu::BufferUsages::COPY_DST | wgpu::BufferUsages::COPY_SRC, mapped_at_creation: false, }) } @@ -121,7 +137,7 @@ impl Arena { let mut free = FreeList::new(capacity); let start = free.take(len).expect("a new page fits one section"); self.pages.push(Page { - buffer: Self::create(device, self.stride, capacity), + buffer: Self::create(device, self.stride, capacity, self.usage), capacity, free, }); @@ -133,7 +149,7 @@ impl Arena { } fn grow(&mut self, device: &wgpu::Device, queue: &wgpu::Queue, page: usize, capacity: u64) { let capacity = capacity.next_multiple_of(GRAIN).min(self.page_limit); - let buffer = Self::create(device, self.stride, capacity); + let buffer = Self::create(device, self.stride, capacity, self.usage); let old = &mut self.pages[page]; let mut encoder = device.create_command_encoder(&wgpu::CommandEncoderDescriptor { label: Some("Grow section arena"), diff --git a/crates/horizon-client/src/gfx/resident.rs b/crates/horizon-client/src/gfx/resident.rs index 58e187090..b7455b765 100644 --- a/crates/horizon-client/src/gfx/resident.rs +++ b/crates/horizon-client/src/gfx/resident.rs @@ -3,7 +3,7 @@ //! the render distance rejects all of its sections with one test, and the //! sections that remain are reached by index rather than by hashing keys. use super::{Frustum, SectionKey}; -use crate::fast_hash::FxHashMap; +use crate::{fast_hash::FxHashMap, streaming::in_view_distance}; use glam::{DVec3, Vec3}; /// One chunk column's resident sections. @@ -58,6 +58,9 @@ impl Resident { pub fn get(&self, slot: u32) -> &T { &self.items[slot as usize].1 } + pub fn get_mut(&mut self, slot: u32) -> &mut T { + &mut self.items[slot as usize].1 + } pub fn clear(&mut self) { self.items.clear(); self.slots.clear(); @@ -149,26 +152,28 @@ fn origin(key: SectionKey, camera: DVec3) -> DVec3 { ) - camera } -/// Whether the section at `key` is drawn: within `distance` blocks of the -/// camera horizontally (plus a section and a half), and partly inside the -/// frustum. `None` when it is not, else its squared centre distance and origin. -fn in_view( - key: SectionKey, - camera: DVec3, - distance: f32, - frustum: &Frustum, -) -> Option<(f64, Vec3)> { - let origin = origin(key, camera); - let center = origin + DVec3::splat(8.0); - let horizontal = (center.x * center.x + center.z * center.z).sqrt(); - if horizontal > f64::from(distance) + 24.0 { +/// The camera's chunk column. +fn camera_column(camera: DVec3) -> (i32, i32) { + ( + (camera.x.floor() as i32) >> 4, + (camera.z.floor() as i32) >> 4, + ) +} + +/// Whether the section at `key` is drawn: its column within the render +/// distance of the camera's (`SectionOcclusionGraph.isInViewDistance`), and +/// partly inside the frustum. `None` when it is not, else its squared centre +/// distance and origin. +fn in_view(key: SectionKey, camera: DVec3, radius: i32, frustum: &Frustum) -> Option<(f64, Vec3)> { + if !in_view_distance(camera_column(camera), radius, key.0, key.2) { return None; } + let origin = origin(key, camera); let min = origin.as_vec3(); if !frustum.intersects(min, min + Vec3::splat(16.0)) { return None; } - Some((center.length_squared(), min)) + Some(((origin + DVec3::splat(8.0)).length_squared(), min)) } impl Resident { @@ -178,22 +183,22 @@ impl Resident { /// Columns are tested first, with the same distance test (it depends /// only on x and z) and a box spanning their sections: every section box /// lies inside it and the frustum test rounds monotonically, so a column - /// the frustum rejects holds no section it would accept. + /// the frustum rejects holds no section it would accept. `radius` is the + /// render distance in chunks. pub fn visible( &self, camera: DVec3, - distance: f32, + radius: i32, frustum: &Frustum, reachable: impl Fn(SectionKey) -> bool, out: &mut Vec, ) { + let from = camera_column(camera); for column in &self.columns { - let low = origin((column.x, column.low, column.z), camera); - let center = low + DVec3::splat(8.0); - if (center.x * center.x + center.z * center.z).sqrt() > f64::from(distance) + 24.0 { + if !in_view_distance(from, radius, column.x, column.z) { continue; } - let min = low.as_vec3(); + let min = origin((column.x, column.low, column.z), camera).as_vec3(); let top = origin((column.x, column.high, column.z), camera).as_vec3(); if !frustum.intersects(min, top + Vec3::splat(16.0)) { continue; @@ -203,7 +208,7 @@ impl Resident { if !reachable(key) { continue; } - if let Some((d, o)) = in_view(key, camera, distance, frustum) { + if let Some((d, o)) = in_view(key, camera, radius, frustum) { out.push(Visible { distance: d.to_bits(), key, @@ -243,20 +248,20 @@ mod tests { (DVec3::new(8.0, 64.0, 8.0), 45.0, 89.0), ]; for (camera, yaw, pitch) in cameras { - for distance in [32.0, 96.0, 160.0] { + for radius in [2, 6, 10] { let projection = super::super::world_projection(70.0, 16.0 / 9.0, 512.0); let view = crate::camera::view_matrix(yaw, pitch); let frustum = Frustum { planes: super::super::frustum(projection * view), }; let mut fast = Vec::new(); - r.visible(camera, distance, &frustum, reachable, &mut fast); + r.visible(camera, radius, &frustum, reachable, &mut fast); let mut slow: Vec<(SectionKey, f64, [f32; 3])> = r .items .iter() .filter(|(k, _)| reachable(*k)) .filter_map(|(k, _)| { - in_view(*k, camera, distance, &frustum).map(|(d, o)| (*k, d, o.to_array())) + in_view(*k, camera, radius, &frustum).map(|(d, o)| (*k, d, o.to_array())) }) .collect(); slow.sort_unstable_by(|a, b| a.1.total_cmp(&b.1).then(a.0.cmp(&b.0))); @@ -268,7 +273,7 @@ mod tests { }) .collect(); assert!(!slow.is_empty()); - assert_eq!(fast, slow, "{camera} {yaw} {pitch} {distance}"); + assert_eq!(fast, slow, "{camera} {yaw} {pitch} {radius}"); } } } @@ -356,7 +361,7 @@ mod tests { let t = Instant::now(); keys.clear(); keys.extend(map.keys().filter_map(|&k| { - in_view(k, camera, 512.0, &frustum).map(|(d, o)| (k, d, o.to_array())) + in_view(k, camera, 32, &frustum).map(|(d, o)| (k, d, o.to_array())) })); keys.sort_unstable_by(|a, b| a.1.total_cmp(&b.1)); for (k, _, _) in &keys { @@ -365,7 +370,7 @@ mod tests { old = old.min(t.elapsed()); let t = Instant::now(); list.clear(); - r.visible(camera, 512.0, &frustum, |_| true, &mut list); + r.visible(camera, 32, &frustum, |_| true, &mut list); for v in &list { sum += i64::from(r.get(v.slot)[1] + r.get(v.slot)[0]); } diff --git a/crates/horizon-client/src/gfx/translucency.rs b/crates/horizon-client/src/gfx/translucency.rs new file mode 100644 index 000000000..927152a39 --- /dev/null +++ b/crates/horizon-client/src/gfx/translucency.rs @@ -0,0 +1,119 @@ +//! Vanilla 26.3 MeshData.SortState / VertexSorting.byDistance. +//! Keep vertex order intact; only the quad indices change as the camera moves. + +/// MeshData.decodeQuadCentroids uses the opposite corners, not all four vertices. +pub fn quad_center(a: [f32; 3], b: [f32; 3]) -> [f32; 3] { + std::array::from_fn(|i| (a[i] + b[i]) / 2.0) +} + +#[derive(Default)] +pub struct Sorter { + order: Vec<(u32, f32)>, + indices: Vec, +} + +impl Sorter { + pub fn sort(&mut self, centers: &[[f32; 3]], camera: [f32; 3]) -> &[u32] { + self.order.clear(); + self.order.extend(centers.iter().enumerate().map(|(i, c)| { + let [x, y, z] = std::array::from_fn(|j| camera[j] - c[j]); + (i as u32, x * x + y * y + z * z) + })); + // VertexSorting uses a stable merge sort, preserving mesh order for ties. + self.order.sort_by(|a, b| b.1.total_cmp(&a.1)); + self.indices.clear(); + for &(quad, _) in &self.order { + let v = quad * 4; + self.indices.extend([v, v + 1, v + 2, v + 2, v + 3, v]); + } + &self.indices + } +} + +pub struct SortState { + centers: Vec<[f32; 3]>, + last: Option<([i32; 3], [i32; 3])>, +} + +impl SortState { + pub fn new(centers: Vec<[f32; 3]>) -> Self { + Self { + centers, + last: None, + } + } + + pub fn is_sorted(&self) -> bool { + self.last.is_some() + } + + /// TranslucencyPointOfView and ResortTransparencyTask: diagonal views keep + /// their ordering until the point of view changes; aligned views also + /// resort on crossing a block boundary. Sub-block movement needs no upload. + pub fn update<'a>( + &mut self, + camera: [f64; 3], + origin: [i32; 3], + sorter: &'a mut Sorter, + ) -> Option<&'a [u32]> { + let block = camera.map(|v| v.floor() as i32); + let pov = std::array::from_fn(|i| ((block[i] >> 4) - (origin[i] >> 4)).clamp(-1, 1)); + if let Some((last_block, last_pov)) = self.last + && last_pov == pov + && (last_block == block || !pov.contains(&0)) + { + return None; + } + self.last = Some((block, pov)); + Some(sorter.sort( + &self.centers, + std::array::from_fn(|i| (camera[i] - f64::from(origin[i])) as f32), + )) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn equal_distances_keep_mesh_order_and_triangle_winding() { + let mut sorter = Sorter::default(); + assert_eq!( + sorter.sort( + &[[1.0, 0.0, 0.0], [-1.0, 0.0, 0.0], [4.0, 0.0, 0.0]], + [0.0; 3] + ), + &[8, 9, 10, 10, 11, 8, 0, 1, 2, 2, 3, 0, 4, 5, 6, 6, 7, 4] + ); + assert!(sorter.sort(&[], [0.0; 3]).is_empty()); + } + + #[test] + fn movement_reverses_faces_without_moving_vertices() { + let centers = [[0.5, 0.5, 0.0], [0.5, 0.5, 1.0]]; + let mut sorter = Sorter::default(); + assert_eq!(sorter.sort(¢ers, [0.5, 0.5, -2.0])[0], 4); + assert_eq!(sorter.sort(¢ers, [0.5, 0.5, 3.0])[0], 0); + } + + #[test] + fn cached_viewpoint_handles_negative_and_large_coordinates() { + let origin = [-30_000_000, -64, 30_000_000]; + let mut state = SortState::new(vec![[0.5; 3]]); + let mut sorter = Sorter::default(); + let camera = [-29_999_999.5, -63.5, 30_000_000.5]; + assert!(state.update(camera, origin, &mut sorter).is_some()); + assert!(state.update(camera, origin, &mut sorter).is_none()); + assert!( + state + .update([-29_999_999.1, -63.1, 30_000_000.9], origin, &mut sorter) + .is_none() + ); + assert!( + state + .update([-29_999_998.9, -63.1, 30_000_000.9], origin, &mut sorter) + .is_some() + ); + } +} diff --git a/crates/horizon-client/src/hud.rs b/crates/horizon-client/src/hud.rs index 77ba0e342..be4bdb52e 100644 --- a/crates/horizon-client/src/hud.rs +++ b/crates/horizon-client/src/hud.rs @@ -139,19 +139,74 @@ impl HudAtlas { (g.rect, g.advance) } pub fn build(root: &Path) -> Result { + Self::pack( + root, + &[ + "gui", + "item", + "block", + "font", + "environment", + "misc", + "painting", + "entity", + "particle", + "mob_effect", + "map", + ], + true, + ) + } + /// Empty slots written at run time: map textures (game/maps.rs), the + /// minimap, downloaded skins, world icons and the Horizon menu text. + fn slots(images: &mut Vec) { + for i in 0..MAP_SLOTS { + images.push((format!("horizon/map{i}"), 128, 128, vec![0; 128 * 128 * 4])); + } + // The minimap extra's image (game/extras.rs). + images.push(( + MINIMAP_SPRITE.into(), + crate::minimap::SIZE as u32, + crate::minimap::SIZE as u32, + vec![0; crate::minimap::SIZE * crate::minimap::SIZE * 4], + )); + // Downloaded skins (game/scoreboard.rs). + for i in 0..SKIN_SLOTS { + images.push((format!("horizon/skin{i}"), 64, 64, vec![0; 64 * 64 * 4])); + } + for i in 0..WORLD_ICON_SLOTS { + images.push(( + format!("horizon/world-icon{i}"), + 64, + 64, + vec![0; 64 * 64 * 4], + )); + } + // Horizon-style menu text and corner masks (ui_theme), rasterized at run time. + images.push(( + "horizon/ui-text".into(), + 2048, + 1024, + vec![0; 2048 * 1024 * 4], + )); + } + /// Only the font's metrics, for a game that draws nothing (the + /// integrated server): text measures as with the full atlas, without + /// holding its textures. The pixels are dropped once the glyph widths are + /// read, since nothing uploads them. + pub fn fonts(root: &Path) -> Result { + let mut atlas = Self::pack(root, &["font"], false)?; + atlas.pixels = Vec::new(); + Ok(atlas) + } + /// Pack the textures under these folders of `textures/`, and for the + /// `full` atlas the generated sprites and the run-time slots too. + fn pack(root: &Path, folders: &[&str], full: bool) -> Result { let textures = root.join("assets/minecraft/textures"); let mut files = Vec::new(); - walk(&textures.join("gui"), &mut files); - walk(&textures.join("item"), &mut files); - walk(&textures.join("block"), &mut files); - walk(&textures.join("font"), &mut files); - walk(&textures.join("environment"), &mut files); - walk(&textures.join("misc"), &mut files); - walk(&textures.join("painting"), &mut files); - walk(&textures.join("entity"), &mut files); - walk(&textures.join("particle"), &mut files); - walk(&textures.join("mob_effect"), &mut files); - walk(&textures.join("map"), &mut files); + for folder in folders { + walk(&textures.join(folder), &mut files); + } files.retain(|p| { let s = p.to_string_lossy(); !s.contains("title/background") && !s.contains("realms") && !s.contains("presets") @@ -187,7 +242,9 @@ impl HudAtlas { .flat_map(|h| h.join().unwrap_or_default()) .collect() }); - images.extend(atlas_sprites(root, &images)); + if full { + images.extend(atlas_sprites(root, &images)); + } // The title screen's "JAVA EDITION" banner reads "RUST EDITION". if let Some(edition) = images .iter_mut() @@ -198,36 +255,9 @@ impl HudAtlas { edition.3 = pixels; } images.push(("horizon/white".into(), 4, 4, vec![255; 64])); - // Map textures (game/maps.rs) are written into these slots at run time. - for i in 0..MAP_SLOTS { - images.push((format!("horizon/map{i}"), 128, 128, vec![0; 128 * 128 * 4])); + if full { + Self::slots(&mut images); } - // The minimap extra's image (game/extras.rs). - images.push(( - MINIMAP_SPRITE.into(), - crate::minimap::SIZE as u32, - crate::minimap::SIZE as u32, - vec![0; crate::minimap::SIZE * crate::minimap::SIZE * 4], - )); - // Downloaded skins (game/scoreboard.rs) likewise. - for i in 0..SKIN_SLOTS { - images.push((format!("horizon/skin{i}"), 64, 64, vec![0; 64 * 64 * 4])); - } - for i in 0..WORLD_ICON_SLOTS { - images.push(( - format!("horizon/world-icon{i}"), - 64, - 64, - vec![0; 64 * 64 * 4], - )); - } - // Horizon-style menu text and corner masks (ui_theme), rasterized at run time. - images.push(( - "horizon/ui-text".into(), - 2048, - 1024, - vec![0; 2048 * 1024 * 4], - )); images.sort_by(|a, b| b.2.cmp(&a.2).then(b.1.cmp(&a.1))); let width = 4096u32; let mut placements = Vec::with_capacity(images.len()); @@ -319,6 +349,7 @@ pub struct HudVertex { pub pos: [f32; 2], pub uv: [f32; 2], pub color: u32, + pub depth: f32, } /// The GUI Scale option (0 = auto). pub static GUI_SCALE: std::sync::atomic::AtomicU32 = std::sync::atomic::AtomicU32::new(0); @@ -330,10 +361,10 @@ pub struct HudBatch { pub height: f32, /// Glyph pixels to upload into the atlas before drawing. pub patches: Vec, - /// Vertex ranges drawn with another blend than alpha blending, in order. + /// Vertex ranges with another blend or depth/scissor state, in order. pub blends: Vec<(std::ops::Range, Blend)>, } -/// How a run of HUD vertices blends with what is under it. +/// How a run of HUD vertices blends and tests depth. #[derive(Clone, Copy, PartialEq, Eq, Debug)] pub enum Blend { /// `BlendFunction.TRANSLUCENT`. @@ -342,6 +373,8 @@ pub enum Blend { Invert, /// `BlendFunction.ADDITIVE` (`RenderPipelines.GUI_TEXT_HIGHLIGHT`). Additive, + /// Alpha blending with an isolated depth buffer and a pixel scissor. + Depth([u32; 4]), } pub fn rgba(r: u8, g: u8, b: u8, a: u8) -> u32 { u32::from(a) << 24 | u32::from(b) << 16 | u32::from(g) << 8 | u32::from(r) @@ -430,9 +463,44 @@ impl HudBatch { pos: p[i], uv: uv[i], color, + depth: 0.0, }); } } + pub fn quad_3d(&mut self, p: [[f32; 3]; 4], uv: [[f32; 2]; 4], color: u32) { + for i in [0, 1, 2, 0, 2, 3] { + self.vertices.push(HudVertex { + pos: [p[i][0], p[i][1]], + uv: uv[i], + color, + depth: p[i][2], + }); + } + } + /// Keep per-vertex depth for an orthographic GUI model, with +z toward the viewer. + pub fn depth_since(&mut self, start: usize, clip: [f32; 4]) { + let vertices = &mut self.vertices[start..]; + if vertices.is_empty() { + return; + } + let min = vertices + .iter() + .map(|v| v.depth) + .fold(f32::INFINITY, f32::min); + let max = vertices + .iter() + .map(|v| v.depth) + .fold(f32::NEG_INFINITY, f32::max); + for v in vertices { + v.depth = (max - v.depth) / (max - min).max(f32::EPSILON); + } + let [x0, y0, x1, y1] = clip.map(|v| (v * self.scale).max(0.0) as u32); + // Separate previews clear depth independently, even if their scissors match. + self.blends.push(( + start as u32..self.vertices.len() as u32, + Blend::Depth([x0, y0, x1.saturating_sub(x0), y1.saturating_sub(y0)]), + )); + } pub fn rect_uv(&mut self, x: f32, y: f32, w: f32, h: f32, r: Rect, color: u32) { self.quad( [[x, y], [x + w, y], [x + w, y + h], [x, y + h]], @@ -569,6 +637,7 @@ impl HudBatch { pos: p[i], uv, color: c[i], + depth: 0.0, }); } } @@ -729,6 +798,7 @@ pub struct HudRenderer { /// `RenderPipelines.CROSSHAIR`. invert_pipeline: wgpu::RenderPipeline, additive_pipeline: wgpu::RenderPipeline, + depth_pipeline: wgpu::RenderPipeline, group: wgpu::BindGroup, uniform: wgpu::Buffer, buffer: wgpu::Buffer, @@ -808,8 +878,8 @@ impl HudRenderer { bind_group_layouts: &[Some(&layout)], ..Default::default() }); - let attributes = wgpu::vertex_attr_array![0=>Float32x2,1=>Float32x2,2=>Unorm8x4]; - let create = |label, blend, fragment_entry| { + let attributes = wgpu::vertex_attr_array![0=>Float32x2,1=>Float32x2,2=>Unorm8x4,3=>Float32]; + let create = |label, blend, fragment_entry, depth: bool| { device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { label: Some(label), layout: Some(&pipeline_layout), @@ -824,7 +894,13 @@ impl HudRenderer { })], }, primitive: Default::default(), - depth_stencil: None, + depth_stencil: depth.then_some(wgpu::DepthStencilState { + format: wgpu::TextureFormat::Depth32Float, + depth_write_enabled: Some(true), + depth_compare: Some(wgpu::CompareFunction::LessEqual), + stencil: Default::default(), + bias: Default::default(), + }), multisample: Default::default(), fragment: Some(wgpu::FragmentState { module: &shader, @@ -840,7 +916,13 @@ impl HudRenderer { cache: None, }) }; - let pipeline = create("HUD", wgpu::BlendState::ALPHA_BLENDING, "fs_main"); + let pipeline = create("HUD", wgpu::BlendState::ALPHA_BLENDING, "fs_main", false); + let depth_pipeline = create( + "HUD model", + wgpu::BlendState::ALPHA_BLENDING, + "fs_main", + true, + ); // BlendFunction.INVERT: ONE_MINUS_DST_COLOR, ONE_MINUS_SRC_COLOR, // ONE, ZERO. let invert_pipeline = create( @@ -858,6 +940,7 @@ impl HudRenderer { }, }, "fs_crosshair", + false, ); // BlendFunction.ADDITIVE: ONE, ONE. let add = wgpu::BlendComponent { @@ -872,6 +955,7 @@ impl HudRenderer { alpha: add, }, "fs_main", + false, ); let capacity = 1 << 16; let buffer = device.create_buffer(&wgpu::BufferDescriptor { @@ -886,6 +970,7 @@ impl HudRenderer { pipeline, invert_pipeline, additive_pipeline, + depth_pipeline, group, uniform, buffer, @@ -935,6 +1020,7 @@ impl HudRenderer { uploads: &mut crate::gfx::upload::Uploads, encoder: &mut wgpu::CommandEncoder, target: &wgpu::TextureView, + depth: &wgpu::TextureView, batch: &HudBatch, size: [u32; 2], ) { @@ -965,7 +1051,7 @@ impl HudRenderer { ); } } - if batch.vertices.is_empty() { + if batch.vertices.is_empty() || size.contains(&0) { return; } let count = batch.vertices.len() as u64; @@ -1001,20 +1087,55 @@ impl HudRenderer { store: wgpu::StoreOp::Store, }, })]; - let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { - label: Some("HUD"), - color_attachments: &attachments, - ..Default::default() - }); - pass.set_bind_group(0, &self.group, &[]); - pass.set_vertex_buffer(0, self.buffer.slice(..)); - for (range, blend) in batch.runs() { - pass.set_pipeline(match blend { - Blend::Alpha => &self.pipeline, - Blend::Invert => &self.invert_pipeline, - Blend::Additive => &self.additive_pipeline, + let runs = batch.runs(); + let mut at = 0; + while at < runs.len() { + let clip = match runs[at].1 { + Blend::Depth(clip) => Some(clip), + _ => None, + }; + let end = if clip.is_some() { + at + 1 + } else { + runs[at..] + .iter() + .position(|(_, b)| matches!(b, Blend::Depth(_))) + .map_or(runs.len(), |n| at + n) + }; + let mut pass = encoder.begin_render_pass(&wgpu::RenderPassDescriptor { + label: Some("HUD"), + color_attachments: &attachments, + depth_stencil_attachment: clip.map(|_| wgpu::RenderPassDepthStencilAttachment { + view: depth, + depth_ops: Some(wgpu::Operations { + load: wgpu::LoadOp::Clear(1.0), + store: wgpu::StoreOp::Discard, + }), + stencil_ops: None, + }), + ..Default::default() }); - pass.draw(range, 0..1); + pass.set_bind_group(0, &self.group, &[]); + pass.set_vertex_buffer(0, self.buffer.slice(..)); + if let Some([x, y, w, h]) = clip { + let (x, y) = (x.min(size[0]), y.min(size[1])); + let (w, h) = (w.min(size[0] - x), h.min(size[1] - y)); + if w == 0 || h == 0 { + at = end; + continue; + } + pass.set_scissor_rect(x, y, w, h); + } + for (range, blend) in &runs[at..end] { + pass.set_pipeline(match blend { + Blend::Alpha => &self.pipeline, + Blend::Invert => &self.invert_pipeline, + Blend::Additive => &self.additive_pipeline, + Blend::Depth(_) => &self.depth_pipeline, + }); + pass.draw(range.clone(), 0..1); + } + at = end; } } } @@ -1023,6 +1144,43 @@ impl HudRenderer { mod tests { use super::*; + #[test] + fn the_fonts_only_atlas_measures_text_like_the_full_one() { + // A synthetic default font: two 8x8 glyphs, 3 and 5 pixels wide. + let root = std::env::temp_dir().join(format!("horizon-fonts-atlas-{}", std::process::id())); + let font = root.join("assets/minecraft/font"); + let textures = root.join("assets/minecraft/textures/font"); + fs::create_dir_all(&font).unwrap(); + fs::create_dir_all(&textures).unwrap(); + fs::write( + font.join("default.json"), + r#"{"providers":[{"type":"bitmap","file":"minecraft:font/test.png","ascent":7,"chars":["ab"]}]}"#, + ) + .unwrap(); + let mut pixels = vec![0u8; 16 * 8 * 4]; + for y in 0..8 { + for x in (0..3).chain(8..13) { + pixels[(y * 16 + x) * 4 + 3] = 255; + } + } + let mut encoder = + png::Encoder::new(fs::File::create(textures.join("test.png")).unwrap(), 16, 8); + encoder.set_color(png::ColorType::Rgba); + encoder.set_depth(png::BitDepth::Eight); + encoder + .write_header() + .unwrap() + .write_image_data(&pixels) + .unwrap(); + let full = HudAtlas::build(&root).unwrap(); + let fonts = HudAtlas::fonts(&root).unwrap(); + let _ = fs::remove_dir_all(&root); + assert!(fonts.pixels.is_empty()); + let width = full.font.width("abba"); + assert!(width > 0.0); + assert_eq!(fonts.font.width("abba"), width); + } + #[test] fn effect_icons_are_painted_from_the_mob_effect_directory() { use crate::{effects::EffectInstance, hud_layout, hud_overlays, inventory_effects}; @@ -1092,6 +1250,41 @@ mod tests { fs::remove_dir_all(root).unwrap(); } + #[test] + fn gui_models_keep_separate_depth_runs_between_overlay_draws() { + let mut batch = HudBatch::new([640, 480]); + batch.scale = 2.0; + let p = [ + [0.0, 0.0, 2.0], + [0.0, 1.0, 2.0], + [1.0, 1.0, 2.0], + [1.0, 0.0, 2.0], + ]; + let uv = [[0.0; 2]; 4]; + batch.quad([[0.0; 2]; 4], uv, u32::MAX); + for _ in 0..2 { + let start = batch.vertices.len(); + batch.quad_3d(p, uv, u32::MAX); + batch.depth_since(start, [1.0, 2.0, 4.0, 6.0]); + } + batch.quad([[0.0; 2]; 4], uv, u32::MAX); + assert_eq!( + batch.runs(), + vec![ + (0..6, Blend::Alpha), + (6..12, Blend::Depth([2, 4, 6, 8])), + (12..18, Blend::Depth([2, 4, 6, 8])), + (18..24, Blend::Alpha), + ] + ); + assert!( + batch + .vertices + .iter() + .all(|v| v.depth.is_finite() && (0.0..=1.0).contains(&v.depth)) + ); + } + #[test] fn hud_shader_validates_both_fragment_entries() { let module = naga::front::wgsl::parse_str(include_str!("hud.wgsl")).unwrap(); diff --git a/crates/horizon-client/src/hud.wgsl b/crates/horizon-client/src/hud.wgsl index 055bfc1f1..17d50d51a 100644 --- a/crates/horizon-client/src/hud.wgsl +++ b/crates/horizon-client/src/hud.wgsl @@ -7,10 +7,10 @@ struct Out { @location(0) uv: vec2, @location(1) color: vec4, } -@vertex fn vs_main(@location(0) pos: vec2, @location(1) uv: vec2, @location(2) color: vec4) -> Out { +@vertex fn vs_main(@location(0) pos: vec2, @location(1) uv: vec2, @location(2) color: vec4, @location(3) depth: f32) -> Out { let pixels = pos * hud.size.z; var out: Out; - out.clip = vec4(pixels.x / hud.size.x * 2.0 - 1.0, 1.0 - pixels.y / hud.size.y * 2.0, 0.0, 1.0); + out.clip = vec4(pixels.x / hud.size.x * 2.0 - 1.0, 1.0 - pixels.y / hud.size.y * 2.0, depth, 1.0); out.uv = uv; out.color = color; return out; diff --git a/crates/horizon-client/src/item_tooltip.rs b/crates/horizon-client/src/item_tooltip.rs index cd827d411..77abbf8cf 100644 --- a/crates/horizon-client/src/item_tooltip.rs +++ b/crates/horizon-client/src/item_tooltip.rs @@ -1,8 +1,9 @@ -//! The lines of an item's tooltip as `ItemStack.getTooltipLines` writes them -//! without a player and without advanced tooltips, for stacks as recipes -//! produce them. The recipe book's search tree matches them, so recipes of -//! tools and armour answer to "attack speed" or "when on head". -use crate::items::{ItemInfo, ItemStack}; +//! Item tooltip formatting: equipment modifiers with a player's base values, +//! and lines without player context for the recipe book's search tree. +use crate::{ + hud::rgba, + items::{ItemInfo, ItemStack}, +}; /// `EquipmentSlotGroup` in declaration order, the order tooltips list them in. const SLOT_GROUPS: [&str; 11] = [ @@ -25,6 +26,7 @@ fn modifier_line( attribute: &str, amount: f64, operation: usize, + total: bool, ) -> Option { let text = |key: &str| translate(key).unwrap_or_else(|| key.to_owned()); let scale = match operation { @@ -32,7 +34,9 @@ fn modifier_line( 0 => 1.0, _ => 100.0, }; - let (kind, shown) = if amount > 0.0 { + let (kind, shown) = if total { + ("equals", amount * scale) + } else if amount > 0.0 { ("plus", amount * scale) } else if amount < 0.0 { ("take", -amount * scale) @@ -47,22 +51,27 @@ fn modifier_line( ) } -/// `ItemStack.addAttributeTooltips` for the item's own modifiers: per slot -/// group an empty line, the group's heading and a line per modifier. -fn attribute_lines(info: &ItemInfo, translate: &dyn Fn(&str) -> Option) -> Vec { +/// `ItemStack.addAttributeTooltips`: slot groups, colors and optional player +/// attack damage/speed bases. Recipe search has no player context. +pub fn attribute_lines( + modifiers: &[(String, String, f64, String, String)], + player_base: Option<[f64; 2]>, + translate: &dyn Fn(&str) -> Option, +) -> Vec<(String, u32)> { let mut lines = Vec::new(); for group in SLOT_GROUPS { let mut first = true; - for (attribute, _, amount, operation, slot) in &info.modifiers { + for (attribute, modifier, amount, operation, slot) in modifiers { if slot != group { continue; } if std::mem::take(&mut first) { - lines.push(String::new()); - lines.push( + lines.push((String::new(), u32::MAX)); + lines.push(( translate(&format!("item.modifiers.{group}")) .unwrap_or_else(|| format!("item.modifiers.{group}")), - ); + rgba(170, 170, 170, 255), + )); } let id = match operation.as_str() { "add_multiplied_base" => 1, @@ -70,7 +79,27 @@ fn attribute_lines(info: &ItemInfo, translate: &dyn Fn(&str) -> Option) _ => 0, }; let path = attribute.strip_prefix("minecraft:").unwrap_or(attribute); - lines.extend(modifier_line(translate, path, *amount, id)); + let base = player_base.and_then(|[damage, speed]| match modifier.as_str() { + "minecraft:base_attack_damage" => Some(damage), + "minecraft:base_attack_speed" => Some(speed), + _ => None, + }); + if let Some(line) = modifier_line( + translate, + path, + amount + base.unwrap_or(0.0), + id, + base.is_some(), + ) { + let (line, color) = if base.is_some() { + (format!(" {line}"), rgba(0, 170, 0, 255)) + } else if *amount > 0.0 { + (line, rgba(85, 85, 255, 255)) + } else { + (line, rgba(255, 85, 85, 255)) + }; + lines.push((line, color)); + } } } lines @@ -102,6 +131,10 @@ pub fn name_line(stack: &ItemStack, translate: &dyn Fn(&str) -> Option) if let Some(name) = stack.custom_name() { return name.to_owned(); } + if stack.data().is_some_and(|d| d.ominous_banner) { + return translate("block.minecraft.ominous_banner") + .unwrap_or_else(|| "Ominous Banner".to_owned()); + } let key = if is_potion_item(&stack.item) { Some(crate::brewing::potion_name_key(&stack.item, stack.potion())) } else if let Some(pattern) = path.strip_suffix("_armor_trim_smithing_template") { @@ -120,6 +153,24 @@ pub fn name_line(stack: &ItemStack, translate: &dyn Fn(&str) -> Option) .unwrap_or_else(|| path.replace('_', " ")) } +/// ItemStack.getRarity: the ominous banner override, then the enchantment upgrade. +pub fn rarity<'a>(stack: &ItemStack, default: &'a str) -> &'a str { + let rarity = if stack.data().is_some_and(|d| d.ominous_banner) { + "uncommon" + } else { + default + }; + if stack.enchantments().is_empty() { + rarity + } else { + match rarity { + "common" | "uncommon" => "rare", + "rare" => "epic", + r => r, + } + } +} + /// The lines of `stack` below its name that its components give, in /// `ItemStack.addDetailsToTooltip` order. pub fn detail_lines( @@ -211,6 +262,7 @@ pub fn detail_lines( attribute, amount * (*amplifier + 1) as f64, operation, + false, ) }) .collect(); @@ -242,7 +294,11 @@ pub fn detail_lines( lines.push(text("item.dyed")); } if let Some(info) = info { - lines.extend(attribute_lines(info, translate)); + lines.extend( + attribute_lines(&info.modifiers, None, translate) + .into_iter() + .map(|(line, _)| line), + ); } lines } @@ -292,6 +348,26 @@ pub fn lines( mod tests { use super::*; + #[test] + fn ominous_banner_name_and_rarity_use_the_stack() { + let translate = |key: &str| match key { + "block.minecraft.ominous_banner" => Some("Ominous Banner".to_owned()), + "block.minecraft.white_banner" => Some("White Banner".to_owned()), + _ => None, + }; + let mut banner = crate::mobs::raider::ominous_banner(); + assert_eq!(name_line(&banner, &translate), "Ominous Banner"); + assert_eq!(name_line(&banner, &|_| None), "Ominous Banner"); + assert_eq!(rarity(&banner, "common"), "uncommon"); + banner.data_mut().custom_name = Some("Captain's Standard".into()); + assert_eq!(name_line(&banner, &translate), "Captain's Standard"); + banner.enchant("minecraft:unbreaking", 1); + assert_eq!(rarity(&banner, "common"), "rare"); + let plain = ItemStack::new("minecraft:white_banner", 1); + assert_eq!(name_line(&plain, &translate), "White Banner"); + assert_eq!(rarity(&plain, "common"), "common"); + } + #[test] fn container_lines_list_five_stacks_then_how_many_more() { let mut chest = ItemStack::new("minecraft:chest", 1); @@ -335,6 +411,115 @@ mod tests { assert_eq!(format_amount(0.375), "0.38"); } + #[test] + fn equipment_attributes_use_player_totals_and_slot_group_colors() { + let modifiers = [ + ("attack_damage", "base_attack_damage", 6.0, "mainhand"), + ("attack_speed", "base_attack_speed", -2.4, "mainhand"), + ("armor", "armor", 3.0, "head"), + ] + .map(|(attribute, id, amount, slot)| { + ( + format!("minecraft:{attribute}"), + format!("minecraft:{id}"), + amount, + "add_value".to_owned(), + slot.to_owned(), + ) + }); + let root = std::env::temp_dir().join(format!( + "horizon-tooltip-language-{}-{}", + std::process::id(), + std::time::SystemTime::now() + .duration_since(std::time::UNIX_EPOCH) + .unwrap() + .as_nanos(), + )); + let dir = root.join("assets/minecraft/lang"); + std::fs::create_dir_all(&dir).unwrap(); + std::fs::write( + dir.join("en_us.json"), + serde_json::to_vec(&serde_json::json!({ + "item.modifiers.mainhand": "When in Main Hand:", + "item.modifiers.head": "When on Head:", + "attribute.modifier.equals.0": "%s %s", + "attribute.modifier.plus.0": "+%s %s", + "attribute.modifier.take.0": "-%s %s", + "attribute.name.attack_damage": "Attack Damage", + "attribute.name.attack_speed": "Attack Speed", + "attribute.name.armor": "Armor", + })) + .unwrap(), + ) + .unwrap(); + let enchantments = crate::enchant::Enchantments::load(&root); + std::fs::remove_dir_all(&root).unwrap(); + let lang = |key: &str| enchantments.translate(key).map(str::to_owned); + let green = rgba(0, 170, 0, 255); + let gray = rgba(170, 170, 170, 255); + let blue = rgba(85, 85, 255, 255); + let red = rgba(255, 85, 85, 255); + assert_eq!( + attribute_lines(&modifiers, Some([1.0, 4.0]), &lang), + [ + (String::new(), u32::MAX), + ("When in Main Hand:".into(), gray), + (" 7 Attack Damage".into(), green), + (" 1.6 Attack Speed".into(), green), + (String::new(), u32::MAX), + ("When on Head:".into(), gray), + ("+3 Armor".into(), blue), + ] + ); + let recipe = attribute_lines(&modifiers, None, &lang); + assert_eq!(recipe[2], ("+6 Attack Damage".into(), blue)); + assert_eq!(recipe[3], ("-2.4 Attack Speed".into(), red)); + let custom_base = attribute_lines(&modifiers, Some([2.0, 6.0]), &lang); + assert_eq!(custom_base[2].0, " 8 Attack Damage"); + assert_eq!(custom_base[3].0, " 3.6 Attack Speed"); + assert_eq!( + modifier_line(&lang, "attack_damage", 0.0, 0, true).as_deref(), + Some("0 Attack Damage") + ); + } + + #[test] + #[ignore = "requires prepared items/language in HORIZON_TEST_RESOURCES"] + fn equipment_attributes_with_prepared_items_and_language() { + let root = std::path::PathBuf::from( + std::env::var_os("HORIZON_TEST_RESOURCES").expect("HORIZON_TEST_RESOURCES"), + ); + let items = crate::items::Items::catalog(&root).unwrap(); + let enchantments = crate::enchant::Enchantments::load(&root); + let translate = |key: &str| enchantments.translate(key).map(str::to_owned); + for (item, expected) in [ + ( + "minecraft:diamond_sword", + [ + "", + "When in Main Hand:", + " 7 Attack Damage", + " 1.6 Attack Speed", + ], + ), + ( + "minecraft:diamond_helmet", + ["", "When on Head:", "+3 Armor", "+2 Armor Toughness"], + ), + ] { + let info = items.get(item).unwrap(); + let lines = attribute_lines(&info.modifiers, Some([1.0, 4.0]), &translate); + assert_eq!( + lines + .iter() + .map(|(text, _)| text.as_str()) + .collect::>(), + expected, + "{item}" + ); + } + } + #[test] fn modifier_lines_follow_the_sign_and_operation() { let lang = |k: &str| { @@ -349,13 +534,13 @@ mod tests { ) }; assert_eq!( - modifier_line(&lang, "movement_speed", 0.2, 2).as_deref(), + modifier_line(&lang, "movement_speed", 0.2, 2, false).as_deref(), Some("+20% Speed") ); assert_eq!( - modifier_line(&lang, "movement_speed", -0.6, 2).as_deref(), + modifier_line(&lang, "movement_speed", -0.6, 2, false).as_deref(), Some("-60% Speed") ); - assert_eq!(modifier_line(&lang, "movement_speed", 0.0, 2), None); + assert_eq!(modifier_line(&lang, "movement_speed", 0.0, 2, false), None); } } diff --git a/crates/horizon-client/src/items.rs b/crates/horizon-client/src/items.rs index 0aca99cef..54a505ab9 100644 --- a/crates/horizon-client/src/items.rs +++ b/crates/horizon-client/src/items.rs @@ -274,16 +274,20 @@ pub(crate) fn load_tags(root: &Path, kind: &str) -> Result Result>> { - let dir = root.join(format!("data/minecraft/tags/{kind}")); let mut raw: HashMap> = HashMap::new(); - fn walk(dir: &Path, base: &Path, raw: &mut HashMap>) -> Result<()> { + fn walk( + dir: &Path, + base: &Path, + namespace: &str, + raw: &mut HashMap>, + ) -> Result<()> { for entry in fs::read_dir(dir)?.flatten() { let p = entry.path(); if p.is_dir() { - walk(&p, base, raw)?; + walk(&p, base, namespace, raw)?; } else if p.extension().is_some_and(|e| e == "json") { let name = format!( - "minecraft:{}", + "{namespace}:{}", p.strip_prefix(base)? .with_extension("") .to_string_lossy() @@ -301,8 +305,12 @@ pub(crate) fn load_tag_lists(root: &Path, kind: &str) -> Result>, out: &mut Vec, depth: u32) { if depth > 32 { diff --git a/crates/horizon-client/src/keybinds.rs b/crates/horizon-client/src/keybinds.rs index 155ffcc24..5f8c0876b 100644 --- a/crates/horizon-client/src/keybinds.rs +++ b/crates/horizon-client/src/keybinds.rs @@ -373,11 +373,60 @@ impl KeyBinds { } Some(Err(button)) } + /// `AbstractContainerScreen.checkHotbarMouseClicked`: off hand, then hotbar. + pub fn container_mouse_key(&self, button: MouseButton, creative: bool) -> Option { + let name = mouse_name(button); + if matches!(button, MouseButton::Left | MouseButton::Right) + || (creative && self.get("key.pickItem") == name) + { + return None; + } + std::iter::once("key.swapOffhand") + .chain( + MAPPINGS + .iter() + .map(|m| m.0) + .filter(|n| n.starts_with("key.hotbar.")), + ) + .find(|mapping| self.get(mapping) == name) + .and_then(|mapping| key_code(Self::default_of(mapping))) + } } #[cfg(test)] mod tests { use super::*; + #[test] + fn container_mouse_hotkeys_follow_slot_bindings_and_pick_item_priority() { + for button in [MouseButton::Back, MouseButton::Forward, MouseButton::Middle] { + for (mapping, default, _) in MAPPINGS.iter().filter(|m| m.0.starts_with("key.hotbar.")) + { + let mut keys = KeyBinds::default(); + keys.set(mapping, &mouse_name(button)); + assert_eq!(keys.container_mouse_key(button, false), key_code(default)); + assert_eq!( + keys.container_mouse_key(button, true), + if button == MouseButton::Middle { + None + } else { + key_code(default) + } + ); + } + } + let mut keys = KeyBinds::default(); + keys.set("key.hotbar.1", "key.mouse.4"); + keys.set("key.swapOffhand", "key.mouse.4"); + assert_eq!( + keys.container_mouse_key(MouseButton::Back, false), + Some(KeyCode::KeyF) + ); + keys.set("key.hotbar.2", "key.mouse.left"); + assert_eq!(keys.container_mouse_key(MouseButton::Left, false), None); + assert_eq!(keys.container_mouse_key(MouseButton::Right, false), None); + assert_eq!(keys.container_mouse_key(MouseButton::Forward, false), None); + } + #[test] fn rebinding_translates_to_defaults() { let mut k = KeyBinds::default(); diff --git a/crates/horizon-client/src/lan/conn.rs b/crates/horizon-client/src/lan/conn.rs index d7c375dcd..77faa7e6f 100644 --- a/crates/horizon-client/src/lan/conn.rs +++ b/crates/horizon-client/src/lan/conn.rs @@ -22,9 +22,10 @@ use horizon_protocol::{ text::translatable, }; use std::{ + cell::Cell, net::TcpStream, sync::{ - Arc, + Arc, Mutex, PoisonError, atomic::{AtomicI32, AtomicI64, AtomicU32, AtomicUsize, Ordering}, mpsc::{Receiver, RecvTimeoutError, Sender, channel}, }, @@ -123,16 +124,64 @@ pub fn valid_name(name: &str) -> bool { (1..=16).contains(&name.len()) && name.chars().all(|c| c as u32 > 32 && (c as u32) < 127) } +thread_local! { + static HOLDING: Cell = const { Cell::new(false) }; +} + +/// While one is alive, the packets this thread sends to each player are held +/// and handed to the player's writer thread together by +/// [`PlayerSender::flush`]: one wake-up per player per tick rather than one +/// per packet. The bytes and their order don't change. +pub struct Holding(bool); +impl Holding { + pub fn start() -> Self { + Self(HOLDING.replace(true)) + } +} +impl Drop for Holding { + fn drop(&mut self) { + HOLDING.set(self.0); + } +} + /// The sending half given to the game thread. #[derive(Clone)] pub struct PlayerSender { pub tx: Sender, pub pending: Arc, + held: Arc>>>, } impl PlayerSender { + pub fn new(tx: Sender, pending: Arc) -> Self { + Self { + tx, + pending, + held: Arc::default(), + } + } + fn held(&self) -> std::sync::MutexGuard<'_, Vec>> { + self.held.lock().unwrap_or_else(PoisonError::into_inner) + } pub fn send(&self, body: Vec) { self.pending.fetch_add(body.len(), Ordering::Relaxed); - let _ = self.tx.send(Out::Packet(body)); + if HOLDING.get() { + self.held().push(body); + } else { + self.flush(); + let _ = self.tx.send(Out::Packet(body)); + } + } + /// Hand the held packets to the writer thread. + pub fn flush(&self) { + let held = std::mem::take(&mut *self.held()); + if !held.is_empty() { + let _ = self.tx.send(Out::Packets(held)); + } + } + /// Send a play disconnect with this reason after what was sent before. + pub fn disconnect(&self, reason: horizon_world::nbt::Tag) { + self.flush(); + let _ = self.tx.send(Out::Disconnect(reason)); } } @@ -528,8 +577,9 @@ fn login_and_configure( properties: Vec, ) -> Result, String> { let e = |e: horizon_protocol::Error| e.to_string(); + // ServerLoginPacketListenerImpl: memory connections are not compressed. let threshold = shared.config.compression_threshold; - if threshold >= 0 { + if threshold >= 0 && !c.is_memory() { c.send(&login::Clientbound::Compression(login::Compression { threshold }).encode()) .map_err(e)?; c.set_compression(threshold); @@ -633,10 +683,7 @@ fn play(c: Connection, shared: &Arc, parts: Parts, address: std::net::So let pending = Arc::new(AtomicUsize::new(0)); let latency = Arc::new(AtomicI32::new(0)); let alive = Arc::new(AtomicI64::new(-1)); - let sender = PlayerSender { - tx: tx.clone(), - pending: pending.clone(), - }; + let sender = PlayerSender::new(tx.clone(), pending.clone()); let writer_thread = { let (alive, latency, pending) = (alive.clone(), latency.clone(), pending.clone()); std::thread::Builder::new() @@ -712,13 +759,18 @@ fn write_loop( let mut sent_at: Option<(i64, Instant)> = None; let mut next = Instant::now() + KEEP_ALIVE_EVERY; loop { + let mut write = |body: Vec| { + pending.fetch_sub(body.len(), Ordering::Relaxed); + writer.send(&body).is_ok() && pending.load(Ordering::Relaxed) <= MAX_PENDING + }; match rx.recv_timeout(Duration::from_millis(200)) { Ok(Out::Packet(body)) => { - pending.fetch_sub(body.len(), Ordering::Relaxed); - if writer.send(&body).is_err() { + if !write(body) { break; } - if pending.load(Ordering::Relaxed) > MAX_PENDING { + } + Ok(Out::Packets(bodies)) => { + if !bodies.into_iter().all(&mut write) { break; } } @@ -762,3 +814,42 @@ fn write_loop( } writer.shutdown(); } + +#[cfg(test)] +mod tests { + use super::*; + + fn bodies(rx: &Receiver) -> Vec>> { + rx.try_iter() + .map(|out| match out { + Out::Packet(body) => vec![body], + Out::Packets(bodies) => bodies, + Out::Disconnect(_) => vec![b"disconnect".to_vec()], + Out::Close => vec![b"close".to_vec()], + }) + .collect() + } + + #[test] + fn held_packets_go_together_and_in_order() { + let (tx, rx) = channel(); + let sender = PlayerSender::new(tx, Arc::default()); + sender.send(vec![1]); + assert_eq!(bodies(&rx), [vec![vec![1]]]); + let holding = Holding::start(); + sender.send(vec![2]); + sender.send(vec![3]); + assert!(bodies(&rx).is_empty()); + drop(holding); + // A packet sent after the tick still follows the held ones. + sender.send(vec![4]); + assert_eq!(bodies(&rx), [vec![vec![2], vec![3]], vec![vec![4]]]); + let holding = Holding::start(); + sender.send(vec![5]); + sender.disconnect(horizon_world::nbt::Tag::Byte(0)); + drop(holding); + sender.flush(); + assert_eq!(bodies(&rx), [vec![vec![5]], vec![b"disconnect".to_vec()]]); + assert_eq!(sender.pending.load(Ordering::Relaxed), 5); + } +} diff --git a/crates/horizon-client/src/lan/containers.rs b/crates/horizon-client/src/lan/containers.rs index e9bfe7fbe..0fa47510a 100644 --- a/crates/horizon-client/src/lan/containers.rs +++ b/crates/horizon-client/src/lan/containers.rs @@ -4,7 +4,7 @@ use super::{ hub::Hub, menu::{self, Menu, Rules, Slots, View}, }; -use crate::{items::ItemStack, world::World}; +use crate::{containers::BlockEntity, items::ItemStack, world::World}; use horizon_protocol::{ item::Hashed, packets::play::{self, Clientbound as C}, @@ -175,6 +175,7 @@ impl Hub { state: 0, sent: Vec::new(), drag: None, + sent_data: Vec::new(), }); self.send_menu_content(i, items); } @@ -416,6 +417,47 @@ impl Hub { } } + /// AbstractContainerMenu.broadcastChanges' data slots: a furnace's + /// AbstractFurnaceBlockEntity.dataAccess (lit time left, lit total, cooking + /// timer, cooking total), each sent when it changes. + fn sync_container_data(&mut self, i: usize, world: &World) { + let g = &mut self.guests[i]; + let Some(m) = g.menu.as_mut() else { + return; + }; + let Slots::Block(pos) = m.slots else { + return; + }; + let Some(BlockEntity::Furnace(f)) = world.block_entities.get(&pos) else { + return; + }; + let data = [ + f.lit_remaining, + f.lit_total, + f.cooking_timer, + f.cooking_total, + ] + .map(|v| v as i16); + m.sent_data.resize(data.len(), i16::MIN); + let window = m.window; + let changed: Vec<(usize, i16)> = data + .iter() + .enumerate() + .filter(|&(k, v)| m.sent_data[k] != *v) + .map(|(k, &v)| (k, v)) + .collect(); + for &(k, value) in &changed { + m.sent_data[k] = value; + } + for (k, value) in changed { + g.send(&C::ContainerSetData(play::ContainerSetData { + container: window, + id: k as i16, + value, + })); + } + } + /// Slot changes of open containers (AbstractContainerMenu.broadcastChanges). pub fn sync_containers(&mut self, world: &World) { for i in 0..self.guests.len() { @@ -457,6 +499,8 @@ impl Hub { })); } } + // Then the data slots, as broadcastChanges does. + self.sync_container_data(i, world); } } } diff --git a/crates/horizon-client/src/lan/data.rs b/crates/horizon-client/src/lan/data.rs index 85c4894a9..8de0e53d3 100644 --- a/crates/horizon-client/src/lan/data.rs +++ b/crates/horizon-client/src/lan/data.rs @@ -109,24 +109,14 @@ pub fn json_to_nbt(v: &Value) -> Tag { } } -/// A component's JSON as NbtOps writes it: a list whose elements differ in -/// type (translation arguments mixing numbers and components) holds each -/// element that isn't a compound wrapped in one under the empty key. +/// A component's JSON as NbtOps writes it. A list whose elements differ in type +/// (translation arguments mixing numbers and components) stays one list: the NBT +/// writer wraps each element that isn't a compound under the empty key, as +/// `ListTag.wrapIfNeeded` does, so wrapping it here too would leave the client a +/// `{"": 1}` argument it cannot read. pub fn component_nbt(v: &Value) -> Tag { match v { - Value::Array(a) => { - let kind = |v: &Value| std::mem::discriminant(&json_to_nbt(v)); - let mixed = a.windows(2).any(|w| kind(&w[0]) != kind(&w[1])); - Tag::List( - a.iter() - .map(|e| match component_nbt(e) { - t @ Tag::Compound(_) => t, - t if mixed => Tag::compound([("", t)]), - t => t, - }) - .collect(), - ) - } + Value::Array(a) => Tag::List(a.iter().map(component_nbt).collect()), Value::Object(o) => Tag::compound(o.iter().map(|(k, v)| (k.as_str(), component_nbt(v)))), other => json_to_nbt(other), } @@ -313,3 +303,27 @@ mod tests { assert!(matches!(t.get("b"), Some(Tag::List(l)) if l.len() == 2)); } } + +#[cfg(test)] +mod component_tests { + use super::*; + use horizon_world::nbt::{Limits, read_network, write_network}; + + /// `/give`'s reply: a count, then components. On the wire each element is wrapped + /// once, and read back (ListTag.addAndUnwrap) it is the number again. + #[test] + fn mixed_translation_arguments_are_wrapped_once() { + let json = serde_json::json!({ + "translate": "commands.give.success.single", + "with": [1, {"text": "Raw Iron"}, {"text": "Tester"}], + }); + let tag = component_nbt(&json); + let Some(Tag::List(args)) = tag.get("with") else { + panic!("no arguments: {tag:?}"); + }; + assert_eq!(args[0], Tag::Int(1)); + // Read back as ListTag.addAndUnwrap does: a wrapper wrapped twice stays a compound. + let bytes = write_network(&tag, Limits::default()).unwrap(); + assert_eq!(read_network(&bytes, Limits::default()).unwrap().0, tag); + } +} diff --git a/crates/horizon-client/src/lan/guest.rs b/crates/horizon-client/src/lan/guest.rs index c6215df70..e07a20929 100644 --- a/crates/horizon-client/src/lan/guest.rs +++ b/crates/horizon-client/src/lan/guest.rs @@ -18,6 +18,16 @@ pub const CREATIVE: i32 = 1; pub const ADVENTURE: i32 = 2; pub const SPECTATOR: i32 = 3; +/// An effect as last sent in ClientboundUpdateMobEffectPacket. +#[derive(Clone, Debug, PartialEq)] +pub struct SentEffect { + pub id: &'static str, + pub amplifier: i32, + pub flags: i8, + pub duration: i32, + pub tick_count: i32, +} + pub struct Guest { pub conn: u32, /// The client's address (IP bans, `list`). @@ -94,11 +104,21 @@ pub struct Guest { pub xp_level: i32, pub xp_progress: f32, pub xp_total: i32, + /// Active status effects (`LivingEntity.activeEffects`). + pub effects: crate::effects::PlayerEffects, + /// What the client was told of each effect: id, amplifier, flags, and + /// the duration and `effects.tick_count` at the time, so a duration that + /// only counted down is not sent again (`ServerPlayer.onEffectUpdated`). + pub sent_effects: Vec, /// What the client believes the inventory menu holds (for change detection). pub menu_sent: Vec>, pub menu_state: i32, pub sent_health: Option<(f32, i32, f32)>, pub sent_xp: Option<(f32, i32, i32)>, + /// Where the player last died (Player.lastDeathLocation), sent with the respawn. + pub last_death: Option<[i32; 3]>, + /// The last action sequence to acknowledge at the next tick (-1: none). + pub ack_up_to: std::cell::Cell, pub sent_selected: usize, pub inbox: VecDeque, pub joined_tick: u64, @@ -177,10 +197,14 @@ impl Guest { xp_level: 0, xp_progress: 0.0, xp_total: 0, + effects: Default::default(), + sent_effects: Vec::new(), menu_sent: vec![None; 46], menu_state: 0, sent_health: None, sent_xp: None, + last_death: None, + ack_up_to: std::cell::Cell::new(-1), sent_selected: 0, inbox: VecDeque::new(), joined_tick: 0, diff --git a/crates/horizon-client/src/lan/hub.rs b/crates/horizon-client/src/lan/hub.rs index e43d47541..9aa3c6a2e 100644 --- a/crates/horizon-client/src/lan/hub.rs +++ b/crates/horizon-client/src/lan/hub.rs @@ -9,9 +9,13 @@ use super::{ chunks::ChunkEncoder, data::SyncData, entity::EntityState, - guest::{Guest, stack_to_network}, + guest::{Guest, SentEffect, stack_to_network}, +}; +use crate::{ + effects::{EffectInstance, PlayerEffects}, + items::ItemStack, + world::World, }; -use crate::{items::ItemStack, world::World}; use horizon_protocol::ids::play::clientbound as ids; use horizon_protocol::{ entity_data::{Value, serializer}, @@ -44,6 +48,10 @@ pub struct HubConfig { pub max_players: usize, /// A dedicated server: operators from `ops`, the dedicated-only commands. pub dedicated: bool, + /// An integrated server (`dedicated` too, since it has no host player): + /// the owner's level from `ops`, the others' from Allow Commands and + /// the guests' command access, and the integrated-only commands. + pub integrated: bool, } /// The host player as the other players see them. @@ -381,7 +389,7 @@ impl Hub { /// Disconnect a guest with a reason (ServerGamePacketListenerImpl.disconnect). pub fn kick(&mut self, conn: u32, reason: Tag) { if let Some(g) = self.guest(conn) { - let _ = g.sender.tx.send(super::Out::Disconnect(reason)); + g.sender.disconnect(reason); } } @@ -446,7 +454,11 @@ impl Hub { /// (IntegratedServer.setGuestCommandAccess); a dedicated server its /// operators' level (`ops`). fn default_permission(&self, uuid: u128) -> u8 { - if self.config.dedicated { + if self.config.integrated { + // IntegratedServer.getProfilePermissions / getCustomPermissionLevel. + let guests = if self.config.guest_commands { 2 } else { 0 }; + self.ops.get(&uuid).copied().unwrap_or(guests) + } else if self.config.dedicated { self.ops.get(&uuid).copied().unwrap_or(0) } else if self.config.guest_commands { 2 @@ -558,11 +570,106 @@ impl Hub { })); g.center_sent = Some((cx, cz)); g.placed_at = self.ticks; + self.send_active_effects(i); // ServerPlayer.sendPostEffects and initInventoryMenu. self.guests[i].send(&raw(ids::POST_EFFECTS, vec![0])); self.send_inventory(i); } + /// ClientboundUpdateMobEffectPacket for one of the guest's effects. + fn send_effect(&mut self, i: usize, e: &EffectInstance, blend: bool) -> Option { + let effect = self + .sync + .id("minecraft:mob_effect", &format!("minecraft:{}", e.id))?; + let flags = PlayerEffects::flags(e); + let g = &self.guests[i]; + g.send(&C::UpdateMobEffect(play::UpdateMobEffect { + id: g.entity_id, + effect, + amplifier: e.amplifier, + duration: e.duration, + flags: flags | if blend { 8 } else { 0 }, + })); + Some(SentEffect { + id: e.id, + amplifier: e.amplifier, + flags, + duration: e.duration, + tick_count: g.effects.tick_count, + }) + } + + /// PlayerList.sendActivePlayerEffects: every effect, unblended, when the + /// player is placed (or respawns). + pub fn send_active_effects(&mut self, i: usize) { + let effects = self.guests[i].effects.list.clone(); + let sent = effects + .iter() + .filter_map(|e| self.send_effect(i, e, false)) + .collect(); + self.guests[i].sent_effects = sent; + } + + /// ServerPlayer.onEffectAdded (blended), onEffectUpdated and + /// onEffectsRemoved, from what changed since the client was told: an + /// effect that only counted down is not sent again, except at every + /// 600 ticks left (`LivingEntity.tickEffects`). + fn sync_effects(&mut self, i: usize) { + let g = &self.guests[i]; + if g.effects.list.is_empty() && g.sent_effects.is_empty() { + return; + } + let now = g.effects.tick_count; + let mut sent = Vec::with_capacity(g.effects.list.len()); + let mut changes = Vec::new(); + for e in &g.effects.list { + match g.sent_effects.iter().find(|s| s.id == e.id) { + None => changes.push((e.clone(), true)), + Some(s) => { + let ticked = now.wrapping_sub(s.tick_count); + let expected = if s.duration == -1 { + -1 + } else { + s.duration - ticked + }; + let refresh = ticked > 0 && e.duration > 0 && e.duration % 600 == 0; + if s.amplifier != e.amplifier + || s.flags != PlayerEffects::flags(e) + || e.duration != expected + || refresh + { + changes.push((e.clone(), false)); + } else { + sent.push(s.clone()); + } + } + } + } + let removed: Vec<&'static str> = g + .sent_effects + .iter() + .filter(|s| g.effects.get(s.id).is_none()) + .map(|s| s.id) + .collect(); + for id in removed { + let Some(effect) = self + .sync + .id("minecraft:mob_effect", &format!("minecraft:{id}")) + else { + continue; + }; + let g = &self.guests[i]; + g.send(&C::RemoveMobEffect(play::RemoveMobEffect { + id: g.entity_id, + effect, + })); + } + for (e, added) in changes { + sent.extend(self.send_effect(i, &e, added)); + } + self.guests[i].sent_effects = sent; + } + /// PlayerList.sendPlayerPermissionLevel: the op level as an entity event /// (24 + level), then the commands that level may use. fn send_permission_level(&self, i: usize) { @@ -588,7 +695,10 @@ impl Hub { fn send_commands(&self, i: usize) { let g = &self.guests[i]; let packet = match &self.sync.commands { - Some(tree) => tree.packet(g.permission, self.config.dedicated, &self.sync.builtin), + Some(tree) => { + let dedicated = self.config.dedicated && !self.config.integrated; + tree.packet(g.permission, dedicated, &self.sync.builtin) + } None => play::Commands { nodes: vec![play::CommandNode { kind: 0, @@ -626,6 +736,22 @@ impl Hub { })); } + /// ClientboundSetDefaultSpawnPositionPacket for the level's respawn data. + fn default_spawn(&self, spawn: [f64; 3]) -> C { + C::SetDefaultSpawnPosition(play::SetDefaultSpawnPosition { + pos: play::GlobalPos { + dimension: self.config.dimension.clone(), + pos: [ + spawn[0].floor() as i32, + spawn[1].floor() as i32, + spawn[2].floor() as i32, + ], + }, + yaw: 0.0, + pitch: 0.0, + }) + } + /// PlayerList.sendLevelInfo. fn send_level_info(&mut self, i: usize, ctx: &TickCtx) { let spawn = ctx.spawn; @@ -642,18 +768,7 @@ impl Hub { warning_time: 300, })); g.send(&self.time_packet(ctx.world, ctx.clock_rate)); - g.send(&C::SetDefaultSpawnPosition(play::SetDefaultSpawnPosition { - pos: play::GlobalPos { - dimension: self.config.dimension.clone(), - pos: [ - spawn[0].floor() as i32, - spawn[1].floor() as i32, - spawn[2].floor() as i32, - ], - }, - yaw: 0.0, - pitch: 0.0, - })); + g.send(&self.default_spawn(spawn)); let w = &ctx.world.weather; if w.raining { g.send(&C::GameEvent(play::GameEvent { @@ -789,11 +904,21 @@ impl Hub { } } self.broadcast_block_changes(ctx.world); + // ServerGamePacketListenerImpl.tick: the actions handled since the last tick are + // acknowledged after the block changes they made were sent, so a client's predicted + // blocks settle on the server's states. + for g in &self.guests { + let sequence = g.ack_up_to.replace(-1); + if sequence >= 0 { + g.send(&C::BlockChangedAck(play::BlockChangedAck { sequence })); + } + } self.sync_containers(ctx.world); self.track_entities(ctx); for i in 0..self.guests.len() { if self.guests[i].placed { self.sync_player_state(i); + self.sync_effects(i); } } let latencies: Vec<_> = self @@ -1302,7 +1427,8 @@ impl Hub { let g = &self.guests[i]; (g.health, g.food, g.saturation) }; - if self.guests[i].sent_health != Some(health) { + // A dead player's health isn't sent (the death screen shows from the combat kill). + if self.guests[i].sent_health != Some(health) && !self.guests[i].dead { let g = &mut self.guests[i]; g.send(&C::SetHealth(play::SetHealth { health: health.0, @@ -1483,11 +1609,13 @@ impl Hub { } /// Acknowledge a block interaction (ClientboundBlockChangedAckPacket). + /// ServerGamePacketListenerImpl.ackBlockChangesUpTo: sent with the next tick's block + /// changes (`tick`). pub fn ack(&self, conn: u32, sequence: i32) { if sequence >= 0 && let Some(g) = self.guest(conn) { - g.send(&C::BlockChangedAck(play::BlockChangedAck { sequence })); + g.ack_up_to.set(g.ack_up_to.get().max(sequence)); } } @@ -1532,7 +1660,7 @@ impl Hub { pub fn disconnect_all(&mut self) { let reason = translatable("multiplayer.disconnect.server_shutdown", &[]); for g in &self.guests { - let _ = g.sender.tx.send(super::Out::Disconnect(reason.clone())); + g.sender.disconnect(reason.clone()); } } } @@ -1552,23 +1680,81 @@ impl Hub { .map(str::to_owned) } - /// ServerPlayer.respawn: back to `at` with a fresh start. - pub fn respawn(&mut self, i: usize, at: [f64; 3]) { + /// PlayerList.respawn: back to `at` with a fresh start. The vanilla client waits on its + /// level loading screen from the Respawn packet until the level info's "level chunks load + /// start" event and the chunk under the player, so the packets follow vanilla's order. + pub fn respawn(&mut self, i: usize, at: [f64; 3], ctx: &TickCtx) { let mode = self.guests[i].game_mode; - let info = self.spawn_info(mode); + let mut info = self.spawn_info(mode); + // Player.getLastDeathLocation, set where the player died. + info.last_death = self.guests[i].last_death.map(|pos| play::GlobalPos { + dimension: self.config.dimension.clone(), + pos, + }); self.close_container_for(i); let g = &mut self.guests[i]; + // ServerLevel.removePlayerImmediately: every entity paired with the dead player is + // removed (ServerEntity.removePairing, one packet each). Its chunks are dropped + // without ForgetLevelChunk (PlayerChunkSender.dropChunk skips dead players): the + // client keeps them and they are all sent again. + for id in g.tracker.ids() { + g.send(&C::RemoveEntities(play::RemoveEntities { ids: vec![id] })); + } + g.tracker.clear(); + g.sent_chunks.clear(); + g.chunk_queue.clear(); + // ServerPlayer.restoreFrom: the game mode again (onUpdateAbilities). + g.send(&C::PlayerAbilities(play::PlayerAbilities { + flags: g.ability_flags(), + flying_speed: 0.05, + walking_speed: 0.1, + })); + // The new player faces the level's respawn direction (snapTo). + g.yaw = 0.0; + g.pitch = 0.0; g.position = at; g.previous_position = at; g.send(&C::Respawn(play::Respawn { spawn: info, keep: 0, })); + // The respawned player is a new one, without effects; so is the + // client's (ClientPacketListener.handleRespawn). + g.effects = Default::default(); + g.sent_effects.clear(); g.center = None; g.sent_chunks.clear(); g.chunk_queue.clear(); g.tracker.clear(); self.teleport(i, None); + let spawn = self.default_spawn(ctx.spawn); + let g = &mut self.guests[i]; + g.send(&spawn); + g.send(&C::ChangeDifficulty(play::ChangeDifficulty { + difficulty: ctx.difficulty, + locked: ctx.difficulty_locked, + })); + let xp = (g.xp_progress, g.xp_level, g.xp_total); + g.send(&C::SetExperience(play::SetExperience { + progress: xp.0, + level: xp.1, + total: xp.2, + })); + g.sent_xp = Some(xp); + // ServerPlayer.sendPostEffects. + g.send(&raw(ids::POST_EFFECTS, vec![0])); + self.send_level_info(i, ctx); + self.send_permission_level(i); + // ServerLevel.addRespawnedPlayer: the chunk tracking starts again at the player. + let g = &mut self.guests[i]; + let (cx, cz) = g.chunk(); + g.send(&C::SetChunkCacheCenter(play::SetChunkCacheCenter { + x: cx, + z: cz, + })); + g.center = None; + g.center_sent = Some((cx, cz)); + // ServerPlayer.initInventoryMenu. self.send_inventory(i); } } diff --git a/crates/horizon-client/src/lan/menu.rs b/crates/horizon-client/src/lan/menu.rs index a2e40bf3d..d37874bc8 100644 --- a/crates/horizon-client/src/lan/menu.rs +++ b/crates/horizon-client/src/lan/menu.rs @@ -28,6 +28,8 @@ pub struct Menu { pub sent: Vec>, /// An unfinished drag: (mode, slots). pub drag: Option<(u8, Vec)>, + /// The data slots the client was last sent (ContainerSetData), a furnace's progress. + pub sent_data: Vec, } /// Where the slots of a container menu live. diff --git a/crates/horizon-client/src/lan/mod.rs b/crates/horizon-client/src/lan/mod.rs index fb9bd5f4c..58badc010 100644 --- a/crates/horizon-client/src/lan/mod.rs +++ b/crates/horizon-client/src/lan/mod.rs @@ -21,7 +21,7 @@ pub mod playerdata; mod recipes; pub mod tracking; -pub use conn::PlayerSender; +pub use conn::{Holding, PlayerSender}; use horizon_protocol::packets::play; use horizon_protocol::{ GAME_VERSION, PROTOCOL_VERSION, crypto::uuid_string, packets::configuration::ClientInformation, @@ -71,6 +71,8 @@ pub struct Joined { pub enum Out { /// A packet body: VarInt id and payload. Packet(Vec), + /// Packet bodies held during a tick, in order. + Packets(Vec>), /// Send a play disconnect with this component, then close. Disconnect(horizon_world::nbt::Tag), Close, @@ -338,6 +340,8 @@ pub struct Lan { events: Receiver, pub port: u16, listener: Option>, + /// Stops the accept loop alone: local connections stay. + listener_stop: Arc, /// The multicast announcement (LanServerPinger); absent when the socket failed. _pinger: Option, } @@ -353,26 +357,8 @@ impl Lan { } /// ServerConnectionListener.startTcpServerListener with a configuration. pub fn start_with(sync: Arc, config: NetConfig) -> io::Result { - let listener = if config.listen { - let bind = config - .bind - .unwrap_or(std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)); - let listener = TcpListener::bind((bind, config.port))?; - listener.set_nonblocking(true)?; - Some(listener) - } else { - None - }; - let port = match &listener { - Some(l) => l.local_addr()?.port(), - None => 0, - }; - let _pinger = if config.announce && listener.is_some() { - discovery::Pinger::start(config.motd.clone(), port).ok() - } else { - None - }; let (tx, events) = channel(); + let (listen, port, announce) = (config.listen, config.port, config.announce); let shared = Arc::new(Shared { sync, events: tx, @@ -385,21 +371,77 @@ impl Lan { random: Mutex::new(crate::server::java_properties::now_millis() as u64 | 1), config, }); - let accept_shared = shared.clone(); - let listener = listener - .map(|listener| { - std::thread::Builder::new() - .name("horizon-lan-accept".into()) - .spawn(move || accept_loop(listener, accept_shared)) - }) - .transpose()?; - Ok(Self { + let mut lan = Self { shared, events, - port, - listener, - _pinger, - }) + port: 0, + listener: None, + listener_stop: Arc::new(AtomicBool::new(false)), + _pinger: None, + }; + if listen { + lan.listen(port, announce)?; + } + Ok(lan) + } + /// Start accepting connections on `port` (0 picks a free one), and + /// announce the world on the local network when `announce` + /// (`IntegratedServer.publishServer`'s LanServerPinger). Returns the port. + pub fn listen(&mut self, port: u16, announce: bool) -> io::Result { + if self.listener.is_some() { + return Err(io::Error::new( + io::ErrorKind::AlreadyExists, + "already listening", + )); + } + let listener = Self::bind(self.shared.config.bind, port)?; + self.listen_on(listener, announce) + } + /// The socket [`Lan::listen`] accepts on, bound ahead so that a failure + /// can be reported before the hub is at hand. + pub fn bind(address: Option, port: u16) -> io::Result { + let bind = address.unwrap_or(std::net::IpAddr::V4(std::net::Ipv4Addr::UNSPECIFIED)); + let listener = TcpListener::bind((bind, port))?; + listener.set_nonblocking(true)?; + Ok(listener) + } + /// [`Lan::listen`] on a socket already bound. + pub fn listen_on(&mut self, listener: TcpListener, announce: bool) -> io::Result { + if self.listener.is_some() { + return Err(io::Error::new( + io::ErrorKind::AlreadyExists, + "already listening", + )); + } + let port = listener.local_addr()?.port(); + let stop = Arc::new(AtomicBool::new(false)); + let (accept_shared, accept_stop) = (self.shared.clone(), stop.clone()); + let thread = std::thread::Builder::new() + .name("horizon-lan-accept".into()) + .spawn(move || accept_loop(listener, accept_shared, &accept_stop))?; + self._pinger = if announce { + discovery::Pinger::start(self.shared.motd(), port).ok() + } else { + None + }; + self.listener = Some(thread); + self.listener_stop = stop; + self.port = port; + Ok(port) + } + /// Stop accepting connections and announcing; connections already in + /// (the local one included) stay. + pub fn stop_listening(&mut self) { + self.listener_stop.store(true, Ordering::Relaxed); + if let Some(t) = self.listener.take() { + let _ = t.join(); + } + self._pinger = None; + self.port = 0; + } + /// Whether connections from the network are accepted. + pub fn listening(&self) -> bool { + self.listener.is_some() } /// `Connection.connectToLocalServer`: a connection through memory, its /// server end served on its own thread like an accepted socket. @@ -430,10 +472,7 @@ impl Lan { } pub fn stop(&mut self) { self.shared.stop.store(true, Ordering::Relaxed); - if let Some(t) = self.listener.take() { - let _ = t.join(); - } - self._pinger = None; + self.stop_listening(); } } impl Drop for Lan { @@ -442,8 +481,8 @@ impl Drop for Lan { } } -fn accept_loop(listener: TcpListener, shared: Arc) { - while !shared.stop.load(Ordering::Relaxed) { +fn accept_loop(listener: TcpListener, shared: Arc, stop: &AtomicBool) { + while !shared.stop.load(Ordering::Relaxed) && !stop.load(Ordering::Relaxed) { match listener.accept() { Ok((stream, addr)) => { let shared = shared.clone(); diff --git a/crates/horizon-client/src/lan/playerdata.rs b/crates/horizon-client/src/lan/playerdata.rs index 3554463ea..d824683d2 100644 --- a/crates/horizon-client/src/lan/playerdata.rs +++ b/crates/horizon-client/src/lan/playerdata.rs @@ -66,6 +66,7 @@ pub fn apply(guest: &mut Guest, level: &Level) { guest.xp_level = level.xp_level; guest.xp_progress = level.xp_progress; guest.xp_total = level.xp_total; + guest.effects = crate::effects::PlayerEffects::from_saved(&level.active_effects); guest.game_mode = level.mode(); guest.selected = (level.selected_slot.max(0) as usize).min(8); guest.saved = level.player_extra.clone(); @@ -119,6 +120,7 @@ pub fn store(guest: &Guest, level: &mut Level) { level.xp_level = guest.xp_level; level.xp_progress = guest.xp_progress; level.xp_total = guest.xp_total; + level.active_effects = guest.effects.saved(); level.game_mode = guest.game_mode; level.creative = guest.game_mode == 1; level.selected_slot = guest.selected as i32; diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index 79a8ddd86..45e09a6a3 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -132,6 +132,7 @@ pub mod particles; pub mod patrol; pub mod perf_overlay; pub mod pick_item; +pub mod poi_sections; pub mod pose; pub mod post; pub mod projectiles; diff --git a/crates/horizon-client/src/light.rs b/crates/horizon-client/src/light.rs index 936bc9655..fd271f40f 100644 --- a/crates/horizon-client/src/light.rs +++ b/crates/horizon-client/src/light.rs @@ -71,7 +71,7 @@ impl LightTables { }; let data = nbt::read_named( &std::fs::read(root.join("occlusion.nbt"))?, - Limits::default(), + Limits::tables(), )? .0 .tag; diff --git a/crates/horizon-client/src/mobs/render.rs b/crates/horizon-client/src/mobs/render.rs index 4c312e5fd..e8120e92e 100644 --- a/crates/horizon-client/src/mobs/render.rs +++ b/crates/horizon-client/src/mobs/render.rs @@ -847,6 +847,10 @@ fn red_overlay(m: &Mob) -> bool { m.hurt_time > 0 || (m.death_time > 0 && !super::creaking::tearing_down(m)) } fn species_scale(m: &Mob, alpha: f32) -> glam::Mat4 { + if m.kind.id == "minecraft:player" { + // AvatarRenderer.scale, shared by the inventory and world renderer. + return glam::Mat4::from_scale(glam::Vec3::splat(0.9375)); + } match m.species() { Species::Wither => { glam::Mat4::from_scale(glam::Vec3::splat(super::wither::scale(m, alpha))) diff --git a/crates/horizon-client/src/mobs/villager_brain.rs b/crates/horizon-client/src/mobs/villager_brain.rs index 942afd233..5b8500c0d 100644 --- a/crates/horizon-client/src/mobs/villager_brain.rs +++ b/crates/horizon-client/src/mobs/villager_brain.rs @@ -924,8 +924,35 @@ fn tickets(m: &Mob, w: &MobWorld, p: [i32; 3]) -> usize { } n } +/// The POIs of chunk `(qx, qz)` from `y0` to `y1` (within the world), in +/// scan order: y, then z, then x. Only the indexed POI blocks of the sections +/// are visited (`PoiManager.getInChunk`). +fn chunk_pois( + w: &MobWorld, + (qx, qz): (i32, i32), + y0: i32, + y1: i32, + mut visit: impl FnMut(Poi, [i32; 3]), +) { + let Some(chunk) = w.world.chunks.get(&(qx, qz)) else { + return; + }; + let kind = |state: u32| poi_of_state(w, horizon_world::registry::BlockStateId(state)); + let min = crate::chunk::MIN_Y; + for s in (y0 - min) >> 4..=(y1 - min) >> 4 { + let section = &chunk.sections[s as usize]; + for &(i, poi) in w.world.pois.get((qx, s, qz), section, kind).iter() { + let i = i32::from(i); + let y = min + s * 16 + (i >> 8); + if (y0..=y1).contains(&y) { + visit(poi, [qx * 16 + (i & 15), y, qz * 16 + (i >> 4 & 15)]); + } + } + } +} /// `PoiManager.getInRange` with `HAS_SPACE`, nearest first (`distSqr` to the -/// block, ties in scan order), filtered by `accept`. +/// block, ties in scan order: chunk by chunk, z then x, and y, z, x within), +/// filtered by `accept`. fn pois_in_range( m: &Mob, w: &MobWorld, @@ -940,62 +967,24 @@ fn pois_in_range( let chunks = radius.div_euclid(16) + 1; let y0 = (c[1] - radius).max(crate::chunk::MIN_Y); let y1 = (c[1] + radius).min(crate::chunk::MIN_Y + crate::chunk::HEIGHT - 1); - // Sections whose palette holds no POI state are skipped whole, as - // PoiManager only visits sections with records: the same positions in - // the same order, without reading and naming every block of the sphere. - let kind = |state: u32| poi_of_state(w, horizon_world::registry::BlockStateId(state)); for qz in cz - chunks..=cz + chunks { for qx in cx - chunks..=cx + chunks { - let Some(chunk) = w.world.chunks.get(&(qx, qz)) else { - continue; - }; - let holds: Vec = chunk - .sections - .iter() - .map(|s| s.blocks.maybe_has(|v| kind(v).is_some())) - .collect(); - for y in y0..=y1 { - if !holds[((y - crate::chunk::MIN_Y) >> 4) as usize] { - continue; + chunk_pois(w, (qx, qz), y0, y1, |poi, p| { + if (p[0] - c[0]).abs() > radius + || (p[2] - c[2]).abs() > radius + || dist_sqr(p, c) > r2 + || !poi_matches(m, f, poi) + || tickets(m, w, p) >= poi.max_tickets() + { + return; } - for z in qz * 16..qz * 16 + 16 { - for x in qx * 16..qx * 16 + 16 { - if (x - c[0]).abs() > radius || (z - c[2]).abs() > radius { - continue; - } - let d = i64::from(x - c[0]).pow(2) - + i64::from(y - c[1]).pow(2) - + i64::from(z - c[2]).pow(2); - if d > r2 { - continue; - } - let state = chunk.block(x & 15, y, z & 15); - if w.facts.is_air(state) { - continue; - } - let Some(poi) = kind(state.0) else { - continue; - }; - if !poi_matches(m, f, poi) { - continue; - } - let p = [x, y, z]; - if tickets(m, w, p) >= poi.max_tickets() { - continue; - } - if accept(p) { - found.push((poi, p)); - } - } + if accept(p) { + found.push((poi, p)); } - } + }); } } - found.sort_by_key(|(_, p)| { - i64::from(p[0] - c[0]).pow(2) - + i64::from(p[1] - c[1]).pow(2) - + i64::from(p[2] - c[2]).pow(2) - }); + found.sort_by_key(|&(_, p)| dist_sqr(p, c)); found } /// `PoiManager.exists(pos, predicate)`. @@ -3389,26 +3378,23 @@ fn dist_sqr(a: [i32; 3], b: [i32; 3]) -> i64 { manhattan_sq(a, b) } /// Every bed head within `radius` (`PoiManager.getInRange(HOME, ..., ANY)`), -/// in scan order. +/// in the order of a scan of the cube: y, then z, then x. fn homes_in_range(m: &Mob, w: &MobWorld, radius: i32) -> Vec<[i32; 3]> { let c = block_of(m.position); let r2 = i64::from(radius) * i64::from(radius); let mut out = Vec::new(); let y0 = (c[1] - radius).max(crate::chunk::MIN_Y); let y1 = (c[1] + radius).min(crate::chunk::MIN_Y + crate::chunk::HEIGHT - 1); - for y in y0..=y1 { - for z in c[2] - radius..=c[2] + radius { - for x in c[0] - radius..=c[0] + radius { - let p = [x, y, z]; - if dist_sqr(p, c) <= r2 - && w.world.loaded(x, z) - && poi_at(w, x, y, z) == Some(Poi::Home) - { + for qz in (c[2] - radius) >> 4..=(c[2] + radius) >> 4 { + for qx in (c[0] - radius) >> 4..=(c[0] + radius) >> 4 { + chunk_pois(w, (qx, qz), y0, y1, |poi, p| { + if poi == Poi::Home && dist_sqr(p, c) <= r2 { out.push(p); } - } + }); } } + out.sort_unstable_by_key(|p| (p[1], p[2], p[0])); out } /// `MoveToSkySeeingSpot.hasNoBlocksAbove`. diff --git a/crates/horizon-client/src/mod_content.rs b/crates/horizon-client/src/mod_content.rs index 3a8c2920e..f78808da7 100644 --- a/crates/horizon-client/src/mod_content.rs +++ b/crates/horizon-client/src/mod_content.rs @@ -314,6 +314,22 @@ impl<'a> Plan<'a> { }); } } + if next > MAX_STATES { + let mut by_mod: BTreeMap<&str, u32> = BTreeMap::new(); + for b in &blocks { + *by_mod + .entry(&b.decl.id[..b.decl.id.find(':').unwrap_or(0)]) + .or_default() += b.rename.states.len() as u32; + } + let list: Vec = by_mod.iter().map(|(m, n)| format!("{m} {n}")).collect(); + return Err(format!( + "{next} block states, more than the {MAX_STATES} chunks can hold ({} in the game; \ + the mods: {})", + names.len(), + list.join(", ") + ) + .into()); + } // Block items copy the template block's item. let gameplay = read_json(&base.join("gameplay-facts.json"))?; let rows = match gameplay.get("states") { @@ -628,6 +644,9 @@ const NBT_TABLES: [&str; 6] = [ /// In a root built here: the content mods' ids, the namespaces loaders /// read besides `minecraft`. const MARKER: &str = "horizon-mod-content.json"; +/// Block states a world can hold: chunk sections store a state in 16 bits +/// (crate::chunk). Mods past it are refused, not wrapped onto other blocks. +pub const MAX_STATES: u32 = 1 << 16; /// The content mods' namespaces in a root built by [`build`]; none in any /// other root, so vanilla's loaders read exactly what they did. @@ -638,6 +657,19 @@ pub fn namespaces(root: &Path) -> Vec { .unwrap_or_default() } +/// The namespaces whose tags loaders read in `root`: `minecraft`, and in a +/// root built by [`build`] the convention tags (`c`, shared between mods as +/// on NeoForge and Fabric) and the content mods' own. +pub fn tag_namespaces(root: &Path) -> Vec { + let mods = namespaces(root); + let mut out = vec!["minecraft".to_owned()]; + if !mods.is_empty() { + out.push("c".to_owned()); + out.extend(mods); + } + out +} + /// Bundles of block tag files (tag id → the tag file's text). const TAG_BUNDLES: [&str; 2] = ["block-tags.json", "worldgen/block_tags.json"]; @@ -735,8 +767,67 @@ fn merge_tag(text: Option<&str>, more: &[J], what: &str) -> Result { Ok(serde_json::to_string_pretty(&doc)?) } +/// The block and item ids and tags a content root has, for tag entries. +struct Known { + blocks: std::collections::BTreeSet, + items: std::collections::BTreeSet, + /// `data//tags//.json` files the mods add. + tag_files: std::collections::BTreeSet, +} +impl Known { + fn new(base: &Path, plan: &Plan, tags: &BTreeMap>) -> Result { + let mut blocks: std::collections::BTreeSet = + plan.defaults.keys().cloned().collect(); + blocks.extend(plan.blocks.iter().map(|b| b.decl.id.clone())); + let mut items = std::collections::BTreeSet::new(); + if let J::Obj(o) = read_json(&base.join("items.json"))? { + items.extend(o.into_iter().map(|(k, _)| k)); + } + items.extend(plan.items.iter().map(|i| i.rename.to.clone())); + Ok(Known { + blocks, + items, + tag_files: tags.keys().cloned().collect(), + }) + } + /// Whether tag `file` keeps entry `v`: a known id or an existing tag, + /// for block and item tags (other registries are kept as they are). + fn keeps(&self, file: &str, v: &J) -> bool { + let id = match v { + J::Str(s) => s.as_str(), + J::Obj(_) => match v.get("id") { + Some(J::Str(s)) => s.as_str(), + _ => return true, + }, + _ => return true, + }; + let Some(kind) = file + .strip_prefix("data/") + .and_then(|r| r.split_once("/tags/")) + .and_then(|(_, rest)| Some(rest.split_once('/')?.0)) + else { + return true; + }; + let ids = match kind { + "block" => &self.blocks, + "item" => &self.items, + _ => return true, + }; + match id.strip_prefix('#') { + Some(tag) => { + let (tns, path) = tag.split_once(':').unwrap_or(("minecraft", tag)); + let rel = format!("data/{tns}/tags/{kind}/{path}.json"); + self.tag_files.contains(&rel) || tns == "minecraft" + } + None => ids.contains(id), + } + } +} + /// Builds the content root for `mods` over `base` in `out`. pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { + // The overlay links to the base: a relative base would leave them dangling. + let base = &std::path::absolute(base)?; let plan = Plan::new(base, mods)?; let overlay = Overlay::new(base, out)?; for name in JSON_TABLES { @@ -754,11 +845,11 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { if !path.exists() { continue; } - let (mut named, _) = nbt::read_named(&fs::read(&path)?, Limits::default()) + let (mut named, _) = nbt::read_named(&fs::read(&path)?, Limits::tables()) .map_err(|e| format!("{name}: {e}"))?; plan.nbt(&mut named.tag); let bytes = - nbt::write_named(&named, Limits::default()).map_err(|e| format!("{name}: {e}"))?; + nbt::write_named(&named, Limits::tables()).map_err(|e| format!("{name}: {e}"))?; overlay.write(name, &bytes)?; } // Mod files, and every tag addition gathered per tag file. @@ -788,6 +879,13 @@ pub fn build(base: &Path, mods: &[ContentMod], out: &Path) -> Result<()> { } } } + // A tag naming an id nobody registers fails as a whole in vanilla, and + // carried-over mods name blocks left out or other mods' ids: such + // entries are dropped instead, so the rest of the tag holds. + let known = Known::new(base, &plan, &tags)?; + for (file, values) in &mut tags { + values.retain(|v| known.keeps(file, v)); + } let mut bundles: Vec<(&str, J)> = Vec::new(); for name in TAG_BUNDLES { if base.join(name).exists() { diff --git a/crates/horizon-client/src/mod_content_tests.rs b/crates/horizon-client/src/mod_content_tests.rs index 6dd92ecfd..7067eef40 100644 --- a/crates/horizon-client/src/mod_content_tests.rs +++ b/crates/horizon-client/src/mod_content_tests.rs @@ -49,14 +49,19 @@ fn tags_merge_without_repeats() { } fn prepared() -> Option { - let root = Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3"); - root.join("gameplay-facts.json").exists().then_some(root) + let manifest = Path::new(env!("CARGO_MANIFEST_DIR")); + [ + manifest.join("../../.local/horizon-client/26.3"), + manifest.join("../../../../.local/horizon-client/26.3"), + ] + .into_iter() + .find(|root| root.join("gameplay-facts.json").exists()) } /// A content mod folder: a full block, a door and an item-less glass from /// templates, an item, a vanilla tag addition and a language file. fn ruby_mod(dir: &Path) -> ContentMod { - let files: [(&str, &str); 7] = [ + let files: [(&str, &str); 8] = [ ( "horizon-mod.toml", "id = \"ruby\"\nname = \"Ruby\"\nversion = \"0.1.0\"\napi = \"0.2\"\nside = \"both\"\ncapabilities = [\"registries\"]\n", @@ -78,9 +83,15 @@ fn ruby_mod(dir: &Path) -> ContentMod { "data/ruby/horizon/item/ruby.json", r#"{"template": "minecraft:diamond", "max_stack_size": 16, "rarity": "rare"}"#, ), + // Carried-over tags name blocks left out and other mods' ids too. ( "data/minecraft/tags/block/mineable/pickaxe.json", - r#"{"values": ["ruby:ruby_block"]}"#, + r##"{"values": ["ruby:ruby_block", "ruby:left_out", "othermod:thing", + {"id": "othermod:optional", "required": false}, "#c:storage_blocks/ruby"]}"##, + ), + ( + "data/c/tags/item/gems/ruby.json", + r#"{"values": ["ruby:ruby", "othermod:ruby"]}"#, ), ( "assets/ruby/lang/en_us.json", @@ -110,7 +121,10 @@ fn content_root_loads_with_every_table() { let scratch = std::env::temp_dir().join(format!("horizon-mod-content-{}", std::process::id())); let _ = fs::remove_dir_all(&scratch); let m = ruby_mod(&scratch.join("ruby")); - let root = resource_root(&base, std::slice::from_ref(&m), &scratch.join("cache")).unwrap(); + // The base as a relative path (cargo runs tests in the crate's folder): + // the overlay's links still resolve. + let relative = Path::new("../../.local/horizon-client/26.3"); + let root = resource_root(relative, std::slice::from_ref(&m), &scratch.join("cache")).unwrap(); assert_ne!(root, base); let vanilla = Assets::load(&base).unwrap(); @@ -137,6 +151,46 @@ fn content_root_loads_with_every_table() { let old_facts = BlockFacts::load(&base, old).unwrap(); let facts = BlockFacts::load(&root, new).unwrap(); + let shared_json: serde_json::Value = serde_json::from_reader(std::io::BufReader::new( + fs::File::open(root.join("block-behaviours.json")).unwrap(), + )) + .unwrap(); + let (shared_facts, shared_behaviours) = + BlockFacts::load_with_behaviours(&root, new, &shared_json).unwrap(); + let independent_behaviours = horizon_world::level::Behaviours::load(new, &root).unwrap(); + let (deferred_facts, gameplay) = + BlockFacts::load_with_projection(&root, new, &shared_json).unwrap(); + let deferred_behaviours = + horizon_world::level::Behaviours::load_with_gameplay(new, &root, &shared_json, &gameplay) + .unwrap(); + assert_eq!(shared_facts.states.len(), facts.states.len()); + for state in [ruby, ruby_door, BlockStateId(n + 65)] { + assert_eq!(shared_facts.get(state).hardness, facts.get(state).hardness); + assert_eq!( + deferred_facts.get(state).hardness, + facts.get(state).hardness + ); + assert_eq!(deferred_facts.get(state).classes, facts.get(state).classes); + assert_eq!( + deferred_behaviours.facts(state).flags, + independent_behaviours.facts(state).flags + ); + assert_eq!( + deferred_behaviours.data(state).sounds.place, + independent_behaviours.data(state).sounds.place + ); + assert_eq!(shared_facts.get(state).emission, facts.get(state).emission); + assert_eq!(shared_facts.get(state).classes, facts.get(state).classes); + assert_eq!( + shared_behaviours.facts(state).flags, + independent_behaviours.facts(state).flags, + ); + assert_eq!( + shared_behaviours.data(state).sounds.place, + independent_behaviours.data(state).sounds.place, + ); + } + let (a, b) = (old_facts.get(iron), facts.get(ruby)); assert_eq!((b.hardness, b.emission, b.friction), (4.0, 3, 0.8)); assert_eq!(b.explosion_resistance, a.explosion_resistance); @@ -169,7 +223,12 @@ fn content_root_loads_with_every_table() { let gem = &items.items["ruby:ruby"]; assert_eq!(gem.max_stack, 16); assert!(!items.items.contains_key("ruby:ruby_glass")); - assert!(items.block_tags["minecraft:mineable/pickaxe"].contains("ruby:ruby_block")); + let pickaxe = &items.block_tags["minecraft:mineable/pickaxe"]; + assert!(pickaxe.contains("ruby:ruby_block")); + assert!(!pickaxe.contains("ruby:left_out") && !pickaxe.contains("othermod:thing")); + assert!(pickaxe.contains("minecraft:stone")); + // Convention tags load, with the ids nobody registers dropped. + assert_eq!(items.item_tag_order["c:gems/ruby"], ["ruby:ruby"]); crate::collisions::load(&root, new).unwrap(); crate::models::load_visibility(&root, new).unwrap(); @@ -206,6 +265,32 @@ fn refuses_unknown_templates_and_vanilla_ids() { fs::remove_dir_all(&scratch).unwrap(); } +#[test] +fn refuses_more_states_than_chunks_hold() { + let Some(base) = prepared() else { + return; + }; + let scratch = std::env::temp_dir().join(format!("horizon-mod-states-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + let mut m = ruby_mod(&scratch.join("ruby")); + // Redstone wire has 1296 states: 40 copies pass any game's 65536. + let mut wire = m.content.blocks[0].clone(); + wire.template = "minecraft:redstone_wire".into(); + for i in 0..40 { + let mut b = wire.clone(); + b.id = format!("ruby:wire_{i}"); + m.content.blocks.push(b); + } + let e = build(&base, std::slice::from_ref(&m), &scratch.join("out")).unwrap_err(); + assert!( + e.to_string() + .contains("more than the 65536 chunks can hold"), + "{e}" + ); + assert!(e.to_string().contains("the mods: ruby "), "{e}"); + fs::remove_dir_all(&scratch).unwrap(); +} + #[test] fn no_content_keeps_the_base_root() { let base = Path::new("/nonexistent/base"); @@ -216,3 +301,184 @@ fn no_content_keeps_the_base_root() { } use horizon_world::registry::BlockStateId; + +#[test] +fn shared_behaviour_document_matches_independent_loaders() { + let Some(root) = prepared() else { return }; + let assets = Assets::load(&root).expect("assets"); + let doc: serde_json::Value = serde_json::from_reader(std::io::BufReader::new( + std::fs::File::open(root.join("block-behaviours.json")).unwrap(), + )) + .unwrap(); + let original = BlockFacts::load(&root, assets.registry).unwrap(); + let (shared, b) = BlockFacts::load_with_behaviours(&root, assets.registry, &doc).unwrap(); + let (deferred_facts, projection) = + BlockFacts::load_with_projection(&root, assets.registry, &doc).unwrap(); + let deferred = horizon_world::level::Behaviours::load_with_gameplay( + assets.registry, + &root, + &doc, + &projection, + ) + .unwrap(); + assert_eq!(deferred_facts.states.len(), shared.states.len()); + assert_eq!(original.states.len(), shared.states.len()); + for (a, b) in original.states.iter().zip(&shared.states) { + assert_eq!(a.classes, b.classes); + assert_eq!(a.legacy_solid, b.legacy_solid); + assert_eq!(a.sturdy_full, b.sturdy_full); + assert_eq!(a.emission, b.emission); + assert_eq!(a.dampening, b.dampening); + assert_eq!(a.hardness, b.hardness); + assert_eq!(a.sounds.volume, b.sounds.volume); + assert_eq!(a.sounds.pitch, b.sounds.pitch); + } + let a = horizon_world::level::Behaviours::load(assets.registry, &root).unwrap(); + for id in 0..assets.registry.len() { + let state = horizon_world::registry::BlockStateId(id as u32); + let (a_state, b_state) = (a.facts(state), b.facts(state)); + let deferred_state = deferred.facts(state); + assert_eq!(b_state.flags, deferred_state.flags); + assert_eq!(b_state.emission, deferred_state.emission); + assert_eq!(b_state.dampening, deferred_state.dampening); + assert_eq!(b_state.hardness, deferred_state.hardness); + assert_eq!(b_state.push, deferred_state.push); + assert_eq!(a_state.flags, b_state.flags); + assert_eq!(a_state.sturdy, b_state.sturdy); + assert_eq!(a_state.weak, b_state.weak); + assert_eq!(a_state.strong, b_state.strong); + assert_eq!(a_state.fluid, b_state.fluid); + assert_eq!(a_state.emission, b_state.emission); + assert_eq!(a_state.dampening, b_state.dampening); + assert_eq!(a_state.hardness, b_state.hardness); + assert_eq!(a_state.push, b_state.push); + assert_eq!(a_state.instrument, b_state.instrument); + assert_eq!(a_state.shape, b_state.shape); + let (a_sound, b_sound) = (&a.data(state).sounds, &b.data(state).sounds); + assert_eq!(a_sound.volume, b_sound.volume); + assert_eq!(a_sound.pitch, b_sound.pitch); + assert_eq!(a_sound.place, b_sound.place); + assert_eq!(a_sound.break_, b_sound.break_); + assert_eq!(a_sound.fall, b_sound.fall); + } +} + +#[cfg(unix)] +#[test] +fn independent_reloads_read_changed_behaviour_json_and_report_invalid_files() { + let Some(base) = prepared() else { return }; + let scratch = std::env::temp_dir().join(format!("horizon-json-reloads-{}", std::process::id())); + let _ = fs::remove_dir_all(&scratch); + fs::create_dir_all(&scratch).unwrap(); + // Keep the remaining resource tree intact without copying a huge prepared client. + for entry in fs::read_dir(&base).unwrap() { + let entry = entry.unwrap(); + #[cfg(unix)] + std::os::unix::fs::symlink(entry.path(), scratch.join(entry.file_name())).unwrap(); + } + let assets = Assets::load(&base).unwrap(); + let registry = assets.registry; + let original = fs::read(base.join("block-behaviours.json")).unwrap(); + fs::remove_file(scratch.join("block-behaviours.json")).unwrap(); + fs::write(scratch.join("block-behaviours.json"), &original).unwrap(); + // Independent facts and behaviours can load first, followed by paired data. + let independent_first = BlockFacts::load(&scratch, registry).unwrap(); + let independent_world_first = + horizon_world::level::Behaviours::load(registry, &scratch).unwrap(); + let (facts, paired_world) = BlockFacts::load_pair(&scratch, registry).unwrap(); + assert_eq!(facts.states.len(), registry.len()); + for state in [0, 1, registry.len() - 1] { + let state = horizon_world::registry::BlockStateId(state as u32); + assert_eq!( + facts.get(state).classes, + independent_first.get(state).classes + ); + assert_eq!( + facts.get(state).sounds.place, + independent_first.get(state).sounds.place + ); + assert_eq!( + paired_world.facts(state).flags, + independent_world_first.facts(state).flags + ); + } + + // Override one exported state bit at the same path and verify that a later + // independent and paired read sees it while the original resource root does not. + let mut changed: serde_json::Value = serde_json::from_slice(&original).unwrap(); + let original_bits = changed["states"][0][1].as_u64().unwrap(); + changed["states"][0][1] = (original_bits ^ 1).into(); + fs::write( + scratch.join("block-behaviours.json"), + serde_json::to_vec(&changed).unwrap(), + ) + .unwrap(); + let changed_pair = BlockFacts::load_pair(&scratch, registry).unwrap(); + let changed_independent = BlockFacts::load(&scratch, registry).unwrap(); + let base_facts = BlockFacts::load(&base, registry).unwrap(); + let state = horizon_world::registry::BlockStateId(0); + assert_ne!( + changed_pair.0.get(state).legacy_solid, + base_facts.get(state).legacy_solid + ); + assert_eq!( + changed_pair.0.get(state).legacy_solid, + changed_independent.get(state).legacy_solid + ); + // The world loader sees the same overridden legacy solidity flag. + let changed_world = horizon_world::level::Behaviours::load(registry, &scratch).unwrap(); + assert_eq!( + changed_world.facts(state).flags & 1 != 0, + changed_pair.0.get(state).legacy_solid + ); + + fs::write(scratch.join("block-behaviours.json"), b"{").unwrap(); + let paired_error = BlockFacts::load_pair(&scratch, registry) + .err() + .unwrap() + .to_string(); + let independent_error = BlockFacts::load(&scratch, registry) + .err() + .unwrap() + .to_string(); + assert_eq!(paired_error, independent_error); + assert!(horizon_world::level::Behaviours::load(registry, &scratch).is_err()); + + fs::write(scratch.join("block-behaviours.json"), b"{}").unwrap(); + assert!(BlockFacts::load_pair(&scratch, registry).is_err()); + assert!(BlockFacts::load(&scratch, registry).is_err()); + assert!(horizon_world::level::Behaviours::load(registry, &scratch).is_err()); + + fs::write(scratch.join("block-behaviours.json"), original).unwrap(); + assert_eq!( + BlockFacts::load_pair(&scratch, registry) + .unwrap() + .0 + .states + .len(), + registry.len() + ); + fs::remove_file(scratch.join("gameplay-facts.json")).unwrap(); + fs::write(scratch.join("gameplay-facts.json"), b"{\"states\":[]}").unwrap(); + assert!(BlockFacts::load_pair(&scratch, registry).is_err()); + assert!(BlockFacts::load(&scratch, registry).is_err()); + assert!(horizon_world::level::Behaviours::load(registry, &scratch).is_err()); + fs::write(scratch.join("gameplay-facts.json"), b"{").unwrap(); + assert!(BlockFacts::load_pair(&scratch, registry).is_err()); + assert!(BlockFacts::load(&scratch, registry).is_err()); + assert!(horizon_world::level::Behaviours::load(registry, &scratch).is_err()); + fs::write( + scratch.join("gameplay-facts.json"), + fs::read(base.join("gameplay-facts.json")).unwrap(), + ) + .unwrap(); + assert_eq!( + BlockFacts::load_pair(&scratch, registry) + .unwrap() + .0 + .states + .len(), + registry.len() + ); + fs::remove_dir_all(&scratch).unwrap(); +} diff --git a/crates/horizon-client/src/models.rs b/crates/horizon-client/src/models.rs index ba5bffefb..110166f29 100644 --- a/crates/horizon-client/src/models.rs +++ b/crates/horizon-client/src/models.rs @@ -120,7 +120,7 @@ pub fn load_visibility( nbt::{self, Limits}, }; use std::{collections::BTreeMap, fs}; - let data = nbt::read_named(&fs::read(root.join("occlusion.nbt"))?, Limits::default())? + let data = nbt::read_named(&fs::read(root.join("occlusion.nbt"))?, Limits::tables())? .0 .tag; let occlusion = CachedCollisionRegistry::from_nbt(&data, registry)?; diff --git a/crates/horizon-client/src/models/catalog.rs b/crates/horizon-client/src/models/catalog.rs index cccf08900..ecedd0f95 100644 --- a/crates/horizon-client/src/models/catalog.rs +++ b/crates/horizon-client/src/models/catalog.rs @@ -34,6 +34,8 @@ pub struct BakedFace { } pub struct BakedModel { pub ambient_occlusion: bool, + /// The model's particle slot also exists for models without block faces. + pub particle: Option, pub faces: Vec, pub cube: Option<[super::cube::CubeStyle; 6]>, } @@ -190,6 +192,10 @@ impl StateModels { let mut baked = BTreeMap::new(); for (name, turns, lock) in variants { let model = library.resolve(&name)?; + let particle = model + .material("particle")? + .map(|material| sprites.insert(root, &material.sprite)) + .transpose()?; let faces: Vec = bake_block_model(&model, BlockModelTransform::new(turns, lock))? .into_iter() @@ -209,6 +215,7 @@ impl StateModels { (name, turns, lock), BakedModel { ambient_occlusion: model.ambient_occlusion, + particle, faces, cube, }, diff --git a/crates/horizon-client/src/motion.rs b/crates/horizon-client/src/motion.rs index b098142dc..ae2486351 100644 --- a/crates/horizon-client/src/motion.rs +++ b/crates/horizon-client/src/motion.rs @@ -63,7 +63,7 @@ impl MotionResources { )?)?)?); let data = nbt::read_named( &fs::read(root.join("movement-properties.nbt"))?, - Limits::default(), + Limits::tables(), )? .0 .tag; diff --git a/crates/horizon-client/src/net.rs b/crates/horizon-client/src/net.rs index d260498a0..f9ad5992a 100644 --- a/crates/horizon-client/src/net.rs +++ b/crates/horizon-client/src/net.rs @@ -29,6 +29,8 @@ use std::{ pub enum Event { /// Progress text for the connecting screen. Status(String), + /// The server's `minecraft:brand` custom payload. + Brand(String), /// Configuration finished: the play state begins with these registries. Joined { registries: Arc, @@ -374,7 +376,7 @@ fn run( .map_err(e)?; let (mut reader, writer) = c.split(); *shared_writer.lock().unwrap() = Some(writer); - let registries = configure(&mut reader, shared_writer, lang)?; + let registries = configure(&mut reader, shared_writer, lang, send)?; let _ = send.send(Event::Joined { registries: Arc::new(registries), uuid: profile.uuid, @@ -413,10 +415,13 @@ fn run( } play::Clientbound::StartConfiguration(_) => { reply(play::Serverbound::ConfigurationAcknowledged(Empty)); - let registries = configure(&mut reader, shared_writer, lang)?; + let registries = configure(&mut reader, shared_writer, lang, send)?; let _ = send.send(Event::Reconfigured(Arc::new(registries))); } play::Clientbound::Disconnect(d) => return Ok(plain(&d.reason, lang)), + play::Clientbound::CustomPayload(p) if p.channel == "minecraft:brand" => { + receive_brand(&p, send); + } other => { if send.send(Event::Packet(Box::new(other))).is_err() { return Ok("Disconnected".into()); @@ -432,6 +437,7 @@ fn configure( reader: &mut ConnReader, writer: &Mutex>, lang: &Language, + send: &Sender, ) -> Result { let mut registries = Dynamic::default(); let reply = |p: cfg::Serverbound| -> Result<(), String> { @@ -444,6 +450,7 @@ fn configure( let (id, payload) = reader.recv().map_err(|e| e.to_string())?; match cfg::Clientbound::decode(id, &payload).map_err(|e| e.to_string())? { cfg::Clientbound::KeepAlive(k) => reply(cfg::Serverbound::KeepAlive(k))?, + cfg::Clientbound::CustomPayload(p) => receive_brand(&p, send), cfg::Clientbound::Ping(p) => reply(cfg::Serverbound::Pong(p))?, // Know no packs: the server then sends every registry entry in full. cfg::Clientbound::SelectKnownPacks(_) => { @@ -477,6 +484,14 @@ fn configure( } } +fn receive_brand(payload: &cfg::CustomPayload, send: &Sender) { + if payload.channel == "minecraft:brand" + && let Ok(brand) = horizon_protocol::buf::Reader::new(&payload.data).string(32767) + { + let _ = send.send(Event::Brand(brand)); + } +} + /// The minecraft:brand payload: a string naming the client. fn brand() -> Vec { let mut w = horizon_protocol::buf::Writer::new(); @@ -565,3 +580,28 @@ pub fn ping(address: &str, timeout: Duration) -> Result<(serde_json::Value, u128 let _ = c.recv().map_err(e)?; Ok((json, start.elapsed().as_millis())) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn server_brand_payload_is_decoded_and_invalid_or_unrelated_payloads_are_ignored() { + let (send, events) = channel(); + let mut writer = horizon_protocol::buf::Writer::new(); + writer.string("vanilla"); + let mut payload = cfg::CustomPayload { + channel: "minecraft:brand".into(), + data: writer.data, + }; + receive_brand(&payload, &send); + assert!(matches!(events.try_recv(), Ok(Event::Brand(brand)) if brand == "vanilla")); + payload.channel = "example:other".into(); + receive_brand(&payload, &send); + assert!(events.try_recv().is_err()); + payload.channel = "minecraft:brand".into(); + payload.data = vec![0xff]; + receive_brand(&payload, &send); + assert!(events.try_recv().is_err()); + } +} diff --git a/crates/horizon-client/src/options_horizon.rs b/crates/horizon-client/src/options_horizon.rs index bfaf05c9b..634bb8d75 100644 --- a/crates/horizon-client/src/options_horizon.rs +++ b/crates/horizon-client/src/options_horizon.rs @@ -587,9 +587,16 @@ impl OptionsUi { } return events; } - // A click anywhere stops waiting for an extra's key. + // Rebinding consumes the click before sidebar or widget actions. if matches!(self.selected_key, Some(extras::ZOOM_KEY | extras::SORT_KEY)) { - self.selected_key = None; + return self.bind_mouse( + s, + if right { + winit::event::MouseButton::Right + } else { + winit::event::MouseButton::Left + }, + ); } match self.horizon_target(s, atlas, x, y) { Some(Target::Nav(i)) => { diff --git a/crates/horizon-client/src/options_ui.rs b/crates/horizon-client/src/options_ui.rs index 38be29576..fab36defa 100644 --- a/crates/horizon-client/src/options_ui.rs +++ b/crates/horizon-client/src/options_ui.rs @@ -708,12 +708,14 @@ pub enum Page { Language, Font, Packs, + Mods, /// Horizon's graphics settings (`graphics_ui`). Graphics, GraphicsAdvanced, /// Horizon's controller settings and button binds. Controller, PadBinds, + Extras, } #[derive(Clone, Debug, PartialEq)] enum Act { @@ -721,6 +723,7 @@ enum Act { Push(Page), Done, Cycle(Opt), + Extra(crate::extras::Item), Key(&'static str), ResetKey(&'static str), ResetKeys, @@ -730,6 +733,7 @@ enum Act { ResetPadKeys, /// Back to Horizon's own menus (Horizon only, see `list_rows`). HorizonMenus, + ToggleMod(usize), } enum WKind { Button { @@ -824,6 +828,8 @@ pub struct OptionsUi { pub pack_selection: Vec, /// The open pack screen, made at its first frame. pub pack_screen: Option, + /// A reload failure, dismissed before returning to the current page. + pub resource_error: Option, /// When the pack screen's search box took the focus (its cursor blink). pack_focus: Instant, pub in_world: bool, @@ -854,6 +860,7 @@ impl Default for OptionsUi { pack_infos: Vec::new(), pack_selection: Vec::new(), pack_screen: None, + resource_error: None, pack_focus: Instant::now(), in_world: false, center_language: false, @@ -971,9 +978,11 @@ impl OptionsUi { Page::Language => "options.language.title", Page::Font => "options.font.title", Page::Packs => "resourcePack.title", + Page::Mods => "Mods", Page::Graphics | Page::GraphicsAdvanced => "options.videoTitle", Page::Controller => "Controller Settings", Page::PadBinds => "Controller Binds", + Page::Extras => "Horizon Extras", } } fn scrollbar_x(&self, width: f32) -> f32 { @@ -986,6 +995,22 @@ impl OptionsUi { } fn list_rows(&self, s: &Settings) -> Vec { match self.page() { + Page::Extras => crate::extras::rows() + .into_iter() + .map(|row| match row { + Err(header) => Row::Header(header), + Ok(item) => { + let value = format!("{}: {}", item.caption(), item.value(&s.extras)); + let label = + if item.key_name().is_some() && self.selected_key == item.key_name() { + Text::literal(format!("> {value} <")).with_color(text::YELLOW) + } else { + Text::literal(value) + }; + Row::Buttons(button(0.0, 0.0, 310.0, label, Act::Extra(item)), None) + } + }) + .collect(), Page::Video => { // Horizon's graphics screens, Simple and Advanced. let mut rows = vec![Row::Buttons( @@ -1161,6 +1186,36 @@ impl OptionsUi { Opt::AutoJump, ]), Page::Font => pairs(&[Opt::ForceUnicode, Opt::JapaneseGlyphs]), + Page::Mods => { + if s.mods.installed.is_empty() { + return vec![Row::Header( + "No mods: put .hmod files in the horizon-mods folder", + )]; + } + s.mods + .installed + .iter() + .enumerate() + .map(|(i, installed)| { + let mut label = Text::literal(format!("{}: ", installed.title)) + .append(on_off(s.mods.is_on(i))); + if installed.problem.is_some() { + label = label.append(" (Stopped)"); + } else if installed.dev { + label = label.append(" (Dev)"); + } + let widget = button(0.0, 0.0, 310.0, label, Act::ToggleMod(i)); + Row::Buttons( + if installed.dev || installed.id.is_empty() { + inactive(widget) + } else { + widget + }, + None, + ) + }) + .collect() + } _ => { let _ = s; Vec::new() @@ -1206,8 +1261,8 @@ impl OptionsUi { ("options.chat", Some(Page::Chat)), ("options.resourcepack", Some(Page::Packs)), ("options.accessibility", Some(Page::Accessibility)), - ("options.telemetry", None), - ("options.credits_and_attribution", None), + ("Mods...", Some(Page::Mods)), + ("Horizon Extras...", Some(Page::Extras)), ]; let gy = (61.0 + 30.0f32).min(h - 33.0 - 120.0); for (i, (key, page)) in entries.iter().enumerate() { @@ -1596,6 +1651,42 @@ impl OptionsUi { cursor: (f32, f32), ) { let (w, h) = (batch.width, batch.height); + if let Some(error) = &self.resource_error { + ui::menu_background(batch, atlas, 0.0, 0.0, w, h, self.in_world); + let title = tr("resourcePack.load_fail"); + batch.text_styled( + atlas, + &title, + ((w - atlas.font.width_text(&title)) / 2.0).floor(), + 30.0, + u32::MAX, + true, + ); + let lines = atlas.font.split(&Text::literal(error), (w - 50.0).max(1.0)); + let limit = ((h - 110.0) / 9.0).max(1.0) as usize; + for (i, line) in lines.iter().take(limit).enumerate() { + batch.text_styled( + atlas, + line, + ((w - atlas.font.width_text(line)) / 2.0).floor(), + 60.0 + i as f32 * 9.0, + u32::MAX, + true, + ); + } + ui::button( + batch, + atlas, + w / 2.0 - 100.0, + h - 28.0, + 200.0, + 20.0, + &tr("gui.done"), + true, + ui::hovered(cursor, w / 2.0 - 100.0, h - 28.0, 200.0, 20.0), + ); + return; + } if crate::ui_theme::horizon() && self.page() == Page::Main { self.draw_horizon(s, batch, atlas, cursor); return; @@ -1631,6 +1722,13 @@ impl OptionsUi { let hover = ui::hovered(cursor, wd.x, wd.y, wd.w, wd.h) && (!wd.list || cursor.1 >= top && cursor.1 < bottom); if hover { + if let WKind::Button { + act: Act::Extra(item), + .. + } = &wd.kind + { + tooltip = Some(atlas.font.split(&Text::literal(item.description()), 170.0)); + } let key = match &wd.kind { WKind::Button { act: Act::Cycle(opt), @@ -1650,6 +1748,21 @@ impl OptionsUi { if let Some(key) = key.filter(|key| lang::has(key)) { tooltip = Some(atlas.font.split(&tr(&key), 170.0)); } + if let WKind::Button { + act: Act::ToggleMod(i), + .. + } = &wd.kind + && let Some(installed) = s.mods.installed.get(*i) + { + let description = installed + .problem + .as_deref() + .unwrap_or(&installed.description); + tooltip = Some(atlas.font.split( + &Text::literal(format!("{}\n{description}", installed.title)), + 170.0, + )); + } } match &wd.kind { WKind::Button { label, active, act } => { @@ -1756,7 +1869,7 @@ impl OptionsUi { if self.page() == Page::Language { let mut search = self.language_search.clone(); search.draw(batch, atlas, None); - if search.value.is_empty() { + if search.value.is_empty() && !search.focused { batch.text_styled( atlas, &tr("gui.language.search").with_color(text::GRAY).italic(), @@ -1784,6 +1897,16 @@ impl OptionsUi { drag: bool, right: bool, ) -> Vec { + if self.resource_error.is_some() { + if !drag + && !right + && ui::hovered((x, y), size.0 / 2.0 - 100.0, size.1 - 28.0, 200.0, 20.0) + { + self.resource_error = None; + return vec![Event::ButtonClick]; + } + return Vec::new(); + } if crate::ui_theme::horizon() && self.page() == Page::Main { return self.click_horizon(s, atlas, x, y, framebuffer, drag, right); } @@ -1876,15 +1999,13 @@ impl OptionsUi { return events; } // Key Binds: a mouse button pressed while waiting binds it. - if let Some(name) = self.selected_key.take() { + if self.selected_key.is_some() { let button = if right { - "key.mouse.right" + winit::event::MouseButton::Right } else { - "key.mouse.left" + winit::event::MouseButton::Left }; - s.keys.set(name, button); - events.push(Event::Changed); - return events; + return self.bind_mouse(s, button); } // AbstractWidget accepts only the primary button. Rebinding above // accepts other mouse buttons, but is not a widget activation. @@ -1919,6 +2040,14 @@ impl OptionsUi { } events.push(Event::Changed); } + Act::Extra(item) => { + if let Some(name) = item.key_name() { + self.selected_key = Some(name); + } else { + item.cycle(&mut s.extras, right); + events.push(Event::Changed); + } + } Act::Key(name) => self.selected_key = Some(name), Act::ResetKey(name) => { s.keys.set(name, KeyBinds::default_of(name)); @@ -1944,6 +2073,10 @@ impl OptionsUi { crate::ui_theme::save_preferences(&dir); } } + Act::ToggleMod(i) => { + s.mods.toggle(i); + events.push(Event::Changed); + } Act::None => {} } } @@ -2012,6 +2145,9 @@ impl OptionsUi { } /// The mouse wheel at the cursor. pub fn scroll_at(&mut self, s: &Settings, size: (f32, f32), cursor: (f32, f32), amount: f32) { + if self.resource_error.is_some() { + return; + } if crate::ui_theme::horizon() && self.page() == Page::Main { self.scroll_horizon(amount); return; @@ -2090,6 +2226,9 @@ impl OptionsUi { } /// Typed text (the language search box). pub fn type_text(&mut self, atlas: &HudAtlas, text: &str) { + if self.resource_error.is_some() { + return; + } if self.page() == Page::Packs && let Some(p) = &mut self.pack_screen { @@ -2109,7 +2248,7 @@ impl OptionsUi { shortcut: crate::clipboard::EditShortcut, clipboard: &mut dyn crate::clipboard::Clipboard, ) -> bool { - if self.selected_key.is_some() { + if self.resource_error.is_some() || self.selected_key.is_some() { return false; } if self.page() == Page::Packs @@ -2126,6 +2265,23 @@ impl OptionsUi { } false } + fn bind_key(&mut self, s: &mut Settings, value: String) -> Vec { + let Some(name) = self.selected_key.take() else { + return Vec::new(); + }; + if !s.extras.set_key(name, value.clone()) { + s.keys.set(name, &value); + } + vec![Event::Changed] + } + /// Mouse bindings use the same settings and persistence as keyboard bindings. + pub fn bind_mouse( + &mut self, + s: &mut Settings, + button: winit::event::MouseButton, + ) -> Vec { + self.bind_key(s, keybinds::mouse_name(button)) + } /// Keys while the options are open. Returns events; `handled` false lets /// Escape fall through. pub fn key( @@ -2136,6 +2292,12 @@ impl OptionsUi { text: Option<&str>, ) -> Vec { use winit::keyboard::KeyCode as K; + if self.resource_error.is_some() { + if matches!(key, K::Escape | K::Enter | K::NumpadEnter) { + self.resource_error = None; + } + return Vec::new(); + } if self.page() == Page::Packs && let Some(p) = &mut self.pack_screen { @@ -2160,7 +2322,7 @@ impl OptionsUi { } return vec![Event::Changed]; } - if let Some(name) = self.selected_key.take() { + if self.selected_key.is_some() { // KeyBindsScreen.keyPressed: Escape unbinds. let value = if key == K::Escape { "key.keyboard.unknown".to_owned() @@ -2169,10 +2331,7 @@ impl OptionsUi { .unwrap_or("key.keyboard.unknown") .to_owned() }; - if !s.extras.set_key(name, value.clone()) { - s.keys.set(name, &value); - } - return vec![Event::Changed]; + return self.bind_key(s, value); } if key == K::Escape { return self.back(s); @@ -2201,6 +2360,137 @@ mod tests { HudAtlas::build(std::path::Path::new("/nonexistent/horizon-test-assets")).unwrap() } + #[test] + fn resource_reload_error_blocks_options_until_dismissed() { + use winit::keyboard::KeyCode; + let atlas = atlas(); + let mut settings = Settings::default(); + let mut ui = OptionsUi { + resource_error: Some("broken pack".into()), + ..Default::default() + }; + let stack = ui.stack.clone(); + assert!( + ui.click( + &mut settings, + &atlas, + 10.0, + 10.0, + (480.0, 270.0), + [960, 540], + false, + false + ) + .is_empty() + ); + assert!(ui.resource_error.is_some()); + assert!( + ui.key(&mut settings, &atlas, KeyCode::KeyW, None) + .is_empty() + ); + assert!(ui.resource_error.is_some()); + ui.key(&mut settings, &atlas, KeyCode::Escape, None); + assert!(ui.resource_error.is_none()); + assert_eq!(ui.stack, stack); + ui.resource_error = Some("broken pack".into()); + assert_eq!( + ui.click( + &mut settings, + &atlas, + 240.0, + 250.0, + (480.0, 270.0), + [960, 540], + false, + false + ), + vec![Event::ButtonClick] + ); + assert!(ui.resource_error.is_none()); + } + + #[test] + fn classic_mods_page_toggles_mods_and_keeps_dev_and_invalid_entries_inactive() { + let atlas = atlas(); + let mut s = Settings::default(); + let mut ui = OptionsUi::default(); + let size = (480.0, 240.0); + assert!(ui.layout(&s, size.0, size.1).0.iter().any(|w| { + matches!( + w.kind, + WKind::Button { + active: true, + act: Act::Push(Page::Mods), + .. + } + ) + })); + ui.show(Page::Mods); + let empty = ui.layout(&s, size.0, size.1).0; + assert!(empty.iter().any(|w| { + matches!(&w.kind, WKind::Text { text, .. } if text.plain().contains(".hmod")) + })); + for (id, dev) in [("normal", false), ("dev", true), ("", false)] { + s.mods.installed.push(crate::mod_list::Installed { + id: id.into(), + title: id.into(), + description: "May show HUD panels.".into(), + problem: id.is_empty().then(|| "Invalid package".into()), + dev, + }); + } + let (widgets, ..) = ui.layout(&s, size.0, size.1); + let rows: Vec<_> = widgets + .iter() + .filter(|w| { + matches!( + w.kind, + WKind::Button { + act: Act::ToggleMod(_), + .. + } + ) + }) + .collect(); + assert_eq!(rows.len(), 3); + for (i, row) in rows.iter().enumerate() { + let events = ui.click( + &mut s, + &atlas, + row.x + 1.0, + row.y + 1.0, + size, + [480, 240], + false, + false, + ); + if i == 0 { + assert!(events.contains(&Event::Changed)); + assert!(events.contains(&Event::ButtonClick)); + } else { + assert!(events.is_empty()); + } + } + assert!(s.mods.is_on(0)); + assert!(s.mods.is_on(1)); + assert!(!s.mods.is_on(2)); + assert_eq!(s.mods.serialize(), "mod.normal:true\n"); + let row = rows[0]; + ui.click( + &mut s, + &atlas, + row.x + 1.0, + row.y + 1.0, + size, + [480, 240], + false, + false, + ); + assert!(!s.mods.is_on(0)); + assert!(!ui.back(&mut s).contains(&Event::Close)); + assert_eq!(ui.page(), Page::Main); + } + #[test] fn option_tooltip_keys_follow_value_dependent_descriptions() { let mut s = Settings::default(); @@ -2316,6 +2606,83 @@ mod tests { } } + #[test] + fn classic_extras_entry_edits_shared_settings_and_returns_without_changing_style() { + let previous_style = style(); + set_style(MenuStyle::Classic); + let atlas = atlas(); + let mut s = Settings::default(); + let mut ui = OptionsUi::default(); + let size = (480.0, 480.0); + let click = |ui: &mut OptionsUi, s: &mut Settings, act: Act| { + let (widgets, _, _, _) = ui.layout(s, size.0, size.1); + let widget = widgets + .iter() + .find( + |w| matches!(&w.kind, WKind::Button { act: a, active: true, .. } if *a == act), + ) + .unwrap(); + ui.click( + s, + &atlas, + widget.x + 1.0, + widget.y + 1.0, + size, + [960, 960], + false, + false, + ) + }; + assert_eq!( + click(&mut ui, &mut s, Act::Push(Page::Extras)), + vec![Event::ButtonClick] + ); + assert_eq!(ui.page(), Page::Extras); + let (widgets, _, _, _) = ui.layout(&s, size.0, size.1); + assert_eq!( + widgets + .iter() + .filter(|w| matches!( + w.kind, + WKind::Button { + act: Act::Extra(_), + .. + } + )) + .count(), + crate::extras::rows().iter().filter(|r| r.is_ok()).count() + ); + assert_eq!( + click(&mut ui, &mut s, Act::Extra(crate::extras::Item::Zoom)), + vec![Event::ButtonClick, Event::Changed] + ); + assert!(s.extras.zoom); + assert_eq!( + click(&mut ui, &mut s, Act::Extra(crate::extras::Item::ZoomKey)), + vec![Event::ButtonClick] + ); + assert_eq!(ui.selected_key, Some(crate::extras::ZOOM_KEY)); + assert_eq!( + ui.key(&mut s, &atlas, winit::keyboard::KeyCode::KeyC, None), + vec![Event::Changed] + ); + assert_eq!(s.extras.zoom_key, "key.keyboard.c"); + assert_eq!( + crate::extras::Extras::parse(&s.extras.serialize()), + s.extras + ); + ui.scroll(&s, size, -1000.0); + let (_, top, bottom, content) = ui.layout(&s, size.0, size.1); + assert_eq!(ui.scroll, content - (bottom - top)); + assert_eq!( + ui.key(&mut s, &atlas, winit::keyboard::KeyCode::Escape, None), + vec![Event::Changed] + ); + assert_eq!(ui.page(), Page::Main); + assert_eq!(style(), MenuStyle::Classic); + set_style(previous_style); + } + #[test] fn classic_graphics_presets_set_the_distances() { let atlas = atlas(); @@ -2613,6 +2980,35 @@ mod tests { ); } + #[test] + fn language_search_hint_does_not_overlap_the_focused_caret() { + let atlas = atlas(); + let settings = Settings::default(); + let mut ui = OptionsUi::default(); + ui.show(Page::Language); + let draw_hint = |ui: &mut OptionsUi| { + let mut batch = HudBatch::new([960, 640]); + ui.draw(&settings, &mut batch, &atlas, (-1.0, -1.0)); + let search = &ui.language_search; + batch.vertices.iter().any(|v| { + v.color == rgba(170, 170, 170, 255) + && v.pos[0] >= search.text_x() + && v.pos[0] < search.x + search.width + && v.pos[1] >= search.text_y() + && v.pos[1] < search.y + search.height + }) + }; + ui.language_search.focused = false; + assert!(draw_hint(&mut ui)); + ui.language_search.focused = true; + assert!(!draw_hint(&mut ui)); + ui.language_search.focused = false; + ui.language_search.set_value("english"); + assert!(!draw_hint(&mut ui)); + ui.language_search.set_value(""); + assert!(draw_hint(&mut ui)); + } + fn languages() -> Vec { (0..30) .map(|i| LanguageInfo { @@ -2831,6 +3227,55 @@ mod tests { } } + #[test] + fn extras_and_vanilla_bindings_accept_mouse_buttons() { + use crate::extras::{Extras, SORT_KEY, ZOOM_KEY}; + use winit::event::MouseButton; + for button in [ + MouseButton::Left, + MouseButton::Right, + MouseButton::Middle, + MouseButton::Back, + MouseButton::Forward, + ] { + for name in [SORT_KEY, ZOOM_KEY, "key.jump"] { + let mut ui = OptionsUi::default(); + let mut s = Settings::default(); + ui.selected_key = Some(name); + assert_eq!(ui.bind_mouse(&mut s, button), vec![Event::Changed]); + assert_eq!(ui.selected_key, None); + let value = keybinds::mouse_name(button); + match name { + SORT_KEY => assert_eq!(s.extras.sort_key, value), + ZOOM_KEY => assert_eq!(s.extras.zoom_key, value), + _ => assert_eq!(s.keys.get(name), value), + } + assert_eq!(Extras::parse(&s.extras.serialize()), s.extras); + if name != "key.jump" { + assert_eq!(s.keys, Settings::default().keys); + } + assert!(ui.bind_mouse(&mut s, button).is_empty()); + } + } + } + + #[test] + fn horizon_extra_mouse_binding_consumes_the_click() { + let atlas = atlas(); + let mut s = Settings::default(); + let mut ui = OptionsUi::default(); + for (right, expected) in [(false, "key.mouse.left"), (true, "key.mouse.right")] { + ui.selected_key = Some(crate::extras::SORT_KEY); + assert_eq!( + ui.click_horizon(&mut s, &atlas, 2.0, 2.0, [960, 480], false, right), + vec![Event::Changed] + ); + assert_eq!(s.extras.sort_key, expected); + assert_eq!(ui.selected_key, None); + assert_eq!(ui.section, 0); + } + } + #[test] fn key_rebinding_takes_precedence_over_scrollbar_dragging() { let atlas = atlas(); diff --git a/crates/horizon-client/src/packs.rs b/crates/horizon-client/src/packs.rs index 917bad561..4c3972291 100644 --- a/crates/horizon-client/src/packs.rs +++ b/crates/horizon-client/src/packs.rs @@ -369,7 +369,8 @@ fn pack_files(pack: &Pack) -> io::Result> { } #[cfg(unix)] fn link(target: &Path, at: &Path) -> io::Result<()> { - std::os::unix::fs::symlink(target, at) + // Relative sources are relative to the process, not the overlay directory. + std::os::unix::fs::symlink(std::path::absolute(target)?, at) } #[cfg(not(unix))] fn link(target: &Path, at: &Path) -> io::Result<()> { @@ -635,9 +636,9 @@ pub fn enabled(all: &[Pack], ids: &[String]) -> Vec { .collect() } /// Resource root for a pack selection: vanilla itself when no pack is on. -pub fn resource_root(vanilla: &Path, packs: &[Pack], cache: &Path) -> PathBuf { +pub fn resource_root(vanilla: &Path, packs: &[Pack], cache: &Path) -> io::Result { if packs.is_empty() { - return vanilla.to_path_buf(); + return Ok(vanilla.to_path_buf()); } let key: String = packs .iter() @@ -648,13 +649,8 @@ pub fn resource_root(vanilla: &Path, packs: &[Pack], cache: &Path) -> PathBuf { (h ^ u64::from(b)).wrapping_mul(0x100_0000_01b3) }); let out = cache.join(format!("{hash:016x}")); - match build_overlay(vanilla, packs, &out) { - Ok(()) => out, - Err(e) => { - log::warn!("Horizon: resource packs not applied: {e}"); - vanilla.to_path_buf() - } - } + build_overlay(vanilla, packs, &out)?; + Ok(out) } /// `Pack.Position`: where a pack goes when it is added to a list. diff --git a/crates/horizon-client/src/packs/cache.rs b/crates/horizon-client/src/packs/cache.rs index 1f17c018b..efcf01e4a 100644 --- a/crates/horizon-client/src/packs/cache.rs +++ b/crates/horizon-client/src/packs/cache.rs @@ -8,7 +8,7 @@ use std::io::Read; use std::{collections::BTreeSet, fs, io, path::Path, path::PathBuf}; // Bump when overlay/merge semantics change. Markers are disposable cache data. -const VERSION: u32 = 2; +const VERSION: u32 = 3; #[derive(Default, Serialize, Deserialize)] pub(super) struct Dependencies { @@ -275,7 +275,6 @@ pub(super) fn build(vanilla: &Path, packs: &[Pack], out: &Path) -> io::Result<() .truncate(false) .open(lock_path(out))?; lock.lock()?; - // Resolve marker identity only; preserve the builder's original link targets. let vanilla_key = absolute(vanilla)?; let marker_path = marker_path(out); let signature = pack_signature(packs); @@ -484,7 +483,9 @@ mod tests { // a cache tree changed outside the builder. packs::build_overlay(&f.0.join("vanilla"), std::slice::from_ref(&low), &out).unwrap(); assert_eq!(fs::read(out.join(resource)).unwrap(), b"corrupt"); - fs::remove_file(&marker).unwrap(); + let mut old: Marker = serde_json::from_slice(&fs::read(&marker).unwrap()).unwrap(); + old.version = VERSION - 1; + fs::write(&marker, serde_json::to_vec(&old).unwrap()).unwrap(); f.rebuild_matches(std::slice::from_ref(&low)); fs::remove_file(out.join(resource)).unwrap(); fs::remove_file(&marker).unwrap(); @@ -564,8 +565,14 @@ mod tests { f.write("pack.zip", &archive(&files(b"ddd"), true)); f.rebuild_matches(std::slice::from_ref(&pack)); fs::remove_file(f.0.join("pack.zip")).unwrap(); - assert!(packs::build_overlay(&f.0.join("vanilla"), &[pack], &out).is_err()); + assert!( + packs::build_overlay(&f.0.join("vanilla"), std::slice::from_ref(&pack), &out).is_err() + ); assert!(!marker_path(&out).exists()); + assert!( + packs::resource_root(&f.0.join("vanilla"), &[pack], &f.0.join("selection-cache")) + .is_err() + ); } #[cfg(unix)] @@ -758,7 +765,7 @@ mod tests { #[cfg(unix)] #[test] - fn relative_builder_targets_are_preserved() { + fn relative_sources_are_readable_through_cached_and_uncached_overlays() { let f = Fixture::new(); f.write("vanilla/facts.json", b"[]"); let mut pack = f.dir_pack("pack"); @@ -777,15 +784,21 @@ mod tests { let out = f.0.join("cached"); let reference = f.0.join("reference"); packs::build_overlay(&vanilla, std::slice::from_ref(&pack), &out).unwrap(); - build_overlay_uncached(&vanilla, &[pack], &reference, &mut Dependencies::default()) - .unwrap(); - let expected = relative.join("pack").join(name); - assert_eq!(fs::read_link(out.join(name)).unwrap(), expected); - assert_eq!(fs::read_link(reference.join(name)).unwrap(), expected); - assert_eq!( - fs::read_link(out.join("facts.json")).unwrap(), - vanilla.join("facts.json") - ); + build_overlay_uncached( + &vanilla, + std::slice::from_ref(&pack), + &reference, + &mut Dependencies::default(), + ) + .unwrap(); + for root in [&out, &reference] { + assert!(fs::read_link(root.join(name)).unwrap().is_absolute()); + assert_eq!(fs::read(root.join(name)).unwrap(), b"pack"); + assert_eq!(fs::read(root.join("facts.json")).unwrap(), b"[]"); + } + packs::build_overlay(&vanilla, &[pack], &out).unwrap(); + assert_eq!(fs::read(out.join(name)).unwrap(), b"pack"); + assert_eq!(fs::read(out.join("facts.json")).unwrap(), b"[]"); } #[test] @@ -827,14 +840,17 @@ mod tests { let cached_cache = f.0.join("cached"); fs::create_dir_all(&reference_cache).unwrap(); let reference = original(&vanilla, selected, &reference_cache); - let out = packs::resource_root(&vanilla, selected, &cached_cache); + let out = packs::resource_root(&vanilla, selected, &cached_cache).unwrap(); assert_ne!(out, vanilla, "resource_root must not fall back to vanilla"); assert!(marker_path(&out).exists()); let expected = output_signature(&reference).unwrap(); assert_eq!(output_signature(&out).unwrap(), expected); for _ in 0..2 { original(&vanilla, selected, &reference_cache); - assert_eq!(packs::resource_root(&vanilla, selected, &cached_cache), out); + assert_eq!( + packs::resource_root(&vanilla, selected, &cached_cache).unwrap(), + out + ); } let mut baseline = Vec::new(); let mut cold = Vec::new(); @@ -861,6 +877,7 @@ mod tests { black_box(selected), black_box(&cached_cache), ) + .unwrap() }; black_box(&result); let ms = started.elapsed().as_secs_f64() * 1000.0; diff --git a/crates/horizon-client/src/poi_sections.rs b/crates/horizon-client/src/poi_sections.rs new file mode 100644 index 000000000..2116362e0 --- /dev/null +++ b/crates/horizon-client/src/poi_sections.rs @@ -0,0 +1,134 @@ +//! The points of interest of each resident section (`PoiSection`), so a +//! search visits the few POI blocks of the sections in range instead of +//! reading every block of them. +//! +//! Vanilla keeps its records up to date from `onBlockStateChange`; here a +//! section's list is rebuilt from its blocks after any edit. Resident +//! sections are immutable behind `Arc` and edited through `Arc::make_mut`, so +//! a weak reference tells an edited or replaced section from the one the list +//! was built from (see `random_tick_sections`), without keeping its blocks. +use crate::{chunk::Section, fast_hash::FxHashMap}; +use std::sync::{Arc, Mutex, Weak}; + +/// A section's POIs: the block index in the section (`y << 8 | z << 4 | x`) +/// and its kind, in ascending index order. +pub type SectionPois = Arc<[(u16, T)]>; + +struct Cached { + section: Weak
, + pois: SectionPois, +} +/// Cached POI lists by section, `(chunk x, section index, chunk z)`. +pub struct PoiSections { + sections: Mutex>, +} +struct Lists { + by_section: FxHashMap<(i32, i32, i32), Cached>, + /// Size above which lists of sections no longer resident are dropped. + prune_at: usize, +} +const MIN_PRUNE: usize = 4096; +impl Default for PoiSections { + fn default() -> Self { + Self { + sections: Mutex::new(Lists { + by_section: FxHashMap::default(), + prune_at: MIN_PRUNE, + }), + } + } +} +impl PoiSections { + /// The POIs of `section`, whose block states `kind` classifies. `kind` + /// must give the same answer for a state on every call. + pub fn get( + &self, + key: (i32, i32, i32), + section: &Arc
, + mut kind: impl FnMut(u32) -> Option, + ) -> SectionPois { + let mut lists = self.sections.lock().unwrap_or_else(|e| e.into_inner()); + if let Some(cached) = lists.by_section.get(&key) + && cached.section.as_ptr() == Arc::as_ptr(section) + { + return cached.pois.clone(); + } + let pois: SectionPois = if section.blocks.maybe_has(|v| kind(v).is_some()) { + let mut states = [0u16; 4096]; + section.unpack(&mut states); + states + .iter() + .enumerate() + .filter_map(|(i, &state)| kind(u32::from(state)).map(|poi| (i as u16, poi))) + .collect() + } else { + Arc::from([]) + }; + if lists.by_section.len() >= lists.prune_at { + lists.by_section.retain(|_, c| c.section.strong_count() > 0); + lists.prune_at = (lists.by_section.len() * 2).max(MIN_PRUNE); + } + lists.by_section.insert( + key, + Cached { + section: Arc::downgrade(section), + pois: pois.clone(), + }, + ); + pois + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::chunk::{Chunk, MIN_Y}; + + #[test] + fn lists_follow_edits_and_replacement() { + let index = PoiSections::::default(); + let kind = |v: u32| (v == 5 || v == 6).then_some(v as u8); + let mut chunk = Arc::new(Chunk::empty(0, 0)); + assert!(index.get((0, 0, 0), &chunk.sections[0], kind).is_empty()); + Arc::make_mut(&mut chunk).set(3, MIN_Y + 2, 1, 6); + Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 5); + let found = index.get((0, 0, 0), &chunk.sections[0], kind); + assert_eq!(&*found, &[(0, 5), (2 << 8 | 1 << 4 | 3, 6)]); + // Unchanged: the cached list, not a rescan. + let again = index.get((0, 0, 0), &chunk.sections[0], |_| panic!("rescanned")); + assert!(Arc::ptr_eq(&found, &again)); + // An edit in place (no other owner) still invalidates the list. + Arc::make_mut(&mut chunk).set(0, MIN_Y, 0, 1); + assert_eq!( + &*index.get((0, 0, 0), &chunk.sections[0], kind), + &[(2 << 8 | 1 << 4 | 3, 6)] + ); + // So does replacing the section, or the chunk. + Arc::make_mut(&mut chunk).sections[0] = Arc::new(Section::default()); + assert!(index.get((0, 0, 0), &chunk.sections[0], kind).is_empty()); + let mut other = Chunk::empty(0, 0); + other.set(1, MIN_Y, 1, 5); + chunk = Arc::new(other); + assert_eq!( + &*index.get((0, 0, 0), &chunk.sections[0], kind), + &[(1 << 4 | 1, 5)] + ); + // An unused palette entry alone yields no POI. + Arc::make_mut(&mut chunk).set(1, MIN_Y, 1, 2); + assert!(index.get((0, 0, 0), &chunk.sections[0], kind).is_empty()); + } + + #[test] + fn unloaded_sections_are_dropped() { + let index = PoiSections::::default(); + let kind = |v: u32| (v == 5).then_some(5); + let kept = Arc::new(Section::default()); + index.get((0, 0, 0), &kept, kind); + for i in 0..MIN_PRUNE as i32 { + index.get((1, i, 0), &Arc::new(Section::default()), kind); + } + let lists = index.sections.lock().unwrap(); + assert!(lists.by_section.len() <= 2); + assert!(lists.by_section.contains_key(&(0, 0, 0))); + } +} diff --git a/crates/horizon-client/src/random_tick_sections.rs b/crates/horizon-client/src/random_tick_sections.rs index d455c9e77..97a1f02fa 100644 --- a/crates/horizon-client/src/random_tick_sections.rs +++ b/crates/horizon-client/src/random_tick_sections.rs @@ -155,7 +155,7 @@ mod tick_parity { position::{BlockPos, ChunkPos}, random::{LegacyRandom, RandomSource}, }; - use horizon_world::level::{Behaviours, LevelAccess, LevelEvent, Ticks, data}; + use horizon_world::level::{LevelAccess, LevelEvent, Ticks, data}; use std::{cell::RefCell, path::PathBuf}; #[derive(Clone)] @@ -208,7 +208,14 @@ mod tick_parity { let root = std::env::var_os("HORIZON_RESOURCES") .map(PathBuf::from) .unwrap_or_else(|| { - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + let manifest = PathBuf::from(env!("CARGO_MANIFEST_DIR")); + let local = manifest.join("../../.local/horizon-client/26.3"); + if local.join("gameplay-facts.json").exists() { + local + } else { + // The branch worktree lives below the main checkout. + manifest.join("../../../../.local/horizon-client/26.3") + } }); if !root.join("gameplay-facts.json").exists() { eprintln!( @@ -218,8 +225,7 @@ mod tick_parity { return; } let registry = data::load_registry(&root).unwrap(); - let mut facts = BlockFacts::load(&root, ®istry).unwrap(); - let behaviours = Behaviours::load(®istry, &root).unwrap(); + let (mut facts, behaviours) = BlockFacts::load_pair(&root, ®istry).unwrap(); let grass = registry.default_state("minecraft:grass_block").unwrap(); let chunk = |x| { let mut c = Chunk::empty(x, 0); diff --git a/crates/horizon-client/src/save.rs b/crates/horizon-client/src/save.rs index a3ee81912..a8553e276 100644 --- a/crates/horizon-client/src/save.rs +++ b/crates/horizon-client/src/save.rs @@ -449,13 +449,17 @@ pub fn encode_block_entity( block: &str, ) -> Tag { use crate::containers::BlockEntity; + let components = match e { + BlockEntity::Banner(b) => b.components.clone(), + _ => Compound::new(), + }; let mut entries = vec![ ("id", string(&block_entity_id(e, block))), ("x", Tag::Int(p.0)), ("y", Tag::Int(p.1)), ("z", Tag::Int(p.2)), ("keepPacked", Tag::Byte(0)), - ("components", Tag::Compound(Compound::new())), + ("components", Tag::Compound(components)), ]; match e { BlockEntity::Chest(items) => entries.push(("Items", encode_items(items))), @@ -583,6 +587,10 @@ pub fn decode_block_entity(tag: &Tag) -> Option<((i32, i32, i32), crate::contain BlockEntity::Banner(crate::containers::Banner { layers: crate::banner::layers_from_nbt(tag.get("patterns")), name: text(tag.get("CustomName")), + components: match tag.get("components") { + Some(Tag::Compound(c)) => c.clone(), + _ => Compound::new(), + }, }), )); } @@ -2361,6 +2369,7 @@ impl Level { "Inventory", "EnderItems", "equipment", + "active_effects", "Pos", "respawn", "recipeBook", @@ -2984,6 +2993,7 @@ mod tests { let banner = crate::containers::Banner { layers: vec![crate::banner::BannerLayer::new("minecraft:creeper", "lime")], name: Some("Flag".into()), + components: [("minecraft:rarity".into(), string("uncommon"))].into(), }; let tag = encode_block_entity( (1, 2, 3), @@ -2997,6 +3007,7 @@ mod tests { }; assert_eq!(back.layers, banner.layers); assert_eq!(back.name, banner.name); + assert_eq!(back.components, banner.components); } use super::*; @@ -3191,4 +3202,19 @@ mod tests { assert!(dir.join("data/minecraft/world_gen_settings.dat").exists()); let _ = fs::remove_dir_all(&dir); } + + #[test] + fn effects_that_ended_are_not_saved_again() { + // A player loaded with an effect that has since run out: the loaded + // compound still holds it, but the save writes only what is active. + let mut level = Level { + active_effects: vec![("minecraft:speed".into(), 200, 1, false, true)], + ..Level::default() + }; + let loaded = level.player_tag(); + assert!(loaded.get("active_effects").is_some()); + level.player_extra = Some(loaded); + level.active_effects.clear(); + assert!(level.player_tag().get("active_effects").is_none()); + } } diff --git a/crates/horizon-client/src/sprites.rs b/crates/horizon-client/src/sprites.rs index d050e5fda..c26e02ea7 100644 --- a/crates/horizon-client/src/sprites.rs +++ b/crates/horizon-client/src/sprites.rs @@ -82,6 +82,10 @@ pub struct Sprites { /// Each layer's frames at the texture's own resolution (square), and the /// frame shown first. native: Vec<(u32, Arc>>, usize)>, + /// Still textures by their pixels and mip settings: mods copy vanilla's + /// textures under their own names, and one layer serves them all (the + /// GPU's array may hold as few as 2048). + still: std::collections::HashMap<(Vec, u32, [u32; 2], [bool; 4]), u32>, } /// Edge length of vanilla's block textures: the smallest array layer. pub const SPRITE_SIZE: u32 = 16; @@ -104,6 +108,10 @@ fn resample(frame: &[u8], from: u32, to: u32) -> Vec { out } impl Sprites { + /// How many array layers the textures take. + pub fn count(&self) -> usize { + self.native.len() + } /// Lay every layer out at one size: the largest texture's (resource packs /// may ship 32x, 64x... textures, like vanilla's stitched atlas), rounded /// up to a power of two and limited to [`MAX_ARRAY_BYTES`]. Smaller @@ -152,7 +160,7 @@ impl Sprites { } }) .collect(); - let layer = u32::try_from(self.layers.len())?; + let layer = u32::try_from(self.native.len())?; self.opaque.push(true); self.translucent.push(false); self.mips.push(crate::mipmap::Settings::default()); @@ -209,7 +217,7 @@ impl Sprites { }; pixels.extend(rgba); } - let layer = u32::try_from(self.layers.len())?; + let layer = u32::try_from(self.native.len())?; let columns = info.width / width; let count = columns * (info.height / height); // Layers are square; a frame of another shape is nearest-resampled to @@ -230,6 +238,46 @@ impl Sprites { } frames.push(frame); } + let settings = crate::mipmap::Settings { + native_size: side, + strategy: texture_section(metadata.as_ref()) + .as_ref() + .and_then(|t| t["mipmap_strategy"].as_str()) + .and_then(crate::mipmap::Strategy::parse) + .unwrap_or_default(), + alpha_cutoff_bias: texture_section(metadata.as_ref()) + .as_ref() + .and_then(|t| t["alpha_cutoff_bias"].as_f64()) + .unwrap_or(0.0) as f32, + has_transparent: pixels.as_chunks::<4>().0.iter().any(|p| p[3] == 0), + item: name + .split_once(':') + .is_some_and(|(_, p)| p.starts_with("item/")), + }; + let opaque = pixels.as_chunks::<4>().0.iter().all(|p| p[3] == 255); + let translucent = pixels + .as_chunks::<4>() + .0 + .iter() + .any(|p| p[3] != 0 && p[3] != 255); + let still = (animation.is_none() && frames.len() == 1).then(|| { + ( + frames[0].clone(), + side, + [ + settings.strategy as u32, + settings.alpha_cutoff_bias.to_bits(), + ], + [settings.has_transparent, settings.item, opaque, translucent], + ) + }); + if let Some(&same) = still.as_ref().and_then(|k| self.still.get(k)) { + self.layers.insert(name.to_owned(), same); + return Ok(same); + } + if let Some(key) = still { + self.still.insert(key, layer); + } let frames = Arc::new(frames); let mut initial = 0; if let Some(clip) = animation @@ -245,43 +293,25 @@ impl Sprites { clip: Arc::new(clip), }); } - // SpriteContents: TextureMetadataSection's mipmap strategy and alpha - // cutoff bias, and the whole image's NativeImage.computeTransparency. - let texture = metadata - .as_ref() - .and_then(|v| v.get("texture")) - .and_then(|v| serde_json::from_str::(v.get()).ok()); - self.mips.push(crate::mipmap::Settings { - native_size: side, - strategy: texture - .as_ref() - .and_then(|t| t["mipmap_strategy"].as_str()) - .and_then(crate::mipmap::Strategy::parse) - .unwrap_or_default(), - alpha_cutoff_bias: texture - .as_ref() - .and_then(|t| t["alpha_cutoff_bias"].as_f64()) - .unwrap_or(0.0) as f32, - has_transparent: pixels.as_chunks::<4>().0.iter().any(|p| p[3] == 0), - item: name - .split_once(':') - .is_some_and(|(_, p)| p.starts_with("item/")), - }); - self.opaque - .push(pixels.as_chunks::<4>().0.iter().all(|p| p[3] == 255)); - self.translucent.push( - pixels - .as_chunks::<4>() - .0 - .iter() - .any(|p| p[3] != 0 && p[3] != 255), - ); + self.mips.push(settings); + self.opaque.push(opaque); + self.translucent.push(translucent); self.layers.insert(name.to_owned(), layer); self.native.push((side, frames, initial)); Ok(layer) } } +/// SpriteContents: the `.mcmeta` `texture` section (TextureMetadataSection's +/// mipmap strategy and alpha cutoff bias). +fn texture_section( + metadata: Option<&BTreeMap>>, +) -> Option { + metadata + .and_then(|v| v.get("texture")) + .and_then(|v| serde_json::from_str(v.get()).ok()) +} + #[cfg(test)] pub(crate) fn test_animation(size: u32, layer: u32) -> SpriteAnimation { test_clip(size, layer, r#"{"frametime":1,"frames":[0,1]}"#) @@ -305,6 +335,46 @@ pub(crate) fn test_clip(size: u32, layer: u32, metadata: &str) -> SpriteAnimatio mod tests { use super::*; #[test] + fn identical_still_textures_share_a_layer() { + let root = + std::env::temp_dir().join(format!("horizon-sprite-share-{}", std::process::id())); + let directory = root.join("assets/a/textures/block"); + fs::create_dir_all(&directory).unwrap(); + let write = |name: &str, red: u8| { + let mut encoder = + png::Encoder::new(File::create(directory.join(name)).unwrap(), 16, 16); + encoder.set_color(png::ColorType::Rgba); + encoder.set_depth(png::BitDepth::Eight); + let pixels: Vec = (0..256).flat_map(|i| [red, i as u8, 0, 255]).collect(); + encoder + .write_header() + .unwrap() + .write_image_data(&pixels) + .unwrap(); + }; + write("one.png", 10); + write("copy.png", 10); + write("other.png", 20); + let mut sprites = Sprites::default(); + let one = sprites.insert(&root, "a:block/one").unwrap(); + let copy = sprites.insert(&root, "a:block/copy").unwrap(); + let other = sprites.insert(&root, "a:block/other").unwrap(); + // The same pixels as an item sprite mip differently: a layer of its own. + fs::create_dir_all(root.join("assets/a/textures/item")).unwrap(); + fs::copy( + directory.join("one.png"), + root.join("assets/a/textures/item/one.png"), + ) + .unwrap(); + let item = sprites.insert(&root, "a:item/one").unwrap(); + fs::remove_dir_all(&root).unwrap(); + assert_eq!((one, copy, other, item), (0, 0, 1, 2)); + assert_eq!((sprites.count(), sprites.layers.len()), (3, 4)); + assert_eq!((sprites.opaque.len(), sprites.mips.len()), (3, 3)); + sprites.finish(); + assert_eq!(sprites.pixels.len(), 3 * 16 * 16 * 4); + } + #[test] fn a_32px_only_atlas_does_not_add_an_extra_mipmap() { let mut sprites = Sprites { native: vec![ diff --git a/crates/horizon-client/src/stations.rs b/crates/horizon-client/src/stations.rs index 35b02c12e..613086112 100644 --- a/crates/horizon-client/src/stations.rs +++ b/crates/horizon-client/src/stations.rs @@ -481,23 +481,30 @@ fn ingredient(v: &Value, items: &Items) -> Ingredient { } impl StationRecipes { pub fn load(root: &Path, items: &Items) -> Self { - let mut files: Vec<_> = fs::read_dir(root.join("data/minecraft/recipe")) - .into_iter() - .flatten() - .flatten() - .map(|e| e.path()) - .filter(|p| p.extension().is_some_and(|e| e == "json")) - .collect(); - files.sort(); + // Vanilla's recipes, then the content mods' (docs/modpacks.md). + let mut files = Vec::new(); + let namespaces = + std::iter::once("minecraft".to_owned()).chain(crate::mod_content::namespaces(root)); + for namespace in namespaces { + let mut more: Vec<_> = fs::read_dir(root.join(format!("data/{namespace}/recipe"))) + .into_iter() + .flatten() + .flatten() + .map(|e| e.path()) + .filter(|p| p.extension().is_some_and(|e| e == "json")) + .collect(); + more.sort(); + files.extend(more.into_iter().map(|p| (namespace.clone(), p))); + } let mut smithing = Vec::new(); let mut stonecutting = Vec::new(); - for p in files { + for (namespace, p) in files { let Ok(bytes) = fs::read(&p) else { continue }; let Ok(v) = serde_json::from_slice::(&bytes) else { continue; }; let id = format!( - "minecraft:{}", + "{namespace}:{}", p.file_stem().and_then(|s| s.to_str()).unwrap_or("") ); match v["type"].as_str().unwrap_or("") { diff --git a/crates/horizon-client/src/streaming.rs b/crates/horizon-client/src/streaming.rs index df1001640..994316324 100644 --- a/crates/horizon-client/src/streaming.rs +++ b/crates/horizon-client/src/streaming.rs @@ -968,7 +968,8 @@ impl Streaming { return (uploads, removed); } // Meshing once the column and its neighbours are lit, and the column - // is within the render distance. Sections set aside past it come back + // is within the render distance (`in_view_distance`, the columns + // vanilla draws). Sections set aside past it come back // when the player moves: a column that loads with its light gets no // other request (#228). let area = (self.center, self.radius, self.forced.len()); @@ -981,7 +982,7 @@ impl Streaming { forced: &self.forced, }; self.waiting.retain(|&(x, sy, z)| { - let back = ranges.in_main_range(x, z, 0); + let back = ranges.in_view(x, z); if back { world.dirty_sections.insert((x, sy, z)); } @@ -996,7 +997,7 @@ impl Streaming { world.dirty_sections.remove(&(x, sy, z)); continue; } - if !self.in_main_range(x, z, 0) { + if !self.ranges().in_view(x, z) { world.dirty_sections.remove(&(x, sy, z)); self.waiting.insert((x, sy, z)); continue; @@ -1204,9 +1205,15 @@ impl Streaming { fn in_range(&self, x: i32, z: i32, margin: i32) -> bool { self.ranges().in_range(x, z, margin) } - fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool { - self.ranges().in_main_range(x, z, margin) - } +} +/// `ChunkTrackingView.isInViewDistance`: whether the column (x, z) is drawn +/// at this render distance around `center`, the camera's column +/// (`SectionOcclusionGraph.isInViewDistance`). Each axis gets a column of +/// slack, so this reaches past the circle of `radius` columns. +pub fn in_view_distance(center: (i32, i32), radius: i32, x: i32, z: i32) -> bool { + let gap = |d: i32| i64::from((d.abs() - 1).max(0)); + let (dx, dz) = (gap(x - center.0), gap(z - center.1)); + dx * dx + dz * dz < i64::from(radius) * i64::from(radius) } /// The areas kept loaded: circles around the player and `extra`, and forced chunks. struct Ranges<'a> { @@ -1271,6 +1278,11 @@ impl Ranges<'_> { dx * dx + dz * dz <= r * r }) } + /// The columns meshed for drawing: those vanilla draws around the + /// player (`in_view_distance`), and forced ones. + fn in_view(&self, x: i32, z: i32) -> bool { + in_view_distance(self.center, self.radius, x, z) || self.forced.contains(&(x, z)) + } fn in_main_range(&self, x: i32, z: i32, margin: i32) -> bool { let r = i64::from(self.radius + margin); let (dx, dz) = (i64::from(x - self.center.0), i64::from(z - self.center.1)); @@ -1306,6 +1318,31 @@ impl Drop for Streaming { mod tests { use super::*; + #[test] + fn view_distance_is_vanillas_shape() { + // ChunkTrackingView.isInViewDistance at render distance 12: a column + // of slack on each axis, so 533 columns rather than the 441 of a + // circle of radius 12. + let count = |r: i32| { + (-40..=40) + .flat_map(|x| (-40..=40).map(move |z| (x, z))) + .filter(|&(x, z)| in_view_distance((5, -7), r, x + 5, z - 7)) + .count() + }; + assert_eq!(count(12), 533); + assert_eq!(count(2), 25); + for (x, z, inside) in [(12, 0, true), (13, 0, false), (9, 9, true), (10, 9, false)] { + assert_eq!(in_view_distance((0, 0), 12, x, z), inside, "({x}, {z})"); + assert_eq!( + in_view_distance((0, 0), 12, -z, -x), + inside, + "({}, {})", + -z, + -x + ); + } + } + struct GenerationFixture { world: World, center: (i32, i32), diff --git a/crates/horizon-client/src/terrain.wgsl b/crates/horizon-client/src/terrain.wgsl index 26e32cb89..4689e7d2b 100644 --- a/crates/horizon-client/src/terrain.wgsl +++ b/crates/horizon-client/src/terrain.wgsl @@ -27,6 +27,29 @@ struct Globals { @group(0) @binding(2) var sprite_sampler: sampler; @group(0) @binding(3) var lightmap: texture_2d; @group(0) @binding(4) var lightmap_sampler: sampler; +// Layers past the first array's SPRITE_SPLIT (2048, gfx.rs): mods' sprites. +@group(0) @binding(5) var sprites_1: texture_2d_array; +@group(0) @binding(6) var sprites_2: texture_2d_array; +@group(0) @binding(7) var sprites_3: texture_2d_array; +// How many sprite arrays hold layers (a pipeline constant): 1 for vanilla, +// which then samples exactly as before. +override SPRITE_ARRAYS: u32 = 1u; +override SPRITE_SPLIT: u32 = 2048u; +fn sprite(uv: vec2, layer: u32) -> vec4 { + if SPRITE_ARRAYS == 1u { + return textureSample(sprites, sprite_sampler, uv, i32(layer)); + } + // The array is chosen per fragment: sample with the derivatives taken here. + let dx = dpdx(uv); + let dy = dpdy(uv); + let l = i32(layer % SPRITE_SPLIT); + switch layer / SPRITE_SPLIT { + case 0u: { return textureSampleGrad(sprites, sprite_sampler, uv, l, dx, dy); } + case 1u: { return textureSampleGrad(sprites_1, sprite_sampler, uv, l, dx, dy); } + case 2u: { return textureSampleGrad(sprites_2, sprite_sampler, uv, l, dx, dy); } + default: { return textureSampleGrad(sprites_3, sprite_sampler, uv, l, dx, dy); } + } +} struct TerrainOut { @builtin(position) clip: vec4, @@ -67,12 +90,12 @@ fn apply_fog(color: vec4, spherical: f32, cylindrical: f32) -> vec4 { } @fragment fn fs_solid(in: TerrainOut) -> @location(0) vec4 { // Bit 15 of the layer marks opaque leaves (forceOpaque): no alpha cutout. - let color = textureSample(sprites, sprite_sampler, in.uv, i32(in.layer & 0x7fffu)) * in.color; + let color = sprite(in.uv, in.layer & 0x7fffu) * in.color; if color.a < 0.5 && (in.layer & 0x8000u) == 0u { discard; } return apply_fog(vec4(color.rgb, 1.0), in.spherical, in.cylindrical); } @fragment fn fs_translucent(in: TerrainOut) -> @location(0) vec4 { - let color = textureSample(sprites, sprite_sampler, in.uv, i32(in.layer & 0x7fffu)) * in.color; + let color = sprite(in.uv, in.layer & 0x7fffu) * in.color; if color.a < 0.01 { discard; } return apply_fog(color, in.spherical, in.cylindrical); } diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index 2a9f0fdf9..172fb821a 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -125,6 +125,8 @@ pub enum Action { } pub struct Title { pub mode: Mode, + /// Direct Connection's address, synchronized with `options.txt`'s `lastServer`. + pub last_server: String, pub saves: PathBuf, pub started: std::time::Instant, search: String, @@ -232,6 +234,7 @@ impl Title { Self { shown: std::cell::Cell::new((std::mem::discriminant(&mode), now)), mode, + last_server: String::new(), saves, started: now, search: String::new(), @@ -397,7 +400,7 @@ impl Title { scroll.wheel(delta, ROW_HEIGHT / 2.0, top, bottom, content); self.scroll.set(scroll); } - fn failed(&mut self, error: impl std::fmt::Display) { + pub fn failed(&mut self, error: impl std::fmt::Display) { let return_to = Box::new(self.mode.clone()); self.mode = Mode::Error { message: error.to_string(), @@ -1162,6 +1165,7 @@ impl Title { /// The action and whether to play `AbstractWidget.playButtonClickSound`. /// Navigation buttons can return `Action::None` while still sounding. pub fn click(&mut self, x: f32, y: f32, size: (f32, f32)) -> (Action, bool) { + self.remember_direct_address(); let (w, h) = (size.0.ceil(), size.1.ceil()); // MenuTabBar: a new tab clears the focus and clicks. if let Mode::Create { focus, options, .. } = &mut self.mode @@ -1195,16 +1199,14 @@ impl Title { self.mode = Mode::Multiplayer(crate::multiplayer::Screen::list(&self.saves)); Action::None } - (Mode::Multiplayer(screen), hit) => { - match screen.click(hit, x, y, (w, h), &self.saves) { - crate::multiplayer::Outcome::Back => { - self.mode = Mode::Main; - Action::None - } - crate::multiplayer::Outcome::Connect(address) => Action::Connect { address }, - crate::multiplayer::Outcome::None => Action::None, + (Mode::Multiplayer(_), hit) => match self.multiplayer_click(hit, x, y, (w, h)) { + crate::multiplayer::Outcome::Back => { + self.mode = Mode::Main; + Action::None } - } + crate::multiplayer::Outcome::Connect(address) => Action::Connect { address }, + crate::multiplayer::Outcome::None => Action::None, + }, (Mode::Main, Some(3)) => Action::Options, (Mode::Main, Some(7)) => Action::Accessibility, (Mode::Main, Some(6)) => Action::Language, @@ -1512,7 +1514,31 @@ impl Title { _ => None, } } + pub(crate) fn remember_direct_address(&mut self) { + if let Mode::Multiplayer(crate::multiplayer::Screen::Direct { address }) = &self.mode { + self.last_server.clone_from(address); + } + } + pub(crate) fn multiplayer_click( + &mut self, + hit: Option, + x: f32, + y: f32, + size: (f32, f32), + ) -> crate::multiplayer::Outcome { + let Mode::Multiplayer(screen) = &mut self.mode else { + return crate::multiplayer::Outcome::None; + }; + let outcome = screen.click(hit, x, y, size, &self.saves); + if hit == Some(41) + && let crate::multiplayer::Screen::Direct { address } = screen + { + address.clone_from(&self.last_server); + } + outcome + } pub fn escape(&mut self) { + self.remember_direct_address(); if let Mode::Multiplayer(screen) = &mut self.mode && !screen.escape(&self.saves) { diff --git a/crates/horizon-client/src/title/tests.rs b/crates/horizon-client/src/title/tests.rs index 4f759f960..572be2ef2 100644 --- a/crates/horizon-client/src/title/tests.rs +++ b/crates/horizon-client/src/title/tests.rs @@ -82,6 +82,41 @@ impl Drop for Fixture { } } +#[test] +fn direct_connection_remembers_the_address_on_cancel_escape_and_join() { + let mut f = Fixture::new(); + f.title.last_server = "localhost:25566".into(); + f.click("Multiplayer"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "localhost:25566"); + + *f.title.focused_field().unwrap().1 = "example.org:25567".into(); + f.click("Cancel"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "example.org:25567"); + + *f.title.focused_field().unwrap().1 = "[::1]:25568".into(); + f.title.escape(); + f.title.escape(); + f.click("Multiplayer"); + f.click("Direct Connection"); + assert_eq!(f.title.focused_field().unwrap().1, "[::1]:25568"); + + *f.title.focused_field().unwrap().1 = "localhost:25569".into(); + assert_eq!( + f.click("Join Server"), + Action::Connect { + address: "localhost:25569".into(), + } + ); + assert_eq!(f.title.last_server, "localhost:25569"); + + f.title.focused_field().unwrap().1.clear(); + f.title.escape(); + f.click("Direct Connection"); + assert!(f.title.focused_field().unwrap().1.is_empty()); +} + #[test] fn empty_singleplayer_opens_creation_and_cancel_returns_to_title() { let mut f = Fixture::new(); diff --git a/crates/horizon-client/src/ui_theme.rs b/crates/horizon-client/src/ui_theme.rs index 1c41add9d..2cb3b5315 100644 --- a/crates/horizon-client/src/ui_theme.rs +++ b/crates/horizon-client/src/ui_theme.rs @@ -267,6 +267,10 @@ struct Cache { thread_local! { static CACHE: RefCell> = const { RefCell::new(None) }; } +/// Reloaded atlases can occupy the same address with empty run-time slots. +pub fn reset_atlas() { + CACHE.with(|cache| *cache.borrow_mut() = None); +} fn with_cache(atlas: &HudAtlas, f: impl FnOnce(&mut Cache) -> R) -> R { CACHE.with(|c| { let mut c = c.borrow_mut(); @@ -690,6 +694,7 @@ impl<'a> Ui<'a> { pos: p[i], uv: [cu, cv], color: c[i], + depth: 0.0, }); } } @@ -1366,3 +1371,36 @@ pub const fn id(name: &str, index: u64) -> u64 { } h ^ index.wrapping_mul(0x9e37_79b9_7f4a_7c15) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn atlas_reload_regenerates_text_and_corners_at_the_same_address() { + let atlas = HudAtlas::build(Path::new("/nonexistent/horizon-test-assets")).unwrap(); + reset_atlas(); + let draw = |cache: &mut Cache| { + let glyph = cache.fonts.face(Face::Regular).glyph_id('A'); + assert!( + cache + .glyph(&atlas, Face::Regular, glyph, 16.0) + .rect + .is_some() + ); + assert!(cache.corner(&atlas, 8, false).is_some()); + std::mem::take(&mut cache.pending) + }; + let before = with_cache(&atlas, draw); + assert_eq!(before.len(), 2); + assert!(with_cache(&atlas, draw).is_empty()); + reset_atlas(); + let after = with_cache(&atlas, draw); + assert_eq!(after.len(), 2); + for (a, b) in before.iter().zip(&after) { + assert_eq!((a.x, a.y, a.width, a.height), (b.x, b.y, b.width, b.height)); + assert_eq!(a.rgba, b.rgba); + } + reset_atlas(); + } +} diff --git a/crates/horizon-client/src/world.rs b/crates/horizon-client/src/world.rs index 2d8213905..44e442ec1 100644 --- a/crates/horizon-client/src/world.rs +++ b/crates/horizon-client/src/world.rs @@ -46,6 +46,8 @@ pub struct World { /// Every block position written since the log was last drained, kept /// only while the world is open to the network (see `lan`). pub block_log: Option>, + /// The villager POIs of each section (`PoiManager`'s sections). + pub pois: crate::poi_sections::PoiSections, } /// ServerLevel weather state (advanceWeatherCycle). #[derive(Clone, Copy, Debug, Default)] @@ -130,6 +132,7 @@ impl World { day_time: 1000, weather: Weather::default(), block_log: None, + pois: Default::default(), } } /// A controlled blockstate scene for the native scene probes: material diff --git a/crates/horizon-client/tests/lan_bot.rs b/crates/horizon-client/tests/lan_bot.rs index 7a4a4a3c4..36f53444c 100644 --- a/crates/horizon-client/tests/lan_bot.rs +++ b/crates/horizon-client/tests/lan_bot.rs @@ -241,6 +241,7 @@ fn server(resources: &Path) -> Server { view_distance: 3, simulation_distance: 3, dedicated: false, + integrated: false, dimension: "minecraft:overworld".into(), seed: 1234, max_players: 8, diff --git a/crates/horizon-mods/src/content.rs b/crates/horizon-mods/src/content.rs index 411fa7277..40b641759 100644 --- a/crates/horizon-mods/src/content.rs +++ b/crates/horizon-mods/src/content.rs @@ -4,7 +4,7 @@ //! //! Content is data, not code: nothing here runs in the sandbox. What a mod //! may add is fenced instead. Its own namespace, plus additions to vanilla -//! tags. Paths are plain, files and totals are capped, and vanilla ids +//! tags and to the convention tags (`c`, which NeoForge and Fabric mods share). Paths are plain, files and totals are capped, and vanilla ids //! cannot be redefined. use crate::manifest::{Capability, MANIFEST, Manifest}; use serde::Deserialize; @@ -242,12 +242,13 @@ impl Content { parts.next().unwrap_or(""), ); let own = namespace == id; - // Vanilla's own files stay vanilla's; a mod may only add to its tags. - let allowed = - own || (top == "data" && namespace == "minecraft" && rest.starts_with("tags/")); + // Vanilla's own files stay vanilla's; a mod may only add to its + // tags, and to the convention tags mods share. + let shared = matches!(namespace, "minecraft" | "c"); + let allowed = own || (top == "data" && shared && rest.starts_with("tags/")); if !allowed { return Err(format!( - "{name}: a mod adds files in its own namespace ({top}/{id}/) or to vanilla tags" + "{name}: a mod adds files in its own namespace ({top}/{id}/) or to vanilla and convention (c) tags" )); } let Some(decl) = (own && top == "data") @@ -417,10 +418,11 @@ capabilities = ["registries"] ("data/ruby/horizon/item/ruby.json", ITEM), ("data/ruby/recipe/ruby_block.json", b"{}"), ("data/minecraft/tags/block/mineable/pickaxe.json", b"{}"), + ("data/c/tags/item/gems/ruby.json", b"{}"), ("assets/ruby/textures/block/ruby_block.png", b"png"), ]; let content = hmod("ok", MANIFEST_TEXT, files).unwrap(); - assert_eq!(content.files.len(), 5); + assert_eq!(content.files.len(), 6); assert_eq!( content.blocks, [Block { @@ -471,6 +473,8 @@ capabilities = ["registries"] "vanilla-recipe", &[("data/minecraft/recipe/stone.json", b"{}")], ); + refused("convention-recipe", &[("data/c/recipe/stone.json", b"{}")]); + refused("convention-asset", &[("assets/c/lang/en_us.json", b"{}")]); let e = refused( "modded-template", &[( diff --git a/crates/horizon-port-pack/src/carry.rs b/crates/horizon-port-pack/src/carry.rs index 42c457f02..a637d6246 100644 --- a/crates/horizon-port-pack/src/carry.rs +++ b/crates/horizon-port-pack/src/carry.rs @@ -3,7 +3,7 @@ //! upgraded from 1.21.1 to 26.3, item models get their `items/` client //! definitions, and each blockstate gets a vanilla template guessed from its //! properties until the oracle's facts replace the guess. -use crate::jar::JarInfo; +use crate::{jar::JarInfo, oracle::Facts}; use serde::Serialize; use serde_json::{Value, json}; use std::{ @@ -20,14 +20,26 @@ pub struct Report { /// data other than tags), kept in `outside/` for review. pub outside: Vec, pub recipes_upgraded: usize, + pub loot_tables_upgraded: usize, + /// Worldgen and structure files (`data//worldgen/`, `structure/`). + pub worldgen: usize, /// Recipes left as they were: custom types, NeoForge conditions. pub recipes_kept: Vec, pub item_definitions_added: usize, - /// Declared blocks and their guessed templates. + /// Templates and properties came from the NeoForge oracle's facts, not + /// from guesses. + pub oracle: bool, + /// Declared blocks and their templates. pub blocks: BTreeMap, - /// Blockstates whose properties match no template. - pub blocks_unknown: Vec, + /// What a declared block's template does not reproduce (oracle only). + pub differences: BTreeMap>, + /// Blocks left out, and why. + pub blocks_unknown: BTreeMap, pub items: usize, + /// Items left out, and why (oracle only). + pub items_unknown: BTreeMap, + /// Convention (`c:`) tags the recipes use, filled in from the oracle. + pub convention_tags: usize, } /// Vanilla blocks by their sorted property names. @@ -42,6 +54,7 @@ const TEMPLATES: &[(&str, &str)] = &[ "minecraft:oak_trapdoor", ), ("east,north,south,waterlogged,west", "minecraft:oak_fence"), + ("east,north,south,waterlogged,west", "minecraft:glass_pane"), ( "east,north,south,up,waterlogged,west", "minecraft:cobblestone_wall", @@ -58,9 +71,24 @@ const TEMPLATES: &[(&str, &str)] = &[ ("stage", "minecraft:oak_sapling"), ]; -pub fn template(properties: &BTreeSet) -> Option<&'static str> { - let key = properties.iter().cloned().collect::>().join(","); - TEMPLATES.iter().find(|(k, _)| *k == key).map(|(_, t)| *t) +/// A guess without the oracle: the template whose properties include the +/// blockstate's with the fewest left over (a blockstate may leave out what +/// its models ignore, such as `powered` or `waterlogged`), the block's name +/// breaking ties (`_pane`, `_gate`, `_trapdoor`). +pub fn template(name: &str, properties: &BTreeSet) -> Option<&'static str> { + let fits = TEMPLATES.iter().filter_map(|(k, t)| { + let have: BTreeSet<&str> = k.split(',').filter(|p| !p.is_empty()).collect(); + properties + .iter() + .all(|p| have.contains(p.as_str())) + .then(|| (have.len() - properties.len(), *t)) + }); + let hint = |t: &str| { + let kind = t.rsplit('_').next().unwrap_or(t); + !name.ends_with(&format!("_{kind}")) + }; + fits.min_by_key(|&(extra, t)| (extra, hint(t))) + .map(|(_, t)| t) } /// The property names a blockstate file uses. @@ -165,7 +193,92 @@ pub fn upgrade_recipe(recipe: &mut Value) -> bool { true } -/// Where a jar entry goes: `Some(true)` inside the content mod, `Some(false)` +/// A 1.21.1 loot table in 26.3's shape, in place: `conditions` lists become +/// one `condition` (`all_of` for several), `functions` become `modifier`, +/// and conditions and functions name their kind in `type`. A tool match on +/// silk touch alone, or on shears alone, becomes 26.3's +/// `minecraft:tool/can_silk_touch` or `minecraft:tool/can_shear`. +pub fn upgrade_loot(v: &mut Value) { + match v { + Value::Array(a) => a.iter_mut().for_each(upgrade_loot), + Value::Object(o) => { + if let Some(Value::Array(mut list)) = o.remove("conditions") { + list.iter_mut().for_each(upgrade_condition); + let one = match <[Value; 1]>::try_from(list) { + Ok([c]) => c, + Err(list) => json!({"type": "minecraft:all_of", "terms": list}), + }; + o.insert("condition".to_owned(), one); + } + if let Some(Value::Array(mut list)) = o.remove("functions") { + for f in &mut list { + rename_kind(f, "function"); + upgrade_loot(f); + } + o.insert("modifier".to_owned(), Value::Array(list)); + } + for (k, x) in o.iter_mut() { + if k != "condition" && k != "modifier" { + upgrade_loot(x); + } + } + } + _ => {} + } +} + +/// `{"": kind, ...}` as `{"type": kind, ...}`. +fn rename_kind(v: &mut Value, key: &str) { + if let Some(o) = v.as_object_mut() + && !o.contains_key("type") + && let Some(kind) = o.remove(key) + { + o.insert("type".to_owned(), kind); + } +} + +/// One 1.21.1 loot condition in 26.3's shape. +fn upgrade_condition(c: &mut Value) { + rename_kind(c, "condition"); + if let Some(term) = c.get_mut("term") { + upgrade_condition(term); + } + if let Some(Value::Array(terms)) = c.get_mut("terms") { + terms.iter_mut().for_each(upgrade_condition); + } + if c["type"] == "minecraft:match_tool" + && let Some(named) = named_tool_condition(&c["predicate"]) + { + *c = json!(named); + } +} + +/// 26.3's named tool conditions for the 1.21.1 predicates they replace. +fn named_tool_condition(predicate: &Value) -> Option<&'static str> { + let keys: Vec<&str> = predicate.as_object()?.keys().map(String::as_str).collect(); + match keys.as_slice() { + ["predicates"] => { + let list = predicate["predicates"]["minecraft:enchantments"].as_array()?; + let [e] = list.as_slice() else { + return None; + }; + let silk = e["enchantments"] == "minecraft:silk_touch" + && e["levels"]["min"].as_u64().unwrap_or(1) <= 1 + && e["levels"].get("max").is_none() + && predicate["predicates"].as_object()?.len() == 1; + silk.then_some("minecraft:tool/can_silk_touch") + } + ["items"] => { + let items = &predicate["items"]; + let shears = items == "minecraft:shears" || *items == json!(["minecraft:shears"]); + shears.then_some("minecraft:tool/can_shear") + } + _ => None, + } +} + +/// Where a jar entry goes: `Some(true)` inside the content mod (its own +/// namespace, additions to vanilla and convention tags), `Some(false)` /// to `outside/`, `None` not carried (code, metadata). fn place(name: &str, id: &str) -> Option { let mut parts = name.splitn(3, '/'); @@ -173,10 +286,21 @@ fn place(name: &str, id: &str) -> Option { if !(root == "assets" || root == "data") || rest.is_empty() || name.ends_with('/') { return None; } - Some(ns == id || (root == "data" && ns == "minecraft" && rest.starts_with("tags/"))) + let shared_tags = + root == "data" && (ns == "minecraft" || ns == "c") && rest.starts_with("tags/"); + Some(ns == id || shared_tags) } -pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { +/// Carries `jar` over into `out/carry/`. With the oracle's `facts`, blocks +/// and items are the ones the mod registers, with the templates and numbers +/// the facts give; `allowed` says which vanilla blocks the game has. +pub fn carry( + jar: &Path, + info: &JarInfo, + out: &Path, + facts: Option<&Facts>, + allowed: &dyn Fn(&str) -> bool, +) -> Result { let id = &info.mod_id; if id.is_empty() || id.len() > 64 @@ -196,6 +320,7 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { let mut report = Report::default(); let mut blockstates: BTreeMap = BTreeMap::new(); let mut item_models = BTreeSet::new(); + let mut convention = BTreeSet::new(); for i in 0..zip.len() { let mut entry = zip.by_index(i).map_err(|e| e.to_string())?; let name = entry.name().to_owned(); @@ -218,11 +343,17 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { }; if keep { let rel = name.split_once(&format!("/{id}/")).map_or("", |(_, r)| r); + if name.starts_with("data/") + && (rel.starts_with("worldgen/") || rel.starts_with("structure/")) + { + report.worldgen += 1; + } if let Some(file) = rel .strip_prefix("recipe/") .filter(|_| name.starts_with("data/")) { if let Ok(mut recipe) = serde_json::from_slice::(&bytes) { + tags_used(&recipe, &mut convention); if upgrade_recipe(&mut recipe) { report.recipes_upgraded += 1; bytes = serde_json::to_vec_pretty(&recipe).unwrap_or(bytes); @@ -230,6 +361,15 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { report.recipes_kept.push(file.to_owned()); } } + } else if rel.starts_with("loot_table/") && name.starts_with("data/") { + if let Ok(mut table) = serde_json::from_slice::(&bytes) { + let before = table.clone(); + upgrade_loot(&mut table); + if table != before { + report.loot_tables_upgraded += 1; + bytes = serde_json::to_vec_pretty(&table).unwrap_or(bytes); + } + } } else if let Some(b) = rel .strip_prefix("blockstates/") .and_then(|b| b.strip_suffix(".json")) @@ -257,25 +397,116 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { } } let decl = inside.join(format!("data/{id}/horizon")); - for (b, state) in &blockstates { - match template(&properties(state)) { - Some(t) => { - write( - &decl.join(format!("block/{b}.json")), - json!({"template": t}).to_string().as_bytes(), - )?; - report.blocks.insert(b.clone(), t.to_owned()); + let write_decl = |kind: &str, name: &str, v: &Value| { + let text = serde_json::to_vec_pretty(v).unwrap_or_default(); + write(&decl.join(format!("{kind}/{name}.json")), &text) + }; + if let Some(facts) = facts { + report.oracle = true; + let prefix = format!("{id}:"); + let mut declared = BTreeSet::new(); + for (full, block) in facts.blocks.range(prefix.clone()..) { + let Some(b) = full.strip_prefix(&prefix) else { + break; + }; + let Some(choice) = facts.template(full, allowed) else { + let props: Vec<&str> = block.properties.keys().map(String::as_str).collect(); + let why = format!( + "no vanilla {} with properties [{}]", + block.vanilla_class().rsplit('.').next().unwrap_or(""), + props.join(", ") + ); + report.blocks_unknown.insert(b.to_owned(), why); + continue; + }; + let mut v = json!({"template": choice.template, "properties": choice.properties}); + if block.item == "minecraft:air" { + v["item"] = json!(false); + } else if block.item != *full { + v["item"] = json!(false); + report + .differences + .entry(b.to_owned()) + .or_default() + .push(format!("its item is {}", block.item)); + } else { + declared.insert(full.clone()); + } + write_decl("block", b, &v)?; + report.blocks.insert(b.to_owned(), choice.template); + if !choice.differences.is_empty() { + report + .differences + .entry(b.to_owned()) + .or_default() + .extend(choice.differences); } - None => report.blocks_unknown.push(b.clone()), } - } - for m in item_models.iter().filter(|m| !blockstates.contains_key(*m)) { - let item = json!({"template": "minecraft:stick"}); - write( - &decl.join(format!("item/{m}.json")), - item.to_string().as_bytes(), - )?; - report.items += 1; + for (full, item) in facts.items.range(prefix.clone()..) { + let Some(name) = full.strip_prefix(&prefix) else { + break; + }; + if declared.contains(full) { + continue; + } + if item + .block + .as_ref() + .is_some_and(|b| facts.blocks.contains_key(b)) + && report.blocks_unknown.contains_key(name) + { + report + .items_unknown + .insert(name.to_owned(), "its block is left out".into()); + continue; + } + match facts.item(full) { + Ok((v, differences)) => { + write_decl("item", name, &v)?; + report.items += 1; + if !differences.is_empty() { + report.differences.insert(name.to_owned(), differences); + } + } + Err(e) => { + report.items_unknown.insert(name.to_owned(), e); + } + } + } + // Convention tags are NeoForge's and the pack's: the recipes get the + // members the oracle saw, those of vanilla and of this mod. + for tag in &convention { + let Some(path) = tag.strip_prefix("c:") else { + continue; + }; + let file = inside.join(format!("data/c/tags/item/{path}.json")); + if file.exists() { + continue; + } + if let Some(members) = facts.tag_members("item", tag, id) { + let v = json!({"values": members}); + write(&file, &serde_json::to_vec_pretty(&v).unwrap_or_default())?; + report.convention_tags += 1; + } + } + } else { + for (b, state) in &blockstates { + match template(b, &properties(state)) { + Some(t) => { + write_decl("block", b, &json!({"template": t}))?; + report.blocks.insert(b.clone(), t.to_owned()); + } + None => { + let props: Vec = properties(state).into_iter().collect(); + let why = format!("no template for properties [{}]", props.join(", ")); + report.blocks_unknown.insert(b.clone(), why); + } + } + } + for m in item_models.iter().filter(|m| !blockstates.contains_key(*m)) { + write_decl("item", m, &json!({"template": "minecraft:stick"}))?; + report.items += 1; + } } let q = |s: &str| toml::Value::String(s.to_owned()).to_string(); let name = if info.name.is_empty() { id } else { &info.name }; @@ -299,6 +530,30 @@ pub fn carry(jar: &Path, info: &JarInfo, out: &Path) -> Result { Ok(report) } +/// The tags a recipe's ingredients name (`#ns:path` or `{"tag": ...}`). +fn tags_used(v: &Value, out: &mut BTreeSet) { + match v { + Value::String(s) => { + if let Some(t) = s.strip_prefix('#') { + out.insert(t.to_owned()); + } + } + Value::Array(a) => a.iter().for_each(|x| tags_used(x, out)), + Value::Object(o) => { + for (k, x) in o { + match (k.as_str(), x) { + ("tag", Value::String(t)) => { + out.insert(t.clone()); + } + ("result", _) => {} + _ => tags_used(x, out), + } + } + } + _ => {} + } +} + fn write(path: &Path, bytes: &[u8]) -> Result<(), String> { if let Some(dir) = path.parent() { fs::create_dir_all(dir).map_err(|e| format!("{}: {e}", dir.display()))?; @@ -325,21 +580,86 @@ mod tests { assert!(!upgrade_recipe(&mut tag_list)); } + #[test] + fn upgrades_loot_tables() { + let mut slab = json!({"type": "minecraft:block", "pools": [{"rolls": 1.0, "entries": [{ + "type": "minecraft:item", "name": "m:slab", + "functions": [{"function": "minecraft:set_count", "count": 2.0, "add": false, + "conditions": [{"condition": "minecraft:block_state_property", "block": "m:slab", + "properties": {"type": "double"}}]}, + {"function": "minecraft:explosion_decay"}]}]}]}); + upgrade_loot(&mut slab); + assert_eq!( + slab["pools"][0]["entries"][0]["modifier"][0], + json!({"type": "minecraft:set_count", "count": 2.0, "add": false, + "condition": {"type": "minecraft:block_state_property", "block": "m:slab", + "properties": {"type": "double"}}}) + ); + let silk = json!({"condition": "minecraft:match_tool", "predicate": {"predicates": { + "minecraft:enchantments": [{"enchantments": "minecraft:silk_touch", "levels": {"min": 1}}]}}}); + let shears = json!({"condition": "minecraft:match_tool", "predicate": {"items": "minecraft:shears"}}); + let mut glass = json!({"pools": [{"conditions": [silk.clone()], "entries": []}, + {"conditions": [{"condition": "minecraft:any_of", "terms": [silk, shears]}, + {"condition": "minecraft:survives_explosion"}], "entries": []}]}); + upgrade_loot(&mut glass); + assert_eq!( + glass["pools"][0]["condition"], + "minecraft:tool/can_silk_touch" + ); + assert_eq!( + glass["pools"][1]["condition"], + json!({"type": "minecraft:all_of", "terms": [ + {"type": "minecraft:any_of", "terms": ["minecraft:tool/can_silk_touch", "minecraft:tool/can_shear"]}, + {"type": "minecraft:survives_explosion"}]}) + ); + // A loot table already in 26.3's shape stays as it is. + let mut new = glass.clone(); + upgrade_loot(&mut new); + assert_eq!(new, glass); + } + #[test] fn templates_from_blockstate_properties() { let door = json!({"variants": {"facing=east,half=lower,hinge=left,open=false,powered=false": {}}}); - assert_eq!(template(&properties(&door)), Some("minecraft:oak_door")); + assert_eq!( + template("gem_door", &properties(&door)), + Some("minecraft:oak_door") + ); + // Models that ignore `powered`: still a door. + let door = json!({"variants": {"facing=east,half=lower,hinge=left,open=false": {}}}); + assert_eq!( + template("gem_door", &properties(&door)), + Some("minecraft:oak_door") + ); + let trapdoor = json!({"variants": {"facing=east,half=top,open=false": {}}}); + assert_eq!( + template("gem_trapdoor", &properties(&trapdoor)), + Some("minecraft:oak_trapdoor") + ); + let sides = json!({"multipart": [{"when": {"north": "true"}}, {"when": {"east": "true"}}, + {"when": {"south": "true"}}, {"when": {"west": "true"}}]}); + assert_eq!( + template("gem_pane", &properties(&sides)), + Some("minecraft:glass_pane") + ); + assert_eq!( + template("gem_fence", &properties(&sides)), + Some("minecraft:oak_fence") + ); let fence = json!({"multipart": [{"apply": {}}, {"when": {"north": "true"}, "apply": {}}, {"when": {"east": "true"}}, {"when": {"south": "true"}}, {"when": {"west": "true"}}, {"when": {"OR": [{"waterlogged": "true"}]}}]}); - assert_eq!(template(&properties(&fence)), Some("minecraft:oak_fence")); assert_eq!( - template(&properties(&json!({"variants": {"": {}}}))), + template("gem_fence", &properties(&fence)), + Some("minecraft:oak_fence") + ); + assert_eq!( + template("gem_block", &properties(&json!({"variants": {"": {}}}))), Some("minecraft:stone") ); let odd = json!({"variants": {"charge=1,mode=fast": {}}}); - assert_eq!(template(&properties(&odd)), None); + assert_eq!(template("gem_core", &properties(&odd)), None); } /// A small NeoForge jar carried over is a content mod Horizon loads. @@ -411,14 +731,17 @@ mod tests { ); assert_eq!(info.census.classes, 1); let out = dir.join("work/mods/gem"); - let report = carry(&jar, &info, &out).unwrap(); - assert_eq!(report.copied, 11); - assert_eq!(report.outside, ["data/c/tags/item/gems.json"]); + let report = carry(&jar, &info, &out, None, &|_| true).unwrap(); + assert_eq!(report.copied, 12); + assert!(report.outside.is_empty()); assert_eq!((report.recipes_upgraded, report.recipes_kept.len()), (1, 1)); assert_eq!(report.item_definitions_added, 2); assert_eq!(report.blocks["gem_door"], "minecraft:oak_door"); assert_eq!(report.blocks["gem_block"], "minecraft:stone"); - assert_eq!(report.blocks_unknown, ["gem_core"]); + assert_eq!( + report.blocks_unknown.keys().collect::>(), + ["gem_core"] + ); assert_eq!(report.items, 1); let carried = out.join("carry"); assert!(!carried.join("com").exists()); diff --git a/crates/horizon-port-pack/src/import.rs b/crates/horizon-port-pack/src/import.rs index b0f5d04c2..dd712acf1 100644 --- a/crates/horizon-port-pack/src/import.rs +++ b/crates/horizon-port-pack/src/import.rs @@ -1,5 +1,6 @@ -//! Step 1, import: a Modrinth `.mrpack`, a CurseForge zip, or an instance -//! folder (Prism / MultiMC, or a plain `mods/` folder) to `pack.json`. +//! Step 1, import: a Modrinth `.mrpack`, a CurseForge zip, an instance +//! folder (Prism / MultiMC, or a plain `mods/` folder), or a Crash Assistant +//! `modlist.json` (the list packs such as ATM10 publish) to `pack.json`. use serde::{Deserialize, Serialize}; use serde_json::Value; use std::{ @@ -37,6 +38,13 @@ pub struct PackFile { /// CurseForge project and file ids. #[serde(default, skip_serializing_if = "Option::is_none")] pub curseforge: Option<(u64, u64)>, + /// The mod id, name and version a mod list states (`modlist.json`). + #[serde(default, skip_serializing_if = "Option::is_none")] + pub mod_id: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub version: Option, /// The jar on disk (instances only). #[serde(default, skip_serializing_if = "Option::is_none")] pub jar: Option, @@ -63,6 +71,12 @@ pub fn import(path: &Path) -> Result { if path.is_dir() { return instance(path); } + if path.extension().is_some_and(|e| e == "json") { + let text = fs::read_to_string(path).map_err(|e| format!("{}: {e}", path.display()))?; + let list: Value = serde_json::from_str(text.trim_start_matches('\u{feff}')) + .map_err(|e| format!("{}: {e}", path.display()))?; + return modlist(&list); + } let file = fs::File::open(path).map_err(|e| format!("{}: {e}", path.display()))?; let mut zip = zip::ZipArchive::new(file).map_err(|e| format!("{}: {e}", path.display()))?; if let Some(index) = read_json(&mut zip, "modrinth.index.json")? { @@ -186,6 +200,54 @@ pub fn curseforge(manifest: &Value, names: &[String]) -> Pack { } } +/// Crash Assistant's `config/crash_assistant/modlist.json`: jar name → mod +/// id, name, version and Modrinth SHA-1, with the loader as an entry of its +/// own. NeoForge `21.1.x` runs Minecraft 1.21.1. +pub fn modlist(list: &Value) -> Result { + let entries = list.as_object().ok_or("modlist.json: not an object")?; + let mut pack = Pack { + name: "modlist".into(), + format: "modlist".into(), + ..Pack::default() + }; + for (key, e) in entries { + let jar = e["jarName"].as_str().unwrap_or(key); + if jar.ends_with("(modloader)") { + let version = str_of(e, "version"); + let (loader, v) = version.split_once('-').unwrap_or(("", &version)); + pack.loader = loader.to_owned(); + pack.loader_version = v.to_owned(); + if loader == "neoforge" { + let mut parts = v.split('.'); + if let (Some(a), Some(b)) = (parts.next(), parts.next()) { + pack.minecraft = if b == "0" { + format!("1.{a}") + } else { + format!("1.{a}.{b}") + }; + } + } + continue; + } + let folder = if jar.ends_with(".zip") { + "datapacks" + } else { + "mods" + }; + let text = |k: &str| e[k].as_str().filter(|v| !v.is_empty()).map(str::to_owned); + pack.files.push(PackFile { + path: format!("{folder}/{jar}"), + sha1: text("modrinthHash"), + mod_id: text("modId"), + name: text("name"), + version: text("version"), + ..PackFile::default() + }); + } + pack.files.sort_by(|a, b| a.path.cmp(&b.path)); + Ok(pack) +} + /// A Prism / MultiMC instance (`mmc-pack.json`), a game folder, or a /// folder of jars: the jars on disk, hashed. fn instance(dir: &Path) -> Result { @@ -319,6 +381,30 @@ mod tests { ); } + #[test] + fn reads_a_crash_assistant_mod_list() { + let list = serde_json::json!({ + "neoforge-21.1.259 (modloader)": {"jarName": "neoforge-21.1.259 (modloader)", + "modId": "neoforge", "version": "neoforge-21.1.259"}, + "mcw-doors-1.1.2.jar": {"jarName": "mcw-doors-1.1.2.jar", "modId": "mcwdoors", + "modrinthHash": "0123456789abcdef0123456789abcdef01234567"}, + "sawmill.zip": {"jarName": "sawmill.zip"} + }); + let pack = modlist(&list).unwrap(); + assert_eq!( + ( + pack.minecraft.as_str(), + pack.loader.as_str(), + pack.loader_version.as_str() + ), + ("1.21.1", "neoforge", "21.1.259") + ); + let paths: Vec<&str> = pack.files.iter().map(|f| f.path.as_str()).collect(); + assert_eq!(paths, ["datapacks/sawmill.zip", "mods/mcw-doors-1.1.2.jar"]); + assert!(pack.files[1].sha1.is_some()); + assert_eq!(pack.files[1].mod_id.as_deref(), Some("mcwdoors")); + } + #[test] fn reads_a_curseforge_manifest() { let manifest = json!({"name": "CF", "overrides": "overrides", diff --git a/crates/horizon-port-pack/src/inventory.rs b/crates/horizon-port-pack/src/inventory.rs index 49661f1ae..57c043ae3 100644 --- a/crates/horizon-port-pack/src/inventory.rs +++ b/crates/horizon-port-pack/src/inventory.rs @@ -78,6 +78,7 @@ pub fn build(pack: &Pack, cache: &Cache, work: &Path) -> Vec { .as_ref() .map(|i| i.mod_id.clone()) .filter(|k| !k.is_empty()) + .or_else(|| f.mod_id.clone()) .or_else(|| Some(p("slug")).filter(|s| !s.is_empty())) .unwrap_or(stem); let (client, server) = match project { @@ -97,7 +98,9 @@ pub fn build(pack: &Pack, cache: &Cache, work: &Path) -> Vec { .as_ref() .map(|i| i.name.clone()) .filter(|n| !n.is_empty()) - .unwrap_or_else(|| p("title")); + .or_else(|| Some(p("title")).filter(|t| !t.is_empty())) + .or_else(|| f.name.clone()) + .unwrap_or_default(); let (class, tier, confidence) = match known.get(&[key.as_str(), p("slug").as_str(), name.as_str()]) { Some((c, t)) => (c.clone(), t.clone(), "known".to_owned()), @@ -123,8 +126,9 @@ pub fn build(pack: &Pack, cache: &Cache, work: &Path) -> Vec { .versions .get(sha1) .and_then(|v| v["version_number"].as_str()) - .unwrap_or("") - .to_owned() + .map(str::to_owned) + .or_else(|| f.version.clone()) + .unwrap_or_default() }), path: f.path.clone(), class, diff --git a/crates/horizon-port-pack/src/jar.rs b/crates/horizon-port-pack/src/jar.rs index 67028755e..86e936e44 100644 --- a/crates/horizon-port-pack/src/jar.rs +++ b/crates/horizon-port-pack/src/jar.rs @@ -50,6 +50,12 @@ pub fn read(path: &Path) -> Result { } else { JarInfo::default() }; + // `version = "${file.jarVersion}"` is filled in from the manifest at load. + if info.version.starts_with("${") { + info.version = text(&mut zip, "META-INF/MANIFEST.MF") + .and_then(|m| manifest_value(&m, "Implementation-Version")) + .unwrap_or_default(); + } let names: Vec = zip.file_names().map(str::to_owned).collect(); info.census = census(&names); if info.mod_id.is_empty() { @@ -61,6 +67,16 @@ pub fn read(path: &Path) -> Result { Ok(info) } +/// A main-section attribute of `META-INF/MANIFEST.MF`. +fn manifest_value(manifest: &str, key: &str) -> Option { + manifest + .lines() + .take_while(|l| !l.trim().is_empty()) + .find_map(|l| l.strip_prefix(key)?.strip_prefix(':')) + .map(|v| v.trim().to_owned()) + .filter(|v| !v.is_empty()) +} + /// `[[mods]]` (the first one is the jar's mod), the top-level `license`, and /// `[[dependencies.]]` with `type = "required"` (or the older /// `mandatory = true`). diff --git a/crates/horizon-port-pack/src/main.rs b/crates/horizon-port-pack/src/main.rs index 4df3ef3ca..3e6cdf4d7 100644 --- a/crates/horizon-port-pack/src/main.rs +++ b/crates/horizon-port-pack/src/main.rs @@ -6,8 +6,15 @@ //! `/modrinth.json`), the jars in the instance or `/jars/`, //! classes, tiers and dependency layers in `/inventory.json` and //! `inventory.csv`. -//! - `carry [...]`: the content mods' data and assets into -//! `/mods//carry/`, a folder mod for local use. +//! - `carry [--resources ] [...]`: the content +//! mods' data and assets into `/mods//carry/`, a folder mod for +//! local use. With the NeoForge oracle's `/oracle/facts.json` +//! (`tools/neoforge_oracle.py`), blocks and items come from its facts; +//! `--resources` limits templates to the blocks Horizon's tables have. +//! +//! - `report `: the coverage report, per mod ported, partial, not yet +//! or not needed with the reason, in `/coverage.json` and +//! `coverage.md`. //! //! The work folder belongs to the user: jars and carried-over files stay //! there and are never uploaded or committed. @@ -17,10 +24,12 @@ mod import; mod inventory; mod jar; mod modrinth; +mod oracle; +mod report; use std::{fs, path::Path, process::ExitCode}; -const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | carry [...]"; +const USAGE: &str = "usage: horizon-port-pack import | inventory [--offline] | carry [--resources ] [...] | report "; fn main() -> ExitCode { let args: Vec = std::env::args().skip(1).collect(); @@ -32,7 +41,20 @@ fn main() -> ExitCode { Some("inventory") => { path(1).and_then(|w| inventory(w, args.iter().any(|a| a == "--offline"))) } - Some("carry") => path(1).and_then(|w| carry(w, &args[2..])), + Some("carry") => path(1).and_then(|w| { + let mut rest = args[2..].to_vec(); + let resources = match rest.iter().position(|a| a == "--resources") { + Some(i) if i + 1 < rest.len() => { + let r = rest.remove(i + 1); + rest.remove(i); + Some(std::path::PathBuf::from(r)) + } + Some(_) => return Err(USAGE.to_owned()), + None => None, + }; + carry(w, resources.as_deref(), &rest) + }), + Some("report") => path(1).and_then(report), _ => Err(USAGE.to_owned()), }; match result { @@ -54,6 +76,22 @@ fn read_json(path: &Path) -> Result { serde_json::from_str(&text).map_err(|e| format!("{}: {e}", path.display())) } +fn report(work: &Path) -> Result<(), String> { + let pack: import::Pack = read_json(&work.join("pack.json"))?; + let records: Vec = read_json(&work.join("inventory.json"))?; + let rows = report::build(&records, work); + write_json(&work.join("coverage.json"), &rows)?; + let title = format!( + "{} ({} {}, Minecraft {})", + pack.name, pack.loader, pack.loader_version, pack.minecraft + ); + fs::write(work.join("coverage.md"), report::markdown(&title, &rows)) + .map_err(|e| format!("coverage.md: {e}"))?; + println!("{title}: {} files: {:?}", rows.len(), report::counts(&rows)); + println!("{}", work.join("coverage.md").display()); + Ok(()) +} + fn import(pack: &Path, work: &Path) -> Result<(), String> { let pack = import::import(pack)?; fs::create_dir_all(work).map_err(|e| format!("{}: {e}", work.display()))?; @@ -107,8 +145,31 @@ fn inventory(work: &Path, offline: bool) -> Result<(), String> { Ok(()) } -fn carry(work: &Path, only: &[String]) -> Result<(), String> { +/// The vanilla blocks a prepared resource root has (`facts.json`'s defaults). +fn vanilla_blocks(resources: &Path) -> Result, String> { + let facts: serde_json::Value = read_json(&resources.join("facts.json"))?; + let defaults = facts["defaults"] + .as_object() + .ok_or("facts.json: no defaults")?; + Ok(defaults.keys().cloned().collect()) +} + +fn carry(work: &Path, resources: Option<&Path>, only: &[String]) -> Result<(), String> { let records: Vec = read_json(&work.join("inventory.json"))?; + let facts_path = work.join("oracle/facts.json"); + let facts = if facts_path.exists() { + let f = oracle::Facts::read(&facts_path)?; + println!( + "oracle facts: {} blocks, {} items", + f.blocks.len(), + f.items.len() + ); + Some(f) + } else { + None + }; + let vanilla = resources.map(vanilla_blocks).transpose()?; + let allowed = |b: &str| vanilla.as_ref().is_none_or(|v| v.contains(b)); let mut done = 0; for r in &records { let chosen = if only.is_empty() { @@ -125,19 +186,29 @@ fn carry(work: &Path, only: &[String]) -> Result<(), String> { if !chosen { continue; } - match carry::carry(jar, info, &work.join("mods").join(&r.key)) { + match carry::carry( + jar, + info, + &work.join("mods").join(&r.key), + facts.as_ref(), + &allowed, + ) { Ok(report) => { done += 1; println!( - "{}: {} files, {} recipes upgraded ({} kept), {} item definitions added, {} blocks ({} without a template), {} items, {} outside", + "{}: {} files, {} recipes upgraded ({} kept), {} loot tables upgraded, {} item definitions added, {} blocks ({} left out, {} with differences), {} items ({} left out), {} convention tags, {} outside", r.key, report.copied, report.recipes_upgraded, report.recipes_kept.len(), + report.loot_tables_upgraded, report.item_definitions_added, report.blocks.len(), report.blocks_unknown.len(), + report.differences.len(), report.items, + report.items_unknown.len(), + report.convention_tags, report.outside.len() ); } diff --git a/crates/horizon-port-pack/src/oracle.rs b/crates/horizon-port-pack/src/oracle.rs new file mode 100644 index 000000000..1fa9a717c --- /dev/null +++ b/crates/horizon-port-pack/src/oracle.rs @@ -0,0 +1,384 @@ +//! The NeoForge oracle's facts (`tools/neoforge_oracle.py export`, in +//! `/oracle/facts.json`): every block, item, recipe and tag the pack's +//! mods register, read from the running game, and the vanilla blocks beside +//! them. Carry-over uses them in place of guesses: a block's template is the +//! vanilla block of the same class and states whose shapes and sounds match +//! best, and the declared properties are the mod's own numbers. +use serde::Deserialize; +use serde_json::{Map, Value, json}; +use std::{ + collections::{BTreeMap, BTreeSet}, + path::Path, +}; + +#[derive(Debug, Deserialize)] +pub struct Facts { + pub blocks: BTreeMap, + pub items: BTreeMap, + #[serde(default)] + pub tags: Tags, +} + +#[derive(Debug, Default, Deserialize)] +pub struct Tags { + #[serde(default)] + pub block: BTreeMap>, + #[serde(default)] + pub item: BTreeMap>, +} + +#[derive(Debug, Deserialize)] +pub struct Block { + /// The class and its superclasses, most derived first. + pub class: Vec, + pub properties: BTreeMap>, + pub default: String, + pub item: String, + pub explosion_resistance: f32, + pub friction: f32, + pub speed_factor: f32, + pub jump_factor: f32, + #[serde(default)] + pub block_set_type: Option, + #[serde(default)] + pub wood_type: Option, + pub states: BTreeMap, +} + +#[derive(Debug, Deserialize)] +pub struct SetType { + pub name: String, + pub can_open_by_hand: bool, +} + +#[derive(Debug, Deserialize)] +pub struct State { + pub hardness: f32, + pub emission: u8, + pub requires_tool: bool, + pub map_color_id: u8, + pub sound: Value, + pub outline: Value, + pub collision: Value, +} + +#[derive(Debug, Deserialize)] +pub struct Item { + pub class: String, + pub max_stack_size: u8, + pub rarity: String, + #[serde(default)] + pub block: Option, + #[serde(default)] + pub food: Option, +} + +const VANILLA_PACKAGE: &str = "net.minecraft."; + +/// A state key with its properties sorted, so keys compare across blocks. +pub fn state_key(key: &str) -> String { + let mut parts: Vec<&str> = key.split(',').filter(|p| !p.is_empty()).collect(); + parts.sort_unstable(); + parts.join(",") +} + +impl Block { + /// The first vanilla class in the chain. + pub fn vanilla_class(&self) -> &str { + self.class + .iter() + .find(|c| c.starts_with(VANILLA_PACKAGE)) + .map_or("", String::as_str) + } + /// Its own class is a mod class: behaviour code the template may not have. + pub fn custom_class(&self) -> bool { + self.class + .first() + .is_some_and(|c| !c.starts_with(VANILLA_PACKAGE)) + } + fn default_state(&self) -> Option<&State> { + self.states + .get(&self.default) + .or_else(|| self.states.values().next()) + } + fn same_states(&self, other: &Block) -> bool { + self.properties.len() == other.properties.len() + && self.properties.iter().all(|(k, v)| { + other.properties.get(k).is_some_and(|w| { + v.iter().collect::>() == w.iter().collect::>() + }) + }) + } + fn by_key(&self) -> BTreeMap { + self.states.iter().map(|(k, s)| (state_key(k), s)).collect() + } +} + +/// The template carry-over gives a mod block. +#[derive(Debug, Clone, PartialEq)] +pub struct Choice { + pub template: String, + /// `data//horizon/block/.json`'s `properties`. + pub properties: Map, + /// What the template does not reproduce, for the report. + pub differences: Vec, +} + +impl Facts { + pub fn read(path: &Path) -> Result { + let bytes = std::fs::read(path).map_err(|e| format!("{}: {e}", path.display()))?; + serde_json::from_slice(&bytes).map_err(|e| format!("{}: {e}", path.display())) + } + + /// The vanilla block that best stands in for `id`: the same vanilla + /// class and states, then the most states with the same outline and + /// collision, then the same sounds, set type, wood type and hardness. + /// `None` when no vanilla block has its class and states. + pub fn template(&self, id: &str, allowed: &dyn Fn(&str) -> bool) -> Option { + let block = self.blocks.get(id)?; + let class = block.vanilla_class(); + let own = block.by_key(); + let ours = block.default_state()?; + let mut best: Option<(i64, &str, &Block)> = None; + for (name, v) in &self.blocks { + if !name.starts_with("minecraft:") + || v.vanilla_class() != class + || v.custom_class() + || !block.same_states(v) + || !allowed(name) + { + continue; + } + let theirs = v.by_key(); + let shapes = own + .iter() + .filter(|(k, s)| { + theirs + .get(*k) + .is_some_and(|t| t.outline == s.outline && t.collision == s.collision) + }) + .count() as i64; + let Some(td) = v.default_state() else { + continue; + }; + let same_set = match (&block.block_set_type, &v.block_set_type) { + (Some(a), Some(b)) => { + (a.name == b.name) as i64 * 2 + + (a.can_open_by_hand == b.can_open_by_hand) as i64 * 8 + } + _ => 0, + }; + let score = shapes * 1000 + + (td.sound == ours.sound) as i64 * 100 + + same_set * 10 + + (block.wood_type.is_some() && block.wood_type == v.wood_type) as i64 * 20 + + (td.hardness == ours.hardness) as i64 * 5 + + (td.map_color_id == ours.map_color_id) as i64; + if best.is_none_or(|(s, n, _)| score > s || (score == s && name.as_str() < n)) { + best = Some((score, name, v)); + } + } + let (_, template, t) = best?; + Some(self.choice(block, template, t, allowed)) + } + + fn choice( + &self, + block: &Block, + template: &str, + t: &Block, + allowed: &dyn Fn(&str) -> bool, + ) -> Choice { + let (ours, theirs) = (block.default_state(), t.default_state()); + let mut properties = Map::new(); + let mut differences = Vec::new(); + if let Some(s) = ours { + properties.insert("hardness".into(), json!(s.hardness)); + properties.insert( + "explosion_resistance".into(), + json!(block.explosion_resistance), + ); + properties.insert("light".into(), json!(s.emission)); + properties.insert("requires_tool".into(), json!(s.requires_tool)); + properties.insert("friction".into(), json!(block.friction)); + properties.insert("map_color".into(), json!(s.map_color_id)); + if theirs.is_none_or(|t| t.sound != s.sound) { + match self.sound_block(&s.sound, allowed) { + Some(b) => { + properties.insert("sounds".into(), json!(b)); + } + None => differences.push("sounds: no vanilla block has them".into()), + } + } + } + if (block.speed_factor, block.jump_factor) != (t.speed_factor, t.jump_factor) { + differences.push(format!( + "speed and jump factors {} {} (template {} {})", + block.speed_factor, block.jump_factor, t.speed_factor, t.jump_factor + )); + } + let theirs = t.by_key(); + let mut shapes = 0; + let mut varying = BTreeSet::new(); + let first = ours.map(|s| (s.hardness, s.emission, s.requires_tool, s.map_color_id)); + for (k, s) in block.by_key() { + let Some(v) = theirs.get(&k) else { + continue; + }; + if v.outline != s.outline || v.collision != s.collision { + shapes += 1; + } + if Some((s.hardness, s.emission, s.requires_tool, s.map_color_id)) != first { + varying.insert("per-state hardness, light or map colour"); + } + } + if shapes > 0 { + differences.push(format!( + "shapes differ from {template} in {shapes} of {} states", + block.states.len() + )); + } + differences.extend(varying.into_iter().map(str::to_owned)); + if block.custom_class() { + differences.push(format!( + "class {} (behaviour beyond {})", + block.class[0], + block.vanilla_class() + )); + } + Choice { + template: template.to_owned(), + properties, + differences, + } + } + + /// A vanilla block whose default state has these sounds. + fn sound_block(&self, sound: &Value, allowed: &dyn Fn(&str) -> bool) -> Option<&str> { + self.blocks + .iter() + .filter(|(n, _)| n.starts_with("minecraft:") && allowed(n)) + .find(|(_, b)| b.default_state().is_some_and(|s| &s.sound == sound)) + .map(|(n, _)| n.as_str()) + } + + /// An item declaration for a mod item that is not a declared block's: + /// a vanilla template item with its stack size and rarity, and what it + /// does not reproduce. + pub fn item(&self, id: &str) -> Result<(Value, Vec), String> { + let item = self.items.get(id).ok_or("not in the oracle")?; + if item.block.is_some() { + return Err("a block item without a block".into()); + } + let mut differences = Vec::new(); + if !item.class.starts_with(VANILLA_PACKAGE) { + differences.push(format!("class {} (behaviour beyond Item)", item.class)); + } + if item.food.is_some() { + return Err("food (no food template yet)".into()); + } + let mut decl = json!({"template": "minecraft:stick"}); + if item.max_stack_size != 64 { + decl["max_stack_size"] = json!(item.max_stack_size); + } + if item.rarity != "common" { + decl["rarity"] = json!(item.rarity); + } + Ok((decl, differences)) + } + + /// A tag's members the content mod may name: vanilla's and its own. + pub fn tag_members(&self, kind: &str, tag: &str, own: &str) -> Option> { + let map = if kind == "block" { + &self.tags.block + } else { + &self.tags.item + }; + let members = map.get(tag)?; + Some( + members + .iter() + .filter(|m| { + m.split_once(':') + .is_some_and(|(ns, _)| ns == "minecraft" || ns == own) + }) + .cloned() + .collect(), + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn state(hardness: f32, sound: &str, outline: Value) -> Value { + json!({"hardness": hardness, "emission": 0, "requires_tool": false, "map_color_id": 13, + "sound": {"break": sound}, "outline": outline, "collision": outline}) + } + fn block(class: &[&str], props: Value, states: Value) -> Value { + let default = states + .as_object() + .unwrap() + .keys() + .next() + .unwrap() + .to_owned(); + json!({"class": class, "properties": props, "default": default, "item": "x", + "explosion_resistance": 3.0, "friction": 0.6, "speed_factor": 1.0, + "jump_factor": 1.0, "states": states}) + } + + #[test] + fn templates_follow_class_shapes_and_sounds() { + let cube = json!([[0, 0, 0, 1, 1, 1]]); + let low = json!([[0, 0, 0, 1, 0.9375, 1]]); + let block_class = ["net.minecraft.world.level.block.Block"]; + let path = ["net.minecraft.world.level.block.DirtPathBlock"]; + let facts: Facts = serde_json::from_value(json!({ + "blocks": { + "minecraft:stone": block(&block_class, json!({}), json!({"": state(1.5, "stone", cube.clone())})), + "minecraft:oak_planks": block(&block_class, json!({}), json!({"": state(2.0, "wood", cube.clone())})), + "minecraft:dirt_path": block(&path, json!({}), json!({"": state(0.65, "grass", low.clone())})), + "m:wood_tile": block(&["m.Tile", block_class[0]], json!({}), json!({"": state(2.5, "wood", cube.clone())})), + "m:path": block(&["m.Path", block_class[0]], json!({}), json!({"": state(1.0, "stone", low.clone())})), + "m:gadget": block(&["m.Gadget", block_class[0]], json!({"mode": ["a", "b"]}), + json!({"mode=a": state(1.0, "stone", cube.clone()), "mode=b": state(1.0, "stone", cube)})), + }, + "items": {} + })) + .unwrap(); + let any = |_: &str| true; + let tile = facts.template("m:wood_tile", &any).unwrap(); + assert_eq!(tile.template, "minecraft:oak_planks"); + assert_eq!(tile.properties["hardness"], json!(2.5)); + assert!(!tile.properties.contains_key("sounds")); + assert_eq!( + tile.differences, + ["class m.Tile (behaviour beyond net.minecraft.world.level.block.Block)"] + ); + // A plain Block is never templated on a DirtPathBlock: shapes differ, and say so. + let path = facts.template("m:path", &any).unwrap(); + assert_eq!(path.template, "minecraft:stone"); + assert!( + path.differences[0].starts_with("shapes differ"), + "{:?}", + path.differences + ); + assert_eq!(facts.template("m:gadget", &any), None); + let tile = facts + .template("m:wood_tile", &|n: &str| n != "minecraft:oak_planks") + .unwrap(); + assert_eq!(tile.template, "minecraft:stone"); + assert_eq!(tile.differences[0], "sounds: no vanilla block has them"); + } + + #[test] + fn state_keys_sort() { + assert_eq!( + state_key("half=lower,facing=north"), + "facing=north,half=lower" + ); + assert_eq!(state_key(""), ""); + } +} diff --git a/crates/horizon-port-pack/src/report.rs b/crates/horizon-port-pack/src/report.rs new file mode 100644 index 000000000..ffa025c79 --- /dev/null +++ b/crates/horizon-port-pack/src/report.rs @@ -0,0 +1,428 @@ +//! Step 7, the coverage report: per mod, ported, partial, not yet or not +//! needed, with the reason, and the totals by class, tier and licence +//! (`coverage.json`, `coverage.md`). It reads what the other steps wrote: +//! the inventory, each mod's `carry-report.json` and its harness result +//! (`tools/mod_port_harness.py`). +use crate::inventory::Record; +use serde::{Deserialize, Serialize}; +use serde_json::Value; +use std::{collections::BTreeMap, fmt::Write, path::Path}; + +#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)] +#[serde(rename_all = "kebab-case")] +pub enum Status { + Ported, + Partial, + NotYet, + NotNeeded, +} +impl Status { + pub const ALL: [Status; 4] = [ + Status::Ported, + Status::Partial, + Status::NotYet, + Status::NotNeeded, + ]; + pub fn label(self) -> &'static str { + match self { + Status::Ported => "ported", + Status::Partial => "partial", + Status::NotYet => "not yet", + Status::NotNeeded => "not needed", + } + } +} + +#[derive(Debug, Serialize, Deserialize)] +pub struct Row { + pub key: String, + pub name: String, + pub class: String, + pub tier: String, + pub license: String, + /// `permissive`, `copyleft`, `all-rights-reserved`, `restricted` + /// (non-commercial or no-derivatives), `custom` or `unknown`. + pub license_kind: String, + pub status: Status, + pub reason: String, +} + +/// The licence family of an SPDX id or a `mods.toml` licence text. +pub fn license_kind(license: &str) -> &'static str { + let l = license.to_ascii_lowercase(); + if l.is_empty() || l.starts_with("unknown") { + "unknown" + } else if l.contains("-nc") || l.contains("-nd") || l.contains("noncommercial") { + "restricted" + } else if l.contains("all-rights-reserved") || l.contains("all rights reserved") || l == "arr" { + "all-rights-reserved" + } else if ["gpl", "mpl", "eupl", "general public"] + .iter() + .any(|k| l.contains(k)) + { + "copyleft" + } else if [ + "mit", + "apache", + "bsd", + "cc0", + "cc-by", + "unlicense", + "isc", + "zlib", + ] + .iter() + .any(|k| l.contains(k)) + { + "permissive" + } else { + "custom" + } +} + +/// What still blocks a mod of this tier that has not been carried over. +fn not_yet(tier: &str) -> &'static str { + match tier { + "data" => "data tier: carry-over only, no agent; its jar is not in the work folder", + "logic" => { + "needs mod API 0.3: behaviour callbacks (use, place, break, ticks) and server events" + } + "machines" => "needs mod API 0.4: block entities, menus, capabilities and mod networking", + "deep" => { + "needs mod API 0.5: entities, custom renderers and models, dimensions, new vanilla hooks" + } + "client" => "needs the client interfaces (keys, target, inventory-read, ui, render)", + _ => "not classified", + } +} + +fn not_needed(class: &str) -> &'static str { + match class { + "library" => "library: its API maps to Horizon's mod API and SDK", + "performance" => "performance mod: Horizon's engine is the replacement", + "other" => "pack tooling or an engine feature, folded into the pipeline or the engine", + "client-ui" => "built into Horizon", + _ => "nothing to port", + } +} + +/// A carried-over mod: ported when its harness passes with nothing left +/// out and nothing the templates miss; else partial, with what is missing. +fn carried(work: &Path, key: &str) -> Option<(Status, String)> { + let dir = work.join("mods").join(key); + let carry: Value = + serde_json::from_slice(&std::fs::read(dir.join("carry-report.json")).ok()?).ok()?; + let count = |k: &str| carry[k].as_object().map_or(0, |o| o.len()); + let (blocks, items) = (count("blocks"), carry["items"].as_u64().unwrap_or(0)); + let (left_blocks, left_items) = (count("blocks_unknown"), count("items_unknown")); + // A class of its own is code the facts cannot show; anything else the + // template misses (shapes, sounds) the harness also reports. + let mut differences = 0; + let mut own_code = 0; + for list in carry["differences"] + .as_object() + .into_iter() + .flatten() + .map(|(_, v)| v) + { + let notes: Vec<&str> = list + .as_array() + .into_iter() + .flatten() + .filter_map(Value::as_str) + .collect(); + if notes.iter().any(|n| n.starts_with("class ")) { + own_code += 1; + } + if notes.iter().any(|n| !n.starts_with("class ")) { + differences += 1; + } + } + let mut gaps = Vec::new(); + if left_blocks > 0 { + gaps.push(format!( + "{left_blocks} blocks left out (no vanilla template)" + )); + } + if left_items > 0 { + gaps.push(format!("{left_items} items left out")); + } + if differences > 0 { + gaps.push(format!( + "{differences} blocks or items differ from their templates" + )); + } + let harness: Option = std::fs::read(dir.join("harness/result.json")) + .ok() + .and_then(|b| serde_json::from_slice(&b).ok()); + let mut checked = 0; + match &harness { + Some(h) => { + for (name, c) in h["checks"].as_object().into_iter().flatten() { + let (p, f) = ( + c["passed"].as_u64().unwrap_or(0), + c["failed"].as_u64().unwrap_or(0), + ); + checked += p + f; + if f > 0 { + gaps.push(format!("{name} {p}/{}", p + f)); + } + } + } + None => gaps.push("harness not run".into()), + } + let worldgen = carry["worldgen"].as_u64().unwrap_or(0); + if blocks == 0 && items == 0 && checked == 0 { + let what = if worldgen > 0 { + format!("{worldgen} worldgen files carried, not compared yet") + } else { + "nothing registered to compare".into() + }; + return Some((Status::Partial, what)); + } + let mut summary = format!("{blocks} blocks, {items} items"); + if own_code > 0 { + let _ = write!( + summary, + " ({own_code} with code of their own, not compared)" + ); + } + if gaps.is_empty() { + Some((Status::Ported, format!("{summary}; harness green"))) + } else { + Some((Status::Partial, format!("{summary}; {}", gaps.join("; ")))) + } +} + +pub fn build(records: &[Record], work: &Path) -> Vec { + let mut rows: Vec = records + .iter() + .map(|r| { + let (status, reason) = if r.class == "unknown" { + let why = + "unclassified: its jar (mods.toml, file census) or a reviewed row decides"; + (Status::NotYet, why.to_owned()) + } else if r.tier == "none" || matches!(r.class.as_str(), "library" | "performance") { + (Status::NotNeeded, not_needed(&r.class).to_owned()) + } else if let Some(c) = carried(work, &r.key) { + c + } else { + let mut why = not_yet(&r.tier).to_owned(); + if r.tier == "data" && r.jar.is_some() { + why = "data tier: carry-over not run yet".into(); + } + if r.class == "integration" { + why = format!("after the mods it joins are ported; {why}"); + } + (Status::NotYet, why) + }; + Row { + key: r.key.clone(), + name: r.name.clone(), + class: r.class.clone(), + tier: r.tier.clone(), + license: r.license.clone(), + license_kind: license_kind(&r.license).to_owned(), + status, + reason, + } + }) + .collect(); + // A mod waits on its dependencies that are not ported. + let status: BTreeMap = rows.iter().map(|r| (r.key.clone(), r.status)).collect(); + for (row, r) in rows.iter_mut().zip(records) { + if row.status == Status::NotNeeded { + continue; + } + let waiting: Vec<&str> = r + .depends + .iter() + .filter(|d| status.get(*d) == Some(&Status::NotYet)) + .map(String::as_str) + .collect(); + if !waiting.is_empty() { + let _ = write!(row.reason, "; waits on {}", waiting.join(", ")); + } + } + rows +} + +/// Rows counted by `field` and status. +fn table(rows: &[Row], title: &str, field: fn(&Row) -> &str) -> String { + let mut counts: BTreeMap<&str, [usize; 4]> = BTreeMap::new(); + for r in rows { + let i = Status::ALL.iter().position(|s| *s == r.status).unwrap_or(0); + counts.entry(field(r)).or_default()[i] += 1; + } + let mut out = format!( + "| {title} | ported | partial | not yet | not needed | all |\n|---|---:|---:|---:|---:|---:|\n" + ); + let mut totals = [0usize; 4]; + for (k, c) in &counts { + let all: usize = c.iter().sum(); + let _ = writeln!( + out, + "| {k} | {} | {} | {} | {} | {all} |", + c[0], c[1], c[2], c[3] + ); + for (t, n) in totals.iter_mut().zip(c) { + *t += n; + } + } + let all: usize = totals.iter().sum(); + let _ = writeln!( + out, + "| **all** | **{}** | **{}** | **{}** | **{}** | **{all}** |", + totals[0], totals[1], totals[2], totals[3] + ); + out +} + +pub fn markdown(pack: &str, rows: &[Row]) -> String { + let mut out = format!("# Coverage: {pack}\n\n"); + out += &table(rows, "class", |r| &r.class); + out += "\n"; + out += &table(rows, "tier", |r| &r.tier); + out += "\n"; + out += &table(rows, "licence", |r| &r.license_kind); + out += "\nPorts stay local. Only permissive or copyleft ports could ever be published, and only after Ludovic asks the authors.\n\n"; + out += "| mod | class | tier | licence | status | why |\n|---|---|---|---|---|---|\n"; + let mut sorted: Vec<&Row> = rows.iter().collect(); + sorted.sort_by(|a, b| (a.status, &a.key).cmp(&(b.status, &b.key))); + for r in sorted { + let _ = writeln!( + out, + "| {} | {} | {} | {} | {} | {} |", + r.key, + r.class, + r.tier, + r.license.replace('|', "/"), + r.status.label(), + r.reason.replace('|', "/") + ); + } + out +} + +pub fn counts(rows: &[Row]) -> Vec<(&'static str, usize)> { + Status::ALL + .iter() + .map(|s| (s.label(), rows.iter().filter(|r| r.status == *s).count())) + .collect() +} + +#[cfg(test)] +mod tests { + use super::*; + + fn record(key: &str, class: &str, tier: &str, depends: &[&str]) -> Record { + Record { + key: key.into(), + class: class.into(), + tier: tier.into(), + license: "MIT".into(), + depends: depends.iter().map(|d| (*d).to_owned()).collect(), + ..Record::default() + } + } + + #[test] + fn statuses_and_reasons() { + let work = std::env::temp_dir().join(format!("horizon-report-{}", std::process::id())); + let _ = std::fs::remove_dir_all(&work); + let write = |key: &str, carry: Value, harness: Option| { + let dir = work.join("mods").join(key); + std::fs::create_dir_all(dir.join("harness")).unwrap(); + std::fs::write(dir.join("carry-report.json"), carry.to_string()).unwrap(); + if let Some(h) = harness { + std::fs::write(dir.join("harness/result.json"), h.to_string()).unwrap(); + } + }; + let green = serde_json::json!({"checks": {"blocks": {"passed": 3, "failed": 0}}}); + write( + "doors", + serde_json::json!({"blocks": {"a": "minecraft:oak_door"}, "items": 0, + "differences": {"a": ["class m.Door (behaviour beyond DoorBlock)"]}}), + Some(green), + ); + write( + "paths", + serde_json::json!({"blocks": {"a": "minecraft:stone"}, "items": 1, + "blocks_unknown": {"b": "no vanilla Block"}}), + Some(serde_json::json!({"checks": {"shapes": {"passed": 1, "failed": 2}}})), + ); + let records = [ + record("doors", "content", "data", &[]), + record("paths", "content", "data", &[]), + record("lib", "library", "none", &[]), + record("machine", "content", "machines", &["lib"]), + record("addon", "integration", "logic", &["machine"]), + record("mystery", "unknown", "none", &[]), + ]; + let rows = build(&records, &work); + let by: BTreeMap<&str, &Row> = rows.iter().map(|r| (r.key.as_str(), r)).collect(); + assert_eq!(by["doors"].status, Status::Ported); + assert!( + by["doors"].reason.contains("1 with code of their own"), + "{}", + by["doors"].reason + ); + assert_eq!(by["paths"].status, Status::Partial); + assert!( + by["paths"].reason.contains("1 blocks left out"), + "{}", + by["paths"].reason + ); + assert!( + by["paths"].reason.contains("shapes 1/3"), + "{}", + by["paths"].reason + ); + assert_eq!(by["lib"].status, Status::NotNeeded); + assert_eq!(by["machine"].status, Status::NotYet); + assert!(by["machine"].reason.contains("0.4")); + assert!(by["addon"].reason.starts_with("after the mods it joins")); + assert!( + by["addon"].reason.ends_with("waits on machine"), + "{}", + by["addon"].reason + ); + assert_eq!(by["mystery"].status, Status::NotYet); + assert_eq!( + counts(&rows), + [ + ("ported", 1), + ("partial", 1), + ("not yet", 3), + ("not needed", 1) + ] + ); + let md = markdown("test", &rows); + assert!( + md.contains("| **all** | **1** | **1** | **3** | **1** | **6** |"), + "{md}" + ); + std::fs::remove_dir_all(&work).unwrap(); + } + + #[test] + fn licence_families() { + assert_eq!(license_kind("MIT"), "permissive"); + assert_eq!(license_kind("CC0-1.0"), "permissive"); + assert_eq!(license_kind("LGPL-3.0-only"), "copyleft"); + assert_eq!( + license_kind("GNU Lesser General Public License v3.0"), + "copyleft" + ); + assert_eq!( + license_kind("LicenseRef-All-Rights-Reserved"), + "all-rights-reserved" + ); + assert_eq!(license_kind("CC-BY-NC-SA-4.0"), "restricted"); + assert_eq!(license_kind("LicenseRef-Stardust-Labs-License"), "custom"); + assert_eq!( + license_kind("unknown (read mods.toml at import)"), + "unknown" + ); + } +} diff --git a/crates/horizon-prepare/src/lib.rs b/crates/horizon-prepare/src/lib.rs index f997be326..6e6ce2e11 100644 --- a/crates/horizon-prepare/src/lib.rs +++ b/crates/horizon-prepare/src/lib.rs @@ -762,8 +762,7 @@ fn steps( "behaviour-export.log", )?; - let items = root.join("items.json"); - if !items.exists() || !run.same("server_sha256") { + if datagen_reports_missing(root) || !run.same("server_sha256") { // Vanilla's own data generator reports every item's default components. (run.log)("Running vanilla's data generator reports"); let datagen = run.local.join("datagen"); @@ -785,20 +784,17 @@ fn steps( ) .map_err(|e| format!("{e} in the data generator; see {}", log_path.display()))?; let reports = datagen.join("out").join("reports"); - let mut components = Value::Object(Vec::new()); - for file in json_files(&reports.join("minecraft/components/item"))? { - let stem = file.file_stem().unwrap().to_string_lossy().into_owned(); - let report = read_json(&file)?; - let component = report - .get("components") - .ok_or_else(|| format!("{} has no components", file.display()))?; - components.insert(&format!("minecraft:{stem}"), component.clone()); - } - write_text(&items, &pyjson::dumps(&components, &pyjson::DEFAULT))?; - let biome_parameters = root.join("biome-parameters"); - mkdir(&biome_parameters)?; - for file in json_files(&reports.join("biome_parameters/minecraft"))? { - copy(&file, &biome_parameters.join(file.file_name().unwrap()))?; + install_datagen_reports(root, &reports)?; + } + + // The registry and command reports, which the game reads next to these + // files when it serves players (a dedicated server, a LAN world): kept + // here too, so this folder works when it is copied or mounted alone. + let reports = run.local.join("datagen").join("out").join("reports"); + for name in ["registries.json", "commands.json"] { + let (from, to) = (reports.join(name), root.join(name)); + if from.is_file() && (!to.exists() || !run.same("server_sha256")) { + copy(&from, &to)?; } } @@ -925,6 +921,37 @@ fn steps( ) } +fn datagen_reports_missing(root: &Path) -> bool { + ["items.json", "registries.json", "commands.json"] + .iter() + .any(|name| !root.join(name).is_file()) +} + +fn install_datagen_reports(root: &Path, reports: &Path) -> Result<(), String> { + for name in ["registries.json", "commands.json"] { + copy(&reports.join(name), &root.join(name))?; + } + let mut components = Value::Object(Vec::new()); + for file in json_files(&reports.join("minecraft/components/item"))? { + let stem = file.file_stem().unwrap().to_string_lossy().into_owned(); + let report = read_json(&file)?; + let component = report + .get("components") + .ok_or_else(|| format!("{} has no components", file.display()))?; + components.insert(&format!("minecraft:{stem}"), component.clone()); + } + write_text( + &root.join("items.json"), + &pyjson::dumps(&components, &pyjson::DEFAULT), + )?; + let biome_parameters = root.join("biome-parameters"); + mkdir(&biome_parameters)?; + for file in json_files(&reports.join("biome_parameters/minecraft"))? { + copy(&file, &biome_parameters.join(file.file_name().unwrap()))?; + } + Ok(()) +} + /// `sorted(directory.glob('*.json'))`: glob skips names starting with a dot. fn json_files(directory: &Path) -> Result, String> { let mut files: Vec = fs::read_dir(directory) @@ -1124,6 +1151,54 @@ fn export_block_behavior(run: &Run, export: &Path) -> Result<(), String> { mod tests { use super::*; + #[test] + fn datagen_reports_are_installed_beside_resources_and_missing_reports_need_repair() { + let dir = + std::env::temp_dir().join(format!("horizon-prepare-reports-{}", std::process::id())); + let root = dir.join("resources"); + let reports = dir.join("reports"); + mkdir(&root).unwrap(); + mkdir(&reports.join("minecraft/components/item")).unwrap(); + mkdir(&reports.join("biome_parameters/minecraft")).unwrap(); + write_text(&root.join("items.json"), "{}").unwrap(); + assert!(datagen_reports_missing(&root)); + write_text( + &reports.join("registries.json"), + "{\"minecraft:item\":{\"entries\":{}}}", + ) + .unwrap(); + write_text(&reports.join("commands.json"), "{\"type\":\"root\"}").unwrap(); + write_text( + &reports.join("minecraft/components/item/stone.json"), + "{\"components\":{\"minecraft:max_stack_size\":64}}", + ) + .unwrap(); + write_text( + &reports.join("biome_parameters/minecraft/overworld.json"), + "{}", + ) + .unwrap(); + install_datagen_reports(&root, &reports).unwrap(); + assert!(!datagen_reports_missing(&root)); + for name in ["registries.json", "commands.json"] { + assert_eq!( + read(&root.join(name)).unwrap(), + read(&reports.join(name)).unwrap() + ); + } + assert!(read_json(&root.join("items.json")) + .unwrap() + .get("minecraft:stone") + .is_some()); + assert!(root.join("biome-parameters/overworld.json").is_file()); + for name in ["registries.json", "commands.json", "items.json"] { + fs::remove_file(root.join(name)).unwrap(); + assert!(datagen_reports_missing(&root), "{name}"); + install_datagen_reports(&root, &reports).unwrap(); + } + fs::remove_dir_all(dir).unwrap(); + } + #[test] fn the_source_digest_is_the_one_build_rs_computed() { assert_eq!( diff --git a/crates/horizon-protocol/src/connection.rs b/crates/horizon-protocol/src/connection.rs index 3c6087f2e..4deda020c 100644 --- a/crates/horizon-protocol/src/connection.rs +++ b/crates/horizon-protocol/src/connection.rs @@ -227,6 +227,10 @@ impl Connection { }, }) } + /// `Connection.isMemoryConnection`: through a [`Pipe`] rather than a socket. + pub fn is_memory(&self) -> bool { + matches!(self.reader.stream, Stream::Memory(_)) + } /// ClientboundLoginCompressionPacket: negative thresholds disable compression. pub fn set_compression(&mut self, threshold: i32) { let t = usize::try_from(threshold).ok(); @@ -355,6 +359,8 @@ mod tests { big.extend(std::iter::repeat_n(3u8, 1000)); c.send(&big).unwrap(); assert_eq!(c.recv().unwrap(), (9, vec![1, 2])); + assert_eq!(c.reader.counters.sent.load(Ordering::Relaxed), 1); + assert_eq!(c.reader.counters.received.load(Ordering::Relaxed), 1); server.join().unwrap(); assert!(c.recv().is_err()); } diff --git a/crates/horizon-world/src/level/data.rs b/crates/horizon-world/src/level/data.rs index 455a2ed57..42f8f75de 100644 --- a/crates/horizon-world/src/level/data.rs +++ b/crates/horizon-world/src/level/data.rs @@ -1,6 +1,6 @@ //! Behaviour tables built once from the pinned client resources. use super::{ - Behaviours, + Behaviours, GameplayStateSource, classes::{Class, ClassSet, Interface}, states::{BlockId, PropKey, StateTable}, }; @@ -653,20 +653,37 @@ pub fn load_registry(root: &Path) -> io::Result { } pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result { + let behaviour = read_json(&root.join("block-behaviours.json"))?; + load_with_behaviour_json(registry, root, &behaviour) +} + +pub(super) fn load_with_behaviour_json( + registry: &BlockRegistry, + root: &Path, + behaviour: &Value, +) -> io::Result { + let gameplay = read_json(&root.join("gameplay-facts.json"))?; + let gameplay_states = gameplay["states"] + .as_array() + .ok_or_else(|| fail("missing gameplay states"))?; + load_with_gameplay(registry, root, behaviour, gameplay_states) +} + +pub(super) fn load_with_gameplay( + registry: &BlockRegistry, + root: &Path, + behaviour: &Value, + gameplay_states: &[G], +) -> io::Result { let mut table = StateTable::new(registry)?; let props = Props::resolve(&mut table); let named = Named::resolve(&table)?; - let behaviour = read_json(&root.join("block-behaviours.json"))?; - let gameplay = read_json(&root.join("gameplay-facts.json"))?; let blocks_json = behaviour["blocks"] .as_array() .ok_or_else(|| fail("missing behaviour blocks"))?; let states_json = behaviour["states"] .as_array() .ok_or_else(|| fail("missing behaviour states"))?; - let gameplay_states = gameplay["states"] - .as_array() - .ok_or_else(|| fail("missing gameplay states"))?; if blocks_json.len() != table.block_count() || states_json.len() != table.state_count() || gameplay_states.len() != table.state_count() @@ -798,7 +815,7 @@ pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result io::Result PushReaction::Push, "POPPED" => PushReaction::Popped, "IMMOVEABLE" => PushReaction::Immoveable, @@ -951,23 +964,13 @@ pub(super) fn load(registry: &BlockRegistry, root: &Path) -> io::Result Direction::Up, }; let default = table.default_state(block); - let sound = &gameplay_states[default.0 as usize]["sound"]; - let sound_id = |key: &str, strings: &mut HashMap>| { - interned( - strings, - sound - .get(key) - .and_then(|s| s.get("id")) - .and_then(Value::as_str) - .unwrap_or(""), - ) - }; + let sound = &gameplay_states[default.0 as usize]; let sounds = Arc::new(Sounds { - volume: sound.get("volume").and_then(Value::as_f64).unwrap_or(1.0) as f32, - pitch: sound.get("pitch").and_then(Value::as_f64).unwrap_or(1.0) as f32, - place: sound_id("place", &mut strings), - break_: sound_id("break", &mut strings), - fall: sound_id("fall", &mut strings), + volume: sound.sound_number("volume") as f32, + pitch: sound.sound_number("pitch") as f32, + place: interned(&mut strings, sound.sound_id("place")), + break_: interned(&mut strings, sound.sound_id("break")), + fall: interned(&mut strings, sound.sound_id("fall")), }); blocks.push(BlockData { class, @@ -1145,3 +1148,31 @@ fn instrument_index(name: &str) -> u8 { .position(|n| n.eq_ignore_ascii_case(name)) .map_or(0, |i| i as u8) } + +impl GameplayStateSource for Value { + fn flag(&self, name: &str) -> bool { + self.get(name).and_then(Value::as_bool).unwrap_or(false) + } + fn unsigned(&self, name: &str) -> u64 { + self.get(name).and_then(Value::as_u64).unwrap_or(0) + } + fn number(&self, name: &str) -> f64 { + self.get(name).and_then(Value::as_f64).unwrap_or(0.0) + } + fn text(&self, name: &str) -> &str { + self.get(name).and_then(Value::as_str).unwrap_or("") + } + fn sound_number(&self, name: &str) -> f64 { + self.get("sound") + .and_then(|s| s.get(name)) + .and_then(Value::as_f64) + .unwrap_or(1.0) + } + fn sound_id(&self, name: &str) -> &str { + self.get("sound") + .and_then(|s| s.get(name)) + .and_then(|s| s.get("id")) + .and_then(Value::as_str) + .unwrap_or("") + } +} diff --git a/crates/horizon-world/src/level/interact.rs b/crates/horizon-world/src/level/interact.rs index 791daf7c2..c38d81f26 100644 --- a/crates/horizon-world/src/level/interact.rs +++ b/crates/horizon-world/src/level/interact.rs @@ -513,6 +513,22 @@ impl Behaviours { sim.game_event_state(pos, "minecraft:block_change", next); } + /// The state change of `RedStoneOreBlock.attack` when mining begins. + pub fn attack_block( + &self, + level: &mut L, + ticks: &mut Ticks, + pos: BlockPos, + ) { + let mut sim = self.sim(level, ticks); + let state = sim.state(pos); + if self.data(state).chain.contains(&Class::RedStoneOre) + && !self.table.bool(state, self.props.lit) + { + sim.set_block_and_update(pos, self.table.set_bool(state, self.props.lit, true)); + } + } + /// `BlockState.useWithoutItem` for a right-click on `pos` (hit face and /// absolute hit location as in `BlockHitResult`). pub fn use_without_item( diff --git a/crates/horizon-world/src/level/mod.rs b/crates/horizon-world/src/level/mod.rs index 00dd115db..dbd877fc5 100644 --- a/crates/horizon-world/src/level/mod.rs +++ b/crates/horizon-world/src/level/mod.rs @@ -431,6 +431,17 @@ pub fn raw_brightness(level: &L, pos: BlockPos, sky_dar (level.sky_light(pos) - sky_darken).max(level.block_light(pos)) } +/// Access only the gameplay fields needed by level behaviour tables. +/// Both the JSON fallback and the client's typed decoder implement this. +pub trait GameplayStateSource { + fn flag(&self, name: &str) -> bool; + fn unsigned(&self, name: &str) -> u64; + fn number(&self, name: &str) -> f64; + fn text(&self, name: &str) -> &str; + fn sound_number(&self, name: &str) -> f64; + fn sound_id(&self, name: &str) -> &str; +} + /// Immutable vanilla block-behaviour tables. Build once with [`Behaviours::load`]. pub struct Behaviours { pub table: StateTable, @@ -456,6 +467,23 @@ impl Behaviours { pub fn load(registry: &BlockRegistry, resources_dir: &Path) -> io::Result { data::load(registry, resources_dir) } + /// Reuse the client's typed gameplay projection while it is borrowed from the JSON bytes. + pub fn load_with_gameplay( + registry: &BlockRegistry, + resources_dir: &Path, + behaviour: &serde_json::Value, + gameplay: &[G], + ) -> io::Result { + data::load_with_gameplay(registry, resources_dir, behaviour, gameplay) + } + + pub fn load_with_behaviour_json( + registry: &BlockRegistry, + resources_dir: &Path, + behaviour: &serde_json::Value, + ) -> io::Result { + data::load_with_behaviour_json(registry, resources_dir, behaviour) + } pub fn facts(&self, state: BlockStateId) -> &StateFacts { &self.facts[state.0 as usize] } diff --git a/crates/horizon-world/src/level/tests.rs b/crates/horizon-world/src/level/tests.rs index 270ee48fa..c0ceddc6f 100644 --- a/crates/horizon-world/src/level/tests.rs +++ b/crates/horizon-world/src/level/tests.rs @@ -634,6 +634,27 @@ impl World<'_> { } } +#[test] +fn mining_lights_redstone_ore_until_its_random_tick() { + let mut w = world!(); + let pos = BlockPos::new(0, 1, 0); + for ore in ["redstone_ore", "deepslate_redstone_ore"] { + let unlit = w.id(ore, &[("lit", "false")]); + let lit = w.id(ore, &[("lit", "true")]); + w.put(pos, unlit); + w.b().attack_block(&mut w.level, &mut w.ticks, pos); + assert_eq!(w.get(pos), lit); + w.b().attack_block(&mut w.level, &mut w.ticks, pos); + assert_eq!(w.get(pos), lit); + w.b().random_tick(&mut w.level, &mut w.ticks, pos); + assert_eq!(w.get(pos), unlit); + } + let stone = w.id("stone", &[]); + w.put(pos, stone); + w.b().attack_block(&mut w.level, &mut w.ticks, pos); + assert_eq!(w.get(pos), stone); +} + #[test] fn placement_orients_blocks() { let mut w = world!(); diff --git a/crates/horizon-world/src/nbt.rs b/crates/horizon-world/src/nbt.rs index 9d5b9355a..7e4d76a41 100644 --- a/crates/horizon-world/src/nbt.rs +++ b/crates/horizon-world/src/nbt.rs @@ -97,6 +97,18 @@ impl Default for Limits { } } } +impl Limits { + /// The prepared per-state block tables (`cached-collision.nbt`, + /// `block-properties.nbt`, ...): trusted files with a row per block + /// state, which content mods' states grow past the default caps. + pub fn tables() -> Self { + Self { + max_bytes: 1 << 28, + max_nodes: 16_000_000, + max_depth: 512, + } + } +} #[derive(Clone, Debug, PartialEq, Eq)] pub struct Error { diff --git a/crates/horizon-world/src/worldgen/generator.rs b/crates/horizon-world/src/worldgen/generator.rs index 65d872c85..12dfc10d9 100644 --- a/crates/horizon-world/src/worldgen/generator.rs +++ b/crates/horizon-world/src/worldgen/generator.rs @@ -51,7 +51,7 @@ use std::{ collections::{BTreeMap, BTreeSet, HashMap}, fs, io, path::Path, - sync::{Arc, Mutex}, + sync::{Arc, Mutex, OnceLock}, }; #[derive(Debug)] @@ -184,7 +184,13 @@ pub struct GeneratorData { /// Block shape-update behaviour (`block-behaviours.json` next to the /// worldgen directory) for template placement's neighbor updates. pub(super) behaviours: Option>, + /// Concentric ring positions by seed and dimension generator (see + /// `rings_for`), most recent last. + ring_cache: Mutex>, } +type RingCacheEntry = ((i64, String), Arc>); +/// Generators whose ring positions are kept: the dimensions of a few worlds. +const RING_CACHE_LIMIT: usize = 8; type Bundle = BTreeMap; fn read_bundle(dir: &Path, name: &str) -> Result { let path = dir.join(format!("{name}.json")); @@ -458,8 +464,30 @@ impl GeneratorData { block_tags, biome_tags, feature_resources, + ring_cache: Mutex::new(Vec::new()), })) } + /// The concentric ring positions (strongholds) of the generator for + /// `seed` and the dimension `generator` definition, shared by every + /// generator built for them. They depend on nothing else, so returning + /// to a dimension, which builds its generator again, finds them done. + pub(super) fn rings_for( + &self, + seed: i64, + generator: &Value, + ) -> Arc> { + let key = (seed, generator.to_string()); + let mut cache = self.ring_cache.lock().unwrap_or_else(|e| e.into_inner()); + let rings = match cache.iter().position(|(k, _)| *k == key) { + Some(i) => cache.remove(i).1, + None => Arc::new(OnceLock::new()), + }; + if cache.len() >= RING_CACHE_LIMIT { + cache.remove(0); + } + cache.push((key, rings.clone())); + rings + } pub fn registry(&self) -> &'static BlockRegistry { self.registry } @@ -821,8 +849,9 @@ where .into_iter() .filter_map(|name| pipeline.data.biome_id(name)) .collect(); + let rings = pipeline.data.rings_for(seed, generator); pipeline.structures = - super::generator_structures::StructureState::new(&pipeline.data, &possible)?; + super::generator_structures::StructureState::new(&pipeline.data, &possible, rings)?; } Ok(pipeline) } @@ -1906,6 +1935,55 @@ impl GeneratorWorker { mod tests { use super::*; + /// Generators of one seed and dimension share their ring positions; + /// another seed or generator gets its own, and old entries go. + #[test] + fn ring_positions_are_shared_by_seed_and_dimension() { + let root = std::env::var_os("HORIZON_RESOURCES") + .map(std::path::PathBuf::from) + .unwrap_or_else(|| { + Path::new(env!("CARGO_MANIFEST_DIR")).join("../../.local/horizon-client/26.3") + }); + if !root.join("worldgen/world_presets.json").exists() { + eprintln!("skipping: no worldgen bundle at {}", root.display()); + return; + } + let registry: &'static BlockRegistry = Box::leak(Box::new( + crate::level::data::load_registry(&root).expect("registry"), + )); + let data = GeneratorData::load(&root.join("worldgen"), registry).expect("worldgen data"); + let first = OverworldGenerator::new(data.clone(), 1234).expect("generator"); + let again = OverworldGenerator::new(data.clone(), 1234).expect("generator"); + { + let cache = data.ring_cache.lock().unwrap(); + assert_eq!(cache.len(), 1); + // The cache's handle and the two generators'. + assert_eq!(Arc::strong_count(&cache[0].1), 3); + } + drop((first, again)); + let a = data.rings_for(1, &serde_json::json!({ "settings": "a" })); + assert!(Arc::ptr_eq( + &a, + &data.rings_for(1, &serde_json::json!({ "settings": "a" })) + )); + assert!(!Arc::ptr_eq( + &a, + &data.rings_for(2, &serde_json::json!({ "settings": "a" })) + )); + assert!(!Arc::ptr_eq( + &a, + &data.rings_for(1, &serde_json::json!({ "settings": "b" })) + )); + for seed in 10..10 + RING_CACHE_LIMIT as i64 { + data.rings_for(seed, &serde_json::json!({ "settings": "a" })); + } + assert_eq!(data.ring_cache.lock().unwrap().len(), RING_CACHE_LIMIT); + assert!(!Arc::ptr_eq( + &a, + &data.rings_for(1, &serde_json::json!({ "settings": "a" })) + )); + } + #[test] fn parameter_reports_quantize_like_vanilla() { let report = r#"{"biomes":[{"biome":"minecraft:plains","parameters":{ diff --git a/crates/horizon-world/src/worldgen/generator_blocks.rs b/crates/horizon-world/src/worldgen/generator_blocks.rs index 6085183ec..dc7db7d20 100644 --- a/crates/horizon-world/src/worldgen/generator_blocks.rs +++ b/crates/horizon-world/src/worldgen/generator_blocks.rs @@ -103,14 +103,12 @@ impl BlockBehavior { return Ok(None); } let read = |name: &str| -> Result { - let limits = Limits { - max_bytes: 1 << 28, - ..Limits::default() - }; - Ok(nbt::read_named(&fs::read(dir.join(name))?, limits) - .map_err(|e| resource(format!("{name}: {e:?}")))? - .0 - .tag) + Ok( + nbt::read_named(&fs::read(dir.join(name))?, Limits::tables()) + .map_err(|e| resource(format!("{name}: {e:?}")))? + .0 + .tag, + ) }; let json = |name: &str| -> Result { Ok(serde_json::from_str(&fs::read_to_string(dir.join(name))?)?) diff --git a/crates/horizon-world/src/worldgen/generator_structures.rs b/crates/horizon-world/src/worldgen/generator_structures.rs index 4f804712f..f5487dc33 100644 --- a/crates/horizon-world/src/worldgen/generator_structures.rs +++ b/crates/horizon-world/src/worldgen/generator_structures.rs @@ -31,8 +31,9 @@ pub(super) struct StructureState { /// Structure indices of each decoration step, in registry order. pub(super) by_step: Vec>, starts: Mutex>, - /// Concentric ring positions, computed on first use. - rings: OnceLock, + /// Concentric ring positions, computed on first use and shared with + /// later generators of the same seed and dimension. + rings: Arc>, /// Their biome searches while they are computed. ring_work: OnceLock, } @@ -131,6 +132,7 @@ impl StructureState { pub(super) fn new( data: &Arc, possible_biomes: &BTreeSet, + rings: Arc>, ) -> Result, GeneratorError> { let (Some(resources), Some(raw)) = (&data.structures, &data.structure_resources) else { return Ok(None); @@ -166,7 +168,7 @@ impl StructureState { possible_sets, by_step, starts: Mutex::new(HashMap::new()), - rings: OnceLock::new(), + rings, ring_work: OnceLock::new(), })) } diff --git a/docs/extras.md b/docs/extras.md index 4129a88eb..3c952d5ac 100644 --- a/docs/extras.md +++ b/docs/extras.md @@ -1,6 +1,6 @@ # Horizon extras -Optional conveniences built into the client: zoom, a minimap, an FPS counter, a +Optional conveniences built into the client: fullbright, zoom, a minimap, an FPS counter, a keystroke overlay, auto-reconnect, Mouse Tweaks-style inventory controls and inventory sorting. **Every one is off by default**: a fresh install looks and behaves exactly like vanilla 26.3, and turning an extra off @@ -36,6 +36,11 @@ Horizon category in Controls, and adding one would put a non-vanilla mapping int ## The extras +### Fullbright (`fullbright`, default off) +- Displays blocks and entities with a fully bright lightmap, including in dark caves. +- Turning it off restores the normal lightmap using the current brightness, + time of day and effects. The brightness slider keeps its normal range. + ### Zoom (`zoom`, default off) - Hold the zoom key (default **Z**, unused by every vanilla 26.3 key mapping; C is `key.saveToolbarActivator`) to zoom. The key does nothing while zoom is off. diff --git a/docs/media/issue563-outline-after.png b/docs/media/issue563-outline-after.png new file mode 100644 index 000000000..ac1041b96 Binary files /dev/null and b/docs/media/issue563-outline-after.png differ diff --git a/docs/media/issue563-outline-before.png b/docs/media/issue563-outline-before.png new file mode 100644 index 000000000..aea7743f1 Binary files /dev/null and b/docs/media/issue563-outline-before.png differ diff --git a/docs/modpacks.md b/docs/modpacks.md index 7951ad4d4..1d8609cef 100644 --- a/docs/modpacks.md +++ b/docs/modpacks.md @@ -237,6 +237,11 @@ How the game loads them, without touching vanilla's path: - Loaders that read only `data/minecraft/` (recipes, loot, tags) read every namespace, as vanilla does. With no mods there is only `minecraft`, so nothing changes. - The overlay is cached like the resource-pack overlay and rebuilt when a mod file changes. +- **Limits.** Chunk sections store a block state in 16 bits: vanilla 26.3 has 35,723 states, + so mods may add 29,813. Past that the content root is refused with the states per mod + (Macaw's Doors, Fences, Paths and Trapdoors alone add 36,154: not all four fit). Block + sprites fill texture arrays of 2048 layers, four at most (8,192; vanilla takes 1,250, + identical textures share a layer). Wider state ids are the next step for big packs. - Not in 0.2: multiplayer registry sync (single player and the integrated server first), worlds opened without a mod they used (vanilla turns unknown blocks into air; we will ask first), vanilla ids redefined by a mod (refused). @@ -262,12 +267,14 @@ inspected: └── coverage.json, coverage.md ``` -**Status.** `import`, `inventory` and `carry` run today: +**Status.** `import`, `inventory`, `carry` and `report` run today: ``` cargo run -p horizon-port-pack -- import ~/Downloads/pack.mrpack ~/.horizon/ports/pack cargo run -p horizon-port-pack -- inventory ~/.horizon/ports/pack # --offline reuses modrinth.json cargo run -p horizon-port-pack -- carry ~/.horizon/ports/pack # content mods with a jar, or name them +# --resources : only templates Horizon's tables have +cargo run -p horizon-port-pack -- report ~/.horizon/ports/pack # coverage.json and coverage.md ``` `inventory` reads metadata only: Modrinth answers by file hash, and the jars only if the user's @@ -277,14 +284,26 @@ classes come from the reviewed table, Modrinth categories and sides; with jars, only): content 54, library 33, client-ui 26, performance 25, other 14, unknown 2, server-only 1; 40 rows matched the reviewed ATM10 table; three dependency layers (93 / 57 / 5). -`carry` copies a jar's `assets//`, `data//` and `data/minecraft/tags/` into -`mods//carry/`, a folder content mod (API 0.2) the game loads with `--dev-mod`, and keeps -what a content mod may not hold (other namespaces such as `c` tags, vanilla data) in `outside/`. -No class file is copied. It upgrades vanilla-type recipes from 1.21.1 ingredients to 26.3's, -adds the `items/` definitions 1.21.4 introduced, and declares each block from a vanilla template -guessed from its blockstate properties (stone for none, a door for `facing, half, hinge, open, -powered`, ...); blocks with an unknown property set, custom recipe types and NeoForge -conditions are listed in `carry-report.json` for the oracle step. +`carry` copies a jar's `assets//`, `data//`, `data/minecraft/tags/` and `data/c/tags/` +into `mods//carry/`, a folder content mod (API 0.2) the game loads with `--dev-mod`, and +keeps what a content mod may not hold (other namespaces, vanilla data) in `outside/`. No class +file is copied. It upgrades from 1.21.1 to 26.3: vanilla-type recipes (ingredients), loot tables +(`conditions` lists become one `condition`, `functions` become `modifier`, kinds move to `type`, +tool matches on silk touch or shears become `minecraft:tool/can_silk_touch` and +`minecraft:tool/can_shear`), and the `items/` definitions 1.21.4 introduced. + +Blocks and items come from the NeoForge oracle when `/oracle/facts.json` exists (step 6): +every block the mod registers gets the vanilla template of the same vanilla class and states +whose shapes, sounds, set type and wood type match best, with the mod's own hardness, blast +resistance, light, tool need, friction, map colour and sounds as overrides. `--resources +` limits templates to blocks Horizon's tables have. `carry-report.json` lists, per +block, what the template does not reproduce (other shapes, sounds no vanilla block has, a class +of its own), and the blocks left out: those whose vanilla class has no vanilla block with the +same states (Macaw's garage doors, multi-part gates). Recipes get the convention tags they use +(`c:dyes/red`), filled from the oracle with vanilla's and the mod's own members. Without the +oracle, templates are guessed from the blockstate's properties: the template whose properties +include them with the fewest left over (models often ignore `powered` or `waterlogged`), the +block's name breaking ties (`_pane`, `_gate`, `_trapdoor`). ### 1. Import @@ -294,6 +313,7 @@ conditions are listed in `carry-report.json` for the oracle step. | CurseForge zip | `manifest.json`: `minecraft.version`, `modLoaders[]`, `files[]` (`projectID`, `fileID`, `required`), `overrides` | the user's installed instance (CurseForge app `minecraftinstance.json` + `mods/`). CurseForge's API needs a key, so we take what the user already downloaded | | Prism / MultiMC instance | `mmc-pack.json` components (`net.minecraft`, `net.neoforged`, `net.fabricmc.fabric-loader`), `instance.cfg`, `.minecraft/mods/*.jar`, packwiz `.pw.toml` files if present | the instance folder | | packwiz | `pack.toml`, `index.toml`, `mods/*.pw.toml` | as listed | +| Crash Assistant `modlist.json` | jar name → mod id, name, version, Modrinth SHA-1; the loader entry (NeoForge `21.1.x` is Minecraft 1.21.1). Packs such as ATM10 publish it in their repository | metadata only; jars put in `/jars/` | `pack.json` holds the Minecraft version, the loader and its version, each file with its hashes and side, and the overrides (configs, KubeJS scripts, datapacks). @@ -386,8 +406,51 @@ A **harness per mod**, generated from the tier: | deep | entity scenarios (spawn, tick N, positions and states, drops), and render captures where the look is the point, compared as images with a tolerance | The comparison skips vanilla 1.21.1-versus-26.3 differences: it normalises ids and components -through the step-4 upgrader and compares only the mod's own ids and effects. The harness format -is `tools/parity.py`'s, so the same runner, local or CI, runs vanilla and mod harnesses. +through the step-4 upgrader and compares only the mod's own ids and effects. + +**Status: the data-tier harness runs.** + +``` +python3 tools/neoforge_oracle.py export --jars --deps --out /oracle/facts.json +cargo run -p horizon-port-pack -- carry --resources .local/horizon-client/26.3 +python3 tools/mod_port_harness.py --work # or --jars/--deps to export first +``` + +- `tools/neoforge_oracle.py` installs a NeoForge server (21.1.259, ATM10's) under + `.local/neoforge-oracle/`, builds the exporter mod in `mods/horizon/src/neoforge-oracle/` (our code, dev + only), starts the server headless with the jars and writes, for every block of the mods' + namespaces and every vanilla block: the class chain, states, per-state hardness, light, tool + need, map colour, sounds, outline and collision shapes; drops for hand, diamond tools, shears + and silk touch; placement on a floor and a wall; BlockSetType and WoodType. Then items (class, + stack size, rarity, food), recipes (type, result, count, resolved ingredients) and tags. One run + with the seven mods below takes about 30 s. +- `crates/horizon-client/examples/mod_port_probe.rs` builds the content root from the carried + mods and dumps what Horizon's loaders make of the same blocks, drops, recipes and tags. +- `tools/mod_port_harness.py` compares them per mod (blocks, properties, sounds, shapes, drops, + placement against the template's in the oracle, vanilla tags, recipes, items) and writes + `/mods//harness/result.json`. It maps 1.21.1 sound events to 26.3's through the + vanilla blocks both versions have (`block.metal.*` became `block.iron.*`) and ignores vanilla + items 1.21.1 did not have (pale oak in `#minecraft:logs`). + +The oracle needs the mods' jars, so it is not a `tools/parity.py` harness: it runs where the +user's jars are, and its facts and results stay in the work folder. The first real mods +(ATM10's own jars from Modrinth, all MIT; checks passed / compared): + +| mod | blocks | properties | sounds | shapes | drops | placement | tags | recipes | items | +|---|---:|---:|---:|---:|---:|---:|---:|---:|---:| +| Macaw's Trapdoors 1.1.5 | 179/179 | 11456/11456 | 11456/11456 | 11456/11456 | 10024/10024 | 358/358 | 534/534 | 191/191 | 191/191 | +| Macaw's Doors 1.1.5 | 239/245 | 15296/15296 | 15296/15296 | 12352/15296 | 13384/13384 | 478/478 | 941/941 | 257/257 | 251/257 | +| Macaw's Fences and Walls 1.2.1 | 150/180 | 4800/4800 | 4800/4800 | 4032/4800 | 33600/33600 | 300/300 | 432/432 | 222/222 | 150/180 | +| Macaw's Paths and Pavings 1.1.1 | 234/315 | 4602/4602 | 4602/4602 | 4472/4602 | 6734/6734 | 468/468 | 416/416 | 315/315 | 234/315 | +| Glassential-renewed 3.4.7 | 105/114 | 5177/5177 | 5177/5177 | 4727/5177 | 6447/6447 | 210/210 | 105/105 | 143/143 | 106/115 | +| Factory Blocks 1.4.0 | 42/49 | 42/42 | 0/42 | 42/42 | 294/294 | 84/84 | 42/42 | 1/1 | 42/49 | +| Cloud Glass 1.1.0 | 16/16 | 16/16 | 16/16 | 0/16 | 112/112 | 32/32 | – | 31/31 | 16/16 | + +What fails is what API 0.2 cannot express yet: shapes of their own (Japanese doors, hedges, +paths a sixteenth high, glass without collision), sound events of the mod's own +(`factory_blocks:metal_sound`), and blocks whose vanilla class has no vanilla block with the +same states (garage doors, double gates, engraved and facing paths, fans). Every drop, recipe +and tag of the declared blocks matches. ### 7. Coverage report @@ -402,16 +465,46 @@ is `tools/parity.py`'s, so the same runner, local or CI, runs vanilla and mod ha Plus the licence summary (how many ports could be published) and, for a pack, "playable?": whether every required content mod is ported or partial with its blocks and items present. -For ATM10 today: 125 of 495 are not needed. The other 370 are not yet ported; 68 of them are -data tier, so they need registries and carry-over but no agent. + +`horizon-port-pack report ` writes the per-mod rows and the class, tier and licence totals from the inventory, each mod's `carry-report.json` +and its harness result. A data-tier mod is **ported** when its harness is green and nothing was +left out (blocks with a class of their own are counted and named, since facts cannot show +their extra code); **partial** with the failing checks, the blocks left out and what the +templates miss; **not yet** with the API tier it waits for, or the dependency; **not needed** +for libraries, performance mods, pack tooling and what Horizon has built in. Unclassified mods +(the 20 CurseForge-only rows the reviewed table only guesses) count as not yet. + +**ATM10, measured** (`import` of ATM10's `modlist.json` from its repository on 2026-10-16, 496 +files; `inventory`; `carry` and the harness on the eight mods we downloaded; `report`): + +| class | ported | partial | not yet | not needed | all | +|---|---:|---:|---:|---:|---:| +| content | 1 | 7 | 268 | 0 | 276 | +| library | 0 | 0 | 0 | 77 | 77 | +| integration | 0 | 0 | 40 | 3 | 43 | +| client-ui | 0 | 0 | 27 | 2 | 29 | +| other | 0 | 0 | 1 | 23 | 24 | +| unknown | 0 | 0 | 20 | 0 | 20 | +| performance | 0 | 0 | 0 | 16 | 16 | +| server-only | 0 | 0 | 11 | 0 | 11 | +| **all** | **1** | **7** | **367** | **121** | **496** | + +By tier, the 367 not yet: data 57 (carry-over and the harness, no agent: their jars were not +downloaded), logic 66 (API 0.3), machines 97 (0.4), deep 92 (0.5), client 35, unclassified 20. +Ported: Macaw's Trapdoors. Partial: Macaw's Doors, Fences and Walls, Paths and Pavings, +Glassential, Factory Blocks, Cloud Glass (see the harness table) and MVS, whose 784 worldgen +files are carried but not yet read by the game. Of the 8, 7 are MIT and one LGPL; nothing is +published. ## Measuring "10x" `tools/perf_bench.py` already measures Horizon against vanilla and vanilla with performance -mods on the same machine, world and camera path ([benchmark.md](benchmark.md)). It gains -`--game pack --pack `: the pack on its own loader (NeoForge 21.1.259 for -ATM10), launched with the pack's recommended JVM arguments, driven by the same dev-only bench -mod, ported to that loader. +mods on the same machine, world and camera path ([benchmark.md](benchmark.md)). With +`--game neoforge --mod-jars --deps ` it runs the mods on their own loader (NeoForge +21.1.259 for ATM10, installed under `.local/neoforge-oracle`, `-Xmx2G` like the launcher), +driven by the same steps through a dev-only NeoForge port of the bench mod +(`mods/horizon/src/neoforge-bench`). `--game horizon --port-work ` loads a work folder's +carried-over mods into Horizon. A whole pack (`--pack`, its recommended JVM arguments) is next. | Metric | How | Horizon side | |---|---|---| @@ -434,6 +527,37 @@ confidence interval. Other ratios are reported as they come out. Caveat (in ever the pack runs Minecraft 1.21.1 and Horizon runs 26.3, so the vanilla parts differ by a few versions. +### Measured: six ported mods against the same mods on NeoForge + +Cloud Glass, Factory Blocks, Glassential, Macaw's Fences, Paths and Trapdoors (Doors left out: +with Trapdoors it passes the block states chunks hold, see Limits), carried over and loaded as +dev mods in Horizon, against their jars plus Architectury and Fusion on NeoForge 21.1.259. +Mac mini M4 Pro, 1280x720, seed 1234, the four flight scenes, 2 games per row, 2026-10-17. +Means; FPS is the mean of the four scenes. "Title" is start to title screen, "world" start to +a fresh world drawn around the spawn, "scene load" a teleport to every chunk drawn. + +| RD | game | title s | world s | scene load s | FPS | RSS steady / peak MB | load avg | +|---|---|---:|---:|---:|---:|---:|---:| +| 8 | Horizon + 6 mods | 9.8 | 16.2 | 4.0 | 122 | 2,644 / 2,771 | 32 | +| 8 | NeoForge + 6 mods | 19.5 | 50.0 | 30.2 | 72 | 2,325 / 2,592 | 56 | +| 8 | Horizon | 5.0 | 11.0 | 4.0 | 124 | 1,559 / 1,629 | 51 | +| 8 | NeoForge | 12.8 | 36.1 | 21.9 | 104 | 2,334 / 2,558 | 30 | +| 16 | Horizon + 6 mods | 10.8 | 27.0 | 11.3 | 90 | 2,925 / 3,032 | 40 | +| 16 | NeoForge + 6 mods | 26.4 | 82.5 | 64.3 | 25 | 3,016 / 3,519 | 45 | +| 16 | Horizon | 3.5 | 10.0 | 7.4 | 119 | 1,732 / 1,871 | 56 | +| 16 | NeoForge | 16.9 | 73.7 | 43.2 | 36 | 2,748 / 3,425 | 48 | + +Same content, Horizon against NeoForge: the world is ready 3.1x sooner at 8 chunks and 3.1x at +16, the title screen 2.0x and 2.4x sooner, and the flights run 1.7x and 3.6x the frames. RAM is +not lower: Horizon's steady RSS grows 1.1 GB with the mods (61,816 block states against +vanilla's 35,723; the per-state tables are the next thing to measure), while the JVM stays +inside its 2 GB heap. Caveats, so none of this is a "10x" claim yet: other sessions and CI +runners kept the machine's load average between 30 and 56 on 14 threads, so the confidence +intervals are as wide as the means (`perf_bench.py --table`); Horizon's frames are paced near +the display's 144 Hz at 8 chunks; Horizon's title time includes building the content overlay on +a first start (about 5 s, cached afterwards); and the pack runs 1.21.1 against Horizon's 26.3. +A quiet rerun with more games is the next measurement. + ## The working version for 2026-10-23 In scope (each is a PR or a small stack): @@ -459,7 +583,11 @@ In scope (each is a PR or a small stack): 6. **Measured:** load time, RAM and FPS of that pack on NeoForge 1.21.1 against Horizon, with the full ATM10 on NeoForge as the reference row. -Out of scope for that date, and next in order: worldgen carry-over (the 30 or so structure and biome mods), +Out of scope for that date, and next in order: worldgen carry-over (the 30 or so structure and biome mods; +the first one looked at, Moog's Voyager Structures, declares all 130 structures and 116 structure +sets with MoogsStructureLib's own `generic_jigsaw_structure` and `advanced_random_spread` types and +NeoForge's `c:` biome tags, so its carry needs a mapping to vanilla's jigsaw and random spread +types plus the oracle's biome tags, not only bundle merging), behaviour callbacks and server events (0.3), block entities, menus, capabilities and net (0.4), the first agent-ported logic-tier mod as the pilot of step 5, then machines. Porting all of ATM10 is a campaign. Each step publishes its coverage number; we make no claim beyond what diff --git a/docs/mods.md b/docs/mods.md index 6203c4066..d1556abd7 100644 --- a/docs/mods.md +++ b/docs/mods.md @@ -277,12 +277,15 @@ blocks and items by declaring them in `data//horizon/block/*.json` and ``` The fence: files go in the mod's own namespace (`assets//`, -`data//`), plus additions to vanilla tags (`data/minecraft/tags/`). +`data//`), plus additions to vanilla tags (`data/minecraft/tags/`) and +to the convention tags NeoForge and Fabric mods share (`data/c/tags/`). Templates and `sounds` name vanilla blocks; unknown keys are refused. Paths use vanilla's resource characters, at most 65,536 files, 16 MiB each and 512 MiB in all. A mod whose content is refused stays off, with the reason on -its row. The game reading this content into its block and item tables comes -next (docs/modpacks.md). +its row. The game reads this content into its block and item tables +(`horizon_client::mod_content`, docs/modpacks.md). A tag entry naming an id +nobody registers (a block a port left out, another mod's item) is dropped, +so the rest of the tag still loads; vanilla would drop the whole tag. ## Versions and compatibility diff --git a/launcher/src/backend.rs b/launcher/src/backend.rs index 898364040..4f5d00ca0 100644 --- a/launcher/src/backend.rs +++ b/launcher/src/backend.rs @@ -583,6 +583,7 @@ fn reuse_prepared(versions: &Path, tag: &str) -> Option { /// `reuse_prepared` for every installed version that is not prepared, oldest first, so a /// version prepared this way can serve the next. Launchers before 0.1.4 installed new versions /// without it; this catches those up. Returns each (version, where its resources came from). +/// Prepared versions also get the run-time reports they lack (`complete_reports`). pub fn reuse_prepared_everywhere() -> Vec<(String, String)> { reuse_prepared_in(&versions_dir()) } @@ -590,9 +591,72 @@ pub fn reuse_prepared_everywhere() -> Vec<(String, String)> { fn reuse_prepared_in(versions: &Path) -> Vec<(String, String)> { let mut tags = installed_in(versions); tags.reverse(); - tags.into_iter() - .filter_map(|tag| reuse_prepared(versions, &tag).map(|from| (tag, from))) - .collect() + let reused = tags + .iter() + .filter_map(|tag| reuse_prepared(versions, tag).map(|from| (tag.clone(), from))) + .collect(); + for tag in &tags { + complete_reports(versions, tag); + } + reused +} + +/// The data generator reports the game reads at run time: the built-in registries' network ids +/// (without them no server can be joined or hosted) and the command tree. +const RUNTIME_REPORTS: [&str; 2] = ["registries.json", "commands.json"]; + +/// Puts the data generator reports the game reads at run time (`RUNTIME_REPORTS`) into a prepared version's +/// resources when they are missing. Preparers before they shipped there left them only in the +/// scratch output of the version that ran the data generator (`.local/datagen/out/reports`), +/// and resources copied from another version (`reuse_prepared`) came without them. They depend +/// only on the server jar, so the version's own scratch output or any installed version pinning +/// the same jar serves, and no new preparation is needed. Returns whether all are there. +fn complete_reports(versions: &Path, tag: &str) -> bool { + let target = versions.join(tag); + let to = resources_at(&target); + if !to.join(RESOURCES_MANIFEST).exists() { + return false; + } + let missing: Vec<_> = RUNTIME_REPORTS + .iter() + .filter(|name| !to.join(name).is_file()) + .collect(); + if missing.is_empty() { + return true; + } + let server = |dir: &Path| { + read_json(&dir.join("research-lock.json")).and_then(|lock| { + let pins = ["version", "server_sha1"].map(|k| lock["minecraft"][k].clone()); + pins.iter().all(|v| v.is_string()).then_some(pins) + }) + }; + let Some(pins) = server(&target) else { + return false; + }; + let reports = |dir: &Path| dir.join(".local/datagen/out/reports"); + let mut sources = vec![reports(&target)]; + for other in installed_in(versions).into_iter().filter(|t| t != tag) { + let dir = versions.join(&other); + if server(&dir).as_ref() == Some(&pins) { + sources.push(resources_at(&dir)); + sources.push(reports(&dir)); + } + } + let mut complete = true; + for name in missing { + let Some(from) = sources.iter().map(|s| s.join(name)).find(|p| p.is_file()) else { + complete = false; + continue; + }; + // Copied beside it and renamed: an interrupted copy never looks complete. + let partial = to.join(format!(".{name}.partial-{}", std::process::id())); + let copied = fs::copy(&from, &partial).and_then(|_| fs::rename(&partial, to.join(name))); + if copied.is_err() { + let _ = fs::remove_file(&partial); + complete = false; + } + } + complete } /// The official launcher's game directory, else the first other launcher's found @@ -1113,6 +1177,7 @@ pub fn server_dir(requested: Option) -> PathBuf { fn need_prepared(tag: &str) -> Result<(), String> { if is_prepared(tag) { + complete_reports(&versions_dir(), tag); Ok(()) } else { Err(format!( @@ -2092,6 +2157,65 @@ mod tests { let _ = fs::remove_dir_all(&versions); } + #[test] + fn prepared_versions_get_the_run_time_reports_without_preparing_again() { + let versions = std::env::temp_dir().join(format!( + "horizon-launcher-reports-test-{}", + std::process::id() + )); + let _ = fs::remove_dir_all(&versions); + let write = |tag: &str, path: &str, text: &str| { + let path = versions.join(tag).join(path); + fs::create_dir_all(path.parent().unwrap()).unwrap(); + fs::write(path, text).unwrap(); + }; + let release = |tag: &str, server: &str| { + write(tag, COMPLETE, ""); + let lock = serde_json::json!({"minecraft": {"version": "26.3", "client_sha1": "c", "server_sha1": server}}); + write(tag, "research-lock.json", &lock.to_string()); + write( + tag, + &format!(".local/horizon-client/26.3/{RESOURCES_MANIFEST}"), + "{}", + ); + }; + let resources = |tag: &str| resources_at(&versions.join(tag)); + let datagen = |tag: &str, name: &str| { + write(tag, &format!(".local/datagen/out/reports/{name}"), tag); + }; + + // Prepared before the reports shipped with the resources: its own scratch output. + release("v2026.10.13", "s"); + datagen("v2026.10.13", "registries.json"); + datagen("v2026.10.13", "commands.json"); + // Resources copied from it: the reports come from the version that ran the generator. + release("v2026.10.14", "s"); + // Another server jar: the reports of the others would be wrong, and it has none. + release("v2026.10.15", "other"); + assert!(reuse_prepared_in(&versions).is_empty()); + for tag in ["v2026.10.13", "v2026.10.14"] { + for name in RUNTIME_REPORTS { + assert_eq!( + fs::read_to_string(resources(tag).join(name)).unwrap(), + "v2026.10.13" + ); + } + } + assert!(!resources("v2026.10.15").join("registries.json").exists()); + assert!(!complete_reports(&versions, "v2026.10.15")); + // Once there they stay as they are, even when the scratch output is gone. + fs::remove_dir_all(versions.join("v2026.10.13/.local/datagen")).unwrap(); + assert!(complete_reports(&versions, "v2026.10.13")); + let left: Vec<_> = fs::read_dir(resources("v2026.10.14")) + .unwrap() + .flatten() + .map(|e| e.file_name().into_string().unwrap()) + .filter(|n| n.contains("partial")) + .collect(); + assert!(left.is_empty()); + let _ = fs::remove_dir_all(&versions); + } + #[test] fn finds_the_app_bundle_around_the_binary() { let exe = Path::new("/Applications/Horizon.app/Contents/MacOS/horizon-launcher"); diff --git a/mods/horizon/src/neoforge-bench/java/dev/horizonlab/neoforgebench/NeoForgeBench.java b/mods/horizon/src/neoforge-bench/java/dev/horizonlab/neoforgebench/NeoForgeBench.java new file mode 100644 index 000000000..5992ed27a --- /dev/null +++ b/mods/horizon/src/neoforge-bench/java/dev/horizonlab/neoforgebench/NeoForgeBench.java @@ -0,0 +1,275 @@ +package dev.horizonlab.neoforgebench; + +import com.google.gson.JsonArray; +import com.google.gson.JsonObject; +import com.google.gson.JsonParser; +import com.mojang.blaze3d.platform.GlUtil; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.StandardOpenOption; +import java.util.ArrayList; +import java.util.List; +import net.minecraft.client.Minecraft; +import net.minecraft.client.gui.screens.TitleScreen; +import net.minecraft.client.player.LocalPlayer; +import net.minecraft.world.Difficulty; +import net.minecraft.world.level.GameRules; +import net.minecraft.world.level.GameType; +import net.minecraft.world.level.LevelSettings; +import net.minecraft.world.level.WorldDataConfiguration; +import net.minecraft.world.level.levelgen.WorldOptions; +import net.minecraft.world.level.levelgen.presets.WorldPresets; +import net.minecraft.world.phys.Vec3; +import net.neoforged.api.distmarker.Dist; +import net.neoforged.fml.common.Mod; +import net.neoforged.neoforge.client.event.RenderFrameEvent; +import net.neoforged.neoforge.common.NeoForge; + +/** + * Dev-only benchmark driver for a NeoForge 1.21.1 client (tools/perf_bench.py + * --game neoforge): the steps of mods/horizon/src/bench's VanillaBench, on + * NeoForge's events. From the title screen it creates a fresh creative world + * with the script's seed, waits for the spawn area, then per scene teleports, + * waits for the chunks, settles and flies a straight line at constant speed, + * recording every frame time. Events go to horizon.bench.out as JSON lines; + * the game quits after the last scene. Never shipped. + */ +@Mod(value = "horizon_bench", dist = Dist.CLIENT) +public final class NeoForgeBench { + private enum Phase { TITLE, WORLD, TELEPORT, LOAD, SETTLE, FLY, DONE } + + private final JsonObject script; + private final Path out; + private final List scenes = new ArrayList<>(); + private Phase phase = Phase.TITLE; + private int scene = -1; + private long phaseStart; + private long lastFrame; + private long readyCheck; + private int lastLoaded = -1; + private long lastChange; + private final List frames = new ArrayList<>(); + + public NeoForgeBench() { + String scriptPath = System.getProperty("horizon.bench.script"); + String outPath = System.getProperty("horizon.bench.out"); + if (scriptPath == null || outPath == null) { + script = null; + out = null; + return; + } + try { + script = JsonParser.parseString(Files.readString(Path.of(scriptPath))).getAsJsonObject(); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + out = Path.of(outPath); + for (var element : script.getAsJsonArray("scenes")) scenes.add(element.getAsJsonObject()); + NeoForge.EVENT_BUS.addListener(RenderFrameEvent.Pre.class, e -> step(Minecraft.getInstance())); + event("init"); + } + + private void step(Minecraft mc) { + long now = System.nanoTime(); + float frameMs = lastFrame == 0 ? 0 : (now - lastFrame) / 1e6f; + lastFrame = now; + double elapsed = (now - phaseStart) / 1e9; + if (phase != Phase.TITLE && phase != Phase.DONE && elapsed > script.get("timeout").getAsDouble() + && phase != Phase.FLY && phase != Phase.SETTLE) { + JsonObject e = new JsonObject(); + e.addProperty("phase", phase.name()); + event("timeout", e); + finish(mc); + return; + } + switch (phase) { + case TITLE -> { + if (mc.screen instanceof TitleScreen && mc.getOverlay() == null) { + event("title", renderer(mc)); + LevelSettings settings = new LevelSettings(script.get("world").getAsString(), GameType.CREATIVE, + false, Difficulty.NORMAL, true, new GameRules(), WorldDataConfiguration.DEFAULT); + WorldOptions options = new WorldOptions(script.get("seed").getAsLong(), true, false); + enter(Phase.WORLD, now); + mc.createWorldOpenFlows().createFreshLevel(script.get("world").getAsString(), settings, options, + WorldPresets::createNormalWorldDimensions, mc.screen); + } + } + case WORLD -> { + if (ready(mc, null, now)) { + JsonObject e = new JsonObject(); + e.addProperty("ready_epoch_ms", epochMs(lastChange)); + e.addProperty("chunks", lastLoaded); + event("world", e); + mc.player.connection.sendCommand("weather clear"); + next(mc, now); + } + } + case TELEPORT -> { + JsonObject s = scenes.get(scene); + Vec3 from = vec(s.getAsJsonArray("from")); + LocalPlayer player = mc.player; + player.connection.sendCommand("time set 6000"); + player.connection.sendCommand(String.format(java.util.Locale.ROOT, "tp @s %.3f %.3f %.3f %.3f %.3f", + from.x, from.y, from.z, yaw(s), s.get("pitch").getAsFloat())); + event("teleport", named(s)); + enter(Phase.LOAD, now); + } + case LOAD -> { + JsonObject s = scenes.get(scene); + Vec3 from = vec(s.getAsJsonArray("from")); + if (mc.player != null && mc.player.position().distanceToSqr(from) < 1024.0) place(mc, s, 0); + if (ready(mc, from, now)) { + JsonObject e = named(s); + e.addProperty("load_ms", (lastChange - phaseStart) / 1e6); + e.addProperty("chunks", lastLoaded); + event("loaded", e); + enter(Phase.SETTLE, now); + } + } + case SETTLE -> { + place(mc, scenes.get(scene), 0); + if (elapsed >= script.get("settle").getAsDouble()) { + frames.clear(); + event("fly", named(scenes.get(scene))); + enter(Phase.FLY, now); + } + } + case FLY -> { + frames.add(frameMs); + double t = Math.min(1.0, elapsed / script.get("seconds").getAsDouble()); + place(mc, scenes.get(scene), t); + if (t >= 1.0) { + JsonObject e = named(scenes.get(scene)); + JsonArray times = new JsonArray(); + frames.forEach(times::add); + e.add("frame_ms", times); + e.addProperty("chunks", mc.level.getChunkSource().getLoadedChunksCount()); + event("flown", e); + next(mc, now); + } + } + case DONE -> { } + } + } + + private void next(Minecraft mc, long now) { + scene++; + if (scene >= scenes.size()) { + finish(mc); + } else { + enter(Phase.TELEPORT, now); + } + } + + private void finish(Minecraft mc) { + phase = Phase.DONE; + event("done"); + mc.stop(); + } + + private void enter(Phase next, long now) { + phase = next; + phaseStart = now; + lastLoaded = -1; + lastChange = now; + } + + /** As VanillaBench.ready: spawned (near {@code at}), no screen, every chunk around, nothing to compile, 1 s stable. */ + private boolean ready(Minecraft mc, Vec3 at, long now) { + if (now - readyCheck < 10_000_000L) return false; + readyCheck = now; + if (mc.level == null || mc.player == null || mc.screen != null || mc.getOverlay() != null + || (at != null && mc.player.position().distanceToSqr(at) > 4.0)) { + lastChange = now; + lastLoaded = -1; + return false; + } + int loaded = mc.level.getChunkSource().getLoadedChunksCount(); + if (loaded != lastLoaded || !mc.levelRenderer.hasRenderedAllSections() || !chunksAround(mc)) { + lastLoaded = loaded; + lastChange = now; + return false; + } + return now - lastChange >= 1_000_000_000L; + } + + private static boolean chunksAround(Minecraft mc) { + int r = Math.max(1, mc.options.getEffectiveRenderDistance() - 1); + var center = mc.player.chunkPosition(); + var chunks = mc.level.getChunkSource(); + for (int dx = -r; dx <= r; dx++) { + for (int dz = -r; dz <= r; dz++) { + if (dx * dx + dz * dz <= r * r && !chunks.hasChunk(center.x + dx, center.z + dz)) return false; + } + } + return true; + } + + private static void place(Minecraft mc, JsonObject s, double t) { + LocalPlayer player = mc.player; + if (player == null) return; + Vec3 a = vec(s.getAsJsonArray("from")), b = vec(s.getAsJsonArray("to")); + Vec3 at = a.add(b.subtract(a).scale(t)); + player.setPos(at.x, at.y, at.z); + player.setYRot(yaw(s)); + player.setXRot(s.get("pitch").getAsFloat()); + player.setOldPosAndRot(); + player.setDeltaMovement(Vec3.ZERO); + player.fallDistance = 0; + if (!player.getAbilities().flying) { + player.getAbilities().flying = true; + player.onUpdateAbilities(); + } + } + + private static float yaw(JsonObject s) { + Vec3 a = vec(s.getAsJsonArray("from")), b = vec(s.getAsJsonArray("to")); + return (float) Math.toDegrees(Math.atan2(-(b.x - a.x), b.z - a.z)); + } + + private static Vec3 vec(JsonArray a) { + return new Vec3(a.get(0).getAsDouble(), a.get(1).getAsDouble(), a.get(2).getAsDouble()); + } + + private static JsonObject named(JsonObject s) { + JsonObject e = new JsonObject(); + e.addProperty("scene", s.get("name").getAsString()); + return e; + } + + private static JsonObject renderer(Minecraft mc) { + JsonObject e = new JsonObject(); + e.addProperty("gpu", GlUtil.getRenderer()); + e.addProperty("vendor", GlUtil.getVendor()); + e.addProperty("backend", "OpenGL"); + e.addProperty("driver", GlUtil.getOpenGLVersion()); + JsonArray size = new JsonArray(); + size.add(mc.getWindow().getWidth()); + size.add(mc.getWindow().getHeight()); + e.add("size", size); + e.addProperty("render_distance", mc.options.renderDistance().get()); + e.addProperty("simulation_distance", mc.options.simulationDistance().get()); + return e; + } + + private static long epochMs(long nanos) { + return System.currentTimeMillis() - (System.nanoTime() - nanos) / 1_000_000L; + } + + private void event(String name) { + event(name, new JsonObject()); + } + + private void event(String name, JsonObject e) { + e.addProperty("event", name); + e.addProperty("epoch_ms", System.currentTimeMillis()); + try { + Files.writeString(out, e + "\n", StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND); + } catch (IOException error) { + throw new UncheckedIOException(error); + } + } +} diff --git a/mods/horizon/src/neoforge-bench/resources/META-INF/neoforge.mods.toml b/mods/horizon/src/neoforge-bench/resources/META-INF/neoforge.mods.toml new file mode 100644 index 000000000..0fb0c3029 --- /dev/null +++ b/mods/horizon/src/neoforge-bench/resources/META-INF/neoforge.mods.toml @@ -0,0 +1,11 @@ +# Dev-only: the benchmark driver for a NeoForge client (tools/perf_bench.py --game neoforge). +# Built and run by tools/perf_bench.py; never shipped. +modLoader = "javafml" +loaderVersion = "[4,)" +license = "MIT" + +[[mods]] +modId = "horizon_bench" +version = "1" +displayName = "Horizon benchmark driver (dev only)" +description = "Flies the client along tools/perf_bench.py's paths and records frame times." diff --git a/mods/horizon/src/neoforge-oracle/java/dev/horizonlab/neoforge/FactsExporter.java b/mods/horizon/src/neoforge-oracle/java/dev/horizonlab/neoforge/FactsExporter.java new file mode 100644 index 000000000..c452e6488 --- /dev/null +++ b/mods/horizon/src/neoforge-oracle/java/dev/horizonlab/neoforge/FactsExporter.java @@ -0,0 +1,426 @@ +package dev.horizonlab.neoforge; + +import com.google.gson.stream.JsonWriter; +import java.io.IOException; +import java.io.Writer; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.LinkedHashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.TreeMap; +import java.util.TreeSet; +import net.minecraft.core.BlockPos; +import net.minecraft.core.Direction; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.core.registries.Registries; +import net.minecraft.resources.ResourceLocation; +import net.minecraft.server.MinecraftServer; +import net.minecraft.server.level.ServerLevel; +import net.minecraft.sounds.SoundEvent; +import net.minecraft.world.InteractionHand; +import net.minecraft.world.item.BlockItem; +import net.minecraft.world.item.Item; +import net.minecraft.world.item.ItemStack; +import net.minecraft.world.item.Items; +import net.minecraft.world.item.context.BlockPlaceContext; +import net.minecraft.world.item.crafting.Ingredient; +import net.minecraft.world.item.crafting.RecipeHolder; +import net.minecraft.world.item.enchantment.Enchantments; +import net.minecraft.world.level.EmptyBlockGetter; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.SoundType; +import net.minecraft.world.level.block.state.BlockState; +import net.minecraft.world.level.block.state.properties.Property; +import net.minecraft.world.level.storage.loot.LootParams; +import net.minecraft.world.level.storage.loot.parameters.LootContextParams; +import net.minecraft.world.phys.AABB; +import net.minecraft.world.phys.BlockHitResult; +import net.minecraft.world.phys.Vec3; +import net.minecraft.world.phys.shapes.VoxelShape; +import net.neoforged.bus.api.IEventBus; +import net.neoforged.fml.common.Mod; +import net.neoforged.neoforge.common.NeoForge; +import net.neoforged.neoforge.event.server.ServerStartedEvent; + +/** + * Dev-only oracle for Horizon's mod ports (docs/modpacks.md): once the server has started, + * writes every block, item, recipe and tag of the namespaces named in {@code + * horizon.oracle.namespaces} (plus the vanilla blocks, so templates can be compared) to {@code + * horizon.oracle.out}, then stops the server. Never shipped to players. + */ +@Mod("horizon_oracle") +public final class FactsExporter { + private static final BlockPos FLOOR = new BlockPos(0, 99, 0); + + public FactsExporter(IEventBus modBus) { + NeoForge.EVENT_BUS.addListener(this::started); + } + + private void started(ServerStartedEvent event) { + MinecraftServer server = event.getServer(); + Path out = Path.of(System.getProperty("horizon.oracle.out", "horizon-oracle.json")); + Set namespaces = + new TreeSet<>(Arrays.asList(System.getProperty("horizon.oracle.namespaces", "").split(","))); + namespaces.remove(""); + try (Writer w = Files.newBufferedWriter(out, StandardCharsets.UTF_8); + JsonWriter json = new JsonWriter(w)) { + new Export(server, namespaces, json).run(); + } catch (Throwable e) { + e.printStackTrace(); + System.exit(3); + } + System.out.println("horizon-oracle: wrote " + out); + server.halt(false); + } + + private static final class Export { + final MinecraftServer server; + final ServerLevel level; + final Set namespaces; + final JsonWriter json; + final List tools = new ArrayList<>(); + final List toolNames = new ArrayList<>(); + + Export(MinecraftServer server, Set namespaces, JsonWriter json) { + this.server = server; + this.level = server.overworld(); + this.namespaces = namespaces; + this.json = json; + tool("hand", ItemStack.EMPTY); + tool("diamond_pickaxe", new ItemStack(Items.DIAMOND_PICKAXE)); + tool("diamond_axe", new ItemStack(Items.DIAMOND_AXE)); + tool("diamond_shovel", new ItemStack(Items.DIAMOND_SHOVEL)); + tool("diamond_hoe", new ItemStack(Items.DIAMOND_HOE)); + tool("shears", new ItemStack(Items.SHEARS)); + ItemStack silk = new ItemStack(Items.DIAMOND_PICKAXE); + silk.enchant( + server.registryAccess() + .registryOrThrow(Registries.ENCHANTMENT) + .getHolderOrThrow(Enchantments.SILK_TOUCH), + 1); + tool("silk_touch_pickaxe", silk); + } + + void tool(String name, ItemStack stack) { + toolNames.add(name); + tools.add(stack); + } + + boolean ours(ResourceLocation id) { + return namespaces.contains(id.getNamespace()); + } + + void run() throws IOException { + json.beginObject(); + json.name("schema").value(1); + json.name("minecraft").value(net.minecraft.SharedConstants.getCurrentVersion().getName()); + json.name("namespaces"); + json.beginArray(); + for (String n : namespaces) json.value(n); + json.endArray(); + json.name("tools"); + json.beginArray(); + for (String n : toolNames) json.value(n); + json.endArray(); + blocks(); + // Vanilla's items, so harnesses can tell later versions' additions apart. + json.name("vanilla_items"); + json.beginArray(); + for (Item item : BuiltInRegistries.ITEM) { + ResourceLocation id = BuiltInRegistries.ITEM.getKey(item); + if (id.getNamespace().equals("minecraft")) json.value(id.toString()); + } + json.endArray(); + items(); + recipes(); + tags(); + json.endObject(); + } + + void blocks() throws IOException { + level.setBlock(FLOOR, Blocks.STONE.defaultBlockState(), 3); + json.name("blocks"); + json.beginObject(); + for (Block block : BuiltInRegistries.BLOCK) { + ResourceLocation id = BuiltInRegistries.BLOCK.getKey(block); + boolean mod = ours(id); + if (!mod && !id.getNamespace().equals("minecraft")) continue; + json.name(id.toString()); + json.beginObject(); + json.name("class"); + json.beginArray(); + for (Class c = block.getClass(); c != null && c != Object.class; c = c.getSuperclass()) { + json.value(c.getName()); + } + json.endArray(); + BlockState def = block.defaultBlockState(); + json.name("properties"); + json.beginObject(); + for (Property p : block.getStateDefinition().getProperties()) { + json.name(p.getName()); + json.beginArray(); + for (Object v : p.getPossibleValues()) json.value(name(p, v)); + json.endArray(); + } + json.endObject(); + json.name("default").value(stateKey(def)); + json.name("item").value(BuiltInRegistries.ITEM.getKey(block.asItem()).toString()); + json.name("explosion_resistance").value(block.getExplosionResistance()); + json.name("friction").value(block.getFriction()); + json.name("speed_factor").value(block.getSpeedFactor()); + json.name("jump_factor").value(block.getJumpFactor()); + blockSetType(block); + json.name("states"); + json.beginObject(); + for (BlockState state : block.getStateDefinition().getPossibleStates()) { + json.name(stateKey(state)); + json.beginObject(); + json.name("hardness").value(state.getDestroySpeed(EmptyBlockGetter.INSTANCE, BlockPos.ZERO)); + json.name("emission").value(state.getLightEmission()); + json.name("requires_tool").value(state.requiresCorrectToolForDrops()); + json.name("can_occlude").value(state.canOcclude()); + json.name("replaceable").value(state.canBeReplaced()); + json.name("random_ticks").value(state.isRandomlyTicking()); + var color = state.getMapColor(EmptyBlockGetter.INSTANCE, BlockPos.ZERO); + json.name("map_color").value(color.col); + json.name("map_color_id").value(color.id); + sound(state.getSoundType(level, FLOOR.above(), null)); + json.name("outline"); + boxes(state.getShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO)); + json.name("collision"); + boxes(state.getCollisionShape(EmptyBlockGetter.INSTANCE, BlockPos.ZERO)); + if (mod && lootSample(block, state)) drops(state); + json.endObject(); + } + json.endObject(); + if (block.asItem() instanceof BlockItem bi) placement(bi); + json.endObject(); + } + json.endObject(); + } + + /** Every state of a small block; else the default and each one-property change from it. */ + boolean lootSample(Block block, BlockState state) { + if (block.getStateDefinition().getPossibleStates().size() <= 32) return true; + BlockState def = block.defaultBlockState(); + int differ = 0; + for (Property p : block.getStateDefinition().getProperties()) { + if (!state.getValue(p).equals(def.getValue(p))) differ++; + } + return differ <= 1; + } + + void drops(BlockState state) throws IOException { + json.name("drops"); + json.beginObject(); + for (int i = 0; i < tools.size(); i++) { + LootParams.Builder params = + new LootParams.Builder(level) + .withParameter(LootContextParams.ORIGIN, Vec3.atCenterOf(FLOOR.above())) + .withParameter(LootContextParams.TOOL, tools.get(i).copy()); + Map counts = new TreeMap<>(); + for (ItemStack s : state.getDrops(params)) { + counts.merge(BuiltInRegistries.ITEM.getKey(s.getItem()).toString(), s.getCount(), Integer::sum); + } + json.name(toolNames.get(i)); + json.beginObject(); + for (Map.Entry e : counts.entrySet()) json.name(e.getKey()).value(e.getValue()); + json.endObject(); + } + json.endObject(); + } + + void placement(BlockItem item) throws IOException { + BlockPos at = FLOOR.above(); + level.setBlock(at, Blocks.AIR.defaultBlockState(), 3); + json.name("placement"); + json.beginObject(); + for (Direction face : new Direction[] {Direction.UP, Direction.NORTH}) { + BlockPos clicked = face == Direction.UP ? FLOOR : at.relative(face.getOpposite()); + level.setBlock(clicked, Blocks.STONE.defaultBlockState(), 3); + Vec3 hit = Vec3.atCenterOf(clicked).add(Vec3.atLowerCornerOf(face.getNormal()).scale(0.5)); + // A player-less context (the constructor is protected, hence the subclass). + BlockPlaceContext ctx = + new BlockPlaceContext( + level, null, InteractionHand.MAIN_HAND, new ItemStack(item), + new BlockHitResult(hit, face, clicked, false)) {}; + BlockState placed = null; + try { + placed = item.getBlock().getStateForPlacement(ctx); + } catch (RuntimeException e) { + // Needs a player: recorded as no placement. + } + json.name(face.getName()).value(placed == null ? null : stateKey(placed)); + if (face != Direction.UP) level.setBlock(clicked, Blocks.AIR.defaultBlockState(), 3); + } + json.endObject(); + } + + /** The BlockSetType (doors, trapdoors, buttons, plates) and WoodType (fence gates) a block keeps. */ + void blockSetType(Block block) throws IOException { + String setType = null, woodType = null; + Boolean byHand = null; + for (Class c = block.getClass(); c != null && c != Object.class; c = c.getSuperclass()) { + for (var f : c.getDeclaredFields()) { + try { + if (f.getType() == net.minecraft.world.level.block.state.properties.BlockSetType.class && setType == null) { + f.setAccessible(true); + var t = (net.minecraft.world.level.block.state.properties.BlockSetType) f.get(block); + if (t != null) { + setType = t.name(); + byHand = t.canOpenByHand(); + } + } else if (f.getType() == net.minecraft.world.level.block.state.properties.WoodType.class && woodType == null) { + f.setAccessible(true); + var t = (net.minecraft.world.level.block.state.properties.WoodType) f.get(block); + if (t != null) woodType = t.name(); + } + } catch (ReflectiveOperationException | RuntimeException e) { + // Not readable: left out. + } + } + } + if (setType != null) { + json.name("block_set_type"); + json.beginObject(); + json.name("name").value(setType); + json.name("can_open_by_hand").value(byHand); + json.endObject(); + } + if (woodType != null) json.name("wood_type").value(woodType); + } + + void sound(SoundType s) throws IOException { + json.name("sound"); + json.beginObject(); + json.name("volume").value(s.getVolume()); + json.name("pitch").value(s.getPitch()); + event("break", s.getBreakSound()); + event("step", s.getStepSound()); + event("place", s.getPlaceSound()); + event("hit", s.getHitSound()); + event("fall", s.getFallSound()); + json.endObject(); + } + + void event(String name, SoundEvent e) throws IOException { + json.name(name).value(e.getLocation().toString()); + } + + void boxes(VoxelShape shape) throws IOException { + json.beginArray(); + for (AABB b : shape.toAabbs()) { + json.beginArray(); + json.value(b.minX).value(b.minY).value(b.minZ).value(b.maxX).value(b.maxY).value(b.maxZ); + json.endArray(); + } + json.endArray(); + } + + void items() throws IOException { + json.name("items"); + json.beginObject(); + for (Item item : BuiltInRegistries.ITEM) { + ResourceLocation id = BuiltInRegistries.ITEM.getKey(item); + if (!ours(id)) continue; + ItemStack stack = new ItemStack(item); + json.name(id.toString()); + json.beginObject(); + json.name("class").value(item.getClass().getName()); + json.name("max_stack_size").value(stack.getMaxStackSize()); + json.name("rarity").value(stack.getRarity().getSerializedName()); + if (item instanceof BlockItem bi) { + json.name("block").value(BuiltInRegistries.BLOCK.getKey(bi.getBlock()).toString()); + } + var food = stack.get(net.minecraft.core.component.DataComponents.FOOD); + if (food != null) { + json.name("food"); + json.beginObject(); + json.name("nutrition").value(food.nutrition()); + json.name("saturation").value(food.saturation()); + json.endObject(); + } + json.endObject(); + } + json.endObject(); + } + + void recipes() throws IOException { + json.name("recipes"); + json.beginObject(); + for (RecipeHolder holder : server.getRecipeManager().getRecipes()) { + if (!ours(holder.id())) continue; + var recipe = holder.value(); + json.name(holder.id().toString()); + json.beginObject(); + json.name("type").value(BuiltInRegistries.RECIPE_TYPE.getKey(recipe.getType()).toString()); + json.name("serializer").value(BuiltInRegistries.RECIPE_SERIALIZER.getKey(recipe.getSerializer()).toString()); + ItemStack result = recipe.getResultItem(server.registryAccess()); + json.name("result").value(BuiltInRegistries.ITEM.getKey(result.getItem()).toString()); + json.name("count").value(result.getCount()); + json.name("ingredients"); + json.beginArray(); + for (Ingredient ing : recipe.getIngredients()) { + Set ids = new TreeSet<>(); + for (ItemStack s : ing.getItems()) ids.add(BuiltInRegistries.ITEM.getKey(s.getItem()).toString()); + json.beginArray(); + for (String i : ids) json.value(i); + json.endArray(); + } + json.endArray(); + json.endObject(); + } + json.endObject(); + } + + /** Tags that hold one of our ids, with only those members; and every `c` tag, whole. */ + void tags() throws IOException { + json.name("tags"); + json.beginObject(); + for (String kind : new String[] {"block", "item"}) { + json.name(kind); + json.beginObject(); + Map> out = new TreeMap<>(); + var registry = kind.equals("block") ? BuiltInRegistries.BLOCK : BuiltInRegistries.ITEM; + registry.getTags().forEach(pair -> { + String tag = pair.getFirst().location().toString(); + boolean convention = pair.getFirst().location().getNamespace().equals("c"); + Set members = new LinkedHashSet<>(); + for (var holder : pair.getSecond()) { + ResourceLocation id = holder.unwrapKey().orElseThrow().location(); + if (convention || ours(id) || ours(pair.getFirst().location())) members.add(id.toString()); + } + if (!members.isEmpty()) out.put(tag, new TreeSet<>(members)); + }); + for (var e : out.entrySet()) { + json.name(e.getKey()); + json.beginArray(); + for (String m : e.getValue()) json.value(m); + json.endArray(); + } + json.endObject(); + } + json.endObject(); + } + + static String stateKey(BlockState state) { + StringBuilder b = new StringBuilder(); + for (Property p : state.getProperties()) { + if (b.length() > 0) b.append(','); + b.append(p.getName()).append('=').append(name(p, state.getValue(p))); + } + return b.toString(); + } + + @SuppressWarnings({"unchecked", "rawtypes"}) + static String name(Property p, Object v) { + return p.getName((Comparable) v); + } + } +} diff --git a/mods/horizon/src/neoforge-oracle/resources/META-INF/neoforge.mods.toml b/mods/horizon/src/neoforge-oracle/resources/META-INF/neoforge.mods.toml new file mode 100644 index 000000000..006875e95 --- /dev/null +++ b/mods/horizon/src/neoforge-oracle/resources/META-INF/neoforge.mods.toml @@ -0,0 +1,11 @@ +# Dev-only: the NeoForge oracle for Horizon's mod-port harnesses (docs/modpacks.md). +# Built and run by tools/neoforge_oracle.py; never shipped. +modLoader = "javafml" +loaderVersion = "[4,)" +license = "Apache-2.0" + +[[mods]] +modId = "horizon_oracle" +version = "1" +displayName = "Horizon oracle (dev only)" +description = "Exports block, item, recipe and tag facts for Horizon's parity harnesses." diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java index 109f33b66..bb438b735 100644 --- a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaProtocolClient.java @@ -125,7 +125,10 @@ public final class VanillaProtocolClient { client.status(host, port); client.join(host, port, name); } catch (Exception e) { - client.lines.add("error " + client.state + " " + e); + // With its causes: a decoder error names the field it could not read there. + StringBuilder line = new StringBuilder("error " + client.state + " " + e); + for (Throwable c = e.getCause(); c != null; c = c.getCause()) line.append(" / caused by ").append(c); + client.lines.add(line.toString()); } finally { if (client.socket != null) client.socket.close(); } @@ -427,9 +430,23 @@ public final class VanillaProtocolClient { boolean loaded = false; PositionMoveRotation position = new PositionMoveRotation(Vec3.ZERO, Vec3.ZERO, 0, 0); long lastChunk = 0; - long deadline = System.currentTimeMillis() + 90_000; + long deadline = System.currentTimeMillis() + 150_000; int step = 0; long stepAt = 0; + // The death the console deals and the respawn after it (PlayerList.respawn): from the + // client's PERFORM_RESPAWN until its level is loaded again (LevelLoadTracker: the level + // info's "level chunks load start" event, then the chunk under the player). + boolean respawning = false; + boolean respawned = false; + boolean loadStart = false; + boolean respawnChunk = false; + String runType = null; + int run = 0; + PositionMoveRotation respawnAt = null; + // The furnace set beside the respawn point: its window and the menu's state id. + net.minecraft.core.BlockPos furnace = null; + int furnaceWindow = -1; + int furnaceState = 0; // The villager the server's console summoned beside the spawn. int villager = -1; net.minecraft.world.phys.Vec3 villagerAt = null; @@ -445,6 +462,22 @@ public final class VanillaProtocolClient { Packet packet = r.packet; counts.merge(r.type, 1, Integer::sum); if (r.type.equals("minecraft:chunk_batch_start")) ordered = false; + if (respawning && !respawned) { + String line = respawnLine(r); + // PlayerList.respawn ends with initInventoryMenu: what follows is the + // next ticks' (health, entity data, chunks), in each server's own order. + respawned = r.type.equals("minecraft:container_set_content") && respawnAt != null; + if (line != null && !line.equals(runType)) { + if (runType != null) record("play-respawn", runLine(runType, run)); + runType = line; + run = 0; + } + if (line != null) run++; + if (packet instanceof net.minecraft.network.protocol.game.ClientboundGameEventPacket p + && p.getEvent() == net.minecraft.network.protocol.game.ClientboundGameEventPacket.LEVEL_CHUNKS_LOAD_START) { + loadStart = true; + } + } if (packet instanceof ClientboundKeepAlivePacket p) { send(new ServerboundKeepAlivePacket(p.getId())); } else if (packet instanceof ClientboundPingPacket p) { @@ -452,9 +485,14 @@ public final class VanillaProtocolClient { } else if (packet instanceof ClientboundPlayerPositionPacket p) { position = PositionMoveRotation.calculateAbsolute(position, p.change(), p.relatives()); record("play-move", text(position.position()) + " " + position.yRot() + " " + position.xRot()); + if (respawning) respawnAt = position; send(new ServerboundAcceptTeleportationPacket(p.id(), position.position().x, position.position().y, position.position().z, position.yRot(), position.xRot())); - send(new ServerboundMovePlayerPacket.PosRot(position.position(), position.yRot(), position.xRot(), false, false)); + // The 26.3 client answers with the acceptance alone; a second position in the + // same tick is an invalid move to vanilla's server once the player is loaded. + if (!respawning) { + send(new ServerboundMovePlayerPacket.PosRot(position.position(), position.yRot(), position.xRot(), false, false)); + } if (!loaded) { send(new ServerboundPlayerLoadedPacket()); loaded = true; @@ -467,20 +505,48 @@ public final class VanillaProtocolClient { lastChunk = now; int px = (int) Math.floor(position.position().x) >> 4; int pz = (int) Math.floor(position.position().z) >> 4; - if (p.x() == px && p.z() == pz) chunkParts(p); - } else if (packet instanceof ClientboundSystemChatPacket p && step > 0) { + if (p.x() == px && p.z() == pz && !respawning) chunkParts(p); + if (p.x() == px && p.z() == pz && respawnAt != null) respawnChunk = true; + } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundContainerSetContentPacket p + && furnaceWindow > 0 && p.containerId() == furnaceWindow) { + furnaceState = p.stateId(); + record("play-furnace", "content " + p.items() + " carried " + p.carriedItem()); + } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundContainerSetSlotPacket p + && furnaceWindow > 0 && p.getContainerId() == furnaceWindow) { + furnaceState = p.getStateId(); + record("play-furnace", "slot " + p.getSlot() + " " + p.getItem()); + } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundContainerSetDataPacket p + && furnaceWindow > 0 && p.getContainerId() == furnaceWindow && (p.getId() == 1 || p.getId() == 3)) { + // Lit total and cooking total; the time left and the cooking timer follow the ticks. + record("play-furnace", "data " + p.getId() + " " + p.getValue()); + } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundEntityEventPacket p + && step == 5 + REPLIES.length && (int) (byte) field(p, "eventId") == 28) { + // Operator (level 4, as the console made it): it may run /kill on itself. + send(new ServerboundChatCommandPacket("kill")); + step++; + } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundPlayerCombatKillPacket p && step > 0) { + // The death message is left out: guests' death messages aren't Horizon's yet. + record("play-death", "death screen"); + send(new net.minecraft.network.protocol.game.ServerboundClientCommandPacket( + net.minecraft.network.protocol.game.ServerboundClientCommandPacket.Action.PERFORM_RESPAWN)); + respawning = true; + } else if (packet instanceof ClientboundSystemChatPacket p && step > 0 && step < 5 + REPLIES.length) { record("play-chat", "system " + p.overlay() + " " + json(p.content())); - } else if (packet instanceof ClientboundPlayerChatPacket p && step > 0) { + } else if (packet instanceof ClientboundPlayerChatPacket p && step > 0 && step < 5 + REPLIES.length) { record("play-chat", "player " + p.sender() + " " + p.index() + " " + p.body().content() + " " + p.signature().isPresent() + " " + p.unsignedContent().map(VanillaProtocolClient::json) + " " + text(p.filterMask()) + " " + p.chatType().chatType().getRegisteredName() + " " + json(p.chatType().name()) + " " + p.chatType().targetName().map(VanillaProtocolClient::json)); - } else if (packet instanceof ClientboundDisguisedChatPacket p && step > 0) { + } else if (packet instanceof ClientboundDisguisedChatPacket p && step > 0 && step < 5 + REPLIES.length) { record("play-chat", "disguised " + json(p.message()) + " " + p.chatType().chatType().getRegisteredName()); } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundAddEntityPacket p && p.getType() == net.minecraft.world.entity.EntityTypes.VILLAGER) { villager = p.getId(); villagerAt = new net.minecraft.world.phys.Vec3(p.getX(), p.getY(), p.getZ()); + } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundOpenScreenPacket p && step > 6 + REPLIES.length) { + furnaceWindow = p.getContainerId(); + record("play-furnace", "open " + net.minecraft.core.registries.BuiltInRegistries.MENU.getKey(p.getType()) + " " + + json(p.getTitle())); } else if (packet instanceof net.minecraft.network.protocol.game.ClientboundOpenScreenPacket p && step > 0) { record("play-trade", "open " + p.getContainerId() + " " + net.minecraft.core.registries.BuiltInRegistries.MENU.getKey(p.getType()) + " " @@ -561,12 +627,101 @@ public final class VanillaProtocolClient { } else if (step == 4 + REPLIES.length && now - stepAt > 1500) { send(new net.minecraft.network.protocol.game.ServerboundContainerClosePacket(1)); record("play-counts", "chunks " + counts.getOrDefault("minecraft:level_chunk_with_light", 0)); + // The harness's console makes the player an operator once it reads this line. + send(new ServerboundChatPacket("ready to respawn", Instant.now(), 0L, Optional.empty(), + new LastSeenMessages.Update(0, new BitSet(), (byte) 0))); + step++; + stepAt = now; + } else if (step == 6 + REPLIES.length && loadStart && respawnChunk) { + // LevelLoadTracker is done: the vanilla client leaves the loading screen. + if (runType != null) record("play-respawn", runLine(runType, run)); + record("play-respawn", "level loaded at " + text(respawnAt.position())); + send(new ServerboundPlayerLoadedPacket()); + // A furnace two blocks south, an ore and its fuel. + furnace = net.minecraft.core.BlockPos.containing(respawnAt.position()).south(2); + send(new ServerboundChatCommandPacket("setblock " + furnace.getX() + " " + furnace.getY() + " " + furnace.getZ() + + " minecraft:furnace")); + send(new ServerboundChatCommandPacket("give @s minecraft:raw_iron")); + send(new ServerboundChatCommandPacket("give @s minecraft:coal")); + step++; + stepAt = now; + } else if (step == 7 + REPLIES.length && now - stepAt > 1500) { + // Right-click it with an empty hand (AbstractFurnaceBlock.useWithoutItem). + send(new net.minecraft.network.protocol.game.ServerboundUseItemOnPacket(net.minecraft.world.InteractionHand.MAIN_HAND, + new net.minecraft.world.phys.BlockHitResult(net.minecraft.world.phys.Vec3.atCenterOf(furnace), + net.minecraft.core.Direction.NORTH, furnace, false), 1)); + step++; + stepAt = now; + } else if (step >= 8 + REPLIES.length && step <= 10 + REPLIES.length && furnaceWindow > 0 + && now - stepAt > (step == 10 + REPLIES.length ? 12_000 : 1500)) { + // Shift-click the ore (hotbar 0) into the input, the coal (hotbar 1) into the + // fuel slot, and the smelted ingot out of the result; nothing claimed, so the + // server sends every slot the click changed. + int slot = step == 8 + REPLIES.length ? 30 : step == 9 + REPLIES.length ? 31 : 2; + record("play-furnace", "quick move " + slot); + send(new net.minecraft.network.protocol.game.ServerboundContainerClickPacket(furnaceWindow, furnaceState, (short) slot, + (byte) 0, net.minecraft.world.inventory.ContainerInput.QUICK_MOVE, + new it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap<>(), net.minecraft.network.HashedStack.EMPTY)); + step++; + stepAt = now; + } else if (step == 11 + REPLIES.length && now - stepAt > 1500) { + send(new net.minecraft.network.protocol.game.ServerboundContainerClosePacket(furnaceWindow)); + record("play-furnace", "closed"); return; } } + if (respawning) { + if (runType != null) record("play-respawn", runLine(runType, run)); + record("play-respawn", "level never loaded: load start " + loadStart + ", chunk under the player " + respawnChunk); + } record("error", "timed out in play, step " + step + ", chunks " + chunks.size()); } + /** + * A run of the same packet: counted, but for the entities removed with the dead player + * (which mobs are tracked near the spawn at that moment varies). + */ + private static String runLine(String type, int run) { + return type + (run > 1 && !type.equals("minecraft:remove_entities") ? " x" + run : ""); + } + + /** + * A packet after the respawn request, as compared: entity ids and periodic or streamed + * packets (time, keep-alive, chunk data and light, whose batching follows each server's + * timing) left out; null to skip it. + */ + private String respawnLine(Received r) { + String t = r.type; + // Entities moving and the dead player's health land in the window between the death + // and the respawn as each server's ticks fall: left out. + if (t.startsWith("minecraft:move_entity") || t.equals("minecraft:rotate_head") || t.equals("minecraft:set_entity_motion") + || t.equals("minecraft:set_entity_data") || t.equals("minecraft:entity_position_sync") || t.equals("minecraft:set_health")) { + return null; + } + if (t.equals("minecraft:keep_alive") || t.equals("minecraft:ping") || t.equals("minecraft:level_chunk_with_light") + || t.equals("minecraft:chunk_batch_start") || t.equals("minecraft:chunk_batch_finished") + || t.equals("minecraft:light_update") || t.equals("minecraft:bundle_delimiter") + // Chat (the death message and /kill's feedback) is compared before the death. + || t.equals("minecraft:system_chat") || t.equals("minecraft:player_chat") || t.equals("minecraft:disguised_chat")) { + return null; + } + if (r.packet instanceof net.minecraft.network.protocol.game.ClientboundSetTimePacket p && p.clockUpdates().isEmpty()) { + return null; + } + if (r.packet instanceof net.minecraft.network.protocol.game.ClientboundEntityEventPacket p) { + return t + " " + field(p, "eventId"); + } + if (r.packet instanceof net.minecraft.network.protocol.game.ClientboundRemoveEntitiesPacket p) { + return t; + } + if (t.equals("minecraft:respawn") || t.equals("minecraft:game_event") || t.equals("minecraft:set_default_spawn_position") + || t.equals("minecraft:change_difficulty") || t.equals("minecraft:set_experience") || t.equals("minecraft:set_health") + || t.equals("minecraft:set_chunk_cache_center") || t.equals("minecraft:initialize_border")) { + return t + " " + text(r.packet); + } + return t; + } + /** * A chunk's blocks, biomes, heightmaps and block entities (its * ClientboundLevelChunkPacketData bytes). The light is left out: both servers relight around chunks diff --git a/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTranslucencyOracle.java b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTranslucencyOracle.java new file mode 100644 index 000000000..0dd308d45 --- /dev/null +++ b/mods/horizon/src/test/java/dev/horizonlab/parity/VanillaTranslucencyOracle.java @@ -0,0 +1,63 @@ +package dev.horizonlab.parity; + +import com.google.gson.*; +import com.mojang.blaze3d.vertex.*; +import com.mojang.renderpearl.api.pipeline.IndexType; +import java.nio.*; +import java.nio.file.*; +import net.minecraft.client.renderer.chunk.TranslucencyPointOfView; +import net.minecraft.core.BlockPos; +import net.minecraft.core.SectionPos; +import net.minecraft.world.phys.Vec3; + +/** Original probe calling the pinned game's centroid, sorting and index writers. */ +public final class VanillaTranslucencyOracle { + public static void main(String[] args) throws Exception { + try (var out = Files.newBufferedWriter(Path.of(args[1]))) { + for (String line : Files.readAllLines(Path.of(args[0]))) { + JsonObject input = JsonParser.parseString(line).getAsJsonObject(); + JsonArray quads = input.getAsJsonArray("quads"); + ByteBuffer vertices = ByteBuffer.allocate(quads.size() * 48).order(ByteOrder.nativeOrder()); + for (JsonElement quad : quads) + for (JsonElement vertex : quad.getAsJsonArray()) + for (JsonElement component : vertex.getAsJsonArray()) vertices.putFloat(component.getAsFloat()); + vertices.flip(); + CompactVectorArray centers = new CompactVectorArray(quads.size()); + MeshData.decodeQuadCentroids(vertices, quads.size() * 4, DefaultVertexFormat.POSITION, centers, 0); + JsonArray centerBits = new JsonArray(); + for (int i = 0; i < centers.size(); i++) { + JsonArray c = new JsonArray(); + c.add(Float.floatToIntBits(centers.getX(i))); + c.add(Float.floatToIntBits(centers.getY(i))); + c.add(Float.floatToIntBits(centers.getZ(i))); + centerBits.add(c); + } + JsonArray origin = input.getAsJsonArray("origin"); + int ox = origin.get(0).getAsInt(), oy = origin.get(1).getAsInt(), oz = origin.get(2).getAsInt(); + long section = SectionPos.asLong(ox >> 4, oy >> 4, oz >> 4); + BlockPos lastBlock = null; + TranslucencyPointOfView lastPov = null; + JsonArray frames = new JsonArray(); + for (JsonElement element : input.getAsJsonArray("cameras")) { + JsonArray c = element.getAsJsonArray(); + Vec3 camera = new Vec3(c.get(0).getAsDouble(), c.get(1).getAsDouble(), c.get(2).getAsDouble()); + BlockPos block = BlockPos.containing(camera); + TranslucencyPointOfView pov = TranslucencyPointOfView.of(camera, section); + boolean update = lastPov == null || !lastPov.equals(pov) || (!block.equals(lastBlock) && pov.isAxisAligned()); + if (update) { lastPov = pov; lastBlock = block; } + var sorting = VertexSorting.byDistance((float)(camera.x - ox), (float)(camera.y - oy), (float)(camera.z - oz)); + ByteBuffer buffer = ByteBuffer.allocate(quads.size() * 24).order(ByteOrder.nativeOrder()); + new MeshData.SortState(centers, IndexType.INT).writeSortedIndexBuffer(buffer, sorting); + buffer.flip(); + JsonArray indices = new JsonArray(); + while (buffer.hasRemaining()) indices.add(buffer.getInt()); + JsonObject frame = new JsonObject(); + frame.add("indices", indices); frame.addProperty("update", update); frames.add(frame); + } + JsonObject result = new JsonObject(); + result.add("centers", centerBits); result.add("frames", frames); + out.write(result.toString()); out.newLine(); + } + } + } +} diff --git a/tests/test_world_input_capture.py b/tests/test_world_input_capture.py new file mode 100644 index 000000000..c7fc4629f --- /dev/null +++ b/tests/test_world_input_capture.py @@ -0,0 +1,155 @@ +"""Exercise the client's capture methods without opening or focusing an OS window. + +The fixture substitutes only winit and mouse state. Method bodies come from the +client, so the assertions observe grab attempts, visibility and stored capture +state. Native Windows delivery remains a manual check. Vanilla 26.3 reference: +LevelLoadingScreen.tick/onClose, Gui.setScreen, MouseHandler.grabMouse/releaseMouse. +""" +from pathlib import Path +import os +import shutil +import subprocess +import tempfile +import unittest + + +ROOT = Path(__file__).resolve().parents[1] + + +def method(source, name): + start = source.index(f" fn {name}(") + opening = source.index("{", start) + depth = 1 + end = opening + 1 + while depth: + depth += (source[end] == "{") - (source[end] == "}") + end += 1 + return source[start:end] + + +class WorldInputCaptureTests(unittest.TestCase): + def test_loading_completion_wiring(self): + game = (ROOT / "crates/horizon-client/src/game.rs").read_text(encoding="utf-8") + remote = (ROOT / "crates/horizon-client/src/game/remote.rs").read_text(encoding="utf-8") + spawn = method(game, "spawn_player") + self.assertLess(spawn.index("self.player = Some(player)"), + spawn.index("self.capture_loaded_world()")) + tick = remote[remote.index(" pub(super) fn tick_remote("):] + ready = tick[tick.index("if self.remote.as_ref().is_some_and(|r| !r.loaded_sent)"):] + self.assertLess(ready.index("r.loaded_sent = true"), + ready.index("self.capture_loaded_world()")) + self.assertLess(ready.index("self.capture_loaded_world()"), + ready.index("let stick = self.pad_move()")) + + @unittest.skipUnless(shutil.which("rustc"), "requires Rust") + def test_capture_lifecycle(self): + source = (ROOT / "crates/horizon-client/src/game.rs").read_text(encoding="utf-8") + fixture = r''' +use std::cell::Cell; +#[derive(Clone, Copy, PartialEq)] +enum Screen { Playing, Paused, Inventory } +enum CursorGrabMode { Locked, Confined, None } +#[derive(Default)] +struct Window { grabs: Cell, visible: Cell, fail: bool } +impl Window { + fn set_cursor_grab(&self, mode: CursorGrabMode) -> Result<(), ()> { + if !matches!(mode, CursorGrabMode::None) { + self.grabs.set(self.grabs.get() + 1); + } + if self.fail { Err(()) } else { Ok(()) } + } + fn set_cursor_visible(&self, visible: bool) { self.visible.set(visible); } +} +#[derive(Default)] +struct Mouse { ignore_first: bool } +impl Mouse { fn set_ignore_first_move(&mut self) { self.ignore_first = true; } } +struct Remote { chat_input: Option<()> } +struct Game { + focused: bool, agent_pointer: bool, window: Option, mouse: Mouse, + captured: bool, title: Option<()>, screen: Screen, remote: Option, + mining: bool, +} +impl Default for Game { + fn default() -> Self { + Self { focused: true, agent_pointer: false, window: Some(Window::default()), + mouse: Mouse::default(), captured: false, title: None, + screen: Screen::Playing, remote: None, mining: false } + } +} +impl Game { + fn stop_mining(&mut self) { self.mining = false; } +METHODS +} +fn main() { + // New and existing worlds, returning to title, and reopening a world. + for remote in [false, true] { + let mut game = Game::default(); + if remote { game.remote = Some(Remote { chat_input: None }); } + for _ in 0..3 { + game.title = Some(()); + game.capture(false); + game.capture_loaded_world(); + assert!(!game.captured); + game.title = None; + game.capture_loaded_world(); + assert!(game.captured); + assert!(game.mouse.ignore_first); + assert!(!game.window.as_ref().unwrap().visible.get()); + for screen in [Screen::Paused, Screen::Inventory] { + game.screen = screen; + game.mining = true; + game.capture(false); + assert!(!game.mining); + game.capture_loaded_world(); + assert!(!game.captured); + assert!(game.window.as_ref().unwrap().visible.get()); + game.screen = Screen::Playing; + game.capture(true); + assert!(game.captured); + } + } + // Alt+Tab during loading: no native grab or agent-pointer reset. + game.focused = false; + game.mining = true; + game.capture(false); + assert!(!game.mining); + assert!(!game.captured); + assert!(game.window.as_ref().unwrap().visible.get()); + game.agent_pointer = true; + let grabs = game.window.as_ref().unwrap().grabs.get(); + game.capture_loaded_world(); + game.capture(true); + assert!(!game.captured); + assert!(game.agent_pointer); + assert_eq!(game.window.as_ref().unwrap().grabs.get(), grabs); + game.focused = true; + assert!(!game.captured); // Focus alone does not resume gameplay. + game.capture(true); // Click or resume action. + assert!(game.captured); + } + let mut game = Game::default(); + game.remote = Some(Remote { chat_input: Some(()) }); + game.capture_loaded_world(); + assert!(!game.captured); + assert_eq!(game.window.as_ref().unwrap().grabs.get(), 0); + game.remote = None; + game.window.as_mut().unwrap().fail = true; + game.capture_loaded_world(); + assert!(!game.captured); + assert!(game.window.as_ref().unwrap().visible.get()); +} +''' + fixture = fixture.replace("METHODS", method(source, "capture") + "\n" + + method(source, "capture_loaded_world")) + with tempfile.TemporaryDirectory(prefix="horizon-input-") as folder: + folder = Path(folder) + rust = folder / "capture.rs" + binary = folder / ("capture.exe" if os.name == "nt" else "capture") + rust.write_text(fixture, encoding="utf-8") + subprocess.run(["rustc", "--edition=2024", str(rust), "-o", str(binary)], + check=True, capture_output=True, text=True, timeout=60) + subprocess.run([str(binary)], check=True, capture_output=True, text=True, timeout=10) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/PROFILE_SHARED_JSON.md b/tools/PROFILE_SHARED_JSON.md new file mode 100644 index 000000000..976e43ecb --- /dev/null +++ b/tools/PROFILE_SHARED_JSON.md @@ -0,0 +1,85 @@ +# Reproducing PR #607 block-facts startup profiles + +These are loader-only measurements. They include `Assets::load`, `BlockFacts`, and +`Behaviours` and exclude content root preparation, graphics, input setup, first +frame, and the rest of the client startup sequence. No full-client startup gain +has been measured. The prepared `26.3` resource root must match the revision. + +## Historical release builds + +From the PR branch checkout, use an absolute prepared resource-root path and a +single, explicit Cargo release profile for each detached historical worktree: + +```sh +PR_ROOT="$(pwd)" +BENCH_ROOT="$(mktemp -d)" +mkdir -p "$BENCH_ROOT/bin" +CARGO_TARGET_DIR="$BENCH_ROOT/target" +export CARGO_TARGET_DIR + +for entry in baseline:e422bd13 a01:a01d47f2 cf:cf837118 cd:cd61e11b; do + name="${entry%%:*}" + revision="${entry#*:}" + git worktree add --detach "$BENCH_ROOT/$name" "$revision" +done + +# e422bd13 predates the profiler; add the same baseline example source used +# for the benchmark, without changing any production implementation. +cp "$PR_ROOT/tools/baseline_loader_profile.rs" \ + "$BENCH_ROOT/baseline/crates/horizon-client/examples/shared_json_startup_profile.rs" + +for name in baseline a01 cf cd; do + (cd "$BENCH_ROOT/$name" && \ + cargo build --offline --release -p horizon-client --example shared_json_startup_profile) + cp "$CARGO_TARGET_DIR/release/examples/shared_json_startup_profile" \ + "$BENCH_ROOT/bin/$name" +done + +cargo build --offline --release -p horizon-client --example shared_json_startup_profile +cp "$CARGO_TARGET_DIR/release/examples/shared_json_startup_profile" "$BENCH_ROOT/bin/current" + +python3 tools/profile_shared_json.py \ + --resources /absolute/path/to/.local/horizon-client/26.3 \ + --baseline-binary "$BENCH_ROOT/bin/baseline" \ + --a01-binary "$BENCH_ROOT/bin/a01" \ + --cf-binary "$BENCH_ROOT/bin/cf" \ + --cd-binary "$BENCH_ROOT/bin/cd" \ + --binary "$BENCH_ROOT/bin/current" \ + --repeats 10 --output "$BENCH_ROOT/five-way.json" +``` + +The source tree of each historical revision is exact. The baseline profiler is +an added standalone measurement example, and therefore does not alter its +historical loader logic. Comparisons use alternating forward/reverse process +order, separate processes, Linux `wait4` CPU time and peak RSS, and saved raw +samples with 25th/75th percentiles, extrema, build hashes and system load. +Keep concurrent compilation and other CPU-intensive jobs idle if possible. +`ru_maxrss` records process peak residency and cannot establish the real game's +memory usage after rendering begins. If system load rises, preserve rather than +discard outliers and report the uncertainty. + +## Decoding passes and buffer lifetime + +```sh +cargo build --offline --release -p horizon-client --example gameplay_decode_profile +python3 tools/profile_gameplay_decode.py \ + --resources /absolute/path/to/.local/horizon-client/26.3 \ + --repeats 10 --output "$BENCH_ROOT/decode.json" + +python3 tools/profile_shared_json.py \ + --resources /absolute/path/to/.local/horizon-client/26.3 \ + --variant released="$BENCH_ROOT/bin/current"@deferred \ + --variant retained="$BENCH_ROOT/bin/current"@deferred-held \ + --repeats 10 --output "$BENCH_ROOT/lifetime.json" +``` + +The decoder profile measures the `Validate` pass and typed `Gameplay` pass +separately, including the JSON read in `total_ms`. The primary facts loader +continues both passes to preserve JSON validation, error precedence and useful +diagnostics, including overwritten fields and raw geometry. The lifetime +comparison measures current process `VmRSS` after materialisation, plus peak +RSS across the process. An explicit `drop` releases owned Rust values, but the +allocator may retain physical pages, so immediate RSS savings are not implied. + +When finished, `git worktree remove` each temporary historical worktree. Do +not `git clean` or reset the active PR working tree. diff --git a/tools/baseline_loader_profile.rs b/tools/baseline_loader_profile.rs new file mode 100644 index 000000000..f966301b8 --- /dev/null +++ b/tools/baseline_loader_profile.rs @@ -0,0 +1,23 @@ +//! Historical baseline comparator for PR #607; one normal independent loading sequence. +use horizon_client::{assets::Assets, blocks::BlockFacts}; +use horizon_world::level::Behaviours; +use std::{path::Path, time::Instant}; + +fn main() -> Result<(), Box> { + let mut args = std::env::args().skip(1); + let root = args.next().ok_or("expected prepared resource directory")?; + if args.next().as_deref() != Some("baseline") { + return Err("expected baseline".into()); + } + let root = Path::new(&root); + let total = Instant::now(); + let assets = Assets::load(root)?; + let started = Instant::now(); + let facts = BlockFacts::load(root, assets.registry)?; + let behaviours = Behaviours::load(assets.registry, root)?; + println!("phase_ms={:.3}", started.elapsed().as_secs_f64() * 1000.0); + println!("total_ms={:.3}", total.elapsed().as_secs_f64() * 1000.0); + println!("states={}", facts.states.len()); + std::hint::black_box((facts, behaviours)); + Ok(()) +} diff --git a/tools/mod_port_harness.py b/tools/mod_port_harness.py new file mode 100644 index 000000000..181e6533b --- /dev/null +++ b/tools/mod_port_harness.py @@ -0,0 +1,217 @@ +#!/usr/bin/env python3 +"""Per-mod parity harness for ported mods (docs/modpacks.md, "The Java oracle and per-mod harnesses"). + + mod_port_harness.py --work WORK [--resources PREPARED] [--jars DIR --deps DIR] [MOD ...] + +WORK is a `horizon-port-pack` work folder whose mods were carried over. With --jars the NeoForge +oracle (tools/neoforge_oracle.py, dev-only) first exports WORK/oracle/facts.json. The Horizon side +is crates/horizon-client/examples/mod_port_probe.rs: the content root built from WORK/mods/*/carry +and what the game's loaders make of it. For every block and item the mod registers, compared: + +- blocks: declared in Horizon at all; per state hardness, explosion resistance, light, tool need, + friction, map colour, sounds, outline and collision shapes; +- drops: the loot of each sampled state for hand, diamond tools, shears and silk touch; +- placement: the state the block takes when placed, against its template's in the same oracle + (Horizon places the template as vanilla does, which the vanilla harnesses check); +- tags: the vanilla block tags (tool and mining tags) holding the mod's blocks; +- recipes: each recipe's result, count and ingredients; +- items: present, and their stack size. + +Each mod is probed on its own, against vanilla. Writes WORK/mods//harness/{horizon,result}.json +per mod and prints a table. Jars, facts and results stay +in WORK, outside the repository. +""" +import argparse +import json +import math +import subprocess +import sys +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +CHECKS = ['blocks', 'properties', 'sounds', 'shapes', 'drops', 'placement', 'tags', 'recipes', 'items'] +EXAMPLES = 8 + + +def key(k): + return ','.join(sorted(p for p in k.split(',') if p)) + + +def cells(boxes, grid=32): + out = set() + for b in boxes: + lo = [math.floor(round(v * grid, 6)) for v in b[:3]] + hi = [math.ceil(round(v * grid, 6)) for v in b[3:]] + for x in range(lo[0], hi[0]): + for y in range(lo[1], hi[1]): + for z in range(lo[2], hi[2]): + out.add((x, y, z)) + return out + + +def same_shape(a, b): + if a is None or b is None: + return a == b + sa, sb = sorted(map(tuple, a)), sorted(map(tuple, b)) + if sa == sb: + return True + return cells(a) == cells(b) + + +class Check: + def __init__(self): + self.passed = 0 + self.failed = 0 + self.examples = [] + + def record(self, ok, what): + if ok: + self.passed += 1 + else: + self.failed += 1 + if len(self.examples) < EXAMPLES: + self.examples.append(what) + + def json(self): + return {'passed': self.passed, 'failed': self.failed, 'examples': self.examples} + + +def sound_upgrades(oracle, horizon): + """The oracle version's vanilla sound sets → this version's, through the vanilla blocks both have.""" + out = {} + for name, hs in horizon.get('vanilla_sounds', {}).items(): + ob = oracle['blocks'].get(name) + if ob: + os_ = ob['states'].get(ob['default']) or next(iter(ob['states'].values())) + out.setdefault(json.dumps(os_['sound'], sort_keys=True), []).append(hs) + return out + + +def compare_mod(mod, oracle, horizon, carry, upgrades, old_items): + checks = {c: Check() for c in CHECKS} + prefix = mod + ':' + templates = {prefix + b: t for b, t in carry.get('blocks', {}).items()} + vanilla_tags = {t: set(m) for t, m in oracle['tags']['block'].items() if t.startswith('minecraft:')} + for name, ob in oracle['blocks'].items(): + if not name.startswith(prefix): + continue + hb = horizon['blocks'].get(name) + checks['blocks'].record(hb is not None, f'{name}: not in Horizon ({carry.get("blocks_unknown", {}).get(name[len(prefix):], "left out")})') + if hb is None: + continue + for k, os_ in ob['states'].items(): + hs = hb['states'].get(k) + if hs is None: + checks['properties'].record(False, f'{name}[{k}]: no such state in Horizon') + continue + props = [('hardness', os_['hardness'], hs['hardness']), + ('explosion_resistance', ob['explosion_resistance'], hs['explosion_resistance']), + ('emission', os_['emission'], hs['emission']), + ('requires_tool', os_['requires_tool'], hs['requires_tool']), + ('friction', ob['friction'], hs['friction']), + ('map_color', os_['map_color_id'], hs['map_color_id'])] + bad = [f'{p} {o} vs {h}' for p, o, h in props + if (abs(o - h) > 1e-6 if isinstance(o, float) else o != h)] + checks['properties'].record(not bad, f'{name}[{k}]: ' + '; '.join(bad)) + upgraded = upgrades.get(json.dumps(os_['sound'], sort_keys=True), []) + checks['sounds'].record(os_['sound'] == hs['sound'] or hs['sound'] in upgraded, + f'{name}[{k}]: {os_["sound"]["break"]} vs {hs["sound"]["break"]}') + shapes_ok = same_shape(os_['outline'], hs['outline']) and same_shape(os_['collision'], hs['collision']) + checks['shapes'].record(shapes_ok, f'{name}[{k}]: outline {os_["outline"]} vs {hs["outline"]}') + for tool, odrops in os_.get('drops', {}).items(): + hdrops = (hs.get('drops') or {}).get(tool) + checks['drops'].record(odrops == hdrops, f'{name}[{k}] {tool}: {odrops} vs {hdrops}') + # Placement: the mod block against its template, both in the oracle. + template = templates.get(name) + ot = oracle['blocks'].get(template) if template else None + if ot and 'placement' in ob: + for face, placed in ob['placement'].items(): + theirs = ot.get('placement', {}).get(face) + checks['placement'].record( + placed is not None and theirs is not None and key(placed) == key(theirs), + f'{name} on {face}: {placed} vs {template} {theirs}') + for tag, members in vanilla_tags.items(): + if name in members: + checks['tags'].record(name in horizon['block_tags'].get(tag, []), f'{name}: not in #{tag}') + for rid, orec in oracle['recipes'].items(): + if not rid.startswith(prefix): + continue + hrec = horizon['recipes'].get(rid) + if hrec is None: + checks['recipes'].record(False, f'{rid} ({orec["type"]}): not loaded') + continue + # Vanilla items added after the oracle's version (pale oak in a log tag) are not the mod's. + newer = lambda i: i.startswith('minecraft:') and old_items and i not in old_items + oing = sorted(sorted(i) for i in orec['ingredients'] if i) + hing = sorted(sorted(x for x in i if not newer(x)) for i in hrec.get('ingredients', []) if i) + ok = (orec['result'], orec['count']) == (hrec.get('result'), hrec.get('count')) and oing == hing + why = f'{rid}: {orec["result"]} x{orec["count"]} vs {hrec.get("result")} x{hrec.get("count")}' + if oing != hing: + missing = [i for i in oing if i not in hing][:2] + why += f'; ingredients differ, e.g. oracle {missing}' + checks['recipes'].record(ok, why) + for iid, oi in oracle['items'].items(): + if not iid.startswith(prefix): + continue + hi = horizon['items'].get(iid) + checks['items'].record(hi is not None and hi['max_stack_size'] == oi['max_stack_size'], + f'{iid}: {"missing" if hi is None else "stack " + str(hi["max_stack_size"])}') + result = {c: v.json() for c, v in checks.items()} + failed = sum(v.failed for v in checks.values()) + total = sum(v.passed + v.failed for v in checks.values()) + return {'mod': mod, 'passed': failed == 0 and total > 0, 'checks': result, + 'blocks_with_differences': len(carry.get('differences', {}))} + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + parser.add_argument('--work', required=True) + parser.add_argument('--resources', default=str(ROOT / '.local/horizon-client/26.3')) + parser.add_argument('--jars', help='export the oracle facts from these jars first') + parser.add_argument('--deps') + parser.add_argument('mods', nargs='*') + args = parser.parse_args() + work = Path(args.work).resolve() + facts = work / 'oracle' / 'facts.json' + if args.jars: + facts.parent.mkdir(parents=True, exist_ok=True) + command = [sys.executable, str(ROOT / 'tools/neoforge_oracle.py'), 'export', '--jars', args.jars, '--out', str(facts)] + if args.deps: + command += ['--deps', args.deps] + subprocess.run(command, check=True) + if not facts.exists(): + sys.exit(f'{facts}: no oracle facts (run with --jars, or tools/neoforge_oracle.py export)') + subprocess.run(['cargo', 'build', '--release', '-q', '-p', 'horizon-client', '--example', 'mod_port_probe'], + cwd=ROOT, check=True) + metadata = json.loads(subprocess.check_output(['cargo', 'metadata', '--no-deps', '--format-version', '1'], + cwd=ROOT, text=True)) + probe = Path(metadata['target_directory']) / 'release/examples/mod_port_probe' + oracle = json.loads(facts.read_text()) + old_items = set(oracle.get('vanilla_items', [])) + mods = args.mods or sorted(p.name for p in (work / 'mods').iterdir() if (p / 'carry').is_dir()) + rows = [] + for mod in mods: + # One mod at a time: each against vanilla, as a player adding it would load it (and a pack's + # sum may pass the block states a world holds, which the probe would refuse). + out = work / 'mods' / mod / 'harness' / 'horizon.json' + out.parent.mkdir(parents=True, exist_ok=True) + subprocess.run([str(probe), args.resources, str(work), str(out), mod], check=True) + horizon = json.loads(out.read_text()) + carry_report = work / 'mods' / mod / 'carry-report.json' + carry = json.loads(carry_report.read_text()) if carry_report.exists() else {} + rows.append(compare_mod(mod, oracle, horizon, carry, sound_upgrades(oracle, horizon), old_items)) + print(f'{"mod":<16} {"result":<7} ' + ' '.join(f'{c:>12}' for c in CHECKS)) + for result in rows: + mod = result['mod'] + (work / 'mods' / mod / 'harness').mkdir(parents=True, exist_ok=True) + (work / 'mods' / mod / 'harness' / 'result.json').write_text(json.dumps(result, indent=1)) + cols = [] + for c in CHECKS: + r = result['checks'][c] + n = r['passed'] + r['failed'] + cols.append(f'{r["passed"]}/{n}' if n else '-') + print(f'{mod:<16} {"pass" if result["passed"] else "FAIL":<7} ' + ' '.join(f'{c:>12}' for c in cols)) + + +if __name__ == '__main__': + main() diff --git a/tools/neoforge_oracle.py b/tools/neoforge_oracle.py new file mode 100644 index 000000000..7410c1fb5 --- /dev/null +++ b/tools/neoforge_oracle.py @@ -0,0 +1,242 @@ +#!/usr/bin/env python3 +"""Dev-only NeoForge oracle for Horizon's mod-port harnesses (docs/modpacks.md). + + neoforge_oracle.py export --jars DIR [--deps DIR] [--namespaces a,b] --out FACTS.json + +installs a NeoForge server under .local/neoforge-oracle (once), builds the exporter mod in +mods/horizon/src/neoforge-oracle, starts the server headless with the given mod jars, and writes every +block, item, recipe and tag of the mods' namespaces (and the vanilla blocks, for templates) to +FACTS.json. The jars are the user's own; nothing here is shipped or committed. +""" +import argparse +import json +import os +import shutil +import subprocess +import sys +import time +import urllib.request +import zipfile +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +LOCAL = ROOT / '.local' / 'neoforge-oracle' +SOURCE = ROOT / 'mods' / 'horizon' / 'src' / 'neoforge-oracle' +# ATM10 8.x: Minecraft 1.21.1 on NeoForge 21.1.259 (docs/modpacks.md). +NEOFORGE = os.environ.get('HORIZON_NEOFORGE', '21.1.259') +MAVEN = 'https://maven.neoforged.net/releases/net/neoforged/neoforge' + + +def install(): + """The NeoForge server, installed once per version.""" + home = LOCAL / NEOFORGE + args = home / f'libraries/net/neoforged/neoforge/{NEOFORGE}/unix_args.txt' + if args.exists(): + return home + home.mkdir(parents=True, exist_ok=True) + installer = LOCAL / f'neoforge-{NEOFORGE}-installer.jar' + if not installer.exists(): + url = f'{MAVEN}/{NEOFORGE}/neoforge-{NEOFORGE}-installer.jar' + with urllib.request.urlopen(url, timeout=120) as r: + installer.write_bytes(r.read()) + with (LOCAL / f'install-{NEOFORGE}.log').open('w') as log: + subprocess.run(['java', '-jar', str(installer), '--installServer', str(home)], + cwd=LOCAL, stdout=log, stderr=subprocess.STDOUT, check=True) + return home + + +def client(): + """The NeoForge client (tools/perf_bench.py --game neoforge), installed once per version: the installer's + own files, then the vanilla libraries its version inherits, fetched as the launcher does (sha1-checked).""" + sys.path.insert(0, str(ROOT / 'tools')) + import hashlib + import lab + home = LOCAL / f'client-{NEOFORGE}' + profile = home / 'versions' / f'neoforge-{NEOFORGE}' / f'neoforge-{NEOFORGE}.json' + if not profile.exists(): + install() # the installer jar + home.mkdir(parents=True, exist_ok=True) + (home / 'launcher_profiles.json').write_text('{"profiles":{}}') + with (LOCAL / f'install-client-{NEOFORGE}.log').open('w') as log: + subprocess.run(['java', '-jar', str(LOCAL / f'neoforge-{NEOFORGE}-installer.jar'), '--installClient', + str(home)], cwd=LOCAL, stdout=log, stderr=subprocess.STDOUT, check=True) + neo = json.loads(profile.read_text()) + base = json.loads((home / 'versions' / neo['inheritsFrom'] / f'{neo["inheritsFrom"]}.json').read_text()) + jars = [] + for manifest in (neo, base): + names = {library['name'] for library in manifest['libraries']} + for library in manifest['libraries']: + artifact = library.get('downloads', {}).get('artifact') + if not artifact or not lab.library_allowed(library) or not lab.native_library_matches(library, None, names): + continue + path = home / 'libraries' / artifact['path'] + if not path.exists(): + with urllib.request.urlopen(artifact['url'], timeout=120) as r: + data = r.read() + if hashlib.sha1(data).hexdigest() != artifact['sha1']: + sys.exit(f'{artifact["url"]}: sha1 mismatch') + path.parent.mkdir(parents=True, exist_ok=True) + path.write_bytes(data) + if path not in jars: + jars.append(path) + return home, neo, base, jars + + +def client_command(java, heap, game_dir, natives, assets, jvm=(), game=()): + """The command the launcher would run for the NeoForge client profile, with no account.""" + home, neo, base, jars = client() + import lab + values = {'library_directory': str(home / 'libraries'), 'classpath_separator': os.pathsep, + 'version_name': f'neoforge-{NEOFORGE}', 'natives_directory': str(natives), + 'launcher_name': 'horizon-bench', 'launcher_version': '1'} + expand = lambda s: s.replace('${', '{').format(**values) + command = [str(java), f'-Xmx{heap}'] + for argument in base['arguments']['jvm']: + if isinstance(argument, dict): + if lab.library_allowed(argument): + command += [argument['value']] if isinstance(argument['value'], str) else argument['value'] + elif argument == '${classpath}': + # Every library; not the vanilla jar, which NeoForge's patched client replaces. + command.append(os.pathsep.join(map(str, jars))) + else: + command.append(expand(argument)) + command += [expand(a) for a in neo['arguments']['jvm']] + command += [*jvm, neo['mainClass']] + command += ['--username', 'Bench', '--uuid', '00000000000000000000000000000000', '--accessToken', '0', + '--version', f'neoforge-{NEOFORGE}', '--gameDir', str(game_dir), '--assetsDir', str(assets), + '--assetIndex', base['assetIndex']['id'], '--userType', 'msa', '--versionType', 'release', + *neo['arguments']['game'], *game] + return home, command + + +def build_bench(home): + """The NeoForge benchmark driver (mods/horizon/src/neoforge-bench), against the client.""" + return build(home, ROOT / 'mods/horizon/src/neoforge-bench', 'horizon-bench-neoforge.jar', client_classpath(home)) + + +def client_classpath(home): + libs = home / 'libraries' + first = [libs / f'net/neoforged/neoforge/{NEOFORGE}/neoforge-{NEOFORGE}-client.jar', + libs / f'net/neoforged/neoforge/{NEOFORGE}/neoforge-{NEOFORGE}-universal.jar'] + rest = sorted(p for p in libs.rglob('*.jar') if p not in first and 'slim' not in p.name + and 'extra' not in p.name and not p.name.endswith('-unpacked.jar')) + return os.pathsep.join(map(str, first + rest)) + + +def classpath(home): + libs = home / 'libraries' + first = [libs / f'net/neoforged/neoforge/{NEOFORGE}/neoforge-{NEOFORGE}-server.jar', + libs / f'net/neoforged/neoforge/{NEOFORGE}/neoforge-{NEOFORGE}-universal.jar'] + rest = sorted(p for p in libs.rglob('*.jar') if p not in first and 'slim' not in p.name + and 'extra' not in p.name and not p.name.endswith('-unpacked.jar')) + return os.pathsep.join(map(str, first + rest)) + + +def build(home, source=SOURCE, name='horizon-oracle.jar', cp=None): + """A dev-only mod jar (by default the exporter), rebuilt when its sources change.""" + out = LOCAL / 'build' + jar = out / name + sources = sorted((source / 'java').rglob('*.java')) + resources = sorted(p for p in (source / 'resources').rglob('*') if p.is_file()) + newest = max(p.stat().st_mtime for p in sources + resources) + if jar.exists() and jar.stat().st_mtime > newest: + return jar + classes = out / f'classes-{Path(name).stem}' + shutil.rmtree(classes, ignore_errors=True) + classes.mkdir(parents=True) + javac = subprocess.run(['javac', '--release', '21', '-proc:none', '-nowarn', '-cp', cp or classpath(home), + '-d', str(classes), *map(str, sources)], capture_output=True, text=True) + if javac.returncode: + sys.exit(f'{name} does not compile:\n{javac.stdout}{javac.stderr}') + with zipfile.ZipFile(jar, 'w', zipfile.ZIP_DEFLATED) as z: + for p in sorted(classes.rglob('*.class')): + z.write(p, p.relative_to(classes).as_posix()) + for p in resources: + z.write(p, p.relative_to(source / 'resources').as_posix()) + return jar + + +SERVER_PROPERTIES = """level-type=minecraft\\:flat +online-mode=false +server-port=0 +spawn-protection=0 +view-distance=2 +simulation-distance=2 +generate-structures=false +spawn-monsters=false +spawn-animals=false +""" + + +def instance(name, jars, extra=()): + """A server folder with `jars` (and the exporter) in mods/.""" + home = install() + run = LOCAL / 'runs' / name + shutil.rmtree(run, ignore_errors=True) + (run / 'mods').mkdir(parents=True) + (run / 'libraries').symlink_to(home / 'libraries') + (run / 'eula.txt').write_text('eula=true\n') + (run / 'server.properties').write_text(SERVER_PROPERTIES) + for jar in [*jars, *extra]: + (run / 'mods' / Path(jar).name).symlink_to(Path(jar).resolve()) + return home, run + + +def start(home, run, props, log_name='server.log', timeout=900): + """Runs the server to its end; returns (seconds, the log).""" + args = home / f'libraries/net/neoforged/neoforge/{NEOFORGE}/unix_args.txt' + command = ['java', '-Xmx4G', *[f'-D{k}={v}' for k, v in props.items()], f'@{args}', 'nogui'] + began = time.monotonic() + with (run / log_name).open('w') as log: + subprocess.run(command, cwd=run, stdin=subprocess.DEVNULL, stdout=log, + stderr=subprocess.STDOUT, timeout=timeout) + return time.monotonic() - began, (run / log_name).read_text(errors='replace') + + +def mod_ids(jar): + """The mod ids a jar declares (its neoforge.mods.toml).""" + import tomllib + with zipfile.ZipFile(jar) as z: + for name in ('META-INF/neoforge.mods.toml', 'META-INF/mods.toml'): + if name in z.namelist(): + try: + toml = tomllib.loads(z.read(name).decode('utf-8', 'replace')) + except tomllib.TOMLDecodeError: + return [] + return [m.get('modId', '') for m in toml.get('mods', [])] + return [] + + +def export(args): + jars = sorted(Path(args.jars).glob('*.jar')) + deps = sorted(Path(args.deps).glob('*.jar')) if args.deps else [] + namespaces = args.namespaces or ','.join(i for j in jars for i in mod_ids(j)) + home = install() + exporter = build(home) + home, run = instance('export', jars, [*deps, exporter]) + out = Path(args.out).resolve() + out.unlink(missing_ok=True) + seconds, log = start(home, run, {'horizon.oracle.out': out, 'horizon.oracle.namespaces': namespaces}) + if not out.exists(): + tail = '\n'.join(log.splitlines()[-40:]) + sys.exit(f'the oracle wrote nothing; the end of {run / "server.log"}:\n{tail}') + facts = json.loads(out.read_text()) + mods = sum(1 for b in facts['blocks'] if not b.startswith('minecraft:')) + print(f'{out}: {mods} mod blocks, {len(facts["items"])} items, {len(facts["recipes"])} recipes ' + f'({namespaces}); server ran {seconds:.1f} s') + + +def main(): + parser = argparse.ArgumentParser(description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) + sub = parser.add_subparsers(dest='command', required=True) + e = sub.add_parser('export', help='write the facts of the mods in --jars') + e.add_argument('--jars', required=True, help='the mods to export') + e.add_argument('--deps', help='their required libraries (loaded, not exported)') + e.add_argument('--namespaces', help='comma-separated; default: the jars\' mod ids') + e.add_argument('--out', required=True) + args = parser.parse_args() + {'export': export}[args.command](args) + + +if __name__ == '__main__': + main() diff --git a/tools/perf_bench.py b/tools/perf_bench.py index 96fb7c8e7..013df41d6 100644 --- a/tools/perf_bench.py +++ b/tools/perf_bench.py @@ -13,6 +13,11 @@ for every game. RSS is sampled from outside the process for every game too. python3 tools/perf_bench.py --game vanilla-mods --label mods-m4 python3 tools/perf_bench.py --table horizon-m4 vanilla-m4 mods-m4 +A ported mod pack against the same mods on their own loader (docs/modpacks.md, "Measured"): + + python3 tools/perf_bench.py --game horizon --port-work WORK --label pack-horizon + python3 tools/perf_bench.py --game neoforge --mod-jars JARS --deps DEPS --label pack-neoforge + Games: horizon target/release/horizon-client, driven over its control socket vanilla your own 26.3 client.jar + Fabric Loader + a dev-only driver @@ -20,6 +25,13 @@ Games: is redistributed vanilla-mods the same + Sodium, Lithium, FerriteCore and ImmediatelyFast (pinned in packs/mods.lock.json) + neoforge Minecraft 1.21.1 on NeoForge (dev-only, tools/neoforge_oracle.py + installs it under .local/neoforge-oracle) + a driver mod + (mods/horizon/src/neoforge-bench) + the jars of --mod-jars and + --deps, the user's own; 26.3 options it does not know are ignored + +--port-work loads the mods a horizon-port-pack work folder carried over into +Horizon as dev mods. Results: benchmarks/perf/