CPreGame.cpp 46 KB

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