<commit_before>#include "GameObjects/WorldChunk.hpp"

const std::string WorldChunk::CityMarker = "city";
const std::string WorldChunk::MapMarker = "MAP";
    
std::map<std::string, void (*)(WorldChunk *self, std::stringstream *ss)> WorldChunk::factoryMap = 
    {
        {WorldChunk::CityMarker, spawnCity}
    }; 

bool WorldChunk::configured = false;
unsigned int WorldChunk::chunk_height = 0;
unsigned int WorldChunk::chunk_width = 0;

int WorldChunk::maxY = 0;
int WorldChunk::maxX = 0;

WorldChunk::WorldChunk()
{
    if(!WorldChunk::configured) {
        this->configure();
    } 
}

WorldChunk::WorldChunk(unsigned int top, unsigned int left)
{
    if(!WorldChunk::configured) {
        this->configure();
    } 
    this->top = top * WorldChunk::chunk_height;
    this->left = left * WorldChunk::chunk_width;
}

WorldChunk::WorldChunk(std::stringstream *ss)
{
    this->fromStringStream(ss);
    if(!WorldChunk::configured) {
        this->configure();
    } 
}

void WorldChunk::setMaxYX(unsigned int y, unsigned int x)
{
    WorldChunk::maxY = y;
    WorldChunk::maxX = x;
}

void WorldChunk::configure()
{
    WorldChunk::chunk_height = ConfigLoader::getIntOption("chunk_height", 30);
    WorldChunk::chunk_width = ConfigLoader::getIntOption("chunk_width", 30);
    WorldChunk::configured = true;
}

void WorldChunk::fromStringStream(std::stringstream *ss)
{
    READ_STATE readState = object;
    std::vector<Tile> tilesToSpawn;
    std::string nextObject;
    (*ss)>>top>>left;
    while ( 
        ss->good() &&
        tilesToSpawn.size() < WorldChunk::chunk_height * WorldChunk::chunk_width 
    ) {
        if(readState == object) {
            (*ss)>>nextObject;
            if (this->factoryMap.find(nextObject) != this->factoryMap.end()) {
                this->factoryMap[nextObject](this, ss);
            } else if (nextObject == WorldChunk::MapMarker) {
                readState = map;
            }
        } else if(readState == map) {
            tilesToSpawn.push_back(Tile(ss));
        }
    }    
    this->organizeTiles(tilesToSpawn);
}

void WorldChunk::toStringStream(std::stringstream *ss)
{
    (*ss)<<top<<Globals::space_delimiter<<left<<Globals::object_delimiter; 
    for(unsigned int i = 0; i < this->cities.size(); ++i) {
        (*ss)<<WorldChunk::CityMarker<<Globals::space_delimiter;
        this->cities[i].toStringStream(ss);
    }
    (*ss)<<Globals::space_delimiter<<WorldChunk::MapMarker<<Globals::space_delimiter;
    for(unsigned int i = 0; i < this->tiles.size(); ++i) {
        for(unsigned int j = 0; j < this->tiles[i].size(); ++j) {
            this->tiles[i][j].toStringStream(ss); 
            (*ss)<<Globals::space_delimiter;
        }
    }
}

void WorldChunk::generateChunk()
{
    this->cities.clear();
    this->tiles.clear();

    for(unsigned int i = 0; i < WorldChunk::chunk_height; ++i) {
        this->tiles.push_back(std::vector<Tile>());
        for( unsigned int j = 0; j < WorldChunk::chunk_width; ++j) {
            this->tiles[i].push_back(Tile(0, 0)); 
        }
    }

}

void WorldChunk::spawnCity(WorldChunk *self, std::stringstream *ss)
{
    std::string c;
    std::string inv;
    std::getline((*ss), c, Globals::object_delimiter);
    std::getline((*ss), inv, Globals::object_delimiter);
    std::stringstream tss;
    tss.str(c + Globals::object_delimiter + inv);
    self->cities.push_back(City(&tss));
}

void WorldChunk::organizeTiles(std::vector<Tile> tiles)
{
    for(unsigned int i = 0; i < WorldChunk::chunk_height; ++i) {
        this->tiles.push_back(std::vector<Tile>());
        for( unsigned int j = 0; j < WorldChunk::chunk_width; ++j) {
            this->tiles[i].push_back(tiles[(i*WorldChunk::chunk_height) + j]);
        }
    }
}


void WorldChunk::draw(Window::window_ptr window, int offsetTop, int offsetLeft)
{
    int currX, currY;
    Tile::setPallete();
    for(unsigned int i = 0; i < WorldChunk::chunk_height; ++i) {
        for(unsigned int j = 0; j < WorldChunk::chunk_width; ++j) {
            this->tiles[i][j].drawAt(
                window, 
                (top + i - offsetTop),
                (left + j - offsetLeft),
                //this->doCullTile(offsetTop, offsetLeft, top + i, left + j, i, j)
                false
            );
        }
    }    
}

City WorldChunk::getCity(int y, int x)
{
    for (int i = 0; i < this->cities.size(); ++i) {
        if (this->cities[i].getPosX() == x && this->cities[i].getPosY() == y) {
            return this->cities[i];
        }
    }
    return City();
}

