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_waypoint.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2008-09-01  |  16.4 KB  |  542 lines

  1. /***************************************************************************
  2.  * waypoint.cpp  -  waypoint class for the Overworld
  3.  *
  4.  * Copyright (C) 2003 - 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_waypoint.h"
  17. #include "../overworld/overworld.h"
  18. #include "../core/game_core.h"
  19. #include "../core/framerate.h"
  20. #include "../core/camera.h"
  21. #include "../user/preferences.h"
  22. #include "../video/renderer.h"
  23. #include "../video/gl_surface.h"
  24. #include "../level/level.h"
  25. #include "../core/math/utilities.h"
  26. #include "../core/i18n.h"
  27.  
  28. /* *** *** *** *** *** *** *** *** cWaypoint *** *** *** *** *** *** *** *** *** */
  29.  
  30. cWaypoint :: cWaypoint( void )
  31. : cSprite()
  32. {
  33.     Init();
  34. }
  35.  
  36. cWaypoint :: cWaypoint( CEGUI::XMLAttributes &attributes )
  37. : cSprite()
  38. {
  39.     Init();
  40.     Create_from_Stream( attributes );
  41. }
  42.  
  43.  
  44. cWaypoint :: ~cWaypoint( void )
  45. {
  46.     //
  47. }
  48.  
  49. void cWaypoint :: Init( void )
  50. {
  51.     sprite_array = ARRAY_PASSIVE;
  52.     type = TYPE_OW_WAYPOINT;
  53.     massivetype = MASS_PASSIVE;
  54.     posz = 0.08f;
  55.     player_range = 0;
  56.     
  57.     waypoint_type = WAYPOINT_NORMAL;
  58.     name = _("Waypoint");
  59.  
  60.     gcolor = Get_Random_Float( 0, 100 );
  61.     glim = 1;
  62.     
  63.     access = 0;
  64.     access_default = 0;
  65.  
  66.     direction_backward = DIR_UNDEFINED;
  67.     direction_forward = DIR_UNDEFINED;
  68.  
  69.     Set_Image( pVideo->Get_Surface( "world/waypoint/default_1.png" ) );
  70.  
  71.     arrow_blue_l = pVideo->Get_Surface( "game/arrow/small/blue/left.png" );
  72.     arrow_blue_r = pVideo->Get_Surface( "game/arrow/small/blue/right.png" );
  73.     arrow_blue_u = pVideo->Get_Surface( "game/arrow/small/blue/up.png" );
  74.     arrow_blue_d = pVideo->Get_Surface( "game/arrow/small/blue/down.png" );
  75.  
  76.     arrow_white_l = pVideo->Get_Surface( "game/arrow/small/white/left.png" );
  77.     arrow_white_r = pVideo->Get_Surface( "game/arrow/small/white/right.png" );
  78.     arrow_white_u = pVideo->Get_Surface( "game/arrow/small/white/up.png" );
  79.     arrow_white_d = pVideo->Get_Surface( "game/arrow/small/white/down.png" );
  80. }
  81.  
  82. cWaypoint *cWaypoint :: Copy( void )
  83. {
  84.     cWaypoint *waypoint = new cWaypoint();
  85.  
  86.     waypoint->Set_Pos( startposx, startposy, 1 );
  87.     waypoint->Set_Image( start_image, 1 );
  88.     waypoint->waypoint_type = waypoint_type;
  89.     waypoint->Set_Destination( destination );
  90.     waypoint->direction_backward = direction_backward;
  91.     waypoint->direction_forward = direction_forward;
  92.     waypoint->Set_Access( access_default, 1 );
  93.  
  94.     return waypoint;
  95. }
  96.  
  97. void cWaypoint :: Create_from_Stream( CEGUI::XMLAttributes &attributes )
  98. {
  99.     // position
  100.     Set_Pos( static_cast<float>(attributes.getValueAsInteger( "x" )), static_cast<float>(attributes.getValueAsInteger( "y" )), 1 );
  101.     // image
  102.     /*if( attributes.exists( "image" ) )
  103.     {
  104.         Set_Image( pVideo->Get_Surface( attributes.getValueAsString( "image" ).c_str() ), 1 ) ;
  105.     }*/
  106.     // type
  107.     waypoint_type = static_cast<Waypoint_type>(attributes.getValueAsInteger( "type", WAYPOINT_NORMAL ));
  108.     // destination
  109.     // pre 0.99.6 : world
  110.     if( attributes.exists( "world" ) )
  111.     {
  112.         Set_Destination( attributes.getValueAsString( "world" ).c_str() );
  113.     }
  114.     // pre 0.99.6 : level
  115.     else if( attributes.exists( "level" ) )
  116.     {
  117.         Set_Destination( attributes.getValueAsString( "level" ).c_str() );
  118.     }
  119.     // default : destination
  120.     else
  121.     {
  122.         Set_Destination( attributes.getValueAsString( "destination" ).c_str() );
  123.     }
  124.     // backward direction
  125.     direction_backward = Get_Direction_Id( attributes.getValueAsString( "direction_backward", "left" ).c_str() );
  126.     // forward direction
  127.     direction_forward = Get_Direction_Id( attributes.getValueAsString( "direction_forward", "right" ).c_str() );
  128.     // access
  129.     Set_Access( attributes.getValueAsBool( "access", 1 ), 1 );
  130. }
  131.  
  132. void cWaypoint :: Save_to_Stream( ofstream &file )
  133. {
  134.     // begin waypoint
  135.     file << "\t<waypoint>" << std::endl;
  136.  
  137.     // position
  138.     file << "\t\t<Property name=\"x\" value=\"" << static_cast<int>(startposx) << "\" />" << std::endl;
  139.     file << "\t\t<Property name=\"y\" value=\"" << static_cast<int>(startposy) << "\" />" << std::endl;
  140.     // image
  141.     /*if( start_image )
  142.     {
  143.         string img_filename = start_image->filename;
  144.  
  145.         // remove pixmaps directory from string
  146.         if( img_filename.find( PIXMAPS_DIR ) == 0 )
  147.         {
  148.             img_filename.erase( 0, strlen( PIXMAPS_DIR ) + 1 );
  149.         }
  150.  
  151.         file << "\t\t<Property name=\"image\" value=\"" << img_filename << "\" />" << std::endl;
  152.     }*/
  153.     // type
  154.     file << "\t\t<Property name=\"type\" value=\"" << static_cast<int>(waypoint_type) << "\" />" << std::endl;
  155.     // destination
  156.     file << "\t\t<Property name=\"destination\" value=\"" << destination << "\" />" << std::endl;
  157.     // direction backward
  158.     file << "\t\t<Property name=\"direction_backward\" value=\"" << Get_Direction_Name( direction_backward ) << "\" />" << std::endl;
  159.     // direction forward
  160.     file << "\t\t<Property name=\"direction_forward\" value=\"" << Get_Direction_Name( direction_forward ) << "\" />" << std::endl;
  161.     // access
  162.     file << "\t\t<Property name=\"access\" value=\"" << access_default << "\" />" << std::endl;
  163.  
  164.     // end waypoint
  165.     file << "\t</waypoint>" << std::endl;
  166. }
  167.  
  168. void cWaypoint :: Update( void )
  169. {
  170.     cSprite::Update();
  171.  
  172.     if( glim )
  173.     {
  174.         gcolor += pFramerate->speedfactor * 3;
  175.     }
  176.     else
  177.     {
  178.         gcolor -= pFramerate->speedfactor * 3;
  179.     }
  180.  
  181.     if( gcolor > 120 )
  182.     {
  183.         glim = 0;
  184.     }
  185.     else if( gcolor < 7 )
  186.     {
  187.         glim = 1;
  188.     }
  189. }
  190.  
  191. void cWaypoint :: Draw( cSurfaceRequest *request /* = NULL  */ )
  192. {    
  193.     if( pOverworld_Manager->debugmode || editor_world_enabled )
  194.     {
  195.         // direction back
  196.         float x = rect.x - pActive_Camera->x;
  197.         float y = rect.y - pActive_Camera->y;
  198.         
  199.  
  200.         if( direction_backward == DIR_RIGHT )
  201.         {
  202.             x += image->w;
  203.             y += (image->h * 0.5f) - (arrow_blue_l->w * 0.5f);
  204.  
  205.             // create request
  206.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  207.             // blit
  208.             arrow_blue_r->Blit( x, y, 0.089f, surface_request );
  209.             surface_request->shadow_pos = 2;
  210.             surface_request->shadow_color = lightgreyalpha64;
  211.             // add request
  212.             pRenderer->Add( surface_request );
  213.         }
  214.         else if( direction_backward == DIR_LEFT )
  215.         {
  216.             x -= arrow_blue_l->w;
  217.             y += (image->h * 0.5f) - (arrow_blue_l->w * 0.5f);
  218.  
  219.             // create request
  220.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  221.             // blit
  222.             arrow_blue_l->Blit( x, y, 0.089f, surface_request );
  223.             surface_request->shadow_pos = 2;
  224.             surface_request->shadow_color = lightgreyalpha64;
  225.             // add request
  226.             pRenderer->Add( surface_request );
  227.         }
  228.         else if( direction_backward == DIR_UP )
  229.         {
  230.             y -= arrow_blue_u->h;
  231.             x += (image->w * 0.5f) - (arrow_blue_u->h * 0.5f);
  232.  
  233.             // create request
  234.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  235.             // blit
  236.             arrow_blue_u->Blit( x, y, 0.089f, surface_request );
  237.             surface_request->shadow_pos = 2;
  238.             surface_request->shadow_color = lightgreyalpha64;
  239.             // add request
  240.             pRenderer->Add( surface_request );
  241.         }
  242.         else if( direction_backward == DIR_DOWN )
  243.         {
  244.             y += image->h;
  245.             x += (image->w * 0.5f) - (arrow_blue_u->h * 0.5f);
  246.  
  247.             // create request
  248.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  249.             // blit
  250.             arrow_blue_d->Blit( x, y, 0.089f, surface_request );
  251.             surface_request->shadow_pos = 2;
  252.             surface_request->shadow_color = lightgreyalpha64;
  253.             // add request
  254.             pRenderer->Add( surface_request );
  255.         }
  256.  
  257.         // direction forward
  258.         x = rect.x - pActive_Camera->x;
  259.         y = rect.y - pActive_Camera->y;
  260.  
  261.         if( direction_forward == DIR_RIGHT )
  262.         {
  263.             x += image->w;
  264.             y += (image->h * 0.5f) - (arrow_white_l->w * 0.5f);
  265.  
  266.             // create request
  267.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  268.             arrow_white_r->Blit( x, y, 0.089f, surface_request );
  269.             surface_request->shadow_pos = 2;
  270.             surface_request->shadow_color = lightgreyalpha64;
  271.             // add request
  272.             pRenderer->Add( surface_request );
  273.         }
  274.         else if( direction_forward == DIR_LEFT )
  275.         {
  276.             x -= arrow_white_l->w;
  277.             y += (image->h * 0.5f) - (arrow_white_l->w * 0.5f);
  278.  
  279.             // create request
  280.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  281.             arrow_white_l->Blit( x, y, 0.089f, surface_request );
  282.             surface_request->shadow_pos = 2;
  283.             surface_request->shadow_color = lightgreyalpha64;
  284.             // add request
  285.             pRenderer->Add( surface_request );
  286.         }
  287.         else if( direction_forward == DIR_UP )
  288.         {
  289.             y -= arrow_white_u->h;
  290.             x += (image->w * 0.5f) - (arrow_white_u->h * 0.5f);
  291.  
  292.             // create request
  293.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  294.             arrow_white_u->Blit( x, y, 0.089f, surface_request );
  295.             surface_request->shadow_pos = 2;
  296.             surface_request->shadow_color = lightgreyalpha64;
  297.             // add request
  298.             pRenderer->Add( surface_request );
  299.         }
  300.         else if( direction_forward == DIR_DOWN )
  301.         {
  302.             y += image->h;
  303.             x += (image->w * 0.5f) - (arrow_white_u->h * 0.5f);
  304.  
  305.             // create request
  306.             cSurfaceRequest *surface_request = new cSurfaceRequest();
  307.             arrow_white_d->Blit( x, y, 0.089f, surface_request );
  308.             surface_request->shadow_pos = 2;
  309.             surface_request->shadow_color = lightgreyalpha64;
  310.             // add request
  311.             pRenderer->Add( surface_request );
  312.         }
  313.     }
  314.  
  315.     // draw waypoint
  316.     if( ( access && ( waypoint_type == 1 || waypoint_type == 2 ) ) || pOverworld_Manager->debugmode || editor_world_enabled )
  317.     {
  318.         bool create_request = 0;
  319.  
  320.         if( !request )
  321.         {
  322.             create_request = 1;
  323.             // create request
  324.             request = new cSurfaceRequest();
  325.         }
  326.  
  327.         // draw
  328.         cSprite::Draw( request );
  329.  
  330.         // default color
  331.         if( !pOverworld_Manager->debugmode )
  332.         {
  333.             request->color = Color( static_cast<Uint8>(255), 100 + static_cast<Uint8>(gcolor), 10 );
  334.         }
  335.         // change to debug color
  336.         else
  337.         {
  338.             if( access )
  339.             {
  340.                 request->color = Color( static_cast<Uint8>(255), 100 + static_cast<Uint8>(gcolor), 200 );
  341.             }
  342.             else
  343.             {
  344.                 request->color =  Color( static_cast<Uint8>(20), 100 + static_cast<Uint8>(gcolor), 10 );
  345.             }
  346.         }
  347.  
  348.         if( create_request )
  349.         {
  350.             // add request
  351.             pRenderer->Add( request );
  352.         }
  353.     }
  354. }
  355.  
  356. void cWaypoint :: Set_Access( bool enabled, bool new_start_access /* = 0 */ )
  357. {
  358.     access = enabled;
  359.     
  360.     if( new_start_access )
  361.     {
  362.         access_default = access;
  363.     }
  364. }
  365.  
  366. void cWaypoint :: Set_Destination( string str )
  367. {
  368.     // normal waypoint
  369.     if( waypoint_type == WAYPOINT_NORMAL )
  370.     {
  371.         // erase file type and directory  if set
  372.         str = Get_Filename( str, 0, 0 );
  373.     }
  374.     // world waypoint
  375.     else if( waypoint_type == WAYPOINT_WORLD_LINK && str.find( DATA_DIR "/" GAME_OVERWORLD_DIR "/" ) != string::npos )
  376.     {
  377.         str.erase( 0, strlen( DATA_DIR "/" GAME_OVERWORLD_DIR "/" ) );
  378.     }
  379.  
  380.     destination = str;
  381. }
  382.  
  383. string cWaypoint :: Get_Destination( bool with_dir /* = 0 */, bool with_end /* = 0 */ )
  384. {
  385.     string name = destination;
  386.  
  387.     if( waypoint_type == WAYPOINT_NORMAL )
  388.     {
  389.         pActive_Level->Get_Path( name );
  390.         name = Get_Filename( name, with_dir, with_end );
  391.     }
  392.     else if( waypoint_type == WAYPOINT_WORLD_LINK )
  393.     {
  394.         if( with_dir && name.find( DATA_DIR "/" GAME_OVERWORLD_DIR "/" ) == string::npos )
  395.         {
  396.             name.insert( 0, DATA_DIR "/" GAME_OVERWORLD_DIR "/" );
  397.         }
  398.     }
  399.  
  400.     return name;
  401. }
  402.  
  403. void cWaypoint :: Editor_Activate( void )
  404. {
  405.     CEGUI::WindowManager &wmgr = CEGUI::WindowManager::getSingleton();
  406.  
  407.     // Type
  408.     CEGUI::Combobox *combobox = static_cast<CEGUI::Combobox *>(wmgr.createWindow( "TaharezLook/Combobox", "waypoint_type" ));
  409.     Editor_Add( UTF8_("Type"), UTF8_("Destination type"), combobox, 120, 80 );
  410.  
  411.     combobox->addItem( new CEGUI::ListboxTextItem( UTF8_("Level") ) );
  412.     combobox->addItem( new CEGUI::ListboxTextItem( UTF8_("World") ) );
  413.  
  414.     if( waypoint_type == WAYPOINT_NORMAL )
  415.     {
  416.         combobox->setText( UTF8_("Level") );
  417.     }
  418.     else
  419.     {
  420.         combobox->setText( UTF8_("World") );
  421.     }
  422.  
  423.     combobox->subscribeEvent( CEGUI::Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber( &cWaypoint::Editor_Type_Select, this ) );
  424.  
  425.  
  426.     // destination
  427.     CEGUI::Editbox *editbox = static_cast<CEGUI::Editbox *>(wmgr.createWindow( "TaharezLook/Editbox", "waypoint_destination" ));
  428.     Editor_Add( UTF8_("Destination"), UTF8_("Destination"), editbox, 150 );
  429.  
  430.     editbox->setText( Get_Destination() );
  431.     editbox->subscribeEvent( CEGUI::Editbox::EventKeyUp, CEGUI::Event::Subscriber( &cWaypoint::Editor_Destination_Key, this ) );
  432.  
  433.     // backward direction
  434.     combobox = static_cast<CEGUI::Combobox *>(wmgr.createWindow( "TaharezLook/Combobox", "waypoint_backward_direction" ));
  435.     Editor_Add( UTF8_("Backward Direction"), UTF8_("Backward Direction"), combobox, 100, 105 );
  436.  
  437.     combobox->addItem( new CEGUI::ListboxTextItem( "up" ) );
  438.     combobox->addItem( new CEGUI::ListboxTextItem( "down" ) );
  439.     combobox->addItem( new CEGUI::ListboxTextItem( "right" ) );
  440.     combobox->addItem( new CEGUI::ListboxTextItem( "left" ) );
  441.     combobox->setText( Get_Direction_Name( direction_backward ) );
  442.  
  443.     combobox->subscribeEvent( CEGUI::Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber( &cWaypoint::Editor_Backward_Direction_Select, this ) );
  444.  
  445.     // forward direction
  446.     combobox = static_cast<CEGUI::Combobox *>(wmgr.createWindow( "TaharezLook/Combobox", "waypoint_forward_direction" ));
  447.     Editor_Add( UTF8_("Forward Direction"), UTF8_("Forward Direction"), combobox, 100, 105 );
  448.  
  449.     combobox->addItem( new CEGUI::ListboxTextItem( "up" ) );
  450.     combobox->addItem( new CEGUI::ListboxTextItem( "down" ) );
  451.     combobox->addItem( new CEGUI::ListboxTextItem( "right" ) );
  452.     combobox->addItem( new CEGUI::ListboxTextItem( "left" ) );
  453.     combobox->setText( Get_Direction_Name( direction_forward ) );
  454.  
  455.     combobox->subscribeEvent( CEGUI::Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber( &cWaypoint::Editor_Forward_Direction_Select, this ) );
  456.  
  457.     // Access
  458.     combobox = static_cast<CEGUI::Combobox *>(wmgr.createWindow( "TaharezLook/Combobox", "waypoint_access" ));
  459.     Editor_Add( UTF8_("Default Access"), UTF8_("Default Access"), combobox, 120, 80 );
  460.  
  461.     combobox->addItem( new CEGUI::ListboxTextItem( UTF8_("Enabled") ) );
  462.     combobox->addItem( new CEGUI::ListboxTextItem( UTF8_("Disabled") ) );
  463.  
  464.     if( access_default == 1 )
  465.     {
  466.         combobox->setText( UTF8_("Enabled") );
  467.     }
  468.     else
  469.     {
  470.         combobox->setText( UTF8_("Disabled") );
  471.     }
  472.  
  473.     combobox->subscribeEvent( CEGUI::Combobox::EventListSelectionAccepted, CEGUI::Event::Subscriber( &cWaypoint::Editor_Access_Select, this ) );
  474.  
  475.     // init
  476.     Editor_Init();
  477. }
  478.  
  479. bool cWaypoint :: Editor_Type_Select( const CEGUI::EventArgs &event )
  480. {
  481.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  482.     CEGUI::ListboxItem *item = static_cast<CEGUI::Combobox *>( windowEventArgs.window )->getSelectedItem();
  483.  
  484.     if( item->getText().compare( UTF8_("Level") ) == 0 )
  485.     {
  486.         waypoint_type = WAYPOINT_NORMAL;
  487.     }
  488.     else
  489.     {
  490.         waypoint_type = WAYPOINT_WORLD_LINK;
  491.     }
  492.  
  493.     return 1;
  494.  
  495. }
  496. bool cWaypoint :: Editor_Destination_Key( const CEGUI::EventArgs &event )
  497. {
  498.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  499.     string str_text = static_cast<CEGUI::Editbox *>( windowEventArgs.window )->getText().c_str();
  500.  
  501.     Set_Destination( str_text );
  502.  
  503.     return 1;
  504. }
  505.  
  506. bool cWaypoint :: Editor_Backward_Direction_Select( const CEGUI::EventArgs &event )
  507. {
  508.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  509.     CEGUI::ListboxItem *item = static_cast<CEGUI::Combobox *>( windowEventArgs.window )->getSelectedItem();
  510.  
  511.     direction_backward = Get_Direction_Id( item->getText().c_str() );
  512.  
  513.     return 1;
  514. }
  515.  
  516. bool cWaypoint :: Editor_Forward_Direction_Select( const CEGUI::EventArgs &event )
  517. {
  518.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  519.     CEGUI::ListboxItem *item = static_cast<CEGUI::Combobox *>( windowEventArgs.window )->getSelectedItem();
  520.  
  521.     direction_forward = Get_Direction_Id( item->getText().c_str() );
  522.  
  523.     return 1;
  524. }
  525.  
  526. bool cWaypoint :: Editor_Access_Select( const CEGUI::EventArgs &event )
  527. {
  528.     const CEGUI::WindowEventArgs &windowEventArgs = static_cast<const CEGUI::WindowEventArgs&>( event );
  529.     CEGUI::ListboxItem *item = static_cast<CEGUI::Combobox *>( windowEventArgs.window )->getSelectedItem();
  530.  
  531.     if( item->getText().compare( UTF8_("Enabled") ) == 0 )
  532.     {
  533.         Set_Access( 1, 1 );
  534.     }
  535.     else
  536.     {
  537.         Set_Access( 0, 1 );
  538.     }
  539.  
  540.     return 1;
  541. }
  542.