CPreGame.cpp 92 KB

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