CPreGame.cpp 74 KB

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