eco2d/code/games/minimal/src/worldgen.c

277 lines
9.2 KiB
C
Raw Normal View History

2021-05-04 17:41:30 +00:00
#include "zpl.h"
#include <math.h>
#include <stdlib.h>
#include "world/world.h"
#include "world/blocks.h"
#include "world/perlin.h"
#include "ecs/components.h"
#include "ents/entity.h"
#include "ents/vehicle.h"
#include "ents/items.h"
2021-11-02 11:49:03 +00:00
#include "world/blocks_info.h"
2021-09-08 14:12:38 +00:00
2021-11-03 18:04:34 +00:00
#define WORLD_BLOCK_OBSERVER(name) block_id name(block_id *data, block_id id, uint32_t block_idx)
2021-05-04 17:41:30 +00:00
typedef WORLD_BLOCK_OBSERVER(world_block_observer_proc);
#define WORLD_PERLIN_FREQ 100
#define WORLD_PERLIN_OCTAVES 1
2021-05-04 17:41:30 +00:00
2021-11-02 11:49:03 +00:00
#define BLOCK_INVALID 0xF
2021-11-03 18:04:34 +00:00
block_id worldgen_biome_find(uint32_t biome, uint32_t kind) {
2021-11-02 11:49:03 +00:00
asset_id asset = ASSET_INVALID;
switch (biome) {
case BLOCK_BIOME_DEV: {
switch (kind) {
case BLOCK_KIND_GROUND: asset = ASSET_GROUND; break;
case BLOCK_KIND_DIRT: asset = ASSET_DIRT; break;
case BLOCK_KIND_WALL: asset = ASSET_WALL; break;
case BLOCK_KIND_HILL_SNOW:
case BLOCK_KIND_HILL: asset = ASSET_HILL; break;
case BLOCK_KIND_WATER: asset = ASSET_WATER; break;
case BLOCK_KIND_LAVA: asset = ASSET_LAVA; break;
}
}
}
return blocks_find(asset);
}
2021-05-12 14:02:12 +00:00
static world_data *world;
2021-11-03 18:04:34 +00:00
static void world_fill_rect(block_id *data, block_id id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, world_block_observer_proc *proc) {
2021-05-04 17:41:30 +00:00
for (uint32_t cy=y; cy<y+h; cy++) {
for (uint32_t cx=x; cx<x+w; cx++) {
2021-05-12 14:02:12 +00:00
if (cx < 0 || cx >= world->dim) continue;
if (cy < 0 || cy >= world->dim) continue;
uint32_t i = (cy*world->dim) + cx;
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
if (proc) {
2021-11-03 18:04:34 +00:00
block_id new_id = (*proc)(data, id, i);
2021-05-04 17:41:30 +00:00
if (new_id != BLOCK_INVALID) {
id = new_id;
}
else continue;
}
2021-07-18 18:30:27 +00:00
2021-11-02 17:09:54 +00:00
data[i] = id;
2021-05-04 17:41:30 +00:00
}
}
}
2021-11-03 18:04:34 +00:00
static void world_fill_circle(block_id *data, block_id id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, world_block_observer_proc *proc) {
2021-05-04 17:41:30 +00:00
for (uint32_t cy=y; cy<y+h; cy++) {
for (uint32_t cx=x; cx<x+w; cx++) {
2021-05-12 14:02:12 +00:00
if (cx < 0 || cx >= world->dim) continue;
if (cy < 0 || cy >= world->dim) continue;
uint32_t i = (cy*world->dim) + cx;
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
if (proc) {
2021-11-03 18:04:34 +00:00
block_id new_id = (*proc)(data, id, i);
2021-05-04 17:41:30 +00:00
if (new_id != BLOCK_INVALID) {
id = new_id;
}
else continue;
}
2021-07-18 18:30:27 +00:00
2021-11-02 17:09:54 +00:00
data[i] = id;
2021-05-04 17:41:30 +00:00
}
}
}
2021-11-03 18:04:34 +00:00
static void world_fill_rect_anchor(block_id *data, block_id id, uint32_t x, uint32_t y, uint32_t w, uint32_t h, float ax, float ay, world_block_observer_proc *proc) {
2021-05-04 17:41:30 +00:00
uint32_t w2 = (uint32_t)floorf(w*ax);
uint32_t h2 = (uint32_t)floorf(h*ay);
2021-11-02 17:09:54 +00:00
world_fill_rect(data, id, x-w2, y-h2, w, h, proc);
2021-05-04 17:41:30 +00:00
}
static WORLD_BLOCK_OBSERVER(shaper) {
2021-11-02 11:49:03 +00:00
uint32_t kind = id;
2021-11-02 17:09:54 +00:00
uint32_t old_kind = data[block_idx];
2021-11-02 11:49:03 +00:00
if (kind == BLOCK_KIND_WALL && kind == old_kind) {
return worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_HILL);
}
if (kind == BLOCK_KIND_HILL && kind == old_kind) {
return worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_HILL_SNOW);
2021-05-04 17:41:30 +00:00
}
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
return id;
}
2021-11-03 18:04:34 +00:00
static block_id world_perlin_cond_offset(uint32_t block_idx, double chance, uint32_t ofx, uint32_t ofy) {
2021-11-02 17:09:54 +00:00
uint32_t x = block_idx % world->dim + ofx;
uint32_t y = block_idx / world->dim + ofy;
2021-07-18 18:30:27 +00:00
return perlin_fbm(world->seed, x, y, WORLD_PERLIN_FREQ, WORLD_PERLIN_OCTAVES) < chance;
2021-05-04 17:41:30 +00:00
}
2021-11-03 18:04:34 +00:00
static block_id world_perlin_cond(uint32_t block_idx, double chance) {
2021-11-02 17:09:54 +00:00
return world_perlin_cond_offset(block_idx, chance, 0, 0);
}
2021-07-19 07:56:15 +00:00
#if 1
2021-05-04 17:41:30 +00:00
static WORLD_BLOCK_OBSERVER(shaper_noise80) {
2021-11-02 17:09:54 +00:00
return world_perlin_cond(block_idx, 0.80) ? shaper(data, id, block_idx) : BLOCK_INVALID;
2021-05-04 17:41:30 +00:00
}
static WORLD_BLOCK_OBSERVER(shaper_noise50) {
2021-11-02 17:09:54 +00:00
return world_perlin_cond(block_idx, 0.50) ? shaper(data, id, block_idx) : BLOCK_INVALID;
2021-05-04 17:41:30 +00:00
}
static WORLD_BLOCK_OBSERVER(shaper_noise33) {
2021-11-02 17:09:54 +00:00
return world_perlin_cond(block_idx, 0.33) ? shaper(data, id, block_idx) : BLOCK_INVALID;
2021-05-04 17:41:30 +00:00
}
2021-08-10 11:19:45 +00:00
static WORLD_BLOCK_OBSERVER(shaper_noise05) {
2021-11-02 17:09:54 +00:00
return world_perlin_cond(block_idx, 0.05) ? shaper(data, id, block_idx) : BLOCK_INVALID;
2021-08-10 11:19:45 +00:00
}
2021-11-02 17:09:54 +00:00
static WORLD_BLOCK_OBSERVER(shaper_noise05b) {
return world_perlin_cond_offset(block_idx, 0.05, 32, 0) ? shaper(data, id, block_idx) : BLOCK_INVALID;
}
static WORLD_BLOCK_OBSERVER(shaper_noise01b) {
return world_perlin_cond_offset(block_idx, 0.01, 32, 0) ? shaper(data, id, block_idx) : BLOCK_INVALID;
}
2021-07-19 07:56:15 +00:00
#else
static WORLD_BLOCK_OBSERVER(shaper_noise80) {
return rand()%10 < 8 ? shaper(id, block_idx) : BLOCK_INVALID;
}
static WORLD_BLOCK_OBSERVER(shaper_noise50) {
return rand()%10 < 5 ? shaper(id, block_idx) : BLOCK_INVALID;
}
static WORLD_BLOCK_OBSERVER(shaper_noise33) {
return rand()%10 < 3 ? shaper(id, block_idx) : BLOCK_INVALID;
}
#endif
2021-05-04 17:41:30 +00:00
2021-05-10 09:35:04 +00:00
#if 0
2021-05-04 17:41:30 +00:00
static void world_fill_mountain(uint32_t x, uint32_t y) {
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
}
2021-05-10 09:35:04 +00:00
#endif
2021-05-04 17:41:30 +00:00
#define RAND_RANGE(x,y) (x + (int)rand()%(y-(x)))
2021-11-02 17:09:54 +00:00
#define RAND_RANGEF(x,y) ((float)RAND_RANGE(x,y))
2021-05-04 17:41:30 +00:00
2022-09-28 05:29:32 +00:00
int32_t worldgen_build(world_data *wld) {
2021-05-12 14:02:12 +00:00
// TODO(zaklaus): pass world as an arg instead
world = wld;
2021-05-04 17:41:30 +00:00
// TODO: perform world gen
// atm, we will fill the world with ground and surround it by walls
2021-11-03 18:04:34 +00:00
block_id wall_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_WALL);
block_id grnd_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_GROUND);
block_id dirt_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_DIRT);
block_id watr_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_WATER);
block_id lava_id = worldgen_biome_find(BLOCK_BIOME_DEV, BLOCK_KIND_LAVA);
block_id tree_id = blocks_find(ASSET_TREE);
2021-07-18 18:30:27 +00:00
2021-05-12 14:02:12 +00:00
srand(world->seed);
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
// walls
2021-11-02 17:09:54 +00:00
world_fill_rect(world->data, wall_id, 0, 0, world->dim, world->dim, NULL);
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
// ground
2021-11-02 17:09:54 +00:00
world_fill_rect(world->data, grnd_id, 1, 1, world->dim-2, world->dim-2, NULL);
world_fill_rect(world->data, dirt_id, 1, 1, world->dim-2, world->dim-2, shaper_noise05);
world_fill_rect(world->outer_data, tree_id, 1, 1, world->dim-2, world->dim-2, shaper_noise01b);
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
// water
2021-07-19 08:28:23 +00:00
#if 1
for (int i=0; i<RAND_RANGE(58, 92); i++) {
2021-11-02 17:09:54 +00:00
world_fill_rect_anchor(world->data, watr_id, RAND_RANGE(0, world->dim), RAND_RANGE(0, world->dim), 4+RAND_RANGE(0,3), 4+RAND_RANGE(0,3), 0.5f, 0.5f, shaper_noise80);
2021-05-04 17:41:30 +00:00
}
2021-07-18 19:06:21 +00:00
#endif
2021-07-18 18:30:27 +00:00
2021-08-11 10:22:46 +00:00
// ice rink
#if 0
2021-11-02 17:09:54 +00:00
world_fill_rect_anchor(world->data, watr_id, 450, 125, 10, 10, 0.0f, 0.0f, NULL);
2021-08-11 10:22:46 +00:00
#endif
2021-07-27 16:34:31 +00:00
// lava
#if 1
for (int i=0; i<RAND_RANGE(48, 62); i++) {
2021-11-02 17:09:54 +00:00
world_fill_rect_anchor(world->data, lava_id, RAND_RANGE(0, world->dim), RAND_RANGE(0, world->dim), 4+RAND_RANGE(0,3), 4+RAND_RANGE(0,3), 0.5f, 0.5f, shaper_noise80);
2021-07-27 16:34:31 +00:00
}
#endif
2021-07-18 18:30:27 +00:00
2021-05-04 17:41:30 +00:00
// hills
2021-07-19 08:28:23 +00:00
#if 1
const uint32_t HILLS_SIZE = 21;
for (int i=0; i<RAND_RANGE(8, 124); i++) {
2021-11-02 17:09:54 +00:00
world_fill_rect_anchor(world->data, wall_id, RAND_RANGE(0, world->dim), RAND_RANGE(0, world->dim), RAND_RANGE(0,HILLS_SIZE), RAND_RANGE(0,HILLS_SIZE), 0.5f, 0.5f, shaper_noise50);
2021-05-04 17:41:30 +00:00
}
2021-07-18 19:06:21 +00:00
#endif
2021-07-18 18:30:27 +00:00
2021-09-08 14:12:38 +00:00
// vehicles
#if 1
for (int i=0; i<RAND_RANGE(258, 1124); i++) {
uint64_t e = vehicle_spawn();
2022-07-31 14:34:47 +00:00
Position *dest = ecs_get_mut(world_ecs(), e, Position);
2021-11-02 17:09:54 +00:00
dest->x = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
entity_set_position(e, dest->x, dest->y);
2021-09-08 14:12:38 +00:00
}
#endif
// items
#if 1
for (int i=0; i<RAND_RANGE(328, 164); i++) {
2021-11-02 10:48:32 +00:00
uint64_t e = item_spawn(ASSET_DEMO_ICEMAKER, 32);
2021-09-08 14:12:38 +00:00
2022-07-31 14:34:47 +00:00
Position *dest = ecs_get_mut(world_ecs(), e, Position);
2021-11-02 17:09:54 +00:00
dest->x = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
entity_set_position(e, dest->x, dest->y);
2021-09-08 14:12:38 +00:00
}
2021-11-02 11:49:03 +00:00
for (int i=0; i<RAND_RANGE(328, 164); i++) {
uint64_t e = item_spawn(ASSET_FENCE, 64);
2022-07-31 14:34:47 +00:00
Position *dest = ecs_get_mut(world_ecs(), e, Position);
2021-11-02 17:09:54 +00:00
dest->x = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
entity_set_position(e, dest->x, dest->y);
2021-11-02 17:09:54 +00:00
}
for (int i=0; i<RAND_RANGE(328, 164); i++) {
uint64_t e = item_spawn(ASSET_WOOD, 64);
2022-07-31 14:34:47 +00:00
Position *dest = ecs_get_mut(world_ecs(), e, Position);
2021-11-02 17:09:54 +00:00
dest->x = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
entity_set_position(e, dest->x, dest->y);
2021-11-02 11:49:03 +00:00
}
2021-11-02 17:09:54 +00:00
2021-11-02 17:58:55 +00:00
for (int i=0; i<RAND_RANGE(128, 564); i++) {
uint64_t e = item_spawn(ASSET_BELT, 999);
2021-11-02 17:09:54 +00:00
2022-07-31 14:34:47 +00:00
Position *dest = ecs_get_mut(world_ecs(), e, Position);
2021-11-02 17:09:54 +00:00
dest->x = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
entity_set_position(e, dest->x, dest->y);
2021-11-02 17:09:54 +00:00
}
2022-08-09 14:46:23 +00:00
for (int i=0; i<RAND_RANGE(128, 964); i++) {
uint64_t e = item_spawn(ASSET_CHEST, 4);
Position *dest = ecs_get_mut(world_ecs(), e, Position);
dest->x = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
dest->y = RAND_RANGEF(0, world->dim*WORLD_BLOCK_SIZE);
entity_set_position(e, dest->x, dest->y);
2022-08-09 14:46:23 +00:00
}
2021-09-08 14:12:38 +00:00
#endif
2021-05-04 17:41:30 +00:00
return WORLD_ERROR_NONE;
}