CPreGame.cpp 46 KB

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