diff --git a/src/client.c b/src/client.c index 649f9fe..4712c1f 100644 --- a/src/client.c +++ b/src/client.c @@ -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) { diff --git a/src/mapblock_meshgen.c b/src/mapblock_meshgen.c index db82edd..10bf853 100644 --- a/src/mapblock_meshgen.c +++ b/src/mapblock_meshgen.c @@ -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 { diff --git a/src/mapgen.c b/src/mapgen.c index 7bbcfc9..f06c6f1 100644 --- a/src/mapgen.c +++ b/src/mapgen.c @@ -1,16 +1,69 @@ #include +#include #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); } diff --git a/src/mapgen.h b/src/mapgen.h index 8062b6a..0c686c0 100644 --- a/src/mapgen.h +++ b/src/mapgen.h @@ -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 diff --git a/src/node.c b/src/node.c index a37827a..161df5c 100644 --- a/src/node.c +++ b/src/node.c @@ -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) diff --git a/src/server.c b/src/server.c index cf360d6..ee5a068 100644 --- a/src/server.c +++ b/src/server.c @@ -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(); diff --git a/src/server.h b/src/server.h index 72c0f4b..8cdfcee 100644 --- a/src/server.h +++ b/src/server.h @@ -25,6 +25,7 @@ typedef struct Client char *name; Server *server; pthread_t thread; + v3f pos; } Client; typedef enum diff --git a/src/servercommands.c b/src/servercommands.c index 1d9e241..07e9109 100644 --- a/src/servercommands.c +++ b/src/servercommands.c @@ -1,5 +1,6 @@ #include #include +#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}, }; diff --git a/src/servercommands.h b/src/servercommands.h index 4e306fa..57a6fc9 100644 --- a/src/servercommands.h +++ b/src/servercommands.h @@ -6,7 +6,6 @@ typedef enum SERVER_COMMAND_NULL, SC_DISCONNECT, SC_AUTH, - SC_GETBLOCK, SC_SETNODE, SC_KICK, SERVER_COMMAND_COUNT,