CPlayerInterface.cpp 133 KB

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