diff --git a/src/gas.rs b/src/gas.rs index ae7c3a5..651820c 100644 --- a/src/gas.rs +++ b/src/gas.rs @@ -193,8 +193,11 @@ impl GasArena { let next_idx = gas_mixtures.len(); gas_mixtures.push(RwLock::new(Mixture::from_vol(init_volume))); - mix.write_var("_extools_pointer_gasmixture", &(next_idx as f32).into()) - .unwrap(); + mix.write_var_id( + byond_string!("_extools_pointer_gasmixture"), + &f32::from_bits(next_idx as u32).into(), + ) + .unwrap(); let mut ids_lock = NEXT_GAS_IDS.write(); let cur_last = gas_mixtures.len(); @@ -222,8 +225,11 @@ impl GasArena { .unwrap() .write() .clear_with_vol(init_volume); - mix.write_var("_extools_pointer_gasmixture", &(idx as f32).into()) - .unwrap(); + mix.write_var_id( + byond_string!("_extools_pointer_gasmixture"), + &f32::from_bits(idx as u32).into(), + ) + .unwrap(); } Ok(ByondValue::null()) } @@ -249,7 +255,8 @@ where F: FnOnce(&Mixture) -> Result, { GasArena::with_gas_mixture( - mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, + mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, f, ) } @@ -262,7 +269,8 @@ where F: FnOnce(&mut Mixture) -> Result, { GasArena::with_gas_mixture_mut( - mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, + mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, f, ) } @@ -275,8 +283,12 @@ where F: FnOnce(&Mixture, &Mixture) -> Result, { GasArena::with_gas_mixtures( - src_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, - arg_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, + src_mix + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, + arg_mix + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, f, ) } @@ -289,8 +301,12 @@ where F: FnOnce(&mut Mixture, &mut Mixture) -> Result, { GasArena::with_gas_mixtures_mut( - src_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, - arg_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, + src_mix + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, + arg_mix + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, f, ) } @@ -303,8 +319,12 @@ where F: FnMut(&RwLock, &RwLock) -> Result, { GasArena::with_gas_mixtures_custom( - src_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, - arg_mix.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize, + src_mix + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, + arg_mix + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize, f, ) } diff --git a/src/gas/types.rs b/src/gas/types.rs index 48d1c49..d3190f6 100644 --- a/src/gas/types.rs +++ b/src/gas/types.rs @@ -235,11 +235,11 @@ pub fn destroy_gas_info_structs() { GAS_INFO_BY_IDX.write().as_mut().unwrap().clear(); GAS_SPECIFIC_HEATS.write().as_mut().unwrap().clear(); TOTAL_NUM_GASES.store(0, Ordering::Release); - CACHED_GAS_IDS.with(|gas_ids| { - gas_ids.borrow_mut().clear(); + CACHED_GAS_IDS.with_borrow_mut(|gas_ids| { + gas_ids.clear(); }); - CACHED_IDX_TO_STRINGS.with(|gas_ids| { - gas_ids.borrow_mut().clear(); + CACHED_IDX_TO_STRINGS.with_borrow_mut(|gas_ids| { + gas_ids.clear(); }); } @@ -274,10 +274,8 @@ fn hook_register_gas(gas: ByondValue) { .unwrap() .push(gas_cache.specific_heat); GAS_INFO_BY_IDX.write().as_mut().unwrap().push(gas_cache); - CACHED_IDX_TO_STRINGS.with(|gas_ids| { - let mut map = gas_ids.borrow_mut(); - map.insert(cached_idx, cached_id.into_boxed_str()) - }); + CACHED_IDX_TO_STRINGS + .with_borrow_mut(|map| map.insert(cached_idx, cached_id.into_boxed_str())); TOTAL_NUM_GASES.fetch_add(1, Ordering::Release); // this is the only thing that stores it other than shutdown } } @@ -456,8 +454,7 @@ pub fn gas_idx_from_string(id: &str) -> Result { /// # Errors /// If the given string is not a string or is not a valid gas ID. pub fn gas_idx_from_value(string_val: &ByondValue) -> Result { - CACHED_GAS_IDS.with(|c| { - let mut cache = c.borrow_mut(); + CACHED_GAS_IDS.with_borrow_mut(|cache| { if let Some(idx) = cache.get(&string_val.get_strid().unwrap()) { Ok(*idx) } else { @@ -473,8 +470,7 @@ pub fn gas_idx_from_value(string_val: &ByondValue) -> Result { /// # Panics /// If an invalid gas index is given to this. This should never happen, so we panic instead of runtiming. pub fn gas_idx_to_id(idx: GasIDX) -> ByondValue { - CACHED_IDX_TO_STRINGS.with(|thin| { - let stuff = thin.borrow(); + CACHED_IDX_TO_STRINGS.with_borrow(|stuff| { ByondValue::new_str( stuff .get(&idx) @@ -513,10 +509,7 @@ pub fn register_gas_manually(gas_id: &'static str, specific_heat: f32) { .unwrap() .push(gas_cache.specific_heat); GAS_INFO_BY_IDX.write().as_mut().unwrap().push(gas_cache); - CACHED_IDX_TO_STRINGS.with(|gas_ids| { - let mut map = gas_ids.borrow_mut(); - map.insert(cached_idx, gas_id.into()) - }); + CACHED_IDX_TO_STRINGS.with_borrow_mut(|map| map.insert(cached_idx, gas_id.into())); TOTAL_NUM_GASES.fetch_add(1, Ordering::Release); // this is the only thing that stores it other than shutdown } diff --git a/src/lib.rs b/src/lib.rs index 7cf86a5..c91c5bd 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -435,7 +435,7 @@ fn react_hook(src: ByondValue, holder: ByondValue) { let reactions = with_mix(&src, |mix| Ok(mix.all_reactable()))?; for reaction in reactions { ret |= ReactionReturn::from_bits_truncate( - react_by_id(reaction, src.clone(), holder.clone())? + react_by_id(reaction, src, holder)? .get_number() .unwrap_or_default() as u32, ); @@ -570,28 +570,30 @@ fn equalize_all_hook(gas_list: ByondValue) { value .read_number_id(byond_string!("_extools_pointer_gasmixture")) .ok() - .map(|f| f as usize) + .map(|f| f.to_bits() as usize) }) .collect::>(); GasArena::with_all_mixtures(move |all_mixtures| { let mut tot = gas::Mixture::new(); let mut tot_vol: f64 = 0.0; - for &id in &gas_list { - if let Some(src_gas_lock) = all_mixtures.get(id) { + gas_list + .iter() + .filter_map(|&id| all_mixtures.get(id)) + .for_each(|src_gas_lock| { let src_gas = src_gas_lock.read(); tot.merge(&src_gas); tot_vol += f64::from(src_gas.volume); - } - } + }); if tot_vol > 0.0 { - for &id in &gas_list { - if let Some(dest_gas_lock) = all_mixtures.get(id) { + gas_list + .iter() + .filter_map(|&id| all_mixtures.get(id)) + .for_each(|dest_gas_lock| { let dest_gas = &mut dest_gas_lock.write(); let vol = dest_gas.volume; // don't wanna borrow it in the below dest_gas.copy_from_mutable(&tot); dest_gas.multiply((f64::from(vol) / tot_vol) as f32); - } - } + }); } }); Ok(ByondValue::null()) diff --git a/src/reaction.rs b/src/reaction.rs index 5d296f4..af68282 100644 --- a/src/reaction.rs +++ b/src/reaction.rs @@ -40,8 +40,8 @@ thread_local! { pub fn clean_up_reaction_values() { crate::turfs::wait_for_tasks(); - REACTION_VALUES.with(|reaction_values| { - reaction_values.borrow_mut().clear(); + REACTION_VALUES.with_borrow_mut(|reaction_values| { + reaction_values.clear(); }); } @@ -53,8 +53,8 @@ pub fn react_by_id( src: ByondValue, holder: ByondValue, ) -> Result { - REACTION_VALUES.with(|r| { - r.borrow().get(&id).map_or_else( + REACTION_VALUES.with_borrow(|r| { + r.get(&id).map_or_else( || Err(eyre::eyre!("Reaction with invalid id")), |reaction| match reaction { ReactionSide::ByondSide(val) => val @@ -134,15 +134,12 @@ impl Reaction { } }?; - REACTION_VALUES.with(|r| -> Result<()> { - let mut reaction_map = r.borrow_mut(); + REACTION_VALUES.with_borrow_mut(|reaction_map| -> Result<()> { match func { Some(function) => { reaction_map.insert(our_reaction.id, ReactionSide::RustSide(function)) } - None => { - reaction_map.insert(our_reaction.id, ReactionSide::ByondSide(reaction.clone())) - } + None => reaction_map.insert(our_reaction.id, ReactionSide::ByondSide(reaction)), }; Ok(()) })?; diff --git a/src/reaction/hooks.rs b/src/reaction/hooks.rs index 6813413..85c997d 100644 --- a/src/reaction/hooks.rs +++ b/src/reaction/hooks.rs @@ -90,14 +90,14 @@ fn plasma_fire(byond_air: ByondValue, holder: ByondValue) -> Result let mut cached_results = byond_air.read_var_id(byond_string!("reaction_results"))?; cached_results.write_list_index("fire", fire_amount)?; if temperature > FIRE_MINIMUM_TEMPERATURE_TO_EXIST { - byondapi::global_call::call_global( - "fire_expose", + byondapi::global_call::call_global_id( + byond_string!("fire_expose"), &[holder, byond_air, temperature.into()], )?; } - Ok(1.0.into()) + Ok(true.into()) } else { - Ok(0.0.into()) + Ok(false.into()) } } @@ -141,18 +141,18 @@ fn tritium_fire(byond_air: ByondValue, holder: ByondValue) -> Result Ok((burned_fuel, energy_released, new_temp)) })?; if burned_fuel > TRITIUM_MINIMUM_RADIATION_FACTOR { - byondapi::global_call::call_global( - "radiation_burn", - &[holder.clone(), energy_released.into()], + byondapi::global_call::call_global_id( + byond_string!("radiation_burn"), + &[holder, energy_released.into()], )?; } if temperature > FIRE_MINIMUM_TEMPERATURE_TO_EXIST { - byondapi::global_call::call_global( - "fire_expose", + byondapi::global_call::call_global_id( + byond_string!("fire_expose"), &[holder, byond_air, temperature.into()], )?; } - Ok(1.0.into()) + Ok(true.into()) } #[cfg(feature = "fusion_hook")] @@ -288,15 +288,15 @@ fn fusion(byond_air: ByondValue, holder: ByondValue) -> Result { Ok(standard_energy) })?; if reaction_energy != 0.0 { - byondapi::global_call::call_global( - "fusion_ball", + byondapi::global_call::call_global_id( + byond_string!("fusion_ball"), &[holder, reaction_energy.into(), standard_energy.into()], )?; - Ok(1.0.into()) + Ok(true.into()) } else if reaction_energy == 0.0 && instability <= FUSION_INSTABILITY_ENDOTHERMALITY { - Ok(1.0.into()) + Ok(true.into()) } else { - Ok(0.0.into()) + Ok(false.into()) } } @@ -396,27 +396,20 @@ fn generic_fire(byond_air: ByondValue, holder: ByondValue) -> Result let mut cached_results = byond_air.read_var_id(byond_string!("reaction_results"))?; cached_results.write_list_index("fire", fire_amount)?; if temperature > FIRE_MINIMUM_TEMPERATURE_TO_EXIST { - byondapi::global_call::call_global( - "fire_expose", - &[holder.clone(), byond_air, temperature.into()], + byondapi::global_call::call_global_id( + byond_string!("fire_expose"), + &[holder, byond_air, temperature.into()], )?; } if radiation_released > 0.0 { - byondapi::global_call::call_global( - "radiation_burn", - &[holder.clone(), radiation_released.into()], + byondapi::global_call::call_global_id( + byond_string!("radiation_burn"), + &[holder, radiation_released.into()], )?; } - Ok({ - if fire_amount > 0.0 { - 1.0 - } else { - 0.0 - } - } - .into()) + Ok((fire_amount > 0.0).into()) } else { - Ok(0.0.into()) + Ok(false.into()) } }) } diff --git a/src/turfs.rs b/src/turfs.rs index 5386709..49c64e6 100644 --- a/src/turfs.rs +++ b/src/turfs.rs @@ -258,8 +258,6 @@ impl TurfGases { Ok(()) } - //This isn't a useless collect(), we can't hold a mutable ref and an immutable ref at once on the graph - #[allow(clippy::needless_collect)] pub fn remove_adjacencies(&mut self, index: NodeIndex) { let edges = self .graph @@ -443,7 +441,9 @@ fn hook_register_turf(src: ByondValue, flag: ByondValue) { if flag >= 0 { let mut to_insert: TurfMixture = TurfMixture::default(); let air = src.read_var_id(byond_string!("air"))?; - to_insert.mix = air.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize; + to_insert.mix = air + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize; to_insert.flags = SimulationFlags::from_bits_truncate(flag as u8); to_insert.id = id; @@ -542,7 +542,9 @@ fn update_visuals(src: ByondValue) -> Result { let mut overlay_types = Vec::new(); let gas_overlays = byondapi::global_call::call_global_id(byond_string!("get_overlays"), &[])?; - let ptr = air.read_number_id(byond_string!("_extools_pointer_gasmixture"))? as usize; + let ptr = air + .read_number_id(byond_string!("_extools_pointer_gasmixture"))? + .to_bits() as usize; GasArena::with_gas_mixture(ptr, |mix| { mix.for_each_gas(|idx, moles| { if let Some(amt) = gas::types::gas_visibility(idx) { diff --git a/src/turfs/groups.rs b/src/turfs/groups.rs index d9b00ec..8b5a72a 100644 --- a/src/turfs/groups.rs +++ b/src/turfs/groups.rs @@ -50,11 +50,14 @@ fn groups_hook(mut src: ByondValue, remaining: ByondValue) { std::column!() ) })?; - src.write_var( - "cost_groups", + src.write_var_id( + byond_string!("cost_groups"), &(0.8 * prev_cost + 0.2 * (bench as f32)).into(), )?; - src.write_var("num_group_turfs_processed", &(num_eq as f32).into())?; + src.write_var_id( + byond_string!("num_group_turfs_processed"), + &(num_eq as f32).into(), + )?; Ok(is_cancelled.into()) } diff --git a/src/turfs/katmos.rs b/src/turfs/katmos.rs index 704e635..6ea413c 100644 --- a/src/turfs/katmos.rs +++ b/src/turfs/katmos.rs @@ -229,12 +229,8 @@ fn give_to_takers( } for cur_index in queue.drain(..).rev() { - let mut turf_info = { - let opt = info.get(&cur_index); - if opt.is_none() { - continue; - } - *opt.unwrap() + let Some(&(mut turf_info)) = info.get(&cur_index) else { + continue; }; if turf_info.curr_transfer_amount != 0.0 && turf_info.curr_transfer_dir.is_some() { if let Some(adj_info) = info.get_mut(&turf_info.curr_transfer_dir.unwrap()) { @@ -302,12 +298,8 @@ fn take_from_givers( queue_idx += 1; } for cur_index in queue.drain(..).rev() { - let mut turf_info = { - let opt = info.get(&cur_index); - if opt.is_none() { - continue; - } - *opt.unwrap() + let Some(&(mut turf_info)) = info.get(&cur_index) else { + continue; }; if turf_info.curr_transfer_amount != 0.0 && turf_info.curr_transfer_dir.is_some() { if let Some(adj_info) = info.get_mut(&turf_info.curr_transfer_dir.unwrap()) { @@ -328,11 +320,9 @@ fn take_from_givers( } fn explosively_depressurize(initial_index: TurfID, equalize_hard_turf_limit: usize) -> Result<()> { - let initial_index = with_turf_gases_read(|arena| arena.get_id(initial_index)); - if initial_index.is_none() { + let Some(initial_index) = with_turf_gases_read(|arena| arena.get_id(initial_index)) else { return Ok(()); - } - let initial_index = initial_index.unwrap(); + }; //1st floodfill let (space_turfs, warned_about_planet_atmos) = { @@ -346,12 +336,8 @@ fn explosively_depressurize(initial_index: TurfID, equalize_hard_turf_limit: usi cur_queue_idx += 1; let mut firelock_considerations = vec![]; with_turf_gases_read(|arena| -> Result<()> { - let cur_mixture = { - let maybe = arena.get(cur_index); - if maybe.is_none() { - return Ok(()); - } - maybe.unwrap() + let Some(cur_mixture) = arena.get(cur_index) else { + return Ok(()); }; if cur_mixture.planetary_atmos.is_some() { warned_about_planet_atmos = true; @@ -359,8 +345,8 @@ fn explosively_depressurize(initial_index: TurfID, equalize_hard_turf_limit: usi } if cur_mixture.is_immutable() { if space_turfs.insert(cur_index) { - ByondValue::new_ref(TURF_TYPE, cur_mixture.id).write_var( - "pressure_specific_target", + ByondValue::new_ref(TURF_TYPE, cur_mixture.id).write_var_id( + byond_string!("pressure_specific_target"), &ByondValue::new_ref(TURF_TYPE, cur_mixture.id), )?; } @@ -435,8 +421,10 @@ fn explosively_depressurize(initial_index: TurfID, equalize_hard_turf_limit: usi let cur_target_turf = ByondValue::new_ref(TURF_TYPE, cur_mixture.id) .read_var_id(byond_string!("pressure_specific_target"))?; - ByondValue::new_ref(TURF_TYPE, adj_mixture.id) - .write_var("pressure_specific_target", &cur_target_turf)?; + ByondValue::new_ref(TURF_TYPE, adj_mixture.id).write_var_id( + byond_string!("pressure_specific_target"), + &cur_target_turf, + )?; adj_orig.set(adj_info); } } @@ -483,24 +471,28 @@ fn explosively_depressurize(initial_index: TurfID, equalize_hard_turf_limit: usi let mut byond_turf_adj = ByondValue::new_ref(TURF_TYPE, cur_mixture.id); - byond_turf - .write_var("pressure_difference", &cur_info.curr_transfer_amount.into())?; - byond_turf.write_var( - "pressure_direction", - &byondapi::global_call::call_global( - "get_dir_multiz", - &[byond_turf.clone(), byond_turf_adj.clone()], + byond_turf.write_var_id( + byond_string!("pressure_difference"), + &cur_info.curr_transfer_amount.into(), + )?; + byond_turf.write_var_id( + byond_string!("pressure_direction"), + &byondapi::global_call::call_global_id( + byond_string!("get_dir"), + &[byond_turf, byond_turf_adj], )?, )?; if adj_info.curr_transfer_dir.is_none() { - byond_turf_adj - .write_var("pressure_difference", &adj_info.curr_transfer_amount.into())?; - byond_turf_adj.write_var( - "pressure_direction", - &byondapi::global_call::call_global( - "get_dir_multiz", - &[byond_turf.clone(), byond_turf_adj.clone()], + byond_turf_adj.write_var_id( + byond_string!("pressure_difference"), + &adj_info.curr_transfer_amount.into(), + )?; + byond_turf_adj.write_var_id( + byond_string!("pressure_direction"), + &byondapi::global_call::call_global_id( + byond_string!("get_dir"), + &[byond_turf, byond_turf_adj], )?, )?; } @@ -516,11 +508,8 @@ fn explosively_depressurize(initial_index: TurfID, equalize_hard_turf_limit: usi Ok(()) } -// Clippy go away, this type is only used once -#[allow(clippy::type_complexity)] fn flood_fill_zones( - index_node: NodeIndex, - index_turf: TurfID, + (index_node, index_turf): (NodeIndex, TurfID), equalize_hard_turf_limit: usize, found_turfs: &mut HashSet, arena: &TurfGases, @@ -580,11 +569,9 @@ fn flood_fill_zones( } fn planet_equalize(initial_index: TurfID, equalize_hard_turf_limit: usize) -> Result<()> { - let initial_index = with_turf_gases_read(|arena| arena.get_id(initial_index)); - if initial_index.is_none() { + let Some(initial_index) = with_turf_gases_read(|arena| arena.get_id(initial_index)) else { return Ok(()); - } - let initial_index = initial_index.unwrap(); + }; let mut cur_queue_idx = 0; let mut warned_about_space = false; @@ -596,12 +583,8 @@ fn planet_equalize(initial_index: TurfID, equalize_hard_turf_limit: usize) -> Re cur_queue_idx += 1; let mut firelock_considerations = vec![]; with_turf_gases_read(|arena| -> Result<()> { - let cur_mixture = { - let maybe = arena.get(cur_index); - if maybe.is_none() { - return Ok(()); - } - maybe.unwrap() + let Some(cur_mixture) = arena.get(cur_index) else { + return Ok(()); }; if cur_mixture.planetary_atmos.is_some() { planet_turfs.insert(cur_index); @@ -717,12 +700,11 @@ fn send_pressure_differences( ) { for (amt, cur_turf, adj_turf) in pressures { drop(sender.try_send(Box::new(move || { - let real_amount = ByondValue::from(amt); let turf = ByondValue::new_ref(TURF_TYPE, cur_turf); let other_turf = ByondValue::new_ref(TURF_TYPE, adj_turf); if let Err(e) = turf.call_id( byond_string!("consider_pressure_difference"), - &[other_turf, real_amount], + &[other_turf, amt.into()], ) { byondapi::global_call::call_global_id( byond_string!("stack_trace"), @@ -755,11 +737,14 @@ fn equalize_hook(mut src: ByondValue, remaining: ByondValue) { let bench = start_time.elapsed().as_millis(); let prev_cost = src.read_number_id(byond_string!("cost_equalize"))?; - src.write_var( - "cost_equalize", + src.write_var_id( + byond_string!("cost_equalize"), &(0.8 * prev_cost + 0.2 * (bench as f32)).into(), )?; - src.write_var("num_equalize_processed", &(num_eq as f32).into())?; + src.write_var_id( + byond_string!("num_equalize_processed"), + &(num_eq as f32).into(), + )?; Ok(is_cancelled.into()) } @@ -805,8 +790,7 @@ fn equalize( } flood_fill_zones( - cur_index_node, - cur_index_turf, + (cur_index_node, cur_index_turf), equalize_hard_turf_limit, &mut found_turfs, arena, diff --git a/src/turfs/processing.rs b/src/turfs/processing.rs index 6672bdc..2a8a19a 100644 --- a/src/turfs/processing.rs +++ b/src/turfs/processing.rs @@ -327,7 +327,7 @@ fn fdm( } else if diff < -5.0 { enemy_tile.call_id( byond_string!("consider_pressure_difference"), - &[turf.clone(), (-diff).into()], + &[turf, (-diff).into()], )?; } } diff --git a/src/turfs/superconduct.rs b/src/turfs/superconduct.rs index ba834ad..e07b8f0 100644 --- a/src/turfs/superconduct.rs +++ b/src/turfs/superconduct.rs @@ -150,8 +150,6 @@ impl TurfHeat { } } - //This isn't a useless collect(), we can't hold a mutable ref and an immutable ref at once on the graph - #[allow(clippy::needless_collect)] pub fn remove_adjacencies(&mut self, index: NodeIndex) { let edges = self .graph