CPreGame.cpp 46 KB

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