CPlayerInterface.cpp 102 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418
  1. #include "stdafx.h"
  2. #include "CAdvmapInterface.h"
  3. #include "CBattleInterface.h"
  4. #include "CCallback.h"
  5. #include "CCastleInterface.h"
  6. #include "CCursorHandler.h"
  7. #include "CGameInfo.h"
  8. #include "CHeroWindow.h"
  9. #include "CMessage.h"
  10. #include "CPathfinder.h"
  11. #include "CPlayerInterface.h"
  12. #include "SDL_Extensions.h"
  13. #include "SDL_Extensions.h"
  14. #include "SDL_framerate.h"
  15. #include "SDL_framerate.h"
  16. #include "client/CCreatureAnimation.h"
  17. #include "client/Graphics.h"
  18. #include "hch/CAbilityHandler.h"
  19. #include "hch/CArtHandler.h"
  20. #include "hch/CGeneralTextHandler.h"
  21. #include "hch/CHeroHandler.h"
  22. #include "hch/CLodHandler.h"
  23. #include "hch/CObjectHandler.h"
  24. #include "hch/CPreGameTextHandler.h"
  25. #include "hch/CSpellHandler.h"
  26. #include "hch/CTownHandler.h"
  27. #include "lib/CondSh.h"
  28. #include "lib/NetPacks.h"
  29. #include "map.h"
  30. #include "mapHandler.h"
  31. #include "timeHandler.h"
  32. #include <boost/algorithm/string.hpp>
  33. #include <boost/algorithm/string/replace.hpp>
  34. #include <boost/thread.hpp>
  35. #include <cmath>
  36. #include <queue>
  37. #include <sstream>
  38. using namespace CSDL_Ext;
  39. extern TTF_Font * GEOR16;
  40. extern bool continueReadingConsole;
  41. CPlayerInterface * LOCPLINT;
  42. extern std::queue<SDL_Event> events;
  43. extern boost::mutex eventsM;
  44. class OCM_HLP_CGIN
  45. {
  46. public:
  47. bool operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  48. {
  49. return (*a.first)<(*b.first);
  50. }
  51. } ocmptwo_cgin ;
  52. void CGarrisonSlot::hover (bool on)
  53. {
  54. Hoverable::hover(on);
  55. if(on)
  56. {
  57. std::string temp;
  58. if(creature)
  59. {
  60. if(owner->highlighted)
  61. {
  62. if(owner->highlighted == this)
  63. {
  64. temp = CGI->townh->tcommands[4];
  65. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  66. }
  67. else if (owner->highlighted->creature == creature)
  68. {
  69. temp = CGI->townh->tcommands[2];
  70. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  71. }
  72. else if (owner->highlighted->creature)
  73. {
  74. temp = CGI->townh->tcommands[7];
  75. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  76. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  77. }
  78. else
  79. {
  80. std::cout << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  81. std::cout << "Highlighted set to NULL"<<std::endl;
  82. owner->highlighted = NULL;
  83. }
  84. }
  85. else
  86. {
  87. if(upg)
  88. {
  89. temp = CGI->townh->tcommands[32];
  90. }
  91. else
  92. {
  93. temp = CGI->townh->tcommands[12];
  94. }
  95. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  96. };
  97. }
  98. else
  99. {
  100. if(owner->highlighted)
  101. {
  102. temp = CGI->townh->tcommands[6];
  103. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  104. }
  105. else
  106. {
  107. temp = CGI->townh->tcommands[11];
  108. }
  109. }
  110. LOCPLINT->statusbar->print(temp);
  111. }
  112. else
  113. {
  114. LOCPLINT->statusbar->clear();
  115. }
  116. }
  117. const CArmedInstance * CGarrisonSlot::getObj()
  118. {
  119. return (!upg)?(owner->oup):(owner->odown);
  120. }
  121. void CGarrisonSlot::clickRight (tribool down)
  122. {
  123. StackState *pom = NULL;
  124. if(down && creature)
  125. {
  126. if(getObj()->ID == 34)
  127. {
  128. pom = new StackState();
  129. const CGHeroInstance *h = static_cast<const CGHeroInstance *>(getObj());
  130. pom->currentHealth = 0;
  131. pom->attackBonus = h->primSkills[0];
  132. pom->defenseBonus = h->primSkills[1];
  133. pom->luck = h->getCurrentLuck();
  134. pom->morale = h->getCurrentMorale();
  135. }
  136. (new CCreInfoWindow(creature->idNumber,0,count,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  137. ->activate();
  138. //LOCPLINT->curint->deactivate();
  139. }
  140. delete pom;
  141. }
  142. void CGarrisonSlot::clickLeft(tribool down)
  143. {
  144. if(owner->ignoreEvent)
  145. {
  146. owner->ignoreEvent = false;
  147. return;
  148. }
  149. if(down)
  150. {
  151. bool refr = false;
  152. if(owner->highlighted)
  153. {
  154. if(owner->highlighted == this) //view info
  155. {
  156. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(),ID);
  157. if(pom.oldID>=0)
  158. {
  159. (new CCreInfoWindow
  160. (creature->idNumber,1,count,NULL,
  161. boost::bind(&CCallback::upgradeCreature,LOCPLINT->cb,getObj(),ID,pom.newID[0]), //if upgrade is possible we'll bind proper function in callback
  162. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID),&pom))
  163. ->activate();
  164. }
  165. else
  166. {
  167. (new CCreInfoWindow
  168. (creature->idNumber,1,count,NULL,0, boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID),NULL) )
  169. ->activate();
  170. }
  171. if(LOCPLINT->curint->subInt)
  172. LOCPLINT->curint->subInt->deactivate();
  173. else
  174. LOCPLINT->curint->deactivate();
  175. owner->highlighted = NULL;
  176. show();
  177. refr = true;
  178. }
  179. else if( !creature && owner->splitting)//split
  180. {
  181. owner->p2 = ID;
  182. owner->pb = upg;
  183. owner->splitting = false;
  184. LOCPLINT->curint->deactivate();
  185. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber,owner->highlighted->count, owner);
  186. spw->activate();
  187. refr = true;
  188. }
  189. else if(creature != owner->highlighted->creature) //swap
  190. {
  191. LOCPLINT->cb->swapCreatures(
  192. (!upg)?(owner->oup):(owner->odown),
  193. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  194. ID,owner->highlighted->ID);
  195. }
  196. else //merge
  197. {
  198. LOCPLINT->cb->mergeStacks(
  199. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  200. (!upg)?(owner->oup):(owner->odown),
  201. owner->highlighted->ID,ID);
  202. }
  203. }
  204. else //highlight
  205. {
  206. if(creature)
  207. owner->highlighted = this;
  208. show();
  209. refr = true;
  210. }
  211. if(refr) {hover(false); hover(true); } //to refresh statusbar
  212. }
  213. }
  214. void CGarrisonSlot::activate()
  215. {
  216. if(!active) active=true;
  217. else return;
  218. ClickableL::activate();
  219. ClickableR::activate();
  220. Hoverable::activate();
  221. }
  222. void CGarrisonSlot::deactivate()
  223. {
  224. if(active) active=false;
  225. else return;
  226. ClickableL::deactivate();
  227. ClickableR::deactivate();
  228. Hoverable::deactivate();
  229. }
  230. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CCreature * Creature, int Count)
  231. {
  232. active = false;
  233. upg = Upg;
  234. count = Count;
  235. ID = IID;
  236. creature = Creature;
  237. pos.x = x;
  238. pos.y = y;
  239. pos.w = 58;
  240. pos.h = 64;
  241. owner = Owner;
  242. }
  243. CGarrisonSlot::~CGarrisonSlot()
  244. {
  245. if(active)
  246. deactivate();
  247. }
  248. void CGarrisonSlot::show()
  249. {
  250. if(creature)
  251. {
  252. char* buf = new char[15];
  253. SDL_itoa(count,buf,10);
  254. blitAt(graphics->bigImgs[creature->idNumber],pos);
  255. printTo(buf,pos.x+56,pos.y+62,GEOR16,zwykly);
  256. if(owner->highlighted==this)
  257. blitAt(graphics->bigImgs[-1],pos);
  258. //if(owner->update)
  259. // updateRect(&pos,screen);
  260. delete [] buf;
  261. }
  262. else
  263. {
  264. SDL_Rect jakis1 = genRect(pos.h,pos.w,owner->offx+ID*(pos.w+owner->interx),owner->offy+upg*(pos.h+owner->intery)), jakis2 = pos;
  265. SDL_BlitSurface(owner->sur,&jakis1,screen,&jakis2);
  266. if(owner->splitting)
  267. blitAt(graphics->bigImgs[-1],pos);
  268. //if(owner->update)
  269. // SDL_UpdateRect(screen,pos.x,pos.y,pos.w,pos.h);
  270. }
  271. }
  272. CGarrisonInt::~CGarrisonInt()
  273. {
  274. if(sup)
  275. {
  276. for(int i=0;i<sup->size();i++)
  277. delete (*sup)[i];
  278. delete sup;
  279. }
  280. if(sdown)
  281. {
  282. for(int i=0;i<sdown->size();i++)
  283. delete (*sdown)[i];
  284. delete sdown;
  285. }
  286. }
  287. void CGarrisonInt::show()
  288. {
  289. if(sup)
  290. {
  291. for(int i = 0; i<sup->size(); i++)
  292. if((*sup)[i])
  293. (*sup)[i]->show();
  294. }
  295. if(sdown)
  296. {
  297. for(int i = 0; i<sdown->size(); i++)
  298. if((*sdown)[i])
  299. (*sdown)[i]->show();
  300. }
  301. }
  302. void CGarrisonInt::deactiveteSlots()
  303. {
  304. if(sup)
  305. {
  306. for(int i = 0; i<sup->size(); i++)
  307. {
  308. if((*sup)[i])
  309. {
  310. (*sup)[i]->deactivate();
  311. }
  312. }
  313. }
  314. if(sdown)
  315. {
  316. for(int i = 0; i<sdown->size(); i++)
  317. {
  318. if((*sdown)[i])
  319. {
  320. (*sdown)[i]->deactivate();
  321. }
  322. }
  323. }
  324. }
  325. void CGarrisonInt::activeteSlots()
  326. {
  327. if(sup)
  328. {
  329. for(int i = 0; i<sup->size(); i++)
  330. {
  331. if((*sup)[i])
  332. {
  333. (*sup)[i]->activate();
  334. }
  335. }
  336. }
  337. if(sdown)
  338. {
  339. for(int i = 0; i<sdown->size(); i++)
  340. {
  341. if((*sdown)[i])
  342. {
  343. (*sdown)[i]->activate();
  344. }
  345. }
  346. }
  347. }
  348. void CGarrisonInt::createSlots()
  349. {
  350. if(set1)
  351. {
  352. sup = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  353. for
  354. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set1->slots.begin();
  355. i!=set1->slots.end(); i++)
  356. {
  357. (*sup)[i->first] =
  358. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y,i->first, 0, &CGI->creh->creatures[i->second.first],i->second.second);
  359. }
  360. for(int i=0; i<sup->size(); i++)
  361. if((*sup)[i] == NULL)
  362. (*sup)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y,i,0,NULL, 0);
  363. }
  364. if(set2)
  365. {
  366. sdown = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  367. for
  368. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set2->slots.begin();
  369. i!=set2->slots.end(); i++)
  370. {
  371. (*sdown)[i->first] =
  372. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y + 64 + intery,i->first,1, &CGI->creh->creatures[i->second.first],i->second.second);
  373. }
  374. for(int i=0; i<sup->size(); i++)
  375. if((*sdown)[i] == NULL)
  376. (*sdown)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y + 64 + intery,i,1, NULL, 0);
  377. }
  378. }
  379. void CGarrisonInt::deleteSlots()
  380. {
  381. if(sup)
  382. {
  383. for(int i = 0; i<sup->size(); i++)
  384. {
  385. if((*sup)[i])
  386. {
  387. delete (*sup)[i];
  388. }
  389. }
  390. delete sup;
  391. sup = NULL;
  392. }
  393. if(sdown)
  394. {
  395. for(int i = 0; i<sdown->size(); i++)
  396. {
  397. if((*sdown)[i])
  398. {
  399. delete (*sdown)[i];
  400. }
  401. }
  402. delete sdown;
  403. sdown = NULL;
  404. }
  405. }
  406. void CGarrisonInt::recreateSlots()
  407. {
  408. splitting = false;
  409. highlighted = NULL;
  410. if(active)
  411. {
  412. deactiveteSlots();
  413. }
  414. deleteSlots();
  415. createSlots();
  416. if(active)
  417. {
  418. //ignoreEvent = true;
  419. activeteSlots();
  420. show();
  421. }
  422. }
  423. void CGarrisonInt::splitClick()
  424. {
  425. if(!highlighted)
  426. return;
  427. splitting = !splitting;
  428. show();
  429. }
  430. void CGarrisonInt::splitStacks(int am2)
  431. {
  432. LOCPLINT->cb->splitStack(
  433. (highlighted->upg)?(odown):(oup),
  434. (pb)?(odown):(oup),
  435. highlighted->ID,
  436. p2,
  437. am2);
  438. }
  439. CGarrisonInt::CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *pomsur, int OX, int OY, const CArmedInstance *s1, const CArmedInstance *s2)
  440. :interx(inx),intery(iny),sur(pomsur),highlighted(NULL),sup(NULL),sdown(NULL),oup(s1),odown(s2),
  441. offx(OX),offy(OY)
  442. {
  443. active = false;
  444. splitting = false;
  445. set1 = LOCPLINT->cb->getGarrison(s1);
  446. set2 = LOCPLINT->cb->getGarrison(s2);
  447. ignoreEvent = false;
  448. update = true;
  449. pos.x=(x);
  450. pos.y=(y);
  451. pos.w=(58);
  452. pos.h=(64);
  453. createSlots();
  454. }
  455. void CGarrisonInt::activate()
  456. {
  457. active = true;
  458. if(sup)
  459. {
  460. for(int i = 0; i<sup->size(); i++)
  461. if((*sup)[i])
  462. (*sup)[i]->activate();
  463. }
  464. if(sdown)
  465. {
  466. for(int i = 0; i<sdown->size(); i++)
  467. if((*sdown)[i])
  468. (*sdown)[i]->activate();
  469. }
  470. }
  471. void CGarrisonInt::deactivate()
  472. {
  473. active = false;
  474. deactiveteSlots();
  475. }
  476. CInfoWindow::CInfoWindow(std::string text, int player, int charperline, const std::vector<SComponent*> &comps, std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons)
  477. {
  478. for(int i=0;i<Buttons.size();i++)
  479. {
  480. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  481. if(!Buttons[i].second) //if no function, then by default we'll set it to close
  482. buttons[i]->callback += boost::bind(&CInfoWindow::close,this);
  483. }
  484. for(int i=0;i<comps.size();i++)
  485. {
  486. components.push_back(comps[i]);
  487. }
  488. CMessage::drawIWindow(this,text,player,charperline);
  489. }
  490. CInfoWindow::CInfoWindow()
  491. {
  492. }
  493. void CInfoWindow::close()
  494. {
  495. deactivate();
  496. delete this;
  497. LOCPLINT->showingDialog->setn(false);
  498. }
  499. void CInfoWindow::show(SDL_Surface * to)
  500. {
  501. CSimpleWindow::show();
  502. for(int i=0;i<buttons.size();i++)
  503. buttons[i]->show();
  504. }
  505. CInfoWindow::~CInfoWindow()
  506. {
  507. if(delComps)
  508. {
  509. for (int i=0;i<components.size();i++)
  510. delete components[i];
  511. }
  512. for(int i=0;i<buttons.size();i++)
  513. delete buttons[i];
  514. }
  515. void CInfoWindow::activate()
  516. {
  517. for (int i=0;i<components.size();i++)
  518. components[i]->activate();
  519. for(int i=0;i<buttons.size();i++)
  520. buttons[i]->activate();
  521. }
  522. void CInfoWindow::deactivate()
  523. {
  524. for (int i=0;i<components.size();i++)
  525. components[i]->deactivate();
  526. for(int i=0;i<buttons.size();i++)
  527. buttons[i]->deactivate();
  528. LOCPLINT->removeObjToBlit(this);
  529. }
  530. void CRClickPopup::clickRight (tribool down)
  531. {
  532. if(down)
  533. return;
  534. close();
  535. }
  536. void CRClickPopup::activate()
  537. {
  538. ClickableR::activate();
  539. LOCPLINT->objsToBlit.push_back(this);
  540. }
  541. void CRClickPopup::deactivate()
  542. {
  543. ClickableR::deactivate();
  544. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  545. }
  546. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  547. :bitmap(Bitmap),free(Free)
  548. {
  549. pos.x = x;
  550. pos.y = y;
  551. pos.h = bitmap->h;
  552. pos.w = bitmap->w;
  553. }
  554. void CInfoPopup::close()
  555. {
  556. deactivate();
  557. if(free)
  558. SDL_FreeSurface(bitmap);
  559. delete this;
  560. if(LOCPLINT->curint == LOCPLINT->adventureInt)
  561. LOCPLINT->adventureInt->show();
  562. else if((LOCPLINT->curint == LOCPLINT->castleInt) && !LOCPLINT->castleInt->subInt)
  563. LOCPLINT->castleInt->showAll();
  564. }
  565. void CInfoPopup::show(SDL_Surface * to)
  566. {
  567. blitAt(bitmap,pos.x,pos.y,(to)?(to):(screen));
  568. }
  569. void SComponent::init(Etype Type, int Subtype, int Val)
  570. {
  571. std::ostringstream oss;
  572. switch (Type)
  573. {
  574. case artifact:
  575. description = CGI->arth->artifacts[Subtype].description;
  576. subtitle = CGI->arth->artifacts[Subtype].name;
  577. break;
  578. case primskill:
  579. description = CGI->generaltexth->arraytxt[2+Subtype];
  580. oss << ((Val>0)?("+"):("-")) << Val << " " << CGI->heroh->pskillsn[Subtype];
  581. subtitle = oss.str();
  582. break;
  583. case secskill44:
  584. subtitle += CGI->abilh->levels[Val] + " " + CGI->abilh->abilities[Subtype]->name;
  585. description = CGI->abilh->abilities[Subtype]->infoTexts[Val];
  586. break;
  587. case resource:
  588. description = CGI->generaltexth->allTexts[242];
  589. oss << Val;
  590. subtitle = oss.str();
  591. break;
  592. case spell:
  593. description = CGI->spellh->spells[Subtype].descriptions[Val];
  594. subtitle = CGI->spellh->spells[Subtype].name;
  595. break;
  596. case experience:
  597. description = CGI->generaltexth->allTexts[241];
  598. oss << Val ;
  599. if(Subtype && Val==1)
  600. {
  601. subtitle = CGI->generaltexth->allTexts[442];
  602. }
  603. else
  604. {
  605. subtitle = oss.str();
  606. }
  607. break;
  608. }
  609. type = Type;
  610. subtype = Subtype;
  611. val = Val;
  612. SDL_Surface * temp = this->getImg();
  613. pos.w = temp->w;
  614. pos.h = temp->h;
  615. }
  616. SComponent::SComponent(Etype Type, int Subtype, int Val)
  617. {
  618. init(Type,Subtype,Val);
  619. }
  620. SComponent::SComponent(const Component &c)
  621. {
  622. if(c.id==5)
  623. init(experience,c.subtype,c.val);
  624. else
  625. init((Etype)c.id,c.subtype,c.val);
  626. if(c.id==2 && c.when==-1)
  627. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  628. }
  629. void SComponent::show(SDL_Surface * to)
  630. {
  631. blitAt(getImg(),pos.x,pos.y,to);
  632. }
  633. SDL_Surface * SComponent::getImg()
  634. {
  635. switch (type)
  636. {
  637. case artifact:
  638. return graphics->artDefs->ourImages[subtype].bitmap;
  639. break;
  640. case primskill:
  641. return graphics->pskillsb->ourImages[subtype].bitmap;
  642. break;
  643. case secskill44:
  644. return CGI->abilh->abils44->ourImages[subtype*3 + 3 + val].bitmap;
  645. break;
  646. case secskill:
  647. return CGI->abilh->abils82->ourImages[subtype*3 + 3 + val].bitmap;
  648. break;
  649. case resource:
  650. return graphics->resources->ourImages[subtype].bitmap;
  651. break;
  652. case experience:
  653. return graphics->pskillsb->ourImages[4].bitmap;
  654. break;
  655. }
  656. return NULL;
  657. }
  658. void SComponent::clickRight (tribool down)
  659. {
  660. LOCPLINT->adventureInt->handleRightClick(description,down,this);
  661. }
  662. void SComponent::activate()
  663. {
  664. ClickableR::activate();
  665. }
  666. void SComponent::deactivate()
  667. {
  668. ClickableR::deactivate();
  669. }
  670. void CSelectableComponent::clickLeft(tribool down)
  671. {
  672. if (down)
  673. {
  674. if(onSelect)
  675. onSelect();
  676. }
  677. }
  678. void CSelectableComponent::init(SDL_Surface * Border)
  679. {
  680. SDL_Surface * symb = SComponent::getImg();
  681. myBitmap = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  682. SDL_SetColorKey(myBitmap,SDL_SRCCOLORKEY,SDL_MapRGB(myBitmap->format,0,255,255));
  683. blitAt(symb,1,1,myBitmap);
  684. if (Border) //use custom border
  685. {
  686. border = Border;
  687. customB = true;
  688. }
  689. else //we need to draw border
  690. {
  691. customB = false;
  692. border = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  693. SDL_FillRect(border,NULL,0x00FFFF);
  694. for (int i=0;i<border->w;i++)
  695. {
  696. SDL_PutPixel(border,i,0,239,215,123);
  697. SDL_PutPixel(border,i,(border->h)-1,239,215,123);
  698. }
  699. for (int i=0;i<border->h;i++)
  700. {
  701. SDL_PutPixel(border,0,i,239,215,123);
  702. SDL_PutPixel(border,(border->w)-1,i,239,215,123);
  703. }
  704. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  705. }
  706. selected = false;
  707. }
  708. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect, SDL_Surface * Border)
  709. :SComponent(c),onSelect(OnSelect)
  710. {
  711. init(Border);
  712. }
  713. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect, SDL_Surface * Border)
  714. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  715. {
  716. init(Border);
  717. }
  718. CSelectableComponent::~CSelectableComponent()
  719. {
  720. SDL_FreeSurface(myBitmap);
  721. if (!customB)
  722. SDL_FreeSurface(border);
  723. }
  724. void CSelectableComponent::activate()
  725. {
  726. SComponent::activate();
  727. ClickableL::activate();
  728. }
  729. void CSelectableComponent::deactivate()
  730. {
  731. SComponent::deactivate();
  732. ClickableL::deactivate();
  733. }
  734. SDL_Surface * CSelectableComponent::getImg()
  735. {
  736. return myBitmap;
  737. }
  738. void CSelectableComponent::select(bool on)
  739. {
  740. if(on != selected)
  741. {
  742. SDL_FillRect(myBitmap,NULL,0x000000);
  743. blitAt(SComponent::getImg(),1,1,myBitmap);
  744. if (on)
  745. {
  746. blitAt(border,0,0,myBitmap);
  747. }
  748. selected = on;
  749. return;
  750. }
  751. else
  752. {
  753. return;
  754. }
  755. }
  756. void CSelectableComponent::show(SDL_Surface * to)
  757. {
  758. blitAt(myBitmap,pos.x,pos.y,to);
  759. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+14,GEOR13,12,zwykly,to);
  760. }
  761. void CSimpleWindow::show(SDL_Surface * to)
  762. {
  763. if(!to)
  764. to=screen;
  765. blitAt(bitmap,pos.x,pos.y,to);
  766. }
  767. CSimpleWindow::~CSimpleWindow()
  768. {
  769. if (bitmap)
  770. {
  771. SDL_FreeSurface(bitmap);
  772. bitmap=NULL;
  773. }
  774. }
  775. void CSelWindow::selectionChange(unsigned to)
  776. {
  777. for (unsigned i=0;i<components.size();i++)
  778. {
  779. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  780. if (!pom)
  781. continue;
  782. pom->select(i==to);
  783. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  784. }
  785. }
  786. CSelWindow::CSelWindow(std::string text, int player, int charperline, std::vector<CSelectableComponent*> &comps, std::vector<std::pair<std::string,boost::function<void()> > > &Buttons)
  787. {
  788. for(int i=0;i<Buttons.size();i++)
  789. {
  790. buttons.push_back(new AdventureMapButton("","",(Buttons[i].second)?(Buttons[i].second):(boost::bind(&CInfoWindow::close,this)),0,0,Buttons[i].first));
  791. }
  792. for(int i=0;i<comps.size();i++)
  793. {
  794. components.push_back(comps[i]);
  795. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  796. }
  797. CMessage::drawIWindow(this,text,player,charperline);
  798. }
  799. void CSelWindow::close()
  800. {
  801. deactivate();
  802. int ret = -1;
  803. for (int i=0;i<components.size();i++)
  804. {
  805. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  806. {
  807. ret = i;
  808. }
  809. }
  810. LOCPLINT->curint->activate();
  811. LOCPLINT->cb->selectionMade(ret,ID);
  812. delete this;
  813. //call owner with selection result
  814. }
  815. CButtonBase::CButtonBase()
  816. {
  817. bitmapOffset = 0;
  818. curimg=0;
  819. type=-1;
  820. abs=false;
  821. active=false;
  822. notFreeButton = false;
  823. ourObj=NULL;
  824. state=0;
  825. }
  826. CButtonBase::~CButtonBase()
  827. {
  828. if(notFreeButton)
  829. return;
  830. for(int i =0; i<imgs.size();i++)
  831. for(int j=0;j<imgs[i].size();j++)
  832. SDL_FreeSurface(imgs[i][j]);
  833. }
  834. void CButtonBase::show(SDL_Surface * to)
  835. {
  836. if(!to)
  837. to=screen;
  838. if (abs)
  839. {
  840. blitAt(imgs[curimg]
  841. [( (state+bitmapOffset) < (imgs[curimg].size()) ) ?
  842. (state+bitmapOffset) :
  843. (imgs[curimg].size()-1) ]
  844. ,pos.x,pos.y,to);
  845. //updateRect(&pos,to);
  846. }
  847. else
  848. {
  849. blitAt(imgs[curimg]
  850. [( (state+bitmapOffset) < (imgs[curimg].size()) ) ?
  851. (state+bitmapOffset) :
  852. (imgs[curimg].size()-1) ],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y,to);
  853. //updateRect(&genRect(pos.h,pos.w,pos.x+ourObj->pos.x,pos.y+ourObj->pos.y),to);
  854. }
  855. }
  856. ClickableL::ClickableL()
  857. {
  858. pressedL=false;
  859. }
  860. void ClickableL::clickLeft(tribool down)
  861. {
  862. if (down)
  863. pressedL=true;
  864. else
  865. pressedL=false;
  866. }
  867. void ClickableL::activate()
  868. {
  869. LOCPLINT->lclickable.push_back(this);
  870. }
  871. void ClickableL::deactivate()
  872. {
  873. LOCPLINT->lclickable.erase
  874. (std::find(LOCPLINT->lclickable.begin(),LOCPLINT->lclickable.end(),this));
  875. }
  876. ClickableR::ClickableR()
  877. {
  878. pressedR=false;
  879. }
  880. void ClickableR::clickRight(tribool down)
  881. {
  882. if (down)
  883. pressedR=true;
  884. else
  885. pressedR=false;
  886. }
  887. void ClickableR::activate()
  888. {
  889. LOCPLINT->rclickable.push_back(this);
  890. }
  891. void ClickableR::deactivate()
  892. {
  893. LOCPLINT->rclickable.erase(std::find(LOCPLINT->rclickable.begin(),LOCPLINT->rclickable.end(),this));
  894. }
  895. void Hoverable::activate()
  896. {
  897. LOCPLINT->hoverable.push_back(this);
  898. }
  899. void Hoverable::deactivate()
  900. {
  901. LOCPLINT->hoverable.erase(std::find(LOCPLINT->hoverable.begin(),LOCPLINT->hoverable.end(),this));
  902. }
  903. void Hoverable::hover(bool on)
  904. {
  905. hovered=on;
  906. }
  907. void KeyInterested::activate()
  908. {
  909. LOCPLINT->keyinterested.push_back(this);
  910. }
  911. void KeyInterested::deactivate()
  912. {
  913. LOCPLINT->
  914. keyinterested.erase(std::find(LOCPLINT->keyinterested.begin(),LOCPLINT->keyinterested.end(),this));
  915. }
  916. void MotionInterested::activate()
  917. {
  918. LOCPLINT->motioninterested.push_back(this);
  919. }
  920. void MotionInterested::deactivate()
  921. {
  922. LOCPLINT->
  923. motioninterested.erase(std::find(LOCPLINT->motioninterested.begin(),LOCPLINT->motioninterested.end(),this));
  924. }
  925. void TimeInterested::activate()
  926. {
  927. LOCPLINT->timeinterested.push_back(this);
  928. }
  929. void TimeInterested::deactivate()
  930. {
  931. LOCPLINT->
  932. timeinterested.erase(std::find(LOCPLINT->timeinterested.begin(),LOCPLINT->timeinterested.end(),this));
  933. }
  934. CPlayerInterface::CPlayerInterface(int Player, int serial)
  935. {
  936. LOCPLINT = this;
  937. playerID=Player;
  938. serialID=serial;
  939. human=true;
  940. pim = new boost::mutex;
  941. showingDialog = new CondSh<bool>(false);
  942. }
  943. CPlayerInterface::~CPlayerInterface()
  944. {
  945. delete pim;
  946. delete showingDialog;
  947. }
  948. void CPlayerInterface::init(ICallback * CB)
  949. {
  950. cb = dynamic_cast<CCallback*>(CB);
  951. adventureInt = new CAdvMapInt(playerID);
  952. castleInt = NULL;
  953. std::vector <const CGHeroInstance *> hh = cb->getHeroesInfo(false);
  954. for(int i=0;i<hh.size();i++)
  955. {
  956. SDL_Surface * pom = infoWin(hh[i]);
  957. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hh[i]->subID,pom));
  958. }
  959. std::vector<const CGTownInstance*> tt = cb->getTownsInfo(false);
  960. for(int i=0;i<tt.size();i++)
  961. {
  962. SDL_Surface * pom = infoWin(tt[i]);
  963. graphics->townWins.insert(std::pair<int,SDL_Surface*>(tt[i]->id,pom));
  964. }
  965. }
  966. void CPlayerInterface::yourTurn()
  967. {
  968. LOCPLINT = this;
  969. makingTurn = true;
  970. adventureInt->infoBar.newDay(cb->getDate(1));
  971. if(adventureInt->heroList.items.size())
  972. adventureInt->select(adventureInt->heroList.items[0].first);
  973. else
  974. adventureInt->select(adventureInt->townList.items[0]);
  975. adventureInt->activate();
  976. //show rest of things
  977. //initializing framerate keeper
  978. mainFPSmng = new FPSmanager;
  979. SDL_initFramerate(mainFPSmng);
  980. SDL_setFramerate(mainFPSmng, 48);
  981. //framerate keeper initialized
  982. timeHandler th;
  983. th.getDif();
  984. for(;makingTurn;) // main loop
  985. {
  986. updateWater();
  987. pim->lock();
  988. int tv = th.getDif();
  989. for (int i=0;i<timeinterested.size();i++)
  990. {
  991. if (timeinterested[i]->toNextTick>=0)
  992. timeinterested[i]->toNextTick-=tv;
  993. if (timeinterested[i]->toNextTick<0)
  994. timeinterested[i]->tick();
  995. }
  996. LOCPLINT->adventureInt->updateScreen = false;
  997. eventsM.lock();
  998. while(!events.empty())
  999. {
  1000. handleEvent(&events.front());
  1001. events.pop();
  1002. }
  1003. eventsM.unlock();
  1004. if (curint == adventureInt) //stuff for advMapInt
  1005. {
  1006. adventureInt->update();
  1007. }
  1008. for(int i=0;i<objsToBlit.size();i++)
  1009. objsToBlit[i]->show();
  1010. CGI->curh->draw1();
  1011. CSDL_Ext::update(screen);
  1012. CGI->curh->draw2();
  1013. pim->unlock();
  1014. SDL_framerateDelay(mainFPSmng);
  1015. }
  1016. adventureInt->hide();
  1017. cb->endTurn();
  1018. }
  1019. inline void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  1020. {
  1021. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  1022. for(int h=0; h<hlp.objects.size(); ++h)
  1023. if(hlp.objects[h].first->id==hid)
  1024. {
  1025. hlp.objects[h].second = r;
  1026. return;
  1027. }
  1028. }
  1029. inline void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  1030. {
  1031. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  1032. for(int h=0; h<hlp.objects.size(); ++h)
  1033. if(hlp.objects[h].first->id==hid)
  1034. {
  1035. hlp.objects.erase(hlp.objects.begin()+h);
  1036. return;
  1037. }
  1038. }
  1039. int getDir(int3 src, int3 dst)
  1040. {
  1041. int ret = -1;
  1042. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  1043. {
  1044. ret = 1;
  1045. }
  1046. else if(dst.x == src.x && dst.y+1 == src.y) //t
  1047. {
  1048. ret = 2;
  1049. }
  1050. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  1051. {
  1052. ret = 3;
  1053. }
  1054. else if(dst.x-1 == src.x && dst.y == src.y) //r
  1055. {
  1056. ret = 4;
  1057. }
  1058. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  1059. {
  1060. ret = 5;
  1061. }
  1062. else if(dst.x == src.x && dst.y-1 == src.y) //b
  1063. {
  1064. ret = 6;
  1065. }
  1066. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  1067. {
  1068. ret = 7;
  1069. }
  1070. else if(dst.x+1 == src.x && dst.y == src.y) //l
  1071. {
  1072. ret = 8;
  1073. }
  1074. return ret;
  1075. }
  1076. void CPlayerInterface::heroMoved(const HeroMoveDetails & details)
  1077. {
  1078. boost::unique_lock<boost::mutex> un(*pim);
  1079. //initializing objects and performing first step of move
  1080. const CGHeroInstance * ho = details.ho; //object representing this hero
  1081. int3 hp = details.src;
  1082. if (!details.successful)
  1083. {
  1084. ho->moveDir = getDir(details.src,details.dst);
  1085. ho->isStanding = true;
  1086. adventureInt->heroList.draw();
  1087. if (adventureInt->terrain.currentPath && ho->movement>145) //TODO: better condition on movement - check cost of endtile
  1088. {
  1089. delete adventureInt->terrain.currentPath;
  1090. adventureInt->terrain.currentPath = NULL;
  1091. adventureInt->heroList.items[adventureInt->heroList.getPosOfHero(ho)].second = NULL;
  1092. }
  1093. return;
  1094. }
  1095. if (adventureInt->terrain.currentPath) //&& hero is moving
  1096. {
  1097. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  1098. }
  1099. int3 buff = details.ho->pos;
  1100. buff.x-=11;
  1101. buff.y-=9;
  1102. buff = repairScreenPos(buff);
  1103. LOCPLINT->adventureInt->position = buff; //actualizing screen pos
  1104. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1105. {
  1106. ho->moveDir = 1;
  1107. ho->isStanding = false;
  1108. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1109. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1110. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1111. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1112. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1113. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1114. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1115. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1116. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1117. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1118. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1119. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1120. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1121. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1122. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1123. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1124. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1125. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1126. }
  1127. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1128. {
  1129. ho->moveDir = 2;
  1130. ho->isStanding = false;
  1131. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1132. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1133. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1134. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1135. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1136. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1137. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1138. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1139. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1140. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1141. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1142. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1143. }
  1144. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1145. {
  1146. ho->moveDir = 3;
  1147. ho->isStanding = false;
  1148. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1149. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1150. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1151. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1152. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1153. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1154. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1155. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1156. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1157. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1158. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1159. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1160. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1161. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1162. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1163. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.end(), ocmptwo_cgin);
  1164. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1165. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1166. }
  1167. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1168. {
  1169. ho->moveDir = 4;
  1170. ho->isStanding = false;
  1171. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1172. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1173. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1174. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1175. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1176. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1177. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1178. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1179. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1180. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1181. }
  1182. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1183. {
  1184. ho->moveDir = 5;
  1185. ho->isStanding = false;
  1186. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1187. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1188. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1189. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1190. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1191. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1192. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1193. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1194. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1195. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1196. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1197. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1198. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1199. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1200. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1201. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1202. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1203. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1204. }
  1205. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1206. {
  1207. ho->moveDir = 6;
  1208. ho->isStanding = false;
  1209. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1210. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1211. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1212. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1213. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1214. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1215. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1216. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1217. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1218. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1219. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1220. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1221. }
  1222. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1223. {
  1224. ho->moveDir = 7;
  1225. ho->isStanding = false;
  1226. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1227. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1228. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1229. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1230. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1231. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1232. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1233. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1234. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1235. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1236. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1237. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1238. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1239. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1240. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1241. std::stable_sort(CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1242. std::stable_sort(CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1243. std::stable_sort(CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.end(), ocmptwo_cgin);
  1244. }
  1245. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1246. {
  1247. ho->moveDir = 8;
  1248. ho->isStanding = false;
  1249. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1250. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1251. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1252. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1253. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1254. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1255. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1256. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1257. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1258. std::stable_sort(CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1259. }
  1260. //first initializing done
  1261. SDL_framerateDelay(mainFPSmng); // after first move
  1262. //main moving
  1263. for(int i=1; i<32; i+=4)
  1264. {
  1265. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1266. {
  1267. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1268. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1269. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1270. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1271. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1272. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1273. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1274. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1275. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1276. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1277. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1278. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1279. }
  1280. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1281. {
  1282. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1283. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1284. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1285. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1286. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1287. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1288. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1289. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1290. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1291. }
  1292. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1293. {
  1294. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1295. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1296. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1297. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1298. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1299. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1300. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1301. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1302. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1303. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1304. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1305. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1306. }
  1307. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1308. {
  1309. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1310. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1311. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1312. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1313. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1314. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1315. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1316. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1317. }
  1318. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1319. {
  1320. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1321. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1322. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1323. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1324. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1325. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1326. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1327. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1328. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1329. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1330. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1331. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1332. }
  1333. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1334. {
  1335. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1336. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1337. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1338. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1339. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1340. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1341. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1342. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1343. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1344. }
  1345. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1346. {
  1347. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1348. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1349. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1350. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1351. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1352. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1353. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1354. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1355. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1356. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1357. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1358. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1359. }
  1360. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1361. {
  1362. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1363. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1364. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1365. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1366. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1367. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1368. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1369. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1370. }
  1371. adventureInt->updateScreen = true;
  1372. LOCPLINT->adventureInt->update(); //updating screen
  1373. CSDL_Ext::update(screen);
  1374. //CGI->screenh->updateScreen();
  1375. ++LOCPLINT->adventureInt->animValHitCount; //for animations
  1376. if(LOCPLINT->adventureInt->animValHitCount == 8)
  1377. {
  1378. LOCPLINT->adventureInt->animValHitCount = 0;
  1379. ++LOCPLINT->adventureInt->anim;
  1380. LOCPLINT->adventureInt->updateScreen = true;
  1381. }
  1382. ++LOCPLINT->adventureInt->heroAnim;
  1383. SDL_Delay(5);
  1384. SDL_framerateDelay(mainFPSmng); //for animation purposes
  1385. } //for(int i=1; i<32; i+=4)
  1386. //main moving done
  1387. //finishing move
  1388. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1389. {
  1390. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1391. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1392. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1393. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1394. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1395. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1396. }
  1397. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1398. {
  1399. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1400. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1401. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1402. }
  1403. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1404. {
  1405. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1406. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1407. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1408. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1409. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1410. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1411. }
  1412. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1413. {
  1414. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1415. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1416. }
  1417. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1418. {
  1419. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1420. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1421. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1422. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1423. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1424. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1425. }
  1426. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1427. {
  1428. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1429. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1430. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1431. }
  1432. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1433. {
  1434. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1435. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1436. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1437. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1438. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1439. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1440. }
  1441. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1442. {
  1443. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1444. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1445. }
  1446. ho->isStanding = true;
  1447. //move finished
  1448. adventureInt->minimap.draw();
  1449. adventureInt->heroList.updateMove(ho);
  1450. }
  1451. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  1452. {
  1453. boost::unique_lock<boost::mutex> un(*pim);
  1454. graphics->heroWins.erase(hero->ID);
  1455. adventureInt->heroList.updateHList();
  1456. }
  1457. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  1458. {
  1459. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  1460. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  1461. }
  1462. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  1463. {
  1464. adventureInt->hide();
  1465. //timeHandler t;
  1466. //t.getDif();
  1467. castleInt = new CCastleInterface(town,true);
  1468. //std::cout << "Loading town screen: " << t.getDif() <<std::endl;
  1469. }
  1470. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  1471. {
  1472. if (specific)
  1473. {
  1474. switch (specific->ID)
  1475. {
  1476. case 34:
  1477. return graphics->drawHeroInfoWin(dynamic_cast<const CGHeroInstance*>(specific));
  1478. break;
  1479. case 98:
  1480. return graphics->drawTownInfoWin(dynamic_cast<const CGTownInstance*>(specific));
  1481. break;
  1482. default:
  1483. return NULL;
  1484. break;
  1485. }
  1486. }
  1487. else
  1488. {
  1489. switch (adventureInt->selection->ID)
  1490. {
  1491. case HEROI_TYPE:
  1492. {
  1493. const CGHeroInstance * curh = (const CGHeroInstance *)adventureInt->selection;
  1494. return graphics->drawHeroInfoWin(curh);
  1495. }
  1496. case TOWNI_TYPE:
  1497. {
  1498. return graphics->drawTownInfoWin((const CGTownInstance *)adventureInt->selection);
  1499. }
  1500. default:
  1501. return NULL;
  1502. }
  1503. }
  1504. return NULL;
  1505. }
  1506. void CPlayerInterface::handleMouseMotion(SDL_Event *sEvent)
  1507. {
  1508. std::vector<int> hlp;
  1509. for (int i=0; i<hoverable.size();i++)
  1510. {
  1511. if (isItIn(&hoverable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  1512. {
  1513. if (!hoverable[i]->hovered)
  1514. hlp.push_back(i);
  1515. }
  1516. else if (hoverable[i]->hovered)
  1517. {
  1518. hoverable[i]->hover(false);
  1519. }
  1520. }
  1521. for(int i=0; i<hlp.size();i++)
  1522. hoverable[hlp[i]]->hover(true);
  1523. for(int i=0; i<motioninterested.size();i++)
  1524. {
  1525. if (motioninterested[i]->strongInterest || isItIn(&motioninterested[i]->pos,sEvent->motion.x,sEvent->motion.y))
  1526. {
  1527. motioninterested[i]->mouseMoved(sEvent->motion);
  1528. }
  1529. }
  1530. if(sEvent->motion.x<15)
  1531. {
  1532. LOCPLINT->adventureInt->scrollingLeft = true;
  1533. }
  1534. else
  1535. {
  1536. LOCPLINT->adventureInt->scrollingLeft = false;
  1537. }
  1538. if(sEvent->motion.x>screen->w-15)
  1539. {
  1540. LOCPLINT->adventureInt->scrollingRight = true;
  1541. }
  1542. else
  1543. {
  1544. LOCPLINT->adventureInt->scrollingRight = false;
  1545. }
  1546. if(sEvent->motion.y<15)
  1547. {
  1548. LOCPLINT->adventureInt->scrollingUp = true;
  1549. }
  1550. else
  1551. {
  1552. LOCPLINT->adventureInt->scrollingUp = false;
  1553. }
  1554. if(sEvent->motion.y>screen->h-15)
  1555. {
  1556. LOCPLINT->adventureInt->scrollingDown = true;
  1557. }
  1558. else
  1559. {
  1560. LOCPLINT->adventureInt->scrollingDown = false;
  1561. }
  1562. }
  1563. void CPlayerInterface::handleKeyUp(SDL_Event *sEvent)
  1564. {
  1565. switch (sEvent->key.keysym.sym)
  1566. {
  1567. case SDLK_LEFT:
  1568. {
  1569. LOCPLINT->adventureInt->scrollingLeft = false;
  1570. break;
  1571. }
  1572. case (SDLK_RIGHT):
  1573. {
  1574. LOCPLINT->adventureInt->scrollingRight = false;
  1575. break;
  1576. }
  1577. case (SDLK_UP):
  1578. {
  1579. LOCPLINT->adventureInt->scrollingUp = false;
  1580. break;
  1581. }
  1582. case (SDLK_DOWN):
  1583. {
  1584. LOCPLINT->adventureInt->scrollingDown = false;
  1585. break;
  1586. }
  1587. case (SDLK_u):
  1588. {
  1589. adventureInt->underground.clickLeft(false);
  1590. break;
  1591. }
  1592. case (SDLK_m):
  1593. {
  1594. adventureInt->moveHero.clickLeft(false);
  1595. break;
  1596. }
  1597. case (SDLK_e):
  1598. {
  1599. adventureInt->endTurn.clickLeft(false);
  1600. break;
  1601. }
  1602. }
  1603. }
  1604. void CPlayerInterface::handleKeyDown(SDL_Event *sEvent)
  1605. {
  1606. switch (sEvent->key.keysym.sym)
  1607. {
  1608. case SDLK_LEFT:
  1609. {
  1610. LOCPLINT->adventureInt->scrollingLeft = true;
  1611. break;
  1612. }
  1613. case (SDLK_RIGHT):
  1614. {
  1615. LOCPLINT->adventureInt->scrollingRight = true;
  1616. break;
  1617. }
  1618. case (SDLK_UP):
  1619. {
  1620. LOCPLINT->adventureInt->scrollingUp = true;
  1621. break;
  1622. }
  1623. case (SDLK_DOWN):
  1624. {
  1625. LOCPLINT->adventureInt->scrollingDown = true;
  1626. break;
  1627. }
  1628. case (SDLK_q):
  1629. {
  1630. continueReadingConsole = false;
  1631. exit(0);
  1632. break;
  1633. }
  1634. case (SDLK_u):
  1635. {
  1636. adventureInt->underground.clickLeft(true);
  1637. break;
  1638. }
  1639. case (SDLK_m):
  1640. {
  1641. adventureInt->moveHero.clickLeft(true);
  1642. break;
  1643. }
  1644. case (SDLK_e):
  1645. {
  1646. adventureInt->endTurn.clickLeft(true);
  1647. break;
  1648. }
  1649. }
  1650. }
  1651. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  1652. {
  1653. current = sEvent;
  1654. if(sEvent->type == SDL_MOUSEMOTION)
  1655. {
  1656. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  1657. }
  1658. if(sEvent->type==SDL_QUIT)
  1659. exit(0);
  1660. else if (sEvent->type==SDL_KEYDOWN)
  1661. {
  1662. handleKeyDown(sEvent);
  1663. } //keydown end
  1664. else if(sEvent->type==SDL_KEYUP)
  1665. {
  1666. handleKeyUp(sEvent);
  1667. }//keyup end
  1668. else if(sEvent->type==SDL_MOUSEMOTION)
  1669. {
  1670. handleMouseMotion(sEvent);
  1671. } //mousemotion end
  1672. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1673. {
  1674. for(int i=0; i<lclickable.size();i++)
  1675. {
  1676. if (isItIn(&lclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  1677. {
  1678. lclickable[i]->clickLeft(true);
  1679. }
  1680. }
  1681. }
  1682. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1683. {
  1684. for(int i=0; i<lclickable.size();i++)
  1685. {
  1686. if (isItIn(&lclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  1687. {
  1688. lclickable[i]->clickLeft(false);
  1689. }
  1690. else
  1691. lclickable[i]->clickLeft(boost::logic::indeterminate);
  1692. }
  1693. }
  1694. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1695. {
  1696. for(int i=0; i<rclickable.size();i++)
  1697. {
  1698. if (isItIn(&rclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  1699. {
  1700. rclickable[i]->clickRight(true);
  1701. }
  1702. }
  1703. }
  1704. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1705. {
  1706. for(int i=0; i<rclickable.size();i++)
  1707. {
  1708. if (isItIn(&rclickable[i]->pos,sEvent->motion.x,sEvent->motion.y))
  1709. {
  1710. rclickable[i]->clickRight(false);
  1711. }
  1712. else
  1713. rclickable[i]->clickRight(boost::logic::indeterminate);
  1714. }
  1715. }
  1716. current = NULL;
  1717. } //event end
  1718. int3 CPlayerInterface::repairScreenPos(int3 pos)
  1719. {
  1720. if(pos.x<=-Woff)
  1721. pos.x = -Woff+1;
  1722. if(pos.y<=-Hoff)
  1723. pos.y = -Hoff+1;
  1724. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff)
  1725. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff;
  1726. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff)
  1727. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff;
  1728. return pos;
  1729. }
  1730. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val)
  1731. {
  1732. boost::unique_lock<boost::mutex> un(*pim);
  1733. SDL_FreeSurface(graphics->heroWins[hero->subID]);//TODO: moznaby zmieniac jedynie fragment bitmapy zwiazany z dana umiejetnoscia
  1734. graphics->heroWins[hero->subID] = infoWin(hero); //a nie przerysowywac calosc. Troche roboty, obecnie chyba nie wartej swieczki.
  1735. if (adventureInt->selection == hero)
  1736. adventureInt->infoBar.draw();
  1737. return;
  1738. }
  1739. void CPlayerInterface::receivedResource(int type, int val)
  1740. {
  1741. boost::unique_lock<boost::mutex> un(*pim);
  1742. adventureInt->resdatabar.draw();
  1743. }
  1744. void CPlayerInterface::showSelDialog(std::string &text, const std::vector<Component*> &components, ui32 askID)
  1745. //void CPlayerInterface::showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID)
  1746. {
  1747. boost::unique_lock<boost::mutex> un(*pim);
  1748. adventureInt->hide(); //dezaktywacja starego interfejsu
  1749. std::vector<CSelectableComponent*> intComps;
  1750. for(int i=0;i<components.size();i++)
  1751. intComps.push_back(new CSelectableComponent(*components[i])); //will be deleted by CSelWindow::close
  1752. std::vector<std::pair<std::string,boost::function<void()> > > pom;
  1753. pom.push_back(std::pair<std::string,boost::function<void()> >("IOKAY.DEF",0));
  1754. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom);
  1755. LOCPLINT->objsToBlit.push_back(temp);
  1756. temp->activate();
  1757. temp->ID = askID;
  1758. intComps[0]->clickLeft(true);
  1759. }
  1760. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  1761. {
  1762. {
  1763. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1764. while(showingDialog->data)
  1765. showingDialog->cond.wait(un);
  1766. }
  1767. boost::unique_lock<boost::mutex> un(*pim);
  1768. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  1769. curint->deactivate();
  1770. lw->activate();
  1771. }
  1772. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  1773. {
  1774. boost::unique_lock<boost::mutex> un(*pim);
  1775. //redraw infowindow
  1776. SDL_FreeSurface(graphics->townWins[town->id]);
  1777. graphics->townWins[town->id] = infoWin(town);
  1778. if(town->garrisonHero)
  1779. {
  1780. CGI->mh->hideObject(town->garrisonHero);
  1781. }
  1782. if(town->visitingHero)
  1783. {
  1784. CGI->mh->printObject(town->visitingHero);
  1785. }
  1786. adventureInt->heroList.updateHList();
  1787. CCastleInterface *c = dynamic_cast<CCastleInterface*>(curint);
  1788. if(c)
  1789. {
  1790. c->garr->highlighted = NULL;
  1791. c->hslotup.hero = town->garrisonHero;
  1792. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  1793. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  1794. c->garr->recreateSlots();
  1795. c->showAll();
  1796. }
  1797. }
  1798. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  1799. {
  1800. if(hero->tempOwner != town->tempOwner)
  1801. return;
  1802. boost::unique_lock<boost::mutex> un(*pim);
  1803. openTownWindow(town);
  1804. }
  1805. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  1806. {
  1807. boost::unique_lock<boost::mutex> un(*pim);
  1808. if(obj->ID == 34) //hero
  1809. {
  1810. const CGHeroInstance * hh;
  1811. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  1812. {
  1813. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  1814. graphics->heroWins[hh->subID] = infoWin(hh);
  1815. }
  1816. CHeroWindow * hw = dynamic_cast<CHeroWindow *>(curint->subInt);
  1817. if(hw)
  1818. {
  1819. hw->garInt->recreateSlots();
  1820. hw->garInt->show();
  1821. }
  1822. if(castleInt) //opened town window - redraw town garrsion slots (change is within hero garr)
  1823. {
  1824. castleInt->garr->highlighted = NULL;
  1825. castleInt->garr->recreateSlots();
  1826. }
  1827. }
  1828. else if (obj->ID == 98) //town
  1829. {
  1830. const CGTownInstance * tt;
  1831. if(tt = static_cast<const CGTownInstance*>(obj))
  1832. {
  1833. SDL_FreeSurface(graphics->townWins[tt->id]);
  1834. graphics->townWins[tt->id] = infoWin(tt);
  1835. }
  1836. if(tt->visitingHero)
  1837. {
  1838. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  1839. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  1840. }
  1841. if(LOCPLINT->castleInt)
  1842. {
  1843. LOCPLINT->castleInt->garr->highlighted = NULL;
  1844. LOCPLINT->castleInt->garr->recreateSlots();
  1845. }
  1846. }
  1847. }
  1848. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  1849. {
  1850. boost::unique_lock<boost::mutex> un(*pim);
  1851. switch (buildingID)
  1852. {
  1853. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  1854. {
  1855. SDL_FreeSurface(graphics->townWins[town->id]);
  1856. graphics->townWins[town->id] = infoWin(town);
  1857. break;
  1858. }
  1859. }
  1860. if(curint!=castleInt)
  1861. return;
  1862. if(castleInt->town!=town)
  1863. return;
  1864. switch(what)
  1865. {
  1866. case 1:
  1867. castleInt->addBuilding(buildingID);
  1868. break;
  1869. case 2:
  1870. castleInt->removeBuilding(buildingID);
  1871. break;
  1872. }
  1873. }
  1874. void CPlayerInterface::battleStart(CCreatureSet *army1, CCreatureSet *army2, int3 tile, CGHeroInstance *hero1, CGHeroInstance *hero2, bool side) //called by engine when battle starts; side=0 - left, side=1 - right
  1875. {
  1876. boost::unique_lock<boost::mutex> un(*pim);
  1877. curint->deactivate();
  1878. curint = new CBattleInterface(army1,army2,hero1,hero2);
  1879. curint->activate();
  1880. LOCPLINT->objsToBlit.push_back(dynamic_cast<IShowable*>(curint));
  1881. }
  1882. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  1883. {
  1884. }
  1885. void CPlayerInterface::battleNewRound(int round) //called at the beggining of each turn, round=-1 is the tactic phase, round=0 is the first "normal" turn
  1886. {
  1887. dynamic_cast<CBattleInterface*>(curint)->newRound(round);
  1888. }
  1889. //void CPlayerInterface::actionStarted(BattleAction action)//occurs BEFORE every action taken by any stack or by the hero
  1890. //{
  1891. //}
  1892. //
  1893. //void CPlayerInterface::actionFinished(BattleAction action)//occurs AFTER every action taken by any stack or by the hero
  1894. //{
  1895. // //dynamic_cast<CBattleInterface*>(curint)->givenCommand = -1;
  1896. //}
  1897. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  1898. {
  1899. CBattleInterface *b = dynamic_cast<CBattleInterface*>(curint);
  1900. {
  1901. boost::unique_lock<boost::mutex> un(*pim);
  1902. b->stackActivated(stackID);
  1903. }
  1904. //wait till BattleInterface sets its command
  1905. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  1906. while(!b->givenCommand->data)
  1907. b->givenCommand->cond.wait(lock);
  1908. //tidy up
  1909. BattleAction ret = *(b->givenCommand->data);
  1910. delete b->givenCommand->data;
  1911. b->givenCommand->data = NULL;
  1912. //return command
  1913. return ret;
  1914. }
  1915. void CPlayerInterface::battleEnd(BattleResult *br)
  1916. {
  1917. boost::unique_lock<boost::mutex> un(*pim);
  1918. curint->deactivate();
  1919. objsToBlit -= curint;
  1920. delete curint;
  1921. curint = adventureInt;
  1922. adventureInt->activate();
  1923. }
  1924. void CPlayerInterface::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)
  1925. {
  1926. boost::unique_lock<boost::mutex> un(*pim);
  1927. dynamic_cast<CBattleInterface*>(curint)->stackMoved(ID, dest, startMoving, endMoving);
  1928. }
  1929. void CPlayerInterface::battleAttack(BattleAttack *ba)
  1930. {
  1931. boost::unique_lock<boost::mutex> un(*pim);
  1932. if(ba->shot())
  1933. dynamic_cast<CBattleInterface*>(curint)->stackIsShooting(ba->stackAttacking,cb->battleGetPos(ba->bsa.stackAttacked));
  1934. else
  1935. dynamic_cast<CBattleInterface*>(curint)->stackAttacking( ba->stackAttacking, cb->battleGetPos(ba->bsa.stackAttacked) );
  1936. if(ba->killed())
  1937. dynamic_cast<CBattleInterface*>(curint)->stackKilled(ba->bsa.stackAttacked, ba->bsa.damageAmount, ba->bsa.killedAmount, ba->stackAttacking, ba->shot());
  1938. else
  1939. dynamic_cast<CBattleInterface*>(curint)->stackIsAttacked(ba->bsa.stackAttacked, ba->bsa.damageAmount, ba->bsa.killedAmount, ba->stackAttacking, ba->shot());
  1940. }
  1941. void CPlayerInterface::battleStackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  1942. {
  1943. dynamic_cast<CBattleInterface*>(curint)->stackKilled(ID, dmg, killed, IDby, byShooting);
  1944. }
  1945. void CPlayerInterface::battleStackIsShooting(int ID, int dest)
  1946. {
  1947. dynamic_cast<CBattleInterface*>(curint)->stackIsShooting(ID, dest);
  1948. }
  1949. void CPlayerInterface::showComp(SComponent comp)
  1950. {
  1951. boost::unique_lock<boost::mutex> un(*pim);
  1952. adventureInt->infoBar.showComp(&comp,4000);
  1953. }
  1954. void CPlayerInterface::showInfoDialog(std::string &text, const std::vector<Component*> &components)
  1955. {
  1956. std::vector<SComponent*> intComps;
  1957. for(int i=0;i<components.size();i++)
  1958. intComps.push_back(new SComponent(*components[i]));
  1959. showInfoDialog(text,intComps);
  1960. }
  1961. void CPlayerInterface::showInfoDialog(std::string &text, const std::vector<SComponent*> & components)
  1962. {
  1963. showingDialog->set(true);
  1964. curint->deactivate(); //dezaktywacja starego interfejsu
  1965. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1966. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1967. CInfoWindow * temp = new CInfoWindow(text,playerID,32,components,pom);
  1968. temp->buttons[0]->callback += boost::bind(&IActivable::activate,LOCPLINT->curint);
  1969. temp->activate();
  1970. LOCPLINT->objsToBlit.push_back(temp);
  1971. }
  1972. void CPlayerInterface::showYesNoDialog(std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool deactivateCur, bool DelComps)
  1973. {
  1974. if(deactivateCur)
  1975. curint->deactivate(); //dezaktywacja starego interfejsu
  1976. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  1977. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  1978. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  1979. CInfoWindow * temp = new CInfoWindow(text,playerID,32,components,pom);
  1980. temp->delComps = DelComps;
  1981. for(int i=0;i<onYes.funcs.size();i++)
  1982. temp->buttons[0]->callback += onYes.funcs[i];
  1983. for(int i=0;i<onNo.funcs.size();i++)
  1984. temp->buttons[1]->callback += onNo.funcs[i];
  1985. //if(onYes)
  1986. // temp->buttons[0]->callback += boost::bind(&CInfoWindow::close,temp);
  1987. //if(onNo)
  1988. // temp->buttons[1]->callback += boost::bind(&CInfoWindow::close,temp);
  1989. temp->activate();
  1990. LOCPLINT->objsToBlit.push_back(temp);
  1991. }
  1992. void CPlayerInterface::showYesNoDialog( std::string &text, const std::vector<Component*> &components, ui32 askID )
  1993. {
  1994. boost::unique_lock<boost::mutex> un(*pim);
  1995. curint->deactivate(); //dezaktywacja starego interfejsu
  1996. std::vector<SComponent*> intComps;
  1997. for(int i=0;i<components.size();i++)
  1998. intComps.push_back(new SComponent(*components[i])); //will be deleted by CSelWindow::close
  1999. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2000. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2001. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  2002. CInfoWindow * temp = new CInfoWindow(text,playerID,32,intComps,pom);
  2003. temp->buttons[0]->callback += boost::bind(&IActivable::activate,curint);
  2004. temp->buttons[1]->callback += boost::bind(&IActivable::activate,curint);
  2005. temp->buttons[0]->callback += boost::bind(&CCallback::selectionMade,cb,0,askID);
  2006. temp->buttons[1]->callback += boost::bind(&CCallback::selectionMade,cb,1,askID);
  2007. temp->delComps = true;
  2008. temp->activate();
  2009. LOCPLINT->objsToBlit.push_back(temp);
  2010. }
  2011. void CPlayerInterface::removeObjToBlit(IShowable* obj)
  2012. {
  2013. objsToBlit.erase
  2014. (std::find(objsToBlit.begin(),objsToBlit.end(),obj));
  2015. //delete obj;
  2016. }
  2017. void CPlayerInterface::tileRevealed(int3 pos)
  2018. {
  2019. adventureInt->minimap.showTile(pos);
  2020. }
  2021. void CPlayerInterface::tileHidden(int3 pos)
  2022. {
  2023. adventureInt->minimap.hideTile(pos);
  2024. }
  2025. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  2026. {
  2027. adventureInt->heroWindow->setHero(hero);
  2028. this->objsToBlit.push_back(adventureInt->heroWindow);
  2029. curint->deactivate();
  2030. if(curint == castleInt)
  2031. castleInt->subInt = adventureInt->heroWindow;
  2032. adventureInt->heroWindow->quitButton->callback.funcs.clear();
  2033. adventureInt->heroWindow->quitButton->callback += boost::bind(&CHeroWindow::deactivate,adventureInt->heroWindow);
  2034. adventureInt->heroWindow->quitButton->callback += boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  2035. adventureInt->heroWindow->activate();
  2036. }
  2037. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  2038. {
  2039. boost::unique_lock<boost::mutex> un(*pim);
  2040. if(curint->subInt == adventureInt->heroWindow)
  2041. {
  2042. adventureInt->heroWindow->deactivate();
  2043. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  2044. adventureInt->heroWindow->activate();
  2045. }
  2046. }
  2047. void CPlayerInterface::updateWater()
  2048. {
  2049. //updating water tiles
  2050. //int wnumber = -1;
  2051. //for(int s=0; s<CGI->mh->reader->defs.size(); ++s)
  2052. //{
  2053. // if(CGI->mh->reader->defs[s]->defName==std::string("WATRTL.DEF"))
  2054. // {
  2055. // wnumber = s;
  2056. // break;
  2057. // }
  2058. //}
  2059. //if(wnumber>=0)
  2060. //{
  2061. // for(int g=0; g<CGI->mh->reader->defs[wnumber]->ourImages.size(); ++g)
  2062. // {
  2063. // SDL_Color tab[32];
  2064. // for(int i=0; i<32; ++i)
  2065. // {
  2066. // tab[i] = CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap->format->palette->colors[160 + (i+1)%32];
  2067. // }
  2068. // //SDL_SaveBMP(CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap,"t1.bmp");
  2069. // for(int i=0; i<32; ++i)
  2070. // {
  2071. // CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap->format->palette->colors[160 + i] = tab[i];
  2072. // }
  2073. // //SDL_SaveBMP(CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap,"t2.bmp");
  2074. // CSDL_Ext::update(CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap);
  2075. // }
  2076. //}
  2077. //water tiles updated
  2078. //CGI->screenh->updateScreen();
  2079. }
  2080. void CPlayerInterface::availableCreaturesChanged( const CGTownInstance *town )
  2081. {
  2082. boost::unique_lock<boost::mutex> un(*pim);
  2083. if(curint == castleInt)
  2084. {
  2085. CFortScreen *fs = dynamic_cast<CFortScreen*>(curint->subInt);
  2086. if(fs)
  2087. fs->draw(castleInt,false);
  2088. }
  2089. }
  2090. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  2091. {
  2092. bg=BitmapHandler::loadBitmap(name);
  2093. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2094. pos.x=x;
  2095. pos.y=y;
  2096. if(maxw >= 0)
  2097. pos.w = min(bg->w,maxw);
  2098. else
  2099. pos.w=bg->w;
  2100. pos.h=bg->h;
  2101. middlex=(pos.w/2)+x;
  2102. middley=(bg->h/2)+y;
  2103. }
  2104. CStatusBar::~CStatusBar()
  2105. {
  2106. SDL_FreeSurface(bg);
  2107. }
  2108. void CStatusBar::clear()
  2109. {
  2110. current="";
  2111. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2112. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
  2113. }
  2114. void CStatusBar::print(std::string text)
  2115. {
  2116. current=text;
  2117. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2118. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
  2119. printAtMiddle(current,middlex,middley,GEOR13,zwykly);
  2120. }
  2121. void CStatusBar::show()
  2122. {
  2123. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2124. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
  2125. printAtMiddle(current,middlex,middley,GEOR13,zwykly);
  2126. }
  2127. std::string CStatusBar::getCurrent()
  2128. {
  2129. return current;
  2130. }
  2131. void CList::activate()
  2132. {
  2133. ClickableL::activate();
  2134. ClickableR::activate();
  2135. Hoverable::activate();
  2136. KeyInterested::activate();
  2137. MotionInterested::activate();
  2138. };
  2139. void CList::deactivate()
  2140. {
  2141. ClickableL::deactivate();
  2142. ClickableR::deactivate();
  2143. Hoverable::deactivate();
  2144. KeyInterested::deactivate();
  2145. MotionInterested::deactivate();
  2146. };
  2147. void CList::clickLeft(tribool down)
  2148. {
  2149. };
  2150. CList::CList(int Size)
  2151. :SIZE(Size)
  2152. {
  2153. }
  2154. CHeroList::CHeroList(int Size)
  2155. :CList(Size)
  2156. {
  2157. pos = genRect(192,64,609,196);
  2158. arrupp = genRect(16,64,609,196);
  2159. arrdop = genRect(16,64,609,372);
  2160. //32px per hero
  2161. posmobx = 610;
  2162. posmoby = 213;
  2163. posporx = 617;
  2164. pospory = 212;
  2165. posmanx = 666;
  2166. posmany = 213;
  2167. arrup = CDefHandler::giveDef("IAM012.DEF");
  2168. arrdo = CDefHandler::giveDef("IAM013.DEF");
  2169. mobile = CDefHandler::giveDef("IMOBIL.DEF");
  2170. mana = CDefHandler::giveDef("IMANA.DEF");
  2171. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  2172. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  2173. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  2174. from = 0;
  2175. pressed = indeterminate;
  2176. }
  2177. void CHeroList::init()
  2178. {
  2179. bg = CSDL_Ext::newSurface(68,193,screen);
  2180. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(193,68,607,196),bg,&genRect(193,68,0,0));
  2181. }
  2182. void CHeroList::genList()
  2183. {
  2184. int howMany = LOCPLINT->cb->howManyHeroes();
  2185. for (int i=0;i<howMany;i++)
  2186. {
  2187. const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  2188. if(!h->inTownGarrison)
  2189. items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  2190. }
  2191. }
  2192. void CHeroList::select(int which)
  2193. {
  2194. if (which<0)
  2195. {
  2196. selected = which;
  2197. LOCPLINT->adventureInt->selection = NULL;
  2198. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  2199. draw();
  2200. LOCPLINT->adventureInt->infoBar.draw(NULL);
  2201. }
  2202. if (which>=items.size())
  2203. return;
  2204. selected = which;
  2205. LOCPLINT->adventureInt->centerOn(items[which].first->pos);
  2206. LOCPLINT->adventureInt->selection = items[which].first;
  2207. LOCPLINT->adventureInt->terrain.currentPath = items[which].second;
  2208. draw();
  2209. LOCPLINT->adventureInt->townList.draw();
  2210. LOCPLINT->adventureInt->infoBar.draw(NULL);
  2211. }
  2212. void CHeroList::clickLeft(tribool down)
  2213. {
  2214. if (down)
  2215. {
  2216. /***************************ARROWS*****************************************/
  2217. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2218. {
  2219. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2220. pressed = true;
  2221. return;
  2222. }
  2223. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2224. {
  2225. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2226. pressed = false;
  2227. return;
  2228. }
  2229. /***************************HEROES*****************************************/
  2230. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2231. hx-=pos.x;
  2232. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2233. int ny = hy/32;
  2234. if (ny>=5 || ny<0)
  2235. return;
  2236. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2237. LOCPLINT->openHeroWindow(items[selected].first);//print hero screen
  2238. select(ny+from);
  2239. }
  2240. else
  2241. {
  2242. if (indeterminate(pressed))
  2243. return;
  2244. if (pressed) //up
  2245. {
  2246. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2247. pressed = indeterminate;
  2248. if (!down)
  2249. {
  2250. from--;
  2251. if (from<0)
  2252. from=0;
  2253. draw();
  2254. }
  2255. }
  2256. else if (!pressed) //down
  2257. {
  2258. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2259. pressed = indeterminate;
  2260. if (!down)
  2261. {
  2262. from++;
  2263. //if (from<items.size()-5)
  2264. // from=items.size()-5;
  2265. draw();
  2266. }
  2267. }
  2268. else
  2269. throw 0;
  2270. }
  2271. }
  2272. void CHeroList::mouseMoved (SDL_MouseMotionEvent & sEvent)
  2273. {
  2274. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2275. {
  2276. if (from>0)
  2277. LOCPLINT->adventureInt->statusbar.print(CGI->preth->zelp[303].first);
  2278. else
  2279. LOCPLINT->adventureInt->statusbar.clear();
  2280. return;
  2281. }
  2282. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2283. {
  2284. if ((items.size()-from) > 5)
  2285. LOCPLINT->adventureInt->statusbar.print(CGI->preth->zelp[304].first);
  2286. else
  2287. LOCPLINT->adventureInt->statusbar.clear();
  2288. return;
  2289. }
  2290. //if not buttons then heroes
  2291. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2292. hx-=pos.x;
  2293. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2294. int ny = hy/32;
  2295. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2296. {
  2297. LOCPLINT->adventureInt->statusbar.clear();
  2298. return;
  2299. }
  2300. std::vector<std::string> temp;
  2301. temp.push_back(items[from+ny].first->name);
  2302. temp.push_back(items[from+ny].first->type->heroClass->name);
  2303. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  2304. //select(ny+from);
  2305. }
  2306. void CHeroList::clickRight(tribool down)
  2307. {
  2308. if (down)
  2309. {
  2310. /***************************ARROWS*****************************************/
  2311. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2312. {
  2313. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[303].second,down,this);
  2314. }
  2315. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2316. {
  2317. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[304].second,down,this);
  2318. }
  2319. //if not buttons then heroes
  2320. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2321. hx-=pos.x;
  2322. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2323. int ny = hy/32;
  2324. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2325. {
  2326. return;
  2327. }
  2328. //show popup
  2329. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[items[from+ny].first->subID],LOCPLINT->current->motion.x-graphics->heroWins[items[from+ny].first->subID]->w,LOCPLINT->current->motion.y-graphics->heroWins[items[from+ny].first->subID]->h,false);
  2330. ip->activate();
  2331. }
  2332. else
  2333. {
  2334. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[303].second,down,this);
  2335. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[304].second,down,this);
  2336. }
  2337. }
  2338. void CHeroList::hover (bool on)
  2339. {
  2340. }
  2341. void CHeroList::keyPressed (SDL_KeyboardEvent & key)
  2342. {
  2343. }
  2344. void CHeroList::updateHList()
  2345. {
  2346. items.clear();
  2347. genList();
  2348. if(selected>=items.size())
  2349. select(items.size()-1);
  2350. if(items.size()==0)
  2351. LOCPLINT->adventureInt->townList.select(0);
  2352. else
  2353. select(selected);
  2354. }
  2355. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  2356. {
  2357. int ser = LOCPLINT->cb->getHeroSerial(which);
  2358. ser -= from;
  2359. int pom = (which->movement)/100;
  2360. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  2361. }
  2362. void CHeroList::draw()
  2363. {
  2364. for (int iT=0+from;iT<5+from;iT++)
  2365. {
  2366. int i = iT-from;
  2367. if (iT>=items.size())
  2368. {
  2369. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32);
  2370. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32);
  2371. blitAt(empty,posporx,pospory+i*32);
  2372. continue;
  2373. }
  2374. const CGHeroInstance *cur = items[iT].first;
  2375. int pom = cur->movement / 100;
  2376. if (pom>25) pom=25;
  2377. if (pom<0) pom=0;
  2378. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32); //move point
  2379. pom = cur->mana / 5; //bylo: .../10;
  2380. if (pom>25) pom=25;
  2381. if (pom<0) pom=0;
  2382. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32); //mana
  2383. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  2384. blitAt(temp,posporx,pospory+i*32);
  2385. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2386. {
  2387. blitAt(selection,posporx,pospory+i*32);
  2388. }
  2389. //TODO: support for custom portraits
  2390. }
  2391. if (from>0)
  2392. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2393. else
  2394. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y);
  2395. if (items.size()-from>5)
  2396. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2397. else
  2398. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y);
  2399. }
  2400. int CHeroList::getPosOfHero(const CArmedInstance* h)
  2401. {
  2402. return vstd::findPos(items,h,boost::bind(vstd::equal<std::pair<const CGHeroInstance*, CPath *>,const CArmedInstance *,const CGHeroInstance*>,_1,&std::pair<const CGHeroInstance*, CPath *>::first,_2));
  2403. }
  2404. CTownList::~CTownList()
  2405. {
  2406. delete arrup;
  2407. delete arrdo;
  2408. }
  2409. CTownList::CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy)
  2410. :CList(Size)
  2411. {
  2412. pos = *Pos;
  2413. arrup = CDefHandler::giveDef("IAM014.DEF");
  2414. arrdo = CDefHandler::giveDef("IAM015.DEF");
  2415. arrupp.x=arupx;
  2416. arrupp.y=arupy;
  2417. arrupp.w=arrup->ourImages[0].bitmap->w;
  2418. arrupp.h=arrup->ourImages[0].bitmap->h;
  2419. arrdop.x=ardox;
  2420. arrdop.y=ardoy;
  2421. arrdop.w=arrdo->ourImages[0].bitmap->w;
  2422. arrdop.h=arrdo->ourImages[0].bitmap->h;
  2423. posporx = arrdop.x;
  2424. pospory = arrupp.y + arrupp.h;
  2425. pressed = indeterminate;
  2426. from = 0;
  2427. }
  2428. void CTownList::genList()
  2429. {
  2430. int howMany = LOCPLINT->cb->howManyTowns();
  2431. for (int i=0;i<howMany;i++)
  2432. {
  2433. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  2434. }
  2435. }
  2436. void CTownList::select(int which)
  2437. {
  2438. if (which>=items.size())
  2439. return;
  2440. selected = which;
  2441. if(!fun.empty())
  2442. fun();
  2443. }
  2444. void CTownList::mouseMoved (SDL_MouseMotionEvent & sEvent)
  2445. {
  2446. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2447. {
  2448. if (from>0)
  2449. LOCPLINT->statusbar->print(CGI->preth->zelp[306].first);
  2450. else
  2451. LOCPLINT->statusbar->clear();
  2452. return;
  2453. }
  2454. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2455. {
  2456. if ((items.size()-from) > SIZE)
  2457. LOCPLINT->statusbar->print(CGI->preth->zelp[307].first);
  2458. else
  2459. LOCPLINT->statusbar->clear();
  2460. return;
  2461. }
  2462. //if not buttons then towns
  2463. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2464. hx-=pos.x;
  2465. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2466. int ny = hy/32;
  2467. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2468. {
  2469. LOCPLINT->statusbar->clear();
  2470. return;
  2471. };
  2472. LOCPLINT->statusbar->print(items[from+ny]->name);
  2473. }
  2474. void CTownList::clickLeft(tribool down)
  2475. {
  2476. if (down)
  2477. {
  2478. /***************************ARROWS*****************************************/
  2479. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2480. {
  2481. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2482. pressed = true;
  2483. return;
  2484. }
  2485. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2486. {
  2487. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2488. pressed = false;
  2489. return;
  2490. }
  2491. /***************************TOWNS*****************************************/
  2492. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2493. hx-=pos.x;
  2494. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2495. int ny = hy/32;
  2496. if (ny>SIZE || ny<0)
  2497. return;
  2498. if (SIZE==5 && (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  2499. LOCPLINT->openTownWindow(items[selected]);//print town screen
  2500. else
  2501. select(ny+from);
  2502. }
  2503. else
  2504. {
  2505. if (indeterminate(pressed))
  2506. return;
  2507. if (pressed) //up
  2508. {
  2509. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2510. pressed = indeterminate;
  2511. if (!down)
  2512. {
  2513. from--;
  2514. if (from<0)
  2515. from=0;
  2516. draw();
  2517. }
  2518. }
  2519. else if (!pressed) //down
  2520. {
  2521. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2522. pressed = indeterminate;
  2523. if (!down)
  2524. {
  2525. from++;
  2526. //if (from<items.size()-5)
  2527. // from=items.size()-5;
  2528. draw();
  2529. }
  2530. }
  2531. else
  2532. throw 0;
  2533. }
  2534. }
  2535. void CTownList::clickRight(tribool down)
  2536. {
  2537. if (down)
  2538. {
  2539. /***************************ARROWS*****************************************/
  2540. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2541. {
  2542. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[306].second,down,this);
  2543. }
  2544. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2545. {
  2546. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[307].second,down,this);
  2547. }
  2548. //if not buttons then towns
  2549. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2550. hx-=pos.x;
  2551. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2552. int ny = hy/32;
  2553. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2554. {
  2555. return;
  2556. }
  2557. //show popup
  2558. CInfoPopup * ip = new CInfoPopup(graphics->townWins[items[from+ny]->id],LOCPLINT->current->motion.x-graphics->townWins[items[from+ny]->id]->w,LOCPLINT->current->motion.y-graphics->townWins[items[from+ny]->id]->h,false);
  2559. ip->activate();
  2560. }
  2561. else
  2562. {
  2563. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[306].second,down,this);
  2564. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[307].second,down,this);
  2565. }
  2566. }
  2567. void CTownList::hover (bool on)
  2568. {
  2569. }
  2570. void CTownList::keyPressed (SDL_KeyboardEvent & key)
  2571. {
  2572. }
  2573. void CTownList::draw()
  2574. {
  2575. for (int iT=0+from;iT<SIZE+from;iT++)
  2576. {
  2577. int i = iT-from;
  2578. if (iT>=items.size())
  2579. {
  2580. blitAt(graphics->getPic(-1),posporx,pospory+i*32);
  2581. continue;
  2582. }
  2583. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32);
  2584. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  2585. {
  2586. blitAt(graphics->getPic(-2),posporx,pospory+i*32);
  2587. }
  2588. }
  2589. if (from>0)
  2590. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2591. else
  2592. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y);
  2593. if (items.size()-from>SIZE)
  2594. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2595. else
  2596. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y);
  2597. }
  2598. CCreaturePic::CCreaturePic(CCreature *cre, bool Big)
  2599. :c(cre),big(Big)
  2600. {
  2601. anim = new CCreatureAnimation(cre->animDefName);
  2602. }
  2603. CCreaturePic::~CCreaturePic()
  2604. {
  2605. delete anim;
  2606. }
  2607. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  2608. {
  2609. SDL_Rect dst;
  2610. if(big)
  2611. {
  2612. blitAt(graphics->backgrounds[c->faction],x,y);//curx-50,pos.y+130-65);
  2613. dst = genRect(130,100,x,y);
  2614. }
  2615. else
  2616. {
  2617. blitAt(graphics->backgroundsm[c->faction],x,y);//curx-50,pos.y+130-65);
  2618. dst = genRect(120,100,x,y);
  2619. }
  2620. if(c->isDoubleWide())
  2621. x-=15;
  2622. return anim->nextFrameMiddle(to,x+70,y+45,true,nextFrame,false,&dst);
  2623. }
  2624. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  2625. {
  2626. //TODO: write
  2627. return NULL;
  2628. }
  2629. void CRecrutationWindow::close()
  2630. {
  2631. deactivate();
  2632. delete this;
  2633. LOCPLINT->curint->activate();
  2634. }
  2635. void CRecrutationWindow::Max()
  2636. {
  2637. slider->moveTo(slider->amount);
  2638. }
  2639. void CRecrutationWindow::Buy()
  2640. {
  2641. recruit(creatures[which].ID,slider->value);
  2642. close();
  2643. }
  2644. void CRecrutationWindow::Cancel()
  2645. {
  2646. close();
  2647. }
  2648. void CRecrutationWindow::sliderMoved(int to)
  2649. {
  2650. }
  2651. void CRecrutationWindow::clickLeft(tribool down)
  2652. {
  2653. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2654. for(int i=0;i<creatures.size();i++)
  2655. {
  2656. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2657. {
  2658. which = i;
  2659. int newAmount = min(amounts[i],creatures[i].amount);
  2660. slider->amount = newAmount;
  2661. if(slider->value > newAmount)
  2662. slider->moveTo(newAmount);
  2663. else
  2664. slider->moveTo(slider->value);
  2665. curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2666. for(int j=0;j<creatures.size();j++)
  2667. {
  2668. if(which==j)
  2669. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  2670. else
  2671. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  2672. curx += 120;
  2673. }
  2674. break;
  2675. }
  2676. curx += 120;
  2677. }
  2678. }
  2679. void CRecrutationWindow::activate()
  2680. {
  2681. LOCPLINT->objsToBlit.push_back(this);
  2682. ClickableL::activate();
  2683. buy->activate();
  2684. max->activate();
  2685. cancel->activate();
  2686. slider->activate();
  2687. }
  2688. void CRecrutationWindow::deactivate()
  2689. {
  2690. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  2691. ClickableL::deactivate();
  2692. buy->deactivate();
  2693. max->deactivate();
  2694. cancel->deactivate();
  2695. slider->deactivate();
  2696. }
  2697. void CRecrutationWindow::show(SDL_Surface * to)
  2698. {
  2699. static char c=0;
  2700. blitAt(bitmap,pos.x,pos.y,to?to:screen);
  2701. buy->show();
  2702. max->show();
  2703. cancel->show();
  2704. slider->show();
  2705. char pom[15];
  2706. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  2707. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,screen);
  2708. SDL_itoa(slider->value,pom,10); //recruit
  2709. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,screen);
  2710. printAtMiddle(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID].namePl,pos.x+243,pos.y+32,GEOR16,tytulowy,screen); //eg "Recruit Dragon flies"
  2711. int curx = pos.x+115-creatures[which].res.size()*16;
  2712. for(int i=0;i<creatures[which].res.size();i++)
  2713. {
  2714. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,screen);
  2715. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,screen);
  2716. SDL_itoa(creatures[which].res[i].second,pom,10);
  2717. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,screen);
  2718. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  2719. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,screen);
  2720. curx+=32;
  2721. }
  2722. curx = pos.x + 192 + 102 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2723. for(int i=0;i<creatures.size();i++)
  2724. {
  2725. creatures[i].pic->blitPic(screen,curx-50,pos.y+130-65,!(c%4));
  2726. curx += 120;
  2727. }
  2728. c++;
  2729. }
  2730. CRecrutationWindow::CRecrutationWindow(const std::vector<std::pair<int,int> > &Creatures, const boost::function<void(int,int)> &Recruit) //creatures - pairs<creature_ID,amount>
  2731. :recruit(Recruit)
  2732. {
  2733. which = 0;
  2734. creatures.resize(Creatures.size());
  2735. amounts.resize(Creatures.size());
  2736. for(int i=0;i<creatures.size();i++)
  2737. {
  2738. creatures[i].amount = Creatures[i].second;
  2739. creatures[i].ID = Creatures[i].first;
  2740. for(int j=0;j<CGI->creh->creatures[Creatures[i].first].cost.size();j++)
  2741. if(CGI->creh->creatures[Creatures[i].first].cost[j])
  2742. creatures[i].res.push_back(std::make_pair(j,CGI->creh->creatures[Creatures[i].first].cost[j]));
  2743. creatures[i].pic = new CCreaturePic(&CGI->creh->creatures[Creatures[i].first]);
  2744. amounts[i] = CGI->creh->creatures[Creatures[i].first].maxAmount(LOCPLINT->cb->getResourceAmount());
  2745. }
  2746. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  2747. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2748. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  2749. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2750. SDL_FreeSurface(hhlp);
  2751. pos.x = screen->w/2 - bitmap->w/2;
  2752. pos.y = screen->h/2 - bitmap->h/2;
  2753. pos.w = bitmap->w;
  2754. pos.h = bitmap->h;
  2755. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecrutationWindow::sliderMoved,this, _1),1,min(amounts[0],creatures[0].amount),0,true);
  2756. std::string pom;
  2757. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  2758. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  2759. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  2760. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  2761. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  2762. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  2763. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  2764. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  2765. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  2766. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  2767. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  2768. //border for creatures
  2769. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2770. for(int i=0;i<creatures.size();i++)
  2771. {
  2772. creatures[i].pos.x = curx+1;
  2773. creatures[i].pos.y = 65;
  2774. creatures[i].pos.w = 100;
  2775. creatures[i].pos.h = 130;
  2776. if(which==i)
  2777. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  2778. else
  2779. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  2780. curx += 120;
  2781. }
  2782. max = new AdventureMapButton("","",boost::bind(&CRecrutationWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF");
  2783. buy = new AdventureMapButton("","",boost::bind(&CRecrutationWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF");
  2784. cancel = new AdventureMapButton("","",boost::bind(&CRecrutationWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF");
  2785. }
  2786. CRecrutationWindow::~CRecrutationWindow()
  2787. {
  2788. for(int i=0;i<creatures.size();i++)
  2789. {
  2790. delete creatures[i].pic;
  2791. }
  2792. delete max;
  2793. delete buy;
  2794. delete cancel;
  2795. SDL_FreeSurface(bitmap);
  2796. delete slider;
  2797. }
  2798. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner)
  2799. {
  2800. c=cid;
  2801. slider = NULL;
  2802. gar = Owner;
  2803. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  2804. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2805. pos.x = screen->w/2 - bitmap->w/2;
  2806. pos.y = screen->h/2 - bitmap->h/2;
  2807. pos.w = bitmap->w;
  2808. pos.h = bitmap->h;
  2809. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF");
  2810. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF");
  2811. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,max,0,true);
  2812. a1 = max;
  2813. a2 = 0;
  2814. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  2815. anim->anim->setType(1);
  2816. std::string title = CGI->generaltexth->allTexts[256];
  2817. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  2818. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  2819. }
  2820. CSplitWindow::~CSplitWindow()
  2821. {
  2822. SDL_FreeSurface(bitmap);
  2823. delete ok;
  2824. delete cancel;
  2825. delete slider;
  2826. delete anim;
  2827. }
  2828. void CSplitWindow::activate()
  2829. {
  2830. LOCPLINT->objsToBlit.push_back(this);
  2831. KeyInterested::activate();
  2832. ok->activate();
  2833. cancel->activate();
  2834. slider->activate();
  2835. }
  2836. void CSplitWindow::deactivate()
  2837. {
  2838. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  2839. KeyInterested::deactivate();
  2840. ok->deactivate();
  2841. cancel->deactivate();
  2842. slider->deactivate();
  2843. }
  2844. void CSplitWindow::split()
  2845. {
  2846. gar->splitStacks(a2);
  2847. close();
  2848. }
  2849. void CSplitWindow::close()
  2850. {
  2851. deactivate();
  2852. delete this;
  2853. LOCPLINT->curint->activate();
  2854. }
  2855. void CSplitWindow::sliderMoved(int to)
  2856. {
  2857. a2 = to;
  2858. if(slider)
  2859. a1 = slider->amount - to;
  2860. }
  2861. void CSplitWindow::show(SDL_Surface * to)
  2862. {
  2863. char pom[15];
  2864. blitAt(bitmap,pos.x,pos.y,screen);
  2865. ok->show();
  2866. cancel->show();
  2867. slider->show();
  2868. SDL_itoa(a1,pom,10);
  2869. printAtMiddle(pom,pos.x+70,pos.y+237,GEOR16,zwykly,screen);
  2870. SDL_itoa(a2,pom,10);
  2871. printAtMiddle(pom,pos.x+233,pos.y+237,GEOR16,zwykly,screen);
  2872. anim->blitPic(screen,pos.x+20,pos.y+54,false);
  2873. anim->blitPic(screen,pos.x+177,pos.y+54,false);
  2874. }
  2875. void CSplitWindow::keyPressed (SDL_KeyboardEvent & key)
  2876. {
  2877. //TODO: make manual typing possible
  2878. }
  2879. void CCreInfoWindow::show(SDL_Surface * to)
  2880. {
  2881. char pom[15];
  2882. blitAt(bitmap,pos.x,pos.y,screen);
  2883. anim->blitPic(screen,pos.x+21,pos.y+48,(type) && !(anf%4));
  2884. if(++anf==4)
  2885. anf=0;
  2886. if(count.size())
  2887. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly);
  2888. if(upgrade)
  2889. upgrade->show();
  2890. if(dismiss)
  2891. dismiss->show();
  2892. if(ok)
  2893. ok->show();
  2894. }
  2895. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  2896. :ok(0),dismiss(0),upgrade(0),type(Type),dsm(Dsm),dependant(0)
  2897. {
  2898. active = false;
  2899. anf = 0;
  2900. c = &CGI->creh->creatures[Cid];
  2901. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  2902. pos.x = screen->w/2 - bitmap->w/2;
  2903. pos.y = screen->h/2 - bitmap->h/2;
  2904. pos.w = bitmap->w;
  2905. pos.h = bitmap->h;
  2906. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  2907. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2908. anim = new CCreaturePic(c);
  2909. if(!type) anim->anim->setType(1);
  2910. char pom[25];int hlp=0;
  2911. if(creatureCount)
  2912. {
  2913. SDL_itoa(creatureCount,pom,10);
  2914. count = pom;
  2915. }
  2916. printAtMiddle(c->namePl,149,30,GEOR13,zwykly,bitmap); //creature name
  2917. //atttack
  2918. printAt(CGI->preth->zelp[435].first,155,48,GEOR13,zwykly,bitmap);
  2919. SDL_itoa(c->attack,pom,10);
  2920. if(State && State->attackBonus)
  2921. {
  2922. int hlp = log10f(c->attack)+2;
  2923. pom[hlp-1] = ' '; pom[hlp] = '(';
  2924. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  2925. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  2926. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2927. }
  2928. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  2929. //defense
  2930. printAt(CGI->preth->zelp[436].first,155,67,GEOR13,zwykly,bitmap);
  2931. SDL_itoa(c->defence,pom,10);
  2932. if(State && State->defenseBonus)
  2933. {
  2934. int hlp = log10f(c->defence)+2;
  2935. pom[hlp-1] = ' '; pom[hlp] = '(';
  2936. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  2937. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  2938. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2939. }
  2940. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  2941. //shots
  2942. if(c->shots)
  2943. {
  2944. printAt(CGI->generaltexth->allTexts[198],155,86,GEOR13,zwykly,bitmap);
  2945. SDL_itoa(c->shots,pom,10);
  2946. printToWR(pom,276,99,GEOR13,zwykly,bitmap);
  2947. }
  2948. //damage
  2949. printAt(CGI->generaltexth->allTexts[199],155,105,GEOR13,zwykly,bitmap);
  2950. SDL_itoa(c->damageMin,pom,10);
  2951. hlp=log10f(c->damageMin)+2;
  2952. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  2953. SDL_itoa(c->damageMax,pom+hlp+2,10);
  2954. printToWR(pom,276,118,GEOR13,zwykly,bitmap);
  2955. //health
  2956. printAt(CGI->preth->zelp[439].first,155,124,GEOR13,zwykly,bitmap);
  2957. SDL_itoa(c->hitPoints,pom,10);
  2958. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  2959. //remaining health
  2960. if(State && State->currentHealth)
  2961. {
  2962. printAt(CGI->preth->zelp[440].first,155,143,GEOR13,zwykly,bitmap);
  2963. SDL_itoa(State->currentHealth,pom,10);
  2964. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  2965. }
  2966. //speed
  2967. printAt(CGI->preth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  2968. SDL_itoa(c->speed,pom,10);
  2969. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  2970. //luck and morale
  2971. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  2972. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  2973. //print abilities text - if r-click popup
  2974. if(type)
  2975. {
  2976. if(Upg && ui)
  2977. {
  2978. bool enough = true;
  2979. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  2980. {
  2981. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  2982. enough = false;
  2983. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  2984. }
  2985. if(enough)
  2986. {
  2987. CFunctionList<void()> fs[2];
  2988. fs[0] += Upg;
  2989. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  2990. fs[1] += boost::bind(&CCreInfoWindow::activate,this);
  2991. CFunctionList<void()> cfl;
  2992. cfl = boost::bind(&CCreInfoWindow::deactivate,this);
  2993. cfl += boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false,false);
  2994. upgrade = new AdventureMapButton("",CGI->preth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF");
  2995. }
  2996. else
  2997. {
  2998. upgrade = new AdventureMapButton("",CGI->preth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  2999. upgrade->callback.funcs.clear();
  3000. upgrade->bitmapOffset = 2;
  3001. }
  3002. }
  3003. if(Dsm)
  3004. {
  3005. CFunctionList<void()> fs[2];
  3006. //on dismiss confirmed
  3007. fs[0] += Dsm; //dismiss
  3008. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  3009. fs[1] += boost::bind(&CCreInfoWindow::activate,this);
  3010. CFunctionList<void()> cfl;
  3011. cfl = boost::bind(&CCreInfoWindow::deactivate,this);
  3012. cfl += boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false,false);
  3013. dismiss = new AdventureMapButton("",CGI->preth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF");
  3014. }
  3015. ok = new AdventureMapButton("",CGI->preth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF");
  3016. }
  3017. else
  3018. {
  3019. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  3020. }
  3021. }
  3022. CCreInfoWindow::~CCreInfoWindow()
  3023. {
  3024. SDL_FreeSurface(bitmap);
  3025. delete anim;
  3026. delete upgrade;
  3027. delete ok;
  3028. delete dismiss;
  3029. for(int i=0; i<upgResCost.size();i++)
  3030. delete upgResCost[i];
  3031. }
  3032. void CCreInfoWindow::activate()
  3033. {
  3034. if(active) return;
  3035. active = true;
  3036. if(!type)
  3037. ClickableR::activate();
  3038. LOCPLINT->objsToBlit.push_back(this);
  3039. if(ok)
  3040. ok->activate();
  3041. if(dismiss)
  3042. dismiss->activate();
  3043. if(upgrade)
  3044. upgrade->activate();
  3045. }
  3046. void CCreInfoWindow::close()
  3047. {
  3048. deactivate();
  3049. if(dynamic_cast<CHeroWindow*>(LOCPLINT->curint->subInt))
  3050. {
  3051. if(type)
  3052. LOCPLINT->curint->subInt->activate();
  3053. }
  3054. else
  3055. {
  3056. CCastleInterface *c = dynamic_cast<CCastleInterface*>(LOCPLINT->curint);
  3057. if(c)
  3058. c->showAll();
  3059. if(type)
  3060. LOCPLINT->curint->activate();
  3061. }
  3062. delete this;
  3063. }
  3064. void CCreInfoWindow::clickRight(boost::logic::tribool down)
  3065. {
  3066. if(down)
  3067. return;
  3068. close();
  3069. }
  3070. void CCreInfoWindow::dismissF()
  3071. {
  3072. dsm();
  3073. close();
  3074. }
  3075. void CCreInfoWindow::keyPressed (SDL_KeyboardEvent & key)
  3076. {
  3077. }
  3078. void CCreInfoWindow::deactivate()
  3079. {
  3080. if(!active) return;
  3081. active = false;
  3082. if(!type)
  3083. ClickableR::deactivate();
  3084. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  3085. if(ok)
  3086. ok->deactivate();
  3087. if(dismiss)
  3088. dismiss->deactivate();
  3089. if(upgrade)
  3090. upgrade->deactivate();
  3091. }
  3092. void CCreInfoWindow::onUpgradeYes()
  3093. {
  3094. dependant->close();
  3095. dependant->deactivate();
  3096. delete dependant;
  3097. LOCPLINT->curint->activate();
  3098. }
  3099. void CCreInfoWindow::onUpgradeNo()
  3100. {
  3101. }
  3102. void CLevelWindow::close()
  3103. {
  3104. deactivate();
  3105. for(int i=0;i<comps.size();i++)
  3106. {
  3107. if(comps[i]->selected)
  3108. {
  3109. cb(i);
  3110. break;
  3111. }
  3112. }
  3113. delete this;
  3114. LOCPLINT->curint->activate();
  3115. }
  3116. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  3117. {
  3118. heroType = hero->subID;
  3119. cb = callback;
  3120. for(int i=0;i<skills.size();i++)
  3121. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  3122. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  3123. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  3124. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3125. pos.x = screen->w/2 - bitmap->w/2;
  3126. pos.y = screen->h/2 - bitmap->h/2;
  3127. pos.w = bitmap->w;
  3128. pos.h = bitmap->h;
  3129. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF");
  3130. //draw window
  3131. char buf[100], buf2[100];
  3132. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  3133. sprintf(buf,buf2,hero->name.c_str());
  3134. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  3135. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  3136. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  3137. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  3138. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  3139. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  3140. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  3141. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  3142. for(int i=0;i<comps.size();i++)
  3143. {
  3144. comps[i]->pos.x = curx+pos.x;
  3145. comps[i]->pos.y = 326+pos.y;
  3146. if( i < (comps.size()-1) )
  3147. {
  3148. curx += 44 + 18; //skill width + margin to "or"
  3149. blitAt(ort,curx,346,bitmap);
  3150. curx += ort->w + 18;
  3151. }
  3152. }
  3153. SDL_FreeSurface(ort);
  3154. }
  3155. void CLevelWindow::selectionChanged(unsigned to)
  3156. {
  3157. for(int i=0;i<comps.size();i++)
  3158. if(i==to)
  3159. comps[i]->select(true);
  3160. else
  3161. comps[i]->select(false);
  3162. }
  3163. CLevelWindow::~CLevelWindow()
  3164. {
  3165. delete ok;
  3166. for(int i=0;i<comps.size();i++)
  3167. delete comps[i];
  3168. SDL_FreeSurface(bitmap);
  3169. }
  3170. void CLevelWindow::activate()
  3171. {
  3172. LOCPLINT->objsToBlit.push_back(this);
  3173. ok->activate();
  3174. for(int i=0;i<comps.size();i++)
  3175. comps[i]->activate();
  3176. }
  3177. void CLevelWindow::deactivate()
  3178. {
  3179. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  3180. ok->deactivate();
  3181. for(int i=0;i<comps.size();i++)
  3182. comps[i]->deactivate();
  3183. }
  3184. void CLevelWindow::show(SDL_Surface * to)
  3185. {
  3186. blitAt(bitmap,pos.x,pos.y,screen);
  3187. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y);
  3188. ok->show();
  3189. for(int i=0;i<comps.size();i++)
  3190. comps[i]->show();
  3191. }
  3192. void CMinorResDataBar::show(SDL_Surface * to)
  3193. {
  3194. blitAt(bg,pos.x,pos.y);
  3195. char * buf = new char[15];
  3196. for (int i=0;i<7;i++)
  3197. {
  3198. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3199. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly);
  3200. }
  3201. std::vector<std::string> temp;
  3202. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  3203. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  3204. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  3205. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  3206. CGI->generaltexth->allTexts[62]
  3207. +": %s, "
  3208. + CGI->generaltexth->allTexts[63]
  3209. + ": %s, "
  3210. + CGI->generaltexth->allTexts[64]
  3211. + ": %s",temp)
  3212. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly);
  3213. temp.clear();
  3214. //updateRect(&pos,screen);
  3215. delete[] buf;
  3216. }
  3217. CMinorResDataBar::CMinorResDataBar()
  3218. {
  3219. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  3220. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3221. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3222. pos.x = 7;
  3223. pos.y = 575;
  3224. pos.w = bg->w;
  3225. pos.h = bg->h;
  3226. }
  3227. CMinorResDataBar::~CMinorResDataBar()
  3228. {
  3229. SDL_FreeSurface(bg);
  3230. }
  3231. SDL_Surface * CCustomImgComponent::getImg()
  3232. {
  3233. return bmp;
  3234. }
  3235. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  3236. :bmp(sur), free(freeSur)
  3237. {
  3238. init(Type,Subtype,Val);
  3239. }
  3240. CCustomImgComponent::~CCustomImgComponent()
  3241. {
  3242. if(free)
  3243. SDL_FreeSurface(bmp);
  3244. }