Archive incari-client (client, 26.3, clean)
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
// Once a frame: where the player is, for the overlay and the screens.
|
||||
fn incari_waypoints_frame(game: &mut crate::game::Game<'_>) {
|
||||
crate::game::incari_waypoints_world::frame(game);
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
# Incari's Waypoints (features/waypoints) for the Rust build (ADR 0051, 0068, 0069). Hooks by name
|
||||
# (native-hooks.json). Its settings are the client's (rust/mods/incari): the module menu shows them.
|
||||
# The waypoints themselves are the client's (`incari_core::waypoint`), in the Java client's files,
|
||||
# so the minimap and the screens share them; this mod opens the world's, watches for deaths, draws
|
||||
# them over the HUD and binds the module's keys.
|
||||
#
|
||||
# The world map's land is what Horizon's minimap scanned (the fork's pending hook
|
||||
# `native_map_tile`, targeted by function), remembered in the Java client's region files.
|
||||
[mod]
|
||||
id = "incari_waypoints"
|
||||
version = "0.1.0"
|
||||
priority = 100
|
||||
|
||||
[[module]]
|
||||
id = "runtime"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_waypoints"
|
||||
source = "runtime.rs"
|
||||
|
||||
[[module]]
|
||||
id = "world"
|
||||
file = "crates/horizon-client/src/game.rs"
|
||||
name = "incari_waypoints_world"
|
||||
source = "world.rs"
|
||||
|
||||
[[injection]]
|
||||
id = "tick"
|
||||
hook = "tick"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "tick.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "frame"
|
||||
hook = "frame"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "frame.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "key"
|
||||
hook = "key"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "key.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "key", ty = "&str", value = "key" },
|
||||
{ name = "pressed", ty = "bool", value = "pressed" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "hud"
|
||||
hook = "hud"
|
||||
hook_version = 1
|
||||
kind = "HEAD"
|
||||
helper = "hud.rs"
|
||||
bindings = [
|
||||
{ name = "game", ty = "&mut crate::game::Game<'_>", value = "&mut *self" },
|
||||
{ name = "batch", ty = "&mut horizon_client::hud::HudBatch", value = "&mut *batch" },
|
||||
{ name = "corners", ty = "&mut horizon_client::extras_hud::Corners", value = "&mut *corners" },
|
||||
]
|
||||
|
||||
[[injection]]
|
||||
id = "map_tile"
|
||||
target = "horizon_client::game::native_hooks::Game<'_>::native_map_tile"
|
||||
kind = "HEAD"
|
||||
helper = "map_tile.rs"
|
||||
bindings = [
|
||||
{ name = "dimension", ty = "&str", value = "dimension" },
|
||||
{ name = "chunk", ty = "[i32; 2]", value = "chunk" },
|
||||
{ name = "colors", ty = "&[u32; 256]", value = "colors" },
|
||||
]
|
||||
@@ -0,0 +1,9 @@
|
||||
// The waypoints over the world: icons and tags, edge arrows, beams.
|
||||
fn incari_waypoints_hud(
|
||||
game: &mut crate::game::Game<'_>,
|
||||
batch: &mut horizon_client::hud::HudBatch,
|
||||
corners: &mut horizon_client::extras_hud::Corners,
|
||||
) {
|
||||
let _ = corners;
|
||||
crate::game::incari_waypoints_world::hud(game, batch);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// The New waypoint (N) and Waypoint list (U) keys, while playing.
|
||||
fn incari_waypoints_key(game: &mut crate::game::Game<'_>, key: &str, pressed: bool) {
|
||||
crate::game::incari_waypoints_world::key(game, key, pressed);
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// A chunk Horizon's minimap scanned: remembered for the world map.
|
||||
fn incari_waypoints_map_tile(dimension: &str, chunk: [i32; 2], colors: &[u32; 256]) {
|
||||
crate::game::incari_waypoints_world::map_tile(dimension, chunk, colors);
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
// Incari's Waypoints for the Rust build (ADR 0051, 0068, 0069), as features/waypoints's
|
||||
// WaypointsModule: what follows the world played and the module's keys. The waypoints, their
|
||||
// file, the death watch and the drawing are the client's (`incari_core::waypoint`,
|
||||
// `incari_core::waypoint_overlay`); `world.rs` is the game side. No game types: rust/lib.rs tests
|
||||
// it.
|
||||
|
||||
/// The module's id, as the Java client's profiles name it.
|
||||
pub const ID: &str = "waypoints";
|
||||
|
||||
/// What changed about the world played since the last tick.
|
||||
#[derive(Clone, Debug, PartialEq, Eq)]
|
||||
pub enum Change {
|
||||
/// The player joined a world (`server/<address>`, `world/<folder>`).
|
||||
Join(String),
|
||||
/// The player left it.
|
||||
Leave,
|
||||
}
|
||||
|
||||
/// Follows the world played, tick to tick (IncariModules.trackWorld): a dimension change keeps
|
||||
/// it, another server replaces it.
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct WorldTracker {
|
||||
world: Option<String>,
|
||||
}
|
||||
|
||||
impl WorldTracker {
|
||||
/// The world now (None outside one): what to do about it, at most a leave then a join.
|
||||
/// Hot path: copies the name only when it changes.
|
||||
pub fn follow(&mut self, now: Option<&str>, out: &mut Vec<Change>) {
|
||||
if self.world.as_deref() == now {
|
||||
return;
|
||||
}
|
||||
if self.world.take().is_some() {
|
||||
out.push(Change::Leave);
|
||||
}
|
||||
if let Some(world) = now {
|
||||
self.world = Some(world.to_owned());
|
||||
out.push(Change::Join(world.to_owned()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// What a key does, by the module's keys.
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum Action {
|
||||
/// A new waypoint where the player stands, in the editor.
|
||||
New,
|
||||
/// The world's waypoints.
|
||||
List,
|
||||
}
|
||||
|
||||
/// The action of `key` (a key going down while playing), given the New waypoint and Waypoint list
|
||||
/// keys; `none` is an unbound key, which binds nothing.
|
||||
pub fn action<K: PartialEq + Copy>(key: K, none: K, new: K, list: K) -> Option<Action> {
|
||||
if key == none {
|
||||
None
|
||||
} else if key == new {
|
||||
Some(Action::New)
|
||||
} else if key == list {
|
||||
Some(Action::List)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn follows_joins_and_leaves() {
|
||||
let mut t = WorldTracker::default();
|
||||
let mut out = Vec::new();
|
||||
t.follow(None, &mut out);
|
||||
assert!(out.is_empty());
|
||||
t.follow(Some("server/a"), &mut out);
|
||||
assert_eq!(out, [Change::Join("server/a".into())]);
|
||||
out.clear();
|
||||
t.follow(Some("server/a"), &mut out);
|
||||
assert!(out.is_empty());
|
||||
t.follow(Some("server/b"), &mut out);
|
||||
assert_eq!(out, [Change::Leave, Change::Join("server/b".into())]);
|
||||
out.clear();
|
||||
t.follow(None, &mut out);
|
||||
assert_eq!(out, [Change::Leave]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn binds_the_modules_keys() {
|
||||
assert_eq!(action(17, 0, 17, 24), Some(Action::New));
|
||||
assert_eq!(action(24, 0, 17, 24), Some(Action::List));
|
||||
assert_eq!(action(5, 0, 17, 24), None);
|
||||
assert_eq!(action(0, 0, 0, 24), None);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
// Once a tick: the world's waypoints and remembered map opened or closed, deaths, due saves.
|
||||
fn incari_waypoints_tick(game: &mut crate::game::Game<'_>) {
|
||||
crate::game::incari_waypoints_world::tick(game);
|
||||
}
|
||||
@@ -0,0 +1,203 @@
|
||||
// The Waypoints mod's game side (module `incari_waypoints_world`): the world's waypoints and
|
||||
// remembered map opened as the player joins and closed as they leave, the player handed to the
|
||||
// client each frame, deaths marked, the overlay drawn and the keys bound. The logic is the
|
||||
// client's (`incari_core::waypoint`, `waypoint_overlay`, `map_memory`); this is its glue. Not
|
||||
// tested alone: `./incari build --only rust` compiles it.
|
||||
use crate::game::incari_client as c;
|
||||
use crate::game::incari_waypoints as m;
|
||||
use incari_core::framework::system::Storage;
|
||||
use incari_core::input::Key;
|
||||
use incari_core::map_memory::{self, Bytes, Zlib};
|
||||
use incari_core::waypoint::{self, DeathWatch};
|
||||
use incari_core::waypoint_overlay::{Look, Overlay, View};
|
||||
use std::cell::RefCell;
|
||||
|
||||
/// `incari/` in the game folder.
|
||||
fn folder() -> std::path::PathBuf {
|
||||
horizon_os::env::current_dir().unwrap_or_default().join("incari")
|
||||
}
|
||||
|
||||
fn read(path: &str) -> Option<Vec<u8>> {
|
||||
horizon_os::fs::read(folder().join(path)).ok()
|
||||
}
|
||||
|
||||
fn write(path: &str, data: &[u8]) {
|
||||
let path = folder().join(path);
|
||||
if let Some(dir) = path.parent() {
|
||||
let _ = horizon_os::fs::create_dir_all(dir);
|
||||
}
|
||||
if let Err(e) = horizon_os::fs::write(&path, data) {
|
||||
log::warn!("Incari: couldn't save {}: {e}", path.display());
|
||||
}
|
||||
}
|
||||
|
||||
/// The waypoints' files, under `incari/modules/` as the profiles are.
|
||||
struct ModuleFiles;
|
||||
|
||||
impl Storage for ModuleFiles {
|
||||
fn read(&self, path: &str) -> Option<String> {
|
||||
read(&format!("modules/{path}")).and_then(|b| String::from_utf8(b).ok())
|
||||
}
|
||||
fn write(&mut self, path: &str, text: &str) {
|
||||
write(&format!("modules/{path}"), text.as_bytes());
|
||||
}
|
||||
fn delete(&mut self, path: &str) {
|
||||
let _ = horizon_os::fs::remove_file(folder().join("modules").join(path));
|
||||
}
|
||||
}
|
||||
|
||||
/// The remembered map's files, under `incari/map/`.
|
||||
struct MapFiles;
|
||||
|
||||
impl Bytes for MapFiles {
|
||||
fn read(&self, path: &str) -> Option<Vec<u8>> {
|
||||
read(path)
|
||||
}
|
||||
fn write(&mut self, path: &str, data: &[u8]) {
|
||||
write(path, data);
|
||||
}
|
||||
}
|
||||
|
||||
/// zlib at its fastest level, as the Java client deflates the map's regions.
|
||||
const ZLIB: Zlib = Zlib {
|
||||
deflate: |bytes| miniz_oxide::deflate::compress_to_vec_zlib(bytes, 1),
|
||||
inflate: |bytes| miniz_oxide::inflate::decompress_to_vec_zlib(bytes).ok(),
|
||||
};
|
||||
|
||||
#[derive(Default)]
|
||||
struct State {
|
||||
tracker: m::WorldTracker,
|
||||
changes: Vec<m::Change>,
|
||||
deaths: DeathWatch,
|
||||
overlay: Overlay,
|
||||
zlib: bool,
|
||||
}
|
||||
|
||||
// The window's thread only: ticks, frames, keys and the HUD all run there.
|
||||
thread_local! {
|
||||
static STATE: RefCell<State> = RefCell::new(State::default());
|
||||
}
|
||||
|
||||
/// Once a tick, the window's: the world followed, deaths watched, due saves.
|
||||
pub(super) fn tick(game: &mut super::Game<'_>) {
|
||||
if !game.native_window() || !c::ready() {
|
||||
return;
|
||||
}
|
||||
// Planned fork helper: `server/<address>` or `world/<folder>`, as `Game.world()` names it.
|
||||
let world = game.native_world_id();
|
||||
let dead = game.native_ui_screen() == "dead";
|
||||
let watch = c::settings(m::ID, |s| (s.bool("deaths"), s.bool("clear-deaths")));
|
||||
let remember = c::settings("minimap", |s| s.bool("remember")).unwrap_or(true);
|
||||
STATE.with(|cell| {
|
||||
let Ok(mut state) = cell.try_borrow_mut() else {
|
||||
return;
|
||||
};
|
||||
let state = &mut *state;
|
||||
if !state.zlib {
|
||||
state.zlib = true;
|
||||
map_memory::with(|mm| mm.set_zlib(ZLIB));
|
||||
}
|
||||
state.changes.clear();
|
||||
state.tracker.follow(world.as_deref(), &mut state.changes);
|
||||
for change in state.changes.drain(..) {
|
||||
match change {
|
||||
m::Change::Leave => {
|
||||
waypoint::with(|w| w.close(&mut ModuleFiles));
|
||||
map_memory::with(|mm| mm.close(&mut MapFiles));
|
||||
}
|
||||
// Before anything joining reads them.
|
||||
m::Change::Join(name) => {
|
||||
waypoint::with(|w| w.open(&name, &mut ModuleFiles));
|
||||
map_memory::with(|mm| mm.open(&name, &mut MapFiles));
|
||||
}
|
||||
}
|
||||
}
|
||||
waypoint::with(|w| {
|
||||
if let Some((mark, clear)) = watch {
|
||||
waypoint::with_player(|p| state.deaths.tick(w, p, dead, mark, clear));
|
||||
}
|
||||
w.tick(&mut ModuleFiles);
|
||||
for problem in w.problems.drain(..) {
|
||||
log::warn!("Incari: waypoints: {problem}");
|
||||
}
|
||||
});
|
||||
map_memory::with(|mm| {
|
||||
mm.set_remember(remember, &MapFiles);
|
||||
mm.tick(&mut MapFiles);
|
||||
for problem in mm.problems.drain(..) {
|
||||
log::warn!("Incari: map: {problem}");
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// Once a frame: where the player is (no copy of their dimension unless it changed).
|
||||
pub(super) fn frame(game: &mut super::Game<'_>) {
|
||||
if !game.native_window() {
|
||||
return;
|
||||
}
|
||||
let player = game.native_player();
|
||||
waypoint::set_player(player.as_ref().map(|p| ([p.x, p.y, p.z], p.yaw, p.dimension)));
|
||||
}
|
||||
|
||||
/// The module's keys, while playing.
|
||||
pub(super) fn key(game: &mut super::Game<'_>, name: &str, pressed: bool) {
|
||||
if !pressed || !game.native_playing() {
|
||||
return;
|
||||
}
|
||||
let Some((new, list)) = c::settings(m::ID, |s| (s.key("new"), s.key("list"))) else {
|
||||
return;
|
||||
};
|
||||
let Some(action) = m::action(Key::from_name(name), Key::None, new, list) else {
|
||||
return;
|
||||
};
|
||||
let in_world = waypoint::with(|w| w.world().is_some()).unwrap_or(false);
|
||||
if !in_world {
|
||||
return;
|
||||
}
|
||||
let screen: Box<dyn incari_core::screen::Screen> = match action {
|
||||
m::Action::List => Box::new(incari_core::screens::waypoint_list::WaypointList::new()),
|
||||
m::Action::New => {
|
||||
let Some(p) = waypoint::player() else {
|
||||
return;
|
||||
};
|
||||
let [x, y, z] = p.block();
|
||||
let Some(draft) = waypoint::with(|w| waypoint::draft(w.all(), x, y, z, &p.dimension)) else {
|
||||
return;
|
||||
};
|
||||
Box::new(incari_core::screens::waypoint_edit::WaypointEdit::new(draft, true))
|
||||
}
|
||||
};
|
||||
// Planned client helper: opens an Incari screen over the world.
|
||||
c::open(game, screen);
|
||||
}
|
||||
|
||||
/// The waypoints over the world. Hot path: once a frame, nothing allocated once warm.
|
||||
pub(super) fn hud(game: &mut super::Game<'_>, batch: &mut horizon_client::hud::HudBatch) {
|
||||
let Some(look) = c::settings(m::ID, Look::of) else {
|
||||
return;
|
||||
};
|
||||
let game: &super::Game<'_> = game;
|
||||
let project = |at: [f64; 3]| game.native_project(at);
|
||||
// Planned client helper: draws with the client's painter over the HUD, at the screen's origin.
|
||||
c::overlay(game, batch, |p, screen| {
|
||||
waypoint::with_player(|player| {
|
||||
let Some(player) = player else {
|
||||
return;
|
||||
};
|
||||
let view = View { player, screen, project: &project };
|
||||
waypoint::with(|store| {
|
||||
STATE.with(|cell| {
|
||||
if let Ok(mut state) = cell.try_borrow_mut() {
|
||||
state.overlay.draw(p, store, &look, &view);
|
||||
}
|
||||
})
|
||||
});
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/// A chunk Horizon's minimap scanned, as the map shades it: remembered for the world map.
|
||||
pub(super) fn map_tile(dimension: &str, chunk: [i32; 2], colors: &[u32; 256]) {
|
||||
map_memory::with(|m| m.record(&mut MapFiles, dimension, chunk[0], chunk[1], colors));
|
||||
}
|
||||
Reference in New Issue
Block a user