CPreGame.cpp 101 KB

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