home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2009 February / maximum-cd-2009-02.iso / DiscContents / SMC_1.6_win32.exe / src / overworld / world_manager.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-07-01  |  6.6 KB  |  289 lines

  1. /***************************************************************************
  2.  * worlds.cpp  -  class for handling worlds data
  3.  *
  4.  * Copyright (C) 2004 - 2008 Florian Richter
  5.  ***************************************************************************/
  6. /*
  7.    This program is free software; you can redistribute it and/or modify
  8.    it under the terms of the GNU General Public License as published by
  9.    the Free Software Foundation; either version 3 of the License, or
  10.    (at your option) any later version.
  11.    
  12.    You should have received a copy of the GNU General Public License
  13.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  14. */
  15.  
  16. #include "../overworld/world_manager.h"
  17. #include "../core/game_core.h"
  18. #include "../overworld/overworld.h"
  19. // boost filesystem
  20. #include "boost/filesystem/convenience.hpp"
  21. namespace fs = boost::filesystem;
  22.  
  23. /* *** *** *** *** *** *** *** *** cOverworld_Manager *** *** *** *** *** *** *** *** *** */
  24.  
  25. cOverworld_Manager :: cOverworld_Manager( void )
  26. : cObject_Manager<cOverworld>()
  27. {
  28.     worlds_filename = DATA_DIR "/" GAME_OVERWORLD_DIR "/worlds.xml";
  29.  
  30.     debugmode = 0;
  31.     draw_layer = 0;
  32.     cameramode = 0;
  33.  
  34.     camera = new cCamera();
  35.  
  36.     Load();
  37. }
  38.  
  39. cOverworld_Manager :: ~cOverworld_Manager( void )
  40. {
  41.     Delete_All();
  42.  
  43.     delete camera;
  44. }
  45.  
  46. bool cOverworld_Manager :: New( string name )
  47. {
  48.     // no name given
  49.     if( name.empty() )
  50.     {
  51.         return 0;
  52.     }
  53.  
  54.     // already available
  55.     if( fs::exists( fs::path( user_data_dir + USER_WORLD_DIR + "/" + name, fs::native ) ) )
  56.     {
  57.         return 0;
  58.     }
  59.  
  60.     cOverworld *overworld = new cOverworld();
  61.  
  62.     // set path
  63.     overworld->description->path = name;
  64.     // default name is the path
  65.     overworld->description->name = name;
  66.  
  67.     objects.push_back( overworld );
  68.  
  69.     pActive_Overworld = overworld;
  70.  
  71.     pOverworld_Player->Reset();
  72.  
  73.     return 1;
  74. }
  75.  
  76. void cOverworld_Manager :: Load( void )
  77. {
  78.     // if already loaded
  79.     if( objects.size() )
  80.     {
  81.         Delete_All();
  82.     }
  83.  
  84.     // Load Worlds
  85.     Load_Dir( user_data_dir + USER_WORLD_DIR, 1 );
  86.     Load_Dir( DATA_DIR "/" GAME_OVERWORLD_DIR );
  87.  
  88.     // Get Overworld User Comments
  89.     if( File_Exists( worlds_filename ) )
  90.     {
  91.         // Parse
  92.         CEGUI::System::getSingleton().getXMLParser()->parseXMLFile( *this, worlds_filename.c_str(), DATA_DIR "/" GAME_SCHEMA_DIR "/Worlds_User_Data.xsd", "" );
  93.     }
  94.     else
  95.     {
  96.         // filename not valid
  97.         printf( "Warning : Couldn't open Worlds description file : %s\n", worlds_filename.c_str() );
  98.     }
  99.  
  100.     // set default overworld
  101.     Set_Active( "World 1" );
  102. }
  103.  
  104. void cOverworld_Manager :: Load_Dir( string dir, bool user_dir /* = 0 */ ) 
  105. {
  106.     // set world directory
  107.     fs::path full_path( dir, fs::native );
  108.     fs::directory_iterator end_iter;
  109.  
  110.     for( fs::directory_iterator dir_itr( full_path ); dir_itr != end_iter; ++dir_itr )
  111.     {
  112.         try
  113.         {
  114.             string current_dir = dir_itr->leaf();
  115.  
  116.             // only directories with an existing description
  117.             if( fs::is_directory( *dir_itr ) && File_Exists( dir + "/" + current_dir + "/description.xml" ) )
  118.             {
  119.                 // already available
  120.                 if( Get_from_Path( current_dir ) )
  121.                 {
  122.                     continue;
  123.                 }
  124.  
  125.                 cOverworld *overworld = new cOverworld();
  126.  
  127.                 // set path
  128.                 overworld->description->path = current_dir;
  129.                 // default name is the path
  130.                 overworld->description->name = current_dir;
  131.                 // set user
  132.                 overworld->description->user = user_dir;
  133.  
  134.                 objects.push_back( overworld );
  135.  
  136.                 overworld->Load();
  137.             }
  138.         }
  139.         catch( const std::exception &ex )
  140.         {
  141.             printf( "%s %s\n", dir_itr->leaf().c_str(), ex.what() );
  142.         }
  143.     }
  144. }
  145.  
  146. bool cOverworld_Manager :: Set_Active( string str ) 
  147. {
  148.     return Set_Active( Get( str ) );
  149. }
  150.  
  151. bool cOverworld_Manager :: Set_Active( cOverworld *world )
  152. {
  153.     if( !world )
  154.     {
  155.         return 0;
  156.     }
  157.  
  158.     pActive_Overworld = world;
  159.  
  160.     // set player start waypoint
  161.     pOverworld_Player->Reset();
  162.     pOverworld_Player->Set_Waypoint( pActive_Overworld->player_start_waypoint, 1 );
  163.  
  164.     return 1;
  165. }
  166.  
  167. void cOverworld_Manager :: Reset( void )
  168. {
  169.     // default Overworld
  170.     Set_Active( "World 1" );
  171.     
  172.     // Set Player to first Waypoint
  173.     pOverworld_Player->Set_Waypoint( pActive_Overworld->player_start_waypoint );
  174.  
  175.     // Reset all Waypoints
  176.     for( vector<cOverworld *>::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  177.     {
  178.         (*itr)->Reset_Waypoints();
  179.     }
  180. }
  181.  
  182. cOverworld *cOverworld_Manager :: Get( string str )
  183. {
  184.     cOverworld *world = Get_from_Name( str );
  185.  
  186.     if( world )
  187.     {
  188.         return world;
  189.     }
  190.  
  191.     return Get_from_Path( str );
  192. }
  193.  
  194. cOverworld *cOverworld_Manager :: Get_from_Path( string path )
  195. {
  196.     for( vector<cOverworld *>::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  197.     {
  198.         cOverworld *obj = (*itr);
  199.  
  200.         if( obj->description->path.compare( path ) == 0 )
  201.         {
  202.             return obj;
  203.         }
  204.     }
  205.  
  206.     return NULL;
  207. }
  208.  
  209. cOverworld *cOverworld_Manager :: Get_from_Name( string name )
  210. {
  211.     for( vector<cOverworld *>::iterator itr = objects.begin(), itr_end = objects.end(); itr != itr_end; ++itr )
  212.     {
  213.         cOverworld *obj = (*itr);
  214.  
  215.         if( obj->description->name.compare( name ) == 0 )
  216.         {
  217.             return obj;
  218.         }
  219.     }
  220.  
  221.     return NULL;
  222. }
  223.  
  224. int cOverworld_Manager :: Get_Array_Num( string path )
  225. {
  226.     for( unsigned int i = 0; i < objects.size(); i++ )
  227.     {
  228.         if( objects[i]->description->path.compare( path ) == 0 )
  229.         {
  230.             return i;
  231.         }
  232.     }
  233.  
  234.     return -1;
  235. }
  236.  
  237. // XML element start
  238. void cOverworld_Manager :: elementStart( const CEGUI::String &element, const CEGUI::XMLAttributes &attributes )
  239. {
  240.     // Property of an Element
  241.     if( element == "Property" )
  242.     {
  243.         xml_attributes.add( attributes.getValueAsString( "Name" ), attributes.getValueAsString( "Value" ) );
  244.     }
  245. }
  246.  
  247. // XML element end
  248. void cOverworld_Manager :: elementEnd( const CEGUI::String &element )
  249. {
  250.     if( element != "Property" )
  251.     {
  252.         if( element == "World" )
  253.         {
  254.             handle_world( xml_attributes );
  255.         }
  256.         else if( element == "Worlds" )
  257.         {
  258.             // ignore
  259.         }
  260.         else if( element.length() )
  261.         {
  262.             printf( "Warning : Overworld Description Unknown element : %s\n", element.c_str() );
  263.         }
  264.  
  265.         // clear
  266.         xml_attributes = CEGUI::XMLAttributes();
  267.     }
  268. }
  269.  
  270. void cOverworld_Manager :: handle_world( const CEGUI::XMLAttributes &attributes )
  271. {
  272.     string ow_name = attributes.getValueAsString( "Name" ).c_str();
  273.     string ow_comment = attributes.getValueAsString( "Comment" ).c_str();
  274.  
  275.     // if available
  276.     cOverworld *overworld = Get_from_Name( ow_name );
  277.  
  278.     // set comment
  279.     if( overworld )
  280.     {
  281.         overworld->description->comment = ow_comment;
  282.     }
  283. }
  284.  
  285. /* *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** */
  286.  
  287. // Overworld information handler
  288. cOverworld_Manager *pOverworld_Manager = NULL;
  289.