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