CPreGame.cpp 44 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677
  1. #include "../stdafx.h"
  2. #include "CPreGame.h"
  3. #include "../hch/CDefHandler.h"
  4. #include <ctime>
  5. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  6. #include <boost/algorithm/string.hpp>
  7. #include <zlib.h>
  8. #include "../timeHandler.h"
  9. #include <sstream>
  10. #include "SDL_Extensions.h"
  11. #include "CGameInfo.h"
  12. #include "../hch/CGeneralTextHandler.h"
  13. #include "CCursorHandler.h"
  14. #include "../hch/CLodHandler.h"
  15. #include "../hch/CTownHandler.h"
  16. #include "../hch/CHeroHandler.h"
  17. #include <cmath>
  18. #include "Graphics.h"
  19. //#include <boost/thread.hpp>
  20. #include <boost/bind.hpp>
  21. #include <cstdlib>
  22. #include "../lib/Connection.h"
  23. #include "../hch/CMusicHandler.h"
  24. #include "../hch/CVideoHandler.h"
  25. #include "AdventureMapButton.h"
  26. #include "GUIClasses.h"
  27. #include "../hch/CCreatureHandler.h"
  28. #include "CPlayerInterface.h"
  29. #include "../CCallback.h"
  30. #include <boost/lexical_cast.hpp>
  31. /*
  32. * CPreGame.cpp, part of VCMI engine
  33. *
  34. * Authors: listed in file AUTHORS in main folder
  35. *
  36. * License: GNU General Public License v2.0 or later
  37. * Full text of license available in license.txt file, in main folder
  38. *
  39. */
  40. namespace fs = boost::filesystem;
  41. using boost::bind;
  42. using boost::ref;
  43. void startGame(StartInfo * options);
  44. CGPreGame * CGP;
  45. static const CMapHeader *curMap = NULL;
  46. static StartInfo *curOpts = NULL;
  47. static int playerColor;
  48. static std::string selectedName; //set when game is started/loaded
  49. CMenuScreen::CMenuScreen( EState which )
  50. {
  51. OBJ_CONSTRUCTION;
  52. bgAd = NULL;
  53. switch(which)
  54. {
  55. case mainMenu:
  56. {
  57. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[3].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[newGame])), 540, 10, "ZMENUNG.DEF", SDLK_n);
  58. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[4].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[loadGame])), 532, 132, "ZMENULG.DEF", SDLK_l);
  59. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[5].second, 0 /*cb*/, 524, 251, "ZMENUHS.DEF", SDLK_h);
  60. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[6].second, 0 /*cb*/, 557, 359, "ZMENUCR.DEF", SDLK_c);
  61. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[7].second, bind(std::exit, 0), 586, 468, "ZMENUQT.DEF", SDLK_ESCAPE);
  62. }
  63. break;
  64. case newGame:
  65. {
  66. bgAd = new CPicture(BitmapHandler::loadBitmap("ZNEWGAM.bmp"), 114, 312, true);
  67. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, newGame), 545, 4, "ZTSINGL.DEF", SDLK_s);
  68. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, 0 /*cb*/, 568, 120, "ZTMULTI.DEF", SDLK_m);
  69. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, 0 /*cb*/, 541, 233, "ZTCAMPN.DEF", SDLK_c);
  70. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  71. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  72. }
  73. break;
  74. case loadGame:
  75. {
  76. bgAd = new CPicture(BitmapHandler::loadBitmap("ZLOADGAM.bmp"), 114, 312, true);
  77. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, loadGame), 545, 4, "ZTSINGL.DEF", SDLK_s);
  78. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, 0 /*cb*/, 568, 120, "ZTMULTI.DEF", SDLK_m);
  79. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, 0 /*cb*/, 541, 233, "ZTCAMPN.DEF", SDLK_c);
  80. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  81. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  82. }
  83. break;
  84. }
  85. for(int i = 0; i < ARRAY_COUNT(buttons); i++)
  86. buttons[i]->hoverable = true;
  87. }
  88. CMenuScreen::~CMenuScreen()
  89. {
  90. }
  91. void CMenuScreen::showAll( SDL_Surface * to )
  92. {
  93. blitAt(CGP->mainbg, 0, 0, to);
  94. CIntObject::showAll(to);
  95. }
  96. void CMenuScreen::show( SDL_Surface * to )
  97. {
  98. CIntObject::show(to);
  99. CGI->videoh->update(pos.x + 8, pos.y + 105, screen, true, false);
  100. }
  101. void CMenuScreen::moveTo( CMenuScreen *next )
  102. {
  103. GH.popInt(this);
  104. GH.pushInt(next);
  105. }
  106. void CGPreGame::run()
  107. {
  108. GH.handleEvents();
  109. #ifdef _WIN32
  110. CGI->videoh->open("ACREDIT.SMK");
  111. #else
  112. CGI->videoh->open("ACREDIT.SMK", true, false);
  113. #endif
  114. GH.pushInt(scrs[mainMenu]);
  115. while(1)
  116. {
  117. CGI->curh->draw1();
  118. SDL_Flip(screen);
  119. CGI->curh->draw2();
  120. SDL_Delay(20); //give time for other apps
  121. GH.topInt()->show(screen);
  122. GH.updateTime();
  123. GH.handleEvents();
  124. }
  125. }
  126. CGPreGame::CGPreGame()
  127. {
  128. GH.defActionsDef = 63;
  129. CGP = this;
  130. mainbg = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  131. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  132. scrs[i] = new CMenuScreen((EState)i);
  133. }
  134. CGPreGame::~CGPreGame()
  135. {
  136. SDL_FreeSurface(mainbg);
  137. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  138. delete scrs[i];
  139. }
  140. void CGPreGame::openSel( EState type )
  141. {
  142. GH.pushInt(new CSelectionScreen(type));
  143. }
  144. void CGPreGame::loadGraphics()
  145. {
  146. victory = CDefHandler::giveDef("SCNRVICT.DEF");
  147. loss = CDefHandler::giveDef("SCNRLOSS.DEF");
  148. bonuses = CDefHandler::giveDef("SCNRSTAR.DEF");
  149. rHero = BitmapHandler::loadBitmap("HPSRAND1.bmp");
  150. rTown = BitmapHandler::loadBitmap("HPSRAND0.bmp");
  151. nHero = BitmapHandler::loadBitmap("HPSRAND6.bmp");
  152. nTown = BitmapHandler::loadBitmap("HPSRAND5.bmp");
  153. }
  154. void CGPreGame::disposeGraphics()
  155. {
  156. delete victory;
  157. delete loss;
  158. SDL_FreeSurface(rHero);
  159. SDL_FreeSurface(nHero);
  160. SDL_FreeSurface(rTown);
  161. SDL_FreeSurface(nTown);
  162. }
  163. CSelectionScreen::CSelectionScreen( EState Type )
  164. {
  165. OBJ_CONSTRUCTION_CAPTURING_ALL;
  166. IShowActivable::type = BLOCK_ADV_HOTKEYS;
  167. pos.w = 762;
  168. pos.h = 584;
  169. if(Type == saveGame)
  170. {
  171. center(pos);
  172. }
  173. else
  174. {
  175. pos.x = 3;
  176. pos.y = 6;
  177. bg = new CPicture(BitmapHandler::loadBitmap(rand()%2 ? "ZPIC1000.bmp" : "ZPIC1001.bmp"), -3, -6, true);
  178. }
  179. CGP->loadGraphics();
  180. type = Type;
  181. curOpts = &sInfo;
  182. sInfo.difficulty = 1;
  183. current = NULL;
  184. sInfo.mode = (Type == newGame ? 0 : 1);
  185. sInfo.turnTime = 0;
  186. curTab = NULL;
  187. card = new InfoCard(type);
  188. opt = new OptionsTab(type/*, sInfo*/);
  189. opt->recActions = DISPOSE;
  190. sel = new SelectionTab(type, bind(&CSelectionScreen::changeSelection, this, _1));
  191. sel->recActions = DISPOSE;
  192. switch(type)
  193. {
  194. case newGame:
  195. {
  196. card->difficulty->onChange = bind(&CSelectionScreen::difficultyChange, this, _1);
  197. card->difficulty->select(1, 0);
  198. AdventureMapButton *select = new AdventureMapButton(CGI->generaltexth->zelp[45], bind(&CSelectionScreen::toggleTab, this, sel), 411, 75, "GSPBUTT.DEF", SDLK_s);
  199. select->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL);
  200. AdventureMapButton *opts = new AdventureMapButton(CGI->generaltexth->zelp[46], bind(&CSelectionScreen::toggleTab, this, opt), 411, 503, "GSPBUTT.DEF", SDLK_a);
  201. opts->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL);
  202. AdventureMapButton *random = new AdventureMapButton(CGI->generaltexth->zelp[47], bind(&CSelectionScreen::toggleTab, this, sel), 411, 99, "GSPBUTT.DEF", SDLK_r);
  203. random->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL);
  204. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRBEG.DEF", SDLK_b);
  205. }
  206. break;
  207. case loadGame:
  208. sel->recActions = 255;
  209. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRLOD.DEF", SDLK_l);
  210. break;
  211. case saveGame:
  212. sel->recActions = 255;
  213. start = new AdventureMapButton("", CGI->generaltexth->zelp[103].second, bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRSAV.DEF");
  214. break;
  215. }
  216. start->assignedKeys.insert(SDLK_RETURN);
  217. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 581, 529, "SCNRBACK.DEF", SDLK_ESCAPE);
  218. }
  219. CSelectionScreen::~CSelectionScreen()
  220. {
  221. curMap = NULL;
  222. curOpts = NULL;
  223. playerColor = -1;
  224. }
  225. void CSelectionScreen::toggleTab(CIntObject *tab)
  226. {
  227. if(curTab && curTab->active)
  228. {
  229. curTab->deactivate();
  230. curTab->recActions = DISPOSE;
  231. }
  232. if(curTab != tab)
  233. {
  234. tab->recActions = 255;
  235. tab->activate();
  236. curTab = tab;
  237. }
  238. else
  239. {
  240. curTab = NULL;
  241. };
  242. GH.totalRedraw();
  243. }
  244. void CSelectionScreen::changeSelection( const CMapInfo *to )
  245. {
  246. curMap = current = to;
  247. if(to && type == loadGame)
  248. curOpts->difficulty = to->seldiff;
  249. updateStartInfo(to);
  250. card->changeSelection(to);
  251. opt->changeSelection(to);
  252. }
  253. void CSelectionScreen::updateStartInfo( const CMapInfo * to )
  254. {
  255. sInfo.playerInfos.clear();
  256. if(!to)
  257. return;
  258. sInfo.playerInfos.resize(to->playerAmnt);
  259. sInfo.mapname = to->filename;
  260. playerColor = -1;
  261. int serialC=0;
  262. for (int i = 0; i < PLAYER_LIMIT; i++)
  263. {
  264. const PlayerInfo &pinfo = to->players[i];
  265. //neither computer nor human can play - no player
  266. if (!(pinfo.canComputerPlay || pinfo.canComputerPlay))
  267. continue;
  268. PlayerSettings &pset = sInfo.playerInfos[serialC];
  269. pset.color = i;
  270. pset.serial = serialC++;
  271. if (pinfo.canHumanPlay && playerColor < 0)
  272. {
  273. pset.name = CGI->generaltexth->allTexts[434]; //Player
  274. pset.human = true;
  275. playerColor = i;
  276. }
  277. else
  278. {
  279. pset.name = CGI->generaltexth->allTexts[468];//Computer
  280. pset.human = false;
  281. }
  282. 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
  283. {
  284. if((1 << j) & pinfo.allowedFactions)
  285. {
  286. if (pset.castle >= 0) //we've already assigned a castle and another one is possible -> set random and let player choose
  287. pset.castle = -1; //breaks
  288. if (pset.castle == -2) //first available castle - pick
  289. pset.castle = j;
  290. }
  291. }
  292. if ((pinfo.generateHeroAtMainTown || to->version==CMapHeader::RoE) && pinfo.hasMainTown //we will generate hero in front of main town
  293. || pinfo.p8) //random hero
  294. pset.hero = -1;
  295. else
  296. pset.hero = -2;
  297. if(pinfo.mainHeroName.length())
  298. {
  299. pset.heroName = pinfo.mainHeroName;
  300. if((pset.heroPortrait = pinfo.mainHeroPortrait) == 255)
  301. pset.heroPortrait = pinfo.p9;
  302. }
  303. pset.handicap = 0;
  304. }
  305. }
  306. void CSelectionScreen::startGame()
  307. {
  308. if(type != saveGame)
  309. {
  310. if(!current)
  311. return;
  312. selectedName = sInfo.mapname;
  313. StartInfo *si = new StartInfo(sInfo);
  314. GH.popIntTotally(this);
  315. GH.popIntTotally(GH.topInt());
  316. curMap = NULL;
  317. curOpts = NULL;
  318. ::startGame(si);
  319. delete si; //rather won't be called...
  320. }
  321. else
  322. {
  323. if(!(sel && sel->txt && sel->txt->text.size()))
  324. return;
  325. selectedName = "Games/" + sel->txt->text + ".vlgm1";
  326. LOCPLINT->cb->save(sel->txt->text);
  327. GH.popIntTotally(this);
  328. }
  329. }
  330. void CSelectionScreen::difficultyChange( int to )
  331. {
  332. assert(type == newGame);
  333. sInfo.difficulty = to;
  334. GH.totalRedraw();
  335. }
  336. // A new size filter (Small, Medium, ...) has been selected. Populate
  337. // selMaps with the relevant data.
  338. void SelectionTab::filter( int size, bool selectFirst )
  339. {
  340. curItems.clear();
  341. for (size_t i=0; i<allItems.size(); i++)
  342. if( allItems[i].version && (!size || allItems[i].width == size))
  343. curItems.push_back(&allItems[i]);
  344. if(curItems.size())
  345. {
  346. slider->block(false);
  347. slider->setAmount(curItems.size());
  348. sort();
  349. if(selectFirst)
  350. {
  351. slider->moveTo(0);
  352. onSelect(curItems[0]);
  353. }
  354. selectAbs(0);
  355. }
  356. else
  357. {
  358. slider->block(true);
  359. onSelect(NULL);
  360. }
  361. }
  362. void SelectionTab::getFiles(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext)
  363. {
  364. if(!boost::filesystem::exists(dirname))
  365. {
  366. tlog1 << "Cannot find " << dirname << " directory!\n";
  367. }
  368. fs::path tie( (fs::initial_path<fs::path>())/dirname );
  369. fs::directory_iterator end_iter;
  370. for ( fs::directory_iterator file (tie); file!=end_iter; ++file )
  371. {
  372. if(fs::is_regular_file(file->status())
  373. && boost::ends_with(file->path().filename(), ext))
  374. {
  375. out.resize(out.size()+1);
  376. out.back().date = fs::last_write_time(file->path());
  377. out.back().name = dirname+"/"+(file->path().leaf());
  378. }
  379. }
  380. allItems.resize(out.size());
  381. }
  382. void SelectionTab::parseMaps(std::vector<FileInfo> &files, int start, int threads)
  383. {
  384. int read=0;
  385. unsigned char mapBuffer[1500];
  386. while(start < allItems.size())
  387. {
  388. gzFile tempf = gzopen(files[start].name.c_str(),"rb");
  389. read = gzread(tempf, mapBuffer, 1500);
  390. gzclose(tempf);
  391. if(read < 50 || !mapBuffer[4])
  392. {
  393. tlog3 << "\t\tWarning: corrupted map file: " << files[start].name << std::endl;
  394. }
  395. else //valid map
  396. {
  397. allItems[start].init(files[start].name, mapBuffer);
  398. //allItems[start].date = "DATEDATE";// files[start].date;
  399. }
  400. start += threads;
  401. }
  402. }
  403. void SelectionTab::parseGames(std::vector<FileInfo> &files)
  404. {
  405. for(int i=0; i<files.size(); i++)
  406. {
  407. CLoadFile lf(files[i].name);
  408. if(!lf.sfile)
  409. continue;
  410. ui8 sign[8];
  411. lf >> sign;
  412. if(std::memcmp(sign,"VCMISVG",7))
  413. {
  414. tlog1 << files[i].name << " is not a correct savefile!" << std::endl;
  415. continue;
  416. }
  417. lf >> static_cast<CMapHeader&>(allItems[i]) >> allItems[i].seldiff;
  418. allItems[i].filename = files[i].name;
  419. allItems[i].countPlayers();
  420. allItems[i].date = std::asctime(std::localtime(&files[i].date));
  421. }
  422. }
  423. SelectionTab::SelectionTab(EState Type, const boost::function<void(CMapInfo *)> &OnSelect)
  424. :onSelect(OnSelect)
  425. {
  426. OBJ_CONSTRUCTION;
  427. selectionPos = 0;
  428. used = LCLICK | WHEEL | KEYBOARD | DOUBLECLICK;
  429. slider = NULL;
  430. txt = NULL;
  431. type = Type;
  432. bg = new CPicture(BitmapHandler::loadBitmap("SCSELBCK.bmp"), 0, 0, true);
  433. pos.w = bg->pos.w;
  434. pos.h = bg->pos.h;
  435. std::vector<FileInfo> toParse;
  436. switch(type)
  437. {
  438. case newGame:
  439. getFiles(toParse, "Maps", "h3m");
  440. parseMaps(toParse);
  441. positions = 18;
  442. break;
  443. case loadGame:
  444. case saveGame:
  445. getFiles(toParse, "Games", "vlgm1");
  446. parseGames(toParse);
  447. if(type == loadGame)
  448. {
  449. positions = 18;
  450. }
  451. else
  452. {
  453. positions = 16;
  454. }
  455. if(type == saveGame)
  456. txt = new CTextInput(Rect(32, 539, 350, 20), Point(-32, -25), "GSSTRIP.bmp", 0);
  457. break;
  458. default:
  459. assert(0);
  460. }
  461. //size filter buttons
  462. {
  463. int sizes[] = {36, 72, 108, 144, 0};
  464. const char * names[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"};
  465. for(int i = 0; i < 5; i++)
  466. new AdventureMapButton("", CGI->generaltexth->zelp[54+i].second, bind(&SelectionTab::filter, this, sizes[i], true), 158 + 47*i, 46, names[i]);
  467. }
  468. {
  469. int xpos[] = {23, 55, 88, 121, 306, 339};
  470. const char * names[] = {"SCBUTT1.DEF", "SCBUTT2.DEF", "SCBUTCP.DEF", "SCBUTT3.DEF", "SCBUTT4.DEF", "SCBUTT5.DEF"};
  471. for(int i = 0; i < 6; i++)
  472. new AdventureMapButton("", CGI->generaltexth->zelp[107+i].second, bind(&SelectionTab::sortBy, this, i), xpos[i], 86, names[i]);
  473. }
  474. slider = new CSlider(372, 86, type != saveGame ? 480 : 430, bind(&SelectionTab::sliderMove, this, _1), positions, curItems.size(), 0, false, 1);
  475. format = CDefHandler::giveDef("SCSELC.DEF");
  476. sortingBy = _format;
  477. ascending = true;
  478. filter(0);
  479. //select(0);
  480. switch(type)
  481. {
  482. case newGame:
  483. selectFName("Maps/Arrogance.h3m");
  484. break;
  485. case loadGame:
  486. select(0);
  487. break;
  488. case saveGame:;
  489. if(selectedName.size())
  490. {
  491. if(selectedName[0] == 'M')
  492. txt->setText("NEWGAME");
  493. else
  494. selectFName(selectedName);
  495. }
  496. }
  497. }
  498. SelectionTab::~SelectionTab()
  499. {
  500. delete format;
  501. }
  502. void SelectionTab::sortBy( int criteria )
  503. {
  504. if(criteria == sortingBy)
  505. {
  506. ascending = !ascending;
  507. }
  508. else
  509. {
  510. sortingBy = (ESortBy)criteria;
  511. ascending = true;
  512. }
  513. sort();
  514. selectAbs(0);
  515. }
  516. void SelectionTab::sort()
  517. {
  518. if(sortingBy != _name)
  519. std::stable_sort(curItems.begin(),curItems.end(),mapSorter(_name));
  520. std::stable_sort(curItems.begin(),curItems.end(),mapSorter(sortingBy));
  521. if(!ascending)
  522. std::reverse(curItems.begin(), curItems.end());
  523. redraw();
  524. }
  525. void SelectionTab::select( int position )
  526. {
  527. if(!curItems.size()) return;
  528. // New selection. py is the index in curItems.
  529. int py = position + slider->value;
  530. amax(py, 0);
  531. amin(py, curItems.size()-1);
  532. CGI->soundh->playSound(soundBase::button);
  533. selectionPos = py;
  534. if(position < 0)
  535. slider->moveTo(slider->value + position);
  536. else if(position >= positions)
  537. slider->moveTo(slider->value + position - positions + 1);
  538. if(txt)
  539. txt->setText(curItems[py]->filename.substr(6,curItems[py]->filename.size()-12));
  540. onSelect(curItems[py]);
  541. }
  542. void SelectionTab::selectAbs( int position )
  543. {
  544. select(position - slider->value);
  545. }
  546. int SelectionTab::getPosition( int x, int y )
  547. {
  548. return -1;
  549. }
  550. void SelectionTab::sliderMove( int slidPos )
  551. {
  552. if(!slider) return; //ignore spurious call when slider is being created
  553. redraw();
  554. }
  555. // Display the tab with the scenario names
  556. //
  557. // elemIdx is the index of the maps or saved game to display on line 0
  558. // slider->capacity contains the number of available screen lines
  559. // slider->positionsAmnt is the number of elements after filtering
  560. void SelectionTab::printMaps(SDL_Surface *to)
  561. {
  562. int elemIdx = slider->value;
  563. // Display all elements if there's enough space
  564. //if(slider->amount < slider->capacity)
  565. // elemIdx = 0;
  566. SDL_Color nasz;
  567. #define POS(xx, yy) pos.x + xx, pos.y + yy + line*25
  568. for (int line = 0; line < positions && elemIdx < curItems.size(); elemIdx++, line++)
  569. {
  570. CMapInfo* curMap = curItems[elemIdx];
  571. if (elemIdx == selectionPos)
  572. nasz=tytulowy;
  573. else
  574. nasz=zwykly;
  575. std::ostringstream ostr(std::ostringstream::out);
  576. ostr << curMap->playerAmnt << "/" << curMap->humenPlayers;
  577. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, nasz, to);
  578. std::string temp2 = "C";
  579. switch (curMap->width)
  580. {
  581. case 36:
  582. temp2="S";
  583. break;
  584. case 72:
  585. temp2="M";
  586. break;
  587. case 108:
  588. temp2="L";
  589. break;
  590. case 144:
  591. temp2="XL";
  592. break;
  593. }
  594. CSDL_Ext::printAtMiddle(temp2, POS(70, 128), FONT_SMALL, nasz, to);
  595. int temp=-1;
  596. switch (curMap->version)
  597. {
  598. case CMapHeader::RoE:
  599. temp=0;
  600. break;
  601. case CMapHeader::AB:
  602. temp=1;
  603. break;
  604. case CMapHeader::SoD:
  605. temp=2;
  606. break;
  607. case CMapHeader::WoG:
  608. temp=3;
  609. break;
  610. default:
  611. // Unknown version. Be safe and ignore that map
  612. tlog2 << "Warning: " << curMap->filename << " has wrong version!\n";
  613. continue;
  614. }
  615. blitAt(format->ourImages[temp].bitmap, POS(88, 117), to);
  616. if (type == newGame)
  617. {
  618. if (!(curMap->name.length()))
  619. curMap->name = "Unnamed";
  620. CSDL_Ext::printAtMiddle(curMap->name, POS(213, 128), FONT_SMALL, nasz, to);
  621. }
  622. else
  623. {
  624. std::string &name = curMap->filename;
  625. CSDL_Ext::printAtMiddle(name.substr(6,name.size()-12), POS(213, 128), FONT_SMALL, nasz, to);
  626. }
  627. if (curMap->victoryCondition.condition == winStandard)
  628. temp = 11;
  629. else
  630. temp = curMap->victoryCondition.condition;
  631. blitAt(CGP->victory->ourImages[temp].bitmap, POS(306, 117), to);
  632. if (curMap->lossCondition.typeOfLossCon == lossStandard)
  633. temp=3;
  634. else
  635. temp=curMap->lossCondition.typeOfLossCon;
  636. blitAt(CGP->loss->ourImages[temp].bitmap, POS(339, 117), to);
  637. }
  638. #undef POS
  639. }
  640. void SelectionTab::showAll( SDL_Surface * to )
  641. {
  642. CIntObject::showAll(to);
  643. printMaps(to);
  644. CSDL_Ext::printAtMiddle(CGI->generaltexth->arraytxt[type==newGame ? 229 : 230], 208, 34, FONT_MEDIUM, tytulowy, to); //Select a Scenario to Play
  645. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[510], 90, 68, FONT_SMALL, tytulowy, to); //Map sizes
  646. }
  647. void SelectionTab::clickLeft( tribool down, bool previousState )
  648. {
  649. int line = getLine();
  650. if(line != -1)
  651. select(line);
  652. }
  653. void SelectionTab::wheelScrolled( bool down, bool in )
  654. {
  655. slider->moveTo(slider->value + 3 * (down ? +1 : -1));
  656. //select(selectionPos - slider->value + (down ? +1 : -1));
  657. }
  658. void SelectionTab::keyPressed( const SDL_KeyboardEvent & key )
  659. {
  660. if(key.state != SDL_PRESSED) return;
  661. int moveBy = 0;
  662. switch(key.keysym.sym)
  663. {
  664. case SDLK_UP:
  665. moveBy = -1;
  666. break;
  667. case SDLK_DOWN:
  668. moveBy = +1;
  669. break;
  670. case SDLK_PAGEUP:
  671. moveBy = -positions+1;
  672. break;
  673. case SDLK_PAGEDOWN:
  674. moveBy = +positions-1;
  675. break;
  676. case SDLK_HOME:
  677. select(-slider->value);
  678. return;
  679. case SDLK_END:
  680. select(curItems.size() - slider->value);
  681. return;
  682. default:
  683. return;
  684. }
  685. select(selectionPos - slider->value + moveBy);
  686. }
  687. void SelectionTab::onDoubleClick()
  688. {
  689. if(getLine() != -1) //double clicked scenarios list
  690. {
  691. //act as start button was pressed
  692. (static_cast<CSelectionScreen*>(parent))->start->callback();
  693. }
  694. }
  695. int SelectionTab::getLine()
  696. {
  697. int line = -1;
  698. Point clickPos(GH.current->button.x, GH.current->button.y);
  699. clickPos -= pos.topLeft();
  700. if (clickPos.y > 115 && clickPos.y < 564 && clickPos.x > 52 && clickPos.x < 366)
  701. {
  702. line = (clickPos.y-115) / 25; //which line
  703. }
  704. return line;
  705. }
  706. void SelectionTab::selectFName( const std::string &fname )
  707. {
  708. for(int i = curItems.size() - 1; i >= 0; i--)
  709. {
  710. if(curItems[i]->filename == fname)
  711. {
  712. slider->moveTo(i);
  713. selectAbs(i);
  714. return;
  715. }
  716. }
  717. selectAbs(0);
  718. }
  719. InfoCard::InfoCard( EState Type )
  720. {
  721. OBJ_CONSTRUCTION;
  722. pos.x += 393;
  723. used = RCLICK;
  724. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  725. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  726. type = Type;
  727. bg = new CPicture(BitmapHandler::loadBitmap("GSELPOP1.bmp"), 0, 0, true);
  728. pos.w = bg->pos.w;
  729. pos.h = bg->pos.h;
  730. difficulty = new CHighlightableButtonsGroup(0);
  731. {
  732. static const char *difButns[] = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  733. BLOCK_CAPTURING;
  734. for(int i = 0; i < 5; i++)
  735. {
  736. difficulty->addButton(new CHighlightableButton("", CGI->generaltexth->zelp[24+i].second, 0, 110 + i*32, 450, difButns[i], i));
  737. difficulty->buttons.back()->pos += pos.topLeft();
  738. }
  739. }
  740. if(type != newGame)
  741. difficulty->block(true);
  742. }
  743. InfoCard::~InfoCard()
  744. {
  745. delete sizes;
  746. delete sFlags;
  747. }
  748. void InfoCard::showAll( SDL_Surface * to )
  749. {
  750. CIntObject::showAll(to);
  751. //blit texts
  752. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 24, 400, FONT_SMALL, zwykly, to); //Allies
  753. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 190, 400, FONT_SMALL, zwykly, to); //Enemies
  754. printAtLoc(CGI->generaltexth->allTexts[494], 33, 430, FONT_SMALL, tytulowy, to);//"Map Diff:"
  755. printAtLoc(CGI->generaltexth->allTexts[492] + ":", 133,430, FONT_SMALL, tytulowy, to); //player difficulty
  756. printAtLoc(CGI->generaltexth->allTexts[218] + ":", 290,430, FONT_SMALL, tytulowy, to); //"Rating:"
  757. printAtLoc(CGI->generaltexth->allTexts[495], 26, 22, FONT_SMALL, tytulowy, to); //Scenario Name:
  758. printAtLoc(CGI->generaltexth->allTexts[496], 26, 132, FONT_SMALL, tytulowy, to); //Scenario Description:
  759. printAtLoc(CGI->generaltexth->allTexts[497], 26, 283, FONT_SMALL, tytulowy, to); //Victory Condition:
  760. printAtLoc(CGI->generaltexth->allTexts[498], 26, 339, FONT_SMALL, tytulowy, to); //Loss Condition:
  761. if(curMap)
  762. {
  763. if(type != newGame)
  764. {
  765. for (int i = 0; i < difficulty->buttons.size(); i++)
  766. {
  767. //if(i == curMap->difficulty)
  768. // difficulty->buttons[i]->state = 3;
  769. //else
  770. // difficulty->buttons[i]->state = 2;
  771. difficulty->buttons[i]->showAll(to);
  772. }
  773. }
  774. int temp = curMap->victoryCondition.condition+1;
  775. if (temp>20) temp=0;
  776. std::string sss = CGI->generaltexth->victoryConditions[temp];
  777. if (temp && curMap->victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  778. printAtLoc(sss, 60, 307, FONT_SMALL, zwykly, to);
  779. temp = curMap->lossCondition.typeOfLossCon+1;
  780. if (temp>20) temp=0;
  781. sss = CGI->generaltexth->lossCondtions[temp];
  782. printAtLoc(sss, 60, 366, FONT_SMALL, zwykly, to);
  783. //blit description
  784. std::vector<std::string> *desc = CMessage::breakText(curMap->description,52);
  785. for (int i=0;i<desc->size();i++)
  786. printAtLoc((*desc)[i], 26, 149 + i*16, FONT_SMALL, zwykly, to);
  787. delete desc;
  788. if (curMap->name.length())
  789. printAtLoc(curMap->name, 26, 39, FONT_BIG, tytulowy, to);
  790. else
  791. printAtLoc("Unnamed", 26, 39, FONT_BIG, tytulowy, to);
  792. assert(curMap->difficulty <= 4);
  793. std::string &diff = CGI->generaltexth->arraytxt[142 + curMap->difficulty];
  794. printAtMiddleLoc(diff, 62, 472, FONT_SMALL, zwykly, to);
  795. switch (curMap->width)
  796. {
  797. case 36:
  798. temp=0;
  799. break;
  800. case 72:
  801. temp=1;
  802. break;
  803. case 108:
  804. temp=2;
  805. break;
  806. case 144:
  807. temp=3;
  808. break;
  809. default:
  810. temp=4;
  811. break;
  812. }
  813. blitAtLoc(sizes->ourImages[temp].bitmap, 318, 22, to);
  814. temp = curMap->victoryCondition.condition;
  815. if (temp>12) temp=11;
  816. blitAtLoc(CGP->victory->ourImages[temp].bitmap, 24, 302, to); //vicotry cond descr
  817. temp=curMap->lossCondition.typeOfLossCon;
  818. if (temp>12) temp=3;
  819. blitAtLoc(CGP->loss->ourImages[temp].bitmap, 24, 359, to); //loss cond
  820. if(type == loadGame)
  821. printToLoc((static_cast<const CMapInfo*>(curMap))->date,308,34, FONT_SMALL, zwykly, to);
  822. //print flags
  823. int fx=64, ex=244, myT;
  824. //if (curMap->howManyTeams)
  825. myT = curMap->players[playerColor].team;
  826. //else
  827. // myT = -1;
  828. for (std::vector<PlayerSettings>::const_iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  829. {
  830. int *myx = ((i->color == playerColor || curMap->players[i->color].team == myT) ? &fx : &ex);
  831. blitAtLoc(sFlags->ourImages[i->color].bitmap, *myx, 399, to);
  832. *myx += sFlags->ourImages[i->color].bitmap->w;
  833. }
  834. std::string tob;
  835. switch (curOpts->difficulty)
  836. {
  837. case 0:
  838. tob="80%";
  839. break;
  840. case 1:
  841. tob="100%";
  842. break;
  843. case 2:
  844. tob="130%";
  845. break;
  846. case 3:
  847. tob="160%";
  848. break;
  849. case 4:
  850. tob="200%";
  851. break;
  852. }
  853. printAtMiddleLoc(tob, 311, 472, FONT_SMALL, zwykly, to);
  854. }
  855. }
  856. void InfoCard::changeSelection( const CMapInfo *to )
  857. {
  858. if(to && type != newGame)
  859. difficulty->select(curOpts->difficulty, 0);
  860. GH.totalRedraw();
  861. }
  862. void InfoCard::clickRight( tribool down, bool previousState )
  863. {
  864. static const Rect flagArea(19, 397, 335, 23);
  865. if(down && curMap && isItInLoc(flagArea, GH.current->motion.x, GH.current->motion.y))
  866. showTeamsPopup();
  867. }
  868. void InfoCard::showTeamsPopup()
  869. {
  870. SDL_Surface *bmp = CMessage::drawBox1(256, 90 + 50 * curMap->howManyTeams);
  871. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[657], 128, 30, FONT_MEDIUM, tytulowy, bmp); //{Team Alignments}
  872. for(int i = 0; i < curMap->howManyTeams; i++)
  873. {
  874. std::vector<ui8> flags;
  875. std::string hlp = CGI->generaltexth->allTexts[656]; //Team %d
  876. hlp.replace(hlp.find("%d"), 2, boost::lexical_cast<std::string>(i+1));
  877. CSDL_Ext::printAtMiddle(hlp, 128, 65 + 50*i, FONT_SMALL, zwykly, bmp);
  878. for(int j = 0; j < PLAYER_LIMIT; j++)
  879. if((curMap->players[j].canHumanPlay || curMap->players[j].canComputerPlay)
  880. && curMap->players[j].team == i)
  881. flags.push_back(j);
  882. int curx = 128 - 9*flags.size();
  883. for(int j = 0; j < flags.size(); j++)
  884. {
  885. blitAt(sFlags->ourImages[flags[j]].bitmap, curx, 75 + 50*i, bmp);
  886. curx += 18;
  887. }
  888. }
  889. GH.pushInt(new CInfoPopup(bmp, true));
  890. }
  891. OptionsTab::OptionsTab( EState Type)
  892. :type(Type)
  893. {
  894. OBJ_CONSTRUCTION;
  895. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTBK.bmp"), 0, 0, true);
  896. pos = bg->pos;
  897. if(type == newGame)
  898. turnDuration = new CSlider(55, 551, 194, bind(&OptionsTab::setTurnLength, this, _1), 1, 11, 11, true, 1);
  899. }
  900. OptionsTab::~OptionsTab()
  901. {
  902. }
  903. void OptionsTab::showAll( SDL_Surface * to )
  904. {
  905. CIntObject::showAll(to);
  906. printAtMiddleLoc(CGI->generaltexth->allTexts[515], 222, 30, FONT_BIG, tytulowy, to);
  907. printAtMiddleWBLoc(CGI->generaltexth->allTexts[516], 222, 58, FONT_SMALL, 55, zwykly, to); //Select starting options, handicap, and name for each player in the game.
  908. printAtMiddleWBLoc(CGI->generaltexth->allTexts[517], 107, 102, FONT_SMALL, 14, tytulowy, to); //Player Name Handicap Type
  909. printAtMiddleWBLoc(CGI->generaltexth->allTexts[518], 197, 102, FONT_SMALL, 10, tytulowy, to); //Starting Town
  910. printAtMiddleWBLoc(CGI->generaltexth->allTexts[519], 273, 102, FONT_SMALL, 10, tytulowy, to); //Starting Hero
  911. printAtMiddleWBLoc(CGI->generaltexth->allTexts[520], 349, 102, FONT_SMALL, 10, tytulowy, to); //Starting Bonus
  912. printAtMiddleLoc(CGI->generaltexth->allTexts[521], 222, 538, FONT_SMALL, tytulowy, to); // Player Turn Duration
  913. if(curOpts->turnTime)
  914. {
  915. std::ostringstream os;
  916. os << (int)curOpts->turnTime << " Minutes";
  917. printAtMiddleLoc(os.str(), 319,559, FONT_SMALL, zwykly, to);
  918. }
  919. else
  920. printAtMiddleLoc("Unlimited",319,559, FONT_SMALL, zwykly, to);
  921. }
  922. void OptionsTab::nextCastle( int player, int dir )
  923. {
  924. PlayerSettings &s = curOpts->playerInfos[player];
  925. si32 &cur = s.castle;
  926. ui32 allowed = curMap->players[s.color].allowedFactions;
  927. if (cur == -2) //no castle - no change
  928. return;
  929. if (cur == -1) //random => first/last available
  930. {
  931. int pom = (dir>0) ? (0) : (F_NUMBER-1); // last or first
  932. for (;pom >= 0 && pom < F_NUMBER; pom+=dir)
  933. {
  934. if((1 << pom) & allowed)
  935. {
  936. cur=pom;
  937. break;
  938. }
  939. }
  940. }
  941. else // next/previous available
  942. {
  943. for (;;)
  944. {
  945. cur+=dir;
  946. if ((1 << cur) & allowed)
  947. break;
  948. if (cur >= F_NUMBER || cur<0)
  949. {
  950. double p1 = log((double)allowed) / log(2.0f)+0.000001f;
  951. double check = p1 - ((int)p1);
  952. if (check < 0.001)
  953. cur = (int)p1;
  954. else
  955. cur = -1;
  956. break;
  957. }
  958. }
  959. }
  960. if(s.hero >= 0)
  961. s.hero = -1;
  962. if(s.bonus == bresource)
  963. s.bonus = brandom;
  964. entries[player]->selectButtons();
  965. redraw();
  966. }
  967. void OptionsTab::nextHero( int player, int dir )
  968. {
  969. PlayerSettings &s = curOpts->playerInfos[player];
  970. int old = s.hero;
  971. if (s.castle < 0 || !s.human || s.hero == -2)
  972. return;
  973. if (s.hero == -1) //random => first/last available
  974. {
  975. int max = (s.castle*HEROES_PER_TYPE*2+15),
  976. min = (s.castle*HEROES_PER_TYPE*2);
  977. s.hero = nextAllowedHero(min,max,0,dir);
  978. }
  979. else
  980. {
  981. if(dir > 0)
  982. s.hero = nextAllowedHero(s.hero,(s.castle*HEROES_PER_TYPE*2+16),1,dir);
  983. else
  984. s.hero = nextAllowedHero(s.castle*HEROES_PER_TYPE*2-1,s.hero,1,dir);
  985. }
  986. if(old != s.hero)
  987. {
  988. usedHeroes.erase(old);
  989. usedHeroes.insert(s.hero);
  990. redraw();
  991. }
  992. }
  993. int OptionsTab::nextAllowedHero( int min, int max, int incl, int dir )
  994. {
  995. if(dir>0)
  996. {
  997. for(int i=min+incl; i<=max-incl; i++)
  998. if(canUseThisHero(i))
  999. return i;
  1000. }
  1001. else
  1002. {
  1003. for(int i=max-incl; i>=min+incl; i--)
  1004. if(canUseThisHero(i))
  1005. return i;
  1006. }
  1007. return -1;
  1008. }
  1009. bool OptionsTab::canUseThisHero( int ID )
  1010. {
  1011. //for(int i=0;i<CPG->ret.playerInfos.size();i++)
  1012. // if(CPG->ret.playerInfos[i].hero==ID) //hero is already taken
  1013. // return false;
  1014. return !vstd::contains(usedHeroes, ID) && curMap->allowedHeroes[ID];
  1015. }
  1016. void OptionsTab::nextBonus( int player, int dir )
  1017. {
  1018. PlayerSettings &s = curOpts->playerInfos[player];
  1019. si8 &ret = s.bonus += dir;
  1020. if (s.hero==-2 && !curMap->players[s.color].heroesNames.size() && ret==bartifact) //no hero - can't be artifact
  1021. {
  1022. if (dir<0)
  1023. ret=brandom;
  1024. else ret=bgold;
  1025. }
  1026. if(ret > bresource)
  1027. ret = brandom;
  1028. if(ret < brandom)
  1029. ret = bresource;
  1030. if (s.castle==-1 && ret==bresource) //random castle - can't be resource
  1031. {
  1032. if (dir<0)
  1033. ret=bgold;
  1034. else ret=brandom;
  1035. }
  1036. redraw();
  1037. }
  1038. void OptionsTab::changeSelection( const CMapHeader *to )
  1039. {
  1040. for(int i = 0; i < entries.size(); i++)
  1041. {
  1042. children -= entries[i];
  1043. delete entries[i];
  1044. }
  1045. entries.clear();
  1046. usedHeroes.clear();
  1047. OBJ_CONSTRUCTION;
  1048. for(int i = 0; i < curOpts->playerInfos.size(); i++)
  1049. {
  1050. entries.push_back(new PlayerOptionsEntry(this, curOpts->playerInfos[i]));
  1051. const std::vector<SheroName> &heroes = curMap->players[curOpts->playerInfos[i].color].heroesNames;
  1052. for(size_t hi=0; hi<heroes.size(); hi++)
  1053. usedHeroes.insert(heroes[hi].heroID);
  1054. }
  1055. }
  1056. void OptionsTab::setTurnLength( int npos )
  1057. {
  1058. static const int times[] = {1, 2, 4, 6, 8, 10, 15, 20, 25, 30, 0};
  1059. amin(npos, ARRAY_COUNT(times) - 1);
  1060. curOpts->turnTime = times[npos];
  1061. redraw();
  1062. }
  1063. void OptionsTab::flagPressed( int player )
  1064. {
  1065. int playerSerial = playerColor; //curMap->players[playerColor].serial;
  1066. PlayerSettings &s = curOpts->playerInfos[player],
  1067. &old = curOpts->playerInfos[playerSerial];
  1068. std::swap(old.human, s.human);
  1069. std::swap(old.name, s.name);
  1070. playerColor = s.color;
  1071. if(!entries[playerSerial]->fixedHero)
  1072. old.hero = -1;
  1073. entries[s.serial]->selectButtons();
  1074. entries[old.serial]->selectButtons();
  1075. GH.totalRedraw();
  1076. }
  1077. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry( OptionsTab *owner, PlayerSettings &S)
  1078. :s(S)
  1079. {
  1080. OBJ_CONSTRUCTION;
  1081. defActions |= SHARE_POS;
  1082. pos = parent->pos + Point(54, 122 + s.serial*50);
  1083. static const char *flags[] = {"AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  1084. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"};
  1085. static const char *bgs[] = {"ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  1086. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"};
  1087. bg = new CPicture(BitmapHandler::loadBitmap(bgs[s.color]), 0, 0, true);
  1088. if(owner->type == newGame)
  1089. {
  1090. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[132], bind(&OptionsTab::nextCastle, owner, s.serial, -1), 107, 5, "ADOPLFA.DEF");
  1091. btns[1] = new AdventureMapButton(CGI->generaltexth->zelp[133], bind(&OptionsTab::nextCastle, owner, s.serial, +1), 168, 5, "ADOPRTA.DEF");
  1092. btns[2] = new AdventureMapButton(CGI->generaltexth->zelp[148], bind(&OptionsTab::nextHero, owner, s.serial, -1), 183, 5, "ADOPLFA.DEF");
  1093. btns[3] = new AdventureMapButton(CGI->generaltexth->zelp[149], bind(&OptionsTab::nextHero, owner, s.serial, +1), 244, 5, "ADOPRTA.DEF");
  1094. btns[4] = new AdventureMapButton(CGI->generaltexth->zelp[164], bind(&OptionsTab::nextBonus, owner, s.serial, -1), 259, 5, "ADOPLFA.DEF");
  1095. btns[5] = new AdventureMapButton(CGI->generaltexth->zelp[165], bind(&OptionsTab::nextBonus, owner, s.serial, +1), 320, 5, "ADOPRTA.DEF");
  1096. }
  1097. else
  1098. for(int i = 0; i < 6; i++)
  1099. btns[i] = NULL;
  1100. fixedHero = s.hero != -1; //if we doesn't start with "random hero" it must be fixed or none
  1101. selectButtons(false);
  1102. if(owner->type != scenarioInfo && curMap->players[s.color].canHumanPlay)
  1103. {
  1104. flag = new AdventureMapButton(CGI->generaltexth->zelp[180], bind(&OptionsTab::flagPressed, owner, s.serial), -43, 2, flags[s.color]);
  1105. flag->hoverable = true;
  1106. }
  1107. else
  1108. flag = NULL;
  1109. defActions &= ~SHARE_POS;
  1110. town = new SelectedBox(TOWN, s.serial);
  1111. town->pos += pos + Point(119, 2);
  1112. hero = new SelectedBox(HERO, s.serial);
  1113. hero->pos += pos + Point(195, 2);
  1114. bonus = new SelectedBox(BONUS, s.serial);
  1115. bonus->pos += pos + Point(271, 2);
  1116. }
  1117. void OptionsTab::PlayerOptionsEntry::showAll( SDL_Surface * to )
  1118. {
  1119. CIntObject::showAll(to);
  1120. printAtMiddleLoc(s.name, 55, 10, FONT_SMALL, zwykly, to);
  1121. }
  1122. void OptionsTab::PlayerOptionsEntry::selectButtons(bool onlyHero)
  1123. {
  1124. if(!btns[0])
  1125. return;
  1126. if(!onlyHero && s.castle != -1)
  1127. {
  1128. btns[0]->disable();
  1129. btns[1]->disable();
  1130. }
  1131. if(fixedHero || !s.human || s.castle < 0)
  1132. {
  1133. btns[2]->disable();
  1134. btns[3]->disable();
  1135. }
  1136. else
  1137. {
  1138. btns[2]->enable(active);
  1139. btns[3]->enable(active);
  1140. }
  1141. }
  1142. void OptionsTab::SelectedBox::showAll( SDL_Surface * to )
  1143. {
  1144. PlayerSettings &s = curOpts->playerInfos[player];
  1145. SDL_Surface *toBlit = getImg();
  1146. const std::string *toPrint = getText();
  1147. blitAt(toBlit, pos, to);
  1148. printAtMiddleLoc(*toPrint, 23, 39, FONT_TINY, zwykly, to);
  1149. }
  1150. OptionsTab::SelectedBox::SelectedBox( SelType Which, ui8 Player )
  1151. :which(Which), player(Player)
  1152. {
  1153. SDL_Surface *img = getImg();
  1154. pos.w = img->w;
  1155. pos.h = img->h;
  1156. used = RCLICK;
  1157. }
  1158. SDL_Surface * OptionsTab::SelectedBox::getImg() const
  1159. {
  1160. PlayerSettings &s = curOpts->playerInfos[player];
  1161. switch(which)
  1162. {
  1163. case TOWN:
  1164. if (s.castle < F_NUMBER && s.castle >= 0)
  1165. return graphics->getPic(s.castle, true, false);
  1166. else if (s.castle == -1)
  1167. return CGP->rTown;
  1168. else if (s.castle == -2)
  1169. return CGP->nTown;
  1170. case HERO:
  1171. if (s.hero == -1)
  1172. {
  1173. return CGP->rHero;
  1174. }
  1175. else if (s.hero == -2)
  1176. {
  1177. if(s.heroPortrait >= 0)
  1178. return graphics->portraitSmall[s.heroPortrait];
  1179. else
  1180. return CGP->nHero;
  1181. }
  1182. else
  1183. {
  1184. return graphics->portraitSmall[s.hero];
  1185. }
  1186. break;
  1187. case BONUS:
  1188. {
  1189. int pom;
  1190. switch (s.bonus)
  1191. {
  1192. case -1:
  1193. pom=10;
  1194. break;
  1195. case 0:
  1196. pom=9;
  1197. break;
  1198. case 1:
  1199. pom=8;
  1200. break;
  1201. case 2:
  1202. pom=CGI->townh->towns[s.castle].bonus;
  1203. break;
  1204. default:
  1205. assert(0);
  1206. }
  1207. return CGP->bonuses->ourImages[pom].bitmap;
  1208. }
  1209. default:
  1210. return NULL;
  1211. }
  1212. }
  1213. const std::string * OptionsTab::SelectedBox::getText() const
  1214. {
  1215. PlayerSettings &s = curOpts->playerInfos[player];
  1216. switch(which)
  1217. {
  1218. case TOWN:
  1219. if (s.castle < F_NUMBER && s.castle >= 0)
  1220. return &CGI->townh->towns[s.castle].Name();
  1221. else if (s.castle == -1)
  1222. return &CGI->generaltexth->allTexts[522];
  1223. else if (s.castle == -2)
  1224. return &CGI->generaltexth->allTexts[523];
  1225. case HERO:
  1226. if (s.hero == -1)
  1227. return &CGI->generaltexth->allTexts[522];
  1228. else if (s.hero == -2)
  1229. {
  1230. if(s.heroPortrait >= 0)
  1231. {
  1232. if(s.heroName.length())
  1233. return &s.heroName;
  1234. else
  1235. return &CGI->heroh->heroes[s.heroPortrait]->name;
  1236. }
  1237. else
  1238. return &CGI->generaltexth->allTexts[523];
  1239. }
  1240. else
  1241. {
  1242. //if(s.heroName.length())
  1243. // return &s.heroName;
  1244. //else
  1245. return &CGI->heroh->heroes[s.hero]->name;
  1246. }
  1247. case BONUS:
  1248. switch (s.bonus)
  1249. {
  1250. case -1:
  1251. return &CGI->generaltexth->allTexts[522];
  1252. default:
  1253. return &CGI->generaltexth->arraytxt[214 + s.bonus];
  1254. }
  1255. default:
  1256. return NULL;
  1257. }
  1258. }
  1259. void OptionsTab::SelectedBox::clickRight( tribool down, bool previousState )
  1260. {
  1261. if(indeterminate(down) || !down) return;
  1262. PlayerSettings &s = curOpts->playerInfos[player];
  1263. SDL_Surface *bmp = NULL;
  1264. const std::string *title = NULL, *subTitle = NULL;
  1265. subTitle = getText();
  1266. int val;
  1267. switch(which)
  1268. {
  1269. case TOWN: val = s.castle; break;
  1270. case HERO:
  1271. val = s.hero;
  1272. if(val < 0)
  1273. {
  1274. int p9 = curMap->players[s.color].p9;
  1275. if(p9 != 255)
  1276. val = p9;
  1277. }
  1278. break;
  1279. case BONUS: val = s.bonus; break;
  1280. }
  1281. if(val == -1 || which == BONUS) //random or bonus box
  1282. {
  1283. bmp = CMessage::drawBox1(256, 190);
  1284. std::string *description = NULL;
  1285. switch(which)
  1286. {
  1287. case TOWN:
  1288. title = &CGI->generaltexth->allTexts[103];
  1289. description = &CGI->generaltexth->allTexts[104];
  1290. break;
  1291. case HERO:
  1292. title = &CGI->generaltexth->allTexts[101];
  1293. description = &CGI->generaltexth->allTexts[102];
  1294. break;
  1295. case BONUS:
  1296. {
  1297. switch(val)
  1298. {
  1299. case brandom:
  1300. title = &CGI->generaltexth->allTexts[86]; //{Random Bonus}
  1301. description = &CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  1302. break;
  1303. case bartifact:
  1304. title = &CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  1305. description = &CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  1306. break;
  1307. case bgold:
  1308. title = &CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  1309. subTitle = &CGI->generaltexth->allTexts[87]; //500-1000
  1310. description = &CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  1311. break;
  1312. case bresource:
  1313. {
  1314. title = &CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  1315. switch(CGI->townh->towns[s.castle].primaryRes)
  1316. {
  1317. case 1:
  1318. subTitle = &CGI->generaltexth->allTexts[694];
  1319. description = &CGI->generaltexth->allTexts[690];
  1320. break;
  1321. case 3:
  1322. subTitle = &CGI->generaltexth->allTexts[695];
  1323. description = &CGI->generaltexth->allTexts[691];
  1324. break;
  1325. case 4:
  1326. subTitle = &CGI->generaltexth->allTexts[692];
  1327. description = &CGI->generaltexth->allTexts[688];
  1328. break;
  1329. case 5:
  1330. subTitle = &CGI->generaltexth->allTexts[693];
  1331. description = &CGI->generaltexth->allTexts[689];
  1332. break;
  1333. case 127:
  1334. subTitle = &CGI->generaltexth->allTexts[89]; //5-10 wood / 5-10 ore
  1335. description = &CGI->generaltexth->allTexts[93]; //At the start of the game, 5-10 wood and 5-10 ore are added to your Kingdom's resource pool
  1336. break;
  1337. }
  1338. }
  1339. break;
  1340. }
  1341. }
  1342. break;
  1343. }
  1344. if(description)
  1345. CSDL_Ext::printAtMiddleWB(*description, 125, 145, FONT_SMALL, 37, zwykly, bmp);
  1346. }
  1347. else if(val == -2)
  1348. {
  1349. return;
  1350. }
  1351. else if(which == TOWN)
  1352. {
  1353. bmp = CMessage::drawBox1(256, 319);
  1354. title = &CGI->generaltexth->allTexts[80];
  1355. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[79], 135, 137, FONT_MEDIUM, tytulowy, bmp);
  1356. const CTown &t = CGI->townh->towns[val];
  1357. //print creatures
  1358. int x = 60, y = 159;
  1359. for(int i = 0; i < 7; i++)
  1360. {
  1361. int c = t.basicCreatures[i];
  1362. blitAt(graphics->smallImgs[c], x, y, bmp);
  1363. CSDL_Ext::printAtMiddleWB(CGI->creh->creatures[c].nameSing, x + 16, y + 45, FONT_TINY, 10, zwykly, bmp);
  1364. if(i == 2)
  1365. {
  1366. x = 40;
  1367. y += 76;
  1368. }
  1369. else
  1370. {
  1371. x += 52;
  1372. }
  1373. }
  1374. }
  1375. else if(val >= 0)
  1376. {
  1377. const CHero *h = CGI->heroh->heroes[val];
  1378. bmp = CMessage::drawBox1(320, 255);
  1379. title = &CGI->generaltexth->allTexts[77];
  1380. CSDL_Ext::printAtMiddle(*title, 167, 36, FONT_MEDIUM, tytulowy, bmp);
  1381. CSDL_Ext::printAtMiddle(*subTitle + " - " + h->heroClass->name, 160, 99, FONT_SMALL, zwykly, bmp);
  1382. blitAt(getImg(), 136, 56, bmp);
  1383. //print specialty
  1384. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[78], 166, 132, FONT_MEDIUM, tytulowy, bmp);
  1385. blitAt(graphics->un44->ourImages[val].bitmap, 140, 150, bmp);
  1386. GH.pushInt(new CInfoPopup(bmp, true));
  1387. return;
  1388. }
  1389. if(title)
  1390. CSDL_Ext::printAtMiddle(*title, 135, 36, FONT_MEDIUM, tytulowy, bmp);
  1391. if(subTitle)
  1392. CSDL_Ext::printAtMiddle(*subTitle, 127, 103, FONT_SMALL, zwykly, bmp);
  1393. blitAt(getImg(), 104, 60, bmp);
  1394. GH.pushInt(new CInfoPopup(bmp, true));
  1395. }
  1396. CScenarioInfo::CScenarioInfo( const CMapHeader *mapInfo, const StartInfo *startInfo )
  1397. {
  1398. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1399. for(size_t i = 0; i < startInfo->playerInfos.size(); i++)
  1400. if(startInfo->playerInfos[i].human)
  1401. playerColor = startInfo->playerInfos[i].color;
  1402. pos.w = 762;
  1403. pos.h = 584;
  1404. center(pos);
  1405. curMap = mapInfo;
  1406. curOpts = (StartInfo*)startInfo;
  1407. card = new InfoCard(scenarioInfo);
  1408. opt = new OptionsTab(scenarioInfo);
  1409. opt->changeSelection(0);
  1410. card->difficulty->select(startInfo->difficulty, 0);
  1411. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 584, 535, "SCNRBACK.DEF", SDLK_ESCAPE);
  1412. }
  1413. CScenarioInfo::~CScenarioInfo()
  1414. {
  1415. }
  1416. CTextInput::CTextInput()
  1417. {
  1418. bg = NULL;
  1419. used = 0;
  1420. }
  1421. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1422. :cb(CB)
  1423. {
  1424. pos += Pos;
  1425. OBJ_CONSTRUCTION;
  1426. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1427. used = LCLICK | KEYBOARD;
  1428. }
  1429. CTextInput::~CTextInput()
  1430. {
  1431. }
  1432. void CTextInput::showAll( SDL_Surface * to )
  1433. {
  1434. CIntObject::showAll(to);
  1435. CSDL_Ext::printAt(text + "_", pos.x, pos.y, FONT_SMALL, zwykly, to);
  1436. }
  1437. void CTextInput::clickLeft( tribool down, bool previousState )
  1438. {
  1439. //TODO
  1440. }
  1441. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1442. {
  1443. if(key.state != SDL_PRESSED) return;
  1444. switch(key.keysym.sym)
  1445. {
  1446. case SDLK_BACKSPACE:
  1447. if(text.size())
  1448. text.resize(text.size()-1);
  1449. break;
  1450. default:
  1451. char c = key.keysym.unicode;
  1452. if(std::isprint(c))
  1453. text += c;
  1454. break;
  1455. }
  1456. redraw();
  1457. cb(text);
  1458. }
  1459. void CTextInput::setText( const std::string &nText, bool callCb )
  1460. {
  1461. text = nText;
  1462. redraw();
  1463. if(callCb)
  1464. cb(text);
  1465. }