]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Implement position sending
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 29 Mar 2021 13:37:31 +0000 (15:37 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 29 Mar 2021 13:37:31 +0000 (15:37 +0200)
src/client.c
src/client.h
src/mapgen.c
src/old_map [new file with mode: 0644]
src/servercommands.c
src/servercommands.h
src/types.c
src/types.h

index 4712c1f8f745b6e0066624c76b1df6a8e32bd86a..a2e197ecbeadcc9af71a9a469b92c14dae76cb4d 100644 (file)
@@ -45,6 +45,12 @@ static void *reciever_thread(void *unused)
        return NULL;
 }
 
+static void update_view_matrix(ShaderProgram *prog, mat4x4 view)
+{
+       mat4x4_translate(view, -client.pos.x, -client.pos.y, -client.pos.z);
+       glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
+}
+
 static void client_loop()
 {
        if(! glfwInit()) {
@@ -91,11 +97,9 @@ static void client_loop()
                return;
        }
 
-       v3f pos = {0.0f, 0.0f, 0.0f};
-
        mat4x4 view, projection;
 
-       mat4x4_translate(view, -pos.x, -pos.y, -pos.z);
+       update_view_matrix(prog, view);
        mat4x4_perspective(projection, 86.1f / 180.0f * M_PI, (float) width / (float) height, 0.01f, 1000.0f);
 
        glUseProgram(prog->id);
@@ -107,31 +111,33 @@ static void client_loop()
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
 
-               bool view_changed = false;
+               bool pos_changed = false;
 
                if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
-                       view_changed = true;
-                       pos.z -= 1.0f;
+                       pos_changed = true;
+                       client.pos.z -= 1.0f;
                } else if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
-                       view_changed = true;
-                       pos.z += 1.0f;
+                       pos_changed = true;
+                       client.pos.z += 1.0f;
                } if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
-                       view_changed = true;
-                       pos.x -= 1.0f;
+                       pos_changed = true;
+                       client.pos.x -= 1.0f;
                } else if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
-                       view_changed = true;
-                       pos.x += 1.0f;
+                       pos_changed = true;
+                       client.pos.x += 1.0f;
                } if (glfwGetKey(window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS) {
-                       view_changed = true;
-                       pos.y -= 1.0f;
+                       pos_changed = true;
+                       client.pos.y -= 1.0f;
                } else if (glfwGetKey(window, GLFW_KEY_SPACE) == GLFW_PRESS) {
-                       view_changed = true;
-                       pos.y += 1.0f;
+                       pos_changed = true;
+                       client.pos.y += 1.0f;
                }
 
-               if (view_changed) {
-                       mat4x4_translate(view, -pos.x, -pos.y, -pos.z);
-                       glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
+               if (pos_changed) {
+                       update_view_matrix(prog, view);
+                       pthread_mutex_lock(&client.mtx);
+                       (void) (write_u32(client.fd, SC_POS) && write_v3f32(client.fd, client.pos));
+                       pthread_mutex_unlock(&client.mtx);
                }
 
                scene_render(client.scene, prog);
@@ -192,6 +198,7 @@ static void client_start(int fd)
        client.name = NULL;
        client.map = map_create();
        client.scene = scene_create();
+       client.pos = (v3f) {0.0f, 0.0f, 0.0f};
 
        mapblock_meshgen_init(client.map, client.scene);
 
index f1bb4e5182e4a722149406841cbd6fac712ecb6f..7e93df1708fc86df807f89dfeeca9a7672e26943 100644 (file)
@@ -17,6 +17,7 @@ typedef struct Client
        char *name;
        Map *map;
        Scene *scene;
+       v3f pos;
 } Client;
 
 void client_disconnect(bool send, const char *detail);
index f06c6f14d0782bf4d179231eba1155a9f0da9ee6..de58bba762eb0ceb761a3f8bf54d42c72cd067e5 100644 (file)
@@ -45,15 +45,17 @@ void mapgen_init(Server *srv)
        server->map->on_block_create = &generate_block;
 }
 
