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"
11 struct ClientPlayer client_player;
13 // to be called whenever the player position changes
14 // rwlock has to be read or write locked
15 static void update_pos()
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,
22 client_player.obj->pos = (v3f32) {client_player.pos.x, client_player.pos.y, client_player.pos.z};
23 object_transform(client_player.obj);
25 debug_menu_update_pos();
26 debug_menu_update_humidity();
27 debug_menu_update_temperature();
30 // get absolute player bounding box
31 // rwlock has to be read- or write locked
32 static aabb3f64 get_box()
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},
40 // get absolute integer box that contains all nodes a float bounding box touches
41 static aabb3s32 round_box(aabb3f64 box)
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)},
49 // return true if node at x, y, z is solid (or unloaded)
50 static bool is_solid(s32 x, s32 y, s32 z)
52 Node node = map_get_node(client_map.map, (v3s32) {x, y, z}).type;
53 return node == NODE_UNLOADED || node_definitions[node].solid;
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()
60 if (client_player.velocity.y != 0.0)
63 aabb3f64 fbox = get_box();
66 aabb3s32 box = round_box(fbox);
68 if (fbox.min.y - (f64) box.min.y > 0.01)
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))
79 // ClientPlayer singleton constructor
80 void client_player_init()
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);
92 // ClientPlayer singleton destructor
93 void client_player_deinit()
95 pthread_rwlock_destroy(&client_player.rwlock);
98 // create mesh object and info hud
99 void client_player_add_to_scene()
101 client_player.obj = object_create();
102 client_player.obj->scale = (v3f32) {0.6, 1.75, 0.6};
103 client_player.obj->visible = false;
105 object_set_texture(client_player.obj, texture_load(RESSOURCE_PATH "textures/player.png", true));
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);
115 pthread_rwlock_rdlock(&client_player.rwlock);
117 pthread_rwlock_unlock(&client_player.rwlock);
119 debug_menu_update_yaw();
120 debug_menu_update_pitch();
124 void client_player_jump()
126 pthread_rwlock_wrlock(&client_player.rwlock);
128 client_player.velocity.y += 10.0;
129 pthread_rwlock_unlock(&client_player.rwlock);
132 // get position (thread-safe)
133 v3f64 client_player_get_position()
137 pthread_rwlock_rdlock(&client_player.rwlock);
138 pos = client_player.pos;
139 pthread_rwlock_unlock(&client_player.rwlock);
144 // set position (thread-safe)
145 void client_player_set_position(v3f64 pos)
147 pthread_rwlock_rdlock(&client_player.rwlock);
148 client_player.pos = pos;
149 pthread_rwlock_unlock(&client_player.rwlock);
152 // to be called every frame
153 void client_player_tick(f64 dtime)
155 pthread_rwlock_wrlock(&client_player.rwlock);
157 v3f64 old_pos = client_player.pos;
158 v3f64 old_velocity = client_player.velocity;
160 if (! client_player.fly)
161 client_player.velocity.y -= 32.0 * dtime;
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; \
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; \
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); \
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); \
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; \
199 a ## _physics_done: (void) 0; \
210 if (! v3f64_equals(old_pos, client_player.pos))
213 pthread_rwlock_unlock(&client_player.rwlock);