updated map file output script

bugfix for showing wrong units in map editor listboxes
This commit is contained in:
devZoGok
2025-07-27 16:20:56 +03:00
parent 8f8d0d0a88
commit 15856bd6fe
6 changed files with 116 additions and 101 deletions
+5 -2
View File
@@ -327,6 +327,7 @@ namespace battleship{
bool resources = (listboxType == RESOURCE_DEPOSITS); bool resources = (listboxType == RESOURCE_DEPOSITS);
sol::table gameObjTable = SOL_LUA_STATE[resources ? "resources" : "units"]; sol::table gameObjTable = SOL_LUA_STATE[resources ? "resources" : "units"];
int numGameObjs = gameObjTable.size(); int numGameObjs = gameObjTable.size();
std::vector<int> gameObjIds;
for(int i = 0; i < numGameObjs; i++){ for(int i = 0; i < numGameObjs; i++){
bool canAdd = true; bool canAdd = true;
@@ -337,15 +338,17 @@ namespace battleship{
canAdd = (v == vehicles); canAdd = (v == vehicles);
} }
if(canAdd) if(canAdd){
lines.push_back(gameObjTable[i + 1]["name"]); lines.push_back(gameObjTable[i + 1]["name"]);
gameObjIds.push_back(i);
}
} }
numLines = lines.size(); numLines = lines.size();
closable = true; closable = true;
maxDisplay = (numLines > numMaxDisplay ? numMaxDisplay : numLines); 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; break;
} }
case SKYBOX_TEXTURES: case SKYBOX_TEXTURES:
+5 -2
View File
@@ -10,12 +10,15 @@ namespace battleship{
using namespace vb01Gui; using namespace vb01Gui;
using namespace gameBase; using namespace gameBase;
GameObjectListbox::GameObjectListbox(bool ul, Vector3 pos, Vector2 size, vector<string> lines, int maxDisplay, string fontPath) : Listbox(pos, size, lines, maxDisplay, fontPath), unitListbox(ul) {} GameObjectListbox::GameObjectListbox(bool ul, Vector3 pos, Vector2 size, vector<string> lines, vector<int> objIds, int maxDisplay, string fontPath) :
Listbox(pos, size, lines, maxDisplay, fontPath),
unitListbox(ul),
gameObjIds(objIds) {}
void GameObjectListbox::onClose(){ void GameObjectListbox::onClose(){
GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton(); GameObjectFrameController *ufCtr = GameObjectFrameController::getSingleton();
GameObject::Type type = (unitListbox ? GameObject::Type::UNIT : GameObject::Type::RESOURCE_DEPOSIT); 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); ufCtr->setPlacingOnSurface(true);
} }
} }
+2 -1
View File
@@ -6,10 +6,11 @@
namespace battleship{ namespace battleship{
class GameObjectListbox : public vb01Gui::Listbox{ class GameObjectListbox : public vb01Gui::Listbox{
public: public:
GameObjectListbox(bool, vb01::Vector3, vb01::Vector2, std::vector<std::string>, int, std::string); GameObjectListbox(bool, vb01::Vector3, vb01::Vector2, std::vector<std::string>, std::vector<int>, int, std::string);
void onClose(); void onClose();
private: private:
bool unitListbox; bool unitListbox;
std::vector<int> gameObjIds;
}; };
} }
+15 -5
View File
@@ -421,9 +421,13 @@ namespace battleship{
} }
void Map::loadPlayerGameObjects(Player *player, sol::table playerTbl){ void Map::loadPlayerGameObjects(Player *player, sol::table playerTbl){
string resDepInd = "resourceDeposits";
sol::state_view SOL_LUA_VIEW = generateView(); sol::state_view SOL_LUA_VIEW = generateView();
sol::table resDepTbl = playerTbl["resourceDeposits"];
string resDepInd = "resourceDeposits";
sol::optional<sol::table> resDepTblOpt = playerTbl[resDepInd];
if(resDepTblOpt != sol::nullopt){
sol::table resDepTbl = playerTbl[resDepInd];
int numNpcObjs = resDepTbl.size(); int numNpcObjs = resDepTbl.size();
for(int j = 0; j < numNpcObjs; j++){ for(int j = 0; j < numNpcObjs; j++){
@@ -439,9 +443,13 @@ namespace battleship{
player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, id, pos, rot, initAmmount)); player->addResourceDeposit(GameObjectFactory::createResourceDeposit(player, id, pos, rot, initAmmount));
} }
}
string unitInd = "units"; string unitInd = "units";
sol::table unitsTbl = playerTbl["units"]; sol::optional<sol::table> unitsTblOpt = playerTbl[unitInd];
if(unitsTblOpt != sol::nullopt){
sol::table unitsTbl = playerTbl[unitInd];
int numUnits = unitsTbl.size(); int numUnits = unitsTbl.size();
for(int j = 0; j < numUnits; j++){ for(int j = 0; j < numUnits; j++){
@@ -458,6 +466,7 @@ namespace battleship{
player->addUnit(GameObjectFactory::createUnit(player, id, pos, rot, buildStatus)); player->addUnit(GameObjectFactory::createUnit(player, id, pos, rot, buildStatus));
} }
} }
}
//TODO move minimap loading elsewhere //TODO move minimap loading elsewhere
void Map::loadPlayersGameObjects(){ void Map::loadPlayersGameObjects(){
@@ -516,8 +525,8 @@ namespace battleship{
//TODO implement toggleable cell rendering //TODO implement toggleable cell rendering
void Map::loadCells(){ void Map::loadCells(){
sol::state_view SOL_LUA_VIEW = generateView(); sol::state_view SOL_LUA_VIEW = generateView();
int numCells = SOL_LUA_VIEW[mapTable]["numCells"];
sol::table cellsTable = SOL_LUA_VIEW[mapTable]["cells"]; sol::table cellsTable = SOL_LUA_VIEW[mapTable]["cells"];
int numCells = cellsTable.size();
for(int i = 0; i < numCells; i++){ for(int i = 0; i < numCells; i++){
sol::table cellTable = cellsTable[i + 1], posTable = cellTable["pos"]; sol::table cellTable = cellsTable[i + 1], posTable = cellTable["pos"];
@@ -575,7 +584,8 @@ namespace battleship{
sol::table sizeTable = SOL_LUA_STATE[mapTable]["size"]; sol::table sizeTable = SOL_LUA_STATE[mapTable]["size"];
mapSize = Vector3(sizeTable["x"], sizeTable["y"], sizeTable["z"]); 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++) for(int i = 0; i < numWaterbodies; i++)
loadTerrainObject(i); loadTerrainObject(i);
+67 -70
View File
@@ -285,7 +285,7 @@ namespace battleship{
XMLElement *meshEl = doc->NewElement("mesh"); XMLElement *meshEl = doc->NewElement("mesh");
MeshData meshData = map->getNodeParent()->getChild(0)->getMesh(0)->getMeshBase(); MeshData meshData = map->getNodeParent()->getChild(0)->getMesh(0)->getMeshBase();
meshEl->SetAttribute("name", "mesh"); 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_faces", meshData.numTris);
meshEl->SetAttribute("num_vertex_groups", 0); meshEl->SetAttribute("num_vertex_groups", 0);
meshEl->SetAttribute("num_shape_keys", 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); stbi_write_jpg(string(mapFolder + "minimap.jpg").c_str(), width, height, numChannels, imgData, 100);
} }
string MapEditorAppState::MapEditor::generatePlayerTableStr(Player* player){
string mapScript = "";
vector<ResourceDeposit*> 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<Unit*> 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<Map::Cell> &cells){ void MapEditorAppState::MapEditor::generateMapScript(vector<Map::Cell> &cells){
int numWaterBodies = map->getNodeParent()->getNumChildren() - 1; int numWaterBodies = map->getNodeParent()->getNumChildren() - 1;
vector<Player*> players = Game::getSingleton()->getPlayers();
string mapScript = "map = {\nlights = {\n"; string mapScript = "map = {\n\tlights = {";
for(Node *light : map->getLights()){ 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){ if(light->getLight(0)->getLightType() == Light::Type::DIRECTIONAL){
Vector3 dir = light->getGlobalAxis(2); 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 += ", color = {x = " + to_string(color.x) + ", y = " + to_string(color.y) + ", z = " + to_string(color.z) + "}},";
} }
mapScript += "\n\t},\n";
Vector3 mapSize = map->getMapSize(); Vector3 mapSize = map->getMapSize();
mapScript += "}\nnumWaterBodies = " + to_string(numWaterBodies) + ",\n"; mapScript += "\tsize = {x = " + to_string(mapSize.x) + ", y = " + to_string(mapSize.y) + ", z = " + to_string(mapSize.z) + "},\n";
mapScript += "size = {x = " + to_string(mapSize.x) + ", y = " + to_string(mapSize.y) + ", z = " + to_string(mapSize.z) + "},\n"; mapScript += "\timpassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n";
mapScript += "impassibleNodeValue = " + to_string(IMPASS_NODE_VAL) + ",\n";
mapScript += "numPlayers = " + to_string(players.size()) + ",\n";
int numSpawnPoints = map->getNumSpawnPoints(); int numSpawnPoints = map->getNumSpawnPoints();
mapScript += "numSpawnPoints = " + to_string(numSpawnPoints) + ",\n"; mapScript += "\tspawnPoints = {\n";
mapScript += "spawnPoints = {\n";
for(int i = 0; i < numSpawnPoints; i++){ for(int i = 0; i < numSpawnPoints; i++){
Vector3 sp = map->getSpawnPoint(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++){ for(Player *player : game->getPlayers())
mapScript += "{\n"; mapScript += "\n\t\t{\n" + generatePlayerTableStr(player) + "\n\t\t},\n";
if(i > 0) mapScript += "\t},\n";
mapScript += "spawnPoint = 0,\n"; mapScript += "\tcivilianPlayer = {\n" + generatePlayerTableStr(game->getCivilianPlayer()) + "\n\t},\n";
else{ mapScript += "\tcells = {\n";
vector<ResourceDeposit*> 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";
for(Map::Cell cell : cells){ for(Map::Cell cell : cells){
Vector3 p = cell.pos; 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) for(Map::Edge edge : cell.edges)
mapScript += "{srcCellId = " + to_string(edge.srcCellId) + ", destCellId = " + to_string(edge.destCellId) + ", weight = " + to_string(edge.weight) + "}, "; mapScript += "{srcCellId = " + to_string(edge.srcCellId) + ", destCellId = " + to_string(edge.destCellId) + ", weight = " + to_string(edge.weight) + "}, ";
@@ -538,10 +535,10 @@ namespace battleship{
mapScript += "}"; mapScript += "}";
} }
mapScript += "},\n"; mapScript += "\t\t},\n";
} }
mapScript += "},\n"; mapScript += "\t},\n";
Root *root = Root::getSingleton(); Root *root = Root::getSingleton();
string skyboxPath = ""; string skyboxPath = "";
@@ -556,20 +553,20 @@ namespace battleship{
skyboxPath = skyboxPath.substr(slashId + 1); skyboxPath = skyboxPath.substr(slashId + 1);
} }
mapScript += "skybox = \"" + skyboxPath + "\",\n"; mapScript += "\tskybox = \"" + skyboxPath + "\",\n";
mapScript += "terrain = {model = \"" + map->getMapName() + ".xml\", albedo = \"" + map->getMapName() + ".jpg\"},\n"; mapScript += "\tterrain = {model = \"" + map->getMapName() + ".xml\", albedo = \"" + map->getMapName() + ".jpg\"},\n";
mapScript += "waterbodies = {\n"; mapScript += "\twaterbodies = {\n";
for(int i = 0; i < numWaterBodies; i++){ for(int i = 0; i < numWaterBodies; i++){
Node *waterNode = map->getNodeParent()->getChild(i + 1); Node *waterNode = map->getNodeParent()->getChild(i + 1);
Vector3 pos = waterNode->getPosition(); Vector3 pos = waterNode->getPosition();
Vector3 size = ((Quad*)waterNode->getMesh(0))->getSize(); Vector3 size = ((Quad*)waterNode->getMesh(0))->getSize();
mapScript += 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\"},"; 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"; string file = GameManager::getSingleton()->getPath() + "Models/Maps/" + map->getMapName() + "/" + map->getMapName() + ".lua";
+1
View File
@@ -61,6 +61,7 @@ namespace battleship{
std::vector<Map::Cell> generateMapCells(); std::vector<Map::Cell> generateMapCells();
void generateLandmassXml(); void generateLandmassXml();
void generateMinimap(std::string, std::vector<Map::Cell>&); void generateMinimap(std::string, std::vector<Map::Cell>&);
std::string generatePlayerTableStr(Player*);
void generateMapScript(std::vector<Map::Cell>&); void generateMapScript(std::vector<Map::Cell>&);
void prepareTerrainObject(vb01::u32**, Map::Cell*, int[3], float, bool); void prepareTerrainObject(vb01::u32**, Map::Cell*, int[3], float, bool);