[PR #950] [MERGED] Server: spawn mobs around every player, and despawn from the nearest of them #2932

Closed
opened 2026-11-02 16:00:13 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/horizon-rust/horizon/pull/950
Author: @ludovic111
Created: 10/19/2026
Status: ✅ Merged
Merged: 10/19/2026
Merged by: @ludovic111

Base: main ← Head: fix/natural-spawn-every-player


📝 Commits (1)

  • 08c77ad Spawn mobs around every player, and despawn from the nearest of them

📊 Changes

7 files changed (+179 additions, -73 deletions)

View changed files

📝 crates/horizon-client/examples/mob_ai_probe.rs (+1 -0)
📝 crates/horizon-client/src/game/creatures.rs (+11 -21)
📝 crates/horizon-client/src/game/lightning.rs (+6 -2)
📝 crates/horizon-client/src/game/natural_spawning.rs (+136 -41)
📝 crates/horizon-client/src/game/wandering_trader.rs (+18 -7)
📝 crates/horizon-client/src/mobs.rs (+3 -0)
📝 crates/horizon-client/src/mobs/ai.rs (+4 -2)

📄 Description

🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself.

Follows #931 (#890). Natural spawning and the systems next to it no longer depend on a local player.

The problem

spawn_natural returned when there was no local player. A dedicated server or a host on --server-thread spawned no mobs naturally, and a LAN host spawned them only around itself. Inhabited time, thunder, the wandering trader and despawning also used only the local player, or every loaded chunk.

What vanilla does

ServerChunkCache.tickChunks:

  • NaturalSpawner.createState(getNaturalSpawnChunkCount(), …, new LocalMobCapCalculator(chunkMap)). The global caps scale with the size of the natural spawn chunk tracker: every chunk within 8 chunks (Chebyshev) of any player. Each mob also counts against the local cap of every non-spectator within 128 blocks of its chunk.
  • ChunkMap.collectSpawningChunks: the tracker's chunks that are ticking and within 128 blocks of a non-spectator. They are shuffled with the level random. Then tickSpawningChunk runs on each: inhabited time +1, tickThunder if the chunk is in entity-ticking range, then spawnForChunk.
  • WanderingTraderSpawner.spawn picks ServerLevel.getRandomPlayer(), one of the living players, drawn from the level random.
  • Mob.checkDespawn measures from getNearestPlayer(this, -1): the nearest non-spectator, of any kind, alive or dead.

What this changes

  • Game::level_players lists every player in the level: the local player, and the LAN guests or server players in it.
  • spawning_chunks is collectSpawningChunks over all of them, and also returns the tracker's size for the global caps. The SpawnState gets every player, so spawn_selection's local caps (already per player, covered by the natural_spawn harness) see them all. The spawner's nearest-player, obstruction and "player near" checks (NaturalLevel, SpawnLevel, SpawnerLevel) read every player.
  • Inhabited time is counted in the same spawning chunks, in spawn_natural; tick_inhabited is gone. Thunder rolls only in spawning chunks that are in entity-ticking range, no longer in every loaded chunk.
  • The wandering trader ticks without a local player and picks a random living player, as vanilla does.
  • MobWorld::nearest_player is the despawn reference, set per mob from every player. Before, it was the player the mob was bound to, which could be a far survival guest while a creative player stood next to it. A lone spectator no longer makes far mobs despawn, as in vanilla.
  • The single-player candidate order is unchanged (the 17×17 square around the player), so a game with one player draws the same randoms as before. Vanilla's order is its hash map's, which isn't reproducible anyway.

Proof

Check Result
cargo clippy --workspace --all-targets -D warnings clean
cargo test -p horizon-client all ok (lib 891 passed)
parity.py --changed origin/main (before the rebase, a1eadd24) 98 passed, 0 failed (natural_spawn, spawn_rules, spawner, finalize_spawn, mob_ai, dedicated_server, world_spawn among them)
integration commit with #951 and #952 over a9bcaa6e: parity.py --changed origin/main 301 passed, 0 failed (every harness)
lan_e2e --check mobs --check damage --check respawn --check growth --check time all PASS
the same with --server-thread all PASS

dedicated_server matches vanilla byte for byte.

Dedicated-server check. This is an ad hoc script, not in the repo. It starts horizon-server on a flat world at midnight, with pause-when-empty-seconds=-1, and joins two idle horizon-bots, 4243 blocks apart. Every 15 s it counts the monsters within 128 blocks of each bot with execute positioned … if entity @e[type=…,distance=..128].

Build Monsters near each bot Monsters in all Creatures
main (ThinkPad) 0, 0 0 0
this branch (Mac mini, 150 s) 68 to 72 each 139 to 144 22
integration (ThinkPad) 70 to 72 each 140 to 142 24

The local cap is 70 monsters around each player, and a pack can overshoot it, as in vanilla. The global cap with two players is 70 × 578 / 289 = 140.

MSPT

tools/server_bench.py --servers horizon --bots 10,25 --behaviour walk --warmup 30 --seconds 120 --nice 19. The servers ran on the ThinkPad and the bots on the Mac mini (--bots-via 'ssh macmini'). The integration commit is this PR with #951 and #952, over main a9bcaa6e.

Server Bots MSPT avg P95 P99 CPU % RSS MB
main a9bcaa6e 10 14.41 25.73 35.27 48.7 756
integration 10 16.01 25.87 33.38 52.1 740
main a9bcaa6e 25 15.90 32.48 45.08 51.8 770
integration 25 22.41 46.58 56.68 65.6 779

Main spawned no mobs on a dedicated server. The extra time is the mobs that now spawn: up to the local cap of 70 monsters around each bot, plus creatures, and their AI. For scale, vanilla 26.3 measured 40 ms MSPT (P95 146) with 10 walking bots in #931's run.

Still tied to the local player

  • End-portal entry for LAN guests of a non-dedicated host. Dedicated servers already move every player through End portals (server_travel.rs). A LAN host keeps only its own level, so a guest can't change level without the host. That needs levels per dimension on LAN, which is out of scope here.

Coordination

This touches game/natural_spawning.rs, game/creatures.rs (the mob loop, one line), lightning.rs, wandering_trader.rs, mobs.rs (one MobWorld field) and mobs/ai.rs (check_despawn). It doesn't touch chunk encoding, tick_lan or the benchmarks.

🤖 Generated with Claude Code


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/horizon-rust/horizon/pull/950 **Author:** [@ludovic111](https://github.com/ludovic111) **Created:** 10/19/2026 **Status:** ✅ Merged **Merged:** 10/19/2026 **Merged by:** [@ludovic111](https://github.com/ludovic111) **Base:** `main` ← **Head:** `fix/natural-spawn-every-player` --- ### 📝 Commits (1) - [`08c77ad`](https://github.com/horizon-rust/horizon/commit/08c77ad78534e5079fed9ba152e1b2d27142b0bf) Spawn mobs around every player, and despawn from the nearest of them ### 📊 Changes **7 files changed** (+179 additions, -73 deletions) <details> <summary>View changed files</summary> 📝 `crates/horizon-client/examples/mob_ai_probe.rs` (+1 -0) 📝 `crates/horizon-client/src/game/creatures.rs` (+11 -21) 📝 `crates/horizon-client/src/game/lightning.rs` (+6 -2) 📝 `crates/horizon-client/src/game/natural_spawning.rs` (+136 -41) 📝 `crates/horizon-client/src/game/wandering_trader.rs` (+18 -7) 📝 `crates/horizon-client/src/mobs.rs` (+3 -0) 📝 `crates/horizon-client/src/mobs/ai.rs` (+4 -2) </details> ### 📄 Description > 🤖 Opened by Ludovic's AI agent (Claude), not by Ludovic himself. Follows #931 (#890). Natural spawning and the systems next to it no longer depend on a local player. ## The problem `spawn_natural` returned when there was no local player. A dedicated server or a host on `--server-thread` spawned no mobs naturally, and a LAN host spawned them only around itself. Inhabited time, thunder, the wandering trader and despawning also used only the local player, or every loaded chunk. ## What vanilla does `ServerChunkCache.tickChunks`: - `NaturalSpawner.createState(getNaturalSpawnChunkCount(), …, new LocalMobCapCalculator(chunkMap))`. The global caps scale with the size of the natural spawn chunk tracker: every chunk within 8 chunks (Chebyshev) of any player. Each mob also counts against the local cap of every non-spectator within 128 blocks of its chunk. - `ChunkMap.collectSpawningChunks`: the tracker's chunks that are ticking and within 128 blocks of a non-spectator. They are shuffled with the level random. Then `tickSpawningChunk` runs on each: inhabited time +1, `tickThunder` if the chunk is in entity-ticking range, then `spawnForChunk`. - `WanderingTraderSpawner.spawn` picks `ServerLevel.getRandomPlayer()`, one of the living players, drawn from the level random. - `Mob.checkDespawn` measures from `getNearestPlayer(this, -1)`: the nearest non-spectator, of any kind, alive or dead. ## What this changes - `Game::level_players` lists every player in the level: the local player, and the LAN guests or server players in it. - `spawning_chunks` is `collectSpawningChunks` over all of them, and also returns the tracker's size for the global caps. The `SpawnState` gets every player, so `spawn_selection`'s local caps (already per player, covered by the `natural_spawn` harness) see them all. The spawner's nearest-player, obstruction and "player near" checks (`NaturalLevel`, `SpawnLevel`, `SpawnerLevel`) read every player. - Inhabited time is counted in the same spawning chunks, in `spawn_natural`; `tick_inhabited` is gone. Thunder rolls only in spawning chunks that are in entity-ticking range, no longer in every loaded chunk. - The wandering trader ticks without a local player and picks a random living player, as vanilla does. - `MobWorld::nearest_player` is the despawn reference, set per mob from every player. Before, it was the player the mob was bound to, which could be a far survival guest while a creative player stood next to it. A lone spectator no longer makes far mobs despawn, as in vanilla. - The single-player candidate order is unchanged (the 17×17 square around the player), so a game with one player draws the same randoms as before. Vanilla's order is its hash map's, which isn't reproducible anyway. ## Proof | Check | Result | | --- | --- | | `cargo clippy --workspace --all-targets -D warnings` | clean | | `cargo test -p horizon-client` | all ok (lib 891 passed) | | `parity.py --changed origin/main` (before the rebase, `a1eadd24`) | 98 passed, 0 failed (natural_spawn, spawn_rules, spawner, finalize_spawn, mob_ai, dedicated_server, world_spawn among them) | | integration commit with #951 and #952 over `a9bcaa6e`: `parity.py --changed origin/main` | 301 passed, 0 failed (every harness) | | `lan_e2e --check mobs --check damage --check respawn --check growth --check time` | all PASS | | the same with `--server-thread` | all PASS | `dedicated_server` matches vanilla byte for byte. **Dedicated-server check.** This is an ad hoc script, not in the repo. It starts `horizon-server` on a flat world at midnight, with `pause-when-empty-seconds=-1`, and joins two idle `horizon-bots`, 4243 blocks apart. Every 15 s it counts the monsters within 128 blocks of each bot with `execute positioned … if entity @e[type=…,distance=..128]`. | Build | Monsters near each bot | Monsters in all | Creatures | | --- | --- | --- | --- | | main (ThinkPad) | 0, 0 | 0 | 0 | | this branch (Mac mini, 150 s) | 68 to 72 each | 139 to 144 | 22 | | integration (ThinkPad) | 70 to 72 each | 140 to 142 | 24 | The local cap is 70 monsters around each player, and a pack can overshoot it, as in vanilla. The global cap with two players is 70 × 578 / 289 = 140. ## MSPT `tools/server_bench.py --servers horizon --bots 10,25 --behaviour walk --warmup 30 --seconds 120 --nice 19`. The servers ran on the ThinkPad and the bots on the Mac mini (`--bots-via 'ssh macmini'`). The integration commit is this PR with #951 and #952, over main `a9bcaa6e`. | Server | Bots | MSPT avg | P95 | P99 | CPU % | RSS MB | | --- | --- | --- | --- | --- | --- | --- | | main `a9bcaa6e` | 10 | 14.41 | 25.73 | 35.27 | 48.7 | 756 | | integration | 10 | 16.01 | 25.87 | 33.38 | 52.1 | 740 | | main `a9bcaa6e` | 25 | 15.90 | 32.48 | 45.08 | 51.8 | 770 | | integration | 25 | 22.41 | 46.58 | 56.68 | 65.6 | 779 | Main spawned no mobs on a dedicated server. The extra time is the mobs that now spawn: up to the local cap of 70 monsters around each bot, plus creatures, and their AI. For scale, vanilla 26.3 measured 40 ms MSPT (P95 146) with 10 walking bots in #931's run. ## Still tied to the local player - End-portal entry for LAN guests of a non-dedicated host. Dedicated servers already move every player through End portals (`server_travel.rs`). A LAN host keeps only its own level, so a guest can't change level without the host. That needs levels per dimension on LAN, which is out of scope here. ## Coordination This touches `game/natural_spawning.rs`, `game/creatures.rs` (the mob loop, one line), `lightning.rs`, `wandering_trader.rs`, `mobs.rs` (one `MobWorld` field) and `mobs/ai.rs` (`check_despawn`). It doesn't touch chunk encoding, `tick_lan` or the benchmarks. 🤖 Generated with [Claude Code](https://claude.com/claude-code) --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-11-02 16:00:13 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
horizon-rust/horizon#2932
No description provided.