perovskite_client 0.2.1

Multiplayer voxel game written in Rust - Game client
Documentation
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
use crate::{
    client_state::{
        entities::GameEntity, items::ClientInventory, ClientState, FastChunkNeighbors, GameAction,
    },
    net_client::{MAX_PROTOCOL_VERSION, MIN_PROTOCOL_VERSION},
};
use anyhow::{anyhow, ensure, Context, Result};
use cgmath::{vec3, InnerSpace, Vector3, Zero};
use futures::StreamExt;
use log::warn;
use parking_lot::Mutex;
use perovskite_core::{
    chat::ChatMessage,
    coordinates::{BlockCoordinate, ChunkCoordinate, PlayerPositionUpdate},
    protocol::entities as entities_proto,
    protocol::game_rpc::{self as rpc, InteractKeyAction, StreamToClient, StreamToServer},
};
use prost::Message;
use std::ops::Deref;
use std::sync::atomic::{AtomicU64, Ordering};
use std::{
    backtrace,
    collections::{hash_map::Entry, HashMap},
    sync::Arc,
    time::{Duration, Instant},
};

use super::mesh_worker::{
    propagate_neighbor_data, MeshBatcher, MeshWorker, NeighborPropagationScratchpad,
    NeighborPropagator,
};
use crate::audio::{
    EvictedAudioHealer, SimpleSoundControlBlock, SOUND_MOVESPEED_ENABLED, SOUND_PRESENT,
    SOUND_SQUARELAW_ENABLED,
};
use crate::client_state::input::BoundAction;
use crate::client_state::ClientPerformanceMetrics;
use perovskite_core::block_id::BlockId;
use perovskite_core::protocol::game_rpc::Footstep;
use perovskite_core::protocol::map::StoredChunk;
use tokio::sync::mpsc;
use tokio::task::spawn_blocking;
use tokio_util::sync::CancellationToken;
use tonic::Streaming;
use tracy_client::{plot, span};

struct SharedState {
    protocol_version: u32,
    // Some messages are sent straight from the inbound context, namely protocol bugchecks
    outbound_tx: mpsc::Sender<StreamToServer>,
    client_state: Arc<ClientState>,
    ack_map: Mutex<HashMap<u64, Instant>>,
    mesh_workers: Vec<Arc<MeshWorker>>,
    neighbor_propagators: Vec<Arc<NeighborPropagator>>,
    audio_healer: Arc<EvictedAudioHealer>,

    batcher: Arc<MeshBatcher>,
    initial_state_notification: Arc<tokio::sync::Notify>,
    cancellation: CancellationToken,

    latest_rx_tick: AtomicU64,
}
impl SharedState {
    async fn send_bugcheck(&self, description: String) -> Result<()> {
        log::error!("Protocol bugcheck: {}", description);
        self.outbound_tx
            .send(StreamToServer {
                sequence: 0,
                client_tick: 0,
                client_message: Some(rpc::stream_to_server::ClientMessage::BugCheck(
                    rpc::ClientBugCheck {
                        description,
                        backtrace: backtrace::Backtrace::force_capture().to_string(),
                        protocol_version: self.protocol_version,
                        min_protocol_version: MIN_PROTOCOL_VERSION,
                        max_protocol_version: MAX_PROTOCOL_VERSION,
                    },
                )),
                want_performance_metrics: self
                    .client_state
                    .want_server_perf
                    .load(Ordering::Relaxed),
            })
            .await?;
        Ok(())
    }
}

