CPreGame.cpp 99 KB

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