CPreGame.cpp 101 KB

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