CPreGame.cpp 75 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629
  1. #include "../stdafx.h"
  2. #include "CPreGame.h"
  3. #include "../hch/CDefHandler.h"
  4. #include <ctime>
  5. #include <SDL.h>
  6. #include <boost/filesystem.hpp> // includes all needed Boost.Filesystem declarations
  7. #include <boost/algorithm/string.hpp>
  8. //#include "boost/foreach.hpp"
  9. #include <zlib.h>
  10. #include "../timeHandler.h"
  11. #include <sstream>
  12. #include "SDL_Extensions.h"
  13. #include "CGameInfo.h"
  14. #include "../hch/CGeneralTextHandler.h"
  15. #include "CCursorHandler.h"
  16. #include "../hch/CLodHandler.h"
  17. #include "../hch/CTownHandler.h"
  18. #include "../hch/CHeroHandler.h"
  19. #include <cmath>
  20. #include "Graphics.h"
  21. #include <boost/thread.hpp>
  22. #include <boost/bind.hpp>
  23. #include <cstdlib>
  24. #include "../lib/Connection.h"
  25. #include "../hch/CMusicHandler.h"
  26. /*
  27. * CPreGame.cpp, part of VCMI engine
  28. *
  29. * Authors: listed in file AUTHORS in main folder
  30. *
  31. * License: GNU General Public License v2.0 or later
  32. * Full text of license available in license.txt file, in main folder
  33. *
  34. */
  35. extern SDL_Surface * screen;
  36. extern SDL_Color tytulowy, tlo, zwykly ;
  37. extern TTF_Font * TNRB16, *TNR, *GEOR13, *GEORXX, *GEORM;
  38. #ifdef min
  39. #undef min
  40. #endif
  41. #ifdef max
  42. #undef max
  43. #endif
  44. CPreGame * CPG;
  45. namespace fs = boost::filesystem;
  46. namespace s = CSDL_Ext;
  47. static int getNextCastle(int current, PlayerInfo * ourInf, bool next=true) //next=flase => previous castle
  48. {
  49. int dir = next ? 1 : -1;
  50. if (current==-2) //no castle - no change
  51. return current;
  52. else if (current==-1) //random => first/last available
  53. {
  54. int pom = (next) ? (0) : (F_NUMBER-1); // last or first
  55. for (;pom>=0 && pom<F_NUMBER;pom+=dir)
  56. {
  57. if (((int)pow((double)2,pom))&ourInf->allowedFactions)
  58. {
  59. current=pom;
  60. break;
  61. }
  62. else continue;
  63. }
  64. }
  65. else // next/previous available
  66. {
  67. for (;;)
  68. {
  69. current+=dir;
  70. if (((int)pow((double)2,(int)current))&ourInf->allowedFactions)
  71. {
  72. break;
  73. }
  74. if (current>=F_NUMBER || current<0)
  75. {
  76. double p1 = log((double)ourInf->allowedFactions)/log(2.0f)+0.000001f;
  77. double check = p1-((int)p1);
  78. if (check < 0.001)
  79. current=(int)p1;
  80. else
  81. current=-1;
  82. break;
  83. }
  84. }
  85. }
  86. return current;
  87. }
  88. HighButton::HighButton( SDL_Rect Pos, CDefHandler* Imgs, bool Sel, int id)
  89. {
  90. type=0;
  91. imgs=Imgs;
  92. selectable=Sel;
  93. selected=false;
  94. state=0;
  95. pos=Pos;
  96. ID=id;
  97. highlightable=false;
  98. freeimgs = false;
  99. };
  100. HighButton::HighButton()
  101. {
  102. freeimgs = true;
  103. state=0;
  104. }
  105. HighButton::~HighButton()
  106. {
  107. if(freeimgs)
  108. delete imgs;
  109. }
  110. void HighButton::show()
  111. {
  112. blitAt(imgs->ourImages[state].bitmap,pos.x,pos.y);
  113. updateRect(&pos);
  114. }
  115. void HighButton::hover(bool on)
  116. {
  117. if (!highlightable) return;
  118. int i;
  119. if (on)
  120. {
  121. state=i=3;
  122. highlighted=true;
  123. }
  124. else
  125. {
  126. state=i=0;
  127. highlighted=false;
  128. }
  129. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,screen,&pos);
  130. updateRect(&pos);
  131. }
  132. void HighButton::press(bool down)
  133. {
  134. int i;
  135. if (down) state=i=1;
  136. else state=i=0;
  137. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,screen,&pos);
  138. updateRect(&pos);
  139. }
  140. Button::Button( SDL_Rect Pos, boost::function<void()> Fun,CDefHandler* Imgs, bool Sel, CGroup* gr, int id)
  141. :HighButton(Pos,Imgs,Sel,id),ourGroup(gr),fun(Fun)
  142. {
  143. type=1;
  144. };
  145. Button::Button()
  146. {
  147. ourGroup=NULL;type=1;
  148. };
  149. SetrButton::SetrButton()
  150. {
  151. type=1;
  152. selectable=false;
  153. selected=false;
  154. state=0;
  155. highlightable=false;
  156. }
  157. void SetrButton::press(bool down)
  158. {
  159. if (!down && state==1)
  160. *poin=key;
  161. HighButton::press(down);
  162. }
  163. void Button::hover(bool on)
  164. {
  165. HighButton::hover(on);
  166. }
  167. void Button::select(bool on)
  168. {
  169. int i;
  170. if (on) state=i=3;
  171. else state=i=0;
  172. SDL_BlitSurface(imgs->ourImages[i].bitmap,NULL,screen,&pos);
  173. updateRect(&pos);
  174. if (ourGroup && on && ourGroup->type==1)
  175. {
  176. if (ourGroup->selected && ourGroup->selected!=this)
  177. ourGroup->selected->select(false);
  178. ourGroup->selected =this;
  179. }
  180. }
  181. void Slider::updateSlid()
  182. {
  183. float perc = -1.0f;
  184. if(positionsAmnt > capacity)
  185. perc = ((float)whereAreWe)/((float)positionsAmnt-capacity);
  186. else
  187. perc = 0;
  188. float myh;
  189. if (vertical)
  190. {
  191. myh=perc*((float)pos.h-48)+pos.y+16;
  192. SDL_FillRect(screen,&genRect(pos.h-32,pos.w,pos.x,pos.y+16),0);
  193. blitAt(slider->imgs->ourImages[0].bitmap,pos.x,(int)myh);
  194. slider->pos.y=(int)myh;
  195. }
  196. else
  197. {
  198. myh=perc*((float)pos.w-48)+pos.x+16;
  199. SDL_FillRect(screen,&genRect(pos.h,pos.w-32,pos.x+16,pos.y),0);
  200. blitAt(slider->imgs->ourImages[0].bitmap,(int)myh,pos.y);
  201. slider->pos.x=(int)myh;
  202. }
  203. updateRect(&pos);
  204. }
  205. void Slider::moveDown()
  206. {
  207. if (whereAreWe<positionsAmnt-capacity)
  208. fun(++whereAreWe);
  209. updateSlid();
  210. }
  211. void Slider::moveUp()
  212. {
  213. if (whereAreWe>0)
  214. fun(--whereAreWe);
  215. updateSlid();
  216. }
  217. Slider::Slider(int x, int y, int h, int amnt, int cap, bool ver)
  218. {
  219. vertical=ver;
  220. positionsAmnt = amnt;
  221. capacity = cap;
  222. if (ver)
  223. {
  224. pos = genRect(h,16,x,y);
  225. down = new Button(genRect(16,16,x,y+h-16),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBDN.DEF"),false);
  226. up = new Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBUP.DEF"),false);
  227. slider = new Button(genRect(16,16,x,y+16),boost::function<void()>(),CDefHandler::giveDef("SCNRBSL.DEF"),false);
  228. }
  229. else
  230. {
  231. pos = genRect(16,h,x,y);
  232. down = new Button(genRect(16,16,x+h-16,y),boost::bind(&Slider::moveDown,this),CDefHandler::giveDef("SCNRBRT.DEF"),false);
  233. up = new Button(genRect(16,16,x,y),boost::bind(&Slider::moveUp,this),CDefHandler::giveDef("SCNRBLF.DEF"),false);
  234. slider = new Button(genRect(16,16,x+16,y),boost::function<void()>(),CDefHandler::giveDef("SCNRBSL.DEF"),false);
  235. }
  236. moving = false;
  237. whereAreWe=0;
  238. }
  239. void Slider::deactivate()
  240. {
  241. CPG->interested.erase(std::find(CPG->interested.begin(),CPG->interested.end(),this));
  242. }
  243. void Slider::activate()
  244. {
  245. SDL_FillRect(screen,&pos,0);
  246. up->show();
  247. down->show();
  248. slider->show();
  249. //SDL_Flip(screen);
  250. CSDL_Ext::update(screen);
  251. CPG->interested.push_back(this);
  252. }
  253. void Slider::handleIt(SDL_Event sEvent)
  254. {
  255. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  256. {
  257. if (isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y))
  258. {
  259. down->press();
  260. }
  261. else if (isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y))
  262. {
  263. up->press();
  264. }
  265. else if (isItIn(&slider->pos,sEvent.motion.x,sEvent.motion.y))
  266. {
  267. //slider->press();
  268. moving=true;
  269. }
  270. else if (isItIn(&pos,sEvent.motion.x,sEvent.motion.y))
  271. {
  272. float dy;
  273. float pe;
  274. if (vertical)
  275. {
  276. dy = sEvent.motion.y-pos.y-16;
  277. pe = dy/((float)(pos.h-32));
  278. if (pe>1) pe=1;
  279. if (pe<0) pe=0;
  280. }
  281. else
  282. {
  283. dy = sEvent.motion.x-pos.x-16;
  284. pe = dy/((float)(pos.w-32));
  285. if (pe>1) pe=1;
  286. if (pe<0) pe=0;
  287. }
  288. whereAreWe = pe*(positionsAmnt-capacity);
  289. if (whereAreWe<0)whereAreWe=0;
  290. updateSlid();
  291. fun(whereAreWe);
  292. }
  293. }
  294. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  295. {
  296. if ((down->state==1) && isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y))
  297. {
  298. this->down->fun();
  299. }
  300. if ((up->state==1) && isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y))
  301. {
  302. this->up->fun();
  303. }
  304. if (down->state==1) down->press(false);
  305. if (up->state==1) up->press(false);
  306. if (moving)
  307. {
  308. //slider->press();
  309. moving=false;
  310. }
  311. }
  312. else if (sEvent.type==SDL_KEYDOWN)
  313. {
  314. switch (sEvent.key.keysym.sym)
  315. {
  316. case (SDLK_UP):
  317. CPG->ourScenSel->mapsel.moveByX(-1);
  318. break;
  319. case (SDLK_DOWN):
  320. CPG->ourScenSel->mapsel.moveByX(+1);
  321. break;
  322. case SDLK_PAGEUP:
  323. CPG->ourScenSel->mapsel.moveByX(-CPG->ourScenSel->mapsel.slid->capacity);
  324. break;
  325. case SDLK_PAGEDOWN:
  326. CPG->ourScenSel->mapsel.moveByX(+CPG->ourScenSel->mapsel.slid->capacity);
  327. break;
  328. case SDLK_HOME:
  329. CPG->ourScenSel->mapsel.moveByX(-CPG->ourScenSel->mapsel.slid->positionsAmnt);
  330. break;
  331. case SDLK_END:
  332. CPG->ourScenSel->mapsel.moveByX(+CPG->ourScenSel->mapsel.slid->positionsAmnt);
  333. break;
  334. default:
  335. //TODO do something nasty here like logs entry..
  336. break;
  337. }
  338. }
  339. else if (moving && sEvent.type==SDL_MOUSEMOTION)
  340. {
  341. if (isItIn(&genRect(pos.h,pos.w+64,pos.x-32,pos.y),sEvent.motion.x,sEvent.motion.y))
  342. {
  343. int my;
  344. int all;
  345. float ile;
  346. if (vertical)
  347. {
  348. my = sEvent.motion.y-(pos.y+16);
  349. all =pos.h-48;
  350. ile = (float)my / (float)all;
  351. if (ile>1) ile=1;
  352. if (ile<0) ile=0;
  353. }
  354. else
  355. {
  356. my = sEvent.motion.x-(pos.x+16);
  357. all =pos.w-48;
  358. ile = (float)my / (float)all;
  359. if (ile>1) ile=1;
  360. if (ile<0) ile=0;
  361. }
  362. int ktory = ile*(positionsAmnt-capacity);
  363. if (ktory!=whereAreWe)
  364. {
  365. whereAreWe=ktory;
  366. updateSlid();
  367. fun(whereAreWe);
  368. }
  369. }
  370. }
  371. /*else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  372. {
  373. if (ourScenSel->pressed)
  374. {
  375. ourScenSel->pressed->press(false);
  376. ourScenSel->pressed=NULL;
  377. }
  378. for (int i=0;i<btns.size(); i++)
  379. {
  380. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  381. {
  382. if (btns[i]->selectable)
  383. btns[i]->select(true);
  384. if (btns[i]->fun)
  385. (this->*(btns[i]->fun))();
  386. return;
  387. }
  388. }
  389. if (isItIn(&down->pos,sEvent.motion.x,sEvent.motion.y))
  390. {
  391. (this->*down->fun)();
  392. }
  393. if (isItIn(&up->pos,sEvent.motion.x,sEvent.motion.y))
  394. {
  395. (this->*up->fun)();
  396. }
  397. if (isItIn(&slider->pos,sEvent.motion.x,sEvent.motion.y))
  398. {
  399. (this->*slider->fun)();
  400. }
  401. }*/
  402. }
  403. Slider::~Slider()
  404. {
  405. delete up;
  406. delete down;
  407. delete slider;
  408. }
  409. IntBut::IntBut()
  410. {
  411. type=2;
  412. fun=NULL;
  413. highlightable=false;
  414. }
  415. void IntBut::set()
  416. {
  417. *what=key;
  418. }
  419. void CPoinGroup::setYour(IntSelBut * your)
  420. {
  421. *gdzie=your->key;
  422. };
  423. IntSelBut::IntSelBut( SDL_Rect Pos, boost::function<void()> Fun,CDefHandler* Imgs, bool Sel, CPoinGroup* gr, int My)
  424. : Button(Pos,Fun,Imgs,Sel,gr),key(My)
  425. {
  426. ourPoinGroup=gr;
  427. };
  428. void IntSelBut::select(bool on)
  429. {
  430. Button::select(on);
  431. ourPoinGroup->setYour(this);
  432. CPG->printRating();
  433. }
  434. /********************************************************************************************/
  435. void PreGameTab::show()
  436. {
  437. if (CPG->currentTab)
  438. CPG->currentTab->hide();
  439. showed=true;
  440. CPG->currentTab=this;
  441. }
  442. void PreGameTab::hide()
  443. {
  444. showed=false;
  445. CPG->currentTab=NULL;
  446. }
  447. PreGameTab::PreGameTab()
  448. {
  449. showed=false;
  450. }
  451. /********************************************************************************************/
  452. Options::PlayerOptions::PlayerOptions(int serial, int player)
  453. :flag(genRect(50,42,14,130+serial*50),CPG->ourOptions->flags[player],player),
  454. Cleft(genRect(24,11,164,133+serial*50),CPG->ourOptions->left,true,-1), //left castle arrow
  455. Cright(genRect(24,11,225,133+serial*50),CPG->ourOptions->right,false,-1), //right castle arrow
  456. Hleft(genRect(24,11,240,133+serial*50),CPG->ourOptions->left,true,0), //left hero arrow
  457. Hright(genRect(24,11,301,133+serial*50),CPG->ourOptions->right,false,0), //right hero arrow
  458. Bleft(genRect(24,11,316,133+serial*50),CPG->ourOptions->left,true,1), //left bonus arrow
  459. Bright(genRect(24,11,377,133+serial*50),CPG->ourOptions->right,false,1) //right bonus arrow
  460. {
  461. Bleft.playerID=Bright.playerID=Hleft.playerID=Hright.playerID=Cleft.playerID=Cright.playerID=player;
  462. Bleft.serialID=Bright.serialID=Hleft.serialID=Hright.serialID=Cleft.serialID=Cright.serialID=serial;
  463. }
  464. bool Options::canUseThisHero(int ID)
  465. {
  466. //TODO: check if hero is allowed on selected map
  467. for(int i=0;i<CPG->ret.playerInfos.size();i++)
  468. if(CPG->ret.playerInfos[i].hero==ID)
  469. return false;
  470. return (usedHeroes.find(ID) == usedHeroes.end());
  471. }
  472. int Options::nextAllowedHero(int min, int max, int incl, int dir) //incl 0 - wlacznie; incl 1 - wylacznie; min-max - zakres szukania
  473. {
  474. if(dir>0)
  475. {
  476. for(int i=min+incl; i<=max-incl; i++)
  477. {
  478. if(canUseThisHero(i))
  479. return i;
  480. }
  481. }
  482. else
  483. {
  484. for(int i=max-incl; i>=min+incl; i--)
  485. {
  486. if(canUseThisHero(i))
  487. return i;
  488. }
  489. }
  490. return -1;
  491. }
  492. void Options::OptionSwitch::press(bool down)
  493. {
  494. HighButton::press(down);
  495. PlayerSettings * ourOpt = &CPG->ret.playerInfos[serialID];
  496. PlayerInfo * ourInf = &CPG->ourScenSel->mapsel.selMaps[CPG->ourScenSel->mapsel.selected]->players[playerID];
  497. int dir = (left) ? (-1) : (1);
  498. if (down) return;
  499. switch (which) //which button is this?
  500. {
  501. case -1: //castle change
  502. {
  503. int nCas = getNextCastle(ourOpt->castle,ourInf,!left);
  504. if (nCas!=ourOpt->castle) //changed castle
  505. {
  506. ourOpt->castle = nCas;
  507. if(ourOpt->hero != -2)
  508. {
  509. ourOpt->hero=-1;
  510. CPG->ourOptions->showIcon(0,serialID,false);
  511. }
  512. if(nCas < 0 && ourOpt->bonus==bresource)
  513. {
  514. ourOpt->bonus = brandom;
  515. }
  516. CPG->ourOptions->showIcon(1,serialID,false);
  517. }
  518. break;
  519. }
  520. case 0: //hero change
  521. {
  522. if (ourOpt->castle<0
  523. || !ourOpt->human
  524. )
  525. {
  526. break;
  527. }
  528. if (ourOpt->hero==-2) //no hero - no change
  529. return;
  530. else if (ourOpt->hero==-1) //random => first/last available
  531. {
  532. int max = (ourOpt->castle*HEROES_PER_TYPE*2+15),
  533. min = (ourOpt->castle*HEROES_PER_TYPE*2);
  534. ourOpt->hero = CPG->ourOptions->nextAllowedHero(min,max,0,dir);
  535. }
  536. else
  537. {
  538. if(dir>0)
  539. ourOpt->hero = CPG->ourOptions->nextAllowedHero(ourOpt->hero,(ourOpt->castle*HEROES_PER_TYPE*2+16),1,dir);
  540. else
  541. ourOpt->hero = CPG->ourOptions->nextAllowedHero(ourOpt->castle*HEROES_PER_TYPE*2-1,ourOpt->hero,1,dir);
  542. }
  543. break;
  544. }
  545. case 1: //bonus change
  546. {
  547. ourOpt->bonus += dir;
  548. if (ourOpt->hero==-2 && !ourInf->heroesNames.size() && ourOpt->bonus==bartifact) //no hero - can't be artifact
  549. {
  550. if (dir<0)
  551. ourOpt->bonus=brandom;
  552. else ourOpt->bonus=bgold;
  553. }
  554. if(ourOpt->bonus > bresource)
  555. ourOpt->bonus = brandom;
  556. if(ourOpt->bonus < brandom)
  557. ourOpt->bonus = bresource;
  558. if (ourOpt->castle==-1 && ourOpt->bonus==bresource) //random castle - can't be resource
  559. {
  560. if (dir<0)
  561. ourOpt->bonus=bgold;
  562. else ourOpt->bonus=brandom;
  563. }
  564. break;
  565. }
  566. }
  567. CPG->ourOptions->showIcon(which,serialID,false);
  568. }
  569. void Options::PlayerFlag::press(bool down)
  570. {
  571. HighButton::press(down);
  572. size_t i=0;
  573. for(;i<CPG->ret.playerInfos.size();i++)
  574. if(CPG->ret.playerInfos[i].color==color)
  575. break;
  576. if (CPG->ret.playerInfos[i].human || (!CPG->ourScenSel->mapsel.selMaps[CPG->ourScenSel->mapsel.selected]->players[CPG->ret.playerInfos[i].color].canHumanPlay))
  577. return; //if this is already human player, or if human is forbidden
  578. size_t j=0;
  579. for(;j<CPG->ret.playerInfos.size();j++)
  580. if(CPG->ret.playerInfos[j].human)
  581. break;
  582. CPG->ret.playerInfos[i].human = true;
  583. CPG->ret.playerInfos[j].human = false;
  584. if(CPG->ret.playerInfos[j].hero >= 0)
  585. {
  586. CPG->ret.playerInfos[j].hero = -1;
  587. CPG->ourOptions->showIcon(0,j,false);
  588. }
  589. std::string pom = CPG->ret.playerInfos[i].name;
  590. CPG->ret.playerInfos[i].name = CPG->ret.playerInfos[j].name;
  591. CPG->ret.playerInfos[j].name = pom;
  592. SDL_BlitSurface(CPG->ourOptions->bgs[CPG->ret.playerInfos[i].color],&genRect(19,99,5,1),screen,&genRect(19,99,62,129+i*50));
  593. SDL_UpdateRect(screen,62,129+50*i,99,19);
  594. SDL_BlitSurface(CPG->ourOptions->bgs[CPG->ret.playerInfos[j].color],&genRect(19,99,5,1),screen,&genRect(19,99,62,129+j*50));
  595. SDL_UpdateRect(screen,62,129+50*j,99,19);
  596. CSDL_Ext::printAtMiddle(CPG->ret.playerInfos[i].name,111,137+i*50,GEOR13,zwykly);
  597. CSDL_Ext::printAtMiddle(CPG->ret.playerInfos[j].name,111,137+j*50,GEOR13,zwykly);
  598. CPG->playerColor = CPG->ret.playerInfos[i].color;
  599. CPG->ourScenSel->mapsel.printFlags();
  600. };
  601. void Options::PlayerFlag::hover(bool on)
  602. {
  603. HighButton::hover(on);
  604. }
  605. void Options::showIcon (int what, int nr, bool abs) //what: -1=castle, 0=hero, 1=bonus, 2=all; abs=true -> nr is absolute
  606. {
  607. if (what==-2)
  608. {
  609. showIcon(-1,nr,abs);
  610. showIcon(0,nr,abs);
  611. showIcon(1,nr,abs);
  612. }
  613. int ab, se;
  614. if (!abs)
  615. {
  616. ab = CPG->ret.playerInfos[nr].color;
  617. se = nr;
  618. }
  619. else
  620. {
  621. ab = nr;
  622. for (size_t i=0; i<CPG->ret.playerInfos.size();++i)
  623. {
  624. if (CPG->ret.playerInfos[i].color==nr)
  625. {
  626. se=i;
  627. break;
  628. }
  629. }
  630. }
  631. PlayerSettings * ourOpt = &CPG->ret.playerInfos[se];
  632. switch (what)
  633. {
  634. case -1:
  635. {
  636. int pom=ourOpt->castle;
  637. if (ourOpt->castle<F_NUMBER && ourOpt->castle>=0)
  638. {
  639. blitAtWR(graphics->getPic(ourOpt->castle,true,false),176,130+50*se);
  640. }
  641. else if (ourOpt->castle==-1)
  642. {
  643. blitAtWR(CPG->ourOptions->rCastle,176,130+50*se);
  644. }
  645. else if (ourOpt->castle==-2)
  646. {
  647. blitAtWR(CPG->ourOptions->nCastle,176,130+50*se);
  648. }
  649. break;
  650. }
  651. case 0:
  652. {
  653. int pom=ourOpt->hero;
  654. if (ourOpt->hero==-1)
  655. {
  656. blitAtWR(CPG->ourOptions->rHero,252,130+50*se);
  657. }
  658. else if (ourOpt->hero==-2)
  659. {
  660. if(ourOpt->heroPortrait>=0)
  661. {
  662. blitAtWR(graphics->portraitSmall[ourOpt->heroPortrait],252,130+50*se);
  663. }
  664. else
  665. {
  666. blitAtWR(CPG->ourOptions->nHero,252,130+50*se);
  667. }
  668. }
  669. else
  670. {
  671. blitAtWR(graphics->portraitSmall[pom],252,130+50*se);
  672. }
  673. break;
  674. }
  675. case 1:
  676. {
  677. int pom;
  678. switch (ourOpt->bonus)
  679. {
  680. case -1:
  681. pom=10;
  682. break;
  683. case 0:
  684. pom=9;
  685. break;
  686. case 1:
  687. pom=8;
  688. break;
  689. case 2:
  690. pom=CGI->townh->towns[ourOpt->castle].bonus;
  691. break;
  692. }
  693. blitAtWR(bonuses->ourImages[pom].bitmap,328,130+50*se);
  694. break;
  695. }
  696. }
  697. }
  698. Options::~Options()
  699. {
  700. if (!inited) {
  701. return;
  702. }
  703. for (size_t i=0; i<bgs.size();i++) {
  704. SDL_FreeSurface(bgs[i]);
  705. }
  706. for (size_t i=0; i<flags.size();i++) {
  707. delete flags[i];
  708. }
  709. SDL_FreeSurface(bg);
  710. SDL_FreeSurface(rHero);
  711. SDL_FreeSurface(rCastle);
  712. SDL_FreeSurface(nHero);
  713. SDL_FreeSurface(nCastle);
  714. delete turnLength;
  715. delete left;
  716. delete right;
  717. delete bonuses;
  718. }
  719. void Options::init()
  720. {
  721. inited=true;
  722. bg = BitmapHandler::loadBitmap("ADVOPTBK.bmp");
  723. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  724. left = CDefHandler::giveDef("ADOPLFA.DEF");
  725. right = CDefHandler::giveDef("ADOPRTA.DEF");
  726. bonuses = CDefHandler::giveDef("SCNRSTAR.DEF");
  727. rHero = BitmapHandler::loadBitmap("HPSRAND1.bmp");
  728. rCastle = BitmapHandler::loadBitmap("HPSRAND0.bmp");
  729. nHero = BitmapHandler::loadBitmap("HPSRAND6.bmp");
  730. nCastle = BitmapHandler::loadBitmap("HPSRAND5.bmp");
  731. turnLength = new Slider(57,557,195,11,1,false);
  732. turnLength->fun=boost::bind(&CPreGame::setTurnLength,CPG,_1);
  733. flags.push_back(CDefHandler::giveDef("AOFLGBR.DEF"));
  734. flags.push_back(CDefHandler::giveDef("AOFLGBB.DEF"));
  735. flags.push_back(CDefHandler::giveDef("AOFLGBY.DEF"));
  736. flags.push_back(CDefHandler::giveDef("AOFLGBG.DEF"));
  737. flags.push_back(CDefHandler::giveDef("AOFLGBO.DEF"));
  738. flags.push_back(CDefHandler::giveDef("AOFLGBP.DEF"));
  739. flags.push_back(CDefHandler::giveDef("AOFLGBT.DEF"));
  740. flags.push_back(CDefHandler::giveDef("AOFLGBS.DEF"));
  741. bgs.push_back(BitmapHandler::loadBitmap("ADOPRPNL.bmp"));
  742. bgs.push_back(BitmapHandler::loadBitmap("ADOPBPNL.bmp"));
  743. bgs.push_back(BitmapHandler::loadBitmap("ADOPYPNL.bmp"));
  744. bgs.push_back(BitmapHandler::loadBitmap("ADOPGPNL.bmp"));
  745. bgs.push_back(BitmapHandler::loadBitmap("ADOPOPNL.bmp"));
  746. bgs.push_back(BitmapHandler::loadBitmap("ADOPPPNL.bmp"));
  747. bgs.push_back(BitmapHandler::loadBitmap("ADOPTPNL.bmp"));
  748. bgs.push_back(BitmapHandler::loadBitmap("ADOPSPNL.bmp"));
  749. }
  750. void Options::show()
  751. {
  752. if (showed)return;
  753. PreGameTab::show();
  754. MapSel & ms = CPG->ourScenSel->mapsel;
  755. blitAt(bg,3,6);
  756. CPG->ourScenSel->listShowed=false;
  757. for (size_t i=0; i < CPG->btns.size(); ++i)
  758. {
  759. if (CPG->btns[i]->ID!=10) //leave only right panel buttons
  760. {
  761. CPG->btns.erase(CPG->btns.begin()+i);
  762. i--;
  763. }
  764. }
  765. CPG->interested.clear();
  766. CSDL_Ext::printAtMiddle("Advanced Options",225,35,GEORXX);
  767. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[521],224,544,GEOR13); // Player Turn Duration
  768. int playersSoFar=0;
  769. for (size_t i=0; i < PLAYER_LIMIT; ++i)
  770. {
  771. if (!(ms.selMaps[ms.selected]->players[i].canComputerPlay || ms.selMaps[ms.selected]->players[i].canComputerPlay))
  772. continue;
  773. for (size_t hi=0; hi<ms.selMaps[ms.selected]->players[i].heroesNames.size(); hi++) {
  774. usedHeroes.insert(ms.selMaps[ms.selected]->players[i].heroesNames[hi].heroID);
  775. }
  776. blitAt(bgs[i],57,128+playersSoFar*50);
  777. poptions.push_back(new PlayerOptions(playersSoFar,i));
  778. poptions[poptions.size()-1]->nr=playersSoFar;
  779. poptions[poptions.size()-1]->color=(Ecolor)i;
  780. if(CPG->ret.playerInfos[playersSoFar].hero == -1)
  781. {
  782. poptions[poptions.size()-1]->Hleft.show();
  783. poptions[poptions.size()-1]->Hright.show();
  784. CPG->btns.push_back(&poptions[poptions.size()-1]->Hleft);
  785. CPG->btns.push_back(&poptions[poptions.size()-1]->Hright);
  786. }
  787. if(getNextCastle(CPG->ret.playerInfos[playersSoFar].castle,&ms.selMaps[ms.selected]->players[i]) != CPG->ret.playerInfos[playersSoFar].castle)
  788. {
  789. poptions[poptions.size()-1]->Cleft.show();
  790. poptions[poptions.size()-1]->Cright.show();
  791. CPG->btns.push_back(&poptions[poptions.size()-1]->Cleft);
  792. CPG->btns.push_back(&poptions[poptions.size()-1]->Cright);
  793. }
  794. poptions[poptions.size()-1]->Bleft.show();
  795. poptions[poptions.size()-1]->Bright.show();
  796. CPG->btns.push_back(&poptions[poptions.size()-1]->Bleft);
  797. CPG->btns.push_back(&poptions[poptions.size()-1]->Bright);
  798. CSDL_Ext::printAtMiddle(CPG->ret.playerInfos[playersSoFar].name,111,137+playersSoFar*50,GEOR13,zwykly);
  799. if (ms.selMaps[ms.selected]->players[i].canHumanPlay)
  800. {
  801. poptions[poptions.size()-1]->flag.show();
  802. CPG->btns.push_back(&poptions[poptions.size()-1]->flag);
  803. if (ms.selMaps[ms.selected]->players[i].canComputerPlay) {
  804. CSDL_Ext::printAtMiddleWB("Human or CPU",86,163+playersSoFar*50,GEORM,7,zwykly);
  805. }
  806. else {
  807. CSDL_Ext::printAtMiddleWB("Human",86,163+playersSoFar*50,GEORM,6,zwykly);
  808. }
  809. }
  810. else {
  811. CSDL_Ext::printAtMiddleWB("CPU",86,163+playersSoFar*50,GEORM,6,zwykly);
  812. }
  813. playersSoFar++;
  814. }
  815. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->allTexts[516],221,63,GEOR13,55,zwykly);
  816. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[256].second),109,109,GEOR13,14);
  817. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[259].second),201,109,GEOR13,10);
  818. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[260].second),275,109,GEOR13,10);
  819. CSDL_Ext::printAtMiddleWB(CGI->generaltexth->getTitle(CGI->generaltexth->zelp[261].second),354,109,GEOR13,10);
  820. turnLength->activate();
  821. for (size_t i=0; i < poptions.size(); ++i) {
  822. showIcon(-2,i,false);
  823. }
  824. for(int i=0;i<12;i++)
  825. turnLength->moveDown();
  826. //SDL_Flip(screen);
  827. CSDL_Ext::update(screen);
  828. }
  829. void Options::hide()
  830. {
  831. if (!showed) {
  832. return;
  833. }
  834. PreGameTab::hide();
  835. for (size_t i=0; i < CPG->btns.size(); ++i)
  836. if (CPG->btns[i]->ID==7)
  837. CPG->btns.erase(CPG->btns.begin()+i--);
  838. for (size_t i=0;i<poptions.size();i++) {
  839. delete poptions[i];
  840. }
  841. poptions.clear();
  842. turnLength->deactivate();
  843. }
  844. MapSel::~MapSel()
  845. {
  846. SDL_FreeSurface(bg);
  847. for (size_t i=0; i < scenImgs.size(); ++i)
  848. {
  849. SDL_FreeSurface(scenImgs[i]);
  850. }
  851. delete sFlags;
  852. }
  853. // Display the tab with the scenario names
  854. //
  855. // elemIdx is the index of the maps or saved game to display on line 0
  856. // slid->capacity contains the number of available screen lines
  857. // slid->positionsAmnt is the number of elements after filtering
  858. void MapSel::printMaps(int elemIdx)
  859. {
  860. // Display all elements if there's enough space
  861. if(slid->positionsAmnt < slid->capacity)
  862. elemIdx = 0;
  863. SDL_Surface * scenin = CSDL_Ext::newSurface(351,25);
  864. SDL_Color nasz;
  865. for (int line=0; line<slid->capacity; elemIdx++)
  866. {
  867. if (elemIdx >= selMaps.size()) {
  868. // No elements left to display, so it's an empty line.
  869. SDL_BlitSurface(bg, &genRect(25, 351, 22, 115+line*25), scenin, NULL);
  870. blitAt(scenin, 25, 121+line*25);
  871. line ++;
  872. continue;
  873. }
  874. CMapInfo* curMap = selMaps[elemIdx];
  875. // Build the line to display.
  876. if (elemIdx == selected)
  877. nasz=tytulowy;
  878. else
  879. nasz=zwykly;
  880. SDL_BlitSurface(bg,&genRect(25, 351, 22, line*25+115), scenin, NULL);
  881. std::ostringstream ostr(std::ostringstream::out);
  882. ostr << curMap->playerAmnt << "/" << curMap->humenPlayers;
  883. CSDL_Ext::printAt(ostr.str(),6,4,GEOR13,nasz,scenin, 2);
  884. std::string temp2;
  885. switch (curMap->width)
  886. {
  887. case 36:
  888. temp2="S";
  889. break;
  890. case 72:
  891. temp2="M";
  892. break;
  893. case 108:
  894. temp2="L";
  895. break;
  896. case 144:
  897. temp2="XL";
  898. break;
  899. default:
  900. temp2="C";
  901. break;
  902. }
  903. CSDL_Ext::printAtMiddle(temp2,50,13,GEOR13,nasz,scenin, 2);
  904. int temp=-1;
  905. switch (curMap->version)
  906. {
  907. case CMapHeader::RoE:
  908. temp=0;
  909. break;
  910. case CMapHeader::AB:
  911. temp=1;
  912. break;
  913. case CMapHeader::SoD:
  914. temp=2;
  915. break;
  916. case CMapHeader::WoG:
  917. temp=3;
  918. break;
  919. default:
  920. // Unknown version. Be safe and ignore that map
  921. tlog2 << "Warning: " << curMap->filename << " has wrong version!\n";
  922. continue;
  923. }
  924. blitAt(Dtypes->ourImages[temp].bitmap,67,2,scenin);
  925. if (CPG->fromMenu == CPG->newGame)
  926. {
  927. if (!(curMap->name.length()))
  928. curMap->name = "Unnamed";
  929. CSDL_Ext::printAtMiddle(curMap->name,192,13,GEOR13,nasz,scenin, 2);
  930. }
  931. else
  932. {
  933. std::string &name = curMap->filename;
  934. CSDL_Ext::printAtMiddle(name.substr(6,name.size()-12),192,13,GEOR13,nasz,scenin, 2);
  935. }
  936. if (curMap->victoryCondition.condition == winStandard)
  937. temp=11;
  938. else
  939. temp=curMap->victoryCondition.condition;
  940. blitAt(Dvic->ourImages[temp].bitmap,285,2,scenin);
  941. if (curMap->lossCondition.typeOfLossCon == lossStandard)
  942. temp=3;
  943. else
  944. temp=curMap->lossCondition.typeOfLossCon;
  945. blitAt(Dloss->ourImages[temp].bitmap,318,2,scenin);
  946. blitAt(scenin,25,121+line*25);
  947. line ++;
  948. }
  949. SDL_FreeSurface(scenin);
  950. SDL_UpdateRect(screen, 25, 121, 351, 19*25);
  951. }
  952. void MapSel::hide()
  953. {
  954. if (!showed)return;
  955. PreGameTab::hide();
  956. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&small));
  957. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&medium));
  958. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&large));
  959. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&xlarge));
  960. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&all));
  961. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&nrplayer));
  962. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&mapsize));
  963. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&type));
  964. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&name));
  965. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&viccon));
  966. CPG->btns.erase(std::find(CPG->btns.begin(),CPG->btns.end(),&loscon));
  967. slid->deactivate();
  968. CPG->currentTab = NULL;
  969. };
  970. void MapSel::show()
  971. {
  972. if (showed)return;
  973. PreGameTab::show();
  974. //blit bg
  975. blitAt(bg,3,6);
  976. CSDL_Ext::printAt("Map Sizes",55,60,GEOR13);
  977. CSDL_Ext::printAt(CGI->generaltexth->arraytxt[CPG->fromMenu==CPG->newGame ? 229 : 230],110,25,TNRB16); //Select a Scenario to Play : Load a Saved Game
  978. //size buttons
  979. small.show();
  980. medium.show();
  981. large.show();
  982. xlarge.show();
  983. all.show();
  984. CPG->btns.push_back(&small);
  985. CPG->btns.push_back(&medium);
  986. CPG->btns.push_back(&large);
  987. CPG->btns.push_back(&xlarge);
  988. CPG->btns.push_back(&all);
  989. //sort by buttons
  990. nrplayer.show();
  991. mapsize.show();
  992. type.show();
  993. name.show();
  994. viccon.show();
  995. loscon.show();
  996. CPG->btns.push_back(&nrplayer);
  997. CPG->btns.push_back(&mapsize);
  998. CPG->btns.push_back(&type);
  999. CPG->btns.push_back(&name);
  1000. CPG->btns.push_back(&viccon);
  1001. CPG->btns.push_back(&loscon);
  1002. //print scenario list
  1003. printMaps();
  1004. slid->whereAreWe = 0;
  1005. slid->activate();
  1006. //SDL_Flip(screen);
  1007. CSDL_Ext::update(screen);
  1008. }
  1009. void MapSel::processMaps(const std::vector<std::string> &pliczkiTemp, int start, int threads)
  1010. {
  1011. int read=0;
  1012. unsigned char sss[1000];
  1013. while(true)
  1014. {
  1015. if(start >= ourMaps.size())
  1016. break;
  1017. ourMaps[start] = NULL;
  1018. gzFile tempf = gzopen(pliczkiTemp[start].c_str(),"rb");
  1019. read = gzread(tempf, sss, 1000);
  1020. gzclose(tempf);
  1021. if(read < 50)
  1022. {
  1023. tlog3<<"\t\tWarning: corrupted map file: "<<pliczkiTemp[start]<<std::endl;
  1024. }
  1025. else if (sss[4]) //valid map
  1026. {
  1027. CMapInfo *mi = new CMapInfo(pliczkiTemp[start],sss);
  1028. ourMaps[start] = mi;
  1029. }
  1030. start += threads;
  1031. }
  1032. }
  1033. void MapSel::processGames(const std::vector<std::string> &pliczkiTemp)
  1034. {
  1035. ourGames.resize(pliczkiTemp.size());
  1036. ui32 hlp;
  1037. for(int i=0; i<pliczkiTemp.size(); i++)
  1038. {
  1039. CLoadFile lf(pliczkiTemp[i]);
  1040. ui8 sign[8];
  1041. lf >> sign >> hlp;
  1042. if(hlp != version)
  1043. {
  1044. tlog3 << "\t\t" << pliczkiTemp[i] << " seems to be too " << ((hlp>version) ? "new" : "old") << " and will be ommited.\n";
  1045. ourGames[i] = NULL;
  1046. continue;
  1047. }
  1048. ourGames[i] = new CMapInfo();
  1049. lf >> static_cast<CMapHeader&>(*ourGames[i]) >> ourGames[i]->seldiff;
  1050. ourGames[i]->filename = pliczkiTemp[i];
  1051. ourGames[i]->countPlayers();
  1052. }
  1053. }
  1054. static bool isNull(CMapInfo*mi)
  1055. {
  1056. return mi==NULL;
  1057. }
  1058. void MapSel::init()
  1059. {
  1060. //get map files names
  1061. std::vector<std::string> pliczkiTemp;
  1062. if(!boost::filesystem::exists("Maps"))
  1063. {
  1064. tlog1 << "Cannot find /Maps directory!\n";
  1065. }
  1066. fs::path tie( (fs::initial_path<fs::path>())/"Maps" );
  1067. fs::directory_iterator end_iter;
  1068. for ( fs::directory_iterator dir (tie); dir!=end_iter; ++dir )
  1069. {
  1070. if (fs::is_regular_file(dir->status()));
  1071. {
  1072. if (boost::ends_with(dir->path().filename(),".h3m"))
  1073. pliczkiTemp.push_back("Maps/"+(dir->path().leaf()));
  1074. }
  1075. }
  1076. ourMaps.resize(pliczkiTemp.size());
  1077. boost::thread_group group;
  1078. if(pliczkiTemp.size())
  1079. {
  1080. int threads = std::max((unsigned int)1,boost::thread::hardware_concurrency());
  1081. for(int ti=0;ti<threads;ti++)
  1082. group.create_thread(boost::bind(&MapSel::processMaps,this,boost::ref(pliczkiTemp),ti,threads));
  1083. }
  1084. else
  1085. {
  1086. tlog1 << "No maps in the /Maps directory!\n";
  1087. }
  1088. bg = BitmapHandler::loadBitmap("SCSELBCK.bmp");
  1089. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1090. small.imgs = CDefHandler::giveDef("SCSMBUT.DEF");
  1091. small.fun = NULL;
  1092. small.pos = genRect(small.imgs->ourImages[0].bitmap->h,small.imgs->ourImages[0].bitmap->w,161,52);
  1093. small.ourGroup=NULL;
  1094. medium.imgs = CDefHandler::giveDef("SCMDBUT.DEF");
  1095. medium.fun = NULL;
  1096. medium.pos = genRect(medium.imgs->ourImages[0].bitmap->h,medium.imgs->ourImages[0].bitmap->w,208,52);
  1097. medium.ourGroup=NULL;
  1098. large.imgs = CDefHandler::giveDef("SCLGBUT.DEF");
  1099. large.fun = NULL;
  1100. large.pos = genRect(large.imgs->ourImages[0].bitmap->h,large.imgs->ourImages[0].bitmap->w,255,52);
  1101. large.ourGroup=NULL;
  1102. xlarge.imgs = CDefHandler::giveDef("SCXLBUT.DEF");
  1103. xlarge.fun = NULL;
  1104. xlarge.pos = genRect(xlarge.imgs->ourImages[0].bitmap->h,xlarge.imgs->ourImages[0].bitmap->w,302,52);
  1105. xlarge.ourGroup=NULL;
  1106. all.imgs = CDefHandler::giveDef("SCALBUT.DEF");
  1107. all.fun = NULL;
  1108. all.pos = genRect(all.imgs->ourImages[0].bitmap->h,all.imgs->ourImages[0].bitmap->w,349,52);
  1109. all.ourGroup=NULL;
  1110. all.selectable=xlarge.selectable=large.selectable=medium.selectable=small.selectable=false;
  1111. small.what=medium.what=large.what=xlarge.what=all.what=&sizeFilter;
  1112. small.key=36;medium.key=72;large.key=108;xlarge.key=144;all.key=0;
  1113. //Button<> nrplayer, mapsize, type, name, viccon, loscon;
  1114. nrplayer.imgs = CDefHandler::giveDef("SCBUTT1.DEF");
  1115. nrplayer.fun = NULL;
  1116. nrplayer.pos = genRect(nrplayer.imgs->ourImages[0].bitmap->h,nrplayer.imgs->ourImages[0].bitmap->w,26,92);
  1117. nrplayer.key=_playerAm;
  1118. mapsize.imgs = CDefHandler::giveDef("SCBUTT2.DEF");
  1119. mapsize.fun = NULL;
  1120. mapsize.pos = genRect(mapsize.imgs->ourImages[0].bitmap->h,mapsize.imgs->ourImages[0].bitmap->w,58,92);
  1121. mapsize.key=_size;
  1122. type.imgs = CDefHandler::giveDef("SCBUTCP.DEF");
  1123. type.fun = NULL;
  1124. type.pos = genRect(type.imgs->ourImages[0].bitmap->h,type.imgs->ourImages[0].bitmap->w,91,92);
  1125. type.key=_format;
  1126. name.imgs = CDefHandler::giveDef("SCBUTT3.DEF");
  1127. name.fun = NULL;
  1128. name.pos = genRect(name.imgs->ourImages[0].bitmap->h,name.imgs->ourImages[0].bitmap->w,124,92);
  1129. name.key=_name;
  1130. viccon.imgs = CDefHandler::giveDef("SCBUTT4.DEF");
  1131. viccon.fun = NULL;
  1132. viccon.pos = genRect(viccon.imgs->ourImages[0].bitmap->h,viccon.imgs->ourImages[0].bitmap->w,309,92);
  1133. viccon.key=_viccon;
  1134. loscon.imgs = CDefHandler::giveDef("SCBUTT5.DEF");
  1135. loscon.fun = NULL;
  1136. loscon.pos = genRect(loscon.imgs->ourImages[0].bitmap->h,loscon.imgs->ourImages[0].bitmap->w,342,92);
  1137. loscon.key=_loscon;
  1138. nrplayer.poin=mapsize.poin=type.poin=name.poin=viccon.poin=loscon.poin=(int*)(&sortBy);
  1139. nrplayer.fun=mapsize.fun=type.fun=name.fun=viccon.fun=loscon.fun=boost::bind(&CPreGame::sortMaps,CPG);
  1140. Dtypes = CDefHandler::giveDef("SCSELC.DEF");
  1141. Dvic = CDefHandler::giveDef("SCNRVICT.DEF");
  1142. Dloss = CDefHandler::giveDef("SCNRLOSS.DEF");
  1143. //Dsizes = CPG->slh->giveDef("SCNRMPSZ.DEF");
  1144. Dsizes = CDefHandler::giveDef("SCNRMPSZ.DEF");
  1145. sFlags = CDefHandler::giveDef("ITGFLAGS.DEF");
  1146. slid = new Slider(375,92,480,ourMaps.size(),-1,true);
  1147. slid->fun = boost::bind(&CPreGame::printMapsFrom,CPG,_1);
  1148. group.join_all();
  1149. std::vector<CMapInfo*>::iterator maps = std::remove_if(ourMaps.begin(),ourMaps.end(),isNull);
  1150. ourMaps.erase(maps,ourMaps.end());
  1151. std::sort(ourMaps.begin(),ourMaps.end(),mapSorter(_name));
  1152. pliczkiTemp.clear();
  1153. std::vector<std::string> datestemp;
  1154. if(!boost::filesystem::exists("Games"))
  1155. {
  1156. tlog1 << "Cannot find /Games directory!\n";
  1157. }
  1158. tie = fs::path( (fs::initial_path<fs::path>())/"/Games" );
  1159. for ( fs::directory_iterator dir (tie); dir!=end_iter; ++dir )
  1160. {
  1161. if (fs::is_regular_file(dir->status()));
  1162. {
  1163. if (boost::ends_with(dir->path().filename(),".vlgm1"))
  1164. {
  1165. if( fs::file_size(dir->path()) < 16000 )
  1166. {
  1167. tlog3 << "\t\tWarning: savegame " << dir->path().filename() << " seems to be corrupted and will be ommited.\n";
  1168. continue;
  1169. }
  1170. pliczkiTemp.push_back("Games/"+(dir->path().leaf()));
  1171. std::time_t time = fs::last_write_time(dir->path());
  1172. datestemp.push_back(std::asctime(std::gmtime(&time)));
  1173. }
  1174. }
  1175. }
  1176. processGames(pliczkiTemp);
  1177. for (int i = 0; i < ourGames.size(); i++)
  1178. {
  1179. if(ourGames[i])
  1180. ourGames[i]->date = datestemp[i];
  1181. }
  1182. maps = std::remove_if(ourGames.begin(),ourGames.end(),isNull);
  1183. ourGames.erase(maps,ourGames.end());
  1184. std::sort(ourGames.begin(),ourGames.end(),mapSorter(_name));
  1185. }
  1186. // Move the list up or down by a specified amount (positive or negative).
  1187. void MapSel::moveByX(int nlines)
  1188. {
  1189. selected += nlines;
  1190. // Adjust selected
  1191. if (selected < 0)
  1192. selected = 0;
  1193. else if (selected >= selMaps.size())
  1194. selected = selMaps.size() - 1;
  1195. // Since we want the selection to appear, we may have to change which
  1196. // maps are to be displayed
  1197. if (selected < slid->whereAreWe)
  1198. slid->whereAreWe = selected;
  1199. else if (selected >= slid->whereAreWe + slid->capacity) {
  1200. slid->whereAreWe = selected - slid->capacity + 1;
  1201. if (slid->whereAreWe >= slid->positionsAmnt)
  1202. slid->whereAreWe = slid->positionsAmnt - 1;
  1203. }
  1204. select(selected);
  1205. slid->updateSlid();
  1206. }
  1207. void MapSel::select(int which, bool updateMapsList, bool forceSettingsUpdate)
  1208. {
  1209. if(which < 0 || which >= selMaps.size())
  1210. // Empty list
  1211. return;
  1212. // If there's currently no default selection, make one
  1213. if (selected == -1 && selMaps.size())
  1214. selected = 0;
  1215. bool dontSaveSettings = ((selected!=which) || (CPG->ret.playerInfos.size()==0) || forceSettingsUpdate);
  1216. if (selected >= 0)
  1217. {
  1218. selected = which;
  1219. CPG->ret.mapname = selMaps[selected]->filename;
  1220. }
  1221. if(updateMapsList)
  1222. printMaps(slid->whereAreWe);
  1223. int serialC=0;
  1224. if(selected >=0 && dontSaveSettings)
  1225. {
  1226. CPG->ret.playerInfos.clear();
  1227. bool wasntpl = true;
  1228. for (int i=0;i<PLAYER_LIMIT;i++)
  1229. {
  1230. if (!(selMaps[selected]->players[i].canComputerPlay
  1231. || selMaps[selected]->players[i].canComputerPlay)
  1232. )
  1233. 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)
  1234. PlayerSettings pset;
  1235. pset.color=(Ecolor)i;
  1236. pset.serial = serialC;
  1237. serialC++;
  1238. pset.bonus=brandom;
  1239. pset.castle=-2;
  1240. if (selMaps[which]->players[i].canHumanPlay && wasntpl)
  1241. {
  1242. pset.name=CGI->generaltexth->allTexts[434]; //Player
  1243. pset.human = true;
  1244. CPG->playerColor = i;
  1245. wasntpl = false;
  1246. }
  1247. else
  1248. {
  1249. pset.name=CGI->generaltexth->allTexts[468];//Computer
  1250. pset.human = false;
  1251. }
  1252. for (int j=0;j<F_NUMBER;j++)
  1253. {
  1254. if (((int)pow((double)2,j))&selMaps[selected]->players[i].allowedFactions)
  1255. {
  1256. if (pset.castle>=0)
  1257. pset.castle=-1;
  1258. if (pset.castle==-2)
  1259. pset.castle=j;
  1260. }
  1261. }
  1262. pset.heroPortrait=-1;
  1263. if (!
  1264. (((selMaps[which]->players[i].generateHeroAtMainTown || selMaps[which]->version==CMapHeader::RoE)
  1265. && selMaps[which]->players[i].hasMainTown)
  1266. || selMaps[which]->players[i].p8)
  1267. )
  1268. pset.hero=-2;
  1269. else
  1270. pset.hero=-1;
  1271. if(selMaps[which]->players[i].mainHeroName.length())
  1272. {
  1273. pset.heroName = selMaps[which]->players[i].mainHeroName;
  1274. if((pset.heroPortrait = selMaps[which]->players[i].mainHeroPortrait)==255)
  1275. pset.heroPortrait = selMaps[which]->players[i].p9;
  1276. }
  1277. pset.handicap=0;
  1278. CPG->ret.playerInfos.push_back(pset);
  1279. }
  1280. }
  1281. printSelectedInfo();
  1282. }
  1283. MapSel::MapSel():selected(-1),sizeFilter(0)
  1284. {
  1285. }
  1286. void MapSel::printSelectedInfo()
  1287. {
  1288. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),screen,&genRect(399,337,413,29));
  1289. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),screen,&genRect(50,91,414,453));
  1290. //blit texts
  1291. CSDL_Ext::printAt(CGI->generaltexth->zelp[21].second,420,25,GEOR13);
  1292. CSDL_Ext::printAt(CGI->generaltexth->allTexts[496],420,135,GEOR13);
  1293. CSDL_Ext::printAt(CGI->generaltexth->allTexts[497],420,285,GEOR13);
  1294. CSDL_Ext::printAt(CGI->generaltexth->allTexts[498],420,340,GEOR13);
  1295. CSDL_Ext::printAt(CGI->generaltexth->allTexts[390],420,406,GEOR13,zwykly);
  1296. CSDL_Ext::printAt(CGI->generaltexth->allTexts[391],585,406,GEOR13,zwykly);
  1297. if(CPG->fromMenu==CPG->newGame)
  1298. {
  1299. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bScens.pos);
  1300. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bOptions.pos);
  1301. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bRandom.pos);
  1302. }
  1303. if (selected >= 0)
  1304. {
  1305. CMapInfo &selMap = *selMaps[selected];
  1306. if(CPG->fromMenu != CPG->newGame)
  1307. {
  1308. CPG->ourScenSel->bEasy.state = 2 + (selMap.seldiff==0);
  1309. CPG->ourScenSel->bNormal.state = 2 + (selMap.seldiff==1);
  1310. CPG->ourScenSel->bHard.state = 2 + (selMap.seldiff==2);
  1311. CPG->ourScenSel->bExpert.state = 2 + (selMap.seldiff==3);
  1312. CPG->ourScenSel->bImpossible.state = 2 + (selMap.seldiff==4);
  1313. CPG->ourScenSel->bEasy.show();
  1314. CPG->ourScenSel->bNormal.show();
  1315. CPG->ourScenSel->bHard.show();
  1316. CPG->ourScenSel->bExpert.show();
  1317. CPG->ourScenSel->bImpossible.show();
  1318. }
  1319. int temp = selMap.victoryCondition.condition+1;
  1320. if (temp>20) temp=0;
  1321. std::string sss = CGI->generaltexth->victoryConditions[temp];
  1322. if (temp && selMap.victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  1323. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  1324. temp = selMap.lossCondition.typeOfLossCon+1;
  1325. if (temp>20) temp=0;
  1326. sss = CGI->generaltexth->lossCondtions[temp];
  1327. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  1328. //blit description
  1329. std::vector<std::string> desc = *CMessage::breakText(selMap.description,50);
  1330. for (int i=0;i<desc.size();i++)
  1331. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  1332. if ((selected < 0) || (selected >= selMaps.size()))
  1333. return;
  1334. if (selMap.name.length())
  1335. CSDL_Ext::printAt(selMap.name,420,41,GEORXX);
  1336. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  1337. std::string diff;
  1338. switch (selMap.difficulty)
  1339. {
  1340. case 0:
  1341. diff=gdiff(CGI->generaltexth->zelp[24].second);
  1342. break;
  1343. case 1:
  1344. diff=gdiff(CGI->generaltexth->zelp[25].second);
  1345. break;
  1346. case 2:
  1347. diff=gdiff(CGI->generaltexth->zelp[26].second);
  1348. break;
  1349. case 3:
  1350. diff=gdiff(CGI->generaltexth->zelp[27].second);
  1351. break;
  1352. case 4:
  1353. diff=gdiff(CGI->generaltexth->zelp[28].second);
  1354. break;
  1355. }
  1356. temp=-1;
  1357. switch (selMap.width)
  1358. {
  1359. case 36:
  1360. temp=0;
  1361. break;
  1362. case 72:
  1363. temp=1;
  1364. break;
  1365. case 108:
  1366. temp=2;
  1367. break;
  1368. case 144:
  1369. temp=3;
  1370. break;
  1371. default:
  1372. temp=4;
  1373. break;
  1374. }
  1375. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  1376. temp = selMap.victoryCondition.condition;
  1377. if (temp>12) temp=11;
  1378. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  1379. temp=selMap.lossCondition.typeOfLossCon;
  1380. if (temp>12) temp=3;
  1381. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  1382. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  1383. CSDL_Ext::printTo(selMap.date,704,40,GEOR13,zwykly);
  1384. printFlags();
  1385. }
  1386. //SDL_Flip(screen);
  1387. CSDL_Ext::update(screen);
  1388. }
  1389. void MapSel::printFlags()
  1390. {
  1391. CMapInfo &selMap = *selMaps[selected];
  1392. int hy=405, fx=460, ex=640, myT;
  1393. if (selMap.howManyTeams)
  1394. myT = selMap.players[CPG->playerColor].team;
  1395. else myT = -1;
  1396. for (int i=0;i<CPG->ret.playerInfos.size();i++)
  1397. {
  1398. if (myT>=0)
  1399. {
  1400. if(selMap.players[CPG->ret.playerInfos[i].color].team==myT)
  1401. {
  1402. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1403. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1404. }
  1405. else
  1406. {
  1407. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1408. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1409. }
  1410. }
  1411. else
  1412. {
  1413. if(CPG->ret.playerInfos[i].color==CPG->playerColor)
  1414. {
  1415. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1416. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1417. }
  1418. else
  1419. {
  1420. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1421. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1422. }
  1423. }
  1424. }
  1425. }
  1426. std::string MapSel::gdiff(std::string ss)
  1427. {
  1428. std::string ret;
  1429. for (int i=2;i<ss.length();i++)
  1430. {
  1431. if (ss[i]==' ')
  1432. break;
  1433. ret+=ss[i];
  1434. }
  1435. return ret;
  1436. }
  1437. // A new size filter (Small, Medium, ...) has been selected. Populate
  1438. // selMaps with the relevant data.
  1439. void MapSel::updateSelection()
  1440. {
  1441. std::vector<CMapInfo*> &maps = CPG->fromMenu==CPG->newGame ? ourMaps : ourGames;
  1442. selMaps.clear();
  1443. if (sizeFilter) {
  1444. // Filter in effect. Add only maps that matches.
  1445. for (int i=0; i<maps.size(); i++) {
  1446. if (maps[i]->width == sizeFilter) {
  1447. selMaps.push_back(maps[i]);
  1448. }
  1449. }
  1450. } else {
  1451. // No size filter. Copy all maps pointers.
  1452. selMaps = maps;
  1453. }
  1454. }
  1455. void CPreGame::printRating()
  1456. {
  1457. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(47,83,271,449),screen,&genRect(47,83,666,455));
  1458. updateRect(&genRect(47,83,666,455));
  1459. std::string tob;
  1460. switch (ourScenSel->selectedDiff)
  1461. {
  1462. case 0:
  1463. tob="80%";
  1464. break;
  1465. case 1:
  1466. tob="100%";
  1467. break;
  1468. case 2:
  1469. tob="130%";
  1470. break;
  1471. case 3:
  1472. tob="160%";
  1473. break;
  1474. case 4:
  1475. tob="200%";
  1476. break;
  1477. }
  1478. CSDL_Ext::printAtMiddle(tob,703,477,GEOR13,zwykly);
  1479. }
  1480. void CPreGame::printMapsFrom(int from)
  1481. {
  1482. ourScenSel->mapsel.printMaps(from);
  1483. }
  1484. void CPreGame::showScenList()
  1485. {
  1486. if (currentTab != &ourScenSel->mapsel)
  1487. {
  1488. ourScenSel->listShowed=true;
  1489. ourScenSel->mapsel.show();
  1490. }
  1491. else
  1492. {
  1493. currentTab->hide();
  1494. showScenSel();
  1495. }
  1496. }
  1497. CPreGame::CPreGame()
  1498. {
  1499. CPG=this;
  1500. highlighted=NULL;
  1501. currentTab=NULL;
  1502. run=true;
  1503. timeHandler tmh;tmh.getDif();
  1504. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  1505. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  1506. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  1507. currentMessage=NULL;
  1508. behindCurMes=NULL;
  1509. initMainMenu();
  1510. tlog0<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  1511. initNewMenu();
  1512. tlog0<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  1513. initLoadMenu();
  1514. tlog0<<"\tCPreGame: loadgame menu initialization: "<<tmh.getDif()<<std::endl;
  1515. initScenSel();
  1516. tlog0<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  1517. initOptions();
  1518. tlog0<<"\tCPreGame: scenario options initialization: "<<tmh.getDif()<<std::endl;
  1519. showMainMenu();
  1520. tlog0<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  1521. playerName="Player";
  1522. }
  1523. void CPreGame::initOptions()
  1524. {
  1525. ourOptions = new Options();
  1526. ourOptions->init();
  1527. }
  1528. void CPreGame::initScenSel()
  1529. {
  1530. ourScenSel = new ScenSel();
  1531. tlog5 << "\t\tLoaded graphics\n";
  1532. ourScenSel->mapsel.init();
  1533. tlog5 << "\t\tLoaded maps\n";
  1534. }
  1535. // New Game or Load Game seletion..
  1536. void CPreGame::showScenSel()
  1537. {
  1538. state=ScenarioList;
  1539. ourScenSel->mapsel.updateSelection();
  1540. ourScenSel->mapsel.slid->positionsAmnt = ourScenSel->mapsel.selMaps.size();
  1541. SDL_BlitSurface(ourScenSel->background,NULL,screen,NULL);
  1542. SDL_BlitSurface(ourScenSel->scenInf,NULL,screen,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,396,6));
  1543. CSDL_Ext::printAt(CGI->generaltexth->allTexts[494],427,438,GEOR13);//"Map Diff:"
  1544. CSDL_Ext::printAt(CGI->generaltexth->allTexts[492],527,438,GEOR13); //player difficulty
  1545. CSDL_Ext::printAt(CGI->generaltexth->allTexts[218],685,438,GEOR13);//"Rating:"
  1546. //blit buttons
  1547. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bEasy.pos);
  1548. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bNormal.pos);
  1549. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bHard.pos);
  1550. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bExpert.pos);
  1551. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bImpossible.pos);
  1552. SDL_BlitSurface((fromMenu==newGame ? ourScenSel->bBegin : ourScenSel->bLoad).imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBegin.pos);
  1553. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBack.pos);
  1554. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  1555. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  1556. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  1557. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  1558. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  1559. //add buttons info
  1560. if(first)
  1561. {
  1562. if(fromMenu==newGame)
  1563. {
  1564. btns.push_back(&ourScenSel->bEasy);
  1565. btns.push_back(&ourScenSel->bNormal);
  1566. btns.push_back(&ourScenSel->bHard);
  1567. btns.push_back(&ourScenSel->bExpert);
  1568. btns.push_back(&ourScenSel->bImpossible);
  1569. btns.push_back(&ourScenSel->bScens);
  1570. btns.push_back(&ourScenSel->bRandom);
  1571. btns.push_back(&ourScenSel->bOptions);
  1572. }
  1573. else
  1574. ourScenSel->mapsel.show();
  1575. btns.push_back(&(fromMenu==newGame ? ourScenSel->bBegin : ourScenSel->bLoad));
  1576. btns.push_back(&ourScenSel->bBack);
  1577. ourScenSel->selectedDiff=1;
  1578. ourScenSel->bNormal.select(true);
  1579. handleOther = &CPreGame::scenHandleEv;
  1580. ourScenSel->mapsel.select(0,false);
  1581. for (size_t i=0; i < btns.size(); ++i)
  1582. {
  1583. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  1584. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  1585. btns[i]->ID=10;
  1586. }
  1587. }
  1588. else
  1589. {
  1590. ourScenSel->mapsel.select(ourScenSel->mapsel.selected,false);
  1591. switch (ourScenSel->selectedDiff)
  1592. {
  1593. case 0:
  1594. ourScenSel->bEasy.select(true);
  1595. break;
  1596. case 1:
  1597. ourScenSel->bNormal.select(true);
  1598. break;
  1599. case 2:
  1600. ourScenSel->bHard.select(true);
  1601. break;
  1602. case 3:
  1603. ourScenSel->bExpert.select(true);
  1604. break;
  1605. case 4:
  1606. ourScenSel->bImpossible.select(true);
  1607. break;
  1608. }
  1609. }
  1610. //ourScenSel->mapsel.printSelectedInfo(); // this is already called in select()
  1611. //SDL_Flip(screen);
  1612. CSDL_Ext::update(screen);
  1613. first = false;
  1614. }
  1615. void CPreGame::showOptions()
  1616. {
  1617. if (ourScenSel->mapsel.selected == -1)
  1618. return;
  1619. if (currentTab != ourOptions)
  1620. ourOptions->show();
  1621. else
  1622. {
  1623. currentTab->hide();
  1624. showScenSel();
  1625. }
  1626. }
  1627. void CPreGame::initNewMenu()
  1628. {
  1629. ourNewMenu = new menuItems();
  1630. ourNewMenu->bgAd = BitmapHandler::loadBitmap("ZNEWGAM.bmp");
  1631. ourNewMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  1632. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  1633. //loading menu buttons
  1634. ourNewMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  1635. ourNewMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  1636. ourNewMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  1637. ourNewMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  1638. ourNewMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  1639. // single scenario
  1640. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  1641. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  1642. ourNewMenu->lNewGame.x=545;
  1643. ourNewMenu->lNewGame.y=4;
  1644. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  1645. //multiplayer
  1646. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  1647. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  1648. ourNewMenu->lLoadGame.x=568;
  1649. ourNewMenu->lLoadGame.y=120;
  1650. ourNewMenu->fLoadGame = NULL;
  1651. //campaign
  1652. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  1653. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  1654. ourNewMenu->lHighScores.x=541;
  1655. ourNewMenu->lHighScores.y=233;
  1656. ourNewMenu->fHighScores = NULL;
  1657. //tutorial
  1658. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  1659. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  1660. ourNewMenu->lCredits.x=545;
  1661. ourNewMenu->lCredits.y=358;
  1662. ourNewMenu->fCredits = NULL;
  1663. //back
  1664. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  1665. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  1666. ourNewMenu->lQuit.x=582;
  1667. ourNewMenu->lQuit.y=464;
  1668. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  1669. ourNewMenu->highlighted=0;
  1670. }
  1671. void CPreGame::showNewMenu()
  1672. {
  1673. if(state == ScenarioList && fromMenu==loadGame)
  1674. {
  1675. showLoadMenu();
  1676. return;
  1677. }
  1678. if (currentTab/*==&ourScenSel->mapsel*/)
  1679. currentTab->hide();
  1680. btns.clear();
  1681. interested.clear();
  1682. handleOther=NULL;
  1683. state = newGame;
  1684. fromMenu = newGame;
  1685. ourScenSel->mapsel.slid->capacity = 18;
  1686. SDL_BlitSurface(ourNewMenu->background,NULL,screen,NULL);
  1687. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lNewGame);
  1688. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lLoadGame);
  1689. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lHighScores);
  1690. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lCredits);
  1691. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lQuit);
  1692. //SDL_Flip(screen);
  1693. CSDL_Ext::update(screen);
  1694. first = true;
  1695. }
  1696. void CPreGame::initMainMenu()
  1697. {
  1698. ourMainMenu = new menuItems();
  1699. ourMainMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  1700. //loading menu buttons
  1701. ourMainMenu->newGame = CDefHandler::giveDef("ZMENUNG.DEF");
  1702. ourMainMenu->loadGame = CDefHandler::giveDef("ZMENULG.DEF");
  1703. ourMainMenu->highScores = CDefHandler::giveDef("ZMENUHS.DEF");
  1704. ourMainMenu->credits = CDefHandler::giveDef("ZMENUCR.DEF");
  1705. ourMainMenu->quit = CDefHandler::giveDef("ZMENUQT.DEF");
  1706. ok = CDefHandler::giveDef("IOKAY.DEF");
  1707. cancel = CDefHandler::giveDef("ICANCEL.DEF");
  1708. // new game button location
  1709. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  1710. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  1711. ourMainMenu->lNewGame.x=540;
  1712. ourMainMenu->lNewGame.y=10;
  1713. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  1714. //load game location
  1715. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  1716. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  1717. ourMainMenu->lLoadGame.x=532;
  1718. ourMainMenu->lLoadGame.y=132;
  1719. ourMainMenu->fLoadGame=&CPreGame::showLoadMenu;
  1720. //high scores
  1721. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  1722. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  1723. ourMainMenu->lHighScores.x=524;
  1724. ourMainMenu->lHighScores.y=251;
  1725. ourMainMenu->fHighScores = NULL;
  1726. //credits
  1727. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  1728. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  1729. ourMainMenu->lCredits.x=557;
  1730. ourMainMenu->lCredits.y=359;
  1731. ourMainMenu->fCredits = NULL;
  1732. //quit
  1733. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  1734. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  1735. ourMainMenu->lQuit.x=586;
  1736. ourMainMenu->lQuit.y=468;
  1737. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  1738. ourMainMenu->highlighted=0;
  1739. }
  1740. void CPreGame::showMainMenu()
  1741. {
  1742. state = mainMenu;
  1743. SDL_BlitSurface(ourMainMenu->background,NULL,screen,NULL);
  1744. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lNewGame);
  1745. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lLoadGame);
  1746. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lHighScores);
  1747. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lCredits);
  1748. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lQuit);
  1749. //SDL_Flip(screen);
  1750. CSDL_Ext::update(screen);
  1751. }
  1752. void CPreGame::highlightButton(int which, int on)
  1753. {
  1754. menuItems * current = currentItems();
  1755. switch (which)
  1756. {
  1757. case 1:
  1758. {
  1759. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,screen,&current->lNewGame);
  1760. break;
  1761. }
  1762. case 2:
  1763. {
  1764. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,screen,&current->lLoadGame);
  1765. break;
  1766. }
  1767. case 3:
  1768. {
  1769. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,screen,&current->lHighScores);
  1770. break;
  1771. }
  1772. case 4:
  1773. {
  1774. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,screen,&current->lCredits);
  1775. break;
  1776. }
  1777. case 5:
  1778. {
  1779. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,screen,&current->lQuit);
  1780. break;
  1781. }
  1782. }
  1783. //SDL_Flip(screen);
  1784. CSDL_Ext::update(screen);
  1785. }
  1786. void CPreGame::showCenBox (std::string data)
  1787. {
  1788. CMessage * cmh = new CMessage();
  1789. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data));
  1790. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1791. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1792. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1793. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1794. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1795. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1796. SDL_FreeSurface(infoBox);
  1797. currentMessage = new SDL_Rect(pos);
  1798. delete cmh;
  1799. }
  1800. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  1801. {
  1802. CMessage * cmh = new CMessage();
  1803. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  1804. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  1805. przyciski->push_back(ok);
  1806. przyciski->push_back(cancel);
  1807. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data), yesOrNO, przyciski, btnspos);
  1808. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1809. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1810. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1811. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1812. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1813. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1814. SDL_FreeSurface(infoBox);
  1815. currentMessage = new SDL_Rect(pos);
  1816. (*btnspos)[0].x+=pos.x;
  1817. (*btnspos)[0].y+=pos.y;
  1818. (*btnspos)[1].x+=pos.x;
  1819. (*btnspos)[1].y+=pos.y;
  1820. btns.push_back(new Button((*btnspos)[0],boost::bind(&CPreGame::quit,this),ok,false, NULL,2));
  1821. btns.push_back(new Button((*btnspos)[1],boost::bind(&CPreGame::hideBox,this),cancel,false, NULL,2));
  1822. delete cmh;
  1823. delete przyciski;
  1824. delete btnspos;
  1825. }
  1826. void CPreGame::hideBox ()
  1827. {
  1828. SDL_BlitSurface(behindCurMes,NULL,screen,currentMessage);
  1829. SDL_UpdateRect
  1830. (screen,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  1831. for (size_t i=0; i < btns.size(); ++i)
  1832. {
  1833. if (btns[i]->ID==2)
  1834. {
  1835. delete btns[i];
  1836. btns.erase(btns.begin()+i);
  1837. i--;
  1838. }
  1839. }
  1840. SDL_FreeSurface(behindCurMes);
  1841. delete currentMessage;
  1842. currentMessage = NULL;
  1843. behindCurMes=NULL;
  1844. }
  1845. CPreGame::menuItems * CPreGame::currentItems()
  1846. {
  1847. switch (state)
  1848. {
  1849. case mainMenu:
  1850. return ourMainMenu;
  1851. case newGame:
  1852. return ourNewMenu;
  1853. case loadGame:
  1854. return ourLoadMenu;
  1855. default:
  1856. return NULL;
  1857. }
  1858. }
  1859. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  1860. {
  1861. if(currentTab==&ourScenSel->mapsel)
  1862. {
  1863. if(sEvent.button.button == SDL_BUTTON_WHEELUP)
  1864. {
  1865. ourScenSel->mapsel.slid->moveUp();
  1866. return;
  1867. }
  1868. else if(sEvent.button.button == SDL_BUTTON_WHEELDOWN)
  1869. {
  1870. ourScenSel->mapsel.slid->moveDown();
  1871. return;
  1872. }
  1873. }
  1874. if(sEvent.type == SDL_MOUSEMOTION)
  1875. {
  1876. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y);
  1877. }
  1878. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1879. {
  1880. for (size_t i=0; i < btns.size(); ++i)
  1881. {
  1882. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1883. {
  1884. CGI->soundh->playSound(soundBase::button);
  1885. btns[i]->press(true);
  1886. ourScenSel->pressed=(Button*)btns[i];
  1887. }
  1888. }
  1889. if ((currentTab==&ourScenSel->mapsel) && (sEvent.button.y>121) &&(sEvent.button.y<570)
  1890. && (sEvent.button.x>55) && (sEvent.button.x<372))
  1891. {
  1892. // New selection. py is the index in selMaps.
  1893. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  1894. if (py < ourScenSel->mapsel.slid->positionsAmnt) {
  1895. CGI->soundh->playSound(soundBase::button);
  1896. ourScenSel->mapsel.select(py);
  1897. }
  1898. }
  1899. }
  1900. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1901. {
  1902. Button * prnr=ourScenSel->pressed;
  1903. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  1904. {
  1905. ourScenSel->pressed->press(false);
  1906. ourScenSel->pressed=NULL;
  1907. }
  1908. for (size_t i=0; i < btns.size(); ++i)
  1909. {
  1910. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1911. {
  1912. if (btns[i]->selectable)
  1913. btns[i]->select(true);
  1914. if (btns[i]->type==1 && ((Button*)btns[i])->fun)
  1915. ((Button*)btns[i])->fun();
  1916. int zz = btns.size();
  1917. if (i>=zz)
  1918. break;
  1919. if (btns[i]==prnr && btns[i]->type==2)
  1920. {
  1921. ((IntBut*)(btns[i]))->set();
  1922. ourScenSel->mapsel.updateSelection();
  1923. ourScenSel->mapsel.slid->whereAreWe=0;
  1924. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.selMaps.size();
  1925. ourScenSel->mapsel.slid->updateSlid();
  1926. ourScenSel->mapsel.select(0);
  1927. ourScenSel->mapsel.printMaps(0);
  1928. }
  1929. }
  1930. }
  1931. }
  1932. else if (sEvent.type==SDL_MOUSEMOTION)
  1933. {
  1934. if (highlighted)
  1935. {
  1936. if (isItIn(&highlighted->pos,sEvent.motion.x,sEvent.motion.y))
  1937. return;
  1938. else
  1939. {
  1940. highlighted->hover(false);
  1941. highlighted = NULL;
  1942. }
  1943. }
  1944. for (size_t i=0; i < btns.size(); ++i)
  1945. {
  1946. if (!btns[i]->highlightable)
  1947. continue;
  1948. if(isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1949. {
  1950. btns[i]->hover(true);
  1951. highlighted=btns[i];
  1952. return;
  1953. }
  1954. else if (btns[i]->highlighted)
  1955. btns[i]->hover(false);
  1956. }
  1957. }
  1958. }
  1959. StartInfo CPreGame::runLoop()
  1960. {
  1961. SDL_Event sEvent;
  1962. ret.turnTime = 0;
  1963. while(run)
  1964. {
  1965. try
  1966. {
  1967. while(SDL_PollEvent(&sEvent)) //handle all events
  1968. {
  1969. menuItems * current = currentItems();
  1970. if(sEvent.type==SDL_QUIT)
  1971. {
  1972. exit(EXIT_SUCCESS);
  1973. return ret;
  1974. }
  1975. for (size_t i=0; i < interested.size(); ++i) {
  1976. interested[i]->handleIt(sEvent);
  1977. }
  1978. if (!current)
  1979. {
  1980. (this->*handleOther)(sEvent);
  1981. }
  1982. else if (sEvent.type==SDL_KEYDOWN)
  1983. {
  1984. if (sEvent.key.keysym.sym==SDLK_q)
  1985. {
  1986. exit(EXIT_SUCCESS);
  1987. }
  1988. if(sEvent.key.keysym.sym==SDLK_F4 && (sEvent.key.keysym.mod & KMOD_LALT)) //Alt+F4
  1989. {
  1990. exit(EXIT_SUCCESS);
  1991. }
  1992. /*if (state==EState::newGame)
  1993. {
  1994. switch (sEvent.key.keysym.sym)
  1995. {
  1996. case SDLK_LEFT:
  1997. {
  1998. if(currentItems()->lNewGame.x>0)
  1999. currentItems()->lNewGame.x--;
  2000. break;
  2001. }
  2002. case (SDLK_RIGHT):
  2003. {
  2004. currentItems()->lNewGame.x++;
  2005. break;
  2006. }
  2007. case (SDLK_UP):
  2008. {
  2009. if(currentItems()->lNewGame.y>0)
  2010. currentItems()->lNewGame.y--;
  2011. break;
  2012. }
  2013. case (SDLK_DOWN):
  2014. {
  2015. currentItems()->lNewGame.y++;
  2016. break;
  2017. }
  2018. }
  2019. showNewMenu();
  2020. }*/
  2021. }
  2022. else if (sEvent.type==SDL_MOUSEMOTION)
  2023. {
  2024. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y); //for graphical mouse
  2025. if (currentMessage) continue;
  2026. if (current->highlighted)
  2027. {
  2028. switch (current->highlighted)
  2029. {
  2030. case 1:
  2031. {
  2032. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2033. continue;
  2034. else
  2035. {
  2036. current->highlighted=0;
  2037. highlightButton(1,0);
  2038. }
  2039. break;
  2040. }
  2041. case 2:
  2042. {
  2043. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2044. continue;
  2045. else
  2046. {
  2047. current->highlighted=0;
  2048. highlightButton(2,0);
  2049. }
  2050. break;
  2051. }
  2052. case 3:
  2053. {
  2054. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2055. continue;
  2056. else
  2057. {
  2058. current->highlighted=0;
  2059. highlightButton(3,0);
  2060. }
  2061. break;
  2062. }
  2063. case 4:
  2064. {
  2065. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2066. continue;
  2067. else
  2068. {
  2069. current->highlighted=0;
  2070. highlightButton(4,0);
  2071. }
  2072. break;
  2073. }
  2074. case 5:
  2075. {
  2076. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2077. continue;
  2078. else
  2079. {
  2080. current->highlighted=0;
  2081. highlightButton(5,0);
  2082. }
  2083. break;
  2084. }
  2085. } //switch (current->highlighted)
  2086. } // if (current->highlighted)
  2087. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2088. {
  2089. highlightButton(1,2);
  2090. current->highlighted=1;
  2091. }
  2092. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2093. {
  2094. highlightButton(2,2);
  2095. current->highlighted=2;
  2096. }
  2097. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2098. {
  2099. highlightButton(3,2);
  2100. current->highlighted=3;
  2101. }
  2102. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2103. {
  2104. highlightButton(4,2);
  2105. current->highlighted=4;
  2106. }
  2107. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2108. {
  2109. highlightButton(5,2);
  2110. current->highlighted=5;
  2111. }
  2112. }
  2113. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  2114. {
  2115. for (size_t i=0; i < btns.size(); ++i)
  2116. {
  2117. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  2118. {
  2119. btns[i]->press(true);
  2120. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,screen,&btns[i].pos);
  2121. //updateRect(&btns[i].pos);
  2122. }
  2123. }
  2124. if (currentMessage) continue;
  2125. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2126. {
  2127. highlightButton(1,1);
  2128. current->highlighted=1;
  2129. }
  2130. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2131. {
  2132. highlightButton(2,1);
  2133. current->highlighted=2;
  2134. }
  2135. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2136. {
  2137. highlightButton(3,1);
  2138. current->highlighted=3;
  2139. }
  2140. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2141. {
  2142. highlightButton(4,1);
  2143. current->highlighted=4;
  2144. }
  2145. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2146. {
  2147. highlightButton(5,1);
  2148. current->highlighted=5;
  2149. }
  2150. if (current->highlighted)
  2151. CGI->soundh->playSound(soundBase::button);
  2152. }
  2153. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  2154. {
  2155. for (size_t i=0; i < btns.size(); ++i)
  2156. {
  2157. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  2158. ((Button*)btns[i])->fun();
  2159. else
  2160. {
  2161. btns[i]->press(false);
  2162. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,screen,&btns[i].pos);
  2163. //updateRect(&btns[i].pos);
  2164. }
  2165. }
  2166. if (currentMessage) continue;
  2167. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2168. {
  2169. highlightButton(1,2);
  2170. current->highlighted=1;
  2171. if(current->fNewGame)
  2172. (this->*(current->fNewGame))();
  2173. }
  2174. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2175. {
  2176. highlightButton(2,2);
  2177. current->highlighted=2;
  2178. if(current->fLoadGame)
  2179. (this->*(current->fLoadGame))();
  2180. }
  2181. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2182. {
  2183. highlightButton(3,2);
  2184. current->highlighted=3;
  2185. }
  2186. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2187. {
  2188. highlightButton(4,2);
  2189. current->highlighted=4;
  2190. }
  2191. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2192. {
  2193. highlightButton(5,2);
  2194. current->highlighted=5;
  2195. if(current->fQuit)
  2196. (this->*(current->fQuit))();
  2197. }
  2198. }
  2199. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  2200. {
  2201. if (currentMessage) continue;
  2202. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2203. {
  2204. showCenBox(buttonText(0));
  2205. }
  2206. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2207. {
  2208. showCenBox(buttonText(1));
  2209. }
  2210. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2211. {
  2212. showCenBox(buttonText(2));
  2213. }
  2214. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2215. {
  2216. showCenBox(buttonText(3));
  2217. }
  2218. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  2219. {
  2220. showCenBox(buttonText(4));
  2221. }
  2222. }
  2223. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  2224. {
  2225. hideBox();
  2226. }
  2227. }
  2228. } HANDLE_EXCEPTION
  2229. CGI->curh->draw1();
  2230. SDL_Flip(screen);
  2231. CGI->curh->draw2();
  2232. SDL_Delay(20); //give time for other apps
  2233. }
  2234. ret.mode = (fromMenu==newGame) ? 0 : 1;
  2235. return ret;
  2236. }
  2237. std::string CPreGame::buttonText(int which)
  2238. {
  2239. if (state==mainMenu)
  2240. {
  2241. switch (which)
  2242. {
  2243. case 0:
  2244. return CGI->generaltexth->zelp[3].second;
  2245. case 1:
  2246. return CGI->generaltexth->zelp[4].second;
  2247. case 2:
  2248. return CGI->generaltexth->zelp[5].second;
  2249. case 3:
  2250. return CGI->generaltexth->zelp[6].second;
  2251. case 4:
  2252. return CGI->generaltexth->zelp[7].second;
  2253. }
  2254. }
  2255. else if (state==newGame || state==loadGame)
  2256. {
  2257. switch (which)
  2258. {
  2259. case 0:
  2260. return CGI->generaltexth->zelp[10].second;
  2261. case 1:
  2262. return CGI->generaltexth->zelp[11].second;
  2263. case 2:
  2264. return CGI->generaltexth->zelp[12].second;
  2265. case 3:
  2266. return CGI->generaltexth->zelp[13].second;
  2267. case 4:
  2268. return CGI->generaltexth->zelp[14].second;
  2269. }
  2270. }
  2271. return std::string();
  2272. }
  2273. void CPreGame::quitAskBox()
  2274. {
  2275. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  2276. }
  2277. // Sort the maps on the new key (map size, map name, ...)
  2278. void CPreGame::sortMaps()
  2279. {
  2280. std::vector<CMapInfo*> &sm = ourScenSel->mapsel.selMaps;
  2281. std::sort(sm.begin(), sm.end(), mapSorter(_name));
  2282. if (ourScenSel->mapsel.sortBy != _name)
  2283. std::stable_sort(sm.begin(), sm.end(),mapSorter(ourScenSel->mapsel.sortBy));
  2284. ourScenSel->mapsel.select(0,false,true);
  2285. ourScenSel->mapsel.slid->whereAreWe=0;
  2286. ourScenSel->mapsel.slid->updateSlid();
  2287. printMapsFrom(0);
  2288. }
  2289. void CPreGame::setTurnLength(int on)
  2290. {
  2291. int min;
  2292. switch (on)
  2293. {
  2294. case 0:
  2295. min=1;
  2296. break;
  2297. case 1:
  2298. min=2;
  2299. break;
  2300. case 2:
  2301. min=4;
  2302. break;
  2303. case 3:
  2304. min=6;
  2305. break;
  2306. case 4:
  2307. min=8;
  2308. break;
  2309. case 5:
  2310. min=10;
  2311. break;
  2312. case 6:
  2313. min=15;
  2314. break;
  2315. case 7:
  2316. min=20;
  2317. break;
  2318. case 8:
  2319. min=25;
  2320. break;
  2321. case 9:
  2322. min=30;
  2323. break;
  2324. case 10:
  2325. min=0;
  2326. break;
  2327. default:
  2328. min=0;
  2329. break;
  2330. }
  2331. SDL_BlitSurface(ourOptions->bg,&genRect(23,134,256,547),screen,&genRect(23,134,258,553));
  2332. updateRect(&genRect(23,134,258,553));
  2333. if (min)
  2334. {
  2335. std::ostringstream os;
  2336. os<<min<<" Minutes";
  2337. CSDL_Ext::printAtMiddle(os.str(),323,563,GEOR13);
  2338. }
  2339. else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13);
  2340. }
  2341. void CPreGame::showLoadMenu()
  2342. {
  2343. if (currentTab/*==&ourScenSel->mapsel*/)
  2344. currentTab->hide();
  2345. btns.clear();
  2346. interested.clear();
  2347. handleOther=NULL;
  2348. state = loadGame;
  2349. fromMenu = loadGame;
  2350. ourScenSel->mapsel.slid->capacity = 18;
  2351. SDL_BlitSurface(ourLoadMenu->background,NULL,screen,NULL);
  2352. SDL_BlitSurface(ourLoadMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lNewGame);
  2353. SDL_BlitSurface(ourLoadMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lLoadGame);
  2354. SDL_BlitSurface(ourLoadMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lHighScores);
  2355. SDL_BlitSurface(ourLoadMenu->credits->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lCredits);
  2356. SDL_BlitSurface(ourLoadMenu->quit->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lQuit);
  2357. //SDL_Flip(screen);
  2358. CSDL_Ext::update(screen);
  2359. first = true;
  2360. }
  2361. void CPreGame::initLoadMenu()
  2362. {
  2363. ourLoadMenu = new menuItems();
  2364. ourLoadMenu->bgAd = BitmapHandler::loadBitmap("ZLOADGAM.bmp");
  2365. ourLoadMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  2366. blitAt(ourLoadMenu->bgAd,114,312,ourLoadMenu->background);
  2367. //loading menu buttons
  2368. ourLoadMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  2369. ourLoadMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  2370. ourLoadMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  2371. ourLoadMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  2372. ourLoadMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  2373. // single scenario
  2374. ourLoadMenu->lNewGame.h=ourLoadMenu->newGame->ourImages[0].bitmap->h;
  2375. ourLoadMenu->lNewGame.w=ourLoadMenu->newGame->ourImages[0].bitmap->w;
  2376. ourLoadMenu->lNewGame.x=545;
  2377. ourLoadMenu->lNewGame.y=4;
  2378. ourLoadMenu->fNewGame=&CPreGame::showScenSel;
  2379. //multiplayer
  2380. ourLoadMenu->lLoadGame.h=ourLoadMenu->loadGame->ourImages[0].bitmap->h;
  2381. ourLoadMenu->lLoadGame.w=ourLoadMenu->loadGame->ourImages[0].bitmap->w;
  2382. ourLoadMenu->lLoadGame.x=568;
  2383. ourLoadMenu->lLoadGame.y=120;
  2384. ourLoadMenu->fLoadGame = NULL;
  2385. //campaign
  2386. ourLoadMenu->lHighScores.h=ourLoadMenu->highScores->ourImages[0].bitmap->h;
  2387. ourLoadMenu->lHighScores.w=ourLoadMenu->highScores->ourImages[0].bitmap->w;
  2388. ourLoadMenu->lHighScores.x=541;
  2389. ourLoadMenu->lHighScores.y=233;
  2390. ourLoadMenu->fHighScores = NULL;
  2391. //tutorial
  2392. ourLoadMenu->lCredits.h=ourLoadMenu->credits->ourImages[0].bitmap->h;
  2393. ourLoadMenu->lCredits.w=ourLoadMenu->credits->ourImages[0].bitmap->w;
  2394. ourLoadMenu->lCredits.x=545;
  2395. ourLoadMenu->lCredits.y=358;
  2396. ourLoadMenu->fCredits = NULL;
  2397. //back
  2398. ourLoadMenu->lQuit.h=ourLoadMenu->quit->ourImages[0].bitmap->h;
  2399. ourLoadMenu->lQuit.w=ourLoadMenu->quit->ourImages[0].bitmap->w;
  2400. ourLoadMenu->lQuit.x=582;
  2401. ourLoadMenu->lQuit.y=464;
  2402. ourLoadMenu->fQuit=&CPreGame::showMainMenu;
  2403. ourLoadMenu->highlighted=0;
  2404. }
  2405. CPreGame::~CPreGame()
  2406. {
  2407. delete ourMainMenu;
  2408. delete ourNewMenu;
  2409. delete ourLoadMenu;
  2410. delete ok;
  2411. delete cancel;
  2412. }
  2413. void CPreGame::begin()
  2414. {
  2415. if(!ret.mapname.size()) //empty mapname (savename) - do nothing (no map/game selected)
  2416. return;
  2417. run = false;
  2418. ret.difficulty=ourScenSel->selectedDiff;
  2419. }
  2420. CPreGame::menuItems::menuItems()
  2421. {
  2422. }
  2423. CPreGame::menuItems::~menuItems()
  2424. {
  2425. delete this->newGame;
  2426. delete this->loadGame;
  2427. delete this->highScores;
  2428. delete this->credits;
  2429. delete this->quit;
  2430. SDL_FreeSurface(bgAd);
  2431. SDL_FreeSurface(background);
  2432. }
  2433. ScenSel::ScenSel()
  2434. :
  2435. difficulty(new CPoinGroup()),
  2436. bScens(genRect(0,0,414,81),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2437. bOptions(genRect(0,0,414,509),boost::bind(&CPreGame::showOptions,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2438. bRandom(genRect(0,0,414,105),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2439. bBegin(genRect(0,0,414,535),boost::bind(&CPreGame::begin,CPG),CDefHandler::giveDef("SCNRBEG.DEF")),
  2440. bLoad(genRect(0,0,414,535),boost::bind(&CPreGame::begin,CPG),CDefHandler::giveDef("SCNRLOD.DEF")),
  2441. bBack(genRect(0,0,584,535),boost::bind(&CPreGame::showNewMenu,CPG),CDefHandler::giveDef("SCNRBACK.DEF")),
  2442. bEasy(genRect(0,0,506,456),NULL,CDefHandler::giveDef("GSPBUT3.DEF"),true,difficulty,0),
  2443. bNormal(genRect(0,0,538,456),NULL,CDefHandler::giveDef("GSPBUT4.DEF"),true,difficulty,1),
  2444. bHard(genRect(0,0,570,456),NULL,CDefHandler::giveDef("GSPBUT5.DEF"),true,difficulty,2),
  2445. bExpert(genRect(0,0,602,456),NULL,CDefHandler::giveDef("GSPBUT6.DEF"),true,difficulty,3),
  2446. bImpossible(genRect(0,0,634,456),NULL,CDefHandler::giveDef("GSPBUT7.DEF"),true,difficulty,4)
  2447. {
  2448. pressed=NULL;
  2449. listShowed=false;
  2450. if (rand()%2)
  2451. background = BitmapHandler::loadBitmap("ZPIC1000.bmp");
  2452. else
  2453. background = BitmapHandler::loadBitmap("ZPIC1001.bmp");
  2454. savenameStrip = BitmapHandler::loadBitmap("GSSTRIP.bmp");
  2455. scenInf = BitmapHandler::loadBitmap("GSELPOP1.bmp");
  2456. randMap = BitmapHandler::loadBitmap("RANMAPBK.bmp");
  2457. options = BitmapHandler::loadBitmap("ADVOPTBK.bmp");
  2458. SDL_SetColorKey(scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(scenInf->format,0,255,255));
  2459. SDL_SetColorKey(randMap,SDL_SRCCOLORKEY,SDL_MapRGB(randMap->format,0,255,255));
  2460. SDL_SetColorKey(options,SDL_SRCCOLORKEY,SDL_MapRGB(options->format,0,255,255));
  2461. difficulty->type=1;
  2462. selectedDiff=-77;
  2463. difficulty->gdzie = &selectedDiff;
  2464. for (size_t i=0; i < bScens.imgs->ourImages.size(); ++i) {
  2465. CSDL_Ext::printAt(
  2466. CGI->generaltexth->allTexts[500],
  2467. 25+i,
  2468. 2+i,
  2469. GEOR13,
  2470. zwykly,
  2471. bScens.imgs->ourImages[i].bitmap
  2472. ); //"Show Available Scenarios"
  2473. }
  2474. for (size_t i=0; i < bRandom.imgs->ourImages.size(); ++i) {
  2475. CSDL_Ext::printAt(
  2476. CGI->generaltexth->allTexts[740],
  2477. 25+i,
  2478. 2+i,
  2479. GEOR13,
  2480. zwykly,
  2481. bRandom.imgs->ourImages[i].bitmap
  2482. );
  2483. }
  2484. for (size_t i=0; i < bOptions.imgs->ourImages.size(); ++i) {
  2485. CSDL_Ext::printAt(
  2486. CGI->generaltexth->allTexts[501],
  2487. 25+i,
  2488. 2+i,
  2489. GEOR13,
  2490. zwykly,
  2491. bOptions.imgs->ourImages[i].bitmap
  2492. ); //"Show Advanced Options"
  2493. }
  2494. }
  2495. ScenSel::~ScenSel()
  2496. {
  2497. delete difficulty;
  2498. SDL_FreeSurface(scenInf);
  2499. SDL_FreeSurface(randMap);
  2500. SDL_FreeSurface(background);
  2501. SDL_FreeSurface(options);
  2502. }