382380fc8b
The official Minimap mod (mods/official/minimap) shows the extra's map through render.set-minimap. The extra's switch and its five Extras rows go; the game still draws a mod's map from the same tiles. A player who had the minimap on gets the mod installed and turned on at the first start, with the corner, size, shape, zoom and turning carried into its storage, and one chat line. horizon-extras.txt keeps the old keys. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
274 lines
11 KiB
Rust
274 lines
11 KiB
Rust
//! Horizon's extras in the game (`docs/extras.md`); a mod's minimap
|
|
//! (`render.set-minimap`: its tiles, image and HUD) and zoom (`render.set-zoom`) on the camera; clicks
|
|
//! and frame times for mods (`input.clicks-per-second`, `render.frame-stats`).
|
|
//! Each hook does nothing while its extra is off and no mod runs.
|
|
use super::{Dim, Game, Screen};
|
|
use horizon_client::{
|
|
extras::{Corner, Cps, FpsCounter, fov_target},
|
|
extras_hud::{self, Corners},
|
|
hud::{HudBatch, MINIMAP_SPRITE},
|
|
minimap::{self, Layer, Minimap, View},
|
|
multiplayer,
|
|
reconnect::{Retry, Tracker},
|
|
title::Mode,
|
|
};
|
|
use std::time::Duration;
|
|
use winit::event::MouseButton;
|
|
|
|
#[derive(Default)]
|
|
pub(super) struct ExtrasState {
|
|
fps: FpsCounter,
|
|
/// Left and right mouse buttons: held, and their clicks.
|
|
mouse: [bool; 2],
|
|
cps: [Cps; 2],
|
|
minimap: Minimap,
|
|
/// The dimension the minimap's tiles belong to.
|
|
minimap_dim: Option<Dim>,
|
|
/// Auto-reconnect's attempts at the current server.
|
|
reconnect: Tracker,
|
|
}
|
|
/// CPU time the minimap's tiles may take per frame.
|
|
const MINIMAP_BUDGET: Duration = Duration::from_micros(300);
|
|
|
|
impl Game<'_> {
|
|
fn extras_now(&self) -> f64 {
|
|
self.started.elapsed().as_secs_f64()
|
|
}
|
|
/// Mouse buttons while playing, for mods' clicks.
|
|
pub(super) fn extras_mouse(&mut self, button: MouseButton, pressed: bool) {
|
|
let i = match button {
|
|
MouseButton::Left => 0,
|
|
MouseButton::Right => 1,
|
|
_ => return,
|
|
};
|
|
self.extras.mouse[i] = pressed;
|
|
// For mods (`input.clicks-per-second`).
|
|
if pressed && self.mods.running() {
|
|
let now = self.extras_now();
|
|
self.extras.cps[i].click(now);
|
|
}
|
|
}
|
|
/// Once per frame: the frame's time, and keys let go behind screens.
|
|
pub(super) fn extras_frame(&mut self, elapsed: Duration) {
|
|
// Under agent control the window may lack focus without pausing.
|
|
let focused = self.focused || self.control.is_some();
|
|
let playing = self.screen == Screen::Playing && self.title.is_none() && focused;
|
|
if !playing {
|
|
self.extras.mouse = [false; 2];
|
|
}
|
|
self.update_minimap();
|
|
// For mods (`render.frame-stats`).
|
|
if self.mods.running() {
|
|
let now = self.extras_now();
|
|
self.extras.fps.record(now, elapsed.as_secs_f32() * 1000.0);
|
|
} else {
|
|
self.extras.fps.clear();
|
|
}
|
|
}
|
|
/// The frame counter for mods: fps, min, 1% low.
|
|
#[cfg(feature = "mods")]
|
|
pub(super) fn frame_reading(&self) -> (u32, u32, u32) {
|
|
let r = self.extras.fps.reading();
|
|
(r.fps, r.min, r.low)
|
|
}
|
|
/// Attack and use clicks in the last second, and whether each is held.
|
|
#[cfg(feature = "mods")]
|
|
pub(super) fn mouse_reading(&mut self) -> ((u32, u32), [bool; 2]) {
|
|
let now = self.extras_now();
|
|
let [l, r] = &mut self.extras.cps;
|
|
let cps = (l.per_second(now) as u32, r.per_second(now) as u32);
|
|
(cps, self.extras.mouse)
|
|
}
|
|
/// Auto-reconnect: a connection to `address` as `name` starts.
|
|
pub(super) fn extras_connecting(&mut self, address: &str, name: &str) {
|
|
self.extras.reconnect.connecting(address, name);
|
|
}
|
|
/// Auto-reconnect: the server let the player in.
|
|
pub(super) fn extras_joined(&mut self) {
|
|
self.extras.reconnect.joined(std::time::Instant::now());
|
|
}
|
|
/// Auto-reconnect: the player left the server on purpose.
|
|
pub(super) fn extras_left(&mut self) {
|
|
self.extras.reconnect.reset();
|
|
}
|
|
/// Auto-reconnect's next attempt after a disconnect, when it is on and
|
|
/// the reason allows one.
|
|
pub(super) fn reconnect_after(&mut self, reason: &str, from_server: bool) -> Option<Retry> {
|
|
if !self.settings.extras.auto_reconnect {
|
|
self.extras.reconnect.reset();
|
|
return None;
|
|
}
|
|
let now = std::time::Instant::now();
|
|
self.extras.reconnect.ended(reason, from_server, now)
|
|
}
|
|
/// Show the attempt's countdown on the Disconnected screen.
|
|
pub(super) fn offer_reconnect(&mut self, retry: Option<Retry>) {
|
|
let Some(retry) = retry else {
|
|
return;
|
|
};
|
|
if let Some(title) = self.title.as_mut()
|
|
&& let Mode::Multiplayer(multiplayer::Screen::Disconnected { retry: slot, .. }) =
|
|
&mut title.mode
|
|
{
|
|
log::info!(
|
|
"Horizon: reconnecting to {} in {} s",
|
|
horizon_client::net::shown_address(&retry.address),
|
|
retry.seconds_left(std::time::Instant::now())
|
|
);
|
|
*slot = Some(retry);
|
|
}
|
|
}
|
|
/// Auto-reconnect: connect again once the countdown is over. An
|
|
/// online-mode server gets the signed-in account again: `connect_server`
|
|
/// takes it from `auth::current()`, held in memory for the whole run
|
|
/// (the launcher's session file is gone after the first read).
|
|
pub(super) fn extras_retry(&mut self) -> horizon_client::Result<()> {
|
|
if let Some(retry) = self.title.as_mut().and_then(|t| t.due_retry()) {
|
|
self.connect_server(&retry.address, &retry.name)?;
|
|
}
|
|
Ok(())
|
|
}
|
|
/// The minimap a mod shows (`render.set-minimap`): its corner, its side
|
|
/// in GUI pixels and its view of the player.
|
|
fn minimap_shown(&self) -> Option<(Corner, f32, View)> {
|
|
let p = self.player.as_ref()?;
|
|
if self.title.is_some() || self.ui.hide_gui {
|
|
return None;
|
|
}
|
|
let m = self.mod_minimap()?;
|
|
let view = View {
|
|
x: p.position.x,
|
|
z: p.position.z,
|
|
yaw: self.yaw,
|
|
rotate: m.rotate,
|
|
blocks_per_pixel: m.blocks_per_pixel,
|
|
round: m.round,
|
|
};
|
|
Some((m.corner, m.size, view))
|
|
}
|
|
/// Refresh the minimap's tiles within the frame budget, recompose its
|
|
/// image when the view or a tile changed, and queue the upload.
|
|
fn update_minimap(&mut self) {
|
|
let Some((_, _, view)) = self.minimap_shown() else {
|
|
if self.mod_minimap().is_none() || self.player.is_none() {
|
|
self.extras.minimap.clear();
|
|
self.extras.minimap_dim = None;
|
|
}
|
|
return;
|
|
};
|
|
if self.extras.minimap_dim != Some(self.dim) {
|
|
self.extras.minimap.clear();
|
|
self.extras.minimap_dim = Some(self.dim);
|
|
}
|
|
let Some(p) = self.player.as_ref() else {
|
|
return;
|
|
};
|
|
let head = (p.position.y + f64::from(p.dimensions.eye_height)).floor() as i32;
|
|
let (bx, bz) = (view.x.floor() as i32, view.z.floor() as i32);
|
|
// Underground, under a roof or in the Nether: the floor around the player.
|
|
let covered = self.world.light(bx, head, bz) >> 4 == 0;
|
|
let layer = if self.dim == Dim::Nether || covered {
|
|
Layer::Below(head)
|
|
} else {
|
|
Layer::Surface
|
|
};
|
|
// The chunks the image can reach (its corners when it turns).
|
|
let reach = minimap::SIZE as f32 / 2.0 * view.blocks_per_pixel * std::f32::consts::SQRT_2;
|
|
let radius = ((reach / 16.0).ceil() as i32 + 1).min(self.settings.render_distance.max(2));
|
|
self.extras.minimap.update(
|
|
&self.world,
|
|
self.res.facts(),
|
|
(bx >> 4, bz >> 4),
|
|
radius,
|
|
layer,
|
|
MINIMAP_BUDGET,
|
|
);
|
|
if !self.extras.minimap.compose(view) {
|
|
return;
|
|
}
|
|
let Some(rect) = self.gui.atlas.sprites.get(MINIMAP_SPRITE).copied() else {
|
|
return;
|
|
};
|
|
let (w, h) = (self.gui.atlas.width as f32, self.gui.atlas.height as f32);
|
|
self.map_patches.push(horizon_client::font::Patch {
|
|
x: (rect.u0 * w).round() as u32,
|
|
y: (rect.v0 * h).round() as u32,
|
|
width: minimap::SIZE as u32,
|
|
height: minimap::SIZE as u32,
|
|
rgba: self.extras.minimap.image.clone(),
|
|
});
|
|
}
|
|
/// A mod's zoom in effect (`render.set-zoom`): (factor, smooth, turning
|
|
/// scale); none is 1.
|
|
fn zoom_now(&self) -> (f32, bool, f64) {
|
|
self.mod_zoom().map_or((1.0, true, 1.0), |z| {
|
|
(z.factor, z.smooth, f64::from(z.turn))
|
|
})
|
|
}
|
|
/// The field-of-view modifier target, divided by a smooth zoom.
|
|
pub(super) fn zoomed_fov_target(&self, vanilla: f32) -> f32 {
|
|
match self.zoom_now() {
|
|
(factor, true, _) => fov_target(vanilla, factor),
|
|
_ => vanilla,
|
|
}
|
|
}
|
|
/// The field of view in degrees, divided by an instant zoom.
|
|
pub(super) fn zoomed_fov_degrees(&self, fov: f32) -> f32 {
|
|
match self.zoom_now() {
|
|
(factor, false, _) => fov / factor,
|
|
_ => fov,
|
|
}
|
|
}
|
|
/// Mouse turning while zoomed.
|
|
pub(super) fn zoom_turn_scale(&self) -> f64 {
|
|
self.zoom_now().2
|
|
}
|
|
/// The minimap and mods' corner panels, in the HUD layer (hidden with F1
|
|
/// by the caller). The top left corner gives way to the debug screen.
|
|
pub(super) fn draw_extras(&mut self, batch: &mut HudBatch) {
|
|
let mod_panels = self.mods.has_panels();
|
|
let map = self.minimap_shown();
|
|
if map.is_none() && !mod_panels {
|
|
return;
|
|
}
|
|
let visible = |corner| !(self.debug && corner == Corner::TopLeft);
|
|
let mut corners = Corners::new(batch.width, batch.height, self.effect_rows_height());
|
|
let atlas = &self.gui.atlas;
|
|
if let Some((map_corner, map_size, view)) = map
|
|
&& visible(map_corner)
|
|
&& !self.extras.minimap.image.is_empty()
|
|
&& let Some(image) = atlas.sprites.get(MINIMAP_SPRITE).copied()
|
|
{
|
|
let (arrow, north) = if view.rotate {
|
|
(0.0, Some(view.north()))
|
|
} else {
|
|
(180.0 + view.yaw, None)
|
|
};
|
|
let look = extras_hud::MinimapLook {
|
|
corner: map_corner,
|
|
size: map_size,
|
|
image,
|
|
arrow,
|
|
north,
|
|
};
|
|
extras_hud::draw_minimap(batch, atlas, &mut corners, &look);
|
|
}
|
|
if mod_panels {
|
|
self.draw_mod_panels(batch, &mut corners);
|
|
}
|
|
}
|
|
/// The height the status effect icons take at the top right
|
|
/// (`Hud.extractEffects`: beneficial at y 1, harmful at y 26).
|
|
fn effect_rows_height(&self) -> f32 {
|
|
let effects = &self.potions.effects.list;
|
|
if effects.is_empty() || self.screen != Screen::Playing {
|
|
return 0.0;
|
|
}
|
|
let harmful = effects.iter().any(|e| {
|
|
horizon_client::brewing::effect(e.id)
|
|
.is_some_and(|d| d.category != horizon_client::brewing::Category::Beneficial)
|
|
});
|
|
if harmful { 50.0 } else { 25.0 }
|
|
}
|
|
}
|