add more observers

isolation_bkp/dynres
Dominik Madarász 2021-01-11 18:30:47 +01:00
parent 181168e9fd
commit cce7262549
2 changed files with 27 additions and 6 deletions

View File

@ -2,23 +2,27 @@
#include "zpl.h" #include "zpl.h"
static uint8_t *world = NULL; static uint8_t *world = NULL;
static uint32_t world_seed = 0;
static uint32_t world_size = 0; static uint32_t world_size = 0;
static uint32_t world_width = 0; static uint32_t world_width = 0;
static uint32_t world_height = 0; static uint32_t world_height = 0;
int32_t world_gen();
int32_t world_init(int32_t seed, uint8_t width, uint8_t height) { int32_t world_init(int32_t seed, uint8_t width, uint8_t height) {
if (world) { if (world) {
world_destroy(); world_destroy();
} }
world_size = width*height; world_seed = seed;
world_width = width; world_width = width;
world_height = height; world_height = height;
world_size = width*height;
world = zpl_malloc(sizeof(uint8_t)*world_size); world = zpl_malloc(sizeof(uint8_t)*world_size);
if (!world) { if (!world) {
return WORLD_ERROR_OUTOFMEM; return WORLD_ERROR_OUTOFMEM;
} }
return world_gen(world, world_size, world_width, world_height, seed); return world_gen();
} }
int32_t world_destroy(void) { int32_t world_destroy(void) {

View File

@ -1,5 +1,6 @@
#include "world.h" #include "world.h"
#include "blocks.h" #include "blocks.h"
#include "perlin.h"
#include "zpl.h" #include "zpl.h"
#include <math.h> #include <math.h>
@ -7,6 +8,9 @@
#define WORLD_BLOCK_OBSERVER(name) uint32_t name(uint32_t id, uint32_t block_idx) #define WORLD_BLOCK_OBSERVER(name) uint32_t name(uint32_t id, uint32_t block_idx)
typedef WORLD_BLOCK_OBSERVER(world_block_observer_proc); 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) { 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 cy=y; cy<y+h; cy++) {
for (uint32_t cx=x; cx<x+w; cx++) { for (uint32_t cx=x; cx<x+w; cx++) {
@ -16,7 +20,10 @@ static void world_fill_rect(uint32_t id, uint32_t x, uint32_t y, uint32_t w, uin
if (proc) { if (proc) {
uint32_t new_id = (*proc)(id, i); uint32_t new_id = (*proc)(id, i);
id = (new_id != BLOCK_INVALID) ? new_id : id; if (new_id != BLOCK_INVALID) {
id = new_id;
}
else continue;
} }
world[i] = id; world[i] = id;
@ -45,7 +52,17 @@ static WORLD_BLOCK_OBSERVER(shaper) {
return id; return id;
} }
int32_t world_gen(int32_t seed) { static WORLD_BLOCK_OBSERVER(shaper_noise80) {
uint32_t x = block_idx % world_width;
uint32_t y = block_idx / world_width;
if (perlin_fbm(world_seed, x, y, WORLD_PERLIN_FREQ, WORLD_PERLIN_OCTAVES) < 0.8)
return shaper(id, block_idx);
else
return BLOCK_INVALID;
}
int32_t world_gen() {
// TODO: perform world gen // TODO: perform world gen
// atm, we will fill the world with ground and surround it by walls // 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 wall_id = blocks_find(BLOCK_BIOME_DEV, BLOCK_KIND_WALL);
@ -62,8 +79,8 @@ int32_t world_gen(int32_t seed) {
world_fill_rect_anchor(watr_id, 8, 8, 4, 4, 0.5f, 0.5f, NULL); world_fill_rect_anchor(watr_id, 8, 8, 4, 4, 0.5f, 0.5f, NULL);
// hills // hills
world_fill_rect_anchor(wall_id, 14, 21, 8, 8, 0.5f, 0.5f, shaper); world_fill_rect_anchor(wall_id, 14, 21, 8, 8, 0.5f, 0.5f, shaper_noise80);
world_fill_rect_anchor(wall_id, 14, 21, 4, 4, 0.5f, 0.5f, shaper); world_fill_rect_anchor(wall_id, 14, 21, 4, 4, 0.5f, 0.5f, shaper_noise80);
return WORLD_ERROR_NONE; return WORLD_ERROR_NONE;