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

71 lines
1.5 KiB
C
Raw Normal View History

2021-01-11 14:42:36 +00:00
#include "zpl.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;
2021-01-17 11:05:29 +00:00
uint16_t chunk_width;
uint16_t chunk_height;
uint16_t chunk_amountx;
uint16_t chunk_amounty;
2021-01-14 16:37:20 +00:00
} world_data;
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();
2021-01-17 11:05:29 +00:00
int32_t world_init(int32_t seed, uint16_t chunk_width, uint16_t chunk_height, uint16_t chunk_amountx, uint16_t chunk_amounty) {
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;
2021-01-17 11:05:29 +00:00
world.width = chunk_width * chunk_amountx;
world.height = chunk_height * chunk_amounty;
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
2021-01-11 17:30:47 +00:00
return world_gen();
2021-01-11 14:42:36 +00:00
}
int32_t world_destroy(void) {
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
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
#include "world_gen.c"
2021-01-17 11:05:29 +00:00
// 11111111111111111111111111111111111111111111111111222222222222222222222222222222222222222222222333333333333333333333333333333333333333
// world 3x3
// chunk 3x3
// 111 111 111
// 222 222 222
// 333 333 333
// 111 111 111
// 222 222 222
// 333 333 333
// 111 111 111
// 222 222 222
// 333 333 333