rebuild SurroundMap (WIP)
This commit is contained in:
parent
7c0c268508
commit
2a4dbe3ac4
@ -6,16 +6,17 @@
|
|||||||
#include <glm/glm.hpp>
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
namespace util {
|
namespace util {
|
||||||
template<class T>
|
|
||||||
using OutCallback = std::function<void(const T&)>;
|
|
||||||
|
|
||||||
template<class T, typename TCoord=int>
|
template<class T, typename TCoord=int>
|
||||||
class AreaMap2D {
|
class AreaMap2D {
|
||||||
|
public:
|
||||||
|
using OutCallback = std::function<void(const T&)>;
|
||||||
|
private:
|
||||||
TCoord offsetX, offsetY;
|
TCoord offsetX, offsetY;
|
||||||
TCoord sizeX, sizeY;
|
TCoord sizeX, sizeY;
|
||||||
std::vector<T> firstBuffer;
|
std::vector<T> firstBuffer;
|
||||||
std::vector<T> secondBuffer;
|
std::vector<T> secondBuffer;
|
||||||
OutCallback<T> outCallback;
|
OutCallback outCallback;
|
||||||
|
|
||||||
size_t valuesCount = 0;
|
size_t valuesCount = 0;
|
||||||
|
|
||||||
@ -93,7 +94,7 @@ namespace util {
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setOutCallback(const OutCallback<T>& callback) {
|
void setOutCallback(const OutCallback& callback) {
|
||||||
outCallback = callback;
|
outCallback = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -4,114 +4,25 @@
|
|||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
|
|
||||||
void SurroundMap::resetMarks() {
|
SurroundMap::SurroundMap(int loadDistance, ubyte maxLevel)
|
||||||
for (auto& [_, entry] : entries) {
|
: areaMap((loadDistance + maxLevel) * 2 + 1,
|
||||||
entry.marked = false;
|
(loadDistance + maxLevel) * 2 + 1),
|
||||||
}
|
levelCallbacks(maxLevel),
|
||||||
|
maxLevel(maxLevel)
|
||||||
|
{}
|
||||||
|
|
||||||
|
void SurroundMap::setLevelCallback(int level, LevelCallback callback) {
|
||||||
|
levelCallbacks.at(level) = callback;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool SurroundMap::createEntry(const glm::ivec2& origin) {
|
void SurroundMap::setOutCallback(util::AreaMap2D<ubyte>::OutCallback callback) {
|
||||||
auto& entry = entries[origin];
|
areaMap.setOutCallback(callback);
|
||||||
if (entry.confirmed) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
entry.confirmed = true;
|
|
||||||
|
|
||||||
// calculate initial number of surrounding entries (any level)
|
|
||||||
int bitOffset = 0;
|
|
||||||
for (int y = -1; y <= 1; y++) {
|
|
||||||
for (int x = -1; x <= 1; x++) {
|
|
||||||
if (x == y && x == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
const auto& found = entries.find(origin + glm::ivec2(x, y));
|
|
||||||
if (found != entries.end()) {
|
|
||||||
entry.surrounding |= (1 << bitOffset);
|
|
||||||
if (found->second.level == 0) {
|
|
||||||
found->second.surrounding |= (1 << (7 - bitOffset));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bitOffset++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<glm::ivec2> SurroundMap::upgrade() {
|
void SurroundMap::completeAt(int x, int y) {
|
||||||
std::vector<glm::ivec2> expansion;
|
// TODO
|
||||||
std::queue<glm::ivec2> expanding;
|
|
||||||
for (const auto& [pos, entry] : entries) {
|
|
||||||
if (entry.confirmed && entry.surrounding != 0xFF) {
|
|
||||||
expanding.push(pos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
while (!expanding.empty()) {
|
|
||||||
assert(expanding.size() < 64);
|
|
||||||
|
|
||||||
glm::ivec2 pos = expanding.front();
|
|
||||||
expanding.pop();
|
|
||||||
|
|
||||||
const auto& found = entries.find(pos);
|
|
||||||
assert(found != entries.end() && "concurrent modification");
|
|
||||||
|
|
||||||
auto& entry = found->second;
|
|
||||||
int uplevelSurrounding = 0;
|
|
||||||
int bitOffset = 0;
|
|
||||||
for (int y = -1; y <= 1; y++) {
|
|
||||||
for (int x = -1; x <= 1; x++) {
|
|
||||||
if (x == y && x == 0) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
glm::ivec2 npos = {pos.x+x, pos.y+y};
|
|
||||||
const auto& nfound = entries.find(npos);
|
|
||||||
|
|
||||||
if (entry.surrounding & (1 << bitOffset)) {
|
|
||||||
auto& nentry = nfound->second;
|
|
||||||
if (nentry.level > entry.level) {
|
|
||||||
uplevelSurrounding |= (1 << bitOffset);
|
|
||||||
}
|
|
||||||
bitOffset++;
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
if (entry.level == 0) {
|
|
||||||
// neighbour entry does not exist
|
|
||||||
createEntry(npos);
|
|
||||||
expansion.push_back(npos);
|
|
||||||
} else{
|
|
||||||
assert(nfound != entries.end() && "invalid map state");
|
|
||||||
if (nfound->second.level == entry.level + 1) {
|
|
||||||
nfound->second.surrounding |= (1 << (7 - bitOffset));
|
|
||||||
expanding.push(npos);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
bitOffset++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// level up
|
|
||||||
entry.surrounding = uplevelSurrounding;
|
|
||||||
entry.level++;
|
|
||||||
}
|
|
||||||
return expansion;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void SurroundMap::getLevels(unsigned char* out, int width, int height, int ox, int oy) const {
|
void SurroundMap::setCenter(int x, int y) {
|
||||||
std::memset(out, 0, width * height);
|
areaMap.setCenter(x, y);
|
||||||
for (const auto& [pos, entry] : entries) {
|
|
||||||
int x = pos.x - ox;
|
|
||||||
int y = pos.y - oy;
|
|
||||||
if (x < 0 || x >= width || y < 0 || y >= height) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
int surroundNum = 0;
|
|
||||||
for (int i = 0; i < 8; i++) {
|
|
||||||
if (entry.surrounding & (1 << i)) {
|
|
||||||
surroundNum++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (surroundNum) {
|
|
||||||
out[y * width + x] = surroundNum + 1;
|
|
||||||
}
|
|
||||||
out[y * width + x] = entry.level + 1;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,43 +5,23 @@
|
|||||||
#define GLM_ENABLE_EXPERIMENTAL
|
#define GLM_ENABLE_EXPERIMENTAL
|
||||||
#include <glm/gtx/hash.hpp>
|
#include <glm/gtx/hash.hpp>
|
||||||
|
|
||||||
|
#include "typedefs.hpp"
|
||||||
|
#include "util/AreaMap2D.hpp"
|
||||||
|
|
||||||
class SurroundMap {
|
class SurroundMap {
|
||||||
struct Entry {
|
|
||||||
/// @brief Level is increased when all surrounding (8) entries having
|
|
||||||
// greather or equal confirmed level
|
|
||||||
int level = 0;
|
|
||||||
/// @brief bits storing surrounding entries having greather or equal
|
|
||||||
/// confirmed level.
|
|
||||||
/// 0 - is x=-1,y=-1 offset, 1 is x=0,y=-1, ... 7 is x=1,y=1
|
|
||||||
/// (Prevents extra access to the entries hashmap)
|
|
||||||
uint8_t surrounding = 0x0;
|
|
||||||
/// @brief level confirmed status (entry is ready to expand)
|
|
||||||
bool confirmed = false;
|
|
||||||
/// @brief mark used on sweep event (extra isles garbage collection)
|
|
||||||
bool marked = false;
|
|
||||||
};
|
|
||||||
std::unordered_map<glm::ivec2, Entry> entries;
|
|
||||||
public:
|
public:
|
||||||
/// @brief Reset all isles marks
|
using LevelCallback = std::function<void(ubyte)>;
|
||||||
void resetMarks();
|
private:
|
||||||
|
util::AreaMap2D<ubyte> areaMap;
|
||||||
|
std::vector<LevelCallback> levelCallbacks;
|
||||||
|
ubyte maxLevel;
|
||||||
|
public:
|
||||||
|
SurroundMap(int loadDistance, ubyte maxLevel);
|
||||||
|
|
||||||
/// @brief Mark all connected entries
|
void setLevelCallback(int level, LevelCallback callback);
|
||||||
/// @param origin origin point
|
void setOutCallback(util::AreaMap2D<ubyte>::OutCallback callback);
|
||||||
void markIsle(const glm::ivec2& origin);
|
|
||||||
|
|
||||||
/// @brief Erase all non-marked isles
|
void completeAt(int x, int y);
|
||||||
/// @return erased entries positions
|
|
||||||
std::vector<glm::ivec2> sweep();
|
|
||||||
|
|
||||||
/// @brief Attempt to upgrade all confirmed entries with specified level
|
void setCenter(int x, int y);
|
||||||
/// @param level target entries level
|
|
||||||
/// @return All upgraded entries positions
|
|
||||||
std::vector<glm::ivec2> upgrade();
|
|
||||||
|
|
||||||
/// @brief Create entry if does not exist
|
|
||||||
/// @param origin entry position
|
|
||||||
/// @return true if new entry has been created
|
|
||||||
bool createEntry(const glm::ivec2& origin);
|
|
||||||
|
|
||||||
void getLevels(unsigned char* out, int width, int height, int ox, int oy) const;
|
|
||||||
};
|
};
|
||||||
|
|||||||
@ -3,11 +3,5 @@
|
|||||||
#include "world/generator/SurroundMap.hpp"
|
#include "world/generator/SurroundMap.hpp"
|
||||||
|
|
||||||
TEST(SurroundMap, InitTest) {
|
TEST(SurroundMap, InitTest) {
|
||||||
int w = 8;
|
SurroundMap map(50, 8);
|
||||||
int h = 8;
|
|
||||||
|
|
||||||
SurroundMap map;
|
|
||||||
map.createEntry({w/2, h/2});
|
|
||||||
map.createEntry({w/2+1, h/2+1});
|
|
||||||
EXPECT_EQ(map.upgrade().size(), 12);
|
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user