From 045d3aac343d92d0e524afd09d280b882853cd99 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 5 Feb 2023 15:18:28 +0200 Subject: [PATCH] parsing terrain objects --- map.h | 2 + mapEditorAppState.cpp | 112 +++++++++++++++++++++++++++++++++++++++--- mapEditorAppState.h | 2 + unit.h | 1 + 4 files changed, 110 insertions(+), 7 deletions(-) diff --git a/map.h b/map.h index 018027d..27ce890 100755 --- a/map.h +++ b/map.h @@ -37,6 +37,7 @@ namespace battleship{ Cell *cells = nullptr; vb01::u32 **weights = nullptr; + TerrainObject(){} TerrainObject(vb01::Vector3 p, vb01::Vector3 s, vb01::Vector3 cs, Type t, vb01::Node *n, int num, Cell *c, vb01::u32 **w) : pos(p), size(s), @@ -67,6 +68,7 @@ namespace battleship{ inline Player* getPlayer(int i){return players[i];} inline void addPlayer(Player *p){players.push_back(p);} inline int getNumPlayers(){return players.size();} + inline int getNumSpawnPoints(){return spawnPoints.size();} inline vb01::Vector3 getSpawnPoint(int i){return spawnPoints[i];} inline void addSpawnPoint(vb01::Vector3 sp){spawnPoints.push_back(sp);} private: diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index 2b6ecd2..93ae5a6 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -19,10 +19,11 @@ #include #include -#include #include +#include + #include namespace battleship{ @@ -244,8 +245,8 @@ namespace battleship{ }; vector skyboxFolders = readDir(gm->getPath() + "Textures/Skyboxes", true); - SkyboxListbox *skyboxListbox = new SkyboxListbox(startPos + 3 * offset, size, skyboxFolders, (skyboxFolders.size() > maxDisplay ? maxDisplay : skyboxFolders.size()), fontPath); - state->addListbox(skyboxListbox); + skyListbox = new SkyboxListbox(startPos + 3 * offset, size, skyboxFolders, (skyboxFolders.size() > maxDisplay ? maxDisplay : skyboxFolders.size()), fontPath); + state->addListbox(skyListbox); class LandTexListbox : public Listbox{ public: @@ -383,7 +384,7 @@ namespace battleship{ cells[i].pos = initPos + Vector3(xId * cellLength, -yId * cellDepth, zId * cellWidth); } - MeshData meshData = map->getTerrainObject(0).node->getChild(0)->getMesh(0)->getMeshBase(); + MeshData meshData = map->getTerrainObject(0).node->getMesh(0)->getMeshBase(); for(int i = 0; i < numCells; i++){ int waterbodyId = -1; @@ -461,11 +462,11 @@ namespace battleship{ void MapEditorAppState::MapEditor::prepareTerrainObjects(){ for(int i = 0; i < map->getNumTerrainObjects(); i++){ - TerrainObject obj = map->getTerrainObject(i); + TerrainObject &obj = map->getTerrainObject(i); Vector3 size = obj.size; int cellsByDim[]{ int(size.x / cellLength), - cellDepth == 0 ? 1 : int(size.y / cellDepth), + i == 0 ? 1 : int(size.y / cellDepth), int(size.z / cellWidth) }; const int numCells = cellsByDim[0] * cellsByDim[1] * cellsByDim[2]; @@ -477,16 +478,113 @@ namespace battleship{ for(int i = 0; i < numCells; i++) weights[i] = new u32[numCells]; + obj.cells = cells; + obj.numCells = numCells; + obj.weights = weights; + prepareTerrainObject(weights, cells, cellsByDim, (i > 0 ? obj.pos.y : 0), i == 0); } } + string MapEditorAppState::MapEditor::parseTerrainObject(TerrainObject &terrObj){ + string terrObjStr = "{\n"; + + if(terrObj.type = TerrainObject::LANDMASS) + terrObjStr += "model = \"" + map->getMapName() + ".xml\",\n"; + + terrObjStr += "albedoMap = \"" + map->getMapName() + ".jpg\",\n"; + terrObjStr += "size = {x = " + to_string(terrObj.pos.x) + ", y = " + to_string(terrObj.pos.y) + ", z = " + to_string(terrObj.pos.z) + "},\n"; + terrObjStr += "nodes = {\n"; + terrObjStr += "numCells = " + to_string(terrObj.numCells) + ",\n"; + terrObjStr += "size = {x = " + to_string(cellLength) + ", y = " + to_string(cellDepth) + ", z = " + to_string(cellWidth) + "},\n"; + terrObjStr += "pos = {\n"; + + for(int i = 0; i < terrObj.numCells; i++) + terrObjStr += "{x = " + to_string(terrObj.cells[i].pos.x) + ", y = " + to_string(terrObj.cells[i].pos.y) + ", z = " + to_string(terrObj.cells[i].pos.z) + "},\n"; + + terrObjStr += "},\nimpassible = {\n"; + + for(int i = 0; i < terrObj.numCells; i++){ + terrObjStr += (terrObj.cells[i].impassible ? "true" : "false"); + terrObjStr += ",\n"; + } + + terrObjStr += "},\nweights = {\n"; + + for(int i = 0; i < terrObj.numCells; i++) + for(int j = 0; j < terrObj.numCells; j++) + terrObjStr += to_string(terrObj.weights[i][j]) + ", "; + + terrObjStr += "}\n}\n},\n"; + + return terrObjStr; + } + void MapEditorAppState::MapEditor::parseMapScript(){ + int numTerrObjs = map->getNumTerrainObjects(); + string mapScript = "map = {\nnumWaterBodies = " + to_string(numTerrObjs) + ",\n"; + mapScript += "impassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n"; + mapScript += "numPlayers = " + to_string(map->getNumPlayers()) + ",\n"; + + int numSpawnPoints = map->getNumSpawnPoints(); + mapScript += "numSpawnPoints = " + to_string(numSpawnPoints) + ",\n"; + mapScript += "spawnPoints = {\n"; + + for(int i = 0; i < numSpawnPoints; i++){ + Vector3 sp = map->getSpawnPoint(i); + mapScript += "{x = " + to_string(sp.x) + ", y = " + to_string(sp.y) + ", z = " + to_string(sp.z) + "},\n"; + } + + mapScript += "},\nplayers = {\n"; + + for(int i = 0; i < map->getNumPlayers(); i++){ + mapScript += "spawnPoint = 0,\n"; + + int numUnits = map->getPlayer(i)->getNumberOfUnits(); + mapScript += "numUnits = " + to_string(numUnits) + ",\n"; + + if(numUnits > 0){ + mapScript += "units = {\n"; + + for(int j = 0; j < numUnits; j++){ + Unit *unit = map->getPlayer(i)->getUnit(j); + + string idStr = "id = " + to_string(unit->getId()); + + Vector3 unitPos = unit->getPos(); + string posStr = "pos = {x = " + to_string(unitPos.x) + ", y = " + to_string(unitPos.y) + ", z = " + to_string(unitPos.z) + "}"; + + Quaternion unitRot = unit->getRot(); + string rotStr = "rot = {w = " + to_string(unitRot.w) + ", x = " + to_string(unitRot.x) + ", y = " + to_string(unitRot.y) + ", z = " + to_string(unitRot.z) + "}"; + + mapScript += "{" + idStr + ", " + posStr + ", " + rotStr + "},\n"; + } + + mapScript += "}\n,"; + mapScript += "}"; + } + } + + mapScript += "},\n"; + mapScript += "skybox = \"" + wstringToString(skyListbox->getContents()[skyListbox->getSelectedOption()]) + "\",\n"; + mapScript += "terrain = " + parseTerrainObject(map->getTerrainObject(0)); + mapScript += "waterbodies = {"; + + for(int i = 1; i < numTerrObjs; i++) + mapScript += parseTerrainObject(map->getTerrainObject(i)); + + mapScript += "}\n}"; + + string file = GameManager::getSingleton()->getPath() + "Models/Maps/" + map->getMapName() + "/" + map->getMapName() + ".lua"; + + std::ofstream outFile(file); + outFile << mapScript; + outFile.close(); } void MapEditorAppState::MapEditor::exportMap(){ - parseLandmass(); prepareTerrainObjects(); + parseLandmass(); parseMapScript(); } diff --git a/mapEditorAppState.h b/mapEditorAppState.h index 6d28805..d751764 100644 --- a/mapEditorAppState.h +++ b/mapEditorAppState.h @@ -62,6 +62,7 @@ namespace battleship{ void prepareTextures(std::string, bool, std::vector&); void toggleSelection(TerrainObject*, bool); void parseLandmass(); + std::string parseTerrainObject(TerrainObject&); void parseMapScript(); void deleteWeights(); void prepareTerrainObject(vb01::u32**, Cell*, int[3], float, bool); @@ -71,6 +72,7 @@ namespace battleship{ Map *map; TerrainObject *selectedTerrainObject = nullptr; MovementAxis movementAxis = X_AXIS; + vb01Gui::Listbox *skyListbox = nullptr; bool pushing = false, movingTerrainObject = false, weightsGenerated = false, newMap; const float MIN_RADIUS = 1, MAX_RADIUS = 100, INCREASE_RATE = 1; const int NUM_SUBDIVS = 100; diff --git a/unit.h b/unit.h index f8277a6..1781af6 100755 --- a/unit.h +++ b/unit.h @@ -67,6 +67,7 @@ namespace battleship{ inline vb01::Vector2 getScreenPos(){return screenPos;} inline vb01::Vector3 getPos() {return pos;} inline vb01::Vector3* getPosPtr() {return &pos;} + inline vb01::Quaternion getRot(){return rot;} inline float getLineOfSight() {return lineOfSight;} inline float getWidth() {return width;} inline float getHeight() {return height;}