aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/client/editor.rs17
-rw-r--r--src/client/map/mod.rs4
-rw-r--r--src/client/map/room_mark.rs6
-rw-r--r--src/net/cargo.rs9
-rw-r--r--src/net/server/connection_manager.rs2
-rw-r--r--src/server/mod.rs13
-rw-r--r--src/stable_vec.rs2
-rw-r--r--src/world/mod.rs16
8 files changed, 62 insertions, 7 deletions
diff --git a/src/client/editor.rs b/src/client/editor.rs
index 0fb5794..e416339 100644
--- a/src/client/editor.rs
+++ b/src/client/editor.rs
@@ -127,6 +127,8 @@ impl Editor {
snapper: &Snapper,
input: &mut Input,
) {
+ self.poll_net();
+
// Handle keybindings for tool change
for (&tool_type, (_, activation_bind)) in self.tools.iter() {
if input.poll_global(&activation_bind) {
@@ -163,6 +165,21 @@ impl Editor {
active_tool.handle_custom_bindings(&mut self.map, &self.server, input);
}
+ fn poll_net(&mut self) {
+ while let Some(cargo) = self.server().next_packet() {
+ match cargo {
+ Cargo::SetRoom((id, new_room)) => {
+ if let Some(mark) = self.map.get_room_mut(id) {
+ mark.set_room(new_room);
+ } else {
+ self.map.add_room(id, new_room);
+ }
+ }
+ other => error!("Unknown packet received: {:?}", other),
+ }
+ }
+ }
+
/// Draw all tools and in case of the active tool also what is currently being edited by it, if
/// that exists.
pub fn draw_tools(&self, rld: &mut RaylibDrawHandle, transform: &Transform) {
diff --git a/src/client/map/mod.rs b/src/client/map/mod.rs
index 27cafc4..9589e59 100644
--- a/src/client/map/mod.rs
+++ b/src/client/map/mod.rs
@@ -166,6 +166,10 @@ impl Map {
pub fn rooms(&self) -> &StableVec<RoomMark> {
&self.rooms
}
+ /// Get a room with the given id mutably, in case it exists.
+ pub fn get_room_mut(&mut self, id: usize) -> Option<&mut RoomMark> {
+ self.rooms.get_mut(id)
+ }
/// Get the walls of this map.
pub fn walls(&self) -> &StableVec<WallMark> {
diff --git a/src/client/map/room_mark.rs b/src/client/map/room_mark.rs
index a9777fb..5c0ca98 100644
--- a/src/client/map/room_mark.rs
+++ b/src/client/map/room_mark.rs
@@ -29,6 +29,12 @@ impl RoomMark {
}
}
+ /// Replace the room this mark describes with the new room.
+ pub fn set_room(&mut self, room: Room) {
+ self.room = room;
+ self.retriangulate();
+ }
+
/* When the internal polygon changes, it must be retriangulated to be drawn on the screen
* properly, so this function must be called any time that happens.
*/
diff --git a/src/net/cargo.rs b/src/net/cargo.rs
index 34e6bb4..6e6ee41 100644
--- a/src/net/cargo.rs
+++ b/src/net/cargo.rs
@@ -16,11 +16,14 @@ pub enum Cargo {
/// Client -> Server: Request to add a wall to the map
AddWall(Wall),
/// Client <-> Server: Update the info of the icon with the given id.
- UpdateIcon((usize, Icon)),
+ /// Server -> Client can also create an item with this id.
+ SetIcon((usize, Icon)),
/// Client <-> Server: Update the info of the room with the given id.
- UpdateRoom((usize, Room)),
+ /// Server -> Client can also create an item with this id.
+ SetRoom((usize, Room)),
/// Client <-> Server: Update the info of the wall with the given id.
- UpdateWall((usize, Wall)),
+ /// Server -> Client can also create an item with this id.
+ SetWall((usize, Wall)),
/// Client -> Server: Request to apply the given matrix to the item with the provided id.
/// If the matrix cannot be applied to an item with the given id, it will do nothing.
ApplyMatrix((usize, Matrix3<f64>)),
diff --git a/src/net/server/connection_manager.rs b/src/net/server/connection_manager.rs
index c47aa32..b79ab2c 100644
--- a/src/net/server/connection_manager.rs
+++ b/src/net/server/connection_manager.rs
@@ -40,7 +40,7 @@ impl<'de, P: 'static + Send + Debug + DeserializeOwned + Serialize> ConnectionMa
};
let mut connections = connections.write().unwrap();
- let id = connections.next_free();
+ let id = dbg!(connections.next_free());
connections
.try_insert(id, Connection::start_rcv(id, stream, packet_tx.clone()))
.expect("Unable to insert client at supposedly valid id");
diff --git a/src/server/mod.rs b/src/server/mod.rs
index 9b55502..fe208ad 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -2,6 +2,7 @@
use crate::net::server::ConnectionManager;
use crate::net::Cargo;
+use crate::world::World;
use std::io;
use std::net::{Ipv4Addr, Ipv6Addr, SocketAddr};
use std::thread::{self, JoinHandle};
@@ -40,9 +41,17 @@ pub fn start_any_port(ipv4: bool) -> Result<(JoinHandle<()>, u16), io::Error> {
fn start(conn_man: ConnectionManager<Cargo>) -> JoinHandle<()> {
info!("Server started on port {}", conn_man.port());
+
+ let mut world = World::new();
thread::spawn(move || loop {
- if let Some(cargo) = conn_man.next_packet() {
- println!("Received cargo: {:?}", cargo);
+ if let Some((user, cargo)) = conn_man.next_packet() {
+ match cargo {
+ Cargo::AddRoom(room) => {
+ let room_id = world.push_room(room.clone());
+ conn_man.broadcast(Cargo::SetRoom((room_id, room)));
+ }
+ other => error!("Unknown packet received from `{}`: {:?}", user, other),
+ }
}
})
}
diff --git a/src/stable_vec.rs b/src/stable_vec.rs
index c3b8685..4d25e0e 100644
--- a/src/stable_vec.rs
+++ b/src/stable_vec.rs
@@ -55,7 +55,7 @@ impl<T> StableVec<T> {
if self.data.is_empty() {
0
} else if self.data.last().unwrap().0 < usize::MAX {
- self.data.last().unwrap().0
+ self.data.last().unwrap().0 + 1
} else {
// Try to find a position in the vector that is still free, starting at the bottom.
let mut prev_id = self.data.first().unwrap().0;
diff --git a/src/world/mod.rs b/src/world/mod.rs
index f163269..a72031d 100644
--- a/src/world/mod.rs
+++ b/src/world/mod.rs
@@ -34,6 +34,16 @@ pub struct World {
}
impl World {
+ /// Create a new, empty world.
+ pub fn new() -> Self {
+ Self {
+ rooms: StableVec::new(),
+ walls: StableVec::new(),
+ icons: StableVec::new(),
+ used_ids: StableVec::new(),
+ }
+ }
+
/// Create a world from the raw parts of the world. The components must fit,
/// meaning no id must exist twice and the used ids must correspond to the
/// actually used ids. Use with care.
@@ -128,3 +138,9 @@ impl World {
&self.walls
}
}
+
+impl Default for World {
+ fn default() -> Self {
+ Self::new()
+ }
+}