CPreGame.cpp 44 KB

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