CPreGame.cpp 100 KB

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