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
#![feature(stmt_expr_attributes)]
#![allow(
    clippy::needless_pass_by_ref_mut //until we find a better way for specs
)]

#[cfg(all(feature = "be-dyn-lib", feature = "use-dyn-lib"))]
compile_error!("Can't use both \"be-dyn-lib\" and \"use-dyn-lib\" features at once");

mod admin;
mod character_states;
mod experimental_shaders;
mod widgets;

use client::{Client, Join, LendJoin, World, WorldExt};
use common::{
    cmd::ServerChatCommand,
    comp,
    comp::{inventory::item::armor::Friction, Poise, PoiseState},
    resources::Time,
};
use core::mem;
use egui::{CollapsingHeader, Color32, Grid, Pos2, ScrollArea, Slider, Ui, Window};
use egui_plot::{Line, Plot, PlotPoints};

use crate::{
    admin::draw_admin_commands_window, character_states::draw_char_state_group,
    experimental_shaders::draw_experimental_shaders_window, widgets::two_col_row,
};
use common::comp::{
    aura::AuraKind::{Buff, ForcePvP, FriendlyFire},
    Body, Fluid,
};
use egui_winit_platform::Platform;
use std::time::Duration;
#[cfg(feature = "use-dyn-lib")]
use {
    common_dynlib::LoadedLib, lazy_static::lazy_static, std::ffi::CStr, std::sync::Arc,
    std::sync::Mutex,
};

#[cfg(feature = "use-dyn-lib")]
lazy_static! {
    static ref LIB: Arc<Mutex<Option<LoadedLib>>> =
        common_dynlib::init("veloren-voxygen-egui", "egui");
}

#[cfg(feature = "use-dyn-lib")]
const MAINTAIN_EGUI_FN: &[u8] = b"maintain_egui_inner\0";

pub struct SelectedEntityInfo {
    entity_id: u32,
    debug_shape_id: Option<u64>,
    character_state_history: Vec<String>,
}

impl SelectedEntityInfo {
    fn new(entity_id: u32) -> Self {
        Self {
            entity_id,
            debug_shape_id: None,
            character_state_history: Vec::new(),
        }
    }
}

pub struct AdminCommandState {
    give_item_qty: u32,
    give_item_selected_idx: usize,
    give_item_search_text: String,
    kits_selected_idx: usize,
    spawn_entity_qty: u32,
    spawn_entity_selected_idx: usize,
    spawn_entity_search_text: String,
}

impl AdminCommandState {
    fn new() -> Self {
        Self {
            give_item_qty: 1,
            give_item_selected_idx: 0,
            give_item_search_text: String::new(),
            kits_selected_idx: 0,
            spawn_entity_qty: 1,
            spawn_entity_selected_idx: 0,
            spawn_entity_search_text: String::new(),
        }
    }
}

pub struct EguiDebugInfo {
    pub frame_time: Duration,
    pub ping_ms: f64,
}

pub struct EguiInnerState {
    selected_entity_info: Option<SelectedEntityInfo>,
    admin_command_state: AdminCommandState,
    max_entity_distance: f32,
    selected_entity_cylinder_height: f32,
    frame_times: Vec<f32>,
    windows: EguiWindows,
    debug_vectors_enabled: bool,
}

#[derive(Clone, Default)]
pub struct EguiWindows {
    admin_commands: bool,
    egui_inspection: bool,
    egui_settings: bool,
    egui_memory: bool,
    frame_time: bool,
    ecs_entities: bool,
    experimental_shaders: bool,
}

impl Default for EguiInnerState {
    fn default() -> Self {
        Self {
            admin_command_state: AdminCommandState::new(),
            selected_entity_info: None,
            max_entity_distance: 100000.0,
            selected_entity_cylinder_height: 10.0,
            frame_times: Vec::new(),
            windows: EguiWindows::default(),
            debug_vectors_enabled: false,
        }
    }
}

pub enum EguiDebugShapeAction {
    AddCylinder {
        radius: f32,
        height: f32,
    },
    RemoveShape(u64),
    SetPosAndColor {
        id: u64,
        pos: [f32; 4],
        color: [f32; 4],
    },
}

pub enum EguiAction {
    ChatCommand {
        cmd: ServerChatCommand,
        args: Vec<String>,
    },
    DebugShape(EguiDebugShapeAction),
    SetExperimentalShader(String, bool),
    SetShowDebugVector(bool),
}

