CPreGame.cpp 120 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040
  1. #include "stdafx.h"
  2. #include "CPreGame.h"
  3. #include "SDL.h"
  4. #include "boost/filesystem.hpp" // includes all needed Boost.Filesystem declarations
  5. #include "boost/algorithm/string.hpp"
  6. //#include "boost/foreach.hpp"
  7. #include "zlib.h"
  8. #include "timeHandler.h"
  9. #include <sstream>
  10. #include "SDL_Extensions.h"
  11. #include "CGameInfo.h"
  12. #include "hch\CGeneralTextHandler.h"
  13. #include "CCursorHandler.h"
  14. #include "CScreenHandler.h"
  15. #include "hch\CLodHandler.h"
  16. #include "hch\CTownHandler.h"
  17. #include "hch\CHeroHandler.h"
  18. #include <cmath>
  19. extern SDL_Surface * ekran;
  20. extern SDL_Color tytulowy, tlo, zwykly ;
  21. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM;
  22. SDL_Rect genRect(int hh, int ww, int xx, int yy);
  23. SDL_Color genRGB(int r, int g, int b, int a=0);
  24. //void printAt(std::string text, int x, int y, TTF_Font * font, SDL_Color kolor=tytulowy, SDL_Surface * dst=ekran);
  25. CPreGame * CPG;
  26. bool isItIn(const SDL_Rect * rect, int x, int y);
  27. namespace fs = boost::filesystem;
  28. namespace s = CSDL_Ext;
  29. void HighButton::show()
  30. {
  31. blitAt(imgs->ourImages[state].bitmap,pos.x,pos.y);
  32. updateRect(&pos);
  33. }
  34. void HighButton::press(bool down)
  35. {
  36. int i;
  37. if (down) state=i=1;
  38. else state=i=0;
  39. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  40. updateRect(&pos);
  41. }
  42. template <class T> void SetrButton<T>::press(bool down=true)
  43. {
  44. if (!down && state==1)
  45. *poin=key;
  46. HighButton::press(down);
  47. }
  48. void HighButton::hover(bool on)
  49. {
  50. if (!highlightable) return;
  51. int i;
  52. if (on)
  53. {
  54. state=i=3;
  55. highlighted=true;
  56. }
  57. else
  58. {
  59. state=i=0;
  60. highlighted=false;
  61. }
  62. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  63. updateRect(&pos);
  64. }
  65. template <class T> void Button<T>::hover(bool on=true)
  66. {
  67. HighButton::hover(on);
  68. }
  69. template <class T> void Button<T>::select(bool on)
  70. {
  71. int i;
  72. if (on) state=i=3;
  73. else state=i=0;
  74. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,ekran,&pos);
  75. updateRect(&pos);
  76. if (ourGroup && on && ourGroup->type==1)
  77. {
  78. if (ourGroup->selected && ourGroup->selected!=this)
  79. ourGroup->selected->select(false);
  80. ourGroup->selected =this;
  81. }
  82. }
  83. //void Slider::clickDown(int x, int y, bool bzgl=true);
  84. //void Slider::clickUp(int x, int y, bool bzgl=true);
  85. //void Slider::mMove(int x, int y, bool bzgl=true);
  86. void Slider<>::updateSlid()
  87. {
  88. float perc = ((float)whereAreWe)/((float)positionsAmnt-capacity);
  89. float myh;
  90. if (vertical)
  91. {
  92. myh=perc*((float)pos.h-48)+pos.y+16;
  93. SDL_FillRect(ekran,&genRect(pos.h-32,pos.w,pos.x,pos.y+16),0);
  94. blitAt(slider.imgs->ourImages[0].bitmap,pos.x,(int)myh);
  95. slider.pos.y=(int)myh;
  96. }
  97. else
  98. {
  99. myh=perc*((float)pos.w-48)+pos.x+16;
  100. SDL_FillRect(ekran,&genRect(pos.h,pos.w-32,pos.x+16,pos.y),0);
  101. blitAt(slider.imgs->ourImages[0].bitmap,(int)myh,pos.y);
  102. slider.pos.x=(int)myh;
  103. }
  104. updateRect(&pos);
  105. }
  106. void Slider<>::moveDown()
  107. {
  108. if (whereAreWe<positionsAmnt-capacity)
  109. (CPG->*fun)(++whereAreWe);
  110. updateSlid();
  111. }
  112. void Slider<>::moveUp()
  113. {
  114. if (whereAreWe>0)
  115. (CPG->*fun)(--whereAreWe);
  116. updateSlid();
  117. }
  118. //void Slider::moveByOne(bool up);
  119. Slider<>::Slider(int x, int y, int h, int amnt, int cap, bool ver)
  120. {
  121. vertical=ver;
  122. positionsAmnt = amnt;
  123. capacity = cap;
  124. if (ver)
  125. {
  126. pos = genRect(h,16,x,y);
  127. down = Button<void(Slider::*)()>(genRect(16,16,x,y+h-16),&Slider::moveDown,CGI->spriteh->giveDef("SCNRBDN.DEF"),false);
  128. up = Button<void(Slider::*)()>(genRect(16,16,x,y),&Slider::moveUp,CGI->spriteh->giveDef("SCNRBUP.DEF"),false);
  129. slider = Button<void(Slider::*)()>(genRect(16,16,x,y+16),NULL,CGI->spriteh->giveDef("SCNRBSL.DEF"),false);
  130. }
  131. else
  132. {
  133. pos = genRect(16,h,x,y);
  134. down = Button<void(Slider::*)()>(genRect(16,16,x+h-16,y),&Slider::moveDown,CGI->spriteh->giveDef("SCNRBRT.DEF"),false);
  135. up = Button<void(Slider::*)()>(genRect(16,16,x,y),&Slider::moveUp,CGI->spriteh->giveDef("SCNRBLF.DEF"),false);
  136. slider = Button<void(Slider::*)()>(genRect(16,16,x+16,y),NULL,CGI->spriteh->giveDef("SCNRBSL.DEF"),false);
  137. }
  138. moving = false;
  139. whereAreWe=0;
  140. }
  141. void Slider<>::deactivate()
  142. {
  143. CPG->interested.erase(std::find(CPG->interested.begin(),CPG->interested.end(),this));
  144. }
  145. void Slider<>::activate()
  146. {
  147. SDL_FillRect(ekran,&pos,0);
  148. up.show();
  149. down.show();
  150. slider.show();
  151. //SDL_Flip(ekran);
  152. CSDL_Ext::update(ekran);
  153. CPG->interested.push_back(this);
  154. }
  155. void Slider<>::handleIt(SDL_Event sEvent)
  156. {
  157. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  158. {
  159. if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  160. {
  161. down.press();
  162. }
  163. else if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  164. {
  165. up.press();
  166. }
  167. else if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y))
  168. {
  169. //slider.press();
  170. moving=true;
  171. }
  172. else if (isItIn(&pos,sEvent.motion.x,sEvent.motion.y))
  173. {
  174. float dy;
  175. float pe;
  176. if (vertical)
  177. {
  178. dy = sEvent.motion.y-pos.y-16;
  179. pe = dy/((float)(pos.h-32));
  180. if (pe>1) pe=1;
  181. if (pe<0) pe=0;
  182. }
  183. else
  184. {
  185. dy = sEvent.motion.x-pos.x-16;
  186. pe = dy/((float)(pos.w-32));
  187. if (pe>1) pe=1;
  188. if (pe<0) pe=0;
  189. }
  190. whereAreWe = pe*(positionsAmnt-capacity);
  191. if (whereAreWe<0)whereAreWe=0;
  192. updateSlid();
  193. (CPG->*fun)(whereAreWe);
  194. }
  195. }
  196. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  197. {
  198. if ((down.state==1) && isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  199. {
  200. (this->*down.fun)();
  201. }
  202. if ((up.state==1) && isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  203. {
  204. (this->*up.fun)();
  205. }
  206. if (down.state==1) down.press(false);
  207. if (up.state==1) up.press(false);
  208. if (moving)
  209. {
  210. //slider.press();
  211. moving=false;
  212. }
  213. }
  214. else if (sEvent.type==SDL_KEYDOWN)
  215. {
  216. switch (sEvent.key.keysym.sym)
  217. {
  218. case (SDLK_UP):
  219. CPG->ourScenSel->mapsel.moveByOne(true);
  220. break;
  221. case (SDLK_DOWN):
  222. CPG->ourScenSel->mapsel.moveByOne(false);
  223. break;
  224. }
  225. }
  226. else if (moving && sEvent.type==SDL_MOUSEMOTION)
  227. {
  228. if (isItIn(&genRect(pos.h,pos.w+64,pos.x-32,pos.y),sEvent.motion.x,sEvent.motion.y))
  229. {
  230. int my;
  231. int all;
  232. float ile;
  233. if (vertical)
  234. {
  235. my = sEvent.motion.y-(pos.y+16);
  236. all =pos.h-48;
  237. ile = (float)my / (float)all;
  238. if (ile>1) ile=1;
  239. if (ile<0) ile=0;
  240. }
  241. else
  242. {
  243. my = sEvent.motion.x-(pos.x+16);
  244. all =pos.w-48;
  245. ile = (float)my / (float)all;
  246. if (ile>1) ile=1;
  247. if (ile<0) ile=0;
  248. }
  249. int ktory = ile*(positionsAmnt-capacity);
  250. if (ktory!=whereAreWe)
  251. {
  252. whereAreWe=ktory;
  253. updateSlid();
  254. }
  255. (CPG->*fun)(whereAreWe);
  256. }
  257. }
  258. /*else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  259. {
  260. if (ourScenSel->pressed)
  261. {
  262. ourScenSel->pressed->press(false);
  263. ourScenSel->pressed=NULL;
  264. }
  265. for (int i=0;i<btns.size(); i++)
  266. {
  267. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  268. {
  269. if (btns[i]->selectable)
  270. btns[i]->select(true);
  271. if (btns[i]->fun)
  272. (this->*(btns[i]->fun))();
  273. return;
  274. }
  275. }
  276. if (isItIn(&down.pos,sEvent.motion.x,sEvent.motion.y))
  277. {
  278. (this->*down.fun)();
  279. }
  280. if (isItIn(&up.pos,sEvent.motion.x,sEvent.motion.y))
  281. {
  282. (this->*up.fun)();
  283. }
  284. if (isItIn(&slider.pos,sEvent.motion.x,sEvent.motion.y))
  285. {
  286. (this->*slider.fun)();
  287. }
  288. }*/
  289. }
  290. /********************************************************************************************/
  291. void PreGameTab::show()
  292. {
  293. if (CPG->currentTab)
  294. CPG->currentTab->hide();
  295. showed=true;
  296. CPG->currentTab=this;
  297. }
  298. void PreGameTab::hide()
  299. {
  300. showed=false;
  301. CPG->currentTab=NULL;
  302. }
  303. PreGameTab::PreGameTab()
  304. {
  305. showed=false;
  306. }
  307. /********************************************************************************************/
  308. Options::PlayerOptions::PlayerOptions(int serial, int player)
  309. :Cleft(genRect(24,11,164,133+serial*50),CPG->ourOptions->left,true,-1), //left castle arrow
  310. Cright(genRect(24,11,225,133+serial*50),CPG->ourOptions->right,false,-1), //right castle arrow
  311. Hleft(genRect(24,11,240,133+serial*50),CPG->ourOptions->left,true,0), //left hero arrow
  312. Hright(genRect(24,11,301,133+serial*50),CPG->ourOptions->right,false,0), //right hero arrow
  313. Bleft(genRect(24,11,316,133+serial*50),CPG->ourOptions->left,true,1), //left bonus arrow
  314. Bright(genRect(24,11,377,133+serial*50),CPG->ourOptions->right,false,1), //right bonus arrow
  315. flag(genRect(50,42,14,130+serial*50),CPG->ourOptions->flags[player],player)
  316. {
  317. Bleft.playerID=Bright.playerID=Hleft.playerID=Hright.playerID=Cleft.playerID=Cright.playerID=player;
  318. Bleft.serialID=Bright.serialID=Hleft.serialID=Hright.serialID=Cleft.serialID=Cright.serialID=serial;
  319. }
  320. void Options::OptionSwitch::press(bool down)
  321. {
  322. HighButton::press(down);
  323. StartInfo::PlayerSettings * ourOpt = &CPG->ret.playerInfos[serialID];
  324. PlayerInfo * ourInf = &CPG->ourScenSel->mapsel.ourMaps[CPG->ourScenSel->mapsel.selected].players[playerID];
  325. int dir = (left) ? (-1) : (1);
  326. if (down) return;
  327. switch (which)
  328. {
  329. case -1:
  330. {
  331. int oCas = ourOpt->castle;
  332. if (ourOpt->castle==-2) //no castle - no change
  333. return;
  334. else if (ourOpt->castle==-1) //random => first/last available
  335. {
  336. int pom = (left) ? (F_NUMBER-1) : (0); // last or first
  337. for (;pom>=0 && pom<F_NUMBER;pom+=dir)
  338. {
  339. if (((int)pow((double)2,pom))&ourInf->allowedFactions)
  340. {
  341. ourOpt->castle=pom;
  342. break;
  343. }
  344. else continue;
  345. }
  346. }
  347. else // next/previous available
  348. {
  349. for (;;)
  350. {
  351. ourOpt->castle+=dir;
  352. if (((int)pow((double)2,ourOpt->castle))&ourInf->allowedFactions)
  353. {
  354. break;
  355. }
  356. if (ourOpt->castle>=F_NUMBER || ourOpt->castle<0)
  357. {
  358. double p1 = log((double)ourInf->allowedFactions)/log(2.0f)+0.000001f;
  359. double check = p1-((int)p1);
  360. if (check < 0.001)
  361. ourOpt->castle=(int)p1;
  362. else
  363. ourOpt->castle=-1;
  364. break;
  365. }
  366. }
  367. }
  368. if (oCas!=ourOpt->castle) //changed castle
  369. {
  370. ourOpt->hero=-1;
  371. ourOpt->bonus = Ebonus::brandom;
  372. CPG->ourOptions->showIcon(0,serialID,false);
  373. CPG->ourOptions->showIcon(1,serialID,false);
  374. }
  375. break;
  376. }
  377. case 0:
  378. {
  379. if (ourOpt->castle<0)
  380. {
  381. break;
  382. }
  383. if (ourOpt->hero==-2) //no hero - no change
  384. return;
  385. else if (ourOpt->hero==-1) //random => first/last available
  386. {
  387. int pom = (left) ? (ourOpt->castle*HEROES_PER_TYPE*2+15) : (ourOpt->castle*HEROES_PER_TYPE*2); // last or first
  388. for (;pom>=0 && pom<CGI->heroh->heroes.size();pom+=dir)
  389. {
  390. //TODO: checked allowed heroes
  391. {
  392. ourOpt->hero=pom;
  393. break;
  394. }
  395. }
  396. }
  397. else
  398. {
  399. ourOpt->hero+=dir;
  400. if (ourOpt->hero>HEROES_PER_TYPE*ourOpt->castle*2+15 || ourOpt->hero<HEROES_PER_TYPE*ourOpt->castle*2)
  401. {
  402. ourOpt->hero=-1;
  403. if( ourOpt->bonus == bartifact )
  404. {
  405. ourOpt->bonus = brandom;
  406. CPG->ourOptions->showIcon(1, serialID, false);
  407. }
  408. }
  409. break;
  410. }
  411. break;
  412. }
  413. case 1:
  414. {
  415. if (dir>0 && ourOpt->bonus==bresource)
  416. ourOpt->bonus=brandom;
  417. else if (dir<0 && ourOpt->bonus==brandom)
  418. ourOpt->bonus=bresource;
  419. else ourOpt->bonus=(Ebonus)(ourOpt->bonus+dir);
  420. if (ourOpt->hero==-2 && ourOpt->bonus==bartifact) //no hero - can't be artifact
  421. {
  422. if (dir>0)
  423. ourOpt->bonus=brandom;
  424. else ourOpt->bonus=bgold;
  425. }
  426. if (ourOpt->castle==-1 && ourOpt->bonus==bresource)
  427. {
  428. if (dir<0)
  429. ourOpt->bonus=bgold;
  430. else ourOpt->bonus=brandom;
  431. }
  432. break;
  433. }
  434. }
  435. CPG->ourOptions->showIcon(which,serialID,false);
  436. }
  437. void Options::PlayerFlag::press(bool down)
  438. {
  439. HighButton::press(down);
  440. };
  441. void Options::PlayerFlag::hover(bool on)
  442. {
  443. HighButton::hover(on);
  444. }
  445. void Options::showIcon (int what, int nr, bool abs) //what: -1=castle, 0=hero, 1=bonus, 2=all; abs=true -> nr is absolute
  446. {
  447. if (what==-2)
  448. {
  449. showIcon(-1,nr,abs);
  450. showIcon(0,nr,abs);
  451. showIcon(1,nr,abs);
  452. }
  453. int ab, se;
  454. if (!abs)
  455. {
  456. ab = CPG->ret.playerInfos[nr].color;
  457. se = nr;
  458. }
  459. else
  460. {
  461. ab = nr;
  462. for (int i=0; i<CPG->ret.playerInfos.size();i++)
  463. {
  464. if (CPG->ret.playerInfos[i].color==nr)
  465. {
  466. se=i;
  467. break;
  468. }
  469. }
  470. }
  471. StartInfo::PlayerSettings * ourOpt = &CPG->ret.playerInfos[se];
  472. switch (what)
  473. {
  474. case -1:
  475. {
  476. int pom=ourOpt->castle;
  477. if (ourOpt->castle<F_NUMBER && ourOpt->castle>=0)
  478. {
  479. blitAtWR(CGI->townh->getPic(ourOpt->castle,true,false),176,130+50*se);
  480. }
  481. else if (ourOpt->castle==-1)
  482. {
  483. blitAtWR(CPG->ourOptions->rCastle,176,130+50*se);
  484. }
  485. else if (ourOpt->castle==-2)
  486. {
  487. blitAtWR(CPG->ourOptions->nCastle,176,130+50*se);
  488. }
  489. break;
  490. }
  491. case 0:
  492. {
  493. int pom=ourOpt->hero;
  494. if (ourOpt->hero==-1)
  495. {
  496. blitAtWR(CPG->ourOptions->rHero,252,130+50*se);
  497. }
  498. else if (ourOpt->hero==-2)
  499. {
  500. blitAtWR(CPG->ourOptions->nHero,252,130+50*se);
  501. }
  502. else
  503. {
  504. blitAtWR(CGI->heroh->heroes[pom]->portraitSmall,252,130+50*se);
  505. }
  506. break;
  507. }
  508. case 1:
  509. {
  510. int pom;
  511. switch (ourOpt->bonus)
  512. {
  513. case -1:
  514. pom=10;
  515. break;
  516. case 0:
  517. pom=9;
  518. break;
  519. case 1:
  520. pom=8;
  521. break;
  522. case 2:
  523. pom=CGI->townh->towns[ourOpt->castle].bonus;
  524. break;
  525. }
  526. blitAtWR(bonuses->ourImages[pom].bitmap,328,130+50*se);
  527. break;
  528. }
  529. }
  530. }
  531. Options::~Options()
  532. {
  533. if (!inited) return;
  534. for (int i=0; i<bgs.size();i++)
  535. SDL_FreeSurface(bgs[i]);
  536. for (int i=0; i<flags.size();i++)
  537. delete flags[i];
  538. SDL_FreeSurface(bg);
  539. SDL_FreeSurface(rHero);
  540. SDL_FreeSurface(rCastle);
  541. SDL_FreeSurface(nHero);
  542. SDL_FreeSurface(nCastle);
  543. delete turnLength;
  544. delete left;
  545. delete right;
  546. delete bonuses;
  547. }
  548. void Options::init()
  549. {
  550. inited=true;
  551. bg = CGI->bitmaph->loadBitmap("ADVOPTBK.bmp");
  552. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  553. left = CGI->spriteh->giveDef("ADOPLFA.DEF");
  554. right = CGI->spriteh->giveDef("ADOPRTA.DEF");
  555. bonuses = CGI->spriteh->giveDef("SCNRSTAR.DEF");
  556. rHero = CGI->bitmaph->loadBitmap("HPSRAND1.bmp");
  557. rCastle = CGI->bitmaph->loadBitmap("HPSRAND0.bmp");
  558. nHero = CGI->bitmaph->loadBitmap("HPSRAND6.bmp");
  559. nCastle = CGI->bitmaph->loadBitmap("HPSRAND5.bmp");
  560. turnLength = new Slider<>(57,557,195,11,1,false);
  561. turnLength->fun=&CPreGame::setTurnLength;
  562. flags.push_back(CGI->spriteh->giveDef("AOFLGBR.DEF"));
  563. flags.push_back(CGI->spriteh->giveDef("AOFLGBB.DEF"));
  564. flags.push_back(CGI->spriteh->giveDef("AOFLGBY.DEF"));
  565. flags.push_back(CGI->spriteh->giveDef("AOFLGBG.DEF"));
  566. flags.push_back(CGI->spriteh->giveDef("AOFLGBO.DEF"));
  567. flags.push_back(CGI->spriteh->giveDef("AOFLGBP.DEF"));
  568. flags.push_back(CGI->spriteh->giveDef("AOFLGBT.DEF"));
  569. flags.push_back(CGI->spriteh->giveDef("AOFLGBS.DEF"));
  570. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPRPNL.bmp"));
  571. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPBPNL.bmp"));
  572. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPYPNL.bmp"));
  573. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPGPNL.bmp"));
  574. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPOPNL.bmp"));
  575. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPPPNL.bmp"));
  576. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPTPNL.bmp"));
  577. bgs.push_back(CGI->bitmaph->loadBitmap("ADOPSPNL.bmp"));
  578. }
  579. void Options::show()
  580. {
  581. if (showed)return;
  582. PreGameTab::show();
  583. MapSel & ms = CPG->ourScenSel->mapsel;
  584. blitAt(bg,3,6);
  585. CPG->ourScenSel->listShowed=false;
  586. for (int i=0;i<CPG->btns.size();i++)
  587. {
  588. if (CPG->btns[i]->ID!=10) //leave only right panel buttons
  589. {
  590. CPG->btns.erase(CPG->btns.begin()+i);
  591. i--;
  592. }
  593. }
  594. CPG->interested.clear();
  595. CSDL_Ext::printAtMiddle("Advanced Options",225,35,GEORXX);
  596. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[521],224,544,GEOR13); // Player Turn Duration
  597. int playersSoFar=0;
  598. for (int i=0;i<PLAYER_LIMIT;i++)
  599. {
  600. if (!(ms.ourMaps[ms.selected].players[i].canComputerPlay || ms.ourMaps[ms.selected].players[i].canComputerPlay))
  601. continue;
  602. blitAt(bgs[i],57,128+playersSoFar*50);
  603. poptions.push_back(new PlayerOptions(playersSoFar,i));
  604. poptions[poptions.size()-1]->nr=playersSoFar;
  605. poptions[poptions.size()-1]->color=(Ecolor)i;
  606. poptions[poptions.size()-1]->Cleft.show();
  607. poptions[poptions.size()-1]->Cright.show();
  608. poptions[poptions.size()-1]->Hleft.show();
  609. poptions[poptions.size()-1]->Hright.show();
  610. poptions[poptions.size()-1]->Bleft.show();
  611. poptions[poptions.size()-1]->Bright.show();
  612. CPG->btns.push_back(&poptions[poptions.size()-1]->Cleft);
  613. CPG->btns.push_back(&poptions[poptions.size()-1]->Cright);
  614. CPG->btns.push_back(&poptions[poptions.size()-1]->Hleft);
  615. CPG->btns.push_back(&poptions[poptions.size()-1]->Hright);
  616. CPG->btns.push_back(&poptions[poptions.size()-1]->Bleft);
  617. CPG->btns.push_back(&poptions[poptions.size()-1]->Bright);
  618. CSDL_Ext::printAtMiddle(CPG->ret.playerInfos[playersSoFar].name,111,137+playersSoFar*50,GEOR13,zwykly);
  619. if (ms.ourMaps[ms.selected].players[i].canHumanPlay)
  620. {
  621. poptions[poptions.size()-1]->flag.show();
  622. CPG->btns.push_back(&poptions[poptions.size()-1]->flag);
  623. if (ms.ourMaps[ms.selected].players[i].canComputerPlay)
  624. CSDL_Ext::printAtMiddleWB("Human or CPU",86,163+playersSoFar*50,GEORM,7,zwykly);
  625. else
  626. CSDL_Ext::printAtMiddleWB("Human",86,163+playersSoFar*50,GEORM,6,zwykly);
  627. }
  628. else
  629. CSDL_Ext::printAtMiddleWB("CPU",86,163+playersSoFar*50,GEORM,6,zwykly);
  630. playersSoFar++;
  631. }
  632. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->allTexts[516],221,63,GEOR13,55,zwykly);
  633. CSDL_Ext::printAtMiddleWB(CGI->preth->getTitle(CGI->preth->singlePlayerHandicapHeaderID),109,109,GEOR13,14);
  634. CSDL_Ext::printAtMiddleWB(CGI->preth->getTitle(CGI->preth->singleStartingTownHeaderWConfluxID),201,109,GEOR13,10);
  635. CSDL_Ext::printAtMiddleWB(CGI->preth->getTitle(CGI->preth->singleStartingHeroHeaderID),275,109,GEOR13,10);
  636. CSDL_Ext::printAtMiddleWB(CGI->preth->getTitle(CGI->preth->singleStartingBonusHeaderID),354,109,GEOR13,10);
  637. turnLength->activate();
  638. for (int i=0;i<poptions.size();i++)
  639. showIcon(-2,i,false);
  640. SDL_Flip(ekran);
  641. }
  642. void Options::hide()
  643. {
  644. if (!showed) return;
  645. PreGameTab::hide();
  646. for (int i=0; i<CPG->btns.size();i++)
  647. if (CPG->btns[i]->ID==7)
  648. CPG->btns.erase(CPG->btns.begin()+i--);
  649. for (int i=0;i<poptions.size();i++)
  650. delete poptions[i];
  651. poptions.clear();
  652. turnLength->deactivate();
  653. }
  654. MapSel::~MapSel()
  655. {
  656. SDL_FreeSurface(bg);
  657. for (int i=0;i<scenImgs.size();i++)
  658. SDL_FreeSurface(scenImgs[i]);
  659. for (int i=0;i<scenList.size();i++)
  660. delete scenList[i];
  661. delete sFlags;
  662. }
  663. int MapSel::countWL()
  664. {
  665. int ret=0;
  666. for (int i=0;i<ourMaps.size();i++)
  667. {
  668. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  669. continue;
  670. else ret++;
  671. }
  672. return ret;
  673. }
  674. void MapSel::printMaps(int from, int to, int at, bool abs)
  675. {
  676. if (true)//
  677. {
  678. int help=-1;
  679. for (int i=0;i<ourMaps.size();i++)
  680. {
  681. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  682. continue;
  683. else help++;
  684. if (help==from)
  685. {
  686. from=i;
  687. break;
  688. }
  689. }
  690. }
  691. SDL_Surface * scenin = CSDL_Ext::newSurface(351,25);
  692. SDL_Color nasz;
  693. for (int i=at;i<to;i++)
  694. {
  695. if ((i-at+from) > ourMaps.size()-1)
  696. {
  697. SDL_Surface * scenin = CSDL_Ext::newSurface(351,25);
  698. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  699. blitAt(scenin,24,121+(i-at)*25);
  700. //SDL_Flip(ekran);
  701. CSDL_Ext::update(ekran);
  702. SDL_FreeSurface(scenin);
  703. continue;
  704. }
  705. if (sizeFilter && ((ourMaps[(i-at)+from].width) != sizeFilter))
  706. {
  707. to++;
  708. at++;
  709. from++;
  710. if (((i-at)+from)>ourMaps.size()-1) break;
  711. else continue;
  712. }
  713. if ((i-at+from) == selected)
  714. nasz=tytulowy;
  715. else nasz=zwykly;
  716. //SDL_Rect pier = genRect(25,351,24,126+(i*25));
  717. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  718. int temp=-1;
  719. std::ostringstream ostr(std::ostringstream::out); ostr << ourMaps[(i-at)+from].playerAmnt << "/" << ourMaps[(i-at)+from].humenPlayers;
  720. CSDL_Ext::printAt(ostr.str(),6,4,GEOR13,nasz,scenin, 2);
  721. std::string temp2;
  722. switch (ourMaps[(i-at)+from].width)
  723. {
  724. case 36:
  725. temp2="S";
  726. break;
  727. case 72:
  728. temp2="M";
  729. break;
  730. case 108:
  731. temp2="L";
  732. break;
  733. case 144:
  734. temp2="XL";
  735. break;
  736. default:
  737. temp2="C";
  738. break;
  739. }
  740. CSDL_Ext::printAtMiddle(temp2,50,13,GEOR13,nasz,scenin, 2);
  741. switch (ourMaps[(i-at)+from].version)
  742. {
  743. case Eformat::RoE:
  744. temp=0;
  745. break;
  746. case Eformat::AB:
  747. temp=1;
  748. break;
  749. case Eformat::SoD:
  750. temp=2;
  751. break;
  752. case Eformat::WoG:
  753. temp=3;
  754. break;
  755. }
  756. blitAt(Dtypes->ourImages[temp].bitmap,67,2,scenin);
  757. if (!(ourMaps[(i-at)+from].name.length()))
  758. ourMaps[(i-at)+from].name = "Unnamed";
  759. CSDL_Ext::printAtMiddle(ourMaps[(i-at)+from].name,192,13,GEOR13,nasz,scenin, 2);
  760. if (ourMaps[(i-at)+from].victoryCondition==EvictoryConditions::winStandard)
  761. temp=11;
  762. else temp=ourMaps[(i-at)+from].victoryCondition;
  763. blitAt(Dvic->ourImages[temp].bitmap,285,2,scenin);
  764. if (ourMaps[(i-at)+from].lossCondition.typeOfLossCon == ElossCon::lossStandard)
  765. temp=3;
  766. else temp=ourMaps[(i-at)+from].lossCondition.typeOfLossCon;
  767. blitAt(Dloss->ourImages[temp].bitmap,318,2,scenin);
  768. blitAt(scenin,24,121+(i-at)*25);
  769. SDL_UpdateRect(ekran,24,121+(i-at)*25,scenin->w,scenin->h);
  770. }
  771. SDL_FreeSurface(scenin);
  772. }
  773. int MapSel::whichWL(int nr)
  774. {
  775. int help=-1;
  776. for (int i=0;i<ourMaps.size();i++)
  777. {
  778. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  779. continue;
  780. else help++;
  781. if (help==nr)
  782. {
  783. help=i;
  784. break;
  785. }
  786. }
  787. return help;
  788. }
  789. void MapSel::hide()
  790. {
  791. if (!showed)return;
  792. PreGameTab::hide();
  793. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&small));
  794. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&medium));
  795. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&large));
  796. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&xlarge));
  797. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&all));
  798. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&nrplayer));
  799. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&mapsize));
  800. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&type));
  801. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&name));
  802. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&viccon));
  803. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&loscon));
  804. slid->deactivate();
  805. };
  806. void MapSel::show()
  807. {
  808. if (showed)return;
  809. PreGameTab::show();
  810. //blit bg
  811. blitAt(bg,3,6);
  812. CSDL_Ext::printAt("Map Sizes",55,60,GEOR13);
  813. CSDL_Ext::printAt("Select a Scenario to Play",110,25,TNRB16);
  814. //size buttons
  815. small.show();
  816. medium.show();
  817. large.show();
  818. xlarge.show();
  819. all.show();
  820. CPG->btns.push_back(&small);
  821. CPG->btns.push_back(&medium);
  822. CPG->btns.push_back(&large);
  823. CPG->btns.push_back(&xlarge);
  824. CPG->btns.push_back(&all);
  825. //sort by buttons
  826. nrplayer.show();
  827. mapsize.show();
  828. type.show();
  829. name.show();
  830. viccon.show();
  831. loscon.show();
  832. CPG->btns.push_back(&nrplayer);
  833. CPG->btns.push_back(&mapsize);
  834. CPG->btns.push_back(&type);
  835. CPG->btns.push_back(&name);
  836. CPG->btns.push_back(&viccon);
  837. CPG->btns.push_back(&loscon);
  838. //print scenario list
  839. printMaps(0,18);
  840. slid->activate();
  841. //SDL_Flip(ekran);
  842. CSDL_Ext::update(ekran);
  843. }
  844. void MapSel::init()
  845. {
  846. bg = CGI->bitmaph->loadBitmap("SCSELBCK.bmp");
  847. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  848. small.imgs = CGI->spriteh->giveDef("SCSMBUT.DEF");
  849. small.fun = NULL;
  850. small.pos = genRect(small.imgs->ourImages[0].bitmap->h,small.imgs->ourImages[0].bitmap->w,161,52);
  851. small.ourGroup=NULL;
  852. medium.imgs = CGI->spriteh->giveDef("SCMDBUT.DEF");
  853. medium.fun = NULL;
  854. medium.pos = genRect(medium.imgs->ourImages[0].bitmap->h,medium.imgs->ourImages[0].bitmap->w,208,52);
  855. medium.ourGroup=NULL;
  856. large.imgs = CGI->spriteh->giveDef("SCLGBUT.DEF");
  857. large.fun = NULL;
  858. large.pos = genRect(large.imgs->ourImages[0].bitmap->h,large.imgs->ourImages[0].bitmap->w,255,52);
  859. large.ourGroup=NULL;
  860. xlarge.imgs = CGI->spriteh->giveDef("SCXLBUT.DEF");
  861. xlarge.fun = NULL;
  862. xlarge.pos = genRect(xlarge.imgs->ourImages[0].bitmap->h,xlarge.imgs->ourImages[0].bitmap->w,302,52);
  863. xlarge.ourGroup=NULL;
  864. all.imgs = CGI->spriteh->giveDef("SCALBUT.DEF");
  865. all.fun = NULL;
  866. all.pos = genRect(all.imgs->ourImages[0].bitmap->h,all.imgs->ourImages[0].bitmap->w,349,52);
  867. all.ourGroup=NULL;
  868. all.selectable=xlarge.selectable=large.selectable=medium.selectable=small.selectable=false;
  869. small.what=medium.what=large.what=xlarge.what=all.what=&sizeFilter;
  870. small.key=36;medium.key=72;large.key=108;xlarge.key=144;all.key=0;
  871. //Button<> nrplayer, mapsize, type, name, viccon, loscon;
  872. nrplayer.imgs = CGI->spriteh->giveDef("SCBUTT1.DEF");
  873. nrplayer.fun = NULL;
  874. nrplayer.pos = genRect(nrplayer.imgs->ourImages[0].bitmap->h,nrplayer.imgs->ourImages[0].bitmap->w,26,92);
  875. nrplayer.key=ESortBy::playerAm;
  876. mapsize.imgs = CGI->spriteh->giveDef("SCBUTT2.DEF");
  877. mapsize.fun = NULL;
  878. mapsize.pos = genRect(mapsize.imgs->ourImages[0].bitmap->h,mapsize.imgs->ourImages[0].bitmap->w,58,92);
  879. mapsize.key=ESortBy::size;
  880. type.imgs = CGI->spriteh->giveDef("SCBUTCP.DEF");
  881. type.fun = NULL;
  882. type.pos = genRect(type.imgs->ourImages[0].bitmap->h,type.imgs->ourImages[0].bitmap->w,91,92);
  883. type.key=ESortBy::format;
  884. name.imgs = CGI->spriteh->giveDef("SCBUTT3.DEF");
  885. name.fun = NULL;
  886. name.pos = genRect(name.imgs->ourImages[0].bitmap->h,name.imgs->ourImages[0].bitmap->w,124,92);
  887. name.key=ESortBy::name;
  888. viccon.imgs = CGI->spriteh->giveDef("SCBUTT4.DEF");
  889. viccon.fun = NULL;
  890. viccon.pos = genRect(viccon.imgs->ourImages[0].bitmap->h,viccon.imgs->ourImages[0].bitmap->w,309,92);
  891. viccon.key=ESortBy::viccon;
  892. loscon.imgs = CGI->spriteh->giveDef("SCBUTT5.DEF");
  893. loscon.fun = NULL;
  894. loscon.pos = genRect(loscon.imgs->ourImages[0].bitmap->h,loscon.imgs->ourImages[0].bitmap->w,342,92);
  895. loscon.key=ESortBy::loscon;
  896. nrplayer.poin=mapsize.poin=type.poin=name.poin=viccon.poin=loscon.poin=(int*)(&sortBy);
  897. nrplayer.fun=mapsize.fun=type.fun=name.fun=viccon.fun=loscon.fun=&CPreGame::sortMaps;
  898. Dtypes = CGI->spriteh->giveDef("SCSELC.DEF");
  899. Dvic = CGI->spriteh->giveDef("SCNRVICT.DEF");
  900. Dloss = CGI->spriteh->giveDef("SCNRLOSS.DEF");
  901. //Dsizes = CPG->slh->giveDef("SCNRMPSZ.DEF");
  902. Dsizes = CGI->spriteh->giveDef("SCNRMPSZ.DEF");
  903. sFlags = CGI->spriteh->giveDef("ITGFLAGS.DEF");
  904. //get map files names
  905. std::vector<std::string> pliczkiTemp;
  906. fs::path tie( (fs::initial_path<fs::path>())/"/maps" );
  907. fs::directory_iterator end_iter;
  908. for ( fs::directory_iterator dir (tie); dir!=end_iter; ++dir )
  909. {
  910. if (fs::is_regular(dir->status()));
  911. {
  912. if (boost::ends_with(dir->path().leaf(),std::string(".h3m")))
  913. pliczkiTemp.push_back("Maps/"+(dir->path().leaf()));
  914. }
  915. }
  916. for (int i=0; i<pliczkiTemp.size();i++)
  917. {
  918. gzFile tempf = gzopen(pliczkiTemp[i].c_str(),"rb");
  919. std::string sss;
  920. int iii=0;
  921. while(++iii)
  922. {
  923. if (iii>4000) break;
  924. int z = gzgetc (tempf);
  925. if (z>=0)
  926. {
  927. sss+=unsigned char(z);
  928. }
  929. else break;
  930. }
  931. gzclose(tempf);
  932. //if (sss[0]<28) continue; //zly format
  933. if (!sss[4]) continue; //nie ma graczy? mapa niegrywalna //już to kiedyś komentowałem- - to bzdura //tu calkiem pasuje...
  934. unsigned char* file2 = new unsigned char[sss.length()];
  935. for (int j=0;j<sss.length();j++)
  936. file2[j]=sss[j];
  937. ourMaps.push_back(CMapInfo(pliczkiTemp[i],file2));
  938. delete file2;
  939. }
  940. std::sort(ourMaps.begin(),ourMaps.end(),mapSorter(ESortBy::name));
  941. slid = new Slider<>(375,92,480,ourMaps.size(),18,true);
  942. slid->fun = &CPreGame::printMapsFrom;
  943. }
  944. void MapSel::moveByOne(bool up)
  945. {
  946. int help=selected;
  947. if (up) selected--;
  948. else selected ++;
  949. for (int i=selected;i<ourMaps.size() && i>=0;)
  950. {
  951. help=i;
  952. if (!(sizeFilter && ((ourMaps[i].width) != sizeFilter)))
  953. break;
  954. if (up)
  955. {
  956. i--;
  957. }
  958. else
  959. {
  960. i++;
  961. if (i<0) break;
  962. }
  963. }
  964. select(help);
  965. slid->updateSlid();
  966. }
  967. void MapSel::select(int which, bool updateMapsList)
  968. {
  969. selected = which;
  970. if(updateMapsList)
  971. printMaps(slid->whereAreWe,18,0,true);
  972. CPG->ret.playerInfos.clear();
  973. bool wasntpl = true;
  974. for (int i=0;i<PLAYER_LIMIT;i++)
  975. {
  976. if (!(ourMaps[selected].players[i].canComputerPlay || ourMaps[selected].players[i].canComputerPlay))
  977. continue; // this caused some serious problems becouse of lack of simple bijection between two sets of player's numbers (one is returned by CPreGame, second is used in h3m)
  978. StartInfo::PlayerSettings pset;
  979. pset.color=(Ecolor)i;
  980. pset.bonus=Ebonus::brandom;
  981. pset.castle=-2;
  982. if (ourMaps[which].players[i].canHumanPlay && wasntpl)
  983. {
  984. pset.name=CGI->generaltexth->allTexts[434];
  985. CPG->playerColor = i;
  986. wasntpl = false;
  987. }
  988. else
  989. {
  990. pset.name=CGI->generaltexth->allTexts[468];
  991. }
  992. if ( (ourMaps[which].players[i].heroesNames.size()>1) ||
  993. (((ourMaps[which].players[i].heroesNames.size()>1)==1) && ourMaps[which].players[i].generateHeroAtMainTown==false)
  994. )
  995. pset.hero=-2;
  996. else if (ourMaps[which].players[i].heroesNames.size()==1)
  997. {
  998. pset.hero=ourMaps[which].players[i].heroesNames[0].heroID;
  999. }
  1000. else
  1001. pset.hero=-1;
  1002. for (int j=0;j<F_NUMBER;j++)
  1003. {
  1004. if (((int)pow((double)2,j))&ourMaps[selected].players[i].allowedFactions)
  1005. {
  1006. if (pset.castle>=0)
  1007. pset.castle=-1;
  1008. if (pset.castle==-2)
  1009. pset.castle=j;
  1010. }
  1011. }
  1012. if (ourMaps[selected].players[i].generateHeroAtMainTown || ourMaps[selected].players[i].generateHero)
  1013. pset.hero=-1;
  1014. else if (ourMaps[selected].players[i].mainHeroName.length())
  1015. {
  1016. pset.heroName=ourMaps[selected].players[i].mainHeroName;
  1017. pset.heroPortrait=ourMaps[selected].players[i].mainHeroPortrait;
  1018. }
  1019. else
  1020. pset.hero=-2;
  1021. pset.handicap=0;
  1022. pset.heroPortrait=-1;
  1023. CPG->ret.playerInfos.push_back(pset);
  1024. }
  1025. printSelectedInfo();
  1026. }
  1027. MapSel::MapSel():selected(0),sizeFilter(0)
  1028. {
  1029. }
  1030. void MapSel::printSelectedInfo()
  1031. {
  1032. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),ekran,&genRect(399,337,413,29));
  1033. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),ekran,&genRect(50,91,414,453));
  1034. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bScens.pos);
  1035. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bOptions.pos);
  1036. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bRandom.pos);
  1037. //blit texts
  1038. CSDL_Ext::printAt(CPG->preth->singleScenarioName,420,25,GEOR13);
  1039. CSDL_Ext::printAt("Scenario Description:",420,135,GEOR13);
  1040. CSDL_Ext::printAt("Victory Condition:",420,285,GEOR13);
  1041. CSDL_Ext::printAt("Loss Condition:",420,340,GEOR13);
  1042. CSDL_Ext::printAt("Allies:",420,406,GEOR13,zwykly);
  1043. CSDL_Ext::printAt("Enemies:",585,406,GEOR13,zwykly);
  1044. int temp = ourMaps[selected].victoryCondition+1;
  1045. if (temp>20) temp=0;
  1046. std::string sss = CPG->preth->victoryConditions[temp];
  1047. if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CPG->preth->victoryConditions[0];
  1048. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  1049. temp = ourMaps[selected].lossCondition.typeOfLossCon+1;
  1050. if (temp>20) temp=0;
  1051. sss = CPG->preth->lossCondtions[temp];
  1052. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  1053. //blit descrption
  1054. std::vector<std::string> desc = *CMessage::breakText(ourMaps[selected].description,50);
  1055. for (int i=0;i<desc.size();i++)
  1056. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  1057. if ((selected < 0) || (selected >= ourMaps.size()))
  1058. return;
  1059. if (ourMaps[selected].name.length())
  1060. CSDL_Ext::printAt(ourMaps[selected].name,420,41,GEORXX);
  1061. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  1062. std::string diff;
  1063. switch (ourMaps[selected].difficulty)
  1064. {
  1065. case 0:
  1066. diff=gdiff(CPG->preth->singleEasy);
  1067. break;
  1068. case 1:
  1069. diff=gdiff(CPG->preth->singleNormal);
  1070. break;
  1071. case 2:
  1072. diff=gdiff(CPG->preth->singleHard);
  1073. break;
  1074. case 3:
  1075. diff=gdiff(CPG->preth->singleExpert);
  1076. break;
  1077. case 4:
  1078. diff=gdiff(CPG->preth->singleImpossible);
  1079. break;
  1080. }
  1081. temp=-1;
  1082. switch (ourMaps[selected].width)
  1083. {
  1084. case 36:
  1085. temp=0;
  1086. break;
  1087. case 72:
  1088. temp=1;
  1089. break;
  1090. case 108:
  1091. temp=2;
  1092. break;
  1093. case 144:
  1094. temp=3;
  1095. break;
  1096. default:
  1097. temp=4;
  1098. break;
  1099. }
  1100. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  1101. temp=ourMaps[selected].victoryCondition;
  1102. if (temp>12) temp=11;
  1103. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  1104. temp=ourMaps[selected].lossCondition.typeOfLossCon;
  1105. if (temp>12) temp=3;
  1106. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  1107. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  1108. //SDL_Flip(ekran);
  1109. printFlags();
  1110. CSDL_Ext::update(ekran);
  1111. }
  1112. void MapSel::printFlags()
  1113. {
  1114. int hy=405, fx=460, ex=640, myT;
  1115. if (ourMaps[selected].howManyTeams)
  1116. myT = ourMaps[selected].players[CPG->playerColor].team;
  1117. else myT = -1;
  1118. for (int i=0;i<CPG->ret.playerInfos.size();i++)
  1119. {
  1120. if (myT>=0)
  1121. {
  1122. if(ourMaps[selected].players[CPG->ret.playerInfos[i].color].team==myT)
  1123. {
  1124. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1125. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1126. }
  1127. else
  1128. {
  1129. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1130. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1131. }
  1132. }
  1133. else
  1134. {
  1135. if(CPG->ret.playerInfos[i].color==CPG->playerColor)
  1136. {
  1137. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1138. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1139. }
  1140. else
  1141. {
  1142. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1143. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1144. }
  1145. }
  1146. }
  1147. }
  1148. std::string MapSel::gdiff(std::string ss)
  1149. {
  1150. std::string ret;
  1151. for (int i=2;i<ss.length();i++)
  1152. {
  1153. if (ss[i]==' ')
  1154. break;
  1155. ret+=ss[i];
  1156. }
  1157. return ret;
  1158. }
  1159. void CPreGame::printRating()
  1160. {
  1161. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(38,71,276,453),ekran,&genRect(38,71,673,459));
  1162. std::string tob;
  1163. switch (ourScenSel->selectedDiff)
  1164. {
  1165. case 1:
  1166. tob="80%";
  1167. break;
  1168. case 2:
  1169. tob="100%";
  1170. break;
  1171. case 3:
  1172. tob="130%";
  1173. break;
  1174. case 4:
  1175. tob="160%";
  1176. break;
  1177. case 5:
  1178. tob="200%";
  1179. break;
  1180. }
  1181. CSDL_Ext::printAtMiddle(tob,703,477,GEOR13,zwykly);
  1182. }
  1183. void CPreGame::printMapsFrom(int from)
  1184. {
  1185. ourScenSel->mapsel.printMaps(from);
  1186. }
  1187. void CPreGame::showScenList()
  1188. {
  1189. if (currentTab!=&ourScenSel->mapsel)
  1190. {
  1191. ourScenSel->listShowed=true;
  1192. ourScenSel->mapsel.show();
  1193. }
  1194. else currentTab->hide();
  1195. }
  1196. CPreGame::CPreGame()
  1197. {
  1198. highlighted=NULL;
  1199. currentTab=NULL;
  1200. run=true;
  1201. timeHandler tmh;tmh.getDif();
  1202. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  1203. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  1204. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  1205. preth = new CPreGameTextHandler;
  1206. preth->loadTexts();
  1207. CGI->preth=preth;
  1208. THC std::cout<<"\tCPreGame: loading txts: "<<tmh.getDif()<<std::endl;
  1209. currentMessage=NULL;
  1210. behindCurMes=NULL;
  1211. initMainMenu();
  1212. THC std::cout<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  1213. initNewMenu();
  1214. THC std::cout<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  1215. initScenSel();
  1216. THC std::cout<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  1217. initOptions();
  1218. THC std::cout<<"\tCPreGame: scenario options initialization: "<<tmh.getDif()<<std::endl;
  1219. showMainMenu();
  1220. THC std::cout<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  1221. CPG=this;
  1222. playerName="Player";
  1223. }
  1224. void CPreGame::initOptions()
  1225. {
  1226. ourOptions = new Options();
  1227. ourOptions->init();
  1228. }
  1229. void CPreGame::initScenSel()
  1230. {
  1231. ourScenSel = new ScenSel();
  1232. ourScenSel->listShowed=false;
  1233. if (rand()%2) ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1000.bmp");
  1234. else ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1001.bmp");
  1235. ourScenSel->pressed=NULL;
  1236. ourScenSel->scenInf=CGI->bitmaph->loadBitmap("GSELPOP1.bmp");//SDL_LoadBMP("h3bitmap.lod\\GSELPOP1.bmp");
  1237. ourScenSel->randMap=CGI->bitmaph->loadBitmap("RANMAPBK.bmp");
  1238. ourScenSel->options=CGI->bitmaph->loadBitmap("ADVOPTBK.bmp");
  1239. SDL_SetColorKey(ourScenSel->scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenInf->format,0,255,255));
  1240. //SDL_SetColorKey(ourScenSel->scenList,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenList->format,0,255,255));
  1241. SDL_SetColorKey(ourScenSel->randMap,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->randMap->format,0,255,255));
  1242. SDL_SetColorKey(ourScenSel->options,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->options->format,0,255,255));
  1243. ourScenSel->difficulty = new CPoinGroup<>();
  1244. ourScenSel->difficulty->type=1;
  1245. ourScenSel->selectedDiff=-77;
  1246. ourScenSel->difficulty->gdzie = &ourScenSel->selectedDiff;
  1247. ourScenSel->bEasy = IntSelBut<>(genRect(0,0,506,456),NULL,CGI->spriteh->giveDef("GSPBUT3.DEF"),true,ourScenSel->difficulty,0);
  1248. ourScenSel->bNormal = IntSelBut<>(genRect(0,0,538,456),NULL,CGI->spriteh->giveDef("GSPBUT4.DEF"),true,ourScenSel->difficulty,1);
  1249. ourScenSel->bHard = IntSelBut<>(genRect(0,0,570,456),NULL,CGI->spriteh->giveDef("GSPBUT5.DEF"),true,ourScenSel->difficulty,2);
  1250. ourScenSel->bExpert = IntSelBut<>(genRect(0,0,602,456),NULL,CGI->spriteh->giveDef("GSPBUT6.DEF"),true,ourScenSel->difficulty,3);
  1251. ourScenSel->bImpossible = IntSelBut<>(genRect(0,0,634,456),NULL,CGI->spriteh->giveDef("GSPBUT7.DEF"),true,ourScenSel->difficulty,4);
  1252. ourScenSel->bBack = Button<>(genRect(0,0,584,535),&CPreGame::showNewMenu,CGI->spriteh->giveDef("SCNRBACK.DEF"));
  1253. ourScenSel->bBegin = Button<>(genRect(0,0,414,535),&CPreGame::begin,CGI->spriteh->giveDef("SCNRBEG.DEF"));
  1254. ourScenSel->bScens = Button<>(genRect(0,0,414,81),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1255. for (int i=0; i<ourScenSel->bScens.imgs->ourImages.size(); i++)
  1256. CSDL_Ext::printAt(CGI->generaltexth->allTexts[500],25+i,2+i,GEOR13,zwykly,ourScenSel->bScens.imgs->ourImages[i].bitmap); //"Show Available Scenarios"
  1257. ourScenSel->bRandom = Button<>(genRect(0,0,414,105),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1258. for (int i=0; i<ourScenSel->bRandom.imgs->ourImages.size(); i++)
  1259. CSDL_Ext::printAt(CGI->generaltexth->allTexts[740],25+i,2+i,GEOR13,zwykly,ourScenSel->bRandom.imgs->ourImages[i].bitmap);
  1260. ourScenSel->bOptions = Button<>(genRect(0,0,414,509),&CPreGame::showOptions,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1261. for (int i=0; i<ourScenSel->bOptions.imgs->ourImages.size(); i++)
  1262. CSDL_Ext::printAt(CGI->generaltexth->allTexts[501],25+i,2+i,GEOR13,zwykly,ourScenSel->bOptions.imgs->ourImages[i].bitmap); //"Show Advanced Options"
  1263. CPG=this;
  1264. ourScenSel->mapsel.init();
  1265. }
  1266. void CPreGame::showScenSel()
  1267. {
  1268. state=EState::ScenarioList;
  1269. SDL_BlitSurface(ourScenSel->background,NULL,ekran,NULL);
  1270. SDL_BlitSurface(ourScenSel->scenInf,NULL,ekran,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,396,6));
  1271. CSDL_Ext::printAt(CGI->generaltexth->allTexts[494],427,438,GEOR13);//"Map Diff:"
  1272. CSDL_Ext::printAt(CGI->generaltexth->allTexts[492],527,438,GEOR13); //player difficulty
  1273. CSDL_Ext::printAt(CGI->generaltexth->allTexts[218],685,438,GEOR13);//"Rating:"
  1274. //blit buttons
  1275. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bEasy.pos);
  1276. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bNormal.pos);
  1277. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bHard.pos);
  1278. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bExpert.pos);
  1279. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bImpossible.pos);
  1280. SDL_BlitSurface(ourScenSel->bBegin.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBegin.pos);
  1281. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBack.pos);
  1282. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  1283. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  1284. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  1285. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  1286. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  1287. //add buttons info
  1288. btns.push_back(&ourScenSel->bEasy);
  1289. btns.push_back(&ourScenSel->bNormal);
  1290. btns.push_back(&ourScenSel->bHard);
  1291. btns.push_back(&ourScenSel->bExpert);
  1292. btns.push_back(&ourScenSel->bImpossible);
  1293. btns.push_back(&ourScenSel->bScens);
  1294. btns.push_back(&ourScenSel->bRandom);
  1295. btns.push_back(&ourScenSel->bOptions);
  1296. btns.push_back(&ourScenSel->bBegin);
  1297. btns.push_back(&ourScenSel->bBack);
  1298. ourScenSel->selectedDiff=1;
  1299. ourScenSel->bNormal.select(true);
  1300. for (int i=0;i<btns.size();i++)
  1301. {
  1302. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  1303. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  1304. btns[i]->ID=10;
  1305. }
  1306. handleOther = &CPreGame::scenHandleEv;
  1307. ourScenSel->mapsel.select(0,false);
  1308. //ourScenSel->mapsel.printSelectedInfo(); // this is already called in select()
  1309. //SDL_Flip(ekran);
  1310. CSDL_Ext::update(ekran);
  1311. }
  1312. void CPreGame::showOptions()
  1313. {
  1314. ourOptions->show();
  1315. }
  1316. void CPreGame::initNewMenu()
  1317. {
  1318. ourNewMenu = new menuItems();
  1319. ourNewMenu->bgAd = CGI->bitmaph->loadBitmap("ZNEWGAM.bmp");
  1320. ourNewMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp");
  1321. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  1322. slh = new CSemiLodHandler();
  1323. slh->openLod("H3sprite.lod");
  1324. //loading menu buttons
  1325. ourNewMenu->newGame = CGI->spriteh->giveDef("ZTSINGL.DEF");
  1326. ourNewMenu->loadGame = CGI->spriteh->giveDef("ZTMULTI.DEF");
  1327. ourNewMenu->highScores = CGI->spriteh->giveDef("ZTCAMPN.DEF");
  1328. ourNewMenu->credits = CGI->spriteh->giveDef("ZTTUTOR.DEF");
  1329. ourNewMenu->quit = CGI->spriteh->giveDef("ZTBACK.DEF");
  1330. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  1331. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  1332. // single scenario
  1333. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  1334. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  1335. ourNewMenu->lNewGame.x=545;
  1336. ourNewMenu->lNewGame.y=4;
  1337. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  1338. //multiplayer
  1339. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  1340. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  1341. ourNewMenu->lLoadGame.x=568;
  1342. ourNewMenu->lLoadGame.y=120;
  1343. //campaign
  1344. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  1345. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  1346. ourNewMenu->lHighScores.x=541;
  1347. ourNewMenu->lHighScores.y=233;
  1348. //tutorial
  1349. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  1350. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  1351. ourNewMenu->lCredits.x=545;
  1352. ourNewMenu->lCredits.y=358;
  1353. //back
  1354. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  1355. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  1356. ourNewMenu->lQuit.x=582;
  1357. ourNewMenu->lQuit.y=464;
  1358. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  1359. ourNewMenu->highlighted=0;
  1360. }
  1361. void CPreGame::showNewMenu()
  1362. {
  1363. btns.clear();
  1364. interested.clear();
  1365. handleOther=NULL;
  1366. state = EState::newGame;
  1367. SDL_BlitSurface(ourNewMenu->background,NULL,ekran,NULL);
  1368. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lNewGame);
  1369. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lLoadGame);
  1370. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lHighScores);
  1371. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lCredits);
  1372. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lQuit);
  1373. //SDL_Flip(ekran);
  1374. CSDL_Ext::update(ekran);
  1375. }
  1376. void CPreGame::initMainMenu()
  1377. {
  1378. ourMainMenu = new menuItems();
  1379. ourMainMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  1380. CSemiLodHandler * slh = new CSemiLodHandler();
  1381. slh->openLod("H3sprite.lod");
  1382. //loading menu buttons
  1383. ourMainMenu->newGame = CGI->spriteh->giveDef("ZMENUNG.DEF");
  1384. ourMainMenu->loadGame = CGI->spriteh->giveDef("ZMENULG.DEF");
  1385. ourMainMenu->highScores = CGI->spriteh->giveDef("ZMENUHS.DEF");
  1386. ourMainMenu->credits = CGI->spriteh->giveDef("ZMENUCR.DEF");
  1387. ourMainMenu->quit = CGI->spriteh->giveDef("ZMENUQT.DEF");
  1388. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  1389. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  1390. // new game button location
  1391. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  1392. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  1393. ourMainMenu->lNewGame.x=540;
  1394. ourMainMenu->lNewGame.y=10;
  1395. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  1396. //load game location
  1397. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  1398. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  1399. ourMainMenu->lLoadGame.x=532;
  1400. ourMainMenu->lLoadGame.y=132;
  1401. //high scores
  1402. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  1403. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  1404. ourMainMenu->lHighScores.x=524;
  1405. ourMainMenu->lHighScores.y=251;
  1406. //credits
  1407. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  1408. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  1409. ourMainMenu->lCredits.x=557;
  1410. ourMainMenu->lCredits.y=359;
  1411. //quit
  1412. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  1413. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  1414. ourMainMenu->lQuit.x=586;
  1415. ourMainMenu->lQuit.y=468;
  1416. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  1417. ourMainMenu->highlighted=0;
  1418. handledLods.push_back(slh);
  1419. delete slh;
  1420. }
  1421. void CPreGame::showMainMenu()
  1422. {
  1423. state = EState::mainMenu;
  1424. SDL_BlitSurface(ourMainMenu->background,NULL,ekran,NULL);
  1425. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lNewGame);
  1426. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lLoadGame);
  1427. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lHighScores);
  1428. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lCredits);
  1429. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lQuit);
  1430. //SDL_Flip(ekran);
  1431. CSDL_Ext::update(ekran);
  1432. }
  1433. void CPreGame::highlightButton(int which, int on)
  1434. {
  1435. menuItems * current = currentItems();
  1436. switch (which)
  1437. {
  1438. case 1:
  1439. {
  1440. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,ekran,&current->lNewGame);
  1441. break;
  1442. }
  1443. case 2:
  1444. {
  1445. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,ekran,&current->lLoadGame);
  1446. break;
  1447. }
  1448. case 3:
  1449. {
  1450. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,ekran,&current->lHighScores);
  1451. break;
  1452. }
  1453. case 4:
  1454. {
  1455. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,ekran,&current->lCredits);
  1456. break;
  1457. }
  1458. case 5:
  1459. {
  1460. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,ekran,&current->lQuit);
  1461. break;
  1462. }
  1463. }
  1464. //SDL_Flip(ekran);
  1465. CSDL_Ext::update(ekran);
  1466. }
  1467. void CPreGame::showCenBox (std::string data)
  1468. {
  1469. CMessage * cmh = new CMessage();
  1470. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data));
  1471. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,ekran);
  1472. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1473. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  1474. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  1475. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  1476. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  1477. SDL_FreeSurface(infoBox);
  1478. currentMessage = new SDL_Rect(pos);
  1479. delete cmh;
  1480. }
  1481. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  1482. {
  1483. CMessage * cmh = new CMessage();
  1484. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  1485. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  1486. przyciski->push_back(ok);
  1487. przyciski->push_back(cancel);
  1488. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data), EWindowType::yesOrNO, przyciski, btnspos);
  1489. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,ekran);
  1490. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1491. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  1492. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  1493. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  1494. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  1495. SDL_FreeSurface(infoBox);
  1496. currentMessage = new SDL_Rect(pos);
  1497. (*btnspos)[0].x+=pos.x;
  1498. (*btnspos)[0].y+=pos.y;
  1499. (*btnspos)[1].x+=pos.x;
  1500. (*btnspos)[1].y+=pos.y;
  1501. btns.push_back(new Button<>((*btnspos)[0],&CPreGame::quit,ok,false, NULL,2));
  1502. btns.push_back(new Button<>((*btnspos)[1],(&CPreGame::hideBox),cancel,false, NULL,2));
  1503. delete cmh;
  1504. delete przyciski;
  1505. delete btnspos;
  1506. }
  1507. void CPreGame::hideBox ()
  1508. {
  1509. SDL_BlitSurface(behindCurMes,NULL,ekran,currentMessage);
  1510. SDL_UpdateRect
  1511. (ekran,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  1512. for (int i=0;i<btns.size();i++)
  1513. {
  1514. if (btns[i]->ID==2)
  1515. {
  1516. delete btns[i];
  1517. btns.erase(btns.begin()+i);
  1518. i--;
  1519. }
  1520. }
  1521. SDL_FreeSurface(behindCurMes);
  1522. delete currentMessage;
  1523. currentMessage = NULL;
  1524. behindCurMes=NULL;
  1525. }
  1526. CPreGame::menuItems * CPreGame::currentItems()
  1527. {
  1528. switch (state)
  1529. {
  1530. case EState::mainMenu:
  1531. return ourMainMenu;
  1532. case EState::newGame:
  1533. return ourNewMenu;
  1534. default:
  1535. return NULL;
  1536. }
  1537. }
  1538. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  1539. {
  1540. if(sEvent.type == SDL_MOUSEMOTION)
  1541. {
  1542. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y);
  1543. }
  1544. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1545. {
  1546. for (int i=0;i<btns.size(); i++)
  1547. {
  1548. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1549. {
  1550. btns[i]->press(true);
  1551. ourScenSel->pressed=(Button<>*)btns[i];
  1552. }
  1553. }
  1554. if (ourScenSel->listShowed && (sEvent.button.y>121) &&(sEvent.button.y<570)
  1555. && (sEvent.button.x>55) && (sEvent.button.x<372))
  1556. {
  1557. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  1558. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  1559. }
  1560. }
  1561. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1562. {
  1563. Button<> * prnr=ourScenSel->pressed;
  1564. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  1565. {
  1566. ourScenSel->pressed->press(false);
  1567. ourScenSel->pressed=NULL;
  1568. }
  1569. for (int i=0;i<btns.size(); i++)
  1570. {
  1571. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1572. {
  1573. if (btns[i]->selectable)
  1574. btns[i]->select(true);
  1575. if (btns[i]->type==1 && ((Button<>*)btns[i])->fun)
  1576. (this->*(((Button<>*)btns[i])->fun))();
  1577. int zz = btns.size();
  1578. if (i>=zz)
  1579. break;
  1580. if (btns[i]==prnr && btns[i]->type==2)
  1581. {
  1582. ((IntBut<> *)(btns[i]))->set();
  1583. ourScenSel->mapsel.slid->whereAreWe=0;
  1584. ourScenSel->mapsel.slid->updateSlid();
  1585. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  1586. ourScenSel->mapsel.printMaps(0);
  1587. }
  1588. }
  1589. }
  1590. }
  1591. else if (sEvent.type==SDL_MOUSEMOTION)
  1592. {
  1593. if (highlighted)
  1594. {
  1595. if (isItIn(&highlighted->pos,sEvent.motion.x,sEvent.motion.y))
  1596. return;
  1597. else
  1598. {
  1599. highlighted->hover(false);
  1600. highlighted = NULL;
  1601. }
  1602. }
  1603. for (int i=0;i<btns.size();i++)
  1604. {
  1605. if (!btns[i]->highlightable)
  1606. continue;
  1607. if(isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1608. {
  1609. btns[i]->hover(true);
  1610. highlighted=btns[i];
  1611. return;
  1612. }
  1613. else if (btns[i]->highlighted)
  1614. btns[i]->hover(false);
  1615. }
  1616. }
  1617. }
  1618. StartInfo CPreGame::runLoop()
  1619. {
  1620. SDL_Event sEvent;
  1621. while(run)
  1622. {
  1623. CGI->screenh->updateScreen();
  1624. try
  1625. {
  1626. if(SDL_PollEvent(&sEvent)) //wait for event...
  1627. {
  1628. menuItems * current = currentItems();
  1629. if(sEvent.type==SDL_QUIT)
  1630. return ret;
  1631. for (int i=0;i<interested.size();i++)
  1632. interested[i]->handleIt(sEvent);
  1633. if (!current)
  1634. {
  1635. (this->*handleOther)(sEvent);
  1636. }
  1637. else if (sEvent.type==SDL_KEYDOWN)
  1638. {
  1639. if (sEvent.key.keysym.sym==SDLK_q)
  1640. {
  1641. return ret;
  1642. break;
  1643. }
  1644. /*if (state==EState::newGame)
  1645. {
  1646. switch (sEvent.key.keysym.sym)
  1647. {
  1648. case SDLK_LEFT:
  1649. {
  1650. if(currentItems()->lNewGame.x>0)
  1651. currentItems()->lNewGame.x--;
  1652. break;
  1653. }
  1654. case (SDLK_RIGHT):
  1655. {
  1656. currentItems()->lNewGame.x++;
  1657. break;
  1658. }
  1659. case (SDLK_UP):
  1660. {
  1661. if(currentItems()->lNewGame.y>0)
  1662. currentItems()->lNewGame.y--;
  1663. break;
  1664. }
  1665. case (SDLK_DOWN):
  1666. {
  1667. currentItems()->lNewGame.y++;
  1668. break;
  1669. }
  1670. }
  1671. showNewMenu();
  1672. }*/
  1673. }
  1674. else if (sEvent.type==SDL_MOUSEMOTION)
  1675. {
  1676. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y); //for graphical mouse
  1677. if (currentMessage) continue;
  1678. if (current->highlighted)
  1679. {
  1680. switch (current->highlighted)
  1681. {
  1682. case 1:
  1683. {
  1684. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1685. continue;
  1686. else
  1687. {
  1688. current->highlighted=0;
  1689. highlightButton(1,0);
  1690. }
  1691. break;
  1692. }
  1693. case 2:
  1694. {
  1695. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1696. continue;
  1697. else
  1698. {
  1699. current->highlighted=0;
  1700. highlightButton(2,0);
  1701. }
  1702. break;
  1703. }
  1704. case 3:
  1705. {
  1706. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1707. continue;
  1708. else
  1709. {
  1710. current->highlighted=0;
  1711. highlightButton(3,0);
  1712. }
  1713. break;
  1714. }
  1715. case 4:
  1716. {
  1717. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1718. continue;
  1719. else
  1720. {
  1721. current->highlighted=0;
  1722. highlightButton(4,0);
  1723. }
  1724. break;
  1725. }
  1726. case 5:
  1727. {
  1728. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1729. continue;
  1730. else
  1731. {
  1732. current->highlighted=0;
  1733. highlightButton(5,0);
  1734. }
  1735. break;
  1736. }
  1737. } //switch (current->highlighted)
  1738. } // if (current->highlighted)
  1739. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1740. {
  1741. highlightButton(1,2);
  1742. current->highlighted=1;
  1743. }
  1744. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1745. {
  1746. highlightButton(2,2);
  1747. current->highlighted=2;
  1748. }
  1749. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1750. {
  1751. highlightButton(3,2);
  1752. current->highlighted=3;
  1753. }
  1754. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1755. {
  1756. highlightButton(4,2);
  1757. current->highlighted=4;
  1758. }
  1759. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1760. {
  1761. highlightButton(5,2);
  1762. current->highlighted=5;
  1763. }
  1764. }
  1765. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1766. {
  1767. mush->playClick();
  1768. for (int i=0;i<btns.size(); i++)
  1769. {
  1770. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1771. {
  1772. btns[i]->press(true);
  1773. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,ekran,&btns[i].pos);
  1774. //updateRect(&btns[i].pos);
  1775. }
  1776. }
  1777. if (currentMessage) continue;
  1778. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1779. {
  1780. highlightButton(1,1);
  1781. current->highlighted=1;
  1782. }
  1783. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1784. {
  1785. highlightButton(2,1);
  1786. current->highlighted=2;
  1787. }
  1788. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1789. {
  1790. highlightButton(3,1);
  1791. current->highlighted=3;
  1792. }
  1793. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1794. {
  1795. highlightButton(4,1);
  1796. current->highlighted=4;
  1797. }
  1798. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1799. {
  1800. highlightButton(5,1);
  1801. current->highlighted=5;
  1802. }
  1803. }
  1804. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1805. {
  1806. for (int i=0;i<btns.size(); i++)
  1807. {
  1808. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1809. (this->*(((Button<>*)btns[i])->fun))();
  1810. else
  1811. {
  1812. btns[i]->press(false);
  1813. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,ekran,&btns[i].pos);
  1814. //updateRect(&btns[i].pos);
  1815. }
  1816. }
  1817. if (currentMessage) continue;
  1818. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1819. {
  1820. highlightButton(1,2);
  1821. current->highlighted=1;
  1822. (this->*(current->fNewGame))();
  1823. }
  1824. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1825. {
  1826. highlightButton(2,2);
  1827. current->highlighted=2;
  1828. }
  1829. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1830. {
  1831. highlightButton(3,2);
  1832. current->highlighted=3;
  1833. }
  1834. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1835. {
  1836. highlightButton(4,2);
  1837. current->highlighted=4;
  1838. }
  1839. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1840. {
  1841. highlightButton(5,2);
  1842. current->highlighted=5;
  1843. (this->*(current->fQuit))();
  1844. }
  1845. }
  1846. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  1847. {
  1848. if (currentMessage) continue;
  1849. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1850. {
  1851. showCenBox(buttonText(0));
  1852. }
  1853. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1854. {
  1855. showCenBox(buttonText(1));
  1856. }
  1857. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1858. {
  1859. showCenBox(buttonText(2));
  1860. }
  1861. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1862. {
  1863. showCenBox(buttonText(3));
  1864. }
  1865. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  1866. {
  1867. showCenBox(buttonText(4));
  1868. }
  1869. }
  1870. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  1871. {
  1872. hideBox();
  1873. }
  1874. }
  1875. }
  1876. catch(...)
  1877. { continue; }
  1878. SDL_Delay(1); //give time for other apps
  1879. }
  1880. return ret;
  1881. }
  1882. std::string CPreGame::buttonText(int which)
  1883. {
  1884. if (state==EState::mainMenu)
  1885. {
  1886. switch (which)
  1887. {
  1888. case 0:
  1889. return preth->mainNewGame;
  1890. case 1:
  1891. return preth->mainLoadGame;
  1892. case 2:
  1893. return preth->mainHighScores;
  1894. case 3:
  1895. return preth->mainCredits;
  1896. case 4:
  1897. return preth->mainQuit;
  1898. }
  1899. }
  1900. else if (state==EState::newGame)
  1901. {
  1902. switch (which)
  1903. {
  1904. case 0:
  1905. return preth->ngSingleScenario;
  1906. case 1:
  1907. return preth->ngMultiplayer;
  1908. case 2:
  1909. return preth->ngCampain;
  1910. case 3:
  1911. return preth->ngTutorial;
  1912. case 4:
  1913. return preth->ngBack;
  1914. }
  1915. }
  1916. return std::string();
  1917. }
  1918. void CPreGame::quitAskBox()
  1919. {
  1920. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  1921. }
  1922. void CPreGame::sortMaps()
  1923. {
  1924. std::sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(ourScenSel->mapsel.sortBy));
  1925. ourScenSel->mapsel.select(0);
  1926. printMapsFrom(0);
  1927. }
  1928. void CPreGame::setTurnLength(int on)
  1929. {
  1930. int min;
  1931. switch (on)
  1932. {
  1933. case 0:
  1934. min=1;
  1935. break;
  1936. case 1:
  1937. min=2;
  1938. break;
  1939. case 2:
  1940. min=4;
  1941. break;
  1942. case 3:
  1943. min=6;
  1944. break;
  1945. case 4:
  1946. min=8;
  1947. break;
  1948. case 5:
  1949. min=10;
  1950. break;
  1951. case 6:
  1952. min=15;
  1953. break;
  1954. case 7:
  1955. min=20;
  1956. break;
  1957. case 8:
  1958. min=25;
  1959. break;
  1960. case 9:
  1961. min=30;
  1962. break;
  1963. case 10:
  1964. min=0;
  1965. break;
  1966. }
  1967. SDL_BlitSurface(ourOptions->bg,&genRect(23,134,256,547),ekran,&genRect(23,134,258,553));
  1968. updateRect(&genRect(23,134,258,553));
  1969. if (min)
  1970. {
  1971. std::ostringstream os;
  1972. os<<min<<" Minutes";
  1973. CSDL_Ext::printAtMiddle(os.str(),323,563,GEOR13);
  1974. }
  1975. else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13);
  1976. }