CPreGame.cpp 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329
  1. #include "../stdafx.h"
  2. #include "CPreGame.h"
  3. #include <ctime>
  4. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  5. #include <boost/algorithm/string.hpp>
  6. #include <zlib.h>
  7. #include "../timeHandler.h"
  8. #include <sstream>
  9. #include "SDL_Extensions.h"
  10. #include "CGameInfo.h"
  11. #include "CCursorHandler.h"
  12. #include "CAnimation.h"
  13. #include "CDefHandler.h"
  14. #include "../lib/CDefObjInfoHandler.h"
  15. #include "../lib/CGeneralTextHandler.h"
  16. #include "../lib/CLodHandler.h"
  17. #include "../lib/CTownHandler.h"
  18. #include "../lib/CHeroHandler.h"
  19. #include "../lib/CObjectHandler.h"
  20. #include "../lib/CCampaignHandler.h"
  21. #include "../lib/CCreatureHandler.h"
  22. #include "CMusicHandler.h"
  23. #include "CVideoHandler.h"
  24. #include <cmath>
  25. #include "Graphics.h"
  26. //#include <boost/thread.hpp>
  27. #include <boost/bind.hpp>
  28. #include <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 "../lib/CSpellHandler.h" /*for campaign bonuses*/
  40. #include "../lib/CArtHandler.h" /*for campaign bonuses*/
  41. #include "../lib/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. #include "../CThreadHelper.h"
  48. /*
  49. * CPreGame.cpp, part of VCMI engine
  50. *
  51. * Authors: listed in file AUTHORS in main folder
  52. *
  53. * License: GNU General Public License v2.0 or later
  54. * Full text of license available in license.txt file, in main folder
  55. *
  56. */
  57. namespace fs = boost::filesystem;
  58. using boost::bind;
  59. using boost::ref;
  60. #if _MSC_VER >= 1600
  61. #define bind boost::bind
  62. #define ref boost::ref
  63. #endif
  64. void startGame(StartInfo * options, CConnection *serv = NULL);
  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;//FIXME: Memory leak in buttons? Images on them definitely were not freed after game start
  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. CCS->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. CCS->musich->playMusic(musicBase::mainMenu, -1);
  286. #ifdef _WIN32
  287. CCS->videoh->open("ACREDIT.SMK");
  288. #else
  289. CCS->videoh->open("ACREDIT.SMK", true, false);
  290. #endif
  291. GH.pushInt(scrs[CMenuScreen::mainMenu]);
  292. }
  293. if(SEL)
  294. SEL->update();
  295. CCS->curh->draw1();
  296. SDL_Flip(screen);
  297. CCS->curh->draw2();
  298. GH.topInt()->show(screen);
  299. GH.updateTime();
  300. GH.handleEvents();
  301. }
  302. CSelectionScreen::CSelectionScreen(CMenuScreen::EState Type, CMenuScreen::EMultiMode MultiPlayer /*= CMenuScreen::SINGLE_PLAYER*/, const std::map<ui32, std::string> *Names /*= NULL*/)
  303. : ISelectionScreenInfo(Names), serverHandlingThread(NULL), mx(new boost::recursive_mutex),
  304. serv(NULL), ongoingClosing(false), myNameID(255)
  305. {
  306. screenType = Type;
  307. multiPlayer = MultiPlayer;
  308. OBJ_CONSTRUCTION_CAPTURING_ALL;
  309. bool network = (MultiPlayer == CMenuScreen::MULTI_NETWORK_GUEST || MultiPlayer == CMenuScreen::MULTI_NETWORK_HOST);
  310. CServerHandler *sh = NULL;
  311. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST)
  312. {
  313. sh = new CServerHandler;
  314. sh->startServer();
  315. }
  316. IShowActivable::type = BLOCK_ADV_HOTKEYS;
  317. pos.w = 762;
  318. pos.h = 584;
  319. if(Type == CMenuScreen::saveGame)
  320. {
  321. center(pos);
  322. }
  323. else if(Type == CMenuScreen::campaignList)
  324. {
  325. bg = new CPicture(BitmapHandler::loadBitmap("CamCust.bmp"), 0, 0, true);
  326. pos.x = 3;
  327. pos.y = 6;
  328. }
  329. else
  330. {
  331. pos.x = 3;
  332. pos.y = 6;
  333. bg = new CPicture(BitmapHandler::loadBitmap(rand()%2 ? "ZPIC1000.bmp" : "ZPIC1001.bmp"), -3, -6, true);
  334. }
  335. CGP->loadGraphics();
  336. sInfo.difficulty = 1;
  337. current = NULL;
  338. sInfo.mode = (Type == CMenuScreen::newGame ? StartInfo::NEW_GAME : StartInfo::LOAD_GAME);
  339. sInfo.turnTime = 0;
  340. curTab = NULL;
  341. card = new InfoCard(network); //right info card
  342. if (screenType == CMenuScreen::campaignList)
  343. {
  344. opt = NULL;
  345. }
  346. else
  347. {
  348. opt = new OptionsTab(); //scenario options tab
  349. opt->recActions = DISPOSE;
  350. }
  351. sel = new SelectionTab(screenType, bind(&CSelectionScreen::changeSelection, this, _1), multiPlayer); //scenario selection tab
  352. sel->recActions = DISPOSE;
  353. switch(screenType)
  354. {
  355. case CMenuScreen::newGame:
  356. {
  357. card->difficulty->onChange = bind(&CSelectionScreen::difficultyChange, this, _1);
  358. card->difficulty->select(1, 0);
  359. AdventureMapButton *select = new AdventureMapButton(CGI->generaltexth->zelp[45], bind(&CSelectionScreen::toggleTab, this, sel), 411, 75, "GSPBUTT.DEF", SDLK_s);
  360. select->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL);
  361. AdventureMapButton *opts = new AdventureMapButton(CGI->generaltexth->zelp[46], bind(&CSelectionScreen::toggleTab, this, opt), 411, 503, "GSPBUTT.DEF", SDLK_a);
  362. opts->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL);
  363. AdventureMapButton *random = new AdventureMapButton(CGI->generaltexth->zelp[47], bind(&CSelectionScreen::toggleTab, this, sel), 411, 99, "GSPBUTT.DEF", SDLK_r);
  364. random->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL);
  365. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRBEG.DEF", SDLK_b);
  366. if(network)
  367. {
  368. AdventureMapButton *hideChat = new AdventureMapButton(CGI->generaltexth->zelp[48], bind(&InfoCard::toggleChat, card), 619, 75, "GSPBUT2.DEF", SDLK_h);
  369. hideChat->addTextOverlay(CGI->generaltexth->allTexts[531], FONT_SMALL);
  370. if(multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  371. {
  372. SDL_Color orange = {232, 184, 32, 0};
  373. select->text->color = opts->text->color = random->text->color = orange;
  374. select->block(true);
  375. opts->block(true);
  376. random->block(true);
  377. start->block(true);
  378. }
  379. }
  380. }
  381. break;
  382. case CMenuScreen::loadGame:
  383. sel->recActions = 255;
  384. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRLOD.DEF", SDLK_l);
  385. break;
  386. case CMenuScreen::saveGame:
  387. sel->recActions = 255;
  388. start = new AdventureMapButton("", CGI->generaltexth->zelp[103].second, bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRSAV.DEF");
  389. break;
  390. case CMenuScreen::campaignList:
  391. sel->recActions = 255;
  392. start = new AdventureMapButton(std::pair<std::string, std::string>(), bind(&CSelectionScreen::startCampaign, this), 411, 529, "SCNRLOD.DEF", SDLK_b);
  393. break;
  394. }
  395. start->assignedKeys.insert(SDLK_RETURN);
  396. std::string backName;
  397. if(Type == CMenuScreen::campaignList)
  398. {
  399. backName = "SCNRBACK.DEF";
  400. }
  401. else
  402. {
  403. backName = "SCNRBACK.DEF";
  404. }
  405. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 581, 529, backName, SDLK_ESCAPE);
  406. if(network)
  407. {
  408. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST)
  409. {
  410. assert(playerNames.size() == 1 && vstd::contains(playerNames, 1)); //TODO hot-seat/network combo
  411. serv = sh->connectToServer();
  412. *serv << (ui8) 4;
  413. myNameID = 1;
  414. }
  415. else
  416. {
  417. serv = CServerHandler::justConnectToServer();
  418. }
  419. *serv << playerNames.begin()->second;
  420. if(multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  421. {
  422. const CMapInfo *map;
  423. *serv >> myNameID >> map;
  424. serv->connectionID = myNameID;
  425. changeSelection(map);
  426. }
  427. else //host
  428. {
  429. if(current)
  430. {
  431. SelectMap sm(*current);
  432. *serv << &sm;
  433. UpdateStartOptions uso(sInfo);
  434. *serv << &uso;
  435. }
  436. }
  437. applier = new CApplier<CBaseForPGApply>;
  438. registerTypes4(*applier);
  439. serverHandlingThread = new boost::thread(&CSelectionScreen::handleConnection, this);
  440. }
  441. delete sh;
  442. }
  443. CSelectionScreen::~CSelectionScreen()
  444. {
  445. ongoingClosing = true;
  446. if(serv)
  447. {
  448. assert(serverHandlingThread);
  449. QuitMenuWithoutStarting qmws;
  450. *serv << &qmws;
  451. // while(!serverHandlingThread->timed_join(boost::posix_time::milliseconds(50)))
  452. // processPacks();
  453. serverHandlingThread->join();
  454. delete serverHandlingThread;
  455. }
  456. playerColor = -1;
  457. playerNames.clear();
  458. assert(!serv);
  459. delNull(applier);
  460. delete mx;
  461. }
  462. void CSelectionScreen::toggleTab(CIntObject *tab)
  463. {
  464. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST && serv)
  465. {
  466. PregameGuiAction pga;
  467. if(tab == curTab)
  468. pga.action = PregameGuiAction::NO_TAB;
  469. else if(tab == opt)
  470. pga.action = PregameGuiAction::OPEN_OPTIONS;
  471. else if(tab == sel)
  472. pga.action = PregameGuiAction::OPEN_SCENARIO_LIST;
  473. *serv << &pga;
  474. }
  475. if(curTab && curTab->active)
  476. {
  477. curTab->deactivate();
  478. curTab->recActions = DISPOSE;
  479. }
  480. if(curTab != tab)
  481. {
  482. tab->recActions = 255;
  483. tab->activate();
  484. curTab = tab;
  485. }
  486. else
  487. {
  488. curTab = NULL;
  489. };
  490. GH.totalRedraw();
  491. }
  492. void CSelectionScreen::changeSelection( const CMapInfo *to )
  493. {
  494. if(multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  495. {
  496. delNull(current);
  497. }
  498. current = to;
  499. if(to && screenType == CMenuScreen::loadGame)
  500. SEL->sInfo.difficulty = to->scenarioOpts->difficulty;
  501. if(screenType != CMenuScreen::campaignList && screenType != CMenuScreen::saveGame)
  502. {
  503. updateStartInfo(to ? to->filename : "", sInfo, to ? to->mapHeader : NULL);
  504. }
  505. card->changeSelection(to);
  506. if(screenType != CMenuScreen::campaignList)
  507. {
  508. opt->recreate();
  509. }
  510. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST && serv)
  511. {
  512. SelectMap sm(*to);
  513. *serv << &sm;
  514. UpdateStartOptions uso(sInfo);
  515. *serv << &uso;
  516. }
  517. }
  518. void CSelectionScreen::startCampaign()
  519. {
  520. CCampaign * ourCampaign = CCampaignHandler::getCampaign(SEL->current->filename, SEL->current->lodCmpgn);
  521. CCampaignState * campState = new CCampaignState();
  522. campState->camp = ourCampaign;
  523. GH.pushInt( new CBonusSelection(campState) );
  524. }
  525. void CSelectionScreen::startGame()
  526. {
  527. if(screenType == CMenuScreen::newGame)
  528. {
  529. //there must be at least one human player before game can be started
  530. std::map<int, PlayerSettings>::const_iterator i;
  531. for(i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  532. if(i->second.human)
  533. break;
  534. if(i == SEL->sInfo.playerInfos.end())
  535. {
  536. GH.pushInt(CInfoWindow::create(CGI->generaltexth->allTexts[530])); //You must position yourself prior to starting the game.
  537. return;
  538. }
  539. }
  540. if(multiPlayer == CMenuScreen::MULTI_NETWORK_HOST)
  541. {
  542. start->block(true);
  543. StartWithCurrentSettings swcs;
  544. *serv << &swcs;
  545. ongoingClosing = true;
  546. return;
  547. }
  548. if(screenType != CMenuScreen::saveGame)
  549. {
  550. if(!current)
  551. return;
  552. selectedName = sInfo.mapname;
  553. StartInfo *si = new StartInfo(sInfo);
  554. GH.popIntTotally(this); //delete me
  555. GH.popInt(GH.topInt()); //only deactivate main menu screen
  556. //SEL->current = NULL;
  557. //curOpts = NULL;
  558. ::startGame(si);
  559. }
  560. else
  561. {
  562. if(!(sel && sel->txt && sel->txt->text.size()))
  563. return;
  564. selectedName = GVCMIDirs.UserPath + "/Games/" + sel->txt->text + ".vlgm1";
  565. CFunctionList<void()> overWrite;
  566. overWrite += bind(&CCallback::save, LOCPLINT->cb, sel->txt->text);
  567. overWrite += bind(&CGuiHandler::popIntTotally, &GH, this);
  568. if(fs::exists(selectedName))
  569. {
  570. std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite?
  571. boost::algorithm::replace_first(hlp, "%s", sel->txt->text);
  572. LOCPLINT->showYesNoDialog(hlp, std::vector<SComponent*>(), overWrite, 0, false);
  573. }
  574. else
  575. overWrite();
  576. // LOCPLINT->cb->save(sel->txt->text);
  577. // GH.popIntTotally(this);
  578. }
  579. }
  580. void CSelectionScreen::difficultyChange( int to )
  581. {
  582. assert(screenType == CMenuScreen::newGame);
  583. sInfo.difficulty = to;
  584. propagateOptions();
  585. GH.totalRedraw();
  586. }
  587. void CSelectionScreen::handleConnection()
  588. {
  589. setThreadName(-1, "CSelectionScreen::handleConnection");
  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. for(int i = 0; i < 5; i++)
  1248. {
  1249. difficulty->addButton(new CHighlightableButton("", CGI->generaltexth->zelp[24+i].second, 0, 110 + i*32, 450, difButns[i], i));
  1250. }
  1251. }
  1252. if(SEL->screenType != CMenuScreen::newGame)
  1253. difficulty->block(true);
  1254. //description needs bg
  1255. moveChild(new CPicture(*bg, descriptionRect), this, mapDescription, true); //move subpicture bg to our description control (by default it's our (Infocard) child)
  1256. if(network)
  1257. {
  1258. playerListBg = new CPicture("CHATPLUG.bmp", 16, 276);
  1259. chat = new CChatBox(descriptionRect);
  1260. 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)
  1261. chatOn = true;
  1262. mapDescription->disable();
  1263. }
  1264. }
  1265. }
  1266. InfoCard::~InfoCard()
  1267. {
  1268. delete sizes;
  1269. delete sFlags;
  1270. }
  1271. void InfoCard::showAll( SDL_Surface * to )
  1272. {
  1273. CIntObject::showAll(to);
  1274. //blit texts
  1275. if(SEL->screenType != CMenuScreen::campaignList)
  1276. {
  1277. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 24, 400, FONT_SMALL, zwykly, to); //Allies
  1278. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 190, 400, FONT_SMALL, zwykly, to); //Enemies
  1279. printAtLoc(CGI->generaltexth->allTexts[494], 33, 430, FONT_SMALL, tytulowy, to);//"Map Diff:"
  1280. printAtLoc(CGI->generaltexth->allTexts[492] + ":", 133,430, FONT_SMALL, tytulowy, to); //player difficulty
  1281. printAtLoc(CGI->generaltexth->allTexts[218] + ":", 290,430, FONT_SMALL, tytulowy, to); //"Rating:"
  1282. printAtLoc(CGI->generaltexth->allTexts[495], 26, 22, FONT_SMALL, tytulowy, to); //Scenario Name:
  1283. if(!chatOn)
  1284. {
  1285. printAtLoc(CGI->generaltexth->allTexts[496], 26, 132, FONT_SMALL, tytulowy, to); //Scenario Description:
  1286. printAtLoc(CGI->generaltexth->allTexts[497], 26, 283, FONT_SMALL, tytulowy, to); //Victory Condition:
  1287. printAtLoc(CGI->generaltexth->allTexts[498], 26, 339, FONT_SMALL, tytulowy, to); //Loss Condition:
  1288. }
  1289. else //players list
  1290. {
  1291. std::map<ui32, std::string> playerNames = SEL->playerNames;
  1292. int playerSoFar = 0;
  1293. for (std::map<int, PlayerSettings>::const_iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  1294. {
  1295. if(i->second.human)
  1296. {
  1297. printAtLoc(i->second.name, 24, 285 + playerSoFar++ * graphics->fonts[FONT_SMALL]->height, FONT_SMALL, zwykly, to);
  1298. playerNames.erase(i->second.human);
  1299. }
  1300. }
  1301. playerSoFar = 0;
  1302. for (std::map<ui32, std::string>::const_iterator i = playerNames.begin(); i != playerNames.end(); i++)
  1303. {
  1304. printAtLoc(i->second, 193, 285 + playerSoFar++ * graphics->fonts[FONT_SMALL]->height, FONT_SMALL, zwykly, to);
  1305. }
  1306. }
  1307. }
  1308. if(SEL->current)
  1309. {
  1310. if(SEL->screenType != CMenuScreen::campaignList)
  1311. {
  1312. if(SEL->screenType != CMenuScreen::newGame)
  1313. {
  1314. for (int i = 0; i < difficulty->buttons.size(); i++)
  1315. {
  1316. //if(i == SEL->current->difficulty)
  1317. // difficulty->buttons[i]->state = 3;
  1318. //else
  1319. // difficulty->buttons[i]->state = 2;
  1320. difficulty->buttons[i]->showAll(to);
  1321. }
  1322. }
  1323. int temp = -1;
  1324. if(!chatOn)
  1325. {
  1326. //victory conditions
  1327. temp = SEL->current->mapHeader->victoryCondition.condition+1;
  1328. if (temp>20) temp=0;
  1329. std::string sss = CGI->generaltexth->victoryConditions[temp];
  1330. if (temp && SEL->current->mapHeader->victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  1331. printAtLoc(sss, 60, 307, FONT_SMALL, zwykly, to);
  1332. temp = SEL->current->mapHeader->victoryCondition.condition;
  1333. if (temp>12) temp=11;
  1334. blitAtLoc(CGP->victory->ourImages[temp].bitmap, 24, 302, to); //victory cond descr
  1335. //loss conditoins
  1336. temp = SEL->current->mapHeader->lossCondition.typeOfLossCon+1;
  1337. if (temp>20) temp=0;
  1338. sss = CGI->generaltexth->lossCondtions[temp];
  1339. printAtLoc(sss, 60, 366, FONT_SMALL, zwykly, to);
  1340. temp=SEL->current->mapHeader->lossCondition.typeOfLossCon;
  1341. if (temp>12) temp=3;
  1342. blitAtLoc(CGP->loss->ourImages[temp].bitmap, 24, 359, to); //loss cond
  1343. }
  1344. //difficulty
  1345. assert(SEL->current->mapHeader->difficulty <= 4);
  1346. std::string &diff = CGI->generaltexth->arraytxt[142 + SEL->current->mapHeader->difficulty];
  1347. printAtMiddleLoc(diff, 62, 472, FONT_SMALL, zwykly, to);
  1348. //selecting size icon
  1349. switch (SEL->current->mapHeader->width)
  1350. {
  1351. case 36:
  1352. temp=0;
  1353. break;
  1354. case 72:
  1355. temp=1;
  1356. break;
  1357. case 108:
  1358. temp=2;
  1359. break;
  1360. case 144:
  1361. temp=3;
  1362. break;
  1363. default:
  1364. temp=4;
  1365. break;
  1366. }
  1367. blitAtLoc(sizes->ourImages[temp].bitmap, 318, 22, to);
  1368. if(SEL->screenType == CMenuScreen::loadGame)
  1369. printToLoc((static_cast<const CMapInfo*>(SEL->current))->date,308,34, FONT_SMALL, zwykly, to);
  1370. //print flags
  1371. int fx = 34 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[390].c_str());
  1372. int ex = 200 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[391].c_str());
  1373. int myT;
  1374. if(playerColor >= 0)
  1375. myT = SEL->current->mapHeader->players[playerColor].team;
  1376. else
  1377. myT = -1;
  1378. for (std::map<int, PlayerSettings>::const_iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  1379. {
  1380. int *myx = ((i->first == playerColor || SEL->current->mapHeader->players[i->first].team == myT) ? &fx : &ex);
  1381. blitAtLoc(sFlags->ourImages[i->first].bitmap, *myx, 399, to);
  1382. *myx += sFlags->ourImages[i->first].bitmap->w;
  1383. }
  1384. std::string tob;
  1385. switch (SEL->sInfo.difficulty)
  1386. {
  1387. case 0:
  1388. tob="80%";
  1389. break;
  1390. case 1:
  1391. tob="100%";
  1392. break;
  1393. case 2:
  1394. tob="130%";
  1395. break;
  1396. case 3:
  1397. tob="160%";
  1398. break;
  1399. case 4:
  1400. tob="200%";
  1401. break;
  1402. }
  1403. printAtMiddleLoc(tob, 311, 472, FONT_SMALL, zwykly, to);
  1404. }
  1405. //blit description
  1406. std::string name;
  1407. if (SEL->screenType == CMenuScreen::campaignList)
  1408. {
  1409. name = SEL->current->campaignHeader->name;
  1410. }
  1411. else
  1412. {
  1413. name = SEL->current->mapHeader->name;
  1414. }
  1415. //name
  1416. if (name.length())
  1417. printAtLoc(name, 26, 39, FONT_BIG, tytulowy, to);
  1418. else
  1419. printAtLoc("Unnamed", 26, 39, FONT_BIG, tytulowy, to);
  1420. }
  1421. }
  1422. void InfoCard::changeSelection( const CMapInfo *to )
  1423. {
  1424. if(to && mapDescription)
  1425. {
  1426. if (SEL->screenType == CMenuScreen::campaignList)
  1427. mapDescription->setTxt(to->campaignHeader->description);
  1428. else
  1429. mapDescription->setTxt(to->mapHeader->description);
  1430. if(SEL->screenType != CMenuScreen::newGame && SEL->screenType != CMenuScreen::campaignList)
  1431. difficulty->select(SEL->sInfo.difficulty, 0);
  1432. }
  1433. GH.totalRedraw();
  1434. }
  1435. void InfoCard::clickRight( tribool down, bool previousState )
  1436. {
  1437. static const Rect flagArea(19, 397, 335, 23);
  1438. if(down && SEL->current && isItInLoc(flagArea, GH.current->motion.x, GH.current->motion.y))
  1439. showTeamsPopup();
  1440. }
  1441. void InfoCard::showTeamsPopup()
  1442. {
  1443. SDL_Surface *bmp = CMessage::drawBox1(256, 90 + 50 * SEL->current->mapHeader->howManyTeams);
  1444. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[657], 128, 30, FONT_MEDIUM, tytulowy, bmp); //{Team Alignments}
  1445. for(int i = 0; i < SEL->current->mapHeader->howManyTeams; i++)
  1446. {
  1447. std::vector<ui8> flags;
  1448. std::string hlp = CGI->generaltexth->allTexts[656]; //Team %d
  1449. hlp.replace(hlp.find("%d"), 2, boost::lexical_cast<std::string>(i+1));
  1450. CSDL_Ext::printAtMiddle(hlp, 128, 65 + 50*i, FONT_SMALL, zwykly, bmp);
  1451. for(int j = 0; j < PLAYER_LIMIT; j++)
  1452. if((SEL->current->mapHeader->players[j].canHumanPlay || SEL->current->mapHeader->players[j].canComputerPlay)
  1453. && SEL->current->mapHeader->players[j].team == i)
  1454. flags.push_back(j);
  1455. int curx = 128 - 9*flags.size();
  1456. for(int j = 0; j < flags.size(); j++)
  1457. {
  1458. blitAt(sFlags->ourImages[flags[j]].bitmap, curx, 75 + 50*i, bmp);
  1459. curx += 18;
  1460. }
  1461. }
  1462. GH.pushInt(new CInfoPopup(bmp, true));
  1463. }
  1464. void InfoCard::toggleChat()
  1465. {
  1466. setChat(!chatOn);
  1467. }
  1468. void InfoCard::setChat(bool activateChat)
  1469. {
  1470. if(chatOn == activateChat)
  1471. return;
  1472. assert(active);
  1473. if(activateChat)
  1474. {
  1475. mapDescription->disable();
  1476. chat->enable();
  1477. playerListBg->enable();
  1478. }
  1479. else
  1480. {
  1481. mapDescription->enable();
  1482. chat->disable();
  1483. playerListBg->disable();
  1484. }
  1485. chatOn = activateChat;
  1486. GH.totalRedraw();
  1487. }
  1488. OptionsTab::OptionsTab():
  1489. turnDuration(NULL)
  1490. {
  1491. OBJ_CONSTRUCTION;
  1492. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTBK.bmp"), 0, 0, true);
  1493. pos = bg->pos;
  1494. if(SEL->screenType == CMenuScreen::newGame)
  1495. turnDuration = new CSlider(55, 551, 194, bind(&OptionsTab::setTurnLength, this, _1), 1, 11, 11, true, 1);
  1496. }
  1497. OptionsTab::~OptionsTab()
  1498. {
  1499. }
  1500. void OptionsTab::showAll( SDL_Surface * to )
  1501. {
  1502. CIntObject::showAll(to);
  1503. printAtMiddleLoc(CGI->generaltexth->allTexts[515], 222, 30, FONT_BIG, tytulowy, to);
  1504. printAtMiddleWBLoc(CGI->generaltexth->allTexts[516], 222, 58, FONT_SMALL, 55, zwykly, to); //Select starting options, handicap, and name for each player in the game.
  1505. printAtMiddleWBLoc(CGI->generaltexth->allTexts[517], 107, 102, FONT_SMALL, 14, tytulowy, to); //Player Name Handicap Type
  1506. printAtMiddleWBLoc(CGI->generaltexth->allTexts[518], 197, 102, FONT_SMALL, 10, tytulowy, to); //Starting Town
  1507. printAtMiddleWBLoc(CGI->generaltexth->allTexts[519], 273, 102, FONT_SMALL, 10, tytulowy, to); //Starting Hero
  1508. printAtMiddleWBLoc(CGI->generaltexth->allTexts[520], 349, 102, FONT_SMALL, 10, tytulowy, to); //Starting Bonus
  1509. printAtMiddleLoc(CGI->generaltexth->allTexts[521], 222, 538, FONT_SMALL, tytulowy, to); // Player Turn Duration
  1510. if (turnDuration)
  1511. printAtMiddleLoc(CGI->generaltexth->turnDurations[turnDuration->value], 319,559, FONT_SMALL, zwykly, to);//Turn duration value
  1512. }
  1513. void OptionsTab::nextCastle( int player, int dir )
  1514. {
  1515. if(SEL->isGuest())
  1516. {
  1517. SEL->postRequest(RequestOptionsChange::TOWN, dir);
  1518. return;
  1519. }
  1520. PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1521. si32 &cur = s.castle;
  1522. ui32 allowed = SEL->current->mapHeader->players[s.color].allowedFactions;
  1523. if (cur == -2) //no castle - no change
  1524. return;
  1525. if (cur == -1) //random => first/last available
  1526. {
  1527. int pom = (dir>0) ? (0) : (F_NUMBER-1); // last or first
  1528. for (;pom >= 0 && pom < F_NUMBER; pom+=dir)
  1529. {
  1530. if((1 << pom) & allowed)
  1531. {
  1532. cur=pom;
  1533. break;
  1534. }
  1535. }
  1536. }
  1537. else // next/previous available
  1538. {
  1539. for (;;)
  1540. {
  1541. cur+=dir;
  1542. if ((1 << cur) & allowed)
  1543. break;
  1544. if (cur >= F_NUMBER || cur<0)
  1545. {
  1546. double p1 = log((double)allowed) / log(2.0f)+0.000001f;
  1547. double check = p1 - ((int)p1);
  1548. if (check < 0.001)
  1549. cur = (int)p1;
  1550. else
  1551. cur = -1;
  1552. break;
  1553. }
  1554. }
  1555. }
  1556. if(s.hero >= 0)
  1557. s.hero = -1;
  1558. if(cur < 0 && s.bonus == PlayerSettings::bresource)
  1559. s.bonus = PlayerSettings::brandom;
  1560. entries[player]->selectButtons();
  1561. SEL->propagateOptions();
  1562. redraw();
  1563. }
  1564. void OptionsTab::nextHero( int player, int dir )
  1565. {
  1566. if(SEL->isGuest())
  1567. {
  1568. SEL->postRequest(RequestOptionsChange::HERO, dir);
  1569. return;
  1570. }
  1571. PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1572. int old = s.hero;
  1573. if (s.castle < 0 || !s.human || s.hero == -2)
  1574. return;
  1575. if (s.hero == -1) //random => first/last available
  1576. {
  1577. int max = (s.castle*HEROES_PER_TYPE*2+15),
  1578. min = (s.castle*HEROES_PER_TYPE*2);
  1579. s.hero = nextAllowedHero(min,max,0,dir);
  1580. }
  1581. else
  1582. {
  1583. if(dir > 0)
  1584. s.hero = nextAllowedHero(s.hero,(s.castle*HEROES_PER_TYPE*2+16),1,dir);
  1585. else
  1586. s.hero = nextAllowedHero(s.castle*HEROES_PER_TYPE*2-1,s.hero,1,dir);
  1587. }
  1588. if(old != s.hero)
  1589. {
  1590. usedHeroes.erase(old);
  1591. usedHeroes.insert(s.hero);
  1592. redraw();
  1593. }
  1594. SEL->propagateOptions();
  1595. }
  1596. int OptionsTab::nextAllowedHero( int min, int max, int incl, int dir )
  1597. {
  1598. if(dir>0)
  1599. {
  1600. for(int i=min+incl; i<=max-incl; i++)
  1601. if(canUseThisHero(i))
  1602. return i;
  1603. }
  1604. else
  1605. {
  1606. for(int i=max-incl; i>=min+incl; i--)
  1607. if(canUseThisHero(i))
  1608. return i;
  1609. }
  1610. return -1;
  1611. }
  1612. bool OptionsTab::canUseThisHero( int ID )
  1613. {
  1614. //for(int i=0;i<CPG->ret.playerInfos.size();i++)
  1615. // if(CPG->ret.playerInfos[i].hero==ID) //hero is already taken
  1616. // return false;
  1617. return !vstd::contains(usedHeroes, ID) && SEL->current->mapHeader->allowedHeroes[ID];
  1618. }
  1619. void OptionsTab::nextBonus( int player, int dir )
  1620. {
  1621. if(SEL->isGuest())
  1622. {
  1623. SEL->postRequest(RequestOptionsChange::BONUS, dir);
  1624. return;
  1625. }
  1626. PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1627. si8 &ret = s.bonus += dir;
  1628. if (s.hero==-2 && !SEL->current->mapHeader->players[s.color].heroesNames.size() && ret==PlayerSettings::bartifact) //no hero - can't be artifact
  1629. {
  1630. if (dir<0)
  1631. ret=PlayerSettings::brandom;
  1632. else ret=PlayerSettings::bgold;
  1633. }
  1634. if(ret > PlayerSettings::bresource)
  1635. ret = PlayerSettings::brandom;
  1636. if(ret < PlayerSettings::brandom)
  1637. ret = PlayerSettings::bresource;
  1638. if (s.castle==-1 && ret==PlayerSettings::bresource) //random castle - can't be resource
  1639. {
  1640. if (dir<0)
  1641. ret=PlayerSettings::bgold;
  1642. else ret=PlayerSettings::brandom;
  1643. }
  1644. SEL->propagateOptions();
  1645. redraw();
  1646. }
  1647. void OptionsTab::recreate()
  1648. {
  1649. bool wasActive = active;
  1650. if(active)
  1651. deactivate();
  1652. for(std::map<int, PlayerOptionsEntry*>::iterator it = entries.begin(); it != entries.end(); ++it)
  1653. {
  1654. children -= it->second;
  1655. delete it->second;
  1656. }
  1657. entries.clear();
  1658. usedHeroes.clear();
  1659. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1660. for(std::map<int, PlayerSettings>::iterator it = SEL->sInfo.playerInfos.begin();
  1661. it != SEL->sInfo.playerInfos.end(); ++it)
  1662. {
  1663. entries.insert(std::make_pair(it->first, new PlayerOptionsEntry(this, it->second)));
  1664. const std::vector<SheroName> &heroes = SEL->current->mapHeader->players[it->first].heroesNames;
  1665. for(size_t hi=0; hi<heroes.size(); hi++)
  1666. usedHeroes.insert(heroes[hi].heroID);
  1667. }
  1668. if(wasActive)
  1669. activate();
  1670. }
  1671. void OptionsTab::setTurnLength( int npos )
  1672. {
  1673. static const int times[] = {1, 2, 4, 6, 8, 10, 15, 20, 25, 30, 0};
  1674. amin(npos, ARRAY_COUNT(times) - 1);
  1675. SEL->sInfo.turnTime = times[npos];
  1676. redraw();
  1677. }
  1678. void OptionsTab::flagPressed( int color )
  1679. {
  1680. PlayerSettings &clicked = SEL->sInfo.playerInfos[color];
  1681. PlayerSettings *old = NULL;
  1682. if(SEL->playerNames.size() == 1) //single player -> just swap
  1683. {
  1684. if(color == playerColor) //that color is already selected, no action needed
  1685. return;
  1686. old = &SEL->sInfo.playerInfos[playerColor];
  1687. swapPlayers(*old, clicked);
  1688. }
  1689. else
  1690. {
  1691. //identify clicked player
  1692. int clickedNameID = clicked.human; //human is a number of player, zero means AI
  1693. if(clickedNameID > 0 && playerToRestore.id == clickedNameID) //player to restore is about to being replaced -> put him back to the old place
  1694. {
  1695. PlayerSettings &restPos = SEL->sInfo.playerInfos[playerToRestore.color];
  1696. SEL->setPlayer(restPos, playerToRestore.id);
  1697. playerToRestore.reset();
  1698. }
  1699. int newPlayer = clickedNameID; //which player will take clicked position
  1700. //who will be put here?
  1701. if(!clickedNameID) //AI player clicked -> if possible replace computer with unallocated player
  1702. {
  1703. newPlayer = SEL->getIdOfFirstUnallocatedPlayer();
  1704. if(!newPlayer) //no "free" player -> get just first one
  1705. newPlayer = SEL->playerNames.begin()->first;
  1706. }
  1707. else //human clicked -> take next
  1708. {
  1709. std::map<ui32, std::string>::const_iterator i = SEL->playerNames.find(clickedNameID); //clicked one
  1710. i++; //player AFTER clicked one
  1711. if(i != SEL->playerNames.end())
  1712. newPlayer = i->first;
  1713. else
  1714. newPlayer = 0; //AI if we scrolled through all players
  1715. }
  1716. SEL->setPlayer(clicked, newPlayer); //put player
  1717. //if that player was somewhere else, we need to replace him with computer
  1718. if(newPlayer) //not AI
  1719. {
  1720. for(std::map<int, PlayerSettings>::iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  1721. {
  1722. int curNameID = i->second.human;
  1723. if(i->first != color && curNameID == newPlayer)
  1724. {
  1725. assert(i->second.human);
  1726. playerToRestore.color = i->first;
  1727. playerToRestore.id = newPlayer;
  1728. SEL->setPlayer(i->second, 0); //set computer
  1729. old = &i->second;
  1730. break;
  1731. }
  1732. }
  1733. }
  1734. }
  1735. entries[clicked.color]->selectButtons();
  1736. if(old)
  1737. {
  1738. entries[old->color]->selectButtons();
  1739. old->hero = entries[old->color]->pi.defaultHero();
  1740. }
  1741. SEL->propagateOptions();
  1742. GH.totalRedraw();
  1743. }
  1744. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry( OptionsTab *owner, PlayerSettings &S)
  1745. : pi(SEL->current->mapHeader->players[S.color]), s(S)
  1746. {
  1747. OBJ_CONSTRUCTION;
  1748. defActions |= SHARE_POS;
  1749. int serial = 0;
  1750. for(int g=0; g < s.color; ++g)
  1751. {
  1752. PlayerInfo &itred = SEL->current->mapHeader->players[g];
  1753. if( itred.canComputerPlay || itred.canHumanPlay)
  1754. serial++;
  1755. }
  1756. pos = parent->pos + Point(54, 122 + serial*50);
  1757. static const char *flags[] = {"AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  1758. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"};
  1759. static const char *bgs[] = {"ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  1760. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"};
  1761. bg = new CPicture(BitmapHandler::loadBitmap(bgs[s.color]), 0, 0, true);
  1762. if(SEL->screenType == CMenuScreen::newGame)
  1763. {
  1764. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[132], bind(&OptionsTab::nextCastle, owner, s.color, -1), 107, 5, "ADOPLFA.DEF");
  1765. btns[1] = new AdventureMapButton(CGI->generaltexth->zelp[133], bind(&OptionsTab::nextCastle, owner, s.color, +1), 168, 5, "ADOPRTA.DEF");
  1766. btns[2] = new AdventureMapButton(CGI->generaltexth->zelp[148], bind(&OptionsTab::nextHero, owner, s.color, -1), 183, 5, "ADOPLFA.DEF");
  1767. btns[3] = new AdventureMapButton(CGI->generaltexth->zelp[149], bind(&OptionsTab::nextHero, owner, s.color, +1), 244, 5, "ADOPRTA.DEF");
  1768. btns[4] = new AdventureMapButton(CGI->generaltexth->zelp[164], bind(&OptionsTab::nextBonus, owner, s.color, -1), 259, 5, "ADOPLFA.DEF");
  1769. btns[5] = new AdventureMapButton(CGI->generaltexth->zelp[165], bind(&OptionsTab::nextBonus, owner, s.color, +1), 320, 5, "ADOPRTA.DEF");
  1770. }
  1771. else
  1772. for(int i = 0; i < 6; i++)
  1773. btns[i] = NULL;
  1774. selectButtons(false);
  1775. assert(SEL->current && SEL->current->mapHeader);
  1776. const PlayerInfo &p = SEL->current->mapHeader->players[s.color];
  1777. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  1778. if(p.canHumanPlay && p.canComputerPlay)
  1779. whoCanPlay = HUMAN_OR_CPU;
  1780. else if(p.canComputerPlay)
  1781. whoCanPlay = CPU;
  1782. else
  1783. whoCanPlay = HUMAN;
  1784. if(SEL->screenType != CMenuScreen::scenarioInfo
  1785. && SEL->current->mapHeader->players[s.color].canHumanPlay
  1786. && SEL->multiPlayer != CMenuScreen::MULTI_NETWORK_GUEST)
  1787. {
  1788. flag = new AdventureMapButton(CGI->generaltexth->zelp[180], bind(&OptionsTab::flagPressed, owner, s.color), -43, 2, flags[s.color]);
  1789. flag->hoverable = true;
  1790. }
  1791. else
  1792. flag = NULL;
  1793. defActions &= ~SHARE_POS;
  1794. town = new SelectedBox(TOWN, s.color);
  1795. town->pos += pos + Point(119, 2);
  1796. hero = new SelectedBox(HERO, s.color);
  1797. hero->pos += pos + Point(195, 2);
  1798. bonus = new SelectedBox(BONUS, s.color);
  1799. bonus->pos += pos + Point(271, 2);
  1800. }
  1801. void OptionsTab::PlayerOptionsEntry::showAll( SDL_Surface * to )
  1802. {
  1803. CIntObject::showAll(to);
  1804. printAtMiddleLoc(s.name, 55, 10, FONT_SMALL, zwykly, to);
  1805. printAtMiddleWBLoc(CGI->generaltexth->arraytxt[206+whoCanPlay], 28, 34, FONT_TINY, 8, zwykly, to);
  1806. }
  1807. void OptionsTab::PlayerOptionsEntry::selectButtons(bool onlyHero)
  1808. {
  1809. if(!btns[0])
  1810. return;
  1811. if( (!onlyHero && pi.defaultCastle() != -1) //fixed tow
  1812. || (SEL->isGuest() && s.color != playerColor)) //or not our player
  1813. {
  1814. btns[0]->disable();
  1815. btns[1]->disable();
  1816. }
  1817. else
  1818. {
  1819. btns[0]->enable(active);
  1820. btns[1]->enable(active);
  1821. }
  1822. if( (pi.defaultHero() != -1 || !s.human || s.castle < 0) //fixed hero
  1823. || (SEL->isGuest() && s.color != playerColor))//or not our player
  1824. {
  1825. btns[2]->disable();
  1826. btns[3]->disable();
  1827. }
  1828. else
  1829. {
  1830. btns[2]->enable(active);
  1831. btns[3]->enable(active);
  1832. }
  1833. if(SEL->isGuest() && s.color != playerColor)//or not our player
  1834. {
  1835. btns[4]->disable();
  1836. btns[5]->disable();
  1837. }
  1838. else
  1839. {
  1840. btns[4]->enable(active);
  1841. btns[5]->enable(active);
  1842. }
  1843. }
  1844. void OptionsTab::SelectedBox::showAll( SDL_Surface * to )
  1845. {
  1846. //PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1847. SDL_Surface *toBlit = getImg();
  1848. const std::string *toPrint = getText();
  1849. blitAt(toBlit, pos, to);
  1850. printAtMiddleLoc(*toPrint, 23, 39, FONT_TINY, zwykly, to);
  1851. }
  1852. OptionsTab::SelectedBox::SelectedBox( SelType Which, ui8 Player )
  1853. :which(Which), player(Player)
  1854. {
  1855. SDL_Surface *img = getImg();
  1856. pos.w = img->w;
  1857. pos.h = img->h;
  1858. used = RCLICK;
  1859. }
  1860. SDL_Surface * OptionsTab::SelectedBox::getImg() const
  1861. {
  1862. const PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1863. switch(which)
  1864. {
  1865. case TOWN:
  1866. if (s.castle < F_NUMBER && s.castle >= 0)
  1867. return graphics->getPic(s.castle, true, false);
  1868. else if (s.castle == -1)
  1869. return CGP->rTown;
  1870. else if (s.castle == -2)
  1871. return CGP->nTown;
  1872. case HERO:
  1873. if (s.hero == -1)
  1874. {
  1875. return CGP->rHero;
  1876. }
  1877. else if (s.hero == -2)
  1878. {
  1879. if(s.heroPortrait >= 0)
  1880. return graphics->portraitSmall[s.heroPortrait];
  1881. else
  1882. return CGP->nHero;
  1883. }
  1884. else
  1885. {
  1886. return graphics->portraitSmall[s.hero];
  1887. }
  1888. break;
  1889. case BONUS:
  1890. {
  1891. int pom;
  1892. switch (s.bonus)
  1893. {
  1894. case -1:
  1895. pom=10;
  1896. break;
  1897. case 0:
  1898. pom=9;
  1899. break;
  1900. case 1:
  1901. pom=8;
  1902. break;
  1903. case 2:
  1904. pom=CGI->townh->towns[s.castle].bonus;
  1905. break;
  1906. default:
  1907. assert(0);
  1908. }
  1909. return CGP->bonuses->ourImages[pom].bitmap;
  1910. }
  1911. default:
  1912. return NULL;
  1913. }
  1914. }
  1915. const std::string * OptionsTab::SelectedBox::getText() const
  1916. {
  1917. const PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1918. switch(which)
  1919. {
  1920. case TOWN:
  1921. if (s.castle < F_NUMBER && s.castle >= 0)
  1922. return &CGI->townh->towns[s.castle].Name();
  1923. else if (s.castle == -1)
  1924. return &CGI->generaltexth->allTexts[522];
  1925. else if (s.castle == -2)
  1926. return &CGI->generaltexth->allTexts[523];
  1927. case HERO:
  1928. if (s.hero == -1)
  1929. return &CGI->generaltexth->allTexts[522];
  1930. else if (s.hero == -2)
  1931. {
  1932. if(s.heroPortrait >= 0)
  1933. {
  1934. if(s.heroName.length())
  1935. return &s.heroName;
  1936. else
  1937. return &CGI->heroh->heroes[s.heroPortrait]->name;
  1938. }
  1939. else
  1940. return &CGI->generaltexth->allTexts[523];
  1941. }
  1942. else
  1943. {
  1944. //if(s.heroName.length())
  1945. // return &s.heroName;
  1946. //else
  1947. return &CGI->heroh->heroes[s.hero]->name;
  1948. }
  1949. case BONUS:
  1950. switch (s.bonus)
  1951. {
  1952. case -1:
  1953. return &CGI->generaltexth->allTexts[522];
  1954. default:
  1955. return &CGI->generaltexth->arraytxt[214 + s.bonus];
  1956. }
  1957. default:
  1958. return NULL;
  1959. }
  1960. }
  1961. void OptionsTab::SelectedBox::clickRight( tribool down, bool previousState )
  1962. {
  1963. if(indeterminate(down) || !down) return;
  1964. const PlayerSettings &s = SEL->sInfo.playerInfos[player];
  1965. SDL_Surface *bmp = NULL;
  1966. const std::string *title = NULL, *subTitle = NULL;
  1967. subTitle = getText();
  1968. int val=-1;
  1969. switch(which)
  1970. {
  1971. case TOWN:
  1972. val = s.castle;
  1973. break;
  1974. case HERO:
  1975. val = s.hero;
  1976. if(val == -2) //none => we may have some preset info
  1977. {
  1978. int p9 = SEL->current->mapHeader->players[s.color].p9;
  1979. if(p9 != 255 && SEL->sInfo.playerInfos[player].heroPortrait >= 0)
  1980. val = p9;
  1981. }
  1982. break;
  1983. case BONUS:
  1984. val = s.bonus;
  1985. break;
  1986. }
  1987. if(val == -1 || which == BONUS) //random or bonus box
  1988. {
  1989. bmp = CMessage::drawBox1(256, 190);
  1990. std::string *description = NULL;
  1991. switch(which)
  1992. {
  1993. case TOWN:
  1994. title = &CGI->generaltexth->allTexts[103];
  1995. description = &CGI->generaltexth->allTexts[104];
  1996. break;
  1997. case HERO:
  1998. title = &CGI->generaltexth->allTexts[101];
  1999. description = &CGI->generaltexth->allTexts[102];
  2000. break;
  2001. case BONUS:
  2002. {
  2003. switch(val)
  2004. {
  2005. case PlayerSettings::brandom:
  2006. title = &CGI->generaltexth->allTexts[86]; //{Random Bonus}
  2007. description = &CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  2008. break;
  2009. case PlayerSettings::bartifact:
  2010. title = &CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  2011. description = &CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  2012. break;
  2013. case PlayerSettings::bgold:
  2014. title = &CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  2015. subTitle = &CGI->generaltexth->allTexts[87]; //500-1000
  2016. description = &CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  2017. break;
  2018. case PlayerSettings::bresource:
  2019. {
  2020. title = &CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  2021. switch(CGI->townh->towns[s.castle].primaryRes)
  2022. {
  2023. case 1:
  2024. subTitle = &CGI->generaltexth->allTexts[694];
  2025. description = &CGI->generaltexth->allTexts[690];
  2026. break;
  2027. case 3:
  2028. subTitle = &CGI->generaltexth->allTexts[695];
  2029. description = &CGI->generaltexth->allTexts[691];
  2030. break;
  2031. case 4:
  2032. subTitle = &CGI->generaltexth->allTexts[692];
  2033. description = &CGI->generaltexth->allTexts[688];
  2034. break;
  2035. case 5:
  2036. subTitle = &CGI->generaltexth->allTexts[693];
  2037. description = &CGI->generaltexth->allTexts[689];
  2038. break;
  2039. case 127:
  2040. subTitle = &CGI->generaltexth->allTexts[89]; //5-10 wood / 5-10 ore
  2041. 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
  2042. break;
  2043. }
  2044. }
  2045. break;
  2046. }
  2047. }
  2048. break;
  2049. }
  2050. if(description)
  2051. CSDL_Ext::printAtMiddleWB(*description, 125, 145, FONT_SMALL, 37, zwykly, bmp);
  2052. }
  2053. else if(val == -2)
  2054. {
  2055. return;
  2056. }
  2057. else if(which == TOWN)
  2058. {
  2059. bmp = CMessage::drawBox1(256, 319);
  2060. title = &CGI->generaltexth->allTexts[80];
  2061. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[79], 135, 137, FONT_MEDIUM, tytulowy, bmp);
  2062. const CTown &t = CGI->townh->towns[val];
  2063. //print creatures
  2064. int x = 60, y = 159;
  2065. for(int i = 0; i < 7; i++)
  2066. {
  2067. int c = t.basicCreatures[i];
  2068. blitAt(graphics->smallImgs[c], x, y, bmp);
  2069. CSDL_Ext::printAtMiddleWB(CGI->creh->creatures[c]->nameSing, x + 16, y + 45, FONT_TINY, 10, zwykly, bmp);
  2070. if(i == 2)
  2071. {
  2072. x = 40;
  2073. y += 76;
  2074. }
  2075. else
  2076. {
  2077. x += 52;
  2078. }
  2079. }
  2080. }
  2081. else if(val >= 0)
  2082. {
  2083. const CHero *h = CGI->heroh->heroes[val];
  2084. bmp = CMessage::drawBox1(320, 255);
  2085. title = &CGI->generaltexth->allTexts[77];
  2086. CSDL_Ext::printAtMiddle(*title, 167, 36, FONT_MEDIUM, tytulowy, bmp);
  2087. CSDL_Ext::printAtMiddle(*subTitle + " - " + h->heroClass->name, 160, 99, FONT_SMALL, zwykly, bmp);
  2088. blitAt(getImg(), 136, 56, bmp);
  2089. //print specialty
  2090. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[78], 166, 132, FONT_MEDIUM, tytulowy, bmp);
  2091. blitAt(graphics->un44->ourImages[val].bitmap, 140, 150, bmp);
  2092. CSDL_Ext::printAtMiddle(CGI->generaltexth->hTxts[val].bonusName, 166, 203, FONT_SMALL, zwykly, bmp);
  2093. GH.pushInt(new CInfoPopup(bmp, true));
  2094. return;
  2095. }
  2096. if(title)
  2097. CSDL_Ext::printAtMiddle(*title, 135, 36, FONT_MEDIUM, tytulowy, bmp);
  2098. if(subTitle)
  2099. CSDL_Ext::printAtMiddle(*subTitle, 127, 103, FONT_SMALL, zwykly, bmp);
  2100. blitAt(getImg(), 104, 60, bmp);
  2101. GH.pushInt(new CInfoPopup(bmp, true));
  2102. }
  2103. CScenarioInfo::CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo)
  2104. {
  2105. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2106. for(std::map<int, PlayerSettings>::const_iterator it = startInfo->playerInfos.begin();
  2107. it != startInfo->playerInfos.end(); ++it)
  2108. {
  2109. if(it->second.human)
  2110. {
  2111. playerColor = it->first;
  2112. }
  2113. }
  2114. pos.w = 762;
  2115. pos.h = 584;
  2116. center(pos);
  2117. assert(LOCPLINT);
  2118. sInfo = *LOCPLINT->cb->getStartInfo();
  2119. assert(!SEL->current);
  2120. current = mapInfoFromGame();
  2121. setPlayersFromGame();
  2122. screenType = CMenuScreen::scenarioInfo;
  2123. card = new InfoCard();
  2124. opt = new OptionsTab();
  2125. opt->recreate();
  2126. card->difficulty->select(startInfo->difficulty, 0);
  2127. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 584, 535, "SCNRBACK.DEF", SDLK_ESCAPE);
  2128. }
  2129. CScenarioInfo::~CScenarioInfo()
  2130. {
  2131. delete current;
  2132. }
  2133. bool mapSorter::operator()(const CMapInfo *aaa, const CMapInfo *bbb)
  2134. {
  2135. const CMapHeader * a = aaa->mapHeader,
  2136. * b = bbb->mapHeader;
  2137. if(a && b) //if we are sorting scenarios
  2138. {
  2139. switch (sortBy)
  2140. {
  2141. case _format: //by map format (RoE, WoG, etc)
  2142. return (a->version<b->version);
  2143. break;
  2144. case _loscon: //by loss conditions
  2145. return (a->lossCondition.typeOfLossCon<b->lossCondition.typeOfLossCon);
  2146. break;
  2147. case _playerAm: //by player amount
  2148. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  2149. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  2150. for (int i=0;i<8;i++)
  2151. {
  2152. if (a->players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  2153. else if (a->players[i].canComputerPlay) {playerAmntA++;}
  2154. if (b->players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  2155. else if (b->players[i].canComputerPlay) {playerAmntB++;}
  2156. }
  2157. if (playerAmntB!=playerAmntA)
  2158. return (playerAmntA<playerAmntB);
  2159. else
  2160. return (humenPlayersA<humenPlayersB);
  2161. break;
  2162. case _size: //by size of map
  2163. return (a->width<b->width);
  2164. break;
  2165. case _viccon: //by victory conditions
  2166. return (a->victoryCondition.condition < b->victoryCondition.condition);
  2167. break;
  2168. case _name: //by name
  2169. return (a->name<b->name);
  2170. break;
  2171. default:
  2172. return (a->name<b->name);
  2173. break;
  2174. }
  2175. }
  2176. else //if we are sorting campaigns
  2177. {
  2178. switch(sortBy)
  2179. {
  2180. case _numOfMaps: //by number of maps in campaign
  2181. return CGI->generaltexth->campaignRegionNames[ aaa->campaignHeader->mapVersion ].size() <
  2182. CGI->generaltexth->campaignRegionNames[ bbb->campaignHeader->mapVersion ].size();
  2183. break;
  2184. case _name: //by name
  2185. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  2186. break;
  2187. default:
  2188. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  2189. break;
  2190. }
  2191. }
  2192. }
  2193. CMultiMode::CMultiMode()
  2194. {
  2195. OBJ_CONSTRUCTION;
  2196. bg = new CPicture("MUPOPUP.bmp");
  2197. bg->convertToScreenBPP(); //so we could draw without problems
  2198. blitAt(CPicture("MUMAP.bmp"), 16, 77, *bg); //blit img
  2199. pos = bg->center(); //center, window has size of bg graphic
  2200. bar = new CGStatusBar(new CPicture(Rect(7, 465, 440, 18), 0));//226, 472
  2201. txt = new CTextInput(Rect(19, 436, 334, 16), *bg);
  2202. txt->setText(GDefaultOptions.playerName); //Player
  2203. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[266], bind(&CMultiMode::openHotseat, this), 373, 78, "MUBHOT.DEF");
  2204. btns[1] = new AdventureMapButton("Host TCP/IP game", "", bind(&CMultiMode::hostTCP, this), 373, 78 + 57*1, "MUBHOST.DEF");
  2205. btns[2] = new AdventureMapButton("Join TCP/IP game", "", bind(&CMultiMode::joinTCP, this), 373, 78 + 57*2, "MUBJOIN.DEF");
  2206. btns[6] = new AdventureMapButton(CGI->generaltexth->zelp[288], bind(&CGuiHandler::popIntTotally, ref(GH), this), 373, 424, "MUBCANC.DEF", SDLK_ESCAPE);
  2207. }
  2208. void CMultiMode::openHotseat()
  2209. {
  2210. GH.pushInt(new CHotSeatPlayers(txt->text));
  2211. }
  2212. void CMultiMode::hostTCP()
  2213. {
  2214. GDefaultOptions.setPlayerName(txt->text);
  2215. GH.popIntTotally(this);
  2216. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, CMenuScreen::MULTI_NETWORK_HOST));
  2217. }
  2218. void CMultiMode::joinTCP()
  2219. {
  2220. GDefaultOptions.setPlayerName(txt->text);
  2221. GH.popIntTotally(this);
  2222. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, CMenuScreen::MULTI_NETWORK_GUEST));
  2223. }
  2224. CHotSeatPlayers::CHotSeatPlayers(const std::string &firstPlayer)
  2225. {
  2226. OBJ_CONSTRUCTION;
  2227. bg = new CPicture("MUHOTSEA.bmp");
  2228. bg->convertToScreenBPP(); //so we could draw without problems
  2229. bg->printAtMiddleWBLoc(CGI->generaltexth->allTexts[446], 185, 55, FONT_BIG, 50, zwykly, *bg); //HOTSEAT Please enter names
  2230. pos = bg->center(); //center, window has size of bg graphic
  2231. for(int i = 0; i < ARRAY_COUNT(txt); i++)
  2232. txt[i] = new CTextInput(Rect(60, 85 + i*30, 280, 16), *bg);
  2233. txt[0]->setText(firstPlayer);
  2234. txt[0]->giveFocus();
  2235. ok = new AdventureMapButton(CGI->generaltexth->zelp[560], bind(&CHotSeatPlayers::enterSelectionScreen, this), 95, 338, "MUBCHCK.DEF", SDLK_RETURN);
  2236. cancel = new AdventureMapButton(CGI->generaltexth->zelp[561], bind(&CGuiHandler::popIntTotally, ref(GH), this), 205, 338, "MUBCANC.DEF", SDLK_ESCAPE);
  2237. bar = new CGStatusBar(new CPicture(Rect(7, 381, 348, 18), 0));//226, 472
  2238. }
  2239. void CHotSeatPlayers::enterSelectionScreen()
  2240. {
  2241. std::map<ui32, std::string> names;
  2242. for(int i = 0, j = 1; i < ARRAY_COUNT(txt); i++)
  2243. if(txt[i]->text.length())
  2244. names[j++] = txt[i]->text;
  2245. assert(names.size() > 1); //two players at least - after all, it's hot seat mode...
  2246. GDefaultOptions.setPlayerName(names.begin()->second); //remember selected name
  2247. GH.popInts(2); //pop MP mode window and this
  2248. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame, CMenuScreen::MULTI_HOT_SEAT, &names));
  2249. }
  2250. CBonusSelection::CBonusSelection( CCampaignState * _ourCampaign )
  2251. : highlightedRegion(NULL), ourCampaign(_ourCampaign), ourHeader(NULL),
  2252. diffLb(NULL), diffRb(NULL), bonuses(NULL)
  2253. {
  2254. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2255. static const std::string bgNames [] = {"E1_BG.BMP", "G2_BG.BMP", "E2_BG.BMP", "G1_BG.BMP", "G3_BG.BMP", "N1_BG.BMP",
  2256. "S1_BG.BMP", "BR_BG.BMP", "IS_BG.BMP", "KR_BG.BMP", "NI_BG.BMP", "TA_BG.BMP", "AR_BG.BMP", "HS_BG.BMP",
  2257. "BB_BG.BMP", "NB_BG.BMP", "EL_BG.BMP", "RN_BG.BMP", "UA_BG.BMP", "SP_BG.BMP"};
  2258. loadPositionsOfGraphics();
  2259. background = BitmapHandler::loadBitmap(bgNames[ourCampaign->camp->header.mapVersion]);
  2260. SDL_Surface * panel = BitmapHandler::loadBitmap("CAMPBRF.BMP");
  2261. blitAt(panel, 456, 6, background);
  2262. startB = new AdventureMapButton("", "", bind(&CBonusSelection::startMap, this), 475, 536, "CBBEGIB.DEF", SDLK_RETURN);
  2263. backB = new AdventureMapButton("", "", bind(&CBonusSelection::goBack, this), 624, 536, "CBCANCB.DEF", SDLK_ESCAPE);
  2264. //campaign name
  2265. if (ourCampaign->camp->header.name.length())
  2266. printAtLoc(ourCampaign->camp->header.name, 481, 28, FONT_BIG, tytulowy, background);
  2267. else
  2268. printAtLoc("Unnamed", 481, 28, FONT_BIG, tytulowy, background);
  2269. //map size icon
  2270. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  2271. //campaign description
  2272. printAtLoc(CGI->generaltexth->allTexts[38], 481, 63, FONT_SMALL, tytulowy, background);
  2273. cmpgDesc = new CTextBox(ourCampaign->camp->header.description, Rect(480, 86, 286, 117), 1);
  2274. cmpgDesc->showAll(background);
  2275. //map description
  2276. mapDesc = new CTextBox("", Rect(480, 280, 286, 117), 1);
  2277. //bonus choosing
  2278. printAtMiddleLoc(CGI->generaltexth->allTexts[71], 562, 438, FONT_MEDIUM, zwykly, background); //Choose a bonus:
  2279. bonuses = new CHighlightableButtonsGroup(bind(&CBonusSelection::selectBonus, this, _1));
  2280. //set left part of window
  2281. for (int g=0; g<ourCampaign->camp->scenarios.size(); ++g)
  2282. {
  2283. if(ourCampaign->camp->conquerable(g))
  2284. {
  2285. regions.push_back(new CRegion(this, true, true, g));
  2286. regions[regions.size()-1]->rclickText = ourCampaign->camp->scenarios[g].regionText;
  2287. if (highlightedRegion == NULL)
  2288. {
  2289. highlightedRegion = regions.back();
  2290. selectMap(g);
  2291. }
  2292. }
  2293. else if (ourCampaign->camp->scenarios[g].conquered) //display as striped
  2294. {
  2295. regions.push_back(new CRegion(this, false, false, g));
  2296. regions[regions.size()-1]->rclickText = ourCampaign->camp->scenarios[g].regionText;
  2297. }
  2298. }
  2299. //init campaign state if necessary
  2300. if (ourCampaign->campaignName.size() == 0)
  2301. {
  2302. ourCampaign->initNewCampaign(sInfo);
  2303. }
  2304. //allies / enemies
  2305. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 486, 407, FONT_SMALL, zwykly, background); //Allies
  2306. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 619, 407, FONT_SMALL, zwykly, background); //Enemies
  2307. SDL_FreeSurface(panel);
  2308. //difficulty
  2309. printAtMiddleLoc(CGI->generaltexth->allTexts[492], 715, 438, FONT_MEDIUM, zwykly, background); //Difficulty
  2310. {//difficulty pics
  2311. for (int b=0; b<ARRAY_COUNT(diffPics); ++b)
  2312. {
  2313. CDefEssential * cde = CDefHandler::giveDefEss("GSPBUT" + boost::lexical_cast<std::string>(b+3) + ".DEF");
  2314. SDL_Surface * surfToDuplicate = cde->ourImages[0].bitmap;
  2315. diffPics[b] = SDL_ConvertSurface(surfToDuplicate, surfToDuplicate->format,
  2316. surfToDuplicate->flags);
  2317. delete cde;
  2318. }
  2319. }
  2320. //difficulty selection buttons
  2321. if (ourCampaign->camp->header.difficultyChoosenByPlayer)
  2322. {
  2323. diffLb = new AdventureMapButton("", "", bind(&CBonusSelection::changeDiff, this, false), 694, 508, "SCNRBLF.DEF");
  2324. diffRb = new AdventureMapButton("", "", bind(&CBonusSelection::changeDiff, this, true), 738, 508, "SCNRBRT.DEF");
  2325. }
  2326. //load miniflags
  2327. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  2328. }
  2329. CBonusSelection::~CBonusSelection()
  2330. {
  2331. SDL_FreeSurface(background);
  2332. delete sizes;
  2333. delete ourHeader;
  2334. delete sFlags;
  2335. for (int b=0; b<ARRAY_COUNT(diffPics); ++b)
  2336. {
  2337. SDL_FreeSurface(diffPics[b]);
  2338. }
  2339. }
  2340. void CBonusSelection::goBack()
  2341. {
  2342. GH.popIntTotally(this);
  2343. }
  2344. void CBonusSelection::showAll( SDL_Surface * to )
  2345. {
  2346. blitAt(background, pos.x, pos.y, to);
  2347. CIntObject::showAll(to);
  2348. show(to);
  2349. }
  2350. void CBonusSelection::loadPositionsOfGraphics()
  2351. {
  2352. std::ifstream is(DATA_DIR "/config/campaign_regions.txt", std::ios_base::binary | std::ios_base::in);
  2353. assert(is.is_open());
  2354. for (int g=0; g<CGI->generaltexth->campaignMapNames.size(); ++g)
  2355. {
  2356. SCampPositions sc;
  2357. is >> sc.campPrefix;
  2358. is >> sc.colorSuffixLength;
  2359. bool contReading = true;
  2360. while(contReading)
  2361. {
  2362. SCampPositions::SRegionDesc rd;
  2363. is >> rd.infix;
  2364. if(rd.infix == "END")
  2365. {
  2366. contReading = false;
  2367. }
  2368. else
  2369. {
  2370. is >> rd.xpos >> rd.ypos;
  2371. sc.regions.push_back(rd);
  2372. }
  2373. }
  2374. campDescriptions.push_back(sc);
  2375. }
  2376. }
  2377. void CBonusSelection::selectMap( int whichOne )
  2378. {
  2379. sInfo.difficulty = ourCampaign->camp->scenarios[whichOne].difficulty;
  2380. sInfo.mapname = ourCampaign->camp->header.filename;
  2381. sInfo.mode = StartInfo::CAMPAIGN;
  2382. //get header
  2383. int i = 0;
  2384. delete ourHeader;
  2385. ourHeader = new CMapHeader();
  2386. ourHeader->initFromMemory((const unsigned char*)ourCampaign->camp->mapPieces.find(whichOne)->second.c_str(), i);
  2387. std::map<ui32, std::string> names;
  2388. names[1] = GDefaultOptions.playerName;
  2389. updateStartInfo(ourCampaign->camp->header.filename, sInfo, ourHeader, names);
  2390. sInfo.turnTime = 0;
  2391. sInfo.whichMapInCampaign = whichOne;
  2392. sInfo.difficulty = ourCampaign->camp->scenarios[whichOne].difficulty;
  2393. ourCampaign->currentMap = whichOne;
  2394. mapDesc->setTxt(ourHeader->description);
  2395. updateBonusSelection();
  2396. }
  2397. void CBonusSelection::show( SDL_Surface * to )
  2398. {
  2399. //blitAt(background, pos.x, pos.y, to);
  2400. //map name
  2401. std::string mapName = ourHeader->name;
  2402. if (mapName.length())
  2403. printAtLoc(mapName, 481, 219, FONT_BIG, tytulowy, to);
  2404. else
  2405. printAtLoc("Unnamed", 481, 219, FONT_BIG, tytulowy, to);
  2406. //map description
  2407. printAtLoc(CGI->generaltexth->allTexts[496], 481, 253, FONT_SMALL, tytulowy, to);
  2408. mapDesc->showAll(to); //showAll because CTextBox has no show()
  2409. //map size icon
  2410. int temp;
  2411. switch (ourHeader->width)
  2412. {
  2413. case 36:
  2414. temp=0;
  2415. break;
  2416. case 72:
  2417. temp=1;
  2418. break;
  2419. case 108:
  2420. temp=2;
  2421. break;
  2422. case 144:
  2423. temp=3;
  2424. break;
  2425. default:
  2426. temp=4;
  2427. break;
  2428. }
  2429. blitAtLoc(sizes->ourImages[temp].bitmap, 735, 26, to);
  2430. //flags
  2431. int fx = 496 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[390].c_str());
  2432. int ex = 629 + graphics->fonts[FONT_SMALL]->getWidth(CGI->generaltexth->allTexts[391].c_str());
  2433. int myT;
  2434. myT = ourHeader->players[playerColor].team;
  2435. for (std::map<int, PlayerSettings>::const_iterator i = sInfo.playerInfos.begin(); i != sInfo.playerInfos.end(); i++)
  2436. {
  2437. int *myx = ((i->first == playerColor || ourHeader->players[i->first].team == myT) ? &fx : &ex);
  2438. blitAtLoc(sFlags->ourImages[i->first].bitmap, *myx, 405, to);
  2439. *myx += sFlags->ourImages[i->first].bitmap->w;
  2440. }
  2441. //difficulty
  2442. blitAt(diffPics[sInfo.difficulty], 709, 455, to);
  2443. CIntObject::show(to);
  2444. }
  2445. void CBonusSelection::updateBonusSelection()
  2446. {
  2447. //graphics:
  2448. //spell - SPELLBON.DEF
  2449. //monster - TWCRPORT.DEF
  2450. //building - BO*.BMP graphics
  2451. //artifact - ARTIFBON.DEF
  2452. //spell scroll - SPELLBON.DEF
  2453. //prim skill - PSKILBON.DEF
  2454. //sec skill - SSKILBON.DEF
  2455. //resource - BORES.DEF
  2456. //player - ?
  2457. //hero -?
  2458. const CCampaignScenario &scenario = ourCampaign->camp->scenarios[sInfo.whichMapInCampaign];
  2459. const std::vector<CScenarioTravel::STravelBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  2460. bonuses->buttons.clear();
  2461. {
  2462. BLOCK_CAPTURING;
  2463. static const char *bonusPics[] = {"SPELLBON.DEF", "TWCRPORT.DEF", "", "ARTIFBON.DEF", "SPELLBON.DEF",
  2464. "PSKILBON.DEF", "SSKILBON.DEF", "BORES.DEF", "CREST58.DEF", "HPL000KN"};
  2465. for(int i = 0; i < bonDescs.size(); i++)
  2466. {
  2467. std::string picName=bonusPics[bonDescs[i].type];
  2468. size_t picNumber=bonDescs[i].info2;
  2469. std::string desc;
  2470. switch(bonDescs[i].type)
  2471. {
  2472. case 0: //spell
  2473. desc = CGI->generaltexth->allTexts[715];
  2474. boost::algorithm::replace_first(desc, "%s", CGI->spellh->spells[bonDescs[i].info2]->name);
  2475. break;
  2476. case 1: //monster
  2477. picNumber = bonDescs[i].info2 + 2;
  2478. desc = CGI->generaltexth->allTexts[717];
  2479. boost::algorithm::replace_first(desc, "%d", boost::lexical_cast<std::string>(bonDescs[i].info3));
  2480. boost::algorithm::replace_first(desc, "%s", CGI->creh->creatures[bonDescs[i].info2]->namePl);
  2481. break;
  2482. case 2: //building
  2483. {
  2484. int faction = -1;
  2485. for(std::map<int, PlayerSettings>::iterator it = sInfo.playerInfos.begin();
  2486. it != sInfo.playerInfos.end(); ++it)
  2487. {
  2488. if (it->second.human)
  2489. {
  2490. faction = it->second.castle;
  2491. break;
  2492. }
  2493. }
  2494. assert(faction != -1);
  2495. picName = graphics->ERMUtoPicture[faction][CBuildingHandler::campToERMU(bonDescs[i].info1, faction, std::set<si32>())];
  2496. picNumber = -1;
  2497. }
  2498. break;
  2499. case 3: //artifact
  2500. desc = CGI->generaltexth->allTexts[715];
  2501. boost::algorithm::replace_first(desc, "%s", CGI->arth->artifacts[bonDescs[i].info2]->Name());
  2502. break;
  2503. case 4: //spell scroll
  2504. desc = CGI->generaltexth->allTexts[716];
  2505. boost::algorithm::replace_first(desc, "%s", CGI->spellh->spells[bonDescs[i].info2]->name);
  2506. break;
  2507. case 5: //primary skill
  2508. {
  2509. int leadingSkill = -1;
  2510. std::vector<std::pair<int, int> > toPrint; //primary skills to be listed <num, val>
  2511. const ui8* ptr = reinterpret_cast<const ui8*>(&bonDescs[i].info2);
  2512. for (int g=0; g<PRIMARY_SKILLS; ++g)
  2513. {
  2514. if (leadingSkill == -1 || ptr[g] > ptr[leadingSkill])
  2515. {
  2516. leadingSkill = g;
  2517. }
  2518. if (ptr[g] != 0)
  2519. {
  2520. toPrint.push_back(std::make_pair(g, ptr[g]));
  2521. }
  2522. }
  2523. picNumber = leadingSkill;
  2524. desc = CGI->generaltexth->allTexts[715];
  2525. std::string substitute; //text to be printed instead of %s
  2526. for (int v=0; v<toPrint.size(); ++v)
  2527. {
  2528. substitute += boost::lexical_cast<std::string>(toPrint[v].second);
  2529. substitute += " " + CGI->generaltexth->primarySkillNames[toPrint[v].first];
  2530. if(v != toPrint.size() - 1)
  2531. {
  2532. substitute += ", ";
  2533. }
  2534. }
  2535. boost::algorithm::replace_first(desc, "%s", substitute);
  2536. break;
  2537. }
  2538. case 6: //secondary skill
  2539. desc = CGI->generaltexth->allTexts[718];
  2540. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->levels[bonDescs[i].info3]); //skill level
  2541. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->skillName[bonDescs[i].info2]); //skill name
  2542. break;
  2543. case 7: //resource
  2544. {
  2545. int serialResID = 0;
  2546. switch(bonDescs[i].info1)
  2547. {
  2548. case 0: case 1: case 2: case 3: case 4: case 5: case 6:
  2549. serialResID = bonDescs[i].info1;
  2550. break;
  2551. case 0xFD: //wood + ore
  2552. serialResID = 7;
  2553. break;
  2554. case 0xFE: //rare resources
  2555. serialResID = 8;
  2556. break;
  2557. }
  2558. picNumber = serialResID;
  2559. desc = CGI->generaltexth->allTexts[717];
  2560. boost::algorithm::replace_first(desc, "%d", boost::lexical_cast<std::string>(bonDescs[i].info2));
  2561. std::string replacement;
  2562. if (serialResID <= 6)
  2563. {
  2564. replacement = CGI->generaltexth->restypes[serialResID];
  2565. }
  2566. else
  2567. {
  2568. replacement = CGI->generaltexth->allTexts[714 + serialResID];
  2569. }
  2570. boost::algorithm::replace_first(desc, "%s", replacement);
  2571. }
  2572. break;
  2573. case 8: //player aka hero crossover
  2574. picNumber = bonDescs[i].info1;
  2575. desc = CGI->generaltexth->allTexts[718];
  2576. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->capColors[bonDescs[i].info1]); //player color
  2577. boost::algorithm::replace_first(desc, "%s", ourCampaign->camp->scenarios[bonDescs[i].info2].mapName); //scenario
  2578. break;
  2579. case 9: //hero
  2580. desc = CGI->generaltexth->allTexts[718];
  2581. boost::algorithm::replace_first(desc, "%s", CGI->generaltexth->capColors[bonDescs[i].info1]); //hero's color
  2582. if (bonDescs[i].info2 == 0xFFFF)
  2583. {
  2584. boost::algorithm::replace_first(desc, "%s", CGI->heroh->heroes[0]->name); //hero's name
  2585. //surfToDuplicate = graphics->portraitLarge[0];
  2586. //TODO: re-enable - need to get filename or CAnimation with heroes pics
  2587. }
  2588. else
  2589. {
  2590. boost::algorithm::replace_first(desc, "%s", CGI->heroh->heroes[bonDescs[i].info2]->name); //hero's name
  2591. //surfToDuplicate = graphics->portraitLarge[bonDescs[i].info2];
  2592. }
  2593. picNumber = -1;
  2594. break;
  2595. }
  2596. bonuses->addButton(new CHighlightableButton(desc, desc, 0, 475 + i*68, 455, "", i));
  2597. //create separate surface with yellow border
  2598. if (picNumber != -1)
  2599. picName += ":" + boost::lexical_cast<std::string>(picNumber);
  2600. CAnimation * anim = new CAnimation();
  2601. anim->setCustom(picName, 0);
  2602. anim->setCustom("TWCRPORT:1", 1);
  2603. bonuses->buttons.back()->setImage(anim);
  2604. //FIXME: use show base
  2605. }
  2606. }
  2607. if (bonuses->buttons.size() > 0)
  2608. {
  2609. bonuses->select(0, 0);
  2610. }
  2611. }
  2612. void CBonusSelection::startMap()
  2613. {
  2614. StartInfo *si = new StartInfo(sInfo);
  2615. if (ourCampaign->mapsConquered.size())
  2616. {
  2617. GH.popInts(1);
  2618. }
  2619. else
  2620. {
  2621. GH.popInts(3);
  2622. }
  2623. ::startGame(si);
  2624. }
  2625. void CBonusSelection::selectBonus( int id )
  2626. {
  2627. sInfo.choosenCampaignBonus = id;
  2628. const CCampaignScenario &scenario = ourCampaign->camp->scenarios[sInfo.whichMapInCampaign];
  2629. const std::vector<CScenarioTravel::STravelBonus> & bonDescs = scenario.travelOptions.bonusesToChoose;
  2630. if (bonDescs[id].type == 8) //hero crossover
  2631. {
  2632. SEL->setPlayer(sInfo.playerInfos[0], bonDescs[id].info1); //TODO: substitute with appropriate color
  2633. }
  2634. }
  2635. void CBonusSelection::changeDiff( bool increase )
  2636. {
  2637. if (increase)
  2638. {
  2639. sInfo.difficulty = std::min(sInfo.difficulty + 1, 4);
  2640. }
  2641. else
  2642. {
  2643. sInfo.difficulty = std::max(sInfo.difficulty - 1, 0);
  2644. }
  2645. }
  2646. CBonusSelection::CRegion::CRegion( CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber )
  2647. : owner(_owner), accessible(_accessible), selectable(_selectable), myNumber(_myNumber)
  2648. {
  2649. OBJ_CONSTRUCTION;
  2650. used = LCLICK | RCLICK;
  2651. static const std::string colors[2][8] = {
  2652. {"R", "B", "N", "G", "O", "V", "T", "P"},
  2653. {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"}};
  2654. const SCampPositions & campDsc = owner->campDescriptions[owner->ourCampaign->camp->header.mapVersion];
  2655. const SCampPositions::SRegionDesc & desc = campDsc.regions[myNumber];
  2656. pos.x = desc.xpos;
  2657. pos.y = desc.ypos;
  2658. //loading of graphics
  2659. std::string prefix = campDsc.campPrefix + desc.infix + "_";
  2660. std::string suffix = colors[campDsc.colorSuffixLength - 1][owner->ourCampaign->camp->scenarios[myNumber].regionColor];
  2661. static const std::string infix [] = {"En", "Se", "Co"};
  2662. for (int g = 0; g < ARRAY_COUNT(infix); g++)
  2663. {
  2664. graphics[g] = BitmapHandler::loadBitmap(prefix + infix[g] + suffix + ".BMP");
  2665. }
  2666. pos.w = graphics[0]->w;
  2667. pos.h = graphics[0]->h;
  2668. }
  2669. CBonusSelection::CRegion::~CRegion()
  2670. {
  2671. for (int g=0; g<ARRAY_COUNT(graphics); ++g)
  2672. {
  2673. SDL_FreeSurface(graphics[g]);
  2674. }
  2675. }
  2676. void CBonusSelection::CRegion::clickLeft( tribool down, bool previousState )
  2677. {
  2678. //select if selectable & clicked inside our graphic
  2679. if ( indeterminate(down) )
  2680. {
  2681. return;
  2682. }
  2683. if( !down && selectable && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) )
  2684. {
  2685. owner->selectMap(myNumber);
  2686. owner->highlightedRegion = this;
  2687. }
  2688. }
  2689. void CBonusSelection::CRegion::clickRight( tribool down, bool previousState )
  2690. {
  2691. //show r-click text
  2692. if( down && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) &&
  2693. rclickText.size() )
  2694. {
  2695. CRClickPopup::createAndPush(rclickText);
  2696. }
  2697. }
  2698. void CBonusSelection::CRegion::show( SDL_Surface * to )
  2699. {
  2700. //const SCampPositions::SRegionDesc & desc = owner->campDescriptions[owner->ourCampaign->camp->header.mapVersion].regions[myNumber];
  2701. if (!accessible)
  2702. {
  2703. //show as striped
  2704. blitAt(graphics[2], pos.x, pos.y, to);
  2705. }
  2706. else if (this == owner->highlightedRegion)
  2707. {
  2708. //show as selected
  2709. blitAt(graphics[1], pos.x, pos.y, to);
  2710. }
  2711. else
  2712. {
  2713. //show as not selected selected
  2714. blitAt(graphics[0], pos.x, pos.y, to);
  2715. }
  2716. }
  2717. CSavingScreen::CSavingScreen(bool hotseat)
  2718. : CSelectionScreen(CMenuScreen::saveGame, hotseat ? CMenuScreen::MULTI_HOT_SEAT : CMenuScreen::SINGLE_PLAYER)
  2719. {
  2720. ourGame = mapInfoFromGame();
  2721. sInfo = *LOCPLINT->cb->getStartInfo();
  2722. setPlayersFromGame();
  2723. }
  2724. CSavingScreen::~CSavingScreen()
  2725. {
  2726. }
  2727. ISelectionScreenInfo::ISelectionScreenInfo(const std::map<ui32, std::string> *Names /*= NULL*/)
  2728. {
  2729. multiPlayer = CMenuScreen::SINGLE_PLAYER;
  2730. assert(!SEL);
  2731. SEL = this;
  2732. current = NULL;
  2733. if(Names && Names->size()) //if have custom set of player names - use it
  2734. playerNames = *Names;
  2735. else
  2736. playerNames[1] = GDefaultOptions.playerName; //by default we have only one player and his name is "Player" (or whatever the last used name was)
  2737. }
  2738. ISelectionScreenInfo::~ISelectionScreenInfo()
  2739. {
  2740. assert(SEL == this);
  2741. SEL = NULL;
  2742. }
  2743. void ISelectionScreenInfo::updateStartInfo(std::string filename, StartInfo & sInfo, const CMapHeader * mapHeader)
  2744. {
  2745. ::updateStartInfo(filename, sInfo, mapHeader, playerNames);
  2746. }
  2747. void ISelectionScreenInfo::setPlayer(PlayerSettings &pset, unsigned player)
  2748. {
  2749. ::setPlayer(pset, player, playerNames);
  2750. }
  2751. int ISelectionScreenInfo::getIdOfFirstUnallocatedPlayer()
  2752. {
  2753. for(std::map<ui32, std::string>::const_iterator i = playerNames.begin(); i != playerNames.end(); i++)
  2754. if(!sInfo.getPlayersSettings(i->first)) //
  2755. return i->first;
  2756. return 0;
  2757. }
  2758. bool ISelectionScreenInfo::isGuest() const
  2759. {
  2760. return multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST;
  2761. }
  2762. bool ISelectionScreenInfo::isHost() const
  2763. {
  2764. return multiPlayer == CMenuScreen::MULTI_NETWORK_HOST;
  2765. }
  2766. void ChatMessage::apply(CSelectionScreen *selScreen)
  2767. {
  2768. selScreen->card->chat->addNewMessage(playerName + ": " + message);
  2769. GH.totalRedraw();
  2770. }
  2771. void QuitMenuWithoutStarting::apply(CSelectionScreen *selScreen)
  2772. {
  2773. if(!selScreen->ongoingClosing)
  2774. {
  2775. *selScreen->serv << this; //resend to confirm
  2776. GH.popIntTotally(selScreen); //will wait with deleting us before this thread ends
  2777. }
  2778. delNull(selScreen->serv);
  2779. }
  2780. void PlayerJoined::apply(CSelectionScreen *selScreen)
  2781. {
  2782. //assert(SEL->playerNames.size() == connectionID); //temporary, TODO when player exits
  2783. SEL->playerNames[connectionID] = playerName;
  2784. //put new player in first slot with AI
  2785. for(std::map<int, PlayerSettings>::iterator i = SEL->sInfo.playerInfos.begin(); i != SEL->sInfo.playerInfos.end(); i++)
  2786. {
  2787. if(!i->second.human)
  2788. {
  2789. selScreen->setPlayer(i->second, connectionID);
  2790. selScreen->opt->entries[i->second.color]->selectButtons();
  2791. break;
  2792. }
  2793. }
  2794. selScreen->propagateNames();
  2795. selScreen->propagateOptions();
  2796. GH.totalRedraw();
  2797. }
  2798. void SelectMap::apply(CSelectionScreen *selScreen)
  2799. {
  2800. if(selScreen->multiPlayer == CMenuScreen::MULTI_NETWORK_GUEST)
  2801. {
  2802. free = false;
  2803. selScreen->changeSelection(mapInfo);
  2804. }
  2805. }
  2806. void UpdateStartOptions::apply(CSelectionScreen *selScreen)
  2807. {
  2808. if(!selScreen->isGuest())
  2809. return;
  2810. selScreen->setSInfo(*options);
  2811. }
  2812. void PregameGuiAction::apply(CSelectionScreen *selScreen)
  2813. {
  2814. if(!selScreen->isGuest())
  2815. return;
  2816. switch(action)
  2817. {
  2818. case NO_TAB:
  2819. selScreen->toggleTab(selScreen->curTab);
  2820. break;
  2821. case OPEN_OPTIONS:
  2822. selScreen->toggleTab(selScreen->opt);
  2823. break;
  2824. case OPEN_SCENARIO_LIST:
  2825. selScreen->toggleTab(selScreen->sel);
  2826. break;
  2827. }
  2828. }
  2829. void RequestOptionsChange::apply(CSelectionScreen *selScreen)
  2830. {
  2831. if(!selScreen->isHost())
  2832. return;
  2833. ui8 color = selScreen->sInfo.getPlayersSettings(playerID)->color;
  2834. switch(what)
  2835. {
  2836. case TOWN:
  2837. selScreen->opt->nextCastle(color, direction);
  2838. break;
  2839. case HERO:
  2840. selScreen->opt->nextHero(color, direction);
  2841. break;
  2842. case BONUS:
  2843. selScreen->opt->nextBonus(color, direction);
  2844. break;
  2845. }
  2846. }
  2847. void PlayerLeft::apply(CSelectionScreen *selScreen)
  2848. {
  2849. if(selScreen->isGuest())
  2850. return;
  2851. SEL->playerNames.erase(playerID);
  2852. if(PlayerSettings *s = selScreen->sInfo.getPlayersSettings(playerID)) //it's possible that player was unallocated
  2853. {
  2854. selScreen->setPlayer(*s, 0);
  2855. selScreen->opt->entries[s->color]->selectButtons();
  2856. }
  2857. selScreen->propagateNames();
  2858. selScreen->propagateOptions();
  2859. GH.totalRedraw();
  2860. }
  2861. void PlayersNames::apply(CSelectionScreen *selScreen)
  2862. {
  2863. if(selScreen->isGuest())
  2864. selScreen->playerNames = playerNames;
  2865. }
  2866. void StartWithCurrentSettings::apply(CSelectionScreen *selScreen)
  2867. {
  2868. startingInfo.reset();
  2869. startingInfo.serv = selScreen->serv;
  2870. startingInfo.sInfo = new StartInfo(selScreen->sInfo);
  2871. if(!selScreen->ongoingClosing)
  2872. {
  2873. *selScreen->serv << this; //resend to confirm
  2874. }
  2875. selScreen->serv = NULL; //hide it so it won't be deleted
  2876. delNull(selScreen->serverHandlingThread); //detach us
  2877. selectedName = selScreen->sInfo.mapname;
  2878. GH.curInt = NULL;
  2879. GH.popIntTotally(selScreen);
  2880. GH.popInt(GH.topInt()); //only deactivate main menu screen
  2881. ::startGame(startingInfo.sInfo, startingInfo.serv);
  2882. throw 666; //EVIL, EVIL, EVIL workaround to kill thread (does "goto catch" outside listening loop)
  2883. }