summaryrefslogtreecommitdiffstats
path: root/game/client/receive.cc
blob: 5b053d031cb07e7a17268cc9bebd2a2c54dffb56 (plain)
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
#include "client/pch.hh"
#include "client/receive.hh"

#include "shared/dimension.hh"
#include "shared/head.hh"
#include "shared/player.hh"
#include "shared/protocol.hh"
#include "shared/transform.hh"
#include "shared/velocity.hh"

#include "client/chat.hh"
#include "client/factory.hh"
#include "client/globals.hh"
#include "client/gui_screen.hh"
#include "client/session.hh"
#include "client/sound.hh"
#include "client/window_title.hh"

static bool synchronize_entity_id(Dimension *dimension, entt::entity entity)
{
    if(dimension->entities.valid(entity)) {
        // Entity ID already exists
        return true;
    }

    auto created = dimension->entities.create(entity);

    if(created == entity) {
        // Synchronized successfully
        return true;
    }

    session::disconnect("protocol.entity_id_desync");
    spdlog::critical("receive: entity desync: network {} resolved as client {}",
        static_cast<std::uint64_t>(entity), static_cast<std::uint64_t>(created));
    return false;
}

static void on_dimension_info_packet(const protocol::DimensionInfo &packet)
{
    if(session::peer) {
        if(globals::dimension) {
            delete globals::dimension;
            globals::dimension = nullptr;
            globals::player = entt::null;
        }

        globals::dimension = new Dimension(packet.name.c_str(), packet.gravity);
    }
}

static void on_chunk_voxels_packet(const protocol::ChunkVoxels &packet)
{
    if(session::peer && globals::dimension) {
        auto chunk = globals::dimension->create_chunk(packet.chunk);
        chunk->set_voxels(packet.voxels);

        ChunkUpdateEvent event;
        event.dimension = globals::dimension;
        event.cpos = packet.chunk;
        event.chunk = chunk;

        globals::dispatcher.trigger(event);

        return;
    }
}

static void on_entity_head_packet(const protocol::EntityHead &packet)
{
    if(session::peer && globals::dimension) {
        if(synchronize_entity_id(globals::dimension, packet.entity)) {
            auto &component = globals::dimension->entities.get_or_emplace<HeadComponent>(packet.entity);
            auto &prevcomp = globals::dimension->entities.get_or_emplace<HeadComponentPrev>(packet.entity);

            // Store the previous component state
            prevcomp.angles = component.angles;
            prevcomp.offset = component.offset;

            // Assign the new component state
            component.angles = packet.angles;
        }
    }
}

static void on_entity_transform_packet(const protocol::EntityTransform &packet)
{
    if(session::peer && globals::dimension) {
        if(synchronize_entity_id(globals::dimension, packet.entity)) {
            auto &component = globals::dimension->entities.get_or_emplace<TransformComponent>(packet.entity);
            auto &prevcomp = globals::dimension->entities.get_or_emplace<TransformComponentPrev>(packet.entity);

            // Store the previous component state
            prevcomp.angles = component.angles;
            prevcomp.chunk = component.chunk;
            prevcomp.local = component.local;

            // Assign the new component state
            component.angles = packet.angles;
            component.chunk = packet.chunk;
            component.local = packet.local;
        }
    }
}

static void on_entity_velocity_packet(const protocol::EntityVelocity &packet)
{
    if(session::peer && globals::dimension) {
        if(synchronize_entity_id(globals::dimension, packet.entity)) {
            auto &component = globals::dimension->entities.get_or_emplace<VelocityComponent>(packet.entity);
            component.value = packet.value;
        }
    }
}

static void on_entity_player_packet(const protocol::EntityPlayer &packet)
{
    if(session::peer && globals::dimension) {
        if(synchronize_entity_id(globals::dimension, packet.entity)) {
            client_factory::create_player(globals::dimension, packet.entity);
        }
    }
}

static void on_spawn_player_packet(const protocol::SpawnPlayer &packet)
{
    if(session::peer && globals::dimension) {
        if(synchronize_entity_id(globals::dimension, packet.entity)) {
            client_factory::create_player(globals::dimension, packet.entity);

            globals::player = packet.entity;
            globals::gui_screen = GUI_SCREEN_NONE;

            client_chat::refresh_timings();

            window_title::update();
        }
    }
}

static void on_remove_entity_packet(const protocol::RemoveEntity &packet)
{
    if(globals::dimension) {
        if(packet.entity == globals::player)
            globals::player = entt::null;
        globals::dimension->entities.destroy(packet.entity);
    }
}

static void on_generic_sound_packet(const protocol::GenericSound &packet)
{
    sound::play_generic(packet.sound.c_str(), packet.looping, packet.pitch);
}

static void on_entity_sound_packet(const protocol::EntitySound &packet)
{
    sound::play_entity(packet.entity, packet.sound.c_str(), packet.looping, packet.pitch);
}

void client_receive::init(void)
{
    globals::dispatcher.sink<protocol::DimensionInfo>().connect<&on_dimension_info_packet>();
    globals::dispatcher.sink<protocol::ChunkVoxels>().connect<&on_chunk_voxels_packet>();
    globals::dispatcher.sink<protocol::EntityHead>().connect<&on_entity_head_packet>();
    globals::dispatcher.sink<protocol::EntityTransform>().connect<&on_entity_transform_packet>();
    globals::dispatcher.sink<protocol::EntityVelocity>().connect<&on_entity_velocity_packet>();
    globals::dispatcher.sink<protocol::EntityPlayer>().connect<&on_entity_player_packet>();
    globals::dispatcher.sink<protocol::SpawnPlayer>().connect<&on_spawn_player_packet>();
    globals::dispatcher.sink<protocol::RemoveEntity>().connect<&on_remove_entity_packet>();
    globals::dispatcher.sink<protocol::GenericSound>().connect<&on_generic_sound_packet>();
    globals::dispatcher.sink<protocol::EntitySound>().connect<&on_entity_sound_packet>();
}