CPreGame.cpp 77 KB

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