CPlayerInterface.cpp 104 KB

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