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