From 25e4e09ab43f7e357bf4e919fff732abe2c95665 Mon Sep 17 00:00:00 2001 From: Woeful_Wolf <54476280+WoefulWolf@users.noreply.github.com> Date: Sat, 20 Jan 2024 19:08:41 +0200 Subject: [PATCH 1/2] Fixes #1923 - Pickaxe tooltip wrong for digging --- voxygen/src/hud/mod.rs | 83 +++++++++++++++++++---------------- voxygen/src/session/mod.rs | 25 +++++++---- voxygen/src/session/target.rs | 6 +-- 3 files changed, 65 insertions(+), 49 deletions(-) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index e21609c4e3..e6a7a61d16 100755 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -669,7 +669,7 @@ pub struct DebugInfo { pub struct HudInfo { pub is_aiming: bool, - pub is_mining: bool, + pub active_mine_tool: Option, pub is_first_person: bool, pub viewpoint_entity: specs::Entity, pub mutable_viewpoint: bool, @@ -2122,43 +2122,52 @@ impl Hud { })] }, BlockInteraction::Mine(mine_tool) => { - if info.is_mining { - match mine_tool { - ToolKind::Pick => { - vec![( - Some(GameInput::Primary), - i18n.get_msg("hud-mine").to_string(), - )] - }, - ToolKind::Shovel => { - vec![( - Some(GameInput::Primary), - i18n.get_msg("hud-dig").to_string(), - )] - }, - _ => { - vec![( - None, - i18n.get_msg("hud-mine-needs_unhandled_case").to_string(), - )] - }, - } - } else { - match mine_tool { - ToolKind::Pick => { - vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())] - }, - ToolKind::Shovel => { - vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())] + match &info.active_mine_tool { + Some(active_mine_tool) => { + match (mine_tool, active_mine_tool) { + (ToolKind::Pick, ToolKind::Pick) => { + vec![( + Some(GameInput::Primary), + i18n.get_msg("hud-mine").to_string(), + )] + }, + (ToolKind::Pick, _) => { + vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())] + }, + (ToolKind::Shovel, ToolKind::Shovel) => { + vec![( + Some(GameInput::Primary), + i18n.get_msg("hud-dig").to_string(), + )] + }, + (ToolKind::Shovel, _) => { + vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())] + }, + _ => { + vec![( + None, + i18n.get_msg("hud-mine-needs_unhandled_case").to_string(), + )] + }, + } + }, + None => { + match mine_tool { + ToolKind::Pick => { + vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())] + }, + ToolKind::Shovel => { + vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())] + } + // TODO: The required tool for mining something may not always be a + // pickaxe! + _ => { + vec![( + None, + i18n.get_msg("hud-mine-needs_unhandled_case").to_string(), + )] + }, } - // TODO: The required tool for mining something may not always be a - // pickaxe! - _ => { - vec![( - None, - i18n.get_msg("hud-mine-needs_unhandled_case").to_string(), - )] - }, } } }, diff --git a/voxygen/src/session/mod.rs b/voxygen/src/session/mod.rs index f0fbbce1fa..976840cb64 100644 --- a/voxygen/src/session/mod.rs +++ b/voxygen/src/session/mod.rs @@ -601,15 +601,22 @@ impl PlayState for SessionState { .get(player_entity) .map_or_else(|| false, |cb| cb.enabled); - let is_mining = client + let active_mine_tool: Option = if client.is_wielding() == Some(true) { client .inventories() .get(player_entity) .and_then(|inv| inv.equipped(EquipSlot::ActiveMainhand)) .and_then(|item| item.tool_info()) - .map_or(false, |tool_kind| { - matches!(tool_kind, ToolKind::Pick | ToolKind::Shovel) + .map_or(None, |tool_kind| { + match tool_kind { + ToolKind::Pick => Some(tool_kind), + ToolKind::Shovel => Some(tool_kind), + _ => None, + } }) - && client.is_wielding() == Some(true); + } else { + None + }; + // Check to see whether we're aiming at anything let (build_target, collect_target, entity_target, mine_target, terrain_target) = @@ -618,7 +625,7 @@ impl PlayState for SessionState { cam_pos, cam_dir, can_build, - is_mining, + active_mine_tool, self.viewpoint_entity().0, ); @@ -651,17 +658,17 @@ impl PlayState for SessionState { // Nearest block to consider with GameInput primary or secondary key. let nearest_block_dist = find_shortest_distance(&[ - mine_target.filter(|_| is_mining).map(|t| t.distance), + mine_target.filter(|_| active_mine_tool.is_some()).map(|t| t.distance), build_target.filter(|_| can_build).map(|t| t.distance), ]); // Nearest block to be highlighted in the scene (self.scene.set_select_pos). let nearest_scene_dist = find_shortest_distance(&[ nearest_block_dist, - collect_target.filter(|_| !is_mining).map(|t| t.distance), + collect_target.filter(|_| active_mine_tool.is_none()).map(|t| t.distance), ]); // Set break_block_pos only if mining is closest. self.inputs.break_block_pos = if let Some(mt) = - mine_target.filter(|mt| is_mining && nearest_scene_dist == Some(mt.distance)) + mine_target.filter(|mt| active_mine_tool.is_some() && nearest_scene_dist == Some(mt.distance)) { self.scene.set_select_pos(Some(mt.position_int())); Some(mt.position) @@ -1507,7 +1514,7 @@ impl PlayState for SessionState { global_state.clock.get_stable_dt(), HudInfo { is_aiming, - is_mining, + active_mine_tool, is_first_person: matches!( self.scene.camera().get_mode(), camera::CameraMode::FirstPerson diff --git a/voxygen/src/session/target.rs b/voxygen/src/session/target.rs index 7f2b322875..c7359a5524 100644 --- a/voxygen/src/session/target.rs +++ b/voxygen/src/session/target.rs @@ -3,7 +3,7 @@ use vek::*; use client::{self, Client}; use common::{ - comp, + comp::{self, tool::ToolKind}, consts::MAX_PICKUP_RANGE, link::Is, mounting::{Mount, Rider}, @@ -50,7 +50,7 @@ pub(super) fn targets_under_cursor( cam_pos: Vec3, cam_dir: Vec3, can_build: bool, - is_mining: bool, + active_mine_tool: Option, viewpoint_entity: specs::Entity, ) -> ( Option>, @@ -103,7 +103,7 @@ pub(super) fn targets_under_cursor( }; let (collect_pos, _, collect_cam_ray) = find_pos(|b: Block| b.is_collectible()); - let (mine_pos, _, mine_cam_ray) = is_mining + let (mine_pos, _, mine_cam_ray) = active_mine_tool.is_some() .then(|| find_pos(|b: Block| b.mine_tool().is_some())) .unwrap_or((None, None, None)); let (solid_pos, place_block_pos, solid_cam_ray) = find_pos(|b: Block| b.is_filled()); From 3cb5132ab9c661bf36d306fa5c0191e9cb6aaa63 Mon Sep 17 00:00:00 2001 From: Woeful_Wolf <54476280+WoefulWolf@users.noreply.github.com> Date: Sat, 20 Jan 2024 20:40:16 +0200 Subject: [PATCH 2/2] Simplified match, use filter and cargo fmt suggestions --- voxygen/src/hud/mod.rs | 70 ++++++++++++----------------------- voxygen/src/session/mod.rs | 32 ++++++++-------- voxygen/src/session/target.rs | 3 +- 3 files changed, 41 insertions(+), 64 deletions(-) diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index e6a7a61d16..0a9f99ea0a 100755 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -2122,53 +2122,31 @@ impl Hud { })] }, BlockInteraction::Mine(mine_tool) => { - match &info.active_mine_tool { - Some(active_mine_tool) => { - match (mine_tool, active_mine_tool) { - (ToolKind::Pick, ToolKind::Pick) => { - vec![( - Some(GameInput::Primary), - i18n.get_msg("hud-mine").to_string(), - )] - }, - (ToolKind::Pick, _) => { - vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())] - }, - (ToolKind::Shovel, ToolKind::Shovel) => { - vec![( - Some(GameInput::Primary), - i18n.get_msg("hud-dig").to_string(), - )] - }, - (ToolKind::Shovel, _) => { - vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())] - }, - _ => { - vec![( - None, - i18n.get_msg("hud-mine-needs_unhandled_case").to_string(), - )] - }, - } + match (mine_tool, &info.active_mine_tool) { + (ToolKind::Pick, Some(ToolKind::Pick)) => { + vec![( + Some(GameInput::Primary), + i18n.get_msg("hud-mine").to_string(), + )] + }, + (ToolKind::Pick, _) => { + vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())] + }, + (ToolKind::Shovel, Some(ToolKind::Shovel)) => { + vec![( + Some(GameInput::Primary), + i18n.get_msg("hud-dig").to_string(), + )] + }, + (ToolKind::Shovel, _) => { + vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())] + }, + _ => { + vec![( + None, + i18n.get_msg("hud-mine-needs_unhandled_case").to_string(), + )] }, - None => { - match mine_tool { - ToolKind::Pick => { - vec![(None, i18n.get_msg("hud-mine-needs_pickaxe").to_string())] - }, - ToolKind::Shovel => { - vec![(None, i18n.get_msg("hud-mine-needs_shovel").to_string())] - } - // TODO: The required tool for mining something may not always be a - // pickaxe! - _ => { - vec![( - None, - i18n.get_msg("hud-mine-needs_unhandled_case").to_string(), - )] - }, - } - } } }, BlockInteraction::Mount => { diff --git a/voxygen/src/session/mod.rs b/voxygen/src/session/mod.rs index 976840cb64..ee23990583 100644 --- a/voxygen/src/session/mod.rs +++ b/voxygen/src/session/mod.rs @@ -601,22 +601,16 @@ impl PlayState for SessionState { .get(player_entity) .map_or_else(|| false, |cb| cb.enabled); - let active_mine_tool: Option = if client.is_wielding() == Some(true) { client - .inventories() - .get(player_entity) - .and_then(|inv| inv.equipped(EquipSlot::ActiveMainhand)) - .and_then(|item| item.tool_info()) - .map_or(None, |tool_kind| { - match tool_kind { - ToolKind::Pick => Some(tool_kind), - ToolKind::Shovel => Some(tool_kind), - _ => None, - } - }) + let active_mine_tool: Option = if client.is_wielding() == Some(true) { + client + .inventories() + .get(player_entity) + .and_then(|inv| inv.equipped(EquipSlot::ActiveMainhand)) + .and_then(|item| item.tool_info()) + .filter(|tool_kind| matches!(tool_kind, ToolKind::Pick | ToolKind::Shovel)) } else { None }; - // Check to see whether we're aiming at anything let (build_target, collect_target, entity_target, mine_target, terrain_target) = @@ -658,17 +652,21 @@ impl PlayState for SessionState { // Nearest block to consider with GameInput primary or secondary key. let nearest_block_dist = find_shortest_distance(&[ - mine_target.filter(|_| active_mine_tool.is_some()).map(|t| t.distance), + mine_target + .filter(|_| active_mine_tool.is_some()) + .map(|t| t.distance), build_target.filter(|_| can_build).map(|t| t.distance), ]); // Nearest block to be highlighted in the scene (self.scene.set_select_pos). let nearest_scene_dist = find_shortest_distance(&[ nearest_block_dist, - collect_target.filter(|_| active_mine_tool.is_none()).map(|t| t.distance), + collect_target + .filter(|_| active_mine_tool.is_none()) + .map(|t| t.distance), ]); // Set break_block_pos only if mining is closest. - self.inputs.break_block_pos = if let Some(mt) = - mine_target.filter(|mt| active_mine_tool.is_some() && nearest_scene_dist == Some(mt.distance)) + self.inputs.break_block_pos = if let Some(mt) = mine_target + .filter(|mt| active_mine_tool.is_some() && nearest_scene_dist == Some(mt.distance)) { self.scene.set_select_pos(Some(mt.position_int())); Some(mt.position) diff --git a/voxygen/src/session/target.rs b/voxygen/src/session/target.rs index c7359a5524..c61f5920b2 100644 --- a/voxygen/src/session/target.rs +++ b/voxygen/src/session/target.rs @@ -103,7 +103,8 @@ pub(super) fn targets_under_cursor( }; let (collect_pos, _, collect_cam_ray) = find_pos(|b: Block| b.is_collectible()); - let (mine_pos, _, mine_cam_ray) = active_mine_tool.is_some() + let (mine_pos, _, mine_cam_ray) = active_mine_tool + .is_some() .then(|| find_pos(|b: Block| b.mine_tool().is_some())) .unwrap_or((None, None, None)); let (solid_pos, place_block_pos, solid_cam_ray) = find_pos(|b: Block| b.is_filled());