pub(crate) async fn make_contexts(
    client_state: Arc<ClientState>,
    tx_send: mpsc::Sender<StreamToServer>,
    stream: Streaming<StreamToClient>,
    action_receiver: mpsc::Receiver<GameAction>,
    // Not yet used, only one protocol version is supported
    protocol_version: u32,
    initial_state_notification: Arc<tokio::sync::Notify>,
) -> Result<(InboundContext, OutboundContext)> {
    let cancellation = client_state.shutdown.clone();

    let mut mesh_workers = vec![];
    let mesh_worker_handles = futures::stream::FuturesUnordered::new();
    for _ in 0..client_state.settings.load().render.num_mesh_workers {
        let (worker, handle) = MeshWorker::new(client_state.clone());
        mesh_workers.push(worker);
        mesh_worker_handles.push(handle);
    }
    let mut neighbor_propagators = vec![];
    let neighbor_propagator_handles = futures::stream::FuturesUnordered::new();
    for _ in 0..client_state.settings.load().render.num_neighbor_propagators {
        let (worker, handle) = NeighborPropagator::new(client_state.clone(), mesh_workers.clone());
        neighbor_propagators.push(worker);
        neighbor_propagator_handles.push(handle);
    }

    let state_clone = client_state.clone();
    let cancel_clone = cancellation.clone();
    let raytrace_worker_handle = spawn_blocking(|| do_raytrace_work(state_clone, cancel_clone));
    let (batcher, batcher_handle) = MeshBatcher::new(client_state.clone());

    let (audio_healer, audio_healer_handle) = EvictedAudioHealer::new(client_state.clone());

    let shared_state = Arc::new(SharedState {
        protocol_version,
        outbound_tx: tx_send,
        client_state,
        ack_map: Mutex::new(HashMap::new()),
        mesh_workers,
        neighbor_propagators,
        batcher,
        initial_state_notification,
        audio_healer,
        cancellation,
        latest_rx_tick: AtomicU64::new(0),
    });

    tokio::task::spawn(stats_loop(shared_state.clone()));

    let inbound = InboundContext {
        inbound_rx: stream,
        shared_state: shared_state.clone(),
        mesh_worker_handles,
        neighbor_propagator_handles,
        batcher_handle,

        snappy_helper: SnappyDecodeHelper::new(),
        inline_nprop_scratchpad: NeighborPropagationScratchpad::default(),
        inline_fcn_scratchpad: FastChunkNeighbors::default(),
        audio_healer_handle,
        raytrace_worker_handle,
    };

    let outbound = OutboundContext {
        shared_state,
        sequence: 1,
        action_receiver,
        last_pos_update_seq: None,
    };

    Ok((inbound, outbound))
}

async fn stats_loop(shared_state: Arc<SharedState>) {
    while !shared_state.cancellation.is_cancelled() {
        tokio::select! {
            _ = shared_state.cancellation.cancelled() => { break; }
            _ = shared_state.client_state.want_new_client_perf.notified() => {}
        }
        let mut new_state = ClientPerformanceMetrics::default();
        for nprop in &shared_state.neighbor_propagators {
            new_state.nprop_queue_lens.push(nprop.queue_len() as u64)
        }
        for mesher in &shared_state.mesh_workers {
            new_state.mesh_queue_lens.push(mesher.queue_len() as u64)
        }
        *shared_state.client_state.client_perf.lock() = Some(new_state);
    }
}

fn do_raytrace_work(client_state: Arc<ClientState>, cancellation: CancellationToken) -> Result<()> {
    tracy_client::set_thread_name!("raytrace_worker");
    while !cancellation.is_cancelled() {
        let next_wakeup = Instant::now() + Duration::from_millis(10);
        let should_raytrace = {
            let settings = client_state.settings.load();
            settings.render.raytracing
                && (!settings.render.on_demand_raytracing
                    || client_state
                        .input
                        .lock()
                        .take_just_pressed(BoundAction::AuxDebugKey))
        };
        if should_raytrace {
            client_state
                .chunks
                .raytrace_buffers()
                .do_rebuild(client_state.chunks.renderable_chunks_cloned_view())?;
        }
        if let Some(remaining) = next_wakeup.checked_duration_since(Instant::now()) {
            std::thread::sleep(remaining);
        }
    }
    Ok(())
}

pub(crate) struct OutboundContext {
    sequence: u64,

    action_receiver: mpsc::Receiver<GameAction>,
    last_pos_update_seq: Option<u64>,
    shared_state: Arc<SharedState>,
}
impl OutboundContext {
    async fn send_sequenced_message(
        &mut self,
        message: rpc::stream_to_server::ClientMessage,
    ) -> Result<u64> {
        // todo tick
        let start_time = Instant::now();
        self.sequence += 1;
        self.shared_state
            .ack_map
            .lock()
            .insert(self.sequence, Instant::now());
        self.shared_state
            .outbound_tx
            .send(StreamToServer {
                sequence: self.sequence,
                client_tick: 0,
                client_message: Some(message),
                want_performance_metrics: self
                    .shared_state
                    .client_state
                    .want_server_perf
                    .load(Ordering::Relaxed),
            })
            .await?;
        let now = Instant::now();
        plot!("send_sequnced wait time", (now - start_time).as_secs_f64());
        Ok(self.sequence)
    }

