CPreGame.cpp 98 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570
  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 "CAnimation.h"
  13. #include "CDefHandler.h"
  14. #include "../lib/CDefObjInfoHandler.h"
  15. #include "../lib/CGeneralTextHandler.h"
  16. #include "../lib/CLodHandler.h"
  17. #include "../lib/CTownHandler.h"
  18. #include "../lib/CHeroHandler.h"
  19. #include "../lib/CObjectHandler.h"
  20. #include "../lib/CCampaignHandler.h"
  21. #include "../lib/CCreatureHandler.h"
  22. #include "../lib/JsonNode.h"
  23. #include "CMusicHandler.h"
  24. #include "CVideoHandler.h"
  25. #include <cmath>
  26. #include "Graphics.h"
  27. //#include <boost/thread.hpp>
  28. #include <boost/bind.hpp>
  29. #include <boost/function.hpp>
  30. #include <boost/ref.hpp>
  31. #include <boost/foreach.hpp>
  32. #include <boost/assign/std/vector.hpp>
  33. #include <boost/assign/list_of.hpp>
  34. #include <cstdlib>
  35. #include "../lib/Connection.h"
  36. #include "../lib/VCMIDirs.h"
  37. #include "../lib/map.h"
  38. #include "AdventureMapButton.h"
  39. #include "GUIClasses.h"
  40. #include "CPlayerInterface.h"
  41. #include "../CCallback.h"
  42. #include <boost/lexical_cast.hpp>
  43. #include <cstdlib>
  44. #include "CMessage.h"
  45. #include "../lib/CSpellHandler.h" /*for campaign bonuses*/
  46. #include "../lib/CArtHandler.h" /*for campaign bonuses*/
  47. #include "../lib/CBuildingHandler.h" /*for campaign bonuses*/
  48. #include "CBitmapHandler.h"
  49. #include "Client.h"
  50. #include "../lib/NetPacks.h"
  51. #include "../lib/RegisterTypes.cpp"
  52. #include <boost/thread/recursive_mutex.hpp>
  53. #include "../CThreadHelper.h"
  54. #include "CConfigHandler.h"
  55. #include "../lib/CFileUtility.h"
  56. /*
  57. * CPreGame.cpp, part of VCMI engine
  58. *
  59. * Authors: listed in file AUTHORS in main folder
  60. *
  61. * License: GNU General Public License v2.0 or later
  62. * Full text of license available in license.txt file, in main folder
  63. *
  64. */
  65. namespace fs = boost::filesystem;
  66. using boost::bind;
  67. using boost::ref;
  68. #if _MSC_VER >= 1600
  69. //#define bind boost::bind
  70. //#define ref boost::ref
  71. #endif
  72. void startGame(StartInfo * options, CConnection *serv = NULL);
  73. extern SystemOptions GDefaultOptions;
  74. CGPreGame * CGP;
  75. ISelectionScreenInfo *SEL;
  76. static int playerColor; //if more than one player - applies to the first
  77. static std::string selectedName; //set when game is started/loaded
  78. struct EvilHlpStruct
  79. {
  80. CConnection *serv;
  81. StartInfo *sInfo;
  82. void reset(bool strong = true)
  83. {
  84. if(strong)
  85. {
  86. delNull(serv);
  87. delNull(sInfo);
  88. }
  89. else
  90. {
  91. serv = NULL;
  92. sInfo = NULL;
  93. }
  94. }
  95. } startingInfo;
  96. static void do_quit()
  97. {
  98. SDL_Event event;
  99. event.quit.type = SDL_QUIT;
  100. SDL_PushEvent(&event);
  101. }
  102. static CMapInfo *mapInfoFromGame()
  103. {
  104. CMapInfo *ret = new CMapInfo();
  105. CMapHeader *headerCopy = new CMapHeader(*LOCPLINT->cb->getMapHeader()); //will be deleted by CMapInfo d-tor
  106. ret->setHeader(headerCopy);
  107. return ret;
  108. }
  109. static void setPlayersFromGame()
  110. {
  111. playerColor = LOCPLINT->playerID;
  112. }
  113. static void swapPlayers(PlayerSettings &a, PlayerSettings &b)
  114. {
  115. std::swap(a.human, b.human);
  116. std::swap(a.name, b.name);
  117. if(a.human == 1)
  118. playerColor = a.color;
  119. else if(b.human == 1)
  120. playerColor = b.color;
  121. }
  122. void setPlayer(PlayerSettings &pset, unsigned player, const std::map<ui32, std::string> &playerNames)
  123. {
  124. if(vstd::contains(playerNames, player))
  125. pset.name = playerNames.find(player)->second;
  126. else
  127. pset.name = CGI->generaltexth->allTexts[468];//Computer
  128. pset.human = player;
  129. if(player == playerNames.begin()->first)
  130. playerColor = pset.color;
  131. }
  132. void updateStartInfo(std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader, const std::map<ui32, std::string> &playerNames)
  133. {
  134. sInfo.playerInfos.clear();
  135. if(!filename.size())
  136. return;
  137. /*sInfo.playerInfos.resize(to->playerAmnt);*/
  138. sInfo.mapname = filename;
  139. playerColor = -1;
  140. std::map<ui32, std::string>::const_iterator namesIt = playerNames.begin();
  141. for (int i = 0; i < PLAYER_LIMIT; i++)
  142. {
  143. const PlayerInfo &pinfo = mapHeader->players[i];
  144. //neither computer nor human can play - no player
  145. if (!(pinfo.canComputerPlay || pinfo.canComputerPlay))
  146. continue;
  147. PlayerSettings &pset = sInfo.playerInfos[i];
  148. pset.color = i;
  149. if(pinfo.canHumanPlay && namesIt != playerNames.end())
  150. setPlayer(pset, namesIt++->first, playerNames);
  151. else
  152. setPlayer(pset, 0, playerNames);
  153. pset.castle = pinfo.defaultCastle();
  154. pset.hero = pinfo.defaultHero();
  155. if(pinfo.mainHeroName.length())
  156. {
  157. pset.heroName = pinfo.mainHeroName;
  158. if((pset.heroPortrait = pinfo.mainHeroPortrait) == 255)
  159. pset.heroPortrait = pinfo.p9;
  160. }
  161. pset.handicap = 0;
  162. }
  163. }
  164. template <typename T> class CApplyOnPG;
  165. class CBaseForPGApply
  166. {
  167. public:
  168. virtual void applyOnPG(CSelectionScreen *selScr, void *pack) const =0;
  169. virtual ~CBaseForPGApply(){};
  170. template<typename U> static CBaseForPGApply *getApplier(const U * t=NULL)
  171. {
  172. return new CApplyOnPG<U>;
  173. }
  174. };
  175. template <typename T> class CApplyOnPG : public CBaseForPGApply
  176. {
  177. public:
  178. void applyOnPG(CSelectionScreen *selScr, void *pack) const
  179. {
  180. T *ptr = static_cast<T*>(pack);
  181. ptr->apply(selScr);
  182. }
  183. };
  184. static CApplier<CBaseForPGApply> *applier = NULL;
  185. static CPicture* createPicture(const JsonNode& config)
  186. {
  187. return new CPicture(config["name"].String(), config["x"].Float(), config["y"].Float());
  188. }
  189. CMenuScreen::CMenuScreen(const JsonNode& configNode):
  190. config(configNode)
  191. {
  192. OBJ_CONSTRUCTION_CAPTURING_ALL;
  193. BOOST_FOREACH(const JsonNode& node, config["items"].Vector())
  194. menuNameToEntry.push_back(node["name"].String());
  195. BOOST_FOREACH(const JsonNode& node, config["images"].Vector())
  196. images.push_back(createPicture(node));
  197. //Hardcoded entry
  198. menuNameToEntry.push_back("credits");
  199. tabs = new CTabbedInt(boost::bind(&CMenuScreen::createTab, this, _1), CTabbedInt::DestroyFunc());
  200. tabs->type |= REDRAW_PARENT;
  201. }
  202. CIntObject * CMenuScreen::createTab(size_t index)
  203. {
  204. if (config["items"].Vector().size() == index)
  205. return new CreditsScreen();
  206. return new CMenuEntry(this, config["items"].Vector()[index]);
  207. }
  208. void CMenuScreen::show(SDL_Surface * to)
  209. {
  210. if (!config["video"].isNull())
  211. CCS->videoh->update(config["video"]["x"].Float(), config["video"]["y"].Float(), to, true, false);
  212. CIntObject::show(to);
  213. }
  214. void CMenuScreen::activate()
  215. {
  216. CCS->musich->playMusic(musicBase::mainMenu, -1);
  217. if (!config["video"].isNull())
  218. CCS->videoh->open(config["video"]["name"].String());
  219. CIntObject::activate();
  220. }
  221. void CMenuScreen::deactivate()
  222. {
  223. if (!config["video"].isNull())
  224. CCS->videoh->close();
  225. CIntObject::deactivate();
  226. }
  227. void CMenuScreen::switchToTab(size_t index)
  228. {
  229. tabs->setActive(index);
  230. }
  231. //funciton for std::string -> boost::function conversion for main menu
  232. static boost::function<void()> genCommand(CMenuScreen* menu, std::vector<std::string> menuType, const std::string &string)
  233. {
  234. static const std::vector<std::string> commandType = boost::assign::list_of
  235. ("to")("campaigns")("start")("load")("exit")("highscores");
  236. static const std::vector<std::string> gameType = boost::assign::list_of
  237. ("single")("multi")("campaign")("tutorial");
  238. std::list<std::string> commands;
  239. boost::split(commands, string, boost::is_any_of("\t "));
  240. if (!commands.empty())
  241. {
  242. size_t index = std::find(commandType.begin(), commandType.end(), commands.front()) - commandType.begin();
  243. commands.pop_front();
  244. if (index > 3 || !commands.empty())
  245. {
  246. switch (index)
  247. {
  248. break; case 0://to - switch to another tab, if such tab exists
  249. {
  250. size_t index2 = std::find(menuType.begin(), menuType.end(), commands.front()) - menuType.begin();
  251. if ( index2 != menuType.size())
  252. return boost::bind(&CMenuScreen::switchToTab, menu, index2);
  253. }
  254. break; case 1://open campaign selection window
  255. {
  256. return boost::bind(&CGPreGame::openCampaignScreen, CGP, commands.front());
  257. }
  258. break; case 2://start
  259. {
  260. switch (std::find(gameType.begin(), gameType.end(), commands.front()) - gameType.begin())
  261. {
  262. case 0: return bind(&CGPreGame::openSel, CGP, CMenuScreen::newGame, CMenuScreen::SINGLE_PLAYER);
  263. case 1: return &pushIntT<CMultiMode>;
  264. case 2: return boost::bind(&CGPreGame::openSel, CGP, CMenuScreen::campaignList, CMenuScreen::SINGLE_PLAYER);
  265. case 3: return boost::function<void()>();//TODO: start tutorial
  266. }
  267. }
  268. break; case 3://load
  269. {
  270. switch (std::find(gameType.begin(), gameType.end(), commands.front()) - gameType.begin())
  271. {
  272. case 0: return boost::bind(&CGPreGame::openSel, CGP, CMenuScreen::loadGame, CMenuScreen::SINGLE_PLAYER);
  273. case 1: return boost::bind(&CGPreGame::openSel, CGP, CMenuScreen::loadGame, CMenuScreen::MULTI_HOT_SEAT);
  274. case 2: return boost::function<void()>();//TODO: load campaign
  275. case 3: return boost::function<void()>();//TODO: load tutorial
  276. }
  277. }
  278. break; case 4://exit
  279. {
  280. return boost::bind(CInfoWindow::showYesNoDialog, boost::ref(CGI->generaltexth->allTexts[69]), (const std::vector<SComponent*>*)0, do_quit, 0, false, 1);
  281. }
  282. break; case 5://highscores
  283. {
  284. return boost::function<void()>(); //TODO: high scores &pushIntT<CHighScores>;
  285. }
  286. }
  287. }
  288. }
  289. tlog0<<"Failed to parse command: "<<string<<"\n";
  290. return boost::function<void()>();
  291. }
  292. AdventureMapButton* CMenuEntry::createButton(CMenuScreen* parent, const JsonNode& button)
  293. {
  294. boost::function<void()> command = genCommand(parent, parent->menuNameToEntry, button["command"].String());
  295. std::pair<std::string, std::string> help;
  296. if (!button["help"].isNull() && button["help"].Float() > 0)
  297. help = CGI->generaltexth->zelp[button["help"].Float()];
  298. return new AdventureMapButton(help, command, button["x"].Float(), button["y"].Float(), button["name"].String(), button["hotkey"].Float());
  299. }
  300. CMenuEntry::CMenuEntry(CMenuScreen* parent, const JsonNode &config)
  301. {
  302. OBJ_CONSTRUCTION_CAPTURING_ALL;
  303. type |= REDRAW_PARENT;
  304. BOOST_FOREACH(const JsonNode& node, config["images"].Vector())
  305. images.push_back(createPicture(node));
  306. BOOST_FOREACH(const JsonNode& node, config["buttons"].Vector())
  307. {
  308. buttons.push_back(createButton(parent, node));
  309. buttons.back()->hoverable = true;
  310. buttons.back()->type |= REDRAW_PARENT;
  311. }
  312. }
  313. CreditsScreen::CreditsScreen()
  314. {
  315. used |= LCLICK | RCLICK;
  316. type |= REDRAW_PARENT;
  317. OBJ_CONSTRUCTION_CAPTURING_ALL;
  318. pos.w = 800;
  319. pos.h = 600;
  320. std::string text = bitmaph->getTextFile("CREDITS");
  321. size_t firstQuote = text.find('\"')+1;
  322. text = text.substr(firstQuote, text.find('\"', firstQuote) - firstQuote );
  323. credits = new CTextBox(text, Rect(450, 600, 350, 32000), 0, FONT_CREDITS, CENTER, zwykly);
  324. credits->pos.h = credits->maxH;
  325. }
  326. void CreditsScreen::show(SDL_Surface *to)
  327. {
  328. static int count = 0;
  329. count++;
  330. if (count == 2)
  331. {
  332. credits->pos.y--;
  333. count = 0;
  334. }
  335. SDL_SetClipRect(screen, &credits->pos);
  336. redraw();
  337. SDL_SetClipRect(screen, NULL);
  338. //end of credits, close this screen
  339. if (credits->pos.y + credits->pos.h < 0)
  340. clickRight(false, false);
  341. }
  342. void CreditsScreen::clickLeft(tribool down, bool previousState)
  343. {
  344. clickRight(down, previousState);
  345. }
  346. void CreditsScreen::clickRight(tribool down, bool previousState)
  347. {
  348. CTabbedInt* menu = dynamic_cast<CTabbedInt*>(parent);
  349. assert(menu);
  350. menu->setActive(0);
  351. }
  352. CGPreGame::CGPreGame():
  353. pregameConfig(new JsonNode(DATA_DIR "/config/mainmenu.json"))
  354. {
  355. OBJ_CONSTRUCTION_CAPTURING_ALL;
  356. GH.defActionsDef = 63;
  357. CGP = this;
  358. menu = new CMenuScreen((*pregameConfig)["window"]);
  359. }
  360. CGPreGame::~CGPreGame()
  361. {
  362. }
  363. void CGPreGame::openSel(CMenuScreen::EState screenType, CMenuScreen::EMultiMode multi /*= CMenuScreen::SINGLE_PLAYER*/)
  364. {
  365. GH.pushInt(new CSelectionScreen(screenType, multi));
  366. }
  367. void CGPreGame::loadGraphics()
  368. {
  369. victory = CDefHandler::giveDef("SCNRVICT.DEF");
  370. loss = CDefHandler::giveDef("SCNRLOSS.DEF");
  371. bonuses = CDefHandler::giveDef("SCNRSTAR.DEF");
  372. rHero = BitmapHandler::loadBitmap("HPSRAND1.bmp");
  373. rTown = BitmapHandler::loadBitmap("HPSRAND0.bmp");
  374. nHero = BitmapHandler::loadBitmap("HPSRAND6.bmp");
  375. nTown = BitmapHandler::loadBitmap("HPSRAND5.bmp");
  376. }
  377. void CGPreGame::disposeGraphics()
  378. {
  379. delete victory;
  380. delete loss;
  381. SDL_FreeSurface(rHero);
  382. SDL_FreeSurface(nHero);
  383. SDL_FreeSurface(rTown);
  384. SDL_FreeSurface(nTown);
  385. }
  386. void CGPreGame::update()
  387. {
  388. if (GH.listInt.empty())
  389. {
  390. GH.pushInt(menu);
  391. menu->switchToTab(0);
  392. }
  393. if(SEL)
  394. SEL->update();
  395. // Handles mouse and key input
  396. GH.updateTime();
  397. GH.handleEvents();
  398. if (GH.curInt == NULL) // no redraw, when a new game was created
  399. return;
  400. GH.topInt()->show(screen);
  401. if (conf.cc.showFPS)
  402. GH.drawFPSCounter();
  403. // draw the mouse cursor and update the screen
  404. CCS->curh->draw1();
  405. CSDL_Ext::update(screen);
  406. CCS->curh->draw2();
  407. }
  408. void CGPreGame::openCampaignScreen(std::string name)
  409. {
  410. BOOST_FOREACH(const JsonNode& node, (*pregameConfig)["campaignsset"].Vector())
  411. {
  412. if (node["name"].String() == name)
  413. {
  414. GH.pushInt(new CCampaignScreen(node));
  415. return;
  416. }
  417. }
  418. tlog1<<"Unknown campaign set: "<<name<<"\n";
  419. }
  420. CSelectionScreen::CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer /*= CMenuScreen::SINGLE_PLAYER*/, const std::map<ui32, std::string> *Names /*= NULL*/)
  421. : ISelectionScreenInfo(Names), serverHandlingThread(NULL), mx(new boost::recursive_mutex),
  422. serv(NULL), ongoingClosing(false), myNameID(255)
  423. {
  424. screenType = Type;
  425. multiPlayer = MultiPlayer;
  426. OBJ_CONSTRUCTION_CAPTURING_ALL;
  427. bool network = (MultiPlayer == CMenuScreen::MULTI_NETWORK_GUEST || MultiPlayer == CMenuScreen::MULTI_NETWORK_HOST);
  428. CServerHandler *sh = NULL;
  429. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST)
  430. {
  431. sh = new CServerHandler;
  432. sh->startServer();
  433. }
  434. IShowActivable::type = BLOCK_ADV_HOTKEYS;
  435. pos.w = 762;
  436. pos.h = 584;
  437. if(Type == CMenuScreen::saveGame)
  438. {
  439. center(pos);
  440. }
  441. else if(Type == CMenuScreen::campaignList)
  442. {
  443. bg = new CPicture(BitmapHandler::loadBitmap("CamCust.bmp"), 0, 0, true);
  444. pos.x = 3;
  445. pos.y = 6;
  446. }
  447. else
  448. {
  449. pos.x = 3;
  450. pos.y = 6;
  451. bg = new CPicture(BitmapHandler::loadBitmap(rand()%2 ? "ZPIC1000.bmp" : "ZPIC1001.bmp"), -3, -6, true);
  452. }
  453. CGP->loadGraphics();
  454. sInfo.difficulty = 1;
  455. current = NULL;
  456. sInfo.mode = (Type == CMenuScreen::newGame ? StartInfo::NEW_GAME : StartInfo::LOAD_GAME);
  457. sInfo.turnTime = 0;
  458. curTab = NULL;
  459. card = new InfoCard(network); //right info card
  460. if (screenType == CMenuScreen::campaignList)
  461. {
  462. opt = NULL;
  463. }
  464. else
  465. {
  466. opt = new OptionsTab(); //scenario options tab
  467. opt->recActions = DISPOSE;
  468. }
  469. sel = new SelectionTab(screenType, bind(&CSelectionScreen::changeSelection, this, _1), multiPlayer); //scenario selection tab
  470. sel->recActions = DISPOSE;
  471. switch(screenType)
  472. {
  473. case CMenuScreen::newGame:
  474. {
  475. card->difficulty->onChange = bind(&CSelectionScreen::difficultyChange, this, _1);
  476. card->difficulty->select(1, 0);
  477. AdventureMapButton *select = new AdventureMapButton(CGI->generaltexth->zelp[45], bind(&CSelectionScreen::toggleTab, this, sel), 411, 75, "GSPBUTT.DEF", SDLK_s);
  478. select->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL);
  479. AdventureMapButton *opts = new AdventureMapButton(CGI->generaltexth->zelp[46], bind(&CSelectionScreen::toggleTab, this, opt), 411, 503, "GSPBUTT.DEF", SDLK_a);
  480. opts->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL);
  481. AdventureMapButton *random = new AdventureMapButton(CGI->generaltexth->zelp[47], bind(&CSelectionScreen::toggleTab, this, sel), 411, 99, "GSPBUTT.DEF", SDLK_r);
  482. random->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL);
  483. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRBEG.DEF", SDLK_b);
  484. if(network)
  485. {
  486. AdventureMapButton *hideChat = new AdventureMapButton(CGI->generaltexth->zelp[48], bind(&InfoCard::toggleChat, card), 619, 75, "GSPBUT2.DEF", SDLK_h);
  487. hideChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL);
  488. if(multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  489. {
  490. SDL_Color orange = {232, 184, 32, 0};
  491. select->text->color = opts->text->color = random->text->color = orange;
  492. select->block(true);
  493. opts->block(true);
  494. random->block(true);
  495. start->block(true);
  496. }
  497. }
  498. }
  499. break;
  500. case CMenuScreen::loadGame:
  501. sel->recActions = 255;
  502. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRLOD.DEF", SDLK_l);
  503. break;
  504. case CMenuScreen::saveGame:
  505. sel->recActions = 255;
  506. start = new AdventureMapButton("", CGI->generaltexth->zelp[103].second, bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRSAV.DEF");
  507. break;
  508. case CMenuScreen::campaignList:
  509. sel->recActions = 255;
  510. start = new AdventureMapButton(std::pair<std::string, std::string>(), bind(&CSelectionScreen::startCampaign, this), 411, 529, "SCNRLOD.DEF", SDLK_b);
  511. break;
  512. }
  513. start->assignedKeys.insert(SDLK_RETURN);
  514. std::string backName;
  515. if(Type == CMenuScreen::campaignList)
  516. {
  517. backName = "SCNRBACK.DEF";
  518. }
  519. else
  520. {
  521. backName = "SCNRBACK.DEF";
  522. }
  523. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 581, 529, backName, SDLK_ESCAPE);
  524. if(network)
  525. {
  526. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST)
  527. {
  528. assert(playerNames.size() == 1 && vstd::contains(playerNames, 1)); //TODO hot-seat/network combo
  529. serv = sh->connectToServer();
  530. *serv << (ui8) 4;
  531. myNameID = 1;
  532. }
  533. else
  534. {
  535. serv = CServerHandler::justConnectToServer();
  536. }
  537. *serv << playerNames.begin()->second;
  538. if(multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  539. {
  540. const CMapInfo *map;
  541. *serv >> myNameID >> map;
  542. serv->connectionID = myNameID;
  543. changeSelection(map);
  544. }
  545. else //host
  546. {
  547. if(current)
  548. {
  549. SelectMap sm(*current);
  550. *serv << &sm;
  551. UpdateStartOptions uso(sInfo);
  552. *serv << &uso;
  553. }
  554. }
  555. applier = new CApplier<CBaseForPGApply>;
  556. registerTypes4(*applier);
  557. serverHandlingThread = new boost::thread(&CSelectionScreen::handleConnection, this);
  558. }
  559. delete sh;
  560. }
  561. CSelectionScreen::~CSelectionScreen()
  562. {
  563. ongoingClosing = true;
  564. if(serv)
  565. {
  566. assert(serverHandlingThread);
  567. QuitMenuWithoutStarting qmws;
  568. *serv << &qmws;
  569. // while(!serverHandlingThread->timed_join(boost::posix_time::milliseconds(50)))
  570. // processPacks();
  571. serverHandlingThread->join();
  572. delete serverHandlingThread;
  573. }
  574. playerColor = -1;
  575. playerNames.clear();
  576. assert(!serv);
  577. delNull(applier);
  578. delete mx;
  579. }
  580. void CSelectionScreen::toggleTab(CIntObject *tab)
  581. {
  582. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST && serv)
  583. {
  584. PregameGuiAction pga;
  585. if(tab == curTab)
  586. pga.action = PregameGuiAction::NO_TAB;
  587. else if(tab == opt)
  588. pga.action = PregameGuiAction::OPEN_OPTIONS;
  589. else if(tab == sel)
  590. pga.action = PregameGuiAction::OPEN_SCENARIO_LIST;
  591. *serv << &pga;
  592. }
  593. if(curTab && curTab->active)
  594. {
  595. curTab->deactivate();
  596. curTab->recActions = DISPOSE;
  597. }
  598. if(curTab != tab)
  599. {
  600. tab->recActions = 255;
  601. tab->activate();
  602. curTab = tab;
  603. }
  604. else
  605. {
  606. curTab = NULL;
  607. };
  608. GH.totalRedraw();
  609. }
  610. void CSelectionScreen::changeSelection( const CMapInfo *to )
  611. {
  612. if(multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  613. {
  614. delNull(current);
  615. }
  616. current = to;
  617. if(to && (screenType == CMenuScreen::loadGame ||
  618. screenType == CMenuScreen::saveGame))
  619. SEL->sInfo.difficulty = to->scenarioOpts->difficulty;
  620. if(screenType != CMenuScreen::campaignList)
  621. {
  622. updateStartInfo(to ? to->filename : "", sInfo, to ? to->mapHeader : NULL);
  623. }
  624. card->changeSelection(to);
  625. if(screenType != CMenuScreen::campaignList)
  626. {
  627. opt->recreate();
  628. }
  629. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST && serv)
  630. {
  631. SelectMap sm(*to);
  632. *serv << &sm;
  633. UpdateStartOptions uso(sInfo);
  634. *serv << &uso;
  635. }
  636. }
  637. void CSelectionScreen::startCampaign()
  638. {
  639. CCampaign * ourCampaign = CCampaignHandler::getCampaign(SEL->current->filename, SEL->current->lodCmpgn);
  640. CCampaignState * campState = new CCampaignState();
  641. campState->camp = ourCampaign;
  642. GH.pushInt( new CBonusSelection(campState) );
  643. }
  644. void CSelectionScreen::startGame()
  645. {
  646. if(screenType == CMenuScreen::newGame)
  647. {
  648. //there must be at least one human player before game can be started
  649. std::map<int, PlayerSettings>::const_iterator i;
  650. for(i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  651. if(i->second.human)
  652. break;
  653. if(i == SEL->sInfo.playerInfos.end())
  654. {
  655. GH.pushInt(CInfoWindow::create(CGI->generaltexth->allTexts[530])); //You must position yourself prior to starting the game.
  656. return;
  657. }
  658. }
  659. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST)
  660. {
  661. start->block(true);
  662. StartWithCurrentSettings swcs;
  663. *serv << &swcs;
  664. ongoingClosing = true;
  665. return;
  666. }
  667. if(screenType != CMenuScreen::saveGame)
  668. {
  669. if(!current)
  670. return;
  671. selectedName = sInfo.mapname;
  672. StartInfo *si = new StartInfo(sInfo);
  673. GH.popIntTotally(this); //delete me
  674. GH.popInt(GH.topInt()); //only deactivate main menu screen
  675. //SEL->current = NULL;
  676. //curOpts = NULL;
  677. ::startGame(si);
  678. }
  679. else
  680. {
  681. if(!(sel && sel->txt && sel->txt->text.size()))
  682. return;
  683. selectedName = GVCMIDirs.UserPath + "/Games/" + sel->txt->text + ".vlgm1";
  684. CFunctionList<void()> overWrite;
  685. overWrite += boost::bind(&CCallback::save, LOCPLINT->cb, sel->txt->text);
  686. overWrite += bind(&CGuiHandler::popIntTotally, &GH, this);
  687. if(fs::exists(selectedName))
  688. {
  689. std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite?
  690. boost::algorithm::replace_first(hlp, "%s", sel->txt->text);
  691. LOCPLINT->showYesNoDialog(hlp, std::vector<SComponent*>(), overWrite, 0, false);
  692. }
  693. else
  694. overWrite();
  695. // LOCPLINT->cb->save(sel->txt->text);
  696. // GH.popIntTotally(this);
  697. }
  698. }
  699. void CSelectionScreen::difficultyChange( int to )
  700. {
  701. assert(screenType == CMenuScreen::newGame);
  702. sInfo.difficulty = to;
  703. propagateOptions();
  704. redraw();
  705. }
  706. void CSelectionScreen::handleConnection()
  707. {
  708. setThreadName(-1, "CSelectionScreen::handleConnection");
  709. try
  710. {
  711. assert(serv);
  712. while(serv)
  713. {
  714. CPackForSelectionScreen *pack = NULL;
  715. *serv >> pack;
  716. assert(pack);
  717. if(QuitMenuWithoutStarting *endingPack = dynamic_cast<QuitMenuWithoutStarting *>(pack))
  718. {
  719. endingPack->apply(this);
  720. }
  721. else if(StartWithCurrentSettings *endingPack = dynamic_cast<StartWithCurrentSettings *>(pack))
  722. {
  723. endingPack->apply(this);
  724. }
  725. else
  726. {
  727. boost::unique_lock<boost::recursive_mutex> lll(*mx);
  728. upcomingPacks.push_back(pack);
  729. }
  730. }
  731. } HANDLE_EXCEPTION
  732. catch(int i)
  733. {
  734. if(i != 666)
  735. throw;
  736. }
  737. }
  738. void CSelectionScreen::setSInfo(const StartInfo &si)
  739. {
  740. std::map<int, PlayerSettings>::const_iterator i;
  741. for(i = si.playerInfos.begin(); i != si.playerInfos.end(); i++)
  742. {
  743. if(i->second.human == myNameID)
  744. {
  745. playerColor = i->first;
  746. break;
  747. }
  748. }
  749. if(i == si.playerInfos.end()) //not found
  750. playerColor = -1;
  751. sInfo = si;
  752. if(current)
  753. opt->recreate(); //will force to recreate using current sInfo
  754. card->difficulty->select(si.difficulty, 0);
  755. GH.totalRedraw();
  756. }
  757. void CSelectionScreen::processPacks()
  758. {
  759. boost::unique_lock<boost::recursive_mutex> lll(*mx);
  760. while(upcomingPacks.size())
  761. {
  762. CPackForSelectionScreen *pack = upcomingPacks.front();
  763. upcomingPacks.pop_front();
  764. CBaseForPGApply *apply = applier->apps[typeList.getTypeID(pack)]; //find the applier
  765. apply->applyOnPG(this, pack);
  766. delete pack;
  767. }
  768. }
  769. void CSelectionScreen::update()
  770. {
  771. if(serverHandlingThread)
  772. processPacks();
  773. }
  774. void CSelectionScreen::propagateOptions()
  775. {
  776. if(isHost() && serv)
  777. {
  778. UpdateStartOptions ups(sInfo);
  779. *serv << &ups;
  780. }
  781. }
  782. void CSelectionScreen::postRequest(ui8 what, ui8 dir)
  783. {
  784. if(!isGuest() || !serv)
  785. return;
  786. RequestOptionsChange roc(what, dir, myNameID);
  787. *serv << &roc;
  788. }
  789. void CSelectionScreen::postChatMessage(const std::string &txt)
  790. {
  791. assert(serv);
  792. ChatMessage cm;
  793. cm.message = txt;
  794. cm.playerName = sInfo.getPlayersSettings(myNameID)->name;
  795. *serv << &cm;
  796. }
  797. void CSelectionScreen::propagateNames()
  798. {
  799. PlayersNames pn;
  800. pn.playerNames = playerNames;
  801. *serv << &pn;
  802. }
  803. // A new size filter (Small, Medium, ...) has been selected. Populate
  804. // selMaps with the relevant data.
  805. void SelectionTab::filter( int size, bool selectFirst )
  806. {
  807. curItems.clear();
  808. if(tabType == CMenuScreen::campaignList)
  809. {
  810. for (size_t i=0; i<allItems.size(); i++)
  811. curItems.push_back(&allItems[i]);
  812. }
  813. else
  814. {
  815. for (size_t i=0; i<allItems.size(); i++)
  816. if( allItems[i].mapHeader && allItems[i].mapHeader->version && (!size || allItems[i].mapHeader->width == size))
  817. curItems.push_back(&allItems[i]);
  818. }
  819. if(curItems.size())
  820. {
  821. slider->block(false);
  822. slider->setAmount(curItems.size());
  823. sort();
  824. if(selectFirst)
  825. {
  826. slider->moveTo(0);
  827. onSelect(curItems[0]);
  828. }
  829. selectAbs(0);
  830. }
  831. else
  832. {
  833. slider->block(true);
  834. onSelect(NULL);
  835. }
  836. }
  837. void SelectionTab::getFiles(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext)
  838. {
  839. CFileUtility::getFilesWithExt(out, dirname, ext);
  840. allItems.resize(out.size());
  841. }
  842. void SelectionTab::parseMaps(std::vector<FileInfo> &files, int start, int threads)
  843. {
  844. int read=0;
  845. unsigned char mapBuffer[1500];
  846. while(start < allItems.size())
  847. {
  848. gzFile tempf = gzopen(files[start].name.c_str(),"rb");
  849. read = gzread(tempf, mapBuffer, 1500);
  850. gzclose(tempf);
  851. if(read < 50 || !mapBuffer[4])
  852. {
  853. tlog3 << "\t\tWarning: corrupted map file: " << files[start].name << std::endl;
  854. }
  855. else //valid map
  856. {
  857. allItems[start].mapInit(files[start].name, mapBuffer);
  858. //allItems[start].date = "DATEDATE";// files[start].date;
  859. }
  860. start += threads;
  861. }
  862. }
  863. void SelectionTab::parseGames(std::vector<FileInfo> &files, bool multi)
  864. {
  865. for(int i=0; i<files.size(); i++)
  866. {
  867. CLoadFile lf(files[i].name);
  868. if(!lf.sfile)
  869. continue;
  870. ui8 sign[8];
  871. lf >> sign;
  872. if(std::memcmp(sign,"VCMISVG",7))
  873. {
  874. tlog1 << files[i].name << " is not a correct savefile!" << std::endl;
  875. continue;
  876. }
  877. allItems[i].mapHeader = new CMapHeader();
  878. lf >> *(allItems[i].mapHeader) >> allItems[i].scenarioOpts;
  879. allItems[i].filename = files[i].name;
  880. allItems[i].countPlayers();
  881. allItems[i].date = std::asctime(std::localtime(&files[i].date));
  882. if((allItems[i].actualHumanPlayers > 1) != multi) //if multi mode then only multi games, otherwise single
  883. {
  884. delete allItems[i].mapHeader;
  885. allItems[i].mapHeader = NULL;
  886. }
  887. }
  888. }
  889. void SelectionTab::parseCampaigns( std::vector<FileInfo> & files )
  890. {
  891. for(int i=0; i<files.size(); i++)
  892. {
  893. //allItems[i].date = std::asctime(std::localtime(&files[i].date));
  894. allItems[i].filename = files[i].name;
  895. allItems[i].lodCmpgn = files[i].inLod;
  896. allItems[i].campaignInit();
  897. }
  898. }
  899. SelectionTab::SelectionTab(CMenuScreen::EState Type, const boost::function<void(CMapInfo *)> &OnSelect, CMenuScreen::EMultiMode MultiPlayer /*= CMenuScreen::SINGLE_PLAYER*/)
  900. :bg(NULL), onSelect(OnSelect)
  901. {
  902. OBJ_CONSTRUCTION;
  903. selectionPos = 0;
  904. used = LCLICK | WHEEL | KEYBOARD | DOUBLECLICK;
  905. slider = NULL;
  906. txt = NULL;
  907. tabType = Type;
  908. if (Type != CMenuScreen::campaignList)
  909. {
  910. bg = new CPicture(BitmapHandler::loadBitmap("SCSELBCK.bmp"), 0, 0, true);
  911. pos.w = bg->pos.w;
  912. pos.h = bg->pos.h;
  913. }
  914. else
  915. {
  916. SDL_Surface * tmp1 = BitmapHandler::loadBitmap("CAMCUST.bmp");
  917. SDL_Surface * tmp = CSDL_Ext::newSurface(400, tmp1->h);
  918. blitAt(tmp1, 0, 0, tmp);
  919. SDL_FreeSurface(tmp1);
  920. bg = new CPicture(tmp, 0, 0, true);
  921. pos.w = bg->pos.w;
  922. pos.h = bg->pos.h;
  923. bg->pos.x = bg->pos.y = 0;
  924. }
  925. if(MultiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  926. {
  927. positions = 18;
  928. }
  929. else
  930. {
  931. std::vector<FileInfo> toParse;
  932. std::vector<CCampaignHeader> cpm;
  933. switch(tabType)
  934. {
  935. case CMenuScreen::newGame:
  936. getFiles(toParse, DATA_DIR "/Maps", "h3m"); //get all maps
  937. /* Load maps from user directory too, unless it is also the
  938. * same as the data directory (as is the case on
  939. * windows). */
  940. if (GVCMIDirs.UserPath != DATA_DIR)
  941. getFiles(toParse, GVCMIDirs.UserPath + "/Maps", "h3m"); //get all maps
  942. parseMaps(toParse);
  943. positions = 18;
  944. break;
  945. case CMenuScreen::loadGame:
  946. case CMenuScreen::saveGame:
  947. getFiles(toParse, GVCMIDirs.UserPath + "/Games", "vlgm1"); //get all saves
  948. parseGames(toParse, MultiPlayer);
  949. if(tabType == CMenuScreen::loadGame)
  950. {
  951. positions = 18;
  952. }
  953. else
  954. {
  955. positions = 16;
  956. }
  957. if(tabType == CMenuScreen::saveGame)
  958. txt = new CTextInput(Rect(32, 539, 350, 20), Point(-32, -25), "GSSTRIP.bmp", 0);
  959. break;
  960. case CMenuScreen::campaignList:
  961. getFiles(toParse, DATA_DIR "/Maps", "h3c"); //get all campaigns
  962. for (int g=0; g<toParse.size(); ++g)
  963. {
  964. toParse[g].inLod = false;
  965. }
  966. //add lod cmpgns
  967. cpm = CCampaignHandler::getCampaignHeaders(CCampaignHandler::ALL);
  968. for (int g = 0; g < cpm.size(); g++)
  969. {
  970. FileInfo fi;
  971. fi.inLod = cpm[g].loadFromLod;
  972. fi.name = cpm[g].filename;
  973. toParse.push_back(fi);
  974. if (cpm[g].loadFromLod)
  975. {
  976. allItems.push_back(CMapInfo(false));
  977. }
  978. }
  979. parseCampaigns(toParse);
  980. positions = 18;
  981. break;
  982. default:
  983. assert(0);
  984. break;
  985. }
  986. }
  987. if (tabType != CMenuScreen::campaignList)
  988. {
  989. //size filter buttons
  990. {
  991. int sizes[] = {36, 72, 108, 144, 0};
  992. const char * names[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"};
  993. for(int i = 0; i < 5; i++)
  994. new AdventureMapButton("", CGI->generaltexth->zelp[54+i].second, bind(&SelectionTab::filter, this, sizes[i], true), 158 + 47*i, 46, names[i]);
  995. }
  996. //sort buttons buttons
  997. {
  998. int xpos[] = {23, 55, 88, 121, 306, 339};
  999. const char * names[] = {"SCBUTT1.DEF", "SCBUTT2.DEF", "SCBUTCP.DEF", "SCBUTT3.DEF", "SCBUTT4.DEF", "SCBUTT5.DEF"};
  1000. for(int i = 0; i < 6; i++)
  1001. new AdventureMapButton("", CGI->generaltexth->zelp[107+i].second, bind(&SelectionTab::sortBy, this, i), xpos[i], 86, names[i]);
  1002. }
  1003. }
  1004. else
  1005. {
  1006. //sort by buttons
  1007. new AdventureMapButton("", "", bind(&SelectionTab::sortBy, this, _numOfMaps), 23, 86, "CamCusM.DEF"); //by num of maps
  1008. new AdventureMapButton("", "", bind(&SelectionTab::sortBy, this, _name), 55, 86, "CamCusL.DEF"); //by name
  1009. }
  1010. slider = new CSlider(372, 86, tabType != CMenuScreen::saveGame ? 480 : 430, bind(&SelectionTab::sliderMove, this, _1), positions, curItems.size(), 0, false, 1);
  1011. slider->changeUsedEvents(WHEEL, true);
  1012. slider->slider->keepFrame = true;
  1013. format = CDefHandler::giveDef("SCSELC.DEF");
  1014. sortingBy = _format;
  1015. ascending = true;
  1016. filter(0);
  1017. //select(0);
  1018. switch(tabType)
  1019. {
  1020. case CMenuScreen::newGame:
  1021. selectFName(DATA_DIR "/Maps/Arrogance.h3m");
  1022. break;
  1023. case CMenuScreen::loadGame:
  1024. case CMenuScreen::campaignList:
  1025. select(0);
  1026. break;
  1027. case CMenuScreen::saveGame:;
  1028. if(selectedName.size())
  1029. {
  1030. if(selectedName[2] == 'M') //name starts with ./Maps instead of ./Games => there was nothing to select
  1031. txt->setText("NEWGAME");
  1032. else
  1033. selectFName(selectedName);
  1034. }
  1035. }
  1036. }
  1037. SelectionTab::~SelectionTab()
  1038. {
  1039. delete format;
  1040. }
  1041. void SelectionTab::sortBy( int criteria )
  1042. {
  1043. if(criteria == sortingBy)
  1044. {
  1045. ascending = !ascending;
  1046. }
  1047. else
  1048. {
  1049. sortingBy = (ESortBy)criteria;
  1050. ascending = true;
  1051. }
  1052. sort();
  1053. selectAbs(0);
  1054. }
  1055. void SelectionTab::sort()
  1056. {
  1057. if(sortingBy != _name)
  1058. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(_name));
  1059. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(sortingBy));
  1060. if(!ascending)
  1061. std::reverse(curItems.begin(), curItems.end());
  1062. redraw();
  1063. }
  1064. void SelectionTab::select( int position )
  1065. {
  1066. if(!curItems.size()) return;
  1067. // New selection. py is the index in curItems.
  1068. int py = position + slider->value;
  1069. amax(py, 0);
  1070. amin(py, curItems.size()-1);
  1071. selectionPos = py;
  1072. if(position < 0)
  1073. slider->moveTo(slider->value + position);
  1074. else if(position >= positions)
  1075. slider->moveTo(slider->value + position - positions + 1);
  1076. if(txt)
  1077. txt->setText(fs::basename(curItems[py]->filename));
  1078. onSelect(curItems[py]);
  1079. }
  1080. void SelectionTab::selectAbs( int position )
  1081. {
  1082. select(position - slider->value);
  1083. }
  1084. int SelectionTab::getPosition( int x, int y )
  1085. {
  1086. return -1;
  1087. }
  1088. void SelectionTab::sliderMove( int slidPos )
  1089. {
  1090. if(!slider) return; //ignore spurious call when slider is being created
  1091. redraw();
  1092. }
  1093. // Display the tab with the scenario names
  1094. //
  1095. // elemIdx is the index of the maps or saved game to display on line 0
  1096. // slider->capacity contains the number of available screen lines
  1097. // slider->positionsAmnt is the number of elements after filtering
  1098. void SelectionTab::printMaps(SDL_Surface *to)
  1099. {
  1100. int elemIdx = slider->value;
  1101. // Display all elements if there's enough space
  1102. //if(slider->amount < slider->capacity)
  1103. // elemIdx = 0;
  1104. SDL_Color itemColor;
  1105. #define POS(xx, yy) pos.x + xx, pos.y + yy + line*25
  1106. for (int line = 0; line < positions && elemIdx < curItems.size(); elemIdx++, line++)
  1107. {
  1108. CMapInfo *currentItem = curItems[elemIdx];
  1109. if (elemIdx == selectionPos)
  1110. itemColor=tytulowy;
  1111. else
  1112. itemColor=zwykly;
  1113. if(tabType != CMenuScreen::campaignList)
  1114. {
  1115. //amount of players
  1116. std::ostringstream ostr(std::ostringstream::out);
  1117. ostr << currentItem->playerAmnt << "/" << currentItem->humenPlayers;
  1118. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, itemColor, to);
  1119. //map size
  1120. std::string temp2 = "C";
  1121. switch (currentItem->mapHeader->width)
  1122. {
  1123. case 36:
  1124. temp2="S";
  1125. break;
  1126. case 72:
  1127. temp2="M";
  1128. break;
  1129. case 108:
  1130. temp2="L";
  1131. break;
  1132. case 144:
  1133. temp2="XL";
  1134. break;
  1135. }
  1136. CSDL_Ext::printAtMiddle(temp2, POS(70, 128), FONT_SMALL, itemColor, to);
  1137. int temp=-1;
  1138. switch (currentItem->mapHeader->version)
  1139. {
  1140. case CMapHeader::RoE:
  1141. temp=0;
  1142. break;
  1143. case CMapHeader::AB:
  1144. temp=1;
  1145. break;
  1146. case CMapHeader::SoD:
  1147. temp=2;
  1148. break;
  1149. case CMapHeader::WoG:
  1150. temp=3;
  1151. break;
  1152. default:
  1153. // Unknown version. Be safe and ignore that map
  1154. tlog2 << "Warning: " << currentItem->filename << " has wrong version!\n";
  1155. continue;
  1156. }
  1157. blitAt(format->ourImages[temp].bitmap, POS(88, 117), to);
  1158. //victory conditions
  1159. if (currentItem->mapHeader->victoryCondition.condition == winStandard)
  1160. temp = 11;
  1161. else
  1162. temp = currentItem->mapHeader->victoryCondition.condition;
  1163. blitAt(CGP->victory->ourImages[temp].bitmap, POS(306, 117), to);
  1164. //loss conditions
  1165. if (currentItem->mapHeader->lossCondition.typeOfLossCon == lossStandard)
  1166. temp=3;
  1167. else
  1168. temp=currentItem->mapHeader->lossCondition.typeOfLossCon;
  1169. blitAt(CGP->loss->ourImages[temp].bitmap, POS(339, 117), to);
  1170. }
  1171. else //if campaign
  1172. {
  1173. //number of maps in campaign
  1174. std::ostringstream ostr(std::ostringstream::out);
  1175. ostr << CGI->generaltexth->campaignRegionNames[ currentItem->campaignHeader->mapVersion ].size();
  1176. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, itemColor, to);
  1177. }
  1178. std::string name;
  1179. if(tabType == CMenuScreen::newGame)
  1180. {
  1181. if (!currentItem->mapHeader->name.length())
  1182. currentItem->mapHeader->name = "Unnamed";
  1183. name = currentItem->mapHeader->name;
  1184. }
  1185. else if(tabType == CMenuScreen::campaignList)
  1186. {
  1187. name = currentItem->campaignHeader->name;
  1188. }
  1189. else
  1190. {
  1191. name = fs::basename(currentItem->filename);
  1192. }
  1193. //print name
  1194. CSDL_Ext::printAtMiddle(name, POS(213, 128), FONT_SMALL, itemColor, to);
  1195. }
  1196. #undef POS
  1197. }
  1198. void SelectionTab::showAll( SDL_Surface * to )
  1199. {
  1200. CIntObject::showAll(to);
  1201. printMaps(to);
  1202. std::string title;
  1203. switch(tabType) {
  1204. case CMenuScreen::newGame:
  1205. title = CGI->generaltexth->arraytxt[229];
  1206. break;
  1207. case CMenuScreen::loadGame:
  1208. title = CGI->generaltexth->arraytxt[230];
  1209. break;
  1210. case CMenuScreen::saveGame:
  1211. title = CGI->generaltexth->arraytxt[231];
  1212. break;
  1213. case CMenuScreen::campaignList:
  1214. title = "Select a Campaign"; //TODO: find where is the title
  1215. break;
  1216. }
  1217. CSDL_Ext::printAtMiddle(title, pos.x+205, pos.y+28, FONT_MEDIUM, tytulowy, to); //Select a Scenario to Play
  1218. if(tabType != CMenuScreen::campaignList)
  1219. {
  1220. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[510], pos.x+87, pos.y+62, FONT_SMALL, tytulowy, to); //Map sizes
  1221. }
  1222. }
  1223. void SelectionTab::clickLeft( tribool down, bool previousState )
  1224. {
  1225. if(down)
  1226. {
  1227. int line = getLine();
  1228. if(line != -1)
  1229. select(line);
  1230. }
  1231. }
  1232. void SelectionTab::keyPressed( const SDL_KeyboardEvent & key )
  1233. {
  1234. if(key.state != SDL_PRESSED) return;
  1235. int moveBy = 0;
  1236. switch(key.keysym.sym)
  1237. {
  1238. case SDLK_UP:
  1239. moveBy = -1;
  1240. break;
  1241. case SDLK_DOWN:
  1242. moveBy = +1;
  1243. break;
  1244. case SDLK_PAGEUP:
  1245. moveBy = -positions+1;
  1246. break;
  1247. case SDLK_PAGEDOWN:
  1248. moveBy = +positions-1;
  1249. break;
  1250. case SDLK_HOME:
  1251. select(-slider->value);
  1252. return;
  1253. case SDLK_END:
  1254. select(curItems.size() - slider->value);
  1255. return;
  1256. default:
  1257. return;
  1258. }
  1259. select(selectionPos - slider->value + moveBy);
  1260. }
  1261. void SelectionTab::onDoubleClick()
  1262. {
  1263. if(getLine() != -1) //double clicked scenarios list
  1264. {
  1265. //act as if start button was pressed
  1266. (static_cast<CSelectionScreen*>(parent))->start->callback();
  1267. }
  1268. }
  1269. int SelectionTab::getLine()
  1270. {
  1271. int line = -1;
  1272. Point clickPos(GH.current->button.x, GH.current->button.y);
  1273. clickPos = clickPos - pos.topLeft();
  1274. if (clickPos.y > 115 && clickPos.y < 564 && clickPos.x > 22 && clickPos.x < 371)
  1275. {
  1276. line = (clickPos.y-115) / 25; //which line
  1277. }
  1278. return line;
  1279. }
  1280. void SelectionTab::selectFName( const std::string &fname )
  1281. {
  1282. for(int i = curItems.size() - 1; i >= 0; i--)
  1283. {
  1284. if(curItems[i]->filename == fname)
  1285. {
  1286. slider->moveTo(i);
  1287. selectAbs(i);
  1288. return;
  1289. }
  1290. }
  1291. selectAbs(0);
  1292. }
  1293. CChatBox::CChatBox(const Rect &rect)
  1294. {
  1295. OBJ_CONSTRUCTION;
  1296. pos += rect;
  1297. used = KEYBOARD;
  1298. captureAllKeys = true;
  1299. const int height = graphics->fonts[FONT_SMALL]->height;
  1300. inputBox = new CTextInput(Rect(0, rect.h - height, rect.w, height));
  1301. inputBox->used &= ~KEYBOARD;
  1302. chatHistory = new CTextBox("", Rect(0, 0, rect.w, rect.h - height), 1);
  1303. SDL_Color green = {0,252,0};
  1304. chatHistory->color = green;
  1305. }
  1306. void CChatBox::keyPressed(const SDL_KeyboardEvent & key)
  1307. {
  1308. if(key.keysym.sym == SDLK_RETURN && key.state == SDL_PRESSED && inputBox->text.size())
  1309. {
  1310. SEL->postChatMessage(inputBox->text);
  1311. inputBox->setText("");
  1312. }
  1313. else
  1314. inputBox->keyPressed(key);
  1315. }
  1316. void CChatBox::addNewMessage(const std::string &text)
  1317. {
  1318. chatHistory->setTxt(chatHistory->text + text + "\n");
  1319. if(chatHistory->slider)
  1320. chatHistory->slider->moveToMax();
  1321. }
  1322. InfoCard::InfoCard( bool Network )
  1323. : bg(NULL), network(Network), chatOn(false), chat(NULL), playerListBg(NULL),
  1324. difficulty(NULL), sizes(NULL), sFlags(NULL)
  1325. {
  1326. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1327. pos.x += 393;
  1328. used = RCLICK;
  1329. mapDescription = NULL;
  1330. Rect descriptionRect(26, 149, 320, 115);
  1331. mapDescription = new CTextBox("", descriptionRect, 1);
  1332. if(SEL->screenType == CMenuScreen::campaignList)
  1333. {
  1334. CSelectionScreen *ss = static_cast<CSelectionScreen*>(parent);
  1335. 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)
  1336. }
  1337. else
  1338. {
  1339. bg = new CPicture(BitmapHandler::loadBitmap("GSELPOP1.bmp"), 0, 0, true);
  1340. moveChild(bg, this, parent);
  1341. parent->children.insert(parent->children.begin()+1, bg);
  1342. parent->children.pop_back();
  1343. pos.w = bg->pos.w;
  1344. pos.h = bg->pos.h;
  1345. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  1346. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  1347. difficulty = new CHighlightableButtonsGroup(0);
  1348. {
  1349. static const char *difButns[] = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  1350. for(int i = 0; i < 5; i++)
  1351. {
  1352. difficulty->addButton(new CHighlightableButton("", CGI->generaltexth->zelp[24+i].second, 0, 110 + i*32, 450, difButns[i], i));
  1353. }
  1354. }
  1355. if(SEL->screenType != CMenuScreen::newGame)
  1356. difficulty->block(true);
  1357. //description needs bg
  1358. moveChild(new CPicture(*bg, descriptionRect), this, mapDescription, true); //move subpicture bg to our description control (by default it's our (Infocard) child)
  1359. if(network)
  1360. {
  1361. playerListBg = new CPicture("CHATPLUG.bmp", 16, 276);
  1362. chat = new CChatBox(descriptionRect);
  1363. moveChild(new CPicture(*bg, chat->chatHistory->pos - pos), this, chat->chatHistory, true); //move subpicture bg to our description control (by default it's our (Infocard) child)
  1364. chatOn = true;
  1365. mapDescription->disable();
  1366. }
  1367. }
  1368. }
  1369. InfoCard::~InfoCard()
  1370. {
  1371. delete sizes;
  1372. delete sFlags;
  1373. }
  1374. void InfoCard::showAll( SDL_Surface * to )
  1375. {
  1376. CIntObject::showAll(to);
  1377. //blit texts
  1378. if(SEL->screenType != CMenuScreen::campaignList)
  1379. {
  1380. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 24, 400, FONT_SMALL, zwykly, to); //Allies
  1381. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 190, 400, FONT_SMALL, zwykly, to); //Enemies
  1382. printAtLoc(CGI->generaltexth->allTexts[494], 33, 430, FONT_SMALL, tytulowy, to);//"Map Diff:"
  1383. printAtLoc(CGI->generaltexth->allTexts[492] + ":", 133,430, FONT_SMALL, tytulowy, to); //player difficulty
  1384. printAtLoc(CGI->generaltexth->allTexts[218] + ":", 290,430, FONT_SMALL, tytulowy, to); //"Rating:"
  1385. printAtLoc(CGI->generaltexth->allTexts[495], 26, 22, FONT_SMALL, tytulowy, to); //Scenario Name:
  1386. if(!chatOn)
  1387. {
  1388. printAtLoc(CGI->generaltexth->allTexts[496], 26, 132, FONT_SMALL, tytulowy, to); //Scenario Description:
  1389. printAtLoc(CGI->generaltexth->allTexts[497], 26, 283, FONT_SMALL, tytulowy, to); //Victory Condition:
  1390. printAtLoc(CGI->generaltexth->allTexts[498], 26, 339, FONT_SMALL, tytulowy, to); //Loss Condition:
  1391. }
  1392. else //players list
  1393. {
  1394. std::map<ui32, std::string> playerNames = SEL->playerNames;
  1395. int playerSoFar = 0;
  1396. for (std::map<int, PlayerSettings>::const_iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  1397. {
  1398. if(i->second.human)
  1399. {
  1400. printAtLoc(i->second.name, 24, 285 + playerSoFar++ * graphics->fonts[FONT_SMALL]->height, FONT_SMALL, zwykly, to);
  1401. playerNames.erase(i->second.human);
  1402. }
  1403. }
  1404. playerSoFar = 0;
  1405. for (std::map<ui32, std::string>::const_iterator i = playerNames.begin(); i != playerNames.end(); i++)
  1406. {
  1407. printAtLoc(i->second, 193, 285 + playerSoFar++ * graphics->fonts[FONT_SMALL]->height, FONT_SMALL, zwykly, to);
  1408. }
  1409. }
  1410. }
  1411. if(SEL->current)
  1412. {
  1413. if(SEL->screenType != CMenuScreen::campaignList)
  1414. {
  1415. int temp = -1;
  1416. if(!chatOn)
  1417. {
  1418. //victory conditions
  1419. temp = SEL->current->mapHeader->victoryCondition.condition+1;
  1420. if (temp>20) temp=0;
  1421. std::string sss = CGI->generaltexth->victoryConditions[temp];
  1422. if (temp && SEL->current->mapHeader->victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  1423. printAtLoc(sss, 60, 307, FONT_SMALL, zwykly, to);
  1424. temp = SEL->current->mapHeader->victoryCondition.condition;
  1425. if (temp>12) temp=11;
  1426. blitAtLoc(CGP->victory->ourImages[temp].bitmap, 24, 302, to); //victory cond descr
  1427. //loss conditoins
  1428. temp = SEL->current->mapHeader->lossCondition.typeOfLossCon+1;
  1429. if (temp>20) temp=0;
  1430. sss = CGI->generaltexth->lossCondtions[temp];
  1431. printAtLoc(sss, 60, 366, FONT_SMALL, zwykly, to);
  1432. temp=SEL->current->mapHeader->lossCondition.typeOfLossCon;
  1433. if (temp>12) temp=3;
  1434. blitAtLoc(CGP->loss->ourImages[temp].bitmap, 24, 359, to); //loss cond
  1435. }
  1436. //difficulty
  1437. assert(SEL->current->mapHeader->difficulty <= 4);
  1438. std::string &diff = CGI->generaltexth->arraytxt[142 + SEL->current->mapHeader->difficulty];
  1439. printAtMiddleLoc(diff, 62, 472, FONT_SMALL, zwykly, to);
  1440. //selecting size icon
  1441. switch (SEL->current->mapHeader->width)
  1442. {
  1443. case 36:
  1444. temp=0;
  1445. break;
  1446. case 72:
  1447. temp=1;
  1448. break;
  1449. case 108:
  1450. temp=2;
  1451. break;
  1452. case 144:
  1453. temp=3;
  1454. break;
  1455. default:
  1456. temp=4;
  1457. break;
  1458. }
  1459. blitAtLoc(sizes->ourImages[temp].bitmap, 318, 22, to);
  1460. if(SEL->screenType == CMenuScreen::loadGame)
  1461. printToLoc((static_cast<const CMapInfo*>(SEL->current))->date,308,34, FONT_SMALL, zwykly, to);
  1462. //print flags
  1463. int fx = 34 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[390].c_str());
  1464. int ex = 200 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[391].c_str());
  1465. int myT;
  1466. if(playerColor >= 0)
  1467. myT = SEL->current->mapHeader->players[playerColor].team;
  1468. else
  1469. myT = -1;
  1470. for (std::map<int, PlayerSettings>::const_iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  1471. {
  1472. int *myx = ((i->first == playerColor || SEL->current->mapHeader->players[i->first].team == myT) ? &fx : &ex);
  1473. blitAtLoc(sFlags->ourImages[i->first].bitmap, *myx, 399, to);
  1474. *myx += sFlags->ourImages[i->first].bitmap->w;
  1475. }
  1476. std::string tob;
  1477. switch (SEL->sInfo.difficulty)
  1478. {
  1479. case 0:
  1480. tob="80%";
  1481. break;
  1482. case 1:
  1483. tob="100%";
  1484. break;
  1485. case 2:
  1486. tob="130%";
  1487. break;
  1488. case 3:
  1489. tob="160%";
  1490. break;
  1491. case 4:
  1492. tob="200%";
  1493. break;
  1494. }
  1495. printAtMiddleLoc(tob, 311, 472, FONT_SMALL, zwykly, to);
  1496. }
  1497. //blit description
  1498. std::string name;
  1499. if (SEL->screenType == CMenuScreen::campaignList)
  1500. {
  1501. name = SEL->current->campaignHeader->name;
  1502. }
  1503. else
  1504. {
  1505. name = SEL->current->mapHeader->name;
  1506. }
  1507. //name
  1508. if (name.length())
  1509. printAtLoc(name, 26, 39, FONT_BIG, tytulowy, to);
  1510. else
  1511. printAtLoc("Unnamed", 26, 39, FONT_BIG, tytulowy, to);
  1512. }
  1513. }
  1514. void InfoCard::changeSelection( const CMapInfo *to )
  1515. {
  1516. if(to && mapDescription)
  1517. {
  1518. if (SEL->screenType == CMenuScreen::campaignList)
  1519. mapDescription->setTxt(to->campaignHeader->description);
  1520. else
  1521. mapDescription->setTxt(to->mapHeader->description);
  1522. if(SEL->screenType != CMenuScreen::newGame && SEL->screenType != CMenuScreen::campaignList) {
  1523. difficulty->block(true);
  1524. difficulty->select(SEL->sInfo.difficulty, 0);
  1525. }
  1526. }
  1527. GH.totalRedraw();
  1528. }
  1529. void InfoCard::clickRight( tribool down, bool previousState )
  1530. {
  1531. static const Rect flagArea(19, 397, 335, 23);
  1532. if(down && SEL->current && isItInLoc(flagArea, GH.current->motion.x, GH.current->motion.y))
  1533. showTeamsPopup();
  1534. }
  1535. void InfoCard::showTeamsPopup()
  1536. {
  1537. SDL_Surface *bmp = CMessage::drawBox1(256, 90 + 50 * SEL->current->mapHeader->howManyTeams);
  1538. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[657], 128, 30, FONT_MEDIUM, tytulowy, bmp); //{Team Alignments}
  1539. for(int i = 0; i < SEL->current->mapHeader->howManyTeams; i++)
  1540. {
  1541. std::vector<ui8> flags;
  1542. std::string hlp = CGI->generaltexth->allTexts[656]; //Team %d
  1543. hlp.replace(hlp.find("%d"), 2, boost::lexical_cast<std::string>(i+1));
  1544. CSDL_Ext::printAtMiddle(hlp, 128, 65 + 50*i, FONT_SMALL, zwykly, bmp);
  1545. for(int j = 0; j < PLAYER_LIMIT; j++)
  1546. if((SEL->current->mapHeader->players[j].canHumanPlay || SEL->current->mapHeader->players[j].canComputerPlay)
  1547. && SEL->current->mapHeader->players[j].team == i)
  1548. flags.push_back(j);
  1549. int curx = 128 - 9*flags.size();
  1550. for(int j = 0; j < flags.size(); j++)
  1551. {
  1552. blitAt(sFlags->ourImages[flags[j]].bitmap, curx, 75 + 50*i, bmp);
  1553. curx += 18;
  1554. }
  1555. }
  1556. GH.pushInt(new CInfoPopup(bmp, true));
  1557. }
  1558. void InfoCard::toggleChat()
  1559. {
  1560. setChat(!chatOn);
  1561. }
  1562. void InfoCard::setChat(bool activateChat)
  1563. {
  1564. if(chatOn == activateChat)
  1565. return;
  1566. assert(active);
  1567. if(activateChat)
  1568. {
  1569. mapDescription->disable();
  1570. chat->enable();
  1571. playerListBg->enable();
  1572. }
  1573. else
  1574. {
  1575. mapDescription->enable();
  1576. chat->disable();
  1577. playerListBg->disable();
  1578. }
  1579. chatOn = activateChat;
  1580. GH.totalRedraw();
  1581. }
  1582. OptionsTab::OptionsTab():
  1583. turnDuration(NULL)
  1584. {
  1585. OBJ_CONSTRUCTION;
  1586. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTBK.bmp"), 0, 0, true);
  1587. pos = bg->pos;
  1588. if(SEL->screenType == CMenuScreen::newGame)
  1589. turnDuration = new CSlider(55, 551, 194, bind(&OptionsTab::setTurnLength, this, _1), 1, 11, 11, true, 1);
  1590. }
  1591. OptionsTab::~OptionsTab()
  1592. {
  1593. }
  1594. void OptionsTab::showAll( SDL_Surface * to )
  1595. {
  1596. CIntObject::showAll(to);
  1597. printAtMiddleLoc(CGI->generaltexth->allTexts[515], 222, 30, FONT_BIG, tytulowy, to);
  1598. printAtMiddleWBLoc(CGI->generaltexth->allTexts[516], 222, 58, FONT_SMALL, 55, zwykly, to); //Select starting options, handicap, and name for each player in the game.
  1599. printAtMiddleWBLoc(CGI->generaltexth->allTexts[517], 107, 102, FONT_SMALL, 14, tytulowy, to); //Player Name Handicap Type
  1600. printAtMiddleWBLoc(CGI->generaltexth->allTexts[518], 197, 102, FONT_SMALL, 10, tytulowy, to); //Starting Town
  1601. printAtMiddleWBLoc(CGI->generaltexth->allTexts[519], 273, 102, FONT_SMALL, 10, tytulowy, to); //Starting Hero
  1602. printAtMiddleWBLoc(CGI->generaltexth->allTexts[520], 349, 102, FONT_SMALL, 10, tytulowy, to); //Starting Bonus
  1603. printAtMiddleLoc(CGI->generaltexth->allTexts[521], 222, 538, FONT_SMALL, tytulowy, to); // Player Turn Duration
  1604. if (turnDuration)
  1605. printAtMiddleLoc(CGI->generaltexth->turnDurations[turnDuration->value], 319,559, FONT_SMALL, zwykly, to);//Turn duration value
  1606. }
  1607. void OptionsTab::nextCastle( int player, int dir )
  1608. {
  1609. if(SEL->isGuest())
  1610. {
  1611. SEL->postRequest(RequestOptionsChange::TOWN, dir);
  1612. return;
  1613. }
  1614. PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1615. si32 &cur = s.castle;
  1616. ui32 allowed = SEL->current->mapHeader->players[s.color].allowedFactions;
  1617. if (cur == -2) //no castle - no change
  1618. return;
  1619. if (cur == -1) //random => first/last available
  1620. {
  1621. int pom = (dir>0) ? (0) : (F_NUMBER-1); // last or first
  1622. for (;pom >= 0 && pom < F_NUMBER; pom+=dir)
  1623. {
  1624. if((1 << pom) & allowed)
  1625. {
  1626. cur=pom;
  1627. break;
  1628. }
  1629. }
  1630. }
  1631. else // next/previous available
  1632. {
  1633. for (;;)
  1634. {
  1635. cur+=dir;
  1636. if ((1 << cur) & allowed)
  1637. break;
  1638. if (cur >= F_NUMBER || cur<0)
  1639. {
  1640. double p1 = log((double)allowed) / log(2.0f)+0.000001f;
  1641. double check = p1 - ((int)p1);
  1642. if (check < 0.001)
  1643. cur = (int)p1;
  1644. else
  1645. cur = -1;
  1646. break;
  1647. }
  1648. }
  1649. }
  1650. if(s.hero >= 0)
  1651. s.hero = -1;
  1652. if(cur < 0 && s.bonus == PlayerSettings::bresource)
  1653. s.bonus = PlayerSettings::brandom;
  1654. entries[player]->selectButtons();
  1655. SEL->propagateOptions();
  1656. redraw();
  1657. }
  1658. void OptionsTab::nextHero( int player, int dir )
  1659. {
  1660. if(SEL->isGuest())
  1661. {
  1662. SEL->postRequest(RequestOptionsChange::HERO, dir);
  1663. return;
  1664. }
  1665. PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1666. int old = s.hero;
  1667. if (s.castle < 0 || !s.human || s.hero == -2)
  1668. return;
  1669. if (s.hero == -1) //random => first/last available
  1670. {
  1671. int max = (s.castle*HEROES_PER_TYPE*2+15),
  1672. min = (s.castle*HEROES_PER_TYPE*2);
  1673. s.hero = nextAllowedHero(min,max,0,dir);
  1674. }
  1675. else
  1676. {
  1677. if(dir > 0)
  1678. s.hero = nextAllowedHero(s.hero,(s.castle*HEROES_PER_TYPE*2+16),1,dir);
  1679. else
  1680. s.hero = nextAllowedHero(s.castle*HEROES_PER_TYPE*2-1,s.hero,1,dir);
  1681. }
  1682. if(old != s.hero)
  1683. {
  1684. usedHeroes.erase(old);
  1685. usedHeroes.insert(s.hero);
  1686. redraw();
  1687. }
  1688. SEL->propagateOptions();
  1689. }
  1690. int OptionsTab::nextAllowedHero( int min, int max, int incl, int dir )
  1691. {
  1692. if(dir>0)
  1693. {
  1694. for(int i=min+incl; i<=max-incl; i++)
  1695. if(canUseThisHero(i))
  1696. return i;
  1697. }
  1698. else
  1699. {
  1700. for(int i=max-incl; i>=min+incl; i--)
  1701. if(canUseThisHero(i))
  1702. return i;
  1703. }
  1704. return -1;
  1705. }
  1706. bool OptionsTab::canUseThisHero( int ID )
  1707. {
  1708. //for(int i=0;i<CPG->ret.playerInfos.size();i++)
  1709. // if(CPG->ret.playerInfos[i].hero==ID) //hero is already taken
  1710. // return false;
  1711. return !vstd::contains(usedHeroes, ID) && SEL->current->mapHeader->allowedHeroes[ID];
  1712. }
  1713. void OptionsTab::nextBonus( int player, int dir )
  1714. {
  1715. if(SEL->isGuest())
  1716. {
  1717. SEL->postRequest(RequestOptionsChange::BONUS, dir);
  1718. return;
  1719. }
  1720. PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1721. si8 &ret = s.bonus += dir;
  1722. if (s.hero==-2 && !SEL->current->mapHeader->players[s.color].heroesNames.size() && ret==PlayerSettings::bartifact) //no hero - can't be artifact
  1723. {
  1724. if (dir<0)
  1725. ret=PlayerSettings::brandom;
  1726. else ret=PlayerSettings::bgold;
  1727. }
  1728. if(ret > PlayerSettings::bresource)
  1729. ret = PlayerSettings::brandom;
  1730. if(ret < PlayerSettings::brandom)
  1731. ret = PlayerSettings::bresource;
  1732. if (s.castle==-1 && ret==PlayerSettings::bresource) //random castle - can't be resource
  1733. {
  1734. if (dir<0)
  1735. ret=PlayerSettings::bgold;
  1736. else ret=PlayerSettings::brandom;
  1737. }
  1738. SEL->propagateOptions();
  1739. redraw();
  1740. }
  1741. void OptionsTab::recreate()
  1742. {
  1743. bool wasActive = active;
  1744. if(active)
  1745. deactivate();
  1746. for(std::map<int, PlayerOptionsEntry*>::iterator it = entries.begin(); it != entries.end(); ++it)
  1747. {
  1748. children -= it->second;
  1749. delete it->second;
  1750. }
  1751. entries.clear();
  1752. usedHeroes.clear();
  1753. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1754. for(std::map<int, PlayerSettings>::iterator it = SEL->sInfo.playerInfos.begin();
  1755. it != SEL->sInfo.playerInfos.end(); ++it)
  1756. {
  1757. entries.insert(std::make_pair(it->first, new PlayerOptionsEntry(this, it->second)));
  1758. const std::vector<SheroName> &heroes = SEL->current->mapHeader->players[it->first].heroesNames;
  1759. for(size_t hi=0; hi<heroes.size(); hi++)
  1760. usedHeroes.insert(heroes[hi].heroID);
  1761. }
  1762. if(wasActive)
  1763. activate();
  1764. }
  1765. void OptionsTab::setTurnLength( int npos )
  1766. {
  1767. static const int times[] = {1, 2, 4, 6, 8, 10, 15, 20, 25, 30, 0};
  1768. amin(npos, ARRAY_COUNT(times) - 1);
  1769. SEL->sInfo.turnTime = times[npos];
  1770. redraw();
  1771. }
  1772. void OptionsTab::flagPressed( int color )
  1773. {
  1774. PlayerSettings &clicked = SEL->sInfo.playerInfos[color];
  1775. PlayerSettings *old = NULL;
  1776. if(SEL->playerNames.size() == 1) //single player -> just swap
  1777. {
  1778. if(color == playerColor) //that color is already selected, no action needed
  1779. return;
  1780. old = &SEL->sInfo.playerInfos[playerColor];
  1781. swapPlayers(*old, clicked);
  1782. }
  1783. else
  1784. {
  1785. //identify clicked player
  1786. int clickedNameID = clicked.human; //human is a number of player, zero means AI
  1787. if(clickedNameID > 0 && playerToRestore.id == clickedNameID) //player to restore is about to being replaced -> put him back to the old place
  1788. {
  1789. PlayerSettings &restPos = SEL->sInfo.playerInfos[playerToRestore.color];
  1790. SEL->setPlayer(restPos, playerToRestore.id);
  1791. playerToRestore.reset();
  1792. }
  1793. int newPlayer; //which player will take clicked position
  1794. //who will be put here?
  1795. if(!clickedNameID) //AI player clicked -> if possible replace computer with unallocated player
  1796. {
  1797. newPlayer = SEL->getIdOfFirstUnallocatedPlayer();
  1798. if(!newPlayer) //no "free" player -> get just first one
  1799. newPlayer = SEL->playerNames.begin()->first;
  1800. }
  1801. else //human clicked -> take next
  1802. {
  1803. std::map<ui32, std::string>::const_iterator i = SEL->playerNames.find(clickedNameID); //clicked one
  1804. i++; //player AFTER clicked one
  1805. if(i != SEL->playerNames.end())
  1806. newPlayer = i->first;
  1807. else
  1808. newPlayer = 0; //AI if we scrolled through all players
  1809. }
  1810. SEL->setPlayer(clicked, newPlayer); //put player
  1811. //if that player was somewhere else, we need to replace him with computer
  1812. if(newPlayer) //not AI
  1813. {
  1814. for(std::map<int, PlayerSettings>::iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  1815. {
  1816. int curNameID = i->second.human;
  1817. if(i->first != color && curNameID == newPlayer)
  1818. {
  1819. assert(i->second.human);
  1820. playerToRestore.color = i->first;
  1821. playerToRestore.id = newPlayer;
  1822. SEL->setPlayer(i->second, 0); //set computer
  1823. old = &i->second;
  1824. break;
  1825. }
  1826. }
  1827. }
  1828. }
  1829. entries[clicked.color]->selectButtons();
  1830. if(old)
  1831. {
  1832. entries[old->color]->selectButtons();
  1833. old->hero = entries[old->color]->pi.defaultHero();
  1834. }
  1835. SEL->propagateOptions();
  1836. GH.totalRedraw();
  1837. }
  1838. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry( OptionsTab *owner, PlayerSettings &S)
  1839. : pi(SEL->current->mapHeader->players[S.color]), s(S)
  1840. {
  1841. OBJ_CONSTRUCTION;
  1842. defActions |= SHARE_POS;
  1843. int serial = 0;
  1844. for(int g=0; g < s.color; ++g)
  1845. {
  1846. PlayerInfo &itred = SEL->current->mapHeader->players[g];
  1847. if( itred.canComputerPlay || itred.canHumanPlay)
  1848. serial++;
  1849. }
  1850. pos = parent->pos + Point(54, 122 + serial*50);
  1851. static const char *flags[] = {"AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  1852. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"};
  1853. static const char *bgs[] = {"ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  1854. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"};
  1855. bg = new CPicture(BitmapHandler::loadBitmap(bgs[s.color]), 0, 0, true);
  1856. if(SEL->screenType == CMenuScreen::newGame)
  1857. {
  1858. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[132], bind(&OptionsTab::nextCastle, owner, s.color, -1), 107, 5, "ADOPLFA.DEF");
  1859. btns[1] = new AdventureMapButton(CGI->generaltexth->zelp[133], bind(&OptionsTab::nextCastle, owner, s.color, +1), 168, 5, "ADOPRTA.DEF");
  1860. btns[2] = new AdventureMapButton(CGI->generaltexth->zelp[148], bind(&OptionsTab::nextHero, owner, s.color, -1), 183, 5, "ADOPLFA.DEF");
  1861. btns[3] = new AdventureMapButton(CGI->generaltexth->zelp[149], bind(&OptionsTab::nextHero, owner, s.color, +1), 244, 5, "ADOPRTA.DEF");
  1862. btns[4] = new AdventureMapButton(CGI->generaltexth->zelp[164], bind(&OptionsTab::nextBonus, owner, s.color, -1), 259, 5, "ADOPLFA.DEF");
  1863. btns[5] = new AdventureMapButton(CGI->generaltexth->zelp[165], bind(&OptionsTab::nextBonus, owner, s.color, +1), 320, 5, "ADOPRTA.DEF");
  1864. }
  1865. else
  1866. for(int i = 0; i < 6; i++)
  1867. btns[i] = NULL;
  1868. selectButtons();
  1869. assert(SEL->current && SEL->current->mapHeader);
  1870. const PlayerInfo &p = SEL->current->mapHeader->players[s.color];
  1871. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  1872. if(p.canHumanPlay && p.canComputerPlay)
  1873. whoCanPlay = HUMAN_OR_CPU;
  1874. else if(p.canComputerPlay)
  1875. whoCanPlay = CPU;
  1876. else
  1877. whoCanPlay = HUMAN;
  1878. if(SEL->screenType != CMenuScreen::scenarioInfo
  1879. && SEL->current->mapHeader->players[s.color].canHumanPlay
  1880. && SEL->multiPlayer != CMenuScreen::MULTI_NETWORK_GUEST)
  1881. {
  1882. flag = new AdventureMapButton(CGI->generaltexth->zelp[180], bind(&OptionsTab::flagPressed, owner, s.color), -43, 2, flags[s.color]);
  1883. flag->hoverable = true;
  1884. }
  1885. else
  1886. flag = NULL;
  1887. defActions &= ~SHARE_POS;
  1888. town = new SelectedBox(TOWN, s.color);
  1889. town->pos += pos + Point(119, 2);
  1890. hero = new SelectedBox(HERO, s.color);
  1891. hero->pos += pos + Point(195, 2);
  1892. bonus = new SelectedBox(BONUS, s.color);
  1893. bonus->pos += pos + Point(271, 2);
  1894. }
  1895. void OptionsTab::PlayerOptionsEntry::showAll( SDL_Surface * to )
  1896. {
  1897. CIntObject::showAll(to);
  1898. printAtMiddleLoc(s.name, 55, 10, FONT_SMALL, zwykly, to);
  1899. printAtMiddleWBLoc(CGI->generaltexth->arraytxt[206+whoCanPlay], 28, 34, FONT_TINY, 8, zwykly, to);
  1900. }
  1901. void OptionsTab::PlayerOptionsEntry::selectButtons()
  1902. {
  1903. if(!btns[0])
  1904. return;
  1905. if( (pi.defaultCastle() != -1) //fixed tow
  1906. || (SEL->isGuest() && s.color != playerColor)) //or not our player
  1907. {
  1908. btns[0]->disable();
  1909. btns[1]->disable();
  1910. }
  1911. else
  1912. {
  1913. btns[0]->enable(active);
  1914. btns[1]->enable(active);
  1915. }
  1916. if( (pi.defaultHero() != -1 || !s.human || s.castle < 0) //fixed hero
  1917. || (SEL->isGuest() && s.color != playerColor))//or not our player
  1918. {
  1919. btns[2]->disable();
  1920. btns[3]->disable();
  1921. }
  1922. else
  1923. {
  1924. btns[2]->enable(active);
  1925. btns[3]->enable(active);
  1926. }
  1927. if(SEL->isGuest() && s.color != playerColor)//or not our player
  1928. {
  1929. btns[4]->disable();
  1930. btns[5]->disable();
  1931. }
  1932. else
  1933. {
  1934. btns[4]->enable(active);
  1935. btns[5]->enable(active);
  1936. }
  1937. }
  1938. void OptionsTab::SelectedBox::showAll( SDL_Surface * to )
  1939. {
  1940. //PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1941. SDL_Surface *toBlit = getImg();
  1942. const std::string *toPrint = getText();
  1943. blitAt(toBlit, pos, to);
  1944. printAtMiddleLoc(*toPrint, 23, 39, FONT_TINY, zwykly, to);
  1945. }
  1946. OptionsTab::SelectedBox::SelectedBox( SelType Which, ui8 Player )
  1947. :which(Which), player(Player)
  1948. {
  1949. SDL_Surface *img = getImg();
  1950. pos.w = img->w;
  1951. pos.h = img->h;
  1952. used = RCLICK;
  1953. }
  1954. SDL_Surface * OptionsTab::SelectedBox::getImg() const
  1955. {
  1956. const PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1957. switch(which)
  1958. {
  1959. case TOWN:
  1960. if (s.castle < F_NUMBER && s.castle >= 0)
  1961. return graphics->getPic(s.castle, true, false);
  1962. else if (s.castle == -1)
  1963. return CGP->rTown;
  1964. else if (s.castle == -2)
  1965. return CGP->nTown;
  1966. case HERO:
  1967. if (s.hero == -1)
  1968. {
  1969. return CGP->rHero;
  1970. }
  1971. else if (s.hero == -2)
  1972. {
  1973. if(s.heroPortrait >= 0)
  1974. return graphics->portraitSmall[s.heroPortrait];
  1975. else
  1976. return CGP->nHero;
  1977. }
  1978. else
  1979. {
  1980. return graphics->portraitSmall[s.hero];
  1981. }
  1982. break;
  1983. case BONUS:
  1984. {
  1985. int pom;
  1986. switch (s.bonus)
  1987. {
  1988. case -1:
  1989. pom=10;
  1990. break;
  1991. case 0:
  1992. pom=9;
  1993. break;
  1994. case 1:
  1995. pom=8;
  1996. break;
  1997. case 2:
  1998. pom=CGI->townh->towns[s.castle].bonus;
  1999. break;
  2000. default:
  2001. assert(0);
  2002. }
  2003. return CGP->bonuses->ourImages[pom].bitmap;
  2004. }
  2005. default:
  2006. return NULL;
  2007. }
  2008. }
  2009. const std::string * OptionsTab::SelectedBox::getText() const
  2010. {
  2011. const PlayerSettings &s = SEL->sInfo.playerInfos[player];
  2012. switch(which)
  2013. {
  2014. case TOWN:
  2015. if (s.castle < F_NUMBER && s.castle >= 0)
  2016. return &CGI->townh->towns[s.castle].Name();
  2017. else if (s.castle == -1)
  2018. return &CGI->generaltexth->allTexts[522];
  2019. else if (s.castle == -2)
  2020. return &CGI->generaltexth->allTexts[523];
  2021. case HERO:
  2022. if (s.hero == -1)
  2023. return &CGI->generaltexth->allTexts[522];
  2024. else if (s.hero == -2)
  2025. {
  2026. if(s.heroPortrait >= 0)
  2027. {
  2028. if(s.heroName.length())
  2029. return &s.heroName;
  2030. else
  2031. return &CGI->heroh->heroes[s.heroPortrait]->name;
  2032. }
  2033. else
  2034. return &CGI->generaltexth->allTexts[523];
  2035. }
  2036. else
  2037. {
  2038. //if(s.heroName.length())
  2039. // return &s.heroName;
  2040. //else
  2041. return &CGI->heroh->heroes[s.hero]->name;
  2042. }
  2043. case BONUS:
  2044. switch (s.bonus)
  2045. {
  2046. case -1:
  2047. return &CGI->generaltexth->allTexts[522];
  2048. default:
  2049. return &CGI->generaltexth->arraytxt[214 + s.bonus];
  2050. }
  2051. default:
  2052. return NULL;
  2053. }
  2054. }
  2055. void OptionsTab::SelectedBox::clickRight( tribool down, bool previousState )
  2056. {
  2057. if(indeterminate(down) || !down) return;
  2058. const PlayerSettings &s = SEL->sInfo.playerInfos[player];
  2059. SDL_Surface *bmp = NULL;
  2060. const std::string *title = NULL, *subTitle = NULL;
  2061. subTitle = getText();
  2062. int val=-1;
  2063. switch(which)
  2064. {
  2065. case TOWN:
  2066. val = s.castle;
  2067. break;
  2068. case HERO:
  2069. val = s.hero;
  2070. if(val == -2) //none => we may have some preset info
  2071. {
  2072. int p9 = SEL->current->mapHeader->players[s.color].p9;
  2073. if(p9 != 255 && SEL->sInfo.playerInfos[player].heroPortrait >= 0)
  2074. val = p9;
  2075. }
  2076. break;
  2077. case BONUS:
  2078. val = s.bonus;
  2079. break;
  2080. }
  2081. if(val == -1 || which == BONUS) //random or bonus box
  2082. {
  2083. bmp = CMessage::drawBox1(256, 190);
  2084. std::string *description = NULL;
  2085. switch(which)
  2086. {
  2087. case TOWN:
  2088. title = &CGI->generaltexth->allTexts[103];
  2089. description = &CGI->generaltexth->allTexts[104];
  2090. break;
  2091. case HERO:
  2092. title = &CGI->generaltexth->allTexts[101];
  2093. description = &CGI->generaltexth->allTexts[102];
  2094. break;
  2095. case BONUS:
  2096. {
  2097. switch(val)
  2098. {
  2099. case PlayerSettings::brandom:
  2100. title = &CGI->generaltexth->allTexts[86]; //{Random Bonus}
  2101. description = &CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  2102. break;
  2103. case PlayerSettings::bartifact:
  2104. title = &CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  2105. description = &CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  2106. break;
  2107. case PlayerSettings::bgold:
  2108. title = &CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  2109. subTitle = &CGI->generaltexth->allTexts[87]; //500-1000
  2110. description = &CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  2111. break;
  2112. case PlayerSettings::bresource:
  2113. {
  2114. title = &CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  2115. switch(CGI->townh->towns[s.castle].primaryRes)
  2116. {
  2117. case 1:
  2118. subTitle = &CGI->generaltexth->allTexts[694];
  2119. description = &CGI->generaltexth->allTexts[690];
  2120. break;
  2121. case 3:
  2122. subTitle = &CGI->generaltexth->allTexts[695];
  2123. description = &CGI->generaltexth->allTexts[691];
  2124. break;
  2125. case 4:
  2126. subTitle = &CGI->generaltexth->allTexts[692];
  2127. description = &CGI->generaltexth->allTexts[688];
  2128. break;
  2129. case 5:
  2130. subTitle = &CGI->generaltexth->allTexts[693];
  2131. description = &CGI->generaltexth->allTexts[689];
  2132. break;
  2133. case 127:
  2134. subTitle = &CGI->generaltexth->allTexts[89]; //5-10 wood / 5-10 ore
  2135. 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
  2136. break;
  2137. }
  2138. }
  2139. break;
  2140. }
  2141. }
  2142. break;
  2143. }
  2144. if(description)
  2145. CSDL_Ext::printAtMiddleWB(*description, 125, 145, FONT_SMALL, 37, zwykly, bmp);
  2146. }
  2147. else if(val == -2)
  2148. {
  2149. return;
  2150. }
  2151. else if(which == TOWN)
  2152. {
  2153. bmp = CMessage::drawBox1(256, 319);
  2154. title = &CGI->generaltexth->allTexts[80];
  2155. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[79], 135, 137, FONT_MEDIUM, tytulowy, bmp);
  2156. const CTown &t = CGI->townh->towns[val];
  2157. //print creatures
  2158. int x = 60, y = 159;
  2159. for(int i = 0; i < 7; i++)
  2160. {
  2161. int c = t.basicCreatures[i];
  2162. blitAt(graphics->smallImgs[c], x, y, bmp);
  2163. CSDL_Ext::printAtMiddleWB(CGI->creh->creatures[c]->nameSing, x + 16, y + 45, FONT_TINY, 10, zwykly, bmp);
  2164. if(i == 2)
  2165. {
  2166. x = 40;
  2167. y += 76;
  2168. }
  2169. else
  2170. {
  2171. x += 52;
  2172. }
  2173. }
  2174. }
  2175. else if(val >= 0)
  2176. {
  2177. const CHero *h = CGI->heroh->heroes[val];
  2178. bmp = CMessage::drawBox1(320, 255);
  2179. title = &CGI->generaltexth->allTexts[77];
  2180. CSDL_Ext::printAtMiddle(*title, 167, 36, FONT_MEDIUM, tytulowy, bmp);
  2181. CSDL_Ext::printAtMiddle(*subTitle + " - " + h->heroClass->name, 160, 99, FONT_SMALL, zwykly, bmp);
  2182. blitAt(getImg(), 136, 56, bmp);
  2183. //print specialty
  2184. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[78], 166, 132, FONT_MEDIUM, tytulowy, bmp);
  2185. blitAt(graphics->un44->ourImages[val].bitmap, 140, 150, bmp);
  2186. CSDL_Ext::printAtMiddle(CGI->generaltexth->hTxts[val].bonusName, 166, 203, FONT_SMALL, zwykly, bmp);
  2187. GH.pushInt(new CInfoPopup(bmp, true));
  2188. return;
  2189. }
  2190. if(title)
  2191. CSDL_Ext::printAtMiddle(*title, 135, 36, FONT_MEDIUM, tytulowy, bmp);
  2192. if(subTitle)
  2193. CSDL_Ext::printAtMiddle(*subTitle, 127, 103, FONT_SMALL, zwykly, bmp);
  2194. blitAt(getImg(), 104, 60, bmp);
  2195. GH.pushInt(new CInfoPopup(bmp, true));
  2196. }
  2197. CScenarioInfo::CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo)
  2198. {
  2199. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2200. for(std::map<int, PlayerSettings>::const_iterator it = startInfo->playerInfos.begin();
  2201. it != startInfo->playerInfos.end(); ++it)
  2202. {
  2203. if(it->second.human)
  2204. {
  2205. playerColor = it->first;
  2206. }
  2207. }
  2208. pos.w = 762;
  2209. pos.h = 584;
  2210. center(pos);
  2211. assert(LOCPLINT);
  2212. sInfo = *LOCPLINT->cb->getStartInfo();
  2213. assert(!SEL->current);
  2214. current = mapInfoFromGame();
  2215. setPlayersFromGame();
  2216. screenType = CMenuScreen::scenarioInfo;
  2217. card = new InfoCard();
  2218. opt = new OptionsTab();
  2219. opt->recreate();
  2220. card->difficulty->select(startInfo->difficulty, 0);
  2221. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 584, 535, "SCNRBACK.DEF", SDLK_ESCAPE);
  2222. }
  2223. CScenarioInfo::~CScenarioInfo()
  2224. {
  2225. delete current;
  2226. }
  2227. bool mapSorter::operator()(const CMapInfo *aaa, const CMapInfo *bbb)
  2228. {
  2229. const CMapHeader * a = aaa->mapHeader,
  2230. * b = bbb->mapHeader;
  2231. if(a && b) //if we are sorting scenarios
  2232. {
  2233. switch (sortBy)
  2234. {
  2235. case _format: //by map format (RoE, WoG, etc)
  2236. return (a->version<b->version);
  2237. break;
  2238. case _loscon: //by loss conditions
  2239. return (a->lossCondition.typeOfLossCon<b->lossCondition.typeOfLossCon);
  2240. break;
  2241. case _playerAm: //by player amount
  2242. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  2243. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  2244. for (int i=0;i<8;i++)
  2245. {
  2246. if (a->players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  2247. else if (a->players[i].canComputerPlay) {playerAmntA++;}
  2248. if (b->players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  2249. else if (b->players[i].canComputerPlay) {playerAmntB++;}
  2250. }
  2251. if (playerAmntB!=playerAmntA)
  2252. return (playerAmntA<playerAmntB);
  2253. else
  2254. return (humenPlayersA<humenPlayersB);
  2255. break;
  2256. case _size: //by size of map
  2257. return (a->width<b->width);
  2258. break;
  2259. case _viccon: //by victory conditions
  2260. return (a->victoryCondition.condition < b->victoryCondition.condition);
  2261. break;
  2262. case _name: //by name
  2263. return (a->name<b->name);
  2264. break;
  2265. default:
  2266. return (a->name<b->name);
  2267. break;
  2268. }
  2269. }
  2270. else //if we are sorting campaigns
  2271. {
  2272. switch(sortBy)
  2273. {
  2274. case _numOfMaps: //by number of maps in campaign
  2275. return CGI->generaltexth->campaignRegionNames[ aaa->campaignHeader->mapVersion ].size() <
  2276. CGI->generaltexth->campaignRegionNames[ bbb->campaignHeader->mapVersion ].size();
  2277. break;
  2278. case _name: //by name
  2279. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  2280. break;
  2281. default:
  2282. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  2283. break;
  2284. }
  2285. }
  2286. }
  2287. CMultiMode::CMultiMode()
  2288. {
  2289. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2290. bg = new CPicture("MUPOPUP.bmp");
  2291. bg->convertToScreenBPP(); //so we could draw without problems
  2292. blitAt(CPicture("MUMAP.bmp"), 16, 77, *bg); //blit img
  2293. pos = bg->center(); //center, window has size of bg graphic
  2294. bar = new CGStatusBar(new CPicture(Rect(7, 465, 440, 18), 0));//226, 472
  2295. txt = new CTextInput(Rect(19, 436, 334, 16), *bg);
  2296. txt->setText(GDefaultOptions.playerName); //Player
  2297. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[266], bind(&CMultiMode::openHotseat, this), 373, 78, "MUBHOT.DEF");
  2298. btns[1] = new AdventureMapButton("Host TCP/IP game", "", bind(&CMultiMode::hostTCP, this), 373, 78 + 57*1, "MUBHOST.DEF");
  2299. btns[2] = new AdventureMapButton("Join TCP/IP game", "", bind(&CMultiMode::joinTCP, this), 373, 78 + 57*2, "MUBJOIN.DEF");
  2300. btns[6] = new AdventureMapButton(CGI->generaltexth->zelp[288], bind(&CGuiHandler::popIntTotally, ref(GH), this), 373, 424, "MUBCANC.DEF", SDLK_ESCAPE);
  2301. }
  2302. void CMultiMode::openHotseat()
  2303. {
  2304. GH.pushInt(new CHotSeatPlayers(txt->text));
  2305. }
  2306. void CMultiMode::hostTCP()
  2307. {
  2308. GDefaultOptions.setPlayerName(txt->text);
  2309. GH.popIntTotally(this);
  2310. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, CMenuScreen::MULTI_NETWORK_HOST));
  2311. }
  2312. void CMultiMode::joinTCP()
  2313. {
  2314. GDefaultOptions.setPlayerName(txt->text);
  2315. GH.popIntTotally(this);
  2316. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, CMenuScreen::MULTI_NETWORK_GUEST));
  2317. }
  2318. CHotSeatPlayers::CHotSeatPlayers(const std::string &firstPlayer)
  2319. {
  2320. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2321. bg = new CPicture("MUHOTSEA.bmp");
  2322. bg->convertToScreenBPP(); //so we could draw without problems
  2323. bg->printAtMiddleWBLoc(CGI->generaltexth->allTexts[446], 185, 55, FONT_BIG, 50, zwykly, *bg); //HOTSEAT Please enter names
  2324. pos = bg->center(); //center, window has size of bg graphic
  2325. for(int i = 0; i < ARRAY_COUNT(txt); i++)
  2326. txt[i] = new CTextInput(Rect(60, 85 + i*30, 280, 16), *bg);
  2327. txt[0]->setText(firstPlayer);
  2328. txt[0]->giveFocus();
  2329. ok = new AdventureMapButton(CGI->generaltexth->zelp[560], bind(&CHotSeatPlayers::enterSelectionScreen, this), 95, 338, "MUBCHCK.DEF", SDLK_RETURN);
  2330. cancel = new AdventureMapButton(CGI->generaltexth->zelp[561], bind(&CGuiHandler::popIntTotally, ref(GH), this), 205, 338, "MUBCANC.DEF", SDLK_ESCAPE);
  2331. bar = new CGStatusBar(new CPicture(Rect(7, 381, 348, 18), 0));//226, 472
  2332. }
  2333. void CHotSeatPlayers::enterSelectionScreen()
  2334. {
  2335. std::map<ui32, std::string> names;
  2336. for(int i = 0, j = 1; i < ARRAY_COUNT(txt); i++)
  2337. if(txt[i]->text.length())
  2338. names[j++] = txt[i]->text;
  2339. assert(names.size() > 1); //two players at least - after all, it's hot seat mode...
  2340. GDefaultOptions.setPlayerName(names.begin()->second); //remember selected name
  2341. GH.popInts(2); //pop MP mode window and this
  2342. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, CMenuScreen::MULTI_HOT_SEAT, &names));
  2343. }
  2344. CBonusSelection::CBonusSelection( CCampaignState * _ourCampaign )
  2345. : highlightedRegion(NULL), ourCampaign(_ourCampaign), ourHeader(NULL),
  2346. diffLb(NULL), diffRb(NULL), bonuses(NULL)
  2347. {
  2348. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2349. static const std::string bgNames [] = {"E1_BG.BMP", "G2_BG.BMP", "E2_BG.BMP", "G1_BG.BMP", "G3_BG.BMP", "N1_BG.BMP",
  2350. "S1_BG.BMP", "BR_BG.BMP", "IS_BG.BMP", "KR_BG.BMP", "NI_BG.BMP", "TA_BG.BMP", "AR_BG.BMP", "HS_BG.BMP",
  2351. "BB_BG.BMP", "NB_BG.BMP", "EL_BG.BMP", "RN_BG.BMP", "UA_BG.BMP", "SP_BG.BMP"};
  2352. loadPositionsOfGraphics();
  2353. background = BitmapHandler::loadBitmap(bgNames[ourCampaign->camp->header.mapVersion]);
  2354. SDL_Surface * panel = BitmapHandler::loadBitmap("CAMPBRF.BMP");
  2355. blitAt(panel, 456, 6, background);
  2356. startB = new AdventureMapButton("", "", bind(&CBonusSelection::startMap, this), 475, 536, "CBBEGIB.DEF", SDLK_RETURN);
  2357. backB = new AdventureMapButton("", "", bind(&CBonusSelection::goBack, this), 624, 536, "CBCANCB.DEF", SDLK_ESCAPE);
  2358. startB->setState(CButtonBase::BLOCKED);
  2359. //campaign name
  2360. if (ourCampaign->camp->header.name.length())
  2361. printAtLoc(ourCampaign->camp->header.name, 481, 28, FONT_BIG, tytulowy, background);
  2362. else
  2363. printAtLoc("Unnamed", 481, 28, FONT_BIG, tytulowy, background);
  2364. //map size icon
  2365. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  2366. //campaign description
  2367. printAtLoc(CGI->generaltexth->allTexts[38], 481, 63, FONT_SMALL, tytulowy, background);
  2368. cmpgDesc = new CTextBox(ourCampaign->camp->header.description, Rect(480, 86, 286, 117), 1);
  2369. cmpgDesc->showAll(background);
  2370. //map description
  2371. mapDesc = new CTextBox("", Rect(480, 280, 286, 117), 1);
  2372. //bonus choosing
  2373. printAtLoc(CGI->generaltexth->allTexts[71], 511, 432, FONT_MEDIUM, zwykly, background); //Choose a bonus:
  2374. bonuses = new CHighlightableButtonsGroup(bind(&CBonusSelection::selectBonus, this, _1));
  2375. //set left part of window
  2376. for (int g=0; g<ourCampaign->camp->scenarios.size(); ++g)
  2377. {
  2378. if(ourCampaign->camp->conquerable(g))
  2379. {
  2380. regions.push_back(new CRegion(this, true, true, g));
  2381. regions[regions.size()-1]->rclickText = ourCampaign->camp->scenarios[g].regionText;
  2382. if (highlightedRegion == NULL)
  2383. {
  2384. highlightedRegion = regions.back();
  2385. selectMap(g);
  2386. }
  2387. }
  2388. else if (ourCampaign->camp->scenarios[g].conquered) //display as striped
  2389. {
  2390. regions.push_back(new CRegion(this, false, false, g));
  2391. regions[regions.size()-1]->rclickText = ourCampaign->camp->scenarios[g].regionText;
  2392. }
  2393. }
  2394. //init campaign state if necessary
  2395. if (ourCampaign->campaignName.size() == 0)
  2396. {
  2397. ourCampaign->initNewCampaign(sInfo);
  2398. }
  2399. //allies / enemies
  2400. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 486, 407, FONT_SMALL, zwykly, background); //Allies
  2401. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 619, 407, FONT_SMALL, zwykly, background); //Enemies
  2402. SDL_FreeSurface(panel);
  2403. //difficulty
  2404. std::vector<std::string> difficulty;
  2405. boost::split(difficulty, CGI->generaltexth->allTexts[492], boost::is_any_of(" "));
  2406. printAtLoc(difficulty.back(), 689, 432, FONT_MEDIUM, zwykly, background); //Difficulty
  2407. //difficulty pics
  2408. for (int b=0; b<ARRAY_COUNT(diffPics); ++b)
  2409. {
  2410. CDefEssential * cde = CDefHandler::giveDefEss("GSPBUT" + boost::lexical_cast<std::string>(b+3) + ".DEF");
  2411. SDL_Surface * surfToDuplicate = cde->ourImages[0].bitmap;
  2412. diffPics[b] = SDL_ConvertSurface(surfToDuplicate, surfToDuplicate->format,
  2413. surfToDuplicate->flags);
  2414. delete cde;
  2415. }
  2416. //difficulty selection buttons
  2417. if (ourCampaign->camp->header.difficultyChoosenByPlayer)
  2418. {
  2419. diffLb = new AdventureMapButton("", "", bind(&CBonusSelection::changeDiff, this, false), 694, 508, "SCNRBLF.DEF");
  2420. diffRb = new AdventureMapButton("", "", bind(&CBonusSelection::changeDiff, this, true), 738, 508, "SCNRBRT.DEF");
  2421. }
  2422. //load miniflags
  2423. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  2424. }
  2425. CBonusSelection::~CBonusSelection()
  2426. {
  2427. SDL_FreeSurface(background);
  2428. delete sizes;
  2429. delete ourHeader;
  2430. delete sFlags;
  2431. for (int b=0; b<ARRAY_COUNT(diffPics); ++b)
  2432. {
  2433. SDL_FreeSurface(diffPics[b]);
  2434. }
  2435. }
  2436. void CBonusSelection::goBack()
  2437. {
  2438. GH.popIntTotally(this);
  2439. }
  2440. void CBonusSelection::showAll( SDL_Surface * to )
  2441. {
  2442. blitAt(background, pos.x, pos.y, to);
  2443. CIntObject::showAll(to);
  2444. show(to);
  2445. }
  2446. void CBonusSelection::loadPositionsOfGraphics()
  2447. {
  2448. const JsonNode config(DATA_DIR "/config/campaign_regions.json");
  2449. int idx = 0;
  2450. BOOST_FOREACH(const JsonNode &campaign, config["campaign_regions"].Vector())
  2451. {
  2452. SCampPositions sc;
  2453. sc.campPrefix = campaign["prefix"].String();
  2454. sc.colorSuffixLength = campaign["color_suffix_length"].Float();
  2455. BOOST_FOREACH(const JsonNode &desc, campaign["desc"].Vector())
  2456. {
  2457. SCampPositions::SRegionDesc rd;
  2458. rd.infix = desc["infix"].String();
  2459. rd.xpos = desc["x"].Float();
  2460. rd.ypos = desc["y"].Float();
  2461. sc.regions.push_back(rd);
  2462. }
  2463. campDescriptions.push_back(sc);
  2464. idx++;
  2465. }
  2466. assert(idx == CGI->generaltexth->campaignMapNames.size());
  2467. }
  2468. void CBonusSelection::selectMap( int whichOne )
  2469. {
  2470. sInfo.difficulty = ourCampaign->camp->scenarios[whichOne].difficulty;
  2471. sInfo.mapname = ourCampaign->camp->header.filename;
  2472. sInfo.mode = StartInfo::CAMPAIGN;
  2473. //get header
  2474. int i = 0;
  2475. delete ourHeader;
  2476. ourHeader = new CMapHeader();
  2477. ourHeader->initFromMemory((const unsigned char*)ourCampaign->camp->mapPieces.find(whichOne)->second.c_str(), i);
  2478. std::map<ui32, std::string> names;
  2479. names[1] = GDefaultOptions.playerName;
  2480. updateStartInfo(ourCampaign->camp->header.filename, sInfo, ourHeader, names);
  2481. sInfo.turnTime = 0;
  2482. sInfo.whichMapInCampaign = whichOne;
  2483. sInfo.difficulty = ourCampaign->camp->scenarios[whichOne].difficulty;
  2484. ourCampaign->currentMap = whichOne;
  2485. mapDesc->setTxt(ourHeader->description);
  2486. updateBonusSelection();
  2487. }
  2488. void CBonusSelection::show( SDL_Surface * to )
  2489. {
  2490. //blitAt(background, pos.x, pos.y, to);
  2491. //map name
  2492. std::string mapName = ourHeader->name;
  2493. if (mapName.length())
  2494. printAtLoc(mapName, 481, 219, FONT_BIG, tytulowy, to);
  2495. else
  2496. printAtLoc("Unnamed", 481, 219, FONT_BIG, tytulowy, to);
  2497. //map description
  2498. printAtLoc(CGI->generaltexth->allTexts[496], 481, 253, FONT_SMALL, tytulowy, to);
  2499. mapDesc->showAll(to); //showAll because CTextBox has no show()
  2500. //map size icon
  2501. int temp;
  2502. switch (ourHeader->width)
  2503. {
  2504. case 36:
  2505. temp=0;
  2506. break;
  2507. case 72:
  2508. temp=1;
  2509. break;
  2510. case 108:
  2511. temp=2;
  2512. break;
  2513. case 144:
  2514. temp=3;
  2515. break;
  2516. default:
  2517. temp=4;
  2518. break;
  2519. }
  2520. blitAtLoc(sizes->ourImages[temp].bitmap, 735, 26, to);
  2521. //flags
  2522. int fx = 496 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[390].c_str());
  2523. int ex = 629 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[391].c_str());
  2524. int myT;
  2525. myT = ourHeader->players[playerColor].team;
  2526. for (std::map<int, PlayerSettings>::const_iterator i = sInfo.playerInfos.begin(); i != sInfo.playerInfos.end(); i++)
  2527. {
  2528. int *myx = ((i->first == playerColor || ourHeader->players[i->first].team == myT) ? &fx : &ex);
  2529. blitAtLoc(sFlags->ourImages[i->first].bitmap, *myx, 405, to);
  2530. *myx += sFlags->ourImages[i->first].bitmap->w;
  2531. }
  2532. //difficulty
  2533. blitAt(diffPics[sInfo.difficulty], 709, 455, to);
  2534. CIntObject::show(to);
  2535. }
  2536. void CBonusSelection::updateBonusSelection()
  2537. {
  2538. //graphics:
  2539. //spell - SPELLBON.DEF
  2540. //monster - TWCRPORT.DEF
  2541. //building - BO*.BMP graphics
  2542. //artifact - ARTIFBON.DEF
  2543. //spell scroll - SPELLBON.DEF
  2544. //prim skill - PSKILBON.DEF
  2545. //sec skill - SSKILBON.DEF
  2546. //resource - BORES.DEF
  2547. //player - CREST58.DEF
  2548. //hero - PORTRAITSLARGE (HPL###.BMPs)
  2549. const CCampaignScenario &scenario = ourCampaign->camp->scenarios[sInfo.whichMapInCampaign];
  2550. const std::vector<CScenarioTravel::STravelBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  2551. for (size_t i=0; i<bonuses->buttons.size(); i++)
  2552. {
  2553. if (bonuses->buttons[i]->active)
  2554. bonuses->buttons[i]->deactivate();
  2555. bonuses->delChild(bonuses->buttons[i]);
  2556. }
  2557. bonuses->buttons.clear();
  2558. static const char *bonusPics[] = {"SPELLBON.DEF", "TWCRPORT.DEF", "", "ARTIFBON.DEF", "SPELLBON.DEF",
  2559. "PSKILBON.DEF", "SSKILBON.DEF", "BORES.DEF", "CREST58.DEF", "PORTRAITSLARGE"};
  2560. for(int i = 0; i < bonDescs.size(); i++)
  2561. {
  2562. std::string picName=bonusPics[bonDescs[i].type];
  2563. size_t picNumber=bonDescs[i].info2;
  2564. std::string desc;
  2565. switch(bonDescs[i].type)
  2566. {
  2567. case 0: //spell
  2568. desc = CGI->generaltexth->allTexts[715];
  2569. boost::algorithm::replace_first(desc, "%s", CGI->spellh->spells[bonDescs[i].info2]->name);
  2570. break;
  2571. case 1: //monster
  2572. picNumber = bonDescs[i].info2 + 2;
  2573. desc = CGI->generaltexth->allTexts[717];
  2574. boost::algorithm::replace_first(desc, "%d", boost::lexical_cast<std::string>(bonDescs[i].info3));
  2575. boost::algorithm::replace_first(desc, "%s", CGI->creh->creatures[bonDescs[i].info2]->namePl);
  2576. break;
  2577. case 2: //building
  2578. {
  2579. int faction = -1;
  2580. for(std::map<int, PlayerSettings>::iterator it = sInfo.playerInfos.begin();
  2581. it != sInfo.playerInfos.end(); ++it)
  2582. {
  2583. if (it->second.human)
  2584. {
  2585. faction = it->second.castle;
  2586. break;
  2587. }
  2588. }
  2589. assert(faction != -1);
  2590. int buildID = CBuildingHandler::campToERMU(bonDescs[i].info1, faction, std::set<si32>());
  2591. picName = graphics->ERMUtoPicture[faction][buildID];
  2592. picNumber = -1;
  2593. tlog1<<CGI->buildh->buildings.size()<<"\t"<<faction<<"\t"<<CGI->buildh->buildings[faction].size()<<"\t"<<buildID<<"\n";
  2594. if (vstd::contains(CGI->buildh->buildings[faction], buildID))
  2595. desc = CGI->buildh->buildings[faction].find(buildID)->second->Description();
  2596. }
  2597. break;
  2598. case 3: //artifact
  2599. desc = CGI->generaltexth->allTexts[715];
  2600. boost::algorithm::replace_first(desc, "%s", CGI->arth->artifacts[bonDescs[i].info2]->Name());
  2601. break;
  2602. case 4: //spell scroll
  2603. desc = CGI->generaltexth->allTexts[716];
  2604. boost::algorithm::replace_first(desc, "%s", CGI->spellh->spells[bonDescs[i].info2]->name);
  2605. break;
  2606. case 5: //primary skill
  2607. {
  2608. int leadingSkill = -1;
  2609. std::vector<std::pair<int, int> > toPrint; //primary skills to be listed <num, val>
  2610. const ui8* ptr = reinterpret_cast<const ui8*>(&bonDescs[i].info2);
  2611. for (int g=0; g<PRIMARY_SKILLS; ++g)
  2612. {
  2613. if (leadingSkill == -1 || ptr[g] > ptr[leadingSkill])
  2614. {
  2615. leadingSkill = g;
  2616. }
  2617. if (ptr[g] != 0)
  2618. {
  2619. toPrint.push_back(std::make_pair(g, ptr[g]));
  2620. }
  2621. }
  2622. picNumber = leadingSkill;
  2623. desc = CGI->generaltexth->allTexts[715];
  2624. std::string substitute; //text to be printed instead of %s
  2625. for (int v=0; v<toPrint.size(); ++v)
  2626. {
  2627. substitute += boost::lexical_cast<std::string>(toPrint[v].second);
  2628. substitute += " " + CGI->generaltexth->primarySkillNames[toPrint[v].first];
  2629. if(v != toPrint.size() - 1)
  2630. {
  2631. substitute += ", ";
  2632. }
  2633. }
  2634. boost::algorithm::replace_first(desc, "%s", substitute);
  2635. break;
  2636. }
  2637. case 6: //secondary skill
  2638. desc = CGI->generaltexth->allTexts[718];
  2639. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->levels[bonDescs[i].info3]); //skill level
  2640. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->skillName[bonDescs[i].info2]); //skill name
  2641. break;
  2642. case 7: //resource
  2643. {
  2644. int serialResID = 0;
  2645. switch(bonDescs[i].info1)
  2646. {
  2647. case 0: case 1: case 2: case 3: case 4: case 5: case 6:
  2648. serialResID = bonDescs[i].info1;
  2649. break;
  2650. case 0xFD: //wood + ore
  2651. serialResID = 7;
  2652. break;
  2653. case 0xFE: //rare resources
  2654. serialResID = 8;
  2655. break;
  2656. }
  2657. picNumber = serialResID;
  2658. desc = CGI->generaltexth->allTexts[717];
  2659. boost::algorithm::replace_first(desc, "%d", boost::lexical_cast<std::string>(bonDescs[i].info2));
  2660. std::string replacement;
  2661. if (serialResID <= 6)
  2662. {
  2663. replacement = CGI->generaltexth->restypes[serialResID];
  2664. }
  2665. else
  2666. {
  2667. replacement = CGI->generaltexth->allTexts[714 + serialResID];
  2668. }
  2669. boost::algorithm::replace_first(desc, "%s", replacement);
  2670. }
  2671. break;
  2672. case 8: //player aka hero crossover
  2673. picNumber = bonDescs[i].info1;
  2674. desc = CGI->generaltexth->allTexts[718];
  2675. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->capColors[bonDescs[i].info1]); //player color
  2676. boost::algorithm::replace_first(desc, "%s", ourCampaign->camp->scenarios[bonDescs[i].info2].mapName); //scenario
  2677. break;
  2678. case 9: //hero
  2679. desc = CGI->generaltexth->allTexts[718];
  2680. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->capColors[bonDescs[i].info1]); //hero's color
  2681. if (bonDescs[i].info2 == 0xFFFF)
  2682. {
  2683. boost::algorithm::replace_first(desc, "%s", CGI->heroh->heroes[0]->name); //hero's name
  2684. picNumber = 0;
  2685. }
  2686. else
  2687. {
  2688. boost::algorithm::replace_first(desc, "%s", CGI->heroh->heroes[bonDescs[i].info2]->name); //hero's name
  2689. }
  2690. break;
  2691. }
  2692. CHighlightableButton *bonusButton = new CHighlightableButton(desc, desc, 0, 475 + i*68, 455, "", i);
  2693. if (picNumber != -1)
  2694. picName += ":" + boost::lexical_cast<std::string>(picNumber);
  2695. CAnimation * anim = new CAnimation();
  2696. anim->setCustom(picName, 0);
  2697. bonusButton->setImage(anim);
  2698. bonusButton->borderColor = Colors::Yellow; // yellow border
  2699. bonuses->addButton(bonusButton);
  2700. }
  2701. if (active)
  2702. for (size_t i=0; i<bonuses->buttons.size(); i++)
  2703. bonuses->buttons[i]->activate();
  2704. }
  2705. void CBonusSelection::startMap()
  2706. {
  2707. StartInfo *si = new StartInfo(sInfo);
  2708. if (ourCampaign->mapsConquered.size())
  2709. {
  2710. GH.popInts(1);
  2711. }
  2712. else
  2713. {
  2714. // Deletes either the Custom campaign selection screen + Bonus selection screen or
  2715. // one of the main campaign selection screens + Bonus selection screen and
  2716. // deactivates the main menu
  2717. GH.popInts(2);
  2718. GH.popInt(GH.topInt());
  2719. }
  2720. ::startGame(si);
  2721. }
  2722. void CBonusSelection::selectBonus( int id )
  2723. {
  2724. // Total redraw is needed because the border around the bonus images
  2725. // have to be undrawn/drawn.
  2726. if (id != sInfo.choosenCampaignBonus)
  2727. {
  2728. sInfo.choosenCampaignBonus = id;
  2729. GH.totalRedraw();
  2730. if (startB->getState() == CButtonBase::BLOCKED)
  2731. startB->setState(CButtonBase::NORMAL);
  2732. }
  2733. const CCampaignScenario &scenario = ourCampaign->camp->scenarios[sInfo.whichMapInCampaign];
  2734. const std::vector<CScenarioTravel::STravelBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  2735. if (bonDescs[id].type == 8) //hero crossover
  2736. {
  2737. SEL->setPlayer(sInfo.playerInfos[0], bonDescs[id].info1); //TODO: substitute with appropriate color
  2738. }
  2739. }
  2740. void CBonusSelection::changeDiff( bool increase )
  2741. {
  2742. if (increase)
  2743. {
  2744. sInfo.difficulty = std::min(sInfo.difficulty + 1, 4);
  2745. }
  2746. else
  2747. {
  2748. sInfo.difficulty = std::max(sInfo.difficulty - 1, 0);
  2749. }
  2750. }
  2751. CBonusSelection::CRegion::CRegion( CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber )
  2752. : owner(_owner), accessible(_accessible), selectable(_selectable), myNumber(_myNumber)
  2753. {
  2754. OBJ_CONSTRUCTION;
  2755. used = LCLICK | RCLICK;
  2756. static const std::string colors[2][8] = {
  2757. {"R", "B", "N", "G", "O", "V", "T", "P"},
  2758. {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"}};
  2759. const SCampPositions & campDsc = owner->campDescriptions[owner->ourCampaign->camp->header.mapVersion];
  2760. const SCampPositions::SRegionDesc & desc = campDsc.regions[myNumber];
  2761. pos.x = desc.xpos;
  2762. pos.y = desc.ypos;
  2763. //loading of graphics
  2764. std::string prefix = campDsc.campPrefix + desc.infix + "_";
  2765. std::string suffix = colors[campDsc.colorSuffixLength - 1][owner->ourCampaign->camp->scenarios[myNumber].regionColor];
  2766. static const std::string infix [] = {"En", "Se", "Co"};
  2767. for (int g = 0; g < ARRAY_COUNT(infix); g++)
  2768. {
  2769. graphics[g] = BitmapHandler::loadBitmap(prefix + infix[g] + suffix + ".BMP");
  2770. }
  2771. pos.w = graphics[0]->w;
  2772. pos.h = graphics[0]->h;
  2773. }
  2774. CBonusSelection::CRegion::~CRegion()
  2775. {
  2776. for (int g=0; g<ARRAY_COUNT(graphics); ++g)
  2777. {
  2778. SDL_FreeSurface(graphics[g]);
  2779. }
  2780. }
  2781. void CBonusSelection::CRegion::clickLeft( tribool down, bool previousState )
  2782. {
  2783. //select if selectable & clicked inside our graphic
  2784. if ( indeterminate(down) )
  2785. {
  2786. return;
  2787. }
  2788. if( !down && selectable && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) )
  2789. {
  2790. owner->selectMap(myNumber);
  2791. owner->highlightedRegion = this;
  2792. parent->showAll(screen);
  2793. }
  2794. }
  2795. void CBonusSelection::CRegion::clickRight( tribool down, bool previousState )
  2796. {
  2797. //show r-click text
  2798. if( down && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) &&
  2799. rclickText.size() )
  2800. {
  2801. CRClickPopup::createAndPush(rclickText);
  2802. }
  2803. }
  2804. void CBonusSelection::CRegion::show( SDL_Surface * to )
  2805. {
  2806. //const SCampPositions::SRegionDesc & desc = owner->campDescriptions[owner->ourCampaign->camp->header.mapVersion].regions[myNumber];
  2807. if (!accessible)
  2808. {
  2809. //show as striped
  2810. blitAt(graphics[2], pos.x, pos.y, to);
  2811. }
  2812. else if (this == owner->highlightedRegion)
  2813. {
  2814. //show as selected
  2815. blitAt(graphics[1], pos.x, pos.y, to);
  2816. }
  2817. else
  2818. {
  2819. //show as not selected selected
  2820. blitAt(graphics[0], pos.x, pos.y, to);
  2821. }
  2822. }
  2823. CSavingScreen::CSavingScreen(bool hotseat)
  2824. : CSelectionScreen(CMenuScreen::saveGame, hotseat ? CMenuScreen::MULTI_HOT_SEAT : CMenuScreen::SINGLE_PLAYER)
  2825. {
  2826. ourGame = mapInfoFromGame();
  2827. sInfo = *LOCPLINT->cb->getStartInfo();
  2828. setPlayersFromGame();
  2829. }
  2830. CSavingScreen::~CSavingScreen()
  2831. {
  2832. }
  2833. ISelectionScreenInfo::ISelectionScreenInfo(const std::map<ui32, std::string> *Names /*= NULL*/)
  2834. {
  2835. multiPlayer = CMenuScreen::SINGLE_PLAYER;
  2836. assert(!SEL);
  2837. SEL = this;
  2838. current = NULL;
  2839. if(Names && Names->size()) //if have custom set of player names - use it
  2840. playerNames = *Names;
  2841. else
  2842. playerNames[1] = GDefaultOptions.playerName; //by default we have only one player and his name is "Player" (or whatever the last used name was)
  2843. }
  2844. ISelectionScreenInfo::~ISelectionScreenInfo()
  2845. {
  2846. assert(SEL == this);
  2847. SEL = NULL;
  2848. }
  2849. void ISelectionScreenInfo::updateStartInfo(std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader)
  2850. {
  2851. ::updateStartInfo(filename, sInfo, mapHeader, playerNames);
  2852. }
  2853. void ISelectionScreenInfo::setPlayer(PlayerSettings &pset, unsigned player)
  2854. {
  2855. ::setPlayer(pset, player, playerNames);
  2856. }
  2857. int ISelectionScreenInfo::getIdOfFirstUnallocatedPlayer()
  2858. {
  2859. for(std::map<ui32, std::string>::const_iterator i = playerNames.begin(); i != playerNames.end(); i++)
  2860. if(!sInfo.getPlayersSettings(i->first)) //
  2861. return i->first;
  2862. return 0;
  2863. }
  2864. bool ISelectionScreenInfo::isGuest() const
  2865. {
  2866. return multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST;
  2867. }
  2868. bool ISelectionScreenInfo::isHost() const
  2869. {
  2870. return multiPlayer == CMenuScreen::MULTI_NETWORK_HOST;
  2871. }
  2872. void ChatMessage::apply(CSelectionScreen *selScreen)
  2873. {
  2874. selScreen->card->chat->addNewMessage(playerName + ": " + message);
  2875. GH.totalRedraw();
  2876. }
  2877. void QuitMenuWithoutStarting::apply(CSelectionScreen *selScreen)
  2878. {
  2879. if(!selScreen->ongoingClosing)
  2880. {
  2881. *selScreen->serv << this; //resend to confirm
  2882. GH.popIntTotally(selScreen); //will wait with deleting us before this thread ends
  2883. }
  2884. delNull(selScreen->serv);
  2885. }
  2886. void PlayerJoined::apply(CSelectionScreen *selScreen)
  2887. {
  2888. //assert(SEL->playerNames.size() == connectionID); //temporary, TODO when player exits
  2889. SEL->playerNames[connectionID] = playerName;
  2890. //put new player in first slot with AI
  2891. for(std::map<int, PlayerSettings>::iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  2892. {
  2893. if(!i->second.human)
  2894. {
  2895. selScreen->setPlayer(i->second, connectionID);
  2896. selScreen->opt->entries[i->second.color]->selectButtons();
  2897. break;
  2898. }
  2899. }
  2900. selScreen->propagateNames();
  2901. selScreen->propagateOptions();
  2902. GH.totalRedraw();
  2903. }
  2904. void SelectMap::apply(CSelectionScreen *selScreen)
  2905. {
  2906. if(selScreen->multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  2907. {
  2908. free = false;
  2909. selScreen->changeSelection(mapInfo);
  2910. }
  2911. }
  2912. void UpdateStartOptions::apply(CSelectionScreen *selScreen)
  2913. {
  2914. if(!selScreen->isGuest())
  2915. return;
  2916. selScreen->setSInfo(*options);
  2917. }
  2918. void PregameGuiAction::apply(CSelectionScreen *selScreen)
  2919. {
  2920. if(!selScreen->isGuest())
  2921. return;
  2922. switch(action)
  2923. {
  2924. case NO_TAB:
  2925. selScreen->toggleTab(selScreen->curTab);
  2926. break;
  2927. case OPEN_OPTIONS:
  2928. selScreen->toggleTab(selScreen->opt);
  2929. break;
  2930. case OPEN_SCENARIO_LIST:
  2931. selScreen->toggleTab(selScreen->sel);
  2932. break;
  2933. }
  2934. }
  2935. void RequestOptionsChange::apply(CSelectionScreen *selScreen)
  2936. {
  2937. if(!selScreen->isHost())
  2938. return;
  2939. ui8 color = selScreen->sInfo.getPlayersSettings(playerID)->color;
  2940. switch(what)
  2941. {
  2942. case TOWN:
  2943. selScreen->opt->nextCastle(color, direction);
  2944. break;
  2945. case HERO:
  2946. selScreen->opt->nextHero(color, direction);
  2947. break;
  2948. case BONUS:
  2949. selScreen->opt->nextBonus(color, direction);
  2950. break;
  2951. }
  2952. }
  2953. void PlayerLeft::apply(CSelectionScreen *selScreen)
  2954. {
  2955. if(selScreen->isGuest())
  2956. return;
  2957. SEL->playerNames.erase(playerID);
  2958. if(PlayerSettings *s = selScreen->sInfo.getPlayersSettings(playerID)) //it's possible that player was unallocated
  2959. {
  2960. selScreen->setPlayer(*s, 0);
  2961. selScreen->opt->entries[s->color]->selectButtons();
  2962. }
  2963. selScreen->propagateNames();
  2964. selScreen->propagateOptions();
  2965. GH.totalRedraw();
  2966. }
  2967. void PlayersNames::apply(CSelectionScreen *selScreen)
  2968. {
  2969. if(selScreen->isGuest())
  2970. selScreen->playerNames = playerNames;
  2971. }
  2972. void StartWithCurrentSettings::apply(CSelectionScreen *selScreen)
  2973. {
  2974. startingInfo.reset();
  2975. startingInfo.serv = selScreen->serv;
  2976. startingInfo.sInfo = new StartInfo(selScreen->sInfo);
  2977. if(!selScreen->ongoingClosing)
  2978. {
  2979. *selScreen->serv << this; //resend to confirm
  2980. }
  2981. selScreen->serv = NULL; //hide it so it won't be deleted
  2982. delNull(selScreen->serverHandlingThread); //detach us
  2983. selectedName = selScreen->sInfo.mapname;
  2984. GH.curInt = NULL;
  2985. GH.popIntTotally(selScreen);
  2986. GH.popInt(GH.topInt()); //only deactivate main menu screen
  2987. ::startGame(startingInfo.sInfo, startingInfo.serv);
  2988. throw 666; //EVIL, EVIL, EVIL workaround to kill thread (does "goto catch" outside listening loop)
  2989. }
  2990. CCampaignScreen::CCampaignButton::CCampaignButton(const JsonNode &config )
  2991. {
  2992. pos.x += config["x"].Float();
  2993. pos.y += config["y"].Float();
  2994. pos.w = 200;
  2995. pos.h = 116;
  2996. campFile = config["file"].String();
  2997. video = config["video"].String();
  2998. //On linux we can only play *.mjpg videos from LOKI release
  2999. #ifdef _WIN32
  3000. std::transform(video.begin(), video.end(), video.begin(), toupper);
  3001. video += ".BIK";
  3002. #else
  3003. std::transform(video.begin(), video.end(), video.begin(), tolower);
  3004. video += ".mjpg";
  3005. #endif
  3006. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3007. status = config["open"].Bool() ? CCampaignScreen::ENABLED : CCampaignScreen::DISABLED;
  3008. CCampaignHeader header = CCampaignHandler::getHeader(campFile, true);
  3009. hoverText = header.name;
  3010. if (status != CCampaignScreen::DISABLED)
  3011. {
  3012. used |= LCLICK | HOVER;
  3013. image = new CPicture(config["image"].String());
  3014. hoverLabel = new CLabel(pos.w / 2, pos.h + 20, FONT_MEDIUM, CENTER, tytulowy, "");
  3015. moveChild(hoverLabel, this, parent);
  3016. }
  3017. if (status == CCampaignScreen::COMPLETED)
  3018. checkMark = new CPicture("CAMPCHK");
  3019. }
  3020. void CCampaignScreen::CCampaignButton::clickLeft(tribool down, bool previousState)
  3021. {
  3022. if (down)
  3023. {
  3024. // Close running video and open the selected campaign
  3025. CCS->videoh->close();
  3026. CCampaign * ourCampaign = CCampaignHandler::getCampaign(campFile, true);
  3027. CCampaignState * campState = new CCampaignState();
  3028. campState->camp = ourCampaign;
  3029. GH.pushInt( new CBonusSelection(campState) );
  3030. }
  3031. }
  3032. void CCampaignScreen::CCampaignButton::hover(bool on)
  3033. {
  3034. if (on)
  3035. hoverLabel->setTxt(hoverText); // Shows the name of the campaign when you get into the bounds of the button
  3036. else
  3037. hoverLabel->setTxt(" ");
  3038. }
  3039. void CCampaignScreen::CCampaignButton::show(SDL_Surface *to)
  3040. {
  3041. if (status == CCampaignScreen::DISABLED)
  3042. return;
  3043. CIntObject::show(to);
  3044. // Play the campaign button video when the mouse cursor is placed over the button
  3045. if (hovered)
  3046. {
  3047. if (CCS->videoh->fname != video)
  3048. CCS->videoh->open(video);
  3049. CCS->videoh->update(pos.x, pos.y, to, true, false); // plays sequentially frame by frame, starts at the beginning when the video is over
  3050. }
  3051. else if (CCS->videoh->fname == video) // When you got out of the bounds of the button then close the video
  3052. {
  3053. CCS->videoh->close();
  3054. redraw();
  3055. }
  3056. }
  3057. AdventureMapButton* CCampaignScreen::createExitButton(const JsonNode& button)
  3058. {
  3059. std::pair<std::string, std::string> help;
  3060. if (!button["help"].isNull() && button["help"].Float() > 0)
  3061. help = CGI->generaltexth->zelp[button["help"].Float()];
  3062. boost::function<void()> close = boost::bind(&CGuiHandler::popIntTotally, &GH, this);
  3063. return new AdventureMapButton(help, close, button["x"].Float(), button["y"].Float(), button["name"].String(), button["hotkey"].Float());
  3064. }
  3065. CCampaignScreen::CCampaignScreen(const JsonNode &config)
  3066. {
  3067. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3068. BOOST_FOREACH(const JsonNode& node, config["images"].Vector())
  3069. images.push_back(createPicture(node));
  3070. if (!config["exitbutton"].isNull())
  3071. {
  3072. back = createExitButton(config["exitbutton"]);
  3073. back->hoverable = true;
  3074. }
  3075. BOOST_FOREACH(const JsonNode& node, config["items"].Vector())
  3076. campButtons.push_back(new CCampaignButton(node));
  3077. }