the rest
parent
481cabf74b
commit
e5d45a8c67
|
@ -1,9 +0,0 @@
|
||||||
#pragma once
|
|
||||||
|
|
||||||
uint64_t world_chunk_size(void) {
|
|
||||||
return 4;
|
|
||||||
}
|
|
||||||
|
|
||||||
uint64_t world_chunk_amount(void) {
|
|
||||||
return 8;
|
|
||||||
}
|
|
|
@ -1,6 +0,0 @@
|
||||||
#pragma once
|
|
||||||
#include "packet_utils.h"
|
|
||||||
#include "packet.h"
|
|
||||||
|
|
||||||
// NOTE(zaklaus): pkt data
|
|
||||||
|
|
|
@ -0,0 +1,49 @@
|
||||||
|
#include "zpl.h"
|
||||||
|
#include "world/blocks.h"
|
||||||
|
|
||||||
|
// todo: csv parsing + utils
|
||||||
|
|
||||||
|
#define BLOCKS_COUNT (sizeof(blocks)/sizeof(block))
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t tex_id;
|
||||||
|
char *name;
|
||||||
|
uint32_t flags;
|
||||||
|
uint32_t kind;
|
||||||
|
uint32_t biome;
|
||||||
|
char symbol;
|
||||||
|
} block;
|
||||||
|
|
||||||
|
#include "blocks_list.c"
|
||||||
|
|
||||||
|
uint8_t blocks_find(uint32_t biome, uint32_t kind) {
|
||||||
|
for (int i=0; i<BLOCKS_COUNT; i++) {
|
||||||
|
if (blocks[i].biome == biome && blocks[i].kind == kind)
|
||||||
|
return i;
|
||||||
|
}
|
||||||
|
return BLOCK_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *blocks_get_name(uint8_t id) {
|
||||||
|
return blocks[id].name;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t blocks_get_tex_id(uint8_t id) {
|
||||||
|
return blocks[id].tex_id;
|
||||||
|
}
|
||||||
|
|
||||||
|
char blocks_get_symbol(uint8_t id) {
|
||||||
|
return blocks[id].symbol;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t blocks_get_flags(uint8_t id) {
|
||||||
|
return blocks[id].flags;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t blocks_get_biome(uint8_t id) {
|
||||||
|
return blocks[id].biome;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint32_t blocks_get_kind(uint8_t id) {
|
||||||
|
return blocks[id].kind;
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
#pragma once
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
|
#define BLOCK_INVALID 0xF
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BLOCK_FLAG_COLLISION = (1 << 0)
|
||||||
|
} block_flags;
|
||||||
|
|
||||||
|
#include "blocks_info.h"
|
||||||
|
|
||||||
|
uint8_t blocks_find(uint32_t biome, uint32_t kind);
|
||||||
|
|
||||||
|
char *blocks_get_name(uint8_t id);
|
||||||
|
uint8_t blocks_get_tex_id(uint8_t id);
|
||||||
|
char blocks_get_symbol(uint8_t id);
|
||||||
|
uint32_t blocks_get_flags(uint8_t id);
|
||||||
|
uint32_t blocks_get_biome(uint8_t id);
|
||||||
|
uint32_t blocks_get_kind(uint8_t id);
|
|
@ -0,0 +1,20 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BLOCK_KIND_DEV,
|
||||||
|
BLOCK_KIND_GROUND,
|
||||||
|
BLOCK_KIND_WATER,
|
||||||
|
BLOCK_KIND_WALL,
|
||||||
|
BLOCK_KIND_HILL,
|
||||||
|
BLOCK_KIND_HILL_SNOW,
|
||||||
|
BLOCK_KIND_HOLE,
|
||||||
|
} block_kind;
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
BLOCK_BIOME_DEV,
|
||||||
|
BLOCK_BIOME_PLAIN,
|
||||||
|
BLOCK_BIOME_FOREST,
|
||||||
|
BLOCK_BIOME_DESERT,
|
||||||
|
BLOCK_BIOME_ICE,
|
||||||
|
BLOCK_BIOME_OCEAN,
|
||||||
|
} block_biome;
|
|
@ -0,0 +1,10 @@
|
||||||
|
#include "atlas_shared.h"
|
||||||
|
#include "world/blocks.h"
|
||||||
|
|
||||||
|
static block blocks[] = {
|
||||||
|
{.tex_id = ATLAS_XY(0, 0), .name = "base-ground", .flags = 0, .kind = BLOCK_KIND_GROUND, .biome = 0, .symbol = '.'},
|
||||||
|
{.tex_id = ATLAS_XY(1, 0), .name = "base-wall", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WALL, .biome = 0, .symbol = '#'},
|
||||||
|
{.tex_id = ATLAS_XY(2, 0), .name = "base-hill", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL, .biome = 0, .symbol = '^'},
|
||||||
|
{.tex_id = ATLAS_XY(3, 0), .name = "base-hill-snow", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_HILL_SNOW, .biome = 0, .symbol = '*'},
|
||||||
|
{.tex_id = ATLAS_XY(0, 1), .name = "base-water", .flags = BLOCK_FLAG_COLLISION, .kind = BLOCK_KIND_WATER, .biome = 0, .symbol = '~'},
|
||||||
|
};
|
|
@ -0,0 +1,70 @@
|
||||||
|
#include <math.h>
|
||||||
|
#include "world/perlin.h"
|
||||||
|
|
||||||
|
// adapted from: https://gist.github.com/nowl/828013#gistcomment-2807232
|
||||||
|
|
||||||
|
static const uint8_t PERLIN_PERM_TABLE[] = {
|
||||||
|
208,34,231,213,32,248,233,56,161,78,24,140,71,48,140,254,245,255,247,247,40,
|
||||||
|
185,248,251,245,28,124,204,204,76,36,1,107,28,234,163,202,224,245,128,167,204,
|
||||||
|
9,92,217,54,239,174,173,102,193,189,190,121,100,108,167,44,43,77,180,204,8,81,
|
||||||
|
70,223,11,38,24,254,210,210,177,32,81,195,243,125,8,169,112,32,97,53,195,13,
|
||||||
|
203,9,47,104,125,117,114,124,165,203,181,235,193,206,70,180,174,0,167,181,41,
|
||||||
|
164,30,116,127,198,245,146,87,224,149,206,57,4,192,210,65,210,129,240,178,105,
|
||||||
|
228,108,245,148,140,40,35,195,38,58,65,207,215,253,65,85,208,76,62,3,237,55,89,
|
||||||
|
232,50,217,64,244,157,199,121,252,90,17,212,203,149,152,140,187,234,177,73,174,
|
||||||
|
193,100,192,143,97,53,145,135,19,103,13,90,135,151,199,91,239,247,33,39,145,
|
||||||
|
101,120,99,3,186,86,99,41,237,203,111,79,220,135,158,42,30,154,120,67,87,167,
|
||||||
|
135,176,183,191,253,115,184,21,233,58,129,233,142,39,128,211,118,137,139,255,
|
||||||
|
114,20,218,113,154,27,127,246,250,1,8,198,250,209,92,222,173,21,88,102,219
|
||||||
|
};
|
||||||
|
|
||||||
|
static int32_t perlin_noise2_sample(int32_t seed, int32_t x, int32_t y) {
|
||||||
|
int32_t yindex = (y + seed) % 256;
|
||||||
|
if (yindex < 0)
|
||||||
|
yindex += 256;
|
||||||
|
int32_t xindex = (PERLIN_PERM_TABLE[yindex] + x) % 256;
|
||||||
|
if (xindex < 0)
|
||||||
|
xindex += 256;
|
||||||
|
return PERLIN_PERM_TABLE[xindex];
|
||||||
|
}
|
||||||
|
|
||||||
|
static double perlin_lerp(double x, double y, double t) {
|
||||||
|
return x + t*(y - x);
|
||||||
|
}
|
||||||
|
|
||||||
|
static double perlin_smooth_lerp(double x, double y, double t) {
|
||||||
|
return perlin_lerp(x, y, t * t * (3-2*t));
|
||||||
|
}
|
||||||
|
|
||||||
|
double perlin_noise2d(int32_t seed, double x, double y) {
|
||||||
|
int32_t x_int = floor(x);
|
||||||
|
int32_t y_int = floor(y);
|
||||||
|
double x_frac = x - x_int;
|
||||||
|
double y_frac = y - y_int;
|
||||||
|
int32_t s = perlin_noise2_sample(seed, x_int, y_int);
|
||||||
|
int32_t t = perlin_noise2_sample(seed, x_int+1, y_int);
|
||||||
|
int32_t u = perlin_noise2_sample(seed, x_int, y_int+1);
|
||||||
|
int32_t v = perlin_noise2_sample(seed, x_int+1, y_int+1);
|
||||||
|
double low = perlin_smooth_lerp(s, t, x_frac);
|
||||||
|
double high = perlin_smooth_lerp(u, v, x_frac);
|
||||||
|
double result = perlin_smooth_lerp(low, high, y_frac);
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
double perlin_fbm(int32_t seed, double x, double y, double freq, uint32_t octaves) {
|
||||||
|
double xa = x*freq;
|
||||||
|
double ya = y*freq;
|
||||||
|
double amp = 1.0;
|
||||||
|
double res = 0.0;
|
||||||
|
double div = 0.0;
|
||||||
|
|
||||||
|
for (uint32_t i=0; i<octaves; i++) {
|
||||||
|
div += 256.0 * amp;
|
||||||
|
res += perlin_noise2d(seed, xa, ya) * amp;
|
||||||
|
amp *= 0.5;
|
||||||
|
xa *= 2.0;
|
||||||
|
ya *= 2.0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return res/div;
|
||||||
|
}
|
|
@ -0,0 +1,6 @@
|
||||||
|
#pragma once
|
||||||
|
|
||||||
|
#include "system.h"
|
||||||
|
|
||||||
|
double perlin_noise2d(int32_t seed, double x, double y);
|
||||||
|
double perlin_fbm(int32_t seed, double x, double y, double freq, uint32_t octaves);
|
|
@ -0,0 +1,175 @@
|
||||||
|
#include "zpl.h"
|
||||||
|
#include "librg.h"
|
||||||
|
#include "modules/general.h"
|
||||||
|
#include "world/world.h"
|
||||||
|
|
||||||
|
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;
|
||||||
|
world_pkt_reader_proc *reader_proc;
|
||||||
|
world_pkt_writer_proc *writer_proc;
|
||||||
|
} world_data;
|
||||||
|
|
||||||
|
|
||||||
|
static world_data world = {0};
|
||||||
|
|
||||||
|
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_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.size = world.width * world.height;
|
||||||
|
world.block_size = block_size;
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
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;
|
||||||
|
world_init_minimal(chunk_size, block_size, world_size, reader_proc, writer_proc);
|
||||||
|
world.data = zpl_malloc(sizeof(uint8_t)*world.size);
|
||||||
|
|
||||||
|
if (!world.data) {
|
||||||
|
return WORLD_ERROR_OUTOFMEM;
|
||||||
|
}
|
||||||
|
|
||||||
|
world.ecs = ecs_init();
|
||||||
|
ecs_set_entity_range(world.ecs, 0, UINT32_MAX);
|
||||||
|
//ecs_set_threads(world.ecs, 4);
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
|
||||||
|
// librg_event_set(world.tracker, LIBRG_WRITE_UPDATE, world_write_update);
|
||||||
|
// librg_event_set(world.tracker, LIBRG_READ_UPDATE, server_read_update);
|
||||||
|
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t world_update() {
|
||||||
|
ecs_progress(world.ecs, 0);
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t world_read(void* data, uint32_t datalen) {
|
||||||
|
if (world.reader_proc) {
|
||||||
|
return world.reader_proc(data, datalen);
|
||||||
|
}
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
int32_t world_write(pkt_header *pkt) {
|
||||||
|
if (world.writer_proc) {
|
||||||
|
return world.writer_proc(pkt);
|
||||||
|
}
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t world_chunk_size(void) {
|
||||||
|
return world.chunk_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint16_t world_chunk_amount(void) {
|
||||||
|
return world.world_size;
|
||||||
|
}
|
||||||
|
|
||||||
|
#include "world_gen.c"
|
|
@ -0,0 +1,32 @@
|
||||||
|
#pragma once
|
||||||
|
#include "system.h"
|
||||||
|
#include "librg.h"
|
||||||
|
#include "packets/packet.h"
|
||||||
|
#include "flecs/flecs.h"
|
||||||
|
|
||||||
|
#define WORLD_ERROR_NONE +0x0000
|
||||||
|
#define WORLD_ERROR_OUTOFMEM -0x0001
|
||||||
|
#define WORLD_ERROR_INVALID_BLOCKS -0x0002
|
||||||
|
#define WORLD_ERROR_INVALID_DIMENSIONS -0x0003
|
||||||
|
#define WORLD_ERROR_INVALID_BUFFER -0x0004
|
||||||
|
#define WORLD_ERROR_TRACKER_FAILED -0x0005
|
||||||
|
|
||||||
|
#define WORLD_PKT_READER(name) int32_t name(void* data, uint32_t datalen)
|
||||||
|
typedef WORLD_PKT_READER(world_pkt_reader_proc);
|
||||||
|
|
||||||
|
#define WORLD_PKT_WRITER(name) int32_t name(pkt_header *pkt)
|
||||||
|
typedef WORLD_PKT_WRITER(world_pkt_writer_proc);
|
||||||
|
|
||||||
|
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);
|
||||||
|
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);
|
||||||
|
int32_t world_destroy(void);
|
||||||
|
int32_t world_update(void);
|
||||||
|
int32_t world_read(void* data, uint32_t datalen);
|
||||||
|
int32_t world_write(pkt_header *pkt);
|
||||||
|
|
||||||
|
uint32_t world_buf(uint8_t const **ptr, uint32_t *width);
|
||||||
|
ecs_world_t * world_ecs(void);
|
||||||
|
librg_world * world_tracker(void);
|
||||||
|
|
||||||
|
uint16_t world_chunk_size(void);
|
||||||
|
uint16_t world_chunk_amount(void);
|
|
@ -0,0 +1,133 @@
|
||||||
|
#include "zpl.h"
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#include "world/world.h"
|
||||||
|
#include "world/blocks.h"
|
||||||
|
#include "world/perlin.h"
|
||||||
|
|
||||||
|
#define WORLD_BLOCK_OBSERVER(name) uint32_t name(uint32_t id, uint32_t block_idx)
|
||||||
|
typedef WORLD_BLOCK_OBSERVER(world_block_observer_proc);
|
||||||
|
|
||||||
|
#define WORLD_PERLIN_FREQ 1.0
|
||||||
|
#define WORLD_PERLIN_OCTAVES 1
|
||||||
|
|
||||||
|
static void world_fill_rect(uint32_t id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, world_block_observer_proc *proc) {
|
||||||
|
for (uint32_t cy=y; cy<y+h; cy++) {
|
||||||
|
for (uint32_t cx=x; cx<x+w; cx++) {
|
||||||
|
if (cx < 0 || cx >= world.width) continue;
|
||||||
|
if (cy < 0 || cy >= world.height) continue;
|
||||||
|
uint32_t i = (cy*world.width) + cx;
|
||||||
|
|
||||||
|
if (proc) {
|
||||||
|
uint32_t new_id = (*proc)(id, i);
|
||||||
|
if (new_id != BLOCK_INVALID) {
|
||||||
|
id = new_id;
|
||||||
|
}
|
||||||
|
else continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
world.data[i] = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void world_fill_circle(uint32_t id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, world_block_observer_proc *proc) {
|
||||||
|
for (uint32_t cy=y; cy<y+h; cy++) {
|
||||||
|
for (uint32_t cx=x; cx<x+w; cx++) {
|
||||||
|
if (cx < 0 || cx >= world.width) continue;
|
||||||
|
if (cy < 0 || cy >= world.height) continue;
|
||||||
|
uint32_t i = (cy*world.width) + cx;
|
||||||
|
|
||||||
|
if (proc) {
|
||||||
|
uint32_t new_id = (*proc)(id, i);
|
||||||
|
if (new_id != BLOCK_INVALID) {
|
||||||
|
id = new_id;
|
||||||
|
}
|
||||||
|
else continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
world.data[i] = id;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static void world_fill_rect_anchor(uint32_t id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, float ax, float ay, world_block_observer_proc *proc) {
|
||||||
|
uint32_t w2 = (uint32_t)floorf(w*ax);
|
||||||
|
uint32_t h2 = (uint32_t)floorf(h*ay);
|
||||||
|
world_fill_rect(id, x-w2, y-h2, w, h, proc);
|
||||||
|
}
|
||||||
|
|
||||||
|
static WORLD_BLOCK_OBSERVER(shaper) {
|
||||||
|
uint32_t biome = blocks_get_biome(id);
|
||||||
|
uint32_t kind = blocks_get_kind(id);
|
||||||
|
uint32_t old_biome = blocks_get_biome(world.data[block_idx]);
|
||||||
|
uint32_t old_kind = blocks_get_kind(world.data[block_idx]);
|
||||||
|
|
||||||
|
if (biome == old_biome) {
|
||||||
|
if (kind == BLOCK_KIND_WALL && kind == old_kind) {
|
||||||
|
return blocks_find(biome, BLOCK_KIND_HILL);
|
||||||
|
}
|
||||||
|
if (kind == BLOCK_KIND_HILL && kind == old_kind) {
|
||||||
|
return blocks_find(biome, BLOCK_KIND_HILL_SNOW);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
static uint8_t world_perlin_cond(uint32_t block_idx, double chance) {
|
||||||
|
uint32_t x = block_idx % world.width;
|
||||||
|
uint32_t y = block_idx / world.width;
|
||||||
|
|
||||||
|
return perlin_fbm(world.seed, x, y, WORLD_PERLIN_FREQ, WORLD_PERLIN_OCTAVES) < chance;
|
||||||
|
}
|
||||||
|
|
||||||
|
static WORLD_BLOCK_OBSERVER(shaper_noise80) {
|
||||||
|
return world_perlin_cond(block_idx, 0.80) ? shaper(id, block_idx) : BLOCK_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
static WORLD_BLOCK_OBSERVER(shaper_noise50) {
|
||||||
|
return world_perlin_cond(block_idx, 0.50) ? shaper(id, block_idx) : BLOCK_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
static WORLD_BLOCK_OBSERVER(shaper_noise33) {
|
||||||
|
return world_perlin_cond(block_idx, 0.33) ? shaper(id, block_idx) : BLOCK_INVALID;
|
||||||
|
}
|
||||||
|
|
||||||
|
static void world_fill_mountain(uint32_t x, uint32_t y) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#define RAND_RANGE(x,y) (x + (int)rand()%(y-(x)))
|
||||||
|
|
||||||
|
int32_t world_gen() {
|
||||||
|
// TODO: perform world gen
|
||||||
|
// atm, we will fill the world with ground and surround it by walls
|
||||||
|
uint32_t wall_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WALL);
|
||||||
|
uint32_t grnd_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_GROUND);
|
||||||
|
uint32_t watr_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER);
|
||||||
|
|
||||||
|
srand(world.seed);
|
||||||
|
|
||||||
|
// walls
|
||||||
|
world_fill_rect(wall_id, 0, 0, world.width, world.height, NULL);
|
||||||
|
|
||||||
|
// ground
|
||||||
|
world_fill_rect(grnd_id, 1, 1, world.width-2, world.height-2, NULL);
|
||||||
|
|
||||||
|
// water
|
||||||
|
for (int i=0; i<RAND_RANGE(0, 12); i++) {
|
||||||
|
world_fill_rect_anchor(watr_id, RAND_RANGE(0, world.width), RAND_RANGE(0, world.height), 4+RAND_RANGE(0,3), 4+RAND_RANGE(0,3), 0.5f, 0.5f, shaper_noise33);
|
||||||
|
}
|
||||||
|
|
||||||
|
const uint32_t HILLS_SIZE = 21;
|
||||||
|
|
||||||
|
// hills
|
||||||
|
for (int i=0; i<RAND_RANGE(8, 224); i++) {
|
||||||
|
world_fill_rect_anchor(wall_id, RAND_RANGE(0, world.width), RAND_RANGE(0, world.height), RAND_RANGE(0,HILLS_SIZE), RAND_RANGE(0,HILLS_SIZE), 0.5f, 0.5f, shaper_noise33);
|
||||||
|
}
|
||||||
|
|
||||||
|
return WORLD_ERROR_NONE;
|
||||||
|
}
|
|
@ -0,0 +1,3 @@
|
||||||
|
file(GLOB MODULES modules/*.h source/*.c)
|
||||||
|
add_library(eco2d-modules STATIC ${MODULES})
|
||||||
|
include_directories(.)
|
|
@ -0,0 +1,28 @@
|
||||||
|
#pragma once
|
||||||
|
#include "flecs/flecs.h"
|
||||||
|
#include "flecs/flecs_meta.h"
|
||||||
|
|
||||||
|
ECS_STRUCT(Input, {
|
||||||
|
double x;
|
||||||
|
double y;
|
||||||
|
uint8_t use;
|
||||||
|
});
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ECS_DECLARE_COMPONENT(Input);
|
||||||
|
ECS_DECLARE_ENTITY(EcsActor);
|
||||||
|
ECS_DECLARE_ENTITY(EcsPlayer);
|
||||||
|
ECS_DECLARE_ENTITY(EcsBuilder);
|
||||||
|
ECS_DECLARE_TYPE(Player);
|
||||||
|
ECS_DECLARE_TYPE(Builder);
|
||||||
|
} Controllers;
|
||||||
|
|
||||||
|
#define ControllersImportHandles(handles)\
|
||||||
|
ECS_IMPORT_COMPONENT(handles, Input);\
|
||||||
|
ECS_IMPORT_TYPE(handles, Player);\
|
||||||
|
ECS_IMPORT_TYPE(handles, Builder);\
|
||||||
|
ECS_IMPORT_ENTITY(handles, EcsActor);\
|
||||||
|
ECS_IMPORT_ENTITY(handles, EcsPlayer);\
|
||||||
|
ECS_IMPORT_ENTITY(handles, EcsBuilder);\
|
||||||
|
|
||||||
|
void ControllersImport(ecs_world_t *ecs);
|
|
@ -0,0 +1,30 @@
|
||||||
|
#pragma once
|
||||||
|
#include "flecs/flecs.h"
|
||||||
|
#include "flecs/flecs_meta.h"
|
||||||
|
|
||||||
|
ECS_STRUCT(Vector2D, {
|
||||||
|
int16_t x;
|
||||||
|
int16_t y;
|
||||||
|
});
|
||||||
|
|
||||||
|
ECS_STRUCT(Drawable, {
|
||||||
|
uint16_t id;
|
||||||
|
});
|
||||||
|
|
||||||
|
ECS_ALIAS(Vector2D, Chunk);
|
||||||
|
ECS_ALIAS(Vector2D, Position);
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ECS_DECLARE_COMPONENT(Chunk);
|
||||||
|
ECS_DECLARE_COMPONENT(Position);
|
||||||
|
ECS_DECLARE_COMPONENT(Vector2D);
|
||||||
|
ECS_DECLARE_COMPONENT(Drawable);
|
||||||
|
} General;
|
||||||
|
|
||||||
|
#define GeneralImportHandles(handles)\
|
||||||
|
ECS_IMPORT_COMPONENT(handles, Chunk);\
|
||||||
|
ECS_IMPORT_COMPONENT(handles, Vector2D);\
|
||||||
|
ECS_IMPORT_COMPONENT(handles, Position);\
|
||||||
|
ECS_IMPORT_COMPONENT(handles, Drawable);\
|
||||||
|
|
||||||
|
void GeneralImport(ecs_world_t *ecs);
|
|
@ -0,0 +1,18 @@
|
||||||
|
#pragma once
|
||||||
|
#include "flecs/flecs.h"
|
||||||
|
#include "flecs/flecs_meta.h"
|
||||||
|
|
||||||
|
ECS_STRUCT(ClientInfo, {
|
||||||
|
uint16_t peer_id;
|
||||||
|
});
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ECS_DECLARE_ENTITY(EcsClient);
|
||||||
|
ECS_DECLARE_COMPONENT(ClientInfo);
|
||||||
|
} Net;
|
||||||
|
|
||||||
|
#define NetImportHandles(handles)\
|
||||||
|
ECS_IMPORT_ENTITY(handles, EcsClient);\
|
||||||
|
ECS_IMPORT_COMPONENT(handles, ClientInfo);\
|
||||||
|
|
||||||
|
void NetImport(ecs_world_t *ecs);
|
|
@ -0,0 +1,23 @@
|
||||||
|
#pragma once
|
||||||
|
#include "flecs/flecs.h"
|
||||||
|
|
||||||
|
#include "modules/general.h"
|
||||||
|
|
||||||
|
typedef Vector2D Velocity;
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
ECS_DECLARE_TYPE(Movement);
|
||||||
|
ECS_DECLARE_ENTITY(Walking);
|
||||||
|
ECS_DECLARE_ENTITY(Flying);
|
||||||
|
ECS_DECLARE_COMPONENT(Velocity);
|
||||||
|
ECS_DECLARE_ENTITY(MoveWalk);
|
||||||
|
} Physics;
|
||||||
|
|
||||||
|
#define PhysicsImportHandles(handles)\
|
||||||
|
ECS_IMPORT_TYPE(handles, Movement);\
|
||||||
|
ECS_IMPORT_ENTITY(handles, Walking);\
|
||||||
|
ECS_IMPORT_ENTITY(handles, Flying);\
|
||||||
|
ECS_IMPORT_COMPONENT(handles, Velocity);\
|
||||||
|
ECS_IMPORT_ENTITY(handles, MoveWalk);\
|
||||||
|
|
||||||
|
void PhysicsImport(ecs_world_t *ecs);
|
|
@ -0,0 +1,30 @@
|
||||||
|
#include "modules/controllers.h"
|
||||||
|
|
||||||
|
#include "modules/general.h"
|
||||||
|
#include "modules/physics.h"
|
||||||
|
|
||||||
|
void ControllersImport(ecs_world_t *ecs) {
|
||||||
|
ECS_MODULE(ecs, Controllers);
|
||||||
|
ecs_set_name_prefix(ecs, "Controllers");
|
||||||
|
|
||||||
|
ECS_IMPORT(ecs, General);
|
||||||
|
ECS_IMPORT(ecs, Physics);
|
||||||
|
|
||||||
|
ECS_IMPORT(ecs, FlecsMeta);
|
||||||
|
ECS_META(ecs, Input);
|
||||||
|
|
||||||
|
ECS_TAG(ecs, EcsActor);
|
||||||
|
ECS_TAG(ecs, EcsPlayer);
|
||||||
|
ECS_TAG(ecs, EcsBuilder);
|
||||||
|
|
||||||
|
ECS_PREFAB(ecs, Base, general.Position, physics.Velocity, Input, EcsActor);
|
||||||
|
ECS_TYPE(ecs, Player, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Walking, EcsActor, EcsPlayer);
|
||||||
|
ECS_TYPE(ecs, Builder, INSTANCEOF | Base, SWITCH | physics.Movement, CASE | physics.Flying, EcsActor, EcsBuilder);
|
||||||
|
|
||||||
|
ECS_SET_COMPONENT(Input);
|
||||||
|
ECS_SET_ENTITY(EcsActor);
|
||||||
|
ECS_SET_ENTITY(EcsPlayer);
|
||||||
|
ECS_SET_ENTITY(EcsBuilder);
|
||||||
|
ECS_SET_TYPE(Builder);
|
||||||
|
ECS_SET_TYPE(Player);
|
||||||
|
}
|
|
@ -0,0 +1,19 @@
|
||||||
|
#include "modules/general.h"
|
||||||
|
|
||||||
|
void GeneralImport(ecs_world_t *ecs) {
|
||||||
|
ECS_MODULE(ecs, General);
|
||||||
|
ecs_set_name_prefix(ecs, "General");
|
||||||
|
|
||||||
|
ECS_COMPONENT(ecs, Chunk);
|
||||||
|
ECS_COMPONENT(ecs, Position);
|
||||||
|
|
||||||
|
ECS_IMPORT(ecs, FlecsMeta);
|
||||||
|
|
||||||
|
ECS_META(ecs, Vector2D);
|
||||||
|
ECS_META(ecs, Drawable);
|
||||||
|
|
||||||
|
ECS_SET_COMPONENT(Chunk);
|
||||||
|
ECS_SET_COMPONENT(Vector2D);
|
||||||
|
ECS_SET_COMPONENT(Position);
|
||||||
|
ECS_SET_COMPONENT(Drawable);
|
||||||
|
}
|
|
@ -0,0 +1,16 @@
|
||||||
|
#include "modules/net.h"
|
||||||
|
|
||||||
|
void NetImport(ecs_world_t *ecs) {
|
||||||
|
ECS_MODULE(ecs, Net);
|
||||||
|
|
||||||
|
ecs_set_name_prefix(ecs, "Net");
|
||||||
|
|
||||||
|
ECS_TAG(ecs, EcsClient);
|
||||||
|
|
||||||
|
ECS_IMPORT(ecs, FlecsMeta);
|
||||||
|
|
||||||
|
ECS_META(ecs, ClientInfo);
|
||||||
|
|
||||||
|
ECS_EXPORT_ENTITY(EcsClient);
|
||||||
|
ECS_EXPORT_COMPONENT(ClientInfo);
|
||||||
|
}
|
|
@ -0,0 +1,31 @@
|
||||||
|
#include "modules/physics.h"
|
||||||
|
|
||||||
|
void MoveWalk(ecs_iter_t *it) {
|
||||||
|
Position *p = ecs_column(it, Position, 1);
|
||||||
|
Velocity *v = ecs_column(it, Velocity, 2);
|
||||||
|
|
||||||
|
for (int i = 0; i < it->count; i++) {
|
||||||
|
// TODO: handle collisions
|
||||||
|
p[i].x += v[i].x * it->delta_time;
|
||||||
|
p[i].y += v[i].y * it->delta_time;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void PhysicsImport(ecs_world_t *ecs) {
|
||||||
|
ECS_MODULE(ecs, Physics);
|
||||||
|
ecs_set_name_prefix(ecs, "Physics");
|
||||||
|
|
||||||
|
ECS_TAG(ecs, Walking);
|
||||||
|
ECS_TAG(ecs, Flying);
|
||||||
|
ECS_TYPE(ecs, Movement, Walking, Flying);
|
||||||
|
|
||||||
|
ECS_COMPONENT(ecs, Velocity);
|
||||||
|
|
||||||
|
ECS_SYSTEM(ecs, MoveWalk, EcsOnUpdate, general.Position, Velocity, SWITCH | Movement, CASE | Walking);
|
||||||
|
|
||||||
|
ECS_SET_TYPE(Movement);
|
||||||
|
ECS_SET_ENTITY(Walking);
|
||||||
|
ECS_SET_ENTITY(Flying);
|
||||||
|
ECS_SET_COMPONENT(Velocity);
|
||||||
|
ECS_SET_ENTITY(MoveWalk);
|
||||||
|
}
|
Loading…
Reference in New Issue