]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_entity.c
You can now see other players
[dragonblocks_alpha.git] / src / client / client_entity.c
1 #include <asprintf/asprintf.h>
2 #include <dragonstd/map.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include "client/cube.h"
6 #include "client/client_config.h"
7 #include "client/client_entity.h"
8 #include "client/client_player.h"
9 #include "client/frustum.h"
10 #include "client/gl_debug.h"
11 #include "client/light.h"
12 #include "client/shader.h"
13 #include "client/window.h"
14
15 ClientEntityType client_entity_types[COUNT_ENTITY];
16
17 ModelShader client_entity_shader;
18 typedef struct {
19         v3f32 position;
20         v3f32 normal;
21 } __attribute__((packed)) EntityVertex;
22 Mesh client_entity_cube = {
23         .layout = &(VertexLayout) {
24                 .attributes = (VertexAttribute[]) {
25                         {GL_FLOAT, 3, sizeof(v3f32)}, // position
26                         {GL_FLOAT, 3, sizeof(v3f32)}, // normal
27                 },
28                 .count = 2,
29                 .size = sizeof(EntityVertex),
30         },
31         .vao = 0,
32         .vbo = 0,
33         .data = NULL,
34         .count = 36,
35         .free_data = false,
36 };
37
38 static GLuint shader_prog;
39 static GLint loc_VP;
40 static LightShader light_shader;
41 static Map entities;
42 static List nametagged;
43 static pthread_mutex_t mtx_nametagged;
44
45 // any thread
46 // called when adding, getting or removing an entity from the map
47 static int cmp_entity(const Refcount *entity, const u64 *id)
48 {
49         return u64_cmp(&((ClientEntity *) entity->obj)->data.id, id);
50 }
51
52 // recv thread
53 // called when server sent removal of entity
54 static void entity_drop(ClientEntity *entity)
55 {
56         if (entity->type->remove)
57                 entity->type->remove(entity);
58
59         if (entity->nametag) {
60                 pthread_mutex_lock(&mtx_nametagged);
61                 list_del(&nametagged, &entity->rc, &cmp_ref, &refcount_drp, NULL, NULL);
62                 pthread_mutex_unlock(&mtx_nametagged);
63
64                 entity->nametag->visible = false;
65         }
66
67         refcount_drp(&entity->rc);
68 }
69
70 // any thread
71 // called when all refs have been dropped
72 static void entity_delete(ClientEntity *entity)
73 {
74         if (entity->type->free)
75                 entity->type->free(entity);
76
77         refcount_dst(&entity->rc);
78
79         if (entity->data.nametag)
80                 free(entity->data.nametag);
81
82         pthread_rwlock_init(&entity->lock_pos_rot, NULL);
83         pthread_rwlock_init(&entity->lock_nametag, NULL);
84         pthread_rwlock_init(&entity->lock_box_off, NULL);
85
86         free(entity);
87 }
88
89 static void update_nametag(ClientEntity *entity)
90 {
91         if (entity->nametag) {
92                 gui_text(entity->nametag, entity->data.nametag);
93
94                 if (!entity->data.nametag)
95                         entity->nametag->visible = false;
96         } else if (entity->data.nametag) {
97                 entity->nametag = gui_add(NULL, (GUIElementDefinition) {
98                         .pos = {-1.0f, -1.0f},
99                         .z_index = 0.1f,
100                         .offset = {0, 0},
101                         .margin = {4, 4},
102                         .align = {0.5f, 0.5f},
103                         .scale = {1.0f, 1.0f},
104                         .scale_type = SCALE_TEXT,
105                         .affect_parent_scale = false,
106                         .text = entity->data.nametag,
107                         .image = NULL,
108                         .text_color = (v4f32) {1.0f, 1.0f, 1.0f, 1.0f},
109                         .bg_color = (v4f32) {0.0f, 0.0f, 0.0f, 0.5f},
110                 });
111
112                 pthread_mutex_lock(&mtx_nametagged);
113                 list_apd(&nametagged, refcount_inc(&entity->rc));
114                 pthread_mutex_unlock(&mtx_nametagged);
115         }
116 }
117
118 static void update_nametag_pos(ClientEntity *entity)
119 {
120         if (!entity->data.nametag)
121                 return;
122
123         pthread_rwlock_rdlock(&entity->lock_pos_rot);
124         pthread_rwlock_rdlock(&entity->lock_box_off);
125
126         mat4x4 mvp;
127         if (entity->nametag_offset)
128                 mat4x4_mul(mvp, frustum, *entity->nametag_offset);
129         else
130                 mat4x4_dup(mvp, frustum);
131
132         vec4 dst, src = {0.0f, 0.0f, 0.0f, 1.0f};
133         mat4x4_mul_vec4(dst, mvp, src);
134
135         dst[0] /= dst[3];
136         dst[1] /= dst[3];
137         dst[2] /= dst[3];
138
139         if ((entity->nametag->visible = dst[2] >= -1.0f && dst[2] <= 1.0f)) {
140                 entity->nametag->def.pos = (v2f32) {dst[0] * 0.5f + 0.5f, 1.0f - (dst[1] * 0.5f + 0.5f)};
141                 gui_transform(entity->nametag);
142         }
143
144         pthread_rwlock_unlock(&entity->lock_box_off);
145         pthread_rwlock_unlock(&entity->lock_pos_rot);
146 }
147
148 // main thread
149 // called on startup
150 void client_entity_init()
151 {
152         map_ini(&entities);
153         list_ini(&nametagged);
154         pthread_mutex_init(&mtx_nametagged, NULL);
155 }
156
157 // main thead
158 // called on shutdown
159 void client_entity_deinit()
160 {
161         // forget all entities
162         map_cnl(&entities, &refcount_drp, NULL, NULL, 0);
163         list_clr(&nametagged, &refcount_drp, NULL, NULL);
164         pthread_mutex_destroy(&mtx_nametagged);
165 }
166
167 bool client_entity_gfx_init()
168 {
169         char *shader_defs;
170         asprintf(&shader_defs, "#define VIEW_DISTANCE %lf\n", client_config.view_distance);
171
172         if (!shader_program_create(RESSOURCE_PATH "shaders/3d/entity", &shader_prog, shader_defs)) {
173                 fprintf(stderr, "[error] failed to create entity shader program\n");
174                 return false;
175         }
176
177         free(shader_defs);
178
179         loc_VP = glGetUniformLocation(shader_prog, "VP"); GL_DEBUG
180
181         EntityVertex vertices[6][6];
182         for (int f = 0; f < 6; f++) {
183                 for (int v = 0; v < 6; v++) {
184                         vertices[f][v].position = cube_vertices[f][v].position;
185                         vertices[f][v].normal = cube_vertices[f][v].normal;
186                 }
187         }
188
189         client_entity_cube.data = vertices;
190         mesh_upload(&client_entity_cube);
191
192         client_entity_shader.prog = shader_prog;
193         client_entity_shader.loc_transform = glGetUniformLocation(shader_prog, "model"); GL_DEBUG
194
195         light_shader.prog = shader_prog;
196         light_shader_locate(&light_shader);
197
198         return true;
199 }
200
201 void client_entity_gfx_deinit()
202 {
203         glDeleteProgram(shader_prog); GL_DEBUG
204         mesh_destroy(&client_entity_cube);
205 }
206
207 void client_entity_gfx_update()
208 {
209         glProgramUniformMatrix4fv(shader_prog, loc_VP, 1, GL_FALSE, frustum[0]); GL_DEBUG
210         light_shader_update(&light_shader);
211
212         pthread_mutex_lock(&mtx_nametagged);
213         list_itr(&nametagged, &update_nametag_pos, NULL, &refcount_obj);
214         pthread_mutex_unlock(&mtx_nametagged);
215 }
216
217 ClientEntity *client_entity_grab(u64 id)
218 {
219         return id ? map_get(&entities, &id, &cmp_entity, &refcount_grb) : NULL;
220 }
221
222 void client_entity_transform(ClientEntity *entity)
223 {
224         if (!entity->model)
225                 return;
226
227         entity->model->root->pos = v3f64_to_f32(entity->data.pos); // ToDo: the render pipeline needs to be updated to handle 64-bit positions
228         entity->model->root->rot = entity->data.rot;
229
230         if (entity->type->transform)
231                 entity->type->transform(entity);
232
233         model_node_transform(entity->model->root);
234 }
235
236 void client_entity_add(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityAdd *pkt)
237 {
238         if (pkt->type >= COUNT_ENTITY)
239                 return;
240
241         ClientEntity *entity = malloc(sizeof *entity);
242
243         entity->data = pkt->data;
244         entity->type = &client_entity_types[pkt->type];
245         refcount_ini(&entity->rc, entity, &entity_delete);
246
247         pkt->data.nametag = NULL;
248
249         entity->model = NULL;
250         entity->nametag = NULL;
251
252         if (entity->type->add)
253                 entity->type->add(entity);
254
255         update_nametag(entity);
256
257         pthread_rwlock_init(&entity->lock_pos_rot, NULL);
258         pthread_rwlock_init(&entity->lock_nametag, NULL);
259         pthread_rwlock_init(&entity->lock_box_off, NULL);
260
261         if (!map_add(&entities, &entity->data.id, &entity->rc, &cmp_entity, &refcount_inc))
262                 fprintf(stderr, "[warning] failed to add entity %lu\n", entity->data.id);
263
264         refcount_drp(&entity->rc);
265 }
266
267 void client_entity_remove(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityRemove *pkt)
268 {
269         map_del(&entities, &pkt->id, &cmp_entity, &entity_drop, NULL, &refcount_obj);
270 }
271
272 void client_entity_update_pos_rot(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityUpdatePosRot *pkt)
273 {
274         ClientEntity *entity = client_entity_grab(pkt->id);
275
276         if (!entity)
277                 return;
278
279         pthread_rwlock_wrlock(&entity->lock_pos_rot);
280
281         entity->data.pos = pkt->pos;
282         entity->data.rot = pkt->rot;
283
284         if (entity->type->update_pos_rot)
285                 entity->type->update_pos_rot(entity);
286
287         client_entity_transform(entity);
288
289         pthread_rwlock_unlock(&entity->lock_pos_rot);
290
291         refcount_drp(&entity->rc);
292 }
293
294 void client_entity_update_nametag(__attribute__((unused)) DragonnetPeer *peer, ToClientEntityUpdateNametag *pkt)
295 {
296         ClientEntity *entity = client_entity_grab(pkt->id);
297
298         if (!entity)
299                 return;
300
301         pthread_rwlock_wrlock(&entity->lock_nametag);
302
303         if (entity->data.nametag)
304                 free(entity->data.nametag);
305
306         entity->data.nametag = pkt->nametag;
307         pkt->nametag = NULL;
308
309         if (entity->type->update_nametag)
310                 entity->type->update_nametag(entity);
311
312         update_nametag(entity);
313         pthread_rwlock_unlock(&entity->lock_nametag);
314
315         refcount_drp(&entity->rc);
316 }