From 1de1f341835e96d56ab4344d5c686b655eab8cfa Mon Sep 17 00:00:00 2001 From: Jovan Gerodetti Date: Mon, 17 Jun 2024 14:23:42 +0200 Subject: [PATCH] Remove unnecessary Arcs --- src/open_city_2k/city.rs | 173 +++++++++++++++++++-------------------- src/open_city_2k/tile.rs | 159 ++++++++++------------------------- 2 files changed, 129 insertions(+), 203 deletions(-) diff --git a/src/open_city_2k/city.rs b/src/open_city_2k/city.rs index 43b875a..9a2c53b 100644 --- a/src/open_city_2k/city.rs +++ b/src/open_city_2k/city.rs @@ -8,6 +8,8 @@ use log::{debug, error, info, warn}; use phf::phf_map; use serde::Serialize; +use crate::open_city_2k::sc_util::int_to_bitstring; + use super::bit_flags::BitFlags; use super::budget::Budget; use super::building::Building; @@ -249,14 +251,14 @@ pub struct City { population_graphs: HashMap>, industry_graphs: HashMap>, simulator_settings: HashMap, - traffic: Arc, - pollution: Arc, - value: Arc, - crime: Arc, - police: Arc, - fire: Arc, - density: Arc, - growth: Arc, + pub(crate) traffic: Minimap, + pub(crate) pollution: Minimap, + pub(crate) value: Minimap, + pub(crate) crime: Minimap, + pub(crate) police: Minimap, + pub(crate) fire: Minimap, + pub(crate) density: Minimap, + pub(crate) growth: Minimap, is_scenario: bool, scenario_text: String, scenario_descriptive_text: String, @@ -293,14 +295,14 @@ impl City { game_settings: HashMap::new(), // Minimaps - traffic: Arc::new(Minimap::new(String::from("traffic"), 64)), - pollution: Arc::new(Minimap::new(String::from("pollution"), 64)), - value: Arc::new(Minimap::new(String::from("value"), 64)), - crime: Arc::new(Minimap::new(String::from("crime"), 64)), - police: Arc::new(Minimap::new(String::from("police"), 32)), - fire: Arc::new(Minimap::new(String::from("fire"), 32)), - density: Arc::new(Minimap::new(String::from("density"), 32)), - growth: Arc::new(Minimap::new(String::from("growth"), 32)), + traffic: Minimap::new(String::from("traffic"), 64), + pollution: Minimap::new(String::from("pollution"), 64), + value: Minimap::new(String::from("value"), 64), + crime: Minimap::new(String::from("crime"), 64), + police: Minimap::new(String::from("police"), 32), + fire: Minimap::new(String::from("fire"), 32), + density: Minimap::new(String::from("density"), 32), + growth: Minimap::new(String::from("growth"), 32), // Optional Scenario stuff is_scenario: false, @@ -342,37 +344,11 @@ impl City { .try_into() .expect("should be 1 byte"); - self.traffic = { - let mut traffic = (*self.traffic).clone(); - - traffic.set_item(tile_key, sc_util::parse_uint8(xtrf)); - - Arc::new(traffic) - }; - - self.pollution = { - let mut pollution = (*self.traffic).clone(); - - pollution.set_item(tile_key, sc_util::parse_uint8(xplt)); - - Arc::new(pollution) - }; - - self.value = { - let mut value = (*self.value).clone(); - - value.set_item(tile_key, sc_util::parse_uint8(xval)); - - Arc::new(value) - }; - - self.crime = { - let mut crime = (*self.crime).clone(); - - crime.set_item(tile_key, sc_util::parse_uint8(xcrm)); - - Arc::new(crime) - }; + self.traffic.set_item(tile_key, sc_util::parse_uint8(xtrf)); + self.pollution + .set_item(tile_key, sc_util::parse_uint8(xplt)); + self.value.set_item(tile_key, sc_util::parse_uint8(xval)); + self.crime.set_item(tile_key, sc_util::parse_uint8(xcrm)); debug!( "{:?}: traffic: {}, pollution: {}, land value: {}, crime: {}", @@ -409,37 +385,10 @@ impl City { .try_into() .expect("should be 1 byte"); - self.police = { - let mut police = (*self.police).clone(); - - police.set_item(tile_key, sc_util::parse_uint8(xplc)); - - Arc::new(police) - }; - - self.fire = { - let mut fire = (*self.fire).clone(); - - fire.set_item(tile_key, sc_util::parse_uint8(xfir)); - - Arc::new(fire) - }; - - self.density = { - let mut density = (*self.density).clone(); - - density.set_item(tile_key, sc_util::parse_uint8(xpop)); - - Arc::new(density) - }; - - self.growth = { - let mut growth = (*self.growth).clone(); - - growth.set_item(tile_key, sc_util::parse_uint8(xrog)); - - Arc::new(growth) - }; + self.police.set_item(tile_key, sc_util::parse_uint8(xplc)); + self.fire.set_item(tile_key, sc_util::parse_uint8(xfir)); + self.density.set_item(tile_key, sc_util::parse_uint8(xpop)); + self.growth.set_item(tile_key, sc_util::parse_uint8(xrog)); debug!( "{:?}: police: {}, fire: {}, densitye: {}, growth: {}", @@ -463,17 +412,7 @@ impl City { for row in 0..self.city_size { for col in 0..self.city_size { - let mut tile = Tile::new( - self.traffic.clone(), - self.pollution.clone(), - self.value.clone(), - self.crime.clone(), - self.police.clone(), - self.fire.clone(), - self.density.clone(), - self.growth.clone(), - self.labels.clone(), - ); + let mut tile = Tile::new(self.labels.clone()); let tile_idx = row * self.city_size + col; let tile_coords = (row, col); @@ -542,6 +481,12 @@ impl City { // Add the new tile to the tilelist self.tilelist.insert((row, col), tile); + + debug!( + "Tile: {}", + self.describe_tile((row, col)) + .expect("tile was just inserted") + ); } } } @@ -1226,4 +1171,54 @@ impl City { output } + + fn describe_tile(&self, tile_coords: (usize, usize)) -> Option { + let tile = self.tilelist.get(&tile_coords)?; + + let terr = int_to_bitstring(*tile.terrain() as u32, 0); + let b_id = match &tile.building() { + Some(building) => format!("{:#04x}", building.building_id), + None => String::from("null"), + }; + + let sign_text = tile + .text() + .map(|text| format!(", Sign: {:?}", text)) + .unwrap_or_default(); + + Some(format!( + r#"Tile at {:?} +Altitude: + tunnel: {}, water: {}, unknown: {}, altitude: {} +Terrain: {} +Buildings: + id: {}, corners {}, zone: {}, underground: {} +Text pointer: {}{} +Flags: {:?} +Minimap: + Traffic: {:?}, pollution: {:?}, value: {:?}, crime: {:?}, police: {:?}, fire: {:?}, density: {:?}, growth: {:?} +"#, + tile.coordinates(), + tile.altitude_tunnel(), + tile.is_water(), + tile.altitude_unknown(), + tile.altitude(), + terr, + b_id, + tile.zone_corners(), + tile.zone(), + tile.underground(), + tile.text_pointer(), + sign_text, + tile.bit_flags(), + tile.traffic(self), + tile.pollution(self), + tile.value(self), + tile.crime(self), + tile.police(self), + tile.fire(self), + tile.density(self), + tile.growth(self) + )) + } } diff --git a/src/open_city_2k/tile.rs b/src/open_city_2k/tile.rs index 91cf5df..511a9d8 100644 --- a/src/open_city_2k/tile.rs +++ b/src/open_city_2k/tile.rs @@ -2,10 +2,9 @@ use super::bit_flags::BitFlags; use super::building::Building; -use super::minimap::Minimap; -use super::sc_util::int_to_bitstring; +use super::City; use serde::Serialize; -use std::fmt::Display; +use std::ops::Deref; use std::sync::Arc; #[derive(Debug, Serialize)] @@ -20,40 +19,14 @@ pub struct Tile { zone_corners: String, zone: u32, underground: u8, - _label: Vec, + label: Vec, text_pointer: i32, bit_flags: Option, - #[serde(skip_serializing)] - _traffic_minimap: Arc, - #[serde(skip_serializing)] - _pollution_minimap: Arc, - #[serde(skip_serializing)] - _value_minimap: Arc, - #[serde(skip_serializing)] - _crime_minimap: Arc, - #[serde(skip_serializing)] - _police_minimap: Arc, - #[serde(skip_serializing)] - _fire_minimap: Arc, - #[serde(skip_serializing)] - _density_minimap: Arc, - #[serde(skip_serializing)] - _growth_minimap: Arc, } impl Tile { #[allow(clippy::too_many_arguments)] - pub fn new( - traffic: Arc, - pollution: Arc, - value: Arc, - crime: Arc, - police: Arc, - fire: Arc, - density: Arc, - growth: Arc, - label: Vec, - ) -> Self { + pub fn new(label: Vec) -> Self { let coordinates = (0, 0); // Altitude map related values. let altitude_tunnel = 0; @@ -67,7 +40,6 @@ impl Tile { let zone_corners = String::from(""); let zone = 0; let underground = 0; - let _label = label; // text/signs let text_pointer = -1; // bit flags @@ -85,36 +57,48 @@ impl Tile { zone_corners, zone, underground, - _label, + label, text_pointer, bit_flags, - _traffic_minimap: traffic, - _pollution_minimap: pollution, - _value_minimap: value, - _crime_minimap: crime, - _police_minimap: police, - _fire_minimap: fire, - _density_minimap: density, - _growth_minimap: growth, } } + pub fn coordinates(&self) -> (usize, usize) { + self.coordinates + } + pub fn set_coordinates(&mut self, value: (usize, usize)) { self.coordinates = value; } + pub fn altitude(&self) -> u32 { + self.altitude + } + pub fn set_altitude(&mut self, value: u32) { self.altitude = value; } + pub fn altitude_unknown(&self) -> u32 { + self.altitude_unknown + } + pub fn set_altitude_unknown(&mut self, value: u32) { self.altitude_unknown = value; } + pub fn altitude_tunnel(&self) -> u32 { + self.altitude_tunnel + } + pub fn set_altitude_tunnel(&mut self, value: u32) { self.altitude_tunnel = value; } + pub fn is_water(&self) -> bool { + self.is_water + } + pub fn set_is_water(&mut self, value: bool) { self.is_water = value; } @@ -151,8 +135,8 @@ impl Tile { self.underground = value; } - pub fn text_pointer(&self) -> &i32 { - &self.text_pointer + pub fn text_pointer(&self) -> i32 { + self.text_pointer } pub fn set_text_pointer(&mut self, value: u8) { @@ -167,40 +151,40 @@ impl Tile { &self.bit_flags } - fn get_traffic(&self) -> &u8 { - self._traffic_minimap.get_scaled(self.coordinates) + pub fn traffic(&self, city: &City) -> u8 { + *city.traffic.get_scaled(self.coordinates) } - fn get_pollution(&self) -> &u8 { - self._pollution_minimap.get_scaled(self.coordinates) + pub fn pollution(&self, city: &City) -> u8 { + *city.pollution.get_scaled(self.coordinates) } - fn get_value(&self) -> &u8 { - self._value_minimap.get_scaled(self.coordinates) + pub fn value(&self, city: &City) -> u8 { + *city.value.get_scaled(self.coordinates) } - fn get_crime(&self) -> &u8 { - self._crime_minimap.get_scaled(self.coordinates) + pub fn crime(&self, city: &City) -> u8 { + *city.crime.get_scaled(self.coordinates) } - fn get_police(&self) -> &u8 { - self._police_minimap.get_scaled(self.coordinates) + pub fn police(&self, city: &City) -> u8 { + *city.police.get_scaled(self.coordinates) } - fn get_fire(&self) -> &u8 { - self._fire_minimap.get_scaled(self.coordinates) + pub fn fire(&self, city: &City) -> u8 { + *city.fire.get_scaled(self.coordinates) } - fn get_density(&self) -> &u8 { - self._density_minimap.get_scaled(self.coordinates) + pub fn density(&self, city: &City) -> u8 { + *city.density.get_scaled(self.coordinates) } - fn get_growth(&self) -> &u8 { - self._growth_minimap.get_scaled(self.coordinates) + pub fn growth(&self, city: &City) -> u8 { + *city.growth.get_scaled(self.coordinates) } - fn get_text(&self) -> &str { - &self._label[self.text_pointer as usize] + pub fn text(&self) -> Option<&str> { + self.label.get(self.text_pointer as usize).map(Deref::deref) } #[allow(dead_code)] @@ -212,56 +196,3 @@ impl Tile { self.building = Some(value); } } - -impl Display for Tile { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let terr = int_to_bitstring(self.terrain as u32, 0); - let b_id = if let Some(building) = &self.building { - format!("{:#04x}", building.building_id) - } else { - String::from("null") - }; - - let sign_text = if self.text_pointer > -1 { - format!(", Sign: {:?}", self.get_text()) - } else { - String::from("") - }; - - write!( - f, - r#"Tile at {:?} -Altitude: - tunnel: {}, water: {}, unknown: {}, altitude: {} -Terrain: {} -Buildings: - id: {}, corners {}, zone: {}, underground: {} -Text pointer: {}{} -Flags: {:?} -Minimap: - Traffic: {:?}, pollution: {:?}, value: {:?}, crime: {:?}, police: {:?}, fire: {:?}, density: {:?}, growth: {:?} -"#, - self.coordinates, - self.altitude_tunnel, - self.is_water, - self.altitude_unknown, - self.altitude, - terr, - b_id, - self.zone_corners, - self.zone, - self.underground, - self.text_pointer, - sign_text, - self.bit_flags, - self.get_traffic(), - self.get_pollution(), - self.get_value(), - self.get_crime(), - self.get_police(), - self.get_fire(), - self.get_density(), - self.get_growth() - ) - } -}