CPreGame.cpp 73 KB

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