CPreGame.cpp 44 KB

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