From cb76c5137c30a6060b131d37a3befc4735db591e Mon Sep 17 00:00:00 2001 From: Lephenixnoir Date: Wed, 24 May 2023 09:53:12 +0200 Subject: [PATCH] lv1: slightly improve random generation --- CMakeLists.txt | 1 + src/generator.h | 14 +++++++++++- src/generator/gen1.cpp | 51 +++++++++++++++++++++++++----------------- src/generator/path.cpp | 22 ++++++++++++++++++ src/settings.h | 4 ++-- 5 files changed, 68 insertions(+), 24 deletions(-) create mode 100644 src/generator/path.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 913654b..c43accc 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -13,6 +13,7 @@ set(SOURCES src/generator/gen1.cpp src/generator/gen2.cpp src/generator/gen3.cpp + src/generator/path.cpp src/level.cpp src/log.cpp src/main.cpp diff --git a/src/generator.h b/src/generator.h index 7d9a8f6..a2f460c 100644 --- a/src/generator.h +++ b/src/generator.h @@ -4,6 +4,18 @@ struct level; typedef struct level level_t; +/* A utility structure holding data for path carver - a generator tool that + produces a random traversable path. */ +struct path +{ + path(int initial_pos); + void next(struct section *section); + +private: + int pos; + bool last_skipped; +}; + struct generator { virtual void generate(level_t *) = 0; @@ -16,7 +28,7 @@ struct gen1 : public generator void generate(level_t *) override; ~gen1() override = default; - int last_pos; + path paths[3]; }; struct gen2 : public generator diff --git a/src/generator/gen1.cpp b/src/generator/gen1.cpp index 2e54512..722bd16 100644 --- a/src/generator/gen1.cpp +++ b/src/generator/gen1.cpp @@ -3,32 +3,41 @@ #define N PLATFORM_COUNT -gen1::gen1() : last_pos{0} +gen1::gen1(): paths{{0}, {N/3}, {N*2/3}} { - srand(0xc0ffee); + srand(0xc0ffee); } void gen1::generate(level_t *level) { - struct section section; - int r = rand() % 3; + int kind = rand() % 2; + int length = 2 + rand() % 6; - if(r == 0) { - for(int i = 0; i < N; i++) - section.platforms[i].type = PLATFORM_WHITE; - } - else if(r == 1) { - for(int i = 0; i < N; i++) { - auto t = (i % 2) ? PLATFORM_WHITE : PLATFORM_EMPTY; - section.platforms[i].type = t; - } - } - else if(r == 2) { - for(int i = 0; i < N; i++) { - auto t = (i % 2) ? PLATFORM_EMPTY : PLATFORM_WHITE; - section.platforms[i].type = t; - } - } + for(int k = 0; k < length; k++) { + struct section section; + for(int i = 0; i < N; i++) + section.platforms[i].type = PLATFORM_EMPTY; - level->section_buffer.push_back(section); + /* Kind #0: Only the random paths */ + for(int i = 0; i < 3; i++) + this->paths[i].next(§ion); + + /* Kind #1: Also add some fixed blocks */ + if(kind == 1) { + int r = rand() % 3; + + if(r == 0 || r == 1) { + for(int i = 0; i < N; i++) { + auto t = ((i ^ k) & 1) ? PLATFORM_WHITE : PLATFORM_EMPTY; + section.platforms[i].type = t; + } + } + if(r == 2) { + for(int i = 0; i < N; i++) + section.platforms[i].type = PLATFORM_WHITE; + } + } + + level->section_buffer.push_back(section); + } } diff --git a/src/generator/path.cpp b/src/generator/path.cpp new file mode 100644 index 0000000..6766b70 --- /dev/null +++ b/src/generator/path.cpp @@ -0,0 +1,22 @@ +#include "../generator.h" +#include "../level.h" + +path::path(int initial_pos): + pos {initial_pos}, last_skipped {true} +{ +} + +void path::next(struct section *section) +{ + int skip = !this->last_skipped && (rand() % 4 == 0); + + if(skip) { + this->last_skipped = true; + } + else { + int diff = rand() % 3 - 1; + section->platforms[this->pos].type = PLATFORM_WHITE; + this->pos = (this->pos + PLATFORM_COUNT + diff) % PLATFORM_COUNT; + this->last_skipped = false; + } +} diff --git a/src/settings.h b/src/settings.h index d7fdd86..d219473 100644 --- a/src/settings.h +++ b/src/settings.h @@ -4,13 +4,13 @@ /* Radius of the level cylinder, in world units. */ #define LEVEL_RADIUS num(4.0) /* Section lengths, in world units. */ -#define SECTION_LENGTH num(4.0) +#define SECTION_LENGTH num(3.0) /* Number of platforms in the world cylinder. */ #define PLATFORM_COUNT 10 /* Magnitude of the gravity field, locally (world units/s^2), when holding the jump button and when releasing it. */ #define HOVERING_GRAVITY num(-1.25) -#define FALLING_GRAVITY num(-6.0) +#define FALLING_GRAVITY num(-8.0) /* Vertical speed granted when a jump starts (world units/s). */ #define JUMP_THRUST num(1.5)