-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathloading.rs
More file actions
175 lines (147 loc) · 5.3 KB
/
loading.rs
File metadata and controls
175 lines (147 loc) · 5.3 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
//! Chunk loading and unloading based on player `View`s.
use std::{
collections::VecDeque,
mem,
time::{Duration, Instant},
};
use ahash::AHashMap;
use base::ChunkPosition;
use ecs::{Entity, SysResult, SystemExecutor};
use quill_common::events::EntityRemoveEvent;
use utils::vec_remove_item;
use crate::{chunk::worker::LoadRequest, events::ViewUpdateEvent, Game};
pub fn register(game: &mut Game, systems: &mut SystemExecutor<Game>) {
game.insert_resource(ChunkLoadState::default());
systems
.group::<ChunkLoadState>()
.add_system(remove_dead_entities)
.add_system(update_tickets_for_players)
.add_system(unload_chunks)
.add_system(load_chunks);
}
/// Amount of time to wait after a chunk has
/// no tickets until it is unloaded.
const UNLOAD_DELAY: Duration = Duration::from_secs(10);
#[derive(Default)]
struct ChunkLoadState {
/// Chunks that have been queued for unloading.
chunk_unload_queue: VecDeque<QueuedChunkUnload>,
chunk_tickets: ChunkTickets,
}
impl ChunkLoadState {
pub fn remove_ticket(&mut self, chunk: ChunkPosition, ticket: Ticket) {
self.chunk_tickets.remove_ticket(chunk, ticket);
// If this was the last ticket, then queue the chunk to be
// unloaded.
if self.chunk_tickets.num_tickets(chunk) == 0 {
self.chunk_tickets.remove_chunk(chunk);
self.chunk_unload_queue
.push_back(QueuedChunkUnload::new(chunk));
}
}
}
#[derive(Copy, Clone, Debug)]
struct QueuedChunkUnload {
pos: ChunkPosition,
/// Time after which the chunk should be unloaded.
unload_at_time: Instant,
}
impl QueuedChunkUnload {
pub fn new(pos: ChunkPosition) -> Self {
Self {
pos,
unload_at_time: Instant::now() + UNLOAD_DELAY,
}
}
}
/// Maintains a list of "tickets" for each loaded chunk.
/// A chunk is queued for unloading when it has no more tickets.
#[derive(Default)]
struct ChunkTickets {
tickets: AHashMap<ChunkPosition, Vec<Ticket>>,
by_entity: AHashMap<Ticket, Vec<ChunkPosition>>,
}
impl ChunkTickets {
pub fn insert_ticket(&mut self, chunk: ChunkPosition, ticket: Ticket) {
self.tickets.entry(chunk).or_default().push(ticket);
self.by_entity.entry(ticket).or_default().push(chunk);
}
pub fn remove_ticket(&mut self, chunk: ChunkPosition, ticket: Ticket) {
if let Some(vec) = self.tickets.get_mut(&chunk) {
vec_remove_item(vec, &ticket);
}
vec_remove_item(self.by_entity.get_mut(&ticket).unwrap(), &chunk);
}
pub fn num_tickets(&self, chunk: ChunkPosition) -> usize {
match self.tickets.get(&chunk) {
Some(vec) => vec.len(),
None => 0,
}
}
pub fn take_entity_tickets(&mut self, ticket: Ticket) -> Vec<ChunkPosition> {
self.by_entity
.get_mut(&ticket)
.map(mem::take)
.unwrap_or_default()
}
pub fn remove_chunk(&mut self, pos: ChunkPosition) {
self.tickets.remove(&pos);
}
}
/// ID of a chunk ticket that keeps a chunk loaded.
///
/// Currently just represents an entity, the player
/// that is keeping this chunk loaded.
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
struct Ticket(Entity);
/// System to populate chunk tickets based on players' views.
fn update_tickets_for_players(game: &mut Game, state: &mut ChunkLoadState) -> SysResult {
for (player, event) in game.ecs.query::<&ViewUpdateEvent>().iter() {
let player_ticket = Ticket(player);
// Remove old tickets
for &old_chunk in &event.old_chunks {
state.remove_ticket(old_chunk, player_ticket);
}
// Create new tickets
for &new_chunk in &event.new_chunks {
state.chunk_tickets.insert_ticket(new_chunk, player_ticket);
// Load if needed
if !game.world.is_chunk_loaded(new_chunk) && !game.world.is_chunk_loading(new_chunk) {
game.world.queue_chunk_load(LoadRequest { pos: new_chunk });
}
}
}
Ok(())
}
/// System to unload chunks from the `ChunkUnloadQueue`.
fn unload_chunks(game: &mut Game, state: &mut ChunkLoadState) -> SysResult {
while let Some(&unload) = state.chunk_unload_queue.get(0) {
if unload.unload_at_time > Instant::now() {
// None of the remaining chunks in the queue are
// ready for unloading, because the queue is ordered
// by time.
break;
}
state.chunk_unload_queue.pop_front();
// If the chunk has acquired new tickets, then abort unloading it.
if state.chunk_tickets.num_tickets(unload.pos) > 0 {
continue;
}
game.world.unload_chunk(unload.pos)?;
}
game.world.cache.purge_unused();
Ok(())
}
fn remove_dead_entities(game: &mut Game, state: &mut ChunkLoadState) -> SysResult {
for (entity, _event) in game.ecs.query::<&EntityRemoveEvent>().iter() {
let entity_ticket = Ticket(entity);
for chunk in state.chunk_tickets.take_entity_tickets(entity_ticket) {
state.remove_ticket(chunk, entity_ticket);
}
}
Ok(())
}
/// System to call `World::load_chunks` each tick
fn load_chunks(game: &mut Game, _state: &mut ChunkLoadState) -> SysResult {
game.world.load_chunks(&mut game.ecs)
}