implemented map regions generation

This commit is contained in:
devZoGok
2026-04-26 10:18:45 +03:00
parent 5e7d5b839e
commit 8d74d09e39
2 changed files with 80 additions and 2 deletions
+76 -2
View File
@@ -560,6 +560,78 @@ namespace battleship{
}
}
void Map::fillRegion(vector<Map::Cell*>& regionCells, int cellId){
Map::Cell::Type type = cells[cellId].type;
if(find(regionCells.begin(), regionCells.end(), &cells[cellId]) == regionCells.end())
regionCells.push_back(&cells[cellId]);
for(int i = 0; i < cells[cellId].edges.size(); i++){
int adjCellId = cells[cellId].edges[i].destCellId;
bool underwaterWaterCell = (
find(cells[cellId].underWaterCellIds.begin(), cells[cellId].underWaterCellIds.end(), adjCellId)
!=
cells[cellId].underWaterCellIds.end()
);
if(underwaterWaterCell || cells[adjCellId].type != type) continue;
bool adjCellInRegion = (find(regionCells.begin(), regionCells.end(), &cells[adjCellId]) == regionCells.end());
if(adjCellInRegion) fillRegion(regionCells, adjCellId);
}
}
void Map::calculateRegions(){
int nextCellId = 0;
bool allCellsInRegions = false;
int numSurfaceCells = (int(mapSize.x / CELL_SIZE.x)) * (int(mapSize.z / CELL_SIZE.z));
while(!allCellsInRegions){
vector<Map::Cell*> regionCells;
fillRegion(regionCells, nextCellId);
regions.push_back(make_pair(cells[nextCellId].type, regionCells));
int sumCellsInRegions = 0;
for(int i = 0; i < regions.size(); i++)
sumCellsInRegions += regions[i].second.size();
allCellsInRegions = (sumCellsInRegions == numSurfaceCells);
if(!allCellsInRegions)
for(int i = 0; i < cells.size(); i++){
bool inRegion = false;
for(int j = 0; j < regions.size(); j++)
if(find(regions[j].second.begin(), regions[j].second.end(), &cells[i]) != regions[j].second.end()){
inRegion = true;
break;
}
if(!inRegion){
nextCellId = i;
break;
}
}
}
for(pair<Map::Cell::Type, vector<Cell*>>& reg : regions){
if(reg.first != Cell::Type::WATER) continue;
int oldNumCells = reg.second.size();
for(int i = 0; i < oldNumCells; i++)
for(int ucId : reg.second[i]->underWaterCellIds)
reg.second.push_back(&cells[ucId]);
}
}
/*
pair<Map::Cell::Type, vector<Map::Cell&>>& Map::getRegionByCellId(int id){
return regions[0];
}
*/
void Map::load(string mapName){
this->mapName = mapName;
@@ -571,10 +643,14 @@ namespace battleship{
SOL_LUA_STATE.script_file(path + "Models/Maps/" + mapName + "/" + mapName + ".lua");
SOL_LUA_STATE.script_file(path + "Models/Maps/" + mapName + "/cells.lua");
sol::table sizeTable = SOL_LUA_STATE[mapTable]["size"];
mapSize = Vector3(sizeTable["x"], sizeTable["y"], sizeTable["z"]);
preprareScene(false);
loadSpawnPoints();
loadSkybox();
loadCells();
calculateRegions();
loadTerrainObject(-1);
sol::optional<sol::table> lightsOpt = SOL_LUA_STATE[mapTable]["lights"];
@@ -582,8 +658,6 @@ namespace battleship{
if(lightsOpt != sol::nullopt)
loadLights();
sol::table sizeTable = SOL_LUA_STATE[mapTable]["size"];
mapSize = Vector3(sizeTable["x"], sizeTable["y"], sizeTable["z"]);
sol::table wbTbl = SOL_LUA_STATE[mapTable]["waterbodies"];
int numWaterbodies = wbTbl.size();
+4
View File
@@ -83,6 +83,7 @@ namespace battleship{
std::vector<int> getSurroundingCells(vb01::Vector3, int);
void blockCells(Unit*);
void unblockCells(Unit*);
//const std::pair<Cell::Type, std::vector<Cell*>>& getRegionByCellId(int);
inline Map::Cell getCell(int i){return cells[i];}
inline std::string getMapName(){return mapName;}
inline vb01::Node* getNodeParent(){return terrainNode;}
@@ -107,6 +108,7 @@ namespace battleship{
std::vector<Cell> cells;
float baseHeight;
std::vector<vb01::Node*> lights;
std::vector<std::pair<Cell::Type, std::vector<Cell*>>> regions;
Map(){}
void preprareScene(bool);
@@ -116,6 +118,8 @@ namespace battleship{
void loadCells();
void loadPlayerGameObjects(Player*, sol::table);
void loadTerrainObject(int);
void fillRegion(std::vector<Cell*>&, int);
void calculateRegions();
void unloadTerrainObjects();
void unloadCells();
void unloadLights();