CPreGame.cpp 69 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518
  1. #include "../stdafx.h"
  2. #include "CPreGame.h"
  3. #include <ctime>
  4. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  5. #include <boost/algorithm/string.hpp>
  6. #include <zlib.h>
  7. #include "../timeHandler.h"
  8. #include <sstream>
  9. #include "SDL_Extensions.h"
  10. #include "CGameInfo.h"
  11. #include "CCursorHandler.h"
  12. #include "../hch/CDefHandler.h"
  13. #include "../hch/CGeneralTextHandler.h"
  14. #include "../hch/CLodHandler.h"
  15. #include "../hch/CTownHandler.h"
  16. #include "../hch/CHeroHandler.h"
  17. #include "../hch/CObjectHandler.h"
  18. #include "../hch/CCampaignHandler.h"
  19. #include "../hch/CCreatureHandler.h"
  20. #include "../hch/CMusicHandler.h"
  21. #include "../hch/CVideoHandler.h"
  22. #include <cmath>
  23. #include "Graphics.h"
  24. //#include <boost/thread.hpp>
  25. #include <boost/bind.hpp>
  26. #include <cstdlib>
  27. #include "../lib/Connection.h"
  28. #include "../lib/VCMIDirs.h"
  29. #include "../lib/map.h"
  30. #include "AdventureMapButton.h"
  31. #include "GUIClasses.h"
  32. #include "CPlayerInterface.h"
  33. #include "../CCallback.h"
  34. #include <boost/lexical_cast.hpp>
  35. #include <cstdlib>
  36. #include "CMessage.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. #if _MSC_VER >= 1600
  50. #define bind boost::bind
  51. #define ref boost::ref
  52. #endif
  53. void startGame(StartInfo * options);
  54. CGPreGame * CGP;
  55. static const CMapInfo *curMap;
  56. static StartInfo *curOpts;
  57. static int playerColor, playerSerial; //if more than one player - applies to the first
  58. static std::vector<std::string> playerNames; // serial id of name <-> player name
  59. static std::string selectedName; //set when game is started/loaded
  60. static void do_quit()
  61. {
  62. SDL_Event event;
  63. event.quit.type = SDL_QUIT;
  64. SDL_PushEvent(&event);
  65. }
  66. static CMapInfo *mapInfoFromGame()
  67. {
  68. CMapInfo *ret = new CMapInfo();
  69. CMapHeader *headerCopy = new CMapHeader(*LOCPLINT->cb->getMapHeader()); //will be deleted by CMapInfo d-tor
  70. ret->setHeader(headerCopy);
  71. return ret;
  72. }
  73. static void setPlayersFromGame()
  74. {
  75. playerColor = LOCPLINT->playerID;
  76. playerSerial = LOCPLINT->serialID;
  77. }
  78. static void clearInfo()
  79. {
  80. delNull(curMap);
  81. delNull(curOpts);
  82. playerColor = playerSerial = -1;
  83. playerNames.clear();
  84. }
  85. void CMapInfo::countPlayers()
  86. {
  87. actualHumanPlayers = playerAmnt = humenPlayers = 0;
  88. for(int i=0;i<PLAYER_LIMIT;i++)
  89. {
  90. if(mapHeader->players[i].canHumanPlay)
  91. {
  92. playerAmnt++;
  93. humenPlayers++;
  94. }
  95. else if(mapHeader->players[i].canComputerPlay)
  96. {
  97. playerAmnt++;
  98. }
  99. }
  100. if(scenarioOpts)
  101. for (std::vector<PlayerSettings>::const_iterator i = scenarioOpts->playerInfos.begin(); i != scenarioOpts->playerInfos.end(); i++)
  102. if(i->human)
  103. actualHumanPlayers++;
  104. }
  105. //CMapInfo::CMapInfo(const std::string &fname, const unsigned char *map )
  106. //: mapHeader(NULL), campaignHeader(NULL)
  107. //{
  108. // init(fname, map);
  109. //}
  110. CMapInfo::CMapInfo(bool map)
  111. : mapHeader(NULL), campaignHeader(NULL), scenarioOpts(NULL)
  112. {
  113. }
  114. void CMapInfo::mapInit(const std::string &fname, const unsigned char *map )
  115. {
  116. filename = fname;
  117. int i = 0;
  118. mapHeader = new CMapHeader();
  119. mapHeader->version = CMapHeader::invalid;
  120. try
  121. {
  122. mapHeader->initFromMemory(map, i);
  123. countPlayers();
  124. }
  125. catch (const std::string &e)
  126. {
  127. tlog1 << "\t\tWarning: evil map file: " << fname << ": " << e << std::endl;
  128. delete mapHeader;
  129. mapHeader = NULL;
  130. }
  131. }
  132. CMapInfo::~CMapInfo()
  133. {
  134. delete mapHeader;
  135. delete campaignHeader;
  136. }
  137. void CMapInfo::campaignInit()
  138. {
  139. campaignHeader = new CCampaignHeader( CCampaignHandler::getHeader(filename, lodCmpgn) );
  140. }
  141. void CMapInfo::setHeader(CMapHeader *header)
  142. {
  143. mapHeader = header;
  144. }
  145. CMenuScreen::CMenuScreen( EState which )
  146. {
  147. OBJ_CONSTRUCTION;
  148. bgAd = NULL;
  149. switch(which)
  150. {
  151. case mainMenu:
  152. {
  153. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[3].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[newGame])), 540, 10, "ZMENUNG.DEF", SDLK_n);
  154. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[4].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[loadGame])), 532, 132, "ZMENULG.DEF", SDLK_l);
  155. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[5].second, 0, 524, 251, "ZMENUHS.DEF", SDLK_h);
  156. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[6].second, 0 /*cb*/, 557, 359, "ZMENUCR.DEF", SDLK_c);
  157. boost::function<void()> confWindow = bind(CInfoWindow::showYesNoDialog, ref(CGI->generaltexth->allTexts[69]), (const std::vector<SComponent*>*)0, do_quit, 0, false, 1);
  158. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[7].second, confWindow, 586, 468, "ZMENUQT.DEF", SDLK_ESCAPE);
  159. }
  160. break;
  161. case newGame:
  162. {
  163. bgAd = new CPicture(BitmapHandler::loadBitmap("ZNEWGAM.bmp"), 114, 312, true);
  164. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, newGame, false), 545, 4, "ZTSINGL.DEF", SDLK_s);
  165. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, &pushIntT<CMultiMode>, 568, 120, "ZTMULTI.DEF", SDLK_m);
  166. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, bind(&CMenuScreen::moveTo, this, ref(CGP->scrs[campaignMain])), 541, 233, "ZTCAMPN.DEF", SDLK_c);
  167. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  168. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  169. }
  170. break;
  171. case loadGame:
  172. {
  173. bgAd = new CPicture(BitmapHandler::loadBitmap("ZLOADGAM.bmp"), 114, 312, true);
  174. buttons[0] = new AdventureMapButton("", CGI->generaltexth->zelp[10].second, bind(&CGPreGame::openSel, CGP, loadGame, false), 545, 4, "ZTSINGL.DEF", SDLK_s);
  175. buttons[1] = new AdventureMapButton("", CGI->generaltexth->zelp[11].second, bind(&CGPreGame::openSel, CGP, loadGame, true), 568, 120, "ZTMULTI.DEF", SDLK_m);
  176. buttons[2] = new AdventureMapButton("", CGI->generaltexth->zelp[12].second, 0 /*cb*/, 541, 233, "ZTCAMPN.DEF", SDLK_c);
  177. buttons[3] = new AdventureMapButton("", CGI->generaltexth->zelp[13].second, 0 /*cb*/, 545, 358, "ZTTUTOR.DEF", SDLK_t);
  178. buttons[4] = new AdventureMapButton("", CGI->generaltexth->zelp[14].second, bind(&CMenuScreen::moveTo, this, CGP->scrs[mainMenu]), 582, 464, "ZTBACK.DEF", SDLK_ESCAPE);
  179. }
  180. break;
  181. case campaignMain:
  182. {
  183. buttons[0] = new AdventureMapButton("", "", 0 /*cb*/, 535, 8, "ZSSSOD.DEF", SDLK_s);
  184. buttons[1] = new AdventureMapButton("", "", 0 /*cb*/, 494, 117, "ZSSROE.DEF", SDLK_m);
  185. buttons[2] = new AdventureMapButton("", "", 0 /*cb*/, 486, 241, "ZSSARM.DEF", SDLK_c);
  186. buttons[3] = new AdventureMapButton("", "", bind(&CGPreGame::openSel, CGP, campaignList, false), 550, 358, "ZSSCUS.DEF", SDLK_t);
  187. buttons[4] = new AdventureMapButton("", "", bind(&CMenuScreen::moveTo, this, CGP->scrs[newGame]), 582, 464, "ZSSEXIT.DEF", SDLK_ESCAPE);
  188. }
  189. break;
  190. }
  191. for(int i = 0; i < ARRAY_COUNT(buttons); i++)
  192. buttons[i]->hoverable = true;
  193. }
  194. CMenuScreen::~CMenuScreen()
  195. {
  196. }
  197. void CMenuScreen::showAll( SDL_Surface * to )
  198. {
  199. blitAt(CGP->mainbg, 0, 0, to);
  200. CIntObject::showAll(to);
  201. }
  202. void CMenuScreen::show( SDL_Surface * to )
  203. {
  204. CIntObject::show(to);
  205. CGI->videoh->update(pos.x + 8, pos.y + 105, screen, true, false);
  206. }
  207. void CMenuScreen::moveTo( CMenuScreen *next )
  208. {
  209. GH.popInt(this);
  210. GH.pushInt(next);
  211. }
  212. CGPreGame::CGPreGame()
  213. {
  214. GH.defActionsDef = 63;
  215. CGP = this;
  216. mainbg = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  217. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  218. scrs[i] = new CMenuScreen((CMenuScreen::EState)i);
  219. }
  220. CGPreGame::~CGPreGame()
  221. {
  222. SDL_FreeSurface(mainbg);
  223. for(int i = 0; i < ARRAY_COUNT(scrs); i++)
  224. delete scrs[i];
  225. }
  226. void CGPreGame::openSel( CMenuScreen::EState type, bool multi )
  227. {
  228. playerNames.clear();
  229. playerNames.push_back(CGI->generaltexth->allTexts[434]); //we have only one player and his name is "Player"
  230. GH.pushInt(new CSelectionScreen(type, multi));
  231. }
  232. void CGPreGame::loadGraphics()
  233. {
  234. victory = CDefHandler::giveDef("SCNRVICT.DEF");
  235. loss = CDefHandler::giveDef("SCNRLOSS.DEF");
  236. bonuses = CDefHandler::giveDef("SCNRSTAR.DEF");
  237. rHero = BitmapHandler::loadBitmap("HPSRAND1.bmp");
  238. rTown = BitmapHandler::loadBitmap("HPSRAND0.bmp");
  239. nHero = BitmapHandler::loadBitmap("HPSRAND6.bmp");
  240. nTown = BitmapHandler::loadBitmap("HPSRAND5.bmp");
  241. }
  242. void CGPreGame::disposeGraphics()
  243. {
  244. delete victory;
  245. delete loss;
  246. SDL_FreeSurface(rHero);
  247. SDL_FreeSurface(nHero);
  248. SDL_FreeSurface(rTown);
  249. SDL_FreeSurface(nTown);
  250. }
  251. void CGPreGame::update()
  252. {
  253. if (GH.listInt.size() == 0)
  254. {
  255. #ifdef _WIN32
  256. CGI->videoh->open("ACREDIT.SMK");
  257. #else
  258. CGI->videoh->open("ACREDIT.SMK", true, false);
  259. #endif
  260. GH.pushInt(scrs[CMenuScreen::mainMenu]);
  261. }
  262. CGI->curh->draw1();
  263. SDL_Flip(screen);
  264. CGI->curh->draw2();
  265. GH.topInt()->show(screen);
  266. GH.updateTime();
  267. GH.handleEvents();
  268. }
  269. CSelectionScreen::CSelectionScreen(CMenuScreen::EState Type, bool MultiPlayer)
  270. :multiPlayer(MultiPlayer)
  271. {
  272. OBJ_CONSTRUCTION_CAPTURING_ALL;
  273. IShowActivable::type = BLOCK_ADV_HOTKEYS;
  274. pos.w = 762;
  275. pos.h = 584;
  276. if(Type == CMenuScreen::saveGame)
  277. {
  278. center(pos);
  279. }
  280. else if(Type == CMenuScreen::campaignList)
  281. {
  282. bg = new CPicture(BitmapHandler::loadBitmap("CamCust.bmp"), 0, 0, true);
  283. pos.x = 3;
  284. pos.y = 6;
  285. }
  286. else
  287. {
  288. pos.x = 3;
  289. pos.y = 6;
  290. bg = new CPicture(BitmapHandler::loadBitmap(rand()%2 ? "ZPIC1000.bmp" : "ZPIC1001.bmp"), -3, -6, true);
  291. }
  292. CGP->loadGraphics();
  293. type = Type;
  294. curOpts = &sInfo;
  295. sInfo.difficulty = 1;
  296. current = NULL;
  297. sInfo.mode = (Type == CMenuScreen::newGame ? 0 : 1);
  298. sInfo.turnTime = 0;
  299. curTab = NULL;
  300. card = new InfoCard(type); //right info card
  301. if (type == CMenuScreen::campaignList)
  302. {
  303. opt = NULL;
  304. }
  305. else
  306. {
  307. opt = new OptionsTab(type/*, sInfo*/); //scenario options tab
  308. opt->recActions = DISPOSE;
  309. }
  310. sel = new SelectionTab(type, bind(&CSelectionScreen::changeSelection, this, _1), multiPlayer); //scenario selection tab
  311. sel->recActions = DISPOSE;
  312. switch(type)
  313. {
  314. case CMenuScreen::newGame:
  315. {
  316. card->difficulty->onChange = bind(&CSelectionScreen::difficultyChange, this, _1);
  317. card->difficulty->select(1, 0);
  318. AdventureMapButton *select = new AdventureMapButton(CGI->generaltexth->zelp[45], bind(&CSelectionScreen::toggleTab, this, sel), 411, 75, "GSPBUTT.DEF", SDLK_s);
  319. select->addTextOverlay(CGI->generaltexth->allTexts[500], FONT_SMALL);
  320. AdventureMapButton *opts = new AdventureMapButton(CGI->generaltexth->zelp[46], bind(&CSelectionScreen::toggleTab, this, opt), 411, 503, "GSPBUTT.DEF", SDLK_a);
  321. opts->addTextOverlay(CGI->generaltexth->allTexts[501], FONT_SMALL);
  322. AdventureMapButton *random = new AdventureMapButton(CGI->generaltexth->zelp[47], bind(&CSelectionScreen::toggleTab, this, sel), 411, 99, "GSPBUTT.DEF", SDLK_r);
  323. random->addTextOverlay(CGI->generaltexth->allTexts[740], FONT_SMALL);
  324. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRBEG.DEF", SDLK_b);
  325. }
  326. break;
  327. case CMenuScreen::loadGame:
  328. sel->recActions = 255;
  329. start = new AdventureMapButton(CGI->generaltexth->zelp[103], bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRLOD.DEF", SDLK_l);
  330. break;
  331. case CMenuScreen::saveGame:
  332. sel->recActions = 255;
  333. start = new AdventureMapButton("", CGI->generaltexth->zelp[103].second, bind(&CSelectionScreen::startGame, this), 411, 529, "SCNRSAV.DEF");
  334. break;
  335. case CMenuScreen::campaignList:
  336. sel->recActions = 255;
  337. start = new AdventureMapButton(std::pair<std::string, std::string>(), bind(&CSelectionScreen::startCampaign, this), 411, 529, "SCNRLOD.DEF", SDLK_b);
  338. break;
  339. }
  340. start->assignedKeys.insert(SDLK_RETURN);
  341. std::string backName;
  342. if(Type == CMenuScreen::campaignList)
  343. {
  344. backName = "SCNRBACK.DEF";
  345. }
  346. else
  347. {
  348. backName = "SCNRBACK.DEF";
  349. }
  350. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 581, 529, backName, SDLK_ESCAPE);
  351. }
  352. CSelectionScreen::~CSelectionScreen()
  353. {
  354. curMap = NULL;
  355. curOpts = NULL;
  356. playerSerial = playerColor = -1;
  357. playerNames.clear();
  358. }
  359. void CSelectionScreen::toggleTab(CIntObject *tab)
  360. {
  361. if(curTab && curTab->active)
  362. {
  363. curTab->deactivate();
  364. curTab->recActions = DISPOSE;
  365. }
  366. if(curTab != tab)
  367. {
  368. tab->recActions = 255;
  369. tab->activate();
  370. curTab = tab;
  371. }
  372. else
  373. {
  374. curTab = NULL;
  375. };
  376. GH.totalRedraw();
  377. }
  378. void CSelectionScreen::changeSelection( const CMapInfo *to )
  379. {
  380. curMap = current = to;;
  381. if(to && type == CMenuScreen::loadGame)
  382. curOpts->difficulty = to->scenarioOpts->difficulty;
  383. if(type != CMenuScreen::campaignList && type != CMenuScreen::saveGame)
  384. {
  385. updateStartInfo(to, sInfo, to ? to->mapHeader : NULL);
  386. }
  387. card->changeSelection(to);
  388. if(type != CMenuScreen::campaignList)
  389. {
  390. opt->changeSelection(to ? to->mapHeader : NULL);
  391. }
  392. }
  393. void setPlayer(PlayerSettings &pset, unsigned player)
  394. {
  395. if(player < playerNames.size())
  396. {
  397. pset.name = playerNames[player];
  398. pset.human = true;
  399. if(playerColor < 0)
  400. {
  401. playerColor = pset.color;
  402. playerSerial = pset.serial;
  403. }
  404. }
  405. else
  406. {
  407. pset.name = CGI->generaltexth->allTexts[468];//Computer
  408. pset.human = false;
  409. }
  410. }
  411. void CSelectionScreen::updateStartInfo( const CMapInfo * to, StartInfo & sInfo, const CMapHeader * mapHeader )
  412. {
  413. sInfo.playerInfos.clear();
  414. if(!to)
  415. return;
  416. sInfo.playerInfos.resize(to->playerAmnt);
  417. sInfo.mapname = to->filename;
  418. playerSerial = playerColor = -1;
  419. ui8 placedPlayers = 0;
  420. int serialC=0;
  421. for (int i = 0; i < PLAYER_LIMIT; i++)
  422. {
  423. const PlayerInfo &pinfo = mapHeader->players[i];
  424. //neither computer nor human can play - no player
  425. if (!(pinfo.canComputerPlay || pinfo.canComputerPlay))
  426. continue;
  427. PlayerSettings &pset = sInfo.playerInfos[serialC];
  428. pset.color = i;
  429. pset.serial = serialC++;
  430. if(pinfo.canHumanPlay)
  431. setPlayer(pset, placedPlayers++);
  432. else
  433. setPlayer(pset, -1);
  434. 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
  435. {
  436. if((1 << j) & pinfo.allowedFactions)
  437. {
  438. if (pset.castle >= 0) //we've already assigned a castle and another one is possible -> set random and let player choose
  439. pset.castle = -1; //breaks
  440. if (pset.castle == -2) //first available castle - pick
  441. pset.castle = j;
  442. }
  443. }
  444. if ((pinfo.generateHeroAtMainTown || mapHeader->version==CMapHeader::RoE) && pinfo.hasMainTown //we will generate hero in front of main town
  445. || pinfo.p8) //random hero
  446. pset.hero = -1;
  447. else
  448. pset.hero = -2;
  449. if(pinfo.mainHeroName.length())
  450. {
  451. pset.heroName = pinfo.mainHeroName;
  452. if((pset.heroPortrait = pinfo.mainHeroPortrait) == 255)
  453. pset.heroPortrait = pinfo.p9;
  454. }
  455. pset.handicap = 0;
  456. }
  457. }
  458. void CSelectionScreen::startCampaign()
  459. {
  460. CCampaign * ourCampaign = CCampaignHandler::getCampaign(curMap->filename, curMap->lodCmpgn);
  461. GH.pushInt( new CBonusSelection(ourCampaign, 0) );
  462. }
  463. void CSelectionScreen::startGame()
  464. {
  465. if(type == CMenuScreen::newGame)
  466. {
  467. //there must be at least one human player before game can be started
  468. std::vector<PlayerSettings>::const_iterator i;
  469. for(i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  470. if(i->human)
  471. break;
  472. if(i == curOpts->playerInfos.end())
  473. {
  474. GH.pushInt(CInfoWindow::create(CGI->generaltexth->allTexts[530])); //You must position yourself prior to starting the game.
  475. return;
  476. }
  477. }
  478. if(type != CMenuScreen::saveGame)
  479. {
  480. if(!current)
  481. return;
  482. selectedName = sInfo.mapname;
  483. StartInfo *si = new StartInfo(sInfo);
  484. GH.popIntTotally(this); //delete me
  485. GH.popInt(GH.topInt()); //only deactivate main menu screen
  486. //curMap = NULL;
  487. curOpts = NULL;
  488. ::startGame(si);
  489. }
  490. else
  491. {
  492. if(!(sel && sel->txt && sel->txt->text.size()))
  493. return;
  494. selectedName = GVCMIDirs.UserPath + "/Games/" + sel->txt->text + ".vlgm1";
  495. CFunctionList<void()> overWrite;
  496. overWrite += bind(&CCallback::save, LOCPLINT->cb, sel->txt->text);
  497. overWrite += bind(&CGuiHandler::popIntTotally, &GH, this);
  498. if(fs::exists(selectedName))
  499. {
  500. std::string hlp = CGI->generaltexth->allTexts[493]; //%s exists. Overwrite?
  501. boost::algorithm::replace_first(hlp, "%s", sel->txt->text);
  502. LOCPLINT->showYesNoDialog(hlp, std::vector<SComponent*>(), overWrite, 0, false);
  503. }
  504. else
  505. overWrite();
  506. // LOCPLINT->cb->save(sel->txt->text);
  507. // GH.popIntTotally(this);
  508. }
  509. }
  510. void CSelectionScreen::difficultyChange( int to )
  511. {
  512. assert(type == CMenuScreen::newGame);
  513. sInfo.difficulty = to;
  514. GH.totalRedraw();
  515. }
  516. // A new size filter (Small, Medium, ...) has been selected. Populate
  517. // selMaps with the relevant data.
  518. void SelectionTab::filter( int size, bool selectFirst )
  519. {
  520. curItems.clear();
  521. if(tabType == CMenuScreen::campaignList)
  522. {
  523. for (size_t i=0; i<allItems.size(); i++)
  524. curItems.push_back(&allItems[i]);
  525. }
  526. else
  527. {
  528. for (size_t i=0; i<allItems.size(); i++)
  529. if( allItems[i].mapHeader && allItems[i].mapHeader->version && (!size || allItems[i].mapHeader->width == size))
  530. curItems.push_back(&allItems[i]);
  531. }
  532. if(curItems.size())
  533. {
  534. slider->block(false);
  535. slider->setAmount(curItems.size());
  536. sort();
  537. if(selectFirst)
  538. {
  539. slider->moveTo(0);
  540. onSelect(curItems[0]);
  541. }
  542. selectAbs(0);
  543. }
  544. else
  545. {
  546. slider->block(true);
  547. onSelect(NULL);
  548. }
  549. }
  550. void SelectionTab::getFiles(std::vector<FileInfo> &out, const std::string &dirname, const std::string &ext)
  551. {
  552. if(!boost::filesystem::exists(dirname))
  553. {
  554. tlog1 << "Cannot find " << dirname << " directory!\n";
  555. }
  556. fs::path tie(dirname);
  557. fs::directory_iterator end_iter;
  558. for ( fs::directory_iterator file (tie); file!=end_iter; ++file )
  559. {
  560. if(fs::is_regular_file(file->status())
  561. && boost::ends_with(file->path().filename(), ext))
  562. {
  563. std::time_t date = 0;
  564. try
  565. {
  566. date = fs::last_write_time(file->path());
  567. out.resize(out.size()+1);
  568. out.back().date = date;
  569. out.back().name = file->path().string();
  570. }
  571. catch(...)
  572. {
  573. tlog2 << "\t\tWarning: very corrupted map file: " << file->path().string() << std::endl;
  574. }
  575. }
  576. }
  577. allItems.resize(out.size());
  578. }
  579. void SelectionTab::parseMaps(std::vector<FileInfo> &files, int start, int threads)
  580. {
  581. int read=0;
  582. unsigned char mapBuffer[1500];
  583. while(start < allItems.size())
  584. {
  585. gzFile tempf = gzopen(files[start].name.c_str(),"rb");
  586. read = gzread(tempf, mapBuffer, 1500);
  587. gzclose(tempf);
  588. if(read < 50 || !mapBuffer[4])
  589. {
  590. tlog3 << "\t\tWarning: corrupted map file: " << files[start].name << std::endl;
  591. }
  592. else //valid map
  593. {
  594. allItems[start].mapInit(files[start].name, mapBuffer);
  595. //allItems[start].date = "DATEDATE";// files[start].date;
  596. }
  597. start += threads;
  598. }
  599. }
  600. void SelectionTab::parseGames(std::vector<FileInfo> &files, bool multi)
  601. {
  602. for(int i=0; i<files.size(); i++)
  603. {
  604. CLoadFile lf(files[i].name);
  605. if(!lf.sfile)
  606. continue;
  607. ui8 sign[8];
  608. lf >> sign;
  609. if(std::memcmp(sign,"VCMISVG",7))
  610. {
  611. tlog1 << files[i].name << " is not a correct savefile!" << std::endl;
  612. continue;
  613. }
  614. allItems[i].mapHeader = new CMapHeader();
  615. lf >> *(allItems[i].mapHeader) >> allItems[i].scenarioOpts;
  616. allItems[i].filename = files[i].name;
  617. allItems[i].countPlayers();
  618. allItems[i].date = std::asctime(std::localtime(&files[i].date));
  619. if((allItems[i].actualHumanPlayers > 1) != multi) //if multi mode then only multi games, otherwise single
  620. {
  621. delete allItems[i].mapHeader;
  622. allItems[i].mapHeader = NULL;
  623. }
  624. }
  625. }
  626. void SelectionTab::parseCampaigns( std::vector<FileInfo> & files )
  627. {
  628. for(int i=0; i<files.size(); i++)
  629. {
  630. //allItems[i].date = std::asctime(std::localtime(&files[i].date));
  631. allItems[i].filename = files[i].name;
  632. allItems[i].lodCmpgn = files[i].inLod;
  633. allItems[i].campaignInit();
  634. }
  635. }
  636. SelectionTab::SelectionTab(CMenuScreen::EState Type, const boost::function<void(CMapInfo *)> &OnSelect, bool MultiPlayer)
  637. :onSelect(OnSelect), bg(NULL)
  638. {
  639. OBJ_CONSTRUCTION;
  640. selectionPos = 0;
  641. used = LCLICK | WHEEL | KEYBOARD | DOUBLECLICK;
  642. slider = NULL;
  643. txt = NULL;
  644. tabType = Type;
  645. if (Type != CMenuScreen::campaignList)
  646. {
  647. bg = new CPicture(BitmapHandler::loadBitmap("SCSELBCK.bmp"), 0, 0, true);
  648. pos.w = bg->pos.w;
  649. pos.h = bg->pos.h;
  650. }
  651. else
  652. {
  653. SDL_Surface * tmp1 = BitmapHandler::loadBitmap("CAMCUST.bmp");
  654. SDL_Surface * tmp = CSDL_Ext::newSurface(400, tmp1->h);
  655. blitAt(tmp1, 0, 0, tmp);
  656. SDL_FreeSurface(tmp1);
  657. bg = new CPicture(tmp, 0, 0, true);
  658. pos.w = bg->pos.w;
  659. pos.h = bg->pos.h;
  660. bg->pos.x = bg->pos.y = 0;
  661. }
  662. std::vector<FileInfo> toParse;
  663. std::vector<CCampaignHeader> cpm;
  664. switch(tabType)
  665. {
  666. case CMenuScreen::newGame:
  667. getFiles(toParse, DATA_DIR "/Maps", "h3m"); //get all maps
  668. parseMaps(toParse);
  669. positions = 18;
  670. break;
  671. case CMenuScreen::loadGame:
  672. case CMenuScreen::saveGame:
  673. getFiles(toParse, GVCMIDirs.UserPath + "/Games", "vlgm1"); //get all saves
  674. parseGames(toParse, MultiPlayer);
  675. if(tabType == CMenuScreen::loadGame)
  676. {
  677. positions = 18;
  678. }
  679. else
  680. {
  681. positions = 16;
  682. }
  683. if(tabType == CMenuScreen::saveGame)
  684. txt = new CTextInput(Rect(32, 539, 350, 20), Point(-32, -25), "GSSTRIP.bmp", 0);
  685. break;
  686. case CMenuScreen::campaignList:
  687. getFiles(toParse, DATA_DIR "/Maps", "h3c"); //get all campaigns
  688. for (int g=0; g<toParse.size(); ++g)
  689. {
  690. toParse[g].inLod = false;
  691. }
  692. //add lod cmpgns
  693. cpm = CCampaignHandler::getCampaignHeaders(CCampaignHandler::ALL);
  694. for (int g = 0; g < cpm.size(); g++)
  695. {
  696. FileInfo fi;
  697. fi.inLod = cpm[g].loadFromLod;
  698. fi.name = cpm[g].filename;
  699. toParse.push_back(fi);
  700. if (cpm[g].loadFromLod)
  701. {
  702. allItems.push_back(CMapInfo(false));
  703. }
  704. }
  705. parseCampaigns(toParse);
  706. positions = 18;
  707. break;
  708. default:
  709. assert(0);
  710. break;
  711. }
  712. if (tabType != CMenuScreen::campaignList)
  713. {
  714. //size filter buttons
  715. {
  716. int sizes[] = {36, 72, 108, 144, 0};
  717. const char * names[] = {"SCSMBUT.DEF", "SCMDBUT.DEF", "SCLGBUT.DEF", "SCXLBUT.DEF", "SCALBUT.DEF"};
  718. for(int i = 0; i < 5; i++)
  719. new AdventureMapButton("", CGI->generaltexth->zelp[54+i].second, bind(&SelectionTab::filter, this, sizes[i], true), 158 + 47*i, 46, names[i]);
  720. }
  721. //sort buttons buttons
  722. {
  723. int xpos[] = {23, 55, 88, 121, 306, 339};
  724. const char * names[] = {"SCBUTT1.DEF", "SCBUTT2.DEF", "SCBUTCP.DEF", "SCBUTT3.DEF", "SCBUTT4.DEF", "SCBUTT5.DEF"};
  725. for(int i = 0; i < 6; i++)
  726. new AdventureMapButton("", CGI->generaltexth->zelp[107+i].second, bind(&SelectionTab::sortBy, this, i), xpos[i], 86, names[i]);
  727. }
  728. }
  729. else
  730. {
  731. //sort by buttons
  732. new AdventureMapButton("", "", bind(&SelectionTab::sortBy, this, _numOfMaps), 23, 86, "CamCusM.DEF"); //by num of maps
  733. new AdventureMapButton("", "", bind(&SelectionTab::sortBy, this, _name), 55, 86, "CamCusL.DEF"); //by name
  734. }
  735. slider = new CSlider(372, 86, tabType != CMenuScreen::saveGame ? 480 : 430, bind(&SelectionTab::sliderMove, this, _1), positions, curItems.size(), 0, false, 1);
  736. slider->changeUsedEvents(WHEEL, true);
  737. format = CDefHandler::giveDef("SCSELC.DEF");
  738. sortingBy = _format;
  739. ascending = true;
  740. filter(0);
  741. //select(0);
  742. switch(tabType)
  743. {
  744. case CMenuScreen::newGame:
  745. selectFName(DATA_DIR "/Maps/Arrogance.h3m");
  746. break;
  747. case CMenuScreen::loadGame:
  748. case CMenuScreen::campaignList:
  749. select(0);
  750. break;
  751. case CMenuScreen::saveGame:;
  752. if(selectedName.size())
  753. {
  754. if(selectedName[2] == 'M') //name starts with ./Maps instead of ./Games => there was nothing to select
  755. txt->setText("NEWGAME");
  756. else
  757. selectFName(selectedName);
  758. }
  759. }
  760. }
  761. SelectionTab::~SelectionTab()
  762. {
  763. delete format;
  764. }
  765. void SelectionTab::sortBy( int criteria )
  766. {
  767. if(criteria == sortingBy)
  768. {
  769. ascending = !ascending;
  770. }
  771. else
  772. {
  773. sortingBy = (ESortBy)criteria;
  774. ascending = true;
  775. }
  776. sort();
  777. selectAbs(0);
  778. }
  779. void SelectionTab::sort()
  780. {
  781. if(sortingBy != _name)
  782. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(_name));
  783. std::stable_sort(curItems.begin(), curItems.end(), mapSorter(sortingBy));
  784. if(!ascending)
  785. std::reverse(curItems.begin(), curItems.end());
  786. redraw();
  787. }
  788. void SelectionTab::select( int position )
  789. {
  790. if(!curItems.size()) return;
  791. // New selection. py is the index in curItems.
  792. int py = position + slider->value;
  793. amax(py, 0);
  794. amin(py, curItems.size()-1);
  795. selectionPos = py;
  796. if(position < 0)
  797. slider->moveTo(slider->value + position);
  798. else if(position >= positions)
  799. slider->moveTo(slider->value + position - positions + 1);
  800. if(txt)
  801. txt->setText(fs::basename(curItems[py]->filename));
  802. onSelect(curItems[py]);
  803. }
  804. void SelectionTab::selectAbs( int position )
  805. {
  806. select(position - slider->value);
  807. }
  808. int SelectionTab::getPosition( int x, int y )
  809. {
  810. return -1;
  811. }
  812. void SelectionTab::sliderMove( int slidPos )
  813. {
  814. if(!slider) return; //ignore spurious call when slider is being created
  815. redraw();
  816. }
  817. // Display the tab with the scenario names
  818. //
  819. // elemIdx is the index of the maps or saved game to display on line 0
  820. // slider->capacity contains the number of available screen lines
  821. // slider->positionsAmnt is the number of elements after filtering
  822. void SelectionTab::printMaps(SDL_Surface *to)
  823. {
  824. int elemIdx = slider->value;
  825. // Display all elements if there's enough space
  826. //if(slider->amount < slider->capacity)
  827. // elemIdx = 0;
  828. SDL_Color itemColor;
  829. #define POS(xx, yy) pos.x + xx, pos.y + yy + line*25
  830. for (int line = 0; line < positions && elemIdx < curItems.size(); elemIdx++, line++)
  831. {
  832. CMapInfo* curMap = curItems[elemIdx];
  833. if (elemIdx == selectionPos)
  834. itemColor=tytulowy;
  835. else
  836. itemColor=zwykly;
  837. if(tabType != CMenuScreen::campaignList)
  838. {
  839. //amount of players
  840. std::ostringstream ostr(std::ostringstream::out);
  841. ostr << curMap->playerAmnt << "/" << curMap->humenPlayers;
  842. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, itemColor, to);
  843. //map size
  844. std::string temp2 = "C";
  845. switch (curMap->mapHeader->width)
  846. {
  847. case 36:
  848. temp2="S";
  849. break;
  850. case 72:
  851. temp2="M";
  852. break;
  853. case 108:
  854. temp2="L";
  855. break;
  856. case 144:
  857. temp2="XL";
  858. break;
  859. }
  860. CSDL_Ext::printAtMiddle(temp2, POS(70, 128), FONT_SMALL, itemColor, to);
  861. int temp=-1;
  862. switch (curMap->mapHeader->version)
  863. {
  864. case CMapHeader::RoE:
  865. temp=0;
  866. break;
  867. case CMapHeader::AB:
  868. temp=1;
  869. break;
  870. case CMapHeader::SoD:
  871. temp=2;
  872. break;
  873. case CMapHeader::WoG:
  874. temp=3;
  875. break;
  876. default:
  877. // Unknown version. Be safe and ignore that map
  878. tlog2 << "Warning: " << curMap->filename << " has wrong version!\n";
  879. continue;
  880. }
  881. blitAt(format->ourImages[temp].bitmap, POS(88, 117), to);
  882. //victory conditions
  883. if (curMap->mapHeader->victoryCondition.condition == winStandard)
  884. temp = 11;
  885. else
  886. temp = curMap->mapHeader->victoryCondition.condition;
  887. blitAt(CGP->victory->ourImages[temp].bitmap, POS(306, 117), to);
  888. //loss conditions
  889. if (curMap->mapHeader->lossCondition.typeOfLossCon == lossStandard)
  890. temp=3;
  891. else
  892. temp=curMap->mapHeader->lossCondition.typeOfLossCon;
  893. blitAt(CGP->loss->ourImages[temp].bitmap, POS(339, 117), to);
  894. }
  895. else //if campaign
  896. {
  897. //number of maps in campaign
  898. std::ostringstream ostr(std::ostringstream::out);
  899. ostr << CGI->generaltexth->campaignRegionNames[ curMap->campaignHeader->mapVersion ].size();
  900. CSDL_Ext::printAt(ostr.str(), POS(29, 120), FONT_SMALL, itemColor, to);
  901. }
  902. std::string name;
  903. if(tabType != CMenuScreen::campaignList)
  904. {
  905. if (!curMap->mapHeader->name.length())
  906. curMap->mapHeader->name = "Unnamed";
  907. name = curMap->mapHeader->name;
  908. }
  909. else
  910. {
  911. name = curMap->campaignHeader->name;
  912. }
  913. //print name
  914. if (tabType == CMenuScreen::newGame || tabType == CMenuScreen::campaignList) {
  915. CSDL_Ext::printAtMiddle(name, POS(213, 128), FONT_SMALL, itemColor, to);
  916. } else
  917. CSDL_Ext::printAtMiddle(fs::basename(curMap->filename), POS(213, 128), FONT_SMALL, itemColor, to);
  918. }
  919. #undef POS
  920. }
  921. void SelectionTab::showAll( SDL_Surface * to )
  922. {
  923. CIntObject::showAll(to);
  924. printMaps(to);
  925. std::string title;
  926. switch(tabType) {
  927. case CMenuScreen::newGame:
  928. title = CGI->generaltexth->arraytxt[229];
  929. break;
  930. case CMenuScreen::loadGame:
  931. title = CGI->generaltexth->arraytxt[230];
  932. break;
  933. case CMenuScreen::saveGame:
  934. title = CGI->generaltexth->arraytxt[231];
  935. break;
  936. case CMenuScreen::campaignList:
  937. title = "Select a Campaign"; //TODO: find where is the title
  938. break;
  939. }
  940. CSDL_Ext::printAtMiddle(title, pos.x+205, pos.y+28, FONT_MEDIUM, tytulowy, to); //Select a Scenario to Play
  941. if(tabType != CMenuScreen::campaignList)
  942. {
  943. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[510], pos.x+87, pos.y+62, FONT_SMALL, tytulowy, to); //Map sizes
  944. }
  945. }
  946. void SelectionTab::clickLeft( tribool down, bool previousState )
  947. {
  948. if(down)
  949. {
  950. int line = getLine();
  951. if(line != -1)
  952. select(line);
  953. }
  954. }
  955. void SelectionTab::keyPressed( const SDL_KeyboardEvent & key )
  956. {
  957. if(key.state != SDL_PRESSED) return;
  958. int moveBy = 0;
  959. switch(key.keysym.sym)
  960. {
  961. case SDLK_UP:
  962. moveBy = -1;
  963. break;
  964. case SDLK_DOWN:
  965. moveBy = +1;
  966. break;
  967. case SDLK_PAGEUP:
  968. moveBy = -positions+1;
  969. break;
  970. case SDLK_PAGEDOWN:
  971. moveBy = +positions-1;
  972. break;
  973. case SDLK_HOME:
  974. select(-slider->value);
  975. return;
  976. case SDLK_END:
  977. select(curItems.size() - slider->value);
  978. return;
  979. default:
  980. return;
  981. }
  982. select(selectionPos - slider->value + moveBy);
  983. }
  984. void SelectionTab::onDoubleClick()
  985. {
  986. if(getLine() != -1) //double clicked scenarios list
  987. {
  988. //act as if start button was pressed
  989. (static_cast<CSelectionScreen*>(parent))->start->callback();
  990. }
  991. }
  992. int SelectionTab::getLine()
  993. {
  994. int line = -1;
  995. Point clickPos(GH.current->button.x, GH.current->button.y);
  996. clickPos = clickPos - pos.topLeft();
  997. if (clickPos.y > 115 && clickPos.y < 564 && clickPos.x > 22 && clickPos.x < 371)
  998. {
  999. line = (clickPos.y-115) / 25; //which line
  1000. }
  1001. return line;
  1002. }
  1003. void SelectionTab::selectFName( const std::string &fname )
  1004. {
  1005. for(int i = curItems.size() - 1; i >= 0; i--)
  1006. {
  1007. if(curItems[i]->filename == fname)
  1008. {
  1009. slider->moveTo(i);
  1010. selectAbs(i);
  1011. return;
  1012. }
  1013. }
  1014. selectAbs(0);
  1015. }
  1016. InfoCard::InfoCard( CMenuScreen::EState Type )
  1017. : difficulty(NULL), sizes(NULL), sFlags(NULL), bg(NULL)
  1018. {
  1019. OBJ_CONSTRUCTION;
  1020. pos.x += 393;
  1021. used = RCLICK;
  1022. mapDescription = NULL;
  1023. type = Type;
  1024. Rect descriptionRect(26, 149, 320, 115);
  1025. mapDescription = new CTextBox("", descriptionRect, 1);
  1026. if(type == CMenuScreen::campaignList)
  1027. {
  1028. CSelectionScreen *ss = static_cast<CSelectionScreen*>(parent);
  1029. moveChild(new CPicture(*ss->bg, descriptionRect + Point(-393, 0)), this, mapDescription, true); //move subpicture bg to our description control (by default it's our (Infocard) child)
  1030. }
  1031. else
  1032. {
  1033. bg = new CPicture(BitmapHandler::loadBitmap("GSELPOP1.bmp"), 0, 0, true);
  1034. std::swap(children.front(), children.back());
  1035. pos.w = bg->pos.w;
  1036. pos.h = bg->pos.h;
  1037. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  1038. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  1039. difficulty = new CHighlightableButtonsGroup(0);
  1040. {
  1041. static const char *difButns[] = {"GSPBUT3.DEF", "GSPBUT4.DEF", "GSPBUT5.DEF", "GSPBUT6.DEF", "GSPBUT7.DEF"};
  1042. BLOCK_CAPTURING;
  1043. for(int i = 0; i < 5; i++)
  1044. {
  1045. difficulty->addButton(new CHighlightableButton("", CGI->generaltexth->zelp[24+i].second, 0, 110 + i*32, 450, difButns[i], i));
  1046. difficulty->buttons.back()->pos += pos.topLeft();
  1047. }
  1048. }
  1049. if(type != CMenuScreen::newGame)
  1050. difficulty->block(true);
  1051. //description needs bg
  1052. moveChild(new CPicture(*bg, descriptionRect), this, mapDescription, true); //move subpicture bg to our description control (by default it's our (Infocard) child)
  1053. }
  1054. }
  1055. InfoCard::~InfoCard()
  1056. {
  1057. delete sizes;
  1058. delete sFlags;
  1059. }
  1060. void InfoCard::showAll( SDL_Surface * to )
  1061. {
  1062. CIntObject::showAll(to);
  1063. //blit texts
  1064. if(type != CMenuScreen::campaignList)
  1065. {
  1066. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 24, 400, FONT_SMALL, zwykly, to); //Allies
  1067. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 190, 400, FONT_SMALL, zwykly, to); //Enemies
  1068. printAtLoc(CGI->generaltexth->allTexts[494], 33, 430, FONT_SMALL, tytulowy, to);//"Map Diff:"
  1069. printAtLoc(CGI->generaltexth->allTexts[492] + ":", 133,430, FONT_SMALL, tytulowy, to); //player difficulty
  1070. printAtLoc(CGI->generaltexth->allTexts[218] + ":", 290,430, FONT_SMALL, tytulowy, to); //"Rating:"
  1071. printAtLoc(CGI->generaltexth->allTexts[495], 26, 22, FONT_SMALL, tytulowy, to); //Scenario Name:
  1072. printAtLoc(CGI->generaltexth->allTexts[496], 26, 132, FONT_SMALL, tytulowy, to); //Scenario Description:
  1073. printAtLoc(CGI->generaltexth->allTexts[497], 26, 283, FONT_SMALL, tytulowy, to); //Victory Condition:
  1074. printAtLoc(CGI->generaltexth->allTexts[498], 26, 339, FONT_SMALL, tytulowy, to); //Loss Condition:
  1075. }
  1076. if(curMap)
  1077. {
  1078. if(type != CMenuScreen::campaignList)
  1079. {
  1080. if(type != CMenuScreen::newGame)
  1081. {
  1082. for (int i = 0; i < difficulty->buttons.size(); i++)
  1083. {
  1084. //if(i == curMap->difficulty)
  1085. // difficulty->buttons[i]->state = 3;
  1086. //else
  1087. // difficulty->buttons[i]->state = 2;
  1088. difficulty->buttons[i]->showAll(to);
  1089. }
  1090. }
  1091. //victory conditions
  1092. int temp = curMap->mapHeader->victoryCondition.condition+1;
  1093. if (temp>20) temp=0;
  1094. std::string sss = CGI->generaltexth->victoryConditions[temp];
  1095. if (temp && curMap->mapHeader->victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  1096. printAtLoc(sss, 60, 307, FONT_SMALL, zwykly, to);
  1097. //loss conditoins
  1098. temp = curMap->mapHeader->lossCondition.typeOfLossCon+1;
  1099. if (temp>20) temp=0;
  1100. sss = CGI->generaltexth->lossCondtions[temp];
  1101. printAtLoc(sss, 60, 366, FONT_SMALL, zwykly, to);
  1102. //difficulty
  1103. assert(curMap->mapHeader->difficulty <= 4);
  1104. std::string &diff = CGI->generaltexth->arraytxt[142 + curMap->mapHeader->difficulty];
  1105. printAtMiddleLoc(diff, 62, 472, FONT_SMALL, zwykly, to);
  1106. //selecting size icon
  1107. switch (curMap->mapHeader->width)
  1108. {
  1109. case 36:
  1110. temp=0;
  1111. break;
  1112. case 72:
  1113. temp=1;
  1114. break;
  1115. case 108:
  1116. temp=2;
  1117. break;
  1118. case 144:
  1119. temp=3;
  1120. break;
  1121. default:
  1122. temp=4;
  1123. break;
  1124. }
  1125. blitAtLoc(sizes->ourImages[temp].bitmap, 318, 22, to);
  1126. //conditions
  1127. temp = curMap->mapHeader->victoryCondition.condition;
  1128. if (temp>12) temp=11;
  1129. blitAtLoc(CGP->victory->ourImages[temp].bitmap, 24, 302, to); //victory cond descr
  1130. temp=curMap->mapHeader->lossCondition.typeOfLossCon;
  1131. if (temp>12) temp=3;
  1132. blitAtLoc(CGP->loss->ourImages[temp].bitmap, 24, 359, to); //loss cond
  1133. if(type == CMenuScreen::loadGame)
  1134. printToLoc((static_cast<const CMapInfo*>(curMap))->date,308,34, FONT_SMALL, zwykly, to);
  1135. //print flags
  1136. int fx=64, ex=244, myT;
  1137. //if (curMap->howManyTeams)
  1138. myT = curMap->mapHeader->players[playerColor].team;
  1139. //else
  1140. // myT = -1;
  1141. for (std::vector<PlayerSettings>::const_iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  1142. {
  1143. int *myx = ((i->color == playerColor || curMap->mapHeader->players[i->color].team == myT) ? &fx : &ex);
  1144. blitAtLoc(sFlags->ourImages[i->color].bitmap, *myx, 399, to);
  1145. *myx += sFlags->ourImages[i->color].bitmap->w;
  1146. }
  1147. std::string tob;
  1148. switch (curOpts->difficulty)
  1149. {
  1150. case 0:
  1151. tob="80%";
  1152. break;
  1153. case 1:
  1154. tob="100%";
  1155. break;
  1156. case 2:
  1157. tob="130%";
  1158. break;
  1159. case 3:
  1160. tob="160%";
  1161. break;
  1162. case 4:
  1163. tob="200%";
  1164. break;
  1165. }
  1166. printAtMiddleLoc(tob, 311, 472, FONT_SMALL, zwykly, to);
  1167. }
  1168. //blit description
  1169. std::string name;
  1170. if (type == CMenuScreen::campaignList)
  1171. {
  1172. name = curMap->campaignHeader->name;
  1173. }
  1174. else
  1175. {
  1176. name = curMap->mapHeader->name;
  1177. }
  1178. //name
  1179. if (name.length())
  1180. printAtLoc(name, 26, 39, FONT_BIG, tytulowy, to);
  1181. else
  1182. printAtLoc("Unnamed", 26, 39, FONT_BIG, tytulowy, to);
  1183. }
  1184. }
  1185. void InfoCard::changeSelection( const CMapInfo *to )
  1186. {
  1187. if(to && mapDescription)
  1188. {
  1189. if (type == CMenuScreen::campaignList)
  1190. mapDescription->setTxt(to->campaignHeader->description);
  1191. else
  1192. mapDescription->setTxt(to->mapHeader->description);
  1193. if(type != CMenuScreen::newGame && type != CMenuScreen::campaignList)
  1194. difficulty->select(curOpts->difficulty, 0);
  1195. }
  1196. GH.totalRedraw();
  1197. }
  1198. void InfoCard::clickRight( tribool down, bool previousState )
  1199. {
  1200. static const Rect flagArea(19, 397, 335, 23);
  1201. if(down && curMap && isItInLoc(flagArea, GH.current->motion.x, GH.current->motion.y))
  1202. showTeamsPopup();
  1203. }
  1204. void InfoCard::showTeamsPopup()
  1205. {
  1206. SDL_Surface *bmp = CMessage::drawBox1(256, 90 + 50 * curMap->mapHeader->howManyTeams);
  1207. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[657], 128, 30, FONT_MEDIUM, tytulowy, bmp); //{Team Alignments}
  1208. for(int i = 0; i < curMap->mapHeader->howManyTeams; i++)
  1209. {
  1210. std::vector<ui8> flags;
  1211. std::string hlp = CGI->generaltexth->allTexts[656]; //Team %d
  1212. hlp.replace(hlp.find("%d"), 2, boost::lexical_cast<std::string>(i+1));
  1213. CSDL_Ext::printAtMiddle(hlp, 128, 65 + 50*i, FONT_SMALL, zwykly, bmp);
  1214. for(int j = 0; j < PLAYER_LIMIT; j++)
  1215. if((curMap->mapHeader->players[j].canHumanPlay || curMap->mapHeader->players[j].canComputerPlay)
  1216. && curMap->mapHeader->players[j].team == i)
  1217. flags.push_back(j);
  1218. int curx = 128 - 9*flags.size();
  1219. for(int j = 0; j < flags.size(); j++)
  1220. {
  1221. blitAt(sFlags->ourImages[flags[j]].bitmap, curx, 75 + 50*i, bmp);
  1222. curx += 18;
  1223. }
  1224. }
  1225. GH.pushInt(new CInfoPopup(bmp, true));
  1226. }
  1227. OptionsTab::OptionsTab( CMenuScreen::EState Type)
  1228. :type(Type)
  1229. {
  1230. OBJ_CONSTRUCTION;
  1231. bg = new CPicture(BitmapHandler::loadBitmap("ADVOPTBK.bmp"), 0, 0, true);
  1232. pos = bg->pos;
  1233. if(type == CMenuScreen::newGame)
  1234. turnDuration = new CSlider(55, 551, 194, bind(&OptionsTab::setTurnLength, this, _1), 1, 11, 11, true, 1);
  1235. }
  1236. OptionsTab::~OptionsTab()
  1237. {
  1238. }
  1239. void OptionsTab::showAll( SDL_Surface * to )
  1240. {
  1241. CIntObject::showAll(to);
  1242. printAtMiddleLoc(CGI->generaltexth->allTexts[515], 222, 30, FONT_BIG, tytulowy, to);
  1243. printAtMiddleWBLoc(CGI->generaltexth->allTexts[516], 222, 58, FONT_SMALL, 55, zwykly, to); //Select starting options, handicap, and name for each player in the game.
  1244. printAtMiddleWBLoc(CGI->generaltexth->allTexts[517], 107, 102, FONT_SMALL, 14, tytulowy, to); //Player Name Handicap Type
  1245. printAtMiddleWBLoc(CGI->generaltexth->allTexts[518], 197, 102, FONT_SMALL, 10, tytulowy, to); //Starting Town
  1246. printAtMiddleWBLoc(CGI->generaltexth->allTexts[519], 273, 102, FONT_SMALL, 10, tytulowy, to); //Starting Hero
  1247. printAtMiddleWBLoc(CGI->generaltexth->allTexts[520], 349, 102, FONT_SMALL, 10, tytulowy, to); //Starting Bonus
  1248. printAtMiddleLoc(CGI->generaltexth->allTexts[521], 222, 538, FONT_SMALL, tytulowy, to); // Player Turn Duration
  1249. if(curOpts->turnTime)
  1250. {
  1251. std::ostringstream os;
  1252. os << (int)curOpts->turnTime << " Minutes";
  1253. printAtMiddleLoc(os.str(), 319,559, FONT_SMALL, zwykly, to);
  1254. }
  1255. else
  1256. printAtMiddleLoc("Unlimited",319,559, FONT_SMALL, zwykly, to);
  1257. }
  1258. void OptionsTab::nextCastle( int player, int dir )
  1259. {
  1260. PlayerSettings &s = curOpts->playerInfos[player];
  1261. si32 &cur = s.castle;
  1262. ui32 allowed = curMap->mapHeader->players[s.color].allowedFactions;
  1263. if (cur == -2) //no castle - no change
  1264. return;
  1265. if (cur == -1) //random => first/last available
  1266. {
  1267. int pom = (dir>0) ? (0) : (F_NUMBER-1); // last or first
  1268. for (;pom >= 0 && pom < F_NUMBER; pom+=dir)
  1269. {
  1270. if((1 << pom) & allowed)
  1271. {
  1272. cur=pom;
  1273. break;
  1274. }
  1275. }
  1276. }
  1277. else // next/previous available
  1278. {
  1279. for (;;)
  1280. {
  1281. cur+=dir;
  1282. if ((1 << cur) & allowed)
  1283. break;
  1284. if (cur >= F_NUMBER || cur<0)
  1285. {
  1286. double p1 = log((double)allowed) / log(2.0f)+0.000001f;
  1287. double check = p1 - ((int)p1);
  1288. if (check < 0.001)
  1289. cur = (int)p1;
  1290. else
  1291. cur = -1;
  1292. break;
  1293. }
  1294. }
  1295. }
  1296. if(s.hero >= 0)
  1297. s.hero = -1;
  1298. if(cur < 0 && s.bonus == bresource)
  1299. s.bonus = brandom;
  1300. entries[player]->selectButtons();
  1301. redraw();
  1302. }
  1303. void OptionsTab::nextHero( int player, int dir )
  1304. {
  1305. PlayerSettings &s = curOpts->playerInfos[player];
  1306. int old = s.hero;
  1307. if (s.castle < 0 || !s.human || s.hero == -2)
  1308. return;
  1309. if (s.hero == -1) //random => first/last available
  1310. {
  1311. int max = (s.castle*HEROES_PER_TYPE*2+15),
  1312. min = (s.castle*HEROES_PER_TYPE*2);
  1313. s.hero = nextAllowedHero(min,max,0,dir);
  1314. }
  1315. else
  1316. {
  1317. if(dir > 0)
  1318. s.hero = nextAllowedHero(s.hero,(s.castle*HEROES_PER_TYPE*2+16),1,dir);
  1319. else
  1320. s.hero = nextAllowedHero(s.castle*HEROES_PER_TYPE*2-1,s.hero,1,dir);
  1321. }
  1322. if(old != s.hero)
  1323. {
  1324. usedHeroes.erase(old);
  1325. usedHeroes.insert(s.hero);
  1326. redraw();
  1327. }
  1328. }
  1329. int OptionsTab::nextAllowedHero( int min, int max, int incl, int dir )
  1330. {
  1331. if(dir>0)
  1332. {
  1333. for(int i=min+incl; i<=max-incl; i++)
  1334. if(canUseThisHero(i))
  1335. return i;
  1336. }
  1337. else
  1338. {
  1339. for(int i=max-incl; i>=min+incl; i--)
  1340. if(canUseThisHero(i))
  1341. return i;
  1342. }
  1343. return -1;
  1344. }
  1345. bool OptionsTab::canUseThisHero( int ID )
  1346. {
  1347. //for(int i=0;i<CPG->ret.playerInfos.size();i++)
  1348. // if(CPG->ret.playerInfos[i].hero==ID) //hero is already taken
  1349. // return false;
  1350. return !vstd::contains(usedHeroes, ID) && curMap->mapHeader->allowedHeroes[ID];
  1351. }
  1352. void OptionsTab::nextBonus( int player, int dir )
  1353. {
  1354. PlayerSettings &s = curOpts->playerInfos[player];
  1355. si8 &ret = s.bonus += dir;
  1356. if (s.hero==-2 && !curMap->mapHeader->players[s.color].heroesNames.size() && ret==bartifact) //no hero - can't be artifact
  1357. {
  1358. if (dir<0)
  1359. ret=brandom;
  1360. else ret=bgold;
  1361. }
  1362. if(ret > bresource)
  1363. ret = brandom;
  1364. if(ret < brandom)
  1365. ret = bresource;
  1366. if (s.castle==-1 && ret==bresource) //random castle - can't be resource
  1367. {
  1368. if (dir<0)
  1369. ret=bgold;
  1370. else ret=brandom;
  1371. }
  1372. redraw();
  1373. }
  1374. void OptionsTab::changeSelection( const CMapHeader *to )
  1375. {
  1376. for(int i = 0; i < entries.size(); i++)
  1377. {
  1378. children -= entries[i];
  1379. delete entries[i];
  1380. }
  1381. entries.clear();
  1382. usedHeroes.clear();
  1383. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1384. for(int i = 0; i < curOpts->playerInfos.size(); i++)
  1385. {
  1386. entries.push_back(new PlayerOptionsEntry(this, curOpts->playerInfos[i]));
  1387. const std::vector<SheroName> &heroes = curMap->mapHeader->players[curOpts->playerInfos[i].color].heroesNames;
  1388. for(size_t hi=0; hi<heroes.size(); hi++)
  1389. usedHeroes.insert(heroes[hi].heroID);
  1390. }
  1391. }
  1392. void OptionsTab::setTurnLength( int npos )
  1393. {
  1394. static const int times[] = {1, 2, 4, 6, 8, 10, 15, 20, 25, 30, 0};
  1395. amin(npos, ARRAY_COUNT(times) - 1);
  1396. curOpts->turnTime = times[npos];
  1397. redraw();
  1398. }
  1399. void OptionsTab::flagPressed( int player )
  1400. {
  1401. static std::pair<int, int> playerToRestore(-1, -1); //<color serial, player name serial>
  1402. PlayerSettings &clicked = curOpts->playerInfos[player];
  1403. PlayerSettings *old = NULL;
  1404. if(playerNames.size() == 1) //single player -> swap
  1405. {
  1406. if(player == playerSerial) //that color is already selected, no action needed
  1407. return;
  1408. old = &curOpts->playerInfos[playerSerial];
  1409. std::swap(old->human, clicked.human);
  1410. std::swap(old->name, clicked.name);
  1411. playerColor = clicked.color;
  1412. playerSerial = player;
  1413. }
  1414. else
  1415. {
  1416. //identify clicked player
  1417. int curNameID = clicked.human
  1418. ? vstd::findPos(playerNames, clicked.name)
  1419. : -1;
  1420. if(curNameID >= 0 && playerToRestore.second == curNameID) //player to restore is about to being replaced -> put him back to the old place
  1421. {
  1422. PlayerSettings &restPos = curOpts->playerInfos[playerToRestore.first];
  1423. setPlayer(restPos, playerToRestore.second);
  1424. playerToRestore.first = playerToRestore.second = 0;
  1425. }
  1426. //who will be put here?
  1427. if(curNameID < 0) //if possible replace computer with unallocated player
  1428. {
  1429. for(int i = 0; i < playerNames.size(); i++)
  1430. {
  1431. if(!curOpts->getPlayersSettings(playerNames[i]))
  1432. {
  1433. curNameID = i-1; //-1 because it'll incremented soon
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. setPlayer(clicked, ++curNameID); //simply next player
  1439. //if that player was somewhere else, we need to replace him with computer
  1440. if(curNameID < playerNames.size())
  1441. {
  1442. for(std::vector<PlayerSettings>::iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  1443. {
  1444. if(i->serial != player && i->name == playerNames[curNameID])
  1445. {
  1446. assert(i->human);
  1447. playerToRestore.first = i->serial;
  1448. playerToRestore.second = vstd::findPos(playerNames, i->name);
  1449. setPlayer(*i, -1); //set computer
  1450. old = &*i;
  1451. break;
  1452. }
  1453. }
  1454. }
  1455. }
  1456. entries[clicked.serial]->selectButtons();
  1457. if(old)
  1458. {
  1459. entries[old->serial]->selectButtons();
  1460. if(!entries[playerSerial]->fixedHero)
  1461. old->hero = -1;
  1462. }
  1463. GH.totalRedraw();
  1464. }
  1465. OptionsTab::PlayerOptionsEntry::PlayerOptionsEntry( OptionsTab *owner, PlayerSettings &S)
  1466. :s(S)
  1467. {
  1468. OBJ_CONSTRUCTION;
  1469. defActions |= SHARE_POS;
  1470. pos = parent->pos + Point(54, 122 + s.serial*50);
  1471. static const char *flags[] = {"AOFLGBR.DEF", "AOFLGBB.DEF", "AOFLGBY.DEF", "AOFLGBG.DEF",
  1472. "AOFLGBO.DEF", "AOFLGBP.DEF", "AOFLGBT.DEF", "AOFLGBS.DEF"};
  1473. static const char *bgs[] = {"ADOPRPNL.bmp", "ADOPBPNL.bmp", "ADOPYPNL.bmp", "ADOPGPNL.bmp",
  1474. "ADOPOPNL.bmp", "ADOPPPNL.bmp", "ADOPTPNL.bmp", "ADOPSPNL.bmp"};
  1475. bg = new CPicture(BitmapHandler::loadBitmap(bgs[s.color]), 0, 0, true);
  1476. if(owner->type == CMenuScreen::newGame)
  1477. {
  1478. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[132], bind(&OptionsTab::nextCastle, owner, s.serial, -1), 107, 5, "ADOPLFA.DEF");
  1479. btns[1] = new AdventureMapButton(CGI->generaltexth->zelp[133], bind(&OptionsTab::nextCastle, owner, s.serial, +1), 168, 5, "ADOPRTA.DEF");
  1480. btns[2] = new AdventureMapButton(CGI->generaltexth->zelp[148], bind(&OptionsTab::nextHero, owner, s.serial, -1), 183, 5, "ADOPLFA.DEF");
  1481. btns[3] = new AdventureMapButton(CGI->generaltexth->zelp[149], bind(&OptionsTab::nextHero, owner, s.serial, +1), 244, 5, "ADOPRTA.DEF");
  1482. btns[4] = new AdventureMapButton(CGI->generaltexth->zelp[164], bind(&OptionsTab::nextBonus, owner, s.serial, -1), 259, 5, "ADOPLFA.DEF");
  1483. btns[5] = new AdventureMapButton(CGI->generaltexth->zelp[165], bind(&OptionsTab::nextBonus, owner, s.serial, +1), 320, 5, "ADOPRTA.DEF");
  1484. }
  1485. else
  1486. for(int i = 0; i < 6; i++)
  1487. btns[i] = NULL;
  1488. fixedHero = s.hero != -1; //if we doesn't start with "random hero" it must be fixed or none
  1489. selectButtons(false);
  1490. assert(curMap && curMap->mapHeader);
  1491. const PlayerInfo &p = curMap->mapHeader->players[s.color];
  1492. assert(p.canComputerPlay || p.canHumanPlay); //someone must be able to control this player
  1493. if(p.canHumanPlay && p.canComputerPlay)
  1494. whoCanPlay = HUMAN_OR_CPU;
  1495. else if(p.canComputerPlay)
  1496. whoCanPlay = CPU;
  1497. else
  1498. whoCanPlay = HUMAN;
  1499. if(owner->type != CMenuScreen::scenarioInfo && curMap->mapHeader->players[s.color].canHumanPlay)
  1500. {
  1501. flag = new AdventureMapButton(CGI->generaltexth->zelp[180], bind(&OptionsTab::flagPressed, owner, s.serial), -43, 2, flags[s.color]);
  1502. flag->hoverable = true;
  1503. }
  1504. else
  1505. flag = NULL;
  1506. defActions &= ~SHARE_POS;
  1507. town = new SelectedBox(TOWN, s.serial);
  1508. town->pos += pos + Point(119, 2);
  1509. hero = new SelectedBox(HERO, s.serial);
  1510. hero->pos += pos + Point(195, 2);
  1511. bonus = new SelectedBox(BONUS, s.serial);
  1512. bonus->pos += pos + Point(271, 2);
  1513. }
  1514. void OptionsTab::PlayerOptionsEntry::showAll( SDL_Surface * to )
  1515. {
  1516. CIntObject::showAll(to);
  1517. printAtMiddleLoc(s.name, 55, 10, FONT_SMALL, zwykly, to);
  1518. printAtMiddleWBLoc(CGI->generaltexth->arraytxt[206+whoCanPlay], 28, 34, FONT_TINY, 6, zwykly, to);
  1519. }
  1520. void OptionsTab::PlayerOptionsEntry::selectButtons(bool onlyHero)
  1521. {
  1522. if(!btns[0])
  1523. return;
  1524. if(!onlyHero && s.castle != -1)
  1525. {
  1526. btns[0]->disable();
  1527. btns[1]->disable();
  1528. }
  1529. if(fixedHero || !s.human || s.castle < 0)
  1530. {
  1531. btns[2]->disable();
  1532. btns[3]->disable();
  1533. }
  1534. else
  1535. {
  1536. btns[2]->enable(active);
  1537. btns[3]->enable(active);
  1538. }
  1539. }
  1540. void OptionsTab::SelectedBox::showAll( SDL_Surface * to )
  1541. {
  1542. //PlayerSettings &s = curOpts->playerInfos[player];
  1543. SDL_Surface *toBlit = getImg();
  1544. const std::string *toPrint = getText();
  1545. blitAt(toBlit, pos, to);
  1546. printAtMiddleLoc(*toPrint, 23, 39, FONT_TINY, zwykly, to);
  1547. }
  1548. OptionsTab::SelectedBox::SelectedBox( SelType Which, ui8 Player )
  1549. :which(Which), player(Player)
  1550. {
  1551. SDL_Surface *img = getImg();
  1552. pos.w = img->w;
  1553. pos.h = img->h;
  1554. used = RCLICK;
  1555. }
  1556. SDL_Surface * OptionsTab::SelectedBox::getImg() const
  1557. {
  1558. const PlayerSettings &s = curOpts->playerInfos[player];
  1559. switch(which)
  1560. {
  1561. case TOWN:
  1562. if (s.castle < F_NUMBER && s.castle >= 0)
  1563. return graphics->getPic(s.castle, true, false);
  1564. else if (s.castle == -1)
  1565. return CGP->rTown;
  1566. else if (s.castle == -2)
  1567. return CGP->nTown;
  1568. case HERO:
  1569. if (s.hero == -1)
  1570. {
  1571. return CGP->rHero;
  1572. }
  1573. else if (s.hero == -2)
  1574. {
  1575. if(s.heroPortrait >= 0)
  1576. return graphics->portraitSmall[s.heroPortrait];
  1577. else
  1578. return CGP->nHero;
  1579. }
  1580. else
  1581. {
  1582. return graphics->portraitSmall[s.hero];
  1583. }
  1584. break;
  1585. case BONUS:
  1586. {
  1587. int pom;
  1588. switch (s.bonus)
  1589. {
  1590. case -1:
  1591. pom=10;
  1592. break;
  1593. case 0:
  1594. pom=9;
  1595. break;
  1596. case 1:
  1597. pom=8;
  1598. break;
  1599. case 2:
  1600. pom=CGI->townh->towns[s.castle].bonus;
  1601. break;
  1602. default:
  1603. assert(0);
  1604. }
  1605. return CGP->bonuses->ourImages[pom].bitmap;
  1606. }
  1607. default:
  1608. return NULL;
  1609. }
  1610. }
  1611. const std::string * OptionsTab::SelectedBox::getText() const
  1612. {
  1613. const PlayerSettings &s = curOpts->playerInfos[player];
  1614. switch(which)
  1615. {
  1616. case TOWN:
  1617. if (s.castle < F_NUMBER && s.castle >= 0)
  1618. return &CGI->townh->towns[s.castle].Name();
  1619. else if (s.castle == -1)
  1620. return &CGI->generaltexth->allTexts[522];
  1621. else if (s.castle == -2)
  1622. return &CGI->generaltexth->allTexts[523];
  1623. case HERO:
  1624. if (s.hero == -1)
  1625. return &CGI->generaltexth->allTexts[522];
  1626. else if (s.hero == -2)
  1627. {
  1628. if(s.heroPortrait >= 0)
  1629. {
  1630. if(s.heroName.length())
  1631. return &s.heroName;
  1632. else
  1633. return &CGI->heroh->heroes[s.heroPortrait]->name;
  1634. }
  1635. else
  1636. return &CGI->generaltexth->allTexts[523];
  1637. }
  1638. else
  1639. {
  1640. //if(s.heroName.length())
  1641. // return &s.heroName;
  1642. //else
  1643. return &CGI->heroh->heroes[s.hero]->name;
  1644. }
  1645. case BONUS:
  1646. switch (s.bonus)
  1647. {
  1648. case -1:
  1649. return &CGI->generaltexth->allTexts[522];
  1650. default:
  1651. return &CGI->generaltexth->arraytxt[214 + s.bonus];
  1652. }
  1653. default:
  1654. return NULL;
  1655. }
  1656. }
  1657. void OptionsTab::SelectedBox::clickRight( tribool down, bool previousState )
  1658. {
  1659. if(indeterminate(down) || !down) return;
  1660. const PlayerSettings &s = curOpts->playerInfos[player];
  1661. SDL_Surface *bmp = NULL;
  1662. const std::string *title = NULL, *subTitle = NULL;
  1663. subTitle = getText();
  1664. int val;
  1665. switch(which)
  1666. {
  1667. case TOWN:
  1668. val = s.castle;
  1669. break;
  1670. case HERO:
  1671. val = s.hero;
  1672. if(val == -2) //none => we may have some preset info
  1673. {
  1674. int p9 = curMap->mapHeader->players[s.color].p9;
  1675. if(p9 != 255 && curOpts->playerInfos[player].heroPortrait >= 0)
  1676. val = p9;
  1677. }
  1678. break;
  1679. case BONUS:
  1680. val = s.bonus;
  1681. break;
  1682. }
  1683. if(val == -1 || which == BONUS) //random or bonus box
  1684. {
  1685. bmp = CMessage::drawBox1(256, 190);
  1686. std::string *description = NULL;
  1687. switch(which)
  1688. {
  1689. case TOWN:
  1690. title = &CGI->generaltexth->allTexts[103];
  1691. description = &CGI->generaltexth->allTexts[104];
  1692. break;
  1693. case HERO:
  1694. title = &CGI->generaltexth->allTexts[101];
  1695. description = &CGI->generaltexth->allTexts[102];
  1696. break;
  1697. case BONUS:
  1698. {
  1699. switch(val)
  1700. {
  1701. case brandom:
  1702. title = &CGI->generaltexth->allTexts[86]; //{Random Bonus}
  1703. description = &CGI->generaltexth->allTexts[94]; //Gold, wood and ore, or an artifact is randomly chosen as your starting bonus
  1704. break;
  1705. case bartifact:
  1706. title = &CGI->generaltexth->allTexts[83]; //{Artifact Bonus}
  1707. description = &CGI->generaltexth->allTexts[90]; //An artifact is randomly chosen and equipped to your starting hero
  1708. break;
  1709. case bgold:
  1710. title = &CGI->generaltexth->allTexts[84]; //{Gold Bonus}
  1711. subTitle = &CGI->generaltexth->allTexts[87]; //500-1000
  1712. description = &CGI->generaltexth->allTexts[92]; //At the start of the game, 500-1000 gold is added to your Kingdom's resource pool
  1713. break;
  1714. case bresource:
  1715. {
  1716. title = &CGI->generaltexth->allTexts[85]; //{Resource Bonus}
  1717. switch(CGI->townh->towns[s.castle].primaryRes)
  1718. {
  1719. case 1:
  1720. subTitle = &CGI->generaltexth->allTexts[694];
  1721. description = &CGI->generaltexth->allTexts[690];
  1722. break;
  1723. case 3:
  1724. subTitle = &CGI->generaltexth->allTexts[695];
  1725. description = &CGI->generaltexth->allTexts[691];
  1726. break;
  1727. case 4:
  1728. subTitle = &CGI->generaltexth->allTexts[692];
  1729. description = &CGI->generaltexth->allTexts[688];
  1730. break;
  1731. case 5:
  1732. subTitle = &CGI->generaltexth->allTexts[693];
  1733. description = &CGI->generaltexth->allTexts[689];
  1734. break;
  1735. case 127:
  1736. subTitle = &CGI->generaltexth->allTexts[89]; //5-10 wood / 5-10 ore
  1737. 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
  1738. break;
  1739. }
  1740. }
  1741. break;
  1742. }
  1743. }
  1744. break;
  1745. }
  1746. if(description)
  1747. CSDL_Ext::printAtMiddleWB(*description, 125, 145, FONT_SMALL, 37, zwykly, bmp);
  1748. }
  1749. else if(val == -2)
  1750. {
  1751. return;
  1752. }
  1753. else if(which == TOWN)
  1754. {
  1755. bmp = CMessage::drawBox1(256, 319);
  1756. title = &CGI->generaltexth->allTexts[80];
  1757. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[79], 135, 137, FONT_MEDIUM, tytulowy, bmp);
  1758. const CTown &t = CGI->townh->towns[val];
  1759. //print creatures
  1760. int x = 60, y = 159;
  1761. for(int i = 0; i < 7; i++)
  1762. {
  1763. int c = t.basicCreatures[i];
  1764. blitAt(graphics->smallImgs[c], x, y, bmp);
  1765. CSDL_Ext::printAtMiddleWB(CGI->creh->creatures[c]->nameSing, x + 16, y + 45, FONT_TINY, 10, zwykly, bmp);
  1766. if(i == 2)
  1767. {
  1768. x = 40;
  1769. y += 76;
  1770. }
  1771. else
  1772. {
  1773. x += 52;
  1774. }
  1775. }
  1776. }
  1777. else if(val >= 0)
  1778. {
  1779. const CHero *h = CGI->heroh->heroes[val];
  1780. bmp = CMessage::drawBox1(320, 255);
  1781. title = &CGI->generaltexth->allTexts[77];
  1782. CSDL_Ext::printAtMiddle(*title, 167, 36, FONT_MEDIUM, tytulowy, bmp);
  1783. CSDL_Ext::printAtMiddle(*subTitle + " - " + h->heroClass->name, 160, 99, FONT_SMALL, zwykly, bmp);
  1784. blitAt(getImg(), 136, 56, bmp);
  1785. //print specialty
  1786. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[78], 166, 132, FONT_MEDIUM, tytulowy, bmp);
  1787. blitAt(graphics->un44->ourImages[val].bitmap, 140, 150, bmp);
  1788. CSDL_Ext::printAtMiddle(CGI->generaltexth->hTxts[val].bonusName, 166, 203, FONT_SMALL, zwykly, bmp);
  1789. GH.pushInt(new CInfoPopup(bmp, true));
  1790. return;
  1791. }
  1792. if(title)
  1793. CSDL_Ext::printAtMiddle(*title, 135, 36, FONT_MEDIUM, tytulowy, bmp);
  1794. if(subTitle)
  1795. CSDL_Ext::printAtMiddle(*subTitle, 127, 103, FONT_SMALL, zwykly, bmp);
  1796. blitAt(getImg(), 104, 60, bmp);
  1797. GH.pushInt(new CInfoPopup(bmp, true));
  1798. }
  1799. CScenarioInfo::CScenarioInfo(const CMapHeader *mapHeader, const StartInfo *startInfo)
  1800. {
  1801. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1802. for(size_t i = 0; i < startInfo->playerInfos.size(); i++)
  1803. {
  1804. if(startInfo->playerInfos[i].human)
  1805. {
  1806. playerColor = startInfo->playerInfos[i].color;
  1807. playerSerial = i;
  1808. }
  1809. }
  1810. pos.w = 762;
  1811. pos.h = 584;
  1812. center(pos);
  1813. assert(LOCPLINT);
  1814. assert(!curOpts);
  1815. curOpts = new StartInfo(*LOCPLINT->cb->getStartInfo()); //deleted by clearInfo
  1816. assert(!curMap);
  1817. curMap = mapInfoFromGame(); //deleted by clearInfo
  1818. setPlayersFromGame();
  1819. card = new InfoCard(CMenuScreen::scenarioInfo);
  1820. opt = new OptionsTab(CMenuScreen::scenarioInfo);
  1821. opt->changeSelection(0);
  1822. card->difficulty->select(startInfo->difficulty, 0);
  1823. back = new AdventureMapButton("", CGI->generaltexth->zelp[105].second, bind(&CGuiHandler::popIntTotally, &GH, this), 584, 535, "SCNRBACK.DEF", SDLK_ESCAPE);
  1824. }
  1825. CScenarioInfo::~CScenarioInfo()
  1826. {
  1827. clearInfo();
  1828. }
  1829. bool mapSorter::operator()(const CMapInfo *aaa, const CMapInfo *bbb)
  1830. {
  1831. const CMapHeader * a = aaa->mapHeader,
  1832. * b = bbb->mapHeader;
  1833. if(a && b) //if we are sorting scenarios
  1834. {
  1835. switch (sortBy)
  1836. {
  1837. case _format: //by map format (RoE, WoG, etc)
  1838. return (a->version<b->version);
  1839. break;
  1840. case _loscon: //by loss conditions
  1841. return (a->lossCondition.typeOfLossCon<b->lossCondition.typeOfLossCon);
  1842. break;
  1843. case _playerAm: //by player amount
  1844. int playerAmntB,humenPlayersB,playerAmntA,humenPlayersA;
  1845. playerAmntB=humenPlayersB=playerAmntA=humenPlayersA=0;
  1846. for (int i=0;i<8;i++)
  1847. {
  1848. if (a->players[i].canHumanPlay) {playerAmntA++;humenPlayersA++;}
  1849. else if (a->players[i].canComputerPlay) {playerAmntA++;}
  1850. if (b->players[i].canHumanPlay) {playerAmntB++;humenPlayersB++;}
  1851. else if (b->players[i].canComputerPlay) {playerAmntB++;}
  1852. }
  1853. if (playerAmntB!=playerAmntA)
  1854. return (playerAmntA<playerAmntB);
  1855. else
  1856. return (humenPlayersA<humenPlayersB);
  1857. break;
  1858. case _size: //by size of map
  1859. return (a->width<b->width);
  1860. break;
  1861. case _viccon: //by victory conditions
  1862. return (a->victoryCondition.condition < b->victoryCondition.condition);
  1863. break;
  1864. case _name: //by name
  1865. return (a->name<b->name);
  1866. break;
  1867. default:
  1868. return (a->name<b->name);
  1869. break;
  1870. }
  1871. }
  1872. else //if we are sorting campaigns
  1873. {
  1874. switch(sortBy)
  1875. {
  1876. case _numOfMaps: //by number of maps in campaign
  1877. return CGI->generaltexth->campaignRegionNames[ aaa->campaignHeader->mapVersion ].size() <
  1878. CGI->generaltexth->campaignRegionNames[ bbb->campaignHeader->mapVersion ].size();
  1879. break;
  1880. case _name: //by name
  1881. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  1882. break;
  1883. default:
  1884. return aaa->campaignHeader->name < bbb->campaignHeader->name;
  1885. break;
  1886. }
  1887. }
  1888. }
  1889. CMultiMode::CMultiMode()
  1890. {
  1891. OBJ_CONSTRUCTION;
  1892. bg = new CPicture("MUPOPUP.bmp");
  1893. bg->convertToScreenBPP(); //so we could draw without problems
  1894. blitAt(CPicture("MUMAP.bmp"), 16, 77, *bg); //blit img
  1895. pos = bg->center(); //center, window has size of bg graphic
  1896. bar = new CGStatusBar(new CPicture(Rect(7, 465, 440, 18), 0));//226, 472
  1897. txt = new CTextInput(Rect(19, 436, 334, 16), *bg);
  1898. txt->setText(CGI->generaltexth->allTexts[434]); //Player
  1899. btns[0] = new AdventureMapButton(CGI->generaltexth->zelp[266], bind(&CMultiMode::openHotseat, this), 373, 78, "MUBHOT.DEF");
  1900. btns[6] = new AdventureMapButton(CGI->generaltexth->zelp[288], bind(&CGuiHandler::popIntTotally, ref(GH), this), 373, 424, "MUBCANC.DEF", SDLK_ESCAPE);
  1901. }
  1902. void CMultiMode::openHotseat()
  1903. {
  1904. GH.pushInt(new CHotSeatPlayers(txt->text));
  1905. }
  1906. CHotSeatPlayers::CHotSeatPlayers(const std::string &firstPlayer)
  1907. {
  1908. OBJ_CONSTRUCTION;
  1909. bg = new CPicture("MUHOTSEA.bmp");
  1910. bg->convertToScreenBPP(); //so we could draw without problems
  1911. bg->printAtMiddleWBLoc(CGI->generaltexth->allTexts[446], 185, 55, FONT_BIG, 50, zwykly, *bg); //HOTSEAT Please enter names
  1912. pos = bg->center(); //center, window has size of bg graphic
  1913. for(int i = 0; i < ARRAY_COUNT(txt); i++)
  1914. txt[i] = new CTextInput(Rect(60, 85 + i*30, 280, 16), *bg);
  1915. txt[0]->setText(firstPlayer);
  1916. txt[0]->giveFocus();
  1917. ok = new AdventureMapButton(CGI->generaltexth->zelp[560], bind(&CHotSeatPlayers::enterSelectionScreen, this), 95, 338, "MUBCHCK.DEF", SDLK_RETURN);
  1918. cancel = new AdventureMapButton(CGI->generaltexth->zelp[561], bind(&CGuiHandler::popIntTotally, ref(GH), this), 205, 338, "MUBCANC.DEF", SDLK_ESCAPE);
  1919. bar = new CGStatusBar(new CPicture(Rect(7, 381, 348, 18), 0));//226, 472
  1920. }
  1921. void CHotSeatPlayers::enterSelectionScreen()
  1922. {
  1923. playerNames.clear();
  1924. for(int i = 0; i < ARRAY_COUNT(txt); i++)
  1925. if(txt[i]->text.length())
  1926. playerNames.push_back(txt[i]->text);
  1927. GH.popInts(2);
  1928. GH.pushInt(new CSelectionScreen(CMenuScreen::newGame));
  1929. }
  1930. CBonusSelection::CBonusSelection( const CCampaign * _ourCampaign, int _whichMap )
  1931. : ourCampaign(_ourCampaign), whichMap(_whichMap), highlightedRegion(NULL), ourHeader(NULL)
  1932. {
  1933. OBJ_CONSTRUCTION;
  1934. static const std::string bgNames [] = {"E1_BG.BMP", "G2_BG.BMP", "E2_BG.BMP", "G1_BG.BMP", "G3_BG.BMP", "N1_BG.BMP",
  1935. "S1_BG.BMP", "BR_BG.BMP", "IS_BG.BMP", "KR_BG.BMP", "NI_BG.BMP", "TA_BG.BMP", "AR_BG.BMP", "HS_BG.BMP",
  1936. "BB_BG.BMP", "NB_BG.BMP", "EL_BG.BMP", "RN_BG.BMP", "UA_BG.BMP", "SP_BG.BMP"};
  1937. loadPositionsOfGraphics();
  1938. background = BitmapHandler::loadBitmap(bgNames[ourCampaign->header.mapVersion]);
  1939. SDL_Surface * panel = BitmapHandler::loadBitmap("CAMPBRF.BMP");
  1940. blitAt(panel, 456, 6, background);
  1941. startB = new AdventureMapButton("", "", bind(&CBonusSelection::startMap, this), 475, 536, "SCNRBEG.DEF", SDLK_RETURN);
  1942. backB = new AdventureMapButton("", "", bind(&CBonusSelection::goBack, this), 624, 536, "SCNRBACK.DEF", SDLK_ESCAPE);
  1943. //campaign name
  1944. if (ourCampaign->header.name.length())
  1945. printAtLoc(ourCampaign->header.name, 481, 28, FONT_BIG, tytulowy, background);
  1946. else
  1947. printAtLoc("Unnamed", 481, 28, FONT_BIG, tytulowy, background);
  1948. //map size icon
  1949. sizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  1950. //campaign description
  1951. printAtLoc(CGI->generaltexth->allTexts[38], 481, 63, FONT_SMALL, tytulowy, background);
  1952. // std::vector<std::string> *desc = CMessage::breakText(ourCampaign->header.description, 45);
  1953. // for (int i=0; i<desc->size() ;i++)
  1954. // printAtLoc((*desc)[i], 481, 86 + i*16, FONT_SMALL, zwykly, background);
  1955. // delete desc;
  1956. //set left part of window
  1957. for (int g=0; g<ourCampaign->scenarios.size(); ++g)
  1958. {
  1959. if(ourCampaign->conquerable(g))
  1960. {
  1961. regions.push_back(new CRegion(this, true, true, g));
  1962. regions[regions.size()-1]->rclickText = ourCampaign->scenarios[g].regionText;
  1963. if (highlightedRegion == NULL)
  1964. {
  1965. highlightedRegion = regions.back();
  1966. selectMap(g);
  1967. }
  1968. }
  1969. else if (ourCampaign->scenarios[g].conquered) //display as striped
  1970. {
  1971. regions.push_back(new CRegion(this, false, false, g));
  1972. regions[regions.size()-1]->rclickText = ourCampaign->scenarios[g].regionText;
  1973. }
  1974. }
  1975. //allies / enemies
  1976. printAtLoc(CGI->generaltexth->allTexts[390] + ":", 486, 407, FONT_SMALL, zwykly, background); //Allies
  1977. printAtLoc(CGI->generaltexth->allTexts[391] + ":", 619, 407, FONT_SMALL, zwykly, background); //Enemies
  1978. int fx=64, ex=244, myT;
  1979. myT = ourHeader->players[playerColor].team;
  1980. /*for (std::vector<PlayerSettings>::const_iterator i = curOpts->playerInfos.begin(); i != curOpts->playerInfos.end(); i++)
  1981. {
  1982. int *myx = ((i->color == playerColor || ourHeader.players[i->color].team == myT) ? &fx : &ex);
  1983. blitAtLoc(sFlags->ourImages[i->color].bitmap, *myx, 399, to);
  1984. *myx += sFlags->ourImages[i->color].bitmap->w;
  1985. }*/
  1986. SDL_FreeSurface(panel);
  1987. //bonus choosing
  1988. printAtLoc(CGI->generaltexth->allTexts[71], 510, 431, FONT_MEDIUM, zwykly, background); //Choose a bonus:
  1989. //difficulty
  1990. printAtLoc("Difficulty", 691, 431, FONT_MEDIUM, zwykly, background); //Difficulty
  1991. }
  1992. CBonusSelection::~CBonusSelection()
  1993. {
  1994. SDL_FreeSurface(background);
  1995. delete sizes;
  1996. delete ourHeader;
  1997. }
  1998. void CBonusSelection::goBack()
  1999. {
  2000. GH.popIntTotally(this);
  2001. }
  2002. void CBonusSelection::showAll( SDL_Surface * to )
  2003. {
  2004. CIntObject::showAll(to);
  2005. blitAt(background, pos.x, pos.y, to);
  2006. }
  2007. void CBonusSelection::loadPositionsOfGraphics()
  2008. {
  2009. std::ifstream is((GVCMIDirs.UserPath + "/config/campaign_regions.txt").c_str(), std::ios_base::binary | std::ios_base::in);
  2010. assert(is.is_open());
  2011. for (int g=0; g<CGI->generaltexth->campaignMapNames.size(); ++g)
  2012. {
  2013. SCampPositions sc;
  2014. is >> sc.campPrefix;
  2015. is >> sc.colorSuffixLength;
  2016. bool contReading = true;
  2017. while(contReading)
  2018. {
  2019. SCampPositions::SRegionDesc rd;
  2020. is >> rd.infix;
  2021. if(rd.infix == "END")
  2022. {
  2023. contReading = false;
  2024. }
  2025. else
  2026. {
  2027. is >> rd.xpos >> rd.ypos;
  2028. sc.regions.push_back(rd);
  2029. }
  2030. }
  2031. campDescriptions.push_back(sc);
  2032. }
  2033. }
  2034. void CBonusSelection::selectMap( int whichOne )
  2035. {
  2036. sInfo.difficulty = ourCampaign->scenarios[whichOne].difficulty;
  2037. sInfo.mapname = ourCampaign->header.filename;
  2038. sInfo.mode = 2;
  2039. //get header
  2040. int i = 0;
  2041. delete ourHeader;
  2042. ourHeader = new CMapHeader();
  2043. ourHeader->initFromMemory((const unsigned char*)ourCampaign->mapPieces[whichOne].c_str(), i);
  2044. CMapInfo *mapInfo = const_cast<CMapInfo *>(curMap);
  2045. mapInfo->mapHeader = ourHeader;
  2046. mapInfo->countPlayers();
  2047. mapInfo->mapHeader = NULL;
  2048. CSelectionScreen::updateStartInfo(curMap, sInfo, ourHeader);
  2049. sInfo.turnTime = 0;
  2050. sInfo.whichMapInCampaign = whichOne;
  2051. }
  2052. void CBonusSelection::show( SDL_Surface * to )
  2053. {
  2054. blitAt(background, pos.x, pos.y, to);
  2055. //map name
  2056. std::string mapDesc = ourHeader->description,
  2057. mapName = ourHeader->name;
  2058. if (mapName.length())
  2059. printAtLoc(mapName, 481, 219, FONT_BIG, tytulowy, to);
  2060. else
  2061. printAtLoc("Unnamed", 481, 219, FONT_BIG, tytulowy, to);
  2062. //map description
  2063. printAtLoc(CGI->generaltexth->allTexts[496], 481, 253, FONT_SMALL, tytulowy, to);
  2064. // std::vector<std::string> *desc = CMessage::breakText(mapDesc, 45);
  2065. // for (int i=0; i<desc->size(); i++)
  2066. // printAtLoc((*desc)[i], 481, 281 + i*16, FONT_SMALL, zwykly, to);
  2067. // delete desc;
  2068. //map size icon
  2069. int temp;
  2070. switch (ourHeader->width)
  2071. {
  2072. case 36:
  2073. temp=0;
  2074. break;
  2075. case 72:
  2076. temp=1;
  2077. break;
  2078. case 108:
  2079. temp=2;
  2080. break;
  2081. case 144:
  2082. temp=3;
  2083. break;
  2084. default:
  2085. temp=4;
  2086. break;
  2087. }
  2088. blitAtLoc(sizes->ourImages[temp].bitmap, 735, 26, to);
  2089. CIntObject::show(to);
  2090. }
  2091. void CBonusSelection::updateBonusSelection()
  2092. {
  2093. //graphics:
  2094. //spell - SPELLBON.DEF
  2095. //monster - TWCRPORT.DEF
  2096. //building - ?
  2097. //artifact - ARTIFBON.DEF
  2098. //spell scroll - SPELLBON.DEF
  2099. //prim skill - PSKILBON.DEF
  2100. //sec skill - SSKILBON.DEF
  2101. //resource - BORES.DEF
  2102. //player - ?
  2103. //hero -?
  2104. bonuses = new CHighlightableButtonsGroup(0);
  2105. {
  2106. static const char *bonDefs[] = {"SPELLBON.DEF", "TWCRPORT.DEF", "GSPBUT5.DEF", "ARTIFBON.DEF", "SPELLBON.DEF",
  2107. "PSKILBON.DEF", "SSKILBON.DEF", "BORES.DEF", "GSPBUT5.DEF", "GSPBUT5.DEF"};
  2108. for(int i = 0; i < 5; i++)
  2109. {
  2110. bonuses->addButton(new CHighlightableButton("", "", 0, 110 + i*32, 450, bonDefs[i], i));
  2111. bonuses->buttons.back()->pos += Point(68, 0);
  2112. }
  2113. }
  2114. }
  2115. void CBonusSelection::startMap()
  2116. {
  2117. StartInfo *si = new StartInfo(sInfo);
  2118. GH.popInts(3);
  2119. curOpts = NULL;
  2120. ::startGame(si);
  2121. }
  2122. CBonusSelection::CRegion::CRegion( CBonusSelection * _owner, bool _accessible, bool _selectable, int _myNumber )
  2123. : owner(_owner), accessible(_accessible), selectable(_selectable), myNumber(_myNumber)
  2124. {
  2125. OBJ_CONSTRUCTION;
  2126. used = LCLICK | RCLICK;
  2127. static const std::string colors[2][8] = {
  2128. {"R", "B", "N", "G", "O", "V", "T", "P"},
  2129. {"Re", "Bl", "Br", "Gr", "Or", "Vi", "Te", "Pi"}};
  2130. const SCampPositions & campDsc = owner->campDescriptions[owner->ourCampaign->header.mapVersion];
  2131. const SCampPositions::SRegionDesc & desc = campDsc.regions[myNumber];
  2132. pos.x = desc.xpos;
  2133. pos.y = desc.ypos;
  2134. //loading of graphics
  2135. std::string prefix = campDsc.campPrefix + desc.infix + "_";
  2136. std::string suffix = colors[campDsc.colorSuffixLength - 1][owner->ourCampaign->scenarios[myNumber].regionColor];
  2137. static const std::string infix [] = {"En", "Se", "Co"};
  2138. for (int g = 0; g < ARRAY_COUNT(infix); g++)
  2139. {
  2140. graphics[g] = BitmapHandler::loadBitmap(prefix + infix[g] + suffix + ".BMP");
  2141. }
  2142. pos.w = graphics[0]->w;
  2143. pos.h = graphics[0]->h;
  2144. }
  2145. CBonusSelection::CRegion::~CRegion()
  2146. {
  2147. for (int g=0; g<ARRAY_COUNT(graphics); ++g)
  2148. {
  2149. SDL_FreeSurface(graphics[g]);
  2150. }
  2151. }
  2152. void CBonusSelection::CRegion::clickLeft( tribool down, bool previousState )
  2153. {
  2154. //select if selectable & clicked inside our graphic
  2155. if ( indeterminate(down) )
  2156. {
  2157. return;
  2158. }
  2159. if( !down && selectable && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) )
  2160. {
  2161. owner->selectMap(myNumber);
  2162. owner->highlightedRegion = this;
  2163. }
  2164. }
  2165. void CBonusSelection::CRegion::clickRight( tribool down, bool previousState )
  2166. {
  2167. //show r-click text
  2168. if( down && !CSDL_Ext::isTransparent(graphics[0], GH.current->motion.x-pos.x, GH.current->motion.y-pos.y) &&
  2169. rclickText.size() )
  2170. {
  2171. CRClickPopup::createAndPush(rclickText);
  2172. }
  2173. }
  2174. void CBonusSelection::CRegion::show( SDL_Surface * to )
  2175. {
  2176. const SCampPositions::SRegionDesc & desc = owner->campDescriptions[owner->ourCampaign->header.mapVersion].regions[myNumber];
  2177. if (!accessible)
  2178. {
  2179. //show as striped
  2180. blitAt(graphics[2], pos.x, pos.y, to);
  2181. }
  2182. else if (this == owner->highlightedRegion)
  2183. {
  2184. //show as selected
  2185. blitAt(graphics[1], pos.x, pos.y, to);
  2186. }
  2187. else
  2188. {
  2189. //show as not selected selected
  2190. blitAt(graphics[0], pos.x, pos.y, to);
  2191. }
  2192. }
  2193. CSavingScreen::CSavingScreen(bool hotseat)
  2194. : CSelectionScreen(CMenuScreen::saveGame, hotseat)
  2195. {
  2196. ourGame = mapInfoFromGame();
  2197. sInfo = *LOCPLINT->cb->getStartInfo();
  2198. setPlayersFromGame();
  2199. }
  2200. CSavingScreen::~CSavingScreen()
  2201. {
  2202. }