Added basic perlin noise gen

isolation_bkp/dynres
Dominik Madarász 2021-01-11 14:47:14 +01:00
parent 10ead34ff6
commit f0713d7fc1
21 changed files with 181 additions and 18 deletions

View File

@ -2,10 +2,10 @@ cmake_minimum_required(VERSION 3.15)
project(eco2d)
set(CMAKE_C_STANDARD 11)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/)
include_directories(eco2d-client code/vendors code/common)
include_directories(eco2d-server code/vendors code/common)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/)
add_subdirectory(code/apps/client)
add_subdirectory(code/apps/server)

View File

@ -1,4 +1,4 @@
#pragma once
void game_init();
void game_shutdown();

View File

@ -1,2 +1 @@
#include "librg.h"
#pragma once

View File

@ -1,3 +1,4 @@
#pragma once
void gfx_init();
void gfx_shutdown();

View File

@ -4,7 +4,7 @@ int main(void)
{
game_init();
while (!WindowShouldClose()) // Detect window close button or ESC key
while (!WindowShouldClose())
{
game_input();
game_update();

View File

@ -4,3 +4,5 @@
#define LIBRG_IMPL
#define LIBRG_CUSTOM_ZPL
#include "librg.h"
#include "network.h"

View File

@ -6,7 +6,7 @@ const uint16_t screenWidth = 800;
const uint16_t screenHeight = 450;
void gfx_init() {
InitWindow(screenWidth, screenHeight, "raylib [core] example - basic window");
InitWindow(screenWidth, screenHeight, "eco2d - client");
SetTargetFPS(60);
}

View File

@ -1,9 +1,17 @@
add_executable(eco2d-server
source/main.c
source/network.c
source/perlin.c
source/options.c
source/world.c
source/blocks.c
header/network.h
header/perlin.h
header/options.h
header/world.h
header/blocks.h
)
include_directories(header/)
include_directories(eco2d-server header)
target_link_libraries(eco2d-server raylib)

View File

@ -0,0 +1,3 @@
#pragma once
#include "system.h"

View File

@ -0,0 +1 @@
#pragma once

View File

@ -0,0 +1,4 @@
#pragma once
#include "system.h"
void generate_minimap(int32_t seed);

View File

@ -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);

View File

@ -0,0 +1,10 @@
#pragma once
#include "system.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
int32_t world_gen(int32_t seed, uint8_t width, uint8_t height, uint8_t *buffer, uint32_t size);

View File

@ -0,0 +1,3 @@
#include "blocks.h"
// todo: csv parsing + utils

View File

@ -1,7 +1,36 @@
#define ZPL_IMPL
#include "zpl.h"
#define LIBRG_IMPL
#define LIBRG_CUSTOM_ZPL
#include "librg.h"
int main(void) {
#include "system.h"
#include "options.h"
#define DEFAULT_WORLD_SEED 302097
int main(int argc, char** argv) {
zpl_opts opts={0};
zpl_opts_init(&opts, zpl_heap(), argv[0]);
zpl_opts_add(&opts, "?", "help", "the HELP section", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "p", "preview-map", "draw world preview", ZPL_OPTS_FLAG);
zpl_opts_add(&opts, "s", "seed", "world seed", ZPL_OPTS_INT);
uint32_t ok = zpl_opts_compile(&opts, argc, argv);
if (!ok) {
zpl_opts_print_errors(&opts);
zpl_opts_print_help(&opts);
return -1;
}
int32_t seed = zpl_opts_integer(&opts, "seed", DEFAULT_WORLD_SEED);
if (zpl_opts_has_arg(&opts, "preview-map")) {
generate_minimap(seed);
return 0;
}
printf("hello world\n");
return 0;
}

View File

@ -0,0 +1,18 @@
#include "options.h"
#include "perlin.h"
#include "zpl.h"
#define TEST_MAP_DIM 32
#define TEST_MAP_DEPTH 18
static char *map_pattern = "~~..,,oo---OO^^^@@";
void generate_minimap(int32_t seed) {
for (uint32_t y=0; y<TEST_MAP_DIM; y++) {
for (uint32_t x=0; x<TEST_MAP_DIM; x++) {
double sample = perlin_fbm(seed, x, y, 1.0, 1) * TEST_MAP_DEPTH;
zpl_printf("%c", map_pattern[(uint32_t)sample]);
}
zpl_printf("\n");
}
}

View File

@ -0,0 +1,69 @@
// adapted from: https://gist.github.com/nowl/828013#gistcomment-2807232
#include "perlin.h"
#include "math.h"
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;
}

View File

@ -0,0 +1,5 @@
#include "world.h"
int32_t world_gen(int32_t seed, uint8_t width, uint8_t height, uint8_t *buffer, uint32_t size) {
return WORLD_ERROR_NONE;
}

View File

@ -1 +1,3 @@
#pragma once
#include <stdint.h>

3
data/blocks.csv 100644
View File

@ -0,0 +1,3 @@
id,name,flags
0,base_ground,0
1,base_wall,1
1 id name flags
2 0 base_ground 0
3 1 base_wall 1