#[derive(Default)]
pub struct EguiActions {
    pub actions: Vec<EguiAction>,
}

#[cfg(feature = "use-dyn-lib")]
pub fn init() { lazy_static::initialize(&LIB); }

pub fn maintain(
    platform: &mut Platform,
    egui_state: &mut EguiInnerState,
    client: &Client,
    debug_info: Option<EguiDebugInfo>,
    added_cylinder_shape_id: Option<u64>,
    experimental_shaders: Vec<(String, bool)>,
) -> EguiActions {
    #[cfg(not(feature = "use-dyn-lib"))]
    {
        maintain_egui_inner(
            platform,
            egui_state,
            client,
            debug_info,
            added_cylinder_shape_id,
            experimental_shaders,
        )
    }

    #[cfg(feature = "use-dyn-lib")]
    {
        let lock = LIB.lock().unwrap();
        let lib = &lock.as_ref().unwrap().lib;

        let maintain_fn: common_dynlib::Symbol<
            fn(
                &mut Platform,
                &mut EguiInnerState,
                &Client,
                Option<EguiDebugInfo>,
                Option<u64>,
                Vec<(String, bool)>,
            ) -> EguiActions,
        > = unsafe { lib.get(MAINTAIN_EGUI_FN) }.unwrap_or_else(|e| {
            panic!(
                "Trying to use: {} but had error: {:?}",
                CStr::from_bytes_with_nul(MAINTAIN_EGUI_FN)
                    .map(CStr::to_str)
                    .unwrap()
                    .unwrap(),
                e
            )
        });

        maintain_fn(
            platform,
            egui_state,
            client,
            debug_info,
            added_cylinder_shape_id,
            experimental_shaders,
        )
    }
}

