CPreGame.cpp 73 KB

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