CPreGame.cpp 75 KB

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