CPreGame.cpp 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505
  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("", "", 0 /*cb*/, 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.name;
  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. const_cast<CMapInfo *>(curMap)->playerAmnt = ourHeader->players.size();
  2043. CSelectionScreen::updateStartInfo(curMap, *curOpts, ourHeader);
  2044. sInfo.turnTime = 0;
  2045. sInfo.whichMapInCampaign = whichOne;
  2046. }
  2047. void CBonusSelection::show( SDL_Surface * to )
  2048. {
  2049. blitAt(background, pos.x, pos.y, to);
  2050. //map name
  2051. std::string mapDesc = ourHeader->description,
  2052. mapName = ourHeader->name;
  2053. if (mapName.length())
  2054. printAtLoc(mapName, 481, 219, FONT_BIG, tytulowy, to);
  2055. else
  2056. printAtLoc("Unnamed", 481, 219, FONT_BIG, tytulowy, to);
  2057. //map description
  2058. printAtLoc(CGI->generaltexth->allTexts[496], 481, 253, FONT_SMALL, tytulowy, to);
  2059. std::vector<std::string> *desc = CMessage::breakText(mapDesc, 45);
  2060. for (int i=0; i<desc->size(); i++)
  2061. printAtLoc((*desc)[i], 481, 281 + i*16, FONT_SMALL, zwykly, to);
  2062. delete desc;
  2063. //map size icon
  2064. int temp;
  2065. switch (ourHeader->width)
  2066. {
  2067. case 36:
  2068. temp=0;
  2069. break;
  2070. case 72:
  2071. temp=1;
  2072. break;
  2073. case 108:
  2074. temp=2;
  2075. break;
  2076. case 144:
  2077. temp=3;
  2078. break;
  2079. default:
  2080. temp=4;
  2081. break;
  2082. }
  2083. blitAtLoc(sizes->ourImages[temp].bitmap, 735, 26, to);
  2084. CIntObject::show(to);
  2085. }
  2086. void CBonusSelection::updateBonusSelection()
  2087. {
  2088. //graphics:
  2089. //spell - SPELLBON.DEF
  2090. //monster - TWCRPORT.DEF
  2091. //building - ?
  2092. //artifact - ARTIFBON.DEF
  2093. //spell scroll - SPELLBON.DEF
  2094. //prim skill - PSKILBON.DEF
  2095. //sec skill - SSKILBON.DEF
  2096. //resource - BORES.DEF
  2097. //player - ?
  2098. //hero -?
  2099. bonuses = new CHighlightableButtonsGroup(0);
  2100. {
  2101. static const char *bonDefs[] = {"SPELLBON.DEF", "TWCRPORT.DEF", "GSPBUT5.DEF", "ARTIFBON.DEF", "SPELLBON.DEF",
  2102. "PSKILBON.DEF", "SSKILBON.DEF", "BORES.DEF", "GSPBUT5.DEF", "GSPBUT5.DEF"};
  2103. for(int i = 0; i < 5; i++)
  2104. {
  2105. bonuses->addButton(new CHighlightableButton("", "", 0, 110 + i*32, 450, bonDefs[i], i));
  2106. bonuses->buttons.back()->pos += Point(68, 0);
  2107. }
  2108. }
  2109. }
  2110. CBonusSelection::CRegion::CRegion( CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber )
  2111. : owner(_owner), accessible(_accessible), selectable(_selectable), myNumber(_myNumber)
  2112. {
  2113. OBJ_CONSTRUCTION;
  2114. used = LCLICK | RCLICK;
  2115. static const std::string colors[2][8] = {
  2116. {"R", "B", "N", "G", "O", "V", "T", "P"},
  2117. {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"}};
  2118. const SCampPositions & campDsc = owner->campDescriptions[owner->ourCampaign->header.mapVersion];
  2119. const SCampPositions::SRegionDesc & desc = campDsc.regions[myNumber];
  2120. pos.x = desc.xpos;
  2121. pos.y = desc.ypos;
  2122. //loading of graphics
  2123. std::string prefix = campDsc.campPrefix + desc.infix + "_";
  2124. std::string suffix = colors[campDsc.colorSuffixLength - 1][owner->ourCampaign->scenarios[myNumber].regionColor];
  2125. static const std::string infix [] = {"En", "Se", "Co"};
  2126. for (int g = 0; g < ARRAY_COUNT(infix); g++)
  2127. {
  2128. graphics[g] = BitmapHandler::loadBitmap(prefix + infix[g] + suffix + ".BMP");
  2129. }
  2130. pos.w = graphics[0]->w;
  2131. pos.h = graphics[0]->h;
  2132. }
  2133. CBonusSelection::CRegion::~CRegion()
  2134. {
  2135. for (int g=0; g<ARRAY_COUNT(graphics); ++g)
  2136. {
  2137. SDL_FreeSurface(graphics[g]);
  2138. }
  2139. }
  2140. void CBonusSelection::CRegion::clickLeft( tribool down, bool previousState )
  2141. {
  2142. //select if selectable & clicked inside our graphic
  2143. if ( indeterminate(down) )
  2144. {
  2145. return;
  2146. }
  2147. if( !down && selectable && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) )
  2148. {
  2149. owner->selectMap(myNumber);
  2150. owner->highlightedRegion = this;
  2151. }
  2152. }
  2153. void CBonusSelection::CRegion::clickRight( tribool down, bool previousState )
  2154. {
  2155. //show r-click text
  2156. if( down && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) &&
  2157. rclickText.size() )
  2158. {
  2159. CRClickPopup::createAndPush(rclickText);
  2160. }
  2161. }
  2162. void CBonusSelection::CRegion::show( SDL_Surface * to )
  2163. {
  2164. const SCampPositions::SRegionDesc & desc = owner->campDescriptions[owner->ourCampaign->header.mapVersion].regions[myNumber];
  2165. if (!accessible)
  2166. {
  2167. //show as striped
  2168. blitAt(graphics[2], pos.x, pos.y, to);
  2169. }
  2170. else if (this == owner->highlightedRegion)
  2171. {
  2172. //show as selected
  2173. blitAt(graphics[1], pos.x, pos.y, to);
  2174. }
  2175. else
  2176. {
  2177. //show as not selected selected
  2178. blitAt(graphics[0], pos.x, pos.y, to);
  2179. }
  2180. }
  2181. CSavingScreen::CSavingScreen()
  2182. : CSelectionScreen(CMenuScreen::saveGame)
  2183. {
  2184. ourGame = mapInfoFromGame();
  2185. sInfo = *LOCPLINT->cb->getStartInfo();
  2186. setPlayersFromGame();
  2187. }
  2188. CSavingScreen::~CSavingScreen()
  2189. {
  2190. }