CPreGame.cpp 74 KB

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