CPreGame.cpp 70 KB

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