CPreGame.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693
  1. #include "../stdafx.h"
  2. #include "CPreGame.h"
  3. #include "../hch/CDefHandler.h"
  4. #include <ctime>
  5. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  6. #include <boost/algorithm/string.hpp>
  7. #include <zlib.h>
  8. #include "../timeHandler.h"
  9. #include <sstream>
  10. #include "SDL_Extensions.h"
  11. #include "CGameInfo.h"
  12. #include "../hch/CGeneralTextHandler.h"
  13. #include "CCursorHandler.h"
  14. #include "../hch/CLodHandler.h"
  15. #include "../hch/CTownHandler.h"
  16. #include "../hch/CHeroHandler.h"
  17. #include <cmath>
  18. #include "Graphics.h"
  19. //#include <boost/thread.hpp>
  20. #include <boost/bind.hpp>
  21. #include <cstdlib>
  22. #include "../lib/Connection.h"
  23. #include "../lib/VCMIDirs.h"
  24. #include "../hch/CMusicHandler.h"
  25. #include "../hch/CVideoHandler.h"
  26. #include "AdventureMapButton.h"
  27. #include "GUIClasses.h"
  28. #include "../hch/CCreatureHandler.h"
  29. #include "CPlayerInterface.h"
  30. #include "../CCallback.h"
  31. #include <boost/lexical_cast.hpp>
  32. /*
  33. * CPreGame.cpp, part of VCMI engine
  34. *
  35. * Authors: listed in file AUTHORS in main folder
  36. *
  37. * License: GNU General Public License v2.0 or later
  38. * Full text of license available in license.txt file, in main folder
  39. *
  40. */
  41. namespace fs = boost::filesystem;
  42. using boost::bind;
  43. using boost::ref;
  44. void startGame(StartInfo * options);
  45. CGPreGame * CGP;
  46. static const CMapHeader *curMap = NULL;
  47. static StartInfo *curOpts = NULL;
  48. static int playerColor, playerSerial;
  49. static std::string selectedName; //set when game is started/loaded
  50. CMenuScreen::CMenuScreen( EState which )
  51. {
  52. OBJ_CONSTRUCTION;
  53. bgAd = NULL;
  54. switch(which)
  55. {
  56. case mainMenu:
  57. {
  58. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[3].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[newGame])), 540, 10, "ZMENUNG.DEF", SDLK_n);
  59. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[4].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[loadGame])), 532, 132, "ZMENULG.DEF", SDLK_l);
  60. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[5].second, 0 /*cb*/, 524, 251, "ZMENUHS.DEF", SDLK_h);
  61. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[6].second, 0 /*cb*/, 557, 359, "ZMENUCR.DEF", SDLK_c);
  62. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[7].second, bind(std::exit, 0), 586, 468, "ZMENUQT.DEF", SDLK_ESCAPE);
  63. }
  64. break;
  65. case newGame:
  66. {
  67. bgAd = new CPicture(BitmapHandler::loadBitmap("ZNEWGAM.bmp"), 114, 312, true);
  68. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, newGame), 545, 4, "ZTSINGL.DEF", SDLK_s);
  69. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, 0 /*cb*/, 568, 120, "ZTMULTI.DEF", SDLK_m);
  70. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, 0 /*cb*/, 541, 233, "ZTCAMPN.DEF", SDLK_c);
  71. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  72. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  73. }
  74. break;
  75. case loadGame:
  76. {
  77. bgAd = new CPicture(BitmapHandler::loadBitmap("ZLOADGAM.bmp"), 114, 312, true);
  78. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, loadGame), 545, 4, "ZTSINGL.DEF", SDLK_s);
  79. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, 0 /*cb*/, 568, 120, "ZTMULTI.DEF", SDLK_m);
  80. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, 0 /*cb*/, 541, 233, "ZTCAMPN.DEF", SDLK_c);
  81. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  82. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  83. }
  84. break;
  85. }
  86. for(int i = 0; i < ARRAY_COUNT(buttons); i++)
  87. buttons[i]->hoverable = true;
  88. }
  89. CMenuScreen::~CMenuScreen()
  90. {
  91. }
  92. void CMenuScreen::showAll( SDL_Surface * to )
  93. {
  94. blitAt(CGP->mainbg, 0, 0, to);
  95. CIntObject::showAll(to);
  96. }
  97. void CMenuScreen::show( SDL_Surface * to )
  98. {
  99. CIntObject::show(to);
  100. CGI->videoh->update(pos.x + 8, pos.y + 105, screen, true, false);
  101. }
  102. void CMenuScreen::moveTo( CMenuScreen *next )
  103. {
  104. GH.popInt(this);
  105. GH.pushInt(next);
  106. }
  107. void CGPreGame::run()
  108. {
  109. GH.handleEvents();
  110. #ifdef _WIN32
  111. CGI->videoh->open("ACREDIT.SMK");
  112. #else
  113. CGI->videoh->open("ACREDIT.SMK", true, false);
  114. #endif
  115. GH.pushInt(scrs[mainMenu]);
  116. while(1)
  117. {
  118. CGI->curh->draw1();
  119. SDL_Flip(screen);
  120. CGI->curh->draw2();
  121. SDL_Delay(20); //give time for other apps
  122. GH.topInt()->show(screen);
  123. GH.updateTime();
  124. GH.handleEvents();
  125. }
  126. }
  127. CGPreGame::CGPreGame()
  128. {
  129. GH.defActionsDef = 63;
  130. CGP = this;
  131. mainbg = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  132. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  133. scrs[i] = new CMenuScreen((EState)i);
  134. }
  135. CGPreGame::~CGPreGame()
  136. {
  137. SDL_FreeSurface(mainbg);
  138. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  139. delete scrs[i];
  140. }
  141. void CGPreGame::openSel( EState type )
  142. {
  143. GH.pushInt(new CSelectionScreen(type));
  144. }
  145. void CGPreGame::loadGraphics()
  146. {
  147. victory = CDefHandler::giveDef("SCNRVICT.DEF");
  148. loss = CDefHandler::giveDef("SCNRLOSS.DEF");
  149. bonuses = CDefHandler::giveDef("SCNRSTAR.DEF");
  150. rHero = BitmapHandler::loadBitmap("HPSRAND1.bmp");
  151. rTown = BitmapHandler::loadBitmap("HPSRAND0.bmp");
  152. nHero = BitmapHandler::loadBitmap("HPSRAND6.bmp");
  153. nTown = BitmapHandler::loadBitmap("HPSRAND5.bmp");
  154. }
  155. void CGPreGame::disposeGraphics()
  156. {
  157. delete victory;
  158. delete loss;
  159. SDL_FreeSurface(rHero);
  160. SDL_FreeSurface(nHero);
  161. SDL_FreeSurface(rTown);
  162. SDL_FreeSurface(nTown);
  163. }
  164. CSelectionScreen::CSelectionScreen( EState Type )
  165. {
  166. OBJ_CONSTRUCTION_CAPTURING_ALL;
  167. IShowActivable::type = BLOCK_ADV_HOTKEYS;
  168. pos.w = 762;
  169. pos.h = 584;
  170. if(Type == saveGame)
  171. {
  172. center(pos);
  173. }
  174. else
  175. {
  176. pos.x = 3;
  177. pos.y = 6;
  178. bg = new CPicture(BitmapHandler::loadBitmap(rand()%2 ? "ZPIC1000.bmp" : "ZPIC1001.bmp"), -3, -6, true);
  179. }
  180. CGP->loadGraphics();
  181. type = Type;
  182. curOpts = &sInfo;
  183. sInfo.difficulty = 1;
  184. current = NULL;
  185. sInfo.mode = (Type == newGame ? 0 : 1);
  186. sInfo.turnTime = 0;
  187. curTab = NULL;
  188. card = new InfoCard(type);
  189. opt = new OptionsTab(type/*, sInfo*/);
  190. opt->recActions = DISPOSE;
  191. sel = new SelectionTab(type, bind(&CSelectionScreen::changeSelection, this, _1));
  192. sel->recActions = DISPOSE;
  193. switch(type)
  194. {
  195. case newGame:
  196. {
  197. card->difficulty->onChange = bind(&CSelectionScreen::difficultyChange, this, _1);
  198. card->difficulty->select(1, 0);
  199. AdventureMapButton *select = new AdventureMapButton(CGI->generaltexth->zelp[45], bind(&CSelectionScreen::toggleTab, this, sel), 411, 75, "GSPBUTT.DEF", SDLK_s);
  200. select->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL);
  201. AdventureMapButton *opts = new AdventureMapButton(CGI->generaltexth->zelp[46], bind(&CSelectionScreen::toggleTab, this, opt), 411, 503, "GSPBUTT.DEF", SDLK_a);
  202. opts->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL);
  203. AdventureMapButton *random = new AdventureMapButton(CGI->generaltexth->zelp[47], bind(&CSelectionScreen::toggleTab, this, sel), 411, 99, "GSPBUTT.DEF", SDLK_r);
  204. random->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL);
  205. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRBEG.DEF", SDLK_b);
  206. }
  207. break;
  208. case loadGame:
  209. sel->recActions = 255;
  210. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRLOD.DEF", SDLK_l);
  211. break;
  212. case saveGame:
  213. sel->recActions = 255;
  214. start = new AdventureMapButton("", CGI->generaltexth->zelp[103].second, bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRSAV.DEF");
  215. break;
  216. }
  217. start->assignedKeys.insert(SDLK_RETURN);
  218. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 581, 529, "SCNRBACK.DEF", SDLK_ESCAPE);
  219. }
  220. CSelectionScreen::~CSelectionScreen()
  221. {
  222. curMap = NULL;
  223. curOpts = NULL;
  224. playerSerial = playerColor = -1;
  225. }
  226. void CSelectionScreen::toggleTab(CIntObject *tab)
  227. {
  228. if(curTab && curTab->active)
  229. {
  230. curTab->deactivate();
  231. curTab->recActions = DISPOSE;
  232. }
  233. if(curTab != tab)
  234. {
  235. tab->recActions = 255;
  236. tab->activate();
  237. curTab = tab;
  238. }
  239. else
  240. {
  241. curTab = NULL;
  242. };
  243. GH.totalRedraw();
  244. }
  245. void CSelectionScreen::changeSelection( const CMapInfo *to )
  246. {
  247. curMap = current = to;
  248. if(to && type == loadGame)
  249. curOpts->difficulty = to->seldiff;
  250. updateStartInfo(to);
  251. card->changeSelection(to);
  252. opt->changeSelection(to);
  253. }
  254. void CSelectionScreen::updateStartInfo( const CMapInfo * to )
  255. {
  256. sInfo.playerInfos.clear();
  257. if(!to)
  258. return;
  259. sInfo.playerInfos.resize(to->playerAmnt);
  260. sInfo.mapname = to->filename;
  261. playerSerial = playerColor = -1;
  262. int serialC=0;
  263. for (int i = 0; i < PLAYER_LIMIT; i++)
  264. {
  265. const PlayerInfo &pinfo = to->players[i];
  266. //neither computer nor human can play - no player
  267. if (!(pinfo.canComputerPlay || pinfo.canComputerPlay))
  268. continue;
  269. PlayerSettings &pset = sInfo.playerInfos[serialC];
  270. pset.color = i;
  271. pset.serial = serialC++;
  272. if (pinfo.canHumanPlay && playerColor < 0)
  273. {
  274. pset.name = CGI->generaltexth->allTexts[434]; //Player
  275. pset.human = true;
  276. playerColor = i;
  277. playerSerial = pset.serial;
  278. }
  279. else
  280. {
  281. pset.name = CGI->generaltexth->allTexts[468];//Computer
  282. pset.human = false;
  283. }
  284. 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
  285. {
  286. if((1 << j) & pinfo.allowedFactions)
  287. {
  288. if (pset.castle >= 0) //we've already assigned a castle and another one is possible -> set random and let player choose
  289. pset.castle = -1; //breaks
  290. if (pset.castle == -2) //first available castle - pick
  291. pset.castle = j;
  292. }
  293. }
  294. if ((pinfo.generateHeroAtMainTown || to->version==CMapHeader::RoE) && pinfo.hasMainTown //we will generate hero in front of main town
  295. || pinfo.p8) //random hero
  296. pset.hero = -1;
  297. else
  298. pset.hero = -2;
  299. if(pinfo.mainHeroName.length())
  300. {
  301. pset.heroName = pinfo.mainHeroName;
  302. if((pset.heroPortrait = pinfo.mainHeroPortrait) == 255)
  303. pset.heroPortrait = pinfo.p9;
  304. }
  305. pset.handicap = 0;
  306. }
  307. }
  308. void CSelectionScreen::startGame()
  309. {
  310. if(type != saveGame)
  311. {
  312. if(!current)
  313. return;
  314. selectedName = sInfo.mapname;
  315. StartInfo *si = new StartInfo(sInfo);
  316. GH.popIntTotally(this);
  317. GH.popIntTotally(GH.topInt());
  318. curMap = NULL;
  319. curOpts = NULL;
  320. ::startGame(si);
  321. delete si; //rather won't be called...
  322. }
  323. else
  324. {
  325. if(!(sel && sel->txt && sel->txt->text.size()))
  326. return;
  327. selectedName = GVCMIDirs.UserPath + "/Games/" + sel->txt->text + ".vlgm1";
  328. LOCPLINT->cb->save(sel->txt->text);
  329. GH.popIntTotally(this);
  330. }
  331. }
  332. void CSelectionScreen::difficultyChange( int to )
  333. {
  334. assert(type == newGame);
  335. sInfo.difficulty = to;
  336. GH.totalRedraw();
  337. }
  338. // A new size filter (Small, Medium, ...) has been selected. Populate
  339. // selMaps with the relevant data.
  340. void SelectionTab::filter( int size, bool selectFirst )
  341. {
  342. curItems.clear();
  343. for (size_t i=0; i<allItems.size(); i++)
  344. if( allItems[i].version && (!size || allItems[i].width == size))
  345. curItems.push_back(&allItems[i]);
  346. if(curItems.size())
  347. {
  348. slider->block(false);
  349. slider->setAmount(curItems.size());
  350. sort();
  351. if(selectFirst)
  352. {
  353. slider->moveTo(0);
  354. onSelect(curItems[0]);
  355. }
  356. selectAbs(0);
  357. }
  358. else
  359. {
  360. slider->block(true);
  361. onSelect(NULL);
  362. }
  363. }
  364. void SelectionTab::getFiles(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext)
  365. {
  366. if(!boost::filesystem::exists(dirname))
  367. {
  368. tlog1 << "Cannot find " << dirname << " directory!\n";
  369. }
  370. fs::path tie(dirname);
  371. fs::directory_iterator end_iter;
  372. for ( fs::directory_iterator file (tie); file!=end_iter; ++file )
  373. {
  374. if(fs::is_regular_file(file->status())
  375. && boost::ends_with(file->path().filename(), ext))
  376. {
  377. out.resize(out.size()+1);
  378. out.back().date = fs::last_write_time(file->path());
  379. out.back().name = file->path().string();
  380. }
  381. }
  382. allItems.resize(out.size());
  383. }
  384. void SelectionTab::parseMaps(std::vector<FileInfo> &files, int start, int threads)
  385. {
  386. int read=0;
  387. unsigned char mapBuffer[1500];
  388. while(start < allItems.size())
  389. {
  390. gzFile tempf = gzopen(files[start].name.c_str(),"rb");
  391. read = gzread(tempf, mapBuffer, 1500);
  392. gzclose(tempf);
  393. if(read < 50 || !mapBuffer[4])
  394. {
  395. tlog3 << "\t\tWarning: corrupted map file: " << files[start].name << std::endl;
  396. }
  397. else //valid map
  398. {
  399. allItems[start].init(files[start].name, mapBuffer);
  400. //allItems[start].date = "DATEDATE";// files[start].date;
  401. }
  402. start += threads;
  403. }
  404. }
  405. void SelectionTab::parseGames(std::vector<FileInfo> &files)
  406. {
  407. for(int i=0; i<files.size(); i++)
  408. {
  409. CLoadFile lf(files[i].name);
  410. if(!lf.sfile)
  411. continue;
  412. ui8 sign[8];
  413. lf >> sign;
  414. if(std::memcmp(sign,"VCMISVG",7))
  415. {
  416. tlog1 << files[i].name << " is not a correct savefile!" << std::endl;
  417. continue;
  418. }
  419. lf >> static_cast<CMapHeader&>(allItems[i]) >> allItems[i].seldiff;
  420. allItems[i].filename = files[i].name;
  421. allItems[i].countPlayers();
  422. allItems[i].date = std::asctime(std::localtime(&files[i].date));
  423. }
  424. }
  425. SelectionTab::SelectionTab(EState Type, const boost::function<void(CMapInfo *)> &OnSelect)
  426. :onSelect(OnSelect)
  427. {
  428. OBJ_CONSTRUCTION;
  429. selectionPos = 0;
  430. used = LCLICK | WHEEL | KEYBOARD | DOUBLECLICK;
  431. slider = NULL;
  432. txt = NULL;
  433. type = Type;
  434. bg = new CPicture(BitmapHandler::loadBitmap("SCSELBCK.bmp"), 0, 0, true);
  435. pos.w = bg->pos.w;
  436. pos.h = bg->pos.h;
  437. std::vector<FileInfo> toParse;
  438. switch(type)
  439. {
  440. case newGame:
  441. getFiles(toParse, DATA_DIR "/Maps", "h3m");
  442. parseMaps(toParse);
  443. positions = 18;
  444. break;
  445. case loadGame:
  446. case saveGame:
  447. getFiles(toParse, GVCMIDirs.UserPath + "/Games", "vlgm1");
  448. parseGames(toParse);
  449. if(type == loadGame)
  450. {
  451. positions = 18;
  452. }
  453. else
  454. {
  455. positions = 16;
  456. }
  457. if(type == saveGame)
  458. txt = new CTextInput(Rect(32, 539, 350, 20), Point(-32, -25), "GSSTRIP.bmp", 0);
  459. break;
  460. default:
  461. assert(0);
  462. }
  463. //size filter buttons
  464. {
  465. int sizes[] = {36, 72, 108, 144, 0};
  466. const char * names[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"};
  467. for(int i = 0; i < 5; i++)
  468. new AdventureMapButton("", CGI->generaltexth->zelp[54+i].second, bind(&SelectionTab::filter, this, sizes[i], true), 158 + 47*i, 46, names[i]);
  469. }
  470. {
  471. int xpos[] = {23, 55, 88, 121, 306, 339};
  472. const char * names[] = {"SCBUTT1.DEF", "SCBUTT2.DEF", "SCBUTCP.DEF", "SCBUTT3.DEF", "SCBUTT4.DEF", "SCBUTT5.DEF"};
  473. for(int i = 0; i < 6; i++)
  474. new AdventureMapButton("", CGI->generaltexth->zelp[107+i].second, bind(&SelectionTab::sortBy, this, i), xpos[i], 86, names[i]);
  475. }
  476. slider = new CSlider(372, 86, type != saveGame ? 480 : 430, bind(&SelectionTab::sliderMove, this, _1), positions, curItems.size(), 0, false, 1);
  477. format = CDefHandler::giveDef("SCSELC.DEF");
  478. sortingBy = _format;
  479. ascending = true;
  480. filter(0);
  481. //select(0);
  482. switch(type)
  483. {
  484. case newGame:
  485. selectFName(DATA_DIR "/Maps/Arrogance.h3m");
  486. break;
  487. case loadGame:
  488. select(0);
  489. break;
  490. case saveGame:;
  491. if(selectedName.size())
  492. {
  493. if(selectedName[0] == 'M')
  494. txt->setText("NEWGAME");
  495. else
  496. selectFName(selectedName);
  497. }
  498. }
  499. }
  500. SelectionTab::~SelectionTab()
  501. {
  502. delete format;
  503. }
  504. void SelectionTab::sortBy( int criteria )
  505. {
  506. if(criteria == sortingBy)
  507. {
  508. ascending = !ascending;
  509. }
  510. else
  511. {
  512. sortingBy = (ESortBy)criteria;
  513. ascending = true;
  514. }
  515. sort();
  516. selectAbs(0);
  517. }
  518. void SelectionTab::sort()
  519. {
  520. if(sortingBy != _name)
  521. std::stable_sort(curItems.begin(),curItems.end(),mapSorter(_name));
  522. std::stable_sort(curItems.begin(),curItems.end(),mapSorter(sortingBy));
  523. if(!ascending)
  524. std::reverse(curItems.begin(), curItems.end());
  525. redraw();
  526. }
  527. void SelectionTab::select( int position )
  528. {
  529. if(!curItems.size()) return;
  530. // New selection. py is the index in curItems.
  531. int py = position + slider->value;
  532. amax(py, 0);
  533. amin(py, curItems.size()-1);
  534. selectionPos = py;
  535. if(position < 0)
  536. slider->moveTo(slider->value + position);
  537. else if(position >= positions)
  538. slider->moveTo(slider->value + position - positions + 1);
  539. onSelect(curItems[py]);
  540. }
  541. void SelectionTab::selectAbs( int position )
  542. {
  543. select(position - slider->value);
  544. }
  545. int SelectionTab::getPosition( int x, int y )
  546. {
  547. return -1;
  548. }
  549. void SelectionTab::sliderMove( int slidPos )
  550. {
  551. if(!slider) return; //ignore spurious call when slider is being created
  552. redraw();
  553. }
  554. // Display the tab with the scenario names
  555. //
  556. // elemIdx is the index of the maps or saved game to display on line 0
  557. // slider->capacity contains the number of available screen lines
  558. // slider->positionsAmnt is the number of elements after filtering
  559. void SelectionTab::printMaps(SDL_Surface *to)
  560. {
  561. int elemIdx = slider->value;
  562. // Display all elements if there's enough space
  563. //if(slider->amount < slider->capacity)
  564. // elemIdx = 0;
  565. SDL_Color nasz;
  566. #define POS(xx, yy) pos.x + xx, pos.y + yy + line*25
  567. for (int line = 0; line < positions && elemIdx < curItems.size(); elemIdx++, line++)
  568. {
  569. CMapInfo* curMap = curItems[elemIdx];
  570. if (elemIdx == selectionPos)
  571. nasz=tytulowy;
  572. else
  573. nasz=zwykly;
  574. std::ostringstream ostr(std::ostringstream::out);
  575. ostr << curMap->playerAmnt << "/" << curMap->humenPlayers;
  576. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, nasz, to);
  577. std::string temp2 = "C";
  578. switch (curMap->width)
  579. {
  580. case 36:
  581. temp2="S";
  582. break;
  583. case 72:
  584. temp2="M";
  585. break;
  586. case 108:
  587. temp2="L";
  588. break;
  589. case 144:
  590. temp2="XL";
  591. break;
  592. }
  593. CSDL_Ext::printAtMiddle(temp2, POS(70, 128), FONT_SMALL, nasz, to);
  594. int temp=-1;
  595. switch (curMap->version)
  596. {
  597. case CMapHeader::RoE:
  598. temp=0;
  599. break;
  600. case CMapHeader::AB:
  601. temp=1;
  602. break;
  603. case CMapHeader::SoD:
  604. temp=2;
  605. break;
  606. case CMapHeader::WoG:
  607. temp=3;
  608. break;
  609. default:
  610. // Unknown version. Be safe and ignore that map
  611. tlog2 << "Warning: " << curMap->filename << " has wrong version!\n";
  612. continue;
  613. }
  614. blitAt(format->ourImages[temp].bitmap, POS(88, 117), to);
  615. if (type == newGame) {
  616. if (!curMap->name.length())
  617. curMap->name = "Unnamed";
  618. CSDL_Ext::printAtMiddle(curMap->name, POS(213, 128), FONT_SMALL, nasz, to);
  619. } else
  620. CSDL_Ext::printAtMiddle(fs::basename(curMap->filename), POS(213, 128), FONT_SMALL, nasz, to);
  621. if (curMap->victoryCondition.condition == winStandard)
  622. temp = 11;
  623. else
  624. temp = curMap->victoryCondition.condition;
  625. blitAt(CGP->victory->ourImages[temp].bitmap, POS(306, 117), to);
  626. if (curMap->lossCondition.typeOfLossCon == lossStandard)
  627. temp=3;
  628. else
  629. temp=curMap->lossCondition.typeOfLossCon;
  630. blitAt(CGP->loss->ourImages[temp].bitmap, POS(339, 117), to);
  631. }
  632. #undef POS
  633. }
  634. void SelectionTab::showAll( SDL_Surface * to )
  635. {
  636. CIntObject::showAll(to);
  637. printMaps(to);
  638. int txtid;
  639. switch(type) {
  640. case newGame: txtid = 229; break;
  641. case loadGame: txtid = 230; break;
  642. case saveGame: txtid = 231; break;
  643. }
  644. CSDL_Ext::printAtMiddle(CGI->generaltexth->arraytxt[txtid], pos.x+205, pos.y+28, FONT_MEDIUM, tytulowy, to); //Select a Scenario to Play
  645. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[510], pos.x+87, pos.y+62, FONT_SMALL, tytulowy, to); //Map sizes
  646. }
  647. void SelectionTab::clickLeft( tribool down, bool previousState )
  648. {
  649. if(down)
  650. {
  651. int line = getLine();
  652. if(line != -1)
  653. select(line);
  654. }
  655. }
  656. void SelectionTab::wheelScrolled( bool down, bool in )
  657. {
  658. slider->moveTo(slider->value + 3 * (down ? +1 : -1));
  659. //select(selectionPos - slider->value + (down ? +1 : -1));
  660. }
  661. void SelectionTab::keyPressed( const SDL_KeyboardEvent & key )
  662. {
  663. if(key.state != SDL_PRESSED) return;
  664. int moveBy = 0;
  665. switch(key.keysym.sym)
  666. {
  667. case SDLK_UP:
  668. moveBy = -1;
  669. break;
  670. case SDLK_DOWN:
  671. moveBy = +1;
  672. break;
  673. case SDLK_PAGEUP:
  674. moveBy = -positions+1;
  675. break;
  676. case SDLK_PAGEDOWN:
  677. moveBy = +positions-1;
  678. break;
  679. case SDLK_HOME:
  680. select(-slider->value);
  681. return;
  682. case SDLK_END:
  683. select(curItems.size() - slider->value);
  684. return;
  685. default:
  686. return;
  687. }
  688. select(selectionPos - slider->value + moveBy);
  689. }
  690. void SelectionTab::onDoubleClick()
  691. {
  692. if(getLine() != -1) //double clicked scenarios list
  693. {
  694. //act as if start button was pressed
  695. (static_cast<CSelectionScreen*>(parent))->start->callback();
  696. }
  697. }
  698. int SelectionTab::getLine()
  699. {
  700. int line = -1;
  701. Point clickPos(GH.current->button.x, GH.current->button.y);
  702. clickPos = clickPos - pos.topLeft();
  703. if (clickPos.y > 115 && clickPos.y < 564 && clickPos.x > 22 && clickPos.x < 371)
  704. {
  705. line = (clickPos.y-115) / 25; //which line
  706. }
  707. return line;
  708. }
  709. void SelectionTab::selectFName( const std::string &fname )
  710. {
  711. for(int i = curItems.size() - 1; i >= 0; i--)
  712. {
  713. if(curItems[i]->filename == fname)
  714. {
  715. slider->moveTo(i);
  716. selectAbs(i);
  717. return;
  718. }
  719. }
  720. selectAbs(0);
  721. }
  722. InfoCard::InfoCard( EState Type )
  723. {
  724. OBJ_CONSTRUCTION;
  725. pos.x += 393;
  726. used = RCLICK;
  727. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  728. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  729. type = Type;
  730. bg = new CPicture(BitmapHandler::loadBitmap("GSELPOP1.bmp"), 0, 0, true);
  731. pos.w = bg->pos.w;
  732. pos.h = bg->pos.h;
  733. difficulty = new CHighlightableButtonsGroup(0);
  734. {
  735. static const char *difButns[] = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  736. BLOCK_CAPTURING;
  737. for(int i = 0; i < 5; i++)
  738. {
  739. difficulty->addButton(new CHighlightableButton("", CGI->generaltexth->zelp[24+i].second, 0, 110 + i*32, 450, difButns[i], i));
  740. difficulty->buttons.back()->pos += pos.topLeft();
  741. }
  742. }
  743. if(type != newGame)
  744. difficulty->block(true);
  745. }
  746. InfoCard::~InfoCard()
  747. {
  748. delete sizes;
  749. delete sFlags;
  750. }
  751. void InfoCard::showAll( SDL_Surface * to )
  752. {
  753. CIntObject::showAll(to);
  754. //blit texts
  755. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 24, 400, FONT_SMALL, zwykly, to); //Allies
  756. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 190, 400, FONT_SMALL, zwykly, to); //Enemies
  757. printAtLoc(CGI->generaltexth->allTexts[494], 33, 430, FONT_SMALL, tytulowy, to);//"Map Diff:"
  758. printAtLoc(CGI->generaltexth->allTexts[492] + ":", 133,430, FONT_SMALL, tytulowy, to); //player difficulty
  759. printAtLoc(CGI->generaltexth->allTexts[218] + ":", 290,430, FONT_SMALL, tytulowy, to); //"Rating:"
  760. printAtLoc(CGI->generaltexth->allTexts[495], 26, 22, FONT_SMALL, tytulowy, to); //Scenario Name:
  761. printAtLoc(CGI->generaltexth->allTexts[496], 26, 132, FONT_SMALL, tytulowy, to); //Scenario Description:
  762. printAtLoc(CGI->generaltexth->allTexts[497], 26, 283, FONT_SMALL, tytulowy, to); //Victory Condition:
  763. printAtLoc(CGI->generaltexth->allTexts[498], 26, 339, FONT_SMALL, tytulowy, to); //Loss Condition:
  764. if(curMap)
  765. {
  766. if(type != newGame)
  767. {
  768. for (int i = 0; i < difficulty->buttons.size(); i++)
  769. {
  770. //if(i == curMap->difficulty)
  771. // difficulty->buttons[i]->state = 3;
  772. //else
  773. // difficulty->buttons[i]->state = 2;
  774. difficulty->buttons[i]->showAll(to);
  775. }
  776. }
  777. int temp = curMap->victoryCondition.condition+1;
  778. if (temp>20) temp=0;
  779. std::string sss = CGI->generaltexth->victoryConditions[temp];
  780. if (temp && curMap->victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  781. printAtLoc(sss, 60, 307, FONT_SMALL, zwykly, to);
  782. temp = curMap->lossCondition.typeOfLossCon+1;
  783. if (temp>20) temp=0;
  784. sss = CGI->generaltexth->lossCondtions[temp];
  785. printAtLoc(sss, 60, 366, FONT_SMALL, zwykly, to);
  786. //blit description
  787. std::vector<std::string> *desc = CMessage::breakText(curMap->description,52);
  788. for (int i=0;i<desc->size();i++)
  789. printAtLoc((*desc)[i], 26, 149 + i*16, FONT_SMALL, zwykly, to);
  790. delete desc;
  791. if (curMap->name.length())
  792. printAtLoc(curMap->name, 26, 39, FONT_BIG, tytulowy, to);
  793. else
  794. printAtLoc("Unnamed", 26, 39, FONT_BIG, tytulowy, to);
  795. assert(curMap->difficulty <= 4);
  796. std::string &diff = CGI->generaltexth->arraytxt[142 + curMap->difficulty];
  797. printAtMiddleLoc(diff, 62, 472, FONT_SMALL, zwykly, to);
  798. switch (curMap->width)
  799. {
  800. case 36:
  801. temp=0;
  802. break;
  803. case 72:
  804. temp=1;
  805. break;
  806. case 108:
  807. temp=2;
  808. break;
  809. case 144:
  810. temp=3;
  811. break;
  812. default:
  813. temp=4;
  814. break;
  815. }
  816. blitAtLoc(sizes->ourImages[temp].bitmap, 318, 22, to);
  817. temp = curMap->victoryCondition.condition;
  818. if (temp>12) temp=11;
  819. blitAtLoc(CGP->victory->ourImages[temp].bitmap, 24, 302, to); //victory cond descr
  820. temp=curMap->lossCondition.typeOfLossCon;
  821. if (temp>12) temp=3;
  822. blitAtLoc(CGP->loss->ourImages[temp].bitmap, 24, 359, to); //loss cond
  823. if(type == loadGame)
  824. printToLoc((static_cast<const CMapInfo*>(curMap))->date,308,34, FONT_SMALL, zwykly, to);
  825. //print flags
  826. int fx=64, ex=244, myT;
  827. //if (curMap->howManyTeams)
  828. myT = curMap->players[playerColor].team;
  829. //else
  830. // myT = -1;
  831. for (std::vector<PlayerSettings>::const_iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  832. {
  833. int *myx = ((i->color == playerColor || curMap->players[i->color].team == myT) ? &fx : &ex);
  834. blitAtLoc(sFlags->ourImages[i->color].bitmap, *myx, 399, to);
  835. *myx += sFlags->ourImages[i->color].bitmap->w;
  836. }
  837. std::string tob;
  838. switch (curOpts->difficulty)
  839. {
  840. case 0:
  841. tob="80%";
  842. break;
  843. case 1:
  844. tob="100%";
  845. break;
  846. case 2:
  847. tob="130%";
  848. break;
  849. case 3:
  850. tob="160%";
  851. break;
  852. case 4:
  853. tob="200%";
  854. break;
  855. }
  856. printAtMiddleLoc(tob, 311, 472, FONT_SMALL, zwykly, to);
  857. }
  858. }
  859. void InfoCard::changeSelection( const CMapInfo *to )
  860. {
  861. if(to && type != newGame)
  862. difficulty->select(curOpts->difficulty, 0);
  863. GH.totalRedraw();
  864. }
  865. void InfoCard::clickRight( tribool down, bool previousState )
  866. {
  867. static const Rect flagArea(19, 397, 335, 23);
  868. if(down && curMap && isItInLoc(flagArea, GH.current->motion.x, GH.current->motion.y))
  869. showTeamsPopup();
  870. }
  871. void InfoCard::showTeamsPopup()
  872. {
  873. SDL_Surface *bmp = CMessage::drawBox1(256, 90 + 50 * curMap->howManyTeams);
  874. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[657], 128, 30, FONT_MEDIUM, tytulowy, bmp); //{Team Alignments}
  875. for(int i = 0; i < curMap->howManyTeams; i++)
  876. {
  877. std::vector<ui8> flags;
  878. std::string hlp = CGI->generaltexth->allTexts[656]; //Team %d
  879. hlp.replace(hlp.find("%d"), 2, boost::lexical_cast<std::string>(i+1));
  880. CSDL_Ext::printAtMiddle(hlp, 128, 65 + 50*i, FONT_SMALL, zwykly, bmp);
  881. for(int j = 0; j < PLAYER_LIMIT; j++)
  882. if((curMap->players[j].canHumanPlay || curMap->players[j].canComputerPlay)
  883. && curMap->players[j].team == i)
  884. flags.push_back(j);
  885. int curx = 128 - 9*flags.size();
  886. for(int j = 0; j < flags.size(); j++)
  887. {
  888. blitAt(sFlags->ourImages[flags[j]].bitmap, curx, 75 + 50*i, bmp);
  889. curx += 18;
  890. }
  891. }
  892. GH.pushInt(new CInfoPopup(bmp, true));
  893. }
  894. OptionsTab::OptionsTab( EState Type)
  895. :type(Type)
  896. {
  897. OBJ_CONSTRUCTION;
  898. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTBK.bmp"), 0, 0, true);
  899. pos = bg->pos;
  900. if(type == newGame)
  901. turnDuration = new CSlider(55, 551, 194, bind(&OptionsTab::setTurnLength, this, _1), 1, 11, 11, true, 1);
  902. }
  903. OptionsTab::~OptionsTab()
  904. {
  905. }
  906. void OptionsTab::showAll( SDL_Surface * to )
  907. {
  908. CIntObject::showAll(to);
  909. printAtMiddleLoc(CGI->generaltexth->allTexts[515], 222, 30, FONT_BIG, tytulowy, to);
  910. printAtMiddleWBLoc(CGI->generaltexth->allTexts[516], 222, 58, FONT_SMALL, 55, zwykly, to); //Select starting options, handicap, and name for each player in the game.
  911. printAtMiddleWBLoc(CGI->generaltexth->allTexts[517], 107, 102, FONT_SMALL, 14, tytulowy, to); //Player Name Handicap Type
  912. printAtMiddleWBLoc(CGI->generaltexth->allTexts[518], 197, 102, FONT_SMALL, 10, tytulowy, to); //Starting Town
  913. printAtMiddleWBLoc(CGI->generaltexth->allTexts[519], 273, 102, FONT_SMALL, 10, tytulowy, to); //Starting Hero
  914. printAtMiddleWBLoc(CGI->generaltexth->allTexts[520], 349, 102, FONT_SMALL, 10, tytulowy, to); //Starting Bonus
  915. printAtMiddleLoc(CGI->generaltexth->allTexts[521], 222, 538, FONT_SMALL, tytulowy, to); // Player Turn Duration
  916. if(curOpts->turnTime)
  917. {
  918. std::ostringstream os;
  919. os << (int)curOpts->turnTime << " Minutes";
  920. printAtMiddleLoc(os.str(), 319,559, FONT_SMALL, zwykly, to);
  921. }
  922. else
  923. printAtMiddleLoc("Unlimited",319,559, FONT_SMALL, zwykly, to);
  924. }
  925. void OptionsTab::nextCastle( int player, int dir )
  926. {
  927. PlayerSettings &s = curOpts->playerInfos[player];
  928. si32 &cur = s.castle;
  929. ui32 allowed = curMap->players[s.color].allowedFactions;
  930. if (cur == -2) //no castle - no change
  931. return;
  932. if (cur == -1) //random => first/last available
  933. {
  934. int pom = (dir>0) ? (0) : (F_NUMBER-1); // last or first
  935. for (;pom >= 0 && pom < F_NUMBER; pom+=dir)
  936. {
  937. if((1 << pom) & allowed)
  938. {
  939. cur=pom;
  940. break;
  941. }
  942. }
  943. }
  944. else // next/previous available
  945. {
  946. for (;;)
  947. {
  948. cur+=dir;
  949. if ((1 << cur) & allowed)
  950. break;
  951. if (cur >= F_NUMBER || cur<0)
  952. {
  953. double p1 = log((double)allowed) / log(2.0f)+0.000001f;
  954. double check = p1 - ((int)p1);
  955. if (check < 0.001)
  956. cur = (int)p1;
  957. else
  958. cur = -1;
  959. break;
  960. }
  961. }
  962. }
  963. if(s.hero >= 0)
  964. s.hero = -1;
  965. if(cur < 0 && s.bonus == bresource)
  966. s.bonus = brandom;
  967. entries[player]->selectButtons();
  968. redraw();
  969. }
  970. void OptionsTab::nextHero( int player, int dir )
  971. {
  972. PlayerSettings &s = curOpts->playerInfos[player];
  973. int old = s.hero;
  974. if (s.castle < 0 || !s.human || s.hero == -2)
  975. return;
  976. if (s.hero == -1) //random => first/last available
  977. {
  978. int max = (s.castle*HEROES_PER_TYPE*2+15),
  979. min = (s.castle*HEROES_PER_TYPE*2);
  980. s.hero = nextAllowedHero(min,max,0,dir);
  981. }
  982. else
  983. {
  984. if(dir > 0)
  985. s.hero = nextAllowedHero(s.hero,(s.castle*HEROES_PER_TYPE*2+16),1,dir);
  986. else
  987. s.hero = nextAllowedHero(s.castle*HEROES_PER_TYPE*2-1,s.hero,1,dir);
  988. }
  989. if(old != s.hero)
  990. {
  991. usedHeroes.erase(old);
  992. usedHeroes.insert(s.hero);
  993. redraw();
  994. }
  995. }
  996. int OptionsTab::nextAllowedHero( int min, int max, int incl, int dir )
  997. {
  998. if(dir>0)
  999. {
  1000. for(int i=min+incl; i<=max-incl; i++)
  1001. if(canUseThisHero(i))
  1002. return i;
  1003. }
  1004. else
  1005. {
  1006. for(int i=max-incl; i>=min+incl; i--)
  1007. if(canUseThisHero(i))
  1008. return i;
  1009. }
  1010. return -1;
  1011. }
  1012. bool OptionsTab::canUseThisHero( int ID )
  1013. {
  1014. //for(int i=0;i<CPG->ret.playerInfos.size();i++)
  1015. // if(CPG->ret.playerInfos[i].hero==ID) //hero is already taken
  1016. // return false;
  1017. return !vstd::contains(usedHeroes, ID) && curMap->allowedHeroes[ID];
  1018. }
  1019. void OptionsTab::nextBonus( int player, int dir )
  1020. {
  1021. PlayerSettings &s = curOpts->playerInfos[player];
  1022. si8 &ret = s.bonus += dir;
  1023. if (s.hero==-2 && !curMap->players[s.color].heroesNames.size() && ret==bartifact) //no hero - can't be artifact
  1024. {
  1025. if (dir<0)
  1026. ret=brandom;
  1027. else ret=bgold;
  1028. }
  1029. if(ret > bresource)
  1030. ret = brandom;
  1031. if(ret < brandom)
  1032. ret = bresource;
  1033. if (s.castle==-1 && ret==bresource) //random castle - can't be resource
  1034. {
  1035. if (dir<0)
  1036. ret=bgold;
  1037. else ret=brandom;
  1038. }
  1039. redraw();
  1040. }
  1041. void OptionsTab::changeSelection( const CMapHeader *to )
  1042. {
  1043. for(int i = 0; i < entries.size(); i++)
  1044. {
  1045. children -= entries[i];
  1046. delete entries[i];
  1047. }
  1048. entries.clear();
  1049. usedHeroes.clear();
  1050. OBJ_CONSTRUCTION;
  1051. for(int i = 0; i < curOpts->playerInfos.size(); i++)
  1052. {
  1053. entries.push_back(new PlayerOptionsEntry(this, curOpts->playerInfos[i]));
  1054. const std::vector<SheroName> &heroes = curMap->players[curOpts->playerInfos[i].color].heroesNames;
  1055. for(size_t hi=0; hi<heroes.size(); hi++)
  1056. usedHeroes.insert(heroes[hi].heroID);
  1057. }
  1058. }
  1059. void OptionsTab::setTurnLength( int npos )
  1060. {
  1061. static const int times[] = {1, 2, 4, 6, 8, 10, 15, 20, 25, 30, 0};
  1062. amin(npos, ARRAY_COUNT(times) - 1);
  1063. curOpts->turnTime = times[npos];
  1064. redraw();
  1065. }
  1066. void OptionsTab::flagPressed( int player )
  1067. {
  1068. if(player == playerSerial) //that color is already selected, no action needed
  1069. return;
  1070. PlayerSettings &s = curOpts->playerInfos[player],
  1071. &old = curOpts->playerInfos[playerSerial];
  1072. std::swap(old.human, s.human);
  1073. std::swap(old.name, s.name);
  1074. playerColor = s.color;
  1075. if(!entries[playerSerial]->fixedHero)
  1076. old.hero = -1;
  1077. playerSerial = player;
  1078. entries[s.serial]->selectButtons();
  1079. entries[old.serial]->selectButtons();
  1080. GH.totalRedraw();
  1081. }
  1082. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry( OptionsTab *owner, PlayerSettings &S)
  1083. :s(S)
  1084. {
  1085. OBJ_CONSTRUCTION;
  1086. defActions |= SHARE_POS;
  1087. pos = parent->pos + Point(54, 122 + s.serial*50);
  1088. static const char *flags[] = {"AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  1089. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"};
  1090. static const char *bgs[] = {"ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  1091. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"};
  1092. bg = new CPicture(BitmapHandler::loadBitmap(bgs[s.color]), 0, 0, true);
  1093. if(owner->type == newGame)
  1094. {
  1095. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[132], bind(&OptionsTab::nextCastle, owner, s.serial, -1), 107, 5, "ADOPLFA.DEF");
  1096. btns[1] = new AdventureMapButton(CGI->generaltexth->zelp[133], bind(&OptionsTab::nextCastle, owner, s.serial, +1), 168, 5, "ADOPRTA.DEF");
  1097. btns[2] = new AdventureMapButton(CGI->generaltexth->zelp[148], bind(&OptionsTab::nextHero, owner, s.serial, -1), 183, 5, "ADOPLFA.DEF");
  1098. btns[3] = new AdventureMapButton(CGI->generaltexth->zelp[149], bind(&OptionsTab::nextHero, owner, s.serial, +1), 244, 5, "ADOPRTA.DEF");
  1099. btns[4] = new AdventureMapButton(CGI->generaltexth->zelp[164], bind(&OptionsTab::nextBonus, owner, s.serial, -1), 259, 5, "ADOPLFA.DEF");
  1100. btns[5] = new AdventureMapButton(CGI->generaltexth->zelp[165], bind(&OptionsTab::nextBonus, owner, s.serial, +1), 320, 5, "ADOPRTA.DEF");
  1101. }
  1102. else
  1103. for(int i = 0; i < 6; i++)
  1104. btns[i] = NULL;
  1105. fixedHero = s.hero != -1; //if we doesn't start with "random hero" it must be fixed or none
  1106. selectButtons(false);
  1107. if(owner->type != scenarioInfo && curMap->players[s.color].canHumanPlay)
  1108. {
  1109. flag = new AdventureMapButton(CGI->generaltexth->zelp[180], bind(&OptionsTab::flagPressed, owner, s.serial), -43, 2, flags[s.color]);
  1110. flag->hoverable = true;
  1111. }
  1112. else
  1113. flag = NULL;
  1114. defActions &= ~SHARE_POS;
  1115. town = new SelectedBox(TOWN, s.serial);
  1116. town->pos += pos + Point(119, 2);
  1117. hero = new SelectedBox(HERO, s.serial);
  1118. hero->pos += pos + Point(195, 2);
  1119. bonus = new SelectedBox(BONUS, s.serial);
  1120. bonus->pos += pos + Point(271, 2);
  1121. }
  1122. void OptionsTab::PlayerOptionsEntry::showAll( SDL_Surface * to )
  1123. {
  1124. CIntObject::showAll(to);
  1125. printAtMiddleLoc(s.name, 55, 10, FONT_SMALL, zwykly, to);
  1126. }
  1127. void OptionsTab::PlayerOptionsEntry::selectButtons(bool onlyHero)
  1128. {
  1129. if(!btns[0])
  1130. return;
  1131. if(!onlyHero && s.castle != -1)
  1132. {
  1133. btns[0]->disable();
  1134. btns[1]->disable();
  1135. }
  1136. if(fixedHero || !s.human || s.castle < 0)
  1137. {
  1138. btns[2]->disable();
  1139. btns[3]->disable();
  1140. }
  1141. else
  1142. {
  1143. btns[2]->enable(active);
  1144. btns[3]->enable(active);
  1145. }
  1146. }
  1147. void OptionsTab::SelectedBox::showAll( SDL_Surface * to )
  1148. {
  1149. PlayerSettings &s = curOpts->playerInfos[player];
  1150. SDL_Surface *toBlit = getImg();
  1151. const std::string *toPrint = getText();
  1152. blitAt(toBlit, pos, to);
  1153. printAtMiddleLoc(*toPrint, 23, 39, FONT_TINY, zwykly, to);
  1154. }
  1155. OptionsTab::SelectedBox::SelectedBox( SelType Which, ui8 Player )
  1156. :which(Which), player(Player)
  1157. {
  1158. SDL_Surface *img = getImg();
  1159. pos.w = img->w;
  1160. pos.h = img->h;
  1161. used = RCLICK;
  1162. }
  1163. SDL_Surface * OptionsTab::SelectedBox::getImg() const
  1164. {
  1165. PlayerSettings &s = curOpts->playerInfos[player];
  1166. switch(which)
  1167. {
  1168. case TOWN:
  1169. if (s.castle < F_NUMBER && s.castle >= 0)
  1170. return graphics->getPic(s.castle, true, false);
  1171. else if (s.castle == -1)
  1172. return CGP->rTown;
  1173. else if (s.castle == -2)
  1174. return CGP->nTown;
  1175. case HERO:
  1176. if (s.hero == -1)
  1177. {
  1178. return CGP->rHero;
  1179. }
  1180. else if (s.hero == -2)
  1181. {
  1182. if(s.heroPortrait >= 0)
  1183. return graphics->portraitSmall[s.heroPortrait];
  1184. else
  1185. return CGP->nHero;
  1186. }
  1187. else
  1188. {
  1189. return graphics->portraitSmall[s.hero];
  1190. }
  1191. break;
  1192. case BONUS:
  1193. {
  1194. int pom;
  1195. switch (s.bonus)
  1196. {
  1197. case -1:
  1198. pom=10;
  1199. break;
  1200. case 0:
  1201. pom=9;
  1202. break;
  1203. case 1:
  1204. pom=8;
  1205. break;
  1206. case 2:
  1207. pom=CGI->townh->towns[s.castle].bonus;
  1208. break;
  1209. default:
  1210. assert(0);
  1211. }
  1212. return CGP->bonuses->ourImages[pom].bitmap;
  1213. }
  1214. default:
  1215. return NULL;
  1216. }
  1217. }
  1218. const std::string * OptionsTab::SelectedBox::getText() const
  1219. {
  1220. PlayerSettings &s = curOpts->playerInfos[player];
  1221. switch(which)
  1222. {
  1223. case TOWN:
  1224. if (s.castle < F_NUMBER && s.castle >= 0)
  1225. return &CGI->townh->towns[s.castle].Name();
  1226. else if (s.castle == -1)
  1227. return &CGI->generaltexth->allTexts[522];
  1228. else if (s.castle == -2)
  1229. return &CGI->generaltexth->allTexts[523];
  1230. case HERO:
  1231. if (s.hero == -1)
  1232. return &CGI->generaltexth->allTexts[522];
  1233. else if (s.hero == -2)
  1234. {
  1235. if(s.heroPortrait >= 0)
  1236. {
  1237. if(s.heroName.length())
  1238. return &s.heroName;
  1239. else
  1240. return &CGI->heroh->heroes[s.heroPortrait]->name;
  1241. }
  1242. else
  1243. return &CGI->generaltexth->allTexts[523];
  1244. }
  1245. else
  1246. {
  1247. //if(s.heroName.length())
  1248. // return &s.heroName;
  1249. //else
  1250. return &CGI->heroh->heroes[s.hero]->name;
  1251. }
  1252. case BONUS:
  1253. switch (s.bonus)
  1254. {
  1255. case -1:
  1256. return &CGI->generaltexth->allTexts[522];
  1257. default:
  1258. return &CGI->generaltexth->arraytxt[214 + s.bonus];
  1259. }
  1260. default:
  1261. return NULL;
  1262. }
  1263. }
  1264. void OptionsTab::SelectedBox::clickRight( tribool down, bool previousState )
  1265. {
  1266. if(indeterminate(down) || !down) return;
  1267. PlayerSettings &s = curOpts->playerInfos[player];
  1268. SDL_Surface *bmp = NULL;
  1269. const std::string *title = NULL, *subTitle = NULL;
  1270. subTitle = getText();
  1271. int val;
  1272. switch(which)
  1273. {
  1274. case TOWN:
  1275. val = s.castle;
  1276. break;
  1277. case HERO:
  1278. val = s.hero;
  1279. if(val < 0)
  1280. {
  1281. int p9 = curMap->players[s.color].p9;
  1282. if(p9 != 255 && curOpts->playerInfos[player].heroPortrait >= 0)
  1283. val = p9;
  1284. }
  1285. break;
  1286. case BONUS:
  1287. val = s.bonus;
  1288. break;
  1289. }
  1290. if(val == -1 || which == BONUS) //random or bonus box
  1291. {
  1292. bmp = CMessage::drawBox1(256, 190);
  1293. std::string *description = NULL;
  1294. switch(which)
  1295. {
  1296. case TOWN:
  1297. title = &CGI->generaltexth->allTexts[103];
  1298. description = &CGI->generaltexth->allTexts[104];
  1299. break;
  1300. case HERO:
  1301. title = &CGI->generaltexth->allTexts[101];
  1302. description = &CGI->generaltexth->allTexts[102];
  1303. break;
  1304. case BONUS:
  1305. {
  1306. switch(val)
  1307. {
  1308. case brandom:
  1309. title = &CGI->generaltexth->allTexts[86]; //{Random Bonus}
  1310. description = &CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  1311. break;
  1312. case bartifact:
  1313. title = &CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  1314. description = &CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  1315. break;
  1316. case bgold:
  1317. title = &CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  1318. subTitle = &CGI->generaltexth->allTexts[87]; //500-1000
  1319. description = &CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  1320. break;
  1321. case bresource:
  1322. {
  1323. title = &CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  1324. switch(CGI->townh->towns[s.castle].primaryRes)
  1325. {
  1326. case 1:
  1327. subTitle = &CGI->generaltexth->allTexts[694];
  1328. description = &CGI->generaltexth->allTexts[690];
  1329. break;
  1330. case 3:
  1331. subTitle = &CGI->generaltexth->allTexts[695];
  1332. description = &CGI->generaltexth->allTexts[691];
  1333. break;
  1334. case 4:
  1335. subTitle = &CGI->generaltexth->allTexts[692];
  1336. description = &CGI->generaltexth->allTexts[688];
  1337. break;
  1338. case 5:
  1339. subTitle = &CGI->generaltexth->allTexts[693];
  1340. description = &CGI->generaltexth->allTexts[689];
  1341. break;
  1342. case 127:
  1343. subTitle = &CGI->generaltexth->allTexts[89]; //5-10 wood / 5-10 ore
  1344. 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
  1345. break;
  1346. }
  1347. }
  1348. break;
  1349. }
  1350. }
  1351. break;
  1352. }
  1353. if(description)
  1354. CSDL_Ext::printAtMiddleWB(*description, 125, 145, FONT_SMALL, 37, zwykly, bmp);
  1355. }
  1356. else if(val == -2)
  1357. {
  1358. return;
  1359. }
  1360. else if(which == TOWN)
  1361. {
  1362. bmp = CMessage::drawBox1(256, 319);
  1363. title = &CGI->generaltexth->allTexts[80];
  1364. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[79], 135, 137, FONT_MEDIUM, tytulowy, bmp);
  1365. const CTown &t = CGI->townh->towns[val];
  1366. //print creatures
  1367. int x = 60, y = 159;
  1368. for(int i = 0; i < 7; i++)
  1369. {
  1370. int c = t.basicCreatures[i];
  1371. blitAt(graphics->smallImgs[c], x, y, bmp);
  1372. CSDL_Ext::printAtMiddleWB(CGI->creh->creatures[c].nameSing, x + 16, y + 45, FONT_TINY, 10, zwykly, bmp);
  1373. if(i == 2)
  1374. {
  1375. x = 40;
  1376. y += 76;
  1377. }
  1378. else
  1379. {
  1380. x += 52;
  1381. }
  1382. }
  1383. }
  1384. else if(val >= 0)
  1385. {
  1386. const CHero *h = CGI->heroh->heroes[val];
  1387. bmp = CMessage::drawBox1(320, 255);
  1388. title = &CGI->generaltexth->allTexts[77];
  1389. CSDL_Ext::printAtMiddle(*title, 167, 36, FONT_MEDIUM, tytulowy, bmp);
  1390. CSDL_Ext::printAtMiddle(*subTitle + " - " + h->heroClass->name, 160, 99, FONT_SMALL, zwykly, bmp);
  1391. blitAt(getImg(), 136, 56, bmp);
  1392. //print specialty
  1393. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[78], 166, 132, FONT_MEDIUM, tytulowy, bmp);
  1394. blitAt(graphics->un44->ourImages[val].bitmap, 140, 150, bmp);
  1395. GH.pushInt(new CInfoPopup(bmp, true));
  1396. return;
  1397. }
  1398. if(title)
  1399. CSDL_Ext::printAtMiddle(*title, 135, 36, FONT_MEDIUM, tytulowy, bmp);
  1400. if(subTitle)
  1401. CSDL_Ext::printAtMiddle(*subTitle, 127, 103, FONT_SMALL, zwykly, bmp);
  1402. blitAt(getImg(), 104, 60, bmp);
  1403. GH.pushInt(new CInfoPopup(bmp, true));
  1404. }
  1405. CScenarioInfo::CScenarioInfo( const CMapHeader *mapInfo, const StartInfo *startInfo )
  1406. {
  1407. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1408. for(size_t i = 0; i < startInfo->playerInfos.size(); i++)
  1409. {
  1410. if(startInfo->playerInfos[i].human)
  1411. {
  1412. playerColor = startInfo->playerInfos[i].color;
  1413. playerSerial = i;
  1414. }
  1415. }
  1416. pos.w = 762;
  1417. pos.h = 584;
  1418. center(pos);
  1419. curMap = mapInfo;
  1420. curOpts = (StartInfo*)startInfo;
  1421. card = new InfoCard(scenarioInfo);
  1422. opt = new OptionsTab(scenarioInfo);
  1423. opt->changeSelection(0);
  1424. card->difficulty->select(startInfo->difficulty, 0);
  1425. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 584, 535, "SCNRBACK.DEF", SDLK_ESCAPE);
  1426. }
  1427. CScenarioInfo::~CScenarioInfo()
  1428. {
  1429. }
  1430. CTextInput::CTextInput()
  1431. {
  1432. bg = NULL;
  1433. used = 0;
  1434. }
  1435. CTextInput::CTextInput( const Rect &Pos, const Point &bgOffset, const std::string &bgName, const CFunctionList<void(const std::string &)> &CB )
  1436. :cb(CB)
  1437. {
  1438. pos += Pos;
  1439. OBJ_CONSTRUCTION;
  1440. bg = new CPicture(bgName, bgOffset.x, bgOffset.y);
  1441. used = LCLICK | KEYBOARD;
  1442. }
  1443. CTextInput::~CTextInput()
  1444. {
  1445. }
  1446. void CTextInput::showAll( SDL_Surface * to )
  1447. {
  1448. CIntObject::showAll(to);
  1449. CSDL_Ext::printAt(text + "_", pos.x, pos.y, FONT_SMALL, zwykly, to);
  1450. }
  1451. void CTextInput::clickLeft( tribool down, bool previousState )
  1452. {
  1453. //TODO
  1454. }
  1455. void CTextInput::keyPressed( const SDL_KeyboardEvent & key )
  1456. {
  1457. if(key.state != SDL_PRESSED) return;
  1458. switch(key.keysym.sym)
  1459. {
  1460. case SDLK_BACKSPACE:
  1461. if(text.size())
  1462. text.resize(text.size()-1);
  1463. break;
  1464. default:
  1465. char c = key.keysym.unicode;
  1466. if(std::isprint(c))
  1467. text += c;
  1468. break;
  1469. }
  1470. redraw();
  1471. cb(text);
  1472. }
  1473. void CTextInput::setText( const std::string &nText, bool callCb )
  1474. {
  1475. text = nText;
  1476. redraw();
  1477. if(callCb)
  1478. cb(text);
  1479. }