CPreGame.cpp 120 KB

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