CPreGame.cpp 77 KB

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