CPreGame.cpp 74 KB

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