    /// Send a position update to the server
    async fn send_position_update(&mut self, pos: PlayerPositionUpdate) -> Result<()> {
        if let Some(last_pos_send) = self
            .last_pos_update_seq
            .and_then(|x| self.shared_state.ack_map.lock().get(&x).copied())
        {
            // We haven't gotten an ack for the last pos update; withhold the current one
            let delay = Instant::now() - last_pos_send;
            if delay > Duration::from_secs_f64(0.25) {
                log::warn!("Waiting {delay:?} for a position update");
            }
            plot!("pos_update_wait", delay.as_secs_f64());
            // todo send an update but signal that we don't want chunks as far as AIMD/pacing on
            // the server
            return Ok(());
        } else {
            plot!("pos_update_wait", 0.);
        }

        // If this overflows, the client is severely behind (by 4 billion chunks!) and may as well crash
        let pending_chunks = self
            .shared_state
            .mesh_workers
            .iter()
            .map(|worker| worker.queue_len())
            .sum::<usize>()
            .max(
                self.shared_state
                    .neighbor_propagators
                    .iter()
                    .map(|x| x.queue_len())
                    .sum::<usize>(),
            )
            .try_into()
            .unwrap();
        let hotbar_slot = self.shared_state.client_state.hud.lock().hotbar_slot;

        let animation_state = self
            .shared_state
            .client_state
            .physics_state
            .lock()
            .take_animation_state();
        let sequence = self
            .send_sequenced_message(rpc::stream_to_server::ClientMessage::PositionUpdate(
                rpc::ClientUpdate {
                    position: Some(pos.to_proto()?),
                    pacing: Some(rpc::ClientPacing {
                        pending_chunks,
                        distance_limit: self
                            .shared_state
                            .client_state
                            .render_distance
                            .load(Ordering::Relaxed),
                        latest_tick_message: self
                            .shared_state
                            .latest_rx_tick
                            .load(Ordering::Relaxed),
                    }),
                    hotbar_slot,
                    footstep_coordinate: animation_state
                        .footstep_coord
                        .into_iter()
                        .map(|(tick, coord)| Footstep {
                            coord: Some(coord.into()),
                            tick,
                        })
                        .collect(),
                },
            ))
            .await?;
        self.last_pos_update_seq = Some(sequence);
        Ok(())
    }

    async fn handle_game_action(&mut self, action: GameAction) -> Result<()> {
        self.send_position_update(self.shared_state.client_state.last_position())
            .await?;
        match action {
            GameAction::Dig(action) => {
                self.send_sequenced_message(rpc::stream_to_server::ClientMessage::Dig(
                    rpc::DigTapAction {
                        action_target: Some(action.target.into()),
                        prev_coord: action.prev.map(|x| x.into()),
                        item_slot: action.item_slot,
                        position: Some(action.player_pos.to_proto()?),
                    },
                ))
                .await?;
            }
            GameAction::Tap(action) => {
                self.send_sequenced_message(rpc::stream_to_server::ClientMessage::Tap(
                    rpc::DigTapAction {
                        action_target: Some(action.target.into()),
                        prev_coord: action.prev.map(|x| x.into()),
                        item_slot: action.item_slot,
                        position: Some(action.player_pos.to_proto()?),
                    },
                ))
                .await?;
            }
            GameAction::Place(action) => {
                self.send_sequenced_message(rpc::stream_to_server::ClientMessage::Place(
                    rpc::PlaceAction {
                        block_coord: action.target.map(Into::into),
                        place_anchor: Some(action.anchor.into()),
                        item_slot: action.item_slot,
                        position: Some(action.player_pos.to_proto()?),
                    },
                ))
                .await?;
            }
            GameAction::Inventory(action) => {
                self.send_sequenced_message(rpc::stream_to_server::ClientMessage::Inventory(
                    rpc::InventoryAction {
                        source_view: action.source_view,
                        source_slot: action.source_slot.try_into()?,
                        destination_view: action.destination_view,
                        destination_slot: action.destination_slot.try_into()?,
                        count: action.count,
                        swap: action.swap,
                    },
                ))
                .await?;
            }
            GameAction::PopupResponse(popup_response) => {
                self.send_sequenced_message(rpc::stream_to_server::ClientMessage::PopupResponse(
                    popup_response,
                ))
                .await?;
            }
            GameAction::InteractKey(action) => {
                self.send_sequenced_message(rpc::stream_to_server::ClientMessage::InteractKey(
                    InteractKeyAction {
                        interaction_target: Some(action.target.into()),
                        position: Some(action.player_pos.to_proto()?),
                        item_slot: action.item_slot,
                        menu_entry: action.menu_entry,
                    },
                ))
                .await?;
            }
            GameAction::ChatMessage(message) => {
                self.send_sequenced_message(rpc::stream_to_server::ClientMessage::ChatMessage(
                    message,
                ))
                .await?;
            }
        }
        Ok(())
    }

