2
0

CPreGame.cpp 74 KB

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