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
use std::{
    clone::Clone,
    collections::{HashMap, HashSet, VecDeque},
    hash::Hash,
    net::SocketAddr,
    time::Duration,
};

use crate::{
    sequence_list::SequenceList,
    world::{
        entity::entity_converters::GlobalWorldManagerType, local_world_manager::LocalWorldManager,
    },
    ComponentKind, DiffMask, EntityAction, HostEntity, Instant, MessageIndex, PacketIndex,
    WorldRefType,
};

use super::{entity_action_event::EntityActionEvent, world_channel::WorldChannel};

const DROP_UPDATE_RTT_FACTOR: f32 = 1.5;
const ACTION_RECORD_TTL: Duration = Duration::from_secs(60);

pub type ActionId = MessageIndex;

/// Manages Entities for a given Client connection and keeps them in
/// sync on the Client
pub struct HostWorldManager<E: Copy + Eq + Hash + Send + Sync> {
    // World
    pub world_channel: WorldChannel<E>,

    // Actions
    pub sent_action_packets: SequenceList<(Instant, Vec<(ActionId, EntityAction<E>)>)>,

    // Updates
    /// Map of component updates and [`DiffMask`] that were written into each packet
    pub sent_updates: HashMap<PacketIndex, (Instant, HashMap<(E, ComponentKind), DiffMask>)>,
    /// Last [`PacketIndex`] where a component update was written by the server
    pub last_update_packet_index: PacketIndex,
}

pub struct HostWorldEvents<E: Copy + Eq + Hash + Send + Sync> {
    pub next_send_actions: VecDeque<(ActionId, EntityActionEvent<E>)>,
    pub next_send_updates: HashMap<E, HashSet<ComponentKind>>,
}

impl<E: Copy + Eq + Hash + Send + Sync> HostWorldEvents<E> {
    pub fn has_events(&self) -> bool {
        !self.next_send_actions.is_empty() || !self.next_send_updates.is_empty()
    }
}

impl<E: Copy + Eq + Hash + Send + Sync> HostWorldManager<E> {
    /// Create a new HostWorldManager, given the client's address
    pub fn new(
        address: &Option<SocketAddr>,
        global_world_manager: &dyn GlobalWorldManagerType<E>,
    ) -> Self {
        HostWorldManager {
            // World
            world_channel: WorldChannel::new(address, global_world_manager),
            sent_action_packets: SequenceList::new(),

            // Update
            sent_updates: HashMap::new(),
            last_update_packet_index: 0,
        }
    }

    // World

    // used when Entity first comes into Connection's scope
    pub fn init_entity(
        &mut self,
        world_manager: &mut LocalWorldManager<E>,
        entity: &E,
        component_kinds: Vec<ComponentKind>,
    ) {
        // add entity
        self.spawn_entity(world_manager, entity, &component_kinds);
        // add components
        for component_kind in component_kinds {
            self.insert_component(entity, &component_kind);
        }
    }

    pub fn spawn_entity(
        &mut self,
        world_manager: &mut LocalWorldManager<E>,
        entity: &E,
        component_kinds: &Vec<ComponentKind>,
    ) {
        self.world_channel
            .host_spawn_entity(world_manager, entity, component_kinds);
    }

    pub fn despawn_entity(&mut self, entity: &E) {
        self.world_channel.host_despawn_entity(entity);
    }

    pub fn client_initiated_despawn(&mut self, entity: &E) {
        self.world_channel.client_initiated_despawn(entity);
    }

    pub fn insert_component(&mut self, entity: &E, component_kind: &ComponentKind) {
        self.world_channel
            .host_insert_component(entity, component_kind);
    }

    pub fn remove_component(&mut self, entity: &E, component_kind: &ComponentKind) {
        self.world_channel
            .host_remove_component(entity, component_kind);
    }

    pub fn host_has_entity(&self, entity: &E) -> bool {
        self.world_channel.host_has_entity(entity)
    }