    pub(crate) async fn run_outbound_loop(&mut self) -> Result<()> {
        let mut position_tx_timer = tokio::time::interval(Duration::from_secs_f64(0.1));
        position_tx_timer.set_missed_tick_behavior(tokio::time::MissedTickBehavior::Delay);
        while !self.shared_state.cancellation.is_cancelled() {
            tokio::select! {
                _ = position_tx_timer.tick() => {
                    // TODO send updates at a lower rate if substantially unchanged
                    self.send_position_update(self.shared_state.client_state.last_position()).await?;
                },

                _ = self.shared_state.cancellation.cancelled() => {
                    log::info!("Outbound stream context detected cancellation and shutting down")
                    // pass
                }

                action = self.action_receiver.recv() => {
                    match action {
                        Some(x) => self.handle_game_action(x).await?,
                        None => {
                            log::warn!("Action sender closed, shutting down outbound loop");
                            self.shared_state.cancellation.cancel();
                        }
                    }
                }
            }
        }
        log::warn!("Exiting outbound loop");
        Ok(())
    }
}

pub(crate) struct InboundContext {
    inbound_rx: Streaming<StreamToClient>,
    shared_state: Arc<SharedState>,

    mesh_worker_handles: futures::stream::FuturesUnordered<tokio::task::JoinHandle<Result<()>>>,
    neighbor_propagator_handles:
        futures::stream::FuturesUnordered<tokio::task::JoinHandle<Result<()>>>,
    batcher_handle: tokio::task::JoinHandle<Result<()>>,

    snappy_helper: SnappyDecodeHelper,
    inline_nprop_scratchpad: NeighborPropagationScratchpad,
    inline_fcn_scratchpad: FastChunkNeighbors,

