Skip to content

Commit

Permalink
Redesign map handling
Browse files Browse the repository at this point in the history
  • Loading branch information
LizzyFleckenstein03 committed Mar 29, 2021
1 parent f97837f commit ea823a4
Show file tree
Hide file tree
Showing 14 changed files with 229 additions and 149 deletions.
4 changes: 2 additions & 2 deletions src/Makefile
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
COMMON = array.o list.o map.o signal.o util.o types.o node.o
SERVER = $(COMMON) server.o servercommands.o mapgen.o perlin.o
CLIENT = $(COMMON) client.o clientcommands.o mesh.o scene.o mapblock_meshgen.o shaders.o
SERVER = $(COMMON) server.o servercommands.o servermap.o
CLIENT = $(COMMON) client.o clientcommands.o clientmap.o mesh.o scene.o shaders.o
LIBRARIES = -lpthread -lm
CLIENT_LIBRARIES = -lGL -lGLEW -lglfw
FLAGS = -g -fmax-errors=4
Expand Down
6 changes: 3 additions & 3 deletions src/client.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <GL/gl.h>
#include <GLFW/glfw3.h>
#include "client.h"
#include "mapblock_meshgen.h"
#include "clientmap.h"
#include "signal.h"
#include "shaders.h"
#include "util.h"
Expand Down Expand Up @@ -200,7 +200,7 @@ static void client_start(int fd)
client.scene = scene_create();
client.pos = (v3f) {0.0f, 0.0f, 0.0f};

mapblock_meshgen_init(client.map, client.scene);
clientmap_init(&client);

pthread_t recv_thread;
pthread_create(&recv_thread, NULL, &reciever_thread, NULL);
Expand All @@ -214,7 +214,7 @@ static void client_start(int fd)
if (client.name)
free(client.name);

mapblock_meshgen_stop();
clientmap_deinit();

map_delete(client.map);
scene_delete(client.scene);
Expand Down
8 changes: 7 additions & 1 deletion src/clientcommands.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <stdio.h>
#include "client.h"
#include "clientmap.h"
#include "types.h"

static bool disconnect_handler(Client *client, bool good)
Expand Down Expand Up @@ -33,7 +34,12 @@ static bool auth_handler(Client *client, bool good)

static bool block_handler(Client *client, bool good)
{
return map_deserialize_block(client->fd, client->map, ! good);
MapBlock *block;
if (! map_deserialize_block(client->fd, client->map, &block, ! good))
return false;
if (good)
clientmap_block_changed(block);
return true;
}

CommandHandler command_handlers[CLIENT_COMMAND_COUNT] = {
Expand Down
40 changes: 19 additions & 21 deletions src/mapblock_meshgen.c → src/clientmap.c
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
#include <stdlib.h>
#include "node.h"
#include "mapblock_meshgen.h"
#include "clientmap.h"

static struct
{
Map *map;
Scene *scene;
List queue;
pthread_mutex_t mtx;
pthread_t thread;
bool cancel;
} meshgen;

static Client *client = NULL;

static v3f vpos[6][6] = {
{
{-0.5f, -0.5f, -0.5f},
Expand Down Expand Up @@ -122,9 +121,9 @@ static void *meshgen_thread(void *unused)
while (! meshgen.cancel) {
ListPair **lptr = &meshgen.queue.first;
if (*lptr) {
MapBlock *block = (*lptr)->key;

pthread_mutex_lock(&meshgen.mtx);
MapBlock *block = (*lptr)->key;
block->state = MBS_READY;
ListPair *next = (*lptr)->next;
free(*lptr);
*lptr = next;
Expand All @@ -137,7 +136,7 @@ static void *meshgen_thread(void *unused)
mesh = mesh_create(vertices.ptr, vertices.siz);
mesh->pos = (v3f) {block->pos.x * 16.0f - 8.0f, block->pos.y * 16.0f - 8.0f, block->pos.z * 16.0f - 8.0f};
mesh_transform(mesh);
scene_add_mesh(meshgen.scene, mesh);
scene_add_mesh(client->scene, mesh);
}

if (block->extra)
Expand All @@ -152,29 +151,28 @@ static void *meshgen_thread(void *unused)
return NULL;
}

static void enqueue_block(MapBlock *block)
void clientmap_init(Client *cli)
{
pthread_mutex_lock(&meshgen.mtx);
list_put(&meshgen.queue, block, NULL);
pthread_mutex_unlock(&meshgen.mtx);
}

void mapblock_meshgen_init(Map *map, Scene *scene)
{
meshgen.map = map;
meshgen.scene = scene;
client = cli;
meshgen.queue = list_create(NULL);
pthread_mutex_init(&meshgen.mtx, NULL);
map->on_block_add = &enqueue_block;
map->on_block_change = &enqueue_block;
pthread_create(&meshgen.thread, NULL, &meshgen_thread, NULL);
}

void mapblock_meshgen_stop()
void clientmap_deinit()
{
meshgen.cancel = true;
pthread_join(meshgen.thread, NULL);
pthread_mutex_destroy(&meshgen.mtx);
ITERATE_LIST(&meshgen.queue, pair) free(pair->key);
list_clear(&meshgen.queue);
}

void clientmap_block_changed(MapBlock *block)
{
pthread_mutex_lock(&meshgen.mtx);
if (block->state != MBS_PROCESSING) {
block->state = MBS_PROCESSING;
list_put(&meshgen.queue, block, NULL);
}
pthread_mutex_unlock(&meshgen.mtx);
}
11 changes: 11 additions & 0 deletions src/clientmap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#ifndef _CLIENTMAP_H_
#define _CLIENTMAP_H_

#include "client.h"

void clientmap_init(Client *cli);
void clientmap_deinit();

void clientmap_block_changed(MapBlock *block);

#endif
30 changes: 15 additions & 15 deletions src/map.c
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,9 @@ static MapBlock *allocate_block(v3s32 pos)
{
MapBlock *block = malloc(sizeof(MapBlock));
block->pos = pos;
block->ready = false;
block->state = MBS_CREATED;
block->extra = NULL;
pthread_mutex_init(&block->mtx, NULL);
return block;
}

Expand Down Expand Up @@ -96,19 +97,17 @@ MapBlock *map_get_block(Map *map, v3s32 pos, bool create)
} else if (create) {
block = allocate_block(pos);
array_insert(&sector->blocks, &block, res.index);

if (map->on_block_create)
map->on_block_create(block);
} else {
return NULL;
}

return block->ready ? block : NULL;
return (create || block->state == MBS_READY) ? block : NULL;
}

void map_free_block(MapBlock *block)
{
ITERATE_MAPBLOCK map_node_clear(&block->data[x][y][z]);
pthread_mutex_destroy(&block->mtx);
free(block);
}

Expand Down Expand Up @@ -140,7 +139,7 @@ bool map_serialize_block(int fd, MapBlock *block)
return true;
}

