CPreGame.cpp 51 KB

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