diff --git a/crates/horizon-client/src/game.rs b/crates/horizon-client/src/game.rs index 7f02bf53b..d120fb6a2 100644 --- a/crates/horizon-client/src/game.rs +++ b/crates/horizon-client/src/game.rs @@ -2,6 +2,7 @@ //! block interaction, inventory and HUD. mod advancements; mod advancements_screen; +mod agent; mod allays; mod armor_stands; mod backdrop; @@ -806,6 +807,8 @@ struct Game<'a> { view_frustum: Option, /// A scripted flight from the control socket (`fly`). flight: Option, + /// The running agent action from the control socket (game/agent.rs). + agent: agent::Agent, /// The overworld's last `sky_darken` and its inputs (time, rain, thunder). sky_darken_memo: std::cell::Cell>, persist: persist::EntityMemory, @@ -8655,6 +8658,7 @@ pub fn run() -> Result<()> { pending_screenshot: None, pending_bench: None, flight: None, + agent: agent::Agent::default(), view_frustum: None, pending_uploads: Vec::new(), frame_graph: Default::default(), diff --git a/crates/horizon-client/src/game/agent.rs b/crates/horizon-client/src/game/agent.rs new file mode 100644 index 000000000..e90b73ba8 --- /dev/null +++ b/crates/horizon-client/src/game/agent.rs @@ -0,0 +1,1725 @@ +//! Control-socket commands for agents and scripts that play the game: read +//! the world, the player, the inventory and the open screen as JSON, and act +//! like a player (walk, look, break, place, use, click buttons). Actions that +//! take time run as a task advanced every frame and reply when they finish or +//! time out, so `horizon-ctl break 10 64 3` returns what the block dropped. +use super::{Game, Screen}; +use horizon_client::{Result, items::ItemStack, widgets}; +use horizon_core::geometry::Vec3; +use horizon_world::registry::BlockStateId; +use serde_json::{Value, json}; +use std::{ + collections::{BTreeMap, HashMap, HashSet}, + sync::mpsc::Sender, + time::{Duration, Instant}, +}; +use winit::{ + application::ApplicationHandler, + dpi::PhysicalPosition, + event::{DeviceId, ElementState, MouseButton, WindowEvent}, + event_loop::ActiveEventLoop, + keyboard::KeyCode, +}; + +pub(super) const HELP: &[&str] = &[ + "state player, world, screen and crosshair target in one reply", + "looking-at the block or entity under the crosshair (distance, face)", + "entities [RADIUS] [TYPE] mobs, dropped items and orbs near the player, nearest first", + "block X Y Z the block state at a position", + "blocks X1 Y1 Z1 X2 Y2 Z2 the non-air blocks of a box (at most 32768 blocks)", + "find-block BLOCK [RADIUS] the nearest loaded blocks of a kind (default radius 32)", + "inventory hotbar, main inventory, armor, offhand and carried stack", + "screen the open screen or menu, its slots and its buttons", + "messages [N] recent chat and system messages, title and action bar", + "move DIR SECONDS [--sprint] [--sneak] [--jump] walk forward|back|left|right", + "jump jump once", + "goto X Y Z [--near R] [--sprint] [--direct] [--timeout S] walk to a position along a path", + "look-at X Y Z | look-at block X Y Z [FACE] | look-at entity ID", + "attack [ID] swing at the target (after looking at entity ID)", + "break X Y Z [--timeout S] mine a block until it breaks; reports drops and pickups", + "place X Y Z [--item ITEM] place the held (or given) item at a position", + "interact X Y Z [FACE] right-click a block (open a door, chest, crafting table...)", + "use-item [--hold S] right-click with the held item (eat, drink, throw, draw a bow)", + "select SLOT|ITEM select a hotbar slot (1-9) or the hotbar slot holding ITEM", + "drop [all] drop one of the held item (all: the stack)", + "open-inventory open the inventory screen", + "click-button LABEL click a button of the open screen or menu by its label", + "click-at X Y [right] click at GUI pixel X Y of the window", + "command TEXT run a /command", + "pause open the pause menu", + "wait TICKS wait for game ticks to pass", + "stop cancel the running action and release every key", +]; + +/// Keys the agent holds for a task (the game's movement reads these). +const MOVE_KEYS: [KeyCode; 7] = [ + KeyCode::KeyW, + KeyCode::KeyS, + KeyCode::KeyA, + KeyCode::KeyD, + KeyCode::Space, + KeyCode::ShiftLeft, + KeyCode::ControlLeft, +]; + +/// Words of a command line: positional values and `--flag [value]` options. +#[derive(Debug, Default, PartialEq)] +pub(super) struct Args<'a> { + pub pos: Vec<&'a str>, + pub flags: HashMap<&'a str, Option<&'a str>>, +} +/// Options that take a value (`--timeout 10` or `--timeout=10`). +const VALUE_FLAGS: &[&str] = &["timeout", "near", "item", "radius", "hold", "face"]; + +pub(super) fn parse_args<'a>(words: &[&'a str]) -> Args<'a> { + let mut args = Args::default(); + let mut i = 0; + while i < words.len() { + let w = words[i]; + if let Some(flag) = w.strip_prefix("--") { + if let Some((k, v)) = flag.split_once('=') { + args.flags.insert(k, Some(v)); + } else if VALUE_FLAGS.contains(&flag) && i + 1 < words.len() { + args.flags.insert(flag, Some(words[i + 1])); + i += 1; + } else { + args.flags.insert(flag, None); + } + } else { + args.pos.push(w); + } + i += 1; + } + args +} +impl Args<'_> { + fn has(&self, flag: &str) -> bool { + self.flags.contains_key(flag) + } + fn num(&self, flag: &str, default: f64) -> Result { + match self.flags.get(flag) { + Some(Some(v)) => Ok(v.parse()?), + Some(None) => Err(format!("--{flag} needs a value").into()), + None => Ok(default), + } + } + fn get(&self, i: usize, what: &str) -> Result<&str> { + self.pos + .get(i) + .copied() + .ok_or_else(|| format!("missing {what}").into()) + } + fn f64(&self, i: usize, what: &str) -> Result { + let v = self.get(i, what)?; + v.parse() + .map_err(|_| format!("{what} must be a number, not {v}").into()) + } + fn i32(&self, i: usize, what: &str) -> Result { + let v = self.get(i, what)?; + v.parse() + .map_err(|_| format!("{what} must be an integer, not {v}").into()) + } + fn block_pos(&self, from: usize) -> Result<[i32; 3]> { + Ok([ + self.i32(from, "X")?, + self.i32(from + 1, "Y")?, + self.i32(from + 2, "Z")?, + ]) + } +} + +fn namespaced(name: &str) -> String { + if name.contains(':') { + name.to_owned() + } else { + format!("minecraft:{name}") + } +} + +const FACES: [(&str, [i32; 3]); 6] = [ + ("down", [0, -1, 0]), + ("up", [0, 1, 0]), + ("north", [0, 0, -1]), + ("south", [0, 0, 1]), + ("west", [-1, 0, 0]), + ("east", [1, 0, 0]), +]; +fn face_name(face: [i32; 3]) -> &'static str { + FACES + .iter() + .find(|(_, d)| *d == face) + .map_or("?", |(n, _)| n) +} +fn face_dir(name: &str) -> Result<[i32; 3]> { + FACES + .iter() + .find(|(n, _)| *n == name) + .map(|(_, d)| *d) + .ok_or_else(|| format!("unknown face {name} (down up north south west east)").into()) +} +fn add(a: [i32; 3], b: [i32; 3]) -> [i32; 3] { + [a[0] + b[0], a[1] + b[1], a[2] + b[2]] +} +fn dist(a: [f64; 3], b: [f64; 3]) -> f64 { + ((a[0] - b[0]).powi(2) + (a[1] - b[1]).powi(2) + (a[2] - b[2]).powi(2)).sqrt() +} +/// Minecraft's yaw and pitch facing from `eye` towards `at` (yaw 0 faces +Z, +/// 90 faces -X; positive pitch looks down). +pub(super) fn yaw_pitch(eye: [f64; 3], at: [f64; 3]) -> (f32, f32) { + let d = [at[0] - eye[0], at[1] - eye[1], at[2] - eye[2]]; + let horizontal = (d[0] * d[0] + d[2] * d[2]).sqrt(); + ( + (-d[0]).atan2(d[2]).to_degrees() as f32, + (-d[1]).atan2(horizontal).to_degrees() as f32, + ) +} + +fn counts(items: impl Iterator) -> BTreeMap { + let mut map = BTreeMap::new(); + for s in items { + *map.entry(s.item.to_string()).or_insert(0) += i64::from(s.count); + } + map +} +/// Item counts that changed between two inventories (positive: gained). +fn delta(before: &BTreeMap, after: &BTreeMap) -> BTreeMap { + let mut out = BTreeMap::new(); + for key in before.keys().chain(after.keys()) { + let d = after.get(key).copied().unwrap_or(0) - before.get(key).copied().unwrap_or(0); + if d != 0 { + out.insert(key.clone(), d); + } + } + out +} + +/// A feet position the A* path walks through. +type Node = [i32; 3]; + +enum TaskKind { + /// Hold keys until a deadline. + Move { + keys: Vec, + until: Instant, + from: Vec3, + }, + Wait { + until_tick: u64, + }, + Jump { + release_tick: u64, + from_y: f64, + }, + Goto(Box), + Break(Box), + /// A right click on a block or a placement: report after a few ticks. + Click { + pos: Option<[i32; 3]>, + before: Option, + inventory: BTreeMap, + report_tick: u64, + release: Option, + place: bool, + }, + Attack { + id: Option, + health: Option, + report_tick: u64, + }, +} +struct Goto { + target: [f64; 3], + near: f64, + sprint: bool, + path: Vec, + next: usize, + /// Tick of the last waypoint reached, and how many replans so far. + progress_tick: u64, + replans: u32, + jump_until: u64, + warning: Option, +} +struct Break { + pos: [i32; 3], + before: BlockStateId, + items_before: HashSet, + inventory: BTreeMap, + started_tick: u64, + /// Set once the block changed: collect drops until this tick. + collect_until: Option, + broke_tick: u64, + drops: BTreeMap, +} +struct Task { + kind: TaskKind, + reply: Sender, + deadline: Instant, + name: &'static str, +} + +#[derive(Default)] +pub(super) struct Agent { + task: Option, +} + +pub(super) enum Handled { + Reply(Value), + /// The reply comes later, from the task. + Deferred, + /// Not an agent command. + No, +} + +impl Game<'_> { + fn state_string(&self, state: BlockStateId) -> String { + let Some(s) = self.res.assets.registry.state(state) else { + return "?".into(); + }; + if s.properties.is_empty() { + return s.name.clone(); + } + let props: Vec = s + .properties + .iter() + .map(|(k, v)| format!("{k}={v}")) + .collect(); + format!("{}[{}]", s.name, props.join(",")) + } + fn block_json(&self, pos: [i32; 3]) -> Value { + let state = self.world.block(pos[0], pos[1], pos[2]); + json!({ + "pos": pos, + "block": &*self.res.facts.get(state).name, + "state": self.state_string(state), + "loaded": self.world.chunks.contains_key(&(pos[0] >> 4, pos[2] >> 4)), + }) + } + fn stack_value(&self, s: &ItemStack) -> Value { + let mut v = json!({ "item": &*s.item, "count": s.count }); + if s.damage > 0 { + v["damage"] = json!(s.damage); + } + if let Some(c) = horizon_client::item_data::encode_components(s) { + v["components"] = json!(horizon_client::snbt::to_snbt(&c)); + } + v + } + fn slot_value(&self, slot: usize, s: &Option) -> Value { + match s { + Some(s) => { + let mut v = self.stack_value(s); + v["slot"] = json!(slot); + v + } + None => json!({ "slot": slot, "item": null }), + } + } + fn need_world(&self) -> Result<()> { + if self.title.is_some() { + return Err("no world open (title screen); use: open WORLD [SEED] [creative]".into()); + } + Ok(()) + } + fn playing(&self) -> Result<()> { + if self.title.is_some() { + return Err("no world open (title screen); use: open WORLD [SEED] [creative]".into()); + } + if self.player.is_none() { + return Err("the player has not spawned yet; try wait-ready".into()); + } + if self.screen != Screen::Playing || self.book.is_some() || self.sign_edit.is_some() { + return Err(format!( + "the {} screen is open; close it first (close, or click-button)", + self.screen_label() + ) + .into()); + } + Ok(()) + } + fn screen_label(&self) -> &'static str { + if self.title.is_some() { + return "title"; + } + if self.book.is_some() { + return "book"; + } + if self.sign_edit.is_some() { + return "sign"; + } + match self.screen { + Screen::Playing => "playing", + Screen::Paused => "paused", + Screen::Options => "options", + Screen::Inventory if self.station.is_some() => "station", + Screen::Inventory if self.container.is_some() => "container", + Screen::Inventory => "inventory", + Screen::Creative => "creative", + Screen::Dead => "dead", + Screen::Chat => "chat", + Screen::Stats => "stats", + Screen::Advancements => "advancements", + Screen::WorldOptions => "world-options", + } + } + fn inventory_counts(&self) -> BTreeMap { + counts( + self.inventory + .iter() + .chain(self.armor.iter()) + .chain(std::iter::once(&self.offhand)) + .chain(std::iter::once(&self.carried)) + .flatten() + .cloned(), + ) + } + + /// The block or entity under the crosshair. + fn looking_at(&self) -> Value { + let Some(eye) = self.eye_position() else { + return Value::Null; + }; + let block = self.pick().map(|hit| { + let mut v = self.block_json(hit.pos); + v["face"] = json!(face_name(hit.face)); + v["point"] = json!(hit.point); + v["distance"] = json!(dist(eye, hit.point)); + v + }); + let entity = self.mob_hit().map(|(i, d)| { + let m = &self.mobs[i]; + json!({ "id": m.id, "type": m.kind.id, "health": m.health, "distance": d, + "pos": [m.position.x, m.position.y, m.position.z] }) + }); + let kind = match (&block, &entity) { + (Some(b), Some(e)) if e["distance"].as_f64() <= b["distance"].as_f64() => "entity", + (_, Some(_)) if block.is_none() => "entity", + (Some(_), _) => "block", + _ => "none", + }; + json!({ "kind": kind, "block": block, "entity": entity }) + } + + fn state_value(&self) -> Value { + let player = self.player.as_ref().map(|p| { + json!({ + "position": [p.position.x, p.position.y, p.position.z], + "block_pos": [p.position.x.floor() as i32, p.position.y.floor() as i32, p.position.z.floor() as i32], + "eye": self.eye_position(), + "yaw": self.yaw, + "pitch": self.pitch, + "velocity": [p.velocity.x, p.velocity.y, p.velocity.z], + "on_ground": p.ground.on_ground, + "flying": p.flying, + "sneaking": self.keys.contains(&KeyCode::ShiftLeft), + "sprinting": self.keys.contains(&KeyCode::ControlLeft), + "health": p.health.health(), + "food": p.health.food.food, + "saturation": p.health.food.saturation, + "xp": { "level": self.experience.level, "progress": self.experience.progress, "total": self.experience.total }, + "effects": self.potions.effects.list.iter().map(|e| json!({ "id": e.id, "duration_ticks": e.duration, "amplifier": e.amplifier })).collect::>(), + }) + }); + json!({ + "screen": self.screen_label(), + "menu": self.title.as_ref().map(|_| self.title_value()), + "world": (self.title.is_none()).then(|| 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) }, + "weather": { "raining": self.world.weather.raining, "thundering": self.world.weather.thundering }, + "paused": self.pause, + "player": player, + "selected": self.selected + 1, + "held": self.held().map(|s| self.stack_value(s)), + "looking_at": self.looking_at(), + "busy": self.agent.task.as_ref().map(|t| t.name), + "ticks": self.ticks, + }) + } + + fn title_value(&self) -> Value { + use horizon_client::title::Mode; + let Some(title) = &self.title else { + return Value::Null; + }; + let worlds = || { + title + .recent_worlds() + .iter() + .map(|w| json!({ "folder": w.folder, "name": w.name, "creative": w.creative })) + .collect::>() + }; + match &title.mode { + Mode::Main => json!({ "mode": "main", "worlds": worlds() }), + Mode::Select { + worlds: list, + selected, + } => json!({ + "mode": "select-world", + "worlds": list.iter().map(|w| json!({ "folder": w.folder, "name": w.name, "creative": w.creative })).collect::>(), + "selected": selected, + }), + Mode::Create { + name, + seed, + creative, + focus, + } => json!({ + "mode": "create-world", + "fields": { "name": name, "seed": seed }, + "focus": match focus { 0 => "name", 1 => "seed", _ => "none" }, + "creative": creative, + }), + Mode::Multiplayer(_) => json!({ "mode": "multiplayer" }), + } + } + + fn entities_value(&self, radius: f64, kind: Option<&str>) -> Value { + let Some(p) = &self.player else { + return json!({ "entities": [] }); + }; + let me = [p.position.x, p.position.y, p.position.z]; + let want = kind.map(namespaced); + let mut list: Vec<(f64, Value)> = Vec::new(); + let mut push = |pos: [f64; 3], ty: &str, v: Value| { + let d = dist(me, pos); + if d <= radius && want.as_deref().is_none_or(|w| w == ty) { + let mut v = v; + v["type"] = json!(ty); + v["pos"] = json!(pos); + v["distance"] = json!(d); + list.push((d, v)); + } + }; + for m in self.mobs.iter().filter(|m| m.alive()) { + push( + [m.position.x, m.position.y, m.position.z], + m.kind.id.as_ref(), + json!({ "id": m.id, "kind": "mob", "health": m.health, "baby": m.is_baby() }), + ); + } + for item in &self.dropped { + push( + [item.position.x, item.position.y, item.position.z], + "minecraft:item", + json!({ "id": item.id, "kind": "item", "item": self.stack_value(&item.stack) }), + ); + } + for orb in &self.orbs { + push( + [orb.position.x, orb.position.y, orb.position.z], + "minecraft:experience_orb", + json!({ "kind": "orb", "value": orb.value }), + ); + } + list.sort_by(|a, b| a.0.total_cmp(&b.0)); + json!({ "entities": list.into_iter().map(|(_, v)| v).collect::>() }) + } + + fn inventory_value(&self) -> Value { + let armor = ["head", "chest", "legs", "feet"]; + json!({ + "selected": self.selected + 1, + "held": self.held().map(|s| self.stack_value(s)), + "hotbar": (0..9).map(|i| self.slot_value(i, &self.inventory[i])).collect::>(), + "main": (9..36).map(|i| self.slot_value(i, &self.inventory[i])).collect::>(), + "armor": armor.iter().enumerate().map(|(i, name)| { + let mut v = self.slot_value(500 + i, &self.armor[i]); + v["part"] = json!(name); + v + }).collect::>(), + "offhand": self.slot_value(504, &self.offhand), + "carried": self.carried.as_ref().map(|s| self.stack_value(s)), + "counts": self.inventory_counts(), + }) + } + + fn screen_value(&mut self) -> Value { + let buttons: Vec = widgets::last() + .iter() + .map(|w| json!({ "label": w.label, "enabled": w.enabled, "x": w.x, "y": w.y, "w": w.w, "h": w.h })) + .collect(); + let mut v = json!({ + "screen": self.screen_label(), + "buttons": buttons, + }); + if self.title.is_some() { + v["menu"] = self.title_value(); + } + if self.screen == Screen::Inventory && self.title.is_none() { + let mut slots = Vec::new(); + for i in self.slots() { + let stack = if i == 200 { + self.craft_result() + } else { + self.slot_mut(i).clone() + }; + slots.push(self.slot_value(i, &stack)); + } + v["slots"] = json!(slots); + v["carried"] = json!(self.carried.as_ref().map(|s| self.stack_value(s))); + v["craft_size"] = json!(self.craft_size); + v["station"] = json!(self.station.as_ref().map(|s| format!("{:?}", s.kind))); + v["container"] = json!(self.container); + v["slot_ids"] = json!( + "0-8 hotbar, 9-35 inventory, 100+ crafting grid (row-major), 200 crafting result, 300+ container, 500-503 armor, 504 offhand, 600+ station" + ); + } + v + } + + fn messages_value(&self, n: usize) -> Value { + let titles = &self.titles; + json!({ + "chat": self.ui.chat.recent_lines(n), + "order": "newest first", + "title": (titles.time > 0).then(|| titles.title.as_ref().map(|t| t.plain())).flatten(), + "subtitle": (titles.time > 0).then(|| titles.subtitle.as_ref().map(|t| t.plain())).flatten(), + "actionbar": (titles.overlay_time > 0).then(|| titles.overlay.as_ref().map(|t| t.plain())).flatten(), + }) + } + + /// Whether a block stops movement, for the path finder. + fn blocks_motion(&self, pos: [i32; 3]) -> bool { + let f = self.res.facts.get(self.world.block(pos[0], pos[1], pos[2])); + f.full_collision || f.legacy_solid + } + fn walkable(&self, n: Node) -> bool { + let below = self.world.block(n[0], n[1] - 1, n[2]); + let lava = |p: [i32; 3]| { + &*self.res.facts.get(self.world.block(p[0], p[1], p[2])).name == "minecraft:lava" + }; + self.world.chunks.contains_key(&(n[0] >> 4, n[2] >> 4)) + && (self.blocks_motion([n[0], n[1] - 1, n[2]]) + || self.res.facts.get(below).fluid != horizon_client::blocks::Fluid::None) + && !self.blocks_motion(n) + && !self.blocks_motion([n[0], n[1] + 1, n[2]]) + && !lava(n) + && !lava([n[0], n[1] - 1, n[2]]) + } + /// A* over standable feet positions: steps up one block, drops up to three. + fn find_path(&self, from: Node, goal: [f64; 3], near: f64) -> Option> { + use std::{cmp::Reverse, collections::BinaryHeap}; + let reached = |n: Node| { + let c = [ + f64::from(n[0]) + 0.5, + f64::from(n[1]), + f64::from(n[2]) + 0.5, + ]; + ((c[0] - goal[0]).powi(2) + (c[2] - goal[2]).powi(2)).sqrt() <= near.max(0.5) + && (c[1] - goal[1]).abs() <= near.max(1.0) + 0.5 + }; + let h = |n: Node| { + ((f64::from(n[0]) + 0.5 - goal[0]).abs() + + (f64::from(n[1]) - goal[1]).abs() + + (f64::from(n[2]) + 0.5 - goal[2]).abs()) + * 10.0 + }; + let mut open = BinaryHeap::new(); + let mut cost: HashMap = HashMap::new(); + let mut came: HashMap = HashMap::new(); + cost.insert(from, 0); + open.push(Reverse((h(from) as u32, from))); + let mut expanded = 0; + while let Some(Reverse((_, n))) = open.pop() { + if reached(n) { + let mut path = vec![n]; + let mut at = n; + while let Some(&p) = came.get(&at) { + path.push(p); + at = p; + } + path.reverse(); + return Some(path); + } + expanded += 1; + if expanded > 30_000 { + return None; + } + let base = cost[&n]; + for d in [[1, 0], [-1, 0], [0, 1], [0, -1]] { + for dy in [0, 1, -1, -2, -3] { + let m = [n[0] + d[0], n[1] + dy, n[2] + d[1]]; + // A step up needs head room above the start; a drop needs + // the column in front clear down to the landing. + let clear = match dy { + 1 => !self.blocks_motion([n[0], n[1] + 2, n[2]]), + d if d < 0 => { + (m[1]..=n[1] + 1).all(|y| !self.blocks_motion([m[0], y, m[2]])) + } + _ => true, + }; + if !clear || !self.walkable(m) { + continue; + } + let water = self.res.facts.get(self.world.block(m[0], m[1], m[2])).fluid + != horizon_client::blocks::Fluid::None; + let step = 10 + if dy != 0 { 5 } else { 0 } + if water { 30 } else { 0 }; + let c = base + step; + if cost.get(&m).is_none_or(|&old| c < old) { + cost.insert(m, c); + came.insert(m, n); + open.push(Reverse((c + h(m) as u32, m))); + } + break; + } + } + } + None + } + + /// Point the view at `at` from the eye. + fn face_point(&mut self, at: [f64; 3]) -> Result<()> { + let eye = self.eye_position().ok_or("no player")?; + let (yaw, pitch) = yaw_pitch(eye, at); + self.yaw = yaw; + self.pitch = pitch.clamp(-90.0, 90.0); + Ok(()) + } + /// Aim at a face of `pos` the crosshair can reach; the face hit. + fn aim_block(&mut self, pos: [i32; 3], face: Option<[i32; 3]>) -> Result<[i32; 3]> { + let eye = self.eye_position().ok_or("no player")?; + let centre = pos.map(|v| f64::from(v) + 0.5); + let mut faces: Vec<[i32; 3]> = match face { + Some(f) => vec![f], + None => FACES.iter().map(|(_, d)| *d).collect(), + }; + // Faces turned towards the eye first, nearest first. + faces.sort_by(|a, b| { + let point = |f: &[i32; 3]| [0, 1, 2].map(|i| centre[i] + f64::from(f[i]) * 0.45); + dist(eye, point(a)).total_cmp(&dist(eye, point(b))) + }); + for f in faces { + for inset in [0.45, 0.3, 0.0] { + let point = [0, 1, 2].map(|i| centre[i] + f64::from(f[i]) * inset); + let (yaw, pitch) = yaw_pitch(eye, point); + let dir = super::view_vector(yaw, pitch); + if let Some(hit) = self.clip_ray(eye, dir, self.reach()) + && hit.pos == pos + && (face.is_none() || hit.face == f) + { + self.yaw = yaw; + self.pitch = pitch; + return Ok(hit.face); + } + } + } + let d = dist(eye, centre); + if d > self.reach() + 0.9 { + return Err(format!( + "block {pos:?} is {d:.1} blocks away, out of reach ({:.1}); goto it first", + self.reach() + ) + .into()); + } + Err(format!("no face of block {pos:?} is visible from here").into()) + } + + /// A left or right click, as the mouse would. + fn agent_mouse(&mut self, button: MouseButton, pressed: bool) -> Result<()> { + self.play_mouse(button, pressed) + } + fn release_agent_keys(&mut self) { + for k in MOVE_KEYS { + self.keys.remove(&k); + } + if self.left { + let _ = self.agent_mouse(MouseButton::Left, false); + } + if self.right { + let _ = self.agent_mouse(MouseButton::Right, false); + } + } + + /// Synthesize the pointer moving to GUI pixel (x, y) and a click there. + fn synth_click( + &mut self, + event_loop: &ActiveEventLoop, + x: f32, + y: f32, + button: MouseButton, + ) -> Result<()> { + let id = self.window.as_ref().ok_or("no window yet")?.id(); + let scale = self + .renderer + .as_ref() + .map(|r| horizon_client::hud::HudBatch::new([r.config.width, r.config.height]).scale) + .ok_or("no renderer yet")?; + let device_id = DeviceId::dummy(); + let position = PhysicalPosition::new(f64::from(x * scale), f64::from(y * scale)); + self.window_event( + event_loop, + id, + WindowEvent::CursorMoved { + device_id, + position, + }, + ); + for state in [ElementState::Pressed, ElementState::Released] { + self.window_event( + event_loop, + id, + WindowEvent::MouseInput { + device_id, + state, + button, + }, + ); + } + Ok(()) + } + + fn start_task( + &mut self, + name: &'static str, + kind: TaskKind, + reply: &Sender, + timeout: f64, + ) -> Result { + self.agent.task = Some(Task { + kind, + reply: reply.clone(), + deadline: Instant::now() + Duration::from_secs_f64(timeout.clamp(0.05, 3600.0)), + name, + }); + Ok(Handled::Deferred) + } + + /// Agent commands; `Handled::No` when `words[0]` is not one of them. + pub(super) fn agent_command( + &mut self, + words: &[&str], + line: &str, + reply: &Sender, + event_loop: &ActiveEventLoop, + ) -> Result { + let name = words[0]; + let a = parse_args(&words[1..]); + let ok = |v: Value| Ok(Handled::Reply(v)); + let blocking = matches!( + name, + "move" + | "jump" + | "goto" + | "break" + | "place" + | "interact" + | "use-item" + | "attack" + | "wait" + ); + if blocking && let Some(task) = &self.agent.task { + return Err(format!("busy with {}; wait for it or send stop", task.name).into()); + } + match name { + "state" => ok(self.state_value()), + "looking-at" | "target" => { + self.need_world()?; + ok(self.looking_at()) + } + "entities" => { + self.need_world()?; + let radius = a.pos.first().map_or(Ok(32.0), |r| r.parse::())?; + ok(self.entities_value(radius, a.pos.get(1).copied())) + } + "block" => { + self.need_world()?; + ok(self.block_json(a.block_pos(0)?)) + } + "blocks" => { + self.need_world()?; + let (p, q) = (a.block_pos(0)?, a.block_pos(3)?); + let lo = [0, 1, 2].map(|i| p[i].min(q[i])); + let hi = [0, 1, 2].map(|i| p[i].max(q[i])); + let volume: i64 = (0..3).map(|i| i64::from(hi[i] - lo[i] + 1)).product(); + if volume > 32768 { + return Err(format!("{volume} blocks; at most 32768 per request").into()); + } + let mut list = Vec::new(); + let mut census: BTreeMap = BTreeMap::new(); + for y in lo[1]..=hi[1] { + for z in lo[2]..=hi[2] { + for x in lo[0]..=hi[0] { + let state = self.world.block(x, y, z); + if self.res.facts.is_air(state) { + continue; + } + let name = self.res.facts.get(state).name.to_string(); + *census.entry(name).or_insert(0) += 1; + list.push(json!([x, y, z, self.state_string(state)])); + } + } + } + ok(json!({ "from": lo, "to": hi, "counts": census, "blocks": list })) + } + "find-block" => { + self.need_world()?; + let want = namespaced(a.get(0, "BLOCK")?); + let radius = a + .pos + .get(1) + .map_or(Ok(32), |r| r.parse::())? + .clamp(1, 64); + let eye = self.eye_position().ok_or("no player")?; + let c = eye.map(|v| v.floor() as i32); + let mut found = Vec::new(); + for y in (c[1] - radius).max(-64)..=(c[1] + radius).min(319) { + for z in c[2] - radius..=c[2] + radius { + for x in c[0] - radius..=c[0] + radius { + let state = self.world.block(x, y, z); + if *self.res.facts.get(state).name == *want { + let d = dist(eye, [x, y, z].map(|v| f64::from(v) + 0.5)); + found.push((d, [x, y, z])); + } + } + } + } + found.sort_by(|a, b| a.0.total_cmp(&b.0)); + let total = found.len(); + ok(json!({ + "block": want, + "count": total, + "nearest": found.iter().take(16).map(|(d, p)| json!({ "pos": p, "distance": d })).collect::>(), + })) + } + "inventory" => { + self.need_world()?; + ok(self.inventory_value()) + } + "screen" => ok(self.screen_value()), + "messages" => { + let n = a.pos.first().map_or(Ok(20), |n| n.parse::())?; + ok(self.messages_value(n)) + } + "move" => { + self.playing()?; + let key = match a.get(0, "DIR")? { + "forward" | "w" => KeyCode::KeyW, + "back" | "backward" | "s" => KeyCode::KeyS, + "left" | "a" => KeyCode::KeyA, + "right" | "d" => KeyCode::KeyD, + other => { + return Err( + format!("DIR is forward, back, left or right, not {other}").into() + ); + } + }; + let seconds = a.f64(1, "SECONDS")?.clamp(0.0, 600.0); + let mut keys = vec![key]; + if a.has("sprint") { + keys.push(KeyCode::ControlLeft); + } + if a.has("sneak") { + keys.push(KeyCode::ShiftLeft); + } + if a.has("jump") { + keys.push(KeyCode::Space); + } + for &k in &keys { + self.keys.insert(k); + } + let from = self.player.as_ref().map_or(Vec3::ZERO, |p| p.position); + let until = Instant::now() + Duration::from_secs_f64(seconds); + self.start_task( + "move", + TaskKind::Move { keys, until, from }, + reply, + seconds + 5.0, + ) + } + "jump" => { + self.playing()?; + self.keys.insert(KeyCode::Space); + let from_y = self.player.as_ref().map_or(0.0, |p| p.position.y); + let release_tick = self.ticks + 3; + self.start_task( + "jump", + TaskKind::Jump { + release_tick, + from_y, + }, + reply, + 5.0, + ) + } + "goto" => { + self.playing()?; + let target = [a.f64(0, "X")?, a.f64(1, "Y")?, a.f64(2, "Z")?]; + let near = a.num("near", 0.6)?.max(0.3); + let timeout = a.num("timeout", 60.0)?; + let p = self.player.as_ref().ok_or("no player")?.position; + let from = [p.x.floor() as i32, p.y.round() as i32, p.z.floor() as i32]; + let (path, warning) = match self.find_path(from, target, near) { + Some(path) => (path, None), + None if a.has("direct") => ( + Vec::new(), + Some("no path found through loaded terrain; walking straight".to_owned()), + ), + None => { + return Err(format!( + "no walking path to {target:?} through loaded terrain (climbs of more than one block, drops of more than three, lava and unloaded chunks are avoided); pick a nearer or lower spot, raise --near, or use --direct to walk straight at it" + ) + .into()); + } + }; + let goto = Goto { + target, + near, + sprint: a.has("sprint"), + path, + next: 1, + progress_tick: self.ticks, + replans: 0, + jump_until: 0, + warning, + }; + self.start_task("goto", TaskKind::Goto(Box::new(goto)), reply, timeout) + } + "look-at" => { + self.need_world()?; + match a.pos.first().copied() { + Some("block") => { + let pos = a.block_pos(1)?; + let face = a.pos.get(4).map(|f| face_dir(f)).transpose()?; + let hit = self.aim_block(pos, face)?; + ok( + json!({ "ok": true, "yaw": self.yaw, "pitch": self.pitch, "face": face_name(hit) }), + ) + } + Some("entity") => { + let id: u32 = a.get(1, "ID")?.parse()?; + let m = self + .mobs + .iter() + .find(|m| m.id == id) + .ok_or_else(|| format!("no entity {id}"))?; + let c = m.center(); + self.face_point(c)?; + ok( + json!({ "ok": true, "yaw": self.yaw, "pitch": self.pitch, "target": self.looking_at() }), + ) + } + _ => { + let at = [a.f64(0, "X")?, a.f64(1, "Y")?, a.f64(2, "Z")?]; + self.face_point(at)?; + ok(json!({ "ok": true, "yaw": self.yaw, "pitch": self.pitch })) + } + } + } + "attack" => { + self.playing()?; + let id = a.pos.first().map(|i| i.parse::()).transpose()?; + if let Some(id) = id { + let m = self + .mobs + .iter() + .find(|m| m.id == id) + .ok_or_else(|| format!("no entity {id}"))?; + let c = m.center(); + self.face_point(c)?; + } + let target = self + .mob_hit() + .map(|(i, _)| (self.mobs[i].id, self.mobs[i].health)); + if id.is_some() && target.map(|t| t.0) != id { + return Err("that entity is out of reach or hidden; get closer".into()); + } + self.agent_mouse(MouseButton::Left, true)?; + self.agent_mouse(MouseButton::Left, false)?; + let report_tick = self.ticks + 4; + self.start_task( + "attack", + TaskKind::Attack { + id: target.map(|t| t.0), + health: target.map(|t| t.1), + report_tick, + }, + reply, + 5.0, + ) + } + "break" => { + self.playing()?; + let pos = a.block_pos(0)?; + let before = self.world.block(pos[0], pos[1], pos[2]); + if self.res.facts.is_air(before) { + return Err(format!("there is no block at {pos:?}").into()); + } + self.aim_block(pos, None)?; + let timeout = a.num("timeout", 30.0)?; + let task = Break { + pos, + before, + items_before: self.dropped.iter().map(|i| i.id).collect(), + inventory: self.inventory_counts(), + started_tick: self.ticks, + collect_until: None, + broke_tick: 0, + drops: BTreeMap::new(), + }; + self.agent_mouse(MouseButton::Left, true)?; + self.start_task("break", TaskKind::Break(Box::new(task)), reply, timeout) + } + "place" => { + self.playing()?; + let pos = a.block_pos(0)?; + if let Some(Some(item)) = a.flags.get("item") { + self.select_item(&namespaced(item))?; + } + let held = self + .held() + .ok_or("nothing in hand to place; select an item")?; + let held = held.item.clone(); + let before = self.world.block(pos[0], pos[1], pos[2]); + if !self.res.facts.get(before).replaceable && !self.res.facts.is_air(before) { + return Err(format!( + "{pos:?} holds {}; break it first", + self.res.facts.get(before).name + ) + .into()); + } + // A replaceable block there (grass, snow) is clicked itself, as + // a player would; otherwise the face of a neighbour touching it. + let mut aimed = !self.res.facts.is_air(before) + && !self.res.facts.get(before).outline.is_empty() + && self.aim_block(pos, None).is_ok(); + for (_, d) in FACES.iter().filter(|_| !aimed) { + let d = *d; + let support = add(pos, d); + let state = self.world.block(support[0], support[1], support[2]); + let f = self.res.facts.get(state); + if self.res.facts.is_air(state) + || f.replaceable + || f.fluid != horizon_client::blocks::Fluid::None && !f.waterlogged + { + continue; + } + let face = d.map(|v| -v); + if self.aim_block(support, Some(face)).is_ok() { + aimed = true; + break; + } + } + if !aimed { + return Err(format!( + "no visible face to place {held} against at {pos:?}; it needs a solid neighbour in reach" + ) + .into()); + } + let inventory = self.inventory_counts(); + self.agent_mouse(MouseButton::Right, true)?; + self.agent_mouse(MouseButton::Right, false)?; + let report_tick = self.ticks + 2; + self.start_task( + "place", + TaskKind::Click { + pos: Some(pos), + before: Some(before), + inventory, + report_tick, + release: None, + place: true, + }, + reply, + 5.0, + ) + } + "interact" | "use-item" => { + self.playing()?; + let hold = a.num("hold", 0.0)?; + let pos = if name == "interact" { + let pos = a.block_pos(0)?; + let face = a.pos.get(3).map(|f| face_dir(f)).transpose()?; + self.aim_block(pos, face)?; + Some(pos) + } else { + None + }; + let before = pos.map(|p| self.world.block(p[0], p[1], p[2])); + let inventory = self.inventory_counts(); + self.agent_mouse(MouseButton::Right, true)?; + let release = if hold > 0.0 { + Some(Instant::now() + Duration::from_secs_f64(hold)) + } else { + self.agent_mouse(MouseButton::Right, false)?; + None + }; + let report_tick = self.ticks + 2; + self.start_task( + if name == "interact" { + "interact" + } else { + "use-item" + }, + TaskKind::Click { + pos, + before, + inventory, + report_tick, + release, + place: false, + }, + reply, + hold + 5.0, + ) + } + "select" => { + self.need_world()?; + let what = a.get(0, "SLOT or ITEM")?; + match what.parse::() { + Ok(n) if (1..=9).contains(&n) => self.selected = n - 1, + Ok(_) => return Err("hotbar slots are 1..9".into()), + Err(_) => self.select_item(&namespaced(what))?, + } + ok( + json!({ "ok": true, "selected": self.selected + 1, "held": self.held().map(|s| self.stack_value(s)) }), + ) + } + "drop" => { + self.playing()?; + let all = a.pos.first() == Some(&"all"); + let before = self.held().map(|s| self.stack_value(s)); + if all { + self.keys.insert(KeyCode::ControlLeft); + } + let result = self.key_pressed(KeyCode::KeyQ); + if all { + self.keys.remove(&KeyCode::ControlLeft); + } + result?; + ok( + json!({ "ok": true, "dropped_from": before, "held": self.held().map(|s| self.stack_value(s)) }), + ) + } + "open-inventory" => { + self.playing()?; + self.key_pressed(KeyCode::KeyE)?; + ok(json!({ "ok": true, "screen": self.screen_label() })) + } + "click-button" => { + let label = line + .trim_start() + .strip_prefix("click-button") + .unwrap_or("") + .trim() + .trim_matches('"'); + if label.is_empty() { + return Err("click-button LABEL".into()); + } + let w = widgets::find(&widgets::last(), label)?; + if !w.enabled { + return Err(format!("button {:?} is disabled", w.label).into()); + } + self.synth_click( + event_loop, + w.x + w.w / 2.0, + w.y + w.h / 2.0, + MouseButton::Left, + )?; + ok(json!({ "ok": true, "clicked": w.label, "screen": self.screen_label() })) + } + "click-at" => { + let (x, y) = (a.f64(0, "X")? as f32, a.f64(1, "Y")? as f32); + let button = if a.pos.get(2) == Some(&"right") { + MouseButton::Right + } else { + MouseButton::Left + }; + self.synth_click(event_loop, x, y, button)?; + ok(json!({ "ok": true, "screen": self.screen_label() })) + } + "type" if self.title.is_some() => { + // type [--field name|seed] [--replace] TEXT on the title screens. + let mut text = line + .trim_start() + .strip_prefix("type") + .unwrap_or("") + .strip_prefix(' ') + .unwrap_or(""); + let mut field = None; + let mut replace = false; + loop { + if let Some(rest) = text.strip_prefix("--replace") { + replace = true; + text = rest.trim_start(); + } else if let Some(rest) = text.strip_prefix("--field") { + let rest = rest.trim_start(); + let (name, rest) = rest.split_once(' ').unwrap_or((rest, "")); + field = Some(name.to_owned()); + text = rest; + } else { + break; + } + } + let title = self.title.as_mut().ok_or("no title screen")?; + if let Some(field) = &field { + match &mut title.mode { + horizon_client::title::Mode::Create { focus, .. } => { + *focus = match field.as_str() { + "name" => 0, + "seed" => 1, + other => { + return Err(format!( + "the create-world fields are name and seed, not {other}" + ) + .into()); + } + } + } + _ => return Err("--field works on the create-world screen".into()), + } + } + if replace { + for _ in 0..64 { + title.backspace(); + } + } + if text == "\\b" { + title.backspace(); + } else { + title.type_text(text); + } + ok(json!({ "ok": true, "menu": self.title_value() })) + } + "command" => { + self.need_world()?; + let text = line + .trim_start() + .strip_prefix("command") + .unwrap_or("") + .trim() + .trim_start_matches('/'); + if text.is_empty() { + return Err("command TEXT".into()); + } + let command = format!("/{text}"); + if self.remote.is_some() { + self.send_chat(&command); + return ok(json!({ "ok": true })); + } + self.handle_chat_input(&command); + ok(json!({ "ok": true, "chat": self.ui.chat.recent_lines(5) })) + } + "respawn" if self.remote.is_none() => { + if self.screen != Screen::Dead { + return Err("respawn needs the death screen".into()); + } + let label = + horizon_client::text::Text::translate("deathScreen.respawn", &[]).plain(); + let w = widgets::find(&widgets::last(), &label)?; + self.synth_click( + event_loop, + w.x + w.w / 2.0, + w.y + w.h / 2.0, + MouseButton::Left, + )?; + ok(json!({ "ok": true, "screen": self.screen_label() })) + } + "pause" => { + self.playing()?; + self.key_pressed(KeyCode::Escape)?; + ok(json!({ "ok": true, "screen": self.screen_label() })) + } + "wait" => { + self.need_world()?; + let ticks: u64 = a.get(0, "TICKS")?.parse()?; + let until_tick = self.ticks + ticks; + let timeout = ticks as f64 / 20.0 * 4.0 + 10.0; + self.start_task("wait", TaskKind::Wait { until_tick }, reply, timeout) + } + "stop" => { + let cancelled = self.agent.task.take().map(|t| { + let _ = t.reply.send( + json!({ "error": "cancelled by stop", "cancelled": true }).to_string(), + ); + t.name + }); + self.release_agent_keys(); + ok(json!({ "ok": true, "cancelled": cancelled })) + } + _ => Ok(Handled::No), + } + } + + /// Select the hotbar slot holding `item`. + fn select_item(&mut self, item: &str) -> Result<()> { + if let Some(i) = + (0..9).find(|&i| self.inventory[i].as_ref().is_some_and(|s| &*s.item == item)) + { + self.selected = i; + return Ok(()); + } + if self.inventory.iter().flatten().any(|s| &*s.item == item) { + return Err(format!( + "{item} is in the main inventory, not the hotbar; move it with open-inventory and click-slot" + ) + .into()); + } + Err(format!("no {item} in the inventory").into()) + } + + /// Advance the running agent task; reply when it ends. + pub(super) fn advance_agent(&mut self) { + let Some(mut task) = self.agent.task.take() else { + return; + }; + let timed_out = Instant::now() >= task.deadline; + let result = self.step_task(&mut task.kind, timed_out); + match result { + Some(value) => { + self.release_agent_keys(); + let _ = task.reply.send(value.to_string()); + } + None => self.agent.task = Some(task), + } + } + + fn position(&self) -> [f64; 3] { + self.player + .as_ref() + .map_or([0.0; 3], |p| [p.position.x, p.position.y, p.position.z]) + } + + /// One frame of a task: Some(reply) when it is over. + fn step_task(&mut self, kind: &mut TaskKind, timed_out: bool) -> Option { + // Leaving the world (death, a screen, the title) ends movement tasks. + let interrupted = if self.title.is_some() || self.player.is_none() { + Some("the world closed") + } else if self.screen == Screen::Dead { + Some("the player died") + } else { + None + }; + match kind { + TaskKind::Move { keys, until, from } => { + if let Some(why) = interrupted { + return Some(json!({ "error": why, "position": self.position() })); + } + for &k in keys.iter() { + self.keys.insert(k); + } + if Instant::now() < *until && !timed_out { + return None; + } + let to = self.position(); + Some( + json!({ "ok": true, "from": [from.x, from.y, from.z], "position": to, + "moved": dist([from.x, from.y, from.z], to) }), + ) + } + TaskKind::Wait { until_tick } => { + if self.ticks >= *until_tick { + Some(json!({ "ok": true, "ticks": self.ticks })) + } else if timed_out { + Some( + json!({ "error": "game ticks are not advancing (paused?)", "ticks": self.ticks }), + ) + } else { + None + } + } + TaskKind::Jump { + release_tick, + from_y, + } => { + if self.ticks >= *release_tick || timed_out { + self.keys.remove(&KeyCode::Space); + let on_ground = self.player.as_ref().is_some_and(|p| p.ground.on_ground); + if on_ground || timed_out { + return Some( + json!({ "ok": true, "from_y": from_y, "position": self.position() }), + ); + } + } + None + } + TaskKind::Goto(goto) => { + if let Some(why) = interrupted { + return Some(json!({ "error": why, "position": self.position() })); + } + if self.screen != Screen::Playing { + return Some( + json!({ "error": format!("the {} screen opened", self.screen_label()), "position": self.position() }), + ); + } + self.step_goto(goto, timed_out) + } + TaskKind::Break(b) => self.step_break(b, timed_out, interrupted), + TaskKind::Click { + pos, + before, + inventory, + report_tick, + release, + place, + } => { + if let Some(at) = release { + if Instant::now() < *at && !timed_out && interrupted.is_none() { + return None; + } + let _ = self.agent_mouse(MouseButton::Right, false); + *release = None; + *report_tick = self.ticks + 2; + } + if self.ticks < *report_tick && !timed_out { + return None; + } + let after = pos.map(|p| self.block_json(p)); + let changed = match (*pos, *before) { + (Some(p), Some(b)) => self.world.block(p[0], p[1], p[2]) != b, + _ => false, + }; + let used = delta(inventory, &self.inventory_counts()); + let mut v = json!({ + "ok": !*place || changed, + "block_before": before.map(|b| self.state_string(b)), + "block": after, + "changed": changed, + "inventory_change": used, + "screen": self.screen_label(), + }); + if *place && !changed { + v["error"] = json!( + "the block did not change (blocked by an entity, or the item cannot be placed)" + ); + } + Some(v) + } + TaskKind::Attack { + id, + health, + report_tick, + } => { + if self.ticks < *report_tick && !timed_out { + return None; + } + let now = id.and_then(|id| self.mobs.iter().find(|m| m.id == id)); + Some(json!({ + "ok": true, + "target": id, + "health_before": health, + "health": now.map(|m| m.health), + "alive": now.is_some_and(|m| m.alive()), + "hit": id.is_some(), + })) + } + } + } + + fn step_goto(&mut self, g: &mut Goto, timed_out: bool) -> Option { + let p = self.position(); + let on_ground = self.player.as_ref().is_some_and(|p| p.ground.on_ground); + let horizontal = + |a: [f64; 3], b: [f64; 3]| ((a[0] - b[0]).powi(2) + (a[2] - b[2]).powi(2)).sqrt(); + let arrived = horizontal(p, g.target) <= g.near + && (p[1] - g.target[1]).abs() <= g.near.max(1.0) + 1.0; + if arrived { + return Some( + json!({ "ok": true, "arrived": true, "position": p, "target": g.target, + "distance": dist(p, g.target), "warning": g.warning }), + ); + } + if timed_out { + return Some( + json!({ "error": "timed out before arriving", "arrived": false, "position": p, + "target": g.target, "distance": dist(p, g.target), "warning": g.warning }), + ); + } + // The next waypoint: path nodes, then the target itself. + let mut aim = g.target; + while g.next < g.path.len() { + let n = g.path[g.next]; + let c = [ + f64::from(n[0]) + 0.5, + f64::from(n[1]), + f64::from(n[2]) + 0.5, + ]; + let last = g.next + 1 == g.path.len(); + if horizontal(p, c) < if last { 0.3 } else { 0.45 } && (p[1] - c[1]).abs() < 1.1 { + g.next += 1; + g.progress_tick = self.ticks; + continue; + } + aim = c; + break; + } + // Stuck: jump, then replan from here. + let stalled = self.ticks.saturating_sub(g.progress_tick); + if stalled > 60 && on_ground { + g.jump_until = self.ticks + 4; + } + if stalled > 120 { + g.replans += 1; + if g.replans > 3 { + return Some( + json!({ "error": "stuck: cannot make progress", "arrived": false, "position": p, + "target": g.target, "distance": dist(p, g.target) }), + ); + } + let from = [ + p[0].floor() as i32, + p[1].round() as i32, + p[2].floor() as i32, + ]; + if let Some(path) = self.find_path(from, g.target, g.near) { + g.path = path; + g.next = 1; + } + g.progress_tick = self.ticks; + } + let d = [aim[0] - p[0], aim[2] - p[2]]; + if d[0].abs() + d[1].abs() > 1e-3 { + self.yaw = (-d[0]).atan2(d[1]).to_degrees() as f32; + } + self.keys.insert(KeyCode::KeyW); + if g.sprint { + self.keys.insert(KeyCode::ControlLeft); + } + // Step up onto the next block, or out of water. + let in_water = { + let b = p.map(|v| v.floor() as i32); + self.res.facts.get(self.world.block(b[0], b[1], b[2])).fluid + != horizon_client::blocks::Fluid::None + }; + if aim[1] > p[1] + 0.5 && horizontal(p, aim) < 1.6 && on_ground { + g.jump_until = self.ticks + 4; + } + if self.ticks < g.jump_until || in_water { + self.keys.insert(KeyCode::Space); + } else { + self.keys.remove(&KeyCode::Space); + } + None + } + + fn step_break( + &mut self, + b: &mut Break, + timed_out: bool, + interrupted: Option<&str>, + ) -> Option { + let state = self.world.block(b.pos[0], b.pos[1], b.pos[2]); + if b.collect_until.is_none() { + if let Some(why) = interrupted { + return Some(json!({ "error": why, "pos": b.pos })); + } + if state != b.before { + // Broken: collect what drops for a moment. + let _ = self.agent_mouse(MouseButton::Left, false); + b.broke_tick = self.ticks; + b.collect_until = Some(self.ticks + 20); + } else if timed_out { + let _ = self.agent_mouse(MouseButton::Left, false); + return Some( + json!({ "error": "timed out before the block broke", "pos": b.pos, + "block": self.state_string(state), "ticks": self.ticks - b.started_tick }), + ); + } else if self.screen != Screen::Playing { + return Some( + json!({ "error": format!("the {} screen opened", self.screen_label()), "pos": b.pos }), + ); + } else { + // Keep the crosshair on the block (the player may drift). + if self.pick().is_none_or(|h| h.pos != b.pos) + && let Err(e) = self.aim_block(b.pos, None) + { + let _ = self.agent_mouse(MouseButton::Left, false); + return Some(json!({ "error": e.to_string(), "pos": b.pos })); + } + if !self.left { + let _ = self.agent_mouse(MouseButton::Left, true); + } + return None; + } + } + // New item entities near the block are its drops. + let centre = b.pos.map(|v| f64::from(v) + 0.5); + for item in &self.dropped { + if !b.items_before.contains(&item.id) + && dist(centre, [item.position.x, item.position.y, item.position.z]) < 3.0 + { + b.drops.insert(item.id, item.stack.clone()); + } + } + let remaining = b + .drops + .keys() + .filter(|id| self.dropped.iter().any(|i| i.id == **id)) + .count(); + let until = b.collect_until.unwrap_or(0); + if (self.ticks < until && (remaining > 0 || self.ticks < b.broke_tick + 3)) && !timed_out { + return None; + } + Some(json!({ + "ok": true, + "pos": b.pos, + "block_before": self.state_string(b.before), + "block": self.state_string(state), + "ticks": b.broke_tick - b.started_tick, + "drops": counts(b.drops.values().cloned()), + "picked_up": delta(&b.inventory, &self.inventory_counts()), + "items_left_on_ground": remaining, + "on_ground": self.dropped.iter().filter(|i| b.drops.contains_key(&i.id)).map(|i| json!({ + "id": i.id, "item": &*i.stack.item, "count": i.stack.count, + "pos": [i.position.x, i.position.y, i.position.z], + })).collect::>(), + })) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn parses_flags_and_values() { + let a = parse_args(&["1", "2", "--near", "3", "--sprint", "--timeout=9", "-4"]); + assert_eq!(a.pos, ["1", "2", "-4"]); + assert_eq!(a.flags["near"], Some("3")); + assert_eq!(a.flags["sprint"], None); + assert_eq!(a.flags["timeout"], Some("9")); + assert_eq!(a.num("near", 0.0).unwrap(), 3.0); + assert_eq!(a.num("missing", 7.0).unwrap(), 7.0); + assert!(a.f64(5, "X").is_err()); + assert_eq!(a.block_pos(0).ok(), Some([1, 2, -4])); + assert!(a.block_pos(1).is_err()); + assert_eq!( + parse_args(&["-1", "64", "2"]).block_pos(0).unwrap(), + [-1, 64, 2] + ); + } + + #[test] + fn yaw_and_pitch_follow_minecraft() { + let (yaw, pitch) = yaw_pitch([0.0; 3], [0.0, 0.0, 1.0]); + assert!(yaw.abs() < 1e-4 && pitch.abs() < 1e-4); + let (yaw, _) = yaw_pitch([0.0; 3], [-1.0, 0.0, 0.0]); + assert!((yaw - 90.0).abs() < 1e-4); + let (_, pitch) = yaw_pitch([0.0; 3], [0.0, -1.0, 0.0]); + assert!((pitch - 90.0).abs() < 1e-4); + } + + #[test] + fn faces_round_trip() { + for (name, dir) in FACES { + assert_eq!(face_dir(name).unwrap(), dir); + assert_eq!(face_name(dir), name); + } + assert!(face_dir("sideways").is_err()); + } + + #[test] + fn inventory_delta() { + let before = BTreeMap::from([("a".to_owned(), 2), ("b".to_owned(), 1)]); + let after = BTreeMap::from([("a".to_owned(), 5), ("c".to_owned(), 1)]); + let d = delta(&before, &after); + assert_eq!( + d, + BTreeMap::from([ + ("a".to_owned(), 3), + ("b".to_owned(), -1), + ("c".to_owned(), 1) + ]) + ); + } +} diff --git a/crates/horizon-client/src/game/control.rs b/crates/horizon-client/src/game/control.rs index e64942da7..01ac9da9d 100644 --- a/crates/horizon-client/src/game/control.rs +++ b/crates/horizon-client/src/game/control.rs @@ -87,6 +87,8 @@ impl Game<'_> { self.pending_bench = Some((deadline, reply)); } } + horizon_client::widgets::end_frame(self.frames); + self.advance_agent(); while let Some(request) = self.control.as_ref().and_then(|c| c.try_recv()) { let reply = match self.control_command(&request.line, &request.reply, event_loop) { Ok(Some(value)) => value, @@ -118,6 +120,11 @@ impl Game<'_> { if let Some(value) = self.lan_control(&words)? { return Ok(Some(value)); } + match self.agent_command(&words, line, reply, event_loop)? { + super::agent::Handled::Reply(value) => return Ok(Some(value)), + super::agent::Handled::Deferred => return Ok(None), + super::agent::Handled::No => {} + } match words[0] { "help" => Ok(Some(json!({ "commands": HELP .iter() @@ -126,6 +133,7 @@ impl Game<'_> { .chain(super::lan::HELP) .chain(super::render_control::HELP) .chain(super::sculk::HELP) + .chain(super::agent::HELP) .collect::>() }))), "setblock" | "fill" | "particle" | "signedit" => self.render_command(&words), "sculk" => Ok(Some(self.sculk_command(&words)?)), diff --git a/crates/horizon-client/src/lib.rs b/crates/horizon-client/src/lib.rs index f7900e743..0bb7e0dd9 100644 --- a/crates/horizon-client/src/lib.rs +++ b/crates/horizon-client/src/lib.rs @@ -128,6 +128,7 @@ pub mod title; pub mod ui; pub mod ui_theme; pub mod vanillagen; +pub mod widgets; pub mod window; pub mod world; pub mod worldborder; diff --git a/crates/horizon-client/src/title.rs b/crates/horizon-client/src/title.rs index c5c467616..bd783bfd2 100644 --- a/crates/horizon-client/src/title.rs +++ b/crates/horizon-client/src/title.rs @@ -408,6 +408,7 @@ impl Title { ), } for b in self.buttons(w, h) { + crate::widgets::record(&b.label, [b.x, b.y, b.w, 20.0], b.enabled); let hover = b.enabled && cursor.0 >= b.x && cursor.0 < b.x + b.w diff --git a/crates/horizon-client/src/ui.rs b/crates/horizon-client/src/ui.rs index b1340eb8a..407504cbd 100644 --- a/crates/horizon-client/src/ui.rs +++ b/crates/horizon-client/src/ui.rs @@ -247,6 +247,7 @@ pub fn button( active: bool, hover: bool, ) { + crate::widgets::record(&label.plain(), [x, y, w, h], active); let sprite = if !active { "gui/sprites/widget/button_disabled" } else if hover { diff --git a/crates/horizon-client/src/ui_theme.rs b/crates/horizon-client/src/ui_theme.rs index 9077ab613..bcbc31a69 100644 --- a/crates/horizon-client/src/ui_theme.rs +++ b/crates/horizon-client/src/ui_theme.rs @@ -845,6 +845,12 @@ impl<'a> Ui<'a> { kind: Kind, enabled: bool, ) -> f32 { + if self.real { + // The control socket's button list, in GUI pixels. + let k = self.density / self.scale; + let (ox, oy) = self.offset; + crate::widgets::record(label, [(x + ox) * k, (y + oy) * k, w * k, h * k], enabled); + } let hover = self.interact(id, x, y, w, h, enabled); let saved = self.opacity; if !enabled { diff --git a/crates/horizon-client/src/widgets.rs b/crates/horizon-client/src/widgets.rs new file mode 100644 index 000000000..75366dcba --- /dev/null +++ b/crates/horizon-client/src/widgets.rs @@ -0,0 +1,120 @@ +//! The labelled buttons drawn in the last frame, for the control socket: a +//! script clicks a button by its label (`horizon-ctl click-button Respawn`) +//! whatever screen or menu style drew it. +use std::cell::RefCell; + +/// A button in GUI pixels (the coordinates screens take input in). +#[derive(Clone, Debug)] +pub struct Widget { + pub label: String, + pub x: f32, + pub y: f32, + pub w: f32, + pub h: f32, + pub enabled: bool, +} + +thread_local! { + /// Buttons drawn since the last `end_frame`, and the last frame's. + static WIDGETS: RefCell<(Vec, Vec, u64)> = const { RefCell::new((Vec::new(), Vec::new(), 0)) }; +} + +/// Called by the button drawers. +pub fn record(label: &str, [x, y, w, h]: [f32; 4], enabled: bool) { + let label = label.trim(); + if label.is_empty() { + return; + } + WIDGETS.with_borrow_mut(|(current, _, _)| { + if current.len() < 512 { + current.push(Widget { + label: label.to_owned(), + x, + y, + w, + h, + enabled, + }); + } + }); +} + +/// Once per event loop turn with the count of rendered frames: when a new +/// frame was drawn since the last call, its buttons become the last frame's. +pub fn end_frame(frames: u64) { + WIDGETS.with_borrow_mut(|(current, last, seen)| { + if *seen != frames { + *seen = frames; + *last = std::mem::take(current); + } + }); +} + +/// The buttons of the last drawn frame, top-most last. +pub fn last() -> Vec { + WIDGETS.with_borrow(|(_, last, _)| last.clone()) +} + +/// The button `label` names: an exact match (case-insensitive), else the only +/// button whose label contains it. The top-most wins among equals. +pub fn find(widgets: &[Widget], label: &str) -> Result { + let want = label.trim().to_lowercase(); + let exact: Vec<&Widget> = widgets + .iter() + .filter(|w| w.label.to_lowercase() == want) + .collect(); + if let Some(w) = exact.last() { + return Ok((*w).clone()); + } + let partial: Vec<&Widget> = widgets + .iter() + .filter(|w| w.label.to_lowercase().contains(&want)) + .collect(); + match partial.as_slice() { + [w] => Ok((*w).clone()), + [] => Err(format!( + "no button labelled {label:?}; buttons: {:?}", + widgets.iter().map(|w| w.label.as_str()).collect::>() + )), + many => Err(format!( + "{label:?} matches several buttons: {:?}", + many.iter().map(|w| w.label.as_str()).collect::>() + )), + } +} + +#[cfg(test)] +mod tests { + use super::*; + + fn w(label: &str) -> Widget { + Widget { + label: label.into(), + x: 0.0, + y: 0.0, + w: 10.0, + h: 10.0, + enabled: true, + } + } + + #[test] + fn finds_exact_then_unique_partial() { + let list = [w("Play Selected World"), w("Create New World"), w("Back")]; + assert_eq!(find(&list, "back").unwrap().label, "Back"); + assert_eq!(find(&list, "create").unwrap().label, "Create New World"); + assert!(find(&list, "world").is_err()); + assert!(find(&list, "options").is_err()); + } + + #[test] + fn frames_rotate_only_when_drawn() { + record("Respawn", [1.0, 2.0, 3.0, 4.0], true); + end_frame(1); + assert_eq!(last().len(), 1); + end_frame(1); + assert_eq!(last().len(), 1); + end_frame(2); + assert!(last().is_empty()); + } +}