CPreGame.cpp 77 KB

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