CPreGame.cpp 73 KB

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