CPreGame.cpp 101 KB

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