diff --git a/docs/server-levels.md b/docs/server-levels.md new file mode 100644 index 000000000..0190c0c03 --- /dev/null +++ b/docs/server-levels.md @@ -0,0 +1,90 @@ +# A server's levels: one per dimension + +Vanilla's `MinecraftServer` holds a `ServerLevel` per dimension, and each player is in one of them. +Horizon's server does the same. That covers the dedicated server and single player's server thread +(`Game::serving`, the default since #1114). So a LAN world's host and each guest can be in different +dimensions at the same time. + +## How the single-level `Game` maps onto vanilla's levels + +The game's tick code reads one level: the one held in `Game`'s own fields (the world, its streaming +and save, block ticks, entities, block entities and their per-level queues). `game/levels.rs` keeps +the other dimensions' levels as `Parked` values and swaps one into place (`enter_level`). The swap is +`mem::swap` of each per-level field, so the tick code runs unchanged, one level at a time. + +| Vanilla | Horizon | +| --- | --- | +| `MinecraftServer.levels` | the level in place plus `Levels::parked` | +| `MinecraftServer.tickChildren`, every level | `tick_server`: each level with a player in it, in Overworld, Nether, End order. The first pass reads the network and runs the shared parts (clock, weather, functions, autosave). | +| `ServerLevel.players` | `Hub::here`: the guests whose `dimension` is the level in place | +| `ServerPlayer.teleport` into another level | `server_travel.rs`: a Respawn packet, then the teleport once the destination has loaded (portals and `execute in … tp`) | +| `PlayerList.respawn` | `guest_respawn`: back into the Overworld's level | +| `ServerLevel.noPlayersEmptyTime`, chunk unloading | `close_idle_levels`: a level other than the Overworld with nobody in it for 300 ticks is saved and closed | +| the levels' shared `LevelData` | not swapped: game time, day time, weather, game rules, scoreboard, maps, entity ids (`next_entity`, unique across levels) | + +The packets of a guest are read in its own level's pass (`Hub::tick_level`), so its actions and +commands run with its level in place. + +The window's own game (`--no-server-thread`, `HORIZON_SERVER_THREAD=0`, and the command harness) keeps +one level, which follows its player (`enter_dimension`). LAN guests of a world run that way stay in the +host's dimension. That mode is the escape hatch now, so it gets no multi-level work. + +## One code path for both + +The dedicated server and the server thread are the same code. The server thread runs a `Game` with +`options.dedicated` set and a hub without a host player. The window joins it through memory as one +more player. Everything above serves both, and `server_dimension_players` (vanilla clients on Horizon's +dedicated server) and `lan_e2e --server-thread` (Horizon clients on the server thread) check it from +the two sides. + +## Cost of an extra level + +Measured on the Mac mini (M4 Pro) at render distance 12, on 2026-10-22. The host and a guest each +played a fresh world on the server thread. RSS is the host's whole process, window included. Tick time +is `/tick query`'s average over 100 ticks. + +| Where the players are | Average tick | P95 | Host RSS | +| --- | --- | --- | --- | +| both in the Overworld | 1.0 ms | 1.3 ms | 1194 MB | +| guest in the Nether (two levels tick) | 2.2 ms | 2.9 ms | 1238 MB | +| guest in the End (the Nether still parked) | 0.9 ms | 1.1 ms | 1324 MB | +| both back, the Nether and End closed after 300 idle ticks | 0.8 ms | 0.9 ms | 1335 MB | + +- A level costs only while a player is in it: its chunks around that player, their entities, and the + tick. With nobody outside the Overworld, `tick_server` runs one pass, as before. +- A closed level's memory goes back to the allocator, not always to the system. +- Entering a level the first time opens its region files and streaming (`open_level`). That cost + comes once, before the player's chunks load. + +## Saving + +`save_game` saves the level in place, then every parked level (`save_parked_levels`). Each level has +its own region folder, as in vanilla (`DIM-1`, `DIM1`). A level closed for being idle is saved first. +Each player's saved data holds its dimension (`Dimension`). A guest who leaves in +the Nether, or whose host saves and quits, joins again in the Nether where it was. + +## What's checked + +`python3 tools/lan_e2e.py --server-thread --check portals --check levels`: + +- `portals`: the guest goes to the Nether through a portal while the host stays. The host then goes + to the End while the guest is in the Nether, so three levels run at once, and both come back. +- `levels`, with the host and the guest each in a level of its own: + - `execute in the_nether run tp` moves the guest alone. + - Mobs and armour stands fall in two levels at once, and every entity id is unique across the + levels. + - The guest leaves in the Nether and joins again there. + - The guest dies in the Nether and respawns in the Overworld. + - The host takes a Nether portal alone. + - The guest takes an End portal and then the exit portal alone. + - The host saves, quits and opens the world again with the guest in the Nether, and the guest + joins again there. + +## Not done yet + +- **End credits for server players.** The first exit from the End should show a player the credits + (`EndPortalBlock.entityInside`, `seenCredits`). The client's WinScreen is #338. +- **Entities through portals between levels.** Mobs, items and minecarts that enter a portal on a + server, and portal tickets (`TeleportTransition.PLACE_PORTAL_TICKET`), are not handled. +- **Unlimited selectors.** `@e` without a distance or volume reads only the source's level. Vanilla's + reads every level. diff --git a/docs/server.md b/docs/server.md index d7959aed0..6a73e768f 100644 --- a/docs/server.md +++ b/docs/server.md @@ -90,7 +90,7 @@ not tick cost, so it is in scope here (see section 9). |---|---| | `online-mode=true` | **Works** (ServerLoginPacketListenerImpl): a 1024-bit RSA key pair at start ("Generating keypair"), the encryption request, the shared secret and challenge, AES/CFB8, and the session service's `hasJoined` check (with the address when `prevent-proxy-connections` is on), with vanilla's log lines and messages. `test_online_mode_parity.py` runs the same logins against vanilla's server and Horizon's with a mock session service. | | Secure chat | **Works** (`SignedMessageChain`, `LastSeenMessagesValidator`, `MessageSignatureCache`): the chat session update and its profile key, checked against the services' `playerCertificateKeys`; signed chat, `say`, `me` and `msg`, relayed with their signatures and last-seen lists; `enforce-secure-profile` (with `online-mode`) refuses unsigned chat; and vanilla's log lines (`[Not Secure]`, the decoder's warnings, the kicks). Offline servers and players without a chat session chat unsigned, as on vanilla. `test_secure_chat_parity.py` runs the same signed and unsigned players against vanilla's server and Horizon's, with test keys and a mock services server. | -| Nether and End | **Players can't change dimension.** The engine keeps one dimension loaded (`Game::enter_dimension` moves only the local single-player player), and the hub serves one `dimension` to everyone. | +| Nether and End | **Works**: the server holds a level per dimension, and each player is in its own (portals, `execute in … tp`, respawn). `test_server_dimension_players_parity.py` checks it against vanilla. How it works, its cost and what's left: [server-levels.md](server-levels.md). | | Refused logins | Logged as vanilla does: `Disconnecting (/address): `, then ` (/address) lost connection: `, and the session errors (`Username '' tried to join with an invalid session`, `Couldn't verify username because servers are unavailable`). | | Gameplay for remote players | Checked against vanilla's server with a vanilla-codec client (#484, `tools/test_dedicated_server_parity.py`): a right click with an item on a block uses it once, a dead player's commands run, players on a team without friendly fire don't hurt each other, and a player's projectiles are theirs (their ender pearl takes them). The recipe book, status effects and block sync while digging (#500) work. Left: a player's projectiles don't hit other players (no bow PvP between guests), and a mob a guest kills counts in the host's statistics and advancements, not the guest's (`mob_died` runs as the host). | | Defaults that surprise owners | `white-list=true` is vanilla 26.3's default, so a fresh server refuses everyone until they are added. That matches vanilla, so the first-run flow (section 3) asks about the whitelist up front. | diff --git a/tools/lan_e2e.py b/tools/lan_e2e.py index c5a90150f..b66e3840b 100755 --- a/tools/lan_e2e.py +++ b/tools/lan_e2e.py @@ -67,6 +67,10 @@ address. The script then checks, from both sides, what a guest's game used to ge - portals: (--server-thread) the server holds a level per dimension: the guest goes to the Nether through a portal while the host stays, then the host goes to the End while the guest is in the Nether, and both come back. +- levels: (--server-thread) the host and the guest each in a level of their own: `execute in` builds + and teleports into another level, mobs fall in two levels at once with ids unique across + them, the guest rejoins in the Nether, dies there and respawns in the Overworld, the host + takes a Nether portal alone, the guest takes an End portal and the exit portal alone, and the host saves, quits and reopens with the guest in the Nether. - commands: the guest's chat completes commands and their arguments over the server's command tree (Tab), and runs what it completed; - recipes: the guest's recipe book lists the planks recipe the logs unlock, and clicking it @@ -172,7 +176,7 @@ ROOT = Path(__file__).resolve().parents[1] GUEST_NAME = 'Guest' # The host plays single player on its server thread (--server-thread). SERVER_THREAD = False -CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts'] +CHECKS = ['tab', 'self', 'armour', 'nametags', 'walls', 'chest', 'crafting', 'sign', 'damage', 'mobs', 'hazards', 'pvp', 'arrows', 'kills', 'hurt', 'respawn', 'containers', 'feedback', 'titles', 'chat', 'commands', 'recipes', 'machines', 'brewing', 'loom', 'stonecutter', 'grindstone', 'anvil', 'smithing', 'enchanting', 'beacon', 'crafter', 'lectern', 'cartography', 'trials', 'clicks', 'sleep', 'predict', 'push', 'eat', 'eating', 'xp', 'objects', 'projectiles', 'worn', 'blast', 'barter', 'deflect', 'fireball', 'light', 'time', 'swings', 'cracks', 'plates', 'effects', 'portals', 'distance', 'growth', 'flight', 'ladder', 'boats', 'minecarts', 'frames', 'lightning', 'riding', 'mounts', 'levels'] class Game: @@ -509,12 +513,17 @@ def portals(host, guest, out, x, y, z, hx, hy, hz): guest('command', command=f'tp @s {x + 0.5} {y} {z + 3.5}') # players_nether_portal_default_delay: 80 ticks in survival. there = wait_dimension(guest, 'minecraft:the_nether', 600) - guest.wait(40) + # On a server the guest is held where it entered until the chunks around the destination are + # lit, then placed in its portal: wait for that before reading where it arrived. + for _ in range(30): + guest.wait(20) + arrived = [int(v // 1) for v in pos_of(guest)] + if guest.raw('block', *arrived).get('block') == 'minecraft:nether_portal': + break guest.shot(out, 'portals-nether') host_dim = host('state').get('dimension') assert there.get('dimension') == 'minecraft:the_nether', f'the guest is in {there.get("dimension")}' assert host_dim == 'minecraft:overworld', f'the host went to {host_dim} with the guest' - arrived = [int(v // 1) for v in pos_of(guest)] below = guest.raw('block', arrived[0], arrived[1] - 1, arrived[2]) assert below.get('loaded') and below.get('block') != 'minecraft:air', f'under the guest in the Nether: {below}' # The host takes an End portal while the guest is in the Nether: three levels at once. @@ -542,6 +551,209 @@ def portals(host, guest, out, x, y, z, hx, hy, hz): return {'guest_nether': arrived, 'host_end': [ex, ey, ez], 'back': pos_of(guest)} +NETHER, END, OVERWORLD = 'minecraft:the_nether', 'minecraft:the_end', 'minecraft:overworld' + + +def check_levels(host, guest, out): + """(--server-thread) Each player in its own level (MinecraftServer.levels), the host and the guest + apart: the steps run in order, each reported, and the check fails if any did.""" + if not SERVER_THREAD: + return 'skipped (needs --server-thread)' + start = {g.name: pos_of(g) for g in (host, guest)} + steps, failed = {}, [] + guest('command', command='gamemode creative') + try: + for name, step in LEVEL_STEPS: + try: + steps[name] = step(host, guest, out) + except (Failed, AssertionError, StopIteration, KeyError) as e: + steps[name], failed = repr(e), failed + [name] + for g in (host, guest): + back_to_game(g) + print(f'[lan] levels {name}: {"FAIL" if name in failed else "PASS"} {steps[name]}') + finally: + for g in (host, guest): + back_to_game(g) + if g('state').get('screen') == 'dead': + g.raw('respawn') + g.wait(20) + x, y, z = start[g.name] + g('command', command=f'execute in {OVERWORLD} run tp @s {x:.2f} {y:.2f} {z:.2f}') + guest('command', command='gamemode survival') + guest.wait(40) + assert not failed, f'failed: {failed}; {steps}' + return steps + + +def level_floor(game, x, y, z): + """An obsidian floor under x y z with air above it, in the level `game` is in (fill needs the + chunks loaded: ServerLevel.hasChunksAt).""" + game('command', command=f'fill {x - 4} {y - 1} {z - 4} {x + 4} {y - 1} {z + 4} minecraft:obsidian') + game('command', command=f'fill {x - 4} {y} {z - 4} {x + 4} {y + 4} {z + 4} minecraft:air') + + +def into_level(game, dimension, x, y, z): + """`execute in DIMENSION run tp` to x y z, onto a floor built once the level has loaded around it.""" + game('command', command=f'execute in {dimension} run tp @s {x + 0.5} {y} {z + 0.5}') + state = wait_dimension(game, dimension, 400) + game.wait(20) + level_floor(game, x, y, z) + game('command', command=f'tp @s {x + 0.5} {y} {z + 0.5}') + game.wait(20) + return state + + +def dims(host, guest): + return host('state').get('dimension'), guest('state').get('dimension') + + +def level_execute(host, guest, out): + """`execute in the_nether run tp` takes the guest alone.""" + x, z = (int(v // 1) for v in pos_of(guest)[::2]) + there = into_level(guest, NETHER, x, 100, z) + below = guest.raw('block', x, 99, z) + assert dims(host, guest) == (OVERWORLD, NETHER), f'host, guest in {dims(host, guest)}' + assert there.get('dimension') == NETHER and below.get('block') == 'minecraft:obsidian', f'{there} on {below}' + return {'guest': pos_of(guest)} + + +def level_ticking(host, guest, out): + """Mobs and objects fall in both levels at once (each level with a player ticks); every entity + has its own id across the levels.""" + gx, gy, gz = pos_of(guest) + hx, hy, hz = pos_of(host) + # A floor of its own beside the host (earlier checks may have broken the open one there). + bx, by, bz = int(hx // 1), int(hy // 1), int(hz // 1) + host('command', command=f'fill {bx - 4} {by - 1} {bz - 4} {bx + 4} {by - 1} {bz - 2} minecraft:stone') + host('command', command=f'fill {bx - 4} {by} {bz - 4} {bx + 4} {by + 4} {bz - 2} minecraft:air') + guest('command', command=f'summon minecraft:pig {gx + 2.5:.2f} {gy + 3:.2f} {gz:.2f}') + guest('command', command=f'summon minecraft:armor_stand {gx - 2.5:.2f} {gy + 3:.2f} {gz:.2f}') + host('command', command=f'summon minecraft:pig {hx + 2.5:.2f} {hy + 3:.2f} {hz - 3:.2f}') + host('command', command=f'summon minecraft:armor_stand {hx - 2.5:.2f} {hy + 3:.2f} {hz - 3:.2f}') + try: + guest.wait(60) + seen = {} + for name, g, floor in (('guest', guest, gy), ('host', host, hy)): + mobs = [m for m in g.raw('mobs', 8)['near'] if m['kind'] == 'minecraft:pig'] + stands = [o for o in g('status')['objects'] if o['kind'] == 'minecraft:armor_stand'] + seen[name] = {'pigs': [(m['id'], round(m['pos'][1], 2)) for m in mobs], + 'stands': [(o['id'], round(o['position'][1], 2)) for o in stands], 'floor': floor} + finally: + guest('command', command='kill @e[type=!minecraft:player,distance=..8]') + host('command', command='kill @e[type=!minecraft:player,distance=..8]') + for name, s in seen.items(): + heights = [y for _, y in s['pigs'] + s['stands']] + assert len(s['pigs']) == 1 and len(s['stands']) == 1, f'{name} sees {s}' + assert all(abs(y - s['floor']) < 0.1 for y in heights), f"{name}'s did not fall to the floor: {seen}" + ids = [i for s in seen.values() for i, _ in s['pigs'] + s['stands']] + assert len(set(ids)) == len(ids), f'ids shared across the levels: {seen}' + return seen + + +def level_rejoin(host, guest, out): + """The guest leaves in the Nether and joins again there (its player data keeps its level).""" + before = pos_of(guest) + port = host.raw('lan', 'status')['port'] + guest.raw('disconnect') + time.sleep(2) + guest.raw('connect', f'127.0.0.1:{port}', GUEST_NAME) + guest.raw('wait-for', 'loaded', '--timeout', 300) + guest.wait(20) + after = pos_of(guest) + assert dims(host, guest) == (OVERWORLD, NETHER), f'host, guest in {dims(host, guest)}' + assert all(abs(a - b) < 1 for a, b in zip(before, after)), f'left at {before}, back at {after}' + return {'at': after} + + +def level_death(host, guest, out): + """The guest dies in the Nether and respawns in the Overworld; the host stays where it is.""" + guest('command', command='kill @s') + guest.wait(20) + guest.raw('respawn') + back = wait_dimension(guest, OVERWORLD, 400) + guest.wait(20) + assert back.get('dimension') == OVERWORLD, f'the guest respawned in {back.get("dimension")}' + assert dims(host, guest) == (OVERWORLD, OVERWORLD), f'host, guest in {dims(host, guest)}' + return {'respawned': back['player']['position']} + + +def level_host_nether(host, guest, out): + """The host goes through a Nether portal while the guest stays in the Overworld.""" + hx, hy, hz = (int(v // 1) for v in pos_of(host)) + nether_portal(host, hx, hy, hz - 3) + try: + host('command', command=f'tp @s {hx + 0.5} {hy} {hz - 2.5}') + there = wait_dimension(host, NETHER, 600) + host.wait(20) + result = dims(host, guest) + assert there.get('dimension') == NETHER, f'the host is in {there.get("dimension")}' + assert result == (NETHER, OVERWORLD), f'host, guest in {result}' + return {'host': there['player']['position']} + finally: + host('command', command=f'execute in {OVERWORLD} run fill {hx - 1} {hy} {hz - 3} {hx + 2} {hy + 3} {hz - 3} ' + 'minecraft:air') + host('command', command=f'execute in {OVERWORLD} run tp @s {hx + 0.5} {hy} {hz + 0.5}') + wait_dimension(host, OVERWORLD, 400) + + +def level_guest_end(host, guest, out): + """The guest steps into an End portal alone and lands on the obsidian platform; an exit portal + takes it back to the Overworld.""" + x, y, z = (int(v // 1) for v in pos_of(guest)) + guest('command', command=f'setblock {x + 2} {y} {z} minecraft:end_portal') + try: + guest('command', command=f'tp @s {x + 2.5} {y} {z + 0.5}') + wait_dimension(guest, END, 600) + # The arrival waits for the platform's chunks; its portal cooldown (10 ticks) runs out after, + # and a player who stands in a portal while it runs stays (Entity.setAsInsidePortal). + guest.wait(60) + landed = pos_of(guest) + assert dims(host, guest) == (OVERWORLD, END), f'host, guest in {dims(host, guest)}' + assert [int(v // 1) for v in landed] == [100, 49, 0], f'the guest landed at {landed}' + finally: + guest('command', command=f'execute in {OVERWORLD} run setblock {x + 2} {y} {z} minecraft:air') + ex, ey, ez = (int(v // 1) for v in landed) + guest('command', command=f'setblock {ex + 2} {ey} {ez} minecraft:end_portal') + guest('command', command=f'tp @s {ex + 2.5} {ey} {ez + 0.5}') + home = wait_dimension(guest, OVERWORLD, 600) + guest.wait(20) + assert dims(host, guest) == (OVERWORLD, OVERWORLD), f'host, guest in {dims(host, guest)}' + return {'landed': landed, 'home': home['player']['position']} + + +def level_reload(host, guest, out): + """The host saves and quits with the guest in the Nether, opens the world again and opens it to the + LAN; the guest joins again where it was, in the Nether.""" + x, z = (int(v // 1) for v in pos_of(guest)[::2]) + into_level(guest, NETHER, x, 100, z) + before = pos_of(guest) + host.raw('disconnect') + # The world saves on its server thread before the title screen shows. + for attempt in range(60): + time.sleep(1) + try: + host.raw('open', 'Lan') + break + except Failed: + if attempt == 59: + raise + host('wait_ready', seconds=400) + port = host.raw('lan', 'open', 0, 'commands')['port'] + back_to_game(guest) + guest.raw('connect', f'127.0.0.1:{port}', GUEST_NAME) + guest.raw('wait-for', 'loaded', '--timeout', 300) + guest.wait(20) + after = pos_of(guest) + assert dims(host, guest) == (OVERWORLD, NETHER), f'host, guest in {dims(host, guest)}' + assert all(abs(a - b) < 1 for a, b in zip(before, after)), f'left at {before}, back at {after}' + return {'at': after} + + +LEVEL_STEPS = [('execute', level_execute), ('ticking', level_ticking), ('rejoin', level_rejoin), + ('death', level_death), ('host_nether', level_host_nether), + ('guest_end', level_guest_end), ('reload', level_reload)] + + def check_damage(host, guest, out): guest('command', command='gamemode survival') guest.wait(10) @@ -3075,7 +3287,8 @@ CHECK_FNS = {'tab': check_tab, 'self': check_self, 'armour': check_armour, 'name 'xp': check_xp, 'objects': check_objects, 'projectiles': check_projectiles, 'blast': check_blast, 'light': check_light, 'plates': check_plates, 'swings': check_swings, 'cracks': check_cracks, - 'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict, 'ladder': check_ladder, 'boats': check_boats, 'minecarts': check_minecarts, 'frames': check_frames, 'lightning': check_lightning, 'riding': check_riding, 'mounts': check_mounts} + 'time': check_time, 'growth': check_growth, 'flight': check_flight, 'fireball': check_fireball, 'deflect': check_deflect, 'barter': check_barter, 'worn': check_worn, 'kills': check_kills, 'arrows': check_arrows, 'predict': check_predict, 'ladder': check_ladder, 'boats': check_boats, 'minecarts': check_minecarts, 'frames': check_frames, 'lightning': check_lightning, 'riding': check_riding, 'mounts': check_mounts, + 'levels': check_levels} def run(ctl, out, resources, checks):