+#define RANGE 3
+
 static void *mapgen_thread(void *cliptr)
 {
        Client *client = cliptr;
 
        while (client->state != CS_DISCONNECTED) {
                v3s32 pos = map_node_to_block_pos((v3s32) {client->pos.x, client->pos.y, client->pos.z}, NULL);
-               for (s32 x = pos.x - 5; x < pos.x + 5; x++)
-                       for (s32 y = pos.y - 5; y < pos.y + 5; y++)
-                               for (s32 z = pos.z - 5; z < pos.z + 5; z++)
+               for (s32 x = pos.x - RANGE; x <= pos.x + RANGE; x++)
+                       for (s32 y = pos.y - RANGE; y <= pos.y + RANGE; y++)
+                               for (s32 z = pos.z - RANGE; z <= pos.z + RANGE; z++)
                                        map_get_block(client->server->map, (v3s32) {x, y, z}, true);
        }
 
diff --git a/src/old_map b/src/old_map
new file mode 100644 (file)
index 0000000..194fee4
Binary files /dev/null and b/src/old_map differ
index 07e9109f7984e3c4700ac798e0c7b33ff7e39df1..8975d6a39599d31cbec62dfe7d258d1b8ee262a0 100644 (file)
@@ -88,10 +88,24 @@ static bool kick_handler(Client *client, bool good)
        return true;
 }
 
+static bool pos_handler(Client *client, bool good)
+{
+       v3f pos;
+
+       if (! read_v3f32(client->fd, &pos))
+               return false;
+
+       if (good)
+               client->pos = pos;
+
+       return true;
+}
+
 CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
        {0},
        {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
        {&auth_handler, "AUTH", CS_CREATED},
        {&setnode_handler, "SETNODE", CS_ACTIVE},
        {&kick_handler, "KICK", CS_ACTIVE},
+       {&pos_handler, "POS", CS_ACTIVE},
 };
index 57a6fc9e3a2889ea9d4f578698ff10ebb1673abc..5e7c70202cfff5a1c17a29b080a213840f780057 100644 (file)
@@ -8,6 +8,7 @@ typedef enum
        SC_AUTH,
        SC_SETNODE,
        SC_KICK,
+       SC_POS,
        SERVER_COMMAND_COUNT,
 } ServerCommand;
 
index 3b5971478ab666b60df5f1ab7a708d5f44106ddf..2a101272cdc648c68d7bc1fa16d60621317842f0 100644 (file)
@@ -80,3 +80,27 @@ DEFTYPES(8)
 DEFTYPES(16)
 DEFTYPES(32)
 DEFTYPES(64)
+
+#define DEFFLOAT(type) \
+       bool read_ ## type(int fd, type *buf) \
+       { \
+               int n_read; \
+               if ((n_read = read(fd, buf, sizeof(type))) != sizeof(type)) { \
+                       if (n_read == -1) \
+                               perror("read"); \
+                       return false; \
+               } \
+               return true; \
+       } \
+       bool write_ ## type(int fd, type val) \
+       { \
+               if (write(fd, &val, sizeof(val)) == -1) { \
+                       perror("write"); \
+                       return false; \
+               } \
+               return true; \
+       } \
+       DEFVEC(type)
+
+DEFFLOAT(f32)
+DEFFLOAT(f64)
index cbf9baaf3cbf9baf6aa6c8e974a89a9c871e749b..435f5069458a6c8ad12b21576e841dfbd8ab5e12 100644 (file)
@@ -31,8 +31,8 @@ DEFTYPES(64)
 typedef float f32;
 typedef double f64;
 
-DEFVEC(f32)
-DEFVEC(f64)
+DEFTYP(float, f32)
+DEFTYP(double, f64)
 
 typedef v2f32 v2f;
 typedef v3f32 v3f;