bool map_deserialize_block(int fd, Map *map, bool dummy)
bool map_deserialize_block(int fd, Map *map, MapBlock **blockptr, bool dummy)
{
v3s32 pos;

Expand All @@ -162,18 +161,17 @@ bool map_deserialize_block(int fd, Map *map, bool dummy)
}

ITERATE_MAPBLOCK {
if (! map_deserialize_node(fd, &block->data[x][y][z]))
if (! map_deserialize_node(fd, &block->data[x][y][z])) {
if (dummy)
map_free_block(block);
return false;
}
}

if (dummy) {
if (dummy)
map_free_block(block);
} else {
block->ready = true;

if (map->on_block_add)
map->on_block_add(block);
}
else if (blockptr)
*blockptr = block;

return true;
}
Expand All @@ -191,7 +189,7 @@ bool map_serialize(int fd, Map *map)

void map_deserialize(int fd, Map *map)
{
while (map_deserialize_block(fd, map, false))
while (map_deserialize_block(fd, map, NULL, false))
;
}

Expand Down Expand Up @@ -220,6 +218,8 @@ void map_set_node(Map *map, v3s32 pos, MapNode node)
MapNode *current_node = &block->data[offset.x][offset.y][offset.z];
map_node_clear(current_node);
*current_node = node;

block->state = MBS_MODIFIED;
}
}

Expand Down
19 changes: 12 additions & 7 deletions src/map.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define _MAP_H_

#include <stdbool.h>
#include <pthread.h>
#include "array.h"
#include "list.h"
#include "node.h"
Expand All @@ -15,11 +16,20 @@ typedef struct
List meta;
} MapNode;

typedef enum
{
MBS_CREATED,
MBS_PROCESSING,
MBS_READY,
MBS_MODIFIED,
} MapBlockState;

typedef struct
{
MapNode data[16][16][16];
v3s32 pos;
bool ready;
MapBlockState state;
pthread_mutex_t mtx;
void *extra;
} MapBlock;

Expand All @@ -30,14 +40,9 @@ typedef struct
u64 hash;
} MapSector;

typedef void (*MapBlockCallback)(MapBlock *block);

typedef struct
{
Array sectors;
MapBlockCallback on_block_create;
MapBlockCallback on_block_add;
MapBlockCallback on_block_change;
} Map;

Map *map_create();
Expand All @@ -52,7 +57,7 @@ void map_free_block(MapBlock *block);

bool map_deserialize_node(int fd, MapNode *buf);
bool map_serialize_block(int fd, MapBlock *block);
bool map_deserialize_block(int fd, Map *map, bool dummy);
bool map_deserialize_block(int fd, Map *map, MapBlock **blockptr, bool dummy);
bool map_serialize(int fd, Map *map);
void map_deserialize(int fd, Map *map);

Expand Down
10 changes: 0 additions & 10 deletions src/mapblock_meshgen.h

This file was deleted.

71 changes: 0 additions & 71 deletions src/mapgen.c

This file was deleted.

1 change: 0 additions & 1 deletion src/perlin.c

This file was deleted.

4 changes: 2 additions & 2 deletions src/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <unistd.h>
#include <errno.h>
#include <netdb.h>
#include "mapgen.h"
#include "server.h"
#include "servermap.h"
#include "signal.h"
#include "util.h"

Expand Down Expand Up @@ -91,7 +91,7 @@ void server_start(int fd)
perror("fopen");
}

mapgen_init(&server);
servermap_init(&server);

while (! interrupted)
server_accept_client();
Expand Down
Loading

0 comments on commit ea823a4

Please sign in to comment.