aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormetamuffin <metamuffin@disroot.org>2024-09-04 20:35:20 +0200
committermetamuffin <metamuffin@disroot.org>2024-09-04 20:35:20 +0200
commiteaae75ef73a2d3cafda300ab63b67a9cfcb64955 (patch)
tree33040feb74d7b03e3655dc32135c93bbda75c229
parent740dc4b45cb59ff184e5cebc5b19f402bb9d54d2 (diff)
downloadhurrycurry-eaae75ef73a2d3cafda300ab63b67a9cfcb64955.tar
hurrycurry-eaae75ef73a2d3cafda300ab63b67a9cfcb64955.tar.bz2
hurrycurry-eaae75ef73a2d3cafda300ab63b67a9cfcb64955.tar.zst
refactor tile tick and improve active recipe behaviour
-rw-r--r--server/src/interaction.rs65
-rw-r--r--server/src/server.rs76
2 files changed, 61 insertions, 80 deletions
diff --git a/server/src/interaction.rs b/server/src/interaction.rs
index e2e4d08b..99537487 100644
--- a/server/src/interaction.rs
+++ b/server/src/interaction.rs
@@ -66,8 +66,15 @@ pub fn interact(
score_changed,
packet_out,
);
- return;
+ } else {
+ packet_out.push_back(PacketC::SetProgress {
+ item: this_loc,
+ position: active.position,
+ speed: active.speed,
+ warn: active.warn,
+ });
}
+ return;
}
}
}
@@ -119,6 +126,12 @@ pub fn interact(
from: other_loc,
to: this_loc,
});
+ packet_out.push_back(PacketC::SetProgress {
+ item: this_loc,
+ position: 0.,
+ speed: 1. / duration,
+ warn: false,
+ });
return;
}
}
@@ -198,40 +211,61 @@ pub fn tick_slot(
data: &Gamedata,
tile: Option<TileIndex>,
slot: &mut Option<Item>,
+ slot_loc: ItemLocation,
score: &mut Score,
-) -> Option<TickEffect> {
+ score_changed: &mut bool,
+ packet_out: &mut VecDeque<PacketC>,
+) {
if let Some(item) = slot {
if let Some(a) = &mut item.active {
let r = &data.recipe(a.recipe);
let prev_speed = a.speed;
- if !r.supports_tile(tile) {
- if let Some(revert_duration) = r.revert_duration() {
- a.speed = -1. / revert_duration
+ if r.supports_tile(tile) {
+ if a.speed < 0. {
+ if let Recipe::Passive { duration, .. } = &data.recipe(a.recipe) {
+ a.speed = 1. / duration;
+ }
}
+ } else if let Some(revert_duration) = r.revert_duration() {
+ a.speed = -1. / revert_duration
}
+ if a.position < 0. {
+ item.active = None;
+ packet_out.push_back(PacketC::ClearProgress { item: slot_loc });
+ return;
+ }
if a.position >= 1. {
if let Recipe::Passive { output, .. } = &data.recipe(a.recipe) {
*slot = output.map(|kind| Item { kind, active: None });
score.passive_recipes += 1;
- return Some(TickEffect::Produce);
+ *score_changed = true;
+ packet_out.push_back(PacketC::SetProgress {
+ warn: false,
+ item: slot_loc,
+ position: 1.,
+ speed: 0.,
+ });
+ packet_out.push_back(PacketC::SetItem {
+ location: slot_loc,
+ item: slot.as_ref().map(|i| i.kind),
+ });
+ return;
};
}
- if a.position < 0. {
- item.active = None;
- return Some(TickEffect::ClearProgress);
- }
a.position += dt * a.speed;
- a.position = a.position.clamp(0., 1.);
+ a.position = a.position.min(1.);
if a.speed != prev_speed {
- return Some(TickEffect::Progress {
- warn: r.warn(),
+ packet_out.push_back(PacketC::SetProgress {
position: a.position,
speed: a.speed,
+ warn: a.warn,
+ item: slot_loc,
});
+ return;
}
} else {
for (ri, recipe) in data.recipes() {
@@ -250,18 +284,19 @@ pub fn tick_slot(
warn: *warn,
speed: 1. / *duration,
});
- return Some(TickEffect::Progress {
+ packet_out.push_back(PacketC::SetProgress {
position: 0.,
speed: 1. / *duration,
warn: *warn,
+ item: slot_loc,
});
+ return;
}
}
}
}
}
}
- None
}
fn produce(
diff --git a/server/src/server.rs b/server/src/server.rs
index 109d5561..47cfdcf4 100644
--- a/server/src/server.rs
+++ b/server/src/server.rs
@@ -18,7 +18,7 @@
use crate::{
data::{DataIndex, Serverdata},
entity::{Entities, EntityContext},
- interaction::{interact, tick_slot, TickEffect},
+ interaction::{interact, tick_slot},
scoreboard::ScoreboardStore,
ConnectionID,
};
@@ -542,43 +542,16 @@ impl Server {
}
for (&pos, tile) in &mut self.game.tiles {
- if let Some(effect) = tick_slot(
+ tick_slot(
dt,
&self.game.data,
Some(tile.kind),
&mut tile.item,
+ ItemLocation::Tile(pos),
&mut self.game.score,
- ) {
- match effect {
- TickEffect::Progress {
- speed,
- position,
- warn,
- } => self.packet_out.push_back(PacketC::SetProgress {
- position,
- speed,
- warn,
- item: ItemLocation::Tile(pos),
- }),
- TickEffect::ClearProgress => {
- self.packet_out.push_back(PacketC::ClearProgress {
- item: ItemLocation::Tile(pos),
- })
- }
- TickEffect::Produce => {
- self.packet_out.push_back(PacketC::SetProgress {
- warn: false,
- item: ItemLocation::Tile(pos),
- position: 1.,
- speed: 0.,
- });
- self.packet_out.push_back(PacketC::SetItem {
- location: ItemLocation::Tile(pos),
- item: tile.item.as_ref().map(|i| i.kind),
- });
- }
- }
- }
+ &mut self.score_changed,
+ &mut self.packet_out,
+ );
}
for (&pid, player) in &mut self.game.players {
@@ -608,43 +581,16 @@ impl Server {
rot: player.movement.rotation,
});
- if let Some(effect) = tick_slot(
+ tick_slot(
dt,
&self.game.data,
None,
&mut player.item,
+ ItemLocation::Player(pid),
&mut self.game.score,
- ) {
- match effect {
- TickEffect::Progress {
- position,
- speed,
- warn,
- } => self.packet_out.push_back(PacketC::SetProgress {
- warn,
- position,
- speed,
- item: ItemLocation::Player(pid),
- }),
- TickEffect::ClearProgress => {
- self.packet_out.push_back(PacketC::ClearProgress {
- item: ItemLocation::Player(pid),
- })
- }
- TickEffect::Produce => {
- self.packet_out.push_back(PacketC::SetProgress {
- warn: false,
- item: ItemLocation::Player(pid),
- position: 1.,
- speed: 0.,
- });
- self.packet_out.push_back(PacketC::SetItem {
- location: ItemLocation::Player(pid),
- item: player.item.as_ref().map(|i| i.kind),
- });
- }
- }
- }
+ &mut self.score_changed,
+ &mut self.packet_out,
+ );
}
let mut players_auto_release = Vec::new();