// Парсинг XML в модели главной таблицы. XMLError MainWindow::ParseMainTableItemModels() { XMLCheckResult(ParseSmallShipsItemModel()); XMLCheckResult(ParseLargeShipsItemModel()); XMLCheckResult(ParseStationsItemModel()); XMLCheckResult(ParseFloatingsItemModel()); XMLCheckResult(ParseAsteroidsItemModel()); ui->tableViewSmallShips->setModel(p_ItemModelSmallShips); ui->tableViewLargeShips->setModel(p_ItemModelLargeShips); ui->tableViewStations->setModel(p_ItemModelStations); ui->tableViewFloatings->setModel(p_ItemModelFloatings); ui->tableViewAsteroids->setModel(p_ItemModelAsteroids); return XML_NO_ERROR; }
bool LevelReader::readItemIDs(XMLElement* _tile) { m_levelItemMap.clear(); m_levelItemMap.insert({ -1, "" }); XMLElement* tile = _tile; while (tile != nullptr) { int tileID; XMLError result = tile->QueryIntAttribute("id", &tileID); XMLCheckResult(result); XMLElement* properties = tile->FirstChildElement("properties"); if (properties == nullptr) { g_logger->logError("LevelReader", "Could not read item tile properties, no tileset->tile->properties tag found."); return false; } XMLElement* _property = properties->FirstChildElement("property"); if (_property == nullptr) { g_logger->logError("LevelReader", "Could not read item tile properties, no tileset->tile->properties->property tag found."); return false; } const char* textAttr = nullptr; textAttr = _property->Attribute("name"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no tileset->tile->properties->property name attribute found."); return false; } std::string name = textAttr; if (name.compare("id") != 0) { g_logger->logError("LevelReader", "XML file could not be read, wrong tile property (not \"id\")."); return false; } textAttr = nullptr; textAttr = _property->Attribute("value"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no tileset->tile->properties->property value attribute found."); return false; } m_levelItemMap.insert({ tileID, std::string(textAttr) }); tile = tile->NextSiblingElement("tile"); } return true; }
int main() { std::vector<t_bomb *> test; tinyxml2::XMLDocument xmlDoc; tinyxml2::XMLError eResult = xmlDoc.LoadFile("test.xml"); XMLCheckResult(eResult); tinyxml2::XMLNode *pRoot = xmlDoc.FirstChild(); if (pRoot == 0) return tinyxml2::XML_ERROR_FILE_READ_ERROR; tinyxml2::XMLElement *pElement = xmlDoc.FirstChildElement("save")->FirstChildElement("players"); if (pElement == 0) {std::cout << "error" << std::endl; return tinyxml2::XML_ERROR_PARSING_ELEMENT;} player(pElement->FirstChildElement("player")); test= bombs(); return (0); }
bool LevelReader::readDimming(XMLElement* _property, LevelData& data) const { // we've found the property "dimming" float dimming = 0.f; XMLError result = _property->QueryFloatAttribute("value", &dimming); XMLCheckResult(result); if (dimming < 0.0f || dimming > 1.0f) { g_logger->logError("LevelReader", "XML file could not be read, dimming value not allowed (only [0,1])."); return false; } data.dimming = dimming; return true; }
bool LevelReader::readFirstGridIDs(XMLElement* map, LevelData& data) { XMLElement* tileset = map->FirstChildElement("tileset"); m_firstGidDynamicTiles = 0; m_firstGidEnemies = 0; m_firstGidItems = 0; const char* textAttr; while (tileset != nullptr) { textAttr = nullptr; textAttr = tileset->Attribute("name"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no tileset->name attribute found."); return false; } std::string name = textAttr; int gid; XMLError result = tileset->QueryIntAttribute("firstgid", &gid); XMLCheckResult(result); if (name.find("dynamic_tiles") != std::string::npos) { m_firstGidDynamicTiles = gid; } else if (name.find("enemies") != std::string::npos) { m_firstGidEnemies = gid; } else if (name.find("levelitems") != std::string::npos) { m_firstGidItems = gid; if (!readItemIDs(tileset->FirstChildElement("tile"))) return false; } tileset = tileset->NextSiblingElement("tileset"); } if (m_firstGidItems <= 0 || m_firstGidDynamicTiles <= 0 || m_firstGidEnemies <= 0) { g_logger->logError("LevelReader", "Could not read firstgids, at least one of the required tilesets is missing."); return false; } return true; }
// Загрузка данных в XML документ. XMLError MainWindow::LoadXMLFromFile(QString& a_StrPath, tinyxml2::XMLDocument& a_xmlDoc) { DialogFileError we; XMLError eErrOut; // eErrOut = XMLCheckResult(a_xmlDoc.LoadFile(a_StrPath.toStdString().c_str())); if(eErrOut) { LOG(LOG_CAT_W, "File error: " << a_StrPath.toStdString()); we.exec(); } else { LOG(LOG_CAT_I, "File loaded: " << a_StrPath.toStdString()); } return eErrOut; }
bool LevelReader::readLevel(const std::string& fileName, LevelData& data) { XMLDocument xmlDoc; XMLError result = xmlDoc.LoadFile(fileName.c_str()); XMLCheckResult(result); XMLElement* map = xmlDoc.FirstChildElement("map"); if (map == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no map node found."); return false; } if (!readLevelProperties(map, data)) return false; if (!readFirstGridIDs(map, data)) return false; if (!readLayers(map, data)) return false; if (!readObjects(map, data)) return false; updateData(data); if (!checkData(data)) return false; return true; }
bool LevelReader::readLevelProperties(XMLElement* map, LevelData& data) const { // check if renderorder is correct const char* textAttr = nullptr; textAttr = map->Attribute("renderorder"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no renderorder attribute found."); return false; } std::string renderorder = textAttr; if (renderorder.compare("right-down") != 0) { g_logger->logError("LevelReader", "XML file could not be read, renderorder is not \"right-down\"."); return false; } // check if orientation is correct textAttr = nullptr; textAttr = map->Attribute("orientation"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no orientation attribute found."); return false; } std::string orientation = textAttr; if (orientation.compare("orthogonal") != 0) { g_logger->logError("LevelReader", "XML file could not be read, renderorder is not \"orthogonal\"."); return false; } // read map width and height XMLError result = map->QueryIntAttribute("width", &data.mapSize.x); XMLCheckResult(result); result = map->QueryIntAttribute("height", &data.mapSize.y); XMLCheckResult(result); // read tile size result = map->QueryIntAttribute("tilewidth", &data.tileSize.x); XMLCheckResult(result); result = map->QueryIntAttribute("tileheight", &data.tileSize.y); XMLCheckResult(result); // read level properties XMLElement* properties = map->FirstChildElement("properties"); if (properties == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no properties node found."); return false; } XMLElement* _property = properties->FirstChildElement("property"); while (_property != nullptr) { textAttr = nullptr; textAttr = _property->Attribute("name"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no property->name attribute found."); return false; } std::string name = textAttr; if (name.compare("name") == 0) { if (!readLevelName(_property, data)) return false; } else if (name.compare("backgroundlayers") == 0) { if (!readBackgroundLayers(_property, data)) return false; } else if (name.compare("tilesetpath") == 0) { if (!readTilesetPath(_property, data)) return false; } else if (name.compare("musicpath") == 0) { if (!readMusicPath(_property, data)) return false; } else if (name.compare("dimming") == 0) { if (!readDimming(_property, data)) return false; } else { g_logger->logError("LevelReader", "XML file could not be read, unknown name attribute found in properties (map->properties->property->name)."); return false; } _property = _property->NextSiblingElement("property"); } return true; }
bool LevelReader::readLights(XMLElement* objectgroup, LevelData& data) const { XMLElement* object = objectgroup->FirstChildElement("object"); while (object != nullptr) { int x; XMLError result = object->QueryIntAttribute("x", &x); XMLCheckResult(result); int y; result = object->QueryIntAttribute("y", &y); XMLCheckResult(result); int width; result = object->QueryIntAttribute("width", &width); XMLCheckResult(result); int height; result = object->QueryIntAttribute("height", &height); XMLCheckResult(result); LightBean bean; bean.radius.x = width / 2.f; bean.radius.y = height / 2.f; bean.center.x = x + bean.radius.x; bean.center.y = y + bean.radius.y; // brightness for light bean XMLElement* properties = object->FirstChildElement("properties"); if (properties != nullptr) { XMLElement* _property = properties->FirstChildElement("property"); while (_property != nullptr) { const char* textAttr = nullptr; textAttr = _property->Attribute("name"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no objectgroup->object->properties->property->name attribute found for light bean."); return false; } std::string name = textAttr; if (name.compare("brightness") == 0) { float brightness; result = _property->QueryFloatAttribute("value", &brightness); XMLCheckResult(result); if (brightness < 0.f || brightness > 1.f) { brightness = 1.f; g_logger->logWarning("LevelReader", "Brightness must be between 0 and 1. It was " + std::to_string(brightness) + ", it is now 1"); } bean.brightness = brightness; } else { g_logger->logError("LevelReader", "XML file could not be read, unknown objectgroup->object->properties->property->name attribute found for light bean."); return false; } _property = _property->NextSiblingElement("property"); } } data.lights.push_back(bean); object = object->NextSiblingElement("object"); } return true; }
bool LevelReader::readEnemies(XMLElement* objectgroup, LevelData& data) const { XMLElement* object = objectgroup->FirstChildElement("object"); while (object != nullptr) { int id; XMLError result = object->QueryIntAttribute("id", &id); XMLCheckResult(result); int gid; result = object->QueryIntAttribute("gid", &gid); XMLCheckResult(result); int x; result = object->QueryIntAttribute("x", &x); XMLCheckResult(result); int y; result = object->QueryIntAttribute("y", &y); XMLCheckResult(result); gid = (gid == 0) ? gid : gid - m_firstGidEnemies + 1; if (m_enemyMap.find(gid) == m_enemyMap.end()) { g_logger->logError("LevelReader", "Enemy ID not recognized: " + std::to_string(gid)); return false; } data.enemies.insert({ id, std::pair<EnemyID, sf::Vector2f>(m_enemyMap.at(gid), sf::Vector2f(static_cast<float>(x), static_cast<float>(y))) }); // enemy loot XMLElement* loot = object->FirstChildElement("properties"); if (loot != nullptr) { std::pair<std::map<std::string, int>, int> items; items.second = 0; items.first.clear(); XMLElement* item = loot->FirstChildElement("property"); while (item != nullptr) { const char* textAttr = nullptr; textAttr = item->Attribute("name"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no objectgroup->object->properties->property->name attribute found."); return false; } std::string itemText = textAttr; if (itemText.compare("questtarget") == 0) { textAttr = item->Attribute("value"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, quest target value attribute is void."); return false; } std::string questtargetText = textAttr; std::string questID = questtargetText.substr(0, questtargetText.find(",")); questtargetText.erase(0, questtargetText.find(",") + 1); data.enemyQuesttarget.insert({id, std::pair<std::string, std::string>(questID, questtargetText)}); } else { int amount; result = item->QueryIntAttribute("value", &amount); XMLCheckResult(result); if (itemText.compare("gold") == 0 || itemText.compare("Gold") == 0) { items.second += amount; } else { items.first.insert({ itemText, amount }); } } item = item->NextSiblingElement("property"); } data.enemyLoot.insert({ id, items}); } object = object->NextSiblingElement("object"); } return true; }
bool LevelReader::readChestTiles(XMLElement* objectgroup, LevelData& data) const { XMLElement* object = objectgroup->FirstChildElement("object"); while (object != nullptr) { int id; XMLError result = object->QueryIntAttribute("id", &id); XMLCheckResult(result); int gid; result = object->QueryIntAttribute("gid", &gid); XMLCheckResult(result); int x; result = object->QueryIntAttribute("x", &x); XMLCheckResult(result); int y; result = object->QueryIntAttribute("y", &y); XMLCheckResult(result); int offset = static_cast<int>(DynamicTileID::Chest) + m_firstGidDynamicTiles - 1; int skinNr = (gid == 0) ? 0 : ((gid - offset) / DYNAMIC_TILE_COUNT) + 1; data.chests.insert({ id, std::pair<int, sf::Vector2f>(skinNr, sf::Vector2f(static_cast<float>(x), static_cast<float>(y))) }); // chest loot XMLElement* loot = object->FirstChildElement("properties"); if (loot != nullptr) { std::pair<std::map<std::string, int>, int> items; items.second = 0; items.first.clear(); XMLElement* item = loot->FirstChildElement("property"); while (item != nullptr) { const char* textAttr = nullptr; textAttr = item->Attribute("name"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no objectgroup->object->properties->property->name attribute found."); return false; } std::string itemText = textAttr; int amount; result = item->QueryIntAttribute("value", &amount); XMLCheckResult(result); if (itemText.compare("gold") == 0 || itemText.compare("Gold") == 0) { items.second += amount; } else if (itemText.compare("strength") == 0 || itemText.compare("Strength") == 0) { if (amount < 0 || amount > 4) { g_logger->logError("LevelReader", "XML file could not be read, strength attribute for chest is out of bounds (must be between 0 and 4)."); return false; } data.chestStrength.insert({ id, amount }); } else { items.first.insert({ itemText, amount }); } item = item->NextSiblingElement("property"); } data.chestLoot.insert({ id, items }); } object = object->NextSiblingElement("object"); } return true; }
bool LevelReader::readLevelExits(XMLElement* objectgroup, LevelData& data) const { XMLElement* object = objectgroup->FirstChildElement("object"); while (object != nullptr) { int x; XMLError result = object->QueryIntAttribute("x", &x); XMLCheckResult(result); int y; result = object->QueryIntAttribute("y", &y); XMLCheckResult(result); int width; result = object->QueryIntAttribute("width", &width); XMLCheckResult(result); int height; result = object->QueryIntAttribute("height", &height); XMLCheckResult(result); LevelExitBean bean; bean.levelExitRect = sf::FloatRect(static_cast<float>(x), static_cast<float>(y), static_cast<float>(width), static_cast<float>(height)); bean.mapID = ""; bean.mapSpawnPoint.x = -1.f; bean.mapSpawnPoint.y = -1.f; // map spawn point for level exit XMLElement* properties = object->FirstChildElement("properties"); if (properties == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no objectgroup->object->properties attribute found for level exit."); return false; } XMLElement* _property = properties->FirstChildElement("property"); while (_property != nullptr) { const char* textAttr = nullptr; textAttr = _property->Attribute("name"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no objectgroup->object->properties->property->name attribute found for level exit."); return false; } std::string name = textAttr; if (name.compare("map id") == 0) { textAttr = nullptr; textAttr = _property->Attribute("value"); if (textAttr == nullptr) { g_logger->logError("LevelReader", "XML file could not be read, no objectgroup->object->properties->property->value attribute found for level exit map id."); return false; } bean.mapID = textAttr; } else if (name.compare("x") == 0) { XMLError result = _property->QueryIntAttribute("value", &x); XMLCheckResult(result); bean.mapSpawnPoint.x = static_cast<float>(x); } else if (name.compare("y") == 0) { XMLError result = _property->QueryIntAttribute("value", &y); XMLCheckResult(result); bean.mapSpawnPoint.y = static_cast<float>(y); } else { g_logger->logError("LevelReader", "XML file could not be read, unknown objectgroup->object->properties->property->name attribute found for level exit."); return false; } _property = _property->NextSiblingElement("property"); } data.levelExits.push_back(bean); object = object->NextSiblingElement("object"); } return true; }