CPreGame.cpp 72 KB

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