#[cfg_attr(feature = "be-dyn-lib", export_name = "maintain_egui_inner")]
pub fn maintain_egui_inner(
    platform: &mut Platform,
    egui_state: &mut EguiInnerState,
    client: &Client,
    debug_info: Option<EguiDebugInfo>,
    added_cylinder_shape_id: Option<u64>,
    experimental_shaders: Vec<(String, bool)>,
) -> EguiActions {
    platform.begin_frame();
    let ctx = &platform.context();

    let mut egui_actions = EguiActions::default();
    let mut previous_selected_entity: Option<SelectedEntityInfo> = None;
    let mut max_entity_distance = egui_state.max_entity_distance;
    let mut selected_entity_cylinder_height = egui_state.selected_entity_cylinder_height;
    let mut windows = egui_state.windows.clone();
    let mut debug_vectors_enabled_mut = egui_state.debug_vectors_enabled;

    // If a debug cylinder was added in the last frame, store it against the
    // selected entity
    if let Some(shape_id) = added_cylinder_shape_id {
        if let Some(selected_entity) = &mut egui_state.selected_entity_info {
            selected_entity.debug_shape_id = Some(shape_id);
        }
    }

    if let Some(debug_info) = debug_info.as_ref() {
        egui_state
            .frame_times
            .push(debug_info.frame_time.as_nanos() as f32);
        if egui_state.frame_times.len() > 250 {
            egui_state.frame_times.remove(0);
        }
    };

    let start_pos = Pos2 { x: 300.0, y: 0.0 };
    Window::new("Debug Control")
        .default_pos(start_pos)
        .default_width(200.0)
        .default_height(200.0)
        .show(&platform.context(), |ui| {
            ui.horizontal(|ui| {
                ui.label(format!(
                    "Ping: {:.1}ms",
                    debug_info.as_ref().map_or(0.0, |x| x.ping_ms)
                ));
            });
            ui.group(|ui| {
                ui.vertical(|ui| {
                    ui.checkbox(&mut windows.admin_commands, "Admin Commands");
                    ui.checkbox(&mut windows.ecs_entities, "ECS Entities");
                    ui.checkbox(&mut windows.frame_time, "Frame Time");
                    ui.checkbox(&mut windows.experimental_shaders, "Experimental Shaders");
                    ui.checkbox(&mut debug_vectors_enabled_mut, "Show Debug Vectors");
                });
            });

            ui.group(|ui| {
                ui.vertical(|ui| {
                    ui.label("Show EGUI Windows");
                    ui.horizontal(|ui| {
                        ui.checkbox(&mut windows.egui_inspection, "🔍 Inspection");
                        ui.checkbox(&mut windows.egui_settings, "🔧 Settings");
                        ui.checkbox(&mut windows.egui_memory, "📝 Memory");
                    })
                })
            });
        });

    Window::new("🔧 Settings")
        .open(&mut windows.egui_settings)
        .vscroll(true)
        .show(ctx, |ui| {
            ctx.settings_ui(ui);
        });
    Window::new("🔍 Inspection")
        .open(&mut windows.egui_inspection)
        .vscroll(true)
        .show(ctx, |ui| {
            ctx.inspection_ui(ui);
        });

    Window::new("📝 Memory")
        .open(&mut windows.egui_memory)
        .resizable(false)
        .show(ctx, |ui| {
            ctx.memory_ui(ui);
        });

    Window::new("Frame Time")
        .open(&mut windows.frame_time)
        .default_width(200.0)
        .default_height(200.0)
        .show(ctx, |ui| {
            Plot::new("Frame Time").show(ui, |plot_ui| {
                plot_ui.line(Line::new(PlotPoints::from_iter(
                    egui_state
                        .frame_times
                        .iter()
                        .enumerate()
                        .map(|(i, x)| [i as f64, *x as f64]),
                )))
            });
        });

    if windows.ecs_entities {
        let ecs = client.state().ecs();

        let positions = client.state().ecs().read_storage::<comp::Pos>();
        let client_pos = positions.get(client.entity());

        Window::new("ECS Entities")
            .open(&mut windows.ecs_entities)
            .default_width(500.0)
            .default_height(500.0)
            .show(ctx, |ui| {
                ui.label(format!("Entity count: {}", &ecs.entities().join().count()));
                ui.add(
                    Slider::new(&mut max_entity_distance, 1.0..=100000.0)
                        .logarithmic(true)
                        .clamp_to_range(true)
                        .text("Max entity distance"),
                );

                ui.add(
                    Slider::new(&mut selected_entity_cylinder_height, 0.1..=100.0)
                        .logarithmic(true)
                        .clamp_to_range(true)
                        .text("Cylinder height"),
                );

                let scroll_area = ScrollArea::vertical().max_height(800.0);
                scroll_area.show(ui, |ui| {
                    Grid::new("entities_grid")
                        .spacing([40.0, 4.0])
                        .max_col_width(300.0)
                        .striped(true)
                        .show(ui, |ui| {
                            ui.label("-");
                            ui.label("ID");
                            ui.label("Pos");
                            ui.label("Vel");
                            ui.label("Name");
                            ui.label("Body");
                            ui.label("Poise");
                            ui.label("Character State");
                            ui.label("Character Activity");
                            ui.end_row();
                            for (
                                entity,
                                body,
                                stats,
                                pos,
                                _ori,
                                vel,
                                poise,
                                character_state,
                                character_activity,
                            ) in (
                                &ecs.entities(),
                                ecs.read_storage::<Body>().maybe(),
                                ecs.read_storage::<comp::Stats>().maybe(),
                                ecs.read_storage::<comp::Pos>().maybe(),
                                ecs.read_storage::<comp::Ori>().maybe(),
                                ecs.read_storage::<comp::Vel>().maybe(),
                                ecs.read_storage::<Poise>().maybe(),
                                ecs.read_storage::<comp::CharacterState>().maybe(),
                                ecs.read_storage::<comp::CharacterActivity>().maybe(),
                            )
                                .join()
                                .filter(
                                    |(_, _, _, pos, _, _, _, _, _)| {
                                        client_pos.map_or(true, |client_pos| {
                                            pos.map_or(0.0, |pos| {
                                                pos.0.distance_squared(client_pos.0)
                                            }) < max_entity_distance
                                        })
                                    },
                                )
                            {
                                if ui.button("View").clicked() {
                                    previous_selected_entity =
                                        mem::take(&mut egui_state.selected_entity_info);

                                    if pos.is_some() {
                                        egui_actions.actions.push(EguiAction::DebugShape(
                                            EguiDebugShapeAction::AddCylinder {
                                                radius: 1.0,
                                                height: egui_state.selected_entity_cylinder_height,
                                            },
                                        ));
                                    }
                                    egui_state.selected_entity_info =
                                        Some(SelectedEntityInfo::new(entity.id()));
                                }

                                ui.label(format!("{}", entity.id()));

                                if let Some(pos) = pos {
                                    ui.label(format!(
                                        "{:.0},{:.0},{:.0}",
                                        pos.0.x, pos.0.y, pos.0.z
                                    ));
                                } else {
                                    ui.label("-");
                                }

                                if let Some(vel) = vel {
                                    ui.label(format!("{:.1}u/s", vel.0.magnitude()));
                                } else {
                                    ui.label("-");
                                }
                                if let Some(stats) = stats {
                                    ui.label(&stats.name);
                                } else {
                                    ui.label("-");
                                }
                                if let Some(body) = body {
                                    ui.label(body_species(body));
                                } else {
                                    ui.label("-");
                                }

                                if let Some(poise) = poise {
                                    poise_state_label(ui, poise);
                                } else {
                                    ui.label("-");
                                }

                                if let Some(character_state) = character_state {
                                    ui.label(character_state.to_string());
                                } else {
                                    ui.label("-");
                                }

                                if let Some(character_activity) = character_activity {
                                    ui.label(format!("{:?}", character_activity));
                                } else {
                                    ui.label("-");
                                }

                                ui.end_row();
                            }
                        });
                    let margin = ui.visuals().clip_rect_margin;

                    let current_scroll = ui.clip_rect().top() - ui.min_rect().top() + margin;
                    let max_scroll =
                        ui.min_rect().height() - ui.clip_rect().height() + 2.0 * margin;
                    (current_scroll, max_scroll)
                });
            });
        if let Some(selected_entity_info) = &mut egui_state.selected_entity_info {
            let selected_entity = ecs.entities().entity(selected_entity_info.entity_id);
            if !selected_entity.gen().is_alive() {
                previous_selected_entity = mem::take(&mut egui_state.selected_entity_info);
            } else {
                selected_entity_window(platform, ecs, selected_entity_info, &mut egui_actions);
            }
        }
    }

    draw_admin_commands_window(
        ctx,
        &mut egui_state.admin_command_state,
        &mut windows.admin_commands,
        &mut egui_actions,
    );

    draw_experimental_shaders_window(
        ctx,
        &mut windows.experimental_shaders,
        &mut egui_actions,
        &experimental_shaders,
    );

    if let Some(previous) = previous_selected_entity {
        if let Some(debug_shape_id) = previous.debug_shape_id {
            egui_actions
                .actions
                .push(EguiAction::DebugShape(EguiDebugShapeAction::RemoveShape(
                    debug_shape_id,
                )));
        }
    };

    if let Some(selected_entity) = &egui_state.selected_entity_info {
        if let Some(debug_shape_id) = selected_entity.debug_shape_id {
            if (egui_state.selected_entity_cylinder_height - selected_entity_cylinder_height).abs()
                > f32::EPSILON
            {
                egui_actions.actions.push(EguiAction::DebugShape(
                    EguiDebugShapeAction::RemoveShape(debug_shape_id),
                ));
                egui_actions.actions.push(EguiAction::DebugShape(
                    EguiDebugShapeAction::AddCylinder {
                        radius: 1.0,
                        height: selected_entity_cylinder_height,
                    },
                ));
            }
        }
    };
    if debug_vectors_enabled_mut != egui_state.debug_vectors_enabled {
        egui_actions
            .actions
            .push(EguiAction::SetShowDebugVector(debug_vectors_enabled_mut));
        egui_state.debug_vectors_enabled = debug_vectors_enabled_mut;
    }

    egui_state.max_entity_distance = max_entity_distance;
    egui_state.selected_entity_cylinder_height = selected_entity_cylinder_height;
    egui_state.windows = windows;
    egui_actions
}

