CPlayerInterface.cpp 102 KB

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