CPreGame.cpp 72 KB

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