diff options
author | metamuffin <metamuffin@disroot.org> | 2024-09-04 20:41:39 +0200 |
---|---|---|
committer | metamuffin <metamuffin@disroot.org> | 2024-09-04 20:41:39 +0200 |
commit | 619c2b27f26e596c8bffc4ffb5b5b94e5bbe72da (patch) | |
tree | b6d9cd0932dd8a3b48ce0edf126a0ea6314b7388 | |
parent | eaae75ef73a2d3cafda300ab63b67a9cfcb64955 (diff) | |
download | hurrycurry-619c2b27f26e596c8bffc4ffb5b5b94e5bbe72da.tar hurrycurry-619c2b27f26e596c8bffc4ffb5b5b94e5bbe72da.tar.bz2 hurrycurry-619c2b27f26e596c8bffc4ffb5b5b94e5bbe72da.tar.zst |
precompute recipe speed
-rw-r--r-- | server/bot/src/algos/simple.rs | 4 | ||||
-rw-r--r-- | server/protocol/src/lib.rs | 18 | ||||
-rw-r--r-- | server/src/data/demands.rs | 4 | ||||
-rw-r--r-- | server/src/data/mod.rs | 6 | ||||
-rw-r--r-- | server/src/interaction.rs | 42 |
5 files changed, 32 insertions, 42 deletions
diff --git a/server/bot/src/algos/simple.rs b/server/bot/src/algos/simple.rs index 27493d74..f0889976 100644 --- a/server/bot/src/algos/simple.rs +++ b/server/bot/src/algos/simple.rs @@ -310,13 +310,13 @@ impl Context<'_, Simple> { Recipe::Active { tile: Some(tile), input, - duration, + speed, .. } => { if let Some(pos) = self.find_tile(*tile) { self.assert_tile_is_clear(pos)?; self.aquire_item(*input)?; - self.interact_with(pos, duration + 0.2)?; + self.interact_with(pos, 1. / speed + 0.2)?; } } Recipe::Passive { diff --git a/server/protocol/src/lib.rs b/server/protocol/src/lib.rs index 68af0e63..055b41c6 100644 --- a/server/protocol/src/lib.rs +++ b/server/protocol/src/lib.rs @@ -258,15 +258,15 @@ pub struct Score { #[serde(rename_all = "snake_case")] pub enum Recipe { Passive { - duration: f32, - revert_duration: Option<f32>, + speed: f32, + revert_speed: Option<f32>, tile: Option<TileIndex>, input: ItemIndex, output: Option<ItemIndex>, warn: bool, }, Active { - duration: f32, + speed: f32, tile: Option<TileIndex>, input: ItemIndex, outputs: [Option<ItemIndex>; 2], @@ -323,18 +323,16 @@ impl Recipe { Recipe::Instant { tile, .. } => *tile, } } - pub fn duration(&self) -> Option<f32> { + pub fn speed(&self) -> Option<f32> { match self { - Recipe::Passive { duration, .. } => Some(*duration), - Recipe::Active { duration, .. } => Some(*duration), + Recipe::Passive { speed, .. } => Some(*speed), + Recipe::Active { speed, .. } => Some(*speed), _ => None, } } - pub fn revert_duration(&self) -> Option<f32> { + pub fn revert_speed(&self) -> Option<f32> { match self { - Recipe::Passive { - revert_duration, .. - } => *revert_duration, + Recipe::Passive { revert_speed, .. } => *revert_speed, _ => None, } } diff --git a/server/src/data/demands.rs b/server/src/data/demands.rs index 3f4b839d..ecfad402 100644 --- a/server/src/data/demands.rs +++ b/server/src/data/demands.rs @@ -57,8 +57,8 @@ pub fn generate_demands( }; let base_cost = match r { - Recipe::Passive { duration, .. } => 2. + duration * 0.1, - Recipe::Active { duration, .. } => 2. + duration, + Recipe::Passive { speed, .. } => 2. + (1. / speed) * 0.1, + Recipe::Active { speed, .. } => 2. + (1. / speed), Recipe::Instant { .. } => 1., }; diff --git a/server/src/data/mod.rs b/server/src/data/mod.rs index 53414178..12d4f23e 100644 --- a/server/src/data/mod.rs +++ b/server/src/data/mod.rs @@ -181,17 +181,17 @@ pub fn build_data( match r.action { RecipeDeclAction::Never => {} RecipeDeclAction::Passive => recipes.push(Recipe::Passive { - duration: r.duration.ok_or(anyhow!("duration for passive missing"))?, + speed: 1. / r.duration.ok_or(anyhow!("duration for passive missing"))?, warn: r.warn, tile, - revert_duration: r.revert_duration, + revert_speed: r.revert_duration.map(|d| 1. / d), input: inputs .next() .ok_or(anyhow!("passive recipe without input"))?, output: outputs.next(), }), RecipeDeclAction::Active => recipes.push(Recipe::Active { - duration: r.duration.ok_or(anyhow!("duration for active missing"))?, + speed: 1. / r.duration.ok_or(anyhow!("duration for active missing"))?, tile, input: inputs .next() diff --git a/server/src/interaction.rs b/server/src/interaction.rs index 99537487..c475e051 100644 --- a/server/src/interaction.rs +++ b/server/src/interaction.rs @@ -17,7 +17,7 @@ */ use hurrycurry_client_lib::{Involvement, Item}; use hurrycurry_protocol::{Gamedata, ItemLocation, PacketC, Recipe, Score, TileIndex}; -use log::info; +use log::{info, warn}; use std::collections::VecDeque; pub fn interact( @@ -42,14 +42,11 @@ pub fn interact( if let Some(active) = &mut item.active { let recipe = &data.recipe(active.recipe); if recipe.supports_tile(tile) { - if let Recipe::Active { - outputs, duration, .. - } = recipe - { + if let Recipe::Active { outputs, speed, .. } = recipe { if edge { - active.speed += 1. / duration; + active.speed += speed; } else { - active.speed -= 1. / duration; + active.speed -= speed; } if active.position >= 1. { let this_had_item = this.is_some(); @@ -89,16 +86,14 @@ pub fn interact( continue; } match recipe { - Recipe::Active { - input, duration, .. - } => { + Recipe::Active { input, speed, .. } => { if other.is_none() { if let Some(item) = this { if item.kind == *input && item.active.is_none() { info!("start active recipe {ri:?}"); item.active = Some(Involvement { recipe: ri, - speed: 1. / duration, + speed: *speed, position: 0., warn: false, }); @@ -109,13 +104,13 @@ pub fn interact( if let Some(item) = &other { if item.kind == *input { let mut item = other.take().unwrap(); - if let Some(active) = &mut item.active { - active.speed += 1. / duration; + if item.active.is_some() { + warn!("interact recipe tested when already active") } else { info!("start active recipe {ri:?}"); item.active = Some(Involvement { recipe: ri, - speed: 1. / duration, + speed: *speed, position: 0., warn: false, }); @@ -129,7 +124,7 @@ pub fn interact( packet_out.push_back(PacketC::SetProgress { item: this_loc, position: 0., - speed: 1. / duration, + speed: *speed, warn: false, }); return; @@ -223,12 +218,12 @@ pub fn tick_slot( if r.supports_tile(tile) { if a.speed < 0. { - if let Recipe::Passive { duration, .. } = &data.recipe(a.recipe) { - a.speed = 1. / duration; + if let Recipe::Passive { speed, .. } = &data.recipe(a.recipe) { + a.speed = *speed; } } - } else if let Some(revert_duration) = r.revert_duration() { - a.speed = -1. / revert_duration + } else if let Some(revert_speed) = r.revert_speed() { + a.speed = revert_speed } if a.position < 0. { @@ -271,10 +266,7 @@ pub fn tick_slot( for (ri, recipe) in data.recipes() { if recipe.supports_tile(tile) { if let Recipe::Passive { - input, - warn, - duration, - .. + input, warn, speed, .. } = recipe { if *input == item.kind { @@ -282,11 +274,11 @@ pub fn tick_slot( recipe: ri, position: 0., warn: *warn, - speed: 1. / *duration, + speed: *speed, }); packet_out.push_back(PacketC::SetProgress { position: 0., - speed: 1. / *duration, + speed: *speed, warn: *warn, item: slot_loc, }); |