CPreGame.cpp 44 KB

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