    audio_healer_handle: tokio::task::JoinHandle<Result<()>>,
    raytrace_worker_handle: tokio::task::JoinHandle<Result<()>>,
}
impl InboundContext {
    pub(crate) async fn run_inbound_loop(&mut self) -> Result<()> {
        while !self.shared_state.cancellation.is_cancelled() {
            tokio::select! {
                message = self.inbound_rx.message() => {
                    match message {
                        Err(e) => {
                            log::warn!("Server sent an error: {:?}", e);
                            *self.shared_state.client_state.pending_error.lock() = Some(anyhow::Error::from(e.clone()));
                            return Err(e.into());
                        },
                        Ok(None) => {
                            log::info!("Server disconnected");
                            let mut pending_error = self.shared_state.client_state.pending_error.lock();
                            if pending_error.is_none() {
                                *pending_error = Some(anyhow!("Server disconnected unexpectedly without sending a detailed error message"));
                            }

                            self.shared_state.cancellation.cancel();
                        }
                        Ok(Some(mut message)) => {
                            match self.handle_message(&mut message).await {
                                Ok(_) => {},
                                Err(e) => {
                                    log::warn!("Client failed to handle message: {:?}, error: {:?}", message, e);
                                },
                            }
                        }
                    }

                }
                _ = self.shared_state.cancellation.cancelled() => {
                    log::info!("Inbound stream context detected cancellation and shutting down")
                    // pass
                },
                result = &mut self.mesh_worker_handles.next() => {
                    match result {
                        Some(Err(e)) => {
                            log::error!("Error awaiting mesh worker: {e:?}");
                            return Err(e.into());
                        }
                        Some(Ok(Err(e))) => {
                            log::error!("Mesh worker crashed: {e:?}");
                            return Err(e.into());
                        }
                        Some(Ok(_)) => {
                            log::info!("Mesh worker exiting");
                        }
                        None => {
                            log::error!("Mesh worker exited unexpectedly");
                        }
                    }
                    break;
                }
                result = &mut self.neighbor_propagator_handles.next() => {
                    match result {
                        Some(Err(e)) => {
                            log::error!("Error awaiting neighbor propagator: {e:?}");
                            return Err(e.into());
                        }
                        Some(Ok(Err(e))) => {
                            log::error!("Neighbor propagator crashed: {e:?}");
                            return Err(e.into());
                        }
                        Some(Ok(_)) => {
                            log::info!("Neighbor propagator exiting");
                        }
                        None => {
                            log::error!("Neighbor propagator exited unexpectedly");
                        }
                    }
                    break;
                },
                result = &mut self.batcher_handle => {
                    match result {
                        Ok(Err(e)) => {
                            log::error!("Mesh batcher crashed: {e:?}");
                            return Err(e.into());
                        },
                        Ok(Ok(_)) => {
                            log::info!("Mesh batcher exiting");
                        },
                        Err(e) => {
                            log::error!("Error awaiting mesh batcher: {e:?}");
                            return Err(e.into());
                        }
                    }
                    break;
                },
                result = &mut self.audio_healer_handle => {
                    match result {
                        Ok(Err(e)) => {
                            log::error!("Audio healer crashed: {e:?}");
                            return Err(e.into());
                        },
                        Ok(Ok(_)) => {
                            log::info!("Audio healer exiting");
                        },
                        Err(e) => {
                            log::error!("Error awaiting audio healer: {e:?}");
                            return Err(e.into());
                        }
                    }
                    break;
                },
                result = &mut self.raytrace_worker_handle => {
                    match result {
                        Ok(Err(e)) => {
                            log::error!("Raytrace worker crashed: {e:?}");
                            return Err(e.into());
                        },
                        Ok(Ok(_)) => {
                            log::info!("Raytrace worker exiting");
                        },
                        Err(e) => {
                            log::error!("Error awaiting raytrace worker: {e:?}");
                            return Err(e.into());
                        }
                    }
                }
            }
        }
        log::warn!("Exiting inbound loop");
        // Notify the mesh worker so it can exit soon
        for worker in self.shared_state.mesh_workers.iter() {
            worker.cancel();
        }
        for worker in self.shared_state.neighbor_propagators.iter() {
            worker.cancel();
        }
        self.shared_state.batcher.cancel();
        Ok(())
    }
    async fn handle_message(&mut self, message: &mut StreamToClient) -> Result<()> {
        if message.tick == 0 {
            log::warn!("Got message with tick 0");
        } else {
            self.shared_state
                .client_state
                .timekeeper
                .update_error(message.tick);
            let old_tick = self
                .shared_state
                .latest_rx_tick
                .swap(message.tick, Ordering::Relaxed);
            // It's tempting to check whether old_tick > message.tick. For protocol version 9, this
            // is actually a wrong check; the server-side prioritizer does interleave messages from
            // different sources
        }
        *self.shared_state.client_state.server_perf.lock() = message.performance_metrics.clone();
        match &mut message.server_message {
            None => {
                log::warn!("Got empty message from server");
            }
            Some(rpc::stream_to_client::ServerMessage::HandledSequence(sequence)) => {
                self.handle_ack(*sequence).await?;
            }
            Some(rpc::stream_to_client::ServerMessage::MapChunk(chunk)) => {
                self.handle_mapchunk(chunk).await?;
            }
            Some(rpc::stream_to_client::ServerMessage::MapChunkUnsubscribe(unsub)) => {
                self.handle_unsubscribe(unsub).await?;
            }
            Some(rpc::stream_to_client::ServerMessage::MapDeltaUpdate(delta_update)) => {
                self.handle_map_delta_update(delta_update).await?;
            }
            Some(rpc::stream_to_client::ServerMessage::InventoryUpdate(inventory_update)) => {
                self.handle_inventory_update(inventory_update).await?;
            }
            Some(rpc::stream_to_client::ServerMessage::ClientState(client_state)) => {
                self.handle_client_state_update(client_state).await?;
            }
            Some(rpc::stream_to_client::ServerMessage::ShowPopup(popup_desc)) => {
                self.shared_state
                    .client_state
                    .egui
                    .lock()
                    .show_popup(popup_desc);
            }
            Some(rpc::stream_to_client::ServerMessage::ChatMessage(message)) => self
                .shared_state
                .client_state
                .chat
                .lock()
                .message_history
                .push(
                    ChatMessage::new(&message.origin, &message.message)
                        .with_color_fixed32(message.color_argb),
                ),
            Some(rpc::stream_to_client::ServerMessage::ShutdownMessage(msg)) => {
                *self.shared_state.client_state.pending_error.lock() =
                    Some(anyhow!("Server shutdown message: {}", &msg));
            }
            Some(rpc::stream_to_client::ServerMessage::EntityUpdate(update)) => {
                let estimated_send_time = self
                    .shared_state
                    .client_state
                    .timekeeper
                    .adjust_server_tick(message.tick);
                self.handle_entity_update(update, estimated_send_time)
                    .await?;
            }
            Some(rpc::stream_to_client::ServerMessage::PlaySampledSound(sound)) => {
                let position = match sound.position {
                    Some(p) => p.try_into()?,
                    None => {
                        ensure!(sound.disable_doppler && sound.disable_falloff);
                        Vector3::zero()
                    }
                };
                let tick_now = self.shared_state.client_state.timekeeper.now();
                let tick = if sound.tick == 0 {
                    tick_now
                } else {
                    sound.tick
                };

                let mut flags = SOUND_PRESENT;
                if !sound.disable_doppler {
                    flags |= SOUND_MOVESPEED_ENABLED;
                };
                if !sound.disable_falloff {
                    flags |= SOUND_SQUARELAW_ENABLED;
                };
                let control_block = SimpleSoundControlBlock {
                    flags,
                    position,
                    volume: sound.volume.clamp(0.0, 1.0),
                    start_tick: tick,
                    id: sound.sound_id,
                    end_tick: tick
                        + self
                            .shared_state
                            .client_state
                            .audio
                            .sampled_sound_length(sound.sound_id)
                            .unwrap_or(0),
                    source: sound.source(),
                };
                self.shared_state
                    .client_state
                    .audio
                    .insert_or_update_simple_sound(
                        tick_now,
                        self.shared_state
                            .client_state
                            .weakly_ordered_last_position()
                            .position,
                        control_block,
                        None,
                    );
            }
            Some(_) => {
                log::warn!("Unimplemented server->client message {:?}", message);
            }
        }
        Ok(())
    }

