void Config::loadFile(const string& path) { XMLDocument document; if (document.LoadFile(path.c_str()) == XML_NO_ERROR) { XMLElement* file = document.FirstChildElement("file"); if (file) { XMLElement* config = file->FirstChildElement("config"); while (config) { const char* name = config->Attribute("name"); if (name) { if (strcmp(name, "DefaultGravity") == 0) config->QueryDoubleAttribute("value", &Config::DefaultGravity); else if (strcmp(name, "PlayerMoveSpeed") == 0) config->QueryDoubleAttribute("value", &Config::PlayerMoveSpeed); else if (strcmp(name, "PlayerJumpSpeed") == 0) config->QueryDoubleAttribute("value", &Config::PlayerJumpSpeed); else if (strcmp(name, "MaxCrosshairDistance") == 0) config->QueryDoubleAttribute("value", &Config::MaxCrosshairDistance); else if (strcmp(name, "MaxGibsOnScreen") == 0) config->QueryUnsignedAttribute("value", &Config::MaxGibsOnScreen); else if (strcmp(name, "GibsMin") == 0) config->QueryIntAttribute("value", &Config::GibsMin); else if (strcmp(name, "GibsMax") == 0) config->QueryIntAttribute("value", &Config::GibsMax); else if (strcmp(name, "BloodParticles") == 0) config->QueryIntAttribute("value", &Config::BloodParticles); else if (strcmp(name, "GibMaxSpeedX") == 0) config->QueryIntAttribute("value", &Config::GibMaxSpeedX); else if (strcmp(name, "GibMaxSpeedY") == 0) config->QueryIntAttribute("value", &Config::GibMaxSpeedY); else if (strcmp(name, "BloodMaxSpeedX") == 0) config->QueryIntAttribute("value", &Config::BloodMaxSpeedX); else if (strcmp(name, "BloodMaxSpeedY") == 0) config->QueryIntAttribute("value", &Config::BloodMaxSpeedY); } config = config->NextSiblingElement("config"); } } } else { cout << document.GetErrorStr1() << endl; cout << document.GetErrorStr2() << endl; } }
// Loads the campaign data int _Campaign::Init() { Campaigns.clear(); Log.Write("_Campaign::Init - Loading file irrlamb.xml"); // Open the XML file std::string LevelFile = std::string("levels/main.xml"); XMLDocument Document; if(Document.LoadFile(LevelFile.c_str()) != XML_NO_ERROR) { Log.Write("Error loading level file with error id = %d", Document.ErrorID()); Log.Write("Error string 1: %s", Document.GetErrorStr1()); Log.Write("Error string 2: %s", Document.GetErrorStr2()); Close(); return 0; } // Check for level tag XMLElement *CampaignsElement = Document.FirstChildElement("campaigns"); if(!CampaignsElement) { Log.Write("Could not find campaigns tag"); return 0; } // Load campaigns XMLElement *CampaignElement = CampaignsElement->FirstChildElement("campaign"); for(; CampaignElement != 0; CampaignElement = CampaignElement->NextSiblingElement("campaign")) { CampaignStruct Campaign; Campaign.Name = CampaignElement->Attribute("name"); // Get levels XMLElement *LevelElement = CampaignElement->FirstChildElement("level"); for(; LevelElement != 0; LevelElement = LevelElement->NextSiblingElement("level")) { LevelStruct Level; Level.File = LevelElement->GetText(); Level.DataPath = Game.GetWorkingPath() + "levels/" + Level.File + "/"; Level.Unlocked = 0; LevelElement->QueryIntAttribute("unlocked", &Level.Unlocked); ::Level.Init(Level.File, true); Level.NiceName = ::Level.GetLevelNiceName(); Campaign.Levels.push_back(Level); } Campaigns.push_back(Campaign); } return 1; }
void ImageMapWorldCreator::readXML(MapDirectory* mapDirectory, Engine* engine, SDL_Surface* surface, World* world) { XMLDocument document; if (document.LoadFile(mapDirectory->getXMLPath().c_str()) == XML_NO_ERROR) { XMLElement* mapElement = document.FirstChildElement("map"); if (mapElement) { XMLElement* backgroundsElement = mapElement->FirstChildElement("backgrounds"); XMLElement* texturesElement = mapElement->FirstChildElement("textures"); if (texturesElement) { readTexturesElement(texturesElement, surface); } if (backgroundsElement) { readBackgroundsElement(backgroundsElement, engine, mapDirectory, world); } } } else { cout << document.GetErrorStr1() << endl; cout << document.GetErrorStr2() << endl; } }
void Game::LoadMap( const std::string & filename ) { XMLDocument doc; doc.LoadFile(filename.c_str()); if ( doc.ErrorID() != XML_NO_ERROR ) { ostringstream ss; ss << "Loadfile:" << g_TinyXmlErrors[doc.ErrorID()] << " " << doc.GetErrorStr1() << " " << doc.GetErrorStr2(); throw XmlParsingException(ss.str()); } // Load rooms XMLElement *pElem = doc.FirstChildElement( "Room"); while( pElem != NULL ) { g_Log << "loading..." << pElem->Attribute("id"); Room *pRoom = NULL; // Custom rooms if ( pElem->Attribute("class")) { std::string classID = pElem->Attribute("class"); pRoom = RoomFactory::Create(classID); } else { pRoom = new Room(); } pRoom->Load( *pElem ); if ( m_Rooms.find(pRoom->GetId()) != m_Rooms.end()) { throw DuplicateRoomIdException(pRoom->GetId()); } m_Rooms[pRoom->GetId()] = pRoom; pElem = pElem->NextSiblingElement("Room"); } // Load transitions pElem = doc.FirstChildElement( "Transitions"); if ( pElem != NULL ) { pElem = pElem->FirstChildElement("Transition"); while( pElem != NULL ) { const char * from = pElem->Attribute("from"); const char * to = pElem->Attribute("to"); const char * direction = pElem->Attribute("direction"); const char * oneway = pElem->Attribute("oneway"); if ( direction == NULL ) throw AttributeMissingException("Direction is missing from transition"); if ( from == NULL ) throw AttributeMissingException("From attribute is missing from transition!"); if ( to == NULL ) throw AttributeMissingException("To attribute is missing from transition!"); // check is from a proper id if ( m_Rooms.find(from) == m_Rooms.end() ) { ostringstream ss; ss << from << " is not a proper room id"; throw InvalidAttributeException(ss.str()); } // check is to a proper id if ( m_Rooms.find(to) == m_Rooms.end() ) { ostringstream ss; ss << to << " is not a proper room id"; throw InvalidAttributeException(ss.str()); } string tmp = direction; transform(tmp.begin(),tmp.end(), tmp.begin(), [] (char c){ return tolower(c);}); Direction dir = kNumDirs; if ( tmp == "east" ) dir = East; else if ( tmp == "north" ) dir = North; else if ( tmp == "south" ) dir = South; else if ( tmp == "west" ) dir = West; else throw InvalidAttributeException("Direction is not properly set"); m_Rooms[from]->SetNextRoom(dir, m_Rooms[to]); if ( !oneway ) { m_Rooms[to]->SetNextRoom(g_Opposite[dir], m_Rooms[from]); } pElem = pElem->NextSiblingElement("Transition"); } } else { throw ElementMissingException("Transitions is missing!"); } // set current room pElem = doc.FirstChildElement("OnStart"); if ( pElem == NULL ) throw ElementMissingException("No OnStart defined"); pElem = pElem->FirstChildElement("CurrentRoom"); if ( pElem == NULL ) { cout << "No current room set, problems might arise\n"; } else { const char *szRoomId = pElem->Attribute("id"); if ( szRoomId == NULL) throw InvalidAttributeException("Bad format CurrentRoom"); string tmp = szRoomId; if ( m_Rooms.find(tmp) == m_Rooms.end() ) { ostringstream ss; ss << "No room " << tmp << " found!"; throw ElementMissingException(ss.str()); } SetCurrentRoom(m_Rooms[tmp]); } pElem = doc.FirstChildElement("OnStart"); pElem = pElem->FirstChildElement("Story"); if ( pElem == NULL ) throw ElementMissingException("Story"); m_Story = (pElem->GetText() == NULL) ? "" : pElem->GetText(); pElem = doc.FirstChildElement("Player"); if ( !pElem ) throw new ElementMissingException("Player"); m_Player.Load(*pElem); }
// Loads a level file int _Level::Init(const std::string &LevelName, bool HeaderOnly) { if(!HeaderOnly) Log.Write("_Level::Init - Loading level: %s", LevelName.c_str()); // Get paths this->LevelName = LevelName; LevelNiceName = ""; std::string LevelFile = LevelName + "/" + LevelName + ".xml"; std::string FilePath = Game.GetWorkingPath() + std::string("levels/") + LevelFile; std::string CustomFilePath = Save.GetCustomLevelsPath() + LevelFile; std::string DataPath = Game.GetWorkingPath() + std::string("levels/") + LevelName + "/"; // See if custom level exists first IsCustomLevel = false; std::ifstream CustomLevelExists(CustomFilePath.c_str()); if(CustomLevelExists) { IsCustomLevel = true; FilePath = CustomFilePath; DataPath = Save.GetCustomLevelsPath() + LevelName + "/"; } CustomLevelExists.close(); // Open the XML file XMLDocument Document; if(Document.LoadFile(FilePath.c_str()) != XML_NO_ERROR) { Log.Write("Error loading level file with error id = %d", Document.ErrorID()); Log.Write("Error string 1: %s", Document.GetErrorStr1()); Log.Write("Error string 2: %s", Document.GetErrorStr2()); Close(); return 0; } // Check for level tag XMLElement *LevelElement = Document.FirstChildElement("level"); if(!LevelElement) { Log.Write("Could not find level tag"); Close(); return 0; } // Level version if(LevelElement->QueryIntAttribute("version", &LevelVersion) == XML_NO_ATTRIBUTE) { Log.Write("Could not find level version"); Close(); return 0; } // Check required game version GameVersion = LevelElement->Attribute("gameversion"); if(GameVersion == "") { Log.Write("Could not find game version attribute"); Close(); return 0; } // Load level info XMLElement *InfoElement = LevelElement->FirstChildElement("info"); if(InfoElement) { XMLElement *NiceNameElement = InfoElement->FirstChildElement("name"); if(NiceNameElement) { LevelNiceName = NiceNameElement->GetText(); } } // Return after header is read if(HeaderOnly) { Close(); return true; } // Load default lua script Scripts.clear(); Scripts.push_back(Game.GetWorkingPath() + "scripts/default.lua"); // Options bool Fog = false; bool EmitLight = false; Level.ClearColor.set(255, 0, 0, 0); // Load options XMLElement *OptionsElement = LevelElement->FirstChildElement("options"); if(OptionsElement) { // Does the player emit light? XMLElement *EmitLightElement = OptionsElement->FirstChildElement("emitlight"); if(EmitLightElement) { EmitLightElement->QueryBoolAttribute("enabled", &EmitLight); } } // Load world XMLElement *ResourcesElement = LevelElement->FirstChildElement("resources"); if(ResourcesElement) { // Load scenes for(XMLElement *SceneElement = ResourcesElement->FirstChildElement("scene"); SceneElement != 0; SceneElement = SceneElement->NextSiblingElement("scene")) { // Get file std::string File = SceneElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on scene"); Close(); return 0; } // Reset fog irrDriver->setFog(video::SColor(0, 0, 0, 0), video::EFT_FOG_EXP, 0, 0, 0.0f); // Load scene if(IsCustomLevel) { irrFile->changeWorkingDirectoryTo(DataPath.c_str()); irrScene->loadScene(File.c_str(), &UserDataLoader); irrFile->changeWorkingDirectoryTo(Game.GetWorkingPath().c_str()); } else { irrScene->loadScene((DataPath + File).c_str(), &UserDataLoader); } // Set texture filters on meshes in the scene core::array<irr::scene::ISceneNode *> MeshNodes; irrScene->getSceneNodesFromType(scene::ESNT_MESH, MeshNodes); for(u32 i = 0; i < MeshNodes.size(); i++) { if(EmitLight && Config.Shaders) { video::SMaterial &Material = MeshNodes[i]->getMaterial(0); int ShaderType = 0; if(Material.MaterialType == video::EMT_TRANSPARENT_ALPHA_CHANNEL) { ShaderType = 1; } MeshNodes[i]->setMaterialType((video::E_MATERIAL_TYPE)Graphics.GetCustomMaterial(ShaderType)); } MeshNodes[i]->setMaterialFlag(video::EMF_TRILINEAR_FILTER, Config.TrilinearFiltering); for(u32 j = 0; j < MeshNodes[i]->getMaterialCount(); j++) { for(int k = 0; k < 4; k++) { MeshNodes[i]->getMaterial(j).TextureLayer[k].AnisotropicFilter = Config.AnisotropicFiltering; if(MeshNodes[i]->getMaterial(j).FogEnable) Fog = true; } } } } // Load collision for(XMLElement *CollisionElement = ResourcesElement->FirstChildElement("collision"); CollisionElement != 0; CollisionElement = CollisionElement->NextSiblingElement("collision")) { // Get file std::string File = CollisionElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on collision"); Close(); return 0; } // Create template TemplateStruct *Template = new TemplateStruct; Template->CollisionFile = DataPath + File; Template->Type = _Object::COLLISION; Template->Mass = 0.0f; Templates.push_back(Template); // Create spawn SpawnStruct *ObjectSpawn = new SpawnStruct; ObjectSpawn->Template = Template; ObjectSpawns.push_back(ObjectSpawn); } // Load scripts for(XMLElement *ScriptElement = ResourcesElement->FirstChildElement("script"); ScriptElement != 0; ScriptElement = ScriptElement->NextSiblingElement("script")) { // Get file std::string File = ScriptElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on script"); Close(); return 0; } Scripts.push_back(DataPath + File); } // Load sounds Sounds.clear(); for(XMLElement *SoundElement = ResourcesElement->FirstChildElement("sound"); SoundElement != 0; SoundElement = SoundElement->NextSiblingElement("sound")) { // Get file std::string File = SoundElement->Attribute("file"); if(File == "") { Log.Write("Could not find file attribute on sound"); Close(); return 0; } // Attempt to load sound if(Audio.LoadBuffer(File)) Sounds.push_back(File); } } // Load templates XMLElement *TemplatesElement = LevelElement->FirstChildElement("templates"); if(TemplatesElement) { int TemplateID = 0; for(XMLElement *TemplateElement = TemplatesElement->FirstChildElement(); TemplateElement != 0; TemplateElement = TemplateElement->NextSiblingElement()) { // Create a template TemplateStruct *Template = new TemplateStruct; Template->Fog = Fog; // Get the template properties if(!GetTemplateProperties(TemplateElement, *Template)) return 0; // Assign options Template->TemplateID = TemplateID; if(EmitLight) { // Use shaders on materials that receive light if(Config.Shaders) Template->CustomMaterial = Graphics.GetCustomMaterial(0); // Set the player to emit light if(Template->Type == _Object::PLAYER || Template->Type == _Object::ORB) Template->EmitLight = true; } TemplateID++; // Store for later Templates.push_back(Template); } } // Load object spawns XMLElement *ObjectsElement = LevelElement->FirstChildElement("objects"); if(ObjectsElement) { for(XMLElement *ObjectElement = ObjectsElement->FirstChildElement(); ObjectElement != 0; ObjectElement = ObjectElement->NextSiblingElement()) { // Create an object spawn SpawnStruct *ObjectSpawn = new SpawnStruct; // Get the object properties if(!GetObjectSpawnProperties(ObjectElement, *ObjectSpawn)) return 0; // Store for later ObjectSpawns.push_back(ObjectSpawn); } } return 1; }