CPreGame.cpp 74 KB

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