2009-05-20 13:08:56 +03:00
# include "../stdafx.h"
2008-08-02 18:08:03 +03:00
# include "CPreGame.h"
2009-05-20 13:08:56 +03:00
# include "../hch/CDefHandler.h"
2009-01-12 22:05:56 +02:00
# include <ctime>
2008-12-21 21:17:35 +02:00
# include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
# include <boost/algorithm/string.hpp>
# include <zlib.h>
2009-05-20 13:08:56 +03:00
# include "../timeHandler.h"
2008-08-02 18:08:03 +03:00
# include <sstream>
# include "SDL_Extensions.h"
# include "CGameInfo.h"
2009-05-20 13:08:56 +03:00
# include "../hch/CGeneralTextHandler.h"
2008-08-02 18:08:03 +03:00
# include "CCursorHandler.h"
2009-05-20 13:08:56 +03:00
# include "../hch/CLodHandler.h"
# include "../hch/CTownHandler.h"
# include "../hch/CHeroHandler.h"
2008-08-02 18:08:03 +03:00
# include <cmath>
2009-05-20 13:08:56 +03:00
# include "Graphics.h"
2009-08-17 11:50:31 +03:00
//#include <boost/thread.hpp>
2008-08-02 18:08:03 +03:00
# include <boost/bind.hpp>
2008-12-21 21:17:35 +02:00
# include <cstdlib>
2009-05-20 13:08:56 +03:00
# include "../lib/Connection.h"
# include "../hch/CMusicHandler.h"
2009-06-24 09:56:36 +03:00
# include "../hch/CVideoHandler.h"
2009-08-17 11:50:31 +03:00
# include "AdventureMapButton.h"
2009-04-15 17:03:31 +03:00
/*
* CPreGame . cpp , part of VCMI engine
*
* Authors : listed in file AUTHORS in main folder
*
* License : GNU General Public License v2 .0 or later
* Full text of license available in license . txt file , in main folder
*
*/
2009-08-17 13:47:08 +03:00
namespace fs = boost : : filesystem ;
2009-08-17 11:50:31 +03:00
void startGame ( StartInfo * options ) ;
2009-04-15 17:03:31 +03:00
2009-08-17 11:50:31 +03:00
CGPreGame * CGP ;
static const CMapInfo * curMap = NULL ;
static StartInfo * curOpts = NULL ;
static int playerColor ;
2008-12-21 21:17:35 +02:00
2009-08-17 11:50:31 +03:00
CMenuScreen : : CMenuScreen ( EState which )
2009-02-06 13:15:39 +02:00
{
2009-08-17 11:50:31 +03:00
OBJ_CONSTRUCTION ;
bgAd = NULL ;
switch ( which )
2009-02-06 13:15:39 +02:00
{
2009-08-17 11:50:31 +03:00
case mainMenu :
2009-02-06 13:15:39 +02:00
{
2009-08-17 11:50:31 +03:00
buttons [ 0 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 3 ] . second , bind ( & CMenuScreen : : moveTo , this , ref ( CGP - > scrs [ newGame ] ) ) , 540 , 10 , " ZMENUNG.DEF " , SDLK_n ) ;
buttons [ 1 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 4 ] . second , bind ( & CMenuScreen : : moveTo , this , ref ( CGP - > scrs [ loadGame ] ) ) , 532 , 132 , " ZMENULG.DEF " , SDLK_l ) ;
buttons [ 2 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 5 ] . second , 0 /*cb*/ , 524 , 251 , " ZMENUHS.DEF " , SDLK_h ) ;
buttons [ 3 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 6 ] . second , 0 /*cb*/ , 557 , 359 , " ZMENUCR.DEF " , SDLK_c ) ;
buttons [ 4 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 7 ] . second , bind ( std : : exit , 0 ) , 586 , 468 , " ZMENUQT.DEF " , SDLK_ESCAPE ) ;
2009-02-06 13:15:39 +02:00
}
2009-08-17 11:50:31 +03:00
break ;
case newGame :
2009-02-06 13:15:39 +02:00
{
2009-08-17 11:50:31 +03:00
bgAd = new CPicture ( BitmapHandler : : loadBitmap ( " ZNEWGAM.bmp " ) , 114 , 312 , true ) ;
buttons [ 0 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 10 ] . second , bind ( & CGPreGame : : openSel , CGP , newGame ) , 545 , 4 , " ZTSINGL.DEF " , SDLK_s ) ;
buttons [ 1 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 11 ] . second , 0 /*cb*/ , 568 , 120 , " ZTMULTI.DEF " , SDLK_m ) ;
buttons [ 2 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 12 ] . second , 0 /*cb*/ , 541 , 233 , " ZTCAMPN.DEF " , SDLK_c ) ;
buttons [ 3 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 13 ] . second , 0 /*cb*/ , 545 , 358 , " ZTTUTOR.DEF " , SDLK_t ) ;
buttons [ 4 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 14 ] . second , bind ( & CMenuScreen : : moveTo , this , CGP - > scrs [ mainMenu ] ) , 582 , 464 , " ZTBACK.DEF " , SDLK_ESCAPE ) ;
}
break ;
case loadGame :
{
bgAd = new CPicture ( BitmapHandler : : loadBitmap ( " ZLOADGAM.bmp " ) , 114 , 312 , true ) ;
buttons [ 0 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 10 ] . second , bind ( & CGPreGame : : openSel , CGP , loadGame ) , 545 , 4 , " ZTSINGL.DEF " , SDLK_s ) ;
buttons [ 1 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 11 ] . second , 0 /*cb*/ , 568 , 120 , " ZTMULTI.DEF " , SDLK_m ) ;
buttons [ 2 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 12 ] . second , 0 /*cb*/ , 541 , 233 , " ZTCAMPN.DEF " , SDLK_c ) ;
buttons [ 3 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 13 ] . second , 0 /*cb*/ , 545 , 358 , " ZTTUTOR.DEF " , SDLK_t ) ;
buttons [ 4 ] = new AdventureMapButton ( " " , CGI - > generaltexth - > zelp [ 14 ] . second , bind ( & CMenuScreen : : moveTo , this , CGP - > scrs [ mainMenu ] ) , 582 , 464 , " ZTBACK.DEF " , SDLK_ESCAPE ) ;
2009-02-06 13:15:39 +02:00
}
2009-08-17 11:50:31 +03:00
break ;
2009-02-06 13:15:39 +02:00
}
2009-08-17 11:50:31 +03:00
for ( int i = 0 ; i < ARRAY_COUNT ( buttons ) ; i + + )
buttons [ i ] - > hoverable = true ;
2009-02-06 13:15:39 +02:00
}
2009-08-17 11:50:31 +03:00
CMenuScreen : : ~ CMenuScreen ( )
2008-08-06 01:11:32 +03:00
{
2009-08-17 11:50:31 +03:00
}
void CMenuScreen : : showAll ( SDL_Surface * to )
2008-08-06 01:11:32 +03:00
{
2009-08-17 11:50:31 +03:00
blitAt ( CGP - > mainbg , 0 , 0 , to ) ;
CIntObject : : showAll ( to ) ;
2008-08-06 01:11:32 +03:00
}
2009-08-17 11:50:31 +03:00
void CMenuScreen : : show ( SDL_Surface * to )
2009-01-25 18:19:34 +02:00
{
2009-08-17 11:50:31 +03:00
CIntObject : : show ( to ) ;
CGI - > videoh - > update ( pos . x + 8 , pos . y + 105 , screen , true , false ) ;
2009-01-25 18:19:34 +02:00
}
2009-08-17 11:50:31 +03:00
void CMenuScreen : : moveTo ( CMenuScreen * next )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
GH . popInt ( this ) ;
GH . pushInt ( next ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void CGPreGame : : run ( )
2009-01-25 18:19:34 +02:00
{
2009-08-17 11:50:31 +03:00
GH . handleEvents ( ) ;
# ifdef _WIN32
CGI - > videoh - > open ( " ACREDIT.SMK " ) ;
# else
CGI - > videoh - > open ( " ACREDIT.SMK " , true , false ) ;
# endif
GH . pushInt ( scrs [ mainMenu ] ) ;
while ( 1 )
2009-01-25 18:19:34 +02:00
{
2009-08-17 11:50:31 +03:00
CGI - > curh - > draw1 ( ) ;
SDL_Flip ( screen ) ;
CGI - > curh - > draw2 ( ) ;
SDL_Delay ( 20 ) ; //give time for other apps
GH . topInt ( ) - > show ( screen ) ;
GH . updateTime ( ) ;
GH . handleEvents ( ) ;
2009-01-25 18:19:34 +02:00
}
}
2009-08-17 11:50:31 +03:00
CGPreGame : : CGPreGame ( )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
GH . defActionsDef = 31 ;
CGP = this ;
mainbg = BitmapHandler : : loadBitmap ( " ZPIC1005.bmp " ) ;
for ( int i = 0 ; i < ARRAY_COUNT ( scrs ) ; i + + )
scrs [ i ] = new CMenuScreen ( ( EState ) i ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
CGPreGame : : ~ CGPreGame ( )
2008-08-06 01:11:32 +03:00
{
2009-08-17 11:50:31 +03:00
SDL_FreeSurface ( mainbg ) ;
for ( int i = 0 ; i < ARRAY_COUNT ( scrs ) ; i + + )
delete scrs [ i ] ;
2008-08-06 01:11:32 +03:00
}
2009-08-17 11:50:31 +03:00
void CGPreGame : : openSel ( EState type )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
GH . pushInt ( new CSelectionScreen ( type ) ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void CGPreGame : : loadGraphics ( )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
victory = CDefHandler : : giveDef ( " SCNRVICT.DEF " ) ;
loss = CDefHandler : : giveDef ( " SCNRLOSS.DEF " ) ;
bonuses = CDefHandler : : giveDef ( " SCNRSTAR.DEF " ) ;
rHero = BitmapHandler : : loadBitmap ( " HPSRAND1.bmp " ) ;
rTown = BitmapHandler : : loadBitmap ( " HPSRAND0.bmp " ) ;
nHero = BitmapHandler : : loadBitmap ( " HPSRAND6.bmp " ) ;
nTown = BitmapHandler : : loadBitmap ( " HPSRAND5.bmp " ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void CGPreGame : : disposeGraphics ( )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
delete victory ;
delete loss ;
SDL_FreeSurface ( rHero ) ;
SDL_FreeSurface ( nHero ) ;
SDL_FreeSurface ( rTown ) ;
SDL_FreeSurface ( nTown ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
CSelectionScreen : : CSelectionScreen ( EState Type )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
OBJ_CONSTRUCTION ;
CGP - > loadGraphics ( ) ;
type = Type ;
curOpts = & sInfo ;
current = NULL ;
sInfo . mode = ( Type = = newGame ? 0 : 1 ) ;
sInfo . turnTime = 0 ;
curTab = NULL ;
bg = new CPicture ( BitmapHandler : : loadBitmap ( rand ( ) % 2 ? " ZPIC1000.bmp " : " ZPIC1001.bmp " ) , 0 , 0 , true ) ;
card = new InfoCard ( type ) ;
opt = new OptionsTab ( type /*, sInfo*/ ) ;
opt - > recActions = DISPOSE ;
sel = new SelectionTab ( type , bind ( & CSelectionScreen : : changeSelection , this , _1 ) ) ;
sel - > recActions = DISPOSE ;
switch ( type )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
case newGame :
{
card - > difficulty - > onChange = bind ( & CSelectionScreen : : difficultyChange , this , _1 ) ;
card - > difficulty - > select ( 1 , 0 ) ;
AdventureMapButton * select = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 45 ] , bind ( & CSelectionScreen : : toggleTab , this , sel ) , 414 , 81 , " GSPBUTT.DEF " , SDLK_s ) ;
select - > addTextOverlay ( CGI - > generaltexth - > allTexts [ 500 ] , FONT_SMALL ) ;
AdventureMapButton * opts = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 46 ] , bind ( & CSelectionScreen : : toggleTab , this , opt ) , 414 , 509 , " GSPBUTT.DEF " , SDLK_a ) ;
opts - > addTextOverlay ( CGI - > generaltexth - > allTexts [ 501 ] , FONT_SMALL ) ;
AdventureMapButton * random = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 47 ] , bind ( & CSelectionScreen : : toggleTab , this , sel ) , 414 , 105 , " GSPBUTT.DEF " , SDLK_r ) ;
random - > addTextOverlay ( CGI - > generaltexth - > allTexts [ 740 ] , FONT_SMALL ) ;
start = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 103 ] , bind ( & CSelectionScreen : : startGame , this ) , 414 , 535 , " SCNRBEG.DEF " , SDLK_b ) ;
}
break ;
case loadGame :
card - > difficulty - > select ( current - > seldiff , 0 ) ;
sel - > recActions = 255 ;
start = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 103 ] , bind ( & CSelectionScreen : : startGame , this ) , 414 , 535 , " SCNRLOD.DEF " , SDLK_b ) ;
break ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
back = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 105 ] , bind ( & CGuiHandler : : popIntTotally , & GH , this ) , 584 , 535 , " SCNRBACK.DEF " , SDLK_ESCAPE ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
CSelectionScreen : : ~ CSelectionScreen ( )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void CSelectionScreen : : toggleTab ( CIntObject * tab )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( curTab & & curTab - > active )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
curTab - > deactivate ( ) ;
curTab - > recActions = DISPOSE ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
if ( curTab ! = tab )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
tab - > recActions = 255 ;
tab - > activate ( ) ;
curTab = tab ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
else
{
curTab = NULL ;
} ;
GH . totalRedraw ( ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void CSelectionScreen : : changeSelection ( const CMapInfo * to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
curMap = current = to ;
updateStartInfo ( to ) ;
card - > changeSelection ( to ) ;
opt - > changeSelection ( to ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void CSelectionScreen : : updateStartInfo ( const CMapInfo * to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
sInfo . mapname = to - > filename ;
sInfo . playerInfos . clear ( ) ;
sInfo . playerInfos . resize ( to - > playerAmnt ) ;
playerColor = - 1 ;
2008-08-02 18:08:03 +03:00
2009-08-17 11:50:31 +03:00
int serialC = 0 ;
for ( int i = 0 ; i < PLAYER_LIMIT ; i + + )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
const PlayerInfo & pinfo = to - > players [ i ] ;
//neither computer nor human can play - no player
if ( ! ( pinfo . canComputerPlay | | pinfo . canComputerPlay ) )
continue ;
PlayerSettings & pset = sInfo . playerInfos [ serialC ] ;
pset . color = i ;
pset . serial = serialC + + ;
if ( pinfo . canHumanPlay & & playerColor < 0 )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
pset . name = CGI - > generaltexth - > allTexts [ 434 ] ; //Player
pset . human = true ;
playerColor = i ;
2009-05-29 04:50:33 +03:00
}
2009-08-17 11:50:31 +03:00
else
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
pset . name = CGI - > generaltexth - > allTexts [ 468 ] ; //Computer
pset . human = false ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
for ( int j = 0 ; j < F_NUMBER & & pset . castle ! = - 1 ; j + + ) //we start with none and find matching faction. if more than one, then set to random
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( ( 1 < < j ) & pinfo . allowedFactions )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( pset . castle > = 0 ) //we've already assigned a castle and another one is possible -> set random and let player choose
pset . castle = - 1 ; //breaks
if ( pset . castle = = - 2 ) //first available castle - pick
pset . castle = j ;
2008-08-02 18:08:03 +03:00
}
}
2009-08-17 11:50:31 +03:00
if ( ( pinfo . generateHeroAtMainTown | | to - > version = = CMapHeader : : RoE ) & & pinfo . hasMainTown //we will generate hero in front of main town
| | pinfo . p8 ) //random hero
pset . hero = - 1 ;
else
pset . hero = - 2 ;
2008-08-02 18:08:03 +03:00
2009-08-17 11:50:31 +03:00
if ( pinfo . mainHeroName . length ( ) )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
pset . heroName = pinfo . mainHeroName ;
if ( ( pset . heroPortrait = pinfo . mainHeroPortrait ) = = 255 )
pset . heroPortrait = pinfo . p9 ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
pset . handicap = 0 ;
}
2008-08-02 18:08:03 +03:00
}
2008-11-16 03:06:15 +02:00
2009-08-17 11:50:31 +03:00
void CSelectionScreen : : startGame ( )
2008-08-06 01:11:32 +03:00
{
2009-08-17 11:50:31 +03:00
if ( ! current )
return ;
//CGP->disposeGraphics();
StartInfo * si = new StartInfo ( sInfo ) ;
GH . popIntTotally ( this ) ;
GH . popIntTotally ( GH . topInt ( ) ) ;
curMap = NULL ;
curOpts = NULL ;
: : startGame ( si ) ;
delete si ; //rather won't be called...
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void CSelectionScreen : : difficultyChange ( int to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
assert ( type = = newGame ) ;
sInfo . difficulty = to ;
GH . totalRedraw ( ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
// A new size filter (Small, Medium, ...) has been selected. Populate
// selMaps with the relevant data.
void SelectionTab : : filter ( int size )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
curItems . clear ( ) ;
for ( size_t i = 0 ; i < allItems . size ( ) ; i + + )
if ( allItems [ i ] . version & & ( ! size | | allItems [ i ] . width = = size ) )
curItems . push_back ( & allItems [ i ] ) ;
if ( curItems . size ( ) )
{
slider - > block ( false ) ;
slider - > setAmount ( curItems . size ( ) ) ;
sort ( ) ;
slider - > moveTo ( 0 ) ;
onSelect ( curItems [ 0 ] ) ;
}
else
{
slider - > block ( true ) ;
onSelect ( NULL ) ;
}
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void SelectionTab : : getFiles ( std : : vector < FileInfo > & out , const std : : string & dirname , const std : : string & ext )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( ! boost : : filesystem : : exists ( dirname ) )
{
tlog1 < < " Cannot find " < < dirname < < " directory! \n " ;
}
fs : : path tie ( ( fs : : initial_path < fs : : path > ( ) ) / dirname ) ;
fs : : directory_iterator end_iter ;
for ( fs : : directory_iterator file ( tie ) ; file ! = end_iter ; + + file )
{
if ( fs : : is_regular_file ( file - > status ( ) )
& & boost : : ends_with ( file - > path ( ) . filename ( ) , ext ) )
{
out . resize ( out . size ( ) + 1 ) ;
out . back ( ) . date = fs : : last_write_time ( file - > path ( ) ) ;
out . back ( ) . name = dirname + " / " + ( file - > path ( ) . leaf ( ) ) ;
}
}
allItems . resize ( out . size ( ) ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void SelectionTab : : parseMaps ( std : : vector < FileInfo > & files , int start , int threads )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
int read = 0 ;
unsigned char mapBuffer [ 1500 ] ;
while ( start < allItems . size ( ) )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
gzFile tempf = gzopen ( files [ start ] . name . c_str ( ) , " rb " ) ;
read = gzread ( tempf , mapBuffer , 1500 ) ;
gzclose ( tempf ) ;
if ( read < 50 | | ! mapBuffer [ 4 ] )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
tlog3 < < " \t \t Warning: corrupted map file: " < < files [ start ] . name < < std : : endl ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
else //valid map
{
allItems [ start ] . init ( files [ start ] . name , mapBuffer ) ;
//allItems[start].date = "DATEDATE";// files[start].date;
}
start + = threads ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
}
void SelectionTab : : parseGames ( std : : vector < FileInfo > & files )
{
for ( int i = 0 ; i < files . size ( ) ; i + + )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
CLoadFile lf ( files [ i ] . name ) ;
if ( ! lf . sfile )
continue ;
ui8 sign [ 8 ] ;
lf > > sign ;
if ( std : : memcmp ( sign , " VCMISVG " , 7 ) )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
tlog1 < < files [ i ] . name < < " is not a correct savefile! " < < std : : endl ;
continue ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
lf > > static_cast < CMapHeader & > ( allItems [ i ] ) > > allItems [ i ] . seldiff ;
allItems [ i ] . filename = files [ i ] . name ;
allItems [ i ] . countPlayers ( ) ;
allItems [ i ] . date = std : : asctime ( std : : localtime ( & files [ i ] . date ) ) ;
2008-08-02 18:08:03 +03:00
}
}
2009-02-06 13:15:39 +02:00
2009-08-17 11:50:31 +03:00
SelectionTab : : SelectionTab ( EState Type , const boost : : function < void ( CMapInfo * ) > & OnSelect )
: onSelect ( OnSelect )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
OBJ_CONSTRUCTION ;
selectionPos = 0 ;
used = LCLICK ;
slider = NULL ;
type = Type ;
std : : vector < FileInfo > toParse ;
switch ( type )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
case newGame :
getFiles ( toParse , " Maps " , " h3m " ) ;
parseMaps ( toParse ) ;
positions = 18 ;
break ;
2009-02-06 13:15:39 +02:00
2009-08-17 11:50:31 +03:00
case loadGame :
case saveGame :
getFiles ( toParse , " Games " , " vlgm1 " ) ;
parseGames ( toParse ) ;
if ( type = = loadGame )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
positions = 18 ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
else
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
positions = 16 ;
}
break ;
2009-02-08 19:19:42 +02:00
2009-08-17 11:50:31 +03:00
default :
assert ( 0 ) ;
}
2009-02-08 19:19:42 +02:00
2009-08-17 11:50:31 +03:00
bg = new CPicture ( BitmapHandler : : loadBitmap ( " SCSELBCK.bmp " ) , 3 , 6 , true ) ;
pos = bg - > pos ;
2009-02-08 19:19:42 +02:00
2009-08-17 11:50:31 +03:00
//size filter buttons
{
int sizes [ ] = { 36 , 72 , 108 , 144 , 0 } ;
const char * names [ ] = { " SCSMBUT.DEF " , " SCMDBUT.DEF " , " SCLGBUT.DEF " , " SCXLBUT.DEF " , " SCALBUT.DEF " } ;
for ( int i = 0 ; i < 5 ; i + + )
new AdventureMapButton ( CGI - > generaltexth - > zelp [ 54 + i ] , bind ( & SelectionTab : : filter , this , sizes [ i ] ) , 161 + 47 * i , 52 , names [ i ] ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
2009-02-11 19:03:30 +02:00
{
2009-08-17 11:50:31 +03:00
int xpos [ ] = { 26 , 58 , 91 , 124 , 309 , 342 } ;
const char * names [ ] = { " SCBUTT1.DEF " , " SCBUTT2.DEF " , " SCBUTCP.DEF " , " SCBUTT3.DEF " , " SCBUTT4.DEF " , " SCBUTT5.DEF " } ;
for ( int i = 0 ; i < 6 ; i + + )
new AdventureMapButton ( CGI - > generaltexth - > zelp [ 107 + i ] , bind ( & SelectionTab : : sortBy , this , i ) , xpos [ i ] , 92 , names [ i ] ) ;
2009-02-11 19:03:30 +02:00
}
2009-08-17 11:50:31 +03:00
slider = new CSlider ( 375 , 92 , 480 , bind ( & SelectionTab : : sliderMove , this , _1 ) , positions , curItems . size ( ) , 0 , false , 1 ) ;
format = CDefHandler : : giveDef ( " SCSELC.DEF " ) ;
sortingBy = _format ;
ascending = true ;
filter ( 0 ) ;
select ( 0 ) ;
}
SelectionTab : : ~ SelectionTab ( )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
delete format ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void SelectionTab : : sortBy ( int criteria )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( criteria = = sortingBy )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
ascending = ! ascending ;
2008-08-02 18:08:03 +03:00
}
else
{
2009-08-17 11:50:31 +03:00
sortingBy = ( ESortBy ) criteria ;
ascending = true ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
sort ( ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void SelectionTab : : sort ( )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( sortingBy ! = _name )
std : : stable_sort ( curItems . begin ( ) , curItems . end ( ) , mapSorter ( _name ) ) ;
std : : stable_sort ( curItems . begin ( ) , curItems . end ( ) , mapSorter ( sortingBy ) ) ;
2009-06-10 23:48:55 +03:00
2009-08-17 11:50:31 +03:00
if ( ! ascending )
std : : reverse ( curItems . begin ( ) , curItems . end ( ) ) ;
2009-06-10 23:48:55 +03:00
2009-08-17 11:50:31 +03:00
redraw ( ) ;
}
2009-06-10 23:48:55 +03:00
2009-08-17 11:50:31 +03:00
void SelectionTab : : select ( int position )
{
// New selection. py is the index in curItems.
int py = position + slider - > value ;
if ( py < curItems . size ( ) )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
CGI - > soundh - > playSound ( soundBase : : button ) ;
selectionPos = py ;
2009-02-06 13:15:39 +02:00
2009-08-17 11:50:31 +03:00
onSelect ( curItems [ py ] ) ;
2008-08-02 18:08:03 +03:00
}
}
2009-08-17 11:50:31 +03:00
int SelectionTab : : getPosition ( int x , int y )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
return - 1 ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void SelectionTab : : sliderMove ( int slidPos )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( ! slider ) return ; //ignore spurious call when slider is being created
redraw ( ) ;
2008-08-02 18:08:03 +03:00
}
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
2009-05-04 02:38:08 +03:00
// Display the tab with the scenario names
//
// elemIdx is the index of the maps or saved game to display on line 0
2009-08-17 11:50:31 +03:00
// slider->capacity contains the number of available screen lines
// slider->positionsAmnt is the number of elements after filtering
void SelectionTab : : printMaps ( SDL_Surface * to )
2008-08-02 18:08:03 +03:00
{
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
int elemIdx = slider - > value ;
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
// Display all elements if there's enough space
//if(slider->amount < slider->capacity)
// elemIdx = 0;
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
SDL_Color nasz ;
# define POS(xx, yy) pos.x + xx, pos.y + yy + line*25
for ( int line = 0 ; line < positions & & elemIdx < curItems . size ( ) ; elemIdx + + , line + + )
{
CMapInfo * curMap = curItems [ elemIdx ] ;
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
if ( elemIdx = = selectionPos )
2008-08-02 18:08:03 +03:00
nasz = tytulowy ;
2009-05-04 02:38:08 +03:00
else
nasz = zwykly ;
std : : ostringstream ostr ( std : : ostringstream : : out ) ;
ostr < < curMap - > playerAmnt < < " / " < < curMap - > humenPlayers ;
2009-08-17 11:50:31 +03:00
CSDL_Ext : : printAt ( ostr . str ( ) , POS ( 29 , 120 ) , FONT_SMALL , nasz , to ) ;
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
std : : string temp2 = " C " ;
2009-05-04 02:38:08 +03:00
switch ( curMap - > width )
2008-08-02 18:08:03 +03:00
{
case 36 :
temp2 = " S " ;
break ;
case 72 :
temp2 = " M " ;
break ;
case 108 :
temp2 = " L " ;
break ;
case 144 :
temp2 = " XL " ;
break ;
}
2009-08-17 11:50:31 +03:00
CSDL_Ext : : printAtMiddle ( temp2 , POS ( 70 , 128 ) , FONT_SMALL , nasz , to ) ;
2009-05-04 02:38:08 +03:00
int temp = - 1 ;
switch ( curMap - > version )
2008-08-02 18:08:03 +03:00
{
2009-05-07 20:20:41 +03:00
case CMapHeader : : RoE :
2008-08-02 18:08:03 +03:00
temp = 0 ;
break ;
2009-05-07 20:20:41 +03:00
case CMapHeader : : AB :
2008-08-02 18:08:03 +03:00
temp = 1 ;
break ;
2009-05-07 20:20:41 +03:00
case CMapHeader : : SoD :
2008-08-02 18:08:03 +03:00
temp = 2 ;
break ;
2009-05-07 20:20:41 +03:00
case CMapHeader : : WoG :
2008-08-02 18:08:03 +03:00
temp = 3 ;
break ;
2009-05-04 02:38:08 +03:00
default :
// Unknown version. Be safe and ignore that map
tlog2 < < " Warning: " < < curMap - > filename < < " has wrong version! \n " ;
continue ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
blitAt ( format - > ourImages [ temp ] . bitmap , POS ( 88 , 117 ) , to ) ;
2009-01-11 00:08:18 +02:00
2009-08-17 11:50:31 +03:00
if ( type = = newGame )
2009-01-11 00:08:18 +02:00
{
2009-05-04 02:38:08 +03:00
if ( ! ( curMap - > name . length ( ) ) )
curMap - > name = " Unnamed " ;
2009-08-17 11:50:31 +03:00
CSDL_Ext : : printAtMiddle ( curMap - > name , POS ( 213 , 128 ) , FONT_SMALL , nasz , to ) ;
2009-01-11 00:08:18 +02:00
}
else
{
2009-05-04 02:38:08 +03:00
std : : string & name = curMap - > filename ;
2009-08-17 11:50:31 +03:00
CSDL_Ext : : printAtMiddle ( name . substr ( 6 , name . size ( ) - 12 ) , POS ( 213 , 128 ) , FONT_SMALL , nasz , to ) ;
2009-01-11 00:08:18 +02:00
}
2009-05-04 02:38:08 +03:00
if ( curMap - > victoryCondition . condition = = winStandard )
2009-08-17 11:50:31 +03:00
temp = 11 ;
2009-01-06 20:42:20 +02:00
else
2009-08-17 11:50:31 +03:00
temp = curMap - > victoryCondition . condition ;
blitAt ( CGP - > victory - > ourImages [ temp ] . bitmap , POS ( 306 , 117 ) , to ) ;
2009-01-06 20:42:20 +02:00
2009-05-04 02:38:08 +03:00
if ( curMap - > lossCondition . typeOfLossCon = = lossStandard )
2008-08-02 18:08:03 +03:00
temp = 3 ;
2009-01-06 20:42:20 +02:00
else
2009-05-04 02:38:08 +03:00
temp = curMap - > lossCondition . typeOfLossCon ;
2009-08-17 11:50:31 +03:00
blitAt ( CGP - > loss - > ourImages [ temp ] . bitmap , POS ( 339 , 117 ) , to ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
# undef POS
2008-08-02 18:08:03 +03:00
}
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
void SelectionTab : : showAll ( SDL_Surface * to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
CIntObject : : showAll ( to ) ;
printMaps ( to ) ;
CSDL_Ext : : printAtMiddle ( CGI - > generaltexth - > arraytxt [ type = = newGame ? 229 : 230 ] , 208 , 34 , FONT_MEDIUM , tytulowy , to ) ; //Select a Scenario to Play
CSDL_Ext : : printAtMiddle ( CGI - > generaltexth - > allTexts [ 510 ] , 90 , 68 , FONT_SMALL , tytulowy , to ) ; //Map sizes
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void SelectionTab : : clickLeft ( tribool down , bool previousState )
2009-01-11 00:08:18 +02:00
{
2009-08-17 11:50:31 +03:00
Point clickPos ( GH . current - > button . x , GH . current - > button . y ) ;
clickPos - = pos . topLeft ( ) ;
2009-01-11 00:08:18 +02:00
2009-08-17 11:50:31 +03:00
if ( clickPos . y > 115 & & clickPos . y < 564 & & clickPos . x > 52 & & clickPos . x < 366 )
2009-01-11 00:08:18 +02:00
{
2009-08-17 11:50:31 +03:00
int line = ( clickPos . y - 115 ) / 25 ; //which line
select ( line ) ;
2009-06-02 01:31:11 +03:00
2009-01-11 00:08:18 +02:00
}
}
2009-02-08 08:42:15 +02:00
2009-08-17 11:50:31 +03:00
InfoCard : : InfoCard ( EState Type )
2009-02-08 08:42:15 +02:00
{
2009-08-17 11:50:31 +03:00
OBJ_CONSTRUCTION ;
sizes = CDefHandler : : giveDef ( " SCNRMPSZ.DEF " ) ;
2008-08-02 18:08:03 +03:00
sFlags = CDefHandler : : giveDef ( " ITGFLAGS.DEF " ) ;
2009-08-17 11:50:31 +03:00
type = Type ;
bg = new CPicture ( BitmapHandler : : loadBitmap ( " GSELPOP1.bmp " ) , 396 , 6 , true ) ;
pos = bg - > pos ;
difficulty = new CHighlightableButtonsGroup ( 0 ) ;
2009-01-12 22:05:56 +02:00
{
2009-08-17 11:50:31 +03:00
BLOCK_CAPTURING ;
difficulty - > addButton ( new CHighlightableButton ( CGI - > generaltexth - > zelp [ 24 ] , 0 , 506 , 456 , " GSPBUT3.DEF " , 0 ) ) ;
difficulty - > addButton ( new CHighlightableButton ( CGI - > generaltexth - > zelp [ 25 ] , 0 , 538 , 456 , " GSPBUT4.DEF " , 1 ) ) ;
difficulty - > addButton ( new CHighlightableButton ( CGI - > generaltexth - > zelp [ 26 ] , 0 , 570 , 456 , " GSPBUT5.DEF " , 2 ) ) ;
difficulty - > addButton ( new CHighlightableButton ( CGI - > generaltexth - > zelp [ 27 ] , 0 , 602 , 456 , " GSPBUT6.DEF " , 3 ) ) ;
difficulty - > addButton ( new CHighlightableButton ( CGI - > generaltexth - > zelp [ 28 ] , 0 , 634 , 456 , " GSPBUT7.DEF " , 4 ) ) ;
2009-01-12 22:05:56 +02:00
}
2008-08-02 18:08:03 +03:00
}
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
InfoCard : : ~ InfoCard ( )
2009-05-29 04:50:33 +03:00
{
2009-08-17 11:50:31 +03:00
delete sizes ;
delete sFlags ;
2008-08-02 18:08:03 +03:00
}
2009-05-29 04:50:33 +03:00
2009-08-17 11:50:31 +03:00
void InfoCard : : showAll ( SDL_Surface * to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
CIntObject : : showAll ( to ) ;
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
//blit texts
printAtLoc ( CGI - > generaltexth - > allTexts [ 390 ] + " : " , 24 , 400 , FONT_SMALL , zwykly , to ) ; //Allies
printAtLoc ( CGI - > generaltexth - > allTexts [ 391 ] + " : " , 190 , 400 , FONT_SMALL , zwykly , to ) ; //Enemies
printAtLoc ( CGI - > generaltexth - > allTexts [ 494 ] , 33 , 430 , FONT_SMALL , tytulowy , to ) ; //"Map Diff:"
printAtLoc ( CGI - > generaltexth - > allTexts [ 492 ] + " : " , 133 , 430 , FONT_SMALL , tytulowy , to ) ; //player difficulty
printAtLoc ( CGI - > generaltexth - > allTexts [ 218 ] + " : " , 290 , 430 , FONT_SMALL , tytulowy , to ) ; //"Rating:"
printAtLoc ( CGI - > generaltexth - > allTexts [ 495 ] , 26 , 22 , FONT_SMALL , tytulowy , to ) ; //Scenario Name:
printAtLoc ( CGI - > generaltexth - > allTexts [ 496 ] , 26 , 132 , FONT_SMALL , tytulowy , to ) ; //Scenario Description:
printAtLoc ( CGI - > generaltexth - > allTexts [ 497 ] , 26 , 283 , FONT_SMALL , tytulowy , to ) ; //Victory Condition:
printAtLoc ( CGI - > generaltexth - > allTexts [ 498 ] , 26 , 339 , FONT_SMALL , tytulowy , to ) ; //Loss Condition:
if ( curMap )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( type ! = newGame )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
for ( int i = 0 ; i < difficulty - > buttons . size ( ) ; i + + )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( i = = curMap - > difficulty )
difficulty - > buttons [ i ] - > state = 3 ;
else
difficulty - > buttons [ i ] - > state = 2 ;
2008-08-02 18:08:03 +03:00
2009-08-17 11:50:31 +03:00
difficulty - > buttons [ i ] - > showAll ( to ) ;
2008-08-02 18:08:03 +03:00
}
2009-05-04 02:38:08 +03:00
}
2008-08-02 18:08:03 +03:00
2009-08-17 11:50:31 +03:00
int temp = curMap - > victoryCondition . condition + 1 ;
2009-05-04 02:38:08 +03:00
if ( temp > 20 ) temp = 0 ;
std : : string sss = CGI - > generaltexth - > victoryConditions [ temp ] ;
2009-08-17 11:50:31 +03:00
if ( temp & & curMap - > victoryCondition . allowNormalVictory ) sss + = " / " + CGI - > generaltexth - > victoryConditions [ 0 ] ;
printAtLoc ( sss , 60 , 307 , FONT_SMALL , zwykly , to ) ;
2008-08-02 18:08:03 +03:00
2009-08-17 11:50:31 +03:00
temp = curMap - > lossCondition . typeOfLossCon + 1 ;
2009-05-04 02:38:08 +03:00
if ( temp > 20 ) temp = 0 ;
sss = CGI - > generaltexth - > lossCondtions [ temp ] ;
2009-08-17 11:50:31 +03:00
printAtLoc ( sss , 60 , 366 , FONT_SMALL , zwykly , to ) ;
2008-08-02 18:08:03 +03:00
2009-05-29 04:50:33 +03:00
//blit description
2009-08-17 11:50:31 +03:00
std : : vector < std : : string > * desc = CMessage : : breakText ( curMap - > description , 52 ) ;
for ( int i = 0 ; i < desc - > size ( ) ; i + + )
printAtLoc ( ( * desc ) [ i ] , 26 , 149 + i * 16 , FONT_SMALL , zwykly , to ) ;
delete desc ;
2009-01-12 22:05:56 +02:00
2009-08-17 11:50:31 +03:00
if ( curMap - > name . length ( ) )
printAtLoc ( curMap - > name , 26 , 39 , FONT_BIG , tytulowy , to ) ;
else
printAtLoc ( " Unnamed " , 26 , 39 , FONT_BIG , tytulowy , to ) ;
2009-06-10 22:44:09 +03:00
2009-08-17 11:50:31 +03:00
assert ( curMap - > difficulty < = 4 ) ;
std : : string & diff = CGI - > generaltexth - > arraytxt [ 142 + curMap - > difficulty ] ;
printAtMiddleLoc ( diff , 62 , 472 , FONT_SMALL , zwykly , to ) ;
2009-06-10 22:44:09 +03:00
2009-08-17 11:50:31 +03:00
switch ( curMap - > width )
2009-05-04 02:38:08 +03:00
{
case 36 :
temp = 0 ;
break ;
case 72 :
temp = 1 ;
break ;
case 108 :
temp = 2 ;
break ;
case 144 :
temp = 3 ;
break ;
default :
temp = 4 ;
break ;
}
2009-08-17 11:50:31 +03:00
blitAtLoc ( sizes - > ourImages [ temp ] . bitmap , 318 , 22 , to ) ;
temp = curMap - > victoryCondition . condition ;
2009-05-04 02:38:08 +03:00
if ( temp > 12 ) temp = 11 ;
2009-08-17 11:50:31 +03:00
blitAt ( CGP - > victory - > ourImages [ temp ] . bitmap , 420 , 308 , to ) ; //vicotry cond descr
temp = curMap - > lossCondition . typeOfLossCon ;
2009-05-04 02:38:08 +03:00
if ( temp > 12 ) temp = 3 ;
2009-08-17 11:50:31 +03:00
blitAt ( CGP - > loss - > ourImages [ temp ] . bitmap , 420 , 365 , to ) ; //loss cond
2009-01-11 00:08:18 +02:00
2009-08-17 11:50:31 +03:00
if ( type = = loadGame )
printToLoc ( curMap - > date , 308 , 34 , FONT_SMALL , zwykly , to ) ;
2009-01-11 00:08:18 +02:00
2009-08-17 11:50:31 +03:00
//print flags
int fx = 64 , ex = 244 , myT ;
if ( curMap - > howManyTeams )
myT = curMap - > players [ playerColor ] . team ;
else
myT = - 1 ;
for ( std : : vector < PlayerSettings > : : const_iterator i = curOpts - > playerInfos . begin ( ) ; i ! = curOpts - > playerInfos . end ( ) ; i + + )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
int * myx = ( ( i - > color = = playerColor | | curMap - > players [ i - > color ] . team = = myT ) ? & fx : & ex ) ;
blitAtLoc ( sFlags - > ourImages [ i - > color ] . bitmap , * myx , 399 , to ) ;
* myx + = sFlags - > ourImages [ i - > color ] . bitmap - > w ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
std : : string tob ;
switch ( type ! = newGame ? curMap - > seldiff : curOpts - > difficulty )
2008-08-02 18:08:03 +03:00
{
case 0 :
2009-08-17 11:50:31 +03:00
tob = " 80% " ;
2008-08-02 18:08:03 +03:00
break ;
case 1 :
2009-08-17 11:50:31 +03:00
tob = " 100% " ;
2008-08-02 18:08:03 +03:00
break ;
case 2 :
2009-08-17 11:50:31 +03:00
tob = " 130% " ;
2008-08-02 18:08:03 +03:00
break ;
case 3 :
2009-08-17 11:50:31 +03:00
tob = " 160% " ;
2008-08-02 18:08:03 +03:00
break ;
case 4 :
2009-08-17 11:50:31 +03:00
tob = " 200% " ;
2008-08-02 18:08:03 +03:00
break ;
}
2009-08-17 11:50:31 +03:00
printAtMiddleLoc ( tob , 311 , 472 , FONT_SMALL , zwykly , to ) ;
2008-08-02 18:08:03 +03:00
}
}
2009-05-04 02:38:08 +03:00
2009-08-17 11:50:31 +03:00
void InfoCard : : changeSelection ( const CMapInfo * to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
//current = to;
GH . totalRedraw ( ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
OptionsTab : : OptionsTab ( EState Type /*, StartInfo &Opts */ )
//:opts(Opts)
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
OBJ_CONSTRUCTION ;
bg = new CPicture ( BitmapHandler : : loadBitmap ( " ADVOPTBK.bmp " ) , 3 , 6 , true ) ;
pos = bg - > pos ;
2008-08-02 18:08:03 +03:00
2009-08-17 11:50:31 +03:00
turnDuration = new CSlider ( pos . x + 55 , pos . y + 551 , 194 , bind ( & OptionsTab : : setTurnLength , this , _1 ) , 1 , 11 , 11 , true , 1 ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
OptionsTab : : ~ OptionsTab ( )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
void OptionsTab : : showAll ( SDL_Surface * to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
CIntObject : : showAll ( to ) ;
printAtMiddleLoc ( CGI - > generaltexth - > allTexts [ 515 ] , 222 , 30 , FONT_BIG , tytulowy , to ) ;
printAtMiddleWBLoc ( CGI - > generaltexth - > allTexts [ 516 ] , 222 , 58 , FONT_SMALL , 55 , zwykly , to ) ; //Select starting options, handicap, and name for each player in the game.
printAtMiddleWBLoc ( CGI - > generaltexth - > allTexts [ 517 ] , 107 , 102 , FONT_SMALL , 14 , tytulowy , to ) ; //Player Name Handicap Type
printAtMiddleWBLoc ( CGI - > generaltexth - > allTexts [ 518 ] , 197 , 102 , FONT_SMALL , 10 , tytulowy , to ) ; //Starting Town
printAtMiddleWBLoc ( CGI - > generaltexth - > allTexts [ 519 ] , 273 , 102 , FONT_SMALL , 10 , tytulowy , to ) ; //Starting Hero
printAtMiddleWBLoc ( CGI - > generaltexth - > allTexts [ 520 ] , 349 , 102 , FONT_SMALL , 10 , tytulowy , to ) ; //Starting Bonus
printAtMiddleLoc ( CGI - > generaltexth - > allTexts [ 521 ] , 222 , 538 , FONT_SMALL , tytulowy , to ) ; // Player Turn Duration
if ( curOpts - > turnTime )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
std : : ostringstream os ;
os < < ( int ) curOpts - > turnTime < < " Minutes " ;
printAtMiddleLoc ( os . str ( ) , 319 , 559 , FONT_SMALL , zwykly , to ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
else
printAtMiddleLoc ( " Unlimited " , 319 , 559 , FONT_SMALL , zwykly , to ) ;
}
void OptionsTab : : nextCastle ( int player , int dir )
{
si32 & cur = curOpts - > playerInfos [ player ] . castle ;
ui32 allowed = curMap - > players [ curOpts - > playerInfos [ player ] . color ] . allowedFactions ;
if ( cur = = - 2 ) //no castle - no change
return ;
if ( cur = = - 1 ) //random => first/last available
{
int pom = ( dir > 0 ) ? ( 0 ) : ( F_NUMBER - 1 ) ; // last or first
for ( ; pom > = 0 & & pom < F_NUMBER ; pom + = dir )
{
if ( ( 1 < < pom ) & allowed )
{
cur = pom ;
break ;
}
}
}
else // next/previous available
{
for ( ; ; )
{
cur + = dir ;
if ( ( 1 < < cur ) & allowed )
break ;
if ( cur > = F_NUMBER | | cur < 0 )
{
double p1 = log ( ( double ) allowed ) / log ( 2.0f ) + 0.000001f ;
double check = p1 - ( ( int ) p1 ) ;
if ( check < 0.001 )
cur = ( int ) p1 ;
else
cur = - 1 ;
break ;
}
}
}
redraw ( ) ;
}
void OptionsTab : : nextHero ( int player , int dir )
{
PlayerSettings & s = curOpts - > playerInfos [ player ] ;
int old = s . hero ;
if ( s . castle < 0 | | ! s . human | | s . hero = = - 2 )
return ;
2008-08-02 18:08:03 +03:00
2009-08-17 11:50:31 +03:00
if ( s . hero = = - 1 ) //random => first/last available
2009-02-11 19:48:40 +02:00
{
2009-08-17 11:50:31 +03:00
int max = ( s . castle * HEROES_PER_TYPE * 2 + 15 ) ,
min = ( s . castle * HEROES_PER_TYPE * 2 ) ;
s . hero = nextAllowedHero ( min , max , 0 , dir ) ;
2009-02-11 19:48:40 +02:00
}
2009-08-17 11:50:31 +03:00
else
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( dir > 0 )
s . hero = nextAllowedHero ( s . hero , ( s . castle * HEROES_PER_TYPE * 2 + 16 ) , 1 , dir ) ;
else
s . hero = nextAllowedHero ( s . castle * HEROES_PER_TYPE * 2 - 1 , s . hero , 1 , dir ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
if ( old ! = s . hero )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
usedHeroes . erase ( old ) ;
usedHeroes . insert ( s . hero ) ;
redraw ( ) ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
}
int OptionsTab : : nextAllowedHero ( int min , int max , int incl , int dir )
{
if ( dir > 0 )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
for ( int i = min + incl ; i < = max - incl ; i + + )
if ( canUseThisHero ( i ) )
return i ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
else
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
for ( int i = max - incl ; i > = min + incl ; i - - )
if ( canUseThisHero ( i ) )
return i ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
return - 1 ;
2008-08-02 18:08:03 +03:00
}
2009-05-01 19:14:29 +03:00
2009-08-17 11:50:31 +03:00
bool OptionsTab : : canUseThisHero ( int ID )
{
//for(int i=0;i<CPG->ret.playerInfos.size();i++)
// if(CPG->ret.playerInfos[i].hero==ID) //hero is already taken
// return false;
2009-06-24 09:56:36 +03:00
2009-08-17 11:50:31 +03:00
return ! vstd : : contains ( usedHeroes , ID ) & & curMap - > allowedHeroes [ ID ] ;
}
2009-02-11 19:03:30 +02:00
2009-08-17 11:50:31 +03:00
void OptionsTab : : nextBonus ( int player , int dir )
{
PlayerSettings & s = curOpts - > playerInfos [ player ] ;
si8 & ret = s . bonus + = dir ;
2009-06-24 09:56:36 +03:00
2009-08-17 11:50:31 +03:00
if ( s . hero = = - 2 & & ! curMap - > players [ s . color ] . heroesNames . size ( ) & & ret = = bartifact ) //no hero - can't be artifact
{
if ( dir < 0 )
ret = brandom ;
else ret = bgold ;
2008-08-02 18:08:03 +03:00
}
2009-06-28 02:33:59 +03:00
2009-08-17 11:50:31 +03:00
if ( ret > bresource )
ret = brandom ;
if ( ret < brandom )
ret = bresource ;
2009-06-24 09:56:36 +03:00
2009-08-17 11:50:31 +03:00
if ( s . castle = = - 1 & & ret = = bresource ) //random castle - can't be resource
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
if ( dir < 0 )
ret = bgold ;
else ret = brandom ;
2008-08-02 18:08:03 +03:00
}
2009-05-29 04:54:45 +03:00
2009-08-17 11:50:31 +03:00
redraw ( ) ;
2008-08-02 18:08:03 +03:00
}
2009-05-29 04:54:45 +03:00
2009-08-17 11:50:31 +03:00
void OptionsTab : : changeSelection ( const CMapInfo * to )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
for ( int i = 0 ; i < entries . size ( ) ; i + + )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
children - = entries [ i ] ;
delete entries [ i ] ;
2008-08-02 18:08:03 +03:00
}
2009-08-17 11:50:31 +03:00
entries . clear ( ) ;
usedHeroes . clear ( ) ;
OBJ_CONSTRUCTION ;
for ( int i = 0 ; i < curOpts - > playerInfos . size ( ) ; i + + )
2008-08-02 18:08:03 +03:00
{
2009-08-17 11:50:31 +03:00
entries . push_back ( new PlayerOptionsEntry ( this , curOpts - > playerInfos [ i ] ) ) ;
const std : : vector < SheroName > & heroes = curMap - > players [ curOpts - > playerInfos [ i ] . color ] . heroesNames ;
for ( size_t hi = 0 ; hi < heroes . size ( ) ; hi + + )
usedHeroes . insert ( heroes [ hi ] . heroID ) ;
2008-08-02 18:08:03 +03:00
}
}
2008-11-16 03:06:15 +02:00
2009-08-17 11:50:31 +03:00
void OptionsTab : : setTurnLength ( int npos )
2008-11-16 03:06:15 +02:00
{
2009-08-17 11:50:31 +03:00
static const int times [ ] = { 1 , 2 , 4 , 6 , 8 , 10 , 15 , 20 , 25 , 30 , 0 } ;
amin ( npos , ARRAY_COUNT ( times ) - 1 ) ;
curOpts - > turnTime = times [ npos ] ;
redraw ( ) ;
2008-11-16 03:06:15 +02:00
}
2009-08-17 11:50:31 +03:00
void OptionsTab : : flagPressed ( int player )
2008-11-16 03:06:15 +02:00
{
2009-08-17 11:50:31 +03:00
PlayerSettings & s = curOpts - > playerInfos [ player ] ;
std : : swap ( curOpts - > playerInfos [ playerColor ] . human , s . human ) ;
std : : swap ( curOpts - > playerInfos [ playerColor ] . name , s . name ) ;
playerColor = s . color ;
GH . totalRedraw ( ) ;
2008-11-16 03:06:15 +02:00
}
2009-08-17 11:50:31 +03:00
OptionsTab : : PlayerOptionsEntry : : PlayerOptionsEntry ( OptionsTab * owner , PlayerSettings & S )
: s ( S )
2008-11-16 03:06:15 +02:00
{
2009-08-17 11:50:31 +03:00
OBJ_CONSTRUCTION ;
defActions | = SHARE_POS ;
pos = parent - > pos + Point ( 54 , 122 + s . serial * 50 ) ;
2008-11-16 03:06:15 +02:00
2009-08-17 11:50:31 +03:00
static const char * flags [ ] = { " AOFLGBR.DEF " , " AOFLGBB.DEF " , " AOFLGBY.DEF " , " AOFLGBG.DEF " ,
" AOFLGBO.DEF " , " AOFLGBP.DEF " , " AOFLGBT.DEF " , " AOFLGBS.DEF " } ;
static const char * bgs [ ] = { " ADOPRPNL.bmp " , " ADOPBPNL.bmp " , " ADOPYPNL.bmp " , " ADOPGPNL.bmp " ,
" ADOPOPNL.bmp " , " ADOPPPNL.bmp " , " ADOPTPNL.bmp " , " ADOPSPNL.bmp " } ;
2008-11-16 03:06:15 +02:00
2009-08-17 11:50:31 +03:00
bg = new CPicture ( BitmapHandler : : loadBitmap ( bgs [ s . color ] ) , 0 , 0 , true ) ;
btns [ 0 ] = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 132 ] , bind ( & OptionsTab : : nextCastle , owner , s . serial , - 1 ) , 107 , 5 , " ADOPLFA.DEF " ) ;
btns [ 1 ] = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 133 ] , bind ( & OptionsTab : : nextCastle , owner , s . serial , + 1 ) , 168 , 5 , " ADOPRTA.DEF " ) ;
btns [ 2 ] = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 148 ] , bind ( & OptionsTab : : nextHero , owner , s . serial , - 1 ) , 183 , 5 , " ADOPLFA.DEF " ) ;
btns [ 3 ] = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 149 ] , bind ( & OptionsTab : : nextHero , owner , s . serial , + 1 ) , 244 , 5 , " ADOPRTA.DEF " ) ;
btns [ 4 ] = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 164 ] , bind ( & OptionsTab : : nextBonus , owner , s . serial , - 1 ) , 259 , 5 , " ADOPLFA.DEF " ) ;
btns [ 5 ] = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 165 ] , bind ( & OptionsTab : : nextBonus , owner , s . serial , + 1 ) , 320 , 5 , " ADOPRTA.DEF " ) ;
2009-05-10 01:19:41 +03:00
2009-08-17 11:50:31 +03:00
if ( curMap - > players [ s . color ] . canHumanPlay )
{
flag = new AdventureMapButton ( CGI - > generaltexth - > zelp [ 180 ] , bind ( & OptionsTab : : flagPressed , owner , s . serial ) , - 43 , 2 , flags [ s . color ] ) ;
flag - > hoverable = true ;
}
else
flag = NULL ;
town = new SelectedBox ( TOWN , s . serial ) ;
town - > pos = pos + Point ( 119 , 2 ) ;
hero = new SelectedBox ( HERO , s . serial ) ;
hero - > pos = pos + Point ( 195 , 2 ) ;
bonus = new SelectedBox ( BONUS , s . serial ) ;
bonus - > pos = pos + Point ( 271 , 2 ) ;
2008-11-16 03:06:15 +02:00
}
2009-08-17 11:50:31 +03:00
void OptionsTab : : PlayerOptionsEntry : : showAll ( SDL_Surface * to )
2008-11-16 03:06:15 +02:00
{
2009-08-17 11:50:31 +03:00
CIntObject : : showAll ( to ) ;
printAtMiddleLoc ( s . name , 55 , 10 , FONT_SMALL , zwykly , to ) ;
2008-11-16 03:06:15 +02:00
}
2009-08-17 11:50:31 +03:00
void OptionsTab : : SelectedBox : : showAll ( SDL_Surface * to )
2008-11-16 03:06:15 +02:00
{
2009-08-17 11:50:31 +03:00
PlayerSettings & s = curOpts - > playerInfos [ player ] ;
switch ( which )
{
case TOWN :
{
if ( s . castle < F_NUMBER & & s . castle > = 0 )
blitAt ( graphics - > getPic ( s . castle , true , false ) , pos , to ) ;
else if ( s . castle = = - 1 )
blitAt ( CGP - > rTown , pos , to ) ;
else if ( s . castle = = - 2 )
blitAt ( CGP - > nTown , pos , to ) ;
}
break ;
case HERO :
{
if ( s . hero = = - 1 )
{
blitAt ( CGP - > rHero , pos , to ) ;
}
else if ( s . hero = = - 2 )
{
if ( s . heroPortrait > = 0 )
{
blitAt ( graphics - > portraitSmall [ s . heroPortrait ] , pos , to ) ;
}
else
{
blitAt ( CGP - > nHero , pos , to ) ;
}
}
else
{
blitAt ( graphics - > portraitSmall [ s . hero ] , pos , to ) ;
}
}
break ;
case BONUS :
{
int pom ;
switch ( s . bonus )
{
case - 1 :
pom = 10 ;
break ;
case 0 :
pom = 9 ;
break ;
case 1 :
pom = 8 ;
break ;
case 2 :
pom = CGI - > townh - > towns [ s . castle ] . bonus ;
break ;
}
blitAt ( CGP - > bonuses - > ourImages [ pom ] . bitmap , pos , to ) ;
}
break ;
}
2008-11-16 03:06:15 +02:00
}
2009-08-17 11:50:31 +03:00
OptionsTab : : SelectedBox : : SelectedBox ( SelType Which , ui8 Player )
: which ( Which ) , player ( Player )
2008-11-16 03:06:15 +02:00
{
2009-08-17 11:50:31 +03:00
}