CPreGame.cpp 44 KB

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