CPreGame.cpp 72 KB

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