CPlayerInterface.cpp 95 KB

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