CPreGame.cpp 77 KB

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