diff options
| author | metamuffin <metamuffin@disroot.org> | 2025-10-19 20:21:27 +0200 |
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2025-10-19 20:21:27 +0200 |
| commit | 231a5ce21fcee9195fcc504ee672e4464d627c47 (patch) | |
| tree | 5d26373c2dbdaa3a4c7dfb6d2dc4e0fb63ca47a1 /server/game-core | |
| parent | 239f139e7cdc2ee9f2658a8038d2870293e20aa4 (diff) | |
| download | hurrycurry-231a5ce21fcee9195fcc504ee672e4464d627c47.tar hurrycurry-231a5ce21fcee9195fcc504ee672e4464d627c47.tar.bz2 hurrycurry-231a5ce21fcee9195fcc504ee672e4464d627c47.tar.zst | |
Rename client-lib crate to game-core
Diffstat (limited to 'server/game-core')
| -rw-r--r-- | server/game-core/Cargo.toml | 23 | ||||
| -rw-r--r-- | server/game-core/src/gamedata_index.rs | 45 | ||||
| -rw-r--r-- | server/game-core/src/lib.rs | 286 | ||||
| -rw-r--r-- | server/game-core/src/network/mod.rs | 21 | ||||
| -rw-r--r-- | server/game-core/src/network/sync.rs | 148 | ||||
| -rw-r--r-- | server/game-core/src/network/tokio.rs | 147 | ||||
| -rw-r--r-- | server/game-core/src/spatial_index.rs | 72 |
7 files changed, 742 insertions, 0 deletions
diff --git a/server/game-core/Cargo.toml b/server/game-core/Cargo.toml new file mode 100644 index 00000000..8d764ab8 --- /dev/null +++ b/server/game-core/Cargo.toml @@ -0,0 +1,23 @@ +[package] +name = "hurrycurry-game-core" +version = "3.0.0" +edition = "2024" + +[dependencies] +hurrycurry-protocol = { path = "../protocol" } +tungstenite = { version = "0.27.0", optional = true, features = [ + "rustls-tls-native-roots", +] } +tokio-tungstenite = { version = "0.27.0", optional = true, features = [ + "rustls-tls-native-roots", +] } +tokio = { version = "1.47.1", features = ["net", "sync"], optional = true } +serde_json = "1.0.145" +log = "0.4.28" +anyhow = "1.0.99" +futures-util = { version = "0.3.31", optional = true } + +[features] +default = ["sync-network", "tokio-network"] +sync-network = ["dep:tungstenite"] +tokio-network = ["dep:tokio-tungstenite", "dep:tokio", "dep:futures-util"] diff --git a/server/game-core/src/gamedata_index.rs b/server/game-core/src/gamedata_index.rs new file mode 100644 index 00000000..8aa5e4af --- /dev/null +++ b/server/game-core/src/gamedata_index.rs @@ -0,0 +1,45 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use hurrycurry_protocol::{Gamedata, ItemIndex, Recipe, RecipeIndex, TileIndex}; +use std::collections::HashMap; + +#[derive(Debug, Default)] +pub struct GamedataIndex { + pub tile_collide: Vec<bool>, + pub recipe_passive_by_input: HashMap<ItemIndex, Vec<RecipeIndex>>, +} + +impl GamedataIndex { + pub fn update(&mut self, data: &Gamedata) { + self.recipe_passive_by_input.clear(); + self.tile_collide.clear(); + + for tile in (0..data.tile_names.len()).map(TileIndex) { + self.tile_collide.push(!data.tile_walkable.contains(&tile)); + } + + for (ri, r) in data.recipes() { + if let Recipe::Passive { input, .. } = r { + self.recipe_passive_by_input + .entry(*input) + .or_default() + .push(ri); + } + } + } +} diff --git a/server/game-core/src/lib.rs b/server/game-core/src/lib.rs new file mode 100644 index 00000000..cc77e570 --- /dev/null +++ b/server/game-core/src/lib.rs @@ -0,0 +1,286 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +pub mod gamedata_index; +pub mod network; +pub mod spatial_index; + +use hurrycurry_protocol::{ + Character, Gamedata, Hand, ItemIndex, ItemLocation, Message, MessageTimeout, PacketC, + PlayerClass, PlayerID, RecipeIndex, Score, TileIndex, glam::IVec2, movement::MovementBase, +}; +use spatial_index::SpatialIndex; +use std::{ + collections::{BTreeSet, HashMap, HashSet, VecDeque}, + sync::Arc, + time::Instant, +}; +use crate::gamedata_index::GamedataIndex; + +#[derive(Debug, Clone, PartialEq)] +pub struct Involvement { + pub position: f32, + pub speed: f32, + pub recipe: RecipeIndex, + pub players: BTreeSet<PlayerID>, + pub warn: bool, +} + +#[derive(Debug, PartialEq)] +pub struct Item { + pub kind: ItemIndex, + pub active: Option<Involvement>, +} + +pub struct Tile { + pub kind: TileIndex, + pub item: Option<Item>, +} + +pub struct Player { + pub name: String, + pub class: PlayerClass, + pub character: Character, + pub interacting: Option<(ItemLocation, Hand)>, + pub items: Vec<Option<Item>>, + pub communicate_persist: Option<(Message, MessageTimeout)>, + + pub movement: MovementBase, +} + +#[derive(Default)] +pub struct Game { + pub data: Arc<Gamedata>, + pub data_index: GamedataIndex, + + pub tiles: HashMap<IVec2, Tile>, + pub players: HashMap<PlayerID, Player>, + pub end: Option<Instant>, + pub lobby: bool, + pub environment_effects: HashSet<String>, + pub score: Score, + pub player_id_counter: i64, + + pub players_spatial_index: SpatialIndex<PlayerID>, + pub walkable: HashSet<IVec2>, + pub tile_index: HashMap<TileIndex, HashSet<IVec2>>, + + pub events: VecDeque<PacketC>, +} + +impl Game { + pub fn apply_packet(&mut self, packet: PacketC) { + match packet { + PacketC::Data { data } => { + self.data = Arc::new(*data); + } + PacketC::AddPlayer { + id, + position, + character, + class, + name, + } => { + self.players.insert( + id, + Player { + name, + character, + class, + interacting: None, + items: (0..self.data.hand_count).map(|_| None).collect(), + communicate_persist: None, + movement: MovementBase::new(position), + }, + ); + } + PacketC::RemovePlayer { id } => { + self.players.remove(&id); + } + PacketC::Movement { + player, + pos, + rot, + boost, + dir, + } => { + if let Some(p) = self.players.get_mut(&player) { + p.movement.input(dir, boost); + p.movement.position = pos; + p.movement.rotation = rot; + } + } + PacketC::MoveItem { from, to } => { + if let Some(item) = self.get_item(to).map(|e| e.take()) { + if let Some(to) = self.get_item(from) { + *to = item; + } else { + // TODO perhaps restore to original position? + } + } + } + PacketC::SetItem { location, item } => { + let location = self.get_item(location); + if let Some(location) = location { + *location = item.map(|kind| Item { kind, active: None }); + } + } + PacketC::ClearProgress { item } => { + if let Some(Some(item)) = self.get_item(item) { + item.active = None; + } + } + PacketC::SetProgress { + item, + position, + players, + speed, + warn, + } => { + if let Some(Some(item)) = self.get_item(item) { + item.active = Some(Involvement { + players, + speed, + warn, + position, + recipe: RecipeIndex(0), + }); + } + } + PacketC::UpdateMap { + tile, + kind, + neighbors: _, + } => { + self.set_tile(tile, kind); + } + PacketC::Communicate { + player, + message, + timeout, + } => { + if let Some(timeout) = &timeout + && let Some(player) = self.players.get_mut(&player) + { + player.communicate_persist = message.to_owned().map(|m| (m, *timeout)); + } + } + PacketC::Score(score) => { + self.score = score; + } + PacketC::SetIngame { state: _, lobby } => { + self.lobby = lobby; + } + PacketC::Environment { effects } => { + self.environment_effects = effects; + } + _ => (), + } + } + + pub fn set_tile(&mut self, pos: IVec2, kind: Option<TileIndex>) { + self.tiles.remove(&pos); + self.walkable.remove(&pos); + if let Some(prev) = self.tiles.get(&pos) { + if let Some(set) = self.tile_index.get_mut(&prev.kind) { + set.remove(&pos); + } + } + if let Some(kind) = kind { + self.tiles.insert(pos, Tile { kind, item: None }); + if self.data_index.tile_collide[kind.0] { + self.walkable.insert(pos); + } + self.tile_index.entry(kind).or_default().insert(pos); + } + self.events.push_back(PacketC::UpdateMap { + tile: pos, + kind, + neighbors: [ + self.tiles.get(&(pos + IVec2::NEG_Y)).map(|e| e.kind), + self.tiles.get(&(pos + IVec2::NEG_X)).map(|e| e.kind), + self.tiles.get(&(pos + IVec2::Y)).map(|e| e.kind), + self.tiles.get(&(pos + IVec2::X)).map(|e| e.kind), + ], + }); + } + + pub fn tick(&mut self, dt: f32) { + self.score.time_remaining -= dt as f64; + self.score.time_remaining -= self.score.time_remaining.max(0.); + + for (&pid, player) in &mut self.players { + player.movement.update(&self.walkable, dt); + if let Some((_, timeout)) = &mut player.communicate_persist { + timeout.remaining -= dt; + if timeout.remaining < 0. { + player.communicate_persist = None; + } + } + self.players_spatial_index + .update_entry(pid, player.movement.position); + } + + for player in self.players.values_mut() { + for item in player.items.iter_mut().flatten() { + if let Some(active) = &mut item.active { + active.position += active.speed; + } + } + } + for tile in self.tiles.values_mut() { + if let Some(item) = &mut tile.item + && let Some(active) = &mut item.active + { + active.position += active.speed; + } + } + + self.players_spatial_index.all(|p1, pos1| { + self.players_spatial_index.query(pos1, 2., |p2, _pos2| { + if let [Some(a), Some(b)] = self.players.get_disjoint_mut([&p1, &p2]) { + a.movement.collide(&mut b.movement, dt) + } + }) + }); + } + + pub fn get_item(&mut self, location: ItemLocation) -> Option<&mut Option<Item>> { + match location { + ItemLocation::Tile(pos) => Some(&mut self.tiles.get_mut(&pos)?.item), + ItemLocation::Player(pid, hand) => { + Some(self.players.get_mut(&pid)?.items.get_mut(hand.0)?) + } + } + } + pub fn get_unused_player_id(&mut self) -> PlayerID { + //! not possible because of join logic in server / multiple entities spawning bots + // let mut id = PlayerID(0); + // while self.players.contains_key(&id) { + // id.0 += 1; + // } + // id + self.player_id_counter += 1; + PlayerID(self.player_id_counter) + } +} + +impl From<TileIndex> for Tile { + fn from(kind: TileIndex) -> Self { + Self { kind, item: None } + } +} diff --git a/server/game-core/src/network/mod.rs b/server/game-core/src/network/mod.rs new file mode 100644 index 00000000..45963567 --- /dev/null +++ b/server/game-core/src/network/mod.rs @@ -0,0 +1,21 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +#[cfg(feature = "sync-network")] +pub mod sync; +#[cfg(feature = "tokio-network")] +pub mod tokio; diff --git a/server/game-core/src/network/sync.rs b/server/game-core/src/network/sync.rs new file mode 100644 index 00000000..9854b58e --- /dev/null +++ b/server/game-core/src/network/sync.rs @@ -0,0 +1,148 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use anyhow::Result; +use hurrycurry_protocol::{PacketC, PacketS, VERSION}; +use log::{debug, info, warn}; +use std::{collections::VecDeque, net::TcpStream}; +use tungstenite::{ + Message, WebSocket, + client::{IntoClientRequest, uri_mode}, + client_tls_with_config, + handshake::client::Request, + stream::{MaybeTlsStream, Mode}, + util::NonBlockingError, +}; + +pub struct Network { + sock: WebSocket<MaybeTlsStream<TcpStream>>, + pub queue_in: VecDeque<PacketC>, + pub queue_out: VecDeque<PacketS>, + use_bincode: bool, +} + +impl Network { + pub fn connect(addr: &str) -> Result<Self> { + let (parts, _) = addr.into_client_request()?.into_parts(); + let mut builder = Request::builder() + .uri(parts.uri.clone().clone()) + .method(parts.method.clone()) + .version(parts.version); + *builder.headers_mut().unwrap() = parts.headers.clone(); + let request = builder.body(())?; + + let host = request.uri().host().unwrap_or("127.0.0.1"); + let host = if host.starts_with('[') { + &host[1..host.len() - 1] + } else { + host + }; + let port = request + .uri() + .port_u16() + .unwrap_or(match uri_mode(request.uri())? { + Mode::Plain => 27032, + Mode::Tls => 443, + }); + + info!("Connecting: host={host:?} port={port}"); + let stream = TcpStream::connect((host, port))?; + stream.set_nodelay(true)?; + + let (mut sock, _) = client_tls_with_config(request, stream, None, None)?; + + match sock.get_mut() { + MaybeTlsStream::Plain(s) => s.set_nonblocking(true)?, + MaybeTlsStream::Rustls(s) => s.sock.set_nonblocking(true)?, + _ => unreachable!(), + }; + + info!("Handshake complete."); + Ok(Self { + sock, + use_bincode: false, + queue_in: VecDeque::new(), + queue_out: VecDeque::new(), + }) + } + + pub fn poll(&mut self) -> anyhow::Result<()> { + loop { + self.queue_in.extend(match self.sock.read() { + Ok(Message::Text(packet)) => match serde_json::from_str(&packet) { + Ok(packet) => { + debug!("<- {packet:?}"); + if let PacketC::Version { + minor, + major, + supports_bincode, + } = &packet + && *minor == VERSION.0 + && *major == VERSION.1 + && *supports_bincode + { + info!("Binary protocol format enabled."); + self.use_bincode = true; + } + Some(packet) + } + Err(e) => { + warn!("invalid json packet: {e:?}"); + None + } + }, + Ok(Message::Binary(_packet)) => { + // match bincode::decode_from_slice(&packet, BINCODE_CONFIG) { + // Ok((packet, _)) => { + // debug!("<- {packet:?}"); + // Some(packet) + // } + // Err(e) => { + // warn!("invalid bincode packet: {e:?}"); + // None + // } + // } + None + } + Ok(_) => None, + Err(e) => { + if let Some(e) = e.into_non_blocking() { + warn!("{e:?}"); + None + } else { + break; + } + } + }); + } + + for packet in self.queue_out.drain(..) { + debug!("-> {packet:?}"); + // if self.use_bincode { + // self.sock.write(Message::Binary( + // bincode::encode_to_vec(&packet, BINCODE_CONFIG)?.into(), + // ))?; + // } else { + self.sock + .write(Message::Text(serde_json::to_string(&packet)?.into()))?; + // } + } + + self.sock.flush()?; + Ok(()) + } +} diff --git a/server/game-core/src/network/tokio.rs b/server/game-core/src/network/tokio.rs new file mode 100644 index 00000000..6e7f0902 --- /dev/null +++ b/server/game-core/src/network/tokio.rs @@ -0,0 +1,147 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use anyhow::{Result, anyhow}; +use futures_util::{ + SinkExt, TryStreamExt, + stream::{SplitSink, SplitStream, StreamExt}, +}; +use hurrycurry_protocol::{PacketC, PacketS, VERSION}; +use log::{debug, info, warn}; +use std::sync::atomic::{AtomicBool, Ordering}; +use tokio::{net::TcpStream, sync::RwLock}; +use tokio_tungstenite::{MaybeTlsStream, WebSocketStream, client_async_tls_with_config}; +use tungstenite::{ + Message, + client::{IntoClientRequest, uri_mode}, + http::Request, + stream::Mode, +}; + +pub struct Network { + sock_recv: RwLock<SplitStream<WebSocketStream<MaybeTlsStream<TcpStream>>>>, + sock_send: RwLock<SplitSink<WebSocketStream<MaybeTlsStream<TcpStream>>, Message>>, + use_binary: AtomicBool, +} + +impl Network { + pub async fn connect(uri: &str) -> Result<Self> { + let (parts, _) = uri.into_client_request().unwrap().into_parts(); + let mut builder = Request::builder() + .uri(parts.uri.clone().clone()) + .method(parts.method.clone()) + .version(parts.version); + *builder.headers_mut().unwrap() = parts.headers.clone(); + let request = builder.body(()).unwrap(); + + let host = request.uri().host().unwrap(); + let host = if host.starts_with('[') { + &host[1..host.len() - 1] + } else { + host + }; + let port = request + .uri() + .port_u16() + .unwrap_or(match uri_mode(request.uri())? { + Mode::Plain => 27032, + Mode::Tls => 443, + }); + + info!("Connecting: host={host:?} port={port}"); + let stream = TcpStream::connect((host, port)).await?; + Self::connect_raw(stream, uri).await + } + pub async fn connect_raw(stream: TcpStream, uri: &str) -> Result<Self> { + let (parts, _) = uri.into_client_request()?.into_parts(); + let mut builder = Request::builder() + .uri(parts.uri.clone().clone()) + .method(parts.method.clone()) + .version(parts.version); + *builder.headers_mut().ok_or(anyhow!("???"))? = parts.headers.clone(); + let request = builder.body(())?; + + stream.set_nodelay(true)?; + let (sock, _) = client_async_tls_with_config(request, stream, None, None).await?; + info!("Handshake complete."); + let (sock_send, sock_recv) = sock.split(); + Ok(Self { + sock_recv: RwLock::new(sock_recv), + sock_send: RwLock::new(sock_send), + use_binary: false.into(), + }) + } + + pub async fn receive(&self) -> anyhow::Result<Option<PacketC>> { + let mut g = self.sock_recv.write().await; + loop { + match g.try_next().await? { + Some(Message::Text(packet)) => match serde_json::from_str(&packet) { + Ok(packet) => { + debug!("<- {packet:?}"); + if let PacketC::Version { + minor, + major, + supports_bincode, + } = &packet + && *minor == VERSION.0 + && *major == VERSION.1 + && *supports_bincode + { + info!("Binary protocol format enabled."); + self.use_binary.store(true, Ordering::Relaxed); + } + return Ok(Some(packet)); + } + Err(e) => { + warn!("invalid json packet: {e:?}"); + } + }, + Some(Message::Binary(_packet)) => { + // match bincode::decode_from_slice(&packet, BINCODE_CONFIG) { + // Ok((packet, _)) => { + // debug!("<- {packet:?}"); + // return Ok(Some(packet)); + // } + // Err(e) => { + // warn!("invalid bincode packet: {e:?}"); + // } + // } + } + _ => (), + }; + } + } + pub async fn send(&self, packet: PacketS) -> anyhow::Result<()> { + debug!("-> {packet:?}"); + let mut g = self.sock_send.write().await; + // if self.use_binary.load(Ordering::Relaxed) { + // g.send(Message::Binary( + // bincode::encode_to_vec(&packet, BINCODE_CONFIG) + // .unwrap() + // .into(), + // )) + // .await?; + // } else { + g.send(Message::Text( + serde_json::to_string(&packet).unwrap().into(), + )) + .await?; + // } + Ok(()) + } +} diff --git a/server/game-core/src/spatial_index.rs b/server/game-core/src/spatial_index.rs new file mode 100644 index 00000000..8dd0cc22 --- /dev/null +++ b/server/game-core/src/spatial_index.rs @@ -0,0 +1,72 @@ +/* + Hurry Curry! - a game about cooking + Copyright (C) 2025 Hurry Curry! Contributors + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, version 3 of the License only. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see <https://www.gnu.org/licenses/>. + +*/ +use hurrycurry_protocol::glam::{IVec2, Vec2, ivec2}; +use std::{collections::HashMap, hash::Hash}; + +pub struct SpatialIndex<T> { + entries: HashMap<T, Vec2>, + bins: HashMap<IVec2, Vec<T>>, +} + +impl<T: Eq + Hash + Copy> SpatialIndex<T> { + pub fn update_entry(&mut self, id: T, position: Vec2) { + self.remove_entry(id); + self.entries.insert(id, position); + let e = self.bins.entry(position.as_ivec2()).or_default(); + if !e.contains(&id) { + e.push(id); + } + } + pub fn remove_entry(&mut self, id: T) { + if let Some(pos) = self.entries.remove(&id) { + self.bins + .entry(pos.as_ivec2()) + .or_default() + .retain(|e| *e != id); + } + } + pub fn all(&self, mut cb: impl FnMut(T, Vec2)) { + for (&e, &pos) in &self.entries { + cb(e, pos) + } + } + pub fn query(&self, position: Vec2, radius: f32, mut cb: impl FnMut(T, Vec2)) { + let p = position.as_ivec2(); + let r = radius.ceil() as i32; + for xo in -r..=r { + for yo in -r..=r { + if let Some(bin) = self.bins.get(&(p + ivec2(xo, yo))) { + for &id in bin { + let p = *self.entries.get(&id).unwrap(); + if p.distance(position) < radius { + cb(id, p) + } + } + } + } + } + } +} +impl<T> Default for SpatialIndex<T> { + fn default() -> Self { + Self { + entries: Default::default(), + bins: Default::default(), + } + } +} |