CPreGame.cpp 76 KB

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