CPreGame.cpp 69 KB

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