CPreGame.cpp 100 KB

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