CPreGame.cpp 92 KB

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