1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
//! The world contains all items of a session and the ways to manipulate them.
//! Generally speaking, manipulation will be done on the server side and then
//! the changes are sent to the client.
pub mod component;
pub mod icon;
pub mod room;
pub mod wall;
pub use component::*;
pub use icon::*;
pub use room::*;
pub use wall::*;
use crate::stable_vec::StableVec;
use ron::de::from_reader;
use ron::ser::{to_string_pretty, PrettyConfig};
use serde::{Deserialize, Serialize};
use std::fs::File;
use std::io::{self, Write};
use std::path::Path;
/// Main structure that contains all information about a world necessary to argue
/// about structures in it. It can be persistified and loaded from disk, as well
/// as sent over the network. Everything in it is assigned an id to make it easy
/// to argue about a specific item. IDs may only be assigned by the server, not
/// the client, since otherwise multiple clients may try to create the same item.
#[derive(Debug, Serialize, Deserialize)]
pub struct World {
rooms: StableVec<Room>,
walls: StableVec<Wall>,
icons: StableVec<Icon>,
used_ids: StableVec<()>,
}
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.
pub fn from_raw_unchecked(
rooms: StableVec<Room>,
walls: StableVec<Wall>,
icons: StableVec<Icon>,
used_ids: StableVec<()>,
) -> Self {
Self {
rooms,
walls,
icons,
used_ids,
}
}
/// Load the world data from a file. Fails if the file does not exist or
/// cannot be correctly parsed.
pub fn load_from_file<P: AsRef<Path>>(path: P) -> io::Result<Self> {
let file = File::open(&path)?;
let data: Self = match from_reader(file) {
Ok(data) => data,
Err(err) => {
return Err(io::Error::new(io::ErrorKind::InvalidData, err));
}
};
Ok(data)
}
/// Write the world data to the file located at `path`. If the file already
/// exists, it will be overwritten. If the write fails, an IO-Error is returned.
pub fn write_to_file<P: AsRef<Path>>(&self, path: P) -> io::Result<()> {
let mut file = File::create(&path)?;
let pretty_conf = PrettyConfig::new()
.with_depth_limit(4)
.with_decimal_floats(true)
.with_separate_tuple_members(true)
.with_indentor("\t".to_owned());
let string = match to_string_pretty(&self, pretty_conf) {
Ok(string) => string,
Err(err) => {
return Err(io::Error::new(io::ErrorKind::InvalidInput, err));
}
};
file.write_all(&string.as_bytes())
}
/// Add a room to the world. Currently, holes are not supported in the polygon, but this might
/// change later.
pub fn push_room(&mut self, room: Room) -> usize {
let id = self.used_ids.insert_anywhere(());
self.rooms
.try_insert(id, room)
.expect("Id vecs out of sync");
id
}
/// Add a wall to the world.
pub fn push_wall(&mut self, wall: Wall) -> usize {
let id = self.used_ids.insert_anywhere(());
self.walls
.try_insert(id, wall)
.expect("Id vecs out of sync");
id
}
/// Add an icon to the world.
pub fn push_icon(&mut self, icon: Icon) -> usize {
let id = self.used_ids.insert_anywhere(());
self.icons
.try_insert(id, icon)
.expect("Id vecs out of sync");
id
}
/// Get all icons of the world.
pub fn icons(&self) -> &StableVec<Icon> {
&self.icons
}
/// Get all rooms of the world.
pub fn rooms(&self) -> &StableVec<Room> {
&self.rooms
}
/// Get all walls of the world.
pub fn walls(&self) -> &StableVec<Wall> {
&self.walls
}
}
impl Default for World {
fn default() -> Self {
Self::new()
}
}
|