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