CPreGame.cpp 100 KB

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