b99de58761
A world can now be opened to the local network from World Options in the pause menu (LAN on or off, port, command access, forcing the game mode, plus the default and personal game mode, cheats and difficulty), or with horizon-ctl lan open. The hosted world runs a server inside the game that speaks the vanilla 26.3 protocol: server list status, offline-mode login, the configuration phase with the synchronized registries and tags (sent as known-pack references to vanilla clients and in full to clients that know no packs), then the play state with chunks and light, time, weather, the player list, the host and other guests as visible players, mobs and dropped items, block changes, chat and keep-alives. Network I/O runs on its own threads and a world that is not open pays one check per tick. A vanilla 26.3 client and a second Horizon client were verified joining and seeing each other and the terrain. Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
138 lines
4.2 KiB
Rust
138 lines
4.2 KiB
Rust
//! Serve a small flat world on the LAN server without the game, for joining
|
|
//! with a real client while developing the protocol:
|
|
//! `cargo run --release --example lan_flat -- RESOURCES PORT`.
|
|
use horizon_client::{
|
|
assets::Assets,
|
|
blocks::BlockFacts,
|
|
chunk::{Chunk, MIN_Y},
|
|
lan::{
|
|
Lan,
|
|
chunks::ChunkEncoder,
|
|
data::SyncData,
|
|
hub::{HostEvent, HostState, Hub, HubConfig, TickCtx},
|
|
},
|
|
world::World,
|
|
};
|
|
use horizon_protocol::packets::play::Serverbound as S;
|
|
use std::{path::PathBuf, sync::Arc, time::Duration};
|
|
|
|
fn main() -> Result<(), Box<dyn std::error::Error>> {
|
|
let mut args = std::env::args().skip(1);
|
|
let root = PathBuf::from(args.next().ok_or("RESOURCES PORT")?);
|
|
let port: u16 = args.next().ok_or("port")?.parse()?;
|
|
let assets = Assets::load(&root)?;
|
|
let facts = BlockFacts::load(&root, assets.registry)?;
|
|
let state = |name: &str| {
|
|
assets
|
|
.registry
|
|
.default_state(name)
|
|
.map_or(0, |s| s.0 as u16)
|
|
};
|
|
let sync = Arc::new(SyncData::load(&root)?);
|
|
let biomes: Vec<String> = sync
|
|
.dynamic
|
|
.registries
|
|
.get("minecraft:worldgen/biome")
|
|
.map(|v| v.iter().map(|(n, _)| n.clone()).collect())
|
|
.unwrap_or_default();
|
|
let encoder = ChunkEncoder::new(
|
|
facts
|
|
.states
|
|
.iter()
|
|
.map(|s| (s.name.clone(), s.fluid_level > 0 || s.waterlogged)),
|
|
&sync.builtin,
|
|
&biomes,
|
|
&biomes,
|
|
);
|
|
let (stone, dirt, grass) = (
|
|
state("minecraft:stone"),
|
|
state("minecraft:dirt"),
|
|
state("minecraft:grass_block"),
|
|
);
|
|
let mut world = World::new(facts.air, 0);
|
|
for cz in -8..=8 {
|
|
for cx in -8..=8 {
|
|
let mut chunk = Chunk::empty(cx, cz);
|
|
for z in 0..16 {
|
|
for x in 0..16 {
|
|
for y in MIN_Y..60 {
|
|
let v = if y == 59 {
|
|
grass
|
|
} else if y > 55 {
|
|
dirt
|
|
} else {
|
|
stone
|
|
};
|
|
chunk.set(x, y, z, v);
|
|
}
|
|
}
|
|
}
|
|
chunk.lit = true;
|
|
world.chunks.insert((cx, cz), Arc::new(chunk));
|
|
}
|
|
}
|
|
let lan = Lan::start(sync.clone(), port, "Flat - Example".into(), 8)?;
|
|
println!("listening on {}", lan.port);
|
|
let mut hub = Hub::new(
|
|
lan,
|
|
sync,
|
|
encoder,
|
|
HubConfig {
|
|
default_game_mode: 1,
|
|
guest_commands: true,
|
|
force_game_mode: false,
|
|
hardcore: false,
|
|
view_distance: 8,
|
|
simulation_distance: 8,
|
|
dimension: "minecraft:overworld".into(),
|
|
seed: 0,
|
|
max_players: 8,
|
|
},
|
|
);
|
|
let mut next_entity = 100u32;
|
|
let host = HostState {
|
|
uuid: 42,
|
|
name: "Host".into(),
|
|
entity_id: 1,
|
|
position: [0.5, 60.0, 0.5],
|
|
yaw: 0.0,
|
|
pitch: 0.0,
|
|
head_yaw: 0.0,
|
|
on_ground: true,
|
|
game_mode: 1,
|
|
sneaking: false,
|
|
sprinting: false,
|
|
equipment: Default::default(),
|
|
health: 20.0,
|
|
};
|
|
loop {
|
|
world.game_time += 1;
|
|
world.day_time += 1;
|
|
let mut ctx = TickCtx {
|
|
world: &mut world,
|
|
host: &host,
|
|
entities: &[],
|
|
next_entity: &mut next_entity,
|
|
spawn: [8.5, 60.0, 8.5],
|
|
difficulty: 2,
|
|
difficulty_locked: false,
|
|
clock_rate: 1.0,
|
|
};
|
|
for event in hub.tick(&mut ctx) {
|
|
match event {
|
|
HostEvent::Joined(conn) => hub.place_new_player(conn, &mut ctx),
|
|
HostEvent::Packet(_, packet) => {
|
|
if std::env::var_os("HORIZON_NET_DEBUG").is_some()
|
|
&& !matches!(*packet, S::ClientTickEnd(_))
|
|
{
|
|
eprintln!("[lan] {packet:?}");
|
|
}
|
|
}
|
|
HostEvent::Chat(line) => println!("{line}"),
|
|
HostEvent::Left(_) => {}
|
|
}
|
|
}
|
|
std::thread::sleep(Duration::from_millis(50));
|
|
}
|
|
}
|