]> git.lizzy.rs Git - dragonblocks_alpha.git/blob - src/client/client_player.c
Use dragonnet
[dragonblocks_alpha.git] / src / client / client_player.c
1 #include <stdio.h>
2 #include "environment.h"
3 #include "client/camera.h"
4 #include "client/client.h"
5 #include "client/client_map.h"
6 #include "client/client_player.h"
7 #include "client/cube.h"
8 #include "client/debug_menu.h"
9 #include "client/texture.h"
10
11 struct ClientPlayer client_player;
12
13 // to be called whenever the player position changes
14 // rwlock has to be read or write locked
15 static void update_pos()
16 {
17         camera_set_position((v3f32) {client_player.pos.x, client_player.pos.y + client_player.eye_height, client_player.pos.z});
18         dragonnet_peer_send_ToServerPos(client, &(ToServerPos) {
19                 .pos = client_player.pos,
20         });
21
22         client_player.obj->pos = (v3f32) {client_player.pos.x, client_player.pos.y, client_player.pos.z};
23         object_transform(client_player.obj);
24
25         debug_menu_update_pos();
26         debug_menu_update_humidity();
27         debug_menu_update_temperature();
28 }
29
30 // get absolute player bounding box
31 // rwlock has to be read- or write locked
32 static aabb3f64 get_box()
33 {
34         return (aabb3f64) {
35                 {client_player.box.min.x + client_player.pos.x, client_player.box.min.y + client_player.pos.y, client_player.box.min.z + client_player.pos.z},
36                 {client_player.box.max.x + client_player.pos.x, client_player.box.max.y + client_player.pos.y, client_player.box.max.z + client_player.pos.z},
37         };
38 }
39
40 // get absolute integer box that contains all nodes a float bounding box touches
41 static aabb3s32 round_box(aabb3f64 box)
42 {
43         return (aabb3s32) {
44                 {floor(box.min.x + 0.5), floor(box.min.y + 0.5), floor(box.min.z + 0.5)},
45                 {ceil(box.max.x - 0.5), ceil(box.max.y - 0.5), ceil(box.max.z - 0.5)},
46         };
47 }
48
49 // return true if node at x, y, z is solid (or unloaded)
50 static bool is_solid(s32 x, s32 y, s32 z)
51 {
52         Node node = map_get_node(client_map.map, (v3s32) {x, y, z}).type;
53         return node == NODE_UNLOADED || node_definitions[node].solid;
54 }
55
56 // determine if player can jump currently (must be standing on a solid block)
57 // rwlock has to be read- or write locked
58 static bool can_jump()
59 {
60         if (client_player.velocity.y != 0.0)
61                 return false;
62
63         aabb3f64 fbox = get_box();
64         fbox.min.y -= 0.5;
65
66         aabb3s32 box = round_box(fbox);
67
68         if (fbox.min.y - (f64) box.min.y > 0.01)
69                 return false;
70
71         for (s32 x = box.min.x; x <= box.max.x; x++)
72                 for (s32 z = box.min.z; z <= box.max.z; z++)
73                         if (is_solid(x, box.min.y, z))
74                                 return true;
75
76         return false;
77 }
78
79 // ClientPlayer singleton constructor
80 void client_player_init()
81 {
82         client_player.pos = (v3f64) {0.0, 0.0, 0.0};
83         client_player.velocity = (v3f64) {0.0, 0.0, 0.0};
84         client_player.box = (aabb3f64) {{-0.3, 0.0, -0.3}, {0.3, 1.75, 0.3}};
85         client_player.yaw = client_player.pitch = 0.0f;
86         client_player.eye_height = 1.5;
87         client_player.fly = false;
88         client_player.collision = true;
89         pthread_rwlock_init(&client_player.rwlock, NULL);
90 }
91
92 // ClientPlayer singleton destructor
93 void client_player_deinit()
94 {
95         pthread_rwlock_destroy(&client_player.rwlock);
96 }
97
98 // create mesh object and info hud
99 void client_player_add_to_scene()
100 {
101         client_player.obj = object_create();
102         client_player.obj->scale = (v3f32) {0.6, 1.75, 0.6};
103         client_player.obj->visible = false;
104
105         object_set_texture(client_player.obj, texture_load(RESSOURCE_PATH "textures/player.png", true));
106
107         for (int f = 0; f < 6; f++) {
108                 for (int v = 0; v < 6; v++) {
109                         Vertex3D vertex = cube_vertices[f][v];
110                         vertex.position.y += 0.5;
111                         object_add_vertex(client_player.obj, &vertex);
112                 }
113         }
114
115         pthread_rwlock_rdlock(&client_player.rwlock);
116         update_pos();
117         pthread_rwlock_unlock(&client_player.rwlock);
118
119         debug_menu_update_yaw();
120         debug_menu_update_pitch();
121 }
122
123 // jump if possible
124 void client_player_jump()
125 {
126         pthread_rwlock_wrlock(&client_player.rwlock);
127         if (can_jump())
128                 client_player.velocity.y += 10.0;
129         pthread_rwlock_unlock(&client_player.rwlock);
130 }
131
132 // get position (thread-safe)
133 v3f64 client_player_get_position()
134 {
135         v3f64 pos;
136
137         pthread_rwlock_rdlock(&client_player.rwlock);
138         pos = client_player.pos;
139         pthread_rwlock_unlock(&client_player.rwlock);
140
141         return pos;
142 }
143
144 // set position (thread-safe)
145 void client_player_set_position(v3f64 pos)
146 {
147         pthread_rwlock_rdlock(&client_player.rwlock);
148         client_player.pos = pos;
149         pthread_rwlock_unlock(&client_player.rwlock);
150 }
151
152 // to be called every frame
153 void client_player_tick(f64 dtime)
154 {
155         pthread_rwlock_wrlock(&client_player.rwlock);
156
157         v3f64 old_pos = client_player.pos;
158         v3f64 old_velocity = client_player.velocity;
159
160         if (! client_player.fly)
161                 client_player.velocity.y -= 32.0 * dtime;
162
163 #define GETS(vec, comp) *(s32 *) ((char *) &vec + offsetof(v3s32, comp))
164 #define GETF(vec, comp) *(f64 *) ((char *) &vec + offsetof(v3f64, comp))
165 #define PHYSICS(a, b, c) { \
166                 f64 v = (GETF(client_player.velocity, a) + GETF(old_velocity, a)) / 2.0f; \
167                 if (v == 0.0) \
168                         goto a ## _physics_done; \
169                 aabb3s32 box = round_box(get_box()); \
170                 v3f64 old_pos = client_player.pos; \
171                 GETF(client_player.pos, a) += v * dtime; \
172                 if (! client_player.collision) \
173                         goto a ## _physics_done; \
174                 s32 dir; \
175                 f64 offset; \
176                 if (v > 0.0) { \
177                         dir = +1; \
178                         offset = GETF(client_player.box.max, a); \
179                         GETS(box.min, a) = ceil(GETF(old_pos, a) + offset + 0.5); \
180                         GETS(box.max, a) = floor(GETF(client_player.pos, a) + offset + 0.5); \
181                 } else { \
182                         dir = -1; \
183                         offset = GETF(client_player.box.min, a); \
184                         GETS(box.min, a) = floor(GETF(old_pos, a) + offset - 0.5); \
185                         GETS(box.max, a) = ceil(GETF(client_player.pos, a) + offset - 0.5); \
186                 } \
187                 GETS(box.max, a) += dir; \
188                 for (s32 a = GETS(box.min, a); a != GETS(box.max, a); a += dir) { \
189                         for (s32 b = GETS(box.min, b); b <= GETS(box.max, b); b++) { \
190                                 for (s32 c = GETS(box.min, c); c <= GETS(box.max, c); c++) { \
191                                         if (is_solid(x, y, z)) { \
192                                                 GETF(client_player.pos, a) = (f64) a - offset - 0.5 * (f64) dir; \
193                                                 GETF(client_player.velocity, a) = 0.0; \
194                                                 goto a ## _physics_done; \
195                                         } \
196                                 } \
197                         } \
198                 } \
199                 a ## _physics_done: (void) 0; \
200         }
201
202         PHYSICS(x, y, z)
203         PHYSICS(y, x, z)
204         PHYSICS(z, x, y)
205
206 #undef GETS
207 #undef GETF
208 #undef PHYSICS
209
210         if (! v3f64_equals(old_pos, client_player.pos))
211                 update_pos();
212
213         pthread_rwlock_unlock(&client_player.rwlock);
214 }