    // used when Remote Entity gains Write Authority (delegation)
    pub fn track_remote_entity(
        &mut self,
        local_world_manager: &mut LocalWorldManager<E>,
        entity: &E,
        component_kinds: Vec<ComponentKind>,
    ) -> HostEntity {
        // add entity
        let new_host_entity =
            self.world_channel
                .track_remote_entity(local_world_manager, entity, &component_kinds);

        // info!("--- tracking remote entity ---");

        // add components
        for component_kind in component_kinds {
            self.track_remote_component(entity, &component_kind);
        }

        // info!("--- ---------------------- ---");

        new_host_entity
    }

    pub fn untrack_remote_entity(
        &mut self,
        local_world_manager: &mut LocalWorldManager<E>,
        entity: &E,
    ) {
        self.world_channel
            .untrack_remote_entity(local_world_manager, entity);
    }

    pub fn track_remote_component(&mut self, entity: &E, component_kind: &ComponentKind) {
        self.world_channel
            .track_remote_component(entity, component_kind);
    }

    // Messages

    pub fn handle_dropped_packets(&mut self, now: &Instant, rtt_millis: &f32) {
        self.handle_dropped_update_packets(now, rtt_millis);
        self.handle_dropped_action_packets(now);
    }

    // Collecting

    fn handle_dropped_action_packets(&mut self, now: &Instant) {
        let mut pop = false;

        loop {
            if let Some((_, (time_sent, _))) = self.sent_action_packets.front() {
                if time_sent.elapsed(now) > ACTION_RECORD_TTL {
                    pop = true;
                }
            } else {
                return;
            }
            if pop {
                self.sent_action_packets.pop_front();
            } else {
                return;
            }
        }
    }

    fn handle_dropped_update_packets(&mut self, now: &Instant, rtt_millis: &f32) {
        let drop_duration = Duration::from_millis((DROP_UPDATE_RTT_FACTOR * rtt_millis) as u64);

        {
            let mut dropped_packets = Vec::new();
            for (packet_index, (time_sent, _)) in &self.sent_updates {
                if time_sent.elapsed(now) > drop_duration {
                    dropped_packets.push(*packet_index);
                }
            }

            for packet_index in dropped_packets {
                self.dropped_update_cleanup(packet_index);
            }
        }
    }

    fn dropped_update_cleanup(&mut self, dropped_packet_index: PacketIndex) {
        if let Some((_, diff_mask_map)) = self.sent_updates.remove(&dropped_packet_index) {
            for (component_index, diff_mask) in &diff_mask_map {
                let (entity, component) = component_index;
                if !self
                    .world_channel
                    .diff_handler
                    .has_component(entity, component)
                {
                    continue;
                }
                let mut new_diff_mask = diff_mask.clone();

                // walk from dropped packet up to most recently sent packet
                if dropped_packet_index == self.last_update_packet_index {
                    continue;
                }

                let mut packet_index = dropped_packet_index.wrapping_add(1);
                while packet_index != self.last_update_packet_index {
                    if let Some((_, diff_mask_map)) = self.sent_updates.get(&packet_index) {
                        if let Some(next_diff_mask) = diff_mask_map.get(component_index) {
                            new_diff_mask.nand(next_diff_mask);
                        }
                    }

                    packet_index = packet_index.wrapping_add(1);
                }

                self.world_channel
                    .diff_handler
                    .or_diff_mask(entity, component, &new_diff_mask);
            }
        }
    }

    pub fn take_outgoing_events<W: WorldRefType<E>>(
        &mut self,
        world: &W,
        global_world_manager: &dyn GlobalWorldManagerType<E>,
        now: &Instant,
        rtt_millis: &f32,
    ) -> HostWorldEvents<E> {
        HostWorldEvents {
            next_send_actions: self.world_channel.take_next_actions(now, rtt_millis),
            next_send_updates: self
                .world_channel
                .collect_next_updates(world, global_world_manager),
        }
    }
}

impl<E: Copy + Eq + Hash + Send + Sync> HostWorldManager<E> {
    pub fn notify_packet_delivered(
        &mut self,
        packet_index: PacketIndex,
        local_world_manager: &mut LocalWorldManager<E>,
    ) {
        // Updates
        self.sent_updates.remove(&packet_index);

        // Actions
        if let Some((_, action_list)) = self
            .sent_action_packets
            .remove_scan_from_front(&packet_index)
        {
            for (action_id, action) in action_list {
                self.world_channel
                    .action_delivered(local_world_manager, action_id, action);
            }
        }
    }
}