-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathgame.rs
More file actions
312 lines (272 loc) · 9.85 KB
/
Copy pathgame.rs
File metadata and controls
312 lines (272 loc) · 9.85 KB
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
use crate::network::{Network, ServerToWorkerMessage};
use crate::task::RunningTasks;
use crate::{BlockUpdateEvent, EntityDespawnEvent, Name, PlayerLeaveEvent};
use ahash::AHashMap;
use bumpalo::Bump;
use feather_core::anvil::level::LevelData;
use feather_core::blocks::BlockId;
use feather_core::chunk_map::ChunkMap;
use feather_core::network::Packet;
use feather_core::util::{BlockPosition, ChunkPosition, Position};
use feather_server_config::Config;
use fecs::{Entity, Event, EventHandlers, IntoQuery, OwnedResources, Read, RefResources, World};
use rand::rngs::SmallRng;
use rand::{Rng, SeedableRng};
use smallvec::SmallVec;
use std::cell::{RefCell, RefMut};
use std::fmt::Display;
use std::ops::{Deref, DerefMut};
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use thread_local::CachedThreadLocal;
/// The `Game` resource, which acts as a central bus to bind together
/// the feather-server-* crates. Resources which are accessed frequently,
/// such as the chunk map, are stored in here.
pub struct Game {
pub chunk_map: ChunkMap,
/// Number of ticks since the program started. Can be used
/// to make a system which only runs at a fixed interval.
pub tick_count: u64,
/// Stores entities which have a hold on chunks,
/// preventing the chunk from being unloaded.
pub chunk_holders: ChunkHolders,
/// The server configuration.
pub config: Arc<Config>,
/// The level data.
pub level: LevelData,
/// Associates chunks with the entities that reside in them. Used
/// as an acceleration structure for spacial lookups.
pub chunk_entities: ChunkEntities,
/// World time, in the Minecraft way.
pub time: Time,
/// Server task manager, which allows executing futures
/// which will not be interrupted on shutdown.
pub running_tasks: RunningTasks,
/// The event handler map.
pub event_handlers: Arc<EventHandlers>,
/// Resources other than `Game`, used to run event handlers.
pub resources: Arc<OwnedResources>,
/// General-purpose, non-cryptographic random number generator
pub rng: CachedThreadLocal<RefCell<SmallRng>>,
/// Shared bump allocator, reset each tick.
pub bump: CachedThreadLocal<Bump>,
/// The server player count.
pub player_count: Arc<AtomicU32>,
}
impl Game {
/// Handles an event or message. All handlers
/// for the given event will be run.
pub fn handle(&mut self, world: &mut World, event: impl Event) {
// TODO: optimize this by avoiding Rc clone.
let resources = Arc::clone(&self.resources);
let event_handlers = Arc::clone(&self.event_handlers);
let resources = RefResources::new(Arc::as_ref(&resources), (self,));
event_handlers.trigger(&resources, world, event);
}
/// Retrieves the block at the given position,
/// or `None` if the block's chunk is not loaded.
pub fn block_at(&self, pos: BlockPosition) -> Option<BlockId> {
self.chunk_map.block_at(pos)
}
/// Sets the block at the given position.
///
/// If the block's chunk's is not loaded, returns `false`;
/// otherwise, returns `true`.
pub fn set_block_at(&mut self, world: &mut World, pos: BlockPosition, block: BlockId) -> bool {
let old = match self.block_at(pos) {
Some(block) => block,
None => return false,
};
let result = self.chunk_map.set_block_at(pos, block);
self.handle(
world,
BlockUpdateEvent {
pos,
old,
new: block,
},
);
result
}
/// Returns a bump allocator.
pub fn bump(&self) -> &Bump {
self.bump.get_or_default()
}
/// Returns a random number generator.
pub fn rng(&self) -> RefMut<impl Rng> {
self.rng
.get_or(|| RefCell::new(SmallRng::from_entropy()))
.borrow_mut()
}
/// Despawns an entity. This should be used instead of `World::despawn`
/// as it properly handles events.
pub fn despawn(&mut self, entity: Entity, world: &mut World) {
self.handle(world, EntityDespawnEvent { entity });
world.despawn(entity);
}
/// Disconnects a player.
pub fn disconnect(&mut self, player: Entity, world: &mut World, reason: impl Display) {
let network = world.get::<Network>(player);
let name = world.get::<Name>(player);
log::info!("{} disconnected: {}", name.0, reason);
let _ = network.tx.send(ServerToWorkerMessage::Disconnect);
drop(name);
drop(network);
self.player_count.fetch_sub(1, Ordering::AcqRel);
self.handle(world, PlayerLeaveEvent { player });
self.despawn(player, world);
}
/* BROADCAST FUNCTIONS */
/// Broadcasts a packet to all online players.
pub fn broadcast_global(&self, world: &World, packet: impl Packet, neq: Option<Entity>) {
self.broadcast_global_boxed(world, Box::new(packet), neq);
}
/// Broadcasts a boxed packet to all online players.
pub fn broadcast_global_boxed(
&self,
world: &World,
packet: Box<dyn Packet>,
neq: Option<Entity>,
) {
for (entity, network) in <Read<Network>>::query().iter_entities(world.inner()) {
if neq.map(|neq| neq == entity).unwrap_or(false) {
continue;
}
network.send_boxed(packet.box_clone());
}
}
/// Broadcasts a packet to all players able to see a given chunk.
pub fn broadcast_chunk_update(
&self,
world: &World,
packet: impl Packet,
chunk: ChunkPosition,
neq: Option<Entity>,
) {
self.broadcast_chunk_update_boxed(world, Box::new(packet), chunk, neq);
}
/// Broadcasts a boxed packet to all players able to see a given chunk.
pub fn broadcast_chunk_update_boxed(
&self,
world: &World,
packet: Box<dyn Packet>,
chunk: ChunkPosition,
neq: Option<Entity>,
) {
// we can use the chunk holders structure to accelerate this
for entity in self.chunk_holders.holders_for(chunk) {
if neq.map(|neq| neq == *entity).unwrap_or(false) {
continue;
}
if let Some(network) = world.try_get::<Network>(*entity) {
network.send_boxed(packet.box_clone());
}
}
}
/// Broadcasts a packet to all players able to see a given entity.
pub fn broadcast_entity_update(
&self,
world: &World,
packet: impl Packet,
entity: Entity,
neq: Option<Entity>,
) {
self.broadcast_entity_update_boxed(world, Box::new(packet), entity, neq);
}
/// Broadcasts a boxed packet to all players able to see a given entity.
pub fn broadcast_entity_update_boxed(
&self,
world: &World,
packet: Box<dyn Packet>,
entity: Entity,
neq: Option<Entity>,
) {
// Send the packet to all players who have a hold on the entity's chunk.
let entity_chunk = world.get::<Position>(entity).chunk();
self.broadcast_chunk_update_boxed(world, packet, entity_chunk, neq);
}
}
/// The chunk holder map contains a mapping
/// of chunk positions to any number of entities, called "holders."
/// When a chunk position has no holders, it will be queued
/// for unloading.
///
/// In addition, the chunk holders map can be used to select
/// which players to broadcast an entity movement to: a player
/// who has a chunk hold on the entity's chunk would be able to see
/// the movement, while other players would be outside of the view
/// distance. This technique allows for higher performance and
/// avoids constant nearby entity queries.
#[derive(Default, Clone, Debug)]
pub struct ChunkHolders {
pub inner: AHashMap<ChunkPosition, SmallVec<[Entity; 4]>>,
}
impl ChunkHolders {
pub fn holders_for(&self, chunk: ChunkPosition) -> &[Entity] {
self.inner
.get(&chunk)
.map(SmallVec::as_slice)
.unwrap_or(&[])
}
pub fn chunk_has_holders(&self, chunk: ChunkPosition) -> bool {
let holders = self.holders_for(chunk);
!holders.is_empty()
}
pub fn insert_holder(&mut self, chunk: ChunkPosition, holder: Entity) {
self.inner.entry(chunk).or_default().push(holder)
}
}
/// Stores which entities belong to every given chunk.
///
/// This data structure can be used to accelerate certain
/// operations, such as querying for entities
/// within some distance of a position. In addition,
/// it can be used to send all entities in a chunk
/// to a player.
///
/// Do note that the information in this structure is not necessarily up to date,
/// although a best effort is made to update the data.
#[derive(Default)]
pub struct ChunkEntities(pub AHashMap<ChunkPosition, SmallVec<[Entity; 4]>>);
impl ChunkEntities {
pub fn new() -> Self {
Self::default()
}
/// Returns a slice of entities in the given chunk.
pub fn entities_in_chunk(&self, chunk: ChunkPosition) -> &[Entity] {
self.0.get(&chunk).map(|vec| vec.as_slice()).unwrap_or(&[])
}
}
/// The current time of the world.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Time(pub u64);
impl Deref for Time {
type Target = u64;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for Time {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl Time {
/// Returns the time of day. This is calculated
/// as `time.0 % 24_000`.
pub fn time_of_day(self) -> u64 {
self.0 % 24_000
}
/// Returns the age of the world in ticks. Equivalent to `time.0`.
pub fn world_age(self) -> u64 {
self.0
}
}
#[fecs::system]
pub fn reset_bump_allocators(game: &mut Game) {
game.bump.iter_mut().for_each(Bump::reset);
}
#[fecs::system]
pub fn increment_tick_count(game: &mut Game) {
game.tick_count += 1;
}