CPlayerInterface.cpp 115 KB

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