diff options
Diffstat (limited to 'src/world.cpp')
-rw-r--r-- | src/world.cpp | 63 |
1 files changed, 36 insertions, 27 deletions
diff --git a/src/world.cpp b/src/world.cpp index 07d5638..e176913 100644 --- a/src/world.cpp +++ b/src/world.cpp @@ -32,40 +32,50 @@ void world_t::generate_tile(tile_t *tile, tile_index_t x) perlin.get(x, 4.0f) * 0.1f + perlin.get(x, 1.0f) * 0.05f; - if (height < waterlevel - 0.2f) - tile->type = -1; - else if (height < waterlevel) - tile->type = 0; - else if (height < waterlevel + 0.05f) - tile->type = 1; - else { - if (perlin.get(x, 5.0f) > 0.0f) - tile->type = 3; - else - tile->type = 2; - - if (height > waterlevel + 0.1f && - perlin.get(x, 2.0f) > 0.3f) - tile->type = 4; - } + if (height < waterlevel) + tile->type = TILE_NONE; + else if (height < waterlevel + 0.3) + tile->type = TILE_DIRT; + else + tile->type = TILE_WALL; } -void world_t::generate(sector_t *sector, sector_index_t index) +void world_t::generate(sector_t *sector, sector_index_t index, bool partial) { sector->index = index; sector->bounds.v[0] = (v2f_t)index * SECTOR_SIZE; sector->bounds.v[1] = sector->bounds.v[0] + v2f_t(SECTOR_SIZE, SECTOR_SIZE); - std::cout << "generating " << index << "\n"; - - for (ssize_t ly = 0; ly < SECTOR_SIZE; ly++) - for (ssize_t lx = 0; lx < SECTOR_SIZE; lx++) + for (coord_t ly = 0; ly < SECTOR_SIZE; ly++) + for (coord_t lx = 0; lx < SECTOR_SIZE; lx++) generate_tile(sector->tiles + ly * SECTOR_SIZE + lx, tile_index_t(index[0] * SECTOR_SIZE + lx, index[1] * SECTOR_SIZE + ly)); sector->empty = false; + + if (partial) + return; + + for (coord_t ly = 0; ly < SECTOR_SIZE; ly++) + for (coord_t lx = 0; lx < SECTOR_SIZE; lx++) { + tile_t *tile; + + tile = sector->tiles + ly * SECTOR_SIZE + lx; + tile->neighbors = 0; + + for (size_t i = 0; i < 4; i++) { + tile_index_t neighbor_index; + tile_t *neighbor; + + neighbor_index = index * SECTOR_SIZE + tile_index_t(lx, ly) + neighbor_offsets[i]; + neighbor = get_tile(neighbor_index, true); + + if (neighbor->type == tile->type) + tile->neighbors |= (1 << i); + } + } } bool world_t::find_path(v2f_t src, v2f_t dst, rectf_t size, entity_t *ignore, @@ -82,8 +92,7 @@ bool world_t::find_path(v2f_t src, v2f_t dst, rectf_t size, entity_t *ignore, tile_index_t index; index = finder.base + tile_index_t(x, y); - node->accessible = (get_tile(index)->type >= 1 && - get_tile(index)->type <= 3); + node->accessible = get_tile(index)->type == TILE_DIRT; } bounds = rectf_t(src, dst).norm(); @@ -116,19 +125,19 @@ bool world_t::find_path(v2f_t src, v2f_t dst, rectf_t size, entity_t *ignore, return true; } -sector_t *world_t::get_sector(sector_index_t index) +sector_t *world_t::get_sector(sector_index_t index, bool partial) { sector_t *sector; sector = §ors[index]; if (sector->empty) - generate(sector, index); + generate(sector, index, partial); return sector; } -tile_t *world_t::get_tile(tile_index_t index) +tile_t *world_t::get_tile(tile_index_t index, bool partial) { sector_index_t sector_index; sector_t *sector; @@ -136,7 +145,7 @@ tile_t *world_t::get_tile(tile_index_t index) sector_index[0] = divide_rmi(index[0], (int64_t)SECTOR_SIZE, &tx); sector_index[1] = divide_rmi(index[1], (int64_t)SECTOR_SIZE, &ty); - sector = get_sector(sector_index); + sector = get_sector(sector_index, partial); return sector->tiles + ty * SECTOR_SIZE + tx; } |