CPreGame.cpp 100 KB

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