    fn enqueue_for_nprop(&self, coord: ChunkCoordinate) {
        self.shared_state.neighbor_propagators
            [coord.hash_u64() as usize % self.shared_state.neighbor_propagators.len()]
        .enqueue(coord);
    }

    fn enqueue_for_meshing(&self, coord: ChunkCoordinate) {
        self.shared_state.mesh_workers
            [coord.hash_u64() as usize % self.shared_state.mesh_workers.len()]
        .enqueue(coord);
    }

    async fn handle_mapchunk(&mut self, chunk: &mut rpc::MapChunk) -> Result<()> {
        match &chunk.chunk_coord {
            Some(coord) => {
                tokio::task::block_in_place(|| {
                    let _span = span!("handle_mapchunk");
                    let coord = coord.into();

                    let data = self
                        .snappy_helper
                        .decode::<StoredChunk>(&chunk.snappy_encoded_bytes)?
                        .chunk_data
                        .take()
                        .with_context(|| "inner chunk_data missing")?;
                    let (block_ids, ced) = match data {
                        perovskite_core::protocol::map::stored_chunk::ChunkData::V1(v1_data) => {
                            ensure!(v1_data.block_ids.len() == 4096);
                            (v1_data.block_ids, v1_data.client_extended_data)
                        }
                    };
                    let extra_chunks = self.shared_state.client_state.chunks.insert_or_update(
                        &self.shared_state.client_state,
                        coord,
                        block_ids.deref().try_into().unwrap(),
                        ced,
                        &self.shared_state.client_state.block_types,
                    )?;

                    self.enqueue_for_nprop(coord);

                    for i in -1..=1 {
                        for j in (-1 - extra_chunks as i32)..=1 {
                            for k in -1..=1 {
                                if let Some(neighbor) = coord.try_delta(i, j, k) {
                                    self.enqueue_for_nprop(neighbor);
                                }
                            }
                        }
                    }
                    Ok::<(), anyhow::Error>(())
                })?;
            }
            None => {
                self.shared_state
                    .send_bugcheck("Got chunk without a coordinate".to_string())
                    .await?;
            }
        };
        Ok(())
    }

