diff options
author | Paweł Redman <pawel.redman@gmail.com> | 2017-10-09 19:24:08 +0200 |
---|---|---|
committer | Paweł Redman <pawel.redman@gmail.com> | 2017-10-09 19:24:08 +0200 |
commit | 8d6cb03cd83b9f6576909058601af54f16c8acfe (patch) | |
tree | 1aae264859680e888c5801f279b638cefa137fb9 /src/world.cpp | |
parent | 9e7d64d052eabcf40d85c3e903aaba44903a380a (diff) |
Refactoring and basic map generation.
Diffstat (limited to 'src/world.cpp')
-rw-r--r-- | src/world.cpp | 64 |
1 files changed, 39 insertions, 25 deletions
diff --git a/src/world.cpp b/src/world.cpp index b5aeea2..dc25f66 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -23,21 +23,39 @@ bool sector_index_t::operator<(sector_index_t B) const return false; } -void sector_t::generate(sector_index_t index) +world_t::world_t(void) { + prng.seed(124); + perlin.generate(&prng, 32); +} + +void world_t::generate(sector_t *sector, sector_index_t index) +{ + sector->bounds.left = index.x * SECTOR_SIZE; + sector->bounds.top = index.y * SECTOR_SIZE; + sector->bounds.width = SECTOR_SIZE; + sector->bounds.height = SECTOR_SIZE; + std::cout << "generating (" << index.x << ", " << index.y << ")\n"; for (ssize_t ly = 0; ly < SECTOR_SIZE; ly++) for (ssize_t lx = 0; lx < SECTOR_SIZE; lx++) { ssize_t x, y; + float noise; x = index.x * SECTOR_SIZE + lx; y = index.y * SECTOR_SIZE + ly; - tiles[ly * SECTOR_SIZE + lx].type = (x ^ y) * 151; + noise = perlin.get(x, y, 30.0f) * 0.6f + + perlin.get(x, y, 15.0f) * 0.25f + + perlin.get(x, y, 7.0f) * 0.1f + + perlin.get(x, y, 3.0f) * 0.05f; + + sector->tiles[ly * SECTOR_SIZE + lx].type = + (noise + 1) * 100; } - empty = false; + sector->empty = false; } sector_t *world_t::get_sector(sector_index_t index) @@ -47,40 +65,36 @@ sector_t *world_t::get_sector(sector_index_t index) sector = §ors[index]; if (sector->empty) - sector->generate(index); + generate(sector, index); return sector; } -// divide and round to minus infinity -static ssize_t divide_rmi(ssize_t x, ssize_t y, size_t *rem) -{ - ssize_t rv; - - if (x >= 0) { - *rem = x % y; - return x / y; - } - - rv = (x + 1) / y - 1; - *rem = x - rv * y; - return rv; -} - tile_t *world_t::get_tile(ssize_t x, ssize_t y) { sector_index_t index; sector_t *sector; - size_t tx, ty; + ssize_t tx, ty; - index.x = divide_rmi(x, SECTOR_SIZE, &tx); - index.y = divide_rmi(y, SECTOR_SIZE, &ty); - printf("get_tile(%zd, %zd)\n", x, y); - printf("\tsector is (%zd, %zd)\n", index.x, index.y); + index.x = divide_rmi(x, (ssize_t)SECTOR_SIZE, &tx); + index.y = divide_rmi(y, (ssize_t)SECTOR_SIZE, &ty); sector = get_sector(index); - printf("\ttile is (%zd, %zd)\n", tx, ty); return sector->tiles + ty * SECTOR_SIZE + tx; } +void entity_t::link(world_t *world) +{ + // TODO +} + +void entity_t::unlink(void) +{ + for (sector_t *sector : parents) + sector->ents.erase(sector->ents.find(this)); + + parents.clear(); + parent_world = nullptr; } + +} // namespace world |