CPlayerInterface.cpp 142 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793
  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 "CPlayerInterface.h"
  11. //#include "SDL_Extensions.h"
  12. #include "SDL_Extensions.h"
  13. //#include "SDL_framerate.h"
  14. #include "SDL_framerate.h"
  15. #include "client/CConfigHandler.h"
  16. #include "client/CCreatureAnimation.h"
  17. #include "client/Graphics.h"
  18. #include "hch/CArtHandler.h"
  19. #include "hch/CGeneralTextHandler.h"
  20. #include "hch/CHeroHandler.h"
  21. #include "hch/CLodHandler.h"
  22. #include "hch/CObjectHandler.h"
  23. #include "lib/Connection.h"
  24. #include "hch/CSpellHandler.h"
  25. #include "hch/CTownHandler.h"
  26. #include "lib/CondSh.h"
  27. #include "lib/NetPacks.h"
  28. #include "map.h"
  29. #include "mapHandler.h"
  30. #include "timeHandler.h"
  31. #include <boost/lexical_cast.hpp>
  32. #include <boost/algorithm/string.hpp>
  33. #include <boost/algorithm/string/replace.hpp>
  34. #include <boost/assign/std/vector.hpp>
  35. #include <boost/assign/list_of.hpp>
  36. #include <boost/date_time/posix_time/posix_time.hpp>
  37. #include <boost/thread.hpp>
  38. #include <cmath>
  39. #include <queue>
  40. #include <sstream>
  41. #ifdef min
  42. #undef min
  43. #endif
  44. #ifdef max
  45. #undef max
  46. #endif
  47. using namespace boost::assign;
  48. using namespace CSDL_Ext;
  49. extern TTF_Font * GEOR16;
  50. CPlayerInterface * LOCPLINT;
  51. extern std::queue<SDL_Event*> events;
  52. extern boost::mutex eventsM;
  53. void processCommand(const std::string &message, CClient *&client);
  54. struct OCM_HLP_CGIN
  55. {
  56. bool inline operator ()(const std::pair<const CGObjectInstance*,SDL_Rect> & a, const std::pair<const CGObjectInstance*,SDL_Rect> & b) const
  57. {
  58. return (*a.first)<(*b.first);
  59. }
  60. } ocmptwo_cgin ;
  61. void KeyShortcut::keyPressed(const SDL_KeyboardEvent & key)
  62. {
  63. if(vstd::contains(assignedKeys,key.keysym.sym))
  64. {
  65. if(key.state == SDL_PRESSED)
  66. clickLeft(true);
  67. else
  68. clickLeft(false);
  69. }
  70. }
  71. void CGarrisonSlot::hover (bool on)
  72. {
  73. Hoverable::hover(on);
  74. if(on)
  75. {
  76. std::string temp;
  77. if(creature)
  78. {
  79. if(owner->highlighted)
  80. {
  81. if(owner->highlighted == this)
  82. {
  83. temp = CGI->generaltexth->tcommands[4];
  84. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  85. }
  86. else if (owner->highlighted->creature == creature)
  87. {
  88. temp = CGI->generaltexth->tcommands[2];
  89. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  90. }
  91. else if (owner->highlighted->creature)
  92. {
  93. temp = CGI->generaltexth->tcommands[7];
  94. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  95. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  96. }
  97. else
  98. {
  99. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  100. tlog2 << "Highlighted set to NULL"<<std::endl;
  101. owner->highlighted = NULL;
  102. }
  103. }
  104. else
  105. {
  106. if(upg)
  107. {
  108. temp = CGI->generaltexth->tcommands[32];
  109. }
  110. else
  111. {
  112. temp = CGI->generaltexth->tcommands[12];
  113. }
  114. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  115. };
  116. }
  117. else
  118. {
  119. if(owner->highlighted)
  120. {
  121. const CArmedInstance *highl = owner->highlighted->getObj();
  122. if( highl->needsLastStack() //we are moving stack from hero's
  123. && highl->army.slots.size() == 1 //it's only stack
  124. && owner->highlighted->upg != upg //we're moving it to the other garrison
  125. )
  126. {
  127. temp = CGI->generaltexth->tcommands[5]; //cannot move last stack!
  128. }
  129. else
  130. {
  131. temp = CGI->generaltexth->tcommands[6];
  132. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  133. }
  134. }
  135. else
  136. {
  137. temp = CGI->generaltexth->tcommands[11];
  138. }
  139. }
  140. LOCPLINT->statusbar->print(temp);
  141. }
  142. else
  143. {
  144. LOCPLINT->statusbar->clear();
  145. }
  146. }
  147. const CArmedInstance * CGarrisonSlot::getObj()
  148. {
  149. return (!upg)?(owner->oup):(owner->odown);
  150. }
  151. StackState* getStackState(const CGObjectInstance *obj, int pos, bool town)
  152. {
  153. const CGHeroInstance *h = dynamic_cast<const CGHeroInstance *>(obj);
  154. if(!h) return NULL;
  155. StackState *pom = new StackState();
  156. pom->currentHealth = 0;
  157. pom->attackBonus = h->getPrimSkillLevel(0);
  158. pom->defenseBonus = h->getPrimSkillLevel(1);
  159. pom->luck = h->getCurrentLuck();
  160. pom->morale = h->getCurrentMorale(pos,town);
  161. pom->speedBonus = h->valOfBonuses(HeroBonus::STACKS_SPEED);
  162. return pom;
  163. }
  164. void CGarrisonSlot::clickRight (tribool down)
  165. {
  166. StackState *pom = getStackState(getObj(),ID,LOCPLINT->curint == LOCPLINT->castleInt);
  167. if(down && creature)
  168. {
  169. (new CCreInfoWindow(creature->idNumber,0,count,pom,boost::function<void()>(),boost::function<void()>(),NULL))
  170. ->activate();
  171. //LOCPLINT->curint->deactivate();
  172. }
  173. delete pom;
  174. }
  175. void CGarrisonSlot::clickLeft(tribool down)
  176. {
  177. if(owner->ignoreEvent)
  178. {
  179. owner->ignoreEvent = false;
  180. return;
  181. }
  182. if(down)
  183. {
  184. bool refr = false;
  185. if(owner->highlighted)
  186. {
  187. if(owner->highlighted == this) //view info
  188. {
  189. StackState *pom2 = getStackState(getObj(),ID,LOCPLINT->curint == LOCPLINT->castleInt);
  190. UpgradeInfo pom = LOCPLINT->cb->getUpgradeInfo(getObj(),ID);
  191. if(pom.oldID>=0)
  192. {
  193. (new CCreInfoWindow
  194. (creature->idNumber,1,count,pom2,
  195. boost::bind(&CCallback::upgradeCreature,LOCPLINT->cb,getObj(),ID,pom.newID[0]), //if upgrade is possible we'll bind proper function in callback
  196. boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID),&pom))
  197. ->activate();
  198. }
  199. else
  200. {
  201. (new CCreInfoWindow
  202. (creature->idNumber,1,count,pom2,0, boost::bind(&CCallback::dismissCreature,LOCPLINT->cb,getObj(),ID),NULL) )
  203. ->activate();
  204. }
  205. if(LOCPLINT->curint->subInt)
  206. LOCPLINT->curint->subInt->deactivate();
  207. else
  208. LOCPLINT->curint->deactivate();
  209. owner->highlighted = NULL;
  210. show();
  211. refr = true;
  212. delete pom2;
  213. }
  214. else if((owner->splitting || LOCPLINT->shiftPressed())
  215. && (!creature
  216. || (creature == owner->highlighted->creature))
  217. )
  218. {
  219. owner->p2 = ID; //store the second stack pos
  220. owner->pb = upg;//store the second stack owner (up or down army)
  221. owner->splitting = false;
  222. LOCPLINT->curint->deactivate();
  223. int totalAmount = owner->highlighted->count;
  224. if(creature)
  225. totalAmount += count;
  226. int last = -1;
  227. if(upg != owner->highlighted->upg) //not splitting within same army
  228. {
  229. if(owner->highlighted->getObj()->army.slots.size() == 1
  230. && owner->highlighted->getObj()->needsLastStack() )
  231. {
  232. last = 0;
  233. }
  234. if(getObj()->army.slots.size() == 1
  235. && getObj()->needsLastStack() )
  236. {
  237. last += 2;
  238. }
  239. }
  240. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  241. spw->activate();
  242. refr = true;
  243. }
  244. else if(creature != owner->highlighted->creature) //swap
  245. {
  246. LOCPLINT->cb->swapCreatures(
  247. (!upg)?(owner->oup):(owner->odown),
  248. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  249. ID,owner->highlighted->ID);
  250. }
  251. else //merge
  252. {
  253. LOCPLINT->cb->mergeStacks(
  254. (!owner->highlighted->upg)?(owner->oup):(owner->odown),
  255. (!upg)?(owner->oup):(owner->odown),
  256. owner->highlighted->ID,ID);
  257. }
  258. }
  259. else //highlight
  260. {
  261. if(creature)
  262. owner->highlighted = this;
  263. show();
  264. refr = true;
  265. }
  266. if(refr) {hover(false); hover(true); } //to refresh statusbar
  267. }
  268. }
  269. void CGarrisonSlot::activate()
  270. {
  271. if(!active) active=true;
  272. else return;
  273. ClickableL::activate();
  274. ClickableR::activate();
  275. Hoverable::activate();
  276. }
  277. void CGarrisonSlot::deactivate()
  278. {
  279. if(active) active=false;
  280. else return;
  281. ClickableL::deactivate();
  282. ClickableR::deactivate();
  283. Hoverable::deactivate();
  284. }
  285. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CCreature * Creature, int Count)
  286. {
  287. active = false;
  288. upg = Upg;
  289. count = Count;
  290. ID = IID;
  291. creature = Creature;
  292. pos.x = x;
  293. pos.y = y;
  294. pos.w = 58;
  295. pos.h = 64;
  296. owner = Owner;
  297. }
  298. CGarrisonSlot::~CGarrisonSlot()
  299. {
  300. if(active)
  301. deactivate();
  302. }
  303. void CGarrisonSlot::show()
  304. {
  305. if(creature)
  306. {
  307. char buf[15];
  308. SDL_itoa(count,buf,10);
  309. blitAt(graphics->bigImgs[creature->idNumber],pos);
  310. printToWR(buf,pos.x+56,pos.y+62,GEOR16,zwykly);
  311. if((owner->highlighted==this)
  312. || (owner->splitting && owner->highlighted->creature == creature))
  313. {
  314. blitAt(graphics->bigImgs[-1],pos);
  315. }
  316. }
  317. else //empty slot
  318. {
  319. SDL_Rect jakis1 = genRect(pos.h,pos.w,owner->offx+ID*(pos.w+owner->interx),owner->offy+upg*(pos.h+owner->intery)),
  320. jakis2 = pos;
  321. SDL_BlitSurface(owner->sur,&jakis1,screen,&jakis2);
  322. if(owner->splitting)
  323. blitAt(graphics->bigImgs[-1],pos);
  324. }
  325. }
  326. CGarrisonInt::~CGarrisonInt()
  327. {
  328. if(sup)
  329. {
  330. for(size_t i=0;i<sup->size();i++)
  331. {
  332. delete (*sup)[i];
  333. }
  334. delete sup;
  335. }
  336. if(sdown)
  337. {
  338. for(size_t i=0;i<sdown->size();i++)
  339. {
  340. delete (*sdown)[i]; //XXX what about smartpointers? boost or auto_ptr from std
  341. }
  342. delete sdown;
  343. }
  344. }
  345. void CGarrisonInt::show()
  346. {
  347. if(sup)
  348. {
  349. for(size_t i = 0; i<sup->size(); i++)
  350. {
  351. if((*sup)[i])
  352. {
  353. (*sup)[i]->show();
  354. }
  355. }
  356. }
  357. if(sdown)
  358. {
  359. for(size_t i = 0; i<sdown->size(); i++)
  360. {
  361. if((*sdown)[i])
  362. {
  363. (*sdown)[i]->show();
  364. }
  365. }
  366. }
  367. }
  368. void CGarrisonInt::deactiveteSlots()
  369. {
  370. if(sup)
  371. {
  372. for(int i = 0; i<sup->size(); i++)
  373. {
  374. if((*sup)[i])
  375. {
  376. (*sup)[i]->deactivate();
  377. }
  378. }
  379. }
  380. if(sdown)
  381. {
  382. for(int i = 0; i<sdown->size(); i++)
  383. {
  384. if((*sdown)[i])
  385. {
  386. (*sdown)[i]->deactivate();
  387. }
  388. }
  389. }
  390. }
  391. void CGarrisonInt::activeteSlots()
  392. {
  393. if(sup)
  394. {
  395. for(int i = 0; i<sup->size(); i++)
  396. {
  397. if((*sup)[i])
  398. {
  399. (*sup)[i]->activate();
  400. }
  401. }
  402. }
  403. if(sdown)
  404. {
  405. for(int i = 0; i<sdown->size(); i++)
  406. {
  407. if((*sdown)[i])
  408. {
  409. (*sdown)[i]->activate();
  410. }
  411. }
  412. }
  413. }
  414. void CGarrisonInt::createSlots()
  415. {
  416. if(set1)
  417. {
  418. sup = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  419. for
  420. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set1->slots.begin();
  421. i!=set1->slots.end(); i++)
  422. {
  423. (*sup)[i->first] =
  424. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y,i->first, 0,
  425. &CGI->creh->creatures[i->second.first],i->second.second);
  426. }
  427. for(int i=0; i<sup->size(); i++)
  428. if((*sup)[i] == NULL)
  429. (*sup)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y,i,0,NULL, 0);
  430. }
  431. if(set2)
  432. {
  433. sdown = new std::vector<CGarrisonSlot*>(7,(CGarrisonSlot *)(NULL));
  434. for
  435. (std::map<si32,std::pair<ui32,si32> >::const_iterator i=set2->slots.begin();
  436. i!=set2->slots.end(); i++)
  437. {
  438. (*sdown)[i->first] =
  439. new CGarrisonSlot(this, pos.x + (i->first*(58+interx)), pos.y + 64 + intery,i->first,1,
  440. &CGI->creh->creatures[i->second.first],i->second.second);
  441. }
  442. for(int i=0; i<sup->size(); i++)
  443. if((*sdown)[i] == NULL)
  444. (*sdown)[i] = new CGarrisonSlot(this, pos.x + (i*(58+interx)), pos.y + 64 + intery,i,1, NULL, 0);
  445. }
  446. }
  447. void CGarrisonInt::deleteSlots()
  448. {
  449. if(sup)
  450. {
  451. for(int i = 0; i<sup->size(); i++)
  452. {
  453. if((*sup)[i])
  454. {
  455. delete (*sup)[i];
  456. }
  457. }
  458. delete sup;
  459. sup = NULL;
  460. }
  461. if(sdown)
  462. {
  463. for(int i = 0; i<sdown->size(); i++)
  464. {
  465. if((*sdown)[i])
  466. {
  467. delete (*sdown)[i];
  468. }
  469. }
  470. delete sdown;
  471. sdown = NULL;
  472. }
  473. }
  474. void CGarrisonInt::recreateSlots()
  475. {
  476. splitting = false;
  477. highlighted = NULL;
  478. if(active)
  479. {
  480. deactiveteSlots();
  481. }
  482. deleteSlots();
  483. createSlots();
  484. if(active)
  485. {
  486. //ignoreEvent = true;
  487. activeteSlots();
  488. show();
  489. }
  490. }
  491. void CGarrisonInt::splitClick()
  492. {
  493. if(!highlighted)
  494. return;
  495. splitting = !splitting;
  496. show();
  497. }
  498. void CGarrisonInt::splitStacks(int am2)
  499. {
  500. LOCPLINT->cb->splitStack(
  501. (highlighted->upg)?(odown):(oup),
  502. (pb)?(odown):(oup),
  503. highlighted->ID,
  504. p2,
  505. am2);
  506. }
  507. CGarrisonInt::CGarrisonInt(int x, int y, int inx, int iny, SDL_Surface *&pomsur, int OX, int OY, const CArmedInstance *s1,
  508. const CArmedInstance *s2)
  509. :interx(inx),intery(iny),sur(pomsur),highlighted(NULL),sup(NULL),sdown(NULL),oup(s1),odown(s2),
  510. offx(OX),offy(OY)
  511. {
  512. active = false;
  513. splitting = false;
  514. set1 = LOCPLINT->cb->getGarrison(s1);
  515. set2 = LOCPLINT->cb->getGarrison(s2);
  516. ignoreEvent = false;
  517. update = true;
  518. pos.x=(x);
  519. pos.y=(y);
  520. pos.w=(58);
  521. pos.h=(64);
  522. createSlots();
  523. }
  524. void CGarrisonInt::activate()
  525. {
  526. active = true;
  527. if(sup)
  528. {
  529. for(int i = 0; i<sup->size(); i++)
  530. if((*sup)[i])
  531. (*sup)[i]->activate();
  532. }
  533. if(sdown)
  534. {
  535. for(int i = 0; i<sdown->size(); i++)
  536. if((*sdown)[i])
  537. (*sdown)[i]->activate();
  538. }
  539. }
  540. void CGarrisonInt::deactivate()
  541. {
  542. active = false;
  543. deactiveteSlots();
  544. }
  545. CInfoWindow::CInfoWindow(std::string text, int player, int charperline, const std::vector<SComponent*> &comps, std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons)
  546. {
  547. ID = -1;
  548. for(int i=0;i<Buttons.size();i++)
  549. {
  550. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  551. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  552. }
  553. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  554. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  555. for(int i=0;i<comps.size();i++)
  556. {
  557. components.push_back(comps[i]);
  558. }
  559. CMessage::drawIWindow(this,text,player,charperline);
  560. }
  561. CInfoWindow::CInfoWindow()
  562. {
  563. ID = -1;
  564. }
  565. void CInfoWindow::close()
  566. {
  567. deactivate();
  568. delete this;
  569. LOCPLINT->showingDialog->setn(false);
  570. }
  571. void CInfoWindow::show(SDL_Surface * to)
  572. {
  573. CSimpleWindow::show();
  574. for(int i=0;i<buttons.size();i++)
  575. buttons[i]->show();
  576. }
  577. CInfoWindow::~CInfoWindow()
  578. {
  579. if(delComps)
  580. {
  581. for (int i=0;i<components.size();i++)
  582. delete components[i];
  583. }
  584. for(int i=0;i<buttons.size();i++)
  585. delete buttons[i];
  586. }
  587. void CInfoWindow::activate()
  588. {
  589. for (int i=0;i<components.size();i++)
  590. components[i]->activate();
  591. for(int i=0;i<buttons.size();i++)
  592. buttons[i]->activate();
  593. }
  594. void CInfoWindow::deactivate()
  595. {
  596. for (int i=0;i<components.size();i++)
  597. components[i]->deactivate();
  598. for(int i=0;i<buttons.size();i++)
  599. buttons[i]->deactivate();
  600. LOCPLINT->removeObjToBlit(this);
  601. }
  602. void CRClickPopup::clickRight (tribool down)
  603. {
  604. if(down)
  605. return;
  606. close();
  607. }
  608. void CRClickPopup::activate()
  609. {
  610. ClickableR::activate();
  611. LOCPLINT->objsToBlit.push_back(this);
  612. }
  613. void CRClickPopup::deactivate()
  614. {
  615. ClickableR::deactivate();
  616. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  617. }
  618. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  619. :bitmap(Bitmap),free(Free)
  620. {
  621. pos.x = x;
  622. pos.y = y;
  623. pos.h = bitmap->h;
  624. pos.w = bitmap->w;
  625. }
  626. void CInfoPopup::close()
  627. {
  628. deactivate();
  629. if(free)
  630. SDL_FreeSurface(bitmap);
  631. delete this;
  632. if(LOCPLINT->curint->subInt)
  633. return;
  634. if(LOCPLINT->curint == LOCPLINT->adventureInt)
  635. LOCPLINT->adventureInt->show();
  636. else if(LOCPLINT->curint == LOCPLINT->castleInt)
  637. LOCPLINT->castleInt->showAll();
  638. }
  639. void CInfoPopup::show(SDL_Surface * to)
  640. {
  641. blitAt(bitmap,pos.x,pos.y,(to)?(to):(screen));
  642. }
  643. void SComponent::init(Etype Type, int Subtype, int Val)
  644. {
  645. std::ostringstream oss;
  646. switch (Type)
  647. {
  648. case artifact:
  649. description = CGI->arth->artifacts[Subtype].Description();
  650. subtitle = CGI->arth->artifacts[Subtype].Name();
  651. break;
  652. case primskill:
  653. description = CGI->generaltexth->arraytxt[2+Subtype];
  654. oss << ((Val>0)?("+"):("-")) << Val << " " << CGI->generaltexth->primarySkillNames[Subtype];
  655. subtitle = oss.str();
  656. break;
  657. case secskill44: case secskill:
  658. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  659. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  660. break;
  661. case resource:
  662. description = CGI->generaltexth->allTexts[242];
  663. oss << Val;
  664. subtitle = oss.str();
  665. break;
  666. case spell:
  667. description = CGI->spellh->spells[Subtype].descriptions[Val];
  668. subtitle = CGI->spellh->spells[Subtype].name;
  669. break;
  670. case experience:
  671. description = CGI->generaltexth->allTexts[241];
  672. oss << Val ;
  673. if(Subtype && Val==1)
  674. {
  675. subtitle = CGI->generaltexth->allTexts[442];
  676. }
  677. else
  678. {
  679. subtitle = oss.str();
  680. }
  681. break;
  682. }
  683. type = Type;
  684. subtype = Subtype;
  685. val = Val;
  686. SDL_Surface * temp = this->getImg();
  687. if(!temp)
  688. {
  689. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  690. return;
  691. }
  692. pos.w = temp->w;
  693. pos.h = temp->h;
  694. }
  695. SComponent::SComponent(Etype Type, int Subtype, int Val)
  696. {
  697. init(Type,Subtype,Val);
  698. }
  699. SComponent::SComponent(const Component &c)
  700. {
  701. if(c.id==5)
  702. init(experience,c.subtype,c.val);
  703. else if(c.id == Component::SPELL)
  704. init(spell,c.subtype,c.val);
  705. else
  706. init((Etype)c.id,c.subtype,c.val);
  707. if(c.id==2 && c.when==-1)
  708. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  709. }
  710. void SComponent::show(SDL_Surface * to)
  711. {
  712. blitAt(getImg(),pos.x,pos.y,to);
  713. }
  714. SDL_Surface * SComponent::getImg()
  715. {
  716. switch (type)
  717. {
  718. case artifact:
  719. return graphics->artDefs->ourImages[subtype].bitmap;
  720. break;
  721. case primskill:
  722. return graphics->pskillsb->ourImages[subtype].bitmap;
  723. break;
  724. case secskill44:
  725. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  726. break;
  727. case secskill:
  728. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  729. break;
  730. case resource:
  731. return graphics->resources->ourImages[subtype].bitmap;
  732. break;
  733. case experience:
  734. return graphics->pskillsb->ourImages[4].bitmap;
  735. break;
  736. case morale:
  737. return graphics->morale82->ourImages[val+3].bitmap;
  738. break;
  739. case luck:
  740. return graphics->luck82->ourImages[val+3].bitmap;
  741. break;
  742. case spell:
  743. return graphics->spellscr->ourImages[subtype].bitmap;
  744. break;
  745. }
  746. return NULL;
  747. }
  748. void SComponent::clickRight (tribool down)
  749. {
  750. LOCPLINT->adventureInt->handleRightClick(description,down,this);
  751. }
  752. void SComponent::activate()
  753. {
  754. ClickableR::activate();
  755. }
  756. void SComponent::deactivate()
  757. {
  758. ClickableR::deactivate();
  759. }
  760. void CSelectableComponent::clickLeft(tribool down)
  761. {
  762. if (down)
  763. {
  764. if(onSelect)
  765. onSelect();
  766. }
  767. }
  768. void CSelectableComponent::init(SDL_Surface * Border)
  769. {
  770. SDL_Surface * symb = SComponent::getImg();
  771. myBitmap = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  772. SDL_SetColorKey(myBitmap,SDL_SRCCOLORKEY,SDL_MapRGB(myBitmap->format,0,255,255));
  773. blitAt(symb,1,1,myBitmap);
  774. if (Border) //use custom border
  775. {
  776. border = Border;
  777. customB = true;
  778. }
  779. else //we need to draw border
  780. {
  781. customB = false;
  782. border = CSDL_Ext::newSurface(symb->w+2,symb->h+2,screen);
  783. SDL_FillRect(border,NULL,0x00FFFF);
  784. for (int i=0;i<border->w;i++)
  785. {
  786. SDL_PutPixelWithoutRefresh(border,i,0,239,215,123);
  787. SDL_PutPixelWithoutRefresh(border,i,(border->h)-1,239,215,123);
  788. }
  789. for (int i=0;i<border->h;i++)
  790. {
  791. SDL_PutPixelWithoutRefresh(border,0,i,239,215,123);
  792. SDL_PutPixelWithoutRefresh(border,(border->w)-1,i,239,215,123);
  793. }
  794. SDL_SetColorKey(border,SDL_SRCCOLORKEY,SDL_MapRGB(border->format,0,255,255));
  795. }
  796. selected = false;
  797. }
  798. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect, SDL_Surface * Border)
  799. :SComponent(c),onSelect(OnSelect)
  800. {
  801. init(Border);
  802. }
  803. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect, SDL_Surface * Border)
  804. :SComponent(Type,Sub,Val),onSelect(OnSelect)
  805. {
  806. init(Border);
  807. }
  808. CSelectableComponent::~CSelectableComponent()
  809. {
  810. SDL_FreeSurface(myBitmap);
  811. if (!customB)
  812. SDL_FreeSurface(border);
  813. }
  814. void CSelectableComponent::activate()
  815. {
  816. KeyInterested::activate();
  817. SComponent::activate();
  818. ClickableL::activate();
  819. }
  820. void CSelectableComponent::deactivate()
  821. {
  822. KeyInterested::deactivate();
  823. SComponent::deactivate();
  824. ClickableL::deactivate();
  825. }
  826. SDL_Surface * CSelectableComponent::getImg()
  827. {
  828. return myBitmap;
  829. }
  830. void CSelectableComponent::select(bool on)
  831. {
  832. if(on != selected)
  833. {
  834. SDL_FillRect(myBitmap,NULL,0x000000);
  835. blitAt(SComponent::getImg(),1,1,myBitmap);
  836. if (on)
  837. {
  838. blitAt(border,0,0,myBitmap);
  839. }
  840. selected = on;
  841. return;
  842. }
  843. else
  844. {
  845. return;
  846. }
  847. }
  848. void CSelectableComponent::show(SDL_Surface * to)
  849. {
  850. blitAt(myBitmap,pos.x,pos.y,to);
  851. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+14,GEOR13,12,zwykly,to);
  852. }
  853. void CSimpleWindow::show(SDL_Surface * to)
  854. {
  855. if(!to)
  856. to=screen;
  857. blitAt(bitmap,pos.x,pos.y,to);
  858. }
  859. CSimpleWindow::~CSimpleWindow()
  860. {
  861. if (bitmap)
  862. {
  863. SDL_FreeSurface(bitmap);
  864. bitmap=NULL;
  865. }
  866. }
  867. void CSelWindow::selectionChange(unsigned to)
  868. {
  869. for (unsigned i=0;i<components.size();i++)
  870. {
  871. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  872. if (!pom)
  873. continue;
  874. pom->select(i==to);
  875. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  876. }
  877. }
  878. CSelWindow::CSelWindow(const std::string &text, int player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, int askID)
  879. {
  880. ID = askID;
  881. for(int i=0;i<Buttons.size();i++)
  882. {
  883. buttons.push_back(new AdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  884. if(!i && askID >= 0)
  885. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  886. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  887. }
  888. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  889. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  890. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  891. buttons.back()->callback += boost::bind(&ICallback::selectionMade,LOCPLINT->cb,0,askID);
  892. for(int i=0;i<comps.size();i++)
  893. {
  894. components.push_back(comps[i]);
  895. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  896. if(i<9)
  897. comps[i]->assignedKeys.insert(SDLK_1+i);
  898. }
  899. CMessage::drawIWindow(this,text,player,charperline);
  900. }
  901. void CSelWindow::close()
  902. {
  903. deactivate();
  904. LOCPLINT->showingDialog->setn(false);
  905. delete this;
  906. }
  907. void CSelWindow::madeChoice()
  908. {
  909. if(ID < 0)
  910. return;
  911. int ret = -1;
  912. for (int i=0;i<components.size();i++)
  913. {
  914. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  915. {
  916. ret = i;
  917. }
  918. }
  919. LOCPLINT->cb->selectionMade(ret+1,ID);
  920. }
  921. CButtonBase::CButtonBase()
  922. {
  923. bitmapOffset = 0;
  924. curimg=0;
  925. type=-1;
  926. abs=false;
  927. active=false;
  928. notFreeButton = false;
  929. ourObj=NULL;
  930. state=0;
  931. }
  932. CButtonBase::~CButtonBase()
  933. {
  934. if(notFreeButton)
  935. return;
  936. for(int i =0; i<imgs.size();i++)
  937. for(int j=0;j<imgs[i].size();j++)
  938. SDL_FreeSurface(imgs[i][j]);
  939. }
  940. void CButtonBase::show(SDL_Surface * to)
  941. {
  942. if(!to)
  943. to=screen;
  944. if (abs)
  945. {
  946. blitAt(imgs[curimg]
  947. [( (state+bitmapOffset) < (imgs[curimg].size()) ) ?
  948. (state+bitmapOffset) :
  949. (imgs[curimg].size()-1) ]
  950. ,pos.x,pos.y,to);
  951. //updateRect(&pos,to);
  952. }
  953. else
  954. {
  955. blitAt(imgs[curimg]
  956. [( (state+bitmapOffset) < (imgs[curimg].size()) ) ?
  957. (state+bitmapOffset) :
  958. (imgs[curimg].size()-1) ],pos.x+ourObj->pos.x,pos.y+ourObj->pos.y,to);
  959. //updateRect(&genRect(pos.h,pos.w,pos.x+ourObj->pos.x,pos.y+ourObj->pos.y),to);
  960. }
  961. }
  962. ClickableL::ClickableL()
  963. {
  964. pressedL=false;
  965. }
  966. ClickableL::~ClickableL()
  967. {}
  968. void ClickableL::clickLeft(tribool down)
  969. {
  970. if (down)
  971. pressedL=true;
  972. else
  973. pressedL=false;
  974. }
  975. void ClickableL::activate()
  976. {
  977. LOCPLINT->lclickable.push_front(this);
  978. }
  979. void ClickableL::deactivate()
  980. {
  981. LOCPLINT->lclickable.erase
  982. (std::find(LOCPLINT->lclickable.begin(),LOCPLINT->lclickable.end(),this));
  983. }
  984. ClickableR::ClickableR()
  985. {
  986. pressedR=false;
  987. }
  988. ClickableR::~ClickableR()
  989. {}
  990. void ClickableR::clickRight(tribool down)
  991. {
  992. if (down)
  993. pressedR=true;
  994. else
  995. pressedR=false;
  996. }
  997. void ClickableR::activate()
  998. {
  999. LOCPLINT->rclickable.push_front(this);
  1000. }
  1001. void ClickableR::deactivate()
  1002. {
  1003. LOCPLINT->rclickable.erase(std::find(LOCPLINT->rclickable.begin(),LOCPLINT->rclickable.end(),this));
  1004. }
  1005. //ClickableR
  1006. Hoverable::~Hoverable()
  1007. {}
  1008. void Hoverable::activate()
  1009. {
  1010. LOCPLINT->hoverable.push_front(this);
  1011. }
  1012. void Hoverable::deactivate()
  1013. {
  1014. LOCPLINT->hoverable.erase(std::find(LOCPLINT->hoverable.begin(),LOCPLINT->hoverable.end(),this));
  1015. }
  1016. void Hoverable::hover(bool on)
  1017. {
  1018. hovered=on;
  1019. }
  1020. //Hoverable
  1021. KeyInterested::~KeyInterested()
  1022. {}
  1023. void KeyInterested::activate()
  1024. {
  1025. LOCPLINT->keyinterested.push_front(this);
  1026. }
  1027. void KeyInterested::deactivate()
  1028. {
  1029. LOCPLINT->
  1030. keyinterested.erase(std::find(LOCPLINT->keyinterested.begin(),LOCPLINT->keyinterested.end(),this));
  1031. }
  1032. //KeyInterested
  1033. void MotionInterested::activate()
  1034. {
  1035. LOCPLINT->motioninterested.push_front(this);
  1036. }
  1037. void MotionInterested::deactivate()
  1038. {
  1039. LOCPLINT->
  1040. motioninterested.erase(std::find(LOCPLINT->motioninterested.begin(),LOCPLINT->motioninterested.end(),this));
  1041. }
  1042. void TimeInterested::activate()
  1043. {
  1044. LOCPLINT->timeinterested.push_back(this);
  1045. }
  1046. void TimeInterested::deactivate()
  1047. {
  1048. LOCPLINT->
  1049. timeinterested.erase(std::find(LOCPLINT->timeinterested.begin(),LOCPLINT->timeinterested.end(),this));
  1050. }
  1051. CPlayerInterface::CPlayerInterface(int Player, int serial)
  1052. {
  1053. LOCPLINT = this;
  1054. curAction = NULL;
  1055. curint = NULL;
  1056. playerID=Player;
  1057. serialID=serial;
  1058. human=true;
  1059. castleInt = NULL;
  1060. adventureInt = NULL;
  1061. pim = new boost::recursive_mutex;
  1062. showingDialog = new CondSh<bool>(false);
  1063. heroMoveSpeed = 2;
  1064. mapScrollingSpeed = 2;
  1065. //initializing framerate keeper
  1066. mainFPSmng = new FPSmanager;
  1067. SDL_initFramerate(mainFPSmng);
  1068. SDL_setFramerate(mainFPSmng, 48);
  1069. //framerate keeper initialized
  1070. cingconsole = new CInGameConsole;
  1071. }
  1072. CPlayerInterface::~CPlayerInterface()
  1073. {
  1074. delete pim;
  1075. delete showingDialog;
  1076. delete mainFPSmng;
  1077. delete adventureInt;
  1078. delete cingconsole;
  1079. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!= graphics->heroWins.end(); i++)
  1080. SDL_FreeSurface(i->second);
  1081. for(std::map<int,SDL_Surface*>::iterator i=graphics->townWins.begin(); i!= graphics->townWins.end(); i++)
  1082. SDL_FreeSurface(i->second);
  1083. }
  1084. void CPlayerInterface::init(ICallback * CB)
  1085. {
  1086. cb = dynamic_cast<CCallback*>(CB);
  1087. adventureInt = new CAdvMapInt(playerID);
  1088. std::vector<const CGTownInstance*> tt = cb->getTownsInfo(false);
  1089. for(int i=0;i<tt.size();i++)
  1090. {
  1091. SDL_Surface * pom = infoWin(tt[i]);
  1092. graphics->townWins.insert(std::pair<int,SDL_Surface*>(tt[i]->id,pom));
  1093. }
  1094. }
  1095. void CPlayerInterface::yourTurn()
  1096. {
  1097. try
  1098. {
  1099. LOCPLINT = this;
  1100. makingTurn = true;
  1101. static int autosaveCount = 0;
  1102. LOCPLINT->cb->save("Autosave_" + boost::lexical_cast<std::string>(autosaveCount++ + 1));
  1103. autosaveCount %= 5;
  1104. for(std::map<int,SDL_Surface*>::iterator i=graphics->heroWins.begin(); i!=graphics->heroWins.end();i++) //redraw hero infoboxes
  1105. SDL_FreeSurface(i->second);
  1106. graphics->heroWins.clear();
  1107. std::vector <const CGHeroInstance *> hh = cb->getHeroesInfo(false);
  1108. for(int i=0;i<hh.size();i++)
  1109. {
  1110. SDL_Surface * pom = infoWin(hh[i]);
  1111. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hh[i]->subID,pom));
  1112. }
  1113. adventureInt->infoBar.newDay(cb->getDate(1));
  1114. //select first hero if available.
  1115. //TODO: check if hero is slept
  1116. if(adventureInt->heroList.items.size())
  1117. adventureInt->select(adventureInt->heroList.items[0].first);
  1118. else
  1119. adventureInt->select(adventureInt->townList.items[0]);
  1120. adventureInt->activate();
  1121. timeHandler th;
  1122. th.getDif();
  1123. while(makingTurn) // main loop
  1124. {
  1125. updateWater();
  1126. pim->lock();
  1127. //if there are any waiting dialogs, show them
  1128. if(dialogs.size() && !showingDialog->get())
  1129. {
  1130. dialogs.front()->buttons[0]->callback += boost::bind(&IActivable::activate,LOCPLINT->curint);
  1131. showingDialog->set(true);
  1132. curint->deactivate(); //dezaktywacja starego interfejsu
  1133. dialogs.front()->activate();
  1134. LOCPLINT->objsToBlit.push_back(dialogs.front());
  1135. dialogs.pop_front();
  1136. }
  1137. int tv = th.getDif();
  1138. std::list<TimeInterested*> hlp = timeinterested;
  1139. for (std::list<TimeInterested*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1140. {
  1141. if(!vstd::contains(timeinterested,*i)) continue;
  1142. if ((*i)->toNextTick>=0)
  1143. (*i)->toNextTick-=tv;
  1144. if ((*i)->toNextTick<0)
  1145. (*i)->tick();
  1146. }
  1147. LOCPLINT->adventureInt->updateScreen = false;
  1148. while(true)
  1149. {
  1150. SDL_Event *ev = NULL;
  1151. {
  1152. boost::unique_lock<boost::mutex> lock(eventsM);
  1153. if(!events.size())
  1154. {
  1155. break;
  1156. }
  1157. else
  1158. {
  1159. ev = events.front();
  1160. events.pop();
  1161. }
  1162. }
  1163. handleEvent(ev);
  1164. delete ev;
  1165. }
  1166. if (curint == adventureInt) //stuff for advMapInt
  1167. {
  1168. adventureInt->update();
  1169. }
  1170. for(int i=0;i<objsToBlit.size();i++)
  1171. objsToBlit[i]->show();
  1172. CGI->curh->draw1();
  1173. CSDL_Ext::update(screen);
  1174. CGI->curh->draw2();
  1175. pim->unlock();
  1176. SDL_framerateDelay(mainFPSmng);
  1177. }
  1178. adventureInt->deactivate();
  1179. cb->endTurn();
  1180. } HANDLE_EXCEPTION
  1181. }
  1182. inline void subRect(const int & x, const int & y, const int & z, const SDL_Rect & r, const int & hid)
  1183. {
  1184. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  1185. for(int h=0; h<hlp.objects.size(); ++h)
  1186. if(hlp.objects[h].first->id==hid)
  1187. {
  1188. hlp.objects[h].second = r;
  1189. return;
  1190. }
  1191. }
  1192. inline void delObjRect(const int & x, const int & y, const int & z, const int & hid)
  1193. {
  1194. TerrainTile2 & hlp = CGI->mh->ttiles[x][y][z];
  1195. for(int h=0; h<hlp.objects.size(); ++h)
  1196. if(hlp.objects[h].first->id==hid)
  1197. {
  1198. hlp.objects.erase(hlp.objects.begin()+h);
  1199. return;
  1200. }
  1201. }
  1202. int getDir(int3 src, int3 dst)
  1203. {
  1204. int ret = -1;
  1205. if(dst.x+1 == src.x && dst.y+1 == src.y) //tl
  1206. {
  1207. ret = 1;
  1208. }
  1209. else if(dst.x == src.x && dst.y+1 == src.y) //t
  1210. {
  1211. ret = 2;
  1212. }
  1213. else if(dst.x-1 == src.x && dst.y+1 == src.y) //tr
  1214. {
  1215. ret = 3;
  1216. }
  1217. else if(dst.x-1 == src.x && dst.y == src.y) //r
  1218. {
  1219. ret = 4;
  1220. }
  1221. else if(dst.x-1 == src.x && dst.y-1 == src.y) //br
  1222. {
  1223. ret = 5;
  1224. }
  1225. else if(dst.x == src.x && dst.y-1 == src.y) //b
  1226. {
  1227. ret = 6;
  1228. }
  1229. else if(dst.x+1 == src.x && dst.y-1 == src.y) //bl
  1230. {
  1231. ret = 7;
  1232. }
  1233. else if(dst.x+1 == src.x && dst.y == src.y) //l
  1234. {
  1235. ret = 8;
  1236. }
  1237. return ret;
  1238. }
  1239. void CPlayerInterface::heroMoved(const HeroMoveDetails & details)
  1240. {
  1241. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1242. adventureInt->centerOn(details.ho->pos); //actualizing screen pos
  1243. if(adventureInt == curint)
  1244. adventureInt->minimap.draw();
  1245. if(details.style>0 || details.src == details.dst)
  1246. return;
  1247. //initializing objects and performing first step of move
  1248. const CGHeroInstance * ho = details.ho; //object representing this hero
  1249. int3 hp = details.src;
  1250. if (!details.successful) //hero failed to move
  1251. {
  1252. if(ho->movement > 50)
  1253. ho->moveDir = getDir(details.src,details.dst);
  1254. ho->isStanding = true;
  1255. adventureInt->heroList.draw();
  1256. if (adventureInt->terrain.currentPath && ho->movement>145) //TODO: better condition on movement - check cost of endtile
  1257. {
  1258. delete adventureInt->terrain.currentPath;
  1259. adventureInt->terrain.currentPath = NULL;
  1260. adventureInt->heroList.items[adventureInt->heroList.getPosOfHero(ho)].second = NULL;
  1261. }
  1262. return;
  1263. }
  1264. if (adventureInt->terrain.currentPath) //&& hero is moving
  1265. {
  1266. adventureInt->terrain.currentPath->nodes.erase(adventureInt->terrain.currentPath->nodes.end()-1);
  1267. }
  1268. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1269. {
  1270. ho->moveDir = 1;
  1271. ho->isStanding = false;
  1272. CGI->mh->ttiles[hp.x-3][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -31)));
  1273. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, -31)));
  1274. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, -31)));
  1275. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, -31)));
  1276. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 1)));
  1277. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 1), ho->id);
  1278. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 1), ho->id);
  1279. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 1), ho->id);
  1280. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 33)));
  1281. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 33), ho->id);
  1282. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 33), ho->id);
  1283. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 33), ho->id);
  1284. 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);
  1285. 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);
  1286. 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);
  1287. 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);
  1288. 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);
  1289. 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);
  1290. }
  1291. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1292. {
  1293. ho->moveDir = 2;
  1294. ho->isStanding = false;
  1295. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, -31)));
  1296. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, -31)));
  1297. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, -31)));
  1298. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1), ho->id);
  1299. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1), ho->id);
  1300. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1), ho->id);
  1301. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33), ho->id);
  1302. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33), ho->id);
  1303. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33), ho->id);
  1304. 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);
  1305. 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);
  1306. 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);
  1307. }
  1308. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1309. {
  1310. ho->moveDir = 3;
  1311. ho->isStanding = false;
  1312. CGI->mh->ttiles[hp.x-2][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, -31)));
  1313. CGI->mh->ttiles[hp.x-1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, -31)));
  1314. CGI->mh->ttiles[hp.x][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, -31)));
  1315. CGI->mh->ttiles[hp.x+1][hp.y-2][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -31)));
  1316. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 1), ho->id);
  1317. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 1), ho->id);
  1318. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 1), ho->id);
  1319. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 1)));
  1320. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 33), ho->id);
  1321. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 33), ho->id);
  1322. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 33), ho->id);
  1323. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 33)));
  1324. 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);
  1325. 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);
  1326. 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);
  1327. 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);
  1328. 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);
  1329. 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);
  1330. }
  1331. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1332. {
  1333. ho->moveDir = 4;
  1334. ho->isStanding = false;
  1335. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, 0), ho->id);
  1336. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, 0), ho->id);
  1337. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, 0), ho->id);
  1338. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 0)));
  1339. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 32), ho->id);
  1340. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 32), ho->id);
  1341. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 32), ho->id);
  1342. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 32)));
  1343. 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);
  1344. 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);
  1345. }
  1346. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1347. {
  1348. ho->moveDir = 5;
  1349. ho->isStanding = false;
  1350. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1, -1), ho->id);
  1351. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31, -1), ho->id);
  1352. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63, -1), ho->id);
  1353. CGI->mh->ttiles[hp.x+1][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, -1)));
  1354. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1, 31), ho->id);
  1355. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31, 31), ho->id);
  1356. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63, 31), ho->id);
  1357. CGI->mh->ttiles[hp.x+1][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 31)));
  1358. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -1, 63)));
  1359. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 31, 63)));
  1360. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 63, 63)));
  1361. CGI->mh->ttiles[hp.x+1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 95, 63)));
  1362. 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);
  1363. 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);
  1364. 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);
  1365. 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);
  1366. 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);
  1367. 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);
  1368. }
  1369. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1370. {
  1371. ho->moveDir = 6;
  1372. ho->isStanding = false;
  1373. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1), ho->id);
  1374. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1), ho->id);
  1375. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1), ho->id);
  1376. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31), ho->id);
  1377. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31), ho->id);
  1378. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31), ho->id);
  1379. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 0, 63)));
  1380. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 32, 63)));
  1381. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 64, 63)));
  1382. 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);
  1383. 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);
  1384. 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);
  1385. }
  1386. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1387. {
  1388. ho->moveDir = 7;
  1389. ho->isStanding = false;
  1390. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, -1)));
  1391. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, -1), ho->id);
  1392. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, -1), ho->id);
  1393. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, -1), ho->id);
  1394. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 31)));
  1395. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 31), ho->id);
  1396. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 31), ho->id);
  1397. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 31), ho->id);
  1398. CGI->mh->ttiles[hp.x-3][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 63)));
  1399. CGI->mh->ttiles[hp.x-2][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 1, 63)));
  1400. CGI->mh->ttiles[hp.x-1][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 33, 63)));
  1401. CGI->mh->ttiles[hp.x][hp.y+1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, 65, 63)));
  1402. 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);
  1403. 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);
  1404. 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);
  1405. 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);
  1406. 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);
  1407. 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);
  1408. }
  1409. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1410. {
  1411. ho->moveDir = 8;
  1412. ho->isStanding = false;
  1413. CGI->mh->ttiles[hp.x-3][hp.y-1][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 0)));
  1414. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1, 0), ho->id);
  1415. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33, 0), ho->id);
  1416. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65, 0), ho->id);
  1417. CGI->mh->ttiles[hp.x-3][hp.y][hp.z].objects.push_back(std::make_pair(ho, genRect(32, 32, -31, 32)));
  1418. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1, 32), ho->id);
  1419. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33, 32), ho->id);
  1420. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65, 32), ho->id);
  1421. 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);
  1422. 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);
  1423. }
  1424. //first initializing done
  1425. SDL_framerateDelay(mainFPSmng); // after first move
  1426. //main moving
  1427. for(int i=1; i<32; i+=2*heroMoveSpeed)
  1428. {
  1429. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1430. {
  1431. //setting advmap shift
  1432. adventureInt->terrain.moveX = i-32;
  1433. adventureInt->terrain.moveY = i-32;
  1434. subRect(hp.x-3, hp.y-2, hp.z, genRect(32, 32, -31+i, -31+i), ho->id);
  1435. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 1+i, -31+i), ho->id);
  1436. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 33+i, -31+i), ho->id);
  1437. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 65+i, -31+i), ho->id);
  1438. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 1+i), ho->id);
  1439. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 1+i), ho->id);
  1440. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 1+i), ho->id);
  1441. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 1+i), ho->id);
  1442. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 33+i), ho->id);
  1443. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 33+i), ho->id);
  1444. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 33+i), ho->id);
  1445. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 33+i), ho->id);
  1446. }
  1447. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1448. {
  1449. //setting advmap shift
  1450. adventureInt->terrain.moveY = i-32;
  1451. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, 0, -31+i), ho->id);
  1452. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 32, -31+i), ho->id);
  1453. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 64, -31+i), ho->id);
  1454. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, 1+i), ho->id);
  1455. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, 1+i), ho->id);
  1456. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, 1+i), ho->id);
  1457. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 33+i), ho->id);
  1458. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 33+i), ho->id);
  1459. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 33+i), ho->id);
  1460. }
  1461. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1462. {
  1463. //setting advmap shift
  1464. adventureInt->terrain.moveX = -i+32;
  1465. adventureInt->terrain.moveY = i-32;
  1466. subRect(hp.x-2, hp.y-2, hp.z, genRect(32, 32, -1-i, -31+i), ho->id);
  1467. subRect(hp.x-1, hp.y-2, hp.z, genRect(32, 32, 31-i, -31+i), ho->id);
  1468. subRect(hp.x, hp.y-2, hp.z, genRect(32, 32, 63-i, -31+i), ho->id);
  1469. subRect(hp.x+1, hp.y-2, hp.z, genRect(32, 32, 95-i, -31+i), ho->id);
  1470. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 1+i), ho->id);
  1471. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 1+i), ho->id);
  1472. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 1+i), ho->id);
  1473. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 1+i), ho->id);
  1474. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 33+i), ho->id);
  1475. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 33+i), ho->id);
  1476. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 33+i), ho->id);
  1477. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 33+i), ho->id);
  1478. }
  1479. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1480. {
  1481. //setting advmap shift
  1482. adventureInt->terrain.moveX = -i+32;
  1483. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, 0), ho->id);
  1484. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, 0), ho->id);
  1485. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, 0), ho->id);
  1486. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, 0), ho->id);
  1487. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 32), ho->id);
  1488. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 32), ho->id);
  1489. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 32), ho->id);
  1490. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 32), ho->id);
  1491. }
  1492. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1493. {
  1494. //setting advmap shift
  1495. adventureInt->terrain.moveX = -i+32;
  1496. adventureInt->terrain.moveY = -i+32;
  1497. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, -1-i, -1-i), ho->id);
  1498. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 31-i, -1-i), ho->id);
  1499. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 63-i, -1-i), ho->id);
  1500. subRect(hp.x+1, hp.y-1, hp.z, genRect(32, 32, 95-i, -1-i), ho->id);
  1501. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, -1-i, 31-i), ho->id);
  1502. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 31-i, 31-i), ho->id);
  1503. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 63-i, 31-i), ho->id);
  1504. subRect(hp.x+1, hp.y, hp.z, genRect(32, 32, 95-i, 31-i), ho->id);
  1505. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, -1-i, 63-i), ho->id);
  1506. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 31-i, 63-i), ho->id);
  1507. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 63-i, 63-i), ho->id);
  1508. subRect(hp.x+1, hp.y+1, hp.z, genRect(32, 32, 95-i, 63-i), ho->id);
  1509. }
  1510. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1511. {
  1512. //setting advmap shift
  1513. adventureInt->terrain.moveY = -i+32;
  1514. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 0, -1-i), ho->id);
  1515. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 32, -1-i), ho->id);
  1516. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 64, -1-i), ho->id);
  1517. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 0, 31-i), ho->id);
  1518. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 32, 31-i), ho->id);
  1519. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 64, 31-i), ho->id);
  1520. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 0, 63-i), ho->id);
  1521. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 32, 63-i), ho->id);
  1522. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 64, 63-i), ho->id);
  1523. }
  1524. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1525. {
  1526. //setting advmap shift
  1527. adventureInt->terrain.moveX = i-32;
  1528. adventureInt->terrain.moveY = -i+32;
  1529. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, -1-i), ho->id);
  1530. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, -1-i), ho->id);
  1531. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, -1-i), ho->id);
  1532. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, -1-i), ho->id);
  1533. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 31-i), ho->id);
  1534. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 31-i), ho->id);
  1535. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 31-i), ho->id);
  1536. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 31-i), ho->id);
  1537. subRect(hp.x-3, hp.y+1, hp.z, genRect(32, 32, -31+i, 63-i), ho->id);
  1538. subRect(hp.x-2, hp.y+1, hp.z, genRect(32, 32, 1+i, 63-i), ho->id);
  1539. subRect(hp.x-1, hp.y+1, hp.z, genRect(32, 32, 33+i, 63-i), ho->id);
  1540. subRect(hp.x, hp.y+1, hp.z, genRect(32, 32, 65+i, 63-i), ho->id);
  1541. }
  1542. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1543. {
  1544. //setting advmap shift
  1545. adventureInt->terrain.moveX = i-32;
  1546. subRect(hp.x-3, hp.y-1, hp.z, genRect(32, 32, -31+i, 0), ho->id);
  1547. subRect(hp.x-2, hp.y-1, hp.z, genRect(32, 32, 1+i, 0), ho->id);
  1548. subRect(hp.x-1, hp.y-1, hp.z, genRect(32, 32, 33+i, 0), ho->id);
  1549. subRect(hp.x, hp.y-1, hp.z, genRect(32, 32, 65+i, 0), ho->id);
  1550. subRect(hp.x-3, hp.y, hp.z, genRect(32, 32, -31+i, 32), ho->id);
  1551. subRect(hp.x-2, hp.y, hp.z, genRect(32, 32, 1+i, 32), ho->id);
  1552. subRect(hp.x-1, hp.y, hp.z, genRect(32, 32, 33+i, 32), ho->id);
  1553. subRect(hp.x, hp.y, hp.z, genRect(32, 32, 65+i, 32), ho->id);
  1554. }
  1555. adventureInt->updateScreen = true;
  1556. LOCPLINT->adventureInt->update(); //updating screen
  1557. CSDL_Ext::update(screen);
  1558. SDL_Delay(5);
  1559. SDL_framerateDelay(mainFPSmng); //for animation purposes
  1560. } //for(int i=1; i<32; i+=4)
  1561. //main moving done
  1562. //finishing move
  1563. //restoring adventureInt->terrain.move*
  1564. adventureInt->terrain.moveX = adventureInt->terrain.moveY = 0;
  1565. if(details.dst.x+1 == details.src.x && details.dst.y+1 == details.src.y) //tl
  1566. {
  1567. delObjRect(hp.x, hp.y-2, hp.z, ho->id);
  1568. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1569. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1570. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1571. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1572. delObjRect(hp.x-3, hp.y, hp.z, ho->id);
  1573. }
  1574. else if(details.dst.x == details.src.x && details.dst.y+1 == details.src.y) //t
  1575. {
  1576. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1577. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1578. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1579. }
  1580. else if(details.dst.x-1 == details.src.x && details.dst.y+1 == details.src.y) //tr
  1581. {
  1582. delObjRect(hp.x-2, hp.y-2, hp.z, ho->id);
  1583. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1584. delObjRect(hp.x+1, hp.y, hp.z, ho->id);
  1585. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1586. delObjRect(hp.x-1, hp.y, hp.z, ho->id);
  1587. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1588. }
  1589. else if(details.dst.x-1 == details.src.x && details.dst.y == details.src.y) //r
  1590. {
  1591. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1592. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1593. }
  1594. else if(details.dst.x-1 == details.src.x && details.dst.y-1 == details.src.y) //br
  1595. {
  1596. delObjRect(hp.x-2, hp.y+1, hp.z, ho->id);
  1597. delObjRect(hp.x-2, hp.y, hp.z, ho->id);
  1598. delObjRect(hp.x+1, hp.y-1, hp.z, ho->id);
  1599. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1600. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1601. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1602. }
  1603. else if(details.dst.x == details.src.x && details.dst.y-1 == details.src.y) //b
  1604. {
  1605. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1606. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1607. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1608. }
  1609. else if(details.dst.x+1 == details.src.x && details.dst.y-1 == details.src.y) //bl
  1610. {
  1611. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1612. delObjRect(hp.x-1, hp.y-1, hp.z, ho->id);
  1613. delObjRect(hp.x-2, hp.y-1, hp.z, ho->id);
  1614. delObjRect(hp.x-3, hp.y-1, hp.z, ho->id);
  1615. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1616. delObjRect(hp.x, hp.y+1, hp.z, ho->id);
  1617. }
  1618. else if(details.dst.x+1 == details.src.x && details.dst.y == details.src.y) //l
  1619. {
  1620. delObjRect(hp.x, hp.y-1, hp.z, ho->id);
  1621. delObjRect(hp.x, hp.y, hp.z, ho->id);
  1622. }
  1623. //restoring good rects
  1624. subRect(details.dst.x-2, details.dst.y-1, details.dst.z, genRect(32, 32, 0, 0), ho->id);
  1625. subRect(details.dst.x-1, details.dst.y-1, details.dst.z, genRect(32, 32, 32, 0), ho->id);
  1626. subRect(details.dst.x, details.dst.y-1, details.dst.z, genRect(32, 32, 64, 0), ho->id);
  1627. subRect(details.dst.x-2, details.dst.y, details.dst.z, genRect(32, 32, 0, 32), ho->id);
  1628. subRect(details.dst.x-1, details.dst.y, details.dst.z, genRect(32, 32, 32, 32), ho->id);
  1629. subRect(details.dst.x, details.dst.y, details.dst.z, genRect(32, 32, 64, 32), ho->id);
  1630. //restoring good order of objects
  1631. std::stable_sort(CGI->mh->ttiles[details.dst.x-2][details.dst.y-1][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-2][details.dst.y-1][details.dst.z].objects.end(), ocmptwo_cgin);
  1632. std::stable_sort(CGI->mh->ttiles[details.dst.x-1][details.dst.y-1][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-1][details.dst.y-1][details.dst.z].objects.end(), ocmptwo_cgin);
  1633. std::stable_sort(CGI->mh->ttiles[details.dst.x][details.dst.y-1][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x][details.dst.y-1][details.dst.z].objects.end(), ocmptwo_cgin);
  1634. std::stable_sort(CGI->mh->ttiles[details.dst.x-2][details.dst.y][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-2][details.dst.y][details.dst.z].objects.end(), ocmptwo_cgin);
  1635. std::stable_sort(CGI->mh->ttiles[details.dst.x-1][details.dst.y][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x-1][details.dst.y][details.dst.z].objects.end(), ocmptwo_cgin);
  1636. std::stable_sort(CGI->mh->ttiles[details.dst.x][details.dst.y][details.dst.z].objects.begin(), CGI->mh->ttiles[details.dst.x][details.dst.y][details.dst.z].objects.end(), ocmptwo_cgin);
  1637. ho->isStanding = true;
  1638. //move finished
  1639. adventureInt->minimap.draw();
  1640. adventureInt->heroList.updateMove(ho);
  1641. //check if user cancelled movement
  1642. {
  1643. boost::unique_lock<boost::mutex> un(eventsM);
  1644. while(events.size())
  1645. {
  1646. SDL_Event *ev = events.front();
  1647. events.pop();
  1648. switch(ev->type)
  1649. {
  1650. case SDL_MOUSEBUTTONDOWN:
  1651. stillMoveHero = false;
  1652. break;
  1653. case SDL_KEYDOWN:
  1654. if(ev->key.keysym.sym < SDLK_F1)
  1655. stillMoveHero = false;
  1656. break;
  1657. }
  1658. delete ev;
  1659. }
  1660. }
  1661. }
  1662. void CPlayerInterface::heroKilled(const CGHeroInstance* hero)
  1663. {
  1664. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1665. graphics->heroWins.erase(hero->ID);
  1666. adventureInt->heroList.updateHList(hero);
  1667. }
  1668. void CPlayerInterface::heroCreated(const CGHeroInstance * hero)
  1669. {
  1670. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1671. if(graphics->heroWins.find(hero->subID)==graphics->heroWins.end())
  1672. graphics->heroWins.insert(std::pair<int,SDL_Surface*>(hero->subID,infoWin(hero)));
  1673. adventureInt->heroList.updateHList();
  1674. }
  1675. void CPlayerInterface::openTownWindow(const CGTownInstance * town)
  1676. {
  1677. adventureInt->deactivate();
  1678. //timeHandler t;
  1679. //t.getDif();
  1680. castleInt = new CCastleInterface(town,true);
  1681. //std::cout << "Loading town screen: " << t.getDif() <<std::endl;
  1682. }
  1683. SDL_Surface * CPlayerInterface::infoWin(const CGObjectInstance * specific) //specific=0 => draws info about selected town/hero
  1684. {
  1685. if (specific)
  1686. {
  1687. switch (specific->ID)
  1688. {
  1689. case HEROI_TYPE:
  1690. return graphics->drawHeroInfoWin(dynamic_cast<const CGHeroInstance*>(specific));
  1691. break;
  1692. case TOWNI_TYPE:
  1693. return graphics->drawTownInfoWin(dynamic_cast<const CGTownInstance*>(specific));
  1694. break;
  1695. default:
  1696. return NULL;
  1697. break;
  1698. }
  1699. }
  1700. else
  1701. {
  1702. switch (adventureInt->selection->ID)
  1703. {
  1704. case HEROI_TYPE:
  1705. {
  1706. const CGHeroInstance * curh = (const CGHeroInstance *)adventureInt->selection;
  1707. return graphics->drawHeroInfoWin(curh);
  1708. }
  1709. case TOWNI_TYPE:
  1710. {
  1711. return graphics->drawTownInfoWin((const CGTownInstance *)adventureInt->selection);
  1712. }
  1713. default:
  1714. tlog1 << "Strange... selection is neither hero nor town\n";
  1715. return NULL;
  1716. }
  1717. }
  1718. }
  1719. void CPlayerInterface::handleMouseMotion(SDL_Event *sEvent)
  1720. {
  1721. //sending active, hovered hoverable objects hover() call
  1722. std::vector<Hoverable*> hlp;
  1723. for(std::list<Hoverable*>::iterator i=hoverable.begin(); i != hoverable.end();i++)
  1724. {
  1725. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1726. {
  1727. if (!(*i)->hovered)
  1728. hlp.push_back((*i));
  1729. }
  1730. else if ((*i)->hovered)
  1731. {
  1732. (*i)->hover(false);
  1733. }
  1734. }
  1735. for(int i=0; i<hlp.size();i++)
  1736. hlp[i]->hover(true);
  1737. //sending active, MotionInterested objects mouseMoved() call
  1738. std::list<MotionInterested*> miCopy = motioninterested;
  1739. for(std::list<MotionInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  1740. {
  1741. if ((*i)->strongInterest || isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1742. {
  1743. (*i)->mouseMoved(sEvent->motion);
  1744. }
  1745. }
  1746. //adventure map scrolling with mouse
  1747. if(!SDL_GetKeyState(NULL)[SDLK_LCTRL])
  1748. {
  1749. if(sEvent->motion.x<15)
  1750. {
  1751. adventureInt->scrollingDir |= CAdvMapInt::LEFT;
  1752. }
  1753. else
  1754. {
  1755. adventureInt->scrollingDir &= ~CAdvMapInt::LEFT;
  1756. }
  1757. if(sEvent->motion.x>screen->w-15)
  1758. {
  1759. adventureInt->scrollingDir |= CAdvMapInt::RIGHT;
  1760. }
  1761. else
  1762. {
  1763. adventureInt->scrollingDir &= ~CAdvMapInt::RIGHT;
  1764. }
  1765. if(sEvent->motion.y<15)
  1766. {
  1767. adventureInt->scrollingDir |= CAdvMapInt::UP;
  1768. }
  1769. else
  1770. {
  1771. adventureInt->scrollingDir &= ~CAdvMapInt::UP;
  1772. }
  1773. if(sEvent->motion.y>screen->h-15)
  1774. {
  1775. adventureInt->scrollingDir |= CAdvMapInt::DOWN;
  1776. }
  1777. else
  1778. {
  1779. adventureInt->scrollingDir &= ~CAdvMapInt::DOWN;
  1780. }
  1781. }
  1782. }
  1783. void CPlayerInterface::handleEvent(SDL_Event *sEvent)
  1784. {
  1785. current = sEvent;
  1786. if (sEvent->type==SDL_KEYDOWN || sEvent->type==SDL_KEYUP)
  1787. {
  1788. SDL_KeyboardEvent key = sEvent->key;
  1789. //translate numpad keys
  1790. if (key.keysym.sym >= SDLK_KP0 && key.keysym.sym <= SDLK_KP9)
  1791. {
  1792. key.keysym.sym = (SDLKey) (key.keysym.sym - SDLK_KP0 + SDLK_0);
  1793. }
  1794. else if(key.keysym.sym == SDLK_KP_ENTER)
  1795. {
  1796. key.keysym.sym = (SDLKey)SDLK_RETURN;
  1797. }
  1798. bool keysCaptured = false;
  1799. for(std::list<KeyInterested*>::iterator i=keyinterested.begin(); i != keyinterested.end();i++)
  1800. {
  1801. if((*i)->captureAllKeys)
  1802. {
  1803. keysCaptured = true;
  1804. break;
  1805. }
  1806. }
  1807. std::list<KeyInterested*> miCopy = keyinterested;
  1808. for(std::list<KeyInterested*>::iterator i=miCopy.begin(); i != miCopy.end();i++)
  1809. if(vstd::contains(keyinterested,*i) && (!keysCaptured || (*i)->captureAllKeys))
  1810. (**i).keyPressed(key);
  1811. }
  1812. else if(sEvent->type==SDL_MOUSEMOTION)
  1813. {
  1814. CGI->curh->cursorMove(sEvent->motion.x, sEvent->motion.y);
  1815. handleMouseMotion(sEvent);
  1816. }
  1817. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1818. {
  1819. std::list<ClickableL*> hlp = lclickable;
  1820. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1821. {
  1822. if(!vstd::contains(lclickable,*i)) continue;
  1823. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1824. {
  1825. (*i)->clickLeft(true);
  1826. }
  1827. }
  1828. }
  1829. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_LEFT))
  1830. {
  1831. std::list<ClickableL*> hlp = lclickable;
  1832. for(std::list<ClickableL*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1833. {
  1834. if(!vstd::contains(lclickable,*i)) continue;
  1835. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1836. {
  1837. (*i)->clickLeft(false);
  1838. }
  1839. else
  1840. (*i)->clickLeft(boost::logic::indeterminate);
  1841. }
  1842. }
  1843. else if ((sEvent->type==SDL_MOUSEBUTTONDOWN) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1844. {
  1845. std::list<ClickableR*> hlp = rclickable;
  1846. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1847. {
  1848. if(!vstd::contains(rclickable,*i)) continue;
  1849. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1850. {
  1851. (*i)->clickRight(true);
  1852. }
  1853. }
  1854. }
  1855. else if ((sEvent->type==SDL_MOUSEBUTTONUP) && (sEvent->button.button == SDL_BUTTON_RIGHT))
  1856. {
  1857. std::list<ClickableR*> hlp = rclickable;
  1858. for(std::list<ClickableR*>::iterator i=hlp.begin(); i != hlp.end();i++)
  1859. {
  1860. if(!vstd::contains(rclickable,*i)) continue;
  1861. if (isItIn(&(*i)->pos,sEvent->motion.x,sEvent->motion.y))
  1862. {
  1863. (*i)->clickRight(false);
  1864. }
  1865. else
  1866. (*i)->clickRight(boost::logic::indeterminate);
  1867. }
  1868. }
  1869. current = NULL;
  1870. } //event end
  1871. int3 CPlayerInterface::repairScreenPos(int3 pos)
  1872. {
  1873. if(pos.x<=-Woff)
  1874. pos.x = -Woff+1;
  1875. if(pos.y<=-Hoff)
  1876. pos.y = -Hoff+1;
  1877. if(pos.x>CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff)
  1878. pos.x = CGI->mh->map->width - this->adventureInt->terrain.tilesw + Woff;
  1879. if(pos.y>CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff)
  1880. pos.y = CGI->mh->map->height - this->adventureInt->terrain.tilesh + Hoff;
  1881. return pos;
  1882. }
  1883. void CPlayerInterface::heroPrimarySkillChanged(const CGHeroInstance * hero, int which, int val)
  1884. {
  1885. if(which >= PRIMARY_SKILLS) //no need to redraw infowin if this is experience (exp is treated as prim skill with id==4)
  1886. return;
  1887. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1888. redrawHeroWin(hero);
  1889. }
  1890. void CPlayerInterface::heroManaPointsChanged(const CGHeroInstance * hero)
  1891. {
  1892. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1893. redrawHeroWin(hero);
  1894. }
  1895. void CPlayerInterface::heroMovePointsChanged(const CGHeroInstance * hero)
  1896. {
  1897. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1898. if(adventureInt == curint)
  1899. adventureInt->heroList.draw();
  1900. }
  1901. void CPlayerInterface::receivedResource(int type, int val)
  1902. {
  1903. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1904. if(!curint)
  1905. return;
  1906. if(curint==adventureInt || screen->h > 600 || screen->w > 800)
  1907. adventureInt->resdatabar.draw();
  1908. if(curint==castleInt && !curint->subInt)
  1909. castleInt->resdatabar->draw();
  1910. }
  1911. void CPlayerInterface::heroGotLevel(const CGHeroInstance *hero, int pskill, std::vector<ui16>& skills, boost::function<void(ui32)> &callback)
  1912. {
  1913. {
  1914. boost::unique_lock<boost::mutex> un(showingDialog->mx);
  1915. while(showingDialog->data)
  1916. showingDialog->cond.wait(un);
  1917. }
  1918. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1919. CLevelWindow *lw = new CLevelWindow(hero,pskill,skills,callback);
  1920. curint->deactivate();
  1921. lw->activate();
  1922. }
  1923. void CPlayerInterface::heroInGarrisonChange(const CGTownInstance *town)
  1924. {
  1925. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1926. //redraw infowindow
  1927. SDL_FreeSurface(graphics->townWins[town->id]);
  1928. graphics->townWins[town->id] = infoWin(town);
  1929. if(town->garrisonHero)
  1930. {
  1931. CGI->mh->hideObject(town->garrisonHero);
  1932. }
  1933. if(town->visitingHero)
  1934. {
  1935. CGI->mh->printObject(town->visitingHero);
  1936. }
  1937. adventureInt->heroList.updateHList();
  1938. CCastleInterface *c = dynamic_cast<CCastleInterface*>(curint);
  1939. if(c)
  1940. {
  1941. c->garr->highlighted = NULL;
  1942. c->hslotup.hero = town->garrisonHero;
  1943. c->garr->odown = c->hslotdown.hero = town->visitingHero;
  1944. c->garr->set2 = town->visitingHero ? &town->visitingHero->army : NULL;
  1945. c->garr->recreateSlots();
  1946. c->showAll();
  1947. }
  1948. }
  1949. void CPlayerInterface::heroVisitsTown(const CGHeroInstance* hero, const CGTownInstance * town)
  1950. {
  1951. if(hero->tempOwner != town->tempOwner)
  1952. return;
  1953. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1954. openTownWindow(town);
  1955. }
  1956. void CPlayerInterface::garrisonChanged(const CGObjectInstance * obj)
  1957. {
  1958. boost::unique_lock<boost::recursive_mutex> un(*pim);
  1959. if(obj->ID == HEROI_TYPE) //hero
  1960. {
  1961. const CGHeroInstance * hh;
  1962. if(hh = dynamic_cast<const CGHeroInstance*>(obj))
  1963. {
  1964. SDL_FreeSurface(graphics->heroWins[hh->subID]);
  1965. graphics->heroWins[hh->subID] = infoWin(hh);
  1966. }
  1967. CHeroWindow * hw = adventureInt->heroWindow;
  1968. if(hw == curint->subInt)
  1969. {
  1970. hw->garInt->recreateSlots();
  1971. hw->garInt->show();
  1972. }
  1973. if(castleInt) //opened town window - redraw town garrison slots (change is within hero garr)
  1974. {
  1975. castleInt->garr->highlighted = NULL;
  1976. castleInt->garr->recreateSlots();
  1977. }
  1978. }
  1979. else if (obj->ID == TOWNI_TYPE) //town
  1980. {
  1981. const CGTownInstance * tt;
  1982. if(tt = static_cast<const CGTownInstance*>(obj))
  1983. {
  1984. SDL_FreeSurface(graphics->townWins[tt->id]);
  1985. graphics->townWins[tt->id] = infoWin(tt);
  1986. }
  1987. if(tt->visitingHero)
  1988. {
  1989. SDL_FreeSurface(graphics->heroWins[tt->visitingHero->subID]);
  1990. graphics->heroWins[tt->visitingHero->subID] = infoWin(tt->visitingHero);
  1991. }
  1992. if(LOCPLINT->castleInt)
  1993. {
  1994. LOCPLINT->castleInt->garr->highlighted = NULL;
  1995. LOCPLINT->castleInt->garr->recreateSlots();
  1996. }
  1997. }
  1998. if(curint == adventureInt)
  1999. adventureInt->infoBar.draw();
  2000. }
  2001. void CPlayerInterface::buildChanged(const CGTownInstance *town, int buildingID, int what) //what: 1 - built, 2 - demolished
  2002. {
  2003. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2004. switch (buildingID)
  2005. {
  2006. case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 15:
  2007. {
  2008. SDL_FreeSurface(graphics->townWins[town->id]);
  2009. graphics->townWins[town->id] = infoWin(town);
  2010. break;
  2011. }
  2012. }
  2013. if(curint!=castleInt)
  2014. return;
  2015. if(castleInt->town!=town)
  2016. return;
  2017. switch(what)
  2018. {
  2019. case 1:
  2020. castleInt->addBuilding(buildingID);
  2021. break;
  2022. case 2:
  2023. castleInt->removeBuilding(buildingID);
  2024. break;
  2025. }
  2026. }
  2027. 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
  2028. {
  2029. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2030. curint->deactivate();
  2031. curint = battleInt = new CBattleInterface(army1, army2, hero1, hero2, genRect(600, 800, (conf.cc.resx - 800)/2, (conf.cc.resy - 600)/2));
  2032. curint->activate();
  2033. LOCPLINT->objsToBlit.push_back(dynamic_cast<IShowable*>(curint));
  2034. }
  2035. void CPlayerInterface::battlefieldPrepared(int battlefieldType, std::vector<CObstacle*> obstacles) //called when battlefield is prepared, prior the battle beginning
  2036. {
  2037. }
  2038. 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
  2039. {
  2040. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2041. battleInt->newRound(round);
  2042. }
  2043. void CPlayerInterface::actionStarted(const BattleAction* action)
  2044. {
  2045. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2046. curAction = new BattleAction(*action);
  2047. if( (action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber))) )
  2048. {
  2049. battleInt->moveStarted = true;
  2050. if(battleInt->creAnims[action->stackNumber]->framesInGroup(20))
  2051. {
  2052. battleInt->creAnims[action->stackNumber]->setType(20);
  2053. }
  2054. }
  2055. battleInt->deactivate();
  2056. CStack *stack = cb->battleGetStackByID(action->stackNumber);
  2057. char txt[400];
  2058. if(action->actionType == 1)
  2059. {
  2060. if(action->side)
  2061. battleInt->defendingHero->setPhase(4);
  2062. else
  2063. battleInt->attackingHero->setPhase(4);
  2064. return;
  2065. }
  2066. if(!stack)
  2067. {
  2068. tlog1<<"Something wrong with stackNumber in actionStarted. Stack number: "<<action->stackNumber<<std::endl;
  2069. return;
  2070. }
  2071. int txtid = 0;
  2072. switch(action->actionType)
  2073. {
  2074. case 3: //defend
  2075. txtid = 120;
  2076. break;
  2077. case 8: //wait
  2078. txtid = 136;
  2079. break;
  2080. case 11: //bad morale
  2081. txtid = -34; //negative -> no separate singular/plural form
  2082. battleInt->displayEffect(30,stack->position);
  2083. break;
  2084. }
  2085. if(txtid > 0 && stack->amount != 1)
  2086. txtid++; //move to plural text
  2087. else if(txtid < 0)
  2088. txtid = -txtid;
  2089. if(txtid)
  2090. {
  2091. sprintf(txt, CGI->generaltexth->allTexts[txtid].c_str(), (stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str(), 0);
  2092. LOCPLINT->battleInt->console->addText(txt);
  2093. }
  2094. }
  2095. void CPlayerInterface::actionFinished(const BattleAction* action)
  2096. {
  2097. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2098. delete curAction;
  2099. curAction = NULL;
  2100. //if((action->actionType==2 || (action->actionType==6 && action->destinationTile!=cb->battleGetPos(action->stackNumber)))) //activating interface when move is finished
  2101. {
  2102. battleInt->activate();
  2103. }
  2104. if(action->actionType == 1)
  2105. {
  2106. if(action->side)
  2107. battleInt->defendingHero->setPhase(0);
  2108. else
  2109. battleInt->attackingHero->setPhase(0);
  2110. }
  2111. }
  2112. BattleAction CPlayerInterface::activeStack(int stackID) //called when it's turn of that stack
  2113. {
  2114. CBattleInterface *b = battleInt;
  2115. {
  2116. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2117. CStack *stack = cb->battleGetStackByID(stackID);
  2118. if(vstd::contains(stack->state,MOVED)) //this stack has moved and makes second action -> high morale
  2119. {
  2120. std::string hlp = CGI->generaltexth->allTexts[33];
  2121. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl : stack->creature->nameSing);
  2122. battleInt->displayEffect(20,stack->position);
  2123. battleInt->console->addText(hlp);
  2124. }
  2125. b->stackActivated(stackID);
  2126. }
  2127. //wait till BattleInterface sets its command
  2128. boost::unique_lock<boost::mutex> lock(b->givenCommand->mx);
  2129. while(!b->givenCommand->data)
  2130. b->givenCommand->cond.wait(lock);
  2131. //tidy up
  2132. BattleAction ret = *(b->givenCommand->data);
  2133. delete b->givenCommand->data;
  2134. b->givenCommand->data = NULL;
  2135. //return command
  2136. return ret;
  2137. }
  2138. void CPlayerInterface::battleEnd(BattleResult *br)
  2139. {
  2140. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2141. ((CBattleInterface*)curint)->battleFinished(*br);
  2142. }
  2143. void CPlayerInterface::battleResultQuited()
  2144. {
  2145. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2146. ((CBattleInterface*)curint)->resWindow->deactivate();
  2147. objsToBlit -= curint;
  2148. delete battleInt;
  2149. battleInt = 0;
  2150. curint = adventureInt;
  2151. adventureInt->activate();
  2152. LOCPLINT->showingDialog->setn(false);
  2153. }
  2154. void CPlayerInterface::battleStackMoved(int ID, int dest, int distance, bool end)
  2155. {
  2156. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2157. battleInt->stackMoved(ID, dest, end, distance);
  2158. }
  2159. void CPlayerInterface::battleSpellCasted(SpellCasted *sc)
  2160. {
  2161. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2162. battleInt->spellCasted(sc);
  2163. }
  2164. void CPlayerInterface::battleStacksEffectsSet(SetStackEffect & sse)
  2165. {
  2166. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2167. battleInt->battleStacksEffectsSet(sse);
  2168. }
  2169. void CPlayerInterface::battleStacksAttacked(std::set<BattleStackAttacked> & bsa)
  2170. {
  2171. tlog5 << "CPlayerInterface::battleStackAttacked - locking...";
  2172. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2173. tlog5 << "done!\n";
  2174. std::vector<CBattleInterface::SStackAttackedInfo> arg;
  2175. for(std::set<BattleStackAttacked>::iterator i = bsa.begin(); i != bsa.end(); i++)
  2176. {
  2177. if(i->isEffect())
  2178. {
  2179. battleInt->displayEffect(i->effect, cb->battleGetStackByID(i->stackAttacked)->position);
  2180. }
  2181. CBattleInterface::SStackAttackedInfo to_put = {i->stackAttacked, i->damageAmount, i->killedAmount, LOCPLINT->curAction->stackNumber, LOCPLINT->curAction->actionType==7, i->killed()};
  2182. arg.push_back(to_put);
  2183. }
  2184. battleInt->stacksAreAttacked(arg);
  2185. }
  2186. void CPlayerInterface::battleAttack(BattleAttack *ba)
  2187. {
  2188. tlog5 << "CPlayerInterface::battleAttack - locking...";
  2189. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2190. tlog5 << "done!\n";
  2191. if(ba->lucky()) //lucky hit
  2192. {
  2193. CStack *stack = cb->battleGetStackByID(ba->stackAttacking);
  2194. std::string hlp = CGI->generaltexth->allTexts[45];
  2195. boost::algorithm::replace_first(hlp,"%s",(stack->amount != 1) ? stack->creature->namePl.c_str() : stack->creature->nameSing.c_str());
  2196. battleInt->console->addText(hlp);
  2197. battleInt->displayEffect(18,stack->position);
  2198. }
  2199. //TODO: bad luck?
  2200. if(ba->shot())
  2201. {
  2202. for(std::set<BattleStackAttacked>::iterator i = ba->bsa.begin(); i != ba->bsa.end(); i++)
  2203. battleInt->stackIsShooting(ba->stackAttacking,cb->battleGetPos(i->stackAttacked));
  2204. }
  2205. else
  2206. {
  2207. CStack * attacker = cb->battleGetStackByID(ba->stackAttacking);
  2208. int shift = 0;
  2209. if(ba->counter() && BattleInfo::mutualPosition(curAction->destinationTile, attacker->position) < 0)
  2210. {
  2211. if(attacker->attackerOwned)
  2212. shift = 1;
  2213. else
  2214. shift = -1;
  2215. }
  2216. battleInt->stackAttacking( ba->stackAttacking, ba->counter() ? curAction->destinationTile + shift : curAction->additionalInfo );
  2217. }
  2218. }
  2219. void CPlayerInterface::showComp(SComponent comp)
  2220. {
  2221. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2222. adventureInt->infoBar.showComp(&comp,4000);
  2223. }
  2224. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<Component*> &components)
  2225. {
  2226. std::vector<SComponent*> intComps;
  2227. for(int i=0;i<components.size();i++)
  2228. intComps.push_back(new SComponent(*components[i]));
  2229. showInfoDialog(text,intComps);
  2230. }
  2231. void CPlayerInterface::showInfoDialog(const std::string &text, const std::vector<SComponent*> & components, bool deactivateCur)
  2232. {
  2233. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2234. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2235. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  2236. CInfoWindow * temp = new CInfoWindow(text,playerID,36,components,pom);
  2237. if(makingTurn && curint)
  2238. {
  2239. showingDialog->set(true);
  2240. if(deactivateCur)
  2241. {
  2242. temp->buttons[0]->callback += boost::bind(&IActivable::activate,LOCPLINT->curint);
  2243. curint->deactivate(); //dezaktywacja starego interfejsu
  2244. }
  2245. temp->activate();
  2246. LOCPLINT->objsToBlit.push_back(temp);
  2247. }
  2248. else
  2249. {
  2250. dialogs.push_back(temp);
  2251. }
  2252. }
  2253. void CPlayerInterface::showYesNoDialog(const std::string &text, const std::vector<SComponent*> & components, CFunctionList<void()> onYes, CFunctionList<void()> onNo, bool deactivateCur, bool DelComps)
  2254. {
  2255. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2256. LOCPLINT->showingDialog->setn(true);
  2257. if(deactivateCur)
  2258. curint->deactivate(); //dezaktywacja starego interfejsu
  2259. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2260. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",boost::bind(&IActivable::activate,curint)));
  2261. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",boost::bind(&IActivable::activate,curint)));
  2262. CInfoWindow * temp = new CInfoWindow(text,playerID,36,components,pom);
  2263. temp->delComps = DelComps;
  2264. for(int i=0;i<onYes.funcs.size();i++)
  2265. temp->buttons[0]->callback += onYes.funcs[i];
  2266. for(int i=0;i<onNo.funcs.size();i++)
  2267. temp->buttons[1]->callback += onNo.funcs[i];
  2268. temp->activate();
  2269. LOCPLINT->objsToBlit.push_back(temp);
  2270. }
  2271. void CPlayerInterface::showBlockingDialog( const std::string &text, const std::vector<Component> &components, ui32 askID, bool selection, bool cancel )
  2272. {
  2273. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2274. if(!selection && cancel) //simple yes/no dialog
  2275. {
  2276. std::vector<SComponent*> intComps;
  2277. for(int i=0;i<components.size();i++)
  2278. intComps.push_back(new SComponent(components[i])); //will be deleted by close in window
  2279. showYesNoDialog(text,intComps,boost::bind(&CCallback::selectionMade,cb,1,askID),boost::bind(&CCallback::selectionMade,cb,0,askID),true,true);
  2280. }
  2281. else if(selection)
  2282. {
  2283. std::vector<CSelectableComponent*> intComps;
  2284. for(int i=0;i<components.size();i++)
  2285. intComps.push_back(new CSelectableComponent(components[i])); //will be deleted by CSelWindow::close
  2286. adventureInt->deactivate(); //dezaktywacja starego interfejsu
  2287. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  2288. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",boost::bind(&IActivable::activate,curint)));
  2289. if(cancel)
  2290. {
  2291. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",boost::bind(&IActivable::activate,curint)));
  2292. }
  2293. CSelWindow * temp = new CSelWindow(text,playerID,35,intComps,pom,askID);
  2294. LOCPLINT->objsToBlit.push_back(temp);
  2295. temp->activate();
  2296. intComps[0]->clickLeft(true);
  2297. }
  2298. }
  2299. void CPlayerInterface::removeObjToBlit(IShowable* obj)
  2300. {
  2301. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2302. objsToBlit.erase
  2303. (std::find(objsToBlit.begin(),objsToBlit.end(),obj));
  2304. //delete obj;
  2305. }
  2306. void CPlayerInterface::tileRevealed(const std::set<int3> &pos)
  2307. {
  2308. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2309. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  2310. adventureInt->minimap.showTile(*i);
  2311. if(curint == adventureInt)
  2312. adventureInt->minimap.draw();
  2313. }
  2314. void CPlayerInterface::tileHidden(const std::set<int3> &pos)
  2315. {
  2316. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2317. for(std::set<int3>::const_iterator i=pos.begin(); i!=pos.end();i++)
  2318. adventureInt->minimap.hideTile(*i);
  2319. if(curint == adventureInt)
  2320. adventureInt->minimap.draw();
  2321. }
  2322. void CPlayerInterface::openHeroWindow(const CGHeroInstance *hero)
  2323. {
  2324. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2325. adventureInt->heroWindow->setHero(hero);
  2326. this->objsToBlit.push_back(adventureInt->heroWindow);
  2327. curint->deactivate();
  2328. if(curint == castleInt)
  2329. castleInt->subInt = adventureInt->heroWindow;
  2330. adventureInt->heroWindow->quitButton->callback.funcs.clear();
  2331. adventureInt->heroWindow->quitButton->callback += boost::bind(&CHeroWindow::deactivate,adventureInt->heroWindow);
  2332. adventureInt->heroWindow->quitButton->callback += boost::bind(&CHeroWindow::quit,adventureInt->heroWindow);
  2333. adventureInt->heroWindow->activate();
  2334. }
  2335. void CPlayerInterface::heroArtifactSetChanged(const CGHeroInstance*hero)
  2336. {
  2337. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2338. if(curint->subInt == adventureInt->heroWindow)
  2339. {
  2340. adventureInt->heroWindow->deactivate();
  2341. adventureInt->heroWindow->setHero(adventureInt->heroWindow->curHero);
  2342. adventureInt->heroWindow->activate();
  2343. }
  2344. }
  2345. void CPlayerInterface::updateWater()
  2346. {
  2347. }
  2348. void CPlayerInterface::availableCreaturesChanged( const CGTownInstance *town )
  2349. {
  2350. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2351. if(curint == castleInt)
  2352. {
  2353. CFortScreen *fs = dynamic_cast<CFortScreen*>(curint->subInt);
  2354. if(fs)
  2355. fs->draw(castleInt,false);
  2356. }
  2357. }
  2358. void CPlayerInterface::heroBonusChanged( const CGHeroInstance *hero, const HeroBonus &bonus, bool gain )
  2359. {
  2360. if(bonus.type == HeroBonus::NONE) return;
  2361. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2362. redrawHeroWin(hero);
  2363. }
  2364. template <typename Handler> void CPlayerInterface::serializeTempl( Handler &h, const int version )
  2365. {
  2366. h & playerID & serialID;
  2367. h & heroMoveSpeed & mapScrollingSpeed;
  2368. h & CBattleInterface::settings;
  2369. }
  2370. void CPlayerInterface::serialize( COSer<CSaveFile> &h, const int version )
  2371. {
  2372. serializeTempl(h,version);
  2373. }
  2374. void CPlayerInterface::serialize( CISer<CLoadFile> &h, const int version )
  2375. {
  2376. serializeTempl(h,version);
  2377. }
  2378. void CPlayerInterface::redrawHeroWin(const CGHeroInstance * hero)
  2379. {
  2380. if(!vstd::contains(graphics->heroWins,hero->subID))
  2381. {
  2382. tlog1 << "Cannot redraw infowindow for hero with subID=" << hero->subID << " - not present in our map\n";
  2383. return;
  2384. }
  2385. SDL_FreeSurface(graphics->heroWins[hero->subID]);
  2386. graphics->heroWins[hero->subID] = infoWin(hero);
  2387. if (adventureInt->selection == hero)
  2388. adventureInt->infoBar.draw();
  2389. }
  2390. bool CPlayerInterface::moveHero( const CGHeroInstance *h, CPath * path )
  2391. {
  2392. if (!h || !path)
  2393. return false; //can't find hero
  2394. bool result = false;
  2395. path->convert(0);
  2396. stillMoveHero = true;
  2397. for(int i=path->nodes.size()-1; i>0; i--)
  2398. {
  2399. {
  2400. boost::unique_lock<boost::recursive_mutex> un(*pim);
  2401. if(!stillMoveHero)
  2402. return result;
  2403. }
  2404. int3 endpos(path->nodes[i-1].coord.x, path->nodes[i-1].coord.y, h->pos.z);
  2405. result = cb->moveHero(h,endpos);
  2406. }
  2407. stillMoveHero = false;
  2408. return result;
  2409. }
  2410. bool CPlayerInterface::shiftPressed() const
  2411. {
  2412. return SDL_GetKeyState(NULL)[SDLK_LSHIFT] || SDL_GetKeyState(NULL)[SDLK_RSHIFT];
  2413. }
  2414. CStatusBar::CStatusBar(int x, int y, std::string name, int maxw)
  2415. {
  2416. bg=BitmapHandler::loadBitmap(name);
  2417. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  2418. pos.x=x;
  2419. pos.y=y;
  2420. if(maxw >= 0)
  2421. pos.w = std::min(bg->w,maxw);
  2422. else
  2423. pos.w=bg->w;
  2424. pos.h=bg->h;
  2425. middlex=(pos.w/2)+x;
  2426. middley=(bg->h/2)+y;
  2427. }
  2428. CStatusBar::~CStatusBar()
  2429. {
  2430. SDL_FreeSurface(bg);
  2431. }
  2432. void CStatusBar::clear()
  2433. {
  2434. if(LOCPLINT->cingconsole->enteredText == "") //for appropriate support for in-game console
  2435. {
  2436. current="";
  2437. show();
  2438. }
  2439. }
  2440. void CStatusBar::print(const std::string & text)
  2441. {
  2442. if(LOCPLINT->cingconsole->enteredText == "" || text == LOCPLINT->cingconsole->enteredText) //for appropriate support for in-game console
  2443. {
  2444. current=text;
  2445. show();
  2446. }
  2447. }
  2448. void CStatusBar::show()
  2449. {
  2450. SDL_Rect pom = genRect(pos.h,pos.w,pos.x,pos.y);
  2451. SDL_BlitSurface(bg,&genRect(pos.h,pos.w,0,0),screen,&pom);
  2452. printAtMiddle(current,middlex,middley,GEOR13,zwykly);
  2453. }
  2454. std::string CStatusBar::getCurrent()
  2455. {
  2456. return current;
  2457. }
  2458. void CList::activate()
  2459. {
  2460. ClickableL::activate();
  2461. ClickableR::activate();
  2462. Hoverable::activate();
  2463. KeyInterested::activate();
  2464. MotionInterested::activate();
  2465. };
  2466. void CList::deactivate()
  2467. {
  2468. ClickableL::deactivate();
  2469. ClickableR::deactivate();
  2470. Hoverable::deactivate();
  2471. KeyInterested::deactivate();
  2472. MotionInterested::deactivate();
  2473. };
  2474. void CList::clickLeft(tribool down)
  2475. {
  2476. };
  2477. CList::CList(int Size)
  2478. :SIZE(Size)
  2479. {
  2480. }
  2481. CHeroList::CHeroList(int Size)
  2482. :CList(Size)
  2483. {
  2484. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  2485. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  2486. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  2487. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  2488. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  2489. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  2490. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  2491. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  2492. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  2493. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  2494. //32px per hero
  2495. posmobx = pos.x+1;
  2496. posmoby = pos.y+arrup->height+1;
  2497. posporx = pos.x+mobile->width+2;
  2498. pospory = pos.y+arrup->height;
  2499. posmanx = pos.x+1+50+mobile->width;
  2500. posmany = pos.y+arrup->height+1;
  2501. from = 0;
  2502. pressed = indeterminate;
  2503. }
  2504. void CHeroList::init()
  2505. {
  2506. int w = pos.w+1, h = pos.h+4;
  2507. bg = CSDL_Ext::newSurface(w,h,screen);
  2508. SDL_BlitSurface(LOCPLINT->adventureInt->bg,&genRect(w,h,pos.x,pos.y),bg,&genRect(w,h,0,0));
  2509. }
  2510. void CHeroList::genList()
  2511. {
  2512. int howMany = LOCPLINT->cb->howManyHeroes();
  2513. for (int i=0;i<howMany;i++)
  2514. {
  2515. const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  2516. if(!h->inTownGarrison)
  2517. items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  2518. }
  2519. }
  2520. void CHeroList::select(int which)
  2521. {
  2522. if (which<0)
  2523. {
  2524. selected = which;
  2525. LOCPLINT->adventureInt->selection = NULL;
  2526. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  2527. draw();
  2528. LOCPLINT->adventureInt->infoBar.draw(NULL);
  2529. }
  2530. if (which>=items.size())
  2531. return;
  2532. selected = which;
  2533. LOCPLINT->adventureInt->centerOn(items[which].first->pos);
  2534. LOCPLINT->adventureInt->selection = items[which].first;
  2535. //recalculationg path in case of something has changed on map
  2536. if(items[which].second)
  2537. {
  2538. CPath * newPath = LOCPLINT->cb->getPath(items[which].second->startPos(), items[which].second->endPos(), items[which].first);
  2539. delete items[which].second;
  2540. LOCPLINT->adventureInt->terrain.currentPath = items[which].second = newPath;
  2541. }
  2542. else
  2543. {
  2544. LOCPLINT->adventureInt->terrain.currentPath = NULL;
  2545. }
  2546. //recalculated and assigned
  2547. draw();
  2548. LOCPLINT->adventureInt->townList.draw();
  2549. LOCPLINT->adventureInt->infoBar.draw(NULL);
  2550. LOCPLINT->cb->setSelection(items[which].first);
  2551. }
  2552. void CHeroList::clickLeft(tribool down)
  2553. {
  2554. if (down)
  2555. {
  2556. /***************************ARROWS*****************************************/
  2557. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2558. {
  2559. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2560. pressed = true;
  2561. return;
  2562. }
  2563. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2564. {
  2565. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2566. pressed = false;
  2567. return;
  2568. }
  2569. /***************************HEROES*****************************************/
  2570. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2571. hx-=pos.x;
  2572. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2573. int ny = hy/32;
  2574. if (ny>=SIZE || ny<0)
  2575. return;
  2576. if ( (ny+from)==selected && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2577. LOCPLINT->openHeroWindow(items[selected].first);//print hero screen
  2578. select(ny+from);
  2579. }
  2580. else
  2581. {
  2582. if (indeterminate(pressed))
  2583. return;
  2584. if (pressed) //up
  2585. {
  2586. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2587. pressed = indeterminate;
  2588. if (!down)
  2589. {
  2590. from--;
  2591. if (from<0)
  2592. from=0;
  2593. draw();
  2594. }
  2595. }
  2596. else if (!pressed) //down
  2597. {
  2598. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2599. pressed = indeterminate;
  2600. if (!down)
  2601. {
  2602. from++;
  2603. //if (from<items.size()-5)
  2604. // from=items.size()-5;
  2605. draw();
  2606. }
  2607. }
  2608. else
  2609. throw 0;
  2610. }
  2611. }
  2612. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  2613. {
  2614. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2615. {
  2616. if (from>0)
  2617. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  2618. else
  2619. LOCPLINT->adventureInt->statusbar.clear();
  2620. return;
  2621. }
  2622. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2623. {
  2624. if ((items.size()-from) > SIZE)
  2625. LOCPLINT->adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  2626. else
  2627. LOCPLINT->adventureInt->statusbar.clear();
  2628. return;
  2629. }
  2630. //if not buttons then heroes
  2631. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2632. hx-=pos.x;
  2633. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2634. int ny = hy/32;
  2635. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2636. {
  2637. LOCPLINT->adventureInt->statusbar.clear();
  2638. return;
  2639. }
  2640. std::vector<std::string> temp;
  2641. temp.push_back(items[from+ny].first->name);
  2642. temp.push_back(items[from+ny].first->type->heroClass->name);
  2643. LOCPLINT->adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  2644. //select(ny+from);
  2645. }
  2646. void CHeroList::clickRight(tribool down)
  2647. {
  2648. if (down)
  2649. {
  2650. /***************************ARROWS*****************************************/
  2651. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2652. {
  2653. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  2654. }
  2655. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2656. {
  2657. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  2658. }
  2659. //if not buttons then heroes
  2660. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2661. hx-=pos.x;
  2662. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2663. int ny = hy/32;
  2664. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2665. {
  2666. return;
  2667. }
  2668. //show popup
  2669. CInfoPopup * ip = new CInfoPopup(graphics->heroWins[items[from+ny].first->subID],
  2670. LOCPLINT->current->motion.x-graphics->heroWins[items[from+ny].first->subID]->w,
  2671. LOCPLINT->current->motion.y-graphics->heroWins[items[from+ny].first->subID]->h,false
  2672. );
  2673. ip->activate();
  2674. }
  2675. else
  2676. {
  2677. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down,this);
  2678. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down,this);
  2679. }
  2680. }
  2681. void CHeroList::hover (bool on)
  2682. {
  2683. }
  2684. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  2685. {
  2686. }
  2687. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  2688. {
  2689. if(toRemove) //remove specific hero
  2690. {
  2691. for (std::vector<std::pair<const CGHeroInstance*, CPath *> >::iterator i=items.begin(); i != items.end(); i++)
  2692. {
  2693. if(i->first == toRemove)
  2694. {
  2695. delete i->second;
  2696. items.erase(i);
  2697. break;
  2698. }
  2699. }
  2700. }
  2701. else
  2702. {
  2703. items.clear();
  2704. genList();
  2705. }
  2706. if(selected>=items.size())
  2707. select(items.size()-1);
  2708. if(items.size()==0)
  2709. LOCPLINT->adventureInt->townList.select(0);
  2710. else
  2711. select(selected);
  2712. }
  2713. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  2714. {
  2715. int ser = -1;
  2716. for(int i=0; i<items.size() && ser<0; i++)
  2717. if(items[i].first->subID == which->subID)
  2718. ser = i;
  2719. ser -= from;
  2720. if(ser<0 || ser > SIZE) return;
  2721. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  2722. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  2723. }
  2724. void CHeroList::draw()
  2725. {
  2726. for (int iT=0+from;iT<SIZE+from;iT++)
  2727. {
  2728. int i = iT-from;
  2729. if (iT>=items.size())
  2730. {
  2731. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32);
  2732. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32);
  2733. blitAt(empty,posporx,pospory+i*32);
  2734. continue;
  2735. }
  2736. const CGHeroInstance *cur = items[iT].first;
  2737. int pom = cur->movement / 100;
  2738. if (pom>25) pom=25;
  2739. if (pom<0) pom=0;
  2740. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32); //move point
  2741. pom = cur->mana / 5;
  2742. if (pom>25) pom=25;
  2743. if (pom<0) pom=0;
  2744. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32); //mana
  2745. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  2746. blitAt(temp,posporx,pospory+i*32);
  2747. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == HEROI_TYPE))
  2748. {
  2749. blitAt(selection,posporx,pospory+i*32);
  2750. }
  2751. //TODO: support for custom portraits
  2752. }
  2753. if (from>0)
  2754. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2755. else
  2756. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y);
  2757. if (items.size()-from > SIZE)
  2758. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2759. else
  2760. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y);
  2761. }
  2762. int CHeroList::getPosOfHero(const CArmedInstance* h)
  2763. {
  2764. return vstd::findPos(
  2765. items,h,
  2766. boost::bind(vstd::equal<std::pair<const CGHeroInstance*, CPath *>,
  2767. const CArmedInstance *,const CGHeroInstance*>,_1,
  2768. &std::pair<const CGHeroInstance*, CPath *>::first,_2));
  2769. }
  2770. CTownList::~CTownList()
  2771. {
  2772. delete arrup;
  2773. delete arrdo;
  2774. }
  2775. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  2776. :CList(Size)
  2777. {
  2778. arrup = CDefHandler::giveDef(arrupg);
  2779. arrdo = CDefHandler::giveDef(arrdog);
  2780. pos.x = x;
  2781. pos.y = y;
  2782. pos.w = std::max(arrdo->width, arrup->width);
  2783. pos.h = arrdo->height + arrup->height + Size*32;
  2784. arrupp.x=x;
  2785. arrupp.y=y;
  2786. arrupp.w=arrup->width;
  2787. arrupp.h=arrup->height;
  2788. arrdop.x=x;
  2789. arrdop.y=y+arrup->height+32*SIZE;
  2790. arrdop.w=arrdo->width;
  2791. arrdop.h=arrdo->height;
  2792. posporx = arrdop.x;
  2793. pospory = arrupp.y + arrupp.h;
  2794. pressed = indeterminate;
  2795. from = 0;
  2796. }
  2797. void CTownList::genList()
  2798. {
  2799. int howMany = LOCPLINT->cb->howManyTowns();
  2800. for (int i=0;i<howMany;i++)
  2801. {
  2802. items.push_back(LOCPLINT->cb->getTownInfo(i,0));
  2803. }
  2804. }
  2805. void CTownList::select(int which)
  2806. {
  2807. if (which>=items.size())
  2808. return;
  2809. selected = which;
  2810. if(!fun.empty())
  2811. fun();
  2812. }
  2813. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  2814. {
  2815. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2816. {
  2817. if (from>0)
  2818. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[306].first);
  2819. else
  2820. LOCPLINT->statusbar->clear();
  2821. return;
  2822. }
  2823. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  2824. {
  2825. if ((items.size()-from) > SIZE)
  2826. LOCPLINT->statusbar->print(CGI->generaltexth->zelp[307].first);
  2827. else
  2828. LOCPLINT->statusbar->clear();
  2829. return;
  2830. }
  2831. //if not buttons then towns
  2832. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2833. hx-=pos.x;
  2834. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2835. int ny = hy/32;
  2836. if ((ny>SIZE || ny<0) || (from+ny>=items.size()))
  2837. {
  2838. LOCPLINT->statusbar->clear();
  2839. return;
  2840. };
  2841. LOCPLINT->statusbar->print(items[from+ny]->name);
  2842. }
  2843. void CTownList::clickLeft(tribool down)
  2844. {
  2845. if (down)
  2846. {
  2847. /***************************ARROWS*****************************************/
  2848. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2849. {
  2850. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  2851. pressed = true;
  2852. return;
  2853. }
  2854. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>SIZE))
  2855. {
  2856. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  2857. pressed = false;
  2858. return;
  2859. }
  2860. /***************************TOWNS*****************************************/
  2861. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2862. hx-=pos.x;
  2863. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2864. int ny = hy/32;
  2865. if (ny>SIZE || ny<0)
  2866. return;
  2867. if(LOCPLINT->curint == LOCPLINT->adventureInt
  2868. && (ny+from)==selected
  2869. && LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE
  2870. )
  2871. LOCPLINT->openTownWindow(items[selected]);//print town screen
  2872. else
  2873. select(ny+from);
  2874. }
  2875. else
  2876. {
  2877. if (indeterminate(pressed))
  2878. return;
  2879. if (pressed) //up
  2880. {
  2881. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2882. pressed = indeterminate;
  2883. if (!down)
  2884. {
  2885. from--;
  2886. if (from<0)
  2887. from=0;
  2888. draw();
  2889. }
  2890. }
  2891. else if (!pressed) //down
  2892. {
  2893. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2894. pressed = indeterminate;
  2895. if (!down)
  2896. {
  2897. from++;
  2898. //if (from<items.size()-5)
  2899. // from=items.size()-5;
  2900. draw();
  2901. }
  2902. }
  2903. else
  2904. throw 0;
  2905. }
  2906. }
  2907. void CTownList::clickRight(tribool down)
  2908. {
  2909. if (down)
  2910. {
  2911. /***************************ARROWS*****************************************/
  2912. if(isItIn(&arrupp,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && from>0)
  2913. {
  2914. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  2915. }
  2916. else if(isItIn(&arrdop,LOCPLINT->current->motion.x,LOCPLINT->current->motion.y) && (items.size()-from>5))
  2917. {
  2918. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  2919. }
  2920. //if not buttons then towns
  2921. int hx = LOCPLINT->current->motion.x, hy = LOCPLINT->current->motion.y;
  2922. hx-=pos.x;
  2923. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  2924. int ny = hy/32;
  2925. if ((ny>5 || ny<0) || (from+ny>=items.size()))
  2926. {
  2927. return;
  2928. }
  2929. //show popup
  2930. 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);
  2931. ip->activate();
  2932. }
  2933. else
  2934. {
  2935. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down,this);
  2936. LOCPLINT->adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down,this);
  2937. }
  2938. }
  2939. void CTownList::hover (bool on)
  2940. {
  2941. }
  2942. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  2943. {
  2944. }
  2945. void CTownList::draw()
  2946. {
  2947. for (int iT=0+from;iT<SIZE+from;iT++)
  2948. {
  2949. int i = iT-from;
  2950. if (iT>=items.size())
  2951. {
  2952. blitAt(graphics->getPic(-1),posporx,pospory+i*32);
  2953. continue;
  2954. }
  2955. blitAt(graphics->getPic(items[iT]->subID,items[iT]->hasFort(),items[iT]->builded),posporx,pospory+i*32);
  2956. if ((selected == iT) && (LOCPLINT->adventureInt->selection->ID == TOWNI_TYPE))
  2957. {
  2958. blitAt(graphics->getPic(-2),posporx,pospory+i*32);
  2959. }
  2960. }
  2961. if (from>0)
  2962. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  2963. else
  2964. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y);
  2965. if (items.size()-from>SIZE)
  2966. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  2967. else
  2968. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y);
  2969. }
  2970. CCreaturePic::CCreaturePic(CCreature *cre, bool Big)
  2971. :c(cre),big(Big)
  2972. {
  2973. anim = new CCreatureAnimation(cre->animDefName);
  2974. }
  2975. CCreaturePic::~CCreaturePic()
  2976. {
  2977. delete anim;
  2978. }
  2979. int CCreaturePic::blitPic(SDL_Surface *to, int x, int y, bool nextFrame)
  2980. {
  2981. SDL_Rect dst;
  2982. if(big)
  2983. {
  2984. blitAt(graphics->backgrounds[c->faction],x,y);//curx-50,pos.y+130-65);
  2985. dst = genRect(130,100,x,y);
  2986. }
  2987. else
  2988. {
  2989. blitAt(graphics->backgroundsm[c->faction],x,y);//curx-50,pos.y+130-65);
  2990. dst = genRect(120,100,x,y);
  2991. }
  2992. if(c->isDoubleWide())
  2993. x-=15;
  2994. return anim->nextFrameMiddle(to,x+78,y+(big ? 55 : 45),true,0,nextFrame,false,false,&dst);
  2995. }
  2996. SDL_Surface * CCreaturePic::getPic(bool nextFrame)
  2997. {
  2998. //TODO: write
  2999. return NULL;
  3000. }
  3001. void CRecrutationWindow::close()
  3002. {
  3003. deactivate();
  3004. delete this;
  3005. LOCPLINT->curint->activate();
  3006. }
  3007. void CRecrutationWindow::Max()
  3008. {
  3009. slider->moveTo(slider->amount);
  3010. }
  3011. void CRecrutationWindow::Buy()
  3012. {
  3013. recruit(creatures[which].ID,slider->value);
  3014. close();
  3015. }
  3016. void CRecrutationWindow::Cancel()
  3017. {
  3018. close();
  3019. }
  3020. void CRecrutationWindow::sliderMoved(int to)
  3021. {
  3022. buy->block(!to);
  3023. }
  3024. void CRecrutationWindow::clickLeft(tribool down)
  3025. {
  3026. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3027. for(int i=0;i<creatures.size();i++)
  3028. {
  3029. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  3030. {
  3031. which = i;
  3032. int newAmount = std::min(amounts[i],creatures[i].amount);
  3033. slider->amount = newAmount;
  3034. if(slider->value > newAmount)
  3035. slider->moveTo(newAmount);
  3036. else
  3037. slider->moveTo(slider->value);
  3038. curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3039. for(int j=0;j<creatures.size();j++)
  3040. {
  3041. if(which==j)
  3042. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  3043. else
  3044. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  3045. curx += 120;
  3046. }
  3047. break;
  3048. }
  3049. curx += 120;
  3050. }
  3051. }
  3052. void CRecrutationWindow::clickRight( boost::logic::tribool down )
  3053. {
  3054. if(down)
  3055. {
  3056. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3057. for(int i=0;i<creatures.size();i++)
  3058. {
  3059. if(isItIn(&genRect(132,102,pos.x+curx,pos.y+64),LOCPLINT->current->motion.x,LOCPLINT->current->motion.y))
  3060. {
  3061. CCreInfoWindow *popup = new CCreInfoWindow(creatures[i].ID,0,0,NULL,NULL,NULL,NULL);
  3062. popup->activate();
  3063. break;
  3064. }
  3065. curx += 120;
  3066. }
  3067. }
  3068. }
  3069. void CRecrutationWindow::activate()
  3070. {
  3071. LOCPLINT->objsToBlit.push_back(this);
  3072. ClickableL::activate();
  3073. ClickableR::activate();
  3074. buy->activate();
  3075. max->activate();
  3076. cancel->activate();
  3077. slider->activate();
  3078. LOCPLINT->statusbar = bar;
  3079. }
  3080. void CRecrutationWindow::deactivate()
  3081. {
  3082. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  3083. ClickableL::deactivate();
  3084. ClickableR::deactivate();
  3085. buy->deactivate();
  3086. max->deactivate();
  3087. cancel->deactivate();
  3088. slider->deactivate();
  3089. }
  3090. void CRecrutationWindow::show(SDL_Surface * to)
  3091. {
  3092. static char c=0;
  3093. blitAt(bitmap,pos.x,pos.y,to?to:screen);
  3094. buy->show();
  3095. max->show();
  3096. cancel->show();
  3097. slider->show();
  3098. char pom[15];
  3099. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  3100. printAtMiddle(pom,pos.x+205,pos.y+252,GEOR13,zwykly,screen);
  3101. SDL_itoa(slider->value,pom,10); //recruit
  3102. printAtMiddle(pom,pos.x+279,pos.y+252,GEOR13,zwykly,screen);
  3103. 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"
  3104. int curx = pos.x+115-creatures[which].res.size()*16;
  3105. for(int i=0;i<creatures[which].res.size();i++)
  3106. {
  3107. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,pos.y+243,screen);
  3108. blitAt(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,pos.y+243,screen);
  3109. SDL_itoa(creatures[which].res[i].second,pom,10);
  3110. printAtMiddle(pom,curx+12,pos.y+286,GEOR13,zwykly,screen);
  3111. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  3112. printAtMiddle(pom,curx+12+258,pos.y+286,GEOR13,zwykly,screen);
  3113. curx+=32;
  3114. }
  3115. curx = pos.x + 192 + 102 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3116. for(int i=0;i<creatures.size();i++)
  3117. {
  3118. creatures[i].pic->blitPic(screen,curx-50,pos.y+130-65,!(c%4));
  3119. curx += 120;
  3120. }
  3121. c++;
  3122. bar->show();
  3123. }
  3124. CRecrutationWindow::CRecrutationWindow(const std::vector<std::pair<int,int> > &Creatures, const boost::function<void(int,int)> &Recruit) //creatures - pairs<creature_ID,amount>
  3125. :recruit(Recruit)
  3126. {
  3127. which = 0;
  3128. creatures.resize(Creatures.size());
  3129. amounts.resize(Creatures.size());
  3130. for(int i=0;i<creatures.size();i++)
  3131. {
  3132. creatures[i].amount = Creatures[i].second;
  3133. creatures[i].ID = Creatures[i].first;
  3134. for(int j=0;j<CGI->creh->creatures[Creatures[i].first].cost.size();j++)
  3135. if(CGI->creh->creatures[Creatures[i].first].cost[j])
  3136. creatures[i].res.push_back(std::make_pair(j,CGI->creh->creatures[Creatures[i].first].cost[j]));
  3137. creatures[i].pic = new CCreaturePic(&CGI->creh->creatures[Creatures[i].first]);
  3138. amounts[i] = CGI->creh->creatures[Creatures[i].first].maxAmount(LOCPLINT->cb->getResourceAmount());
  3139. }
  3140. SDL_Surface *hhlp = BitmapHandler::loadBitmap("TPRCRT.bmp");
  3141. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  3142. bitmap = SDL_ConvertSurface(hhlp,screen->format,0); //na 8bitowej mapie by sie psulo
  3143. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3144. SDL_FreeSurface(hhlp);
  3145. pos.x = screen->w/2 - bitmap->w/2;
  3146. pos.y = screen->h/2 - bitmap->h/2;
  3147. pos.w = bitmap->w;
  3148. pos.h = bitmap->h;
  3149. bar = new CStatusBar(pos.x+8, pos.y+370, "APHLFTRT.bmp", 471);
  3150. max = new AdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecrutationWindow::Max,this),pos.x+134,pos.y+313,"IRCBTNS.DEF",SDLK_m);
  3151. buy = new AdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecrutationWindow::Buy,this),pos.x+212,pos.y+313,"IBY6432.DEF",SDLK_RETURN);
  3152. cancel = new AdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecrutationWindow::Cancel,this),pos.x+290,pos.y+313,"ICN6432.DEF",SDLK_ESCAPE);
  3153. slider = new CSlider(pos.x+176,pos.y+279,135,boost::bind(&CRecrutationWindow::sliderMoved,this, _1),1,std::min(amounts[0],creatures[0].amount),0,true);
  3154. std::string pom;
  3155. printAtMiddle(CGI->generaltexth->allTexts[346],113,231,GEOR13,zwykly,bitmap); //cost per troop t
  3156. printAtMiddle(CGI->generaltexth->allTexts[465],205,231,GEOR13,zwykly,bitmap); //available t
  3157. printAtMiddle(CGI->generaltexth->allTexts[16],279,231,GEOR13,zwykly,bitmap); //recruit t
  3158. printAtMiddle(CGI->generaltexth->allTexts[466],373,231,GEOR13,zwykly,bitmap); //total cost t
  3159. drawBorder(bitmap,172,222,67,42,int3(239,215,123));
  3160. drawBorder(bitmap,246,222,67,42,int3(239,215,123));
  3161. drawBorder(bitmap,64,222,99,76,int3(239,215,123));
  3162. drawBorder(bitmap,322,222,99,76,int3(239,215,123));
  3163. drawBorder(bitmap,133,312,66,34,int3(173,142,66));
  3164. drawBorder(bitmap,211,312,66,34,int3(173,142,66));
  3165. drawBorder(bitmap,289,312,66,34,int3(173,142,66));
  3166. //border for creatures
  3167. int curx = 192 + 51 - (102*creatures.size()/2) - (18*(creatures.size()-1)/2);
  3168. for(int i=0;i<creatures.size();i++)
  3169. {
  3170. creatures[i].pos.x = curx+1;
  3171. creatures[i].pos.y = 65;
  3172. creatures[i].pos.w = 100;
  3173. creatures[i].pos.h = 130;
  3174. if(which==i)
  3175. drawBorder(bitmap,curx,64,102,132,int3(255,0,0));
  3176. else
  3177. drawBorder(bitmap,curx,64,102,132,int3(239,215,123));
  3178. curx += 120;
  3179. }
  3180. if(!creatures[0].amount || !amounts[0])
  3181. {
  3182. max->block(true);
  3183. slider->block(true);
  3184. }
  3185. //buy->block(true); //not needed, will be blocked by initing slider on 0
  3186. }
  3187. CRecrutationWindow::~CRecrutationWindow()
  3188. {
  3189. for(int i=0;i<creatures.size();i++)
  3190. {
  3191. delete creatures[i].pic;
  3192. }
  3193. delete max;
  3194. delete buy;
  3195. delete cancel;
  3196. SDL_FreeSurface(bitmap);
  3197. delete slider;
  3198. delete bar;
  3199. }
  3200. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  3201. {
  3202. last = Last;
  3203. which = 1;
  3204. c=cid;
  3205. slider = NULL;
  3206. gar = Owner;
  3207. bitmap = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  3208. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  3209. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3210. pos.x = screen->w/2 - bitmap->w/2;
  3211. pos.y = screen->h/2 - bitmap->h/2;
  3212. pos.w = bitmap->w;
  3213. pos.h = bitmap->h;
  3214. ok = new AdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  3215. cancel = new AdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  3216. int sliderPositions = max - (last>=0) - (last==2);
  3217. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),1,sliderPositions,val,true);
  3218. a1 = max-val;
  3219. a2 = val;
  3220. anim = new CCreaturePic(&CGI->creh->creatures[cid]);
  3221. anim->anim->setType(1);
  3222. std::string title = CGI->generaltexth->allTexts[256];
  3223. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid].namePl);
  3224. printAtMiddle(title,150,34,GEOR16,tytulowy,bitmap);
  3225. }
  3226. CSplitWindow::~CSplitWindow()
  3227. {
  3228. SDL_FreeSurface(bitmap);
  3229. delete ok;
  3230. delete cancel;
  3231. delete slider;
  3232. delete anim;
  3233. }
  3234. void CSplitWindow::activate()
  3235. {
  3236. ClickableL::activate();
  3237. LOCPLINT->objsToBlit.push_back(this);
  3238. KeyInterested::activate();
  3239. ok->activate();
  3240. cancel->activate();
  3241. slider->activate();
  3242. }
  3243. void CSplitWindow::deactivate()
  3244. {
  3245. ClickableL::deactivate();
  3246. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  3247. KeyInterested::deactivate();
  3248. ok->deactivate();
  3249. cancel->deactivate();
  3250. slider->deactivate();
  3251. }
  3252. void CSplitWindow::split()
  3253. {
  3254. gar->splitStacks(a2);
  3255. close();
  3256. }
  3257. void CSplitWindow::close()
  3258. {
  3259. deactivate();
  3260. delete this;
  3261. LOCPLINT->curint->activate();
  3262. }
  3263. void CSplitWindow::sliderMoved(int to)
  3264. {
  3265. int all = a1+a2;
  3266. a2 = to + (last==1 || last==2);
  3267. if(slider)
  3268. a1 = all - a2;
  3269. }
  3270. void CSplitWindow::show(SDL_Surface * to)
  3271. {
  3272. blitAt(bitmap,pos.x,pos.y,screen);
  3273. ok->show();
  3274. cancel->show();
  3275. slider->show();
  3276. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,GEOR16,zwykly,screen);
  3277. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,GEOR16,zwykly,screen);
  3278. anim->blitPic(screen,pos.x+20,pos.y+54,false);
  3279. anim->blitPic(screen,pos.x+177,pos.y+54,false);
  3280. }
  3281. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  3282. {
  3283. if(key.state != SDL_PRESSED)
  3284. return;
  3285. int &cur = (which ? a2 : a1),
  3286. &sec = (which ? a1 : a2),
  3287. ncur = cur;
  3288. if (key.keysym.sym == SDLK_BACKSPACE)
  3289. {
  3290. ncur /= 10;
  3291. }
  3292. else if(key.keysym.sym == SDLK_TAB)
  3293. {
  3294. which = !which;
  3295. }
  3296. else
  3297. {
  3298. int number = key.keysym.sym - SDLK_0;
  3299. if (number < 0 || number > 9) //not a number pressed
  3300. {
  3301. return;
  3302. }
  3303. ncur = cur*10 + number;
  3304. }
  3305. int delta = ncur - cur;
  3306. if(delta > sec)
  3307. {
  3308. cur += sec;
  3309. sec = 0;
  3310. }
  3311. slider->moveTo(which ? ncur : a1+a2-ncur);
  3312. }
  3313. void CSplitWindow::clickLeft( boost::logic::tribool down )
  3314. {
  3315. if(down)
  3316. {
  3317. Point click(LOCPLINT->current->motion.x,LOCPLINT->current->motion.y);
  3318. click -= pos.topLeft();
  3319. if(Rect(19,216,105,40).isIn(click))
  3320. which = 0;
  3321. else if(Rect(175,216,105,40).isIn(click))
  3322. which = 1;
  3323. }
  3324. }
  3325. void CCreInfoWindow::show(SDL_Surface * to)
  3326. {
  3327. char pom[15];
  3328. blitAt(bitmap,pos.x,pos.y,screen);
  3329. anim->blitPic(screen,pos.x+21,pos.y+48,(type) && !(anf%4));
  3330. if(++anf==4)
  3331. anf=0;
  3332. if(count.size())
  3333. printTo(count.c_str(),pos.x+114,pos.y+174,GEOR16,zwykly);
  3334. if(upgrade)
  3335. upgrade->show();
  3336. if(dismiss)
  3337. dismiss->show();
  3338. if(ok)
  3339. ok->show();
  3340. }
  3341. CCreInfoWindow::CCreInfoWindow(int Cid, int Type, int creatureCount, StackState *State, boost::function<void()> Upg, boost::function<void()> Dsm, UpgradeInfo *ui)
  3342. :ok(0),dismiss(0),upgrade(0),type(Type),dsm(Dsm),dependant(0)
  3343. {
  3344. active = false;
  3345. anf = 0;
  3346. c = &CGI->creh->creatures[Cid];
  3347. bitmap = BitmapHandler::loadBitmap("CRSTKPU.bmp");
  3348. pos.x = screen->w/2 - bitmap->w/2;
  3349. pos.y = screen->h/2 - bitmap->h/2;
  3350. pos.w = bitmap->w;
  3351. pos.h = bitmap->h;
  3352. graphics->blueToPlayersAdv(bitmap,LOCPLINT->playerID);
  3353. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3354. anim = new CCreaturePic(c);
  3355. if(!type) anim->anim->setType(2);
  3356. char pom[75];int hlp=0;
  3357. if(creatureCount)
  3358. {
  3359. SDL_itoa(creatureCount,pom,10);
  3360. count = pom;
  3361. }
  3362. printAtMiddle(c->namePl,149,30,GEOR13,tytulowy,bitmap); //creature name
  3363. //atttack
  3364. printAt(CGI->generaltexth->primarySkillNames[0],155,48,GEOR13,zwykly,bitmap);
  3365. SDL_itoa(c->attack,pom,10);
  3366. if(State && State->attackBonus)
  3367. {
  3368. int hlp;
  3369. if(c->attack > 0)
  3370. hlp = log10f(c->attack)+2;
  3371. else
  3372. hlp = 2;
  3373. pom[hlp-1] = ' '; pom[hlp] = '(';
  3374. SDL_itoa(c->attack+State->attackBonus,pom+hlp+1,10);
  3375. hlp += 2+(int)log10f(State->attackBonus+c->attack);
  3376. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3377. }
  3378. printToWR(pom,276,61,GEOR13,zwykly,bitmap);
  3379. //defense
  3380. printAt(CGI->generaltexth->primarySkillNames[1],155,67,GEOR13,zwykly,bitmap);
  3381. SDL_itoa(c->defence,pom,10);
  3382. if(State && State->defenseBonus)
  3383. {
  3384. int hlp;
  3385. if(c->defence > 0)
  3386. hlp = log10f(c->defence)+2;
  3387. else
  3388. hlp = 2;
  3389. pom[hlp-1] = ' '; pom[hlp] = '(';
  3390. SDL_itoa(c->defence+State->defenseBonus,pom+hlp+1,10);
  3391. hlp += 2+(int)log10f(State->defenseBonus+c->defence);
  3392. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3393. }
  3394. printToWR(pom,276,80,GEOR13,zwykly,bitmap);
  3395. //shots
  3396. if(c->shots)
  3397. {
  3398. printAt(CGI->generaltexth->allTexts[198],155,86,GEOR13,zwykly,bitmap);
  3399. if(State)
  3400. sprintf(pom,"%d(%d)",c->shots,State->shotsLeft);
  3401. else
  3402. SDL_itoa(c->shots,pom,10);
  3403. printToWR(pom,276,99,GEOR13,zwykly,bitmap);
  3404. }
  3405. //damage
  3406. printAt(CGI->generaltexth->allTexts[199],155,105,GEOR13,zwykly,bitmap);
  3407. SDL_itoa(c->damageMin,pom,10);
  3408. if(c->damageMin > 0)
  3409. hlp = log10f(c->damageMin)+2;
  3410. else
  3411. hlp = 2;
  3412. pom[hlp-1]=' '; pom[hlp]='-'; pom[hlp+1]=' ';
  3413. SDL_itoa(c->damageMax,pom+hlp+2,10);
  3414. printToWR(pom,276,118,GEOR13,zwykly,bitmap);
  3415. //health
  3416. printAt(CGI->generaltexth->allTexts[388],155,124,GEOR13,zwykly,bitmap);
  3417. SDL_itoa(c->hitPoints,pom,10);
  3418. printToWR(pom,276,137,GEOR13,zwykly,bitmap);
  3419. //remaining health
  3420. if(State && State->currentHealth)
  3421. {
  3422. printAt(CGI->generaltexth->allTexts[200],155,143,GEOR13,zwykly,bitmap);
  3423. SDL_itoa(State->currentHealth,pom,10);
  3424. printToWR(pom,276,156,GEOR13,zwykly,bitmap);
  3425. }
  3426. //speed
  3427. printAt(CGI->generaltexth->zelp[441].first,155,162,GEOR13,zwykly,bitmap);
  3428. SDL_itoa(c->speed,pom,10);
  3429. if(State && State->speedBonus)
  3430. {
  3431. int hlp;
  3432. if(c->speed > 0)
  3433. hlp = log10f(c->speed)+2;
  3434. else
  3435. hlp = 2;
  3436. pom[hlp-1] = ' '; pom[hlp] = '(';
  3437. SDL_itoa(c->speed + State->speedBonus, pom+hlp+1, 10);
  3438. hlp += 2+(int)log10f(c->speed + State->speedBonus);
  3439. pom[hlp] = ')'; pom[hlp+1] = '\0';
  3440. }
  3441. printToWR(pom,276,175,GEOR13,zwykly,bitmap);
  3442. //luck and morale
  3443. blitAt(graphics->morale42->ourImages[(State)?(State->morale+3):(3)].bitmap,24,189,bitmap);
  3444. blitAt(graphics->luck42->ourImages[(State)?(State->luck+3):(3)].bitmap,77,189,bitmap);
  3445. //print abilities text - if r-click popup
  3446. if(type)
  3447. {
  3448. if(Upg && ui)
  3449. {
  3450. bool enough = true;
  3451. for(std::set<std::pair<int,int> >::iterator i=ui->cost[0].begin(); i!=ui->cost[0].end(); i++) //calculate upgrade cost
  3452. {
  3453. if(LOCPLINT->cb->getResourceAmount(i->first) < i->second*creatureCount)
  3454. enough = false;
  3455. upgResCost.push_back(new SComponent(SComponent::resource,i->first,i->second*creatureCount));
  3456. }
  3457. if(enough)
  3458. {
  3459. CFunctionList<void()> fs[2];
  3460. fs[0] += Upg;
  3461. fs[0] += boost::bind(&CCreInfoWindow::close,this);
  3462. fs[1] += boost::bind(&CCreInfoWindow::activate,this);
  3463. CFunctionList<void()> cfl;
  3464. cfl = boost::bind(&CCreInfoWindow::deactivate,this);
  3465. cfl += boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[207],boost::ref(upgResCost),fs[0],fs[1],false,false);
  3466. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,cfl,pos.x+76,pos.y+237,"IVIEWCR.DEF",SDLK_u);
  3467. }
  3468. else
  3469. {
  3470. upgrade = new AdventureMapButton("",CGI->generaltexth->zelp[446].second,boost::function<void()>(),pos.x+76,pos.y+237,"IVIEWCR.DEF");
  3471. upgrade->callback.funcs.clear();
  3472. upgrade->bitmapOffset = 2;
  3473. }
  3474. }
  3475. if(Dsm)
  3476. {
  3477. CFunctionList<void()> fs[2];
  3478. //on dismiss confirmed
  3479. fs[0] += Dsm; //dismiss
  3480. fs[0] += boost::bind(&CCreInfoWindow::close,this);//close this window
  3481. fs[1] += boost::bind(&CCreInfoWindow::activate,this);
  3482. CFunctionList<void()> cfl;
  3483. cfl = boost::bind(&CCreInfoWindow::deactivate,this);
  3484. cfl += boost::bind(&CPlayerInterface::showYesNoDialog,LOCPLINT,CGI->generaltexth->allTexts[12],std::vector<SComponent*>(),fs[0],fs[1],false,false);
  3485. dismiss = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,cfl,pos.x+21,pos.y+237,"IVIEWCR2.DEF",SDLK_d);
  3486. }
  3487. ok = new AdventureMapButton("",CGI->generaltexth->zelp[445].second,boost::bind(&CCreInfoWindow::close,this),pos.x+216,pos.y+237,"IOKAY.DEF",SDLK_RETURN);
  3488. }
  3489. else
  3490. {
  3491. printAtWB(c->abilityText,17,231,GEOR13,35,zwykly,bitmap);
  3492. }
  3493. //spell effects
  3494. if(State)
  3495. {
  3496. int printed=0; //how many effect pics have been printed
  3497. for(std::set<int>::const_iterator it = State->effects.begin(); it!=State->effects.end(); ++it)
  3498. {
  3499. blitAt(graphics->spellEffectsPics->ourImages[*it + 1].bitmap, 127 + 52 * printed, 186, bitmap);
  3500. ++printed;
  3501. if(printed >= 3)
  3502. {
  3503. break;
  3504. }
  3505. }
  3506. }
  3507. }
  3508. CCreInfoWindow::~CCreInfoWindow()
  3509. {
  3510. SDL_FreeSurface(bitmap);
  3511. delete anim;
  3512. delete upgrade;
  3513. delete ok;
  3514. delete dismiss;
  3515. for(int i=0; i<upgResCost.size();i++)
  3516. delete upgResCost[i];
  3517. }
  3518. void CCreInfoWindow::activate()
  3519. {
  3520. if(active) return;
  3521. active = true;
  3522. if(!type)
  3523. ClickableR::activate();
  3524. LOCPLINT->objsToBlit.push_back(this);
  3525. if(ok)
  3526. ok->activate();
  3527. if(dismiss)
  3528. dismiss->activate();
  3529. if(upgrade)
  3530. upgrade->activate();
  3531. }
  3532. void CCreInfoWindow::close()
  3533. {
  3534. deactivate();
  3535. if(dynamic_cast<CHeroWindow*>(LOCPLINT->curint->subInt))
  3536. {
  3537. if(type)
  3538. LOCPLINT->curint->subInt->activate();
  3539. }
  3540. else
  3541. {
  3542. CCastleInterface *c = dynamic_cast<CCastleInterface*>(LOCPLINT->curint);
  3543. if(c && !c->subInt)
  3544. c->showAll();
  3545. if(type)
  3546. LOCPLINT->curint->activate();
  3547. }
  3548. delete this;
  3549. }
  3550. void CCreInfoWindow::clickRight(boost::logic::tribool down)
  3551. {
  3552. if(down)
  3553. return;
  3554. close();
  3555. }
  3556. void CCreInfoWindow::dismissF()
  3557. {
  3558. dsm();
  3559. close();
  3560. }
  3561. void CCreInfoWindow::keyPressed (const SDL_KeyboardEvent & key)
  3562. {
  3563. }
  3564. void CCreInfoWindow::deactivate()
  3565. {
  3566. if(!active) return;
  3567. active = false;
  3568. if(!type)
  3569. ClickableR::deactivate();
  3570. LOCPLINT->objsToBlit -= static_cast<IShowable*>(this);
  3571. if(ok)
  3572. ok->deactivate();
  3573. if(dismiss)
  3574. dismiss->deactivate();
  3575. if(upgrade)
  3576. upgrade->deactivate();
  3577. }
  3578. void CCreInfoWindow::onUpgradeYes()
  3579. {
  3580. dependant->close();
  3581. dependant->deactivate();
  3582. delete dependant;
  3583. LOCPLINT->curint->activate();
  3584. }
  3585. void CCreInfoWindow::onUpgradeNo()
  3586. {
  3587. }
  3588. void CLevelWindow::close()
  3589. {
  3590. deactivate();
  3591. for(int i=0;i<comps.size();i++)
  3592. {
  3593. if(comps[i]->selected)
  3594. {
  3595. cb(i);
  3596. break;
  3597. }
  3598. }
  3599. delete this;
  3600. LOCPLINT->curint->activate();
  3601. LOCPLINT->showingDialog->setn(false);
  3602. }
  3603. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  3604. {
  3605. LOCPLINT->showingDialog->setn(true);
  3606. heroType = hero->subID;
  3607. cb = callback;
  3608. for(int i=0;i<skills.size();i++)
  3609. {
  3610. comps.push_back(new CSelectableComponent(SComponent::secskill44,skills[i],hero->getSecSkillLevel(skills[i])+1,boost::bind(&CLevelWindow::selectionChanged,this,i)));
  3611. comps.back()->assignedKeys.insert(SDLK_1 + i);
  3612. }
  3613. bitmap = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  3614. graphics->blueToPlayersAdv(bitmap,hero->tempOwner);
  3615. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  3616. pos.x = screen->w/2 - bitmap->w/2;
  3617. pos.y = screen->h/2 - bitmap->h/2;
  3618. pos.w = bitmap->w;
  3619. pos.h = bitmap->h;
  3620. ok = new AdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  3621. //draw window
  3622. char buf[100], buf2[100];
  3623. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  3624. sprintf(buf,buf2,hero->name.c_str());
  3625. printAtMiddle(buf,192,35,GEOR16,zwykly,bitmap);
  3626. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  3627. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  3628. printAtMiddle(buf,192,162,GEOR16,zwykly,bitmap);
  3629. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  3630. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,252,GEOR16,zwykly,bitmap);
  3631. SDL_Surface * ort = TTF_RenderText_Blended(GEOR16,CGI->generaltexth->allTexts[4].c_str(),zwykly);
  3632. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+ort->w) )/2;
  3633. for(int i=0;i<comps.size();i++)
  3634. {
  3635. comps[i]->pos.x = curx+pos.x;
  3636. comps[i]->pos.y = 326+pos.y;
  3637. if( i < (comps.size()-1) )
  3638. {
  3639. curx += 44 + 18; //skill width + margin to "or"
  3640. blitAt(ort,curx,346,bitmap);
  3641. curx += ort->w + 18;
  3642. }
  3643. }
  3644. if(comps.size() > 1)
  3645. {
  3646. ok->block(true);
  3647. }
  3648. else if(comps.size() == 1)
  3649. {
  3650. comps[0]->select(true);
  3651. }
  3652. SDL_FreeSurface(ort);
  3653. }
  3654. void CLevelWindow::selectionChanged(unsigned to)
  3655. {
  3656. if(ok->blocked)
  3657. ok->block(false);
  3658. for(int i=0;i<comps.size();i++)
  3659. if(i==to)
  3660. comps[i]->select(true);
  3661. else
  3662. comps[i]->select(false);
  3663. }
  3664. CLevelWindow::~CLevelWindow()
  3665. {
  3666. delete ok;
  3667. for(int i=0;i<comps.size();i++)
  3668. delete comps[i];
  3669. SDL_FreeSurface(bitmap);
  3670. }
  3671. void CLevelWindow::activate()
  3672. {
  3673. LOCPLINT->objsToBlit.push_back(this);
  3674. ok->activate();
  3675. for(int i=0;i<comps.size();i++)
  3676. comps[i]->activate();
  3677. }
  3678. void CLevelWindow::deactivate()
  3679. {
  3680. LOCPLINT->objsToBlit.erase(std::find(LOCPLINT->objsToBlit.begin(),LOCPLINT->objsToBlit.end(),this));
  3681. ok->deactivate();
  3682. for(int i=0;i<comps.size();i++)
  3683. comps[i]->deactivate();
  3684. }
  3685. void CLevelWindow::show(SDL_Surface * to)
  3686. {
  3687. blitAt(bitmap,pos.x,pos.y,screen);
  3688. blitAt(graphics->portraitLarge[heroType],170+pos.x,66+pos.y);
  3689. ok->show();
  3690. for(int i=0;i<comps.size();i++)
  3691. comps[i]->show();
  3692. }
  3693. void CMinorResDataBar::show(SDL_Surface * to)
  3694. {
  3695. blitAt(bg,pos.x,pos.y);
  3696. char * buf = new char[15];
  3697. for (int i=0;i<7;i++)
  3698. {
  3699. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3700. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,GEOR13,zwykly);
  3701. }
  3702. std::vector<std::string> temp;
  3703. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  3704. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  3705. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  3706. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  3707. CGI->generaltexth->allTexts[62]
  3708. +": %s, "
  3709. + CGI->generaltexth->allTexts[63]
  3710. + ": %s, "
  3711. + CGI->generaltexth->allTexts[64]
  3712. + ": %s",temp)
  3713. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,GEOR13,zwykly);
  3714. temp.clear();
  3715. //updateRect(&pos,screen);
  3716. delete[] buf;
  3717. }
  3718. CMinorResDataBar::CMinorResDataBar()
  3719. {
  3720. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  3721. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  3722. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  3723. pos.x = 7;
  3724. pos.y = 575;
  3725. pos.w = bg->w;
  3726. pos.h = bg->h;
  3727. }
  3728. CMinorResDataBar::~CMinorResDataBar()
  3729. {
  3730. SDL_FreeSurface(bg);
  3731. }
  3732. SDL_Surface * CCustomImgComponent::getImg()
  3733. {
  3734. return bmp;
  3735. }
  3736. CCustomImgComponent::CCustomImgComponent( Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur )
  3737. :bmp(sur), free(freeSur)
  3738. {
  3739. init(Type,Subtype,Val);
  3740. }
  3741. CCustomImgComponent::~CCustomImgComponent()
  3742. {
  3743. if(free)
  3744. SDL_FreeSurface(bmp);
  3745. }
  3746. CMarketplaceWindow::CTradeableItem::CTradeableItem( int Type, int ID, bool Left)
  3747. {
  3748. left = Left;
  3749. type = Type;
  3750. id = ID;
  3751. }
  3752. void CMarketplaceWindow::CTradeableItem::show( SDL_Surface * to/*=NULL*/ )
  3753. {
  3754. SDL_Surface *hlp = getSurface();
  3755. blitAt(hlp,pos.x+19,pos.y+9,to);
  3756. }
  3757. void CMarketplaceWindow::CTradeableItem::clickLeft( boost::logic::tribool down )
  3758. {
  3759. CMarketplaceWindow *mw = static_cast<CMarketplaceWindow *>(LOCPLINT->curint->subInt);
  3760. if(down)
  3761. {
  3762. if(left)
  3763. {
  3764. if(mw->hLeft != this)
  3765. mw->hLeft = this;
  3766. else
  3767. return;
  3768. }
  3769. else
  3770. {
  3771. if(mw->hRight != this)
  3772. mw->hRight = this;
  3773. else
  3774. return;
  3775. }
  3776. mw->selectionChanged(left);
  3777. }
  3778. }
  3779. void CMarketplaceWindow::CTradeableItem::activate()
  3780. {
  3781. ClickableL::activate();
  3782. }
  3783. void CMarketplaceWindow::CTradeableItem::deactivate()
  3784. {
  3785. ClickableL::deactivate();
  3786. }
  3787. SDL_Surface * CMarketplaceWindow::CTradeableItem::getSurface()
  3788. {
  3789. switch(type)
  3790. {
  3791. case 0:
  3792. return graphics->resources32->ourImages[id].bitmap;
  3793. case 1:
  3794. return graphics->artDefs->ourImages[id].bitmap;
  3795. default:
  3796. return NULL;
  3797. }
  3798. }
  3799. void initItems( std::vector<CMarketplaceWindow::CTradeableItem*> &i, std::vector<SDL_Rect> &p, int type, int amount, bool left, std::vector<int> *ids/*=NULL*/ )
  3800. {
  3801. i.resize(amount);
  3802. for(int j=0;j<amount;j++)
  3803. {
  3804. i[j] = new CMarketplaceWindow::CTradeableItem(type,(ids && ids->size()>j) ? (*ids)[j] : j, left);
  3805. i[j]->pos = p[j];
  3806. }
  3807. }
  3808. void CMarketplaceWindow::setMode( int mode )
  3809. {
  3810. std::vector<SDL_Rect> lpos, rpos;
  3811. clear();
  3812. switch(mode)
  3813. {
  3814. case 0:
  3815. {
  3816. SDL_Surface *bg2 = BitmapHandler::loadBitmap("TPMRKRES.bmp");
  3817. SDL_SetColorKey(bg2,SDL_SRCCOLORKEY,SDL_MapRGB(bg2->format,0,255,255));
  3818. graphics->blueToPlayersAdv(bg2,LOCPLINT->playerID);
  3819. bg = SDL_ConvertSurface(bg2,screen->format,0);
  3820. SDL_FreeSurface(bg2);
  3821. lpos += genRect(66,69,39,180), genRect(66,69,122,180), genRect(66,69,204,180),
  3822. genRect(66,69,39,259), genRect(66,69,122,259), genRect(66,69,204,259),
  3823. genRect(66,69,122,338);
  3824. for(int i=0;i<lpos.size();i++)
  3825. {
  3826. lpos[i].x += pos.x;
  3827. lpos[i].y += pos.y;
  3828. rpos.push_back(lpos[i]);
  3829. rpos[rpos.size()-1].x += 288;
  3830. }
  3831. initItems(left,lpos,0,7,true,NULL);
  3832. initItems(right,rpos,0,7,false,NULL);
  3833. printAtMiddle(CGI->generaltexth->allTexts[158],303,28,GEORXX,tytulowy,bg); //title
  3834. printAtMiddle(CGI->generaltexth->allTexts[270],158,148,GEOR13,zwykly,bg); //kingdom res.
  3835. printAtMiddle(CGI->generaltexth->allTexts[168],450,148,GEOR13,zwykly,bg); //available for trade
  3836. }
  3837. }
  3838. }
  3839. void CMarketplaceWindow::clear()
  3840. {
  3841. for(int i=0;i<left.size();i++)
  3842. delete left[i];
  3843. for(int i=0;i<right.size();i++)
  3844. delete right[i];
  3845. left.clear();
  3846. right.clear();
  3847. SDL_FreeSurface(bg);
  3848. }
  3849. CMarketplaceWindow::CMarketplaceWindow(int Mode)
  3850. {
  3851. mode = Mode;
  3852. bg = NULL;
  3853. ok = max = deal = NULL;
  3854. pos.x = screen->w/2 - 300;
  3855. pos.y = screen->h/2 - 296;
  3856. slider = new CSlider(pos.x+231,pos.y+490,137,boost::bind(&CMarketplaceWindow::sliderMoved,this,_1),0,0);
  3857. setMode(mode);
  3858. hLeft = hRight = NULL;
  3859. ok = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::deactivate,this),pos.x+516,pos.y+520,"IOK6432.DEF",SDLK_RETURN);
  3860. ok->callback += boost::bind(&CMarketplaceWindow::clear,this); //clear
  3861. ok->callback += boost::bind(vstd::delObj<CMarketplaceWindow>,this); //will delete
  3862. ok->callback += boost::bind(&CMainInterface::activate,LOCPLINT->curint);
  3863. deal = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::makeDeal,this),pos.x+307,pos.y+520,"TPMRKB.DEF");
  3864. max = new AdventureMapButton("","",boost::bind(&CMarketplaceWindow::setMax,this),pos.x+229,pos.y+520,"IRCBTNS.DEF");
  3865. max->block(true);
  3866. deal->block(true);
  3867. }
  3868. CMarketplaceWindow::~CMarketplaceWindow()
  3869. {
  3870. delete slider;
  3871. }
  3872. void CMarketplaceWindow::show( SDL_Surface * to/*=NULL*/ )
  3873. {
  3874. blitAt(bg,pos);
  3875. if(hRight)
  3876. CSDL_Ext::drawBorder(screen,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  3877. if(hLeft)
  3878. CSDL_Ext::drawBorder(screen,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  3879. ok->show();
  3880. deal->show();
  3881. max->show();
  3882. slider->show();
  3883. for(int i=0;i<left.size();i++)
  3884. left[i]->show();
  3885. for(int i=0;i<right.size();i++)
  3886. right[i]->show();
  3887. if(mode==0)
  3888. {
  3889. char buf[15];
  3890. for(int i=0;i<left.size();i++)
  3891. {
  3892. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  3893. printAtMiddle(buf,left[i]->pos.x+35,left[i]->pos.y+56,GEOR13,zwykly);
  3894. }
  3895. if(hLeft) //print prices
  3896. {
  3897. for(int i=0; i<right.size();i++)
  3898. {
  3899. if(right[i]->id != hLeft->id)
  3900. printAtMiddle(rSubs[i],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly);
  3901. else
  3902. printAtMiddle(CGI->generaltexth->allTexts[164],right[i]->pos.x+35,right[i]->pos.y+56,GEOR13,zwykly);
  3903. }
  3904. }
  3905. if(hLeft && hRight && hLeft->id!= hRight->id)
  3906. {
  3907. blitAt(hLeft->getSurface(),pos.x+142,pos.y+457,screen);
  3908. blitAt(hRight->getSurface(),pos.x+430,pos.y+457,screen);
  3909. SDL_itoa(slider->value * r1,buf,10);
  3910. printAtMiddle(buf,pos.x+158,pos.y+504,GEOR13,zwykly);
  3911. SDL_itoa(slider->value * r2,buf,10);
  3912. printAtMiddle(buf,pos.x+446,pos.y+504,GEOR13,zwykly);
  3913. }
  3914. }
  3915. }
  3916. void CMarketplaceWindow::activate()
  3917. {
  3918. LOCPLINT->objsToBlit += this;
  3919. LOCPLINT->curint->subInt = this;
  3920. for(int i=0;i<left.size();i++)
  3921. left[i]->activate();
  3922. for(int i=0;i<right.size();i++)
  3923. right[i]->activate();
  3924. ok->activate();
  3925. max->activate();
  3926. deal->activate();
  3927. slider->activate();
  3928. }
  3929. void CMarketplaceWindow::deactivate()
  3930. {
  3931. LOCPLINT->objsToBlit -= this;
  3932. LOCPLINT->curint->subInt = NULL;
  3933. for(int i=0;i<left.size();i++)
  3934. left[i]->deactivate();
  3935. for(int i=0;i<right.size();i++)
  3936. right[i]->deactivate();
  3937. ok->deactivate();
  3938. max->deactivate();
  3939. deal->deactivate();
  3940. slider->deactivate();
  3941. }
  3942. void CMarketplaceWindow::setMax()
  3943. {
  3944. slider->moveTo(slider->amount);
  3945. }
  3946. void CMarketplaceWindow::makeDeal()
  3947. {
  3948. LOCPLINT->cb->trade(mode,hLeft->id,hRight->id,slider->value*r1);
  3949. slider->moveTo(0);
  3950. hLeft = NULL;
  3951. selectionChanged(true);
  3952. }
  3953. void CMarketplaceWindow::sliderMoved( int to )
  3954. {
  3955. }
  3956. void CMarketplaceWindow::selectionChanged(bool side)
  3957. {
  3958. if(hLeft && hRight && hLeft->id!= hRight->id)
  3959. {
  3960. LOCPLINT->cb->getMarketOffer(hLeft->id,hRight->id,r1,r2,0);
  3961. slider->amount = LOCPLINT->cb->getResourceAmount(hLeft->id) / r1;
  3962. slider->moveTo(0);
  3963. max->block(false);
  3964. deal->block(false);
  3965. }
  3966. else
  3967. {
  3968. max->block(true);
  3969. deal->block(true);
  3970. slider->amount = 0;
  3971. slider->moveTo(0);
  3972. }
  3973. if(side && hLeft) //left selection changed, recalculate offers
  3974. {
  3975. rSubs.clear();
  3976. rSubs.resize(right.size());
  3977. int h1, h2;
  3978. for(int i=0;i<right.size();i++)
  3979. {
  3980. std::ostringstream oss;
  3981. LOCPLINT->cb->getMarketOffer(hLeft->id,i,h1,h2,0);
  3982. oss << h2;
  3983. if(h1!=1)
  3984. oss << "/" << h1;
  3985. rSubs[i] = oss.str();
  3986. }
  3987. }
  3988. }
  3989. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  3990. {
  3991. this->pos = pos;
  3992. background = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  3993. graphics->blueToPlayersAdv(background, LOCPLINT->playerID);
  3994. //printing texts
  3995. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 240, 32, GEOR16, tytulowy, background); //window title
  3996. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 65, GEOR16, tytulowy, background); //hero speed
  3997. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 131, GEOR16, tytulowy, background); //enemy speed
  3998. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 197, GEOR16, tytulowy, background); //map scroll speed
  3999. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 263, GEOR16, tytulowy, background); //video quality
  4000. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 348, GEOR16, tytulowy, background); //music volume
  4001. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 414, GEOR16, tytulowy, background); //effects volume
  4002. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 283, 57, GEOR16, zwykly, background); //show move path
  4003. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 283, 89, GEOR16, zwykly, background); //show hero reminder
  4004. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 283, 121, GEOR16, zwykly, background); //quick combat
  4005. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 283, 153, GEOR16, zwykly, background); //video subtitles
  4006. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 283, 185, GEOR16, zwykly, background); //town building outlines
  4007. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 283, 217, GEOR16, zwykly, background); //spell book animation
  4008. //setting up buttons
  4009. save = new AdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::bsavef, this), 516, 354, "SOSAVE.DEF", SDLK_s);
  4010. std::swap(save->imgs[0][0], save->imgs[0][1]);
  4011. quitGame = new AdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), 405, 471, "soquit.def", SDLK_q);
  4012. std::swap(quitGame->imgs[0][0], quitGame->imgs[0][1]);
  4013. backToMap = new AdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), 516, 471, "soretrn.def", SDLK_RETURN);
  4014. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  4015. std::swap(backToMap->imgs[0][0], backToMap->imgs[0][1]);
  4016. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  4017. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].first),CGI->generaltexth->zelp[349].second, "sysopb1.def", 187, 134, 1);
  4018. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].first),CGI->generaltexth->zelp[350].second, "sysopb2.def", 235, 134, 2);
  4019. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].first),CGI->generaltexth->zelp[351].second, "sysopb3.def", 283, 134, 4);
  4020. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].first),CGI->generaltexth->zelp[352].second, "sysopb4.def", 331, 134, 8);
  4021. heroMoveSpeed->select(owner->heroMoveSpeed, 1);
  4022. heroMoveSpeed->onChange = boost::bind(&CPlayerInterface::setHeroMoveSpeed, owner, _1);
  4023. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  4024. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].first),CGI->generaltexth->zelp[357].second, "sysopb9.def", 187, 267, 1);
  4025. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].first),CGI->generaltexth->zelp[358].second, "sysob10.def", 251, 267, 2);
  4026. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].first),CGI->generaltexth->zelp[359].second, "sysob11.def", 315, 267, 4);
  4027. mapScrollSpeed->select(owner->mapScrollingSpeed, 1);
  4028. mapScrollSpeed->onChange = boost::bind(&CPlayerInterface::setMapScrollingSpeed, owner, _1);
  4029. }
  4030. CSystemOptionsWindow::~CSystemOptionsWindow()
  4031. {
  4032. SDL_FreeSurface(background);
  4033. delete save;
  4034. delete quitGame;
  4035. delete backToMap;
  4036. delete heroMoveSpeed;
  4037. delete mapScrollSpeed;
  4038. }
  4039. void CSystemOptionsWindow::bquitf()
  4040. {
  4041. this->deactivate();
  4042. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<SComponent*>(), boost::bind(exit, 0), boost::bind(&CSystemOptionsWindow::activate, this), false, false);
  4043. }
  4044. void CSystemOptionsWindow::breturnf()
  4045. {
  4046. deactivate();
  4047. for(int g=0; g<LOCPLINT->objsToBlit.size(); ++g)
  4048. {
  4049. if(dynamic_cast<CSystemOptionsWindow*>(LOCPLINT->objsToBlit[g]))
  4050. {
  4051. LOCPLINT->objsToBlit.erase(LOCPLINT->objsToBlit.begin()+g);
  4052. break;
  4053. }
  4054. }
  4055. delete this;
  4056. LOCPLINT->curint->activate();
  4057. }
  4058. void CSystemOptionsWindow::bsavef()
  4059. {
  4060. using namespace boost::posix_time;
  4061. std::ostringstream fnameStream;
  4062. fnameStream << second_clock::local_time();
  4063. std::string fname = fnameStream.str();
  4064. boost::algorithm::replace_all(fname,":","");
  4065. boost::algorithm::replace_all(fname," ","-");
  4066. LOCPLINT->showYesNoDialog("Do you want to save current game as " + fname, std::vector<SComponent*>(), boost::bind(&CCallback::save, LOCPLINT->cb, fname), boost::bind(&CSystemOptionsWindow::activate, this), false, false);
  4067. }
  4068. void CSystemOptionsWindow::activate()
  4069. {
  4070. save->activate();
  4071. quitGame->activate();
  4072. backToMap->activate();
  4073. heroMoveSpeed->activate();
  4074. mapScrollSpeed->activate();
  4075. }
  4076. void CSystemOptionsWindow::deactivate()
  4077. {
  4078. save->deactivate();
  4079. quitGame->deactivate();
  4080. backToMap->deactivate();
  4081. heroMoveSpeed->deactivate();
  4082. mapScrollSpeed->deactivate();
  4083. }
  4084. void CSystemOptionsWindow::show(SDL_Surface *to)
  4085. {
  4086. //evaluating to
  4087. if(!to)
  4088. to = screen;
  4089. SDL_BlitSurface(background, NULL, to, &pos);
  4090. save->show(to);
  4091. quitGame->show(to);
  4092. backToMap->show(to);
  4093. heroMoveSpeed->show(to);
  4094. mapScrollSpeed->show(to);
  4095. }
  4096. CTavernWindow::CTavernWindow(const CGHeroInstance *H1, const CGHeroInstance *H2, const std::string &gossip)
  4097. :h1(selected,0,72,299,H1),h2(selected,1,162,299,H2)
  4098. {
  4099. if(H1)
  4100. selected = 0;
  4101. else
  4102. selected = -1;
  4103. bg = BitmapHandler::loadBitmap("TPTAVERN.bmp");
  4104. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  4105. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  4106. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,GEOR16,tytulowy,bg);
  4107. printAtMiddle("2500",320,328,GEOR13,zwykly,bg);
  4108. printAtMiddle(CGI->generaltexth->jktexts[38],146,283,GEOR16,tytulowy,bg);
  4109. printAtMiddleWB(gossip,200,220,GEOR13,50,zwykly,bg);
  4110. pos.w = bg->w;
  4111. pos.h = bg->h;
  4112. pos.x = (screen->w-bg->w)/2;
  4113. pos.y = (screen->h-bg->h)/2;
  4114. bar = new CStatusBar(pos.x+8, pos.y+478, "APHLFTRT.bmp", 380);
  4115. h1.pos.x += pos.x;
  4116. h2.pos.x += pos.x;
  4117. h1.pos.y += pos.y;
  4118. h2.pos.y += pos.y;
  4119. cancel = new AdventureMapButton(CGI->generaltexth->tavernInfo[7],"",boost::bind(&CTavernWindow::close,this),pos.x+310,pos.y+428,"ICANCEL.DEF",SDLK_ESCAPE);
  4120. recruit = new AdventureMapButton("","",boost::bind(&CTavernWindow::recruitb,this),pos.x+272,pos.y+355,"TPTAV01.DEF",SDLK_RETURN);
  4121. thiefGuild = new AdventureMapButton(CGI->generaltexth->tavernInfo[5],"",0,pos.x+22,pos.y+428,"TPTAV02.DEF",SDLK_t);
  4122. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  4123. {
  4124. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  4125. recruit->block(2);
  4126. }
  4127. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  4128. {
  4129. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  4130. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  4131. recruit->block(2);
  4132. }
  4133. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  4134. {
  4135. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  4136. recruit->block(2);
  4137. }
  4138. else
  4139. {
  4140. if(H1)
  4141. {
  4142. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  4143. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",H1->name);
  4144. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",H1->type->heroClass->name);
  4145. }
  4146. else
  4147. recruit->block(1);
  4148. }
  4149. }
  4150. void CTavernWindow::recruitb()
  4151. {
  4152. const CGHeroInstance *toBuy = (selected ? h2 : h1).h;
  4153. close();
  4154. LOCPLINT->cb->recruitHero(LOCPLINT->castleInt->town,toBuy);
  4155. }
  4156. CTavernWindow::~CTavernWindow()
  4157. {
  4158. SDL_FreeSurface(bg);
  4159. delete cancel;
  4160. delete thiefGuild;
  4161. delete recruit;
  4162. delete bar;
  4163. }
  4164. void CTavernWindow::activate()
  4165. {
  4166. LOCPLINT->objsToBlit += this;
  4167. LOCPLINT->curint->subInt = this;
  4168. thiefGuild->activate();
  4169. cancel->activate();
  4170. if(h1.h)
  4171. h1.activate();
  4172. if(h2.h)
  4173. h2.activate();
  4174. recruit->activate();
  4175. LOCPLINT->statusbar = bar;
  4176. }
  4177. void CTavernWindow::deactivate()
  4178. {
  4179. LOCPLINT->objsToBlit -= this;
  4180. thiefGuild->deactivate();
  4181. cancel->deactivate();
  4182. if(h1.h)
  4183. h1.deactivate();
  4184. if(h2.h)
  4185. h2.deactivate();
  4186. recruit->deactivate();
  4187. }
  4188. void CTavernWindow::close()
  4189. {
  4190. LOCPLINT->curint->subInt = NULL;
  4191. deactivate();
  4192. delete this;
  4193. LOCPLINT->curint->activate();
  4194. }
  4195. void CTavernWindow::show(SDL_Surface * to)
  4196. {
  4197. blitAt(bg,pos.x,pos.y,screen);
  4198. if(h1.h)
  4199. h1.show();
  4200. if(h2.h)
  4201. h2.show();
  4202. thiefGuild->show();
  4203. cancel->show();
  4204. recruit->show();
  4205. bar->show();
  4206. if(selected >= 0)
  4207. {
  4208. HeroPortrait *sel = selected ? &h2 : &h1;
  4209. char descr[300];
  4210. int artifs = sel->h->artifWorn.size()+sel->h->artifacts.size();
  4211. for(int i=13; i<=17; i++) //war machines and spellbook doesn't count
  4212. if(vstd::contains(sel->h->artifWorn,i))
  4213. artifs--;
  4214. sprintf_s(descr,300,CGI->generaltexth->allTexts[215].c_str(),
  4215. sel->h->name.c_str(),sel->h->level,sel->h->type->heroClass->name.c_str(),artifs);
  4216. printAtMiddleWB(descr,pos.x+146,pos.y+389,GEOR13,40,zwykly,screen);
  4217. CSDL_Ext::drawBorder(screen,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  4218. }
  4219. }
  4220. void CTavernWindow::HeroPortrait::clickLeft(boost::logic::tribool down)
  4221. {
  4222. if(pressedL && !down)
  4223. as();
  4224. ClickableL::clickLeft(down);
  4225. }
  4226. void CTavernWindow::HeroPortrait::activate()
  4227. {
  4228. ClickableL::activate();
  4229. ClickableR::activate();
  4230. Hoverable::activate();
  4231. }
  4232. void CTavernWindow::HeroPortrait::deactivate()
  4233. {
  4234. ClickableL::deactivate();
  4235. ClickableR::deactivate();
  4236. Hoverable::deactivate();
  4237. }
  4238. void CTavernWindow::HeroPortrait::clickRight(boost::logic::tribool down)
  4239. {
  4240. if(down)
  4241. {
  4242. LOCPLINT->adventureInt->heroWindow->setHero(h);
  4243. LOCPLINT->objsToBlit += LOCPLINT->adventureInt->heroWindow;
  4244. LOCPLINT->curint->deactivate();
  4245. ClickableR::activate();
  4246. }
  4247. else if(vstd::contains(LOCPLINT->objsToBlit,LOCPLINT->adventureInt->heroWindow))
  4248. {
  4249. LOCPLINT->objsToBlit -= LOCPLINT->adventureInt->heroWindow;
  4250. ClickableR::deactivate();
  4251. LOCPLINT->castleInt->showAll(0,true);
  4252. LOCPLINT->curint->subInt->activate();
  4253. }
  4254. }
  4255. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  4256. :as(sel,id)
  4257. {
  4258. h = H;
  4259. pos.x = x;
  4260. pos.y = y;
  4261. pos.w = 58;
  4262. pos.h = 64;
  4263. if(H)
  4264. {
  4265. hoverName = CGI->generaltexth->tavernInfo[4];
  4266. boost::algorithm::replace_first(hoverName,"%s",H->name);
  4267. }
  4268. }
  4269. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  4270. {
  4271. blitAt(graphics->portraitLarge[h->subID],pos);
  4272. }
  4273. void CTavernWindow::HeroPortrait::hover( bool on )
  4274. {
  4275. Hoverable::hover(on);
  4276. if(on)
  4277. LOCPLINT->statusbar->print(hoverName);
  4278. else
  4279. LOCPLINT->statusbar->clear();
  4280. }
  4281. void CInGameConsole::activate()
  4282. {
  4283. KeyInterested::activate();
  4284. }
  4285. void CInGameConsole::deactivate()
  4286. {
  4287. KeyInterested::deactivate();
  4288. }
  4289. void CInGameConsole::show(SDL_Surface * to)
  4290. {
  4291. int number = 0;
  4292. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  4293. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  4294. {
  4295. SDL_Color green = {0,0xff,0,0};
  4296. Point leftBottomCorner(0, screen->h);
  4297. if(LOCPLINT->battleInt == LOCPLINT->curint)
  4298. {
  4299. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  4300. }
  4301. CSDL_Ext::printAtWR(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, TNRB16, green);
  4302. if(SDL_GetTicks() - it->second > defaultTimeout)
  4303. {
  4304. toDel.push_back(it);
  4305. }
  4306. }
  4307. for(int it=0; it<toDel.size(); ++it)
  4308. {
  4309. texts.erase(toDel[it]);
  4310. }
  4311. }
  4312. void CInGameConsole::print(const std::string &txt)
  4313. {
  4314. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  4315. if(texts.size() > maxDisplayedTexts)
  4316. {
  4317. texts.pop_front();
  4318. }
  4319. }
  4320. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  4321. {
  4322. if(key.type != SDL_KEYDOWN) return;
  4323. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  4324. switch(key.keysym.sym)
  4325. {
  4326. case SDLK_TAB:
  4327. {
  4328. if(captureAllKeys)
  4329. {
  4330. captureAllKeys = false;
  4331. endEnteringText(false);
  4332. }
  4333. else
  4334. {
  4335. captureAllKeys = true;
  4336. startEnteringText();
  4337. }
  4338. break;
  4339. }
  4340. case SDLK_RETURN: //enter key
  4341. {
  4342. if(enteredText.size() > 0 && captureAllKeys)
  4343. {
  4344. captureAllKeys = false;
  4345. endEnteringText(true);
  4346. }
  4347. break;
  4348. }
  4349. case SDLK_BACKSPACE:
  4350. {
  4351. if(enteredText.size() > 1)
  4352. {
  4353. enteredText.resize(enteredText.size()-1);
  4354. enteredText[enteredText.size()-1] = '_';
  4355. refreshEnteredText();
  4356. }
  4357. break;
  4358. }
  4359. case SDLK_UP: //up arrow
  4360. {
  4361. if(prevEntDisp == -1)
  4362. {
  4363. prevEntDisp = previouslyEntered.size() - 1;
  4364. enteredText = previouslyEntered[prevEntDisp] + "_";
  4365. refreshEnteredText();
  4366. }
  4367. else if( prevEntDisp > 0)
  4368. {
  4369. --prevEntDisp;
  4370. enteredText = previouslyEntered[prevEntDisp] + "_";
  4371. refreshEnteredText();
  4372. }
  4373. break;
  4374. }
  4375. case SDLK_DOWN: //down arrow
  4376. {
  4377. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  4378. {
  4379. ++prevEntDisp;
  4380. enteredText = previouslyEntered[prevEntDisp] + "_";
  4381. refreshEnteredText();
  4382. }
  4383. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  4384. {
  4385. prevEntDisp = -1;
  4386. enteredText = "_";
  4387. refreshEnteredText();
  4388. }
  4389. break;
  4390. }
  4391. default:
  4392. {
  4393. if(enteredText.size() > 0)
  4394. {
  4395. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  4396. {
  4397. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  4398. enteredText += "_";
  4399. refreshEnteredText();
  4400. }
  4401. }
  4402. break;
  4403. }
  4404. }
  4405. }
  4406. void CInGameConsole::startEnteringText()
  4407. {
  4408. enteredText = "_";
  4409. if(LOCPLINT->curint == LOCPLINT->adventureInt)
  4410. {
  4411. LOCPLINT->statusbar->print(enteredText);
  4412. }
  4413. else if(LOCPLINT->curint == LOCPLINT->battleInt)
  4414. {
  4415. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  4416. }
  4417. }
  4418. void CInGameConsole::endEnteringText(bool printEnteredText)
  4419. {
  4420. prevEntDisp = -1;
  4421. if(printEnteredText)
  4422. {
  4423. std::string txt = enteredText.substr(0, enteredText.size()-1);
  4424. LOCPLINT->cb->sendMessage(txt);
  4425. previouslyEntered.push_back(txt);
  4426. print(txt);
  4427. }
  4428. enteredText = "";
  4429. if(LOCPLINT->curint == LOCPLINT->adventureInt)
  4430. {
  4431. LOCPLINT->statusbar->clear();
  4432. }
  4433. else if(LOCPLINT->curint == LOCPLINT->battleInt)
  4434. {
  4435. LOCPLINT->battleInt->console->ingcAlter = "";
  4436. }
  4437. }
  4438. void CInGameConsole::refreshEnteredText()
  4439. {
  4440. if(LOCPLINT->curint == LOCPLINT->adventureInt)
  4441. {
  4442. LOCPLINT->statusbar->print(enteredText);
  4443. }
  4444. else if(LOCPLINT->curint == LOCPLINT->battleInt)
  4445. {
  4446. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  4447. }
  4448. }
  4449. CInGameConsole::CInGameConsole() : defaultTimeout(10000), maxDisplayedTexts(10), prevEntDisp(-1)
  4450. {
  4451. }