]> git.lizzy.rs Git - dragonblocks_alpha.git/commitdiff
Redesign MapBlock sending
authorElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 29 Mar 2021 13:17:12 +0000 (15:17 +0200)
committerElias Fleckenstein <eliasfleckenstein@web.de>
Mon, 29 Mar 2021 13:17:12 +0000 (15:17 +0200)
src/client.c
src/mapblock_meshgen.c
src/mapgen.c
src/mapgen.h
src/node.c
src/server.c
src/server.h
src/servercommands.c
src/servercommands.h

index 649f9fee784315525fe9b3689f3b402cdc502574..4712c1f8f745b6e0066624c76b1df6a8e32bd86a 100644 (file)
@@ -58,8 +58,8 @@ static void client_loop()
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
 
        int width, height;
-       width = 1024;
-       height = 768;
+       width = 1250;
+       height = 750;
 
        GLFWwindow *window = glfwCreateWindow(width, height, "Dragonblocks", NULL, NULL);
 
@@ -102,23 +102,11 @@ static void client_loop()
        glUniformMatrix4fv(prog->loc_view, 1, GL_FALSE, view[0]);
        glUniformMatrix4fv(prog->loc_projection, 1, GL_FALSE, projection[0]);
 
-       bool e_was_pressed = false;
-
        while (! glfwWindowShouldClose(window) && client.state != CS_DISCONNECTED && ! interrupted) {
                glEnable(GL_DEPTH_TEST);
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glClearColor(0.52941176470588f, 0.8078431372549f, 0.92156862745098f, 1.0f);
 
-               bool e_is_pressed = glfwGetKey(window, GLFW_KEY_E) == GLFW_PRESS;
-
-               if (e_is_pressed && ! e_was_pressed) {
-                       pthread_mutex_lock(&client.mtx);
-                       (void) (write_u32(client.fd, SC_GETBLOCK) && write_v3s32(client.fd, map_node_to_block_pos((v3s32) {pos.x, pos.y, pos.z}, NULL)));
-                       pthread_mutex_unlock(&client.mtx);
-               }
-
-               e_was_pressed = e_is_pressed;
-
                bool view_changed = false;
 
                if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
index db82edd7981696c46d7902591b361e4aa8d8d6ad..10bf853f87d13bcccfe0a51aa3ff603ee82d11aa 100644 (file)
@@ -141,7 +141,7 @@ static void *meshgen_thread(void *unused)
                        }
 
                        if (block->extra)
-                               mesh->remove = true;
+                               ((Mesh *) block->extra)->remove = true;
 
                        block->extra = mesh;
                } else {
index 7bbcfc9e1f1ed2905cc8f6deb3246e17f849bdd7..f06c6f14d0782bf4d179231eba1155a9f0da9ee6 100644 (file)
@@ -1,16 +1,69 @@
 #include <stdlib.h>
+#include <perlin/perlin.h>
 #include "mapgen.h"
 
+int seed = 0;
+static Server *server = NULL;
+
 // mapgen prototype
 static void generate_block(MapBlock *block)
 {
-       ITERATE_MAPBLOCK {
-               block->data[x][y][z] = map_node_create(rand() % 4 + 1);
+       for (u8 x = 0; x < 16; x++) {
+               s32 abs_x = x + block->pos.x * 16;
+               for (u8 z = 0; z < 16; z++) {
+                       s32 abs_z = z + block->pos.z * 16;
+                       s32 height = noise2d(abs_x / 16, abs_z / 16, 1, seed) * 16;
+                       for (u8 y = 0; y < 16; y++) {
+                               s32 abs_y = y + block->pos.y * 16;
+                               Node type;
+                               if (abs_y > height)
+                                       type = NODE_AIR;
+                               else if (abs_y == height)
+                                       type = NODE_GRASS;
+                               else if (abs_y >= height - 4)
+                                       type = NODE_DIRT;
+                               else
+                                       type = NODE_STONE;
+                               block->data[x][y][z] = map_node_create(type);
+                       }
+               }
+       }
+       ITERATE_LIST(&server->clients, pair) {
+               Client *client = pair->value;
+               if (client->state == CS_ACTIVE) {
+                       pthread_mutex_lock(&client->mtx);
+                       (void) (write_u32(client->fd, CC_BLOCK) && map_serialize_block(client->fd, block));
+                       pthread_mutex_unlock(&client->mtx);
+               }
        }
        block->ready = true;
 }
 
-void mapgen_init(Map *map)
+void mapgen_init(Server *srv)
+{
+       server = srv;
+       server->map->on_block_create = &generate_block;
+}
+
+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++)
+                                       map_get_block(client->server->map, (v3s32) {x, y, z}, true);
+       }
+
+       return NULL;
+}
+
+void mapgen_start_thread(Client *client)
 {
-       map->on_block_create = &generate_block;
+       (void) client;
+       (void) mapgen_thread;
+       pthread_t thread;
+       pthread_create(&thread, NULL, &mapgen_thread, client);
 }
