CPreGame.cpp 44 KB

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