#include "common.hpp" namespace world { sector_index_t::sector_index_t () { } sector_index_t::sector_index_t (int64_t x_, int64_t y_) { x = x_; y = y_; } bool sector_index_t::operator<(sector_index_t B) const { if (x < B.x) return true; if (x > B.x) return false; if (y < B.y) return true; return false; } 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; 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; } sector->empty = false; } sector_t *world_t::get_sector(sector_index_t index) { sector_t *sector; sector = §ors[index]; if (sector->empty) generate(sector, index); return sector; } tile_t *world_t::get_tile(ssize_t x, ssize_t y) { sector_index_t index; sector_t *sector; ssize_t tx, ty; index.x = divide_rmi(x, (ssize_t)SECTOR_SIZE, &tx); index.y = divide_rmi(y, (ssize_t)SECTOR_SIZE, &ty); sector = get_sector(index); return sector->tiles + ty * SECTOR_SIZE + tx; } static sector_index_t get_index(float x, float y) { return sector_index_t(floor(x), floor(y)); } void entity_t::link_to_sector(sector_t *sector) { parents.push_back(sector); sector->ents.insert(this); } void entity_t::link(world_t *world) { sector_index_t base; sector_t *sector; ssize_t dx, dy; // An entity gets linked to at least one sector. base = get_index(bounds.left, bounds.top); sector = world->get_sector(base); link_to_sector(sector); // There might be more, though. //for (dy = 0; dy < } void entity_t::unlink(void) { for (sector_t *sector : parents) sector->ents.erase(sector->ents.find(this)); parents.clear(); parent_world = nullptr; } } // namespace world