CPreGame.cpp 92 KB

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