From: <eg...@us...> - 2007-05-20 19:13:42
|
Revision: 492 http://svn.sourceforge.net/opengate/?rev=492&view=rev Author: egore Date: 2007-05-20 12:13:41 -0700 (Sun, 20 May 2007) Log Message: ----------- Add an entity manager that is able to load "everything" (at a later point of development) Subclass EntityManager into the VesselManager Add an equipment manager that is currently able to load capacitors Modified Paths: -------------- branches/ogsector/src/Entity.h branches/ogsector/src/GameStateManager.cpp branches/ogsector/src/GameStateManager.h branches/ogsector/src/Makefile.in branches/ogsector/src/Sector.cpp branches/ogsector/src/VesselManager.cpp branches/ogsector/src/VesselManager.h Added Paths: ----------- branches/ogsector/src/EntityManager.cpp branches/ogsector/src/EntityManager.h branches/ogsector/src/VesselTemplate.h Modified: branches/ogsector/src/Entity.h =================================================================== --- branches/ogsector/src/Entity.h 2007-05-20 16:51:58 UTC (rev 491) +++ branches/ogsector/src/Entity.h 2007-05-20 19:13:41 UTC (rev 492) @@ -49,6 +49,9 @@ inline void setClassName( const std::string & name ) { className_ = name; } inline std::string className( ) const { return className_; } + inline void setID( int id ){ id_ = id; } + inline int id( ) const { return id_; } + inline void setTechLevel( int level ){ techLevel_ = level; } inline int techLevel( ) const { return techLevel_; } @@ -58,6 +61,7 @@ protected: EntityTemplate(): factionName_( "unknown" ), name_( "unknown" ), className_( "unknown" ) { + id_ = 0; techLevel_ = 0; mass_ = 0; } @@ -68,6 +72,8 @@ std::string className_; + int id_; + int techLevel_; /*! The mass of this entity. */ long mass_; Added: branches/ogsector/src/EntityManager.cpp =================================================================== --- branches/ogsector/src/EntityManager.cpp (rev 0) +++ branches/ogsector/src/EntityManager.cpp 2007-05-20 19:13:41 UTC (rev 492) @@ -0,0 +1,158 @@ +/*************************************************************************** + * Copyright (C) 2006-2007 by OpenGate development team * + * eg...@us... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#include "EntityManager.h" +#include "tinyxml/tinyxml.h" + +namespace OpenGate { + +EntityManager::~EntityManager(){ + for ( std::map< int, EntityTemplate * >::iterator it = templatesByID_.begin(); it != templatesByID_.end(); it ++ ){ + delete it->second; + } + templatesByID_.end(); +} + +void EntityManager::load( const std::set < std::string > & resourcePaths, const std::string & resourceName ){ + + for ( std::set < std::string >::iterator it = resourcePaths.begin(); it != resourcePaths.end(); it ++ ){ + std::string resourceFile( (*it) + "/" + resourceName ); + + TiXmlDocument doc( resourceFile ); + bool loadOkay = doc.LoadFile(); + + std::map < int, std::string > entities; + + if ( loadOkay ) { + log_->info( std::string( "open resource file: ") + resourceFile ); + TiXmlHandle docHandle( &doc ); + TiXmlElement* pElem; + TiXmlHandle hRoot( 0 ); + + pElem = docHandle.FirstChildElement().Element(); + hRoot = TiXmlHandle( pElem ); + + if ( pElem ) { + for ( pElem = hRoot.FirstChild( "Resource" ).Element(); pElem != 0; pElem = pElem->NextSiblingElement() ) { + log_->info( std::string( "Found ressource: " ) + pElem->Attribute("location") ); + entities[ toInt( pElem->Attribute("id") ) ] = pElem->Attribute("location"); + } + } else { + log_->fatal( resourceFile + " cannot read first node." ); + } + } else { + // log_->info( resourceFile + " cannot open resourceFile." ); + } + + for ( std::map < int, std::string >::iterator it = entities.begin(); it != entities.end(); it ++ ){ + loadAndCreate( it->first, it->second ); + } + } +} + +EntityTemplate * EquipmentManager::loadAndCreate( int id, const std::string & fileName ){ + + TiXmlDocument doc( fileName ); + bool loadOkay = doc.LoadFile(); + + if ( !loadOkay ) { + log_->fatal( std::string( "Failed to load file: " + fileName ) ); + return NULL; + } + + TiXmlHandle docHandle( &doc ); + TiXmlElement* pElem; + TiXmlNode* pNode; + TiXmlHandle hRoot( 0 ); + + //** block: equipment; + pElem = docHandle.FirstChildElement().Element(); + hRoot = TiXmlHandle( pElem ); + + if ( !pElem ) { + log_->fatal( fileName + " cannot read first node." ); + return NULL; + } + + if ( strcmp( "equipment", pElem->Value() ) != 0 ){ + log_->fatal( fileName + " is no equipment description" ); + return NULL; + } + + // FIXME: we can only load capacitors by now + Capacitor *entity = new Capacitor(); + + // Naming + pElem = hRoot.ChildElement( "faction", 0 ).Element(); + if ( pElem ) { + entity->setFactionName( pElem->FirstChild()->Value() ); + } else { + log_->fatal( fileName + " has no mandatory factionname (mandatory)." ); + return NULL; + } + pElem = hRoot.ChildElement( "name", 0 ).Element(); + if ( pElem ) { + entity->setName( pElem->FirstChild()->Value() ); + } else { + log_->fatal( fileName + " has no name (mandatory)." ); + return NULL; + } + + // Generic Data + pElem = hRoot.ChildElement( "class", 0 ).Element(); + if ( pElem ) entity->setClassName( pElem->FirstChild()->Value() ); + pElem = hRoot.ChildElement( "techlevel", 0 ).Element(); + if ( pElem ) entity->setTechLevel( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "size", 0 ).Element(); + if ( pElem ) entity->setSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "mass", 0 ).Element(); + if ( pElem ) entity->setMass( toInt( pElem->FirstChild()->Value() ) ); + + // Specific Data + pElem = hRoot.ChildElement( "efficiency", 0 ).Element(); + if ( pElem ) entity->setEfficiency( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "capacity", 0 ).Element(); + if ( pElem ) entity->setCapacity( toInt( pElem->FirstChild()->Value() ) ); + + if ( templatesByName_.count( entity->name() ) == 0 ){ + entity->setID( id ); + templatesByName_[ entity->name() ] = entity; + templatesByID_[ id ] = entity; + + factions_.insert( entity->factionName() ); + log_->info( std::string( "Loaded: " ) + entity->factionName() + "/" + entity->name() ); + } else { + log_->warn( std::string( "Equipment with name: " ) + entity->factionName() + "/" + entity->name() + + " allready loaded" ); + } + return entity; +} + +template<> EquipmentManager * Ogre::Singleton< EquipmentManager >::ms_Singleton = 0; + +EquipmentManager::EquipmentManager(): EntityManager(){ +} + +EquipmentManager::~EquipmentManager() { +} + +} + + Property changes on: branches/ogsector/src/EntityManager.cpp ___________________________________________________________________ Name: svn:mime-type + text/x-c++src Name: svn:eol-style + native Added: branches/ogsector/src/EntityManager.h =================================================================== --- branches/ogsector/src/EntityManager.h (rev 0) +++ branches/ogsector/src/EntityManager.h 2007-05-20 19:13:41 UTC (rev 492) @@ -0,0 +1,98 @@ +/*************************************************************************** + * Copyright (C) 2006-2007 by OpenGate development team * + * eg...@us... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _OPENGATE_ENTITYMANAGER_H +#define _OPENGATE_ENTITYMANAGER_H + +#include "LogManager.h" +#include "Entity.h" + +namespace OpenGate { + +/*! + * \brief This class is the generic class that all extend that like to load + * something from an XML file. + * \author Christoph Brill <eg...@us...> + */ +class EntityManager{ +public: + /*! \brief This method loads equipment and stores it in an interal storage. + * + * The file we load from does not contain the definitions itself, but the + * places where we find the definition files. The loading from the definition + * files is done by loadAndCreate(). + * \param resourcePaths An array of all available resource paths + * \param resourceName The name of the file to load the equipment from */ + void load( const std::set < std::string > & resourcePaths, const std::string & resourceName ); + + inline std::set < std::string > factionNames( ) const { return factions_; } + +protected: + + /*! \brief This loads enities from there definition files, called from load() + * \param id The id of the entity in the places-file + * \param filename The filename we load from the places-file. It's the + * filename of the defintion file. + */ + virtual EntityTemplate * loadAndCreate( int id, const std::string & filename ) = 0; + + EntityManager() { + log_ = LogManager::getSingletonPtr(); + } + + virtual ~EntityManager(); + + LogManager *log_; + + std::map < std::string, EntityTemplate * > templatesByName_; + + std::map < int, EntityTemplate * > templatesByID_; + + std::set < std::string > factions_; +}; + +/*! + * \brief This class is able to load equipment (reactors, engines, guns, etc.) + * from XML files. + * \author Christoph Brill <eg...@us...> + */ +class EquipmentManager : public Ogre::Singleton< EquipmentManager >, public EntityManager{ +public: + + /*! Get the single instance of the EquipmentManager */ + static EquipmentManager & getSingleton( ); + + /*! Get the pointer to single instance of the EquipmentManager */ + static EquipmentManager * getSingletonPtr( ); + + /*! Constructor */ + EquipmentManager(); + + /*! Destructor that free's all loaded vessels */ + virtual ~EquipmentManager(); + + virtual EntityTemplate * loadAndCreate( int id, const std::string & filename ); + +}; + +} // namespace OpenGate + +#endif //_OPENGATE_ENTITYMANAGER_H + Property changes on: branches/ogsector/src/EntityManager.h ___________________________________________________________________ Name: svn:mime-type + text/x-c++hdr Name: svn:eol-style + native Modified: branches/ogsector/src/GameStateManager.cpp =================================================================== --- branches/ogsector/src/GameStateManager.cpp 2007-05-20 16:51:58 UTC (rev 491) +++ branches/ogsector/src/GameStateManager.cpp 2007-05-20 19:13:41 UTC (rev 492) @@ -29,6 +29,7 @@ #include "InputManager.h" #include "VesselManager.h" #include "UnDockedState.h" +#include "EntityManager.h" #include <OgreFrameListener.h> namespace OpenGate { @@ -292,8 +293,11 @@ devices_.console->addCommand( "/ai", &GameStateManager::CMD_spawnAi, "[name] Spawn ai subject. /ai help show all available ships." ); vesselManager_ = new VesselManager( ); - vesselManager_->loadVessels( resourcePaths_, "ships.xml" ); + vesselManager_->load( resourcePaths_, "ships.xml" ); + equipmentManager_ = new EquipmentManager( ); + equipmentManager_->load( resourcePaths_, "equipment.xml" ); + devices_.gameStateRoot = this; return true; Modified: branches/ogsector/src/GameStateManager.h =================================================================== --- branches/ogsector/src/GameStateManager.h 2007-05-20 16:51:58 UTC (rev 491) +++ branches/ogsector/src/GameStateManager.h 2007-05-20 19:13:41 UTC (rev 492) @@ -31,6 +31,7 @@ class LogManager; class InputManager; class VesselManager; +class EquipmentManager; /*! * \brief The class defines the gamestate manager. @@ -99,6 +100,7 @@ LogManager * log_; VesselManager * vesselManager_; + EquipmentManager * equipmentManager_; std::set < std::string > resourcePaths_; std::vector< GameState * > stateStack_; Modified: branches/ogsector/src/Makefile.in =================================================================== --- branches/ogsector/src/Makefile.in 2007-05-20 16:51:58 UTC (rev 491) +++ branches/ogsector/src/Makefile.in 2007-05-20 19:13:41 UTC (rev 492) @@ -51,7 +51,8 @@ am_opengateclient_OBJECTS = opengateclient.$(OBJEXT) common.$(OBJEXT) \ networkClient.$(OBJEXT) tinyxml.$(OBJEXT) \ tinyxmlerror.$(OBJEXT) tinyxmlparser.$(OBJEXT) \ - Console.$(OBJEXT) LogManager.$(OBJEXT) InputManager.$(OBJEXT) \ + Console.$(OBJEXT) Entity.$(OBJEXT) EntityManager.$(OBJEXT) \ + LogManager.$(OBJEXT) InputManager.$(OBJEXT) \ VesselManager.$(OBJEXT) GameStateManager.$(OBJEXT) \ GameState.$(OBJEXT) DockedState.$(OBJEXT) \ ShipConfigDialog.$(OBJEXT) UnDockedState.$(OBJEXT) \ @@ -191,12 +192,17 @@ tinyxml/tinyxmlparser.cpp \ Console.h \ Console.cpp \ + Entity.h \ + Entity.cpp \ + EntityManager.h \ + EntityManager.cpp \ LogManager.h \ LogManager.cpp \ InputManager.h \ InputManager.cpp \ VesselManager.h \ VesselManager.cpp \ + VesselTemplate.h \ GameStateManager.h \ GameStateManager.cpp \ GameState.h \ @@ -306,6 +312,8 @@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Console.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/DockedState.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/Entity.Po@am__quote@ +@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/EntityManager.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GameState.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/GameStateManager.Po@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/InputManager.Po@am__quote@ Modified: branches/ogsector/src/Sector.cpp =================================================================== --- branches/ogsector/src/Sector.cpp 2007-05-20 16:51:58 UTC (rev 491) +++ branches/ogsector/src/Sector.cpp 2007-05-20 19:13:41 UTC (rev 492) @@ -616,7 +616,7 @@ if ( obj ) { log_->info( std::string( "Send register " ) + obj->name() ); MessageBodyShipRegister msg( obj->userID(), obj->childID(), obj->name(), obj->mainNode()->getPosition(), - obj->vesselBluePrint()->vesselID() ); + obj->vesselBluePrint()->id() ); network_->send( msg ); } Modified: branches/ogsector/src/VesselManager.cpp =================================================================== --- branches/ogsector/src/VesselManager.cpp 2007-05-20 16:51:58 UTC (rev 491) +++ branches/ogsector/src/VesselManager.cpp 2007-05-20 19:13:41 UTC (rev 492) @@ -27,179 +27,132 @@ template<> VesselManager * Ogre::Singleton< VesselManager >::ms_Singleton = 0; -VesselManager::VesselManager(){ - log_ = LogManager::getSingletonPtr(); +VesselManager::VesselManager(): EntityManager(){ } VesselManager::~VesselManager(){ - for ( std::map< int, Vessel * >::iterator it = vesselID_.begin(); it != vesselID_.end(); it ++ ){ - delete it->second; - } - vesselID_.end(); } -void VesselManager::loadVessels( const std::set < std::string > & resourcePaths, const std::string & resourceName ){ +EntityTemplate * VesselManager::loadAndCreate( int vesselID, const std::string & fileName ){ - for ( std::set < std::string >::iterator it = resourcePaths.begin(); it != resourcePaths.end(); it ++ ){ - std::string shipResourceFile( (*it) + "/" + resourceName ); - - TiXmlDocument doc( shipResourceFile ); - bool loadOkay = doc.LoadFile(); - - std::map < int, std::string > ships; - - if ( loadOkay ) { - log_->info( std::string( "open resource file for ships: ") + shipResourceFile ); - TiXmlHandle docHandle( &doc ); - TiXmlElement* pElem; - TiXmlHandle hRoot( 0 ); - - //** block: Ships; - pElem = docHandle.FirstChildElement().Element(); - hRoot = TiXmlHandle( pElem ); - - if ( pElem ) { - for ( pElem = hRoot.FirstChild( "Resource" ).Element(); pElem != 0; pElem = pElem->NextSiblingElement() ) { - log_->info( std::string( "Found ressource: " ) + pElem->Attribute("location") ); - ships[ toInt( pElem->Attribute("id") ) ] = pElem->Attribute("location"); - } - } else { - log_->fatal( shipResourceFile + " cannot read first node." ); - } - } else { - // log_->info( shipResourceFile + " cannot open shipResourceFile." ); - } - - for ( std::map < int, std::string >::iterator it = ships.begin(); it != ships.end(); it ++ ){ - loadAndCreateVessel( it->first, it->second ); - } - } -} - -Vessel * VesselManager::loadAndCreateVessel( int vesselID, const std::string & fileName ){ - TiXmlDocument doc( fileName ); bool loadOkay = doc.LoadFile(); - if ( loadOkay ) { + if ( !loadOkay ) { + log_->fatal( std::string( "Failed to load file: " + fileName ) ); + return NULL; + } - TiXmlHandle docHandle( &doc ); - TiXmlElement* pElem; - TiXmlNode* pNode; - TiXmlHandle hRoot( 0 ); + TiXmlHandle docHandle( &doc ); + TiXmlElement* pElem; + TiXmlNode* pNode; + TiXmlHandle hRoot( 0 ); - //** block: Ship; - pElem = docHandle.FirstChildElement().Element(); - hRoot = TiXmlHandle( pElem ); + //** block: Ship; + pElem = docHandle.FirstChildElement().Element(); + hRoot = TiXmlHandle( pElem ); - if ( !pElem ) { - log_->fatal( fileName + " cannot read first node." ); - return NULL; - } + if ( !pElem ) { + log_->fatal( fileName + " cannot read first node." ); + return NULL; + } - if ( strcmp( "ship", pElem->Value() ) == 0 ){ + if ( strcmp( "ship", pElem->Value() ) != 0 ){ + log_->fatal( fileName + " is no ship description" ); + return NULL; + } - Vessel *vessel = new Vessel(); + Vessel *vessel = new Vessel(); - pElem = hRoot.ChildElement( "faction", 0 ).Element(); - if ( pElem ) { - vessel->setFactionName( pElem->FirstChild()->Value() ); + pElem = hRoot.ChildElement( "faction", 0 ).Element(); + if ( pElem ) { + vessel->setFactionName( pElem->FirstChild()->Value() ); } else { - log_->fatal( fileName + " has no mandatory factionname (mandatory)." ); - return NULL; + log_->fatal( fileName + " has no mandatory factionname (mandatory)." ); + return NULL; } - pElem = hRoot.ChildElement( "name", 0 ).Element(); - if ( pElem ) { - vessel->setName( pElem->FirstChild()->Value() ); - } else { - log_->fatal( fileName + " has no name (mandatory)." ); - return NULL; - } + pElem = hRoot.ChildElement( "name", 0 ).Element(); + if ( pElem ) { + vessel->setName( pElem->FirstChild()->Value() ); + } else { + log_->fatal( fileName + " has no name (mandatory)." ); + return NULL; + } - pElem = hRoot.ChildElement( "class", 0 ).Element(); - if ( pElem ) vessel->setClassName( pElem->FirstChild()->Value() ); + pElem = hRoot.ChildElement( "class", 0 ).Element(); + if ( pElem ) vessel->setClassName( pElem->FirstChild()->Value() ); - pElem = hRoot.ChildElement( "techlevel", 0 ).Element(); - if ( pElem ) vessel->setTechLevel( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "techlevel", 0 ).Element(); + if ( pElem ) vessel->setTechLevel( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "size", 0 ).Element(); - if ( pElem ) vessel->setBaseSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "baseyaw", 0 ).Element(); - if ( pElem ) vessel->setBaseYaw( toFloat( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "mass", 0 ).Element(); - if ( pElem ) vessel->setMass( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "armor", 0 ).Element(); - if ( pElem ) vessel->setArmor( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "size", 0 ).Element(); + if ( pElem ) vessel->setBaseSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "baseyaw", 0 ).Element(); + if ( pElem ) vessel->setBaseYaw( toFloat( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "mass", 0 ).Element(); + if ( pElem ) vessel->setMass( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "armor", 0 ).Element(); + if ( pElem ) vessel->setArmor( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "yaw", 0 ).Element(); - if ( pElem ) vessel->setYaw( toFloat( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "pitch", 0 ).Element(); - if ( pElem ) vessel->setPitch( toFloat( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "roll", 0 ).Element(); - if ( pElem ) vessel->setRoll( toFloat( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "dragfactor", 0 ).Element(); - if ( pElem ) vessel->setDragFactor( toFloat( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "yaw", 0 ).Element(); + if ( pElem ) vessel->setYaw( toFloat( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "pitch", 0 ).Element(); + if ( pElem ) vessel->setPitch( toFloat( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "roll", 0 ).Element(); + if ( pElem ) vessel->setRoll( toFloat( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "dragfactor", 0 ).Element(); + if ( pElem ) vessel->setDragFactor( toFloat( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "guns", 0 ).Element(); - if ( pElem ) vessel->setAmountGunHardPoints( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "engines", 0 ).Element(); - if ( pElem ) vessel->setAmountEngines( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "modx", 0 ).Element(); - if ( pElem ) vessel->setAmountModX( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "missiles", 0 ).Element(); - if ( pElem ) vessel->setAmountMissiles( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "guns", 0 ).Element(); + if ( pElem ) vessel->setAmountGunHardPoints( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "engines", 0 ).Element(); + if ( pElem ) vessel->setAmountEngines( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "modx", 0 ).Element(); + if ( pElem ) vessel->setAmountModX( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "missiles", 0 ).Element(); + if ( pElem ) vessel->setAmountMissiles( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "cargocapacity", 0 ).Element(); - if ( pElem ) vessel->setCargoSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "cargocapacity", 0 ).Element(); + if ( pElem ) vessel->setCargoSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "capacitorsize", 0 ).Element(); - if ( pElem ) vessel->setCapacitorSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "powerplantsize", 0 ).Element(); - if ( pElem ) vessel->setPowerPlantSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "enginesize", 0 ).Element(); - if ( pElem ) vessel->setEngineSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "radarsize", 0 ).Element(); - if ( pElem ) vessel->setRadarSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "shieldsize", 0 ).Element(); - if ( pElem ) vessel->setShieldSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "ecmsize", 0 ).Element(); - if ( pElem ) vessel->setEcmSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "capacitorsize", 0 ).Element(); + if ( pElem ) vessel->setCapacitorSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "powerplantsize", 0 ).Element(); + if ( pElem ) vessel->setPowerPlantSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "enginesize", 0 ).Element(); + if ( pElem ) vessel->setEngineSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "radarsize", 0 ).Element(); + if ( pElem ) vessel->setRadarSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "shieldsize", 0 ).Element(); + if ( pElem ) vessel->setShieldSize( toInt( pElem->FirstChild()->Value() ) ); + pElem = hRoot.ChildElement( "ecmsize", 0 ).Element(); + if ( pElem ) vessel->setEcmSize( toInt( pElem->FirstChild()->Value() ) ); - pElem = hRoot.ChildElement( "mesh", 0 ).Element(); - if ( pElem ) { - std::string meshName( pElem->FirstChild()->Value() ); - if ( meshName.find( "/", 0 ) != std::string::npos ){ - vessel->setMesh( meshName, false ); - } else { - vessel->setMesh( meshName, true ); - } - } else { - log_->fatal( fileName + " has no mesh entry" ); - return NULL; - } - - if ( vessels_.count( vessel->name() ) == 0 ){ - vessel->setVesselID( vesselID ); - vessels_[ vessel->name() ] = vessel; - vesselID_[ vesselID ] = vessel; - - factions_.insert( vessel->factionName() ); - log_->info( std::string( "Loaded: " ) + vessel->factionName() + "/" + vessel->name() ); - } else { - log_->warn( std::string( "Ship with name: " ) + vessel->factionName() + "/" + vessel->name() + - " allready loaded" ); - } - - return vessel; + pElem = hRoot.ChildElement( "mesh", 0 ).Element(); + if ( pElem ) { + std::string meshName( pElem->FirstChild()->Value() ); + if ( meshName.find( "/", 0 ) != std::string::npos ){ + vessel->setMesh( meshName, false ); } else { - log_->fatal( fileName + " is no ship description" ); - return NULL; + vessel->setMesh( meshName, true ); } } else { - log_->fatal( std::string( "Failed to load file: " + fileName ) ); + log_->fatal( fileName + " has no mesh entry" ); return NULL; } - return NULL; + + if ( templatesByName_.count( vessel->name() ) == 0 ){ + vessel->setID( vesselID ); + templatesByName_[ vessel->name() ] = vessel; + templatesByID_[ vesselID ] = vessel; + + factions_.insert( vessel->factionName() ); + log_->info( std::string( "Loaded: " ) + vessel->factionName() + "/" + vessel->name() ); + } else { + log_->warn( std::string( "Ship with name: " ) + vessel->factionName() + "/" + vessel->name() + + " allready loaded" ); + } + return vessel; } VesselManager & VesselManager::getSingleton( ){ Modified: branches/ogsector/src/VesselManager.h =================================================================== --- branches/ogsector/src/VesselManager.h 2007-05-20 16:51:58 UTC (rev 491) +++ branches/ogsector/src/VesselManager.h 2007-05-20 19:13:41 UTC (rev 492) @@ -22,174 +22,15 @@ #define VESSELMANAGER__H #include <OgreSingleton.h> -#include <OgreMesh.h> -#include <OgreMeshManager.h> -#include <iostream> -#include <map> #include "LogManager.h" #include "Entity.h" +#include "VesselTemplate.h" +#include "EntityManager.h" namespace OpenGate{ /*! - * \brief This class defines a vessel. - * - * A vessel in opengate is that thing, that the user needs to fly around in - * space. Others might call it "starship", "spaceshit" or something like that. - * A vessel has several properties. It has a weight, a yaw-. a roll- and a - * pitch-latency. It also has a drag factor and many properties more. Each - * vessel must have these options. The data of a vessel is loaded from an XML - * document. This is more or less a logical storage for the vessel, not more and - * not less. - * \todo Check how to load the XML data into a vessel. My first approach was that - * a vessel was passed the name of the XML document and it load everything - * from that file. The better approach would be to create something like - * an XMLManager, that is able to load a vessel by it's name. - * \todo See how to attach objects (capacitory, power plants, weapons) to the - * vessel. - * \author Carsten <spo...@us...> - */ -class Vessel : public EntityTemplate{ -public: - - /*! Constructor creating a dummy vessel */ - Vessel(): EntityTemplate(){ - - vesselID_ = 0; - - vesselSize_ = 10.0; - vesselArmor_ = 1.0; - vesselYaw_ = 0; - - yaw_ = 1.0; - pitch_ = 1.0; - roll_ = 1.0; - dragFactor_ = 1.0; - - amountGunHardPoints_ = 0; - amountEngines_ = 0; - amountModX_ = 0; - amountMissiles_ = 0; - - shieldSize_ = 0; - radarSize_ = 0; - engineSize_ = 0; - capacitorSize_ = 0; - powerPlantSize_ = 0; - cargoSize_ = 0; - ecmSize_ = 0; - - } - - /*! Desctructor */ - ~Vessel(){} - - inline void setVesselID( int id ){ vesselID_ = id; } - inline int vesselID( ) const { return vesselID_; } - - inline void setBaseSize( float vesselSize ){ vesselSize_ = vesselSize; } - inline float baseSize( ) const { return vesselSize_; } - - inline void setBaseYaw( float vesselYaw ){ vesselYaw_ = vesselYaw; } - inline float baseYaw( ) const { return vesselYaw_; } - - inline void setArmor( float vesselArmor ){ vesselArmor_ = vesselArmor; } - inline float armor( ) const { return vesselArmor_; } - - inline void setYaw( float yaw ){ - yaw_ = yaw; if ( yaw_ < 1.0 ) yaw_ = 1000.0 * ( 180.0 * yaw_ ) / 3.141592 ; - } - inline float yaw( ) const { return yaw_; } - - inline void setPitch( float pitch ){ - pitch_ = pitch; if ( pitch_ < 1.0 ) pitch_ = 1000.0 * ( 180.0 * pitch_ ) / 3.141592 ; - } - inline float pitch( ) const { return pitch_; } - - inline void setRoll( float roll ){ - roll_ = roll; if ( roll_ < 1.0 ) roll_ = 1000.0 * ( 180.0 * roll_ ) / 3.141592 ; - } - inline float roll( ) const { return roll_; } - - inline void setDragFactor( float drag ){ dragFactor_ = drag; } - inline float dragFactor( ) const { return dragFactor_; } - - inline void setAmountGunHardPoints( int amount ){ amountGunHardPoints_ = amount; } - inline int amountGunHardPoints( ) const { return amountGunHardPoints_; } - - inline void setAmountEngines( int amount ){ amountEngines_ = amount; } - inline int amountEngines( ) const { return amountEngines_; } - - inline void setAmountModX( int amount ){ amountModX_ = amount; } - inline int amountModX( ) const { return amountModX_; } - - inline void setAmountMissiles( int amount ){ amountMissiles_ = amount; } - inline int amountMissiles( ) const { return amountMissiles_; } - - inline void setShieldSize( int size ){ shieldSize_ = size; } - inline int shieldSize( ) const { return shieldSize_; } - - inline void setRadarSize( int size ){ radarSize_ = size; } - inline int radarSize( ) const { return radarSize_; } - - inline void setEngineSize( int size ){ engineSize_ = size; } - inline int engineSize( ) const { return engineSize_; } - - inline void setCapacitorSize( int size ){ capacitorSize_ = size; } - inline int capacitorSize( ) const { return capacitorSize_; } - - inline void setPowerPlantSize( int size ){ powerPlantSize_ = size; } - inline int powerPlantSize( ) const { return powerPlantSize_; } - - inline void setCargoSize( int size ){ cargoSize_ = size; } - inline int cargoSize( ) const { return cargoSize_; } - - inline void setEcmSize( int size ){ ecmSize_ = size; } - inline int ecmSize( ) const { return ecmSize_; } - - void setMesh( const std::string & meshName, bool genPath = true ){ - std::string fileName; - if ( genPath ){ - fileName = std::string("ships") + PATHSEPARATOR + factionName_ + PATHSEPARATOR + name_ + PATHSEPARATOR + meshName; - } else { - fileName = meshName; - } - pMesh_ = Ogre::MeshManager::getSingleton().load( fileName, "General" ); - // pMesh_ = Ogre::MeshManager::getSingleton().load( meshName, "../../trunk/data/ships/octavius/apteryx/" ); - } - Ogre::MeshPtr & meshPtr( ){ return pMesh_; } - -protected: - int vesselID_; - - float vesselSize_; - float vesselArmor_; - float vesselYaw_; - - float yaw_; - float pitch_; - float roll_; - float dragFactor_; - - int amountEngines_; - int amountGunHardPoints_; - int amountModX_; - int amountMissiles_; - - int cargoSize_; - - int capacitorSize_; - int powerPlantSize_; - int shieldSize_; - int radarSize_; - int engineSize_; - int ecmSize_; - - Ogre::MeshPtr pMesh_; -}; - -/*! * \brief This singleton class is able to load a vessel by it's name * * The vessel manager is a class to handle access to all vessels. This way a @@ -203,7 +44,7 @@ * similar. * \author Carsten <spo...@us...> */ -class VesselManager : public Ogre::Singleton< VesselManager >{ +class VesselManager : public Ogre::Singleton< VesselManager >, public EntityManager{ public: /*! Get the single instance of the VesselManager */ @@ -216,29 +57,24 @@ VesselManager(); /*! Destructor that free's all loaded vessels */ - ~VesselManager(); + virtual ~VesselManager(); - /*! This method loads vessels and stores them in an interal storage - * \param resourcePaths An array of all available resource paths - * \param resourceName The name of the file to load the vessels from */ - void loadVessels( const std::set < std::string > & resourcePaths, const std::string & resourceName ); + virtual EntityTemplate * loadAndCreate( int id, const std::string & filename ); - Vessel * loadAndCreateVessel( int vesselID, const std::string & filename ); - /*! This returns a vessel by it's given unique ID \remarks The vessel needs to be loaded before \param vesselID The unique ID of a vessel */ Vessel * vessel( int vesselID ){ - if ( vesselID_.count( vesselID ) ){ - return vesselID_[ vesselID ]; + if ( templatesByID_.count( vesselID ) ){ + return static_cast<Vessel *>(templatesByID_[ vesselID ]); } else { log_->fatal( std::string( "Requested vessel information for ID: " ) + toStr( vesselID ) + ( "unknown" ) ); return NULL; } } - /*! This returns a vessel by it's given name \remarks The vessel needs to be loaded before \param vesselID The name of a vessel */ + /*! This returns a vessel by it's given name \remarks The vessel needs to be loaded before \param vesselName The name of a vessel */ Vessel * vessel( const std::string & vesselName ){ - if ( vessels_.count( vesselName ) ){ - return vessels_[ vesselName ]; + if ( templatesByName_.count( vesselName ) ){ + return static_cast<Vessel *>(templatesByName_[ vesselName ]); } else { log_->fatal( std::string( "Unknown vessel with name :" ) + vesselName ); return NULL; @@ -249,21 +85,14 @@ std::set < Vessel * > factionVessels( const std::string & factionName ){ std::set < Vessel * > vessels; - for ( std::map < std::string, Vessel * >::iterator it = vessels_.begin(); it != vessels_.end(); it ++ ){ - if ( it->second->factionName() == factionName || factionName == "All" ){ - vessels.insert( it->second ); + for ( std::map < std::string, EntityTemplate * >::iterator it = templatesByName_.begin(); it != templatesByName_.end(); it ++ ){ + if ( static_cast<Vessel *>(it->second)->factionName() == factionName || factionName == "All" ){ + vessels.insert( static_cast<Vessel *>(it->second) ); } } return vessels; } - inline std::set < std::string > factionNames( ) const { return factions_; } - -protected: - LogManager *log_; - std::map < std::string, Vessel * > vessels_; - std::map < int, Vessel * > vesselID_; - std::set < std::string > factions_; }; } //namespace OpenGate{ Added: branches/ogsector/src/VesselTemplate.h =================================================================== --- branches/ogsector/src/VesselTemplate.h (rev 0) +++ branches/ogsector/src/VesselTemplate.h 2007-05-20 19:13:41 UTC (rev 492) @@ -0,0 +1,184 @@ +/*************************************************************************** + * Copyright (C) 2006-2007 by OpenGate development team * + * spo...@us... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU General Public License as published by * + * the Free Software Foundation; either version 2 of the License, or * + * (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU General Public License * + * along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ + +#ifndef _OPENGATE_VESSEL__H +#define _OPENGATE_VESSEL__H + +#include "OgreMesh.h" +#include "OgreMeshManager.h" + +#include "Entity.h" + +namespace OpenGate{ + +/*! + * \brief This class defines a vessel. + * + * A vessel in opengate is that thing, that the user needs to fly around in + * space. Others might call it "starship", "spaceshit" or something like that. + * A vessel has several properties. It has a weight, a yaw-. a roll- and a + * pitch-latency. It also has a drag factor and many properties more. Each + * vessel must have these options. The data of a vessel is loaded from an XML + * document. This is more or less a logical storage for the vessel, not more and + * not less. + * \todo Check how to load the XML data into a vessel. My first approach was that + * a vessel was passed the name of the XML document and it load everything + * from that file. The better approach would be to create something like + * an XMLManager, that is able to load a vessel by it's name. + * \todo See how to attach objects (capacitory, power plants, weapons) to the + * vessel. + * \author Carsten <spo...@us...> + */ +class Vessel : public EntityTemplate{ +public: + + /*! Constructor creating a dummy vessel */ + Vessel(): EntityTemplate(){ + + vesselSize_ = 10.0; + vesselArmor_ = 1.0; + vesselYaw_ = 0; + + yaw_ = 1.0; + pitch_ = 1.0; + roll_ = 1.0; + dragFactor_ = 1.0; + + amountGunHardPoints_ = 0; + amountEngines_ = 0; + amountModX_ = 0; + amountMissiles_ = 0; + + shieldSize_ = 0; + radarSize_ = 0; + engineSize_ = 0; + capacitorSize_ = 0; + powerPlantSize_ = 0; + cargoSize_ = 0; + ecmSize_ = 0; + + } + + /*! Desctructor */ + ~Vessel(){} + + inline void setBaseSize( float vesselSize ){ vesselSize_ = vesselSize; } + inline float baseSize( ) const { return vesselSize_; } + + inline void setBaseYaw( float vesselYaw ){ vesselYaw_ = vesselYaw; } + inline float baseYaw( ) const { return vesselYaw_; } + + inline void setArmor( float vesselArmor ){ vesselArmor_ = vesselArmor; } + inline float armor( ) const { return vesselArmor_; } + + inline void setYaw( float yaw ){ + yaw_ = yaw; if ( yaw_ < 1.0 ) yaw_ = 1000.0 * ( 180.0 * yaw_ ) / 3.141592 ; + } + inline float yaw( ) const { return yaw_; } + + inline void setPitch( float pitch ){ + pitch_ = pitch; if ( pitch_ < 1.0 ) pitch_ = 1000.0 * ( 180.0 * pitch_ ) / 3.141592 ; + } + inline float pitch( ) const { return pitch_; } + + inline void setRoll( float roll ){ + roll_ = roll; if ( roll_ < 1.0 ) roll_ = 1000.0 * ( 180.0 * roll_ ) / 3.141592 ; + } + inline float roll( ) const { return roll_; } + + inline void setDragFactor( float drag ){ dragFactor_ = drag; } + inline float dragFactor( ) const { return dragFactor_; } + + inline void setAmountGunHardPoints( int amount ){ amountGunHardPoints_ = amount; } + inline int amountGunHardPoints( ) const { return amountGunHardPoints_; } + + inline void setAmountEngines( int amount ){ amountEngines_ = amount; } + inline int amountEngines( ) const { return amountEngines_; } + + inline void setAmountModX( int amount ){ amountModX_ = amount; } + inline int amountModX( ) const { return amountModX_; } + + inline void setAmountMissiles( int amount ){ amountMissiles_ = amount; } + inline int amountMissiles( ) const { return amountMissiles_; } + + inline void setShieldSize( int size ){ shieldSize_ = size; } + inline int shieldSize( ) const { return shieldSize_; } + + inline void setRadarSize( int size ){ radarSize_ = size; } + inline int radarSize( ) const { return radarSize_; } + + inline void setEngineSize( int size ){ engineSize_ = size; } + inline int engineSize( ) const { return engineSize_; } + + inline void setCapacitorSize( int size ){ capacitorSize_ = size; } + inline int capacitorSize( ) const { return capacitorSize_; } + + inline void setPowerPlantSize( int size ){ powerPlantSize_ = size; } + inline int powerPlantSize( ) const { return powerPlantSize_; } + + inline void setCargoSize( int size ){ cargoSize_ = size; } + inline int cargoSize( ) const { return cargoSize_; } + + inline void setEcmSize( int size ){ ecmSize_ = size; } + inline int ecmSize( ) const { return ecmSize_; } + + void setMesh( const std::string & meshName, bool genPath = true ){ + std::string fileName; + if ( genPath ){ + fileName = std::string("ships") + PATHSEPARATOR + factionName_ + PATHSEPARATOR + name_ + PATHSEPARATOR + meshName; + } else { + fileName = meshName; + } + pMesh_ = Ogre::MeshManager::getSingleton().load( fileName, "General" ); + // pMesh_ = Ogre::MeshManager::getSingleton().load( meshName, "../../trunk/data/ships/octavius/apteryx/" ); + } + Ogre::MeshPtr & meshPtr( ){ return pMesh_; } + +protected: + + float vesselSize_; + float vesselArmor_; + float vesselYaw_; + + float yaw_; + float pitch_; + float roll_; + float dragFactor_; + + int amountEngines_; + int amountGunHardPoints_; + int amountModX_; + int amountMissiles_; + + int cargoSize_; + + int capacitorSize_; + int powerPlantSize_; + int shieldSize_; + int radarSize_; + int engineSize_; + int ecmSize_; + + Ogre::MeshPtr pMesh_; +}; + +} + +#endif //_OPENGATE_VESSEL__H Property changes on: branches/ogsector/src/VesselTemplate.h ___________________________________________________________________ Name: svn:mime-type + text/x-c++hdr Name: svn:eol-style + native This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |