CPreGame.cpp 66 KB

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