eco2d/code/apps/server/source/world/world.c

136 lines
3.6 KiB
C
Raw Normal View History

2021-01-11 14:42:36 +00:00
#include "zpl.h"
#include "librg.h"
2021-01-17 15:33:56 +00:00
#include "components/general.h"
#include "world/world.h"
2021-01-11 13:47:14 +00:00
2021-01-14 16:37:20 +00:00
typedef struct {
uint8_t *data;
uint32_t seed;
uint32_t size;
uint32_t width;
uint32_t height;
uint16_t block_size;
uint16_t chunk_size;
uint16_t world_size;
ecs_world_t *ecs;
librg_world *tracker;
2021-01-14 16:37:20 +00:00
} world_data;
2021-01-14 16:37:20 +00:00
static world_data world = {0};
2021-01-11 14:51:42 +00:00
2021-01-11 17:30:47 +00:00
int32_t world_gen();
int32_t world_write_update(librg_world *w, librg_event *e) {
int64_t owner_id = librg_event_owner_get(w, e);
int64_t entity_id = librg_event_entity_get(w, e);
return 0;
// /* prevent sending updates to users who own that entity */
// /* since they will be responsible on telling where that entity is supposed to be */
// if (librg_entity_owner_get(w, entity_id) == owner_id) {
// return LIBRG_WRITE_REJECT;
// }
// /* read our current position */
// ENetPeer *peer = (ENetPeer *)librg_entity_userdata_get(w, entity_id);
// char *buffer = librg_event_buffer_get(w, e);
// size_t max_length = librg_event_size_get(w, e);
// /* check if we have enough space to write and valid position */
// if (sizeof(vec3) > max_length || !peer->data) {
// return LIBRG_WRITE_REJECT;
// }
// /* write data and return how much we've written */
// memcpy(buffer, peer->data, sizeof(vec3));
// return sizeof(vec3);
}
int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size) {
2021-01-14 16:37:20 +00:00
if (world.data) {
2021-01-11 14:48:17 +00:00
world_destroy();
}
2021-01-14 16:37:20 +00:00
world.seed = seed;
world.block_size = block_size;
world.chunk_size = chunk_size;
world.world_size = world_size;
world.width = chunk_size * world_size;
world.height = chunk_size * world_size;
world.size = world.width * world.height;
2021-01-14 16:37:20 +00:00
world.data = zpl_malloc(sizeof(uint8_t)*world.size);
2021-01-11 14:48:17 +00:00
2021-01-14 16:37:20 +00:00
if (!world.data) {
2021-01-11 14:48:17 +00:00
return WORLD_ERROR_OUTOFMEM;
}
2021-01-17 11:05:29 +00:00
world.ecs = ecs_init();
world.tracker = librg_world_create();
if (world.tracker == NULL) {
zpl_printf("[ERROR] An error occurred while trying to create a server world.\n");
return WORLD_ERROR_TRACKER_FAILED;
}
zpl_printf("[INFO] Created a new server world\n");
/* config our world grid */
librg_config_chunksize_set(world.tracker, block_size * chunk_size, block_size * chunk_size, 1);
librg_config_chunkamount_set(world.tracker, world_size, world_size, 1);
librg_config_chunkoffset_set(world.tracker, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID, LIBRG_OFFSET_MID);
2021-01-17 15:33:56 +00:00
ECS_IMPORT(world.ecs, Common);
for (int i = 0; i < chunk_size * chunk_size; ++i) {
ecs_entity_t e = ecs_new(world.ecs, 0);
2021-01-17 15:33:56 +00:00
ecs_set(world.ecs, e, Chunk, {
.x = i % chunk_size,
.y = i / chunk_size,
});
librg_entity_track(world.tracker, e);
librg_entity_chunk_set(world.tracker, e, i);
}
// librg_event_set(world.tracker, LIBRG_WRITE_UPDATE, world_write_update);
// librg_event_set(world.tracker, LIBRG_READ_UPDATE, server_read_update);
2021-01-11 17:30:47 +00:00
return world_gen();
2021-01-11 14:42:36 +00:00
}
int32_t world_destroy(void) {
librg_world_destroy(world.tracker);
ecs_fini(world.ecs);
2021-01-14 16:37:20 +00:00
zpl_mfree(world.data);
zpl_memset(&world, 0, sizeof(world));
2021-01-11 13:47:14 +00:00
return WORLD_ERROR_NONE;
}
2021-01-11 14:42:36 +00:00
int32_t world_update() {
ecs_progress(world.ecs, 0);
return 0;
}
2021-01-11 14:48:17 +00:00
uint32_t world_buf(uint8_t const **ptr, uint32_t *width) {
2021-01-14 16:37:20 +00:00
ZPL_ASSERT_NOT_NULL(world.data);
2021-01-11 14:42:36 +00:00
ZPL_ASSERT_NOT_NULL(ptr);
2021-01-14 16:37:20 +00:00
*ptr = world.data;
if (width) *width = world.width;
return world.size;
2021-01-11 14:42:36 +00:00
}
2021-01-11 16:49:00 +00:00
ecs_world_t * world_ecs() {
return world.ecs;
}
2021-01-17 11:05:29 +00:00
librg_world * world_tracker() {
return world.tracker;
}
2021-01-17 11:05:29 +00:00
#include "world_gen.c"