2
0

CPreGame.cpp 44 KB

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