CPreGame.cpp 66 KB

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