CPreGame.cpp 44 KB

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