eco2d/code/common/world/world.c

238 lines
6.9 KiB
C
Raw Normal View History

2021-05-04 17:41:30 +00:00
#include "zpl.h"
#include "librg.h"
#include "modules/general.h"
2021-05-05 13:14:02 +00:00
#include "modules/net.h"
2021-05-04 17:41:30 +00:00
#include "world/world.h"
2021-05-05 13:14:02 +00:00
#include "packets/pkt_send_librg_update.h"
2021-05-04 17:41:30 +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;
2021-05-05 13:52:28 +00:00
uint64_t tracker_update;
2021-05-04 17:41:30 +00:00
ecs_world_t *ecs;
librg_world *tracker;
world_pkt_reader_proc *reader_proc;
world_pkt_writer_proc *writer_proc;
} world_data;
static world_data world = {0};
2021-05-05 13:52:28 +00:00
#define WORLD_TRACKER_UPDATE_MS 100
2021-05-04 17:41:30 +00:00
int32_t world_gen();
2021-05-05 22:03:43 +00:00
int32_t tracker_read_create(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);
zpl_printf("[INFO] An entity %d was created for owner: %d\n", (int)entity_id, (int)owner_id);
return 0;
}
int32_t tracker_read_remove(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);
zpl_printf("[INFO] An entity %d was removed for owner: %d\n", (int)entity_id, (int)owner_id);
return 0;
}
int32_t tracker_read_update(librg_world *w, librg_event *e) {
// int64_t entity_id = librg_event_entity_get(w, e);
size_t actual_length = librg_event_size_get(w, e);
char *buffer = librg_event_buffer_get(w, e);
return 0;
}
int32_t tracker_write_create(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;
}
int32_t tracker_write_remove(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;
}
int32_t tracker_write_update(librg_world *w, librg_event *e) {
// int64_t entity_id = librg_event_entity_get(w, e);
#if 0
size_t actual_length = librg_event_size_get(w, e);
char *buffer = librg_event_buffer_get(w, e);
#endif
return 0;
}
2021-05-04 17:41:30 +00:00
int32_t world_init_minimal(uint16_t block_size, uint16_t chunk_size, uint16_t world_size, world_pkt_reader_proc *reader_proc, world_pkt_writer_proc *writer_proc) {
world.chunk_size = chunk_size;
world.world_size = world_size;
if (reader_proc) world.reader_proc = reader_proc;
if (writer_proc) world.writer_proc = writer_proc;
world.width = chunk_size * world_size;
world.height = chunk_size * world_size;
world.block_size = block_size;
2021-05-04 20:32:12 +00:00
world.size = world.width * world.height;
2021-05-04 17:41:30 +00:00
if (world.tracker != NULL) {
librg_world_destroy(world.tracker);
world.tracker = NULL;
}
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;
}
/* 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-05-05 22:03:43 +00:00
librg_event_set(world.tracker, LIBRG_READ_CREATE, tracker_read_create);
librg_event_set(world.tracker, LIBRG_READ_REMOVE, tracker_read_remove);
librg_event_set(world.tracker, LIBRG_READ_UPDATE, tracker_read_update);
librg_event_set(world.tracker, LIBRG_WRITE_CREATE, tracker_write_create);
librg_event_set(world.tracker, LIBRG_WRITE_REMOVE, tracker_write_remove);
librg_event_set(world.tracker, LIBRG_WRITE_UPDATE, tracker_write_update);
2021-05-04 17:41:30 +00:00
return 0;
}
int32_t world_init(int32_t seed, uint16_t block_size, uint16_t chunk_size, uint16_t world_size, world_pkt_reader_proc *reader_proc, world_pkt_writer_proc *writer_proc) {
if (world.data) {
return 0;
}
world.seed = seed;
2021-05-04 20:32:12 +00:00
world_init_minimal(block_size, chunk_size, world_size, reader_proc, writer_proc);
2021-05-04 17:41:30 +00:00
world.data = zpl_malloc(sizeof(uint8_t)*world.size);
2021-05-05 13:52:28 +00:00
world.tracker_update = 0;
2021-05-04 17:41:30 +00:00
if (!world.data) {
return WORLD_ERROR_OUTOFMEM;
}
world.ecs = ecs_init();
ecs_set_entity_range(world.ecs, 0, UINT32_MAX);
ECS_IMPORT(world.ecs, General);
for (int i = 0; i < chunk_size * chunk_size; ++i) {
ecs_entity_t e = ecs_new(world.ecs, 0);
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);
}
zpl_printf("[INFO] Created a new server world\n");
return world_gen();
}
int32_t world_destroy(void) {
librg_world_destroy(world.tracker);
ecs_fini(world.ecs);
zpl_mfree(world.data);
zpl_memset(&world, 0, sizeof(world));
zpl_printf("[INFO] World was destroyed.\n");
return WORLD_ERROR_NONE;
}
2021-05-05 13:52:28 +00:00
static void world_tracker_update(void) {
if (world.tracker_update > zpl_time_rel_ms()) return;
2021-05-05 22:03:43 +00:00
world.tracker_update = zpl_time_rel_ms() + WORLD_TRACKER_UPDATE_MS;
2021-05-05 13:14:02 +00:00
ECS_IMPORT(world.ecs, Net);
ecs_query_t *query = ecs_query_new(world.ecs, "Net.ClientInfo");
2021-05-05 13:52:28 +00:00
2021-05-05 13:14:02 +00:00
ecs_iter_t it = ecs_query_iter(query);
static char buffer[16000] = {0};
static int32_t datalen = 16000;
2021-05-05 13:52:28 +00:00
2021-05-05 13:14:02 +00:00
while (ecs_query_next(&it)) {
ClientInfo *p = ecs_column(&it, ClientInfo, 1);
2021-05-05 13:52:28 +00:00
2021-05-05 13:14:02 +00:00
for (int i = 0; i < it.count; i++) {
datalen = 16000;
int32_t result = librg_world_write(world_tracker(), it.entities[i], buffer, &datalen, NULL);
2021-05-05 13:52:28 +00:00
2021-05-05 13:14:02 +00:00
if (result > 0) {
zpl_printf("[info] buffer size was not enough, please increase it by at least: %d\n", result);
} else if (result < 0) {
zpl_printf("[error] an error happened writing the world %d\n", result);
}
2021-05-05 13:52:28 +00:00
2021-05-05 13:14:02 +00:00
pkt_world_write(MSG_ID_LIBRG_UPDATE, pkt_send_librg_update_encode(buffer, datalen), 1, p[i].peer);
}
}
2021-05-05 13:52:28 +00:00
}
int32_t world_update() {
ecs_progress(world.ecs, 0);
world_tracker_update();
2021-05-04 17:41:30 +00:00
return 0;
}
2021-05-04 19:22:55 +00:00
int32_t world_read(void* data, uint32_t datalen, void *udata) {
2021-05-04 17:41:30 +00:00
if (world.reader_proc) {
2021-05-04 19:22:55 +00:00
return world.reader_proc(data, datalen, udata);
2021-05-04 17:41:30 +00:00
}
return -1;
}
2021-05-04 19:22:55 +00:00
int32_t world_write(pkt_header *pkt, void *udata) {
2021-05-04 17:41:30 +00:00
if (world.writer_proc) {
2021-05-04 19:22:55 +00:00
return world.writer_proc(pkt, udata);
2021-05-04 17:41:30 +00:00
}
return -1;
}
uint32_t world_buf(uint8_t const **ptr, uint32_t *width) {
ZPL_ASSERT_NOT_NULL(world.data);
ZPL_ASSERT_NOT_NULL(ptr);
*ptr = world.data;
if (width) *width = world.width;
return world.size;
}
ecs_world_t * world_ecs() {
return world.ecs;
}
librg_world * world_tracker() {
return world.tracker;
}
2021-05-04 19:22:55 +00:00
uint16_t world_block_size(void) {
return world.block_size;
}
2021-05-04 17:41:30 +00:00
uint16_t world_chunk_size(void) {
return world.chunk_size;
}
2021-05-04 19:22:55 +00:00
uint16_t world_world_size(void) {
2021-05-04 17:41:30 +00:00
return world.world_size;
}
#include "world_gen.c"