fn selected_entity_window(
    platform: &mut Platform,
    ecs: &World,
    selected_entity_info: &mut SelectedEntityInfo,
    egui_actions: &mut EguiActions,
) {
    let entity_id = selected_entity_info.entity_id;
    for (
        _entity,
        body,
        stats,
        pos,
        _ori,
        vel,
        poise,
        buffs,
        auras,
        character_state,
        character_activity,
        physics_state,
        alignment,
        scale,
        (mass, density, health, energy),
    ) in (
        &ecs.entities(),
        ecs.read_storage::<Body>().maybe(),
        ecs.read_storage::<comp::Stats>().maybe(),
        ecs.read_storage::<comp::Pos>().maybe(),
        ecs.read_storage::<comp::Ori>().maybe(),
        ecs.read_storage::<comp::Vel>().maybe(),
        ecs.read_storage::<Poise>().maybe(),
        ecs.read_storage::<comp::Buffs>().maybe(),
        ecs.read_storage::<comp::Auras>().maybe(),
        ecs.read_storage::<comp::CharacterState>().maybe(),
        ecs.read_storage::<comp::CharacterActivity>().maybe(),
        ecs.read_storage::<comp::PhysicsState>().maybe(),
        ecs.read_storage::<comp::Alignment>().maybe(),
        ecs.read_storage::<comp::Scale>().maybe(),
        (
            ecs.read_storage::<comp::Mass>().maybe(),
            ecs.read_storage::<comp::Density>().maybe(),
            ecs.read_storage::<comp::Health>().maybe(),
            ecs.read_storage::<comp::Energy>().maybe(),
        ),
    )
        .join()
        .filter(|(e, _, _, _, _, _, _, _, _, _, _, _, _, _, (_, _, _, _))| e.id() == entity_id)
    {
        let time = ecs.read_resource::<Time>();
        if let Some(pos) = pos {
            if let Some(shape_id) = selected_entity_info.debug_shape_id {
                egui_actions.actions.push(EguiAction::DebugShape(
                    EguiDebugShapeAction::SetPosAndColor {
                        id: shape_id,
                        color: [1.0, 1.0, 0.0, 0.5],
                        pos: [pos.0.x, pos.0.y, pos.0.z + 2.0, 0.0],
                    },
                ));
            }
        };

        Window::new("Selected Entity")
            .default_width(300.0)
            .default_height(200.0)
            .show(&platform.context(), |ui| {
                ui.vertical(|ui| {
                    CollapsingHeader::new("General").default_open(true).show(ui, |ui| {
                        Grid::new("selected_entity_general_grid")
                            .spacing([40.0, 4.0])
                            .striped(true)
                            .show(ui, |ui| {
                                two_col_row(ui, "Health", health.map_or("-".to_owned(), |x| format!("{:.1}/{:.1}", x.current(), x.maximum())));
                                two_col_row(ui, "Energy", energy.map_or("-".to_owned(), |x| format!("{:.1}/{:.1}", x.current(), x.maximum())));
                                two_col_row(ui, "Position", pos.map_or("-".to_owned(), |x| format!("({:.1},{:.1},{:.1})", x.0.x, x.0.y, x.0.z)));
                                two_col_row(ui, "Velocity", vel.map_or("-".to_owned(), |x| format!("({:.1},{:.1},{:.1}) ({:.1} u/s)", x.0.x, x.0.y, x.0.z, x.0.magnitude())));
                                two_col_row(ui, "Alignment", alignment.map_or("-".to_owned(), |x| format!("{:?}", x)));
                                two_col_row(ui, "Scale", scale.map_or("-".to_owned(), |x| format!("{:?}", x)));
                                two_col_row(ui, "Mass", mass.map_or("-".to_owned(), |x| format!("{:.1}", x.0)));
                                two_col_row(ui, "Density", density.map_or("-".to_owned(), |x| format!("{:.1}", x.0)));
                            });

                    });
                if let Some(stats) = stats {
                    CollapsingHeader::new("Stats").default_open(true).show(ui, |ui| {
                        Grid::new("selected_entity_stats_grid")
                            .spacing([40.0, 4.0])
                            .striped(true)
                            .show(ui, |ui| {
                                two_col_row(ui, "Name", stats.name.to_string());
                                two_col_row(ui, "Damage Reduction", format!("{:.1}", stats.damage_reduction));
                                two_col_row(ui, "Multiplicative Max Health Modifier", format!("{:.1}", stats.max_health_modifiers.mult_mod));
                                two_col_row(ui, "Move Speed Modifier", format!("{:.1}", stats.move_speed_modifier));
                            });

                    });
                }
                if let Some(body) = body {
                    CollapsingHeader::new("Body").default_open(false).show(ui, |ui| {
                        Grid::new("selected_entity_body_grid")
                            .spacing([40.0, 4.0])
                            .striped(true)
                            .show(ui, |ui| {
                                two_col_row(ui, "Type", body.to_string());
                                two_col_row(ui, "Species", body_species(body));
                            });

                    });
                }
                if let Some(pos) = pos {
                    CollapsingHeader::new("Pos").default_open(false).show(ui, |ui| {
                            Grid::new("selected_entity_pos_grid")
                                .spacing([40.0, 4.0])
                                .max_col_width(100.0)
                                .striped(true)
                                .show(ui, |ui| {
                                    two_col_row(ui, "x", format!("{:.1}", pos.0.x));
                                    two_col_row(ui, "y", format!("{:.1}", pos.0.y));
                                    two_col_row(ui, "z", format!("{:.1}", pos.0.z));
                                });

                    });
                }
                if let Some(poise) = poise {
                    CollapsingHeader::new("Poise").default_open(false).show(ui, |ui| {
                            Grid::new("selected_entity_poise_grid")
                                .spacing([40.0, 4.0])
                                .max_col_width(100.0)
                                .striped(true)
                                // Apparently, if the #[rustfmt::skip] is in front of the closure scope, rust-analyzer can't
                                // parse the code properly. Things will *sometimes* work if the skip is on the other side of
                                // the opening bracket (even though that should only skip formatting the first line of the
                                // closure), but things as arbitrary as adding a comment to the code cause it to be formatted
                                // again. Thus, there is a completely pointless inner scope in this closure, just so that the
                                // code doesn't take up an unreasonable amount of space when formatted. We need that space for
                                // interesting and educational code comments like this one.
                                .show(ui, |ui| { #[rustfmt::skip] {
                                    ui.label("State");
                                    poise_state_label(ui, poise);
                                    ui.end_row();
                                    two_col_row(ui, "Current", format!("{:.1}/{:.1}", poise.current(), poise.maximum()));
                                    two_col_row(ui, "Base Max", format!("{:.1}", poise.base_max()));
                                }});
                        });
                }

                if let Some(buffs) = buffs {
                    CollapsingHeader::new("Buffs").default_open(false).show(ui, |ui| {
                        Grid::new("selected_entity_buffs_grid")
                            .spacing([40.0, 4.0])
                            .max_col_width(100.0)
                            .striped(true)
                            .show(ui, |ui| {
                                ui.label("Kind");
                                ui.label("Time");
                                ui.label("Source");
                                ui.end_row();
                                buffs.buffs.iter().for_each(|(_, v)| {
                                    ui.label(format!("{:?}", v.kind));
                                    ui.label(
                                        v.end_time.map_or("-".to_string(), |end| {
                                            format!("{:?}", end.0 - time.0)
                                        }),
                                    );
                                    ui.label(format!("{:?}", v.source));
                                    ui.end_row();
                                });
                            });
                    });
                }

                if let Some(auras) = auras {
                    CollapsingHeader::new("Auras").default_open(false).show(ui, |ui| {
                        Grid::new("selected_entity_auras_grid")
                            .spacing([40.0, 4.0])
                            .striped(true)
                            .show(ui, |ui| {
                                ui.label("Kind");
                                ui.label("Radius");
                                ui.label("Duration");
                                ui.label("Target");
                                ui.end_row();
                                auras.auras.iter().for_each(|(_, v)| {
                                    ui.label(match v.aura_kind {
                                        Buff { kind, .. } =>  format!("Buff - {:?}", kind),
                                        FriendlyFire =>  "Friendly Fire".to_string(),
                                        ForcePvP =>  "ForcedPvP".to_string(),
                                    });
                                    ui.label(format!("{:1}", v.radius));
                                    ui.label(v.end_time.map_or("-".to_owned(), |x| format!("{:1}s", x.0 - time.0)));
                                    ui.label(format!("{:?}", v.target));
                                    ui.end_row();
                                });
                            });
                    });
                }

                if let Some(character_state) = character_state {
                    if selected_entity_info
                        .character_state_history
                        .first()
                        .unwrap_or(&"-".to_owned())
                        != &character_state.to_string()
                    {
                        selected_entity_info
                            .character_state_history
                            .insert(0, character_state.to_string());
                        if selected_entity_info.character_state_history.len() > 50 {
                            selected_entity_info.character_state_history.pop();
                        }
                    }

                    CollapsingHeader::new("Character State").default_open(false).show(ui, |ui| {
                        draw_char_state_group(ui, selected_entity_info, character_state);
                    });
                }

                if let Some(character_activity) = character_activity {
                    CollapsingHeader::new("CharacterActivity").default_open(false).show(ui, |ui| {
                            Grid::new("selected_entity_character_activity_grid")
                                .spacing([40.0, 4.0])
                                .max_col_width(100.0)
                                .striped(true)
                                .show(ui, |ui| {
                                    two_col_row(ui, "look_dir", format!("{:.3?}", character_activity.look_dir));
                                });
                    });
                }

                if let Some(physics_state) = physics_state {
                    CollapsingHeader::new("Physics State").default_open(false).show(ui, |ui| {
                        Grid::new("selected_entity_physics_state_grid")
                            .spacing([40.0, 4.0])
                            .striped(true)
                            .show(ui, |ui| {
                                two_col_row(ui, "On Ground", physics_state.on_ground.map_or("None".to_owned(), |x| format!("{:?}", x)));
                                two_col_row(ui, "On Ceiling", (if physics_state.on_ceiling { "True" } else { "False " }).to_string());
                                two_col_row(ui, "On Wall", physics_state.on_wall.map_or("-".to_owned(), |x| format!("{:.1},{:.1},{:.1}", x.x, x.y, x.z )));
                                two_col_row(ui, "Touching Entities", physics_state.touch_entities.len().to_string());
                                two_col_row(ui, "In Fluid", match physics_state.in_fluid {
                                    Some(Fluid::Air { elevation, vel, .. }) => format!("Air (Elevation: {:.1}), vel: ({:.1},{:.1},{:.1}) ({:.1} u/s)", elevation, vel.0.x, vel.0.y, vel.0.z, vel.0.magnitude()),
                                    Some(Fluid::Liquid { depth, kind, .. }) => format!("{:?} (Depth: {:.1})", kind, depth),
                                    _ => "None".to_owned() });
                                });
                                two_col_row(ui, "Footwear", match physics_state.footwear{ Friction::Ski => "Ski", Friction::Skate => "Skate", /* Friction::Snowshoe => "Snowshoe", Friction::Spikes => "Spikes", */ Friction::Normal=>"Normal",}.to_string());
                            });
                }
            });
        });
    }
}

