CPreGame.cpp 74 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625
  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 RoE:
  909. temp=0;
  910. break;
  911. case AB:
  912. temp=1;
  913. break;
  914. case SoD:
  915. temp=2;
  916. break;
  917. case 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)
  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. selected = which;
  1237. CPG->ret.mapname = curVector()[selected]->filename;
  1238. }
  1239. if(updateMapsList)
  1240. printMaps(slid->whereAreWe);
  1241. int serialC=0;
  1242. if(selected >=0 && dontSaveSettings)
  1243. {
  1244. CPG->ret.playerInfos.clear();
  1245. bool wasntpl = true;
  1246. for (int i=0;i<PLAYER_LIMIT;i++)
  1247. {
  1248. if (!(curVector()[selected]->players[i].canComputerPlay
  1249. || curVector()[selected]->players[i].canComputerPlay)
  1250. )
  1251. 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)
  1252. PlayerSettings pset;
  1253. pset.color=(Ecolor)i;
  1254. pset.serial = serialC;
  1255. serialC++;
  1256. pset.bonus=brandom;
  1257. pset.castle=-2;
  1258. if (curVector()[which]->players[i].canHumanPlay && wasntpl)
  1259. {
  1260. pset.name=CGI->generaltexth->allTexts[434]; //Player
  1261. pset.human = true;
  1262. CPG->playerColor = i;
  1263. wasntpl = false;
  1264. }
  1265. else
  1266. {
  1267. pset.name=CGI->generaltexth->allTexts[468];//Computer
  1268. pset.human = false;
  1269. }
  1270. for (int j=0;j<F_NUMBER;j++)
  1271. {
  1272. if (((int)pow((double)2,j))&curVector()[selected]->players[i].allowedFactions)
  1273. {
  1274. if (pset.castle>=0)
  1275. pset.castle=-1;
  1276. if (pset.castle==-2)
  1277. pset.castle=j;
  1278. }
  1279. }
  1280. pset.heroPortrait=-1;
  1281. if (!
  1282. (((curVector()[which]->players[i].generateHeroAtMainTown || curVector()[which]->version==RoE)
  1283. && curVector()[which]->players[i].hasMainTown)
  1284. || curVector()[which]->players[i].p8)
  1285. )
  1286. pset.hero=-2;
  1287. else
  1288. pset.hero=-1;
  1289. if(curVector()[which]->players[i].mainHeroName.length())
  1290. {
  1291. pset.heroName = curVector()[which]->players[i].mainHeroName;
  1292. if((pset.heroPortrait = curVector()[which]->players[i].mainHeroPortrait)==255)
  1293. pset.heroPortrait = curVector()[which]->players[i].p9;
  1294. }
  1295. pset.handicap=0;
  1296. CPG->ret.playerInfos.push_back(pset);
  1297. }
  1298. }
  1299. printSelectedInfo();
  1300. }
  1301. MapSel::MapSel():selected(-1),sizeFilter(0)
  1302. {
  1303. }
  1304. void MapSel::printSelectedInfo()
  1305. {
  1306. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(399,337,17,23),screen,&genRect(399,337,413,29));
  1307. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(50,91,18,447),screen,&genRect(50,91,414,453));
  1308. //blit texts
  1309. CSDL_Ext::printAt(CGI->generaltexth->zelp[21].second,420,25,GEOR13);
  1310. CSDL_Ext::printAt(CGI->generaltexth->allTexts[496],420,135,GEOR13);
  1311. CSDL_Ext::printAt(CGI->generaltexth->allTexts[497],420,285,GEOR13);
  1312. CSDL_Ext::printAt(CGI->generaltexth->allTexts[498],420,340,GEOR13);
  1313. CSDL_Ext::printAt(CGI->generaltexth->allTexts[390],420,406,GEOR13,zwykly);
  1314. CSDL_Ext::printAt(CGI->generaltexth->allTexts[391],585,406,GEOR13,zwykly);
  1315. if(CPG->fromMenu==CPG->newGame)
  1316. {
  1317. SDL_BlitSurface(CPG->ourScenSel->bScens.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bScens.pos);
  1318. SDL_BlitSurface(CPG->ourScenSel->bOptions.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bOptions.pos);
  1319. SDL_BlitSurface(CPG->ourScenSel->bRandom.imgs->ourImages[0].bitmap,NULL,screen,&CPG->ourScenSel->bRandom.pos);
  1320. }
  1321. if (selected >= 0)
  1322. {
  1323. CMapInfo &selMap = selectedMap();
  1324. if(CPG->fromMenu != CPG->newGame)
  1325. {
  1326. CPG->ourScenSel->bEasy.state = 2 + (selMap.seldiff==0);
  1327. CPG->ourScenSel->bNormal.state = 2 + (selMap.seldiff==1);
  1328. CPG->ourScenSel->bHard.state = 2 + (selMap.seldiff==2);
  1329. CPG->ourScenSel->bExpert.state = 2 + (selMap.seldiff==3);
  1330. CPG->ourScenSel->bImpossible.state = 2 + (selMap.seldiff==4);
  1331. CPG->ourScenSel->bEasy.show();
  1332. CPG->ourScenSel->bNormal.show();
  1333. CPG->ourScenSel->bHard.show();
  1334. CPG->ourScenSel->bExpert.show();
  1335. CPG->ourScenSel->bImpossible.show();
  1336. }
  1337. int temp = selMap.victoryCondition.condition+1;
  1338. if (temp>20) temp=0;
  1339. std::string sss = CGI->generaltexth->victoryConditions[temp];
  1340. if (temp && selMap.victoryCondition.allowNormalVictory) sss+= "/" + CGI->generaltexth->victoryConditions[0];
  1341. CSDL_Ext::printAt(sss,452,310,GEOR13,zwykly);
  1342. temp = selMap.lossCondition.typeOfLossCon+1;
  1343. if (temp>20) temp=0;
  1344. sss = CGI->generaltexth->lossCondtions[temp];
  1345. CSDL_Ext::printAt(sss,452,370,GEOR13,zwykly);
  1346. //blit descrption
  1347. std::vector<std::string> desc = *CMessage::breakText(selMap.description,50);
  1348. for (int i=0;i<desc.size();i++)
  1349. CSDL_Ext::printAt(desc[i],417,152+i*13,GEOR13,zwykly);
  1350. if ((selected < 0) || (selected >= ourMaps.size()))
  1351. return;
  1352. if (selMap.name.length())
  1353. CSDL_Ext::printAt(selMap.name,420,41,GEORXX);
  1354. else CSDL_Ext::printAt("Unnamed",420,41,GEORXX);
  1355. std::string diff;
  1356. switch (selMap.difficulty)
  1357. {
  1358. case 0:
  1359. diff=gdiff(CGI->generaltexth->zelp[24].second);
  1360. break;
  1361. case 1:
  1362. diff=gdiff(CGI->generaltexth->zelp[25].second);
  1363. break;
  1364. case 2:
  1365. diff=gdiff(CGI->generaltexth->zelp[26].second);
  1366. break;
  1367. case 3:
  1368. diff=gdiff(CGI->generaltexth->zelp[27].second);
  1369. break;
  1370. case 4:
  1371. diff=gdiff(CGI->generaltexth->zelp[28].second);
  1372. break;
  1373. }
  1374. temp=-1;
  1375. switch (selMap.width)
  1376. {
  1377. case 36:
  1378. temp=0;
  1379. break;
  1380. case 72:
  1381. temp=1;
  1382. break;
  1383. case 108:
  1384. temp=2;
  1385. break;
  1386. case 144:
  1387. temp=3;
  1388. break;
  1389. default:
  1390. temp=4;
  1391. break;
  1392. }
  1393. blitAt(Dsizes->ourImages[temp].bitmap,714,28);
  1394. temp = selMap.victoryCondition.condition;
  1395. if (temp>12) temp=11;
  1396. blitAt(Dvic->ourImages[temp].bitmap,420,308); //v
  1397. temp=selMap.lossCondition.typeOfLossCon;
  1398. if (temp>12) temp=3;
  1399. blitAt(Dloss->ourImages[temp].bitmap,420,366); //l
  1400. CSDL_Ext::printAtMiddle(diff,458,477,GEOR13,zwykly);
  1401. CSDL_Ext::printTo(selMap.date,704,40,GEOR13,zwykly);
  1402. printFlags();
  1403. }
  1404. //SDL_Flip(screen);
  1405. CSDL_Ext::update(screen);
  1406. }
  1407. void MapSel::printFlags()
  1408. {
  1409. CMapInfo &selMap = selectedMap();
  1410. int hy=405, fx=460, ex=640, myT;
  1411. if (selMap.howManyTeams)
  1412. myT = selMap.players[CPG->playerColor].team;
  1413. else myT = -1;
  1414. for (int i=0;i<CPG->ret.playerInfos.size();i++)
  1415. {
  1416. if (myT>=0)
  1417. {
  1418. if(selMap.players[CPG->ret.playerInfos[i].color].team==myT)
  1419. {
  1420. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1421. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1422. }
  1423. else
  1424. {
  1425. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1426. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1427. }
  1428. }
  1429. else
  1430. {
  1431. if(CPG->ret.playerInfos[i].color==CPG->playerColor)
  1432. {
  1433. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,fx,hy);
  1434. fx+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1435. }
  1436. else
  1437. {
  1438. blitAtWR(sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap,ex,hy);
  1439. ex+=sFlags->ourImages[CPG->ret.playerInfos[i].color].bitmap->w;
  1440. }
  1441. }
  1442. }
  1443. }
  1444. std::string MapSel::gdiff(std::string ss)
  1445. {
  1446. std::string ret;
  1447. for (int i=2;i<ss.length();i++)
  1448. {
  1449. if (ss[i]==' ')
  1450. break;
  1451. ret+=ss[i];
  1452. }
  1453. return ret;
  1454. }
  1455. CMapInfo & MapSel::selectedMap()
  1456. {
  1457. if(CPG->fromMenu==CPG->newGame)
  1458. return *ourMaps[selected];
  1459. else
  1460. return *ourGames[selected];
  1461. }
  1462. std::vector<CMapInfo*> & MapSel::curVector()
  1463. {
  1464. if (CPG->fromMenu==CPG->newGame)
  1465. return ourMaps;
  1466. else
  1467. return ourGames;
  1468. }
  1469. void CPreGame::printRating()
  1470. {
  1471. SDL_BlitSurface(CPG->ourScenSel->scenInf,&genRect(47,83,271,449),screen,&genRect(47,83,666,455));
  1472. updateRect(&genRect(47,83,666,455));
  1473. std::string tob;
  1474. switch (ourScenSel->selectedDiff)
  1475. {
  1476. case 0:
  1477. tob="80%";
  1478. break;
  1479. case 1:
  1480. tob="100%";
  1481. break;
  1482. case 2:
  1483. tob="130%";
  1484. break;
  1485. case 3:
  1486. tob="160%";
  1487. break;
  1488. case 4:
  1489. tob="200%";
  1490. break;
  1491. }
  1492. CSDL_Ext::printAtMiddle(tob,703,477,GEOR13,zwykly);
  1493. }
  1494. void CPreGame::printMapsFrom(int from)
  1495. {
  1496. ourScenSel->mapsel.printMaps(from);
  1497. }
  1498. void CPreGame::showScenList()
  1499. {
  1500. if (currentTab!=&ourScenSel->mapsel)
  1501. {
  1502. ourScenSel->listShowed=true;
  1503. ourScenSel->mapsel.show();
  1504. }
  1505. else
  1506. {
  1507. currentTab->hide();
  1508. showScenSel();
  1509. }
  1510. }
  1511. CPreGame::CPreGame()
  1512. {
  1513. CPG=this;
  1514. highlighted=NULL;
  1515. currentTab=NULL;
  1516. run=true;
  1517. timeHandler tmh;tmh.getDif();
  1518. tytulowy.r=229;tytulowy.g=215;tytulowy.b=123;tytulowy.unused=0;
  1519. zwykly.r=255;zwykly.g=255;zwykly.b=255;zwykly.unused=0; //gbr
  1520. tlo.r=66;tlo.g=44;tlo.b=24;tlo.unused=0;
  1521. currentMessage=NULL;
  1522. behindCurMes=NULL;
  1523. initMainMenu();
  1524. tlog0<<"\tCPreGame: main menu initialization: "<<tmh.getDif()<<std::endl;
  1525. initNewMenu();
  1526. tlog0<<"\tCPreGame: newgame menu initialization: "<<tmh.getDif()<<std::endl;
  1527. initLoadMenu();
  1528. tlog0<<"\tCPreGame: loadgame menu initialization: "<<tmh.getDif()<<std::endl;
  1529. initScenSel();
  1530. tlog0<<"\tCPreGame: scenario choice initialization: "<<tmh.getDif()<<std::endl;
  1531. initOptions();
  1532. tlog0<<"\tCPreGame: scenario options initialization: "<<tmh.getDif()<<std::endl;
  1533. showMainMenu();
  1534. tlog0<<"\tCPreGame: displaying main menu: "<<tmh.getDif()<<std::endl;
  1535. playerName="Player";
  1536. }
  1537. void CPreGame::initOptions()
  1538. {
  1539. ourOptions = new Options();
  1540. ourOptions->init();
  1541. }
  1542. void CPreGame::initScenSel()
  1543. {
  1544. ourScenSel = new ScenSel();
  1545. tlog5 << "\t\tLoaded graphics\n";
  1546. ourScenSel->mapsel.init();
  1547. tlog5 << "\t\tLoaded maps\n";
  1548. }
  1549. void CPreGame::showScenSel()
  1550. {
  1551. state=ScenarioList;
  1552. ourScenSel->mapsel.slid->positionsAmnt = ourScenSel->mapsel.curVector().size();
  1553. SDL_BlitSurface(ourScenSel->background,NULL,screen,NULL);
  1554. SDL_BlitSurface(ourScenSel->scenInf,NULL,screen,&genRect(ourScenSel->scenInf->h,ourScenSel->scenInf->w,396,6));
  1555. CSDL_Ext::printAt(CGI->generaltexth->allTexts[494],427,438,GEOR13);//"Map Diff:"
  1556. CSDL_Ext::printAt(CGI->generaltexth->allTexts[492],527,438,GEOR13); //player difficulty
  1557. CSDL_Ext::printAt(CGI->generaltexth->allTexts[218],685,438,GEOR13);//"Rating:"
  1558. //blit buttons
  1559. SDL_BlitSurface(ourScenSel->bEasy.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bEasy.pos);
  1560. SDL_BlitSurface(ourScenSel->bNormal.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bNormal.pos);
  1561. SDL_BlitSurface(ourScenSel->bHard.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bHard.pos);
  1562. SDL_BlitSurface(ourScenSel->bExpert.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bExpert.pos);
  1563. SDL_BlitSurface(ourScenSel->bImpossible.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bImpossible.pos);
  1564. SDL_BlitSurface((fromMenu==newGame ? ourScenSel->bBegin : ourScenSel->bLoad).imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBegin.pos);
  1565. SDL_BlitSurface(ourScenSel->bBack.imgs->ourImages[0].bitmap,NULL,screen,&ourScenSel->bBack.pos);
  1566. //blitAt(ourScenSel->bScens.imgs->ourImages[0].bitmap,ourScenSel->bScens.pos.x,ourScenSel->bScens.pos.y);
  1567. //blitAt(ourScenSel->bRandom.imgs->ourImages[0].bitmap,414,105);
  1568. //blitAt(ourScenSel->bOptions.imgs->ourImages[0].bitmap,414,509);
  1569. //blitAt(ourScenSel->bBegin.imgs->ourImages[0].bitmap,414,535);
  1570. //blitAt(ourScenSel->bBack.imgs->ourImages[0].bitmap,584,535);
  1571. //add buttons info
  1572. if(first)
  1573. {
  1574. if(fromMenu==newGame)
  1575. {
  1576. btns.push_back(&ourScenSel->bEasy);
  1577. btns.push_back(&ourScenSel->bNormal);
  1578. btns.push_back(&ourScenSel->bHard);
  1579. btns.push_back(&ourScenSel->bExpert);
  1580. btns.push_back(&ourScenSel->bImpossible);
  1581. btns.push_back(&ourScenSel->bScens);
  1582. btns.push_back(&ourScenSel->bRandom);
  1583. btns.push_back(&ourScenSel->bOptions);
  1584. }
  1585. else
  1586. ourScenSel->mapsel.show();
  1587. btns.push_back(&(fromMenu==newGame ? ourScenSel->bBegin : ourScenSel->bLoad));
  1588. btns.push_back(&ourScenSel->bBack);
  1589. ourScenSel->selectedDiff=1;
  1590. ourScenSel->bNormal.select(true);
  1591. handleOther = &CPreGame::scenHandleEv;
  1592. ourScenSel->mapsel.select(0,false);
  1593. for (size_t i=0; i < btns.size(); ++i)
  1594. {
  1595. btns[i]->pos.w=btns[i]->imgs->ourImages[0].bitmap->w;
  1596. btns[i]->pos.h=btns[i]->imgs->ourImages[0].bitmap->h;
  1597. btns[i]->ID=10;
  1598. }
  1599. }
  1600. else
  1601. {
  1602. ourScenSel->mapsel.select(ourScenSel->mapsel.selected,false);
  1603. switch (ourScenSel->selectedDiff)
  1604. {
  1605. case 0:
  1606. ourScenSel->bEasy.select(true);
  1607. break;
  1608. case 1:
  1609. ourScenSel->bNormal.select(true);
  1610. break;
  1611. case 2:
  1612. ourScenSel->bHard.select(true);
  1613. break;
  1614. case 3:
  1615. ourScenSel->bExpert.select(true);
  1616. break;
  1617. case 4:
  1618. ourScenSel->bImpossible.select(true);
  1619. break;
  1620. }
  1621. }
  1622. //ourScenSel->mapsel.printSelectedInfo(); // this is already called in select()
  1623. //SDL_Flip(screen);
  1624. CSDL_Ext::update(screen);
  1625. first = false;
  1626. }
  1627. void CPreGame::showOptions()
  1628. {
  1629. if (ourScenSel->mapsel.selected == -1)
  1630. return;
  1631. if (currentTab != ourOptions)
  1632. ourOptions->show();
  1633. else
  1634. {
  1635. currentTab->hide();
  1636. showScenSel();
  1637. }
  1638. }
  1639. void CPreGame::initNewMenu()
  1640. {
  1641. ourNewMenu = new menuItems();
  1642. ourNewMenu->bgAd = BitmapHandler::loadBitmap("ZNEWGAM.bmp");
  1643. ourNewMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  1644. blitAt(ourNewMenu->bgAd,114,312,ourNewMenu->background);
  1645. //loading menu buttons
  1646. ourNewMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  1647. ourNewMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  1648. ourNewMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  1649. ourNewMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  1650. ourNewMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  1651. // single scenario
  1652. ourNewMenu->lNewGame.h=ourNewMenu->newGame->ourImages[0].bitmap->h;
  1653. ourNewMenu->lNewGame.w=ourNewMenu->newGame->ourImages[0].bitmap->w;
  1654. ourNewMenu->lNewGame.x=545;
  1655. ourNewMenu->lNewGame.y=4;
  1656. ourNewMenu->fNewGame=&CPreGame::showScenSel;
  1657. //multiplayer
  1658. ourNewMenu->lLoadGame.h=ourNewMenu->loadGame->ourImages[0].bitmap->h;
  1659. ourNewMenu->lLoadGame.w=ourNewMenu->loadGame->ourImages[0].bitmap->w;
  1660. ourNewMenu->lLoadGame.x=568;
  1661. ourNewMenu->lLoadGame.y=120;
  1662. ourNewMenu->fLoadGame = NULL;
  1663. //campaign
  1664. ourNewMenu->lHighScores.h=ourNewMenu->highScores->ourImages[0].bitmap->h;
  1665. ourNewMenu->lHighScores.w=ourNewMenu->highScores->ourImages[0].bitmap->w;
  1666. ourNewMenu->lHighScores.x=541;
  1667. ourNewMenu->lHighScores.y=233;
  1668. ourNewMenu->fHighScores = NULL;
  1669. //tutorial
  1670. ourNewMenu->lCredits.h=ourNewMenu->credits->ourImages[0].bitmap->h;
  1671. ourNewMenu->lCredits.w=ourNewMenu->credits->ourImages[0].bitmap->w;
  1672. ourNewMenu->lCredits.x=545;
  1673. ourNewMenu->lCredits.y=358;
  1674. ourNewMenu->fCredits = NULL;
  1675. //back
  1676. ourNewMenu->lQuit.h=ourNewMenu->quit->ourImages[0].bitmap->h;
  1677. ourNewMenu->lQuit.w=ourNewMenu->quit->ourImages[0].bitmap->w;
  1678. ourNewMenu->lQuit.x=582;
  1679. ourNewMenu->lQuit.y=464;
  1680. ourNewMenu->fQuit=&CPreGame::showMainMenu;
  1681. ourNewMenu->highlighted=0;
  1682. }
  1683. void CPreGame::showNewMenu()
  1684. {
  1685. if(state == ScenarioList && fromMenu==loadGame)
  1686. {
  1687. showLoadMenu();
  1688. return;
  1689. }
  1690. if (currentTab/*==&ourScenSel->mapsel*/)
  1691. currentTab->hide();
  1692. btns.clear();
  1693. interested.clear();
  1694. handleOther=NULL;
  1695. state = newGame;
  1696. fromMenu = newGame;
  1697. ourScenSel->mapsel.slid->capacity = 18;
  1698. SDL_BlitSurface(ourNewMenu->background,NULL,screen,NULL);
  1699. SDL_BlitSurface(ourNewMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lNewGame);
  1700. SDL_BlitSurface(ourNewMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lLoadGame);
  1701. SDL_BlitSurface(ourNewMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lHighScores);
  1702. SDL_BlitSurface(ourNewMenu->credits->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lCredits);
  1703. SDL_BlitSurface(ourNewMenu->quit->ourImages[0].bitmap,NULL,screen,&ourNewMenu->lQuit);
  1704. //SDL_Flip(screen);
  1705. CSDL_Ext::update(screen);
  1706. first = true;
  1707. }
  1708. void CPreGame::initMainMenu()
  1709. {
  1710. ourMainMenu = new menuItems();
  1711. ourMainMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp"); //SDL_LoadBMP("h3bitmap.lod\\ZPIC1005.bmp");
  1712. //loading menu buttons
  1713. ourMainMenu->newGame = CDefHandler::giveDef("ZMENUNG.DEF");
  1714. ourMainMenu->loadGame = CDefHandler::giveDef("ZMENULG.DEF");
  1715. ourMainMenu->highScores = CDefHandler::giveDef("ZMENUHS.DEF");
  1716. ourMainMenu->credits = CDefHandler::giveDef("ZMENUCR.DEF");
  1717. ourMainMenu->quit = CDefHandler::giveDef("ZMENUQT.DEF");
  1718. ok = CDefHandler::giveDef("IOKAY.DEF");
  1719. cancel = CDefHandler::giveDef("ICANCEL.DEF");
  1720. // new game button location
  1721. ourMainMenu->lNewGame.h=ourMainMenu->newGame->ourImages[0].bitmap->h;
  1722. ourMainMenu->lNewGame.w=ourMainMenu->newGame->ourImages[0].bitmap->w;
  1723. ourMainMenu->lNewGame.x=540;
  1724. ourMainMenu->lNewGame.y=10;
  1725. ourMainMenu->fNewGame=&CPreGame::showNewMenu;
  1726. //load game location
  1727. ourMainMenu->lLoadGame.h=ourMainMenu->loadGame->ourImages[0].bitmap->h;
  1728. ourMainMenu->lLoadGame.w=ourMainMenu->loadGame->ourImages[0].bitmap->w;
  1729. ourMainMenu->lLoadGame.x=532;
  1730. ourMainMenu->lLoadGame.y=132;
  1731. ourMainMenu->fLoadGame=&CPreGame::showLoadMenu;
  1732. //high scores
  1733. ourMainMenu->lHighScores.h=ourMainMenu->highScores->ourImages[0].bitmap->h;
  1734. ourMainMenu->lHighScores.w=ourMainMenu->highScores->ourImages[0].bitmap->w;
  1735. ourMainMenu->lHighScores.x=524;
  1736. ourMainMenu->lHighScores.y=251;
  1737. ourMainMenu->fHighScores = NULL;
  1738. //credits
  1739. ourMainMenu->lCredits.h=ourMainMenu->credits->ourImages[0].bitmap->h;
  1740. ourMainMenu->lCredits.w=ourMainMenu->credits->ourImages[0].bitmap->w;
  1741. ourMainMenu->lCredits.x=557;
  1742. ourMainMenu->lCredits.y=359;
  1743. ourMainMenu->fCredits = NULL;
  1744. //quit
  1745. ourMainMenu->lQuit.h=ourMainMenu->quit->ourImages[0].bitmap->h;
  1746. ourMainMenu->lQuit.w=ourMainMenu->quit->ourImages[0].bitmap->w;
  1747. ourMainMenu->lQuit.x=586;
  1748. ourMainMenu->lQuit.y=468;
  1749. ourMainMenu->fQuit=&CPreGame::quitAskBox;
  1750. ourMainMenu->highlighted=0;
  1751. }
  1752. void CPreGame::showMainMenu()
  1753. {
  1754. state = mainMenu;
  1755. SDL_BlitSurface(ourMainMenu->background,NULL,screen,NULL);
  1756. SDL_BlitSurface(ourMainMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lNewGame);
  1757. SDL_BlitSurface(ourMainMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lLoadGame);
  1758. SDL_BlitSurface(ourMainMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lHighScores);
  1759. SDL_BlitSurface(ourMainMenu->credits->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lCredits);
  1760. SDL_BlitSurface(ourMainMenu->quit->ourImages[0].bitmap,NULL,screen,&ourMainMenu->lQuit);
  1761. //SDL_Flip(screen);
  1762. CSDL_Ext::update(screen);
  1763. }
  1764. void CPreGame::highlightButton(int which, int on)
  1765. {
  1766. menuItems * current = currentItems();
  1767. switch (which)
  1768. {
  1769. case 1:
  1770. {
  1771. SDL_BlitSurface(current->newGame->ourImages[on].bitmap,NULL,screen,&current->lNewGame);
  1772. break;
  1773. }
  1774. case 2:
  1775. {
  1776. SDL_BlitSurface(current->loadGame->ourImages[on].bitmap,NULL,screen,&current->lLoadGame);
  1777. break;
  1778. }
  1779. case 3:
  1780. {
  1781. SDL_BlitSurface(current->highScores->ourImages[on].bitmap,NULL,screen,&current->lHighScores);
  1782. break;
  1783. }
  1784. case 4:
  1785. {
  1786. SDL_BlitSurface(current->credits->ourImages[on].bitmap,NULL,screen,&current->lCredits);
  1787. break;
  1788. }
  1789. case 5:
  1790. {
  1791. SDL_BlitSurface(current->quit->ourImages[on].bitmap,NULL,screen,&current->lQuit);
  1792. break;
  1793. }
  1794. }
  1795. //SDL_Flip(screen);
  1796. CSDL_Ext::update(screen);
  1797. }
  1798. void CPreGame::showCenBox (std::string data)
  1799. {
  1800. CMessage * cmh = new CMessage();
  1801. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data));
  1802. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1803. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1804. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1805. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1806. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1807. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1808. SDL_FreeSurface(infoBox);
  1809. currentMessage = new SDL_Rect(pos);
  1810. delete cmh;
  1811. }
  1812. void CPreGame::showAskBox (std::string data, void(*f1)(),void(*f2)())
  1813. {
  1814. CMessage * cmh = new CMessage();
  1815. std::vector<CDefHandler*> * przyciski = new std::vector<CDefHandler*>(0);
  1816. std::vector<SDL_Rect> * btnspos= new std::vector<SDL_Rect>(0);
  1817. przyciski->push_back(ok);
  1818. przyciski->push_back(cancel);
  1819. SDL_Surface * infoBox = cmh->genMessage(CGI->generaltexth->getTitle(data), CGI->generaltexth->getDescr(data), yesOrNO, przyciski, btnspos);
  1820. behindCurMes = CSDL_Ext::newSurface(infoBox->w,infoBox->h,screen);
  1821. SDL_Rect pos = genRect(infoBox->h,infoBox->w,
  1822. (screen->w/2)-(infoBox->w/2),(screen->h/2)-(infoBox->h/2));
  1823. SDL_BlitSurface(screen,&pos,behindCurMes,NULL);
  1824. SDL_BlitSurface(infoBox,NULL,screen,&pos);
  1825. SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  1826. SDL_FreeSurface(infoBox);
  1827. currentMessage = new SDL_Rect(pos);
  1828. (*btnspos)[0].x+=pos.x;
  1829. (*btnspos)[0].y+=pos.y;
  1830. (*btnspos)[1].x+=pos.x;
  1831. (*btnspos)[1].y+=pos.y;
  1832. btns.push_back(new Button((*btnspos)[0],boost::bind(&CPreGame::quit,this),ok,false, NULL,2));
  1833. btns.push_back(new Button((*btnspos)[1],boost::bind(&CPreGame::hideBox,this),cancel,false, NULL,2));
  1834. delete cmh;
  1835. delete przyciski;
  1836. delete btnspos;
  1837. }
  1838. void CPreGame::hideBox ()
  1839. {
  1840. SDL_BlitSurface(behindCurMes,NULL,screen,currentMessage);
  1841. SDL_UpdateRect
  1842. (screen,currentMessage->x,currentMessage->y,currentMessage->w,currentMessage->h);
  1843. for (size_t i=0; i < btns.size(); ++i)
  1844. {
  1845. if (btns[i]->ID==2)
  1846. {
  1847. delete btns[i];
  1848. btns.erase(btns.begin()+i);
  1849. i--;
  1850. }
  1851. }
  1852. SDL_FreeSurface(behindCurMes);
  1853. delete currentMessage;
  1854. currentMessage = NULL;
  1855. behindCurMes=NULL;
  1856. }
  1857. CPreGame::menuItems * CPreGame::currentItems()
  1858. {
  1859. switch (state)
  1860. {
  1861. case mainMenu:
  1862. return ourMainMenu;
  1863. case newGame:
  1864. return ourNewMenu;
  1865. case loadGame:
  1866. return ourLoadMenu;
  1867. default:
  1868. return NULL;
  1869. }
  1870. }
  1871. void CPreGame::scenHandleEv(SDL_Event& sEvent)
  1872. {
  1873. if(currentTab==&ourScenSel->mapsel)
  1874. {
  1875. if(sEvent.button.button == SDL_BUTTON_WHEELUP)
  1876. {
  1877. ourScenSel->mapsel.slid->moveUp();
  1878. return;
  1879. }
  1880. else if(sEvent.button.button == SDL_BUTTON_WHEELDOWN)
  1881. {
  1882. ourScenSel->mapsel.slid->moveDown();
  1883. return;
  1884. }
  1885. }
  1886. if(sEvent.type == SDL_MOUSEMOTION)
  1887. {
  1888. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y);
  1889. }
  1890. if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1891. {
  1892. for (size_t i=0; i < btns.size(); ++i)
  1893. {
  1894. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1895. {
  1896. CGI->audioh->playSound(soundBase::button);
  1897. btns[i]->press(true);
  1898. ourScenSel->pressed=(Button*)btns[i];
  1899. }
  1900. }
  1901. if ((currentTab==&ourScenSel->mapsel) && (sEvent.button.y>121) &&(sEvent.button.y<570)
  1902. && (sEvent.button.x>55) && (sEvent.button.x<372))
  1903. {
  1904. int py = ((sEvent.button.y-121)/25)+ourScenSel->mapsel.slid->whereAreWe;
  1905. CGI->audioh->playSound(soundBase::button);
  1906. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(py));
  1907. }
  1908. }
  1909. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  1910. {
  1911. Button * prnr=ourScenSel->pressed;
  1912. if (ourScenSel->pressed && ourScenSel->pressed->state==1)
  1913. {
  1914. ourScenSel->pressed->press(false);
  1915. ourScenSel->pressed=NULL;
  1916. }
  1917. for (size_t i=0; i < btns.size(); ++i)
  1918. {
  1919. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1920. {
  1921. if (btns[i]->selectable)
  1922. btns[i]->select(true);
  1923. if (btns[i]->type==1 && ((Button*)btns[i])->fun)
  1924. ((Button*)btns[i])->fun();
  1925. int zz = btns.size();
  1926. if (i>=zz)
  1927. break;
  1928. if (btns[i]==prnr && btns[i]->type==2)
  1929. {
  1930. ((IntBut*)(btns[i]))->set();
  1931. ourScenSel->mapsel.slid->whereAreWe=0;
  1932. ourScenSel->mapsel.slid->updateSlid();
  1933. ourScenSel->mapsel.slid->positionsAmnt=ourScenSel->mapsel.countWL();
  1934. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(0));
  1935. ourScenSel->mapsel.printMaps(0);
  1936. }
  1937. }
  1938. }
  1939. }
  1940. else if (sEvent.type==SDL_MOUSEMOTION)
  1941. {
  1942. if (highlighted)
  1943. {
  1944. if (isItIn(&highlighted->pos,sEvent.motion.x,sEvent.motion.y))
  1945. return;
  1946. else
  1947. {
  1948. highlighted->hover(false);
  1949. highlighted = NULL;
  1950. }
  1951. }
  1952. for (size_t i=0; i < btns.size(); ++i)
  1953. {
  1954. if (!btns[i]->highlightable)
  1955. continue;
  1956. if(isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  1957. {
  1958. btns[i]->hover(true);
  1959. highlighted=btns[i];
  1960. return;
  1961. }
  1962. else if (btns[i]->highlighted)
  1963. btns[i]->hover(false);
  1964. }
  1965. }
  1966. }
  1967. StartInfo CPreGame::runLoop()
  1968. {
  1969. SDL_Event sEvent;
  1970. ret.turnTime = 0;
  1971. while(run)
  1972. {
  1973. try
  1974. {
  1975. while(SDL_PollEvent(&sEvent)) //handle all events
  1976. {
  1977. menuItems * current = currentItems();
  1978. if(sEvent.type==SDL_QUIT)
  1979. {
  1980. exit(EXIT_SUCCESS);
  1981. return ret;
  1982. }
  1983. for (size_t i=0; i < interested.size(); ++i) {
  1984. interested[i]->handleIt(sEvent);
  1985. }
  1986. if (!current)
  1987. {
  1988. (this->*handleOther)(sEvent);
  1989. }
  1990. else if (sEvent.type==SDL_KEYDOWN)
  1991. {
  1992. if (sEvent.key.keysym.sym==SDLK_q)
  1993. {
  1994. exit(EXIT_SUCCESS);
  1995. }
  1996. if(sEvent.key.keysym.sym==SDLK_F4 && (sEvent.key.keysym.mod & KMOD_LALT)) //Alt+F4
  1997. {
  1998. exit(EXIT_SUCCESS);
  1999. }
  2000. /*if (state==EState::newGame)
  2001. {
  2002. switch (sEvent.key.keysym.sym)
  2003. {
  2004. case SDLK_LEFT:
  2005. {
  2006. if(currentItems()->lNewGame.x>0)
  2007. currentItems()->lNewGame.x--;
  2008. break;
  2009. }
  2010. case (SDLK_RIGHT):
  2011. {
  2012. currentItems()->lNewGame.x++;
  2013. break;
  2014. }
  2015. case (SDLK_UP):
  2016. {
  2017. if(currentItems()->lNewGame.y>0)
  2018. currentItems()->lNewGame.y--;
  2019. break;
  2020. }
  2021. case (SDLK_DOWN):
  2022. {
  2023. currentItems()->lNewGame.y++;
  2024. break;
  2025. }
  2026. }
  2027. showNewMenu();
  2028. }*/
  2029. }
  2030. else if (sEvent.type==SDL_MOUSEMOTION)
  2031. {
  2032. CGI->curh->cursorMove(sEvent.motion.x, sEvent.motion.y); //for graphical mouse
  2033. if (currentMessage) continue;
  2034. if (current->highlighted)
  2035. {
  2036. switch (current->highlighted)
  2037. {
  2038. case 1:
  2039. {
  2040. if(isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2041. continue;
  2042. else
  2043. {
  2044. current->highlighted=0;
  2045. highlightButton(1,0);
  2046. }
  2047. break;
  2048. }
  2049. case 2:
  2050. {
  2051. if(isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2052. continue;
  2053. else
  2054. {
  2055. current->highlighted=0;
  2056. highlightButton(2,0);
  2057. }
  2058. break;
  2059. }
  2060. case 3:
  2061. {
  2062. if(isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2063. continue;
  2064. else
  2065. {
  2066. current->highlighted=0;
  2067. highlightButton(3,0);
  2068. }
  2069. break;
  2070. }
  2071. case 4:
  2072. {
  2073. if(isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2074. continue;
  2075. else
  2076. {
  2077. current->highlighted=0;
  2078. highlightButton(4,0);
  2079. }
  2080. break;
  2081. }
  2082. case 5:
  2083. {
  2084. if(isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2085. continue;
  2086. else
  2087. {
  2088. current->highlighted=0;
  2089. highlightButton(5,0);
  2090. }
  2091. break;
  2092. }
  2093. } //switch (current->highlighted)
  2094. } // if (current->highlighted)
  2095. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2096. {
  2097. highlightButton(1,2);
  2098. current->highlighted=1;
  2099. }
  2100. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2101. {
  2102. highlightButton(2,2);
  2103. current->highlighted=2;
  2104. }
  2105. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2106. {
  2107. highlightButton(3,2);
  2108. current->highlighted=3;
  2109. }
  2110. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2111. {
  2112. highlightButton(4,2);
  2113. current->highlighted=4;
  2114. }
  2115. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2116. {
  2117. highlightButton(5,2);
  2118. current->highlighted=5;
  2119. }
  2120. }
  2121. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_LEFT))
  2122. {
  2123. for (size_t i=0; i < btns.size(); ++i)
  2124. {
  2125. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  2126. {
  2127. btns[i]->press(true);
  2128. //SDL_BlitSurface((btns[i].imgs)->ourImages[1].bitmap,NULL,screen,&btns[i].pos);
  2129. //updateRect(&btns[i].pos);
  2130. }
  2131. }
  2132. if (currentMessage) continue;
  2133. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2134. {
  2135. highlightButton(1,1);
  2136. current->highlighted=1;
  2137. }
  2138. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2139. {
  2140. highlightButton(2,1);
  2141. current->highlighted=2;
  2142. }
  2143. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2144. {
  2145. highlightButton(3,1);
  2146. current->highlighted=3;
  2147. }
  2148. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2149. {
  2150. highlightButton(4,1);
  2151. current->highlighted=4;
  2152. }
  2153. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2154. {
  2155. highlightButton(5,1);
  2156. current->highlighted=5;
  2157. }
  2158. if (current->highlighted)
  2159. CGI->audioh->playSound(soundBase::button);
  2160. }
  2161. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_LEFT))
  2162. {
  2163. for (size_t i=0; i < btns.size(); ++i)
  2164. {
  2165. if (isItIn(&btns[i]->pos,sEvent.motion.x,sEvent.motion.y))
  2166. ((Button*)btns[i])->fun();
  2167. else
  2168. {
  2169. btns[i]->press(false);
  2170. //SDL_BlitSurface((btns[i].imgs)->ourImages[0].bitmap,NULL,screen,&btns[i].pos);
  2171. //updateRect(&btns[i].pos);
  2172. }
  2173. }
  2174. if (currentMessage) continue;
  2175. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2176. {
  2177. highlightButton(1,2);
  2178. current->highlighted=1;
  2179. if(current->fNewGame)
  2180. (this->*(current->fNewGame))();
  2181. }
  2182. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2183. {
  2184. highlightButton(2,2);
  2185. current->highlighted=2;
  2186. if(current->fLoadGame)
  2187. (this->*(current->fLoadGame))();
  2188. }
  2189. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2190. {
  2191. highlightButton(3,2);
  2192. current->highlighted=3;
  2193. }
  2194. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2195. {
  2196. highlightButton(4,2);
  2197. current->highlighted=4;
  2198. }
  2199. else if (isItIn(&current->lQuit,sEvent.motion.x,sEvent.motion.y))
  2200. {
  2201. highlightButton(5,2);
  2202. current->highlighted=5;
  2203. if(current->fQuit)
  2204. (this->*(current->fQuit))();
  2205. }
  2206. }
  2207. else if ((sEvent.type==SDL_MOUSEBUTTONDOWN) && (sEvent.button.button == SDL_BUTTON_RIGHT))
  2208. {
  2209. if (currentMessage) continue;
  2210. if (isItIn(&current->lNewGame,sEvent.motion.x,sEvent.motion.y))
  2211. {
  2212. showCenBox(buttonText(0));
  2213. }
  2214. else if (isItIn(&current->lLoadGame,sEvent.motion.x,sEvent.motion.y))
  2215. {
  2216. showCenBox(buttonText(1));
  2217. }
  2218. else if (isItIn(&current->lHighScores,sEvent.motion.x,sEvent.motion.y))
  2219. {
  2220. showCenBox(buttonText(2));
  2221. }
  2222. else if (isItIn(&current->lCredits,sEvent.motion.x,sEvent.motion.y))
  2223. {
  2224. showCenBox(buttonText(3));
  2225. }
  2226. else if (isItIn(&ourMainMenu->lQuit,sEvent.motion.x,sEvent.motion.y))
  2227. {
  2228. showCenBox(buttonText(4));
  2229. }
  2230. }
  2231. else if ((sEvent.type==SDL_MOUSEBUTTONUP) && (sEvent.button.button == SDL_BUTTON_RIGHT) && currentMessage)
  2232. {
  2233. hideBox();
  2234. }
  2235. }
  2236. } HANDLE_EXCEPTION
  2237. CGI->curh->draw1();
  2238. SDL_Flip(screen);
  2239. CGI->curh->draw2();
  2240. SDL_Delay(20); //give time for other apps
  2241. }
  2242. ret.mode = (fromMenu==newGame) ? 0 : 1;
  2243. return ret;
  2244. }
  2245. std::string CPreGame::buttonText(int which)
  2246. {
  2247. if (state==mainMenu)
  2248. {
  2249. switch (which)
  2250. {
  2251. case 0:
  2252. return CGI->generaltexth->zelp[3].second;
  2253. case 1:
  2254. return CGI->generaltexth->zelp[4].second;
  2255. case 2:
  2256. return CGI->generaltexth->zelp[5].second;
  2257. case 3:
  2258. return CGI->generaltexth->zelp[6].second;
  2259. case 4:
  2260. return CGI->generaltexth->zelp[7].second;
  2261. }
  2262. }
  2263. else if (state==newGame || state==loadGame)
  2264. {
  2265. switch (which)
  2266. {
  2267. case 0:
  2268. return CGI->generaltexth->zelp[10].second;
  2269. case 1:
  2270. return CGI->generaltexth->zelp[11].second;
  2271. case 2:
  2272. return CGI->generaltexth->zelp[12].second;
  2273. case 3:
  2274. return CGI->generaltexth->zelp[13].second;
  2275. case 4:
  2276. return CGI->generaltexth->zelp[14].second;
  2277. }
  2278. }
  2279. return std::string();
  2280. }
  2281. void CPreGame::quitAskBox()
  2282. {
  2283. showAskBox("\"{} Are you sure you want to quit?\"",NULL,NULL);
  2284. }
  2285. void CPreGame::sortMaps()
  2286. {
  2287. std::sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(_name));
  2288. if(ourScenSel->mapsel.sortBy != _name)
  2289. std::stable_sort(ourScenSel->mapsel.ourMaps.begin(),ourScenSel->mapsel.ourMaps.end(),mapSorter(ourScenSel->mapsel.sortBy));
  2290. ourScenSel->mapsel.select(ourScenSel->mapsel.whichWL(0),false,true);
  2291. ourScenSel->mapsel.slid->whereAreWe=0;
  2292. ourScenSel->mapsel.slid->updateSlid();
  2293. printMapsFrom(0);
  2294. }
  2295. void CPreGame::setTurnLength(int on)
  2296. {
  2297. int min;
  2298. switch (on)
  2299. {
  2300. case 0:
  2301. min=1;
  2302. break;
  2303. case 1:
  2304. min=2;
  2305. break;
  2306. case 2:
  2307. min=4;
  2308. break;
  2309. case 3:
  2310. min=6;
  2311. break;
  2312. case 4:
  2313. min=8;
  2314. break;
  2315. case 5:
  2316. min=10;
  2317. break;
  2318. case 6:
  2319. min=15;
  2320. break;
  2321. case 7:
  2322. min=20;
  2323. break;
  2324. case 8:
  2325. min=25;
  2326. break;
  2327. case 9:
  2328. min=30;
  2329. break;
  2330. case 10:
  2331. min=0;
  2332. break;
  2333. default:
  2334. min=0;
  2335. break;
  2336. }
  2337. SDL_BlitSurface(ourOptions->bg,&genRect(23,134,256,547),screen,&genRect(23,134,258,553));
  2338. updateRect(&genRect(23,134,258,553));
  2339. if (min)
  2340. {
  2341. std::ostringstream os;
  2342. os<<min<<" Minutes";
  2343. CSDL_Ext::printAtMiddle(os.str(),323,563,GEOR13);
  2344. }
  2345. else CSDL_Ext::printAtMiddle("Unlimited",323,563,GEOR13);
  2346. }
  2347. void CPreGame::showLoadMenu()
  2348. {
  2349. if (currentTab/*==&ourScenSel->mapsel*/)
  2350. currentTab->hide();
  2351. btns.clear();
  2352. interested.clear();
  2353. handleOther=NULL;
  2354. state = loadGame;
  2355. fromMenu = loadGame;
  2356. ourScenSel->mapsel.slid->capacity = 18;
  2357. SDL_BlitSurface(ourLoadMenu->background,NULL,screen,NULL);
  2358. SDL_BlitSurface(ourLoadMenu->newGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lNewGame);
  2359. SDL_BlitSurface(ourLoadMenu->loadGame->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lLoadGame);
  2360. SDL_BlitSurface(ourLoadMenu->highScores->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lHighScores);
  2361. SDL_BlitSurface(ourLoadMenu->credits->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lCredits);
  2362. SDL_BlitSurface(ourLoadMenu->quit->ourImages[0].bitmap,NULL,screen,&ourLoadMenu->lQuit);
  2363. //SDL_Flip(screen);
  2364. CSDL_Ext::update(screen);
  2365. first = true;
  2366. }
  2367. void CPreGame::initLoadMenu()
  2368. {
  2369. ourLoadMenu = new menuItems();
  2370. ourLoadMenu->bgAd = BitmapHandler::loadBitmap("ZLOADGAM.bmp");
  2371. ourLoadMenu->background = BitmapHandler::loadBitmap("ZPIC1005.bmp");
  2372. blitAt(ourLoadMenu->bgAd,114,312,ourLoadMenu->background);
  2373. //loading menu buttons
  2374. ourLoadMenu->newGame = CDefHandler::giveDef("ZTSINGL.DEF");
  2375. ourLoadMenu->loadGame = CDefHandler::giveDef("ZTMULTI.DEF");
  2376. ourLoadMenu->highScores = CDefHandler::giveDef("ZTCAMPN.DEF");
  2377. ourLoadMenu->credits = CDefHandler::giveDef("ZTTUTOR.DEF");
  2378. ourLoadMenu->quit = CDefHandler::giveDef("ZTBACK.DEF");
  2379. // single scenario
  2380. ourLoadMenu->lNewGame.h=ourLoadMenu->newGame->ourImages[0].bitmap->h;
  2381. ourLoadMenu->lNewGame.w=ourLoadMenu->newGame->ourImages[0].bitmap->w;
  2382. ourLoadMenu->lNewGame.x=545;
  2383. ourLoadMenu->lNewGame.y=4;
  2384. ourLoadMenu->fNewGame=&CPreGame::showScenSel;
  2385. //multiplayer
  2386. ourLoadMenu->lLoadGame.h=ourLoadMenu->loadGame->ourImages[0].bitmap->h;
  2387. ourLoadMenu->lLoadGame.w=ourLoadMenu->loadGame->ourImages[0].bitmap->w;
  2388. ourLoadMenu->lLoadGame.x=568;
  2389. ourLoadMenu->lLoadGame.y=120;
  2390. ourLoadMenu->fLoadGame = NULL;
  2391. //campaign
  2392. ourLoadMenu->lHighScores.h=ourLoadMenu->highScores->ourImages[0].bitmap->h;
  2393. ourLoadMenu->lHighScores.w=ourLoadMenu->highScores->ourImages[0].bitmap->w;
  2394. ourLoadMenu->lHighScores.x=541;
  2395. ourLoadMenu->lHighScores.y=233;
  2396. ourLoadMenu->fHighScores = NULL;
  2397. //tutorial
  2398. ourLoadMenu->lCredits.h=ourLoadMenu->credits->ourImages[0].bitmap->h;
  2399. ourLoadMenu->lCredits.w=ourLoadMenu->credits->ourImages[0].bitmap->w;
  2400. ourLoadMenu->lCredits.x=545;
  2401. ourLoadMenu->lCredits.y=358;
  2402. ourLoadMenu->fCredits = NULL;
  2403. //back
  2404. ourLoadMenu->lQuit.h=ourLoadMenu->quit->ourImages[0].bitmap->h;
  2405. ourLoadMenu->lQuit.w=ourLoadMenu->quit->ourImages[0].bitmap->w;
  2406. ourLoadMenu->lQuit.x=582;
  2407. ourLoadMenu->lQuit.y=464;
  2408. ourLoadMenu->fQuit=&CPreGame::showMainMenu;
  2409. ourLoadMenu->highlighted=0;
  2410. }
  2411. CPreGame::~CPreGame()
  2412. {
  2413. delete ourMainMenu;
  2414. delete ourNewMenu;
  2415. delete ourLoadMenu;
  2416. delete ok;
  2417. delete cancel;
  2418. }
  2419. CPreGame::menuItems::menuItems()
  2420. {
  2421. }
  2422. CPreGame::menuItems::~menuItems()
  2423. {
  2424. delete this->newGame;
  2425. delete this->loadGame;
  2426. delete this->highScores;
  2427. delete this->credits;
  2428. delete this->quit;
  2429. SDL_FreeSurface(bgAd);
  2430. SDL_FreeSurface(background);
  2431. }
  2432. ScenSel::ScenSel()
  2433. :
  2434. difficulty(new CPoinGroup()),
  2435. bScens(genRect(0,0,414,81),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2436. bOptions(genRect(0,0,414,509),boost::bind(&CPreGame::showOptions,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2437. bRandom(genRect(0,0,414,105),boost::bind(&CPreGame::showScenList,CPG),CDefHandler::giveDef("GSPBUTT.DEF")),
  2438. bBegin(genRect(0,0,414,535),boost::bind(&CPreGame::begin,CPG),CDefHandler::giveDef("SCNRBEG.DEF")),
  2439. bLoad(genRect(0,0,414,535),boost::bind(&CPreGame::begin,CPG),CDefHandler::giveDef("SCNRLOD.DEF")),
  2440. bBack(genRect(0,0,584,535),boost::bind(&CPreGame::showNewMenu,CPG),CDefHandler::giveDef("SCNRBACK.DEF")),
  2441. bEasy(genRect(0,0,506,456),NULL,CDefHandler::giveDef("GSPBUT3.DEF"),true,difficulty,0),
  2442. bNormal(genRect(0,0,538,456),NULL,CDefHandler::giveDef("GSPBUT4.DEF"),true,difficulty,1),
  2443. bHard(genRect(0,0,570,456),NULL,CDefHandler::giveDef("GSPBUT5.DEF"),true,difficulty,2),
  2444. bExpert(genRect(0,0,602,456),NULL,CDefHandler::giveDef("GSPBUT6.DEF"),true,difficulty,3),
  2445. bImpossible(genRect(0,0,634,456),NULL,CDefHandler::giveDef("GSPBUT7.DEF"),true,difficulty,4)
  2446. {
  2447. pressed=NULL;
  2448. listShowed=false;
  2449. if (rand()%2)
  2450. background = BitmapHandler::loadBitmap("ZPIC1000.bmp");
  2451. else
  2452. background = BitmapHandler::loadBitmap("ZPIC1001.bmp");
  2453. savenameStrip = BitmapHandler::loadBitmap("GSSTRIP.bmp");
  2454. scenInf = BitmapHandler::loadBitmap("GSELPOP1.bmp");
  2455. randMap = BitmapHandler::loadBitmap("RANMAPBK.bmp");
  2456. options = BitmapHandler::loadBitmap("ADVOPTBK.bmp");
  2457. SDL_SetColorKey(scenInf,SDL_SRCCOLORKEY,SDL_MapRGB(scenInf->format,0,255,255));
  2458. SDL_SetColorKey(randMap,SDL_SRCCOLORKEY,SDL_MapRGB(randMap->format,0,255,255));
  2459. SDL_SetColorKey(options,SDL_SRCCOLORKEY,SDL_MapRGB(options->format,0,255,255));
  2460. difficulty->type=1;
  2461. selectedDiff=-77;
  2462. difficulty->gdzie = &selectedDiff;
  2463. for (size_t i=0; i < bScens.imgs->ourImages.size(); ++i) {
  2464. CSDL_Ext::printAt(
  2465. CGI->generaltexth->allTexts[500],
  2466. 25+i,
  2467. 2+i,
  2468. GEOR13,
  2469. zwykly,
  2470. bScens.imgs->ourImages[i].bitmap
  2471. ); //"Show Available Scenarios"
  2472. }
  2473. for (size_t i=0; i < bRandom.imgs->ourImages.size(); ++i) {
  2474. CSDL_Ext::printAt(
  2475. CGI->generaltexth->allTexts[740],
  2476. 25+i,
  2477. 2+i,
  2478. GEOR13,
  2479. zwykly,
  2480. bRandom.imgs->ourImages[i].bitmap
  2481. );
  2482. }
  2483. for (size_t i=0; i < bOptions.imgs->ourImages.size(); ++i) {
  2484. CSDL_Ext::printAt(
  2485. CGI->generaltexth->allTexts[501],
  2486. 25+i,
  2487. 2+i,
  2488. GEOR13,
  2489. zwykly,
  2490. bOptions.imgs->ourImages[i].bitmap
  2491. ); //"Show Advanced Options"
  2492. }
  2493. }
  2494. ScenSel::~ScenSel()
  2495. {
  2496. delete difficulty;
  2497. SDL_FreeSurface(scenInf);
  2498. SDL_FreeSurface(randMap);
  2499. SDL_FreeSurface(background);
  2500. SDL_FreeSurface(options);
  2501. }