index 8062b6a36ac8cd8341dfbadc188786f976f23ed5..0c686c0f1c979e2c9bc08b7324b6047c4d26e371 100644 (file)
@@ -1,8 +1,10 @@
 #ifndef _MAPGEN_H_
 #define _MAPGEN_H_
 
+#include "server.h"
 #include "map.h"
 
-void mapgen_init(Map *map);
+void mapgen_init(Server *srv);
+void mapgen_start_thread(Client *client);
 
 #endif
index a37827a6539d4e3c47ea509a1dd24b5a76f63e16..161df5cc8d3a724297031c0ed6c3b445476cb7c0 100644 (file)
@@ -2,11 +2,11 @@
 #include "util.h"
 
 NodeDefintion node_definitions[NODE_UNLOADED] = {
-       {true,  false, "#F44026", {0.0f, 0.0f, 0.0f}},
+       {true,  false, "#991300", {0.0f, 0.0f, 0.0f}},
        {false, false, "",        {0.0f, 0.0f, 0.0f}},
-       {true,  false, "#00CB1F", {0.0f, 0.0f, 0.0f}},
-       {true,  false, "#854025", {0.0f, 0.0f, 0.0f}},
-       {true,  false, "#7A7A7A", {0.0f, 0.0f, 0.0f}},
+       {true,  false, "#137822", {0.0f, 0.0f, 0.0f}},
+       {true,  false, "#6B3627", {0.0f, 0.0f, 0.0f}},
+       {true,  false, "#4F4F4F", {0.0f, 0.0f, 0.0f}},
 };
 
 v3f get_node_color(NodeDefintion *def)
index cf360d603932869360344decc3980f8827d6dec5..ee5a06836022fdffb26f8e6eea5e24a6d86cc34d 100644 (file)
@@ -71,6 +71,7 @@ static void server_accept_client()
        client->address = address_string((struct sockaddr_in6 *) &client_address);
        client->name = client->address;
        client->server = &server;
+       client->pos = (v3f) {0.0f, 0.0f, 0.0f};
        pthread_create(&client->thread, NULL, &server_reciever_thread, client);
 
        printf("Connected %s\n", client->address);
@@ -90,7 +91,7 @@ void server_start(int fd)
                perror("fopen");
        }
 
-       mapgen_init(server.map);
+       mapgen_init(&server);
 
        while (! interrupted)
                server_accept_client();
index 72c0f4ba3f328342508b128ad6bb734d46e1180e..8cdfcee6eb0d0a59e42fba1dcab7547867a7157d 100644 (file)
@@ -25,6 +25,7 @@ typedef struct Client
        char *name;
        Server *server;
        pthread_t thread;
+       v3f pos;
 } Client;
 
 typedef enum
index 1d9e241f9f624db646550fa92835e6f11e03b442..07e9109f7984e3c4700ac798e0c7b33ff7e39df1 100644 (file)
@@ -1,5 +1,6 @@
 #include <stdio.h>
 #include <stdlib.h>
+#include "mapgen.h"
 #include "server.h"
 #include "util.h"
 
@@ -40,39 +41,18 @@ static bool auth_handler(Client *client, bool good)
        if (success) {
                client->name = name;
                client->state = CS_ACTIVE;
+               mapgen_start_thread(client);
        } else {
                free(name);
        }
 
        pthread_mutex_lock(&client->mtx);
-       bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success) && send_map(client);
+       bool ret = write_u32(client->fd, CC_AUTH) && write_u8(client->fd, success) && (success ? send_map(client) : true);
        pthread_mutex_unlock(&client->mtx);
 
        return ret;
 }
 
-static bool getblock_handler(Client *client, bool good)
-{
-       v3s32 pos;
-
-       if (! read_v3s32(client->fd, &pos))
-               return false;
-
-       if (! good)
-               return true;
-
-       MapBlock *block = map_get_block(client->server->map, pos, true);
-       if (block) {
-               pthread_mutex_lock(&client->mtx);
-               bool ret = write_u32(client->fd, CC_BLOCK) && map_serialize_block(client->fd, block);
-               pthread_mutex_unlock(&client->mtx);
-
-               return ret;
-       }
-
-       return true;
-}
-
 static bool setnode_handler(Client *client, bool good)
 {
        v3s32 pos;
@@ -112,7 +92,6 @@ CommandHandler command_handlers[SERVER_COMMAND_COUNT] = {
        {0},
        {&disconnect_handler, "DISCONNECT", CS_CREATED | CS_ACTIVE},
        {&auth_handler, "AUTH", CS_CREATED},
-       {&getblock_handler, "GETBLOCK", CS_ACTIVE},
        {&setnode_handler, "SETNODE", CS_ACTIVE},
        {&kick_handler, "KICK", CS_ACTIVE},
 };
index 4e306fa06bb0c0c498b8eeca78fdc9fd0817285b..57a6fc9e3a2889ea9d4f578698ff10ebb1673abc 100644 (file)
@@ -6,7 +6,6 @@ typedef enum
        SERVER_COMMAND_NULL,
        SC_DISCONNECT,
        SC_AUTH,
-       SC_GETBLOCK,
        SC_SETNODE,
        SC_KICK,
        SERVER_COMMAND_COUNT,