diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-06-20 01:20:51 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-06-23 19:21:49 +0200 | 
| commit | 6f7b995dd9fa3bea95be8c24e2452f015b410839 (patch) | |
| tree | 2c1d2d03a759ee4597a07d5998dae8480bd73a67 | |
| parent | 67ccafad4d7d481bdf60be750f96a086ae452e27 (diff) | |
| download | hurrycurry-6f7b995dd9fa3bea95be8c24e2452f015b410839.tar hurrycurry-6f7b995dd9fa3bea95be8c24e2452f015b410839.tar.bz2 hurrycurry-6f7b995dd9fa3bea95be8c24e2452f015b410839.tar.zst | |
change the protocol yet again
| -rw-r--r-- | data/demands.yaml | 10 | ||||
| -rw-r--r-- | data/recipes.yaml | 13 | ||||
| -rw-r--r-- | server/src/customer/mod.rs | 29 | ||||
| -rw-r--r-- | server/src/data.rs | 21 | ||||
| -rw-r--r-- | server/src/game.rs | 94 | ||||
| -rw-r--r-- | server/src/protocol.rs | 40 | ||||
| -rw-r--r-- | test-client/main.ts | 26 | ||||
| -rw-r--r-- | test-client/protocol.ts | 6 | 
8 files changed, 125 insertions, 114 deletions
| diff --git a/data/demands.yaml b/data/demands.yaml index 80679e29..6862395e 100644 --- a/data/demands.yaml +++ b/data/demands.yaml @@ -1,5 +1,5 @@ -- burger-meal -- tomatoburger-meal -- tomatosteak-meal -- steak-meal -- sliced-tomato-meal +- { from: burger-meal, to: dirty-dish } +- { from: tomatoburger-meal, to: dirty-dish } +- { from: tomatosteak-meal, to: dirty-dish } +- { from: steak-meal, to: dirty-dish } +- { from: sliced-tomato-mea, to: dirty-dish } diff --git a/data/recipes.yaml b/data/recipes.yaml index 98de3a9c..2f0b4660 100644 --- a/data/recipes.yaml +++ b/data/recipes.yaml @@ -1,22 +1,9 @@ -# - { tile: floor, action: !never  } - -# - { tile: window, inputs: [steak-meal, void] } -# - { tile: window, inputs: [sliced-tomato-meal, void] } -# - { tile: window, inputs: [bread-meal, void] } -# - { tile: window, inputs: [burger-meal, void] } -# - { tile: window, inputs: [tomatosteak-meal, void] } -# - { tile: window, inputs: [tomatoburger-meal, void] } -  - { tile: trash, action: !instant , inputs: [raw-steak] }  - { tile: trash, action: !instant , inputs: [steak] }  - { tile: trash, action: !instant , inputs: [tomato] }  - { tile: trash, action: !instant , inputs: [flour] }  - { tile: trash, action: !instant , inputs: [dough] }  - { tile: trash, action: !instant , inputs: [steak-meal] } -# - { tile: counter, inputs: [raw-steak, void] } -# - { tile: counter, inputs: [sliced-tomato, void] } -# - { tile: counter, inputs: [dough, void] } -# - { tile: counter, inputs: [bread, void] }  - tile: tomato-crate # Tomato pipeline    outputs: [tomato] diff --git a/server/src/customer/mod.rs b/server/src/customer/mod.rs index 7c6cd235..ecd99250 100644 --- a/server/src/customer/mod.rs +++ b/server/src/customer/mod.rs @@ -4,13 +4,13 @@ mod pathfinding;  use crate::{      data::Gamedata,      game::Game, -    protocol::{ItemIndex, Message, PacketC, PacketS, PlayerID}, +    protocol::{DemandIndex, ItemIndex, Message, PacketC, PacketS, PlayerID},  };  use glam::{IVec2, Vec2};  use log::{debug, error};  use movement::MovementBase;  use pathfinding::{find_path, Path}; -use rand::thread_rng; +use rand::{random, thread_rng};  use std::{      collections::{HashMap, HashSet},      sync::Arc, @@ -36,7 +36,7 @@ struct DemandState {  enum CustomerState {      WalkingToChair { path: Path, chair: IVec2 }, -    Waiting { chair: IVec2, demand: ItemIndex }, +    Waiting { chair: IVec2, demand: DemandIndex },      Exiting { path: Path },  } @@ -83,8 +83,7 @@ pub async fn customer(game: Arc<RwLock<Game>>, mut grx: broadcast::Receiver<Pack                  match packet.unwrap() {                      PacketC::PutItem { .. }                      | PacketC::TakeItem { .. } -                    | PacketC::ProduceItem { .. } -                    | PacketC::ConsumeItem { .. } => { +                    | PacketC::SetTileItem { .. } => {                          let g = game.read().await;                          update_items(&mut state, &g)                      }, @@ -117,10 +116,10 @@ impl DemandState {          // TODO insert sofa magic formula          5      } -    pub fn generate_demand(&self) -> usize { +    pub fn generate_demand(&self) -> DemandIndex {          // TODO insert sofa magic formula -        use rand::seq::IndexedRandom; -        *self.data.demands.choose(&mut thread_rng()).unwrap() + +        random::<usize>() % self.data.demands.len()      }  } @@ -165,7 +164,7 @@ impl CustomerManager {                          packets_out.push((                              id,                              PacketS::Communicate { -                                message: Some(Message::Item(demand)), +                                message: Some(Message::Item(self.demand.data.demands[demand].from)),                              },                          ));                          p.state = CustomerState::Waiting { @@ -186,11 +185,21 @@ impl CustomerManager {                              }                          });                      if let Some(pos) = demand_pos { -                        if self.items.get(&pos) == Some(demand) { +                        let demand = &self.demand.data.demands[*demand]; +                        if self.items.get(&pos) == Some(&demand.from) {                              packets_out.push((id, PacketS::Communicate { message: None }));                              for edge in [true, false] {                                  packets_out.push((id, PacketS::Interact { pos, edge }))                              } +                            packets_out.push(( +                                id, +                                PacketS::ReplaceHand { +                                    item: Some(demand.to), +                                }, +                            )); +                            for edge in [true, false] { +                                packets_out.push((id, PacketS::Interact { pos, edge })) +                            }                              let path = find_path(                                  &self.walkable,                                  p.movement.position.as_ivec2(), diff --git a/server/src/data.rs b/server/src/data.rs index 3526d786..e467fefa 100644 --- a/server/src/data.rs +++ b/server/src/data.rs @@ -31,22 +31,34 @@ pub struct InitialMap {      tiles: HashMap<String, String>,  } +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct DemandDecl { +    from: String, +    to: String, +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +pub struct Demand { +    pub from: usize, +    pub to: usize, +} +  #[derive(Debug, Clone, Serialize, Deserialize, Default)]  pub struct Gamedata {      pub recipes: Vec<Recipe>, +    pub demands: Vec<Demand>,      pub item_names: Vec<String>,      pub tile_names: Vec<String>,      #[serde(skip)]      pub initial_map: HashMap<IVec2, TileIndex>,      pub chef_spawn: Vec2,      pub customer_spawn: Vec2, -    pub demands: Vec<usize>,  }  pub fn build_gamedata(      recipes_in: Vec<RecipeDecl>,      map_in: InitialMap, -    demands_in: Vec<String>, +    demands_in: Vec<DemandDecl>,  ) -> Gamedata {      let item_names = RwLock::new(Vec::new());      let tile_names = RwLock::new(Vec::new()); @@ -85,7 +97,10 @@ pub fn build_gamedata(      }      for d in demands_in { -        demands.push(register(&item_names, d)) +        demands.push(Demand { +            from: register(&item_names, d.from), +            to: register(&item_names, d.to), +        })      }      let mut chef_spawn = Vec2::new(0., 0.); diff --git a/server/src/game.rs b/server/src/game.rs index 225455bf..b48e1833 100644 --- a/server/src/game.rs +++ b/server/src/game.rs @@ -80,8 +80,13 @@ impl Game {                  position: player.position,                  character: player.character,                  name: player.name.clone(), -                item: player.item.as_ref().map(|i| i.kind),              }); +            if let Some(item) = &player.item { +                out.push(PacketC::SetPlayerItem { +                    player: id, +                    item: Some(item.kind), +                }) +            }              if let Some(c) = &player.communicate {                  out.push(PacketC::Communicate {                      player: id, @@ -101,9 +106,9 @@ impl Game {                  tile: tdata.kind.clone(),              });              if let Some(item) = &tdata.item { -                out.push(PacketC::ProduceItem { +                out.push(PacketC::SetTileItem {                      tile, -                    item: item.kind, +                    item: Some(item.kind),                  })              }          } @@ -133,7 +138,6 @@ impl Game {                      name,                      position: self.data.chef_spawn,                      character, -                    item: None,                  });              }              PacketS::Leave => { @@ -145,9 +149,9 @@ impl Game {                      let pos = p.position.floor().as_ivec2();                      if let Some(tile) = self.tiles.get_mut(&pos) {                          if tile.item.is_none() { -                            self.packet_out.push_back(PacketC::ProduceItem { +                            self.packet_out.push_back(PacketC::SetTileItem {                                  tile: pos, -                                item: item.kind, +                                item: Some(item.kind),                              });                              tile.item = Some(item);                          } @@ -204,21 +208,25 @@ impl Game {                          }),                          InteractEffect::Produce => {                              if tile_had_item { -                                self.packet_out -                                    .push_back(PacketC::ConsumeItem { tile: pos }); +                                self.packet_out.push_back(PacketC::SetTileItem { +                                    tile: pos, +                                    item: None, +                                });                              }                              if player_had_item {                                  self.packet_out.push_back(PacketC::PutItem {                                      player: pid,                                      tile: pos,                                  }); -                                self.packet_out -                                    .push_back(PacketC::ConsumeItem { tile: pos }); +                                self.packet_out.push_back(PacketC::SetTileItem { +                                    tile: pos, +                                    item: None, +                                });                              }                              if let Some(i) = &player.item { -                                self.packet_out.push_back(PacketC::ProduceItem { +                                self.packet_out.push_back(PacketC::SetTileItem {                                      tile: pos, -                                    item: i.kind, +                                    item: Some(i.kind),                                  });                                  self.packet_out.push_back(PacketC::TakeItem {                                      player: pid, @@ -226,9 +234,9 @@ impl Game {                                  })                              }                              if let Some(i) = &tile.item { -                                self.packet_out.push_back(PacketC::ProduceItem { +                                self.packet_out.push_back(PacketC::SetTileItem {                                      tile: pos, -                                    item: i.kind, +                                    item: Some(i.kind),                                  });                              }                          } @@ -245,6 +253,18 @@ impl Game {                  self.packet_out                      .push_back(PacketC::Communicate { player, message })              } +            PacketS::ReplaceHand { item } => { +                let pdata = self +                    .players +                    .get_mut(&player) +                    .ok_or(anyhow!("player does not exist"))?; +                pdata.item = item.map(|i| Item { +                    kind: i, +                    active: None, +                }); +                self.packet_out +                    .push_back(PacketC::SetPlayerItem { player, item }) +            }          }          Ok(())      } @@ -264,51 +284,13 @@ impl Game {                              .map(|i| i.progress),                      }),                      TickEffect::Produce => { -                        self.packet_out -                            .push_back(PacketC::ConsumeItem { tile: pos }); -                        if let Some(item) = &tile.item { -                            self.packet_out.push_back(PacketC::ProduceItem { -                                tile: pos, -                                item: item.kind, -                            }); -                        } +                        self.packet_out.push_back(PacketC::SetTileItem { +                            tile: pos, +                            item: tile.item.as_ref().map(|i| i.kind), +                        });                      }                  }              } -            // let items = tile.items.iter().map(|e| self.items[e]).collect::<Vec<_>>(); -            // tick_tile( -            //     dt, -            //     &self.data, -            //     tile.kind, -            //     &mut tile.active, -            //     items, -            //     |out| match out { -            //         Out::Take(_) | Out::Put => { -            //             unreachable!() -            //         } -            //         Out::Produce(kind) => { -            //             info!("produce"); -            //             let id = self.item_id_counter; -            //             self.item_id_counter += 1; -            //             self.items.insert(id, kind); -            //             tile.items.push(id); -            //             self.packet_out -            //                 .push_back(PacketC::ProduceItem { id, pos, kind }); -            //         } -            //         Out::Consume(index) => { -            //             info!("consume"); -            //             let id = tile.items.remove(index); -            //             info!("left {:?}", tile.items); -            //             self.packet_out.push_back(PacketC::ConsumeItem { id, pos }); -            //         } -            //         Out::SetActive(progress) => { -            //             self.packet_out.push_back(PacketC::SetActive { -            //                 tile: pos, -            //                 progress, -            //             }); -            //         } -            //     }, -            // );          }      }  } diff --git a/server/src/protocol.rs b/server/src/protocol.rs index 7eff2ba1..24fe2468 100644 --- a/server/src/protocol.rs +++ b/server/src/protocol.rs @@ -6,16 +6,36 @@ pub type PlayerID = i64;  pub type ItemIndex = usize;  pub type TileIndex = usize;  pub type RecipeIndex = usize; +pub type DemandIndex = usize;  #[derive(Debug, Clone, Serialize, Deserialize)]  #[serde(rename_all = "snake_case", tag = "type")]  pub enum PacketS { -    Join { name: String, character: usize }, +    Join { +        name: String, +        character: usize, +    },      Leave, -    Position { pos: Vec2, rot: f32 }, -    Interact { pos: IVec2, edge: bool }, -    Collide { player: PlayerID, force: Vec2 }, -    Communicate { message: Option<Message> }, +    Position { +        pos: Vec2, +        rot: f32, +    }, +    Interact { +        pos: IVec2, +        edge: bool, +    }, +    Collide { +        player: PlayerID, +        force: Vec2, +    }, +    Communicate { +        message: Option<Message>, +    }, + +    #[serde(skip)] +    ReplaceHand { +        item: Option<ItemIndex>, +    },  }  #[derive(Debug, Clone, Serialize, Deserialize)] @@ -37,7 +57,6 @@ pub enum PacketC {          position: Vec2,          character: usize,          name: String, -        item: Option<ItemIndex>,      },      RemovePlayer {          id: PlayerID, @@ -55,12 +74,13 @@ pub enum PacketC {          player: PlayerID,          tile: IVec2,      }, -    ProduceItem { +    SetTileItem {          tile: IVec2, -        item: ItemIndex, +        item: Option<ItemIndex>,      }, -    ConsumeItem { -        tile: IVec2, +    SetPlayerItem { +        player: PlayerID, +        item: Option<ItemIndex>,      },      SetActive {          tile: IVec2, diff --git a/test-client/main.ts b/test-client/main.ts index b34220df..fd49fca2 100644 --- a/test-client/main.ts +++ b/test-client/main.ts @@ -85,15 +85,12 @@ function packet(p: PacketC) {              data = p.data              break;          case "add_player": { -            let item = undefined -            if (p.item) item = { kind: p.item, x: 0, y: 0 };              players.set(p.id, {                  x: p.position[0],                  y: p.position[1],                  character: p.character,                  name: p.name,                  rot: 0, -                item,                  facing: { x: 0, y: 1 },                  vel: { x: 0, y: 0 },              }) @@ -126,15 +123,18 @@ function packet(p: PacketC) {              player.item = undefined              break;          } -        case "produce_item": { -            const item = { kind: p.item, x: p.tile[0] + 0.5, y: p.tile[1] + 0.5 } -            tiles.get(p.tile.toString())!.item = item -            break; -        } -        case "consume_item": { +        case "set_tile_item": {              const tile = tiles.get(p.tile.toString())! -            if (tile.item) items_removed.add(tile.item) +            if (tile.item !== undefined && tile.item !== null) items_removed.add(tile.item)              tile.item = undefined +            if (p.item !== undefined && p.item !== null) tile.item = { kind: p.item, x: p.tile[0] + 0.5, y: p.tile[1] + 0.5 } +            break; +        } +        case "set_player_item": { +            const player = players.get(p.player)! +            if (player.item !== undefined && player.item !== null) items_removed.add(player.item) +            player.item = undefined +            if (p.item !== undefined && p.item !== null) player.item = { kind: p.item, x: player.x + 0.5, y: player.y + 0.5 }              break;          }          case "set_active": { @@ -212,10 +212,10 @@ function frame_update(dt: number) {          if (item.tracking) lerp_exp_v2_mut(item, item.tracking, dt * 10.)      }      for (const [_, player] of players) { -        if (player.item) update_item(player.item) +        if (player.item !== undefined && player.item !== null) update_item(player.item)      }      for (const [_, tile] of tiles) { -        if (tile.item) update_item(tile.item) +        if (tile.item !== undefined && tile.item !== null) update_item(tile.item)      }      const remove = [] @@ -269,7 +269,5 @@ function collide_player(p: PlayerData) {          const vdotn = (grad_x * p.vel.x) + (grad_y * p.vel.y)          p.vel.x -= grad_x * vdotn          p.vel.y -= grad_y * vdotn - -      }  } diff --git a/test-client/protocol.ts b/test-client/protocol.ts index c20588ce..f9592ab9 100644 --- a/test-client/protocol.ts +++ b/test-client/protocol.ts @@ -17,13 +17,13 @@ export type PacketS =  export type PacketC =      { type: "init", id: PlayerID, data: Gamedata } // You joined -    | { type: "add_player", id: PlayerID, name: string, item?: ItemIndex, position: Vec2, character: number } // Somebody else joined (or was already in the game) +    | { type: "add_player", id: PlayerID, name: string, position: Vec2, character: number } // Somebody else joined (or was already in the game)      | { type: "remove_player", id: PlayerID }  // Somebody left      | { type: "position", player: PlayerID, pos: Vec2, rot: number } // Update the position of a players (your own position is included here)      | { type: "take_item", tile: Vec2, player: PlayerID }  // An item was taken from a tile      | { type: "put_item", tile: Vec2, player: PlayerID } // An item was put on a tile -    | { type: "produce_item", tile: Vec2, item: ItemIndex } // A tile generated a new item -    | { type: "consume_item", tile: Vec2 } // A tile removed an item +    | { type: "set_tile_item", tile: Vec2, item?: ItemIndex } // A tile generated a new item +    | { type: "set_player_item", player: PlayerID, item?: ItemIndex } // A tile removed an item      | { type: "set_active", tile: Vec2, progress?: number } // A tile is doing something. progress goes from 0 to 1, then null when finished      | { type: "update_map", pos: Vec2, tile: TileIndex, neighbors: [TileIndex | null] } // A map tile was changed      | { type: "communicate", player: PlayerID, message?: Message } // A map tile was changed | 
