CPreGame.cpp 63 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291
  1. #include "../stdafx.h"
  2. #include "CPreGame.h"
  3. #include <ctime>
  4. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  5. #include <boost/algorithm/string.hpp>
  6. #include <zlib.h>
  7. #include "../timeHandler.h"
  8. #include <sstream>
  9. #include "SDL_Extensions.h"
  10. #include "CGameInfo.h"
  11. #include "CCursorHandler.h"
  12. #include "../hch/CDefHandler.h"
  13. #include "../hch/CGeneralTextHandler.h"
  14. #include "../hch/CLodHandler.h"
  15. #include "../hch/CTownHandler.h"
  16. #include "../hch/CHeroHandler.h"
  17. #include "../hch/CObjectHandler.h"
  18. #include "../hch/CCampaignHandler.h"
  19. #include "../hch/CCreatureHandler.h"
  20. #include "../hch/CMusicHandler.h"
  21. #include "../hch/CVideoHandler.h"
  22. #include <cmath>
  23. #include "Graphics.h"
  24. //#include <boost/thread.hpp>
  25. #include <boost/bind.hpp>
  26. #include <cstdlib>
  27. #include "../lib/Connection.h"
  28. #include "../lib/VCMIDirs.h"
  29. #include "../lib/map.h"
  30. #include "AdventureMapButton.h"
  31. #include "GUIClasses.h"
  32. #include "CPlayerInterface.h"
  33. #include "../CCallback.h"
  34. #include <boost/lexical_cast.hpp>
  35. #include <cstdlib>
  36. #include "CMessage.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 CMapInfo *curMap;
  52. static StartInfo *curOpts;
  53. static int playerColor, playerSerial; //if more than one player - applies to the first
  54. static std::vector<std::string> playerNames; // serial id of name <-> player name
  55. static std::string selectedName; //set when game is started/loaded
  56. static void do_quit()
  57. {
  58. SDL_Event event;
  59. event.quit.type = SDL_QUIT;
  60. SDL_PushEvent(&event);
  61. }
  62. void CMapInfo::countPlayers()
  63. {
  64. playerAmnt = humenPlayers = 0;
  65. for(int i=0;i<PLAYER_LIMIT;i++)
  66. {
  67. if(mapHeader->players[i].canHumanPlay)
  68. {
  69. playerAmnt++;
  70. humenPlayers++;
  71. }
  72. else if(mapHeader->players[i].canComputerPlay)
  73. {
  74. playerAmnt++;
  75. }
  76. }
  77. }
  78. //CMapInfo::CMapInfo(const std::string &fname, const unsigned char *map )
  79. //: mapHeader(NULL), campaignHeader(NULL)
  80. //{
  81. // init(fname, map);
  82. //}
  83. CMapInfo::CMapInfo(bool map)
  84. : mapHeader(NULL), campaignHeader(NULL)
  85. {
  86. }
  87. void CMapInfo::mapInit(const std::string &fname, const unsigned char *map )
  88. {
  89. filename = fname;
  90. int i = 0;
  91. mapHeader = new CMapHeader();
  92. mapHeader->version = CMapHeader::invalid;
  93. mapHeader->initFromMemory(map, i);
  94. countPlayers();
  95. }
  96. CMapInfo::~CMapInfo()
  97. {
  98. delete mapHeader;
  99. delete campaignHeader;
  100. }
  101. void CMapInfo::campaignInit()
  102. {
  103. campaignHeader = new CCampaignHeader( CCampaignHandler::getHeader(filename) );
  104. }
  105. CMenuScreen::CMenuScreen( EState which )
  106. {
  107. OBJ_CONSTRUCTION;
  108. bgAd = NULL;
  109. switch(which)
  110. {
  111. case mainMenu:
  112. {
  113. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[3].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[newGame])), 540, 10, "ZMENUNG.DEF", SDLK_n);
  114. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[4].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[loadGame])), 532, 132, "ZMENULG.DEF", SDLK_l);
  115. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[5].second, 0, 524, 251, "ZMENUHS.DEF", SDLK_h);
  116. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[6].second, 0 /*cb*/, 557, 359, "ZMENUCR.DEF", SDLK_c);
  117. boost::function<void()> confWindow = bind(CInfoWindow::showYesNoDialog, ref(CGI->generaltexth->allTexts[69]), (const std::vector<SComponent*>*)0, do_quit, 0, false, 1);
  118. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[7].second, confWindow, 586, 468, "ZMENUQT.DEF", SDLK_ESCAPE);
  119. }
  120. break;
  121. case newGame:
  122. {
  123. bgAd = new CPicture(BitmapHandler::loadBitmap("ZNEWGAM.bmp"), 114, 312, true);
  124. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, newGame), 545, 4, "ZTSINGL.DEF", SDLK_s);
  125. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, &pushIntT<CMultiMode>, 568, 120, "ZTMULTI.DEF", SDLK_m);
  126. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[campaignMain])), 541, 233, "ZTCAMPN.DEF", SDLK_c);
  127. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  128. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  129. }
  130. break;
  131. case loadGame:
  132. {
  133. bgAd = new CPicture(BitmapHandler::loadBitmap("ZLOADGAM.bmp"), 114, 312, true);
  134. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, loadGame), 545, 4, "ZTSINGL.DEF", SDLK_s);
  135. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, 0 /*cb*/, 568, 120, "ZTMULTI.DEF", SDLK_m);
  136. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, 0 /*cb*/, 541, 233, "ZTCAMPN.DEF", SDLK_c);
  137. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  138. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  139. }
  140. break;
  141. case campaignMain:
  142. {
  143. buttons[0] = new AdventureMapButton("", "", 0 /*cb*/, 535, 8, "ZSSSOD.DEF", SDLK_s);
  144. buttons[1] = new AdventureMapButton("", "", 0 /*cb*/, 494, 117, "ZSSROE.DEF", SDLK_m);
  145. buttons[2] = new AdventureMapButton("", "", 0 /*cb*/, 486, 241, "ZSSARM.DEF", SDLK_c);
  146. buttons[3] = new AdventureMapButton("", "", bind(&CGPreGame::openSel, CGP, campaignList), 550, 358, "ZSSCUS.DEF", SDLK_t);
  147. buttons[4] = new AdventureMapButton("", "", bind(&CMenuScreen::moveTo, this, CGP->scrs[newGame]), 582, 464, "ZSSEXIT.DEF", SDLK_ESCAPE);
  148. ////just for testing
  149. CCampaignHandler * ch = new CCampaignHandler();
  150. ch->getCampaignHeaders();
  151. //ch->getCampaign("./Maps/ALEXIS.h3c");
  152. }
  153. break;
  154. }
  155. for(int i = 0; i < ARRAY_COUNT(buttons); i++)
  156. buttons[i]->hoverable = true;
  157. }
  158. CMenuScreen::~CMenuScreen()
  159. {
  160. }
  161. void CMenuScreen::showAll( SDL_Surface * to )
  162. {
  163. blitAt(CGP->mainbg, 0, 0, to);
  164. CIntObject::showAll(to);
  165. }
  166. void CMenuScreen::show( SDL_Surface * to )
  167. {
  168. CIntObject::show(to);
  169. CGI->videoh->update(pos.x + 8, pos.y + 105, screen, true, false);
  170. }
  171. void CMenuScreen::moveTo( CMenuScreen *next )
  172. {
  173. GH.popInt(this);
  174. GH.pushInt(next);
  175. }
  176. CGPreGame::CGPreGame()
  177. {
  178. GH.defActionsDef = 63;
  179. CGP = this;
  180. mainbg = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  181. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  182. scrs[i] = new CMenuScreen((CMenuScreen::EState)i);
  183. }
  184. CGPreGame::~CGPreGame()
  185. {
  186. SDL_FreeSurface(mainbg);
  187. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  188. delete scrs[i];
  189. }
  190. void CGPreGame::openSel( CMenuScreen::EState type )
  191. {
  192. playerNames.push_back(CGI->generaltexth->allTexts[434]); //we have only one player and his name is "Player"
  193. GH.pushInt(new CSelectionScreen(type));
  194. }
  195. void CGPreGame::loadGraphics()
  196. {
  197. victory = CDefHandler::giveDef("SCNRVICT.DEF");
  198. loss = CDefHandler::giveDef("SCNRLOSS.DEF");
  199. bonuses = CDefHandler::giveDef("SCNRSTAR.DEF");
  200. rHero = BitmapHandler::loadBitmap("HPSRAND1.bmp");
  201. rTown = BitmapHandler::loadBitmap("HPSRAND0.bmp");
  202. nHero = BitmapHandler::loadBitmap("HPSRAND6.bmp");
  203. nTown = BitmapHandler::loadBitmap("HPSRAND5.bmp");
  204. }
  205. void CGPreGame::disposeGraphics()
  206. {
  207. delete victory;
  208. delete loss;
  209. SDL_FreeSurface(rHero);
  210. SDL_FreeSurface(nHero);
  211. SDL_FreeSurface(rTown);
  212. SDL_FreeSurface(nTown);
  213. }
  214. void CGPreGame::update()
  215. {
  216. if (GH.listInt.size() == 0)
  217. {
  218. #ifdef _WIN32
  219. CGI->videoh->open("ACREDIT.SMK");
  220. #else
  221. CGI->videoh->open("ACREDIT.SMK", true, false);
  222. #endif
  223. GH.pushInt(scrs[CMenuScreen::mainMenu]);
  224. }
  225. CGI->curh->draw1();
  226. SDL_Flip(screen);
  227. CGI->curh->draw2();
  228. GH.topInt()->show(screen);
  229. GH.updateTime();
  230. GH.handleEvents();
  231. }
  232. CSelectionScreen::CSelectionScreen(CMenuScreen::EState Type)
  233. {
  234. OBJ_CONSTRUCTION_CAPTURING_ALL;
  235. IShowActivable::type = BLOCK_ADV_HOTKEYS;
  236. pos.w = 762;
  237. pos.h = 584;
  238. if(Type == CMenuScreen::saveGame)
  239. {
  240. center(pos);
  241. }
  242. else if(Type == CMenuScreen::campaignList)
  243. {
  244. bg = new CPicture(BitmapHandler::loadBitmap("CamCust.bmp"), 0, 0, true);
  245. pos.x = 3;
  246. pos.y = 6;
  247. }
  248. else
  249. {
  250. pos.x = 3;
  251. pos.y = 6;
  252. bg = new CPicture(BitmapHandler::loadBitmap(rand()%2 ? "ZPIC1000.bmp" : "ZPIC1001.bmp"), -3, -6, true);
  253. }
  254. CGP->loadGraphics();
  255. type = Type;
  256. curOpts = &sInfo;
  257. sInfo.difficulty = 1;
  258. current = NULL;
  259. sInfo.mode = (Type == CMenuScreen::newGame ? 0 : 1);
  260. sInfo.turnTime = 0;
  261. curTab = NULL;
  262. card = new InfoCard(type); //right info card
  263. if (type == CMenuScreen::campaignList)
  264. {
  265. opt = NULL;
  266. }
  267. else
  268. {
  269. opt = new OptionsTab(type/*, sInfo*/); //scenario options tab
  270. opt->recActions = DISPOSE;
  271. }
  272. sel = new SelectionTab(type, bind(&CSelectionScreen::changeSelection, this, _1)); //scenario selection tab
  273. sel->recActions = DISPOSE;
  274. switch(type)
  275. {
  276. case CMenuScreen::newGame:
  277. {
  278. card->difficulty->onChange = bind(&CSelectionScreen::difficultyChange, this, _1);
  279. card->difficulty->select(1, 0);
  280. AdventureMapButton *select = new AdventureMapButton(CGI->generaltexth->zelp[45], bind(&CSelectionScreen::toggleTab, this, sel), 411, 75, "GSPBUTT.DEF", SDLK_s);
  281. select->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL);
  282. AdventureMapButton *opts = new AdventureMapButton(CGI->generaltexth->zelp[46], bind(&CSelectionScreen::toggleTab, this, opt), 411, 503, "GSPBUTT.DEF", SDLK_a);
  283. opts->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL);
  284. AdventureMapButton *random = new AdventureMapButton(CGI->generaltexth->zelp[47], bind(&CSelectionScreen::toggleTab, this, sel), 411, 99, "GSPBUTT.DEF", SDLK_r);
  285. random->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL);
  286. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRBEG.DEF", SDLK_b);
  287. }
  288. break;
  289. case CMenuScreen::loadGame:
  290. sel->recActions = 255;
  291. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRLOD.DEF", SDLK_l);
  292. break;
  293. case CMenuScreen::saveGame:
  294. sel->recActions = 255;
  295. start = new AdventureMapButton("", CGI->generaltexth->zelp[103].second, bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRSAV.DEF");
  296. break;
  297. case CMenuScreen::campaignList:
  298. sel->recActions = 255;
  299. start = new AdventureMapButton(std::pair<std::string, std::string>(), bind(&CSelectionScreen::startCampaign, this), 411, 529, "SCNRLOD.DEF", SDLK_b);
  300. break;
  301. }
  302. start->assignedKeys.insert(SDLK_RETURN);
  303. std::string backName;
  304. if(Type == CMenuScreen::campaignList)
  305. {
  306. backName = "SCNRBACK.DEF";
  307. }
  308. else
  309. {
  310. backName = "SCNRBACK.DEF";
  311. }
  312. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 581, 529, backName, SDLK_ESCAPE);
  313. }
  314. CSelectionScreen::~CSelectionScreen()
  315. {
  316. curMap = NULL;
  317. curOpts = NULL;
  318. playerSerial = playerColor = -1;
  319. playerNames.clear();
  320. }
  321. void CSelectionScreen::toggleTab(CIntObject *tab)
  322. {
  323. if(curTab && curTab->active)
  324. {
  325. curTab->deactivate();
  326. curTab->recActions = DISPOSE;
  327. }
  328. if(curTab != tab)
  329. {
  330. tab->recActions = 255;
  331. tab->activate();
  332. curTab = tab;
  333. }
  334. else
  335. {
  336. curTab = NULL;
  337. };
  338. GH.totalRedraw();
  339. }
  340. void CSelectionScreen::changeSelection( const CMapInfo *to )
  341. {
  342. curMap = current = to;;
  343. if(to && type == CMenuScreen::loadGame)
  344. curOpts->difficulty = to->seldiff;
  345. if(type != CMenuScreen::campaignList)
  346. {
  347. updateStartInfo(to);
  348. }
  349. card->changeSelection(to);
  350. if(type != CMenuScreen::campaignList)
  351. {
  352. opt->changeSelection(to->mapHeader);
  353. }
  354. }
  355. void setPlayer(PlayerSettings &pset, unsigned player)
  356. {
  357. if(player < playerNames.size())
  358. {
  359. pset.name = playerNames[player];
  360. pset.human = true;
  361. if(playerColor < 0)
  362. {
  363. playerColor = pset.color;
  364. playerSerial = pset.serial;
  365. }
  366. }
  367. else
  368. {
  369. pset.name = CGI->generaltexth->allTexts[468];//Computer
  370. pset.human = false;
  371. }
  372. }
  373. void CSelectionScreen::updateStartInfo( const CMapInfo * to )
  374. {
  375. sInfo.playerInfos.clear();
  376. if(!to)
  377. return;
  378. sInfo.playerInfos.resize(to->playerAmnt);
  379. sInfo.mapname = to->filename;
  380. playerSerial = playerColor = -1;
  381. ui8 placedPlayers = 0;
  382. int serialC=0;
  383. for (int i = 0; i < PLAYER_LIMIT; i++)
  384. {
  385. const PlayerInfo &pinfo = to->mapHeader->players[i];
  386. //neither computer nor human can play - no player
  387. if (!(pinfo.canComputerPlay || pinfo.canComputerPlay))
  388. continue;
  389. PlayerSettings &pset = sInfo.playerInfos[serialC];
  390. pset.color = i;
  391. pset.serial = serialC++;
  392. setPlayer(pset, placedPlayers++);
  393. 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
  394. {
  395. if((1 << j) & pinfo.allowedFactions)
  396. {
  397. if (pset.castle >= 0) //we've already assigned a castle and another one is possible -> set random and let player choose
  398. pset.castle = -1; //breaks
  399. if (pset.castle == -2) //first available castle - pick
  400. pset.castle = j;
  401. }
  402. }
  403. if ((pinfo.generateHeroAtMainTown || to->mapHeader->version==CMapHeader::RoE) && pinfo.hasMainTown //we will generate hero in front of main town
  404. || pinfo.p8) //random hero
  405. pset.hero = -1;
  406. else
  407. pset.hero = -2;
  408. if(pinfo.mainHeroName.length())
  409. {
  410. pset.heroName = pinfo.mainHeroName;
  411. if((pset.heroPortrait = pinfo.mainHeroPortrait) == 255)
  412. pset.heroPortrait = pinfo.p9;
  413. }
  414. pset.handicap = 0;
  415. }
  416. }
  417. void CSelectionScreen::startCampaign()
  418. {
  419. CCampaign * ourCampaign = CCampaignHandler::getCampaign(curMap->filename);
  420. GH.pushInt( new CBonusSelection(ourCampaign, 0) );
  421. }
  422. void CSelectionScreen::startGame()
  423. {
  424. if(type != CMenuScreen::saveGame)
  425. {
  426. if(!current)
  427. return;
  428. selectedName = sInfo.mapname;
  429. StartInfo *si = new StartInfo(sInfo);
  430. GH.popInt(this);
  431. GH.popInt(GH.topInt());
  432. curMap = NULL;
  433. curOpts = NULL;
  434. ::startGame(si);
  435. }
  436. else
  437. {
  438. if(!(sel && sel->txt && sel->txt->text.size()))
  439. return;
  440. selectedName = GVCMIDirs.UserPath + "/Games/" + sel->txt->text + ".vlgm1";
  441. LOCPLINT->cb->save(sel->txt->text);
  442. GH.popInt(this);
  443. }
  444. }
  445. void CSelectionScreen::difficultyChange( int to )
  446. {
  447. assert(type == CMenuScreen::newGame);
  448. sInfo.difficulty = to;
  449. GH.totalRedraw();
  450. }
  451. // A new size filter (Small, Medium, ...) has been selected. Populate
  452. // selMaps with the relevant data.
  453. void SelectionTab::filter( int size, bool selectFirst )
  454. {
  455. curItems.clear();
  456. if(tabType == CMenuScreen::campaignList)
  457. {
  458. for (size_t i=0; i<allItems.size(); i++)
  459. curItems.push_back(&allItems[i]);
  460. }
  461. else
  462. {
  463. for (size_t i=0; i<allItems.size(); i++)
  464. if( allItems[i].mapHeader && allItems[i].mapHeader->version && (!size || allItems[i].mapHeader->width == size))
  465. curItems.push_back(&allItems[i]);
  466. }
  467. if(curItems.size())
  468. {
  469. slider->block(false);
  470. slider->setAmount(curItems.size());
  471. sort();
  472. if(selectFirst)
  473. {
  474. slider->moveTo(0);
  475. onSelect(curItems[0]);
  476. }
  477. selectAbs(0);
  478. }
  479. else
  480. {
  481. slider->block(true);
  482. onSelect(NULL);
  483. }
  484. }
  485. void SelectionTab::getFiles(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext)
  486. {
  487. if(!boost::filesystem::exists(dirname))
  488. {
  489. tlog1 << "Cannot find " << dirname << " directory!\n";
  490. }
  491. fs::path tie(dirname);
  492. fs::directory_iterator end_iter;
  493. for ( fs::directory_iterator file (tie); file!=end_iter; ++file )
  494. {
  495. if(fs::is_regular_file(file->status())
  496. && boost::ends_with(file->path().filename(), ext))
  497. {
  498. out.resize(out.size()+1);
  499. out.back().date = fs::last_write_time(file->path());
  500. out.back().name = file->path().string();
  501. }
  502. }
  503. allItems.resize(out.size());
  504. }
  505. void SelectionTab::parseMaps(std::vector<FileInfo> &files, int start, int threads)
  506. {
  507. int read=0;
  508. unsigned char mapBuffer[1500];
  509. while(start < allItems.size())
  510. {
  511. gzFile tempf = gzopen(files[start].name.c_str(),"rb");
  512. read = gzread(tempf, mapBuffer, 1500);
  513. gzclose(tempf);
  514. if(read < 50 || !mapBuffer[4])
  515. {
  516. tlog3 << "\t\tWarning: corrupted map file: " << files[start].name << std::endl;
  517. }
  518. else //valid map
  519. {
  520. allItems[start].mapInit(files[start].name, mapBuffer);
  521. //allItems[start].date = "DATEDATE";// files[start].date;
  522. }
  523. start += threads;
  524. }
  525. }
  526. void SelectionTab::parseGames(std::vector<FileInfo> &files)
  527. {
  528. for(int i=0; i<files.size(); i++)
  529. {
  530. CLoadFile lf(files[i].name);
  531. if(!lf.sfile)
  532. continue;
  533. ui8 sign[8];
  534. lf >> sign;
  535. if(std::memcmp(sign,"VCMISVG",7))
  536. {
  537. tlog1 << files[i].name << " is not a correct savefile!" << std::endl;
  538. continue;
  539. }
  540. allItems[i].mapHeader = new CMapHeader();
  541. lf >> *(allItems[i].mapHeader) >> allItems[i].seldiff;
  542. allItems[i].filename = files[i].name;
  543. allItems[i].countPlayers();
  544. allItems[i].date = std::asctime(std::localtime(&files[i].date));
  545. }
  546. }
  547. void SelectionTab::parseCampaigns( std::vector<FileInfo> & files )
  548. {
  549. for(int i=0; i<files.size(); i++)
  550. {
  551. allItems[i].date = std::asctime(std::localtime(&files[i].date));
  552. allItems[i].filename = files[i].name;
  553. allItems[i].campaignInit();
  554. }
  555. }
  556. SelectionTab::SelectionTab(CMenuScreen::EState Type, const boost::function<void(CMapInfo *)> &OnSelect)
  557. :onSelect(OnSelect), bg(NULL)
  558. {
  559. OBJ_CONSTRUCTION;
  560. selectionPos = 0;
  561. used = LCLICK | WHEEL | KEYBOARD | DOUBLECLICK;
  562. slider = NULL;
  563. txt = NULL;
  564. tabType = Type;
  565. if (Type != CMenuScreen::campaignList)
  566. {
  567. bg = new CPicture(BitmapHandler::loadBitmap("SCSELBCK.bmp"), 0, 0, true);
  568. pos.w = bg->pos.w;
  569. pos.h = bg->pos.h;
  570. }
  571. else
  572. {
  573. SDL_Surface * tmp1 = BitmapHandler::loadBitmap("CAMCUST.bmp");
  574. SDL_Surface * tmp = CSDL_Ext::newSurface(400, tmp1->h);
  575. blitAt(tmp1, 0, 0, tmp);
  576. SDL_FreeSurface(tmp1);
  577. bg = new CPicture(tmp, 0, 0, true);
  578. pos.w = bg->pos.w;
  579. pos.h = bg->pos.h;
  580. bg->pos.x = bg->pos.y = 0;
  581. }
  582. std::vector<FileInfo> toParse;
  583. switch(tabType)
  584. {
  585. case CMenuScreen::newGame:
  586. getFiles(toParse, DATA_DIR "/Maps", "h3m"); //get all maps
  587. parseMaps(toParse);
  588. positions = 18;
  589. break;
  590. case CMenuScreen::loadGame:
  591. case CMenuScreen::saveGame:
  592. getFiles(toParse, GVCMIDirs.UserPath + "/Games", "vlgm1"); //get all saves
  593. parseGames(toParse);
  594. if(tabType == CMenuScreen::loadGame)
  595. {
  596. positions = 18;
  597. }
  598. else
  599. {
  600. positions = 16;
  601. }
  602. if(tabType == CMenuScreen::saveGame)
  603. txt = new CTextInput(Rect(32, 539, 350, 20), Point(-32, -25), "GSSTRIP.bmp", 0);
  604. break;
  605. case CMenuScreen::campaignList:
  606. getFiles(toParse, DATA_DIR "/Maps", "h3c"); //get all campaigns
  607. parseCampaigns(toParse);
  608. positions = 18;
  609. break;
  610. default:
  611. assert(0);
  612. break;
  613. }
  614. if (tabType != CMenuScreen::campaignList)
  615. {
  616. //size filter buttons
  617. {
  618. int sizes[] = {36, 72, 108, 144, 0};
  619. const char * names[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"};
  620. for(int i = 0; i < 5; i++)
  621. new AdventureMapButton("", CGI->generaltexth->zelp[54+i].second, bind(&SelectionTab::filter, this, sizes[i], true), 158 + 47*i, 46, names[i]);
  622. }
  623. //sort buttons buttons
  624. {
  625. int xpos[] = {23, 55, 88, 121, 306, 339};
  626. const char * names[] = {"SCBUTT1.DEF", "SCBUTT2.DEF", "SCBUTCP.DEF", "SCBUTT3.DEF", "SCBUTT4.DEF", "SCBUTT5.DEF"};
  627. for(int i = 0; i < 6; i++)
  628. new AdventureMapButton("", CGI->generaltexth->zelp[107+i].second, bind(&SelectionTab::sortBy, this, i), xpos[i], 86, names[i]);
  629. }
  630. }
  631. else
  632. {
  633. //sort by buttons
  634. new AdventureMapButton("", "", bind(&SelectionTab::sortBy, this, _numOfMaps), 23, 86, "CamCusM.DEF"); //by num of maps
  635. new AdventureMapButton("", "", bind(&SelectionTab::sortBy, this, _name), 55, 86, "CamCusL.DEF"); //by name
  636. }
  637. slider = new CSlider(372, 86, tabType != CMenuScreen::saveGame ? 480 : 430, bind(&SelectionTab::sliderMove, this, _1), positions, curItems.size(), 0, false, 1);
  638. format = CDefHandler::giveDef("SCSELC.DEF");
  639. sortingBy = _format;
  640. ascending = true;
  641. filter(0);
  642. //select(0);
  643. switch(tabType)
  644. {
  645. case CMenuScreen::newGame:
  646. selectFName(DATA_DIR "/Maps/Arrogance.h3m");
  647. break;
  648. case CMenuScreen::loadGame:
  649. case CMenuScreen::campaignList:
  650. select(0);
  651. break;
  652. case CMenuScreen::saveGame:;
  653. if(selectedName.size())
  654. {
  655. if(selectedName[0] == 'M')
  656. txt->setText("NEWGAME");
  657. else
  658. selectFName(selectedName);
  659. }
  660. }
  661. }
  662. SelectionTab::~SelectionTab()
  663. {
  664. delete format;
  665. }
  666. void SelectionTab::sortBy( int criteria )
  667. {
  668. if(criteria == sortingBy)
  669. {
  670. ascending = !ascending;
  671. }
  672. else
  673. {
  674. sortingBy = (ESortBy)criteria;
  675. ascending = true;
  676. }
  677. sort();
  678. selectAbs(0);
  679. }
  680. void SelectionTab::sort()
  681. {
  682. if(sortingBy != _name)
  683. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(_name));
  684. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(sortingBy));
  685. if(!ascending)
  686. std::reverse(curItems.begin(), curItems.end());
  687. redraw();
  688. }
  689. void SelectionTab::select( int position )
  690. {
  691. if(!curItems.size()) return;
  692. // New selection. py is the index in curItems.
  693. int py = position + slider->value;
  694. amax(py, 0);
  695. amin(py, curItems.size()-1);
  696. selectionPos = py;
  697. if(position < 0)
  698. slider->moveTo(slider->value + position);
  699. else if(position >= positions)
  700. slider->moveTo(slider->value + position - positions + 1);
  701. onSelect(curItems[py]);
  702. }
  703. void SelectionTab::selectAbs( int position )
  704. {
  705. select(position - slider->value);
  706. }
  707. int SelectionTab::getPosition( int x, int y )
  708. {
  709. return -1;
  710. }
  711. void SelectionTab::sliderMove( int slidPos )
  712. {
  713. if(!slider) return; //ignore spurious call when slider is being created
  714. redraw();
  715. }
  716. // Display the tab with the scenario names
  717. //
  718. // elemIdx is the index of the maps or saved game to display on line 0
  719. // slider->capacity contains the number of available screen lines
  720. // slider->positionsAmnt is the number of elements after filtering
  721. void SelectionTab::printMaps(SDL_Surface *to)
  722. {
  723. int elemIdx = slider->value;
  724. // Display all elements if there's enough space
  725. //if(slider->amount < slider->capacity)
  726. // elemIdx = 0;
  727. SDL_Color itemColor;
  728. #define POS(xx, yy) pos.x + xx, pos.y + yy + line*25
  729. for (int line = 0; line < positions && elemIdx < curItems.size(); elemIdx++, line++)
  730. {
  731. CMapInfo* curMap = curItems[elemIdx];
  732. if (elemIdx == selectionPos)
  733. itemColor=tytulowy;
  734. else
  735. itemColor=zwykly;
  736. if(tabType != CMenuScreen::campaignList)
  737. {
  738. //amount of players
  739. std::ostringstream ostr(std::ostringstream::out);
  740. ostr << curMap->playerAmnt << "/" << curMap->humenPlayers;
  741. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, itemColor, to);
  742. //map size
  743. std::string temp2 = "C";
  744. switch (curMap->mapHeader->width)
  745. {
  746. case 36:
  747. temp2="S";
  748. break;
  749. case 72:
  750. temp2="M";
  751. break;
  752. case 108:
  753. temp2="L";
  754. break;
  755. case 144:
  756. temp2="XL";
  757. break;
  758. }
  759. CSDL_Ext::printAtMiddle(temp2, POS(70, 128), FONT_SMALL, itemColor, to);
  760. int temp=-1;
  761. switch (curMap->mapHeader->version)
  762. {
  763. case CMapHeader::RoE:
  764. temp=0;
  765. break;
  766. case CMapHeader::AB:
  767. temp=1;
  768. break;
  769. case CMapHeader::SoD:
  770. temp=2;
  771. break;
  772. case CMapHeader::WoG:
  773. temp=3;
  774. break;
  775. default:
  776. // Unknown version. Be safe and ignore that map
  777. tlog2 << "Warning: " << curMap->filename << " has wrong version!\n";
  778. continue;
  779. }
  780. blitAt(format->ourImages[temp].bitmap, POS(88, 117), to);
  781. //victory conditions
  782. if (curMap->mapHeader->victoryCondition.condition == winStandard)
  783. temp = 11;
  784. else
  785. temp = curMap->mapHeader->victoryCondition.condition;
  786. blitAt(CGP->victory->ourImages[temp].bitmap, POS(306, 117), to);
  787. //loss conditions
  788. if (curMap->mapHeader->lossCondition.typeOfLossCon == lossStandard)
  789. temp=3;
  790. else
  791. temp=curMap->mapHeader->lossCondition.typeOfLossCon;
  792. blitAt(CGP->loss->ourImages[temp].bitmap, POS(339, 117), to);
  793. }
  794. else //if campaign
  795. {
  796. //number of maps in campaign
  797. std::ostringstream ostr(std::ostringstream::out);
  798. ostr << CGI->generaltexth->campaignRegionNames[ curMap->campaignHeader->mapVersion ].size();
  799. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, itemColor, to);
  800. }
  801. std::string name;
  802. if(tabType != CMenuScreen::campaignList)
  803. {
  804. if (!curMap->mapHeader->name.length())
  805. curMap->mapHeader->name = "Unnamed";
  806. name = curMap->mapHeader->name;
  807. }
  808. else
  809. {
  810. name = curMap->campaignHeader->name;
  811. }
  812. //print name
  813. if (tabType == CMenuScreen::newGame || tabType == CMenuScreen::campaignList) {
  814. CSDL_Ext::printAtMiddle(name, POS(213, 128), FONT_SMALL, itemColor, to);
  815. } else
  816. CSDL_Ext::printAtMiddle(fs::basename(curMap->filename), POS(213, 128), FONT_SMALL, itemColor, to);
  817. }
  818. #undef POS
  819. }
  820. void SelectionTab::showAll( SDL_Surface * to )
  821. {
  822. CIntObject::showAll(to);
  823. printMaps(to);
  824. std::string title;
  825. switch(tabType) {
  826. case CMenuScreen::newGame:
  827. title = CGI->generaltexth->arraytxt[229];
  828. break;
  829. case CMenuScreen::loadGame:
  830. title = CGI->generaltexth->arraytxt[230];
  831. break;
  832. case CMenuScreen::saveGame:
  833. title = CGI->generaltexth->arraytxt[231];
  834. break;
  835. case CMenuScreen::campaignList:
  836. title = "Select a Campaign"; //TODO: find where is the title
  837. break;
  838. }
  839. CSDL_Ext::printAtMiddle(title, pos.x+205, pos.y+28, FONT_MEDIUM, tytulowy, to); //Select a Scenario to Play
  840. if(tabType != CMenuScreen::campaignList)
  841. {
  842. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[510], pos.x+87, pos.y+62, FONT_SMALL, tytulowy, to); //Map sizes
  843. }
  844. }
  845. void SelectionTab::clickLeft( tribool down, bool previousState )
  846. {
  847. if(down)
  848. {
  849. int line = getLine();
  850. if(line != -1)
  851. select(line);
  852. }
  853. }
  854. void SelectionTab::wheelScrolled( bool down, bool in )
  855. {
  856. slider->moveTo(slider->value + 3 * (down ? +1 : -1));
  857. //select(selectionPos - slider->value + (down ? +1 : -1));
  858. }
  859. void SelectionTab::keyPressed( const SDL_KeyboardEvent & key )
  860. {
  861. if(key.state != SDL_PRESSED) return;
  862. int moveBy = 0;
  863. switch(key.keysym.sym)
  864. {
  865. case SDLK_UP:
  866. moveBy = -1;
  867. break;
  868. case SDLK_DOWN:
  869. moveBy = +1;
  870. break;
  871. case SDLK_PAGEUP:
  872. moveBy = -positions+1;
  873. break;
  874. case SDLK_PAGEDOWN:
  875. moveBy = +positions-1;
  876. break;
  877. case SDLK_HOME:
  878. select(-slider->value);
  879. return;
  880. case SDLK_END:
  881. select(curItems.size() - slider->value);
  882. return;
  883. default:
  884. return;
  885. }
  886. select(selectionPos - slider->value + moveBy);
  887. }
  888. void SelectionTab::onDoubleClick()
  889. {
  890. if(getLine() != -1) //double clicked scenarios list
  891. {
  892. //act as if start button was pressed
  893. (static_cast<CSelectionScreen*>(parent))->start->callback();
  894. }
  895. }
  896. int SelectionTab::getLine()
  897. {
  898. int line = -1;
  899. Point clickPos(GH.current->button.x, GH.current->button.y);
  900. clickPos = clickPos - pos.topLeft();
  901. if (clickPos.y > 115 && clickPos.y < 564 && clickPos.x > 22 && clickPos.x < 371)
  902. {
  903. line = (clickPos.y-115) / 25; //which line
  904. }
  905. return line;
  906. }
  907. void SelectionTab::selectFName( const std::string &fname )
  908. {
  909. for(int i = curItems.size() - 1; i >= 0; i--)
  910. {
  911. if(curItems[i]->filename == fname)
  912. {
  913. slider->moveTo(i);
  914. selectAbs(i);
  915. return;
  916. }
  917. }
  918. selectAbs(0);
  919. }
  920. InfoCard::InfoCard( CMenuScreen::EState Type )
  921. : difficulty(NULL), sizes(NULL), sFlags(NULL), bg(NULL)
  922. {
  923. OBJ_CONSTRUCTION;
  924. pos.x += 393;
  925. used = RCLICK;
  926. type = Type;
  927. if(type == CMenuScreen::campaignList)
  928. {
  929. /*bg = new CPicture(BitmapHandler::loadBitmap("CamCust.bmp"), 0, 0, true);
  930. bg->pos.x = 0;
  931. bg->pos.y = 0;*/
  932. }
  933. else
  934. {
  935. bg = new CPicture(BitmapHandler::loadBitmap("GSELPOP1.bmp"), 0, 0, true);
  936. pos.w = bg->pos.w;
  937. pos.h = bg->pos.h;
  938. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  939. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  940. difficulty = new CHighlightableButtonsGroup(0);
  941. {
  942. static const char *difButns[] = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  943. BLOCK_CAPTURING;
  944. for(int i = 0; i < 5; i++)
  945. {
  946. difficulty->addButton(new CHighlightableButton("", CGI->generaltexth->zelp[24+i].second, 0, 110 + i*32, 450, difButns[i], i));
  947. difficulty->buttons.back()->pos += pos.topLeft();
  948. }
  949. }
  950. if(type != CMenuScreen::newGame)
  951. difficulty->block(true);
  952. }
  953. }
  954. InfoCard::~InfoCard()
  955. {
  956. delete sizes;
  957. delete sFlags;
  958. }
  959. void InfoCard::showAll( SDL_Surface * to )
  960. {
  961. CIntObject::showAll(to);
  962. //blit texts
  963. if(type != CMenuScreen::campaignList)
  964. {
  965. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 24, 400, FONT_SMALL, zwykly, to); //Allies
  966. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 190, 400, FONT_SMALL, zwykly, to); //Enemies
  967. printAtLoc(CGI->generaltexth->allTexts[494], 33, 430, FONT_SMALL, tytulowy, to);//"Map Diff:"
  968. printAtLoc(CGI->generaltexth->allTexts[492] + ":", 133,430, FONT_SMALL, tytulowy, to); //player difficulty
  969. printAtLoc(CGI->generaltexth->allTexts[218] + ":", 290,430, FONT_SMALL, tytulowy, to); //"Rating:"
  970. printAtLoc(CGI->generaltexth->allTexts[495], 26, 22, FONT_SMALL, tytulowy, to); //Scenario Name:
  971. printAtLoc(CGI->generaltexth->allTexts[496], 26, 132, FONT_SMALL, tytulowy, to); //Scenario Description:
  972. printAtLoc(CGI->generaltexth->allTexts[497], 26, 283, FONT_SMALL, tytulowy, to); //Victory Condition:
  973. printAtLoc(CGI->generaltexth->allTexts[498], 26, 339, FONT_SMALL, tytulowy, to); //Loss Condition:
  974. }
  975. if(curMap)
  976. {
  977. if(type != CMenuScreen::campaignList)
  978. {
  979. if(type != CMenuScreen::newGame)
  980. {
  981. for (int i = 0; i < difficulty->buttons.size(); i++)
  982. {
  983. //if(i == curMap->difficulty)
  984. // difficulty->buttons[i]->state = 3;
  985. //else
  986. // difficulty->buttons[i]->state = 2;
  987. difficulty->buttons[i]->showAll(to);
  988. }
  989. }
  990. //victory conditions
  991. int temp = curMap->mapHeader->victoryCondition.condition+1;
  992. if (temp>20) temp=0;
  993. std::string sss = CGI->generaltexth->victoryConditions[temp];
  994. if (temp && curMap->mapHeader->victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  995. printAtLoc(sss, 60, 307, FONT_SMALL, zwykly, to);
  996. //loss conditoins
  997. temp = curMap->mapHeader->lossCondition.typeOfLossCon+1;
  998. if (temp>20) temp=0;
  999. sss = CGI->generaltexth->lossCondtions[temp];
  1000. printAtLoc(sss, 60, 366, FONT_SMALL, zwykly, to);
  1001. //difficulty
  1002. assert(curMap->mapHeader->difficulty <= 4);
  1003. std::string &diff = CGI->generaltexth->arraytxt[142 + curMap->mapHeader->difficulty];
  1004. printAtMiddleLoc(diff, 62, 472, FONT_SMALL, zwykly, to);
  1005. //selecting size icon
  1006. switch (curMap->mapHeader->width)
  1007. {
  1008. case 36:
  1009. temp=0;
  1010. break;
  1011. case 72:
  1012. temp=1;
  1013. break;
  1014. case 108:
  1015. temp=2;
  1016. break;
  1017. case 144:
  1018. temp=3;
  1019. break;
  1020. default:
  1021. temp=4;
  1022. break;
  1023. }
  1024. blitAtLoc(sizes->ourImages[temp].bitmap, 318, 22, to);
  1025. //conditions
  1026. temp = curMap->mapHeader->victoryCondition.condition;
  1027. if (temp>12) temp=11;
  1028. blitAtLoc(CGP->victory->ourImages[temp].bitmap, 24, 302, to); //victory cond descr
  1029. temp=curMap->mapHeader->lossCondition.typeOfLossCon;
  1030. if (temp>12) temp=3;
  1031. blitAtLoc(CGP->loss->ourImages[temp].bitmap, 24, 359, to); //loss cond
  1032. if(type == CMenuScreen::loadGame)
  1033. printToLoc((static_cast<const CMapInfo*>(curMap))->date,308,34, FONT_SMALL, zwykly, to);
  1034. //print flags
  1035. int fx=64, ex=244, myT;
  1036. //if (curMap->howManyTeams)
  1037. myT = curMap->mapHeader->players[playerColor].team;
  1038. //else
  1039. // myT = -1;
  1040. for (std::vector<PlayerSettings>::const_iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  1041. {
  1042. int *myx = ((i->color == playerColor || curMap->mapHeader->players[i->color].team == myT) ? &fx : &ex);
  1043. blitAtLoc(sFlags->ourImages[i->color].bitmap, *myx, 399, to);
  1044. *myx += sFlags->ourImages[i->color].bitmap->w;
  1045. }
  1046. std::string tob;
  1047. switch (curOpts->difficulty)
  1048. {
  1049. case 0:
  1050. tob="80%";
  1051. break;
  1052. case 1:
  1053. tob="100%";
  1054. break;
  1055. case 2:
  1056. tob="130%";
  1057. break;
  1058. case 3:
  1059. tob="160%";
  1060. break;
  1061. case 4:
  1062. tob="200%";
  1063. break;
  1064. }
  1065. printAtMiddleLoc(tob, 311, 472, FONT_SMALL, zwykly, to);
  1066. }
  1067. //blit description
  1068. std::string itemDesc, name;
  1069. if (type == CMenuScreen::campaignList)
  1070. {
  1071. itemDesc = curMap->campaignHeader->description;
  1072. name = curMap->campaignHeader->name;
  1073. }
  1074. else
  1075. {
  1076. itemDesc = curMap->mapHeader->description;
  1077. name = curMap->mapHeader->name;
  1078. }
  1079. std::vector<std::string> *desc = CMessage::breakText(itemDesc,52);
  1080. for (int i=0;i<desc->size();i++)
  1081. printAtLoc((*desc)[i], 26, 149 + i*16, FONT_SMALL, zwykly, to);
  1082. delete desc;
  1083. //name
  1084. if (name.length())
  1085. printAtLoc(name, 26, 39, FONT_BIG, tytulowy, to);
  1086. else
  1087. printAtLoc("Unnamed", 26, 39, FONT_BIG, tytulowy, to);
  1088. }
  1089. }
  1090. void InfoCard::changeSelection( const CMapInfo *to )
  1091. {
  1092. if(to && type != CMenuScreen::newGame && type != CMenuScreen::campaignList)
  1093. difficulty->select(curOpts->difficulty, 0);
  1094. GH.totalRedraw();
  1095. }
  1096. void InfoCard::clickRight( tribool down, bool previousState )
  1097. {
  1098. static const Rect flagArea(19, 397, 335, 23);
  1099. if(down && curMap && isItInLoc(flagArea, GH.current->motion.x, GH.current->motion.y))
  1100. showTeamsPopup();
  1101. }
  1102. void InfoCard::showTeamsPopup()
  1103. {
  1104. SDL_Surface *bmp = CMessage::drawBox1(256, 90 + 50 * curMap->mapHeader->howManyTeams);
  1105. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[657], 128, 30, FONT_MEDIUM, tytulowy, bmp); //{Team Alignments}
  1106. for(int i = 0; i < curMap->mapHeader->howManyTeams; i++)
  1107. {
  1108. std::vector<ui8> flags;
  1109. std::string hlp = CGI->generaltexth->allTexts[656]; //Team %d
  1110. hlp.replace(hlp.find("%d"), 2, boost::lexical_cast<std::string>(i+1));
  1111. CSDL_Ext::printAtMiddle(hlp, 128, 65 + 50*i, FONT_SMALL, zwykly, bmp);
  1112. for(int j = 0; j < PLAYER_LIMIT; j++)
  1113. if((curMap->mapHeader->players[j].canHumanPlay || curMap->mapHeader->players[j].canComputerPlay)
  1114. && curMap->mapHeader->players[j].team == i)
  1115. flags.push_back(j);
  1116. int curx = 128 - 9*flags.size();
  1117. for(int j = 0; j < flags.size(); j++)
  1118. {
  1119. blitAt(sFlags->ourImages[flags[j]].bitmap, curx, 75 + 50*i, bmp);
  1120. curx += 18;
  1121. }
  1122. }
  1123. GH.pushInt(new CInfoPopup(bmp, true));
  1124. }
  1125. OptionsTab::OptionsTab( CMenuScreen::EState Type)
  1126. :type(Type)
  1127. {
  1128. OBJ_CONSTRUCTION;
  1129. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTBK.bmp"), 0, 0, true);
  1130. pos = bg->pos;
  1131. if(type == CMenuScreen::newGame)
  1132. turnDuration = new CSlider(55, 551, 194, bind(&OptionsTab::setTurnLength, this, _1), 1, 11, 11, true, 1);
  1133. }
  1134. OptionsTab::~OptionsTab()
  1135. {
  1136. }
  1137. void OptionsTab::showAll( SDL_Surface * to )
  1138. {
  1139. CIntObject::showAll(to);
  1140. printAtMiddleLoc(CGI->generaltexth->allTexts[515], 222, 30, FONT_BIG, tytulowy, to);
  1141. printAtMiddleWBLoc(CGI->generaltexth->allTexts[516], 222, 58, FONT_SMALL, 55, zwykly, to); //Select starting options, handicap, and name for each player in the game.
  1142. printAtMiddleWBLoc(CGI->generaltexth->allTexts[517], 107, 102, FONT_SMALL, 14, tytulowy, to); //Player Name Handicap Type
  1143. printAtMiddleWBLoc(CGI->generaltexth->allTexts[518], 197, 102, FONT_SMALL, 10, tytulowy, to); //Starting Town
  1144. printAtMiddleWBLoc(CGI->generaltexth->allTexts[519], 273, 102, FONT_SMALL, 10, tytulowy, to); //Starting Hero
  1145. printAtMiddleWBLoc(CGI->generaltexth->allTexts[520], 349, 102, FONT_SMALL, 10, tytulowy, to); //Starting Bonus
  1146. printAtMiddleLoc(CGI->generaltexth->allTexts[521], 222, 538, FONT_SMALL, tytulowy, to); // Player Turn Duration
  1147. if(curOpts->turnTime)
  1148. {
  1149. std::ostringstream os;
  1150. os << (int)curOpts->turnTime << " Minutes";
  1151. printAtMiddleLoc(os.str(), 319,559, FONT_SMALL, zwykly, to);
  1152. }
  1153. else
  1154. printAtMiddleLoc("Unlimited",319,559, FONT_SMALL, zwykly, to);
  1155. }
  1156. void OptionsTab::nextCastle( int player, int dir )
  1157. {
  1158. PlayerSettings &s = curOpts->playerInfos[player];
  1159. si32 &cur = s.castle;
  1160. ui32 allowed = curMap->mapHeader->players[s.color].allowedFactions;
  1161. if (cur == -2) //no castle - no change
  1162. return;
  1163. if (cur == -1) //random => first/last available
  1164. {
  1165. int pom = (dir>0) ? (0) : (F_NUMBER-1); // last or first
  1166. for (;pom >= 0 && pom < F_NUMBER; pom+=dir)
  1167. {
  1168. if((1 << pom) & allowed)
  1169. {
  1170. cur=pom;
  1171. break;
  1172. }
  1173. }
  1174. }
  1175. else // next/previous available
  1176. {
  1177. for (;;)
  1178. {
  1179. cur+=dir;
  1180. if ((1 << cur) & allowed)
  1181. break;
  1182. if (cur >= F_NUMBER || cur<0)
  1183. {
  1184. double p1 = log((double)allowed) / log(2.0f)+0.000001f;
  1185. double check = p1 - ((int)p1);
  1186. if (check < 0.001)
  1187. cur = (int)p1;
  1188. else
  1189. cur = -1;
  1190. break;
  1191. }
  1192. }
  1193. }
  1194. if(s.hero >= 0)
  1195. s.hero = -1;
  1196. if(cur < 0 && s.bonus == bresource)
  1197. s.bonus = brandom;
  1198. entries[player]->selectButtons();
  1199. redraw();
  1200. }
  1201. void OptionsTab::nextHero( int player, int dir )
  1202. {
  1203. PlayerSettings &s = curOpts->playerInfos[player];
  1204. int old = s.hero;
  1205. if (s.castle < 0 || !s.human || s.hero == -2)
  1206. return;
  1207. if (s.hero == -1) //random => first/last available
  1208. {
  1209. int max = (s.castle*HEROES_PER_TYPE*2+15),
  1210. min = (s.castle*HEROES_PER_TYPE*2);
  1211. s.hero = nextAllowedHero(min,max,0,dir);
  1212. }
  1213. else
  1214. {
  1215. if(dir > 0)
  1216. s.hero = nextAllowedHero(s.hero,(s.castle*HEROES_PER_TYPE*2+16),1,dir);
  1217. else
  1218. s.hero = nextAllowedHero(s.castle*HEROES_PER_TYPE*2-1,s.hero,1,dir);
  1219. }
  1220. if(old != s.hero)
  1221. {
  1222. usedHeroes.erase(old);
  1223. usedHeroes.insert(s.hero);
  1224. redraw();
  1225. }
  1226. }
  1227. int OptionsTab::nextAllowedHero( int min, int max, int incl, int dir )
  1228. {
  1229. if(dir>0)
  1230. {
  1231. for(int i=min+incl; i<=max-incl; i++)
  1232. if(canUseThisHero(i))
  1233. return i;
  1234. }
  1235. else
  1236. {
  1237. for(int i=max-incl; i>=min+incl; i--)
  1238. if(canUseThisHero(i))
  1239. return i;
  1240. }
  1241. return -1;
  1242. }
  1243. bool OptionsTab::canUseThisHero( int ID )
  1244. {
  1245. //for(int i=0;i<CPG->ret.playerInfos.size();i++)
  1246. // if(CPG->ret.playerInfos[i].hero==ID) //hero is already taken
  1247. // return false;
  1248. return !vstd::contains(usedHeroes, ID) && curMap->mapHeader->allowedHeroes[ID];
  1249. }
  1250. void OptionsTab::nextBonus( int player, int dir )
  1251. {
  1252. PlayerSettings &s = curOpts->playerInfos[player];
  1253. si8 &ret = s.bonus += dir;
  1254. if (s.hero==-2 && !curMap->mapHeader->players[s.color].heroesNames.size() && ret==bartifact) //no hero - can't be artifact
  1255. {
  1256. if (dir<0)
  1257. ret=brandom;
  1258. else ret=bgold;
  1259. }
  1260. if(ret > bresource)
  1261. ret = brandom;
  1262. if(ret < brandom)
  1263. ret = bresource;
  1264. if (s.castle==-1 && ret==bresource) //random castle - can't be resource
  1265. {
  1266. if (dir<0)
  1267. ret=bgold;
  1268. else ret=brandom;
  1269. }
  1270. redraw();
  1271. }
  1272. void OptionsTab::changeSelection( const CMapHeader *to )
  1273. {
  1274. for(int i = 0; i < entries.size(); i++)
  1275. {
  1276. children -= entries[i];
  1277. delete entries[i];
  1278. }
  1279. entries.clear();
  1280. usedHeroes.clear();
  1281. OBJ_CONSTRUCTION;
  1282. for(int i = 0; i < curOpts->playerInfos.size(); i++)
  1283. {
  1284. entries.push_back(new PlayerOptionsEntry(this, curOpts->playerInfos[i]));
  1285. const std::vector<SheroName> &heroes = curMap->mapHeader->players[curOpts->playerInfos[i].color].heroesNames;
  1286. for(size_t hi=0; hi<heroes.size(); hi++)
  1287. usedHeroes.insert(heroes[hi].heroID);
  1288. }
  1289. }
  1290. void OptionsTab::setTurnLength( int npos )
  1291. {
  1292. static const int times[] = {1, 2, 4, 6, 8, 10, 15, 20, 25, 30, 0};
  1293. amin(npos, ARRAY_COUNT(times) - 1);
  1294. curOpts->turnTime = times[npos];
  1295. redraw();
  1296. }
  1297. void OptionsTab::flagPressed( int player )
  1298. {
  1299. static std::pair<int, int> playerToRestore(-1, -1); //<color serial, player name serial>
  1300. PlayerSettings &clicked = curOpts->playerInfos[player];
  1301. PlayerSettings *old = NULL;
  1302. if(playerNames.size() == 1) //single player -> swap
  1303. {
  1304. if(player == playerSerial) //that color is already selected, no action needed
  1305. return;
  1306. old = &curOpts->playerInfos[playerSerial];
  1307. std::swap(old->human, clicked.human);
  1308. std::swap(old->name, clicked.name);
  1309. playerColor = clicked.color;
  1310. playerSerial = player;
  1311. }
  1312. else
  1313. {
  1314. //identify clicked player
  1315. int curNameID = clicked.human
  1316. ? vstd::findPos(playerNames, clicked.name)
  1317. : -1;
  1318. if(curNameID >= 0 && playerToRestore.second == curNameID) //player to restore is about to being replaced -> put him back to the old place
  1319. {
  1320. PlayerSettings &restPos = curOpts->playerInfos[playerToRestore.first];
  1321. setPlayer(restPos, playerToRestore.second);
  1322. playerToRestore.first = playerToRestore.second = 0;
  1323. }
  1324. //who will be put here?
  1325. if(curNameID < 0) //if possible replace computer with unallocated player
  1326. {
  1327. for(int i = 0; i < playerNames.size(); i++)
  1328. {
  1329. if(!curOpts->getPlayersSettings(playerNames[i]))
  1330. {
  1331. curNameID = i-1; //-1 because it'll incremented soon
  1332. break;
  1333. }
  1334. }
  1335. }
  1336. setPlayer(clicked, ++curNameID); //simply next player
  1337. //if that player was somewhere else, we need to replace him with computer
  1338. if(curNameID < playerNames.size())
  1339. {
  1340. for(std::vector<PlayerSettings>::iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  1341. {
  1342. if(i->serial != player && i->name == playerNames[curNameID])
  1343. {
  1344. assert(i->human);
  1345. playerToRestore.first = i->serial;
  1346. playerToRestore.second = vstd::findPos(playerNames, i->name);
  1347. setPlayer(*i, -1); //set computer
  1348. old = &*i;
  1349. break;
  1350. }
  1351. }
  1352. }
  1353. }
  1354. entries[clicked.serial]->selectButtons();
  1355. if(old)
  1356. {
  1357. entries[old->serial]->selectButtons();
  1358. if(!entries[playerSerial]->fixedHero)
  1359. old->hero = -1;
  1360. }
  1361. GH.totalRedraw();
  1362. }
  1363. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry( OptionsTab *owner, PlayerSettings &S)
  1364. :s(S)
  1365. {
  1366. OBJ_CONSTRUCTION;
  1367. defActions |= SHARE_POS;
  1368. pos = parent->pos + Point(54, 122 + s.serial*50);
  1369. static const char *flags[] = {"AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  1370. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"};
  1371. static const char *bgs[] = {"ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  1372. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"};
  1373. bg = new CPicture(BitmapHandler::loadBitmap(bgs[s.color]), 0, 0, true);
  1374. if(owner->type == CMenuScreen::newGame)
  1375. {
  1376. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[132], bind(&OptionsTab::nextCastle, owner, s.serial, -1), 107, 5, "ADOPLFA.DEF");
  1377. btns[1] = new AdventureMapButton(CGI->generaltexth->zelp[133], bind(&OptionsTab::nextCastle, owner, s.serial, +1), 168, 5, "ADOPRTA.DEF");
  1378. btns[2] = new AdventureMapButton(CGI->generaltexth->zelp[148], bind(&OptionsTab::nextHero, owner, s.serial, -1), 183, 5, "ADOPLFA.DEF");
  1379. btns[3] = new AdventureMapButton(CGI->generaltexth->zelp[149], bind(&OptionsTab::nextHero, owner, s.serial, +1), 244, 5, "ADOPRTA.DEF");
  1380. btns[4] = new AdventureMapButton(CGI->generaltexth->zelp[164], bind(&OptionsTab::nextBonus, owner, s.serial, -1), 259, 5, "ADOPLFA.DEF");
  1381. btns[5] = new AdventureMapButton(CGI->generaltexth->zelp[165], bind(&OptionsTab::nextBonus, owner, s.serial, +1), 320, 5, "ADOPRTA.DEF");
  1382. }
  1383. else
  1384. for(int i = 0; i < 6; i++)
  1385. btns[i] = NULL;
  1386. fixedHero = s.hero != -1; //if we doesn't start with "random hero" it must be fixed or none
  1387. selectButtons(false);
  1388. if(owner->type != CMenuScreen::scenarioInfo && curMap->mapHeader->players[s.color].canHumanPlay)
  1389. {
  1390. flag = new AdventureMapButton(CGI->generaltexth->zelp[180], bind(&OptionsTab::flagPressed, owner, s.serial), -43, 2, flags[s.color]);
  1391. flag->hoverable = true;
  1392. }
  1393. else
  1394. flag = NULL;
  1395. defActions &= ~SHARE_POS;
  1396. town = new SelectedBox(TOWN, s.serial);
  1397. town->pos += pos + Point(119, 2);
  1398. hero = new SelectedBox(HERO, s.serial);
  1399. hero->pos += pos + Point(195, 2);
  1400. bonus = new SelectedBox(BONUS, s.serial);
  1401. bonus->pos += pos + Point(271, 2);
  1402. }
  1403. void OptionsTab::PlayerOptionsEntry::showAll( SDL_Surface * to )
  1404. {
  1405. CIntObject::showAll(to);
  1406. printAtMiddleLoc(s.name, 55, 10, FONT_SMALL, zwykly, to);
  1407. }
  1408. void OptionsTab::PlayerOptionsEntry::selectButtons(bool onlyHero)
  1409. {
  1410. if(!btns[0])
  1411. return;
  1412. if(!onlyHero && s.castle != -1)
  1413. {
  1414. btns[0]->disable();
  1415. btns[1]->disable();
  1416. }
  1417. if(fixedHero || !s.human || s.castle < 0)
  1418. {
  1419. btns[2]->disable();
  1420. btns[3]->disable();
  1421. }
  1422. else
  1423. {
  1424. btns[2]->enable(active);
  1425. btns[3]->enable(active);
  1426. }
  1427. }
  1428. void OptionsTab::SelectedBox::showAll( SDL_Surface * to )
  1429. {
  1430. //PlayerSettings &s = curOpts->playerInfos[player];
  1431. SDL_Surface *toBlit = getImg();
  1432. const std::string *toPrint = getText();
  1433. blitAt(toBlit, pos, to);
  1434. printAtMiddleLoc(*toPrint, 23, 39, FONT_TINY, zwykly, to);
  1435. }
  1436. OptionsTab::SelectedBox::SelectedBox( SelType Which, ui8 Player )
  1437. :which(Which), player(Player)
  1438. {
  1439. SDL_Surface *img = getImg();
  1440. pos.w = img->w;
  1441. pos.h = img->h;
  1442. used = RCLICK;
  1443. }
  1444. SDL_Surface * OptionsTab::SelectedBox::getImg() const
  1445. {
  1446. const PlayerSettings &s = curOpts->playerInfos[player];
  1447. switch(which)
  1448. {
  1449. case TOWN:
  1450. if (s.castle < F_NUMBER && s.castle >= 0)
  1451. return graphics->getPic(s.castle, true, false);
  1452. else if (s.castle == -1)
  1453. return CGP->rTown;
  1454. else if (s.castle == -2)
  1455. return CGP->nTown;
  1456. case HERO:
  1457. if (s.hero == -1)
  1458. {
  1459. return CGP->rHero;
  1460. }
  1461. else if (s.hero == -2)
  1462. {
  1463. if(s.heroPortrait >= 0)
  1464. return graphics->portraitSmall[s.heroPortrait];
  1465. else
  1466. return CGP->nHero;
  1467. }
  1468. else
  1469. {
  1470. return graphics->portraitSmall[s.hero];
  1471. }
  1472. break;
  1473. case BONUS:
  1474. {
  1475. int pom;
  1476. switch (s.bonus)
  1477. {
  1478. case -1:
  1479. pom=10;
  1480. break;
  1481. case 0:
  1482. pom=9;
  1483. break;
  1484. case 1:
  1485. pom=8;
  1486. break;
  1487. case 2:
  1488. pom=CGI->townh->towns[s.castle].bonus;
  1489. break;
  1490. default:
  1491. assert(0);
  1492. }
  1493. return CGP->bonuses->ourImages[pom].bitmap;
  1494. }
  1495. default:
  1496. return NULL;
  1497. }
  1498. }
  1499. const std::string * OptionsTab::SelectedBox::getText() const
  1500. {
  1501. const PlayerSettings &s = curOpts->playerInfos[player];
  1502. switch(which)
  1503. {
  1504. case TOWN:
  1505. if (s.castle < F_NUMBER && s.castle >= 0)
  1506. return &CGI->townh->towns[s.castle].Name();
  1507. else if (s.castle == -1)
  1508. return &CGI->generaltexth->allTexts[522];
  1509. else if (s.castle == -2)
  1510. return &CGI->generaltexth->allTexts[523];
  1511. case HERO:
  1512. if (s.hero == -1)
  1513. return &CGI->generaltexth->allTexts[522];
  1514. else if (s.hero == -2)
  1515. {
  1516. if(s.heroPortrait >= 0)
  1517. {
  1518. if(s.heroName.length())
  1519. return &s.heroName;
  1520. else
  1521. return &CGI->heroh->heroes[s.heroPortrait]->name;
  1522. }
  1523. else
  1524. return &CGI->generaltexth->allTexts[523];
  1525. }
  1526. else
  1527. {
  1528. //if(s.heroName.length())
  1529. // return &s.heroName;
  1530. //else
  1531. return &CGI->heroh->heroes[s.hero]->name;
  1532. }
  1533. case BONUS:
  1534. switch (s.bonus)
  1535. {
  1536. case -1:
  1537. return &CGI->generaltexth->allTexts[522];
  1538. default:
  1539. return &CGI->generaltexth->arraytxt[214 + s.bonus];
  1540. }
  1541. default:
  1542. return NULL;
  1543. }
  1544. }
  1545. void OptionsTab::SelectedBox::clickRight( tribool down, bool previousState )
  1546. {
  1547. if(indeterminate(down) || !down) return;
  1548. const PlayerSettings &s = curOpts->playerInfos[player];
  1549. SDL_Surface *bmp = NULL;
  1550. const std::string *title = NULL, *subTitle = NULL;
  1551. subTitle = getText();
  1552. int val;
  1553. switch(which)
  1554. {
  1555. case TOWN:
  1556. val = s.castle;
  1557. break;
  1558. case HERO:
  1559. val = s.hero;
  1560. if(val == -2) //none => we may have some preset info
  1561. {
  1562. int p9 = curMap->mapHeader->players[s.color].p9;
  1563. if(p9 != 255 && curOpts->playerInfos[player].heroPortrait >= 0)
  1564. val = p9;
  1565. }
  1566. break;
  1567. case BONUS:
  1568. val = s.bonus;
  1569. break;
  1570. }
  1571. if(val == -1 || which == BONUS) //random or bonus box
  1572. {
  1573. bmp = CMessage::drawBox1(256, 190);
  1574. std::string *description = NULL;
  1575. switch(which)
  1576. {
  1577. case TOWN:
  1578. title = &CGI->generaltexth->allTexts[103];
  1579. description = &CGI->generaltexth->allTexts[104];
  1580. break;
  1581. case HERO:
  1582. title = &CGI->generaltexth->allTexts[101];
  1583. description = &CGI->generaltexth->allTexts[102];
  1584. break;
  1585. case BONUS:
  1586. {
  1587. switch(val)
  1588. {
  1589. case brandom:
  1590. title = &CGI->generaltexth->allTexts[86]; //{Random Bonus}
  1591. description = &CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  1592. break;
  1593. case bartifact:
  1594. title = &CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  1595. description = &CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  1596. break;
  1597. case bgold:
  1598. title = &CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  1599. subTitle = &CGI->generaltexth->allTexts[87]; //500-1000
  1600. description = &CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  1601. break;
  1602. case bresource:
  1603. {
  1604. title = &CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  1605. switch(CGI->townh->towns[s.castle].primaryRes)
  1606. {
  1607. case 1:
  1608. subTitle = &CGI->generaltexth->allTexts[694];
  1609. description = &CGI->generaltexth->allTexts[690];
  1610. break;
  1611. case 3:
  1612. subTitle = &CGI->generaltexth->allTexts[695];
  1613. description = &CGI->generaltexth->allTexts[691];
  1614. break;
  1615. case 4:
  1616. subTitle = &CGI->generaltexth->allTexts[692];
  1617. description = &CGI->generaltexth->allTexts[688];
  1618. break;
  1619. case 5:
  1620. subTitle = &CGI->generaltexth->allTexts[693];
  1621. description = &CGI->generaltexth->allTexts[689];
  1622. break;
  1623. case 127:
  1624. subTitle = &CGI->generaltexth->allTexts[89]; //5-10 wood / 5-10 ore
  1625. 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
  1626. break;
  1627. }
  1628. }
  1629. break;
  1630. }
  1631. }
  1632. break;
  1633. }
  1634. if(description)
  1635. CSDL_Ext::printAtMiddleWB(*description, 125, 145, FONT_SMALL, 37, zwykly, bmp);
  1636. }
  1637. else if(val == -2)
  1638. {
  1639. return;
  1640. }
  1641. else if(which == TOWN)
  1642. {
  1643. bmp = CMessage::drawBox1(256, 319);
  1644. title = &CGI->generaltexth->allTexts[80];
  1645. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[79], 135, 137, FONT_MEDIUM, tytulowy, bmp);
  1646. const CTown &t = CGI->townh->towns[val];
  1647. //print creatures
  1648. int x = 60, y = 159;
  1649. for(int i = 0; i < 7; i++)
  1650. {
  1651. int c = t.basicCreatures[i];
  1652. blitAt(graphics->smallImgs[c], x, y, bmp);
  1653. CSDL_Ext::printAtMiddleWB(CGI->creh->creatures[c].nameSing, x + 16, y + 45, FONT_TINY, 10, zwykly, bmp);
  1654. if(i == 2)
  1655. {
  1656. x = 40;
  1657. y += 76;
  1658. }
  1659. else
  1660. {
  1661. x += 52;
  1662. }
  1663. }
  1664. }
  1665. else if(val >= 0)
  1666. {
  1667. const CHero *h = CGI->heroh->heroes[val];
  1668. bmp = CMessage::drawBox1(320, 255);
  1669. title = &CGI->generaltexth->allTexts[77];
  1670. CSDL_Ext::printAtMiddle(*title, 167, 36, FONT_MEDIUM, tytulowy, bmp);
  1671. CSDL_Ext::printAtMiddle(*subTitle + " - " + h->heroClass->name, 160, 99, FONT_SMALL, zwykly, bmp);
  1672. blitAt(getImg(), 136, 56, bmp);
  1673. //print specialty
  1674. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[78], 166, 132, FONT_MEDIUM, tytulowy, bmp);
  1675. blitAt(graphics->un44->ourImages[val].bitmap, 140, 150, bmp);
  1676. CSDL_Ext::printAtMiddle(CGI->generaltexth->hTxts[val].bonusName, 166, 203, FONT_SMALL, zwykly, bmp);
  1677. GH.pushInt(new CInfoPopup(bmp, true));
  1678. return;
  1679. }
  1680. if(title)
  1681. CSDL_Ext::printAtMiddle(*title, 135, 36, FONT_MEDIUM, tytulowy, bmp);
  1682. if(subTitle)
  1683. CSDL_Ext::printAtMiddle(*subTitle, 127, 103, FONT_SMALL, zwykly, bmp);
  1684. blitAt(getImg(), 104, 60, bmp);
  1685. GH.pushInt(new CInfoPopup(bmp, true));
  1686. }
  1687. CScenarioInfo::CScenarioInfo( const CMapHeader *mapHeader, const StartInfo *startInfo, const CMapInfo * makeItCurrent )
  1688. {
  1689. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1690. for(size_t i = 0; i < startInfo->playerInfos.size(); i++)
  1691. {
  1692. if(startInfo->playerInfos[i].human)
  1693. {
  1694. playerColor = startInfo->playerInfos[i].color;
  1695. playerSerial = i;
  1696. }
  1697. }
  1698. pos.w = 762;
  1699. pos.h = 584;
  1700. center(pos);
  1701. if(makeItCurrent)
  1702. {
  1703. curMap = makeItCurrent;
  1704. }
  1705. curOpts = const_cast<StartInfo*>(startInfo); //I hope it's safe
  1706. card = new InfoCard(CMenuScreen::scenarioInfo);
  1707. opt = new OptionsTab(CMenuScreen::scenarioInfo);
  1708. opt->changeSelection(0);
  1709. card->difficulty->select(startInfo->difficulty, 0);
  1710. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 584, 535, "SCNRBACK.DEF", SDLK_ESCAPE);
  1711. }
  1712. CScenarioInfo::~CScenarioInfo()
  1713. {
  1714. }
  1715. bool mapSorter::operator()(const CMapInfo *aaa, const CMapInfo *bbb)
  1716. {
  1717. const CMapHeader * a = aaa->mapHeader,
  1718. * b = bbb->mapHeader;
  1719. if(a && b) //if we are sorting scenarios
  1720. {
  1721. switch (sortBy)
  1722. {
  1723. case _format: //by map format (RoE, WoG, etc)
  1724. return (a->version<b->version);
  1725. break;
  1726. case _loscon: //by loss conditions
  1727. return (a->lossCondition.typeOfLossCon<b->lossCondition.typeOfLossCon);
  1728. break;
  1729. case _playerAm: //by player amount
  1730. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  1731. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  1732. for (int i=0;i<8;i++)
  1733. {
  1734. if (a->players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  1735. else if (a->players[i].canComputerPlay) {playerAmntA++;}
  1736. if (b->players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  1737. else if (b->players[i].canComputerPlay) {playerAmntB++;}
  1738. }
  1739. if (playerAmntB!=playerAmntA)
  1740. return (playerAmntA<playerAmntB);
  1741. else
  1742. return (humenPlayersA<humenPlayersB);
  1743. break;
  1744. case _size: //by size of map
  1745. return (a->width<b->width);
  1746. break;
  1747. case _viccon: //by victory conditions
  1748. return (a->victoryCondition.condition < b->victoryCondition.condition);
  1749. break;
  1750. case _name: //by name
  1751. return (a->name<b->name);
  1752. break;
  1753. default:
  1754. return (a->name<b->name);
  1755. break;
  1756. }
  1757. }
  1758. else //if we are sorting campaigns
  1759. {
  1760. switch(sortBy)
  1761. {
  1762. case _numOfMaps: //by number of maps in campaign
  1763. return CGI->generaltexth->campaignRegionNames[ aaa->campaignHeader->mapVersion ].size() <
  1764. CGI->generaltexth->campaignRegionNames[ bbb->campaignHeader->mapVersion ].size();
  1765. break;
  1766. case _name: //by name
  1767. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  1768. break;
  1769. default:
  1770. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  1771. break;
  1772. }
  1773. }
  1774. }
  1775. CMultiMode::CMultiMode()
  1776. {
  1777. OBJ_CONSTRUCTION;
  1778. bg = new CPicture("MUPOPUP.bmp");
  1779. bg->convertToScreenBPP(); //so we could draw without problems
  1780. blitAt(CPicture("MUMAP.bmp"), 16, 77, *bg); //blit img
  1781. pos = bg->center(); //center, window has size of bg graphic
  1782. bar = new CGStatusBar(new CPicture(Rect(7, 465, 440, 18), 0));//226, 472
  1783. txt = new CTextInput(Rect(19, 436, 334, 16), *bg);
  1784. txt->setText(CGI->generaltexth->allTexts[434]); //Player
  1785. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[266], bind(&CMultiMode::openHotseat, this), 373, 78, "MUBHOT.DEF");
  1786. btns[6] = new AdventureMapButton(CGI->generaltexth->zelp[288], bind(&CGuiHandler::popIntTotally, ref(GH), this), 373, 424, "MUBCANC.DEF");
  1787. }
  1788. void CMultiMode::openHotseat()
  1789. {
  1790. GH.pushInt(new CHotSeatPlayers(txt->text));
  1791. }
  1792. CHotSeatPlayers::CHotSeatPlayers(const std::string &firstPlayer)
  1793. {
  1794. OBJ_CONSTRUCTION;
  1795. bg = new CPicture("MUHOTSEA.bmp");
  1796. bg->convertToScreenBPP(); //so we could draw without problems
  1797. bg->printAtMiddleWBLoc(CGI->generaltexth->allTexts[446], 185, 55, FONT_BIG, 50, zwykly, *bg); //HOTSEAT Please enter names
  1798. pos = bg->center(); //center, window has size of bg graphic
  1799. for(int i = 0; i < ARRAY_COUNT(txt); i++)
  1800. txt[i] = new CTextInput(Rect(60, 85 + i*30, 280, 16), *bg);
  1801. txt[0]->setText(firstPlayer);
  1802. txt[0]->giveFocus();
  1803. ok = new AdventureMapButton(CGI->generaltexth->zelp[560], bind(&CHotSeatPlayers::enterSelectionScreen, this), 95, 338, "MUBCHCK.DEF");
  1804. cancel = new AdventureMapButton(CGI->generaltexth->zelp[561], bind(&CGuiHandler::popIntTotally, ref(GH), this), 205, 338, "MUBCANC.DEF");
  1805. bar = new CGStatusBar(new CPicture(Rect(7, 381, 348, 18), 0));//226, 472
  1806. }
  1807. void CHotSeatPlayers::enterSelectionScreen()
  1808. {
  1809. for(int i = 0; i < ARRAY_COUNT(txt); i++)
  1810. if(txt[i]->text.length())
  1811. playerNames.push_back(txt[i]->text);
  1812. GH.popInts(2);
  1813. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame));
  1814. }
  1815. CBonusSelection::CBonusSelection( const CCampaign * _ourCampaign, int _whichMap )
  1816. : ourCampaign(_ourCampaign), whichMap(_whichMap), highlightedRegion(NULL)
  1817. {
  1818. OBJ_CONSTRUCTION;
  1819. static const std::string bgNames [] = {"E1_BG.BMP", "G2_BG.BMP", "E2_BG.BMP", "G1_BG.BMP", "G3_BG.BMP", "N1_BG.BMP",
  1820. "S1_BG.BMP", "BR_BG.BMP", "IS_BG.BMP", "KR_BG.BMP", "NI_BG.BMP", "TA_BG.BMP", "AR_BG.BMP", "HS_BG.BMP",
  1821. "BB_BG.BMP", "NB_BG.BMP", "EL_BG.BMP", "RN_BG.BMP", "UA_BG.BMP", "SP_BG.BMP"};
  1822. loadPositionsOfGraphics();
  1823. background = BitmapHandler::loadBitmap(bgNames[ourCampaign->header.mapVersion]);
  1824. SDL_Surface * panel = BitmapHandler::loadBitmap("CAMPBRF.BMP");
  1825. blitAt(panel, 456, 6, background);
  1826. startB = new AdventureMapButton("", "", 0 /*cb*/, 475, 536, "SCNRBEG.DEF", SDLK_RETURN);
  1827. backB = new AdventureMapButton("", "", boost::bind(&CBonusSelection::goBack, this), 624, 536, "SCNRBACK.DEF", SDLK_ESCAPE);
  1828. CMapHeader ourHeader;
  1829. int _it=0;
  1830. ourHeader.initFromMemory((const unsigned char*)ourCampaign->mapPieces[whichMap].c_str(), _it);
  1831. //campaign name
  1832. if (ourCampaign->header.name.length())
  1833. printAtLoc(ourCampaign->header.name, 481, 28, FONT_BIG, tytulowy, background);
  1834. else
  1835. printAtLoc("Unnamed", 481, 28, FONT_BIG, tytulowy, background);
  1836. //map size icon
  1837. CDefHandler *sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  1838. int temp;
  1839. switch (ourHeader.width)
  1840. {
  1841. case 36:
  1842. temp=0;
  1843. break;
  1844. case 72:
  1845. temp=1;
  1846. break;
  1847. case 108:
  1848. temp=2;
  1849. break;
  1850. case 144:
  1851. temp=3;
  1852. break;
  1853. default:
  1854. temp=4;
  1855. break;
  1856. }
  1857. blitAtLoc(sizes->ourImages[temp].bitmap, 735, 26, background);
  1858. delete sizes;
  1859. //campaign description
  1860. printAtLoc(CGI->generaltexth->allTexts[38], 481, 63, FONT_SMALL, tytulowy, background);
  1861. std::vector<std::string> *desc = CMessage::breakText(ourCampaign->header.description, 45);
  1862. for (int i=0; i<desc->size() ;i++)
  1863. printAtLoc((*desc)[i], 481, 86 + i*16, FONT_SMALL, zwykly, background);
  1864. delete desc;
  1865. //map name
  1866. std::string mapDesc = ourHeader.description, mapName = ourHeader.name;
  1867. if (mapName.length())
  1868. printAtLoc(mapName, 481, 219, FONT_BIG, tytulowy, background);
  1869. else
  1870. printAtLoc("Unnamed", 481, 219, FONT_BIG, tytulowy, background);
  1871. //map description
  1872. printAtLoc(CGI->generaltexth->allTexts[496], 481, 253, FONT_SMALL, tytulowy, background);
  1873. desc = CMessage::breakText(mapDesc, 45);
  1874. for (int i=0; i<desc->size(); i++)
  1875. printAtLoc((*desc)[i], 481, 281 + i*16, FONT_SMALL, zwykly, background);
  1876. delete desc;
  1877. //allies / enemies
  1878. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 486, 407, FONT_SMALL, zwykly, background); //Allies
  1879. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 619, 407, FONT_SMALL, zwykly, background); //Enemies
  1880. int fx=64, ex=244, myT;
  1881. myT = ourHeader.players[playerColor].team;
  1882. /*for (std::vector<PlayerSettings>::const_iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  1883. {
  1884. int *myx = ((i->color == playerColor || ourHeader.players[i->color].team == myT) ? &fx : &ex);
  1885. blitAtLoc(sFlags->ourImages[i->color].bitmap, *myx, 399, to);
  1886. *myx += sFlags->ourImages[i->color].bitmap->w;
  1887. }*/
  1888. SDL_FreeSurface(panel);
  1889. //bonus choosing
  1890. printAtLoc(CGI->generaltexth->allTexts[71], 510, 431, FONT_MEDIUM, zwykly, background); //Choose a bonus:
  1891. //difficulty
  1892. printAtLoc("Difficulty", 691, 431, FONT_MEDIUM, zwykly, background); //Difficulty
  1893. //set left part of window
  1894. for (int g=0; g<ourCampaign->scenarios.size(); ++g)
  1895. {
  1896. if(ourCampaign->conquerable(g))
  1897. {
  1898. regions.push_back(new CRegion(this, true, true, g));
  1899. if (highlightedRegion == NULL)
  1900. {
  1901. highlightedRegion = regions.back();
  1902. }
  1903. }
  1904. else if (ourCampaign->scenarios[g].conquered) //display as striped
  1905. {
  1906. regions.push_back(new CRegion(this, false, false, g));
  1907. }
  1908. }
  1909. }
  1910. CBonusSelection::~CBonusSelection()
  1911. {
  1912. SDL_FreeSurface(background);
  1913. }
  1914. void CBonusSelection::goBack()
  1915. {
  1916. GH.popIntTotally(this);
  1917. }
  1918. void CBonusSelection::showAll( SDL_Surface * to )
  1919. {
  1920. CIntObject::showAll(to);
  1921. blitAt(background, pos.x, pos.y, to);
  1922. }
  1923. void CBonusSelection::loadPositionsOfGraphics()
  1924. {
  1925. std::ifstream is((GVCMIDirs.UserPath + "/config/campaign_regions.txt").c_str(), std::ios_base::binary | std::ios_base::in);
  1926. assert(is.is_open());
  1927. for (int g=0; g<CGI->generaltexth->campaignMapNames.size(); ++g)
  1928. {
  1929. SCampPositions sc;
  1930. is >> sc.campPrefix;
  1931. is >> sc.colorSuffixLength;
  1932. bool contReading = true;
  1933. while(contReading)
  1934. {
  1935. SCampPositions::SRegionDesc rd;
  1936. is >> rd.infix;
  1937. if(rd.infix == "END")
  1938. {
  1939. contReading = false;
  1940. }
  1941. else
  1942. {
  1943. is >> rd.xpos >> rd.ypos;
  1944. sc.regions.push_back(rd);
  1945. }
  1946. }
  1947. campDescriptions.push_back(sc);
  1948. }
  1949. }
  1950. CBonusSelection::CRegion::CRegion( CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber )
  1951. : owner(_owner), accessible(_accessible), selectable(_selectable), myNumber(_myNumber)
  1952. {
  1953. OBJ_CONSTRUCTION;
  1954. static const std::string colors[2][8] = {
  1955. {"R", "B", "N", "G", "O", "V", "T", "P"},
  1956. {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"}};
  1957. const SCampPositions & campDsc = owner->campDescriptions[owner->ourCampaign->header.mapVersion];
  1958. const SCampPositions::SRegionDesc & desc = campDsc.regions[myNumber];
  1959. pos.x = desc.xpos;
  1960. pos.y = desc.ypos;
  1961. //loading of graphics
  1962. std::string prefix = campDsc.campPrefix + desc.infix + "_";
  1963. std::string suffix = colors[campDsc.colorSuffixLength - 1][owner->ourCampaign->scenarios[myNumber].regionColor];
  1964. static const std::string infix [] = {"En", "Se", "Co"};
  1965. for (int g = 0; g < ARRAY_COUNT(infix); g++)
  1966. {
  1967. graphics[g] = BitmapHandler::loadBitmap(prefix + infix[g] + suffix + ".BMP");
  1968. }
  1969. }
  1970. CBonusSelection::CRegion::~CRegion()
  1971. {
  1972. for (int g=0; g<ARRAY_COUNT(graphics); ++g)
  1973. {
  1974. SDL_FreeSurface(graphics[g]);
  1975. }
  1976. }
  1977. void CBonusSelection::CRegion::clickLeft( tribool down, bool previousState )
  1978. {
  1979. //select if selectable & clicked inside our graphic
  1980. if(!down && selectable)
  1981. {
  1982. //owner->highlightedRegion = this;
  1983. }
  1984. }
  1985. void CBonusSelection::CRegion::clickRight( tribool down, bool previousState )
  1986. {
  1987. //show r-click text
  1988. }
  1989. void CBonusSelection::CRegion::show( SDL_Surface * to )
  1990. {
  1991. const SCampPositions::SRegionDesc & desc = owner->campDescriptions[owner->ourCampaign->header.mapVersion].regions[myNumber];
  1992. if (!accessible)
  1993. {
  1994. //show as striped
  1995. blitAt(graphics[2], pos.x, pos.y, to);
  1996. }
  1997. else if (this == owner->highlightedRegion)
  1998. {
  1999. //show as selected
  2000. blitAt(graphics[1], pos.x, pos.y, to);
  2001. }
  2002. else
  2003. {
  2004. //show as not selected selected
  2005. blitAt(graphics[0], pos.x, pos.y, to);
  2006. }
  2007. }