CPreGame.cpp 73 KB

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