CPlayerInterface.cpp 132 KB

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