CPreGame.cpp 44 KB

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