7bb3347edd
game/agent.rs adds commands an agent can play with: state, looking-at, entities, block, blocks, find-block, inventory, screen and messages read the game as JSON; move, jump, goto (A* over standable blocks), look-at, attack, break, place, interact, use-item, select, drop, open-inventory, click-button, click-at, command, respawn, pause, wait and stop act like a player. Actions that take time run as a task advanced every frame and reply when done, with what happened (break reports drops and pickups, place the block placed). Buttons are clicked by label: the classic and Horizon button drawers record what they draw each frame (widgets.rs), and a click is a synthesized pointer move and press, so it behaves exactly like the mouse on every screen. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
121 lines
3.5 KiB
Rust
121 lines
3.5 KiB
Rust
//! 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<Widget>, Vec<Widget>, 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<Widget> {
|
|
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<Widget, String> {
|
|
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::<Vec<_>>()
|
|
)),
|
|
many => Err(format!(
|
|
"{label:?} matches several buttons: {:?}",
|
|
many.iter().map(|w| w.label.as_str()).collect::<Vec<_>>()
|
|
)),
|
|
}
|
|
}
|
|
|
|
#[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());
|
|
}
|
|
}
|