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);
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) {
}
if (block->extra)
- mesh->remove = true;
+ ((Mesh *) block->extra)->remove = true;
block->extra = mesh;
} else {
#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);
}
#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
#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)
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);
perror("fopen");
}
- mapgen_init(server.map);
+ mapgen_init(&server);
while (! interrupted)
server_accept_client();
char *name;
Server *server;
pthread_t thread;
+ v3f pos;
} Client;
typedef enum
#include <stdio.h>
#include <stdlib.h>
+#include "mapgen.h"
#include "server.h"
#include "util.h"
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;
{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},
};
SERVER_COMMAND_NULL,
SC_DISCONNECT,
SC_AUTH,
- SC_GETBLOCK,
SC_SETNODE,
SC_KICK,
SERVER_COMMAND_COUNT,