CPreGame.cpp 120 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044
  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. CSDL_Ext::update(ekran);
  642. }
  643. void Options::hide()
  644. {
  645. if (!showed) return;
  646. PreGameTab::hide();
  647. for (int i=0; i<CPG->btns.size();i++)
  648. if (CPG->btns[i]->ID==7)
  649. CPG->btns.erase(CPG->btns.begin()+i--);
  650. for (int i=0;i<poptions.size();i++)
  651. delete poptions[i];
  652. poptions.clear();
  653. turnLength->deactivate();
  654. }
  655. MapSel::~MapSel()
  656. {
  657. SDL_FreeSurface(bg);
  658. for (int i=0;i<scenImgs.size();i++)
  659. SDL_FreeSurface(scenImgs[i]);
  660. for (int i=0;i<scenList.size();i++)
  661. delete scenList[i];
  662. delete sFlags;
  663. }
  664. int MapSel::countWL()
  665. {
  666. int ret=0;
  667. for (int i=0;i<ourMaps.size();i++)
  668. {
  669. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  670. continue;
  671. else ret++;
  672. }
  673. return ret;
  674. }
  675. void MapSel::printMaps(int from, int to, int at, bool abs)
  676. {
  677. if (true)//
  678. {
  679. int help=-1;
  680. for (int i=0;i<ourMaps.size();i++)
  681. {
  682. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  683. continue;
  684. else help++;
  685. if (help==from)
  686. {
  687. from=i;
  688. break;
  689. }
  690. }
  691. }
  692. SDL_Surface * scenin = CSDL_Ext::newSurface(351,25);
  693. SDL_Color nasz;
  694. for (int i=at;i<to;i++)
  695. {
  696. if ((i-at+from) > ourMaps.size()-1)
  697. {
  698. SDL_Surface * scenin = CSDL_Ext::newSurface(351,25);
  699. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  700. blitAt(scenin,24,121+(i-at)*25);
  701. //SDL_Flip(ekran);
  702. CSDL_Ext::update(ekran);
  703. SDL_FreeSurface(scenin);
  704. continue;
  705. }
  706. if (sizeFilter && ((ourMaps[(i-at)+from].width) != sizeFilter))
  707. {
  708. to++;
  709. at++;
  710. from++;
  711. if (((i-at)+from)>ourMaps.size()-1) break;
  712. else continue;
  713. }
  714. if ((i-at+from) == selected)
  715. nasz=tytulowy;
  716. else nasz=zwykly;
  717. //SDL_Rect pier = genRect(25,351,24,126+(i*25));
  718. SDL_BlitSurface(bg,&genRect(25,351,22,(i-at)*25+115),scenin,NULL);
  719. int temp=-1;
  720. std::ostringstream ostr(std::ostringstream::out); ostr << ourMaps[(i-at)+from].playerAmnt << "/" << ourMaps[(i-at)+from].humenPlayers;
  721. CSDL_Ext::printAt(ostr.str(),6,4,GEOR13,nasz,scenin, 2);
  722. std::string temp2;
  723. switch (ourMaps[(i-at)+from].width)
  724. {
  725. case 36:
  726. temp2="S";
  727. break;
  728. case 72:
  729. temp2="M";
  730. break;
  731. case 108:
  732. temp2="L";
  733. break;
  734. case 144:
  735. temp2="XL";
  736. break;
  737. default:
  738. temp2="C";
  739. break;
  740. }
  741. CSDL_Ext::printAtMiddle(temp2,50,13,GEOR13,nasz,scenin, 2);
  742. switch (ourMaps[(i-at)+from].version)
  743. {
  744. case Eformat::RoE:
  745. temp=0;
  746. break;
  747. case Eformat::AB:
  748. temp=1;
  749. break;
  750. case Eformat::SoD:
  751. temp=2;
  752. break;
  753. case Eformat::WoG:
  754. temp=3;
  755. break;
  756. }
  757. blitAt(Dtypes->ourImages[temp].bitmap,67,2,scenin);
  758. if (!(ourMaps[(i-at)+from].name.length()))
  759. ourMaps[(i-at)+from].name = "Unnamed";
  760. CSDL_Ext::printAtMiddle(ourMaps[(i-at)+from].name,192,13,GEOR13,nasz,scenin, 2);
  761. if (ourMaps[(i-at)+from].victoryCondition==EvictoryConditions::winStandard)
  762. temp=11;
  763. else temp=ourMaps[(i-at)+from].victoryCondition;
  764. blitAt(Dvic->ourImages[temp].bitmap,285,2,scenin);
  765. if (ourMaps[(i-at)+from].lossCondition.typeOfLossCon == ElossCon::lossStandard)
  766. temp=3;
  767. else temp=ourMaps[(i-at)+from].lossCondition.typeOfLossCon;
  768. blitAt(Dloss->ourImages[temp].bitmap,318,2,scenin);
  769. blitAt(scenin,24,121+(i-at)*25);
  770. SDL_UpdateRect(ekran,24,121+(i-at)*25,scenin->w,scenin->h);
  771. }
  772. SDL_FreeSurface(scenin);
  773. }
  774. int MapSel::whichWL(int nr)
  775. {
  776. int help=-1;
  777. for (int i=0;i<ourMaps.size();i++)
  778. {
  779. if (sizeFilter && ((ourMaps[i].width) != sizeFilter))
  780. continue;
  781. else help++;
  782. if (help==nr)
  783. {
  784. help=i;
  785. break;
  786. }
  787. }
  788. return help;
  789. }
  790. void MapSel::hide()
  791. {
  792. if (!showed)return;
  793. PreGameTab::hide();
  794. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&small));
  795. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&medium));
  796. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&large));
  797. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&xlarge));
  798. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&all));
  799. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&nrplayer));
  800. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&mapsize));
  801. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&type));
  802. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&name));
  803. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&viccon));
  804. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&loscon));
  805. slid->deactivate();
  806. };
  807. void MapSel::show()
  808. {
  809. if (showed)return;
  810. PreGameTab::show();
  811. //blit bg
  812. blitAt(bg,3,6);
  813. CSDL_Ext::printAt("Map Sizes",55,60,GEOR13);
  814. CSDL_Ext::printAt("Select a Scenario to Play",110,25,TNRB16);
  815. //size buttons
  816. small.show();
  817. medium.show();
  818. large.show();
  819. xlarge.show();
  820. all.show();
  821. CPG->btns.push_back(&small);
  822. CPG->btns.push_back(&medium);
  823. CPG->btns.push_back(&large);
  824. CPG->btns.push_back(&xlarge);
  825. CPG->btns.push_back(&all);
  826. //sort by buttons
  827. nrplayer.show();
  828. mapsize.show();
  829. type.show();
  830. name.show();
  831. viccon.show();
  832. loscon.show();
  833. CPG->btns.push_back(&nrplayer);
  834. CPG->btns.push_back(&mapsize);
  835. CPG->btns.push_back(&type);
  836. CPG->btns.push_back(&name);
  837. CPG->btns.push_back(&viccon);
  838. CPG->btns.push_back(&loscon);
  839. //print scenario list
  840. printMaps(0,18);
  841. slid->activate();
  842. //SDL_Flip(ekran);
  843. CSDL_Ext::update(ekran);
  844. }
  845. void MapSel::init()
  846. {
  847. bg = CGI->bitmaph->loadBitmap("SCSELBCK.bmp");
  848. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  849. small.imgs = CGI->spriteh->giveDef("SCSMBUT.DEF");
  850. small.fun = NULL;
  851. small.pos = genRect(small.imgs->ourImages[0].bitmap->h,small.imgs->ourImages[0].bitmap->w,161,52);
  852. small.ourGroup=NULL;
  853. medium.imgs = CGI->spriteh->giveDef("SCMDBUT.DEF");
  854. medium.fun = NULL;
  855. medium.pos = genRect(medium.imgs->ourImages[0].bitmap->h,medium.imgs->ourImages[0].bitmap->w,208,52);
  856. medium.ourGroup=NULL;
  857. large.imgs = CGI->spriteh->giveDef("SCLGBUT.DEF");
  858. large.fun = NULL;
  859. large.pos = genRect(large.imgs->ourImages[0].bitmap->h,large.imgs->ourImages[0].bitmap->w,255,52);
  860. large.ourGroup=NULL;
  861. xlarge.imgs = CGI->spriteh->giveDef("SCXLBUT.DEF");
  862. xlarge.fun = NULL;
  863. xlarge.pos = genRect(xlarge.imgs->ourImages[0].bitmap->h,xlarge.imgs->ourImages[0].bitmap->w,302,52);
  864. xlarge.ourGroup=NULL;
  865. all.imgs = CGI->spriteh->giveDef("SCALBUT.DEF");
  866. all.fun = NULL;
  867. all.pos = genRect(all.imgs->ourImages[0].bitmap->h,all.imgs->ourImages[0].bitmap->w,349,52);
  868. all.ourGroup=NULL;
  869. all.selectable=xlarge.selectable=large.selectable=medium.selectable=small.selectable=false;
  870. small.what=medium.what=large.what=xlarge.what=all.what=&sizeFilter;
  871. small.key=36;medium.key=72;large.key=108;xlarge.key=144;all.key=0;
  872. //Button<> nrplayer, mapsize, type, name, viccon, loscon;
  873. nrplayer.imgs = CGI->spriteh->giveDef("SCBUTT1.DEF");
  874. nrplayer.fun = NULL;
  875. nrplayer.pos = genRect(nrplayer.imgs->ourImages[0].bitmap->h,nrplayer.imgs->ourImages[0].bitmap->w,26,92);
  876. nrplayer.key=ESortBy::playerAm;
  877. mapsize.imgs = CGI->spriteh->giveDef("SCBUTT2.DEF");
  878. mapsize.fun = NULL;
  879. mapsize.pos = genRect(mapsize.imgs->ourImages[0].bitmap->h,mapsize.imgs->ourImages[0].bitmap->w,58,92);
  880. mapsize.key=ESortBy::size;
  881. type.imgs = CGI->spriteh->giveDef("SCBUTCP.DEF");
  882. type.fun = NULL;
  883. type.pos = genRect(type.imgs->ourImages[0].bitmap->h,type.imgs->ourImages[0].bitmap->w,91,92);
  884. type.key=ESortBy::format;
  885. name.imgs = CGI->spriteh->giveDef("SCBUTT3.DEF");
  886. name.fun = NULL;
  887. name.pos = genRect(name.imgs->ourImages[0].bitmap->h,name.imgs->ourImages[0].bitmap->w,124,92);
  888. name.key=ESortBy::name;
  889. viccon.imgs = CGI->spriteh->giveDef("SCBUTT4.DEF");
  890. viccon.fun = NULL;
  891. viccon.pos = genRect(viccon.imgs->ourImages[0].bitmap->h,viccon.imgs->ourImages[0].bitmap->w,309,92);
  892. viccon.key=ESortBy::viccon;
  893. loscon.imgs = CGI->spriteh->giveDef("SCBUTT5.DEF");
  894. loscon.fun = NULL;
  895. loscon.pos = genRect(loscon.imgs->ourImages[0].bitmap->h,loscon.imgs->ourImages[0].bitmap->w,342,92);
  896. loscon.key=ESortBy::loscon;
  897. nrplayer.poin=mapsize.poin=type.poin=name.poin=viccon.poin=loscon.poin=(int*)(&sortBy);
  898. nrplayer.fun=mapsize.fun=type.fun=name.fun=viccon.fun=loscon.fun=&CPreGame::sortMaps;
  899. Dtypes = CGI->spriteh->giveDef("SCSELC.DEF");
  900. Dvic = CGI->spriteh->giveDef("SCNRVICT.DEF");
  901. Dloss = CGI->spriteh->giveDef("SCNRLOSS.DEF");
  902. //Dsizes = CPG->slh->giveDef("SCNRMPSZ.DEF");
  903. Dsizes = CGI->spriteh->giveDef("SCNRMPSZ.DEF");
  904. sFlags = CGI->spriteh->giveDef("ITGFLAGS.DEF");
  905. //get map files names
  906. std::vector<std::string> pliczkiTemp;
  907. fs::path tie( (fs::initial_path<fs::path>())/"/maps" );
  908. fs::directory_iterator end_iter;
  909. for ( fs::directory_iterator dir (tie); dir!=end_iter; ++dir )
  910. {
  911. if (fs::is_regular(dir->status()));
  912. {
  913. if (boost::ends_with(dir->path().leaf(),std::string(".h3m")))
  914. pliczkiTemp.push_back("Maps/"+(dir->path().leaf()));
  915. }
  916. }
  917. for (int i=0; i<pliczkiTemp.size();i++)
  918. {
  919. gzFile tempf = gzopen(pliczkiTemp[i].c_str(),"rb");
  920. std::string sss;
  921. int iii=0;
  922. while(++iii)
  923. {
  924. if (iii>4000) break;
  925. int z = gzgetc (tempf);
  926. if (z>=0)
  927. {
  928. sss+=unsigned char(z);
  929. }
  930. else break;
  931. }
  932. gzclose(tempf);
  933. //if (sss[0]<28) continue; //zly format
  934. if (!sss[4]) continue; //nie ma graczy? mapa niegrywalna //już to kiedyś komentowałem- - to bzdura //tu calkiem pasuje...
  935. unsigned char* file2 = new unsigned char[sss.length()];
  936. for (int j=0;j<sss.length();j++)
  937. file2[j]=sss[j];
  938. ourMaps.push_back(CMapInfo(pliczkiTemp[i],file2));
  939. delete[] file2;
  940. }
  941. std::sort(ourMaps.begin(),ourMaps.end(),mapSorter(ESortBy::name));
  942. slid = new Slider<>(375,92,480,ourMaps.size(),18,true);
  943. slid->fun = &CPreGame::printMapsFrom;
  944. }
  945. void MapSel::moveByOne(bool up)
  946. {
  947. int help=selected;
  948. if (up) selected--;
  949. else selected ++;
  950. for (int i=selected;i<ourMaps.size() && i>=0;)
  951. {
  952. help=i;
  953. if (!(sizeFilter && ((ourMaps[i].width) != sizeFilter)))
  954. break;
  955. if (up)
  956. {
  957. i--;
  958. }
  959. else
  960. {
  961. i++;
  962. if (i<0) break;
  963. }
  964. }
  965. select(help);
  966. slid->updateSlid();
  967. }
  968. void MapSel::select(int which, bool updateMapsList)
  969. {
  970. selected = which;
  971. if(updateMapsList)
  972. printMaps(slid->whereAreWe,18,0,true);
  973. CPG->ret.playerInfos.clear();
  974. bool wasntpl = true;
  975. for (int i=0;i<PLAYER_LIMIT;i++)
  976. {
  977. if (!(ourMaps[selected].players[i].canComputerPlay || ourMaps[selected].players[i].canComputerPlay))
  978. 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)
  979. StartInfo::PlayerSettings pset;
  980. pset.color=(Ecolor)i;
  981. pset.bonus=Ebonus::brandom;
  982. pset.castle=-2;
  983. if (ourMaps[which].players[i].canHumanPlay && wasntpl)
  984. {
  985. pset.name=CGI->generaltexth->allTexts[434];
  986. CPG->playerColor = i;
  987. wasntpl = false;
  988. }
  989. else
  990. {
  991. pset.name=CGI->generaltexth->allTexts[468];
  992. }
  993. if ( (ourMaps[which].players[i].heroesNames.size()>1) ||
  994. (((ourMaps[which].players[i].heroesNames.size()>1)==1) && ourMaps[which].players[i].generateHeroAtMainTown==false)
  995. )
  996. pset.hero=-2;
  997. else if (ourMaps[which].players[i].heroesNames.size()==1)
  998. {
  999. pset.hero=ourMaps[which].players[i].heroesNames[0].heroID;
  1000. }
  1001. else
  1002. pset.hero=-1;
  1003. for (int j=0;j<F_NUMBER;j++)
  1004. {
  1005. if (((int)pow((double)2,j))&ourMaps[selected].players[i].allowedFactions)
  1006. {
  1007. if (pset.castle>=0)
  1008. pset.castle=-1;
  1009. if (pset.castle==-2)
  1010. pset.castle=j;
  1011. }
  1012. }
  1013. if (ourMaps[selected].players[i].generateHeroAtMainTown || ourMaps[selected].players[i].generateHero)
  1014. pset.hero=-1;
  1015. else if (ourMaps[selected].players[i].mainHeroName.length())
  1016. {
  1017. pset.heroName=ourMaps[selected].players[i].mainHeroName;
  1018. pset.heroPortrait=ourMaps[selected].players[i].mainHeroPortrait;
  1019. }
  1020. else
  1021. pset.hero=-2;
  1022. pset.handicap=0;
  1023. pset.heroPortrait=-1;
  1024. CPG->ret.playerInfos.push_back(pset);
  1025. }
  1026. printSelectedInfo();
  1027. }
  1028. MapSel::MapSel():selected(0),sizeFilter(0)
  1029. {
  1030. }
  1031. void MapSel::printSelectedInfo()
  1032. {
  1033. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),ekran,&genRect(399,337,413,29));
  1034. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),ekran,&genRect(50,91,414,453));
  1035. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bScens.pos);
  1036. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bOptions.pos);
  1037. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,ekran,&CPG->ourScenSel->bRandom.pos);
  1038. //blit texts
  1039. CSDL_Ext::printAt(CPG->preth->singleScenarioName,420,25,GEOR13);
  1040. CSDL_Ext::printAt("Scenario Description:",420,135,GEOR13);
  1041. CSDL_Ext::printAt("Victory Condition:",420,285,GEOR13);
  1042. CSDL_Ext::printAt("Loss Condition:",420,340,GEOR13);
  1043. CSDL_Ext::printAt("Allies:",420,406,GEOR13,zwykly);
  1044. CSDL_Ext::printAt("Enemies:",585,406,GEOR13,zwykly);
  1045. int temp = ourMaps[selected].victoryCondition+1;
  1046. if (temp>20) temp=0;
  1047. std::string sss = CPG->preth->victoryConditions[temp];
  1048. if (temp && ourMaps[selected].vicConDetails->allowNormalVictory) sss+= "/" + CPG->preth->victoryConditions[0];
  1049. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  1050. temp = ourMaps[selected].lossCondition.typeOfLossCon+1;
  1051. if (temp>20) temp=0;
  1052. sss = CPG->preth->lossCondtions[temp];
  1053. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  1054. //blit descrption
  1055. std::vector<std::string> desc = *CMessage::breakText(ourMaps[selected].description,50);
  1056. for (int i=0;i<desc.size();i++)
  1057. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  1058. if ((selected < 0) || (selected >= ourMaps.size()))
  1059. return;
  1060. if (ourMaps[selected].name.length())
  1061. CSDL_Ext::printAt(ourMaps[selected].name,420,41,GEORXX);
  1062. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  1063. std::string diff;
  1064. switch (ourMaps[selected].difficulty)
  1065. {
  1066. case 0:
  1067. diff=gdiff(CPG->preth->singleEasy);
  1068. break;
  1069. case 1:
  1070. diff=gdiff(CPG->preth->singleNormal);
  1071. break;
  1072. case 2:
  1073. diff=gdiff(CPG->preth->singleHard);
  1074. break;
  1075. case 3:
  1076. diff=gdiff(CPG->preth->singleExpert);
  1077. break;
  1078. case 4:
  1079. diff=gdiff(CPG->preth->singleImpossible);
  1080. break;
  1081. }
  1082. temp=-1;
  1083. switch (ourMaps[selected].width)
  1084. {
  1085. case 36:
  1086. temp=0;
  1087. break;
  1088. case 72:
  1089. temp=1;
  1090. break;
  1091. case 108:
  1092. temp=2;
  1093. break;
  1094. case 144:
  1095. temp=3;
  1096. break;
  1097. default:
  1098. temp=4;
  1099. break;
  1100. }
  1101. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  1102. temp=ourMaps[selected].victoryCondition;
  1103. if (temp>12) temp=11;
  1104. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  1105. temp=ourMaps[selected].lossCondition.typeOfLossCon;
  1106. if (temp>12) temp=3;
  1107. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  1108. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  1109. //SDL_Flip(ekran);
  1110. printFlags();
  1111. CSDL_Ext::update(ekran);
  1112. }
  1113. void MapSel::printFlags()
  1114. {
  1115. int hy=405, fx=460, ex=640, myT;
  1116. if (ourMaps[selected].howManyTeams)
  1117. myT = ourMaps[selected].players[CPG->playerColor].team;
  1118. else myT = -1;
  1119. for (int i=0;i<CPG->ret.playerInfos.size();i++)
  1120. {
  1121. if (myT>=0)
  1122. {
  1123. if(ourMaps[selected].players[CPG->ret.playerInfos[i].color].team==myT)
  1124. {
  1125. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1126. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1127. }
  1128. else
  1129. {
  1130. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1131. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1132. }
  1133. }
  1134. else
  1135. {
  1136. if(CPG->ret.playerInfos[i].color==CPG->playerColor)
  1137. {
  1138. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1139. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1140. }
  1141. else
  1142. {
  1143. blitAt(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1144. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1145. }
  1146. }
  1147. }
  1148. }
  1149. std::string MapSel::gdiff(std::string ss)
  1150. {
  1151. std::string ret;
  1152. for (int i=2;i<ss.length();i++)
  1153. {
  1154. if (ss[i]==' ')
  1155. break;
  1156. ret+=ss[i];
  1157. }
  1158. return ret;
  1159. }
  1160. void CPreGame::printRating()
  1161. {
  1162. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(38,71,276,453),ekran,&genRect(38,71,673,459));
  1163. std::string tob;
  1164. switch (ourScenSel->selectedDiff)
  1165. {
  1166. case 1:
  1167. tob="80%";
  1168. break;
  1169. case 2:
  1170. tob="100%";
  1171. break;
  1172. case 3:
  1173. tob="130%";
  1174. break;
  1175. case 4:
  1176. tob="160%";
  1177. break;
  1178. case 5:
  1179. tob="200%";
  1180. break;
  1181. }
  1182. CSDL_Ext::printAtMiddle(tob,703,477,GEOR13,zwykly);
  1183. }
  1184. void CPreGame::printMapsFrom(int from)
  1185. {
  1186. ourScenSel->mapsel.printMaps(from);
  1187. }
  1188. void CPreGame::showScenList()
  1189. {
  1190. if (currentTab!=&ourScenSel->mapsel)
  1191. {
  1192. ourScenSel->listShowed=true;
  1193. ourScenSel->mapsel.show();
  1194. }
  1195. else currentTab->hide();
  1196. }
  1197. CPreGame::CPreGame()
  1198. {
  1199. highlighted=NULL;
  1200. currentTab=NULL;
  1201. run=true;
  1202. timeHandler tmh;tmh.getDif();
  1203. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  1204. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  1205. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  1206. preth = new CPreGameTextHandler;
  1207. preth->loadTexts();
  1208. CGI->preth=preth;
  1209. THC std::cout<<"\tCPreGame: loading txts: "<<tmh.getDif()<<std::endl;
  1210. currentMessage=NULL;
  1211. behindCurMes=NULL;
  1212. initMainMenu();
  1213. THC std::cout<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  1214. initNewMenu();
  1215. THC std::cout<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  1216. initScenSel();
  1217. THC std::cout<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  1218. initOptions();
  1219. THC std::cout<<"\tCPreGame: scenario options initialization: "<<tmh.getDif()<<std::endl;
  1220. showMainMenu();
  1221. THC std::cout<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  1222. CPG=this;
  1223. playerName="Player";
  1224. }
  1225. void CPreGame::initOptions()
  1226. {
  1227. ourOptions = new Options();
  1228. ourOptions->init();
  1229. }
  1230. void CPreGame::initScenSel()
  1231. {
  1232. ourScenSel = new ScenSel();
  1233. ourScenSel->listShowed=false;
  1234. if (rand()%2) ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1000.bmp");
  1235. else ourScenSel->background=CGI->bitmaph->loadBitmap("ZPIC1001.bmp");
  1236. ourScenSel->pressed=NULL;
  1237. ourScenSel->scenInf=CGI->bitmaph->loadBitmap("GSELPOP1.bmp");//SDL_LoadBMP("h3bitmap.lod\\GSELPOP1.bmp");
  1238. ourScenSel->randMap=CGI->bitmaph->loadBitmap("RANMAPBK.bmp");
  1239. ourScenSel->options=CGI->bitmaph->loadBitmap("ADVOPTBK.bmp");
  1240. SDL_SetColorKey(ourScenSel->scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenInf->format,0,255,255));
  1241. //SDL_SetColorKey(ourScenSel->scenList,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->scenList->format,0,255,255));
  1242. SDL_SetColorKey(ourScenSel->randMap,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->randMap->format,0,255,255));
  1243. SDL_SetColorKey(ourScenSel->options,SDL_SRCCOLORKEY,SDL_MapRGB(ourScenSel->options->format,0,255,255));
  1244. ourScenSel->difficulty = new CPoinGroup<>();
  1245. ourScenSel->difficulty->type=1;
  1246. ourScenSel->selectedDiff=-77;
  1247. ourScenSel->difficulty->gdzie = &ourScenSel->selectedDiff;
  1248. ourScenSel->bEasy = IntSelBut<>(genRect(0,0,506,456),NULL,CGI->spriteh->giveDef("GSPBUT3.DEF"),true,ourScenSel->difficulty,0);
  1249. ourScenSel->bNormal = IntSelBut<>(genRect(0,0,538,456),NULL,CGI->spriteh->giveDef("GSPBUT4.DEF"),true,ourScenSel->difficulty,1);
  1250. ourScenSel->bHard = IntSelBut<>(genRect(0,0,570,456),NULL,CGI->spriteh->giveDef("GSPBUT5.DEF"),true,ourScenSel->difficulty,2);
  1251. ourScenSel->bExpert = IntSelBut<>(genRect(0,0,602,456),NULL,CGI->spriteh->giveDef("GSPBUT6.DEF"),true,ourScenSel->difficulty,3);
  1252. ourScenSel->bImpossible = IntSelBut<>(genRect(0,0,634,456),NULL,CGI->spriteh->giveDef("GSPBUT7.DEF"),true,ourScenSel->difficulty,4);
  1253. ourScenSel->bBack = Button<>(genRect(0,0,584,535),&CPreGame::showNewMenu,CGI->spriteh->giveDef("SCNRBACK.DEF"));
  1254. ourScenSel->bBegin = Button<>(genRect(0,0,414,535),&CPreGame::begin,CGI->spriteh->giveDef("SCNRBEG.DEF"));
  1255. ourScenSel->bScens = Button<>(genRect(0,0,414,81),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1256. for (int i=0; i<ourScenSel->bScens.imgs->ourImages.size(); i++)
  1257. CSDL_Ext::printAt(CGI->generaltexth->allTexts[500],25+i,2+i,GEOR13,zwykly,ourScenSel->bScens.imgs->ourImages[i].bitmap); //"Show Available Scenarios"
  1258. ourScenSel->bRandom = Button<>(genRect(0,0,414,105),&CPreGame::showScenList,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1259. for (int i=0; i<ourScenSel->bRandom.imgs->ourImages.size(); i++)
  1260. CSDL_Ext::printAt(CGI->generaltexth->allTexts[740],25+i,2+i,GEOR13,zwykly,ourScenSel->bRandom.imgs->ourImages[i].bitmap);
  1261. ourScenSel->bOptions = Button<>(genRect(0,0,414,509),&CPreGame::showOptions,CGI->spriteh->giveDef("GSPBUTT.DEF"));
  1262. for (int i=0; i<ourScenSel->bOptions.imgs->ourImages.size(); i++)
  1263. CSDL_Ext::printAt(CGI->generaltexth->allTexts[501],25+i,2+i,GEOR13,zwykly,ourScenSel->bOptions.imgs->ourImages[i].bitmap); //"Show Advanced Options"
  1264. CPG=this;
  1265. ourScenSel->mapsel.init();
  1266. }
  1267. void CPreGame::showScenSel()
  1268. {
  1269. state=EState::ScenarioList;
  1270. SDL_BlitSurface(ourScenSel->background,NULL,ekran,NULL);
  1271. SDL_BlitSurface(ourScenSel->scenInf,NULL,ekran,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,396,6));
  1272. CSDL_Ext::printAt(CGI->generaltexth->allTexts[494],427,438,GEOR13);//"Map Diff:"
  1273. CSDL_Ext::printAt(CGI->generaltexth->allTexts[492],527,438,GEOR13); //player difficulty
  1274. CSDL_Ext::printAt(CGI->generaltexth->allTexts[218],685,438,GEOR13);//"Rating:"
  1275. //blit buttons
  1276. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bEasy.pos);
  1277. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bNormal.pos);
  1278. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bHard.pos);
  1279. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bExpert.pos);
  1280. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bImpossible.pos);
  1281. SDL_BlitSurface(ourScenSel->bBegin.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBegin.pos);
  1282. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,ekran,&ourScenSel->bBack.pos);
  1283. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  1284. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  1285. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  1286. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  1287. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  1288. //add buttons info
  1289. btns.push_back(&ourScenSel->bEasy);
  1290. btns.push_back(&ourScenSel->bNormal);
  1291. btns.push_back(&ourScenSel->bHard);
  1292. btns.push_back(&ourScenSel->bExpert);
  1293. btns.push_back(&ourScenSel->bImpossible);
  1294. btns.push_back(&ourScenSel->bScens);
  1295. btns.push_back(&ourScenSel->bRandom);
  1296. btns.push_back(&ourScenSel->bOptions);
  1297. btns.push_back(&ourScenSel->bBegin);
  1298. btns.push_back(&ourScenSel->bBack);
  1299. ourScenSel->selectedDiff=1;
  1300. ourScenSel->bNormal.select(true);
  1301. for (int i=0;i<btns.size();i++)
  1302. {
  1303. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  1304. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  1305. btns[i]->ID=10;
  1306. }
  1307. handleOther = &CPreGame::scenHandleEv;
  1308. ourScenSel->mapsel.select(0,false);
  1309. //ourScenSel->mapsel.printSelectedInfo(); // this is already called in select()
  1310. //SDL_Flip(ekran);
  1311. CSDL_Ext::update(ekran);
  1312. }
  1313. void CPreGame::showOptions()
  1314. {
  1315. ourOptions->show();
  1316. }
  1317. void CPreGame::initNewMenu()
  1318. {
  1319. ourNewMenu = new menuItems();
  1320. ourNewMenu->bgAd = CGI->bitmaph->loadBitmap("ZNEWGAM.bmp");
  1321. ourNewMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp");
  1322. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  1323. slh = new CSemiLodHandler();
  1324. slh->openLod("H3sprite.lod");
  1325. //loading menu buttons
  1326. ourNewMenu->newGame = CGI->spriteh->giveDef("ZTSINGL.DEF");
  1327. ourNewMenu->loadGame = CGI->spriteh->giveDef("ZTMULTI.DEF");
  1328. ourNewMenu->highScores = CGI->spriteh->giveDef("ZTCAMPN.DEF");
  1329. ourNewMenu->credits = CGI->spriteh->giveDef("ZTTUTOR.DEF");
  1330. ourNewMenu->quit = CGI->spriteh->giveDef("ZTBACK.DEF");
  1331. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  1332. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  1333. // single scenario
  1334. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  1335. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  1336. ourNewMenu->lNewGame.x=545;
  1337. ourNewMenu->lNewGame.y=4;
  1338. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  1339. //multiplayer
  1340. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  1341. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  1342. ourNewMenu->lLoadGame.x=568;
  1343. ourNewMenu->lLoadGame.y=120;
  1344. //campaign
  1345. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  1346. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  1347. ourNewMenu->lHighScores.x=541;
  1348. ourNewMenu->lHighScores.y=233;
  1349. //tutorial
  1350. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  1351. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  1352. ourNewMenu->lCredits.x=545;
  1353. ourNewMenu->lCredits.y=358;
  1354. //back
  1355. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  1356. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  1357. ourNewMenu->lQuit.x=582;
  1358. ourNewMenu->lQuit.y=464;
  1359. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  1360. ourNewMenu->highlighted=0;
  1361. }
  1362. void CPreGame::showNewMenu()
  1363. {
  1364. btns.clear();
  1365. interested.clear();
  1366. handleOther=NULL;
  1367. state = EState::newGame;
  1368. SDL_BlitSurface(ourNewMenu->background,NULL,ekran,NULL);
  1369. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lNewGame);
  1370. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lLoadGame);
  1371. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lHighScores);
  1372. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lCredits);
  1373. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourNewMenu->lQuit);
  1374. //SDL_Flip(ekran);
  1375. CSDL_Ext::update(ekran);
  1376. }
  1377. void CPreGame::initMainMenu()
  1378. {
  1379. ourMainMenu = new menuItems();
  1380. ourMainMenu->background = CGI->bitmaph->loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  1381. CSemiLodHandler * slh = new CSemiLodHandler();
  1382. slh->openLod("H3sprite.lod");
  1383. //loading menu buttons
  1384. ourMainMenu->newGame = CGI->spriteh->giveDef("ZMENUNG.DEF");
  1385. ourMainMenu->loadGame = CGI->spriteh->giveDef("ZMENULG.DEF");
  1386. ourMainMenu->highScores = CGI->spriteh->giveDef("ZMENUHS.DEF");
  1387. ourMainMenu->credits = CGI->spriteh->giveDef("ZMENUCR.DEF");
  1388. ourMainMenu->quit = CGI->spriteh->giveDef("ZMENUQT.DEF");
  1389. ok = CGI->spriteh->giveDef("IOKAY.DEF");
  1390. cancel = CGI->spriteh->giveDef("ICANCEL.DEF");
  1391. // new game button location
  1392. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  1393. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  1394. ourMainMenu->lNewGame.x=540;
  1395. ourMainMenu->lNewGame.y=10;
  1396. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  1397. //load game location
  1398. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  1399. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  1400. ourMainMenu->lLoadGame.x=532;
  1401. ourMainMenu->lLoadGame.y=132;
  1402. //high scores
  1403. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  1404. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  1405. ourMainMenu->lHighScores.x=524;
  1406. ourMainMenu->lHighScores.y=251;
  1407. //credits
  1408. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  1409. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  1410. ourMainMenu->lCredits.x=557;
  1411. ourMainMenu->lCredits.y=359;
  1412. //quit
  1413. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  1414. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  1415. ourMainMenu->lQuit.x=586;
  1416. ourMainMenu->lQuit.y=468;
  1417. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  1418. ourMainMenu->highlighted=0;
  1419. handledLods.push_back(slh);
  1420. delete slh;
  1421. }
  1422. void CPreGame::showMainMenu()
  1423. {
  1424. state = EState::mainMenu;
  1425. SDL_BlitSurface(ourMainMenu->background,NULL,ekran,NULL);
  1426. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lNewGame);
  1427. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lLoadGame);
  1428. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lHighScores);
  1429. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lCredits);
  1430. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,ekran,&ourMainMenu->lQuit);
  1431. //SDL_Flip(ekran);
  1432. CSDL_Ext::update(ekran);
  1433. }
  1434. void CPreGame::highlightButton(int which, int on)
  1435. {
  1436. menuItems * current = currentItems();
  1437. switch (which)
  1438. {
  1439. case 1:
  1440. {
  1441. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,ekran,&current->lNewGame);
  1442. break;
  1443. }
  1444. case 2:
  1445. {
  1446. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,ekran,&current->lLoadGame);
  1447. break;
  1448. }
  1449. case 3:
  1450. {
  1451. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,ekran,&current->lHighScores);
  1452. break;
  1453. }
  1454. case 4:
  1455. {
  1456. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,ekran,&current->lCredits);
  1457. break;
  1458. }
  1459. case 5:
  1460. {
  1461. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,ekran,&current->lQuit);
  1462. break;
  1463. }
  1464. }
  1465. //SDL_Flip(ekran);
  1466. CSDL_Ext::update(ekran);
  1467. }
  1468. void CPreGame::showCenBox (std::string data)
  1469. {
  1470. CMessage * cmh = new CMessage();
  1471. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data));
  1472. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,ekran);
  1473. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1474. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  1475. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  1476. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  1477. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  1478. SDL_FreeSurface(infoBox);
  1479. currentMessage = new SDL_Rect(pos);
  1480. delete cmh;
  1481. }
  1482. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  1483. {
  1484. CMessage * cmh = new CMessage();
  1485. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  1486. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  1487. przyciski->push_back(ok);
  1488. przyciski->push_back(cancel);
  1489. SDL_Surface * infoBox = cmh->genMessage(preth->getTitle(data), preth->getDescr(data), EWindowType::yesOrNO, przyciski, btnspos);
  1490. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,ekran);
  1491. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1492. (ekran->w/2)-(infoBox->w/2),(ekran->h/2)-(infoBox->h/2));
  1493. SDL_BlitSurface(ekran,&pos,behindCurMes,NULL);
  1494. SDL_BlitSurface(infoBox,NULL,ekran,&pos);
  1495. SDL_UpdateRect(ekran,pos.x,pos.y,pos.w,pos.h);
  1496. SDL_FreeSurface(infoBox);
  1497. currentMessage = new SDL_Rect(pos);
  1498. (*btnspos)[0].x+=pos.x;
  1499. (*btnspos)[0].y+=pos.y;
  1500. (*btnspos)[1].x+=pos.x;
  1501. (*btnspos)[1].y+=pos.y;
  1502. btns.push_back(new Button<>((*btnspos)[0],&CPreGame::quit,ok,false, NULL,2));
  1503. btns.push_back(new Button<>((*btnspos)[1],(&CPreGame::hideBox),cancel,false, NULL,2));
  1504. delete cmh;
  1505. delete przyciski;
  1506. delete btnspos;
  1507. }
  1508. void CPreGame::hideBox ()
  1509. {
  1510. SDL_BlitSurface(behindCurMes,NULL,ekran,currentMessage);
  1511. SDL_UpdateRect
  1512. (ekran,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  1513. for (int i=0;i<btns.size();i++)
  1514. {
  1515. if (btns[i]->ID==2)
  1516. {
  1517. delete btns[i];
  1518. btns.erase(btns.begin()+i);
  1519. i--;
  1520. }
  1521. }
  1522. SDL_FreeSurface(behindCurMes);
  1523. delete currentMessage;
  1524. currentMessage = NULL;
  1525. behindCurMes=NULL;
  1526. }
  1527. CPreGame::menuItems * CPreGame::currentItems()
  1528. {
  1529. switch (state)
  1530. {
  1531. case EState::mainMenu:
  1532. return ourMainMenu;
  1533. case EState::newGame:
  1534. return ourNewMenu;
  1535. default:
  1536. return NULL;
  1537. }
  1538. }
  1539. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  1540. {
  1541. if(sEvent.type == SDL_MOUSEMOTION)
  1542. {
  1543. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y);
  1544. }
  1545. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1546. {
  1547. for (int i=0;i<btns.size(); i++)
  1548. {
  1549. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1550. {
  1551. btns[i]->press(true);
  1552. ourScenSel->pressed=(Button<>*)btns[i];
  1553. }
  1554. }
  1555. if (ourScenSel->listShowed && (sEvent.button.y>121) &&(sEvent.button.y<570)
  1556. && (sEvent.button.x>55) && (sEvent.button.x<372))
  1557. {
  1558. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  1559. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  1560. }
  1561. }
  1562. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1563. {
  1564. Button<> * prnr=ourScenSel->pressed;
  1565. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  1566. {
  1567. ourScenSel->pressed->press(false);
  1568. ourScenSel->pressed=NULL;
  1569. }
  1570. for (int i=0;i<btns.size(); i++)
  1571. {
  1572. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1573. {
  1574. if (btns[i]->selectable)
  1575. btns[i]->select(true);
  1576. if (btns[i]->type==1 && ((Button<>*)btns[i])->fun)
  1577. (this->*(((Button<>*)btns[i])->fun))();
  1578. int zz = btns.size();
  1579. if (i>=zz)
  1580. break;
  1581. if (btns[i]==prnr && btns[i]->type==2)
  1582. {
  1583. ((IntBut<> *)(btns[i]))->set();
  1584. ourScenSel->mapsel.slid->whereAreWe=0;
  1585. ourScenSel->mapsel.slid->updateSlid();
  1586. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  1587. ourScenSel->mapsel.printMaps(0);
  1588. }
  1589. }
  1590. }
  1591. }
  1592. else if (sEvent.type==SDL_MOUSEMOTION)
  1593. {
  1594. if (highlighted)
  1595. {
  1596. if (isItIn(&highlighted->pos,sEvent.motion.x,sEvent.motion.y))
  1597. return;
  1598. else
  1599. {
  1600. highlighted->hover(false);
  1601. highlighted = NULL;
  1602. }
  1603. }
  1604. for (int i=0;i<btns.size();i++)
  1605. {
  1606. if (!btns[i]->highlightable)
  1607. continue;
  1608. if(isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1609. {
  1610. btns[i]->hover(true);
  1611. highlighted=btns[i];
  1612. return;
  1613. }
  1614. else if (btns[i]->highlighted)
  1615. btns[i]->hover(false);
  1616. }
  1617. }
  1618. }
  1619. StartInfo CPreGame::runLoop()
  1620. {
  1621. SDL_Event sEvent;
  1622. while(run)
  1623. {
  1624. CGI->screenh->updateScreen();
  1625. try
  1626. {
  1627. if(SDL_PollEvent(&sEvent)) //wait for event...
  1628. {
  1629. menuItems * current = currentItems();
  1630. if(sEvent.type==SDL_QUIT)
  1631. return ret;
  1632. for (int i=0;i<interested.size();i++)
  1633. interested[i]->handleIt(sEvent);
  1634. if (!current)
  1635. {
  1636. (this->*handleOther)(sEvent);
  1637. }
  1638. else if (sEvent.type==SDL_KEYDOWN)
  1639. {
  1640. if (sEvent.key.keysym.sym==SDLK_q)
  1641. {
  1642. return ret;
  1643. break;
  1644. }
  1645. /*if (state==EState::newGame)
  1646. {
  1647. switch (sEvent.key.keysym.sym)
  1648. {
  1649. case SDLK_LEFT:
  1650. {
  1651. if(currentItems()->lNewGame.x>0)
  1652. currentItems()->lNewGame.x--;
  1653. break;
  1654. }
  1655. case (SDLK_RIGHT):
  1656. {
  1657. currentItems()->lNewGame.x++;
  1658. break;
  1659. }
  1660. case (SDLK_UP):
  1661. {
  1662. if(currentItems()->lNewGame.y>0)
  1663. currentItems()->lNewGame.y--;
  1664. break;
  1665. }
  1666. case (SDLK_DOWN):
  1667. {
  1668. currentItems()->lNewGame.y++;
  1669. break;
  1670. }
  1671. }
  1672. showNewMenu();
  1673. }*/
  1674. }
  1675. else if (sEvent.type==SDL_MOUSEMOTION)
  1676. {
  1677. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y); //for graphical mouse
  1678. if (currentMessage) continue;
  1679. if (current->highlighted)
  1680. {
  1681. switch (current->highlighted)
  1682. {
  1683. case 1:
  1684. {
  1685. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1686. continue;
  1687. else
  1688. {
  1689. current->highlighted=0;
  1690. highlightButton(1,0);
  1691. }
  1692. break;
  1693. }
  1694. case 2:
  1695. {
  1696. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1697. continue;
  1698. else
  1699. {
  1700. current->highlighted=0;
  1701. highlightButton(2,0);
  1702. }
  1703. break;
  1704. }
  1705. case 3:
  1706. {
  1707. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1708. continue;
  1709. else
  1710. {
  1711. current->highlighted=0;
  1712. highlightButton(3,0);
  1713. }
  1714. break;
  1715. }
  1716. case 4:
  1717. {
  1718. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1719. continue;
  1720. else
  1721. {
  1722. current->highlighted=0;
  1723. highlightButton(4,0);
  1724. }
  1725. break;
  1726. }
  1727. case 5:
  1728. {
  1729. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1730. continue;
  1731. else
  1732. {
  1733. current->highlighted=0;
  1734. highlightButton(5,0);
  1735. }
  1736. break;
  1737. }
  1738. } //switch (current->highlighted)
  1739. } // if (current->highlighted)
  1740. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1741. {
  1742. highlightButton(1,2);
  1743. current->highlighted=1;
  1744. }
  1745. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1746. {
  1747. highlightButton(2,2);
  1748. current->highlighted=2;
  1749. }
  1750. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1751. {
  1752. highlightButton(3,2);
  1753. current->highlighted=3;
  1754. }
  1755. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1756. {
  1757. highlightButton(4,2);
  1758. current->highlighted=4;
  1759. }
  1760. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1761. {
  1762. highlightButton(5,2);
  1763. current->highlighted=5;
  1764. }
  1765. }
  1766. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1767. {
  1768. mush->playClick();
  1769. for (int i=0;i<btns.size(); i++)
  1770. {
  1771. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1772. {
  1773. btns[i]->press(true);
  1774. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,ekran,&btns[i].pos);
  1775. //updateRect(&btns[i].pos);
  1776. }
  1777. }
  1778. if (currentMessage) continue;
  1779. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1780. {
  1781. highlightButton(1,1);
  1782. current->highlighted=1;
  1783. }
  1784. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1785. {
  1786. highlightButton(2,1);
  1787. current->highlighted=2;
  1788. }
  1789. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1790. {
  1791. highlightButton(3,1);
  1792. current->highlighted=3;
  1793. }
  1794. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1795. {
  1796. highlightButton(4,1);
  1797. current->highlighted=4;
  1798. }
  1799. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1800. {
  1801. highlightButton(5,1);
  1802. current->highlighted=5;
  1803. }
  1804. }
  1805. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1806. {
  1807. for (int i=0;i<btns.size(); i++)
  1808. {
  1809. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1810. (this->*(((Button<>*)btns[i])->fun))();
  1811. else
  1812. {
  1813. btns[i]->press(false);
  1814. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,ekran,&btns[i].pos);
  1815. //updateRect(&btns[i].pos);
  1816. }
  1817. }
  1818. if (currentMessage) continue;
  1819. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1820. {
  1821. highlightButton(1,2);
  1822. current->highlighted=1;
  1823. (this->*(current->fNewGame))();
  1824. }
  1825. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1826. {
  1827. highlightButton(2,2);
  1828. current->highlighted=2;
  1829. }
  1830. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1831. {
  1832. highlightButton(3,2);
  1833. current->highlighted=3;
  1834. }
  1835. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1836. {
  1837. highlightButton(4,2);
  1838. current->highlighted=4;
  1839. }
  1840. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  1841. {
  1842. highlightButton(5,2);
  1843. current->highlighted=5;
  1844. (this->*(current->fQuit))();
  1845. }
  1846. }
  1847. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  1848. {
  1849. if (currentMessage) continue;
  1850. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  1851. {
  1852. showCenBox(buttonText(0));
  1853. }
  1854. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  1855. {
  1856. showCenBox(buttonText(1));
  1857. }
  1858. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  1859. {
  1860. showCenBox(buttonText(2));
  1861. }
  1862. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  1863. {
  1864. showCenBox(buttonText(3));
  1865. }
  1866. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  1867. {
  1868. showCenBox(buttonText(4));
  1869. }
  1870. }
  1871. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  1872. {
  1873. hideBox();
  1874. }
  1875. }
  1876. }
  1877. catch(...)
  1878. { continue; }
  1879. SDL_Delay(1); //give time for other apps
  1880. }
  1881. return ret;
  1882. }
  1883. std::string CPreGame::buttonText(int which)
  1884. {
  1885. if (state==EState::mainMenu)
  1886. {
  1887. switch (which)
  1888. {
  1889. case 0:
  1890. return preth->mainNewGame;
  1891. case 1:
  1892. return preth->mainLoadGame;
  1893. case 2:
  1894. return preth->mainHighScores;
  1895. case 3:
  1896. return preth->mainCredits;
  1897. case 4:
  1898. return preth->mainQuit;
  1899. }
  1900. }
  1901. else if (state==EState::newGame)
  1902. {
  1903. switch (which)
  1904. {
  1905. case 0:
  1906. return preth->ngSingleScenario;
  1907. case 1:
  1908. return preth->ngMultiplayer;
  1909. case 2:
  1910. return preth->ngCampain;
  1911. case 3:
  1912. return preth->ngTutorial;
  1913. case 4:
  1914. return preth->ngBack;
  1915. }
  1916. }
  1917. return std::string();
  1918. }
  1919. void CPreGame::quitAskBox()
  1920. {
  1921. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  1922. }
  1923. void CPreGame::sortMaps()
  1924. {
  1925. std::sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(ourScenSel->mapsel.sortBy));
  1926. ourScenSel->mapsel.select(0);
  1927. printMapsFrom(0);
  1928. }
  1929. void CPreGame::setTurnLength(int on)
  1930. {
  1931. int min;
  1932. switch (on)
  1933. {
  1934. case 0:
  1935. min=1;
  1936. break;
  1937. case 1:
  1938. min=2;
  1939. break;
  1940. case 2:
  1941. min=4;
  1942. break;
  1943. case 3:
  1944. min=6;
  1945. break;
  1946. case 4:
  1947. min=8;
  1948. break;
  1949. case 5:
  1950. min=10;
  1951. break;
  1952. case 6:
  1953. min=15;
  1954. break;
  1955. case 7:
  1956. min=20;
  1957. break;
  1958. case 8:
  1959. min=25;
  1960. break;
  1961. case 9:
  1962. min=30;
  1963. break;
  1964. case 10:
  1965. min=0;
  1966. break;
  1967. default:
  1968. min=0;
  1969. break;
  1970. }
  1971. SDL_BlitSurface(ourOptions->bg,&genRect(23,134,256,547),ekran,&genRect(23,134,258,553));
  1972. updateRect(&genRect(23,134,258,553));
  1973. if (min)
  1974. {
  1975. std::ostringstream os;
  1976. os<<min<<" Minutes";
  1977. CSDL_Ext::printAtMiddle(os.str(),323,563,GEOR13);
  1978. }
  1979. else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13);
  1980. }