fn body_species(body: &Body) -> String {
    match body {
        Body::Humanoid(body) => format!("{:?}", body.species),
        Body::QuadrupedSmall(body) => format!("{:?}", body.species),
        Body::QuadrupedMedium(body) => format!("{:?}", body.species),
        Body::BirdMedium(body) => format!("{:?}", body.species),
        Body::FishMedium(body) => format!("{:?}", body.species),
        Body::Dragon(body) => format!("{:?}", body.species),
        Body::BirdLarge(body) => format!("{:?}", body.species),
        Body::FishSmall(body) => format!("{:?}", body.species),
        Body::BipedLarge(body) => format!("{:?}", body.species),
        Body::BipedSmall(body) => format!("{:?}", body.species),
        Body::Object(body) => format!("{:?}", body),
        Body::ItemDrop(body) => format!("{:?}", body),
        Body::Golem(body) => format!("{:?}", body.species),
        Body::Theropod(body) => format!("{:?}", body.species),
        Body::QuadrupedLow(body) => format!("{:?}", body.species),
        Body::Arthropod(body) => format!("{:?}", body.species),
        Body::Ship(body) => format!("{:?}", body),
        Body::Crustacean(body) => format!("{:?}", body.species),
    }
}

fn poise_state_label(ui: &mut Ui, poise: &Poise) {
    match poise.poise_state() {
        PoiseState::Normal => {
            ui.label("Normal");
        },
        PoiseState::Interrupted => {
            ui.colored_label(Color32::YELLOW, "Interrupted");
        },
        PoiseState::Stunned => {
            ui.colored_label(Color32::RED, "Stunned");
        },
        PoiseState::Dazed => {
            ui.colored_label(Color32::RED, "Dazed");
        },
        PoiseState::KnockedDown => {
            ui.colored_label(Color32::BLUE, "Knocked Down");
        },
    };
}