CPreGame.cpp 44 KB

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