    async fn handle_ack(&mut self, seq: u64) -> Result<()> {
        let send_time = self.shared_state.ack_map.lock().remove(&seq);
        match send_time {
            Some(time) => {
                // todo track in a histogram
                //log::info!("Seq {} took {:?}", seq, Instant::now() - time)
                plot!("ack_rtt", (Instant::now() - time).as_secs_f64());
            }
            None => {
                let desc = format!("got ack for seq {} which we didn't send", seq);
                self.shared_state.send_bugcheck(desc).await?;
            }
        }
        Ok(())
    }
    async fn handle_unsubscribe(&mut self, unsub: &rpc::MapChunkUnsubscribe) -> Result<()> {
        // TODO hold more old chunks (possibly LRU) to provide a higher render distance
        let mut bad_coords = vec![];
        let mut chunk_lock = self.shared_state.client_state.chunks.chunk_lock();
        for coord in unsub.chunk_coord.iter() {
            let coord = coord.into();
            match self
                .shared_state
                .client_state
                .chunks
                .remove_locked(&coord, &mut chunk_lock)
            {
                Some(_x) => {
                    self.shared_state
                        .client_state
                        .world_audio
                        .lock()
                        .remove_chunk(coord);
                }
                None => {
                    bad_coords.push(coord.clone());
                }
            }
        }
        if !bad_coords.is_empty() {
            self.shared_state
                .send_bugcheck(format!(
                    "Asked to unsubscribe to chunks we never subscribed to: {:?}",
                    bad_coords
                ))
                .await?
        }
        Ok(())
    }

    async fn handle_map_delta_update(
        &mut self,
        batch: &mut rpc::MapDeltaUpdateBatch,
    ) -> Result<()> {
        let (missing_coord, unknown_coords) =
            tokio::task::block_in_place(|| self.map_delta_update_sync(batch))?;
        if missing_coord {
            self.shared_state
                .send_bugcheck("Missing block_coord in delta update".to_string())
                .await?;
        }
        // In the latest concurrency model, this is not a bug - it's a more conservative/harmless
        // outcome of a race condition.
        for coord in unknown_coords {
            warn!("Received coord {coord:?} is not in cache. This is fine if it happens occasionally, but if it happens constantly, it suggests either a bug or severe server overload.")
        }

        Ok(())
    }

    fn apply_map_audio_delta_batch(&self, batch: &rpc::MapDeltaUpdateBatch) {
        let tick = self.shared_state.client_state.timekeeper.now();
        let pos = self
            .shared_state
            .client_state
            .weakly_ordered_last_position();
        let block_types = self.shared_state.client_state.block_types.deref();
        let mut map_sound = self.shared_state.client_state.world_audio.lock();

        for entry in &batch.updates {
            let coord: BlockCoordinate = match entry.block_coord {
                None => {
                    // The 3d rendering map will complain again, we can be silent here
                    continue;
                }
                Some(c) => c.into(),
            };
            match block_types.block_sound(BlockId::from(entry.new_id)) {
                None => map_sound.remove(coord),
                Some((id, vol)) => {
                    map_sound.insert_or_update(tick, pos.position, coord, id.get(), vol)
                }
            }
        }
    }

    fn map_delta_update_sync(
        &mut self,
        batch: &mut rpc::MapDeltaUpdateBatch,
    ) -> Result<(bool, Vec<BlockCoordinate>), anyhow::Error> {
        self.apply_map_audio_delta_batch(&batch);

        let (needs_remesh, unknown_coords, missing_coord) =
            self.shared_state
                .client_state
                .chunks
                .apply_delta_batch(batch, &self.shared_state.client_state.block_types)?;

        {
            let _span = span!("remesh for delta");

            // Only do inline meshing for chunks within 3 chunks of the current player location
            // Otherwise, we tie up the network thread for too long
            let current_position = self.shared_state.client_state.last_position().position;
            let eligible_for_inline = |coord: ChunkCoordinate| {
                let base = vec3(
                    coord.x as f64 * 16.0 + 8.0,
                    coord.y as f64 * 16.0 + 8.0,
                    coord.z as f64 * 16.0 + 8.0,
                );
                (base - current_position).magnitude2() < (48.0 * 48.0)
            };
            for &coord in needs_remesh.iter() {
                if eligible_for_inline(coord) {
                    self.shared_state.client_state.chunks.cloned_neighbors_fast(
                        coord,
                        &self.shared_state.client_state.block_types,
                        &mut self.inline_fcn_scratchpad,
                    );
                    propagate_neighbor_data(
                        &self.shared_state.client_state.block_types,
                        &self.inline_fcn_scratchpad,
                        &mut self.inline_nprop_scratchpad,
                    )?;
                } else {
                    self.enqueue_for_nprop(coord);
                }
            }
            for coord in needs_remesh {
                if eligible_for_inline(coord) {
                    self.shared_state
                        .client_state
                        .chunks
                        .maybe_mesh_and_maybe_promote(
                            coord,
                            &self.shared_state.client_state.block_renderer,
                        )?;
                } else {
                    self.enqueue_for_meshing(coord);
                }
            }
        }
        Ok((missing_coord, unknown_coords))
    }

