Skip to content

Instantly share code, notes, and snippets.

else if(roomOrientation == RoomOrientation::staggered) {
if(!staggeredIsoMapUtil) {
errStream << "Error loading tiles for staggered iso map, staggered map details incomplete." << std::endl;
return false;
}
if(staggeredIsoMapUtil->staggerAxis == "x") {
tile->set_x(currX * mapTileWidth / 2);
if((currX%2 == 0 && staggeredIsoMapUtil->staggerIndex == "odd") ||
else if(orientation == "staggered") {
roomOrientation = RoomOrientation::staggered;
staggeredIsoMapUtil = std::make_unique<StaggerUtil>();
staggeredIsoMapUtil->staggerAxis = resNode->room().staggeraxis();
staggeredIsoMapUtil->staggerIndex = resNode->room().staggerindex();
if(staggeredIsoMapUtil->staggerAxis == "x") {
resNode->mutable_room()->set_width(nHoriTiles * tileWidthPixels / 2);
resNode->mutable_room()->set_height(nVertTiles * tileHeightPixels);
}
else if(roomOrientation == RoomOrientation::isometric) {
int xStart = ((layerWidth * mapTileWidth) / 2) /*Mid x coordinate of the room in pixels*/
- (mapTileWidth / 2) /*Reposition by an offset of half of tileWidth*/
- (currY * (mapTileWidth / 2)); /*Reposition x coordinate depending on the row index or currY*/
int x = xStart + (currX * mapTileWidth / 2);
int y = (currY * mapTileHeight / 2) + (currX * mapTileHeight / 2);
tile->set_x(x);
tile->set_y(y);
}
struct StaggerUtil {
/**
* @brief Specifies the alignment of hex tiles, can be either "x" or "y"
*/
std::string staggerAxis;
/**
* @brief Specifies index(among each pair of neighbouring indices) to be shifted to create hex map,
* can be either "even" or "odd"
*/
std::string staggerIndex;
if(hexMapUtil) {
if(hexMapUtil->staggerAxis == "x") {
tile->set_x(currX * (tileWidth - (hexMapUtil->hexSideLength/2)));
if((currX%2 == 0 && hexMapUtil->staggerIndex == "odd") || (currX%2 != 0 && hexMapUtil->staggerIndex == "even"))
tile->set_y(currY*tileHeight);
else
tile->set_y(currY*tileHeight + hexMapUtil->hexSideLength);
}
else {
unsigned int nHoriTiles = resNode->room().width();
unsigned int nVertTiles = resNode->room().height();
unsigned int tileWidthPixels = resNode->room().hsnap();
unsigned int tileHeightPixels = resNode->room().vsnap();
if(resNode->mutable_room()->orientation() == "hexagonal") {
hexMapUtil = std::make_unique<HexMapUtil>();
hexMapUtil->hexSideLength = resNode->room().hexsidelength();
hexMapUtil->staggerAxis = resNode->room().staggeraxis();
hexMapUtil->staggerIndex = resNode->room().staggerindex();
struct HexMapUtil {
unsigned int hexSideLength;
string staggerAxis;
string staggerIndex;
};
std::unique_ptr<char[]> Decoder::decompress(size_t &length, size_t off) {
std::unique_ptr<char[]> src = read(length, off);
z_stream zs = {}; // zero-initialize
// using inflateInit2 with windowBits(2nd argument) set to MAX_WBITS | 32, triggers header detection to properly
// decompress both zlib and gzip streams
if (inflateInit2(&zs, MAX_WBITS | 32) != Z_OK) {
errStream << "Failed to initialize zlib inflate" << std::endl;
return nullptr;
}
bool LoadBase64ZstdLayerData(const std::string &decodedStr, const size_t &expectedSize, buffers::TreeNode *resNode,
const int& tileWidth, const int& tileHeight, const int& layerWidth, const int& layerHeight) {
std::vector<unsigned char> outTileData;
outTileData.resize(expectedSize);
// decompress zstd compressed data
size_t const outSize = ZSTD_decompress(outTileData.data(), outTileData.size(),
decodedStr.data(), decodedStr.size());
if(ZSTD_isError(outSize)) {
find_library(LIB_ZSTD NAMES zstd)
target_link_libraries(${LIB_EGM} PRIVATE ${LIB_ZSTD})