Skip to content

Commit

Permalink
Redesign MapBlock sending
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Mar 29, 2021
1 parent 240d28a commit a46f592
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 50 deletions.
16 changes: 2 additions & 14 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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) {
Expand Down
2 changes: 1 addition & 1 deletion src/mapblock_meshgen.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
61 changes: 57 additions & 4 deletions src/mapgen.c
Original file line number Diff line number Diff line change
@@ -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);
}
4 changes: 3 additions & 1 deletion src/mapgen.h
Original file line number Diff line number Diff line change
@@ -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
8 changes: 4 additions & 4 deletions src/node.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 2 additions & 1 deletion src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -90,7 +91,7 @@ void server_start(int fd)
perror("fopen");
}

mapgen_init(server.map);
mapgen_init(&server);

while (! interrupted)
server_accept_client();
Expand Down
1 change: 1 addition & 0 deletions src/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ typedef struct Client
char *name;
Server *server;
pthread_t thread;
v3f pos;
} Client;

typedef enum
Expand Down
27 changes: 3 additions & 24 deletions src/servercommands.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
#include "mapgen.h"
#include "server.h"
#include "util.h"

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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},
};
1 change: 0 additions & 1 deletion src/servercommands.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ typedef enum
SERVER_COMMAND_NULL,
SC_DISCONNECT,
SC_AUTH,
SC_GETBLOCK,
SC_SETNODE,
SC_KICK,
SERVER_COMMAND_COUNT,
Expand Down

0 comments on commit a46f592

Please sign in to comment.