    async fn handle_inventory_update(
        &mut self,
        inventory_update: &rpc::InventoryUpdate,
    ) -> Result<()> {
        if inventory_update.inventory.is_none() {
            return self
                .shared_state
                .send_bugcheck("Missing inventory in InventoryUpdate".to_string())
                .await;
        };
        let inv = ClientInventory::from_proto(inventory_update);
        let mut hud_lock = self.shared_state.client_state.hud.lock();
        if Some(inventory_update.view_id) == hud_lock.hotbar_view_id {
            hud_lock.invalidate_hotbar();
            let slot = hud_lock.hotbar_slot();
            drop(hud_lock);
            self.shared_state
                .client_state
                .tool_controller
                .lock()
                .change_held_item(
                    &self.shared_state.client_state,
                    slot,
                    inv.contents()[slot as usize]
                        .clone()
                        .and_then(|x| self.shared_state.client_state.items.get(&x.item_name))
                        .cloned(),
                )
        }

        self.shared_state
            .client_state
            .inventories
            .lock()
            .inventory_views
            .insert(inventory_update.view_id, inv);
        Ok(())
    }

    async fn handle_client_state_update(
        &mut self,
        state_update: &rpc::SetClientState,
    ) -> Result<()> {
        self.shared_state.initial_state_notification.notify_one();
        self.shared_state
            .client_state
            .handle_server_update(state_update)
    }

    async fn handle_entity_update(
        &mut self,
        update: &entities_proto::EntityUpdate,
        estimated_send_tick: u64,
    ) -> Result<()> {
        if update.remove {
            if self
                .shared_state
                .client_state
                .entities
                .lock()
                .remove_entity(update.id, &self.shared_state.client_state)
                .is_none()
            {
                self.shared_state
                    .send_bugcheck(format!("Got remove for non-existent entity {}", update.id))
                    .await?;
            }
            return Ok(());
        }

        let outcome = match self
            .shared_state
            .client_state
            .entities
            .lock()
            .entities
            .entry(update.id)
        {
            Entry::Occupied(mut entry) => entry.get_mut().update(update, estimated_send_tick),
            Entry::Vacant(entry) => {
                match GameEntity::from_proto(update, self.shared_state.client_state.deref()) {
                    Ok(x) => {
                        entry.insert(x);
                        Ok(())
                    }
                    Err(e) => Err(e),
                }
            }
        };

        if let Err(e) = outcome {
            self.shared_state
                .send_bugcheck(format!("Failed to handle entity update: {:?}", e))
                .await?;
        }

        Ok(())
    }
}

pub(crate) struct SnappyDecodeHelper {
    snappy_decoder: snap::raw::Decoder,
    snappy_output_buffer: Vec<u8>,
}
impl SnappyDecodeHelper {
    fn decode<T>(&mut self, data: &[u8]) -> Result<T>
    where
        T: Message + Default,
    {
        let decode_len = snap::raw::decompress_len(data)?;
        if self.snappy_output_buffer.len() < decode_len {
            self.snappy_output_buffer.resize(decode_len, 0);
        }
        let decompressed_len = self
            .snappy_decoder
            .decompress(data, &mut self.snappy_output_buffer)?;
        Ok(T::decode(&self.snappy_output_buffer[0..decompressed_len])?)
    }

    pub(crate) fn new() -> SnappyDecodeHelper {
        SnappyDecodeHelper {
            snappy_decoder: snap::raw::Decoder::new(),
            snappy_output_buffer: Vec::new(),
        }
    }
}