From 5ddc55be528bb55d74e361429831ee3dbcfdddd1 Mon Sep 17 00:00:00 2001 From: Thegaming Life Date: Mon, 6 Mar 2023 21:03:35 +0000 Subject: [PATCH] replaced CHUNK_SIZE by RECT_SIZE everywhere, and change the use (and remove... --- client/src/lib.rs | 9 +++++---- common/src/terrain/mod.rs | 11 +++++++++++ server/src/cmd.rs | 12 +++++------- server/src/sys/msg/terrain.rs | 5 +++-- server/src/sys/subscription.rs | 11 +++++------ voxygen/src/hud/map.rs | 14 +++++++++----- voxygen/src/hud/minimap.rs | 6 ++++-- world/examples/water.rs | 4 ++-- world/src/civ/mod.rs | 17 +++++++---------- world/src/column/mod.rs | 7 +++---- world/src/layer/cave.rs | 11 +++-------- world/src/sim/map.rs | 4 ++-- world/src/sim/mod.rs | 19 ++++++------------- 13 files changed, 65 insertions(+), 65 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 293c668399..34812b897b 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -42,8 +42,9 @@ use common::{ resources::{GameMode, PlayerEntity, TimeOfDay}, spiral::Spiral2d, terrain::{ - block::Block, map::MapConfig, neighbors, site::DungeonKindMeta, BiomeKind, SiteKindMeta, - SpriteKind, TerrainChunk, TerrainChunkSize, TerrainGrid, + block::Block, map::MapConfig, neighbors, site::DungeonKindMeta, BiomeKind, + CoordinateConversions, SiteKindMeta, SpriteKind, TerrainChunk, TerrainChunkSize, + TerrainGrid, }, trade::{PendingTrade, SitePrices, TradeAction, TradeId, TradeResult}, uid::{Uid, UidAllocator}, @@ -569,7 +570,7 @@ impl Client { ) }, |wpos| { - let pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as i32); + let pos = wpos.wpos_to_cpos(); rescale_height(if bounds_check(pos) { scale_height_big(alt[pos]) } else { @@ -597,7 +598,7 @@ impl Client { ) }, |wpos| { - let pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as i32); + let pos = wpos.wpos_to_cpos(); rescale_height(if bounds_check(pos) { scale_height_big(alt[pos]) } else { diff --git a/common/src/terrain/mod.rs b/common/src/terrain/mod.rs index 3db2b2c92b..7b75623635 100644 --- a/common/src/terrain/mod.rs +++ b/common/src/terrain/mod.rs @@ -82,17 +82,28 @@ pub trait CoordinateConversions { } impl CoordinateConversions for Vec2 { + #[inline] fn wpos_to_cpos(&self) -> Self { self.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e / sz as i32) } + #[inline] fn cpos_to_wpos(&self) -> Self { self.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e * sz as i32) } } impl CoordinateConversions for Vec2 { + #[inline] fn wpos_to_cpos(&self) -> Self { self.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e / sz as f32) } + #[inline] fn cpos_to_wpos(&self) -> Self { self.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e * sz as f32) } } +impl CoordinateConversions for Vec2 { + #[inline] + fn wpos_to_cpos(&self) -> Self { self.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e / sz as f64)} + #[inline] + fn cpos_to_wpos(&self) -> Self { self.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e * sz as f64)} +} + // TerrainChunkMeta #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/server/src/cmd.rs b/server/src/cmd.rs index 16e8efa3ea..829668b8d4 100644 --- a/server/src/cmd.rs +++ b/server/src/cmd.rs @@ -43,9 +43,9 @@ use common::{ outcome::Outcome, parse_cmd_args, resources::{BattleMode, PlayerPhysicsSettings, Time, TimeOfDay}, - terrain::{Block, BlockKind, SpriteKind, TerrainChunkSize}, + terrain::{Block, BlockKind, CoordinateConversions, SpriteKind, TerrainChunkSize}, uid::{Uid, UidAllocator}, - vol::{ReadVol, RectVolSize}, + vol::ReadVol, weather, Damage, DamageKind, DamageSource, Explosion, LoadoutBuilder, RadiusEffect, }; use common_net::{ @@ -2793,7 +2793,7 @@ fn handle_debug_column( let rockiness = sim.get_interpolated(wpos, |chunk| chunk.rockiness)?; let tree_density = sim.get_interpolated(wpos, |chunk| chunk.tree_density)?; let spawn_rate = sim.get_interpolated(wpos, |chunk| chunk.spawn_rate)?; - let chunk_pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / sz as i32); + let chunk_pos = wpos.wpos_to_cpos(); let chunk = sim.get(chunk_pos)?; let col = sampler.get((wpos, server.index.as_index_ref(), Some(calendar)))?; let gradient = sim.get_gradient_approx(chunk_pos)?; @@ -2873,7 +2873,7 @@ fn handle_debug_ways( pos.0.xy().map(|x| x as i32) }; let msg_generator = || { - let chunk_pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / sz as i32); + let chunk_pos = wpos.wpos_to_cpos(); let mut ret = String::new(); for delta in LOCALITY { let pos = chunk_pos + delta; @@ -3316,9 +3316,7 @@ fn handle_battlemode( // get chunk position let pos = position(server, target, "target")?; let wpos = pos.0.xy().map(|x| x as i32); - let chunk_pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |wpos, size: u32| { - wpos / size as i32 - }); + let chunk_pos = wpos.wpos_to_cpos(); server.world.civs().sites().any(|site| { // empirical const RADIUS: f32 = 9.0; diff --git a/server/src/sys/msg/terrain.rs b/server/src/sys/msg/terrain.rs index c8760d6d48..6eb01033a3 100644 --- a/server/src/sys/msg/terrain.rs +++ b/server/src/sys/msg/terrain.rs @@ -6,7 +6,7 @@ use common::{ comp::Pos, event::{EventBus, ServerEvent}, spiral::Spiral2d, - terrain::{TerrainChunkSize, TerrainGrid}, + terrain::{CoordinateConversions, TerrainChunkSize, TerrainGrid}, vol::RectVolSize, }; use common_ecs::{Job, Origin, ParMode, Phase, System}; @@ -128,7 +128,8 @@ impl<'a> System<'a> for Sys { let player_chunk = pos .0 .xy() - .map2(TerrainChunkSize::RECT_SIZE, |e, sz| e as i32 / sz as i32); + .as_::() + .wpos_to_cpos(); for rpos in Spiral2d::new().take((crate::MIN_VD as usize + 1).pow(2)) { let key = player_chunk + rpos; if terrain.get_key(key).is_none() { diff --git a/server/src/sys/subscription.rs b/server/src/sys/subscription.rs index 5c513a8612..1be6e66d28 100644 --- a/server/src/sys/subscription.rs +++ b/server/src/sys/subscription.rs @@ -6,7 +6,7 @@ use crate::{ use common::{ comp::{Ori, Pos, Vel}, region::{region_in_vd, regions_in_vd, Event as RegionEvent, RegionMap}, - terrain::TerrainChunkSize, + terrain::{CoordinateConversions, TerrainChunkSize}, uid::Uid, vol::RectVolSize, }; @@ -81,8 +81,7 @@ impl<'a> System<'a> for Sys { { let vd = presence.entity_view_distance.current(); // Calculate current chunk - let chunk = (Vec2::::from(pos.0)) - .map2(TerrainChunkSize::RECT_SIZE, |e, sz| e as i32 / sz as i32); + let chunk = (Vec2::::from(pos.0)).as_::().wpos_to_cpos(); // Only update regions when moving to a new chunk or if view distance has // changed. // @@ -104,8 +103,7 @@ impl<'a> System<'a> for Sys { // Update the view distance subscription.last_entity_view_distance = vd; // Update current chunk - subscription.fuzzy_chunk = Vec2::::from(pos.0) - .map2(TerrainChunkSize::RECT_SIZE, |e, sz| e as i32 / sz as i32); + subscription.fuzzy_chunk = Vec2::::from(pos.0).as_::().wpos_to_cpos(); // Use the largest side length as our chunk size let chunk_size = TerrainChunkSize::RECT_SIZE.reduce_max() as f32; // Iterate through currently subscribed regions @@ -218,7 +216,8 @@ pub fn initialize_region_subscription(world: &World, entity: specs::Entity) { world.write_storage::().get(entity), ) { let fuzzy_chunk = (Vec2::::from(client_pos.0)) - .map2(TerrainChunkSize::RECT_SIZE, |e, sz| e as i32 / sz as i32); + .as_::() + .wpos_to_cpos(); let chunk_size = TerrainChunkSize::RECT_SIZE.reduce_max() as f32; let regions = regions_in_vd( client_pos.0, diff --git a/voxygen/src/hud/map.rs b/voxygen/src/hud/map.rs index e9a33d01a6..427700de69 100644 --- a/voxygen/src/hud/map.rs +++ b/voxygen/src/hud/map.rs @@ -11,7 +11,13 @@ use crate::{ GlobalState, }; use client::{self, Client, SiteInfoRich}; -use common::{comp, comp::group::Role, terrain::TerrainChunkSize, trade::Good, vol::RectVolSize}; +use common::{ + comp, + comp::group::Role, + terrain::{CoordinateConversions, TerrainChunkSize}, + trade::Good, + vol::RectVolSize, +}; use common_net::msg::world_msg::{PoiKind, SiteId, SiteKind}; use conrod_core::{ color, @@ -383,9 +389,7 @@ impl<'a> Widget for Map<'a> { }, MarkerChange::ClickPos => { let tmp: Vec2 = Vec2::::from(click.xy) / zoom - drag; - let wpos = tmp - .map2(TerrainChunkSize::RECT_SIZE, |e, sz| e as f32 * sz as f32) - + player_pos; + let wpos = tmp.as_::().cpos_to_wpos() + player_pos; events.push(Event::SetLocationMarker(wpos.as_())); }, MarkerChange::Remove => events.push(Event::RemoveMarker), @@ -873,7 +877,7 @@ impl<'a> Widget for Map<'a> { // Site pos in world coordinates relative to the player let rwpos = wpos - player_pos; // Convert to chunk coordinates - let rcpos = rwpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e / sz as f32) + let rcpos = rwpos.wpos_to_cpos() // Add map dragging + drag.map(|e| e as f32); // Convert to relative pixel coordinates from the center of the map diff --git a/voxygen/src/hud/minimap.rs b/voxygen/src/hud/minimap.rs index b8d83c3048..8cfcae7930 100644 --- a/voxygen/src/hud/minimap.rs +++ b/voxygen/src/hud/minimap.rs @@ -15,7 +15,9 @@ use common::{ comp::group::Role, grid::Grid, slowjob::SlowJobPool, - terrain::{Block, BlockKind, TerrainChunk, TerrainChunkSize, TerrainGrid}, + terrain::{ + Block, BlockKind, CoordinateConversions, TerrainChunk, TerrainChunkSize, TerrainGrid, + }, vol::{ReadVol, RectVolSize}, }; use common_net::msg::world_msg::SiteKind; @@ -660,7 +662,7 @@ impl<'a> Widget for MiniMap<'a> { // Site pos in world coordinates relative to the player let rwpos = wpos - player_pos; // Convert to chunk coordinates - let rcpos = rwpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz| e / sz as f32); + let rcpos = rwpos.wpos_to_cpos(); // Convert to fractional coordinates relative to the worldsize let rfpos = rcpos / max_zoom as f32; // Convert to unrotated pixel coordinates from the player location on the map diff --git a/world/examples/water.rs b/world/examples/water.rs index 1a023d2ecb..3a27760688 100644 --- a/world/examples/water.rs +++ b/world/examples/water.rs @@ -2,7 +2,7 @@ use common::{ terrain::{ map::{MapConfig, MapDebug, MapSample}, - uniform_idx_as_vec2, vec2_as_uniform_idx, TerrainChunkSize, + uniform_idx_as_vec2, vec2_as_uniform_idx, CoordinateConversions, TerrainChunkSize, }, vol::RectVolSize, }; @@ -286,7 +286,7 @@ fn main() { let chunk_pos = (Vec2::::from(focus) + (Vec2::new(mx as f64, my as f64) * scale)) .map(|e| e as i32); - let block_pos = chunk_pos.map2(TerrainChunkSize::RECT_SIZE, |e, f| e * f as i32); + let block_pos = chunk_pos.cpos_to_wpos(); println!( "Block: ({}, {}), Chunk: ({}, {})", block_pos.x, block_pos.y, chunk_pos.x, chunk_pos.y diff --git a/world/src/civ/mod.rs b/world/src/civ/mod.rs index ad438bb541..3f7d6e7ea7 100644 --- a/world/src/civ/mod.rs +++ b/world/src/civ/mod.rs @@ -16,7 +16,8 @@ use common::{ spiral::Spiral2d, store::{Id, Store}, terrain::{ - uniform_idx_as_vec2, BiomeKind, MapSizeLg, TerrainChunkSize, TERRAIN_CHUNK_BLOCKS_LG, + uniform_idx_as_vec2, BiomeKind, CoordinateConversions, MapSizeLg, TerrainChunkSize, + TERRAIN_CHUNK_BLOCKS_LG, }, vol::RectVolSize, }; @@ -1389,8 +1390,7 @@ fn loc_suitable_for_site(sim: &WorldSim, loc: Vec2, site_kind: SiteKind) -> fn check_chunk_occupation(sim: &WorldSim, loc: Vec2, radius: i32) -> bool { for x in (-radius)..radius { for y in (-radius)..radius { - let check_loc = - loc + Vec2::new(x, y).map2(TerrainChunkSize::RECT_SIZE, |e, sz| e * sz as i32); + let check_loc = loc + Vec2::new(x, y).cpos_to_wpos(); if sim.get(check_loc).map_or(false, |c| !c.sites.is_empty()) { return false; } @@ -1424,10 +1424,9 @@ fn find_site_loc( } loc = ctx.sim.get(test_loc).and_then(|c| { - site_kind.is_suitable_loc(test_loc, ctx.sim).then_some( - c.downhill? - .map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / (sz as i32)), - ) + site_kind + .is_suitable_loc(test_loc, ctx.sim) + .then_some(c.downhill?.wpos_to_cpos()) }); } } @@ -1530,9 +1529,7 @@ impl SiteKind { let mut land_chunks = 0; for x in (-RESOURCE_RADIUS)..RESOURCE_RADIUS { for y in (-RESOURCE_RADIUS)..RESOURCE_RADIUS { - let check_loc = loc - + Vec2::new(x, y) - .map2(TerrainChunkSize::RECT_SIZE, |e, sz| e * sz as i32); + let check_loc = loc + Vec2::new(x, y).cpos_to_wpos(); sim.get(check_loc).map(|c| { if num::abs(chunk.alt - c.alt) < 200.0 { if c.river.is_river() { diff --git a/world/src/column/mod.rs b/world/src/column/mod.rs index 0d55f142c7..bf5fb29b32 100644 --- a/world/src/column/mod.rs +++ b/world/src/column/mod.rs @@ -9,7 +9,7 @@ use common::{ calendar::{Calendar, CalendarEvent}, terrain::{ quadratic_nearest_point, river_spline_coeffs, uniform_idx_as_vec2, vec2_as_uniform_idx, - TerrainChunkSize, + CoordinateConversions, TerrainChunkSize, }, vol::RectVolSize, }; @@ -68,7 +68,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> { fn get(&self, (wpos, index, calendar): Self::Index) -> Option> { let wposf = wpos.map(|e| e as f64); - let chunk_pos = wpos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / sz as i32); + let chunk_pos = wpos.wpos_to_cpos(); let sim = &self.sim; @@ -247,8 +247,7 @@ impl<'a> Sampler<'a> for ColumnGen<'a> { } let neighbor_pass_wpos = neighbor_pass_pos.map(|e| e as f64) + neighbor_coef * 0.5; - let neighbor_pass_pos = neighbor_pass_pos - .map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / sz as i32); + let neighbor_pass_pos = neighbor_pass_pos.wpos_to_cpos(); let coeffs = river_spline_coeffs( neighbor_wpos, spline_derivative, diff --git a/world/src/layer/cave.rs b/world/src/layer/cave.rs index 7aa63bc9a9..3388061553 100644 --- a/world/src/layer/cave.rs +++ b/world/src/layer/cave.rs @@ -7,10 +7,9 @@ use crate::{ use common::{ generation::EntityInfo, terrain::{ - quadratic_nearest_point, river_spline_coeffs, Block, BlockKind, SpriteKind, - TerrainChunkSize, + quadratic_nearest_point, river_spline_coeffs, Block, BlockKind, CoordinateConversions, + SpriteKind, }, - vol::RectVolSize, }; use noise::NoiseFn; use rand::prelude::*; @@ -70,11 +69,7 @@ fn node_at(cell: Vec2, level: u32, land: &Land) -> Option { } pub fn surface_entrances<'a>(land: &'a Land) -> impl Iterator> + 'a { - let sz_cells = to_cell( - land.size() - .map2(TerrainChunkSize::RECT_SIZE, |e, sz| (e * sz) as i32), - 0, - ); + let sz_cells = to_cell(land.size().as_::().cpos_to_wpos(), 0); (0..sz_cells.x + 1) .flat_map(move |x| (0..sz_cells.y + 1).map(move |y| Vec2::new(x, y))) .filter_map(|cell| Some(tunnel_below_from_cell(cell, 0, land)?.a.wpos)) diff --git a/world/src/sim/map.rs b/world/src/sim/map.rs index dbe7ff75de..0100ca2887 100644 --- a/world/src/sim/map.rs +++ b/world/src/sim/map.rs @@ -7,7 +7,7 @@ use crate::{ use common::{ terrain::{ map::{Connection, ConnectionKind, MapConfig, MapSample}, - vec2_as_uniform_idx, TerrainChunkSize, NEIGHBOR_DELTA, + vec2_as_uniform_idx, CoordinateConversions, TerrainChunkSize, NEIGHBOR_DELTA, }, vol::RectVolSize, }; @@ -218,7 +218,7 @@ pub fn sample_pos( RiverKind::Lake { .. } | RiverKind::Ocean => TerrainChunkSize::RECT_SIZE.x as f32, }); if let (Some(river_width), true) = (river_width, is_water) { - let downhill_pos = downhill_wpos.map2(TerrainChunkSize::RECT_SIZE, |e, f| e / f as i32); + let downhill_pos = downhill_wpos.wpos_to_cpos(); NEIGHBOR_DELTA .iter() .zip(connections.iter_mut()) diff --git a/world/src/sim/mod.rs b/world/src/sim/mod.rs index 92d1983eca..fa499f46ae 100644 --- a/world/src/sim/mod.rs +++ b/world/src/sim/mod.rs @@ -46,8 +46,8 @@ use common::{ spiral::Spiral2d, store::Id, terrain::{ - map::MapConfig, uniform_idx_as_vec2, vec2_as_uniform_idx, BiomeKind, MapSizeLg, - TerrainChunk, TerrainChunkSize, + map::MapConfig, uniform_idx_as_vec2, vec2_as_uniform_idx, BiomeKind, CoordinateConversions, + MapSizeLg, TerrainChunk, TerrainChunkSize, }, vol::RectVolSize, }; @@ -1907,8 +1907,7 @@ impl WorldSim { pub fn get_gradient_approx(&self, chunk_pos: Vec2) -> Option { let a = self.get(chunk_pos)?; if let Some(downhill) = a.downhill { - let b = - self.get(downhill.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| e / (sz as i32)))?; + let b = self.get(downhill.wpos_to_cpos())?; Some((a.alt - b.alt).abs() / TerrainChunkSize::RECT_SIZE.x as f32) } else { Some(0.0) @@ -1968,9 +1967,7 @@ impl WorldSim { T: Copy + Default + Add + Mul, F: FnMut(&SimChunk) -> T, { - let pos = pos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| { - e as f64 / sz as f64 - }); + let pos = pos.as_::().wpos_to_cpos(); let cubic = |a: T, b: T, c: T, d: T, x: f32| -> T { let x2 = x * x; @@ -2016,9 +2013,7 @@ impl WorldSim { // // Note that these are only guaranteed monotone in one dimension; fortunately, // that is sufficient for our purposes. - let pos = pos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| { - e as f64 / sz as f64 - }); + let pos = pos.as_::().wpos_to_cpos(); let secant = |b: T, c: T| c - b; @@ -2088,9 +2083,7 @@ impl WorldSim { // // Note that these are only guaranteed monotone in one dimension; fortunately, // that is sufficient for our purposes. - let pos = pos.map2(TerrainChunkSize::RECT_SIZE, |e, sz: u32| { - e as f64 / sz as f64 - }); + let pos = pos.as_::().wpos_to_cpos(); // Orient the chunk in the direction of the most downhill point of the four. If // there is no "most downhill" point, then we don't care.