CPreGame.cpp 69 KB

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