// @todo: move to render pipeline
bool WorldChunk::doCullTile(
    int playerY, 
    int playerX, 
    int tileY, 
    int tileX, 
    int localTileI, 
    int localTileJ<commit_msg>get server play working again<commit_after>#include "GameObjects/WorldChunk.hpp"

const std::string WorldChunk::CityMarker = "city";
const std::string WorldChunk::MapMarker = "MAP";
    
std::map<std::string, void (*)(WorldChunk *self, std::stringstream *ss)> WorldChunk::factoryMap = 
    {
        {WorldChunk::CityMarker, spawnCity}
    }; 

bool WorldChunk::configured = false;
unsigned int WorldChunk::chunk_height = 0;
unsigned int WorldChunk::chunk_width = 0;

int WorldChunk::maxY = 0;
int WorldChunk::maxX = 0;

WorldChunk::WorldChunk()
{
    if(!WorldChunk::configured) {
        this->configure();
    } 
}

WorldChunk::WorldChunk(unsigned int top, unsigned int left)
{
    if(!WorldChunk::configured) {
        this->configure();
    } 
    this->top = top * WorldChunk::chunk_height;
    this->left = left * WorldChunk::chunk_width;
}

WorldChunk::WorldChunk(std::stringstream *ss)
{
    this->fromStringStream(ss);
    if(!WorldChunk::configured) {
        this->configure();
    } 
}

void WorldChunk::setMaxYX(unsigned int y, unsigned int x)
{
    WorldChunk::maxY = y;
    WorldChunk::maxX = x;
}

void WorldChunk::configure()
{
    WorldChunk::chunk_height = ConfigLoader::getIntOption("chunk_height", 30);
    WorldChunk::chunk_width = ConfigLoader::getIntOption("chunk_width", 30);
    WorldChunk::configured = true;
}

void WorldChunk::fromStringStream(std::stringstream *ss)
{
    READ_STATE readState = object;
    std::vector<Tile> tilesToSpawn;
    std::string nextObject;
    (*ss)>>top>>left;
    while ( 
        ss->good() &&
        tilesToSpawn.size() < WorldChunk::chunk_height * WorldChunk::chunk_width 
    ) {
        if(readState == object) {
            (*ss)>>nextObject;
            if (this->factoryMap.find(nextObject) != this->factoryMap.end()) {
                this->factoryMap[nextObject](this, ss);
            } else if (nextObject == WorldChunk::MapMarker) {
                readState = map;
            }
        } else if(readState == map) {
            tilesToSpawn.push_back(Tile(ss));
        }
    }    
    this->organizeTiles(tilesToSpawn);
}

void WorldChunk::toStringStream(std::stringstream *ss)
{
    (*ss)<<top<<Globals::space_delimiter<<left<<Globals::object_delimiter; 
    for(unsigned int i = 0; i < this->cities.size(); ++i) {
        (*ss)<<WorldChunk::CityMarker<<Globals::space_delimiter;
        this->cities[i].toStringStream(ss);
    }
    (*ss)<<Globals::space_delimiter<<WorldChunk::MapMarker<<Globals::space_delimiter;
    for(unsigned int i = 0; i < this->tiles.size(); ++i) {
        for(unsigned int j = 0; j < this->tiles[i].size(); ++j) {
            this->tiles[i][j].toStringStream(ss); 
            (*ss)<<Globals::space_delimiter;
        }
    }
}

void WorldChunk::generateChunk()
{
    this->cities.clear();
    this->tiles.clear();

    for(unsigned int i = 0; i < WorldChunk::chunk_height; ++i) {
        this->tiles.push_back(std::vector<Tile>());
        for( unsigned int j = 0; j < WorldChunk::chunk_width; ++j) {
            this->tiles[i].push_back(Tile(0, 0)); 
        }
    }

}

void WorldChunk::spawnCity(WorldChunk *self, std::stringstream *ss)
{
    std::string c;
    std::string inv;
    std::getline((*ss), c, Globals::object_delimiter);
    std::getline((*ss), inv, Globals::object_delimiter);
    std::stringstream tss;
    tss.str(c + Globals::object_delimiter + inv);
    self->cities.push_back(City(&tss));
}

void WorldChunk::organizeTiles(std::vector<Tile> tiles)
{
    for(unsigned int i = 0; i < WorldChunk::chunk_height; ++i) {
        this->tiles.push_back(std::vector<Tile>());
        for( unsigned int j = 0; j < WorldChunk::chunk_width; ++j) {
            this->tiles[i].push_back(tiles[(i*WorldChunk::chunk_height) + j]);
        }
    }
}


void WorldChunk::draw(Window::window_ptr window, int offsetTop, int offsetLeft)
{
    int currX, currY;
    Tile::setPallete();
    for(unsigned int i = 0; i < this->tiles.size(); ++i) {
        for(unsigned int j = 0; j < this->tiles[i].size(); ++j) {
            this->tiles[i][j].drawAt(
                window, 
                (top + i - offsetTop),
                (left + j - offsetLeft),
                //this->doCullTile(offsetTop, offsetLeft, top + i, left + j, i, j)
                false
            );
        }
    }    
}

City WorldChunk::getCity(int y, int x)
{
    for (int i = 0; i < this->cities.size(); ++i) {
        if (this->cities[i].getPosX() == x && this->cities[i].getPosY() == y) {
            return this->cities[i];
        }
    }
    return City();
}

// @todo: move to render pipeline
bool WorldChunk::doCullTile(
    int playerY, 
    int playerX, 
    int tileY, 
    int tileX, 
    int localTileI, 
    int localTileJ
{'fraction_non_alphanumeric': 0.16203605514316013, 'fraction_numerical': 0.0042417815482502655, 'mean_word_length': 2.96427070197562, 'pattern_counts': {'":': 0, '<': 75, '<?xml version=': 0, '>': 85, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2622081', 'n_tokens_mistral': 3213, 'n_tokens_neox': 3146, 'n_words': 583}