CPreGame.cpp 73 KB

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