CPlayerInterface.cpp 111 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734
  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->primSkills[0];
  133. pom->defenseBonus = h->primSkills[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] + " " + CGI->abilh->abilities[Subtype]->name;
  586. description = CGI->abilh->abilities[Subtype]->infoTexts[Val];
  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].bitmap;
  646. break;
  647. case secskill:
  648. return CGI->abilh->abils82->ourImages[subtype*3 + 3 + val].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::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::mutex> un(*pim);
  1082. //initializing objects and performing first step of move
  1083. const CGHeroInstance * ho = details.ho; //object representing this hero
  1084. int3 hp = details.src;
  1085. if (!details.successful)
  1086. {
  1087. ho->moveDir = getDir(details.src,details.dst);
  1088. ho->isStanding = true;
  1089. adventureInt->heroList.draw();
  1090. if (adventureInt->terrain.currentPath && ho->movement>145) //TODO: better condition on movement - check cost of endtile
  1091. {
  1092. delete adventureInt->terrain.currentPath;
  1093. adventureInt->terrain.currentPath = NULL;
  1094. adventureInt->heroList.items[adventureInt->heroList.getPosOfHero(ho)].second = NULL;
  1095. }
  1096. return;
  1097. }
  1098. if (adventureInt->terrain.currentPath) //&& hero is moving
  1099. {
  1100. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  1101. }
  1102. int3 buff = details.ho->pos;
  1103. buff.x-=11;
  1104. buff.y-=9;
  1105. buff = repairScreenPos(buff);
  1106. LOCPLINT->adventureInt->position = buff; //actualizing screen pos
  1107. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1108. {
  1109. ho->moveDir = 1;
  1110. ho->isStanding = false;
  1111. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1112. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1113. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1114. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1115. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1116. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1117. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1118. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1119. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1120. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1121. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1122. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1123. 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);
  1124. 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);
  1125. 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);
  1126. 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);
  1127. 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);
  1128. 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);
  1129. }
  1130. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1131. {
  1132. ho->moveDir = 2;
  1133. ho->isStanding = false;
  1134. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1135. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1136. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1137. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1138. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1139. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1140. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1141. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1142. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1143. 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);
  1144. 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);
  1145. 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);
  1146. }
  1147. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1148. {
  1149. ho->moveDir = 3;
  1150. ho->isStanding = false;
  1151. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1152. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1153. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1154. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1155. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1156. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1157. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1158. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1159. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1160. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1161. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1162. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1163. 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);
  1164. 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);
  1165. 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);
  1166. 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);
  1167. 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);
  1168. 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);
  1169. }
  1170. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1171. {
  1172. ho->moveDir = 4;
  1173. ho->isStanding = false;
  1174. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1175. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1176. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1177. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1178. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1179. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1180. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1181. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1182. 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);
  1183. 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);
  1184. }
  1185. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1186. {
  1187. ho->moveDir = 5;
  1188. ho->isStanding = false;
  1189. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1190. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1191. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1192. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1193. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1194. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1195. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1196. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1197. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1198. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1199. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1200. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1201. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.end(), ocmptwo_cgin);
  1202. std::stable_sort(CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.begin(), CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.end(), ocmptwo_cgin);
  1203. 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);
  1204. 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);
  1205. 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);
  1206. 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);
  1207. }
  1208. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1209. {
  1210. ho->moveDir = 6;
  1211. ho->isStanding = false;
  1212. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1213. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1214. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1215. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1216. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1217. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1218. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1219. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1220. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1221. 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);
  1222. 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);
  1223. 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);
  1224. }
  1225. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1226. {
  1227. ho->moveDir = 7;
  1228. ho->isStanding = false;
  1229. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1230. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1231. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1232. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1233. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1234. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1235. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1236. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1237. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1238. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1239. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1240. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1241. 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);
  1242. 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);
  1243. 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);
  1244. 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);
  1245. 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);
  1246. 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);
  1247. }
  1248. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1249. {
  1250. ho->moveDir = 8;
  1251. ho->isStanding = false;
  1252. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1253. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1254. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1255. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1256. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1257. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1258. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1259. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1260. 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);
  1261. 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);
  1262. }
  1263. //first initializing done
  1264. SDL_framerateDelay(mainFPSmng); // after first move
  1265. //main moving
  1266. for(int i=1; i<32; i+=4)
  1267. {
  1268. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1269. {
  1270. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1271. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1272. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1273. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1274. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1275. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1276. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1277. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1278. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1279. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1280. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1281. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1282. }
  1283. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1284. {
  1285. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1286. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1287. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1288. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1289. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1290. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1291. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1292. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1293. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1294. }
  1295. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1296. {
  1297. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1298. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1299. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1300. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1301. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1302. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1303. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1304. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1305. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1306. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1307. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1308. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1309. }
  1310. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1311. {
  1312. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1313. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1314. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1315. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1316. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1317. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1318. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1319. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1320. }
  1321. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1322. {
  1323. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1324. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1325. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1326. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1327. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1328. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1329. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1330. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1331. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1332. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1333. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1334. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1335. }
  1336. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1337. {
  1338. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1339. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1340. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1341. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1342. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1343. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1344. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1345. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1346. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1347. }
  1348. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1349. {
  1350. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1351. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1352. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1353. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1354. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1355. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1356. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1357. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1358. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1359. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1360. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1361. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1362. }
  1363. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1364. {
  1365. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1366. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1367. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1368. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1369. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1370. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1371. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1372. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1373. }
  1374. adventureInt->updateScreen = true;
  1375. LOCPLINT->adventureInt->update(); //updating screen
  1376. CSDL_Ext::update(screen);
  1377. //CGI->screenh->updateScreen();
  1378. ++LOCPLINT->adventureInt->animValHitCount; //for animations
  1379. if(LOCPLINT->adventureInt->animValHitCount == 8)
  1380. {
  1381. LOCPLINT->adventureInt->animValHitCount = 0;
  1382. ++LOCPLINT->adventureInt->anim;
  1383. LOCPLINT->adventureInt->updateScreen = true;
  1384. }
  1385. ++LOCPLINT->adventureInt->heroAnim;
  1386. SDL_Delay(5);
  1387. SDL_framerateDelay(mainFPSmng); //for animation purposes
  1388. } //for(int i=1; i<32; i+=4)
  1389. //main moving done
  1390. //finishing move
  1391. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1392. {
  1393. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1394. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1395. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1396. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1397. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1398. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1399. }
  1400. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1401. {
  1402. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1403. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1404. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1405. }
  1406. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1407. {
  1408. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1409. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1410. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1411. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1412. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1413. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1414. }
  1415. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1416. {
  1417. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1418. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1419. }
  1420. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1421. {
  1422. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1423. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1424. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1425. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1426. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1427. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1428. }
  1429. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1430. {
  1431. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1432. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1433. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1434. }
  1435. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1436. {
  1437. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1438. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1439. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1440. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1441. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1442. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1443. }
  1444. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1445. {
  1446. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1447. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1448. }
  1449. //restoring good order of objects
  1450. 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);
  1451. 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);
  1452. 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);
  1453. 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);
  1454. 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);
  1455. 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);
  1456. ho->isStanding = true;
  1457. //move finished
  1458. adventureInt->minimap.draw();
  1459. adventureInt->heroList.updateMove(ho);
  1460. }
  1461. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  1462. {
  1463. boost::unique_lock<boost::mutex> un(*pim);
  1464. graphics->heroWins.erase(hero->ID);
  1465. adventureInt->heroList.updateHList();
  1466. }
  1467. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  1468. {
  1469. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  1470. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  1471. }
  1472. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  1473. {
  1474. adventureInt->hide();
  1475. //timeHandler t;
  1476. //t.getDif();
  1477. castleInt = new CCastleInterface(town,true);
  1478. //std::cout << "Loading town screen: " << t.getDif() <<std::endl;
  1479. }
  1480. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  1481. {
  1482. if (specific)
  1483. {
  1484. switch (specific->ID)
  1485. {
  1486. case 34:
  1487. return graphics->drawHeroInfoWin(dynamic_cast<const CGHeroInstance*>(specific));
  1488. break;
  1489. case 98:
  1490. return graphics->drawTownInfoWin(dynamic_cast<const CGTownInstance*>(specific));
  1491. break;
  1492. default:
  1493. return NULL;
  1494. break;
  1495. }
  1496. }
  1497. else
  1498. {
  1499. switch (adventureInt->selection->ID)
  1500. {
  1501. case HEROI_TYPE:
  1502. {
  1503. const CGHeroInstance * curh = (const CGHeroInstance *)adventureInt->selection;
  1504. return graphics->drawHeroInfoWin(curh);
  1505. }
  1506. case TOWNI_TYPE:
  1507. {
  1508. return graphics->drawTownInfoWin((const CGTownInstance *)adventureInt->selection);
  1509. }
  1510. default:
  1511. return NULL;
  1512. }
  1513. }
  1514. return NULL;
  1515. }
  1516. void CPlayerInterface::handleMouseMotion(SDL_Event *sEvent)
  1517. {
  1518. std::vector<Hoverable*> hlp;
  1519. for(std::list<Hoverable*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  1520. {
  1521. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1522. {
  1523. if (!(*i)->hovered)
  1524. hlp.push_back((*i));
  1525. }
  1526. else if ((*i)->hovered)
  1527. {
  1528. (*i)->hover(false);
  1529. }
  1530. }
  1531. for(int i=0; i<hlp.size();i++)
  1532. hlp[i]->hover(true);
  1533. for(std::list<MotionInterested*>::iterator i=motioninterested.begin(); i != motioninterested.end();i++)
  1534. {
  1535. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1536. {
  1537. (*i)->mouseMoved(sEvent->motion);
  1538. }
  1539. }
  1540. if(sEvent->motion.x<15)
  1541. {
  1542. LOCPLINT->adventureInt->scrollingLeft = true;
  1543. }
  1544. else
  1545. {
  1546. LOCPLINT->adventureInt->scrollingLeft = false;
  1547. }
  1548. if(sEvent->motion.x>screen->w-15)
  1549. {
  1550. LOCPLINT->adventureInt->scrollingRight = true;
  1551. }
  1552. else
  1553. {
  1554. LOCPLINT->adventureInt->scrollingRight = false;
  1555. }
  1556. if(sEvent->motion.y<15)
  1557. {
  1558. LOCPLINT->adventureInt->scrollingUp = true;
  1559. }
  1560. else
  1561. {
  1562. LOCPLINT->adventureInt->scrollingUp = false;
  1563. }
  1564. if(sEvent->motion.y>screen->h-15)
  1565. {
  1566. LOCPLINT->adventureInt->scrollingDown = true;
  1567. }
  1568. else
  1569. {
  1570. LOCPLINT->adventureInt->scrollingDown = false;
  1571. }
  1572. }
  1573. void CPlayerInterface::handleKeyUp(SDL_Event *sEvent)
  1574. {
  1575. switch (sEvent->key.keysym.sym)
  1576. {
  1577. case SDLK_LEFT:
  1578. {
  1579. LOCPLINT->adventureInt->scrollingLeft = false;
  1580. break;
  1581. }
  1582. case (SDLK_RIGHT):
  1583. {
  1584. LOCPLINT->adventureInt->scrollingRight = false;
  1585. break;
  1586. }
  1587. case (SDLK_UP):
  1588. {
  1589. LOCPLINT->adventureInt->scrollingUp = false;
  1590. break;
  1591. }
  1592. case (SDLK_DOWN):
  1593. {
  1594. LOCPLINT->adventureInt->scrollingDown = false;
  1595. break;
  1596. }
  1597. case (SDLK_u):
  1598. {
  1599. adventureInt->underground.clickLeft(false);
  1600. break;
  1601. }
  1602. case (SDLK_m):
  1603. {
  1604. adventureInt->moveHero.clickLeft(false);
  1605. break;
  1606. }
  1607. case (SDLK_e):
  1608. {
  1609. adventureInt->endTurn.clickLeft(false);
  1610. break;
  1611. }
  1612. case (SDLK_c):
  1613. {
  1614. if( dynamic_cast<CBattleInterface*> (curint) )
  1615. {
  1616. dynamic_cast<CBattleInterface*> (curint)->showStackQueue = false;
  1617. }
  1618. break;
  1619. }
  1620. }
  1621. }
  1622. void CPlayerInterface::handleKeyDown(SDL_Event *sEvent)
  1623. {
  1624. switch (sEvent->key.keysym.sym)
  1625. {
  1626. case SDLK_LEFT:
  1627. {
  1628. LOCPLINT->adventureInt->scrollingLeft = true;
  1629. break;
  1630. }
  1631. case (SDLK_RIGHT):
  1632. {
  1633. LOCPLINT->adventureInt->scrollingRight = true;
  1634. break;
  1635. }
  1636. case (SDLK_UP):
  1637. {
  1638. LOCPLINT->adventureInt->scrollingUp = true;
  1639. break;
  1640. }
  1641. case (SDLK_DOWN):
  1642. {
  1643. LOCPLINT->adventureInt->scrollingDown = true;
  1644. break;
  1645. }
  1646. case (SDLK_F4):
  1647. {
  1648. if(sEvent->key.keysym.mod & KMOD_LALT) //Alt+F4
  1649. {
  1650. exit(0);
  1651. break;
  1652. }
  1653. }
  1654. //case (SDLK_q):
  1655. // {
  1656. // exit(0);
  1657. // break;
  1658. // }
  1659. case (SDLK_u):
  1660. {
  1661. adventureInt->underground.clickLeft(true);
  1662. break;
  1663. }
  1664. case (SDLK_m):
  1665. {
  1666. adventureInt->moveHero.clickLeft(true);
  1667. break;
  1668. }
  1669. case (SDLK_e):
  1670. {
  1671. adventureInt->endTurn.clickLeft(true);
  1672. break;
  1673. }
  1674. case (SDLK_c):
  1675. {
  1676. if( dynamic_cast<CBattleInterface*> (curint) )
  1677. {
  1678. dynamic_cast<CBattleInterface*> (curint)->showStackQueue = true;
  1679. }
  1680. break;
  1681. }
  1682. }
  1683. }
  1684. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  1685. {
  1686. current = sEvent;
  1687. if(sEvent->type == SDL_MOUSEMOTION)
  1688. {
  1689. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  1690. }
  1691. if(sEvent->type==SDL_QUIT)
  1692. exit(0);
  1693. else if (sEvent->type==SDL_KEYDOWN)
  1694. {
  1695. handleKeyDown(sEvent);
  1696. } //keydown end
  1697. else if(sEvent->type==SDL_KEYUP)
  1698. {
  1699. handleKeyUp(sEvent);
  1700. }//keyup end
  1701. else if(sEvent->type==SDL_MOUSEMOTION)
  1702. {
  1703. handleMouseMotion(sEvent);
  1704. } //mousemotion end
  1705. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1706. {
  1707. std::list<ClickableL*> hlp = lclickable;
  1708. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1709. {
  1710. if(!vstd::contains(lclickable,*i)) continue;
  1711. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1712. {
  1713. (*i)->clickLeft(true);
  1714. }
  1715. }
  1716. }
  1717. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1718. {
  1719. std::list<ClickableL*> hlp = lclickable;
  1720. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1721. {
  1722. if(!vstd::contains(lclickable,*i)) continue;
  1723. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1724. {
  1725. (*i)->clickLeft(false);
  1726. }
  1727. else
  1728. (*i)->clickLeft(boost::logic::indeterminate);
  1729. }
  1730. }
  1731. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1732. {
  1733. std::list<ClickableR*> hlp = rclickable;
  1734. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1735. {
  1736. if(!vstd::contains(rclickable,*i)) continue;
  1737. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1738. {
  1739. (*i)->clickRight(true);
  1740. }
  1741. }
  1742. }
  1743. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1744. {
  1745. std::list<ClickableR*> hlp = rclickable;
  1746. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1747. {
  1748. if(!vstd::contains(rclickable,*i)) continue;
  1749. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1750. {
  1751. (*i)->clickRight(false);
  1752. }
  1753. else
  1754. (*i)->clickRight(boost::logic::indeterminate);
  1755. }
  1756. }
  1757. current = NULL;
  1758. } //event end
  1759. int3 CPlayerInterface::repairScreenPos(int3 pos)
  1760. {
  1761. if(pos.x<=-Woff)
  1762. pos.x = -Woff+1;
  1763. if(pos.y<=-Hoff)
  1764. pos.y = -Hoff+1;
  1765. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff)
  1766. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff;
  1767. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff)
  1768. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff;
  1769. return pos;
  1770. }
  1771. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val)
  1772. {
  1773. boost::unique_lock<boost::mutex> un(*pim);
  1774. SDL_FreeSurface(graphics->heroWins[hero->subID]);//TODO: moznaby zmieniac jedynie fragment bitmapy zwiazany z dana umiejetnoscia
  1775. graphics->heroWins[hero->subID] = infoWin(hero); //a nie przerysowywac calosc. Troche roboty, obecnie chyba nie wartej swieczki.
  1776. if (adventureInt->selection == hero)
  1777. adventureInt->infoBar.draw();
  1778. return;
  1779. }
  1780. void CPlayerInterface::receivedResource(int type, int val)
  1781. {
  1782. boost::unique_lock<boost::mutex> un(*pim);
  1783. if(!curint->subInt)
  1784. adventureInt->resdatabar.draw();
  1785. }
  1786. void CPlayerInterface::showSelDialog(std::string &text, const std::vector<Component*> &components, ui32 askID)
  1787. //void CPlayerInterface::showSelDialog(std::string text, std::vector<CSelectableComponent*> & components, int askID)
  1788. {
  1789. boost::unique_lock<boost::mutex> un(*pim);
  1790. adventureInt->hide(); //dezaktywacja starego interfejsu
  1791. std::vector<CSelectableComponent*> intComps;
  1792. for(int i=0;i<components.size();i++)
  1793. intComps.push_back(new CSelectableComponent(*components[i])); //will be deleted by CSelWindow::close
  1794. std::vector<std::pair<std::string,boost::function<void()> > > pom;
  1795. pom.push_back(std::pair<std::string,boost::function<void()> >("IOKAY.DEF",0));
  1796. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom);
  1797. LOCPLINT->objsToBlit.push_back(temp);
  1798. temp->activate();
  1799. temp->ID = askID;
  1800. intComps[0]->clickLeft(true);
  1801. }
  1802. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  1803. {
  1804. {
  1805. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1806. while(showingDialog->data)
  1807. showingDialog->cond.wait(un);
  1808. }
  1809. boost::unique_lock<boost::mutex> un(*pim);
  1810. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  1811. curint->deactivate();
  1812. lw->activate();
  1813. }
  1814. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  1815. {
  1816. boost::unique_lock<boost::mutex> un(*pim);
  1817. //redraw infowindow
  1818. SDL_FreeSurface(graphics->townWins[town->id]);
  1819. graphics->townWins[town->id] = infoWin(town);
  1820. if(town->garrisonHero)
  1821. {
  1822. CGI->mh->hideObject(town->garrisonHero);
  1823. }
  1824. if(town->visitingHero)
  1825. {
  1826. CGI->mh->printObject(town->visitingHero);
  1827. }
  1828. adventureInt->heroList.updateHList();
  1829. CCastleInterface *c = dynamic_cast<CCastleInterface*>(curint);
  1830. if(c)
  1831. {
  1832. c->garr->highlighted = NULL;
  1833. c->hslotup.hero = town->garrisonHero;
  1834. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  1835. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  1836. c->garr->recreateSlots();
  1837. c->showAll();
  1838. }
  1839. }
  1840. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  1841. {
  1842. if(hero->tempOwner != town->tempOwner)
  1843. return;
  1844. boost::unique_lock<boost::mutex> un(*pim);
  1845. openTownWindow(town);
  1846. }
  1847. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  1848. {
  1849. boost::unique_lock<boost::mutex> un(*pim);
  1850. if(obj->ID == 34) //hero
  1851. {
  1852. const CGHeroInstance * hh;
  1853. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  1854. {
  1855. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  1856. graphics->heroWins[hh->subID] = infoWin(hh);
  1857. }
  1858. CHeroWindow * hw = dynamic_cast<CHeroWindow *>(curint->subInt);
  1859. if(hw)
  1860. {
  1861. hw->garInt->recreateSlots();
  1862. hw->garInt->show();
  1863. }
  1864. if(castleInt) //opened town window - redraw town garrsion slots (change is within hero garr)
  1865. {
  1866. castleInt->garr->highlighted = NULL;
  1867. castleInt->garr->recreateSlots();
  1868. }
  1869. }
  1870. else if (obj->ID == 98) //town
  1871. {
  1872. const CGTownInstance * tt;
  1873. if(tt = static_cast<const CGTownInstance*>(obj))
  1874. {
  1875. SDL_FreeSurface(graphics->townWins[tt->id]);
  1876. graphics->townWins[tt->id] = infoWin(tt);
  1877. }
  1878. if(tt->visitingHero)
  1879. {
  1880. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  1881. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  1882. }
  1883. if(LOCPLINT->castleInt)
  1884. {
  1885. LOCPLINT->castleInt->garr->highlighted = NULL;
  1886. LOCPLINT->castleInt->garr->recreateSlots();
  1887. }
  1888. }
  1889. }
  1890. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  1891. {
  1892. boost::unique_lock<boost::mutex> un(*pim);
  1893. switch (buildingID)
  1894. {
  1895. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  1896. {
  1897. SDL_FreeSurface(graphics->townWins[town->id]);
  1898. graphics->townWins[town->id] = infoWin(town);
  1899. break;
  1900. }
  1901. }
  1902. if(curint!=castleInt)
  1903. return;
  1904. if(castleInt->town!=town)
  1905. return;
  1906. switch(what)
  1907. {
  1908. case 1:
  1909. castleInt->addBuilding(buildingID);
  1910. break;
  1911. case 2:
  1912. castleInt->removeBuilding(buildingID);
  1913. break;
  1914. }
  1915. }
  1916. 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
  1917. {
  1918. boost::unique_lock<boost::mutex> un(*pim);
  1919. curint->deactivate();
  1920. curint = new CBattleInterface(army1,army2,hero1,hero2);
  1921. curint->activate();
  1922. LOCPLINT->objsToBlit.push_back(dynamic_cast<IShowable*>(curint));
  1923. }
  1924. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  1925. {
  1926. }
  1927. 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
  1928. {
  1929. dynamic_cast<CBattleInterface*>(curint)->newRound(round);
  1930. }
  1931. //void CPlayerInterface::actionStarted(BattleAction action)//occurs BEFORE every action taken by any stack or by the hero
  1932. //{
  1933. //}
  1934. //
  1935. //void CPlayerInterface::actionFinished(BattleAction action)//occurs AFTER every action taken by any stack or by the hero
  1936. //{
  1937. // //dynamic_cast<CBattleInterface*>(curint)->givenCommand = -1;
  1938. //}
  1939. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  1940. {
  1941. CBattleInterface *b = dynamic_cast<CBattleInterface*>(curint);
  1942. {
  1943. boost::unique_lock<boost::mutex> un(*pim);
  1944. b->stackActivated(stackID);
  1945. }
  1946. //wait till BattleInterface sets its command
  1947. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  1948. while(!b->givenCommand->data)
  1949. b->givenCommand->cond.wait(lock);
  1950. //tidy up
  1951. BattleAction ret = *(b->givenCommand->data);
  1952. delete b->givenCommand->data;
  1953. b->givenCommand->data = NULL;
  1954. //return command
  1955. return ret;
  1956. }
  1957. void CPlayerInterface::battleEnd(BattleResult *br)
  1958. {
  1959. ((CBattleInterface*)curint)->battleFinished(*br);
  1960. }
  1961. void CPlayerInterface::battleResultQuited()
  1962. {
  1963. //boost::unique_lock<boost::mutex> un(*pim);
  1964. ((CBattleInterface*)curint)->resWindow->deactivate();
  1965. objsToBlit -= curint;
  1966. delete curint;
  1967. curint = adventureInt;
  1968. adventureInt->activate();
  1969. }
  1970. void CPlayerInterface::battleStackMoved(int ID, int dest, bool startMoving, bool endMoving)
  1971. {
  1972. boost::unique_lock<boost::mutex> un(*pim);
  1973. dynamic_cast<CBattleInterface*>(curint)->stackMoved(ID, dest, startMoving, endMoving);
  1974. }
  1975. void CPlayerInterface::battleAttack(BattleAttack *ba)
  1976. {
  1977. boost::unique_lock<boost::mutex> un(*pim);
  1978. if(ba->shot())
  1979. dynamic_cast<CBattleInterface*>(curint)->stackIsShooting(ba->stackAttacking,cb->battleGetPos(ba->bsa.stackAttacked));
  1980. else
  1981. dynamic_cast<CBattleInterface*>(curint)->stackAttacking( ba->stackAttacking, cb->battleGetPos(ba->bsa.stackAttacked) );
  1982. if(ba->killed())
  1983. dynamic_cast<CBattleInterface*>(curint)->stackKilled(ba->bsa.stackAttacked, ba->bsa.damageAmount, ba->bsa.killedAmount, ba->stackAttacking, ba->shot());
  1984. else
  1985. dynamic_cast<CBattleInterface*>(curint)->stackIsAttacked(ba->bsa.stackAttacked, ba->bsa.damageAmount, ba->bsa.killedAmount, ba->stackAttacking, ba->shot());
  1986. }
  1987. void CPlayerInterface::battleStackKilled(int ID, int dmg, int killed, int IDby, bool byShooting)
  1988. {
  1989. dynamic_cast<CBattleInterface*>(curint)->stackKilled(ID, dmg, killed, IDby, byShooting);
  1990. }
  1991. void CPlayerInterface::battleStackIsShooting(int ID, int dest)
  1992. {
  1993. dynamic_cast<CBattleInterface*>(curint)->stackIsShooting(ID, dest);
  1994. }
  1995. void CPlayerInterface::showComp(SComponent comp)
  1996. {
  1997. boost::unique_lock<boost::mutex> un(*pim);
  1998. adventureInt->infoBar.showComp(&comp,4000);
  1999. }
  2000. void CPlayerInterface::showInfoDialog(std::string &text, const std::vector<Component*> &components)
  2001. {
  2002. std::vector<SComponent*> intComps;
  2003. for(int i=0;i<components.size();i++)
  2004. intComps.push_back(new SComponent(*components[i]));
  2005. showInfoDialog(text,intComps);
  2006. }
  2007. void CPlayerInterface::showInfoDialog(std::string &text, const std::vector<SComponent*> & components)
  2008. {
  2009. showingDialog->set(true);
  2010. curint->deactivate(); //dezaktywacja starego interfejsu
  2011. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2012. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2013. CInfoWindow * temp = new CInfoWindow(text,playerID,32,components,pom);
  2014. temp->buttons[0]->callback += boost::bind(&IActivable::activate,LOCPLINT->curint);
  2015. temp->activate();
  2016. LOCPLINT->objsToBlit.push_back(temp);
  2017. }
  2018. void CPlayerInterface::showYesNoDialog(std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool deactivateCur, bool DelComps)
  2019. {
  2020. if(deactivateCur)
  2021. curint->deactivate(); //dezaktywacja starego interfejsu
  2022. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2023. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2024. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  2025. CInfoWindow * temp = new CInfoWindow(text,playerID,32,components,pom);
  2026. temp->delComps = DelComps;
  2027. for(int i=0;i<onYes.funcs.size();i++)
  2028. temp->buttons[0]->callback += onYes.funcs[i];
  2029. for(int i=0;i<onNo.funcs.size();i++)
  2030. temp->buttons[1]->callback += onNo.funcs[i];
  2031. //if(onYes)
  2032. // temp->buttons[0]->callback += boost::bind(&CInfoWindow::close,temp);
  2033. //if(onNo)
  2034. // temp->buttons[1]->callback += boost::bind(&CInfoWindow::close,temp);
  2035. temp->activate();
  2036. LOCPLINT->objsToBlit.push_back(temp);
  2037. }
  2038. void CPlayerInterface::showYesNoDialog( std::string &text, const std::vector<Component*> &components, ui32 askID )
  2039. {
  2040. boost::unique_lock<boost::mutex> un(*pim);
  2041. curint->deactivate(); //dezaktywacja starego interfejsu
  2042. std::vector<SComponent*> intComps;
  2043. for(int i=0;i<components.size();i++)
  2044. intComps.push_back(new SComponent(*components[i])); //will be deleted by CSelWindow::close
  2045. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2046. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2047. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  2048. CInfoWindow * temp = new CInfoWindow(text,playerID,32,intComps,pom);
  2049. temp->buttons[0]->callback += boost::bind(&IActivable::activate,curint);
  2050. temp->buttons[1]->callback += boost::bind(&IActivable::activate,curint);
  2051. temp->buttons[0]->callback += boost::bind(&CCallback::selectionMade,cb,0,askID);
  2052. temp->buttons[1]->callback += boost::bind(&CCallback::selectionMade,cb,1,askID);
  2053. temp->delComps = true;
  2054. temp->activate();
  2055. LOCPLINT->objsToBlit.push_back(temp);
  2056. }
  2057. void CPlayerInterface::removeObjToBlit(IShowable* obj)
  2058. {
  2059. objsToBlit.erase
  2060. (std::find(objsToBlit.begin(),objsToBlit.end(),obj));
  2061. //delete obj;
  2062. }
  2063. void CPlayerInterface::tileRevealed(int3 pos)
  2064. {
  2065. adventureInt->minimap.showTile(pos);
  2066. }
  2067. void CPlayerInterface::tileHidden(int3 pos)
  2068. {
  2069. adventureInt->minimap.hideTile(pos);
  2070. }
  2071. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  2072. {
  2073. adventureInt->heroWindow->setHero(hero);
  2074. this->objsToBlit.push_back(adventureInt->heroWindow);
  2075. curint->deactivate();
  2076. if(curint == castleInt)
  2077. castleInt->subInt = adventureInt->heroWindow;
  2078. adventureInt->heroWindow->quitButton->callback.funcs.clear();
  2079. adventureInt->heroWindow->quitButton->callback += boost::bind(&CHeroWindow::deactivate,adventureInt->heroWindow);
  2080. adventureInt->heroWindow->quitButton->callback += boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  2081. adventureInt->heroWindow->activate();
  2082. }
  2083. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  2084. {
  2085. boost::unique_lock<boost::mutex> un(*pim);
  2086. if(curint->subInt == adventureInt->heroWindow)
  2087. {
  2088. adventureInt->heroWindow->deactivate();
  2089. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  2090. adventureInt->heroWindow->activate();
  2091. }
  2092. }
  2093. void CPlayerInterface::updateWater()
  2094. {
  2095. //updating water tiles
  2096. //int wnumber = -1;
  2097. //for(int s=0; s<CGI->mh->reader->defs.size(); ++s)
  2098. //{
  2099. // if(CGI->mh->reader->defs[s]->defName==std::string("WATRTL.DEF"))
  2100. // {
  2101. // wnumber = s;
  2102. // break;
  2103. // }
  2104. //}
  2105. //if(wnumber>=0)
  2106. //{
  2107. // for(int g=0; g<CGI->mh->reader->defs[wnumber]->ourImages.size(); ++g)
  2108. // {
  2109. // SDL_Color tab[32];
  2110. // for(int i=0; i<32; ++i)
  2111. // {
  2112. // tab[i] = CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap->format->palette->colors[160 + (i+1)%32];
  2113. // }
  2114. // //SDL_SaveBMP(CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap,"t1.bmp");
  2115. // for(int i=0; i<32; ++i)
  2116. // {
  2117. // CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap->format->palette->colors[160 + i] = tab[i];
  2118. // }
  2119. // //SDL_SaveBMP(CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap,"t2.bmp");
  2120. // CSDL_Ext::update(CGI->mh->reader->defs[wnumber]->ourImages[g].bitmap);
  2121. // }
  2122. //}
  2123. //water tiles updated
  2124. //CGI->screenh->updateScreen();
  2125. }
  2126. void CPlayerInterface::availableCreaturesChanged( const CGTownInstance *town )
  2127. {
  2128. boost::unique_lock<boost::mutex> un(*pim);
  2129. if(curint == castleInt)
  2130. {
  2131. CFortScreen *fs = dynamic_cast<CFortScreen*>(curint->subInt);
  2132. if(fs)
  2133. fs->draw(castleInt,false);
  2134. }
  2135. }
  2136. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  2137. {
  2138. bg=BitmapHandler::loadBitmap(name);
  2139. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2140. pos.x=x;
  2141. pos.y=y;
  2142. if(maxw >= 0)
  2143. pos.w = min(bg->w,maxw);
  2144. else
  2145. pos.w=bg->w;
  2146. pos.h=bg->h;
  2147. middlex=(pos.w/2)+x;
  2148. middley=(bg->h/2)+y;
  2149. }
  2150. CStatusBar::~CStatusBar()
  2151. {
  2152. SDL_FreeSurface(bg);
  2153. }
  2154. void CStatusBar::clear()
  2155. {
  2156. current="";
  2157. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2158. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
  2159. }
  2160. void CStatusBar::print(std::string text)
  2161. {
  2162. current=text;
  2163. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2164. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
  2165. printAtMiddle(current,middlex,middley,GEOR13,zwykly);
  2166. }
  2167. void CStatusBar::show()
  2168. {
  2169. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2170. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
  2171. printAtMiddle(current,middlex,middley,GEOR13,zwykly);
  2172. }
  2173. std::string CStatusBar::getCurrent()
  2174. {
  2175. return current;
  2176. }
  2177. void CList::activate()
  2178. {
  2179. ClickableL::activate();
  2180. ClickableR::activate();
  2181. Hoverable::activate();
  2182. KeyInterested::activate();
  2183. MotionInterested::activate();
  2184. };
  2185. void CList::deactivate()
  2186. {
  2187. ClickableL::deactivate();
  2188. ClickableR::deactivate();
  2189. Hoverable::deactivate();
  2190. KeyInterested::deactivate();
  2191. MotionInterested::deactivate();
  2192. };
  2193. void CList::clickLeft(tribool down)
  2194. {
  2195. };
  2196. CList::CList(int Size)
  2197. :SIZE(Size)
  2198. {
  2199. }
  2200. CHeroList::CHeroList(int Size)
  2201. :CList(Size)
  2202. {
  2203. pos = genRect(192,64,609,196);
  2204. arrupp = genRect(16,64,609,196);
  2205. arrdop = genRect(16,64,609,372);
  2206. //32px per hero
  2207. posmobx = 610;
  2208. posmoby = 213;
  2209. posporx = 617;
  2210. pospory = 212;
  2211. posmanx = 666;
  2212. posmany = 213;
  2213. arrup = CDefHandler::giveDef("IAM012.DEF");
  2214. arrdo = CDefHandler::giveDef("IAM013.DEF");
  2215. mobile = CDefHandler::giveDef("IMOBIL.DEF");
  2216. mana = CDefHandler::giveDef("IMANA.DEF");
  2217. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  2218. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  2219. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  2220. from = 0;
  2221. pressed = indeterminate;
  2222. }
  2223. void CHeroList::init()
  2224. {
  2225. bg = CSDL_Ext::newSurface(68,193,screen);
  2226. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(193,68,607,196),bg,&genRect(193,68,0,0));
  2227. }
  2228. void CHeroList::genList()
  2229. {
  2230. int howMany = LOCPLINT->cb->howManyHeroes();
  2231. for (int i=0;i<howMany;i++)
  2232. {
  2233. const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  2234. if(!h->inTownGarrison)
  2235. items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  2236. }
  2237. }
  2238. void CHeroList::select(int which)
  2239. {
  2240. if (which<0)
  2241. {
  2242. selected = which;
  2243. LOCPLINT->adventureInt->selection = NULL;
  2244. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  2245. draw();
  2246. LOCPLINT->adventureInt->infoBar.draw(NULL);
  2247. }
  2248. if (which>=items.size())
  2249. return;
  2250. selected = which;
  2251. LOCPLINT->adventureInt->centerOn(items[which].first->pos);
  2252. LOCPLINT->adventureInt->selection = items[which].first;
  2253. LOCPLINT->adventureInt->terrain.currentPath = items[which].second;
  2254. draw();
  2255. LOCPLINT->adventureInt->townList.draw();
  2256. LOCPLINT->adventureInt->infoBar.draw(NULL);
  2257. }
  2258. void CHeroList::clickLeft(tribool down)
  2259. {
  2260. if (down)
  2261. {
  2262. /***************************ARROWS*****************************************/
  2263. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2264. {
  2265. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2266. pressed = true;
  2267. return;
  2268. }
  2269. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2270. {
  2271. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2272. pressed = false;
  2273. return;
  2274. }
  2275. /***************************HEROES*****************************************/
  2276. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2277. hx-=pos.x;
  2278. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2279. int ny = hy/32;
  2280. if (ny>=5 || ny<0)
  2281. return;
  2282. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2283. LOCPLINT->openHeroWindow(items[selected].first);//print hero screen
  2284. select(ny+from);
  2285. }
  2286. else
  2287. {
  2288. if (indeterminate(pressed))
  2289. return;
  2290. if (pressed) //up
  2291. {
  2292. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2293. pressed = indeterminate;
  2294. if (!down)
  2295. {
  2296. from--;
  2297. if (from<0)
  2298. from=0;
  2299. draw();
  2300. }
  2301. }
  2302. else if (!pressed) //down
  2303. {
  2304. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2305. pressed = indeterminate;
  2306. if (!down)
  2307. {
  2308. from++;
  2309. //if (from<items.size()-5)
  2310. // from=items.size()-5;
  2311. draw();
  2312. }
  2313. }
  2314. else
  2315. throw 0;
  2316. }
  2317. }
  2318. void CHeroList::mouseMoved (SDL_MouseMotionEvent & sEvent)
  2319. {
  2320. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2321. {
  2322. if (from>0)
  2323. LOCPLINT->adventureInt->statusbar.print(CGI->preth->zelp[303].first);
  2324. else
  2325. LOCPLINT->adventureInt->statusbar.clear();
  2326. return;
  2327. }
  2328. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2329. {
  2330. if ((items.size()-from) > 5)
  2331. LOCPLINT->adventureInt->statusbar.print(CGI->preth->zelp[304].first);
  2332. else
  2333. LOCPLINT->adventureInt->statusbar.clear();
  2334. return;
  2335. }
  2336. //if not buttons then heroes
  2337. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2338. hx-=pos.x;
  2339. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2340. int ny = hy/32;
  2341. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2342. {
  2343. LOCPLINT->adventureInt->statusbar.clear();
  2344. return;
  2345. }
  2346. std::vector<std::string> temp;
  2347. temp.push_back(items[from+ny].first->name);
  2348. temp.push_back(items[from+ny].first->type->heroClass->name);
  2349. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  2350. //select(ny+from);
  2351. }
  2352. void CHeroList::clickRight(tribool down)
  2353. {
  2354. if (down)
  2355. {
  2356. /***************************ARROWS*****************************************/
  2357. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2358. {
  2359. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[303].second,down,this);
  2360. }
  2361. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2362. {
  2363. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[304].second,down,this);
  2364. }
  2365. //if not buttons then heroes
  2366. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2367. hx-=pos.x;
  2368. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2369. int ny = hy/32;
  2370. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2371. {
  2372. return;
  2373. }
  2374. //show popup
  2375. 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);
  2376. ip->activate();
  2377. }
  2378. else
  2379. {
  2380. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[303].second,down,this);
  2381. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[304].second,down,this);
  2382. }
  2383. }
  2384. void CHeroList::hover (bool on)
  2385. {
  2386. }
  2387. void CHeroList::keyPressed (SDL_KeyboardEvent & key)
  2388. {
  2389. }
  2390. void CHeroList::updateHList()
  2391. {
  2392. items.clear();
  2393. genList();
  2394. if(selected>=items.size())
  2395. select(items.size()-1);
  2396. if(items.size()==0)
  2397. LOCPLINT->adventureInt->townList.select(0);
  2398. else
  2399. select(selected);
  2400. }
  2401. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  2402. {
  2403. int ser = LOCPLINT->cb->getHeroSerial(which);
  2404. ser -= from;
  2405. int pom = (which->movement)/100;
  2406. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  2407. }
  2408. void CHeroList::draw()
  2409. {
  2410. for (int iT=0+from;iT<5+from;iT++)
  2411. {
  2412. int i = iT-from;
  2413. if (iT>=items.size())
  2414. {
  2415. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32);
  2416. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32);
  2417. blitAt(empty,posporx,pospory+i*32);
  2418. continue;
  2419. }
  2420. const CGHeroInstance *cur = items[iT].first;
  2421. int pom = cur->movement / 100;
  2422. if (pom>25) pom=25;
  2423. if (pom<0) pom=0;
  2424. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32); //move point
  2425. pom = cur->mana / 5; //bylo: .../10;
  2426. if (pom>25) pom=25;
  2427. if (pom<0) pom=0;
  2428. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32); //mana
  2429. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  2430. blitAt(temp,posporx,pospory+i*32);
  2431. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2432. {
  2433. blitAt(selection,posporx,pospory+i*32);
  2434. }
  2435. //TODO: support for custom portraits
  2436. }
  2437. if (from>0)
  2438. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2439. else
  2440. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y);
  2441. if (items.size()-from>5)
  2442. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2443. else
  2444. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y);
  2445. }
  2446. int CHeroList::getPosOfHero(const CArmedInstance* h)
  2447. {
  2448. 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));
  2449. }
  2450. CTownList::~CTownList()
  2451. {
  2452. delete arrup;
  2453. delete arrdo;
  2454. }
  2455. CTownList::CTownList(int Size, SDL_Rect * Pos, int arupx, int arupy, int ardox, int ardoy)
  2456. :CList(Size)
  2457. {
  2458. pos = *Pos;
  2459. arrup = CDefHandler::giveDef("IAM014.DEF");
  2460. arrdo = CDefHandler::giveDef("IAM015.DEF");
  2461. arrupp.x=arupx;
  2462. arrupp.y=arupy;
  2463. arrupp.w=arrup->ourImages[0].bitmap->w;
  2464. arrupp.h=arrup->ourImages[0].bitmap->h;
  2465. arrdop.x=ardox;
  2466. arrdop.y=ardoy;
  2467. arrdop.w=arrdo->ourImages[0].bitmap->w;
  2468. arrdop.h=arrdo->ourImages[0].bitmap->h;
  2469. posporx = arrdop.x;
  2470. pospory = arrupp.y + arrupp.h;
  2471. pressed = indeterminate;
  2472. from = 0;
  2473. }
  2474. void CTownList::genList()
  2475. {
  2476. int howMany = LOCPLINT->cb->howManyTowns();
  2477. for (int i=0;i<howMany;i++)
  2478. {
  2479. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  2480. }
  2481. }
  2482. void CTownList::select(int which)
  2483. {
  2484. if (which>=items.size())
  2485. return;
  2486. selected = which;
  2487. if(!fun.empty())
  2488. fun();
  2489. }
  2490. void CTownList::mouseMoved (SDL_MouseMotionEvent & sEvent)
  2491. {
  2492. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2493. {
  2494. if (from>0)
  2495. LOCPLINT->statusbar->print(CGI->preth->zelp[306].first);
  2496. else
  2497. LOCPLINT->statusbar->clear();
  2498. return;
  2499. }
  2500. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2501. {
  2502. if ((items.size()-from) > SIZE)
  2503. LOCPLINT->statusbar->print(CGI->preth->zelp[307].first);
  2504. else
  2505. LOCPLINT->statusbar->clear();
  2506. return;
  2507. }
  2508. //if not buttons then towns
  2509. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2510. hx-=pos.x;
  2511. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2512. int ny = hy/32;
  2513. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2514. {
  2515. LOCPLINT->statusbar->clear();
  2516. return;
  2517. };
  2518. LOCPLINT->statusbar->print(items[from+ny]->name);
  2519. }
  2520. void CTownList::clickLeft(tribool down)
  2521. {
  2522. if (down)
  2523. {
  2524. /***************************ARROWS*****************************************/
  2525. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2526. {
  2527. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2528. pressed = true;
  2529. return;
  2530. }
  2531. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2532. {
  2533. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2534. pressed = false;
  2535. return;
  2536. }
  2537. /***************************TOWNS*****************************************/
  2538. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2539. hx-=pos.x;
  2540. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2541. int ny = hy/32;
  2542. if (ny>SIZE || ny<0)
  2543. return;
  2544. if (SIZE==5 && (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  2545. LOCPLINT->openTownWindow(items[selected]);//print town screen
  2546. else
  2547. select(ny+from);
  2548. }
  2549. else
  2550. {
  2551. if (indeterminate(pressed))
  2552. return;
  2553. if (pressed) //up
  2554. {
  2555. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2556. pressed = indeterminate;
  2557. if (!down)
  2558. {
  2559. from--;
  2560. if (from<0)
  2561. from=0;
  2562. draw();
  2563. }
  2564. }
  2565. else if (!pressed) //down
  2566. {
  2567. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2568. pressed = indeterminate;
  2569. if (!down)
  2570. {
  2571. from++;
  2572. //if (from<items.size()-5)
  2573. // from=items.size()-5;
  2574. draw();
  2575. }
  2576. }
  2577. else
  2578. throw 0;
  2579. }
  2580. }
  2581. void CTownList::clickRight(tribool down)
  2582. {
  2583. if (down)
  2584. {
  2585. /***************************ARROWS*****************************************/
  2586. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2587. {
  2588. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[306].second,down,this);
  2589. }
  2590. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2591. {
  2592. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[307].second,down,this);
  2593. }
  2594. //if not buttons then towns
  2595. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2596. hx-=pos.x;
  2597. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2598. int ny = hy/32;
  2599. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2600. {
  2601. return;
  2602. }
  2603. //show popup
  2604. 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);
  2605. ip->activate();
  2606. }
  2607. else
  2608. {
  2609. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[306].second,down,this);
  2610. LOCPLINT->adventureInt->handleRightClick(CGI->preth->zelp[307].second,down,this);
  2611. }
  2612. }
  2613. void CTownList::hover (bool on)
  2614. {
  2615. }
  2616. void CTownList::keyPressed (SDL_KeyboardEvent & key)
  2617. {
  2618. }
  2619. void CTownList::draw()
  2620. {
  2621. for (int iT=0+from;iT<SIZE+from;iT++)
  2622. {
  2623. int i = iT-from;
  2624. if (iT>=items.size())
  2625. {
  2626. blitAt(graphics->getPic(-1),posporx,pospory+i*32);
  2627. continue;
  2628. }
  2629. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32);
  2630. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  2631. {
  2632. blitAt(graphics->getPic(-2),posporx,pospory+i*32);
  2633. }
  2634. }
  2635. if (from>0)
  2636. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2637. else
  2638. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y);
  2639. if (items.size()-from>SIZE)
  2640. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2641. else
  2642. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y);
  2643. }
  2644. CCreaturePic::CCreaturePic(CCreature *cre, bool Big)
  2645. :c(cre),big(Big)
  2646. {
  2647. anim = new CCreatureAnimation(cre->animDefName);
  2648. }
  2649. CCreaturePic::~CCreaturePic()
  2650. {
  2651. delete anim;
  2652. }
  2653. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  2654. {
  2655. SDL_Rect dst;
  2656. if(big)
  2657. {
  2658. blitAt(graphics->backgrounds[c->faction],x,y);//curx-50,pos.y+130-65);
  2659. dst = genRect(130,100,x,y);
  2660. }
  2661. else
  2662. {
  2663. blitAt(graphics->backgroundsm[c->faction],x,y);//curx-50,pos.y+130-65);
  2664. dst = genRect(120,100,x,y);
  2665. }
  2666. if(c->isDoubleWide())
  2667. x-=15;
  2668. return anim->nextFrameMiddle(to,x+70,y+45,true,nextFrame,false,&dst);
  2669. }
  2670. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  2671. {
  2672. //TODO: write
  2673. return NULL;
  2674. }
  2675. void CRecrutationWindow::close()
  2676. {
  2677. deactivate();
  2678. delete this;
  2679. LOCPLINT->curint->activate();
  2680. }
  2681. void CRecrutationWindow::Max()
  2682. {
  2683. slider->moveTo(slider->amount);
  2684. }
  2685. void CRecrutationWindow::Buy()
  2686. {
  2687. recruit(creatures[which].ID,slider->value);
  2688. close();
  2689. }
  2690. void CRecrutationWindow::Cancel()
  2691. {
  2692. close();
  2693. }
  2694. void CRecrutationWindow::sliderMoved(int to)
  2695. {
  2696. }
  2697. void CRecrutationWindow::clickLeft(tribool down)
  2698. {
  2699. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2700. for(int i=0;i<creatures.size();i++)
  2701. {
  2702. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2703. {
  2704. which = i;
  2705. int newAmount = min(amounts[i],creatures[i].amount);
  2706. slider->amount = newAmount;
  2707. if(slider->value > newAmount)
  2708. slider->moveTo(newAmount);
  2709. else
  2710. slider->moveTo(slider->value);
  2711. curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2712. for(int j=0;j<creatures.size();j++)
  2713. {
  2714. if(which==j)
  2715. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  2716. else
  2717. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  2718. curx += 120;
  2719. }
  2720. break;
  2721. }
  2722. curx += 120;
  2723. }
  2724. }
  2725. void CRecrutationWindow::activate()
  2726. {
  2727. LOCPLINT->objsToBlit.push_back(this);
  2728. ClickableL::activate();
  2729. buy->activate();
  2730. max->activate();
  2731. cancel->activate();
  2732. slider->activate();
  2733. }
  2734. void CRecrutationWindow::deactivate()
  2735. {
  2736. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  2737. ClickableL::deactivate();
  2738. buy->deactivate();
  2739. max->deactivate();
  2740. cancel->deactivate();
  2741. slider->deactivate();
  2742. }
  2743. void CRecrutationWindow::show(SDL_Surface * to)
  2744. {
  2745. static char c=0;
  2746. blitAt(bitmap,pos.x,pos.y,to?to:screen);
  2747. buy->show();
  2748. max->show();
  2749. cancel->show();
  2750. slider->show();
  2751. char pom[15];
  2752. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  2753. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,screen);
  2754. SDL_itoa(slider->value,pom,10); //recruit
  2755. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,screen);
  2756. 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"
  2757. int curx = pos.x+115-creatures[which].res.size()*16;
  2758. for(int i=0;i<creatures[which].res.size();i++)
  2759. {
  2760. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,screen);
  2761. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,screen);
  2762. SDL_itoa(creatures[which].res[i].second,pom,10);
  2763. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,screen);
  2764. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  2765. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,screen);
  2766. curx+=32;
  2767. }
  2768. curx = pos.x + 192 + 102 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2769. for(int i=0;i<creatures.size();i++)
  2770. {
  2771. creatures[i].pic->blitPic(screen,curx-50,pos.y+130-65,!(c%4));
  2772. curx += 120;
  2773. }
  2774. c++;
  2775. }
  2776. CRecrutationWindow::CRecrutationWindow(const std::vector<std::pair<int,int> > &Creatures, const boost::function<void(int,int)> &Recruit) //creatures - pairs<creature_ID,amount>
  2777. :recruit(Recruit)
  2778. {
  2779. which = 0;
  2780. creatures.resize(Creatures.size());
  2781. amounts.resize(Creatures.size());
  2782. for(int i=0;i<creatures.size();i++)
  2783. {
  2784. creatures[i].amount = Creatures[i].second;
  2785. creatures[i].ID = Creatures[i].first;
  2786. for(int j=0;j<CGI->creh->creatures[Creatures[i].first].cost.size();j++)
  2787. if(CGI->creh->creatures[Creatures[i].first].cost[j])
  2788. creatures[i].res.push_back(std::make_pair(j,CGI->creh->creatures[Creatures[i].first].cost[j]));
  2789. creatures[i].pic = new CCreaturePic(&CGI->creh->creatures[Creatures[i].first]);
  2790. amounts[i] = CGI->creh->creatures[Creatures[i].first].maxAmount(LOCPLINT->cb->getResourceAmount());
  2791. }
  2792. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  2793. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  2794. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  2795. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2796. SDL_FreeSurface(hhlp);
  2797. pos.x = screen->w/2 - bitmap->w/2;
  2798. pos.y = screen->h/2 - bitmap->h/2;
  2799. pos.w = bitmap->w;
  2800. pos.h = bitmap->h;
  2801. 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);
  2802. std::string pom;
  2803. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  2804. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  2805. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  2806. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  2807. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  2808. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  2809. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  2810. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  2811. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  2812. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  2813. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  2814. //border for creatures
  2815. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  2816. for(int i=0;i<creatures.size();i++)
  2817. {
  2818. creatures[i].pos.x = curx+1;
  2819. creatures[i].pos.y = 65;
  2820. creatures[i].pos.w = 100;
  2821. creatures[i].pos.h = 130;
  2822. if(which==i)
  2823. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  2824. else
  2825. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  2826. curx += 120;
  2827. }
  2828. max = new AdventureMapButton("","",boost::bind(&CRecrutationWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF");
  2829. buy = new AdventureMapButton("","",boost::bind(&CRecrutationWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF");
  2830. cancel = new AdventureMapButton("","",boost::bind(&CRecrutationWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF");
  2831. if(!creatures[0].amount)
  2832. {
  2833. max->block(true);
  2834. buy->block(true);
  2835. }
  2836. }
  2837. CRecrutationWindow::~CRecrutationWindow()
  2838. {
  2839. for(int i=0;i<creatures.size();i++)
  2840. {
  2841. delete creatures[i].pic;
  2842. }
  2843. delete max;
  2844. delete buy;
  2845. delete cancel;
  2846. SDL_FreeSurface(bitmap);
  2847. delete slider;
  2848. }
  2849. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner)
  2850. {
  2851. c=cid;
  2852. slider = NULL;
  2853. gar = Owner;
  2854. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  2855. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2856. pos.x = screen->w/2 - bitmap->w/2;
  2857. pos.y = screen->h/2 - bitmap->h/2;
  2858. pos.w = bitmap->w;
  2859. pos.h = bitmap->h;
  2860. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF");
  2861. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF");
  2862. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,max,0,true);
  2863. a1 = max;
  2864. a2 = 0;
  2865. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  2866. anim->anim->setType(1);
  2867. std::string title = CGI->generaltexth->allTexts[256];
  2868. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  2869. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  2870. }
  2871. CSplitWindow::~CSplitWindow()
  2872. {
  2873. SDL_FreeSurface(bitmap);
  2874. delete ok;
  2875. delete cancel;
  2876. delete slider;
  2877. delete anim;
  2878. }
  2879. void CSplitWindow::activate()
  2880. {
  2881. LOCPLINT->objsToBlit.push_back(this);
  2882. KeyInterested::activate();
  2883. ok->activate();
  2884. cancel->activate();
  2885. slider->activate();
  2886. }
  2887. void CSplitWindow::deactivate()
  2888. {
  2889. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  2890. KeyInterested::deactivate();
  2891. ok->deactivate();
  2892. cancel->deactivate();
  2893. slider->deactivate();
  2894. }
  2895. void CSplitWindow::split()
  2896. {
  2897. gar->splitStacks(a2);
  2898. close();
  2899. }
  2900. void CSplitWindow::close()
  2901. {
  2902. deactivate();
  2903. delete this;
  2904. LOCPLINT->curint->activate();
  2905. }
  2906. void CSplitWindow::sliderMoved(int to)
  2907. {
  2908. a2 = to;
  2909. if(slider)
  2910. a1 = slider->amount - to;
  2911. }
  2912. void CSplitWindow::show(SDL_Surface * to)
  2913. {
  2914. char pom[15];
  2915. blitAt(bitmap,pos.x,pos.y,screen);
  2916. ok->show();
  2917. cancel->show();
  2918. slider->show();
  2919. SDL_itoa(a1,pom,10);
  2920. printAtMiddle(pom,pos.x+70,pos.y+237,GEOR16,zwykly,screen);
  2921. SDL_itoa(a2,pom,10);
  2922. printAtMiddle(pom,pos.x+233,pos.y+237,GEOR16,zwykly,screen);
  2923. anim->blitPic(screen,pos.x+20,pos.y+54,false);
  2924. anim->blitPic(screen,pos.x+177,pos.y+54,false);
  2925. }
  2926. void CSplitWindow::keyPressed (SDL_KeyboardEvent & key)
  2927. {
  2928. //TODO: make manual typing possible
  2929. }
  2930. void CCreInfoWindow::show(SDL_Surface * to)
  2931. {
  2932. char pom[15];
  2933. blitAt(bitmap,pos.x,pos.y,screen);
  2934. anim->blitPic(screen,pos.x+21,pos.y+48,(type) && !(anf%4));
  2935. if(++anf==4)
  2936. anf=0;
  2937. if(count.size())
  2938. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly);
  2939. if(upgrade)
  2940. upgrade->show();
  2941. if(dismiss)
  2942. dismiss->show();
  2943. if(ok)
  2944. ok->show();
  2945. }
  2946. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  2947. :ok(0),dismiss(0),upgrade(0),type(Type),dsm(Dsm),dependant(0)
  2948. {
  2949. active = false;
  2950. anf = 0;
  2951. c = &CGI->creh->creatures[Cid];
  2952. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  2953. pos.x = screen->w/2 - bitmap->w/2;
  2954. pos.y = screen->h/2 - bitmap->h/2;
  2955. pos.w = bitmap->w;
  2956. pos.h = bitmap->h;
  2957. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  2958. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  2959. anim = new CCreaturePic(c);
  2960. if(!type) anim->anim->setType(1);
  2961. char pom[25];int hlp=0;
  2962. if(creatureCount)
  2963. {
  2964. SDL_itoa(creatureCount,pom,10);
  2965. count = pom;
  2966. }
  2967. printAtMiddle(c->namePl,149,30,GEOR13,zwykly,bitmap); //creature name
  2968. //atttack
  2969. printAt(CGI->preth->zelp[435].first,155,48,GEOR13,zwykly,bitmap);
  2970. SDL_itoa(c->attack,pom,10);
  2971. if(State && State->attackBonus)
  2972. {
  2973. int hlp = log10f(c->attack)+2;
  2974. pom[hlp-1] = ' '; pom[hlp] = '(';
  2975. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  2976. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  2977. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2978. }
  2979. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  2980. //defense
  2981. printAt(CGI->preth->zelp[436].first,155,67,GEOR13,zwykly,bitmap);
  2982. SDL_itoa(c->defence,pom,10);
  2983. if(State && State->defenseBonus)
  2984. {
  2985. int hlp = log10f(c->defence)+2;
  2986. pom[hlp-1] = ' '; pom[hlp] = '(';
  2987. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  2988. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  2989. pom[hlp] = ')'; pom[hlp+1] = '\0';
  2990. }
  2991. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  2992. //shots
  2993. if(c->shots)
  2994. {
  2995. printAt(CGI->generaltexth->allTexts[198],155,86,GEOR13,zwykly,bitmap);
  2996. SDL_itoa(c->shots,pom,10);
  2997. printToWR(pom,276,99,GEOR13,zwykly,bitmap);
  2998. }
  2999. //damage
  3000. printAt(CGI->generaltexth->allTexts[199],155,105,GEOR13,zwykly,bitmap);
  3001. SDL_itoa(c->damageMin,pom,10);
  3002. hlp=log10f(c->damageMin)+2;
  3003. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  3004. SDL_itoa(c->damageMax,pom+hlp+2,10);
  3005. printToWR(pom,276,118,GEOR13,zwykly,bitmap);
  3006. //health
  3007. printAt(CGI->preth->zelp[439].first,155,124,GEOR13,zwykly,bitmap);
  3008. SDL_itoa(c->hitPoints,pom,10);
  3009. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  3010. //remaining health
  3011. if(State && State->currentHealth)
  3012. {
  3013. printAt(CGI->preth->zelp[440].first,155,143,GEOR13,zwykly,bitmap);
  3014. SDL_itoa(State->currentHealth,pom,10);
  3015. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  3016. }
  3017. //speed
  3018. printAt(CGI->preth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  3019. SDL_itoa(c->speed,pom,10);
  3020. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  3021. //luck and morale
  3022. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  3023. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  3024. //print abilities text - if r-click popup
  3025. if(type)
  3026. {
  3027. if(Upg && ui)
  3028. {
  3029. bool enough = true;
  3030. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  3031. {
  3032. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  3033. enough = false;
  3034. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  3035. }
  3036. if(enough)
  3037. {
  3038. CFunctionList<void()> fs[2];
  3039. fs[0] += Upg;
  3040. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  3041. fs[1] += boost::bind(&CCreInfoWindow::activate,this);
  3042. CFunctionList<void()> cfl;
  3043. cfl = boost::bind(&CCreInfoWindow::deactivate,this);
  3044. cfl += boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false,false);
  3045. upgrade = new AdventureMapButton("",CGI->preth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF");
  3046. }
  3047. else
  3048. {
  3049. upgrade = new AdventureMapButton("",CGI->preth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  3050. upgrade->callback.funcs.clear();
  3051. upgrade->bitmapOffset = 2;
  3052. }
  3053. }
  3054. if(Dsm)
  3055. {
  3056. CFunctionList<void()> fs[2];
  3057. //on dismiss confirmed
  3058. fs[0] += Dsm; //dismiss
  3059. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  3060. fs[1] += boost::bind(&CCreInfoWindow::activate,this);
  3061. CFunctionList<void()> cfl;
  3062. cfl = boost::bind(&CCreInfoWindow::deactivate,this);
  3063. cfl += boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false,false);
  3064. dismiss = new AdventureMapButton("",CGI->preth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF");
  3065. }
  3066. ok = new AdventureMapButton("",CGI->preth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF");
  3067. }
  3068. else
  3069. {
  3070. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  3071. }
  3072. }
  3073. CCreInfoWindow::~CCreInfoWindow()
  3074. {
  3075. SDL_FreeSurface(bitmap);
  3076. delete anim;
  3077. delete upgrade;
  3078. delete ok;
  3079. delete dismiss;
  3080. for(int i=0; i<upgResCost.size();i++)
  3081. delete upgResCost[i];
  3082. }
  3083. void CCreInfoWindow::activate()
  3084. {
  3085. if(active) return;
  3086. active = true;
  3087. if(!type)
  3088. ClickableR::activate();
  3089. LOCPLINT->objsToBlit.push_back(this);
  3090. if(ok)
  3091. ok->activate();
  3092. if(dismiss)
  3093. dismiss->activate();
  3094. if(upgrade)
  3095. upgrade->activate();
  3096. }
  3097. void CCreInfoWindow::close()
  3098. {
  3099. deactivate();
  3100. if(dynamic_cast<CHeroWindow*>(LOCPLINT->curint->subInt))
  3101. {
  3102. if(type)
  3103. LOCPLINT->curint->subInt->activate();
  3104. }
  3105. else
  3106. {
  3107. CCastleInterface *c = dynamic_cast<CCastleInterface*>(LOCPLINT->curint);
  3108. if(c)
  3109. c->showAll();
  3110. if(type)
  3111. LOCPLINT->curint->activate();
  3112. }
  3113. delete this;
  3114. }
  3115. void CCreInfoWindow::clickRight(boost::logic::tribool down)
  3116. {
  3117. if(down)
  3118. return;
  3119. close();
  3120. }
  3121. void CCreInfoWindow::dismissF()
  3122. {
  3123. dsm();
  3124. close();
  3125. }
  3126. void CCreInfoWindow::keyPressed (SDL_KeyboardEvent & key)
  3127. {
  3128. }
  3129. void CCreInfoWindow::deactivate()
  3130. {
  3131. if(!active) return;
  3132. active = false;
  3133. if(!type)
  3134. ClickableR::deactivate();
  3135. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  3136. if(ok)
  3137. ok->deactivate();
  3138. if(dismiss)
  3139. dismiss->deactivate();
  3140. if(upgrade)
  3141. upgrade->deactivate();
  3142. }
  3143. void CCreInfoWindow::onUpgradeYes()
  3144. {
  3145. dependant->close();
  3146. dependant->deactivate();
  3147. delete dependant;
  3148. LOCPLINT->curint->activate();
  3149. }
  3150. void CCreInfoWindow::onUpgradeNo()
  3151. {
  3152. }
  3153. void CLevelWindow::close()
  3154. {
  3155. deactivate();
  3156. for(int i=0;i<comps.size();i++)
  3157. {
  3158. if(comps[i]->selected)
  3159. {
  3160. cb(i);
  3161. break;
  3162. }
  3163. }
  3164. delete this;
  3165. LOCPLINT->curint->activate();
  3166. }
  3167. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  3168. {
  3169. heroType = hero->subID;
  3170. cb = callback;
  3171. for(int i=0;i<skills.size();i++)
  3172. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  3173. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  3174. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  3175. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3176. pos.x = screen->w/2 - bitmap->w/2;
  3177. pos.y = screen->h/2 - bitmap->h/2;
  3178. pos.w = bitmap->w;
  3179. pos.h = bitmap->h;
  3180. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF");
  3181. //draw window
  3182. char buf[100], buf2[100];
  3183. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  3184. sprintf(buf,buf2,hero->name.c_str());
  3185. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  3186. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  3187. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  3188. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  3189. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  3190. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  3191. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  3192. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  3193. for(int i=0;i<comps.size();i++)
  3194. {
  3195. comps[i]->pos.x = curx+pos.x;
  3196. comps[i]->pos.y = 326+pos.y;
  3197. if( i < (comps.size()-1) )
  3198. {
  3199. curx += 44 + 18; //skill width + margin to "or"
  3200. blitAt(ort,curx,346,bitmap);
  3201. curx += ort->w + 18;
  3202. }
  3203. }
  3204. SDL_FreeSurface(ort);
  3205. }
  3206. void CLevelWindow::selectionChanged(unsigned to)
  3207. {
  3208. for(int i=0;i<comps.size();i++)
  3209. if(i==to)
  3210. comps[i]->select(true);
  3211. else
  3212. comps[i]->select(false);
  3213. }
  3214. CLevelWindow::~CLevelWindow()
  3215. {
  3216. delete ok;
  3217. for(int i=0;i<comps.size();i++)
  3218. delete comps[i];
  3219. SDL_FreeSurface(bitmap);
  3220. }
  3221. void CLevelWindow::activate()
  3222. {
  3223. LOCPLINT->objsToBlit.push_back(this);
  3224. ok->activate();
  3225. for(int i=0;i<comps.size();i++)
  3226. comps[i]->activate();
  3227. }
  3228. void CLevelWindow::deactivate()
  3229. {
  3230. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  3231. ok->deactivate();
  3232. for(int i=0;i<comps.size();i++)
  3233. comps[i]->deactivate();
  3234. }
  3235. void CLevelWindow::show(SDL_Surface * to)
  3236. {
  3237. blitAt(bitmap,pos.x,pos.y,screen);
  3238. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y);
  3239. ok->show();
  3240. for(int i=0;i<comps.size();i++)
  3241. comps[i]->show();
  3242. }
  3243. void CMinorResDataBar::show(SDL_Surface * to)
  3244. {
  3245. blitAt(bg,pos.x,pos.y);
  3246. char * buf = new char[15];
  3247. for (int i=0;i<7;i++)
  3248. {
  3249. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3250. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly);
  3251. }
  3252. std::vector<std::string> temp;
  3253. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  3254. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  3255. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  3256. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  3257. CGI->generaltexth->allTexts[62]
  3258. +": %s, "
  3259. + CGI->generaltexth->allTexts[63]
  3260. + ": %s, "
  3261. + CGI->generaltexth->allTexts[64]
  3262. + ": %s",temp)
  3263. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly);
  3264. temp.clear();
  3265. //updateRect(&pos,screen);
  3266. delete[] buf;
  3267. }
  3268. CMinorResDataBar::CMinorResDataBar()
  3269. {
  3270. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  3271. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3272. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3273. pos.x = 7;
  3274. pos.y = 575;
  3275. pos.w = bg->w;
  3276. pos.h = bg->h;
  3277. }
  3278. CMinorResDataBar::~CMinorResDataBar()
  3279. {
  3280. SDL_FreeSurface(bg);
  3281. }
  3282. SDL_Surface * CCustomImgComponent::getImg()
  3283. {
  3284. return bmp;
  3285. }
  3286. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  3287. :bmp(sur), free(freeSur)
  3288. {
  3289. init(Type,Subtype,Val);
  3290. }
  3291. CCustomImgComponent::~CCustomImgComponent()
  3292. {
  3293. if(free)
  3294. SDL_FreeSurface(bmp);
  3295. }
  3296. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  3297. {
  3298. left = Left;
  3299. type = Type;
  3300. id = ID;
  3301. }
  3302. void CMarketplaceWindow::CTradeableItem::show( SDL_Surface * to/*=NULL*/ )
  3303. {
  3304. SDL_Surface *hlp = getSurface();
  3305. blitAt(hlp,pos.x+19,pos.y+9,to);
  3306. }
  3307. void CMarketplaceWindow::CTradeableItem::clickLeft( boost::logic::tribool down )
  3308. {
  3309. CMarketplaceWindow *mw = static_cast<CMarketplaceWindow *>(LOCPLINT->curint->subInt);
  3310. if(down)
  3311. {
  3312. if(left)
  3313. {
  3314. if(mw->hLeft != this)
  3315. mw->hLeft = this;
  3316. else
  3317. return;
  3318. }
  3319. else
  3320. {
  3321. if(mw->hRight != this)
  3322. mw->hRight = this;
  3323. else
  3324. return;
  3325. }
  3326. mw->selectionChanged(left);
  3327. }
  3328. }
  3329. void CMarketplaceWindow::CTradeableItem::activate()
  3330. {
  3331. ClickableL::activate();
  3332. }
  3333. void CMarketplaceWindow::CTradeableItem::deactivate()
  3334. {
  3335. ClickableL::deactivate();
  3336. }
  3337. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  3338. {
  3339. switch(type)
  3340. {
  3341. case 0:
  3342. return graphics->resources32->ourImages[id].bitmap;
  3343. case 1:
  3344. return graphics->artDefs->ourImages[id].bitmap;
  3345. default:
  3346. return NULL;
  3347. }
  3348. }
  3349. void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  3350. {
  3351. i.resize(amount);
  3352. for(int j=0;j<amount;j++)
  3353. {
  3354. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  3355. i[j]->pos = p[j];
  3356. }
  3357. }
  3358. void CMarketplaceWindow::setMode( int mode )
  3359. {
  3360. std::vector<SDL_Rect> lpos, rpos;
  3361. clear();
  3362. switch(mode)
  3363. {
  3364. case 0:
  3365. {
  3366. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  3367. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  3368. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  3369. bg = SDL_ConvertSurface(bg2,screen->format,0);
  3370. SDL_FreeSurface(bg2);
  3371. lpos += genRect(66,69,39,180), genRect(66,69,122,180), genRect(66,69,204,180),
  3372. genRect(66,69,39,259), genRect(66,69,122,259), genRect(66,69,204,259),
  3373. genRect(66,69,122,338);
  3374. for(int i=0;i<lpos.size();i++)
  3375. {
  3376. lpos[i].x += pos.x;
  3377. lpos[i].y += pos.y;
  3378. rpos.push_back(lpos[i]);
  3379. rpos[rpos.size()-1].x += 288;
  3380. }
  3381. initItems(left,lpos,0,7,true,NULL);
  3382. initItems(right,rpos,0,7,false,NULL);
  3383. printAtMiddle(CGI->generaltexth->allTexts[158],303,28,GEORXX,tytulowy,bg); //title
  3384. printAtMiddle(CGI->generaltexth->allTexts[270],158,148,GEOR13,zwykly,bg); //kingdom res.
  3385. printAtMiddle(CGI->generaltexth->allTexts[168],450,148,GEOR13,zwykly,bg); //available for trade
  3386. }
  3387. }
  3388. }
  3389. void CMarketplaceWindow::clear()
  3390. {
  3391. for(int i=0;i<left.size();i++)
  3392. delete left[i];
  3393. for(int i=0;i<right.size();i++)
  3394. delete right[i];
  3395. left.clear();
  3396. right.clear();
  3397. SDL_FreeSurface(bg);
  3398. }
  3399. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  3400. {
  3401. mode = Mode;
  3402. bg = NULL;
  3403. ok = max = deal = NULL;
  3404. pos.x = screen->w/2 - 300;
  3405. pos.y = screen->h/2 - 296;
  3406. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  3407. setMode(mode);
  3408. hLeft = hRight = NULL;
  3409. ok = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::deactivate,this),pos.x+516,pos.y+520,"IOK6432.DEF");
  3410. ok->callback += boost::bind(&CMarketplaceWindow::clear,this); //clear
  3411. ok->callback += boost::bind(vstd::delObj<CMarketplaceWindow>,this); //will delete
  3412. ok->callback += boost::bind(&CMainInterface::activate,LOCPLINT->curint);
  3413. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  3414. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  3415. max->block(true);
  3416. deal->block(true);
  3417. }
  3418. CMarketplaceWindow::~CMarketplaceWindow()
  3419. {
  3420. delete slider;
  3421. }
  3422. void CMarketplaceWindow::show( SDL_Surface * to/*=NULL*/ )
  3423. {
  3424. blitAt(bg,pos);
  3425. if(hRight)
  3426. CSDL_Ext::drawBorder(screen,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  3427. if(hLeft)
  3428. CSDL_Ext::drawBorder(screen,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  3429. ok->show();
  3430. deal->show();
  3431. max->show();
  3432. slider->show();
  3433. for(int i=0;i<left.size();i++)
  3434. left[i]->show();
  3435. for(int i=0;i<right.size();i++)
  3436. right[i]->show();
  3437. if(mode==0)
  3438. {
  3439. char buf[15];
  3440. for(int i=0;i<left.size();i++)
  3441. {
  3442. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3443. printAtMiddle(buf,left[i]->pos.x+35,left[i]->pos.y+56,GEOR13,zwykly);
  3444. }
  3445. if(hLeft) //print prices
  3446. {
  3447. for(int i=0; i<right.size();i++)
  3448. {
  3449. if(right[i]->id != hLeft->id)
  3450. printAtMiddle(rSubs[i],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly);
  3451. else
  3452. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly);
  3453. }
  3454. }
  3455. if(hLeft && hRight && hLeft->id!= hRight->id)
  3456. {
  3457. blitAt(hLeft->getSurface(),pos.x+142,pos.y+457,screen);
  3458. blitAt(hRight->getSurface(),pos.x+430,pos.y+457,screen);
  3459. SDL_itoa(slider->value * r1,buf,10);
  3460. printAtMiddle(buf,pos.x+158,pos.y+504,GEOR13,zwykly);
  3461. SDL_itoa(slider->value * r2,buf,10);
  3462. printAtMiddle(buf,pos.x+446,pos.y+504,GEOR13,zwykly);
  3463. }
  3464. }
  3465. }
  3466. void CMarketplaceWindow::activate()
  3467. {
  3468. LOCPLINT->objsToBlit += this;
  3469. LOCPLINT->curint->subInt = this;
  3470. for(int i=0;i<left.size();i++)
  3471. left[i]->activate();
  3472. for(int i=0;i<right.size();i++)
  3473. right[i]->activate();
  3474. ok->activate();
  3475. max->activate();
  3476. deal->activate();
  3477. slider->activate();
  3478. }
  3479. void CMarketplaceWindow::deactivate()
  3480. {
  3481. LOCPLINT->objsToBlit -= this;
  3482. LOCPLINT->curint->subInt = NULL;
  3483. for(int i=0;i<left.size();i++)
  3484. left[i]->deactivate();
  3485. for(int i=0;i<right.size();i++)
  3486. right[i]->deactivate();
  3487. ok->deactivate();
  3488. max->deactivate();
  3489. deal->deactivate();
  3490. slider->deactivate();
  3491. }
  3492. void CMarketplaceWindow::setMax()
  3493. {
  3494. slider->moveTo(slider->amount);
  3495. }
  3496. void CMarketplaceWindow::makeDeal()
  3497. {
  3498. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  3499. slider->moveTo(0);
  3500. hLeft = NULL;
  3501. selectionChanged(true);
  3502. }
  3503. void CMarketplaceWindow::sliderMoved( int to )
  3504. {
  3505. }
  3506. void CMarketplaceWindow::selectionChanged(bool side)
  3507. {
  3508. if(hLeft && hRight && hLeft->id!= hRight->id)
  3509. {
  3510. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  3511. slider->amount = LOCPLINT->cb->getResourceAmount(hLeft->id) / r1;
  3512. slider->moveTo(0);
  3513. max->block(false);
  3514. deal->block(false);
  3515. }
  3516. else
  3517. {
  3518. max->block(true);
  3519. deal->block(true);
  3520. slider->amount = 0;
  3521. slider->moveTo(0);
  3522. }
  3523. if(side && hLeft) //left selection changed, recalculate offers
  3524. {
  3525. rSubs.clear();
  3526. rSubs.resize(right.size());
  3527. int h1, h2;
  3528. for(int i=0;i<right.size();i++)
  3529. {
  3530. std::ostringstream oss;
  3531. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  3532. oss << h2;
  3533. if(h1!=1)
  3534. oss << "/" << h1;
  3535. rSubs[i] = oss.str();
  3536. }
  3537. }
  3538. }