From 15856bd6fec42da41a62811e0809ac11418f04b4 Mon Sep 17 00:00:00 2001 From: devZoGok Date: Sun, 27 Jul 2025 16:20:56 +0300 Subject: [PATCH] updated map file output script bugfix for showing wrong units in map editor listboxes --- concreteGuiManager.cpp | 7 ++- gameObjectListbox.cpp | 7 ++- gameObjectListbox.h | 3 +- map.cpp | 62 +++++++++++-------- mapEditorAppState.cpp | 137 ++++++++++++++++++++--------------------- mapEditorAppState.h | 1 + 6 files changed, 116 insertions(+), 101 deletions(-) diff --git a/concreteGuiManager.cpp b/concreteGuiManager.cpp index fe32be2..865282c 100644 --- a/concreteGuiManager.cpp +++ b/concreteGuiManager.cpp @@ -327,6 +327,7 @@ namespace battleship{ bool resources = (listboxType == RESOURCE_DEPOSITS); sol::table gameObjTable = SOL_LUA_STATE[resources ? "resources" : "units"]; int numGameObjs = gameObjTable.size(); + std::vector gameObjIds; for(int i = 0; i < numGameObjs; i++){ bool canAdd = true; @@ -337,15 +338,17 @@ namespace battleship{ canAdd = (v == vehicles); } - if(canAdd) + if(canAdd){ lines.push_back(gameObjTable[i + 1]["name"]); + gameObjIds.push_back(i); + } } numLines = lines.size(); closable = true; maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines); - listbox = new GameObjectListbox(!resources, pos, size, lines, maxDisplay, fontPath); + listbox = new GameObjectListbox(!resources, pos, size, lines, gameObjIds, maxDisplay, fontPath); break; } case SKYBOX_TEXTURES: diff --git a/gameObjectListbox.cpp b/gameObjectListbox.cpp index cae5092..2725daa 100644 --- a/gameObjectListbox.cpp +++ b/gameObjectListbox.cpp @@ -10,12 +10,15 @@ namespace battleship{ using namespace vb01Gui; using namespace gameBase; - GameObjectListbox::GameObjectListbox(bool ul, Vector3 pos, Vector2 size, vector lines, int maxDisplay, string fontPath) : Listbox(pos, size, lines, maxDisplay, fontPath), unitListbox(ul) {} + GameObjectListbox::GameObjectListbox(bool ul, Vector3 pos, Vector2 size, vector lines, vector objIds, int maxDisplay, string fontPath) : + Listbox(pos, size, lines, maxDisplay, fontPath), + unitListbox(ul), + gameObjIds(objIds) {} void GameObjectListbox::onClose(){ GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); GameObject::Type type = (unitListbox ? GameObject::Type::UNIT : GameObject::Type::RESOURCE_DEPOSIT); - ufCtr->addGameObjectFrame(GameObjectFrame(selectedOption, type)); + ufCtr->addGameObjectFrame(GameObjectFrame(gameObjIds[selectedOption], type)); ufCtr->setPlacingOnSurface(true); } } diff --git a/gameObjectListbox.h b/gameObjectListbox.h index de919f9..1e15ec1 100644 --- a/gameObjectListbox.h +++ b/gameObjectListbox.h @@ -6,10 +6,11 @@ namespace battleship{ class GameObjectListbox : public vb01Gui::Listbox{ public: - GameObjectListbox(bool, vb01::Vector3, vb01::Vector2, std::vector, int, std::string); + GameObjectListbox(bool, vb01::Vector3, vb01::Vector2, std::vector, std::vector, int, std::string); void onClose(); private: bool unitListbox; + std::vector gameObjIds; }; } diff --git a/map.cpp b/map.cpp index 15724e6..21a524c 100755 --- a/map.cpp +++ b/map.cpp @@ -421,41 +421,50 @@ namespace battleship{ } void Map::loadPlayerGameObjects(Player *player, sol::table playerTbl){ - string resDepInd = "resourceDeposits"; sol::state_view SOL_LUA_VIEW = generateView(); - sol::table resDepTbl = playerTbl["resourceDeposits"]; - int numNpcObjs = resDepTbl.size(); - for(int j = 0; j < numNpcObjs; j++){ - sol::table npcObjTable = resDepTbl[j + 1]; - int id = npcObjTable["id"]; + string resDepInd = "resourceDeposits"; + sol::optional resDepTblOpt = playerTbl[resDepInd]; - sol::table posTable = npcObjTable["pos"]; - Vector3 pos = Vector3(posTable["x"], posTable["y"], posTable["z"]); + if(resDepTblOpt != sol::nullopt){ + sol::table resDepTbl = playerTbl[resDepInd]; + int numNpcObjs = resDepTbl.size(); - sol::table rotTable = npcObjTable["rot"]; - Quaternion rot = Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"]); - int initAmmount = resDepTbl[j + 1]["initAmmount"]; + for(int j = 0; j < numNpcObjs; j++){ + sol::table npcObjTable = resDepTbl[j + 1]; + int id = npcObjTable["id"]; - player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, id, pos, rot, initAmmount)); + sol::table posTable = npcObjTable["pos"]; + Vector3 pos = Vector3(posTable["x"], posTable["y"], posTable["z"]); + + sol::table rotTable = npcObjTable["rot"]; + Quaternion rot = Quaternion(rotTable["w"], rotTable["x"], rotTable["y"], rotTable["z"]); + int initAmmount = resDepTbl[j + 1]["initAmmount"]; + + player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, id, pos, rot, initAmmount)); + } } string unitInd = "units"; - sol::table unitsTbl = playerTbl["units"]; - int numUnits = unitsTbl.size(); - - for(int j = 0; j < numUnits; j++){ - sol::table unitTable = unitsTbl[j + 1]; + sol::optional unitsTblOpt = playerTbl[unitInd]; - string posInd = "pos"; - Vector3 pos = Vector3(unitTable[posInd]["x"], unitTable[posInd]["y"], unitTable[posInd]["z"]); + if(unitsTblOpt != sol::nullopt){ + sol::table unitsTbl = playerTbl[unitInd]; + int numUnits = unitsTbl.size(); + + for(int j = 0; j < numUnits; j++){ + sol::table unitTable = unitsTbl[j + 1]; - string rotInd = "rot"; - Quaternion rot = Quaternion(unitTable[rotInd]["w"], unitTable[rotInd]["x"], unitTable[rotInd]["x"], unitTable[rotInd]["x"]); + string posInd = "pos"; + Vector3 pos = Vector3(unitTable[posInd]["x"], unitTable[posInd]["y"], unitTable[posInd]["z"]); - int id = unitTable["id"]; - int buildStatus = unitTable["buildStatus"].get_or(0); - player->addUnit(GameObjectFactory::createUnit(player, id, pos, rot, buildStatus)); + string rotInd = "rot"; + Quaternion rot = Quaternion(unitTable[rotInd]["w"], unitTable[rotInd]["x"], unitTable[rotInd]["x"], unitTable[rotInd]["x"]); + + int id = unitTable["id"]; + int buildStatus = unitTable["buildStatus"].get_or(0); + player->addUnit(GameObjectFactory::createUnit(player, id, pos, rot, buildStatus)); + } } } @@ -516,8 +525,8 @@ namespace battleship{ //TODO implement toggleable cell rendering void Map::loadCells(){ sol::state_view SOL_LUA_VIEW = generateView(); - int numCells = SOL_LUA_VIEW[mapTable]["numCells"]; sol::table cellsTable = SOL_LUA_VIEW[mapTable]["cells"]; + int numCells = cellsTable.size(); for(int i = 0; i < numCells; i++){ sol::table cellTable = cellsTable[i + 1], posTable = cellTable["pos"]; @@ -575,7 +584,8 @@ namespace battleship{ sol::table sizeTable = SOL_LUA_STATE[mapTable]["size"]; mapSize = Vector3(sizeTable["x"], sizeTable["y"], sizeTable["z"]); - int numWaterbodies = SOL_LUA_STATE[mapTable]["numWaterBodies"]; + sol::table wbTbl = SOL_LUA_STATE[mapTable]["waterbodies"]; + int numWaterbodies = wbTbl.size(); for(int i = 0; i < numWaterbodies; i++) loadTerrainObject(i); diff --git a/mapEditorAppState.cpp b/mapEditorAppState.cpp index c9c1658..c21389f 100644 --- a/mapEditorAppState.cpp +++ b/mapEditorAppState.cpp @@ -285,7 +285,7 @@ namespace battleship{ XMLElement *meshEl = doc->NewElement("mesh"); MeshData meshData = map->getNodeParent()->getChild(0)->getMesh(0)->getMeshBase(); meshEl->SetAttribute("name", "mesh"); - meshEl->SetAttribute("num_vertex_pos", meshData.numPos); + meshEl->SetAttribute("num_vertex_pos", 3 * meshData.numTris); meshEl->SetAttribute("num_faces", meshData.numTris); meshEl->SetAttribute("num_vertex_groups", 0); meshEl->SetAttribute("num_shape_keys", 0); @@ -432,14 +432,56 @@ namespace battleship{ stbi_write_jpg(string(mapFolder + "minimap.jpg").c_str(), width, height, numChannels, imgData, 100); } + string MapEditorAppState::MapEditor::generatePlayerTableStr(Player* player){ + string mapScript = ""; + vector resourceDeposits = player->getResourceDeposits(); + + if(!resourceDeposits.empty()){ + mapScript += "\t\t\tresourceDeposits = {\n"; + + for(ResourceDeposit *rd : resourceDeposits){ + Vector3 pos = rd->getPos(); + string posStr = "x = " + to_string(pos.x) + ", y = " + to_string(pos.y) + ", z = " + to_string(pos.z); + + Quaternion rot = rd->getRot(); + string rotStr = "w = " + to_string(rot.w) + ", x = " + to_string(rot.x) + ", y = " + to_string(rot.y) + ", z = " + to_string(rot.z); + + mapScript += "\t\t\t\t{id = " + to_string(rd->getId()) + ", pos = {" + posStr + "}, rot = {" + rotStr + "}},\n"; + } + + mapScript += "\t\t\t},\n"; + } + + vector units = player->getUnits(); + + if(!units.empty()){ + mapScript += "\t\t\tunits = {\n"; + + for(Unit *unit : units){ + 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 += "\t\t\t\t{" + idStr + ", " + posStr + ", " + rotStr + "},\n"; + } + + mapScript += "\t\t\t}\n"; + } + + return mapScript; + } + void MapEditorAppState::MapEditor::generateMapScript(vector &cells){ int numWaterBodies = map->getNodeParent()->getNumChildren() - 1; - vector players = Game::getSingleton()->getPlayers(); - string mapScript = "map = {\nlights = {\n"; + string mapScript = "map = {\n\tlights = {"; for(Node *light : map->getLights()){ - mapScript += "{type = " + to_string((int)light->getLight(0)->getLightType()); + mapScript += "\n\t\t{type = " + to_string((int)light->getLight(0)->getLightType()); if(light->getLight(0)->getLightType() == Light::Type::DIRECTIONAL){ Vector3 dir = light->getGlobalAxis(2); @@ -450,78 +492,33 @@ namespace battleship{ mapScript += ", color = {x = " + to_string(color.x) + ", y = " + to_string(color.y) + ", z = " + to_string(color.z) + "}},"; } + mapScript += "\n\t},\n"; Vector3 mapSize = map->getMapSize(); - mapScript += "}\nnumWaterBodies = " + to_string(numWaterBodies) + ",\n"; - mapScript += "size = {x = " + to_string(mapSize.x) + ", y = " + to_string(mapSize.y) + ", z = " + to_string(mapSize.z) + "},\n"; - mapScript += "impassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n"; - mapScript += "numPlayers = " + to_string(players.size()) + ",\n"; + mapScript += "\tsize = {x = " + to_string(mapSize.x) + ", y = " + to_string(mapSize.y) + ", z = " + to_string(mapSize.z) + "},\n"; + mapScript += "\timpassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n"; int numSpawnPoints = map->getNumSpawnPoints(); - mapScript += "numSpawnPoints = " + to_string(numSpawnPoints) + ",\n"; - mapScript += "spawnPoints = {\n"; + mapScript += "\tspawnPoints = {\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 += "\t\t{x = " + to_string(sp.x) + ", y = " + to_string(sp.y) + ", z = " + to_string(sp.z) + "},\n"; } - mapScript += "},\nplayers = {\n"; + mapScript += "\t},\n"; + mapScript += "\tchoosablePlayers = {"; + Game *game = Game::getSingleton(); - for(int i = 0; i < players.size(); i++){ - mapScript += "{\n"; + for(Player *player : game->getPlayers()) + mapScript += "\n\t\t{\n" + generatePlayerTableStr(player) + "\n\t\t},\n"; - if(i > 0) - mapScript += "spawnPoint = 0,\n"; - else{ - vector resourceDeposits = players[0]->getResourceDeposits(); - mapScript += "numResourceDeposits = " + to_string(resourceDeposits.size()) + ",\n"; - mapScript += "resourceDeposits = {\n"; - - for(ResourceDeposit *rd : resourceDeposits){ - Vector3 pos = rd->getPos(); - string posStr = "x = " + to_string(pos.x) + ", y = " + to_string(pos.y) + ", z = " + to_string(pos.z); - - Quaternion rot = rd->getRot(); - string rotStr = "w = " + to_string(rot.w) + ", x = " + to_string(rot.x) + ", y = " + to_string(rot.y) + ", z = " + to_string(rot.z); - - mapScript += "{id = " + to_string(rd->getId()) + ", pos = {" + posStr + "}, rot = {" + rotStr + "}},\n"; - } - - mapScript += "},\n"; - } - - int numUnits = players[i]->getNumUnits(); - mapScript += "numUnits = " + to_string(numUnits) + ",\n"; - - if(numUnits > 0){ - mapScript += "units = {\n"; - - for(int j = 0; j < numUnits; j++){ - Unit *unit = Game::getSingleton()->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 += "}\n"; - } - - mapScript += "},\nnumCells = " + to_string(cells.size()) + ",\n"; - mapScript += "cells = {\n"; + mapScript += "\t},\n"; + mapScript += "\tcivilianPlayer = {\n" + generatePlayerTableStr(game->getCivilianPlayer()) + "\n\t},\n"; + mapScript += "\tcells = {\n"; for(Map::Cell cell : cells){ Vector3 p = cell.pos; - mapScript += "{type = " + to_string((int)cell.type) + ", pos = {x = " + to_string(p.x) + ", y = " + to_string(p.y) + ", z = " + to_string(p.z) + "}, numEdges = " + to_string(cell.edges.size()) + ", edges = {"; + mapScript += "\t\t{type = " + to_string((int)cell.type) + ", pos = {x = " + to_string(p.x) + ", y = " + to_string(p.y) + ", z = " + to_string(p.z) + "}, numEdges = " + to_string(cell.edges.size()) + ", edges = {"; for(Map::Edge edge : cell.edges) mapScript += "{srcCellId = " + to_string(edge.srcCellId) + ", destCellId = " + to_string(edge.destCellId) + ", weight = " + to_string(edge.weight) + "}, "; @@ -538,10 +535,10 @@ namespace battleship{ mapScript += "}"; } - mapScript += "},\n"; + mapScript += "\t\t},\n"; } - mapScript += "},\n"; + mapScript += "\t},\n"; Root *root = Root::getSingleton(); string skyboxPath = ""; @@ -556,20 +553,20 @@ namespace battleship{ skyboxPath = skyboxPath.substr(slashId + 1); } - mapScript += "skybox = \"" + skyboxPath + "\",\n"; - mapScript += "terrain = {model = \"" + map->getMapName() + ".xml\", albedo = \"" + map->getMapName() + ".jpg\"},\n"; - mapScript += "waterbodies = {\n"; + mapScript += "\tskybox = \"" + skyboxPath + "\",\n"; + mapScript += "\tterrain = {model = \"" + map->getMapName() + ".xml\", albedo = \"" + map->getMapName() + ".jpg\"},\n"; + mapScript += "\twaterbodies = {\n"; for(int i = 0; i < numWaterBodies; i++){ Node *waterNode = map->getNodeParent()->getChild(i + 1); Vector3 pos = waterNode->getPosition(); Vector3 size = ((Quad*)waterNode->getMesh(0))->getSize(); mapScript += - "{pos = {x = " + to_string(pos.x) + ", y = " + to_string(pos.y) + ", z = " + to_string(pos.z) + "},\ + "\t\t{pos = {x = " + to_string(pos.x) + ", y = " + to_string(pos.y) + ", z = " + to_string(pos.z) + "},\ size = {x = " + to_string(size.x) + ", y = " + to_string(size.y) + "}, albedo = \"water.png\"},"; } - mapScript += "}\n}"; + mapScript += "\t}\n}"; string file = GameManager::getSingleton()->getPath() + "Models/Maps/" + map->getMapName() + "/" + map->getMapName() + ".lua"; diff --git a/mapEditorAppState.h b/mapEditorAppState.h index 72f9bf5..cb3d041 100644 --- a/mapEditorAppState.h +++ b/mapEditorAppState.h @@ -61,6 +61,7 @@ namespace battleship{ std::vector generateMapCells(); void generateLandmassXml(); void generateMinimap(std::string, std::vector&); + std::string generatePlayerTableStr(Player*); void generateMapScript(std::vector&); void prepareTerrainObject(vb01::u32**, Map::Cell*, int[3], float, bool);