GUIClasses.cpp 178 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266
  1. #include "StdInc.h"
  2. #include "GUIClasses.h"
  3. #include "UIFramework/SDL_Extensions.h"
  4. #include "CAdvmapInterface.h"
  5. #include "BattleInterface/CBattleInterface.h"
  6. #include "BattleInterface/CBattleInterfaceClasses.h"
  7. #include "../CCallback.h"
  8. #include "CCastleInterface.h"
  9. #include "CCreatureWindow.h"
  10. #include "UIFramework/CCursorHandler.h"
  11. #include "CGameInfo.h"
  12. #include "CHeroWindow.h"
  13. #include "CMessage.h"
  14. #include "CConfigHandler.h"
  15. #include "CConfigHandler.h"
  16. #include "BattleInterface/CCreatureAnimation.h"
  17. #include "CPlayerInterface.h"
  18. #include "Graphics.h"
  19. #include "CAnimation.h"
  20. #include "../lib/CArtHandler.h"
  21. #include "../lib/CBuildingHandler.h"
  22. #include "../lib/CGeneralTextHandler.h"
  23. #include "../lib/CHeroHandler.h"
  24. #include "../lib/CLodHandler.h"
  25. #include "../lib/CObjectHandler.h"
  26. #include "../lib/CSpellHandler.h"
  27. #include "../lib/CTownHandler.h"
  28. #include "../lib/CondSh.h"
  29. #include "../lib/map.h"
  30. #include "mapHandler.h"
  31. #include "../lib/CStopWatch.h"
  32. #include "../lib/NetPacks.h"
  33. #include "CSpellWindow.h"
  34. #include "CHeroWindow.h"
  35. #include "CVideoHandler.h"
  36. #include "../lib/StartInfo.h"
  37. #include "CPreGame.h"
  38. #include "../lib/HeroBonus.h"
  39. #include "../lib/CCreatureHandler.h"
  40. #include "CMusicHandler.h"
  41. #include "../lib/BattleState.h"
  42. #include "../lib/CGameState.h"
  43. #include "../lib/GameConstants.h"
  44. #include "UIFramework/CGuiHandler.h"
  45. /*
  46. * GUIClasses.cpp, part of VCMI engine
  47. *
  48. * Authors: listed in file AUTHORS in main folder
  49. *
  50. * License: GNU General Public License v2.0 or later
  51. * Full text of license available in license.txt file, in main folder
  52. *
  53. */
  54. using namespace boost::assign;
  55. using namespace CSDL_Ext;
  56. extern std::queue<SDL_Event*> events;
  57. extern boost::mutex eventsM;
  58. std::list<CFocusable*> CFocusable::focusables;
  59. CFocusable * CFocusable::inputWithFocus;
  60. #undef min
  61. #undef max
  62. void CGarrisonSlot::hover (bool on)
  63. {
  64. ////Hoverable::hover(on);
  65. if(on)
  66. {
  67. std::string temp;
  68. if(creature)
  69. {
  70. if(owner->highlighted)
  71. {
  72. if(owner->highlighted == this)
  73. {
  74. temp = CGI->generaltexth->tcommands[4]; //View %s
  75. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  76. }
  77. else if (owner->highlighted->creature == creature)
  78. {
  79. temp = CGI->generaltexth->tcommands[2]; //Combine %s armies
  80. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  81. }
  82. else if (owner->highlighted->creature)
  83. {
  84. temp = CGI->generaltexth->tcommands[7]; //Exchange %s with %s
  85. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  86. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  87. }
  88. else
  89. {
  90. tlog2 << "Warning - shouldn't be - highlighted void slot "<<owner->highlighted<<std::endl;
  91. tlog2 << "Highlighted set to NULL"<<std::endl;
  92. owner->highlighted = NULL;
  93. }
  94. }
  95. else
  96. {
  97. if(upg)
  98. {
  99. temp = CGI->generaltexth->tcommands[32]; //Select %s (visiting)
  100. }
  101. else if(owner->armedObjs[0] && owner->armedObjs[0]->ID == GameConstants::TOWNI_TYPE)
  102. {
  103. temp = CGI->generaltexth->tcommands[12]; //Select %s (in garrison)
  104. }
  105. else
  106. {
  107. temp = CGI->generaltexth->allTexts[481]; //Select %s
  108. }
  109. boost::algorithm::replace_first(temp,"%s",creature->nameSing);
  110. };
  111. }
  112. else
  113. {
  114. if(owner->highlighted)
  115. {
  116. const CArmedInstance *highl = owner->highlighted->getObj();
  117. if( highl->needsLastStack() //we are moving stack from hero's
  118. && highl->stacksCount() == 1 //it's only stack
  119. && owner->highlighted->upg != upg //we're moving it to the other garrison
  120. )
  121. {
  122. temp = CGI->generaltexth->tcommands[5]; //Cannot move last army to garrison
  123. }
  124. else
  125. {
  126. temp = CGI->generaltexth->tcommands[6]; //Move %s
  127. boost::algorithm::replace_first(temp,"%s",owner->highlighted->creature->nameSing);
  128. }
  129. }
  130. else
  131. {
  132. temp = CGI->generaltexth->tcommands[11]; //Empty
  133. }
  134. }
  135. GH.statusbar->print(temp);
  136. }
  137. else
  138. {
  139. GH.statusbar->clear();
  140. }
  141. }
  142. const CArmedInstance * CGarrisonSlot::getObj()
  143. {
  144. return (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]);
  145. }
  146. bool CGarrisonSlot::our()
  147. {
  148. return upg?(owner->owned[1]):(owner->owned[0]);
  149. }
  150. void CGarrisonSlot::clickRight(tribool down, bool previousState)
  151. {
  152. if(down && creature)
  153. {
  154. GH.pushInt(createCreWindow(myStack, 2));
  155. }
  156. }
  157. void CGarrisonSlot::clickLeft(tribool down, bool previousState)
  158. {
  159. if(down)
  160. {
  161. bool refr = false;
  162. if(owner->highlighted)
  163. {
  164. if(owner->highlighted == this) //view info
  165. {
  166. UpgradeInfo pom;
  167. LOCPLINT->cb->getUpgradeInfo(getObj(), ID, pom);
  168. bool canUpgrade = getObj()->tempOwner == LOCPLINT->playerID && pom.oldID>=0; //upgrade is possible
  169. bool canDismiss = getObj()->tempOwner == LOCPLINT->playerID && (getObj()->stacksCount()>1 || !getObj()->needsLastStack());
  170. boost::function<void()> upgr = NULL;
  171. boost::function<void()> dism = NULL;
  172. if (canUpgrade) upgr = boost::bind(&CCallback::upgradeCreature, LOCPLINT->cb, getObj(), ID, pom.newID[0]);
  173. if (canDismiss) dism = boost::bind(&CCallback::dismissCreature, LOCPLINT->cb, getObj(), ID);
  174. owner->highlighted = NULL;
  175. owner->splitting = false;
  176. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  177. owner->splitButtons[i]->block(true);
  178. redraw();
  179. refr = true;
  180. CIntObject *creWindow = createCreWindow(myStack, 3, upgr, dism, &pom);
  181. GH.pushInt(creWindow);
  182. }
  183. else
  184. {
  185. // Only allow certain moves if troops aren't removable or not ours.
  186. if ( ( owner->highlighted->our()//our creature is selected
  187. || owner->highlighted->creature == creature )//or we are rebalancing army
  188. && ( owner->removableUnits
  189. || (upg == 0 && ( owner->highlighted->upg == 1 && !creature ) )
  190. || (upg == 1 && owner->highlighted->upg == 1 ) ) )
  191. {
  192. //we want to split
  193. if((owner->splitting || LOCPLINT->shiftPressed())
  194. && (!creature
  195. || (creature == owner->highlighted->creature)))
  196. {
  197. owner->p2 = ID; //store the second stack pos
  198. owner->pb = upg;//store the second stack owner (up or down army)
  199. owner->splitting = false;
  200. int totalAmount = owner->highlighted->count;
  201. if(creature)
  202. totalAmount += count;
  203. int last = -1;
  204. if(upg != owner->highlighted->upg) //not splitting within same army
  205. {
  206. if(owner->highlighted->getObj()->stacksCount() == 1 //we're splitting away the last stack
  207. && owner->highlighted->getObj()->needsLastStack() )
  208. {
  209. last = 0;
  210. }
  211. if(getObj()->stacksCount() == 1 //destination army can't be emptied, unless we're rebalancing two stacks of same creature
  212. && owner->highlighted->creature == creature
  213. && getObj()->needsLastStack() )
  214. {
  215. last += 2;
  216. }
  217. }
  218. CSplitWindow * spw = new CSplitWindow(owner->highlighted->creature->idNumber, totalAmount, owner, last, count);
  219. GH.pushInt(spw);
  220. refr = true;
  221. }
  222. else if(creature != owner->highlighted->creature) //swap
  223. {
  224. LOCPLINT->cb->swapCreatures(
  225. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  226. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  227. ID,owner->highlighted->ID);
  228. }
  229. else //merge
  230. {
  231. LOCPLINT->cb->mergeStacks(
  232. (!owner->highlighted->upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  233. (!upg)?(owner->armedObjs[0]):(owner->armedObjs[1]),
  234. owner->highlighted->ID,ID);
  235. }
  236. }
  237. else // Highlight
  238. {
  239. if(creature)
  240. owner->highlighted = this;
  241. redraw();
  242. refr = true;
  243. }
  244. }
  245. }
  246. else //highlight
  247. {
  248. if(creature)
  249. {
  250. owner->highlighted = this;
  251. for(size_t i = 0; i<owner->splitButtons.size(); i++)
  252. owner->splitButtons[i]->block(false);
  253. }
  254. redraw();
  255. refr = true;
  256. }
  257. if(refr) {hover(false); hover(true); } //to refresh statusbar
  258. }
  259. }
  260. void CGarrisonSlot::activate()
  261. {
  262. if(!active) active=true;
  263. else return;
  264. activateLClick();
  265. activateRClick();
  266. activateHover();
  267. }
  268. void CGarrisonSlot::deactivate()
  269. {
  270. if(active) active=false;
  271. else return;
  272. deactivateLClick();
  273. deactivateRClick();
  274. deactivateHover();
  275. }
  276. CGarrisonSlot::CGarrisonSlot(CGarrisonInt *Owner, int x, int y, int IID, int Upg, const CStackInstance * Creature)
  277. {
  278. //assert(Creature == CGI->creh->creatures[Creature->idNumber]);
  279. active = false;
  280. upg = Upg;
  281. ID = IID;
  282. myStack = Creature;
  283. creature = Creature ? Creature->type : NULL;
  284. count = Creature ? Creature->count : 0;
  285. pos.x += x;
  286. pos.y += y;
  287. if(Owner->smallIcons)
  288. {
  289. pos.w = 32;
  290. pos.h = 32;
  291. }
  292. else
  293. {
  294. pos.w = 58;
  295. pos.h = 64;
  296. }
  297. owner = Owner;
  298. }
  299. CGarrisonSlot::~CGarrisonSlot()
  300. {
  301. if(active)
  302. deactivate();
  303. }
  304. void CGarrisonSlot::showAll(SDL_Surface * to)
  305. {
  306. std::map<int,SDL_Surface*> &imgs = (owner->smallIcons ? graphics->smallImgs : graphics->bigImgs);
  307. if(creature)
  308. {
  309. char buf[15];
  310. SDL_itoa(count,buf,10);
  311. blitAt(imgs[creature->idNumber],pos,to);
  312. printTo(buf, pos.x+pos.w, pos.y+pos.h+1, owner->smallIcons ? FONT_TINY : FONT_MEDIUM, Colors::Cornsilk, to);
  313. if((owner->highlighted==this)
  314. || (owner->splitting && owner->highlighted->creature == creature))
  315. {
  316. blitAt(imgs[-1],pos,to);
  317. }
  318. }
  319. else//empty slot
  320. {
  321. if(owner->splitting && owner->highlighted->our())
  322. blitAt(imgs[-1],pos,to);
  323. }
  324. }
  325. CGarrisonInt::~CGarrisonInt()
  326. {/*
  327. for(size_t i = 0; i<splitButtons.size(); i++)
  328. delete splitButtons[i];*/
  329. }
  330. void CGarrisonInt::addSplitBtn(CAdventureMapButton * button)
  331. {
  332. addChild(button);
  333. button->recActions = defActions;
  334. splitButtons.push_back(button);
  335. }
  336. void CGarrisonInt::createSet(std::vector<CGarrisonSlot*> &ret, const CCreatureSet * set, int posX, int posY, int distance, int Upg )
  337. {
  338. ret.resize(7);
  339. for(TSlots::const_iterator i=set->Slots().begin(); i!=set->Slots().end(); i++)
  340. {
  341. ret[i->first] = new CGarrisonSlot(this, posX + (i->first*distance), posY, i->first, Upg, i->second);
  342. }
  343. for(int i=0; i<ret.size(); i++)
  344. if(!ret[i])
  345. ret[i] = new CGarrisonSlot(this, posX + (i*distance), posY,i,Upg,NULL);
  346. if (twoRows)
  347. for (int i=4; i<ret.size(); i++)
  348. {
  349. ret[i]->pos.x -= 126;
  350. ret[i]->pos.y += 37;
  351. };
  352. }
  353. void CGarrisonInt::createSlots()
  354. {
  355. OBJ_CONSTRUCTION_CAPTURING_ALL;
  356. int width = smallIcons? 32 : 58;
  357. if(armedObjs[0])
  358. createSet(slotsUp, armedObjs[0], 0, 0, width+interx, 0);
  359. if(armedObjs[1])
  360. createSet (slotsDown, armedObjs[1], garOffset.x, garOffset.y, width+interx, 1);
  361. }
  362. void CGarrisonInt::deleteSlots()
  363. {
  364. for (int i=0; i<slotsUp.size(); i++)
  365. delChildNUll(slotsUp[i]);
  366. for (int i=0; i<slotsDown.size(); i++)
  367. delChildNUll(slotsDown[i]);
  368. }
  369. void CGarrisonInt::recreateSlots()
  370. {
  371. splitting = false;
  372. highlighted = NULL;
  373. for(size_t i = 0; i<splitButtons.size(); i++)
  374. splitButtons[i]->block(true);
  375. bool wasActive = active;
  376. if(active)
  377. {
  378. deactivate();
  379. }
  380. deleteSlots();
  381. createSlots();
  382. if(wasActive)
  383. {
  384. activate();
  385. showAll(screen2);
  386. }
  387. }
  388. void CGarrisonInt::splitClick()
  389. {
  390. if(!highlighted)
  391. return;
  392. splitting = !splitting;
  393. redraw();
  394. }
  395. void CGarrisonInt::splitStacks(int am2)
  396. {
  397. LOCPLINT->cb->splitStack(armedObjs[highlighted->upg], armedObjs[pb], highlighted->ID, p2, am2);
  398. }
  399. CGarrisonInt::CGarrisonInt(int x, int y, int inx, const Point &garsOffset,
  400. SDL_Surface *pomsur, const Point& SurOffset,
  401. const CArmedInstance *s1, const CArmedInstance *s2,
  402. bool _removableUnits, bool smallImgs, bool _twoRows )
  403. : interx(inx), garOffset(garsOffset), highlighted(NULL), splitting(false),
  404. smallIcons(smallImgs), removableUnits (_removableUnits), twoRows(_twoRows)
  405. {
  406. setArmy(s1, false);
  407. setArmy(s2, true);
  408. pos.x += x;
  409. pos.y += y;
  410. createSlots();
  411. }
  412. void CGarrisonInt::activate()
  413. {
  414. for(size_t i = 0; i<splitButtons.size(); i++)
  415. if( (splitButtons[i]->isBlocked()) != !highlighted)
  416. splitButtons[i]->block(!highlighted);
  417. CIntObject::activate();
  418. }
  419. void CGarrisonInt::setArmy(const CArmedInstance *army, bool bottomGarrison)
  420. {
  421. owned[bottomGarrison] = army ? (army->tempOwner == LOCPLINT->playerID || army->tempOwner == 254) : false; //254 - neutral objects (pandora, banks)
  422. armedObjs[bottomGarrison] = army;
  423. }
  424. CInfoWindow::CInfoWindow(std::string Text, int player, const TCompsInfo &comps, const TButtonsInfo &Buttons, bool delComps)
  425. {
  426. OBJ_CONSTRUCTION_CAPTURING_ALL;
  427. type |= BLOCK_ADV_HOTKEYS;
  428. ID = -1;
  429. for(int i=0;i<Buttons.size();i++)
  430. {
  431. CAdventureMapButton *button = new CAdventureMapButton("","",boost::bind(&CInfoWindow::close,this),0,0,Buttons[i].first);
  432. button->borderColor = Colors::MetallicGold;
  433. button->borderEnabled = true;
  434. button->callback.add(Buttons[i].second); //each button will close the window apart from call-defined actions
  435. buttons.push_back(button);
  436. }
  437. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::Cornsilk);
  438. if(!text->slider)
  439. {
  440. text->pos.w = text->maxW;
  441. text->pos.h = text->maxH;
  442. }
  443. text->redrawParentOnScrolling = true;
  444. if(buttons.size())
  445. {
  446. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  447. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  448. }
  449. for(int i=0;i<comps.size();i++)
  450. {
  451. comps[i]->recActions = 0xff;
  452. addChild(comps[i]);
  453. comps[i]->recActions &= ~(SHOWALL | UPDATE);
  454. components.push_back(comps[i]);
  455. }
  456. setDelComps(delComps);
  457. CMessage::drawIWindow(this,Text,player);
  458. }
  459. CInfoWindow::CInfoWindow()
  460. {
  461. ID = -1;
  462. setDelComps(false);
  463. text = NULL;
  464. }
  465. void CInfoWindow::close()
  466. {
  467. GH.popIntTotally(this);
  468. if(LOCPLINT)
  469. LOCPLINT->showingDialog->setn(false);
  470. }
  471. void CInfoWindow::show(SDL_Surface * to)
  472. {
  473. CIntObject::show(to);
  474. }
  475. CInfoWindow::~CInfoWindow()
  476. {
  477. if(!delComps)
  478. {
  479. for (int i=0;i<components.size();i++)
  480. removeChild(components[i]);
  481. }
  482. }
  483. void CInfoWindow::showAll(SDL_Surface * to)
  484. {
  485. CSimpleWindow::show(to);
  486. CIntObject::showAll(to);
  487. }
  488. void CInfoWindow::showYesNoDialog(const std::string & text, const std::vector<CComponent*> *components, const CFunctionList<void( ) > &onYes, const CFunctionList<void()> &onNo, bool DelComps, int player)
  489. {
  490. assert(!LOCPLINT || LOCPLINT->showingDialog->get());
  491. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  492. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  493. pom.push_back(std::pair<std::string,CFunctionList<void()> >("ICANCEL.DEF",0));
  494. CInfoWindow * temp = new CInfoWindow(text, player, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  495. for(int i=0;i<onYes.funcs.size();i++)
  496. temp->buttons[0]->callback += onYes.funcs[i];
  497. for(int i=0;i<onNo.funcs.size();i++)
  498. temp->buttons[1]->callback += onNo.funcs[i];
  499. GH.pushInt(temp);
  500. }
  501. CInfoWindow * CInfoWindow::create(const std::string &text, int playerID /*= 1*/, const std::vector<CComponent*> *components /*= NULL*/, bool DelComps)
  502. {
  503. std::vector<std::pair<std::string,CFunctionList<void()> > > pom;
  504. pom.push_back(std::pair<std::string,CFunctionList<void()> >("IOKAY.DEF",0));
  505. CInfoWindow * ret = new CInfoWindow(text, playerID, components ? *components : std::vector<CComponent*>(), pom, DelComps);
  506. return ret;
  507. }
  508. void CInfoWindow::setDelComps(bool DelComps)
  509. {
  510. delComps = DelComps;
  511. BOOST_FOREACH(CComponent *comp, components)
  512. {
  513. if(delComps)
  514. comp->recActions |= DISPOSE;
  515. else
  516. comp->recActions &= ~DISPOSE;
  517. }
  518. }
  519. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, int x, int y, bool Free)
  520. :free(Free),bitmap(Bitmap)
  521. {
  522. init(x, y);
  523. }
  524. CInfoPopup::CInfoPopup(SDL_Surface * Bitmap, const Point &p, EAlignment alignment, bool Free/*=false*/)
  525. : free(Free),bitmap(Bitmap)
  526. {
  527. switch(alignment)
  528. {
  529. case BOTTOMRIGHT:
  530. init(p.x - Bitmap->w, p.y - Bitmap->h);
  531. break;
  532. case CENTER:
  533. init(p.x - Bitmap->w/2, p.y - Bitmap->h/2);
  534. break;
  535. case TOPLEFT:
  536. init(p.x, p.y);
  537. break;
  538. default:
  539. assert(0); //not implemented
  540. }
  541. }
  542. CInfoPopup::CInfoPopup(SDL_Surface *Bitmap, bool Free)
  543. {
  544. CCS->curh->hide();
  545. free=Free;
  546. bitmap=Bitmap;
  547. if(bitmap)
  548. {
  549. pos.x = screen->w/2 - bitmap->w/2;
  550. pos.y = screen->h/2 - bitmap->h/2;
  551. pos.h = bitmap->h;
  552. pos.w = bitmap->w;
  553. }
  554. }
  555. void CInfoPopup::close()
  556. {
  557. if(free)
  558. SDL_FreeSurface(bitmap);
  559. GH.popIntTotally(this);
  560. }
  561. void CInfoPopup::show(SDL_Surface * to)
  562. {
  563. blitAt(bitmap,pos.x,pos.y,to);
  564. }
  565. CInfoPopup::~CInfoPopup()
  566. {
  567. CCS->curh->show();
  568. }
  569. void CInfoPopup::init(int x, int y)
  570. {
  571. CCS->curh->hide();
  572. pos.x = x;
  573. pos.y = y;
  574. pos.h = bitmap->h;
  575. pos.w = bitmap->w;
  576. // Put the window back on screen if necessary
  577. vstd::amax(pos.x, 0);
  578. vstd::amax(pos.y, 0);
  579. vstd::amin(pos.x, conf.cc.resx - bitmap->w);
  580. vstd::amin(pos.y, conf.cc.resy - bitmap->h);
  581. }
  582. void CComponent::init(Etype Type, int Subtype, int Val)
  583. {
  584. std::ostringstream oss;
  585. switch (Type)
  586. {
  587. case artifact:
  588. description = CGI->arth->artifacts[Subtype]->Description();
  589. subtitle = CGI->arth->artifacts[Subtype]->Name();
  590. break;
  591. case primskill:
  592. oss << std::showpos << Val << " ";
  593. if(Subtype < 4)
  594. {
  595. description = CGI->generaltexth->arraytxt[2+Subtype];
  596. oss << CGI->generaltexth->primarySkillNames[Subtype];
  597. }
  598. else if(Subtype == 5) //spell points
  599. {
  600. description = CGI->generaltexth->allTexts[149];
  601. oss << CGI->generaltexth->allTexts[387];
  602. }
  603. else
  604. {
  605. tlog1 << "Wrong subtype=" << Subtype << std::endl;
  606. }
  607. subtitle = oss.str();
  608. break;
  609. case building:
  610. description = CGI->buildh->buildings[Subtype][Val]->Description();
  611. subtitle = CGI->buildh->buildings[Subtype][Val]->Name();
  612. break;
  613. case secskill44: case secskill:
  614. subtitle += CGI->generaltexth->levels[Val-1] + " " + CGI->generaltexth->skillName[Subtype];
  615. description = CGI->generaltexth->skillInfoTexts[Subtype][Val-1];
  616. break;
  617. case morale:
  618. description = CGI->generaltexth->heroscrn[ 4 - (val>0) + (val<0)];
  619. break;
  620. case luck:
  621. description = CGI->generaltexth->heroscrn[ 7 - (val>0) + (val<0)];
  622. break;
  623. case resource:
  624. description = CGI->generaltexth->allTexts[242];
  625. oss << Val;
  626. subtitle = oss.str();
  627. break;
  628. case spell:
  629. description = CGI->spellh->spells[Subtype]->descriptions[Val];
  630. subtitle = CGI->spellh->spells[Subtype]->name;
  631. break;
  632. case creature:
  633. subtitle = (Val? boost::lexical_cast<std::string>(Val) + " " : "") + CGI->creh->creatures[Subtype]->*(Val != 1 ? &CCreature::namePl : &CCreature::nameSing);
  634. break;
  635. case experience:
  636. description = CGI->generaltexth->allTexts[241];
  637. oss << Val ;
  638. if(Subtype && Val==1)
  639. {
  640. subtitle = CGI->generaltexth->allTexts[442];
  641. }
  642. else
  643. {
  644. subtitle = oss.str();
  645. }
  646. break;
  647. case hero:
  648. subtitle = description = CGI->heroh->heroes[Subtype]->name;
  649. break;
  650. case flag:
  651. subtitle = CGI->generaltexth->capColors[Subtype];
  652. break;
  653. }
  654. img = NULL;
  655. free = false;
  656. type = Type;
  657. subtype = Subtype;
  658. val = Val;
  659. SDL_Surface * temp = this->getImg();
  660. if(!temp)
  661. {
  662. tlog1 << "Error: cannot find graphic for component with id=" << type << " subid=" << subtype << " val=" << val << std::endl;
  663. return;
  664. }
  665. pos.w = temp->w;
  666. pos.h = temp->h;
  667. }
  668. CComponent::CComponent(Etype Type, int Subtype, int Val, SDL_Surface *sur, bool freeSur):img(sur),free(freeSur)
  669. {
  670. init(Type,Subtype,Val);
  671. }
  672. CComponent::CComponent(const Component &c)
  673. {
  674. if(c.id==5)
  675. init(experience,c.subtype,c.val);
  676. else if(c.id == Component::SPELL)
  677. init(spell,c.subtype,c.val);
  678. else
  679. init((Etype)c.id,c.subtype,c.val);
  680. if(c.id==2 && c.when==-1)
  681. subtitle += CGI->generaltexth->allTexts[3].substr(2,CGI->generaltexth->allTexts[3].length()-2);
  682. }
  683. CComponent::CComponent()
  684. {
  685. img = NULL;
  686. }
  687. CComponent::~CComponent()
  688. {
  689. if (free && img)
  690. SDL_FreeSurface(img);
  691. }
  692. SDL_Surface * CComponent::setSurface(std:: string defname, int imagepos)
  693. {
  694. if (img)
  695. tlog1<<"CComponent::setSurface: Warning - surface is already set!\n";
  696. CDefEssential * def = CDefHandler::giveDefEss(defname);
  697. free = true;
  698. img = def->ourImages[imagepos].bitmap;
  699. img->refcount++;//to preserve surface whed def is deleted
  700. delete def;
  701. return img;
  702. }
  703. void CComponent::show(SDL_Surface * to)
  704. {
  705. blitAt(getImg(),pos.x,pos.y,to);
  706. }
  707. SDL_Surface * CComponent::getImg()
  708. {
  709. if (img)
  710. return img;
  711. switch (type)
  712. {
  713. case artifact:
  714. return graphics->artDefs->ourImages[subtype].bitmap;
  715. case primskill:
  716. return graphics->pskillsb->ourImages[subtype].bitmap;
  717. case secskill44:
  718. return graphics->abils44->ourImages[subtype*3 + 3 + val - 1].bitmap;
  719. case secskill:
  720. return graphics->abils82->ourImages[subtype*3 + 3 + val - 1].bitmap;
  721. case resource:
  722. return graphics->resources->ourImages[subtype].bitmap;
  723. case experience:
  724. return graphics->pskillsb->ourImages[4].bitmap;
  725. case morale:
  726. return graphics->morale82->ourImages[val+3].bitmap;
  727. case luck:
  728. return graphics->luck82->ourImages[val+3].bitmap;
  729. case spell:
  730. return graphics->spellscr->ourImages[subtype].bitmap;
  731. case building:
  732. return setSurface(graphics->buildingPics[subtype],val);
  733. case creature:
  734. return graphics->bigImgs[subtype];
  735. case hero:
  736. return graphics->portraitLarge[subtype];
  737. case flag:
  738. return graphics->flags->ourImages[subtype].bitmap;
  739. }
  740. return NULL;
  741. }
  742. void CComponent::clickRight(tribool down, bool previousState)
  743. {
  744. if(description.size())
  745. adventureInt->handleRightClick(description,down);
  746. }
  747. void CComponent::activate()
  748. {
  749. activateRClick();
  750. }
  751. void CComponent::deactivate()
  752. {
  753. deactivateRClick();
  754. }
  755. void CSelectableComponent::clickLeft(tribool down, bool previousState)
  756. {
  757. if (down)
  758. {
  759. if(onSelect)
  760. onSelect();
  761. }
  762. }
  763. void CSelectableComponent::init()
  764. {
  765. selected = false;
  766. }
  767. CSelectableComponent::CSelectableComponent(const Component &c, boost::function<void()> OnSelect)
  768. :CComponent(c),onSelect(OnSelect)
  769. {
  770. init();
  771. }
  772. CSelectableComponent::CSelectableComponent(Etype Type, int Sub, int Val, boost::function<void()> OnSelect)
  773. :CComponent(Type,Sub,Val),onSelect(OnSelect)
  774. {
  775. init();
  776. }
  777. CSelectableComponent::~CSelectableComponent()
  778. {
  779. }
  780. void CSelectableComponent::activate()
  781. {
  782. activateKeys();
  783. CComponent::activate();
  784. activateLClick();
  785. }
  786. void CSelectableComponent::deactivate()
  787. {
  788. deactivateKeys();
  789. CComponent::deactivate();
  790. deactivateLClick();
  791. }
  792. void CSelectableComponent::select(bool on)
  793. {
  794. if(on != selected)
  795. {
  796. selected = on;
  797. return;
  798. }
  799. else
  800. {
  801. return;
  802. }
  803. }
  804. void CSelectableComponent::show(SDL_Surface * to)
  805. {
  806. blitAt(getImg(),pos.x,pos.y,to);
  807. if(selected)
  808. {
  809. CSDL_Ext::drawBorder(to, Rect::around(Rect(pos.x, pos.y, getImg()->w, getImg()->h)), int3(239,215,123));
  810. }
  811. printAtMiddleWB(subtitle,pos.x+pos.w/2,pos.y+pos.h+25,FONT_SMALL,12,Colors::Cornsilk,to);
  812. }
  813. void CSelWindow::selectionChange(unsigned to)
  814. {
  815. for (unsigned i=0;i<components.size();i++)
  816. {
  817. CSelectableComponent * pom = dynamic_cast<CSelectableComponent*>(components[i]);
  818. if (!pom)
  819. continue;
  820. pom->select(i==to);
  821. blitAt(pom->getImg(),pom->pos.x-pos.x,pom->pos.y-pos.y,bitmap);
  822. }
  823. }
  824. CSelWindow::CSelWindow(const std::string &Text, int player, int charperline, const std::vector<CSelectableComponent*> &comps, const std::vector<std::pair<std::string,CFunctionList<void()> > > &Buttons, int askID)
  825. {
  826. OBJ_CONSTRUCTION_CAPTURING_ALL;
  827. ID = askID;
  828. for(int i=0;i<Buttons.size();i++)
  829. {
  830. buttons.push_back(new CAdventureMapButton("","",Buttons[i].second,0,0,Buttons[i].first));
  831. if(!i && askID >= 0)
  832. buttons.back()->callback += boost::bind(&CSelWindow::madeChoice,this);
  833. buttons[i]->callback += boost::bind(&CInfoWindow::close,this); //each button will close the window apart from call-defined actions
  834. }
  835. text = new CTextBox(Text, Rect(0, 0, 250, 100), 0, FONT_MEDIUM, CENTER, Colors::Cornsilk);
  836. text->redrawParentOnScrolling = true;
  837. buttons.front()->assignedKeys.insert(SDLK_RETURN); //first button - reacts on enter
  838. buttons.back()->assignedKeys.insert(SDLK_ESCAPE); //last button - reacts on escape
  839. if(buttons.size() > 1 && askID >= 0) //cancel button functionality
  840. buttons.back()->callback += boost::bind(&CCallback::selectionMade,LOCPLINT->cb,0,askID);
  841. for(int i=0;i<comps.size();i++)
  842. {
  843. comps[i]->recActions = 255;
  844. addChild(comps[i]);
  845. components.push_back(comps[i]);
  846. comps[i]->onSelect = boost::bind(&CSelWindow::selectionChange,this,i);
  847. if(i<9)
  848. comps[i]->assignedKeys.insert(SDLK_1+i);
  849. }
  850. CMessage::drawIWindow(this, Text, player);
  851. BOOST_FOREACH(CComponent *c, components)
  852. c->subtitle = "";//workaround - erase subtitles since they were hard-blitted by function drawing window
  853. }
  854. void CSelWindow::madeChoice()
  855. {
  856. if(ID < 0)
  857. return;
  858. int ret = -1;
  859. for (int i=0;i<components.size();i++)
  860. {
  861. if(dynamic_cast<CSelectableComponent*>(components[i])->selected)
  862. {
  863. ret = i;
  864. }
  865. }
  866. LOCPLINT->cb->selectionMade(ret+1,ID);
  867. }
  868. CHeroList::CHeroList(int Size)
  869. :CList(Size)
  870. {
  871. arrup = CDefHandler::giveDef(conf.go()->ac.hlistAU);
  872. arrdo = CDefHandler::giveDef(conf.go()->ac.hlistAD);
  873. mobile = CDefHandler::giveDef(conf.go()->ac.hlistMB);
  874. mana = CDefHandler::giveDef(conf.go()->ac.hlistMN);
  875. empty = BitmapHandler::loadBitmap("HPSXXX.bmp");
  876. selection = BitmapHandler::loadBitmap("HPSYYY.bmp");
  877. SDL_SetColorKey(selection,SDL_SRCCOLORKEY,SDL_MapRGB(selection->format,0,255,255));
  878. pos = genRect(32*SIZE+arrup->height+arrdo->height, std::max(arrup->width,arrdo->width), conf.go()->ac.hlistX, conf.go()->ac.hlistY);
  879. arrupp = genRect(arrup->height, arrup->width, pos.x, pos.y);
  880. arrdop = genRect(arrdo->height, arrdo->width, pos.x, pos.y+32*SIZE+arrup->height);
  881. //32px per hero
  882. posmobx = pos.x+1;
  883. posmoby = pos.y+arrup->height+1;
  884. posporx = pos.x+mobile->width+2;
  885. pospory = pos.y+arrup->height;
  886. posmanx = pos.x+1+50+mobile->width;
  887. posmany = pos.y+arrup->height+1;
  888. from = 0;
  889. selected = -1;
  890. pressed = indeterminate;
  891. }
  892. void CHeroList::init()
  893. {
  894. int w = pos.w+1, h = pos.h+4;
  895. bg = CSDL_Ext::newSurface(w,h,screen);
  896. Rect srcRect = genRect(w, h, pos.x, pos.y);
  897. Rect dstRect = genRect(w, h, 0, 0);
  898. CSDL_Ext::blitSurface(adventureInt->bg, &srcRect, bg, &dstRect);
  899. }
  900. void CHeroList::genList()
  901. {
  902. //int howMany = LOCPLINT->cb->howManyHeroes();
  903. //for (int i=0;i<howMany;i++)
  904. //{
  905. // const CGHeroInstance * h = LOCPLINT->cb->getHeroInfo(i,0);
  906. // if(!h->inTownGarrison)
  907. // items.push_back(std::pair<const CGHeroInstance *,CPath *>(h,NULL));
  908. //}
  909. }
  910. void CHeroList::select(int which)
  911. {
  912. if (which<0)
  913. {
  914. selected = which;
  915. adventureInt->selection = NULL;
  916. adventureInt->terrain.currentPath = NULL;
  917. draw(screen);
  918. adventureInt->infoBar.showAll(screen);
  919. }
  920. if (which>=LOCPLINT->wanderingHeroes.size())
  921. return;
  922. selected = which;
  923. adventureInt->select(LOCPLINT->wanderingHeroes[which]);
  924. fixPos();
  925. draw(screen);
  926. }
  927. void CHeroList::clickLeft(tribool down, bool previousState)
  928. {
  929. if (down)
  930. {
  931. /***************************ARROWS*****************************************/
  932. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  933. {
  934. if(from>0)
  935. {
  936. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y);
  937. pressed = true;
  938. }
  939. return;
  940. }
  941. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  942. {
  943. if(LOCPLINT->wanderingHeroes.size()-from>SIZE)
  944. {
  945. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y);
  946. pressed = false;
  947. }
  948. return;
  949. }
  950. /***************************HEROES*****************************************/
  951. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  952. hx-=pos.x;
  953. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  954. int ny = hy/32;
  955. if (ny>=SIZE || ny<0)
  956. return;
  957. if ( (ny+from)==selected && (adventureInt->selection->ID == GameConstants::HEROI_TYPE))
  958. LOCPLINT->openHeroWindow(LOCPLINT->wanderingHeroes[selected]);//print hero screen
  959. select(ny+from);
  960. }
  961. else
  962. {
  963. if (indeterminate(pressed))
  964. return;
  965. if (pressed) //up
  966. {
  967. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y);
  968. pressed = indeterminate;
  969. if (!down)
  970. {
  971. from--;
  972. if (from<0)
  973. from=0;
  974. draw(screen);
  975. }
  976. }
  977. else if (!pressed) //down
  978. {
  979. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y);
  980. pressed = indeterminate;
  981. if (!down)
  982. {
  983. from++;
  984. //if (from<items.size()-5)
  985. // from=items.size()-5;
  986. draw(screen);
  987. }
  988. }
  989. else
  990. throw 0;
  991. }
  992. }
  993. void CHeroList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  994. {
  995. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  996. {
  997. if (from>0)
  998. adventureInt->statusbar.print(CGI->generaltexth->zelp[303].first);
  999. else
  1000. adventureInt->statusbar.clear();
  1001. return;
  1002. }
  1003. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1004. {
  1005. if ((LOCPLINT->wanderingHeroes.size()-from) > SIZE)
  1006. adventureInt->statusbar.print(CGI->generaltexth->zelp[304].first);
  1007. else
  1008. adventureInt->statusbar.clear();
  1009. return;
  1010. }
  1011. //if not buttons then heroes
  1012. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1013. hx-=pos.x;
  1014. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1015. int ny = hy/32;
  1016. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1017. {
  1018. adventureInt->statusbar.clear();
  1019. return;
  1020. }
  1021. std::vector<std::string> temp;
  1022. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->name);
  1023. temp.push_back(LOCPLINT->wanderingHeroes[from+ny]->type->heroClass->name);
  1024. adventureInt->statusbar.print( processStr(CGI->generaltexth->allTexts[15],temp) );
  1025. //select(ny+from);
  1026. }
  1027. void CHeroList::clickRight(tribool down, bool previousState)
  1028. {
  1029. if (down)
  1030. {
  1031. /***************************ARROWS*****************************************/
  1032. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1033. {
  1034. adventureInt->handleRightClick(CGI->generaltexth->zelp[303].second,down);
  1035. }
  1036. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->wanderingHeroes.size()-from>5))
  1037. {
  1038. adventureInt->handleRightClick(CGI->generaltexth->zelp[304].second,down);
  1039. }
  1040. else
  1041. {
  1042. //if not buttons then heroes
  1043. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1044. hx-=pos.x;
  1045. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1046. int ny = hy/32;
  1047. if ((ny>SIZE || ny<0) || (from+ny>=LOCPLINT->wanderingHeroes.size()))
  1048. {
  1049. return;
  1050. } //show popup
  1051. CRClickPopup::createAndPush(LOCPLINT->wanderingHeroes[from+ny], GH.current->motion);
  1052. }
  1053. }
  1054. }
  1055. void CHeroList::hover (bool on)
  1056. {
  1057. }
  1058. void CHeroList::keyPressed (const SDL_KeyboardEvent & key)
  1059. {
  1060. }
  1061. void CHeroList::updateHList(const CGHeroInstance *toRemove)
  1062. {
  1063. if(toRemove) //remove specific hero
  1064. LOCPLINT->wanderingHeroes -= toRemove;
  1065. else
  1066. LOCPLINT->recreateHeroTownList();
  1067. if(selected >= LOCPLINT->wanderingHeroes.size())
  1068. select(LOCPLINT->wanderingHeroes.size()-1);
  1069. if(toRemove)
  1070. {
  1071. if(LOCPLINT->wanderingHeroes.size() == 0)
  1072. adventureInt->townList.select(0);
  1073. else
  1074. select(selected);
  1075. }
  1076. adventureInt->updateNextHero(NULL);
  1077. }
  1078. void CHeroList::updateMove(const CGHeroInstance* which) //draws move points bar
  1079. {
  1080. int ser = -1;
  1081. for(int i=0; i<LOCPLINT->wanderingHeroes.size() && ser<0; i++)
  1082. if(LOCPLINT->wanderingHeroes[i]->subID == which->subID)
  1083. ser = i;
  1084. ser -= from;
  1085. if(ser<0 || ser >= SIZE) return;
  1086. int pom = std::min((which->movement)/100,(si32)mobile->ourImages.size()-1);
  1087. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+ser*32); //move point
  1088. }
  1089. void CHeroList::draw(SDL_Surface * to)
  1090. {
  1091. for (int iT=0+from;iT<SIZE+from;iT++)
  1092. {
  1093. int i = iT-from;
  1094. if (iT>=LOCPLINT->wanderingHeroes.size())
  1095. {
  1096. blitAt(mobile->ourImages[0].bitmap,posmobx,posmoby+i*32,to);
  1097. blitAt(mana->ourImages[0].bitmap,posmanx,posmany+i*32,to);
  1098. blitAt(empty,posporx,pospory+i*32,to);
  1099. continue;
  1100. }
  1101. const CGHeroInstance *cur = LOCPLINT->wanderingHeroes[iT];
  1102. int pom = cur->movement / 100;
  1103. if (pom>25) pom=25;
  1104. if (pom<0) pom=0;
  1105. blitAt(mobile->ourImages[pom].bitmap,posmobx,posmoby+i*32,to); //move point
  1106. pom = cur->mana / 5;
  1107. if (pom>25) pom=25;
  1108. if (pom<0) pom=0;
  1109. blitAt(mana->ourImages[pom].bitmap,posmanx,posmany+i*32,to); //mana
  1110. SDL_Surface * temp = graphics->portraitSmall[cur->portrait];
  1111. blitAt(temp,posporx,pospory+i*32,to);
  1112. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == GameConstants::HEROI_TYPE))
  1113. {
  1114. blitAt(selection,posporx,pospory+i*32,to);
  1115. }
  1116. //TODO: support for custom portraits
  1117. }
  1118. if (from>0)
  1119. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1120. else
  1121. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1122. if (LOCPLINT->wanderingHeroes.size()-from > SIZE)
  1123. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1124. else
  1125. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1126. }
  1127. int CHeroList::getPosOfHero(const CGHeroInstance* h)
  1128. {
  1129. return vstd::find_pos(LOCPLINT->wanderingHeroes, h, std::equal_to<const CGHeroInstance*>());
  1130. }
  1131. void CHeroList::show(SDL_Surface * to)
  1132. {
  1133. }
  1134. void CHeroList::showAll(SDL_Surface * to)
  1135. {
  1136. draw(to);
  1137. }
  1138. int CHeroList::size()
  1139. {
  1140. return LOCPLINT->wanderingHeroes.size();
  1141. }
  1142. CTownList::~CTownList()
  1143. {
  1144. delete arrup;
  1145. delete arrdo;
  1146. }
  1147. CTownList::CTownList(int Size, int x, int y, std::string arrupg, std::string arrdog)
  1148. :CList(Size)
  1149. {
  1150. arrup = CDefHandler::giveDef(arrupg);
  1151. arrdo = CDefHandler::giveDef(arrdog);
  1152. pos.x += x;
  1153. pos.y += y;
  1154. pos.w = std::max(arrdo->width, arrup->width);
  1155. pos.h = arrdo->height + arrup->height + Size*32;
  1156. arrupp.x=pos.x;
  1157. arrupp.y=pos.y;
  1158. arrupp.w=arrup->width;
  1159. arrupp.h=arrup->height;
  1160. arrdop.x=pos.x;
  1161. arrdop.y=pos.y+arrup->height+32*SIZE;
  1162. arrdop.w=arrdo->width;
  1163. arrdop.h=arrdo->height;
  1164. posporx = arrdop.x;
  1165. pospory = arrupp.y + arrupp.h;
  1166. pressed = indeterminate;
  1167. from = 0;
  1168. selected = -1;
  1169. }
  1170. void CTownList::genList()
  1171. {
  1172. // LOCPLINT->towns.clear();
  1173. // int howMany = LOCPLINT->cb->howManyTowns();
  1174. // for (int i=0;i<howMany;i++)
  1175. // {
  1176. // LOCPLINT->towns.push_back(LOCPLINT->cb->getTownInfo(i,0));
  1177. // }
  1178. }
  1179. void CTownList::select(int which)
  1180. {
  1181. if (which>=LOCPLINT->towns.size())
  1182. return;
  1183. selected = which;
  1184. fixPos();
  1185. if(!fun.empty())
  1186. fun();
  1187. }
  1188. void CTownList::mouseMoved (const SDL_MouseMotionEvent & sEvent)
  1189. {
  1190. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1191. {
  1192. if (from>0)
  1193. GH.statusbar->print(CGI->generaltexth->zelp[306].first);
  1194. else
  1195. GH.statusbar->clear();
  1196. return;
  1197. }
  1198. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1199. {
  1200. if ((LOCPLINT->towns.size()-from) > SIZE)
  1201. GH.statusbar->print(CGI->generaltexth->zelp[307].first);
  1202. else
  1203. GH.statusbar->clear();
  1204. return;
  1205. }
  1206. //if not buttons then towns
  1207. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1208. hx-=pos.x;
  1209. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1210. int ny = hy/32;
  1211. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1212. {
  1213. GH.statusbar->clear();
  1214. return;
  1215. };
  1216. std::string temp = CGI->generaltexth->tcommands[4];
  1217. boost::algorithm::replace_first(temp,"%s",LOCPLINT->towns[from+ny]->name);
  1218. temp += ", "+LOCPLINT->towns[from+ny]->town->Name();
  1219. GH.statusbar->print(temp);
  1220. }
  1221. void CTownList::clickLeft(tribool down, bool previousState)
  1222. {
  1223. if (down)
  1224. {
  1225. /***************************ARROWS*****************************************/
  1226. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y))
  1227. {
  1228. if(from>0)
  1229. {
  1230. blitAt(arrup->ourImages[1].bitmap,arrupp.x,arrupp.y,screenBuf);
  1231. pressed = true;
  1232. }
  1233. return;
  1234. }
  1235. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y))
  1236. {
  1237. if(LOCPLINT->towns.size()-from > SIZE)
  1238. {
  1239. blitAt(arrdo->ourImages[1].bitmap,arrdop.x,arrdop.y,screenBuf);
  1240. pressed = false;
  1241. }
  1242. return;
  1243. }
  1244. /***************************TOWNS*****************************************/
  1245. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1246. hx-=pos.x;
  1247. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1248. int ny = hy/32;
  1249. if (ny>=SIZE || ny<0)
  1250. return;
  1251. if(GH.topInt() == adventureInt
  1252. && (ny+from)==selected
  1253. && adventureInt->selection->ID == GameConstants::TOWNI_TYPE
  1254. )
  1255. LOCPLINT->openTownWindow(LOCPLINT->towns[selected]);//print town screen
  1256. else
  1257. select(ny+from);
  1258. }
  1259. else
  1260. {
  1261. if (indeterminate(pressed))
  1262. return;
  1263. if (pressed) //up
  1264. {
  1265. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,screenBuf);
  1266. pressed = indeterminate;
  1267. if (!down)
  1268. {
  1269. from--;
  1270. if (from<0)
  1271. from=0;
  1272. draw(screenBuf);
  1273. }
  1274. }
  1275. else if (!pressed) //down
  1276. {
  1277. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,screenBuf);
  1278. pressed = indeterminate;
  1279. if (!down)
  1280. {
  1281. from++;
  1282. //if (from<LOCPLINT->towns.size()-5)
  1283. // from=LOCPLINT->towns.size()-5;
  1284. draw(screenBuf);
  1285. }
  1286. }
  1287. else
  1288. throw 0;
  1289. }
  1290. }
  1291. void CTownList::clickRight(tribool down, bool previousState)
  1292. {
  1293. if (down)
  1294. {
  1295. /***************************ARROWS*****************************************/
  1296. if(isItIn(&arrupp,GH.current->motion.x,GH.current->motion.y) && from>0)
  1297. {
  1298. adventureInt->handleRightClick(CGI->generaltexth->zelp[306].second,down);
  1299. }
  1300. else if(isItIn(&arrdop,GH.current->motion.x,GH.current->motion.y) && (LOCPLINT->towns.size()-from>5))
  1301. {
  1302. adventureInt->handleRightClick(CGI->generaltexth->zelp[307].second,down);
  1303. }
  1304. //if not buttons then towns
  1305. int hx = GH.current->motion.x, hy = GH.current->motion.y;
  1306. hx-=pos.x;
  1307. hy-=pos.y; hy-=arrup->ourImages[0].bitmap->h;
  1308. int ny = hy/32;
  1309. if ((ny>=SIZE || ny<0) || (from+ny>=LOCPLINT->towns.size()))
  1310. {
  1311. return;
  1312. }
  1313. //show popup
  1314. CRClickPopup::createAndPush(LOCPLINT->towns[from+ny], GH.current->motion);
  1315. }
  1316. }
  1317. void CTownList::hover (bool on)
  1318. {
  1319. }
  1320. void CTownList::keyPressed (const SDL_KeyboardEvent & key)
  1321. {
  1322. }
  1323. void CTownList::draw(SDL_Surface * to)
  1324. {
  1325. for (int iT=0+from;iT<SIZE+from;iT++)
  1326. {
  1327. int i = iT-from;
  1328. if (iT>=LOCPLINT->towns.size())
  1329. {
  1330. blitAt(graphics->getPic(-1),posporx,pospory+i*32,to);
  1331. continue;
  1332. }
  1333. blitAt(graphics->getPic(LOCPLINT->towns[iT]->subID,LOCPLINT->towns[iT]->hasFort(),LOCPLINT->towns[iT]->builded),posporx,pospory+i*32,to);
  1334. if (adventureInt->selection && (selected == iT) && (adventureInt->selection->ID == GameConstants::TOWNI_TYPE))
  1335. {
  1336. blitAt(graphics->getPic(-2),posporx,pospory+i*32,to);
  1337. }
  1338. }
  1339. if (from>0)
  1340. blitAt(arrup->ourImages[0].bitmap,arrupp.x,arrupp.y,to);
  1341. else
  1342. blitAt(arrup->ourImages[2].bitmap,arrupp.x,arrupp.y,to);
  1343. if (LOCPLINT->towns.size()-from>SIZE)
  1344. blitAt(arrdo->ourImages[0].bitmap,arrdop.x,arrdop.y,to);
  1345. else
  1346. blitAt(arrdo->ourImages[2].bitmap,arrdop.x,arrdop.y,to);
  1347. }
  1348. void CTownList::show(SDL_Surface * to)
  1349. {
  1350. }
  1351. void CTownList::showAll(SDL_Surface * to)
  1352. {
  1353. draw(to);
  1354. }
  1355. int CTownList::size()
  1356. {
  1357. return LOCPLINT->towns.size();
  1358. }
  1359. CCreaturePic::CCreaturePic(int x, int y, const CCreature *cre, bool Big, bool Animated)
  1360. {
  1361. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1362. pos.x+=x;
  1363. pos.y+=y;
  1364. if(Big)
  1365. bg = new CPicture(graphics->backgrounds[cre->faction],0,0,false);
  1366. else
  1367. bg = new CPicture(graphics->backgroundsm[cre->faction],0,0,false);
  1368. bg->needRefresh = true;
  1369. anim = new CCreatureAnim(0, 0, cre->animDefName, Rect());
  1370. anim->clipRect(cre->doubleWide?170:150, 155, bg->pos.w, bg->pos.h);
  1371. anim->startPreview();
  1372. }
  1373. CCreaturePic::~CCreaturePic()
  1374. {
  1375. }
  1376. void CRecruitmentWindow::close()
  1377. {
  1378. GH.popIntTotally(this);
  1379. }
  1380. void CRecruitmentWindow::Max()
  1381. {
  1382. slider->moveToMax();
  1383. }
  1384. void CRecruitmentWindow::Buy()
  1385. {
  1386. int crid = creatures[which].ID,
  1387. dstslot = dst-> getSlotFor(crid);
  1388. if(dstslot < 0 && !vstd::contains(CGI->arth->bigArtifacts,CGI->arth->convertMachineID(crid, true))) //no available slot
  1389. {
  1390. std::string txt;
  1391. if(dst->ID == GameConstants::HEROI_TYPE)
  1392. {
  1393. txt = CGI->generaltexth->allTexts[425]; //The %s would join your hero, but there aren't enough provisions to support them.
  1394. boost::algorithm::replace_first(txt, "%s", slider->value > 1 ? CGI->creh->creatures[crid]->namePl : CGI->creh->creatures[crid]->nameSing);
  1395. }
  1396. else
  1397. {
  1398. txt = CGI->generaltexth->allTexts[17]; //There is no room in the garrison for this army.
  1399. }
  1400. LOCPLINT->showInfoDialog(txt);
  1401. return;
  1402. }
  1403. recruit(crid, slider->value);
  1404. if(level >= 0)
  1405. close();
  1406. else
  1407. slider->moveTo(0);
  1408. }
  1409. void CRecruitmentWindow::Cancel()
  1410. {
  1411. close();
  1412. }
  1413. void CRecruitmentWindow::sliderMoved(int to)
  1414. {
  1415. buy->block(!to);
  1416. redraw();
  1417. }
  1418. void CRecruitmentWindow::clickLeft(tribool down, bool previousState)
  1419. {
  1420. for(int i=0;i<creatures.size();i++)
  1421. {
  1422. Rect creaPos = Rect(creatures[i].pos) + pos;
  1423. if(isItIn(&creaPos, GH.current->motion.x, GH.current->motion.y))
  1424. {
  1425. which = i;
  1426. int newAmount = std::min(amounts[i],creatures[i].amount);
  1427. slider->setAmount(newAmount);
  1428. max->block(!newAmount);
  1429. if(slider->value > newAmount)
  1430. slider->moveTo(newAmount);
  1431. else
  1432. slider->moveTo(slider->value);
  1433. redraw();
  1434. break;
  1435. }
  1436. }
  1437. }
  1438. void CRecruitmentWindow::clickRight(tribool down, bool previousState)
  1439. {
  1440. if(down)
  1441. {
  1442. int curx = 192 + 51 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1443. for(int i=0;i<creatures.size();i++)
  1444. {
  1445. const int sCREATURE_WIDTH = CREATURE_WIDTH; // gcc -O0 workaround
  1446. Rect creatureRect = genRect(132, sCREATURE_WIDTH, pos.x+curx, pos.y+64);
  1447. if(isItIn(&creatureRect, GH.current->motion.x, GH.current->motion.y))
  1448. {
  1449. CIntObject *popup = createCreWindow(creatures[i].ID, 0, 0);
  1450. GH.pushInt(popup);
  1451. break;
  1452. }
  1453. curx += TOTAL_CREATURE_WIDTH;
  1454. }
  1455. }
  1456. }
  1457. void CRecruitmentWindow::showAll(SDL_Surface * to)
  1458. {
  1459. CIntObject::showAll(to);
  1460. char pom[15];
  1461. SDL_itoa(creatures[which].amount-slider->value,pom,10); //available
  1462. printAtMiddleLoc(pom,205,253,FONT_SMALL,Colors::Cornsilk,to);
  1463. SDL_itoa(slider->value,pom,10); //recruit
  1464. printAtMiddleLoc(pom,279,253,FONT_SMALL,Colors::Cornsilk,to);
  1465. printAtMiddleLoc(CGI->generaltexth->allTexts[16] + " " + CGI->creh->creatures[creatures[which].ID]->namePl,243,32,FONT_BIG,Colors::Jasmine,to); //eg "Recruit Dragon flies"
  1466. int curx = 122-creatures[which].res.size()*24;
  1467. for(int i=creatures[which].res.size()-1; i>=0; i--)// decrement used to make gold displayed as first res
  1468. {
  1469. blitAtLoc(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx,243,to);
  1470. blitAtLoc(graphics->resources32->ourImages[creatures[which].res[i].first].bitmap,curx+258,243,to);
  1471. SDL_itoa(creatures[which].res[i].second,pom,10);
  1472. printAtMiddleLoc(pom,curx+15,287,FONT_SMALL,Colors::Cornsilk,to);
  1473. SDL_itoa(creatures[which].res[i].second * slider->value,pom,10);
  1474. printAtMiddleLoc(pom,curx+15+258,287,FONT_SMALL,Colors::Cornsilk,to);
  1475. curx+=32+16;//size of bitmap + distance between them
  1476. }
  1477. for(int j=0;j<creatures.size();j++)
  1478. {
  1479. if(which==j)
  1480. drawBorderLoc(to,creatures[j].pos,int3(255,0,0));
  1481. else
  1482. drawBorderLoc(to,creatures[j].pos,int3(239,215,123));
  1483. }
  1484. }
  1485. CRecruitmentWindow::CRecruitmentWindow(const CGDwelling *Dwelling, int Level, const CArmedInstance *Dst, const boost::function<void(int,int)> &Recruit, int y_offset)
  1486. :recruit(Recruit), dwelling(Dwelling), level(Level), dst(Dst)
  1487. {
  1488. used = LCLICK | RCLICK;
  1489. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1490. which = 0;
  1491. bitmap = new CPicture("TPRCRT.bmp");
  1492. bitmap->colorizeAndConvert(LOCPLINT->playerID);
  1493. bitmap->center();
  1494. pos = (bitmap->pos += Point(0, y_offset));
  1495. bar = new CGStatusBar(8, 370, "APHLFTRT.bmp", 471);
  1496. max = new CAdventureMapButton(CGI->generaltexth->zelp[553],boost::bind(&CRecruitmentWindow::Max,this),134,313,"IRCBTNS.DEF",SDLK_m);
  1497. buy = new CAdventureMapButton(CGI->generaltexth->zelp[554],boost::bind(&CRecruitmentWindow::Buy,this),212,313,"IBY6432.DEF",SDLK_RETURN);
  1498. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[555],boost::bind(&CRecruitmentWindow::Cancel,this),290,313,"ICN6432.DEF",SDLK_ESCAPE);
  1499. slider = new CSlider(176,279,135,0,0,0,0,true);
  1500. slider->moved = boost::bind(&CRecruitmentWindow::sliderMoved,this, _1);
  1501. initCres();
  1502. printAtMiddle(CGI->generaltexth->allTexts[346],113,232,FONT_SMALL,Colors::Cornsilk,*bitmap); //cost per troop t
  1503. printAtMiddle(CGI->generaltexth->allTexts[465],205,233,FONT_SMALL,Colors::Cornsilk,*bitmap); //available t
  1504. printAtMiddle(CGI->generaltexth->allTexts[16],279,233,FONT_SMALL,Colors::Cornsilk,*bitmap); //recruit t
  1505. printAtMiddle(CGI->generaltexth->allTexts[466],371,232,FONT_SMALL,Colors::Cornsilk,*bitmap); //total cost t
  1506. drawBorder(*bitmap,172,222,67,42,int3(239,215,123));
  1507. drawBorder(*bitmap,246,222,67,42,int3(239,215,123));
  1508. drawBorder(*bitmap,64,222,99,76,int3(239,215,123));
  1509. drawBorder(*bitmap,322,222,99,76,int3(239,215,123));
  1510. drawBorder(*bitmap,133,312,66,34,int3(173,142,66));
  1511. drawBorder(*bitmap,211,312,66,34,int3(173,142,66));
  1512. drawBorder(*bitmap,289,312,66,34,int3(173,142,66));
  1513. //border for creatures
  1514. int curx = 192 + 50 - (CREATURE_WIDTH*creatures.size()/2) - (SPACE_BETWEEN*(creatures.size()-1)/2);
  1515. for(int i=0;i<creatures.size();i++)
  1516. {
  1517. creatures[i].pos.x = curx-1;
  1518. creatures[i].pos.y = 65 - 1;
  1519. creatures[i].pos.w = 100 + 2;
  1520. creatures[i].pos.h = 130 + 2;
  1521. // if(which==i)
  1522. // drawBorder(*bitmap,curx-1,64,CREATURE_WIDTH,132,int3(255,0,0));
  1523. // else
  1524. // drawBorder(*bitmap,curx-1,64,CREATURE_WIDTH,132,int3(239,215,123));
  1525. creatures[i].pic = new CCreaturePic(curx, 65, CGI->creh->creatures[creatures[i].ID]);
  1526. curx += TOTAL_CREATURE_WIDTH;
  1527. }
  1528. if(!creatures[0].amount || !amounts[0])
  1529. {
  1530. max->block(true);
  1531. slider->block(true);
  1532. }
  1533. buy->block(true);
  1534. }
  1535. CRecruitmentWindow::~CRecruitmentWindow()
  1536. {
  1537. }
  1538. void CRecruitmentWindow::initCres()
  1539. {
  1540. creatures.clear();
  1541. amounts.clear();
  1542. for(int i=0; i<dwelling->creatures.size(); i++)
  1543. {
  1544. if(level >= 0 && i != level)
  1545. continue;
  1546. for(int j = dwelling->creatures[i].second.size() - 1; j >= 0 ; j--)
  1547. {
  1548. creatures.resize(creatures.size()+1);
  1549. creinfo &cur = creatures.back();
  1550. cur.amount = dwelling->creatures[i].first;
  1551. cur.ID = dwelling->creatures[i].second[j];
  1552. const CCreature * cre= CGI->creh->creatures[cur.ID];
  1553. for(int k=0; k<cre->cost.size(); k++)
  1554. if(cre->cost[k])
  1555. cur.res.push_back(std::make_pair(k,cre->cost[k]));
  1556. amounts.push_back(cre->maxAmount(LOCPLINT->cb->getResourceAmount()));
  1557. }
  1558. }
  1559. slider->setAmount(std::min(amounts[which],creatures[which].amount));
  1560. }
  1561. CSplitWindow::CSplitWindow(int cid, int max, CGarrisonInt *Owner, int Last, int val)
  1562. {
  1563. last = Last;
  1564. which = 1;
  1565. c=cid;
  1566. slider = NULL;
  1567. gar = Owner;
  1568. SDL_Surface *hhlp = BitmapHandler::loadBitmap("GPUCRDIV.bmp");
  1569. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1570. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1571. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1572. SDL_FreeSurface(hhlp);
  1573. pos.x = screen->w/2 - bitmap->w/2;
  1574. pos.y = screen->h/2 - bitmap->h/2;
  1575. pos.w = bitmap->w;
  1576. pos.h = bitmap->h;
  1577. ok = new CAdventureMapButton("","",boost::bind(&CSplitWindow::split,this),pos.x+20,pos.y+263,"IOK6432.DEF",SDLK_RETURN);
  1578. cancel = new CAdventureMapButton("","",boost::bind(&CSplitWindow::close,this),pos.x+214,pos.y+263,"ICN6432.DEF",SDLK_ESCAPE);
  1579. int sliderPositions = max - (last>=0) - (last==2);
  1580. slider = new CSlider(pos.x+21,pos.y+194,257,boost::bind(&CSplitWindow::sliderMoved,this,_1),0,sliderPositions,val,true);
  1581. a1 = max-val;
  1582. a2 = val;
  1583. animLeft = new CCreaturePic(pos.x+20, pos.y+54, CGI->creh->creatures[cid], true, false);
  1584. animRight = new CCreaturePic(pos.x+177, pos.y+54, CGI->creh->creatures[cid], true, false);
  1585. std::string title = CGI->generaltexth->allTexts[256];
  1586. boost::algorithm::replace_first(title,"%s",CGI->creh->creatures[cid]->namePl);
  1587. printAtMiddle(title,150,34,FONT_BIG,Colors::Jasmine,bitmap);
  1588. }
  1589. CSplitWindow::~CSplitWindow() //d-tor
  1590. {
  1591. SDL_FreeSurface(bitmap);
  1592. delete ok;
  1593. delete cancel;
  1594. delete slider;
  1595. delete animLeft;
  1596. delete animRight;
  1597. }
  1598. void CSplitWindow::activate()
  1599. {
  1600. activateLClick();
  1601. activateKeys();
  1602. ok->activate();
  1603. cancel->activate();
  1604. slider->activate();
  1605. }
  1606. void CSplitWindow::deactivate()
  1607. {
  1608. deactivateLClick();
  1609. deactivateKeys();
  1610. ok->deactivate();
  1611. cancel->deactivate();
  1612. slider->deactivate();
  1613. }
  1614. void CSplitWindow::split()
  1615. {
  1616. gar->splitStacks(a2);
  1617. close();
  1618. }
  1619. void CSplitWindow::close()
  1620. {
  1621. GH.popIntTotally(this);
  1622. }
  1623. void CSplitWindow::sliderMoved(int to)
  1624. {
  1625. int all = a1+a2;
  1626. a2 = to + (last==1 || last==2);
  1627. if(slider)
  1628. a1 = all - a2;
  1629. }
  1630. void CSplitWindow::show(SDL_Surface * to)
  1631. {
  1632. blitAt(bitmap,pos.x,pos.y,to);
  1633. ok->showAll(to);
  1634. cancel->showAll(to);
  1635. slider->showAll(to);
  1636. printAtMiddle(boost::lexical_cast<std::string>(a1) + (!which ? "_" : ""),pos.x+70,pos.y+237,FONT_BIG,Colors::Cornsilk,to);
  1637. printAtMiddle(boost::lexical_cast<std::string>(a2) + (which ? "_" : ""),pos.x+233,pos.y+237,FONT_BIG,Colors::Cornsilk,to);
  1638. animLeft->show(to);
  1639. animRight->show(to);
  1640. }
  1641. void CSplitWindow::keyPressed (const SDL_KeyboardEvent & key)
  1642. {
  1643. SDLKey k = key.keysym.sym;
  1644. if (CGuiHandler::isNumKey(k)) //convert numpad number to normal digit
  1645. k = CGuiHandler::numToDigit(k);
  1646. if(key.state != SDL_PRESSED)
  1647. return;
  1648. int &cur = (which ? a2 : a1),
  1649. &sec = (which ? a1 : a2),
  1650. ncur = cur;
  1651. if (k == SDLK_BACKSPACE)
  1652. {
  1653. ncur /= 10;
  1654. }
  1655. else if(k == SDLK_TAB)
  1656. {
  1657. which = !which;
  1658. }
  1659. else if (k == SDLK_LEFT)
  1660. {
  1661. ncur--;
  1662. }
  1663. else if (k == SDLK_RIGHT)
  1664. {
  1665. ncur++;
  1666. }
  1667. else
  1668. {
  1669. int number = k - SDLK_0;
  1670. if (number < 0 || number > 9) //not a number pressed
  1671. {
  1672. return;
  1673. }
  1674. ncur = cur*10 + number;
  1675. }
  1676. int delta = ncur - cur;
  1677. if(delta > sec)
  1678. {
  1679. cur += sec;
  1680. sec = 0;
  1681. }
  1682. slider->moveTo(which ? ncur : a1+a2-ncur);
  1683. }
  1684. void CSplitWindow::clickLeft(tribool down, bool previousState)
  1685. {
  1686. if(down)
  1687. {
  1688. Point click(GH.current->motion.x,GH.current->motion.y);
  1689. click = click - pos.topLeft();
  1690. if(Rect(19,216,105,40).isIn(click)) //left picture
  1691. which = 0;
  1692. else if(Rect(175,216,105,40).isIn(click)) //right picture
  1693. which = 1;
  1694. }
  1695. }
  1696. void CLevelWindow::close()
  1697. {
  1698. for(int i=0;i<comps.size();i++)
  1699. {
  1700. if(comps[i]->selected)
  1701. {
  1702. cb(i);
  1703. break;
  1704. }
  1705. }
  1706. GH.popIntTotally(this);
  1707. LOCPLINT->showingDialog->setn(false);
  1708. }
  1709. CLevelWindow::CLevelWindow(const CGHeroInstance *hero, int pskill, std::vector<ui16> &skills, boost::function<void(ui32)> &callback)
  1710. {
  1711. LOCPLINT->showingDialog->setn(true);
  1712. heroPortrait = hero->portrait;
  1713. cb = callback;
  1714. for(int i=0;i<skills.size();i++)
  1715. {
  1716. comps.push_back(new CSelectableComponent(CComponent::secskill44, skills[i],
  1717. hero->getSecSkillLevel( static_cast<CGHeroInstance::SecondarySkill>(skills[i]) )+1,
  1718. boost::bind(&CLevelWindow::selectionChanged,this,i)));
  1719. comps.back()->assignedKeys.insert(SDLK_1 + i);
  1720. }
  1721. SDL_Surface *hhlp = BitmapHandler::loadBitmap("LVLUPBKG.bmp");
  1722. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  1723. bitmap = SDL_ConvertSurface(hhlp,screen->format,0);
  1724. SDL_SetColorKey(bitmap,SDL_SRCCOLORKEY,SDL_MapRGB(bitmap->format,0,255,255));
  1725. SDL_FreeSurface(hhlp);
  1726. pos.x = screen->w/2 - bitmap->w/2;
  1727. pos.y = screen->h/2 - bitmap->h/2;
  1728. pos.w = bitmap->w;
  1729. pos.h = bitmap->h;
  1730. ok = new CAdventureMapButton("","",boost::bind(&CLevelWindow::close,this),pos.x+297,pos.y+413,"IOKAY.DEF",SDLK_RETURN);
  1731. //draw window
  1732. char buf[100], buf2[100];
  1733. strcpy(buf2,CGI->generaltexth->allTexts[444].c_str()); //%s has gained a level.
  1734. sprintf(buf,buf2,hero->name.c_str());
  1735. printAtMiddle(buf,192,33,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1736. strcpy(buf2,CGI->generaltexth->allTexts[445].c_str()); //%s is now a level %d %s.
  1737. sprintf(buf,buf2,hero->name.c_str(),hero->level,hero->type->heroClass->name.c_str());
  1738. printAtMiddle(buf,192,162,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1739. blitAt(graphics->pskillsm->ourImages[pskill].bitmap,174,190,bitmap);
  1740. printAtMiddle((CGI->generaltexth->primarySkillNames[pskill] + " +1"),192,253,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1741. const Font *f = graphics->fonts[FONT_MEDIUM];
  1742. std::string text = CGI->generaltexth->allTexts[4];
  1743. int fontWidth = f->getWidth(text.c_str())/2;
  1744. int curx = bitmap->w/2 - ( skills.size()*44 + (skills.size()-1)*(36+fontWidth) )/2;
  1745. for(int i=0;i<comps.size();i++)
  1746. {
  1747. comps[i]->pos.x = curx+pos.x;
  1748. comps[i]->pos.y = 326+pos.y;
  1749. if( i < (comps.size()-1) )
  1750. {
  1751. curx += 44+21; //skill width + margin to "or"
  1752. printAtMiddle(text ,curx,346,FONT_MEDIUM,Colors::Cornsilk,bitmap);
  1753. curx += fontWidth+15;
  1754. }
  1755. }
  1756. if(comps.size())
  1757. {
  1758. comps[0]->select(true);
  1759. }
  1760. }
  1761. void CLevelWindow::selectionChanged(unsigned to)
  1762. {
  1763. if(ok->isBlocked())
  1764. ok->block(false);
  1765. for(int i=0;i<comps.size();i++)
  1766. if(i==to)
  1767. comps[i]->select(true);
  1768. else
  1769. comps[i]->select(false);
  1770. }
  1771. CLevelWindow::~CLevelWindow()
  1772. {
  1773. delete ok;
  1774. for(int i=0;i<comps.size();i++)
  1775. delete comps[i];
  1776. SDL_FreeSurface(bitmap);
  1777. }
  1778. void CLevelWindow::activate()
  1779. {
  1780. ok->activate();
  1781. for(int i=0;i<comps.size();i++)
  1782. comps[i]->activate();
  1783. }
  1784. void CLevelWindow::deactivate()
  1785. {
  1786. ok->deactivate();
  1787. for(int i=0;i<comps.size();i++)
  1788. comps[i]->deactivate();
  1789. }
  1790. void CLevelWindow::show(SDL_Surface * to)
  1791. {
  1792. blitAt(bitmap,pos.x,pos.y,to);
  1793. blitAt(graphics->portraitLarge[heroPortrait],170+pos.x,66+pos.y,to);
  1794. ok->showAll(to);
  1795. for(int i=0;i<comps.size();i++)
  1796. comps[i]->show(to);
  1797. }
  1798. void CMinorResDataBar::show(SDL_Surface * to)
  1799. {
  1800. blitAt(bg,pos.x,pos.y,to);
  1801. char buf[30];
  1802. for (int i=0;i<7;i++)
  1803. {
  1804. SDL_itoa(LOCPLINT->cb->getResourceAmount(i),buf,10);
  1805. CSDL_Ext::printAtMiddle(buf,pos.x + 50 + 76*i,pos.y+pos.h/2,FONT_SMALL,Colors::Cornsilk,to);
  1806. }
  1807. std::vector<std::string> temp;
  1808. SDL_itoa(LOCPLINT->cb->getDate(3),buf,10); temp.push_back(std::string(buf));
  1809. SDL_itoa(LOCPLINT->cb->getDate(2),buf,10); temp.push_back(buf);
  1810. SDL_itoa(LOCPLINT->cb->getDate(1),buf,10); temp.push_back(buf);
  1811. CSDL_Ext::printAtMiddle(CSDL_Ext::processStr(
  1812. CGI->generaltexth->allTexts[62]
  1813. +": %s, "
  1814. + CGI->generaltexth->allTexts[63]
  1815. + ": %s, "
  1816. + CGI->generaltexth->allTexts[64]
  1817. + ": %s",temp)
  1818. ,pos.x+545+(pos.w-545)/2,pos.y+pos.h/2,FONT_SMALL,Colors::Cornsilk,to);
  1819. }
  1820. void CMinorResDataBar::showAll(SDL_Surface * to)
  1821. {
  1822. show(to);
  1823. }
  1824. CMinorResDataBar::CMinorResDataBar()
  1825. {
  1826. bg = BitmapHandler::loadBitmap("Z2ESBAR.bmp");
  1827. SDL_SetColorKey(bg,SDL_SRCCOLORKEY,SDL_MapRGB(bg->format,0,255,255));
  1828. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  1829. pos.x = 7;
  1830. pos.y = 575;
  1831. pos.w = bg->w;
  1832. pos.h = bg->h;
  1833. }
  1834. CMinorResDataBar::~CMinorResDataBar()
  1835. {
  1836. SDL_FreeSurface(bg);
  1837. }
  1838. CObjectListWindow::CObjectListWindow(const std::vector<int> &_items, CPicture * titlePic, std::string _title, std::string _descr,
  1839. boost::function<void(int)> Callback, int initState)
  1840. :title(_title), descr(_descr),items(_items),selected(initState)
  1841. {
  1842. init = false;
  1843. OBJ_CONSTRUCTION_CAPTURING_ALL;
  1844. defActions = ACTIVATE | DEACTIVATE | UPDATE | SHOWALL | DISPOSE | SHARE_POS;
  1845. used = LCLICK | KEYBOARD;
  1846. onSelect = Callback;
  1847. length = 9;
  1848. pos.x = (screen->w-306)/2;
  1849. pos.y = (screen->h-468)/2;
  1850. bg = new CPicture("TPGATE.pcx");//x=0, y=0
  1851. bg->colorizeAndConvert(LOCPLINT->playerID);
  1852. slider = new CSlider(277, 120, 256, boost::bind(&CObjectListWindow::moveList,this, _1), length, items.size(), 0, false, 0);
  1853. ok = new CAdventureMapButton("","",boost::bind(&CObjectListWindow::elementSelected, this),15,402,"IOKAY.DEF", SDLK_RETURN);
  1854. exit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally,&GH, this),228,402,"ICANCEL.DEF",SDLK_ESCAPE);
  1855. pos.w = bg->pos.w;
  1856. pos.h = bg->pos.h;
  1857. titleImage = titlePic;
  1858. titleImage->pos.x =153+pos.x-titleImage->pos.w/2;
  1859. titleImage->pos.y =75 +pos.y-titleImage->pos.h/2;
  1860. for (int i=0; i<length; i++)
  1861. areas.push_back(genRect(22, 260, pos.x+15, pos.y+152+i*25 ));//rects for selecting\printing items
  1862. init = true;
  1863. }
  1864. CObjectListWindow::~CObjectListWindow()
  1865. {
  1866. delete titleImage;
  1867. }
  1868. void CObjectListWindow::elementSelected()
  1869. {
  1870. boost::function<void(int)> toCall = onSelect;//save
  1871. int where = items[selected]; //required variables
  1872. GH.popIntTotally(this);//then destroy window
  1873. toCall(where);//and send selected object
  1874. }
  1875. void CObjectListWindow::moveList(int which)
  1876. {
  1877. if (init)//TODO: is there a way to disable running this when CSlider is created?
  1878. showAll(screen2);
  1879. }
  1880. void CObjectListWindow::clickLeft(tribool down, bool previousState)
  1881. {
  1882. if (previousState && (!down))
  1883. {
  1884. for (int i=0; i<areas.size(); i++)
  1885. if(slider->value+i < items.size() && isItIn(&areas[i],GH.current->motion.x,GH.current->motion.y))
  1886. {//check all areas to find element which was clicked
  1887. selected = i+slider->value;
  1888. showAll(screen2);
  1889. return;
  1890. }
  1891. }
  1892. }
  1893. void CObjectListWindow::keyPressed (const SDL_KeyboardEvent & key)
  1894. {
  1895. if(key.state != SDL_PRESSED) return;
  1896. int sel = selected;
  1897. switch(key.keysym.sym)
  1898. {
  1899. case SDLK_UP:
  1900. sel -=1;
  1901. break;
  1902. case SDLK_DOWN:
  1903. sel +=1;
  1904. break;
  1905. case SDLK_PAGEUP:
  1906. sel -=length;
  1907. break;
  1908. case SDLK_PAGEDOWN:
  1909. sel +=length;
  1910. break;
  1911. case SDLK_HOME:
  1912. sel = 0;
  1913. break;
  1914. case SDLK_END:
  1915. sel = slider->amount;
  1916. break;
  1917. default:
  1918. return;
  1919. }
  1920. if (sel<-1)//nothing was selected & list was moved up
  1921. return;
  1922. if (sel<0)//start of list reached
  1923. sel = 0;
  1924. if ( sel >= slider->amount )//end of list reached
  1925. sel = slider->amount-1;
  1926. if ( sel >= items.size() )
  1927. sel = items.size()-1;
  1928. if ( sel < slider->value )//need to move list up
  1929. slider->moveTo(sel);
  1930. else
  1931. if ( sel >= slider->value+length )//move to bottom
  1932. slider->moveTo(sel-length+1);
  1933. selected = sel;
  1934. showAll(screen2);
  1935. }
  1936. void CObjectListWindow::show(SDL_Surface * to)
  1937. {
  1938. }
  1939. void CObjectListWindow::showAll(SDL_Surface * to)
  1940. {
  1941. ok->block((selected<0)?2:0);
  1942. CIntObject::showAll(to);
  1943. CSDL_Ext::printAtMiddle(title,pos.x+152,pos.y+27,FONT_BIG,Colors::Jasmine,to);//"castle gate"
  1944. CSDL_Ext::printAtMiddle(descr,pos.x+145,pos.y+133,FONT_SMALL,Colors::Cornsilk,to);//"select destination"
  1945. titleImage->showAll(to);
  1946. if ( selected >= slider->value && selected < slider->value+length )//if selected item is visible
  1947. {
  1948. SDL_Rect a = areas[selected-slider->value];
  1949. CSDL_Ext::drawBorder(to, a.x, a.y, a.w, a.h, int3(255, 231, 148));
  1950. CSDL_Ext::drawBorder(to, a.x-1, a.y-1, a.w+2, a.h+2, int3(255, 231, 148));//border shoul be 2 pixels width
  1951. }
  1952. int position = slider->value;
  1953. for ( int i = 0; i<9 && i<items.size()-position; i++)
  1954. CSDL_Ext::printAtMiddle(CGI->mh->map->objects[items[i+position]]->hoverName,pos.x+145,pos.y+163+25*i,
  1955. FONT_SMALL, Colors::Cornsilk, to);//print item names in list
  1956. }
  1957. CTradeWindow::CTradeableItem::CTradeableItem( EType Type, int ID, bool Left, int Serial)
  1958. {
  1959. serial = Serial;
  1960. left = Left;
  1961. type = Type;
  1962. id = ID;
  1963. used = LCLICK | HOVER | RCLICK;
  1964. downSelection = false;
  1965. hlp = NULL;
  1966. }
  1967. void CTradeWindow::CTradeableItem::showAll(SDL_Surface * to)
  1968. {
  1969. Point posToBitmap;
  1970. Point posToSubCenter;
  1971. switch(type)
  1972. {
  1973. case RESOURCE:
  1974. posToBitmap = Point(19,9);
  1975. posToSubCenter = Point(36, 59);
  1976. break;
  1977. case CREATURE_PLACEHOLDER:
  1978. case CREATURE:
  1979. posToSubCenter = Point(29, 76);
  1980. if(downSelection)
  1981. posToSubCenter.y += 5;
  1982. break;
  1983. case PLAYER:
  1984. posToSubCenter = Point(31, 76);
  1985. break;
  1986. case ARTIFACT_PLACEHOLDER:
  1987. case ARTIFACT_INSTANCE:
  1988. posToSubCenter = Point(19, 55);
  1989. if(downSelection)
  1990. posToSubCenter.y += 8;
  1991. break;
  1992. case ARTIFACT_TYPE:
  1993. posToSubCenter = Point(19, 58);
  1994. break;
  1995. }
  1996. if(SDL_Surface *hlp = getSurface())
  1997. blitAt(hlp, pos + posToBitmap, to);
  1998. printAtMiddleLoc(subtitle, posToSubCenter, FONT_SMALL, Colors::Cornsilk, to);
  1999. }
  2000. void CTradeWindow::CTradeableItem::clickLeft(tribool down, bool previousState)
  2001. {
  2002. CTradeWindow *mw = dynamic_cast<CTradeWindow *>(parent);
  2003. assert(mw);
  2004. if(down)
  2005. {
  2006. if(type == ARTIFACT_PLACEHOLDER)
  2007. {
  2008. CAltarWindow *aw = static_cast<CAltarWindow *>(mw);
  2009. if(const CArtifactInstance *movedArt = aw->arts->commonInfo->src.art)
  2010. {
  2011. aw->moveFromSlotToAltar(aw->arts->commonInfo->src.slotID, this, movedArt);
  2012. }
  2013. else if(const CArtifactInstance *art = getArtInstance())
  2014. {
  2015. aw->arts->commonInfo->src.AOH = aw->arts;
  2016. aw->arts->commonInfo->src.art = art;
  2017. aw->arts->commonInfo->src.slotID = aw->hero->getArtPos(art);
  2018. aw->arts->markPossibleSlots(art);
  2019. //aw->arts->commonInfo->dst.AOH = aw->arts;
  2020. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[art->artType->id].bitmap);
  2021. aw->arts->artifactsOnAltar.erase(art);
  2022. id = -1;
  2023. subtitle = "";
  2024. aw->deal->block(!aw->arts->artifactsOnAltar.size());
  2025. }
  2026. aw->calcTotalExp();
  2027. return;
  2028. }
  2029. if(left)
  2030. {
  2031. if(mw->hLeft != this)
  2032. mw->hLeft = this;
  2033. else
  2034. return;
  2035. }
  2036. else
  2037. {
  2038. if(mw->hRight != this)
  2039. mw->hRight = this;
  2040. else
  2041. return;
  2042. }
  2043. mw->selectionChanged(left);
  2044. }
  2045. }
  2046. SDL_Surface * CTradeWindow::CTradeableItem::getSurface()
  2047. {
  2048. switch(type)
  2049. {
  2050. case RESOURCE:
  2051. return graphics->resources32->ourImages[id].bitmap;
  2052. case PLAYER:
  2053. return graphics->flags->ourImages[id].bitmap;
  2054. case ARTIFACT_TYPE:
  2055. case ARTIFACT_PLACEHOLDER:
  2056. case ARTIFACT_INSTANCE:
  2057. return id >= 0 ? graphics->artDefs->ourImages[id].bitmap : NULL;
  2058. case CREATURE:
  2059. return graphics->bigImgs[id];
  2060. default:
  2061. return NULL;
  2062. }
  2063. }
  2064. void CTradeWindow::CTradeableItem::showAllAt(const Point &dstPos, const std::string &customSub, SDL_Surface * to)
  2065. {
  2066. Rect oldPos = pos;
  2067. std::string oldSub = subtitle;
  2068. downSelection = true;
  2069. pos = dstPos;
  2070. subtitle = customSub;
  2071. showAll(to);
  2072. downSelection = false;
  2073. pos = oldPos;
  2074. subtitle = oldSub;
  2075. }
  2076. void CTradeWindow::CTradeableItem::hover(bool on)
  2077. {
  2078. if(!on)
  2079. {
  2080. GH.statusbar->clear();
  2081. return;
  2082. }
  2083. switch(type)
  2084. {
  2085. case CREATURE:
  2086. case CREATURE_PLACEHOLDER:
  2087. GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2088. break;
  2089. case ARTIFACT_PLACEHOLDER:
  2090. if(id < 0)
  2091. GH.statusbar->print(CGI->generaltexth->zelp[582].first);
  2092. else
  2093. GH.statusbar->print(CGI->arth->artifacts[id]->Name());
  2094. break;
  2095. }
  2096. }
  2097. void CTradeWindow::CTradeableItem::clickRight(tribool down, bool previousState)
  2098. {
  2099. if(down)
  2100. {
  2101. switch(type)
  2102. {
  2103. case CREATURE:
  2104. case CREATURE_PLACEHOLDER:
  2105. //GH.statusbar->print(boost::str(boost::format(CGI->generaltexth->allTexts[481]) % CGI->creh->creatures[id]->namePl));
  2106. break;
  2107. case ARTIFACT_TYPE:
  2108. case ARTIFACT_PLACEHOLDER:
  2109. if(id >= 0)
  2110. adventureInt->handleRightClick(CGI->arth->artifacts[id]->Description(), down);
  2111. break;
  2112. }
  2113. }
  2114. }
  2115. std::string CTradeWindow::CTradeableItem::getName(int number /*= -1*/) const
  2116. {
  2117. switch(type)
  2118. {
  2119. case PLAYER:
  2120. return CGI->generaltexth->capColors[id];
  2121. case RESOURCE:
  2122. return CGI->generaltexth->restypes[id];
  2123. case CREATURE:
  2124. if(number == 1)
  2125. return CGI->creh->creatures[id]->nameSing;
  2126. else
  2127. return CGI->creh->creatures[id]->namePl;
  2128. case ARTIFACT_TYPE:
  2129. case ARTIFACT_INSTANCE:
  2130. return CGI->arth->artifacts[id]->Name();
  2131. }
  2132. assert(0);
  2133. return "";
  2134. }
  2135. const CArtifactInstance * CTradeWindow::CTradeableItem::getArtInstance() const
  2136. {
  2137. switch(type)
  2138. {
  2139. case ARTIFACT_PLACEHOLDER:
  2140. case ARTIFACT_INSTANCE:
  2141. return (const CArtifactInstance *)hlp;
  2142. default:
  2143. return NULL;
  2144. }
  2145. }
  2146. void CTradeWindow::CTradeableItem::setArtInstance(const CArtifactInstance *art)
  2147. {
  2148. assert(type == ARTIFACT_PLACEHOLDER || type == ARTIFACT_INSTANCE);
  2149. hlp = art;
  2150. if(art)
  2151. id = art->artType->id;
  2152. else
  2153. id = -1;
  2154. }
  2155. CTradeWindow::CTradeWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  2156. : market(Market), hero(Hero), arts(NULL), hLeft(NULL), hRight(NULL), readyToTrade(false)
  2157. {
  2158. type |= BLOCK_ADV_HOTKEYS;
  2159. mode = Mode;
  2160. initTypes();
  2161. }
  2162. void CTradeWindow::initTypes()
  2163. {
  2164. switch(mode)
  2165. {
  2166. case EMarketMode::RESOURCE_RESOURCE:
  2167. itemsType[1] = RESOURCE;
  2168. itemsType[0] = RESOURCE;
  2169. break;
  2170. case EMarketMode::RESOURCE_PLAYER:
  2171. itemsType[1] = RESOURCE;
  2172. itemsType[0] = PLAYER;
  2173. break;
  2174. case EMarketMode::CREATURE_RESOURCE:
  2175. itemsType[1] = CREATURE;
  2176. itemsType[0] = RESOURCE;
  2177. break;
  2178. case EMarketMode::RESOURCE_ARTIFACT:
  2179. itemsType[1] = RESOURCE;
  2180. itemsType[0] = ARTIFACT_TYPE;
  2181. break;
  2182. case EMarketMode::ARTIFACT_RESOURCE:
  2183. itemsType[1] = ARTIFACT_INSTANCE;
  2184. itemsType[0] = RESOURCE;
  2185. break;
  2186. case EMarketMode::CREATURE_EXP:
  2187. itemsType[1] = CREATURE;
  2188. itemsType[0] = CREATURE_PLACEHOLDER;
  2189. break;
  2190. case EMarketMode::ARTIFACT_EXP:
  2191. itemsType[1] = ARTIFACT_TYPE;
  2192. itemsType[0] = ARTIFACT_PLACEHOLDER;
  2193. break;
  2194. }
  2195. }
  2196. void CTradeWindow::initItems(bool Left)
  2197. {
  2198. if(Left && (itemsType[1] == ARTIFACT_TYPE || itemsType[1] == ARTIFACT_INSTANCE))
  2199. {
  2200. int xOffset = 0, yOffset = 0;
  2201. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2202. {
  2203. xOffset = -361;
  2204. yOffset = +46;
  2205. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], -1, 1, 0);
  2206. hlp->recActions &= ~(UPDATE | SHOWALL);
  2207. hlp->pos += Rect(137, 469, 42, 42);
  2208. items[Left].push_back(hlp);
  2209. }
  2210. else //ARTIFACT_EXP
  2211. {
  2212. xOffset = -363;
  2213. yOffset = -12;
  2214. }
  2215. BLOCK_CAPTURING;
  2216. arts = new CArtifactsOfHero(Point(pos.x+xOffset, pos.y+yOffset));
  2217. arts->commonInfo = new CArtifactsOfHero::SCommonPart;
  2218. arts->commonInfo->participants.insert(arts);
  2219. arts->recActions = 255;
  2220. arts->setHero(hero);
  2221. arts->allowedAssembling = false;
  2222. addChild(arts);
  2223. artSets.push_back(arts);
  2224. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2225. arts->highlightModeCallback = boost::bind(&CTradeWindow::artifactSelected, this, _1);
  2226. return;
  2227. }
  2228. std::vector<int> *ids = getItemsIds(Left);
  2229. std::vector<Rect> pos;
  2230. int amount = -1;
  2231. getPositionsFor(pos, Left, itemsType[Left]);
  2232. if(Left || !ids)
  2233. amount = 7;
  2234. else
  2235. amount = ids->size();
  2236. if(ids)
  2237. vstd::amin(amount, ids->size());
  2238. for(int j=0; j<amount; j++)
  2239. {
  2240. int id = (ids && ids->size()>j) ? (*ids)[j] : j;
  2241. if(id < 0 && mode != EMarketMode::ARTIFACT_EXP) //when sacrificing artifacts we need to prepare empty slots
  2242. continue;
  2243. CTradeableItem *hlp = new CTradeableItem(itemsType[Left], id, Left, j);
  2244. hlp->pos = pos[j] + hlp->pos;
  2245. items[Left].push_back(hlp);
  2246. }
  2247. initSubs(Left);
  2248. }
  2249. std::vector<int> *CTradeWindow::getItemsIds(bool Left)
  2250. {
  2251. std::vector<int> *ids = NULL;
  2252. if(mode == EMarketMode::ARTIFACT_EXP)
  2253. return new std::vector<int>(22, -1);
  2254. if(Left)
  2255. {
  2256. switch(itemsType[1])
  2257. {
  2258. case CREATURE:
  2259. ids = new std::vector<int>;
  2260. for(int i = 0; i < 7; i++)
  2261. {
  2262. if(const CCreature *c = hero->getCreature(i))
  2263. ids->push_back(c->idNumber);
  2264. else
  2265. ids->push_back(-1);
  2266. }
  2267. break;
  2268. }
  2269. }
  2270. else
  2271. {
  2272. switch(itemsType[0])
  2273. {
  2274. case PLAYER:
  2275. ids = new std::vector<int>;
  2276. for(int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  2277. if(i != LOCPLINT->playerID && LOCPLINT->cb->getPlayerStatus(i) == PlayerState::INGAME)
  2278. ids->push_back(i);
  2279. break;
  2280. case ARTIFACT_TYPE:
  2281. ids = new std::vector<int>(market->availableItemsIds(mode));
  2282. break;
  2283. }
  2284. }
  2285. return ids;
  2286. }
  2287. void CTradeWindow::getPositionsFor(std::vector<Rect> &poss, bool Left, EType type) const
  2288. {
  2289. if(mode == EMarketMode::ARTIFACT_EXP && !Left)
  2290. {
  2291. //22 boxes, 5 in row, last row: two boxes centered
  2292. int h, w, x, y, dx, dy;
  2293. h = w = 44;
  2294. x = 317;
  2295. y = 53;
  2296. dx = 54;
  2297. dy = 70;
  2298. for (int i = 0; i < 4 ; i++)
  2299. for (int j = 0; j < 5 ; j++)
  2300. poss += Rect(x + dx*j, y + dy*i, w, h);
  2301. poss += Rect(x + dx*1.5, y + dy*4, w, h);
  2302. poss += Rect(x + dx*2.5, y + dy*4, w, h);
  2303. }
  2304. else
  2305. {
  2306. //seven boxes:
  2307. // X X X
  2308. // X X X
  2309. // X
  2310. int h, w, x, y, dx, dy;
  2311. int leftToRightOffset;
  2312. getBaseForPositions(type, dx, dy, x, y, h, w, !Left, leftToRightOffset);
  2313. poss += genRect(h, w, x, y), genRect(h, w, x + dx, y), genRect(h, w, x + 2*dx, y),
  2314. genRect(h, w, x, y + dy), genRect(h, w, x + dx, y + dy), genRect(h, w, x + 2*dx, y + dy),
  2315. genRect(h, w, x + dx, y + 2*dy);
  2316. if(!Left)
  2317. {
  2318. BOOST_FOREACH(Rect &r, poss)
  2319. r.x += leftToRightOffset;
  2320. }
  2321. }
  2322. }
  2323. void CTradeWindow::initSubs(bool Left)
  2324. {
  2325. BOOST_FOREACH(CTradeableItem *t, items[Left])
  2326. {
  2327. if(Left)
  2328. {
  2329. switch(itemsType[1])
  2330. {
  2331. case CREATURE:
  2332. t->subtitle = boost::lexical_cast<std::string>(hero->getStackCount(t->serial));
  2333. break;
  2334. case RESOURCE:
  2335. t->subtitle = boost::lexical_cast<std::string>(LOCPLINT->cb->getResourceAmount(t->serial));
  2336. break;
  2337. }
  2338. }
  2339. else //right side
  2340. {
  2341. if(itemsType[0] == PLAYER)
  2342. {
  2343. t->subtitle = CGI->generaltexth->capColors[t->id];
  2344. }
  2345. else if(hLeft)//artifact, creature
  2346. {
  2347. int h1, h2; //hlp variables for getting offer
  2348. market->getOffer(hLeft->id, t->id, h1, h2, mode);
  2349. if(t->id != hLeft->id || mode != EMarketMode::RESOURCE_RESOURCE) //don't allow exchanging same resources
  2350. {
  2351. std::ostringstream oss;
  2352. oss << h2;
  2353. if(h1!=1)
  2354. oss << "/" << h1;
  2355. t->subtitle = oss.str();
  2356. }
  2357. else
  2358. t->subtitle = CGI->generaltexth->allTexts[164]; // n/a
  2359. }
  2360. else
  2361. t->subtitle = "";
  2362. }
  2363. }
  2364. }
  2365. void CTradeWindow::showAll(SDL_Surface * to)
  2366. {
  2367. CIntObject::showAll(to);
  2368. if(hRight)
  2369. CSDL_Ext::drawBorder(to,hRight->pos.x-1,hRight->pos.y-1,hRight->pos.w+2,hRight->pos.h+2,int3(255,231,148));
  2370. if(hLeft && hLeft->type != ARTIFACT_INSTANCE)
  2371. CSDL_Ext::drawBorder(to,hLeft->pos.x-1,hLeft->pos.y-1,hLeft->pos.w+2,hLeft->pos.h+2,int3(255,231,148));
  2372. if(readyToTrade)
  2373. {
  2374. hLeft->showAllAt(pos.topLeft() + selectionOffset(true), selectionSubtitle(true), to);
  2375. hRight->showAllAt(pos.topLeft() + selectionOffset(false), selectionSubtitle(false), to);
  2376. }
  2377. }
  2378. void CTradeWindow::removeItems(const std::set<CTradeableItem *> &toRemove)
  2379. {
  2380. BOOST_FOREACH(CTradeableItem *t, toRemove)
  2381. removeItem(t);
  2382. }
  2383. void CTradeWindow::removeItem(CTradeableItem * t)
  2384. {
  2385. if(active)
  2386. t->deactivate();
  2387. items[t->left] -= t;
  2388. delChild(t);
  2389. if(hRight == t)
  2390. {
  2391. hRight = NULL;
  2392. selectionChanged(false);
  2393. }
  2394. }
  2395. void CTradeWindow::getEmptySlots(std::set<CTradeableItem *> &toRemove)
  2396. {
  2397. BOOST_FOREACH(CTradeableItem *t, items[1])
  2398. if(!hero->getStackCount(t->serial))
  2399. toRemove.insert(t);
  2400. }
  2401. void CTradeWindow::setMode(EMarketMode::EMarketMode Mode)
  2402. {
  2403. const IMarket *m = market;
  2404. const CGHeroInstance *h = hero;
  2405. CTradeWindow *nwindow = NULL;
  2406. GH.popIntTotally(this);
  2407. switch(Mode)
  2408. {
  2409. case EMarketMode::CREATURE_EXP:
  2410. case EMarketMode::ARTIFACT_EXP:
  2411. nwindow = new CAltarWindow(m, h, Mode);
  2412. break;
  2413. default:
  2414. nwindow = new CMarketplaceWindow(m, h, Mode);
  2415. break;
  2416. }
  2417. GH.pushInt(nwindow);
  2418. }
  2419. void CTradeWindow::artifactSelected(CArtPlace *slot)
  2420. {
  2421. assert(mode == EMarketMode::ARTIFACT_RESOURCE);
  2422. items[1][0]->setArtInstance(slot->ourArt);
  2423. if(slot->ourArt)
  2424. hLeft = items[1][0];
  2425. else
  2426. hLeft = NULL;
  2427. selectionChanged(true);
  2428. }
  2429. CMarketplaceWindow::CMarketplaceWindow(const IMarket *Market, const CGHeroInstance *Hero, EMarketMode::EMarketMode Mode)
  2430. : CTradeWindow(Market, Hero, Mode)
  2431. {
  2432. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2433. madeTransaction = false;
  2434. std::string bgName;
  2435. bool sliderNeeded = true;
  2436. switch(Mode)
  2437. {
  2438. case EMarketMode::RESOURCE_RESOURCE:
  2439. bgName = "TPMRKRES.bmp";
  2440. break;
  2441. case EMarketMode::RESOURCE_PLAYER:
  2442. bgName = "TPMRKPTS.bmp";
  2443. break;
  2444. case EMarketMode::CREATURE_RESOURCE:
  2445. bgName = "TPMRKCRS.bmp";
  2446. break;
  2447. case EMarketMode::RESOURCE_ARTIFACT:
  2448. bgName = "TPMRKABS.bmp";
  2449. sliderNeeded = false;
  2450. break;
  2451. case EMarketMode::ARTIFACT_RESOURCE:
  2452. bgName = "TPMRKASS.bmp";
  2453. sliderNeeded = false;
  2454. break;
  2455. }
  2456. bg = new CPicture(bgName);
  2457. bg->colorizeAndConvert(LOCPLINT->playerID);
  2458. pos = bg->center();
  2459. new CGStatusBar(302, 576);
  2460. if(market->o->ID == 7) //black market
  2461. {
  2462. printAtMiddle(CGI->generaltexth->allTexts[349],300,27,FONT_BIG,Colors::Jasmine,*bg); //title
  2463. }
  2464. else if(market->o->ID == 99 || market->o->ID == 221) //trading post
  2465. {
  2466. printAtMiddle(CGI->generaltexth->allTexts[159],300,27,FONT_BIG,Colors::Jasmine,*bg); //title
  2467. }
  2468. else if(mode == EMarketMode::CREATURE_RESOURCE)
  2469. {
  2470. if(market->o->ID == GameConstants::TOWNI_TYPE)
  2471. printAtMiddle(CGI->buildh->buildings[6][21]->Name(), 300, 27, FONT_BIG, Colors::Jasmine, *bg); //title
  2472. else
  2473. printAtMiddle(market->o->getHoverText(), 300, 27, FONT_BIG, Colors::Jasmine, *bg); //title
  2474. }
  2475. else if(mode == EMarketMode::RESOURCE_ARTIFACT || mode == EMarketMode::ARTIFACT_RESOURCE)
  2476. {
  2477. const std::string &title = market->o->ID == GameConstants::TOWNI_TYPE
  2478. ? CGI->buildh->buildings[market->o->subID][17]->Name()
  2479. : market->o->getHoverText();
  2480. printAtMiddle(title, 300, 27, FONT_BIG, Colors::Jasmine, *bg); //title
  2481. }
  2482. else
  2483. {
  2484. printAtMiddle(CGI->generaltexth->allTexts[158],300,27,FONT_BIG,Colors::Jasmine,*bg); //marketplace
  2485. }
  2486. initItems(false);
  2487. initItems(true);
  2488. ok = new CAdventureMapButton(CGI->generaltexth->zelp[600],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2489. ok->assignedKeys.insert(SDLK_ESCAPE);
  2490. deal = new CAdventureMapButton(CGI->generaltexth->zelp[595],boost::bind(&CMarketplaceWindow::makeDeal,this),307,520,"TPMRKB.DEF");
  2491. deal->block(true);
  2492. //slider and buttons must be created after bg
  2493. if(sliderNeeded)
  2494. {
  2495. slider = new CSlider(231,490,137,0,0,0);
  2496. slider->moved = boost::bind(&CMarketplaceWindow::sliderMoved,this,_1);
  2497. max = new CAdventureMapButton(CGI->generaltexth->zelp[596],boost::bind(&CMarketplaceWindow::setMax,this),229,520,"IRCBTNS.DEF");
  2498. max->block(true);
  2499. }
  2500. else
  2501. {
  2502. slider = NULL;
  2503. max = NULL;
  2504. deal->moveBy(Point(-30, 0));
  2505. }
  2506. Rect traderTextRect;
  2507. //left side
  2508. switch(Mode)
  2509. {
  2510. case EMarketMode::RESOURCE_RESOURCE:
  2511. case EMarketMode::RESOURCE_PLAYER:
  2512. case EMarketMode::RESOURCE_ARTIFACT:
  2513. printAtMiddle(CGI->generaltexth->allTexts[270],154,148,FONT_SMALL,Colors::Cornsilk,*bg); //kingdom res.
  2514. break;
  2515. case EMarketMode::CREATURE_RESOURCE:
  2516. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 152, 102, FONT_SMALL, Colors::Cornsilk, *bg); //%s's Creatures
  2517. break;
  2518. case EMarketMode::ARTIFACT_RESOURCE:
  2519. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[271]) % hero->name), 152, 57, FONT_SMALL, Colors::Cornsilk, *bg); //%s's Artifacts
  2520. break;
  2521. }
  2522. //right side
  2523. switch(Mode)
  2524. {
  2525. case EMarketMode::RESOURCE_RESOURCE:
  2526. case EMarketMode::CREATURE_RESOURCE:
  2527. case EMarketMode::RESOURCE_ARTIFACT:
  2528. case EMarketMode::ARTIFACT_RESOURCE:
  2529. printAtMiddle(CGI->generaltexth->allTexts[168],445,148,FONT_SMALL,Colors::Cornsilk,*bg); //available for trade
  2530. traderTextRect = Rect(316, 48, 260, 75);
  2531. break;
  2532. case EMarketMode::RESOURCE_PLAYER:
  2533. printAtMiddle(CGI->generaltexth->allTexts[169],445,55,FONT_SMALL,Colors::Cornsilk,*bg); //players
  2534. traderTextRect = Rect(28, 48, 260, 75);
  2535. break;
  2536. }
  2537. traderText = new CTextBox("", traderTextRect, 0, FONT_SMALL, CENTER);
  2538. int specialOffset = mode == EMarketMode::ARTIFACT_RESOURCE ? 35 : 0; //in selling artifacts mode we need to move res-res and art-res buttons down
  2539. if(printButtonFor(EMarketMode::RESOURCE_PLAYER))
  2540. new CAdventureMapButton(CGI->generaltexth->zelp[612],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_PLAYER), 18, 520,"TPMRKBU1.DEF");
  2541. if(printButtonFor(EMarketMode::RESOURCE_RESOURCE))
  2542. new CAdventureMapButton(CGI->generaltexth->zelp[605],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_RESOURCE), 516, 450 + specialOffset,"TPMRKBU5.DEF");
  2543. if(printButtonFor(EMarketMode::CREATURE_RESOURCE))
  2544. new CAdventureMapButton(CGI->generaltexth->zelp[599],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::CREATURE_RESOURCE), 516, 485,"TPMRKBU4.DEF"); //was y=450, changed to not overlap res-res in some conditions
  2545. if(printButtonFor(EMarketMode::RESOURCE_ARTIFACT))
  2546. new CAdventureMapButton(CGI->generaltexth->zelp[598],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::RESOURCE_ARTIFACT), 18, 450 + specialOffset,"TPMRKBU2.DEF");
  2547. if(printButtonFor(EMarketMode::ARTIFACT_RESOURCE))
  2548. new CAdventureMapButton(CGI->generaltexth->zelp[613],boost::bind(&CMarketplaceWindow::setMode,this, EMarketMode::ARTIFACT_RESOURCE), 18, 485,"TPMRKBU3.DEF"); //was y=450, changed to not overlap res-art in some conditions
  2549. updateTraderText();
  2550. }
  2551. CMarketplaceWindow::~CMarketplaceWindow()
  2552. {
  2553. hLeft = hRight = NULL;
  2554. for(int i=0;i<items[1].size();i++)
  2555. delChild(items[1][i]);
  2556. for(int i=0;i<items[0].size();i++)
  2557. delChild(items[0][i]);
  2558. items[1].clear();
  2559. items[0].clear();
  2560. delChild(bg);
  2561. bg = NULL;
  2562. }
  2563. void CMarketplaceWindow::setMax()
  2564. {
  2565. slider->moveToMax();
  2566. }
  2567. void CMarketplaceWindow::makeDeal()
  2568. {
  2569. int sliderValue = 0;
  2570. if(slider)
  2571. sliderValue = slider->value;
  2572. else
  2573. sliderValue = !deal->isBlocked(); //should always be 1
  2574. if(!sliderValue)
  2575. return;
  2576. int leftIdToSend = -1;
  2577. if(mode == EMarketMode::CREATURE_RESOURCE)
  2578. leftIdToSend = hLeft->serial;
  2579. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2580. leftIdToSend = hLeft->getArtInstance()->id;
  2581. else
  2582. leftIdToSend = hLeft->id;
  2583. if(slider)
  2584. {
  2585. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, slider->value*r1, hero);
  2586. slider->moveTo(0);
  2587. }
  2588. else
  2589. {
  2590. LOCPLINT->cb->trade(market->o, mode, leftIdToSend, hRight->id, r2, hero);
  2591. }
  2592. madeTransaction = true;
  2593. hLeft = NULL;
  2594. hRight = NULL;
  2595. selectionChanged(true);
  2596. }
  2597. void CMarketplaceWindow::sliderMoved( int to )
  2598. {
  2599. redraw();
  2600. }
  2601. void CMarketplaceWindow::selectionChanged(bool side)
  2602. {
  2603. readyToTrade = hLeft && hRight;
  2604. if(mode == EMarketMode::RESOURCE_RESOURCE)
  2605. readyToTrade = readyToTrade && (hLeft->id != hRight->id); //for resource trade, two DIFFERENT resources must be selected
  2606. if(mode == EMarketMode::ARTIFACT_RESOURCE && !hLeft)
  2607. arts->unmarkSlots(false);
  2608. if(readyToTrade)
  2609. {
  2610. int soldItemId = hLeft->id;
  2611. market->getOffer(soldItemId, hRight->id, r1, r2, mode);
  2612. if(slider)
  2613. {
  2614. int newAmount = -1;
  2615. if(itemsType[1] == RESOURCE)
  2616. newAmount = LOCPLINT->cb->getResourceAmount(soldItemId);
  2617. else if(itemsType[1] == CREATURE)
  2618. newAmount = hero->getStackCount(hLeft->serial) - (hero->Slots().size() == 1 && hero->needsLastStack());
  2619. else
  2620. assert(0);
  2621. slider->setAmount(newAmount / r1);
  2622. slider->moveTo(0);
  2623. max->block(false);
  2624. deal->block(false);
  2625. }
  2626. else if(itemsType[1] == RESOURCE) //buying -> check if we can afford transaction
  2627. {
  2628. deal->block(LOCPLINT->cb->getResourceAmount(soldItemId) < r1);
  2629. }
  2630. else
  2631. deal->block(false);
  2632. }
  2633. else
  2634. {
  2635. if(slider)
  2636. {
  2637. max->block(true);
  2638. slider->setAmount(0);
  2639. slider->moveTo(0);
  2640. }
  2641. deal->block(true);
  2642. }
  2643. if(side && itemsType[0] != PLAYER) //items[1] selection changed, recalculate offers
  2644. initSubs(false);
  2645. updateTraderText();
  2646. redraw();
  2647. }
  2648. bool CMarketplaceWindow::printButtonFor(EMarketMode::EMarketMode M) const
  2649. {
  2650. return market->allowsTrade(M) && M != mode && (hero || ( M != EMarketMode::CREATURE_RESOURCE && M != EMarketMode::RESOURCE_ARTIFACT && M != EMarketMode::ARTIFACT_RESOURCE ));
  2651. }
  2652. void CMarketplaceWindow::garrisonChanged()
  2653. {
  2654. if(mode != EMarketMode::CREATURE_RESOURCE)
  2655. return;
  2656. std::set<CTradeableItem *> toRemove;
  2657. getEmptySlots(toRemove);
  2658. removeItems(toRemove);
  2659. initSubs(true);
  2660. }
  2661. void CMarketplaceWindow::artifactsChanged(bool Left)
  2662. {
  2663. assert(!Left);
  2664. if(mode != EMarketMode::RESOURCE_ARTIFACT)
  2665. return;
  2666. std::vector<int> available = market->availableItemsIds(mode);
  2667. std::set<CTradeableItem *> toRemove;
  2668. BOOST_FOREACH(CTradeableItem *t, items[0])
  2669. if(!vstd::contains(available, t->id))
  2670. toRemove.insert(t);
  2671. removeItems(toRemove);
  2672. redraw();
  2673. }
  2674. std::string CMarketplaceWindow::selectionSubtitle(bool Left) const
  2675. {
  2676. if(Left)
  2677. {
  2678. switch(itemsType[1])
  2679. {
  2680. case RESOURCE:
  2681. case CREATURE:
  2682. {
  2683. int val = slider
  2684. ? slider->value * r1
  2685. : (((deal->isBlocked())) ? 0 : r1);
  2686. return boost::lexical_cast<std::string>(val);
  2687. }
  2688. case ARTIFACT_INSTANCE:
  2689. return ((deal->isBlocked()) ? "0" : "1");
  2690. }
  2691. }
  2692. else
  2693. {
  2694. switch(itemsType[0])
  2695. {
  2696. case RESOURCE:
  2697. if(slider)
  2698. return boost::lexical_cast<std::string>( slider->value * r2 );
  2699. else
  2700. return boost::lexical_cast<std::string>(r2);
  2701. case ARTIFACT_TYPE:
  2702. return ((deal->isBlocked()) ? "0" : "1");
  2703. case PLAYER:
  2704. return (hRight ? CGI->generaltexth->capColors[hRight->id] : "");
  2705. }
  2706. }
  2707. return "???";
  2708. }
  2709. Point CMarketplaceWindow::selectionOffset(bool Left) const
  2710. {
  2711. if(Left)
  2712. {
  2713. switch(itemsType[1])
  2714. {
  2715. case RESOURCE:
  2716. return Point(122, 446);
  2717. case CREATURE:
  2718. return Point(128, 450);
  2719. case ARTIFACT_INSTANCE:
  2720. return Point(134, 466);
  2721. }
  2722. }
  2723. else
  2724. {
  2725. switch(itemsType[0])
  2726. {
  2727. case RESOURCE:
  2728. if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2729. return Point(410, 469);
  2730. else
  2731. return Point(410, 446);
  2732. case ARTIFACT_TYPE:
  2733. return Point(425, 447);
  2734. case PLAYER:
  2735. return Point(417, 451);
  2736. }
  2737. }
  2738. assert(0);
  2739. return Point(0,0);
  2740. }
  2741. void CMarketplaceWindow::resourceChanged(int type, int val)
  2742. {
  2743. initSubs(true);
  2744. }
  2745. void CMarketplaceWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2746. {
  2747. switch(type)
  2748. {
  2749. case RESOURCE:
  2750. dx = 82;
  2751. dy = 79;
  2752. x = 39;
  2753. y = 180;
  2754. h = 66;
  2755. w = 74;
  2756. break;
  2757. case PLAYER:
  2758. dx = 83;
  2759. dy = 118;
  2760. h = 64;
  2761. w = 58;
  2762. x = 44;
  2763. y = 83;
  2764. assert(Right);
  2765. break;
  2766. case CREATURE://45,123
  2767. x = 45;
  2768. y = 123;
  2769. w = 58;
  2770. h = 64;
  2771. dx = 83;
  2772. dy = 98;
  2773. assert(!Right);
  2774. break;
  2775. case ARTIFACT_TYPE://45,123
  2776. x = 340-289;
  2777. y = 180;
  2778. w = 44;
  2779. h = 44;
  2780. dx = 83;
  2781. dy = 79;
  2782. break;
  2783. }
  2784. leftToRightOffset = 289;
  2785. }
  2786. void CMarketplaceWindow::updateTraderText()
  2787. {
  2788. if(readyToTrade)
  2789. {
  2790. if(mode == EMarketMode::RESOURCE_PLAYER)
  2791. {
  2792. //I can give %s to the %s player.
  2793. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[165]) % hLeft->getName() % hRight->getName()));
  2794. }
  2795. else if(mode == EMarketMode::RESOURCE_ARTIFACT)
  2796. {
  2797. //I can offer you the %s for %d %s of %s.
  2798. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[267]) % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2799. }
  2800. else if(mode == EMarketMode::RESOURCE_RESOURCE)
  2801. {
  2802. //I can offer you %d %s of %s for %d %s of %s.
  2803. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[157]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % CGI->generaltexth->allTexts[160 + (r1==1)] % hLeft->getName()));
  2804. }
  2805. else if(mode == EMarketMode::CREATURE_RESOURCE)
  2806. {
  2807. //I can offer you %d %s of %s for %d %s.
  2808. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[269]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % r1 % hLeft->getName(r1)));
  2809. }
  2810. else if(mode == EMarketMode::ARTIFACT_RESOURCE)
  2811. {
  2812. //I can offer you %d %s of %s for your %s.
  2813. traderText->setTxt(boost::str(boost::format(CGI->generaltexth->allTexts[268]) % r2 % CGI->generaltexth->allTexts[160 + (r2==1)] % hRight->getName() % hLeft->getName(r1)));
  2814. }
  2815. return;
  2816. }
  2817. int gnrtxtnr = -1;
  2818. if(madeTransaction)
  2819. {
  2820. if(mode == EMarketMode::RESOURCE_PLAYER)
  2821. gnrtxtnr = 166; //Are there any other resources you'd like to give away?
  2822. else
  2823. gnrtxtnr = 162; //You have received quite a bargain. I expect to make no profit on the deal. Can I interest you in any of my other wares?
  2824. }
  2825. else
  2826. {
  2827. if(mode == EMarketMode::RESOURCE_PLAYER)
  2828. gnrtxtnr = 167; //If you'd like to give any of your resources to another player, click on the item you wish to give and to whom.
  2829. else
  2830. gnrtxtnr = 163; //Please inspect our fine wares. If you feel like offering a trade, click on the items you wish to trade with and for.
  2831. }
  2832. traderText->setTxt(CGI->generaltexth->allTexts[gnrtxtnr]);
  2833. }
  2834. CAltarWindow::CAltarWindow(const IMarket *Market, const CGHeroInstance *Hero /*= NULL*/, EMarketMode::EMarketMode Mode)
  2835. :CTradeWindow(Market, Hero, Mode)
  2836. {
  2837. OBJ_CONSTRUCTION_CAPTURING_ALL;
  2838. bg = new CPicture(Mode == EMarketMode::CREATURE_EXP ? "ALTARMON.bmp" : "ALTRART2.bmp");
  2839. bg->colorizeAndConvert(LOCPLINT->playerID);
  2840. pos = bg->center();
  2841. if(Mode == EMarketMode::CREATURE_EXP)
  2842. {
  2843. printAtMiddle(boost::str(boost::format(CGI->generaltexth->allTexts[272]) % hero->name), 155, 30, FONT_SMALL, Colors::Jasmine, *bg); //%s's Creatures
  2844. printAtMiddle(CGI->generaltexth->allTexts[479], 450, 30, FONT_SMALL, Colors::Jasmine, *bg); //Altar of Sacrifice
  2845. printAtMiddleWB(CGI->generaltexth->allTexts[480], 450, 70, FONT_SMALL, 45, Colors::Jasmine, *bg); //To sacrifice creatures, move them from your army on to the Altar and click Sacrifice
  2846. slider = new CSlider(231,481,137,0,0,0);
  2847. slider->moved = boost::bind(&CAltarWindow::sliderMoved,this,_1);
  2848. max = new CAdventureMapButton(CGI->generaltexth->zelp[578],boost::bind(&CSlider::moveToMax, slider),147,520,"IRCBTNS.DEF");
  2849. sacrificedUnits.resize(GameConstants::ARMY_SIZE, 0);
  2850. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[579],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTARMY.DEF");
  2851. sacrificeBackpack = NULL;
  2852. initItems(true);
  2853. mimicCres();
  2854. }
  2855. else
  2856. {
  2857. printAtMiddle(CGI->generaltexth->allTexts[477], 450, 34, FONT_SMALL, Colors::Jasmine, *bg); //Sacrifice artifacts for experience
  2858. printAtMiddle(CGI->generaltexth->allTexts[478], 302, 423, FONT_SMALL, Colors::Jasmine, *bg); //%s's Creatures
  2859. sacrificeAll = new CAdventureMapButton(CGI->generaltexth->zelp[571],boost::bind(&CAltarWindow::SacrificeAll,this),393,520,"ALTFILL.DEF");
  2860. sacrificeAll->block(!hero->artifactsInBackpack.size() && !hero->artifactsWorn.size());
  2861. sacrificeBackpack = new CAdventureMapButton(CGI->generaltexth->zelp[570],boost::bind(&CAltarWindow::SacrificeBackpack,this),147,520,"ALTEMBK.DEF");
  2862. sacrificeBackpack->block(!hero->artifactsInBackpack.size());
  2863. slider = NULL;
  2864. max = NULL;
  2865. initItems(true);
  2866. initItems(false);
  2867. }
  2868. printAtMiddleWB(CGI->generaltexth->allTexts[475], 72, 437, FONT_SMALL, 17, Colors::Jasmine, *bg); //Experience needed to reach next level
  2869. printAtMiddleWB(CGI->generaltexth->allTexts[476], 72, 505, FONT_SMALL, 17, Colors::Jasmine, *bg); //Total experience on the Altar
  2870. new CGStatusBar(302, 576);
  2871. ok = new CAdventureMapButton(CGI->generaltexth->zelp[568],boost::bind(&CGuiHandler::popIntTotally,&GH,this),516,520,"IOK6432.DEF",SDLK_RETURN);
  2872. ok->assignedKeys.insert(SDLK_ESCAPE);
  2873. deal = new CAdventureMapButton(CGI->generaltexth->zelp[585],boost::bind(&CAltarWindow::makeDeal,this),269,520,"ALTSACR.DEF");
  2874. if(Hero->getAlignment() != ::EAlignment::EVIL && Mode == EMarketMode::CREATURE_EXP)
  2875. new CAdventureMapButton(CGI->generaltexth->zelp[580], boost::bind(&CTradeWindow::setMode,this, EMarketMode::ARTIFACT_EXP), 516, 421, "ALTART.DEF");
  2876. if(Hero->getAlignment() != ::EAlignment::GOOD && Mode == EMarketMode::ARTIFACT_EXP)
  2877. new CAdventureMapButton(CGI->generaltexth->zelp[572], boost::bind(&CTradeWindow::setMode,this, EMarketMode::CREATURE_EXP), 516, 421, "ALTSACC.DEF");
  2878. expPerUnit.resize(GameConstants::ARMY_SIZE, 0);
  2879. getExpValues();
  2880. expToLevel = new CLabel(73, 475, FONT_SMALL, CENTER);
  2881. expOnAltar = new CLabel(73, 543, FONT_SMALL, CENTER);
  2882. setExpToLevel();
  2883. calcTotalExp();
  2884. blockTrade();
  2885. }
  2886. CAltarWindow::~CAltarWindow()
  2887. {
  2888. }
  2889. void CAltarWindow::getBaseForPositions(EType type, int &dx, int &dy, int &x, int &y, int &h, int &w, bool Right, int &leftToRightOffset) const
  2890. {
  2891. leftToRightOffset = 289;
  2892. x = 45;
  2893. y = 110;
  2894. w = 58;
  2895. h = 64;
  2896. dx = 83;
  2897. dy = 98;
  2898. }
  2899. void CAltarWindow::sliderMoved(int to)
  2900. {
  2901. sacrificedUnits[hLeft->serial] = to;
  2902. updateRight(hRight);
  2903. deal->block(!to);
  2904. calcTotalExp();
  2905. redraw();
  2906. }
  2907. void CAltarWindow::makeDeal()
  2908. {
  2909. if(mode == EMarketMode::CREATURE_EXP)
  2910. {
  2911. blockTrade();
  2912. slider->value = 0;
  2913. std::vector<int> toSacrifice = sacrificedUnits;
  2914. for (int i = 0; i < toSacrifice.size(); i++)
  2915. {
  2916. if(toSacrifice[i])
  2917. LOCPLINT->cb->trade(market->o, mode, i, 0, toSacrifice[i], hero);
  2918. }
  2919. BOOST_FOREACH(int& val, sacrificedUnits)
  2920. val = 0;
  2921. BOOST_FOREACH(CTradeableItem *t, items[0])
  2922. {
  2923. t->type = CREATURE_PLACEHOLDER;
  2924. t->subtitle = "";
  2925. }
  2926. }
  2927. else
  2928. {
  2929. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar) //sacrifice each artifact on the list
  2930. {
  2931. LOCPLINT->cb->trade(market->o, mode, hero->getArtPos(art), -1, 1, hero);
  2932. }
  2933. arts->artifactsOnAltar.clear();
  2934. BOOST_FOREACH(CTradeableItem *t, items[0])
  2935. {
  2936. t->id = -1;
  2937. t->subtitle = "";
  2938. }
  2939. arts->commonInfo->reset();
  2940. //arts->scrollBackpack(0);
  2941. deal->block(true);
  2942. }
  2943. calcTotalExp();
  2944. }
  2945. void CAltarWindow::SacrificeAll()
  2946. {
  2947. if(mode == EMarketMode::CREATURE_EXP)
  2948. {
  2949. bool movedAnything = false;
  2950. BOOST_FOREACH(CTradeableItem *t, items[1])
  2951. sacrificedUnits[t->serial] = hero->getStackCount(t->serial);
  2952. sacrificedUnits[items[1].front()->serial]--;
  2953. BOOST_FOREACH(CTradeableItem *t, items[0])
  2954. {
  2955. updateRight(t);
  2956. if(t->type == CREATURE)
  2957. movedAnything = true;
  2958. }
  2959. deal->block(!movedAnything);
  2960. calcTotalExp();
  2961. }
  2962. else
  2963. {
  2964. for(std::map<ui16, ArtSlotInfo>::const_iterator i = hero->artifactsWorn.begin(); i != hero->artifactsWorn.end(); i++)
  2965. {
  2966. if(i->second.artifact->artType->id != 145) //ignore locks from assembled artifacts
  2967. moveFromSlotToAltar(i->first, NULL, i->second.artifact);
  2968. }
  2969. SacrificeBackpack();
  2970. }
  2971. redraw();
  2972. }
  2973. void CAltarWindow::selectionChanged(bool side)
  2974. {
  2975. if(mode != EMarketMode::CREATURE_EXP)
  2976. return;
  2977. CTradeableItem *&selected = side ? hLeft : hRight;
  2978. CTradeableItem *&theOther = side ? hRight : hLeft;
  2979. theOther = *std::find_if(items[!side].begin(), items[!side].end(), boost::bind(&CTradeableItem::serial, _1) == selected->serial);
  2980. int stackCount = 0;
  2981. for (int i = 0; i < GameConstants::ARMY_SIZE; i++)
  2982. if(hero->getStackCount(i) > sacrificedUnits[i])
  2983. stackCount++;
  2984. slider->setAmount(hero->getStackCount(hLeft->serial) - (stackCount == 1));
  2985. slider->block(!slider->amount);
  2986. slider->value = sacrificedUnits[hLeft->serial];
  2987. max->block(!slider->amount);
  2988. readyToTrade = true;
  2989. redraw();
  2990. }
  2991. void CAltarWindow::mimicCres()
  2992. {
  2993. std::vector<Rect> positions;
  2994. getPositionsFor(positions, false, CREATURE);
  2995. BOOST_FOREACH(CTradeableItem *t, items[1])
  2996. {
  2997. CTradeableItem *hlp = new CTradeableItem(CREATURE_PLACEHOLDER, t->id, false, t->serial);
  2998. hlp->pos = positions[t->serial] + hlp->pos;
  2999. items[0].push_back(hlp);
  3000. }
  3001. }
  3002. Point CAltarWindow::selectionOffset(bool Left) const
  3003. {
  3004. if(Left)
  3005. return Point(150, 421);
  3006. else
  3007. return Point(396, 421);
  3008. }
  3009. std::string CAltarWindow::selectionSubtitle(bool Left) const
  3010. {
  3011. if(Left && slider && hLeft)
  3012. return boost::lexical_cast<std::string>(slider->value);
  3013. else if(!Left && hRight)
  3014. return hRight->subtitle;
  3015. else
  3016. return "";
  3017. }
  3018. void CAltarWindow::artifactsChanged(bool left)
  3019. {
  3020. }
  3021. void CAltarWindow::garrisonChanged()
  3022. {
  3023. if(mode != EMarketMode::CREATURE_EXP)
  3024. return;
  3025. std::set<CTradeableItem *> empty;
  3026. getEmptySlots(empty);
  3027. BOOST_FOREACH(CTradeableItem *t, empty)
  3028. {
  3029. removeItem(*std::find_if(items[0].begin(), items[0].end(), boost::bind(&CTradeableItem::serial, _1) == t->serial));
  3030. removeItem(t);
  3031. }
  3032. initSubs(true);
  3033. getExpValues();
  3034. }
  3035. void CAltarWindow::getExpValues()
  3036. {
  3037. int dump;
  3038. BOOST_FOREACH(CTradeableItem *t, items[1])
  3039. if(t->id >= 0)
  3040. market->getOffer(t->id, 0, dump, expPerUnit[t->serial], EMarketMode::CREATURE_EXP);
  3041. }
  3042. void CAltarWindow::calcTotalExp()
  3043. {
  3044. int val = 0;
  3045. if(mode == EMarketMode::CREATURE_EXP)
  3046. {
  3047. for (int i = 0; i < sacrificedUnits.size(); i++)
  3048. {
  3049. val += expPerUnit[i] * sacrificedUnits[i];
  3050. }
  3051. }
  3052. else
  3053. {
  3054. BOOST_FOREACH(const CArtifactInstance *art, arts->artifactsOnAltar)
  3055. {
  3056. int dmp, valOfArt;
  3057. market->getOffer(art->artType->id, 0, dmp, valOfArt, mode);
  3058. val += valOfArt; //WAS val += valOfArt * arts->artifactsOnAltar.count(*i);
  3059. }
  3060. }
  3061. val *=(100+hero->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0;
  3062. expOnAltar->setTxt(boost::lexical_cast<std::string>(val));
  3063. }
  3064. void CAltarWindow::setExpToLevel()
  3065. {
  3066. expToLevel->setTxt(boost::lexical_cast<std::string>(CGI->heroh->reqExp(CGI->heroh->level(hero->exp)+1) - hero->exp));
  3067. }
  3068. void CAltarWindow::blockTrade()
  3069. {
  3070. hLeft = hRight = NULL;
  3071. readyToTrade = false;
  3072. if(slider)
  3073. {
  3074. slider->block(true);
  3075. max->block(true);
  3076. }
  3077. deal->block(true);
  3078. }
  3079. void CAltarWindow::updateRight(CTradeableItem *toUpdate)
  3080. {
  3081. int val = sacrificedUnits[toUpdate->serial];
  3082. toUpdate->type = val ? CREATURE : CREATURE_PLACEHOLDER;
  3083. toUpdate->subtitle = val ? boost::str(boost::format(CGI->generaltexth->allTexts[122]) % boost::lexical_cast<std::string>(val * expPerUnit[toUpdate->serial])) : ""; //%s exp
  3084. }
  3085. int CAltarWindow::firstFreeSlot()
  3086. {
  3087. int ret = -1;
  3088. while(items[0][++ret]->id >= 0 && ret + 1 < items[0].size());
  3089. return ret < items[0].size() ? ret : -1;
  3090. }
  3091. void CAltarWindow::SacrificeBackpack()
  3092. {
  3093. std::multiset<const CArtifactInstance *> toOmmit = arts->artifactsOnAltar;
  3094. for (int i = 0; i < hero->artifactsInBackpack.size(); i++)
  3095. {
  3096. if(vstd::contains(toOmmit, hero->artifactsInBackpack[i].artifact))
  3097. {
  3098. toOmmit -= hero->artifactsInBackpack[i].artifact;
  3099. continue;
  3100. }
  3101. putOnAltar(NULL, hero->artifactsInBackpack[i].artifact);
  3102. }
  3103. arts->scrollBackpack(0);
  3104. calcTotalExp();
  3105. }
  3106. void CAltarWindow::artifactPicked()
  3107. {
  3108. redraw();
  3109. }
  3110. void CAltarWindow::showAll(SDL_Surface * to)
  3111. {
  3112. CTradeWindow::showAll(to);
  3113. if(mode == EMarketMode::ARTIFACT_EXP && arts && arts->commonInfo->src.art)
  3114. {
  3115. blitAtLoc(graphics->artDefs->ourImages[arts->commonInfo->src.art->artType->id].bitmap, 281, 442, to);
  3116. int dmp, val;
  3117. market->getOffer(arts->commonInfo->src.art->artType->id, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  3118. printAtMiddleLoc(boost::lexical_cast<std::string>(val), 304, 498, FONT_SMALL, Colors::Cornsilk, to);
  3119. }
  3120. }
  3121. bool CAltarWindow::putOnAltar(CTradeableItem* altarSlot, const CArtifactInstance *art)
  3122. {
  3123. int artID = art->artType->id;
  3124. if(artID != 1 && artID < 7) //special art
  3125. {
  3126. tlog2 << "Cannot put special artifact on altar!\n";
  3127. return false;
  3128. }
  3129. if(!altarSlot)
  3130. {
  3131. int slotIndex = firstFreeSlot();
  3132. if(slotIndex < 0)
  3133. {
  3134. tlog2 << "No free slots on altar!\n";
  3135. return false;
  3136. }
  3137. altarSlot = items[0][slotIndex];
  3138. }
  3139. int dmp, val;
  3140. market->getOffer(artID, 0, dmp, val, EMarketMode::ARTIFACT_EXP);
  3141. arts->artifactsOnAltar.insert(art);
  3142. altarSlot->setArtInstance(art);
  3143. altarSlot->subtitle = boost::lexical_cast<std::string>(val);
  3144. deal->block(false);
  3145. return true;
  3146. }
  3147. void CAltarWindow::moveFromSlotToAltar(int slotID, CTradeableItem* altarSlot, const CArtifactInstance *art)
  3148. {
  3149. int freeBackpackSlot = hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START;
  3150. if(arts->commonInfo->src.art)
  3151. {
  3152. arts->commonInfo->dst.slotID = freeBackpackSlot;
  3153. arts->commonInfo->dst.AOH = arts;
  3154. }
  3155. if(putOnAltar(altarSlot, art))
  3156. {
  3157. if(slotID < GameConstants::BACKPACK_START)
  3158. LOCPLINT->cb->swapArtifacts(hero, slotID, hero, freeBackpackSlot);
  3159. else
  3160. {
  3161. arts->commonInfo->src.clear();
  3162. arts->commonInfo->dst.clear();
  3163. CCS->curh->dragAndDropCursor(NULL);
  3164. arts->unmarkSlots(false);
  3165. }
  3166. }
  3167. }
  3168. CSystemOptionsWindow::CSystemOptionsWindow(const SDL_Rect &pos, CPlayerInterface * owner)
  3169. {
  3170. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3171. this->pos = pos;
  3172. SDL_Surface *hhlp = BitmapHandler::loadBitmap("SysOpbck.bmp", true);
  3173. graphics->blueToPlayersAdv(hhlp,LOCPLINT->playerID);
  3174. background = SDL_ConvertSurface(hhlp,screen->format,0);
  3175. SDL_SetColorKey(background,SDL_SRCCOLORKEY,SDL_MapRGB(background->format,0,255,255));
  3176. SDL_FreeSurface(hhlp);
  3177. //printing texts
  3178. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[568], 242, 32, FONT_BIG, Colors::Jasmine, background); //window title
  3179. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[569], 122, 64, FONT_MEDIUM, Colors::Jasmine, background); //hero speed
  3180. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[570], 122, 130, FONT_MEDIUM, Colors::Jasmine, background); //enemy speed
  3181. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[571], 122, 196, FONT_MEDIUM, Colors::Jasmine, background); //map scroll speed
  3182. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[20], 122, 262, FONT_MEDIUM, Colors::Jasmine, background); //video quality
  3183. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[394], 122, 347, FONT_MEDIUM, Colors::Jasmine, background); //music volume
  3184. CSDL_Ext::printAtMiddle(CGI->generaltexth->allTexts[395], 122, 412, FONT_MEDIUM, Colors::Jasmine, background); //effects volume
  3185. CSDL_Ext::printAt(CGI->generaltexth->allTexts[572], 282, 57, FONT_MEDIUM, Colors::Cornsilk, background); //show move path
  3186. CSDL_Ext::printAt(CGI->generaltexth->allTexts[573], 282, 89, FONT_MEDIUM, Colors::Cornsilk, background); //show hero reminder
  3187. CSDL_Ext::printAt(CGI->generaltexth->allTexts[574], 282, 121, FONT_MEDIUM, Colors::Cornsilk, background); //quick combat
  3188. CSDL_Ext::printAt(CGI->generaltexth->allTexts[575], 282, 153, FONT_MEDIUM, Colors::Cornsilk, background); //video subtitles
  3189. CSDL_Ext::printAt(CGI->generaltexth->allTexts[576], 282, 185, FONT_MEDIUM, Colors::Cornsilk, background); //town building outlines
  3190. CSDL_Ext::printAt(CGI->generaltexth->allTexts[577], 282, 217, FONT_MEDIUM, Colors::Cornsilk, background); //spell book animation
  3191. //setting up buttons
  3192. // load = new CAdventureMapButton (CGI->generaltexth->zelp[321].first, CGI->generaltexth->zelp[321].second, boost::bind(&CSystemOptionsWindow::loadf, this), pos.x+246, pos.y+298, "SOLOAD.DEF", SDLK_l);
  3193. // std::swap(save->imgs[0][0], load->imgs[0][1]);
  3194. save = new CAdventureMapButton (CGI->generaltexth->zelp[322].first, CGI->generaltexth->zelp[322].second, boost::bind(&CSystemOptionsWindow::bsavef, this), 357, 298, "SOSAVE.DEF", SDLK_s);
  3195. save->swappedImages = true;
  3196. save->update();
  3197. // restart = new CAdventureMapButton (CGI->generaltexth->zelp[323].first, CGI->generaltexth->zelp[323].second, boost::bind(&CSystemOptionsWindow::bmainmenuf, this), pos.x+346, pos.y+357, "SORSTRT", SDLK_r);
  3198. // std::swap(save->imgs[0][0], restart->imgs[0][1]);
  3199. mainMenu = new CAdventureMapButton (CGI->generaltexth->zelp[320].first, CGI->generaltexth->zelp[320].second, boost::bind(&CSystemOptionsWindow::bmainmenuf, this), 357, 357, "SOMAIN.DEF", SDLK_m);
  3200. mainMenu->swappedImages = true;
  3201. mainMenu->update();
  3202. quitGame = new CAdventureMapButton (CGI->generaltexth->zelp[324].first, CGI->generaltexth->zelp[324].second, boost::bind(&CSystemOptionsWindow::bquitf, this), 246, 415, "soquit.def", SDLK_q);
  3203. quitGame->swappedImages = true;
  3204. quitGame->update();
  3205. backToMap = new CAdventureMapButton (CGI->generaltexth->zelp[325].first, CGI->generaltexth->zelp[325].second, boost::bind(&CSystemOptionsWindow::breturnf, this), 357, 415, "soretrn.def", SDLK_RETURN);
  3206. backToMap->swappedImages = true;
  3207. backToMap->update();
  3208. backToMap->assignedKeys.insert(SDLK_ESCAPE);
  3209. heroMoveSpeed = new CHighlightableButtonsGroup(0);
  3210. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[349].second),CGI->generaltexth->zelp[349].second, "sysopb1.def", 28, 77, 1);
  3211. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[350].second),CGI->generaltexth->zelp[350].second, "sysopb2.def", 76, 77, 2);
  3212. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[351].second),CGI->generaltexth->zelp[351].second, "sysopb3.def", 124, 77, 4);
  3213. heroMoveSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[352].second),CGI->generaltexth->zelp[352].second, "sysopb4.def", 172, 77, 8);
  3214. heroMoveSpeed->select(owner->sysOpts.heroMoveSpeed, 1);
  3215. heroMoveSpeed->onChange = boost::bind(&SystemOptions::setHeroMoveSpeed, &owner->sysOpts, _1);
  3216. mapScrollSpeed = new CHighlightableButtonsGroup(0);
  3217. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[357].second),CGI->generaltexth->zelp[357].second, "sysopb9.def", 28, 210, 1);
  3218. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[358].second),CGI->generaltexth->zelp[358].second, "sysob10.def", 92, 210, 2);
  3219. mapScrollSpeed->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[359].second),CGI->generaltexth->zelp[359].second, "sysob11.def", 156, 210, 4);
  3220. mapScrollSpeed->select(owner->sysOpts.mapScrollingSpeed, 1);
  3221. mapScrollSpeed->onChange = boost::bind(&SystemOptions::setMapScrollingSpeed, &owner->sysOpts, _1);
  3222. musicVolume = new CHighlightableButtonsGroup(0, true);
  3223. for(int i=0; i<10; ++i)
  3224. {
  3225. musicVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[326+i].second),CGI->generaltexth->zelp[326+i].second, "syslb.def", 29 + 19*i, 359, i*11);
  3226. }
  3227. musicVolume->select(CCS->musich->getVolume(), 1);
  3228. musicVolume->onChange = boost::bind(&SystemOptions::setMusicVolume, &owner->sysOpts, _1);
  3229. effectsVolume = new CHighlightableButtonsGroup(0, true);
  3230. for(int i=0; i<10; ++i)
  3231. {
  3232. effectsVolume->addButton(boost::assign::map_list_of(0,CGI->generaltexth->zelp[336+i].second),CGI->generaltexth->zelp[336+i].second, "syslb.def", 29 + 19*i, 425, i*11);
  3233. }
  3234. effectsVolume->select(CCS->soundh->getVolume(), 1);
  3235. effectsVolume->onChange = boost::bind(&SystemOptions::setSoundVolume, &owner->sysOpts, _1);
  3236. }
  3237. CSystemOptionsWindow::~CSystemOptionsWindow()
  3238. {
  3239. SDL_FreeSurface(background);
  3240. }
  3241. void CSystemOptionsWindow::pushSDLEvent(int type, int usercode)
  3242. {
  3243. GH.popIntTotally(this);
  3244. SDL_Event event;
  3245. event.type = type;
  3246. event.user.code = usercode; // not necessarily used
  3247. SDL_PushEvent(&event);
  3248. }
  3249. void CSystemOptionsWindow::bquitf()
  3250. {
  3251. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<CComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_QUIT, 0), 0, false);
  3252. }
  3253. void CSystemOptionsWindow::breturnf()
  3254. {
  3255. GH.popIntTotally(this);
  3256. }
  3257. void CSystemOptionsWindow::bmainmenuf()
  3258. {
  3259. LOCPLINT->showYesNoDialog(CGI->generaltexth->allTexts[578], std::vector<CComponent*>(), boost::bind(&CSystemOptionsWindow::pushSDLEvent, this, SDL_USEREVENT, 2), 0, false);
  3260. }
  3261. void CSystemOptionsWindow::bsavef()
  3262. {
  3263. GH.popIntTotally(this);
  3264. GH.pushInt(new CSavingScreen(CPlayerInterface::howManyPeople > 1));
  3265. /*using namespace boost::posix_time;
  3266. std::ostringstream fnameStream;
  3267. fnameStream << second_clock::local_time();
  3268. std::string fname = fnameStream.str();
  3269. boost::algorithm::replace_all(fname,":","");
  3270. boost::algorithm::replace_all(fname," ","-");
  3271. LOCPLINT->showYesNoDialog("Do you want to save current game as " + fname, std::vector<CComponent*>(), boost::bind(&CCallback::save, LOCPLINT->cb, fname), boost::bind(&CSystemOptionsWindow::activate, this), false);*/
  3272. }
  3273. void CSystemOptionsWindow::showAll(SDL_Surface * to)
  3274. {
  3275. CSDL_Ext::blitSurface(background, NULL, to, &pos);
  3276. CIntObject::showAll(to);
  3277. }
  3278. CTavernWindow::CTavernWindow(const CGObjectInstance *TavernObj)
  3279. : tavernObj(TavernObj)
  3280. {
  3281. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3282. std::vector<const CGHeroInstance*> h = LOCPLINT->cb->getAvailableHeroes(TavernObj);
  3283. assert(h.size() == 2);
  3284. h1 = new HeroPortrait(selected,0,72,299,h[0]);
  3285. h2 = new HeroPortrait(selected,1,162,299,h[1]);
  3286. if(h[0])
  3287. selected = 0;
  3288. else
  3289. selected = -1;
  3290. oldSelected = -1;
  3291. bg = new CPicture("TPTAVERN.bmp");
  3292. bg->colorizeAndConvert(LOCPLINT->playerID);
  3293. pos = center(bg->pos);
  3294. printAtMiddle(CGI->generaltexth->jktexts[37],200,35,FONT_BIG,Colors::Jasmine,*bg);
  3295. printAtMiddle("2500",320,328,FONT_SMALL,Colors::Cornsilk,*bg);
  3296. // printAtMiddle(CGI->generaltexth->jktexts[38],146,283,FONT_BIG,Colors::Jasmine,bg); //what is this???
  3297. printAtMiddleWB(LOCPLINT->cb->getTavernGossip(tavernObj), 200, 220, FONT_SMALL, 50, Colors::Cornsilk, *bg);
  3298. bar = new CGStatusBar(8, 478, "APHLFTRT.bmp", 380);
  3299. cancel = new CAdventureMapButton(CGI->generaltexth->tavernInfo[7],"", boost::bind(&CTavernWindow::close, this), 310, 428, "ICANCEL.DEF", SDLK_ESCAPE);
  3300. recruit = new CAdventureMapButton("", "", boost::bind(&CTavernWindow::recruitb, this), 272, 355, "TPTAV01.DEF", SDLK_RETURN);
  3301. thiefGuild = new CAdventureMapButton(CGI->generaltexth->tavernInfo[5],"", boost::bind(&CTavernWindow::thievesguildb, this), 22, 428, "TPTAV02.DEF", SDLK_t);
  3302. if(LOCPLINT->cb->getResourceAmount(6) < 2500) //not enough gold
  3303. {
  3304. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[0]; //Cannot afford a Hero
  3305. recruit->block(2);
  3306. }
  3307. else if(LOCPLINT->cb->howManyHeroes(false) >= 8)
  3308. {
  3309. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[1]; //Cannot recruit. You already have %d Heroes.
  3310. boost::algorithm::replace_first(recruit->hoverTexts[0],"%d",boost::lexical_cast<std::string>(LOCPLINT->cb->howManyHeroes()));
  3311. recruit->block(2);
  3312. }
  3313. else if(LOCPLINT->castleInt && LOCPLINT->castleInt->town->visitingHero)
  3314. {
  3315. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[2]; //Cannot recruit. You already have a Hero in this town.
  3316. recruit->block(2);
  3317. }
  3318. else
  3319. {
  3320. if(!h[0])
  3321. recruit->block(1);
  3322. }
  3323. CCS->videoh->open(VIDEO_TAVERN);
  3324. }
  3325. void CTavernWindow::recruitb()
  3326. {
  3327. const CGHeroInstance *toBuy = (selected ? h2 : h1)->h;
  3328. const CGObjectInstance *obj = tavernObj;
  3329. close();
  3330. LOCPLINT->cb->recruitHero(obj, toBuy);
  3331. }
  3332. void CTavernWindow::thievesguildb()
  3333. {
  3334. GH.pushInt( new CThievesGuildWindow(tavernObj) );
  3335. }
  3336. CTavernWindow::~CTavernWindow()
  3337. {
  3338. CCS->videoh->close();
  3339. }
  3340. void CTavernWindow::close()
  3341. {
  3342. GH.popIntTotally(this);
  3343. }
  3344. void CTavernWindow::show(SDL_Surface * to)
  3345. {
  3346. CIntObject::show(to);
  3347. CCS->videoh->update(pos.x+70, pos.y+56, to, true, false);
  3348. if(selected >= 0)
  3349. {
  3350. HeroPortrait *sel = selected ? h2 : h1;
  3351. if (selected != oldSelected && !recruit->isBlocked())
  3352. {
  3353. // Selected hero just changed. Update RECRUIT button hover text if recruitment is allowed.
  3354. oldSelected = selected;
  3355. recruit->hoverTexts[0] = CGI->generaltexth->tavernInfo[3]; //Recruit %s the %s
  3356. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->name);
  3357. boost::algorithm::replace_first(recruit->hoverTexts[0],"%s",sel->h->type->heroClass->name);
  3358. }
  3359. printAtMiddleWB(sel->descr,pos.x+146,pos.y+389,FONT_SMALL,40,Colors::Cornsilk,to);
  3360. CSDL_Ext::drawBorder(to,sel->pos.x-2,sel->pos.y-2,sel->pos.w+4,sel->pos.h+4,int3(247,223,123));
  3361. }
  3362. }
  3363. void CTavernWindow::HeroPortrait::clickLeft(tribool down, bool previousState)
  3364. {
  3365. if(previousState && !down && h)
  3366. *_sel = _id;
  3367. }
  3368. void CTavernWindow::HeroPortrait::clickRight(tribool down, bool previousState)
  3369. {
  3370. if(down && h)
  3371. {
  3372. GH.pushInt(new CRClickPopupInt(new CHeroWindow(h), true));
  3373. }
  3374. }
  3375. CTavernWindow::HeroPortrait::HeroPortrait(int &sel, int id, int x, int y, const CGHeroInstance *H)
  3376. : h(H), _sel(&sel), _id(id)
  3377. {
  3378. used = LCLICK | RCLICK | HOVER;
  3379. h = H;
  3380. pos.x = x;
  3381. pos.y = y;
  3382. pos.w = 58;
  3383. pos.h = 64;
  3384. if(H)
  3385. {
  3386. hoverName = CGI->generaltexth->tavernInfo[4];
  3387. boost::algorithm::replace_first(hoverName,"%s",H->name);
  3388. int artifs = h->artifactsWorn.size() + h->artifactsInBackpack.size();
  3389. for(int i=13; i<=17; i++) //war machines and spellbook don't count
  3390. if(vstd::contains(h->artifactsWorn,i))
  3391. artifs--;
  3392. sprintf_s(descr, sizeof(descr),CGI->generaltexth->allTexts[215].c_str(),
  3393. h->name.c_str(), h->level, h->type->heroClass->name.c_str(), artifs);
  3394. descr[sizeof(descr)-1] = '\0';
  3395. }
  3396. }
  3397. void CTavernWindow::HeroPortrait::show(SDL_Surface * to)
  3398. {
  3399. blitAt(graphics->portraitLarge[h->subID],pos,to);
  3400. }
  3401. void CTavernWindow::HeroPortrait::hover( bool on )
  3402. {
  3403. //Hoverable::hover(on);
  3404. if(on)
  3405. GH.statusbar->print(hoverName);
  3406. else
  3407. GH.statusbar->clear();
  3408. }
  3409. void CInGameConsole::activate()
  3410. {
  3411. activateKeys();
  3412. }
  3413. void CInGameConsole::deactivate()
  3414. {
  3415. deactivateKeys();
  3416. }
  3417. void CInGameConsole::show(SDL_Surface * to)
  3418. {
  3419. int number = 0;
  3420. std::vector<std::list< std::pair< std::string, int > >::iterator> toDel;
  3421. texts_mx.lock();
  3422. for(std::list< std::pair< std::string, int > >::iterator it = texts.begin(); it != texts.end(); ++it, ++number)
  3423. {
  3424. SDL_Color green = {0,0xff,0,0};
  3425. Point leftBottomCorner(0, screen->h);
  3426. if(LOCPLINT->battleInt)
  3427. {
  3428. leftBottomCorner = LOCPLINT->battleInt->pos.bottomLeft();
  3429. }
  3430. CSDL_Ext::printAt(it->first, leftBottomCorner.x + 50, leftBottomCorner.y - texts.size() * 20 - 80 + number*20, FONT_MEDIUM, green);
  3431. if(SDL_GetTicks() - it->second > defaultTimeout)
  3432. {
  3433. toDel.push_back(it);
  3434. }
  3435. }
  3436. for(int it=0; it<toDel.size(); ++it)
  3437. {
  3438. texts.erase(toDel[it]);
  3439. }
  3440. texts_mx.unlock();
  3441. }
  3442. void CInGameConsole::print(const std::string &txt)
  3443. {
  3444. texts_mx.lock();
  3445. int lineLen = conf.go()->ac.outputLineLength;
  3446. if(txt.size() < lineLen)
  3447. {
  3448. texts.push_back(std::make_pair(txt, SDL_GetTicks()));
  3449. if(texts.size() > maxDisplayedTexts)
  3450. {
  3451. texts.pop_front();
  3452. }
  3453. }
  3454. else
  3455. {
  3456. assert(lineLen);
  3457. for(int g=0; g<txt.size() / lineLen + 1; ++g)
  3458. {
  3459. std::string part = txt.substr(g * lineLen, lineLen);
  3460. if(part.size() == 0)
  3461. break;
  3462. texts.push_back(std::make_pair(part, SDL_GetTicks()));
  3463. if(texts.size() > maxDisplayedTexts)
  3464. {
  3465. texts.pop_front();
  3466. }
  3467. }
  3468. }
  3469. texts_mx.unlock();
  3470. }
  3471. void CInGameConsole::keyPressed (const SDL_KeyboardEvent & key)
  3472. {
  3473. if(key.type != SDL_KEYDOWN) return;
  3474. if(!captureAllKeys && key.keysym.sym != SDLK_TAB) return; //because user is not entering any text
  3475. switch(key.keysym.sym)
  3476. {
  3477. case SDLK_TAB:
  3478. case SDLK_ESCAPE:
  3479. {
  3480. if(captureAllKeys)
  3481. {
  3482. captureAllKeys = false;
  3483. endEnteringText(false);
  3484. }
  3485. else if(SDLK_TAB)
  3486. {
  3487. captureAllKeys = true;
  3488. startEnteringText();
  3489. }
  3490. break;
  3491. }
  3492. case SDLK_RETURN: //enter key
  3493. {
  3494. if(enteredText.size() > 0 && captureAllKeys)
  3495. {
  3496. captureAllKeys = false;
  3497. endEnteringText(true);
  3498. }
  3499. break;
  3500. }
  3501. case SDLK_BACKSPACE:
  3502. {
  3503. if(enteredText.size() > 1)
  3504. {
  3505. enteredText.resize(enteredText.size()-1);
  3506. enteredText[enteredText.size()-1] = '_';
  3507. refreshEnteredText();
  3508. }
  3509. break;
  3510. }
  3511. case SDLK_UP: //up arrow
  3512. {
  3513. if(previouslyEntered.size() == 0)
  3514. break;
  3515. if(prevEntDisp == -1)
  3516. {
  3517. prevEntDisp = previouslyEntered.size() - 1;
  3518. enteredText = previouslyEntered[prevEntDisp] + "_";
  3519. refreshEnteredText();
  3520. }
  3521. else if( prevEntDisp > 0)
  3522. {
  3523. --prevEntDisp;
  3524. enteredText = previouslyEntered[prevEntDisp] + "_";
  3525. refreshEnteredText();
  3526. }
  3527. break;
  3528. }
  3529. case SDLK_DOWN: //down arrow
  3530. {
  3531. if(prevEntDisp != -1 && prevEntDisp+1 < previouslyEntered.size())
  3532. {
  3533. ++prevEntDisp;
  3534. enteredText = previouslyEntered[prevEntDisp] + "_";
  3535. refreshEnteredText();
  3536. }
  3537. else if(prevEntDisp+1 == previouslyEntered.size()) //useful feature
  3538. {
  3539. prevEntDisp = -1;
  3540. enteredText = "_";
  3541. refreshEnteredText();
  3542. }
  3543. break;
  3544. }
  3545. default:
  3546. {
  3547. if(enteredText.size() > 0 && enteredText.size() < conf.go()->ac.inputLineLength)
  3548. {
  3549. if( key.keysym.unicode < 0x80 && key.keysym.unicode > 0 )
  3550. {
  3551. enteredText[enteredText.size()-1] = (char)key.keysym.unicode;
  3552. enteredText += "_";
  3553. refreshEnteredText();
  3554. }
  3555. }
  3556. break;
  3557. }
  3558. }
  3559. }
  3560. void CInGameConsole::startEnteringText()
  3561. {
  3562. enteredText = "_";
  3563. if(GH.topInt() == adventureInt)
  3564. {
  3565. GH.statusbar->print(enteredText);
  3566. }
  3567. else if(LOCPLINT->battleInt)
  3568. {
  3569. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3570. }
  3571. }
  3572. void CInGameConsole::endEnteringText(bool printEnteredText)
  3573. {
  3574. prevEntDisp = -1;
  3575. if(printEnteredText)
  3576. {
  3577. std::string txt = enteredText.substr(0, enteredText.size()-1);
  3578. LOCPLINT->cb->sendMessage(txt);
  3579. previouslyEntered.push_back(txt);
  3580. print(txt);
  3581. }
  3582. enteredText = "";
  3583. if(GH.topInt() == adventureInt)
  3584. {
  3585. GH.statusbar->clear();
  3586. }
  3587. else if(LOCPLINT->battleInt)
  3588. {
  3589. LOCPLINT->battleInt->console->ingcAlter = "";
  3590. }
  3591. }
  3592. void CInGameConsole::refreshEnteredText()
  3593. {
  3594. if(GH.topInt() == adventureInt)
  3595. {
  3596. GH.statusbar->print(enteredText);
  3597. }
  3598. else if(LOCPLINT->battleInt)
  3599. {
  3600. LOCPLINT->battleInt->console->ingcAlter = enteredText;
  3601. }
  3602. }
  3603. CInGameConsole::CInGameConsole() : prevEntDisp(-1), defaultTimeout(10000), maxDisplayedTexts(10)
  3604. {
  3605. }
  3606. void CGarrisonWindow::close()
  3607. {
  3608. GH.popIntTotally(this);
  3609. }
  3610. CGarrisonWindow::CGarrisonWindow( const CArmedInstance *up, const CGHeroInstance *down, bool removableUnits )
  3611. {
  3612. OBJ_CONSTRUCTION_CAPTURING_ALL;
  3613. bg = new CPicture("GARRISON.bmp");
  3614. bg->colorizeAndConvert(LOCPLINT->playerID);
  3615. pos = bg->center();
  3616. garr = new CGarrisonInt(92, 127, 4, Point(0,96), bg->bg, Point(93,127), up, down, removableUnits);
  3617. {
  3618. CAdventureMapButton *split = new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),88,314,"IDV6432.DEF");
  3619. removeChild(split);
  3620. garr->addSplitBtn(split);
  3621. }
  3622. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8],"",boost::bind(&CGarrisonWindow::close,this),399,314,"IOK6432.DEF",SDLK_RETURN);
  3623. std::string titleText;
  3624. if (garr->armedObjs[1]->tempOwner == garr->armedObjs[0]->tempOwner)
  3625. titleText = CGI->generaltexth->allTexts[709];
  3626. else
  3627. {
  3628. titleText = CGI->generaltexth->allTexts[35];
  3629. boost::algorithm::replace_first(titleText, "%s", garr->armedObjs[0]->Slots().begin()->second->type->namePl);
  3630. }
  3631. title = new CLabel(275, 30, FONT_BIG, CENTER, Colors::Jasmine, titleText);
  3632. }
  3633. CGarrisonWindow::~CGarrisonWindow()
  3634. {
  3635. }
  3636. void CGarrisonWindow::showAll(SDL_Surface * to)
  3637. {
  3638. CIntObject::showAll(to);
  3639. blitAtLoc(graphics->flags->ourImages[garr->armedObjs[1]->getOwner()].bitmap,28,124,to);
  3640. blitAtLoc(graphics->portraitLarge[static_cast<const CGHeroInstance*>(garr->armedObjs[1])->portrait],29,222,to);
  3641. }
  3642. IShowActivatable::IShowActivatable()
  3643. {
  3644. type = 0;
  3645. }
  3646. CGarrisonHolder::CGarrisonHolder()
  3647. {
  3648. type |= WITH_GARRISON;
  3649. }
  3650. void CWindowWithGarrison::updateGarrisons()
  3651. {
  3652. garr->recreateSlots();
  3653. }
  3654. CArtPlace::CArtPlace(const CArtifactInstance* Art)
  3655. :picked(false), marked(false), locked(false), ourArt(Art)
  3656. {
  3657. }
  3658. CArtPlace::CArtPlace(Point position, const CArtifactInstance * Art):
  3659. picked(false), marked(false), locked(false), ourArt(Art)
  3660. {
  3661. pos += position;
  3662. pos.w = pos.h = 44;
  3663. }
  3664. void CArtPlace::activate()
  3665. {
  3666. if(!active)
  3667. {
  3668. LRClickableAreaWTextComp::activate();
  3669. }
  3670. }
  3671. void CArtPlace::clickLeft(tribool down, bool previousState)
  3672. {
  3673. //LRClickableAreaWTextComp::clickLeft(down);
  3674. bool inBackpack = slotID >= GameConstants::BACKPACK_START,
  3675. srcInBackpack = ourOwner->commonInfo->src.slotID >= GameConstants::BACKPACK_START,
  3676. srcInSameHero = ourOwner->commonInfo->src.AOH == ourOwner;
  3677. if(ourOwner->highlightModeCallback && ourArt)
  3678. {
  3679. if(down)
  3680. {
  3681. if(ourArt->artType->id < 7) //War Machine or Spellbook
  3682. {
  3683. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[21]); //This item can't be traded.
  3684. }
  3685. else
  3686. {
  3687. ourOwner->unmarkSlots(false);
  3688. marked = true;
  3689. ourOwner->highlightModeCallback(this);
  3690. }
  3691. }
  3692. return;
  3693. }
  3694. // If clicked on spellbook, open it only if no artifact is held at the moment.
  3695. if(ourArt && !down && previousState && !ourOwner->commonInfo->src.AOH)
  3696. {
  3697. if(ourArt->artType->id == 0)
  3698. {
  3699. CSpellWindow * spellWindow = new CSpellWindow(genRect(595, 620, (conf.cc.resx - 620)/2, (conf.cc.resy - 595)/2), ourOwner->curHero, LOCPLINT, LOCPLINT->battleInt);
  3700. GH.pushInt(spellWindow);
  3701. }
  3702. }
  3703. if (!down && previousState)
  3704. {
  3705. if(ourArt && ourArt->artType->id == 0) //spellbook
  3706. return; //this is handled separately
  3707. if(!ourOwner->commonInfo->src.AOH) //nothing has been clicked
  3708. {
  3709. if(ourArt //to prevent selecting empty slots (bugfix to what GrayFace reported)
  3710. && ourOwner->curHero->tempOwner == LOCPLINT->playerID)//can't take art from another player
  3711. {
  3712. if(ourArt->artType->id == 3) //catapult cannot be highlighted
  3713. {
  3714. std::vector<CComponent *> catapult(1, new CComponent(CComponent::artifact, 3, 0));
  3715. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[312], catapult); //The Catapult must be equipped.
  3716. return;
  3717. }
  3718. select();
  3719. }
  3720. }
  3721. else if(ourArt == ourOwner->commonInfo->src.art) //restore previously picked artifact
  3722. {
  3723. deselect();
  3724. }
  3725. else //perform artifact transition
  3726. {
  3727. if(inBackpack) // Backpack destination.
  3728. {
  3729. if(srcInBackpack && slotID == ourOwner->commonInfo->src.slotID + 1) //next slot (our is not visible, so visually same as "old" place) to the art -> make nothing, return artifact to slot
  3730. {
  3731. deselect();
  3732. }
  3733. else
  3734. {
  3735. const CArtifact * const cur = ourOwner->commonInfo->src.art->artType;
  3736. switch(cur->id)
  3737. {
  3738. case 3:
  3739. //should not happen, catapult cannot be selected
  3740. assert(cur->id != 3);
  3741. break;
  3742. case 4: case 5: case 6: //war machines cannot go to backpack
  3743. LOCPLINT->showInfoDialog(boost::str(boost::format(CGI->generaltexth->allTexts[153]) % cur->Name()));
  3744. break;
  3745. default:
  3746. setMeAsDest();
  3747. vstd::amin(ourOwner->commonInfo->dst.slotID, ourOwner->curHero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  3748. if(srcInBackpack && srcInSameHero)
  3749. {
  3750. if(!ourArt //cannot move from backpack to AFTER backpack -> combined with vstd::amin above it will guarantee that dest is at most the last artifact
  3751. || ourOwner->commonInfo->src.slotID < ourOwner->commonInfo->dst.slotID) //rearranging arts in backpack after taking src artifact, the dest id will be shifted
  3752. ourOwner->commonInfo->dst.slotID--;
  3753. }
  3754. if(srcInSameHero && ourOwner->commonInfo->dst.slotID == ourOwner->commonInfo->src.slotID) //we came to src == dst
  3755. deselect();
  3756. else
  3757. ourOwner->realizeCurrentTransaction();
  3758. break;
  3759. }
  3760. }
  3761. }
  3762. //check if swap is possible
  3763. else if (fitsHere(ourOwner->commonInfo->src.art) &&
  3764. (!ourArt || ourOwner->curHero->tempOwner == LOCPLINT->playerID))
  3765. {
  3766. setMeAsDest();
  3767. //
  3768. // // Special case when the dest artifact can't be fit into the src slot.
  3769. // //CGI->arth->unequipArtifact(ourOwner->curHero->artifWorn, slotID);
  3770. // const CArtifactsOfHero* srcAOH = ourOwner->commonInfo->src.AOH;
  3771. // ui16 srcSlotID = ourOwner->commonInfo->src.slotID;
  3772. // if (ourArt && srcSlotID < 19 && !ourArt->canBePutAt(ArtifactLocation(srcAOH->curHero, srcSlotID)))
  3773. // {
  3774. // // Put dest artifact into owner's backpack.
  3775. // ourOwner->commonInfo->src.AOH = ourOwner;
  3776. // ourOwner->commonInfo->src.slotID = ourOwner->curHero->artifacts.size() + 19;
  3777. // }
  3778. ourOwner->realizeCurrentTransaction();
  3779. }
  3780. }
  3781. }
  3782. }
  3783. void CArtPlace::clickRight(tribool down, bool previousState)
  3784. {
  3785. if(down && ourArt && !locked && text.size() && !picked) //if there is no description or it's a lock, do nothing ;]
  3786. {
  3787. if (slotID < 19)
  3788. {
  3789. if(ourOwner->allowedAssembling)
  3790. {
  3791. std::vector<const CArtifact *> assemblyPossibilities = ourArt->assemblyPossibilities(ourOwner->curHero);
  3792. // If the artifact can be assembled, display dialog.
  3793. BOOST_FOREACH(const CArtifact *combination, assemblyPossibilities)
  3794. {
  3795. LOCPLINT->showArtifactAssemblyDialog(
  3796. ourArt->artType->id,
  3797. combination->id,
  3798. true,
  3799. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, true, combination->id),
  3800. 0);
  3801. if(assemblyPossibilities.size() > 2)
  3802. {
  3803. tlog3 << "More than one possibility of assembling... taking only first\n";
  3804. break;
  3805. }
  3806. return;
  3807. }
  3808. // Otherwise if the artifact can be diasassembled, display dialog.
  3809. if(ourArt->canBeDisassembled())
  3810. {
  3811. LOCPLINT->showArtifactAssemblyDialog(
  3812. ourArt->artType->id,
  3813. 0,
  3814. false,
  3815. boost::bind(&CCallback::assembleArtifacts, LOCPLINT->cb, ourOwner->curHero, slotID, false, 0),
  3816. 0);
  3817. return;
  3818. }
  3819. }
  3820. }
  3821. // Lastly just show the artifact description.
  3822. LRClickableAreaWTextComp::clickRight(down, previousState);
  3823. }
  3824. }
  3825. /**
  3826. * Selects artifact slot so that the containing artifact looks like it's picked up.
  3827. */
  3828. void CArtPlace::select ()
  3829. {
  3830. if (locked)
  3831. return;
  3832. picked = true;
  3833. if(ourArt->canBeDisassembled() && slotID < GameConstants::BACKPACK_START) //worn combined artifact -> locks have to disappear
  3834. {
  3835. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3836. {
  3837. CArtPlace *ap = ourOwner->getArtPlace(i);
  3838. ap->picked = ourArt->isPart(ap->ourArt);
  3839. }
  3840. }
  3841. //int backpackCorrection = -(slotID - Arts::BACKPACK_START < ourOwner->backpackPos);
  3842. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[ourArt->artType->id].bitmap);
  3843. ourOwner->commonInfo->src.setTo(this, false);
  3844. ourOwner->markPossibleSlots(ourArt);
  3845. if(slotID >= GameConstants::BACKPACK_START)
  3846. ourOwner->scrollBackpack(0); //will update slots
  3847. ourOwner->updateParentWindow();
  3848. ourOwner->safeRedraw();
  3849. }
  3850. /**
  3851. * Deselects the artifact slot. FIXME: Not used. Maybe it should?
  3852. */
  3853. void CArtPlace::deselect ()
  3854. {
  3855. picked = false;
  3856. if(ourArt && ourArt->canBeDisassembled()) //combined art returned to its slot -> restore locks
  3857. {
  3858. for(int i = 0; i < GameConstants::BACKPACK_START; i++)
  3859. ourOwner->getArtPlace(i)->picked = false;
  3860. }
  3861. CCS->curh->dragAndDropCursor(NULL);
  3862. ourOwner->unmarkSlots();
  3863. ourOwner->commonInfo->src.clear();
  3864. if(slotID >= GameConstants::BACKPACK_START)
  3865. ourOwner->scrollBackpack(0); //will update slots
  3866. ourOwner->updateParentWindow();
  3867. ourOwner->safeRedraw();
  3868. }
  3869. void CArtPlace::deactivate()
  3870. {
  3871. if(active)
  3872. {
  3873. LRClickableAreaWTextComp::deactivate();
  3874. }
  3875. }
  3876. void CArtPlace::showAll(SDL_Surface * to)
  3877. {
  3878. if (ourArt && !picked && ourArt == ourOwner->curHero->getArt(slotID, false)) //last condition is needed for disassembling -> artifact may be gone, but we don't know yet TODO: real, nice solution
  3879. {
  3880. int graphic = locked ? 145 : ourArt->artType->id;
  3881. blitAt(graphics->artDefs->ourImages[graphic].bitmap, pos.x, pos.y, to);
  3882. }
  3883. if(marked && active)
  3884. {
  3885. // Draw vertical bars.
  3886. for (int i = 0; i < pos.h; ++i)
  3887. {
  3888. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x, pos.y + i, 240, 220, 120);
  3889. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + pos.w - 1, pos.y + i, 240, 220, 120);
  3890. }
  3891. // Draw horizontal bars.
  3892. for (int i = 0; i < pos.w; ++i)
  3893. {
  3894. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y, 240, 220, 120);
  3895. CSDL_Ext::SDL_PutPixelWithoutRefresh(to, pos.x + i, pos.y + pos.h - 1, 240, 220, 120);
  3896. }
  3897. }
  3898. }
  3899. bool CArtPlace::fitsHere(const CArtifactInstance * art) const
  3900. {
  3901. // You can place 'no artifact' anywhere.
  3902. if(!art)
  3903. return true;
  3904. // Anything can but War Machines can be placed in backpack.
  3905. if (slotID >= 19)
  3906. return !CGI->arth->isBigArtifact(art->id);
  3907. return art->canBePutAt(ArtifactLocation(ourOwner->curHero, slotID), true);
  3908. }
  3909. CArtPlace::~CArtPlace()
  3910. {
  3911. deactivate();
  3912. }
  3913. void CArtPlace::setMeAsDest(bool backpackAsVoid /*= true*/)
  3914. {
  3915. ourOwner->commonInfo->dst.setTo(this, backpackAsVoid);
  3916. }
  3917. void CArtPlace::setArtifact(const CArtifactInstance *art)
  3918. {
  3919. baseType = -1; //by default we don't store any component
  3920. ourArt = art;
  3921. if(!art)
  3922. {
  3923. text = std::string();
  3924. hoverText = CGI->generaltexth->allTexts[507];
  3925. }
  3926. else
  3927. {
  3928. text = ourArt->artType->Description();
  3929. if(art->artType->id == 1) //spell scroll
  3930. {
  3931. // we expect scroll description to be like this: This scroll contains the [spell name] spell which is added into your spell book for as long as you carry the scroll.
  3932. // so we want to replace text in [...] with a spell name
  3933. // however other language versions don't have name placeholder at all, so we have to be careful
  3934. int spellID = art->getGivenSpellID();
  3935. int nameStart = text.find_first_of('[');
  3936. int nameEnd = text.find_first_of(']', nameStart);
  3937. if(spellID >= 0)
  3938. {
  3939. if(nameStart != std::string::npos && nameEnd != std::string::npos)
  3940. text = text.replace(nameStart, nameEnd - nameStart + 1, CGI->spellh->spells[spellID]->name);
  3941. //add spell component info (used to provide a pic in r-click popup)
  3942. baseType = CComponent::spell;
  3943. type = spellID;
  3944. bonusValue = 0;
  3945. }
  3946. }
  3947. if (locked) // Locks should appear as empty.
  3948. hoverText = CGI->generaltexth->allTexts[507];
  3949. else
  3950. hoverText = boost::str(boost::format(CGI->generaltexth->heroscrn[1]) % ourArt->artType->Name());
  3951. }
  3952. }
  3953. void LRClickableAreaWTextComp::clickLeft(tribool down, bool previousState)
  3954. {
  3955. if((!down) && previousState)
  3956. {
  3957. std::vector<CComponent*> comp(1, createComponent());
  3958. LOCPLINT->showInfoDialog(text, comp);
  3959. }
  3960. }
  3961. LRClickableAreaWTextComp::LRClickableAreaWTextComp(const Rect &Pos, int BaseType)
  3962. : LRClickableAreaWText(Pos), baseType(BaseType), bonusValue(-1)
  3963. {
  3964. }
  3965. CComponent * LRClickableAreaWTextComp::createComponent() const
  3966. {
  3967. if(baseType >= 0)
  3968. return new CComponent(CComponent::Etype(baseType), type, bonusValue);
  3969. else
  3970. return NULL;
  3971. }
  3972. void LRClickableAreaWTextComp::clickRight(tribool down, bool previousState)
  3973. {
  3974. if(down)
  3975. {
  3976. if(CComponent *comp = createComponent())
  3977. {
  3978. CRClickPopup::createAndPush(text, CInfoWindow::TCompsInfo(1, comp));
  3979. return;
  3980. }
  3981. }
  3982. LRClickableAreaWText::clickRight(down, previousState); //only if with-component variant not occurred
  3983. }
  3984. CHeroArea::CHeroArea(int x, int y, const CGHeroInstance * _hero):hero(_hero)
  3985. {
  3986. used = LCLICK | RCLICK | HOVER;
  3987. pos.x += x; pos.w = 58;
  3988. pos.y += y; pos.h = 64;
  3989. }
  3990. void CHeroArea::clickLeft(tribool down, bool previousState)
  3991. {
  3992. if((!down) && previousState && hero)
  3993. LOCPLINT->openHeroWindow(hero);
  3994. }
  3995. void CHeroArea::clickRight(tribool down, bool previousState)
  3996. {
  3997. if((!down) && previousState && hero)
  3998. LOCPLINT->openHeroWindow(hero);
  3999. }
  4000. void CHeroArea::hover(bool on)
  4001. {
  4002. if (on && hero)
  4003. GH.statusbar->print(hero->hoverName);
  4004. else
  4005. GH.statusbar->clear();
  4006. }
  4007. void CHeroArea::showAll(SDL_Surface * to)
  4008. {
  4009. if (hero)
  4010. blitAtLoc(graphics->portraitLarge[hero->portrait],0,0,to);
  4011. }
  4012. void LRClickableAreaOpenTown::clickLeft(tribool down, bool previousState)
  4013. {
  4014. if((!down) && previousState && town)
  4015. {
  4016. LOCPLINT->openTownWindow(town);
  4017. if ( type == 2 )
  4018. LOCPLINT->castleInt->builds->buildingClicked(10);
  4019. else if ( type == 3 && town->fortLevel() )
  4020. LOCPLINT->castleInt->builds->buildingClicked(7);
  4021. }
  4022. }
  4023. void LRClickableAreaOpenTown::clickRight(tribool down, bool previousState)
  4024. {
  4025. if((!down) && previousState && town)
  4026. LOCPLINT->openTownWindow(town);//TODO: popup?
  4027. }
  4028. LRClickableAreaOpenTown::LRClickableAreaOpenTown()
  4029. : LRClickableAreaWTextComp(Rect(0,0,0,0), -1)
  4030. {
  4031. }
  4032. void CArtifactsOfHero::SCommonPart::reset()
  4033. {
  4034. src.clear();
  4035. dst.clear();
  4036. CCS->curh->dragAndDropCursor(NULL);
  4037. }
  4038. void CArtifactsOfHero::setHero(const CGHeroInstance * hero)
  4039. {
  4040. // // An update is made, rather than initialization.
  4041. // if (curHero && curHero->id == hero->id)
  4042. // {
  4043. // if(curHero != hero)
  4044. // {
  4045. // //delete curHero;
  4046. // curHero = hero; //was: creating a copy
  4047. // }
  4048. //
  4049. // // Compensate backpack pos if an artifact was insertad before it.
  4050. // if (commonInfo->dst.slotID >= 19 && commonInfo->destAOH == this
  4051. // && commonInfo->dst.slotID - 19 < backpackPos)
  4052. // {
  4053. // backpackPos++;
  4054. // }
  4055. //
  4056. // if (updateState && commonInfo->srcAOH == this)
  4057. // {
  4058. // // A swap was made, make the replaced artifact the current selected.
  4059. // if (commonInfo->dst.slotID < 19 && commonInfo->destArtifact)
  4060. // {
  4061. // // // Temporarily remove artifact from hero.
  4062. // // if (commonInfo->srcSlotID < 19)
  4063. // // CGI->arth->unequipArtifact(curHero->artifWorn, commonInfo->srcSlotID);
  4064. // // else
  4065. // // curHero->artifacts.erase(curHero->artifacts.begin() + (commonInfo->srcSlotID - 19));
  4066. //
  4067. // updateParentWindow(); //TODO: evil! but does the thing
  4068. //
  4069. // // Source <- Dest
  4070. // commonInfo->srcArtifact = commonInfo->destArtifact;
  4071. //
  4072. // // Reset destination parameters.
  4073. // commonInfo->dst.clear();
  4074. //
  4075. // CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[commonInfo->srcArtifact->id].bitmap);
  4076. // markPossibleSlots(commonInfo->srcArtifact);
  4077. // }
  4078. // else if (commonInfo->destAOH != NULL)
  4079. // {
  4080. // // Reset all parameters.
  4081. // commonInfo->reset();
  4082. // unmarkSlots();
  4083. // }
  4084. // }
  4085. // }
  4086. // else
  4087. // {
  4088. // commonInfo->reset();
  4089. // }
  4090. //
  4091. // if(hero != curHero)
  4092. // {
  4093. // // delete curHero;
  4094. // // curHero = new CGHeroInstance(*hero);
  4095. // curHero = hero; //was: creating a copy
  4096. // }
  4097. curHero = hero;
  4098. if (curHero->artifactsInBackpack.size() > 0)
  4099. backpackPos %= curHero->artifactsInBackpack.size();
  4100. else
  4101. backpackPos = 0;
  4102. // Fill the slots for worn artifacts and backpack.
  4103. for (int g = 0; g < artWorn.size() ; g++)
  4104. setSlotData(artWorn[g], g);
  4105. scrollBackpack(0);
  4106. }
  4107. void CArtifactsOfHero::dispose()
  4108. {
  4109. //vstd::clear_pointer(curHero);
  4110. //unmarkSlots(false);
  4111. CCS->curh->dragAndDropCursor(NULL);
  4112. }
  4113. void CArtifactsOfHero::scrollBackpack(int dir)
  4114. {
  4115. int artsInBackpack = curHero->artifactsInBackpack.size();
  4116. backpackPos += dir;
  4117. if(backpackPos < 0)// No guarantee of modulus behavior with negative operands -> we keep it positive
  4118. backpackPos += artsInBackpack;
  4119. if(artsInBackpack)
  4120. backpackPos %= artsInBackpack;
  4121. std::multiset<const CArtifactInstance *> toOmit = artifactsOnAltar;
  4122. if(commonInfo->src.art) //if we picked an art from backapck, its slot has to be omitted
  4123. toOmit.insert(commonInfo->src.art);
  4124. int omitedSoFar = 0;
  4125. //set new data
  4126. size_t s = 0;
  4127. for( ; s < artsInBackpack; ++s)
  4128. {
  4129. if (s < artsInBackpack)
  4130. {
  4131. int slotID = 19 + (s + backpackPos)%artsInBackpack;
  4132. const CArtifactInstance *art = curHero->getArt(slotID);
  4133. assert(art);
  4134. if(!vstd::contains(toOmit, art))
  4135. {
  4136. if(s - omitedSoFar < backpack.size())
  4137. setSlotData(backpack[s-omitedSoFar], slotID);
  4138. }
  4139. else
  4140. {
  4141. toOmit -= art;
  4142. omitedSoFar++;
  4143. continue;
  4144. }
  4145. }
  4146. }
  4147. for( ; s - omitedSoFar < backpack.size(); s++)
  4148. eraseSlotData(backpack[s-omitedSoFar], 19 + s);
  4149. //in artifact merchant selling artifacts we may have highlight on one of backpack artifacts -> market needs update, cause artifact under highlight changed
  4150. if(highlightModeCallback)
  4151. {
  4152. for(int i = 0; i < backpack.size(); i++)
  4153. {
  4154. if(backpack[i]->marked)
  4155. {
  4156. highlightModeCallback(backpack[i]);
  4157. break;
  4158. }
  4159. }
  4160. }
  4161. //blocking scrolling if there is not enough artifacts to scroll
  4162. bool scrollingPossible = artsInBackpack - omitedSoFar > backpack.size();
  4163. leftArtRoll->block(!scrollingPossible);
  4164. rightArtRoll->block(!scrollingPossible);
  4165. safeRedraw();
  4166. }
  4167. /**
  4168. * Marks possible slots where a given artifact can be placed, except backpack.
  4169. *
  4170. * @param art Artifact checked against.
  4171. */
  4172. void CArtifactsOfHero::markPossibleSlots(const CArtifactInstance* art)
  4173. {
  4174. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4175. BOOST_FOREACH(CArtPlace *place, aoh->artWorn)
  4176. place->marked = art->canBePutAt(ArtifactLocation(aoh->curHero, place->slotID), true);
  4177. safeRedraw();
  4178. }
  4179. /**
  4180. * Unamarks all slots.
  4181. */
  4182. void CArtifactsOfHero::unmarkSlots(bool withRedraw /*= true*/)
  4183. {
  4184. if(commonInfo)
  4185. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4186. aoh->unmarkLocalSlots(false);
  4187. else
  4188. unmarkLocalSlots(false);\
  4189. if(withRedraw)
  4190. safeRedraw();
  4191. }
  4192. void CArtifactsOfHero::unmarkLocalSlots(bool withRedraw /*= true*/)
  4193. {
  4194. BOOST_FOREACH(CArtPlace *place, artWorn)
  4195. place->marked = false;
  4196. BOOST_FOREACH(CArtPlace *place, backpack)
  4197. place->marked = false;
  4198. if(withRedraw)
  4199. safeRedraw();
  4200. }
  4201. /**
  4202. * Assigns an artifacts to an artifact place depending on it's new slot ID.
  4203. */
  4204. void CArtifactsOfHero::setSlotData(CArtPlace* artPlace, int slotID)
  4205. {
  4206. if(!artPlace && slotID >= GameConstants::BACKPACK_START) //spurious call from artifactMoved in attempt to update hidden backpack slot
  4207. {
  4208. return;
  4209. }
  4210. artPlace->picked = false;
  4211. artPlace->slotID = slotID;
  4212. if(const ArtSlotInfo *asi = curHero->getSlot(slotID))
  4213. {
  4214. artPlace->setArtifact(asi->artifact);
  4215. artPlace->locked = asi->locked;
  4216. }
  4217. else
  4218. artPlace->setArtifact(NULL);
  4219. }
  4220. /**
  4221. * Makes given artifact slot appear as empty with a certain slot ID.
  4222. */
  4223. void CArtifactsOfHero::eraseSlotData (CArtPlace* artPlace, int slotID)
  4224. {
  4225. artPlace->picked = false;
  4226. artPlace->slotID = slotID;
  4227. artPlace->setArtifact(NULL);
  4228. }
  4229. CArtifactsOfHero::CArtifactsOfHero(std::vector<CArtPlace *> ArtWorn, std::vector<CArtPlace *> Backpack,
  4230. CAdventureMapButton *leftScroll, CAdventureMapButton *rightScroll, bool createCommonPart):
  4231. curHero(NULL),
  4232. artWorn(ArtWorn), backpack(Backpack),
  4233. backpackPos(0), commonInfo(NULL), updateState(false),
  4234. leftArtRoll(leftScroll), rightArtRoll(rightScroll),
  4235. allowedAssembling(true), highlightModeCallback(0)
  4236. {
  4237. if(createCommonPart)
  4238. {
  4239. commonInfo = new CArtifactsOfHero::SCommonPart;
  4240. commonInfo->participants.insert(this);
  4241. }
  4242. // Init slots for worn artifacts.
  4243. for (size_t g = 0; g < artWorn.size() ; g++)
  4244. {
  4245. artWorn[g]->ourOwner = this;
  4246. eraseSlotData(artWorn[g], g);
  4247. }
  4248. // Init slots for the backpack.
  4249. for(size_t s=0; s<backpack.size(); ++s)
  4250. {
  4251. backpack[s]->ourOwner = this;
  4252. eraseSlotData(backpack[s], 19 + s);
  4253. }
  4254. leftArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1);
  4255. rightArtRoll->callback += boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1);
  4256. }
  4257. CArtifactsOfHero::CArtifactsOfHero(const Point& position, bool createCommonPart /*= false*/)
  4258. : curHero(NULL), backpackPos(0), commonInfo(NULL), updateState(false), allowedAssembling(true), highlightModeCallback(0)
  4259. {
  4260. if(createCommonPart)
  4261. {
  4262. commonInfo = new CArtifactsOfHero::SCommonPart;
  4263. commonInfo->participants.insert(this);
  4264. }
  4265. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4266. pos += position;
  4267. artWorn.resize(19);
  4268. std::vector<Rect> slotPos;
  4269. slotPos += genRect(44,44,509,30), genRect(44,44,567,240), genRect(44,44,509,80),
  4270. genRect(44,44,383,68), genRect(44,44,564,183), genRect(44,44,509,130),
  4271. genRect(44,44,431,68), genRect(44,44,610,183), genRect(44,44,515,295),
  4272. genRect(44,44,383,143), genRect(44,44,399,194), genRect(44,44,415,245),
  4273. genRect(44,44,431,296), genRect(44,44,564,30), genRect(44,44,610,30),
  4274. genRect(44,44,610,76), genRect(44,44,610,122), genRect(44,44,610,310),
  4275. genRect(44,44,381,296);
  4276. // Create slots for worn artifacts.
  4277. for (size_t g = 0; g < 19 ; g++)
  4278. {
  4279. artWorn[g] = new CArtPlace(NULL);
  4280. artWorn[g]->pos = slotPos[g] + pos;
  4281. artWorn[g]->ourOwner = this;
  4282. eraseSlotData(artWorn[g], g);
  4283. }
  4284. // Create slots for the backpack.
  4285. for(size_t s=0; s<5; ++s)
  4286. {
  4287. CArtPlace * add = new CArtPlace(NULL);
  4288. add->ourOwner = this;
  4289. add->pos.x = pos.x + 403 + 46*s;
  4290. add->pos.y = pos.y + 365;
  4291. add->pos.h = add->pos.w = 44;
  4292. eraseSlotData(add, 19 + s);
  4293. backpack.push_back(add);
  4294. }
  4295. leftArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,-1), 379, 364, "hsbtns3.def", SDLK_LEFT);
  4296. rightArtRoll = new CAdventureMapButton(std::string(), std::string(), boost::bind(&CArtifactsOfHero::scrollBackpack,this,+1), 632, 364, "hsbtns5.def", SDLK_RIGHT);
  4297. }
  4298. CArtifactsOfHero::~CArtifactsOfHero()
  4299. {
  4300. dispose();
  4301. }
  4302. void CArtifactsOfHero::updateParentWindow()
  4303. {
  4304. if (CHeroWindow* chw = dynamic_cast<CHeroWindow*>(GH.topInt()))
  4305. {
  4306. if(updateState)
  4307. chw->curHero = curHero;
  4308. else
  4309. chw->update(curHero, true);
  4310. }
  4311. else if(CExchangeWindow* cew = dynamic_cast<CExchangeWindow*>(GH.topInt()))
  4312. {
  4313. //use our copy of hero to draw window
  4314. if(cew->heroInst[0]->id == curHero->id)
  4315. cew->heroInst[0] = curHero;
  4316. else
  4317. cew->heroInst[1] = curHero;
  4318. if(!updateState)
  4319. {
  4320. cew->deactivate();
  4321. // for(int g=0; g<ARRAY_COUNT(cew->heroInst); ++g)
  4322. // {
  4323. // if(cew->heroInst[g] == curHero)
  4324. // {
  4325. // cew->artifs[g]->setHero(curHero);
  4326. // }
  4327. // }
  4328. cew->prepareBackground();
  4329. cew->redraw();
  4330. cew->activate();
  4331. }
  4332. }
  4333. }
  4334. void CArtifactsOfHero::safeRedraw()
  4335. {
  4336. if (active)
  4337. {
  4338. if(parent)
  4339. parent->redraw();
  4340. else
  4341. redraw();
  4342. }
  4343. }
  4344. void CArtifactsOfHero::realizeCurrentTransaction()
  4345. {
  4346. assert(commonInfo->src.AOH);
  4347. assert(commonInfo->dst.AOH);
  4348. LOCPLINT->cb->swapArtifacts(commonInfo->src.AOH->curHero, commonInfo->src.slotID,
  4349. commonInfo->dst.AOH->curHero, commonInfo->dst.slotID);
  4350. }
  4351. void CArtifactsOfHero::artifactMoved(const ArtifactLocation &src, const ArtifactLocation &dst)
  4352. {
  4353. if(src.hero == curHero && src.slot >= GameConstants::BACKPACK_START)
  4354. updateSlot(src.slot);
  4355. if(dst.hero == curHero && dst.slot >= GameConstants::BACKPACK_START)
  4356. updateSlot(dst.slot);
  4357. if(src.hero == curHero || dst.hero == curHero) //we need to update all slots, artifact might be combined and affect more slots
  4358. updateWornSlots(false);
  4359. if (src.hero != curHero && dst.hero != curHero)
  4360. return;
  4361. if(commonInfo->src == src) //artifact was taken from us
  4362. {
  4363. assert(commonInfo->dst == dst || dst.slot == dst.hero->artifactsInBackpack.size() + GameConstants::BACKPACK_START);
  4364. commonInfo->reset();
  4365. unmarkSlots();
  4366. }
  4367. else if(commonInfo->dst == src) //the dest artifact was moved -> we are picking it
  4368. {
  4369. assert(dst.slot >= GameConstants::BACKPACK_START);
  4370. commonInfo->reset();
  4371. CArtPlace *ap = NULL;
  4372. BOOST_FOREACH(CArtifactsOfHero *aoh, commonInfo->participants)
  4373. {
  4374. if(aoh->curHero == dst.hero)
  4375. {
  4376. commonInfo->src.AOH = aoh;
  4377. if((ap = aoh->getArtPlace(dst.slot)))
  4378. break;
  4379. }
  4380. }
  4381. if(ap)
  4382. {
  4383. ap->select();
  4384. }
  4385. else
  4386. {
  4387. commonInfo->src.art = dst.getArt();
  4388. commonInfo->src.slotID = dst.slot;
  4389. assert(commonInfo->src.AOH);
  4390. CCS->curh->dragAndDropCursor(graphics->artDefs->ourImages[dst.getArt()->artType->id].bitmap);
  4391. markPossibleSlots(dst.getArt());
  4392. }
  4393. }
  4394. else if(src.slot >= GameConstants::BACKPACK_START &&
  4395. src.slot < commonInfo->src.slotID &&
  4396. src.hero == commonInfo->src.AOH->curHero) //artifact taken from before currently picked one
  4397. {
  4398. //int fixedSlot = src.hero->getArtPos(commonInfo->src.art);
  4399. commonInfo->src.slotID--;
  4400. assert(commonInfo->src.valid());
  4401. }
  4402. else
  4403. {
  4404. //when moving one artifact onto another it leads to two art movements: dst->backapck; src->dst
  4405. // however after first movement we pick the art from backpack and the second movement coming when
  4406. // we have a different artifact may look surprising... but it's valid.
  4407. //tlog1 << "Unexpected artifact movement...\n";
  4408. }
  4409. updateParentWindow();
  4410. int shift = 0;
  4411. // if(dst.slot >= Arts::BACKPACK_START && dst.slot - Arts::BACKPACK_START < backpackPos)
  4412. // shift++;
  4413. //
  4414. if(src.slot < GameConstants::BACKPACK_START && dst.slot - GameConstants::BACKPACK_START < backpackPos)
  4415. shift++;
  4416. if(dst.slot < GameConstants::BACKPACK_START && src.slot - GameConstants::BACKPACK_START < backpackPos)
  4417. shift--;
  4418. if( (src.hero == curHero && src.slot >= GameConstants::BACKPACK_START)
  4419. || (dst.hero == curHero && dst.slot >= GameConstants::BACKPACK_START) )
  4420. scrollBackpack(shift); //update backpack slots
  4421. }
  4422. void CArtifactsOfHero::artifactRemoved(const ArtifactLocation &al)
  4423. {
  4424. if(al.hero == curHero)
  4425. {
  4426. if(al.slot < GameConstants::BACKPACK_START)
  4427. updateWornSlots(0);
  4428. else
  4429. scrollBackpack(0); //update backpack slots
  4430. }
  4431. }
  4432. CArtPlace * CArtifactsOfHero::getArtPlace(int slot)
  4433. {
  4434. if(slot < GameConstants::BACKPACK_START)
  4435. {
  4436. return artWorn[slot];
  4437. }
  4438. else
  4439. {
  4440. BOOST_FOREACH(CArtPlace *ap, backpack)
  4441. if(ap->slotID == slot)
  4442. return ap;
  4443. }
  4444. return NULL;
  4445. }
  4446. void CArtifactsOfHero::artifactAssembled(const ArtifactLocation &al)
  4447. {
  4448. if(al.hero == curHero)
  4449. updateWornSlots();
  4450. }
  4451. void CArtifactsOfHero::artifactDisassembled(const ArtifactLocation &al)
  4452. {
  4453. if(al.hero == curHero)
  4454. updateWornSlots();
  4455. }
  4456. void CArtifactsOfHero::updateWornSlots(bool redrawParent /*= true*/)
  4457. {
  4458. for(int i = 0; i < artWorn.size(); i++)
  4459. updateSlot(i);
  4460. if(redrawParent)
  4461. updateParentWindow();
  4462. }
  4463. const CGHeroInstance * CArtifactsOfHero::getHero() const
  4464. {
  4465. return curHero;
  4466. }
  4467. void CArtifactsOfHero::updateSlot(int slotID)
  4468. {
  4469. setSlotData(getArtPlace(slotID), slotID);
  4470. }
  4471. void CExchangeWindow::close()
  4472. {
  4473. GH.popIntTotally(this);
  4474. }
  4475. void CExchangeWindow::showAll(SDL_Surface * to)
  4476. {
  4477. blitAt(bg, pos, to);
  4478. CIntObject::showAll(to);
  4479. //printing border around window
  4480. if(screen->w != 800 || screen->h !=600)
  4481. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4482. }
  4483. void CExchangeWindow::questlog(int whichHero)
  4484. {
  4485. CCS->curh->dragAndDropCursor(NULL);
  4486. }
  4487. void CExchangeWindow::prepareBackground()
  4488. {
  4489. if(bg)
  4490. SDL_FreeSurface(bg);
  4491. SDL_Surface * bgtemp; //loaded as 8bpp surface
  4492. bgtemp = BitmapHandler::loadBitmap("TRADE2.BMP");
  4493. graphics->blueToPlayersAdv(bgtemp, heroInst[0]->tempOwner);
  4494. bg = SDL_ConvertSurface(bgtemp, screen->format, screen->flags); //to 24 bpp
  4495. SDL_FreeSurface(bgtemp);
  4496. //printing heroes' names and levels
  4497. std::ostringstream os, os2;
  4498. os<<heroInst[0]->name<<", Level "<<heroInst[0]->level<<" "<<heroInst[0]->type->heroClass->name;
  4499. CSDL_Ext::printAtMiddle(os.str(), 147, 25, FONT_SMALL, Colors::Cornsilk, bg);
  4500. os2<<heroInst[1]->name<<", Level "<<heroInst[1]->level<<" "<<heroInst[1]->type->heroClass->name;
  4501. CSDL_Ext::printAtMiddle(os2.str(), 653, 25, FONT_SMALL, Colors::Cornsilk, bg);
  4502. //printing primary skills
  4503. CDefHandler * skilldef = CDefHandler::giveDef("PSKIL32.DEF");
  4504. for(int g=0; g<4; ++g)
  4505. {
  4506. //graphics
  4507. blitAt(skilldef->ourImages[g].bitmap, genRect(32, 32, 385, 19 + 36 * g), bg);
  4508. }
  4509. CDefHandler * un32 = CDefHandler::giveDef("UN32.DEF");
  4510. //heroes related thing
  4511. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4512. {
  4513. CHeroWithMaybePickedArtifact heroWArt = CHeroWithMaybePickedArtifact(this, heroInst[b]);
  4514. //printing primary skills' amounts
  4515. for(int m=0; m<4; ++m)
  4516. {
  4517. std::ostringstream primarySkill;
  4518. primarySkill << heroWArt.getPrimSkillLevel(m);
  4519. CSDL_Ext::printAtMiddle(primarySkill.str(), 352 + 93 * b, 35 + 36 * m, FONT_SMALL, Colors::Cornsilk, bg);
  4520. }
  4521. //printing secondary skills
  4522. for(int m=0; m<heroInst[b]->secSkills.size(); ++m)
  4523. {
  4524. blitAt(graphics->abils32->ourImages[heroInst[b]->secSkills[m].first * 3 + heroInst[b]->secSkills[m].second + 2].bitmap, genRect(32, 32, 32 + 36 * m + 454 * b, 88), bg);
  4525. }
  4526. //hero's specialty
  4527. blitAt(un32->ourImages[heroInst[b]->subID].bitmap, 67 + 490*b, 45, bg);
  4528. //experience
  4529. blitAt(skilldef->ourImages[4].bitmap, 103 + 490*b, 45, bg);
  4530. printAtMiddle( makeNumberShort(heroInst[b]->exp), 119 + 490*b, 71, FONT_SMALL, Colors::Cornsilk, bg );
  4531. //mana points
  4532. blitAt(skilldef->ourImages[5].bitmap, 139 + 490*b, 45, bg);
  4533. printAtMiddle( makeNumberShort(heroInst[b]->mana), 155 + 490*b, 71, FONT_SMALL, Colors::Cornsilk, bg );
  4534. //setting morale
  4535. blitAt(graphics->morale30->ourImages[heroWArt.MoraleVal()+3].bitmap, 177 + 490*b, 45, bg);
  4536. //setting luck
  4537. blitAt(graphics->luck30->ourImages[heroWArt.LuckVal()+3].bitmap, 213 + 490*b, 45, bg);
  4538. }
  4539. //printing portraits
  4540. blitAt(graphics->portraitLarge[heroInst[0]->portrait], 257, 13, bg);
  4541. blitAt(graphics->portraitLarge[heroInst[1]->portrait], 485, 13, bg);
  4542. delete un32;
  4543. delete skilldef;
  4544. }
  4545. CExchangeWindow::CExchangeWindow(si32 hero1, si32 hero2) : bg(NULL)
  4546. {
  4547. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4548. char bufor[400];
  4549. heroInst[0] = LOCPLINT->cb->getHero(hero1);
  4550. heroInst[1] = LOCPLINT->cb->getHero(hero2);
  4551. prepareBackground();
  4552. pos.x = screen->w/2 - bg->w/2;
  4553. pos.y = screen->h/2 - bg->h/2;
  4554. pos.w = screen->w;
  4555. pos.h = screen->h;
  4556. artifs[0] = new CArtifactsOfHero(Point(pos.x + -334, pos.y + 150));
  4557. artifs[0]->commonInfo = new CArtifactsOfHero::SCommonPart;
  4558. artifs[0]->commonInfo->participants.insert(artifs[0]);
  4559. artifs[0]->setHero(heroInst[0]);
  4560. artifs[1] = new CArtifactsOfHero(Point(pos.x + 96, pos.y + 150));
  4561. artifs[1]->commonInfo = artifs[0]->commonInfo;
  4562. artifs[1]->commonInfo->participants.insert(artifs[1]);
  4563. artifs[1]->setHero(heroInst[1]);
  4564. artSets.push_back(artifs[0]);
  4565. artSets.push_back(artifs[1]);
  4566. //primary skills
  4567. for(int g=0; g<4; ++g)
  4568. {
  4569. //primary skill's clickable areas
  4570. primSkillAreas.push_back(new LRClickableAreaWTextComp());
  4571. primSkillAreas[g]->pos = genRect(32, 140, pos.x+329, pos.y + 19 + 36 * g);
  4572. primSkillAreas[g]->text = CGI->generaltexth->arraytxt[2+g];
  4573. primSkillAreas[g]->type = g;
  4574. primSkillAreas[g]->bonusValue = -1;
  4575. primSkillAreas[g]->baseType = 0;
  4576. sprintf(bufor, CGI->generaltexth->heroscrn[1].c_str(), CGI->generaltexth->primarySkillNames[g].c_str());
  4577. primSkillAreas[g]->hoverText = std::string(bufor);
  4578. }
  4579. //heroes related thing
  4580. for(int b=0; b<ARRAY_COUNT(heroInst); b++)
  4581. {
  4582. //secondary skill's clickable areas
  4583. for(int g=0; g<heroInst[b]->secSkills.size(); ++g)
  4584. {
  4585. int skill = heroInst[b]->secSkills[g].first,
  4586. level = heroInst[b]->secSkills[g].second; // <1, 3>
  4587. secSkillAreas[b].push_back(new LRClickableAreaWTextComp());
  4588. secSkillAreas[b][g]->pos = genRect(32, 32, pos.x + 32 + g*36 + b*454 , pos.y + 88);
  4589. secSkillAreas[b][g]->baseType = 1;
  4590. secSkillAreas[b][g]->type = skill;
  4591. secSkillAreas[b][g]->bonusValue = level;
  4592. secSkillAreas[b][g]->text = CGI->generaltexth->skillInfoTexts[skill][level-1];
  4593. sprintf(bufor, CGI->generaltexth->heroscrn[21].c_str(), CGI->generaltexth->levels[level - 1].c_str(), CGI->generaltexth->skillName[skill].c_str());
  4594. secSkillAreas[b][g]->hoverText = std::string(bufor);
  4595. }
  4596. portrait[b] = new CHeroArea(pos.x + 257 + 228*b, pos.y + 13, heroInst[b]);
  4597. speciality[b] = new LRClickableAreaWText();
  4598. speciality[b]->pos = genRect(32, 32, pos.x + 69 + 490*b, pos.y + 45);
  4599. speciality[b]->hoverText = CGI->generaltexth->heroscrn[27];
  4600. speciality[b]->text = CGI->generaltexth->hTxts[heroInst[b]->subID].longBonus;
  4601. experience[b] = new LRClickableAreaWText();
  4602. experience[b]->pos = genRect(32, 32, pos.x + 105 + 490*b, pos.y + 45);
  4603. experience[b]->hoverText = CGI->generaltexth->heroscrn[9];
  4604. experience[b]->text = CGI->generaltexth->allTexts[2].c_str();
  4605. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->level));
  4606. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(CGI->heroh->reqExp(heroInst[b]->level+1)));
  4607. boost::replace_first(experience[b]->text, "%d", boost::lexical_cast<std::string>(heroInst[b]->exp));
  4608. spellPoints[b] = new LRClickableAreaWText();
  4609. spellPoints[b]->pos = genRect(32, 32, pos.x + 141 + 490*b, pos.y + 45);
  4610. spellPoints[b]->hoverText = CGI->generaltexth->heroscrn[22];
  4611. sprintf(bufor, CGI->generaltexth->allTexts[205].c_str(), heroInst[b]->name.c_str(), heroInst[b]->mana, heroInst[b]->manaLimit());
  4612. spellPoints[b]->text = std::string(bufor);
  4613. //setting morale
  4614. morale[b] = new MoraleLuckBox(true, genRect(32, 32, pos.x + 177 + 490*b, pos.y + 45));
  4615. morale[b]->set(heroInst[b]);
  4616. //setting luck
  4617. luck[b] = new MoraleLuckBox(false, genRect(32, 32, pos.x + 213 + 490*b, pos.y + 45));
  4618. luck[b]->set(heroInst[b]);
  4619. }
  4620. //buttons
  4621. quit = new CAdventureMapButton(CGI->generaltexth->tcommands[8], "", boost::bind(&CExchangeWindow::close, this), pos.x+732, pos.y+567, "IOKAY.DEF", SDLK_RETURN);
  4622. questlogButton[0] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 0), pos.x+10, pos.y+44, "hsbtns4.def");
  4623. questlogButton[1] = new CAdventureMapButton(CGI->generaltexth->heroscrn[0], std::string(), boost::bind(&CExchangeWindow::questlog,this, 1), pos.x+740, pos.y+44, "hsbtns4.def");
  4624. //statusbar
  4625. //FIXME - this image is a bit bigger than required - part of background should be used instead
  4626. ourBar = new CGStatusBar(pos.x + 3, pos.y + 577, "KSTATBAR");
  4627. //garrison interface
  4628. garr = new CGarrisonInt(pos.x + 69, pos.y + 131, 4, Point(418,0), bg, Point(69,131), heroInst[0],heroInst[1], true, true);
  4629. {
  4630. BLOCK_CAPTURING;
  4631. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+10,pos.y+132,"TSBTNS.DEF"));
  4632. garr->addSplitBtn(new CAdventureMapButton(CGI->generaltexth->tcommands[3],"",boost::bind(&CGarrisonInt::splitClick,garr),pos.x+740,pos.y+132,"TSBTNS.DEF"));
  4633. }
  4634. }
  4635. CExchangeWindow::~CExchangeWindow() //d-tor
  4636. {
  4637. SDL_FreeSurface(bg);
  4638. delete artifs[0]->commonInfo;
  4639. artifs[0]->commonInfo = NULL;
  4640. artifs[1]->commonInfo = NULL;
  4641. }
  4642. CShipyardWindow::CShipyardWindow(const std::vector<si32> &cost, int state, int boatType, const boost::function<void()> &onBuy)
  4643. {
  4644. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4645. bg = new CPicture("TPSHIP");
  4646. bg->colorize(LOCPLINT->playerID);
  4647. pos = bg->center();
  4648. bgWater = new CPicture("TPSHIPBK", 100, 69);
  4649. std::string boatFilenames[3] = {"AB01_", "AB02_", "AB03_"};
  4650. Point waterCenter = Point(bgWater->pos.x+bgWater->pos.w/2, bgWater->pos.y+bgWater->pos.h/2);
  4651. bgShip = new CAnimImage(boatFilenames[boatType], 0, 7, 120, 96, CShowableAnim::USE_RLE);
  4652. bgShip->center(waterCenter);
  4653. // Create resource icons and costs.
  4654. std::string goldValue = boost::lexical_cast<std::string>(cost[Res::GOLD]);
  4655. std::string woodValue = boost::lexical_cast<std::string>(cost[Res::WOOD]);
  4656. goldCost = new CLabel(118, 294, FONT_SMALL, CENTER, Colors::Cornsilk, goldValue);
  4657. woodCost = new CLabel(212, 294, FONT_SMALL, CENTER, Colors::Cornsilk, woodValue);
  4658. goldPic = new CAnimImage("RESOURCE", Res::GOLD, 0, 100, 244);
  4659. woodPic = new CAnimImage("RESOURCE", Res::WOOD, 0, 196, 244);
  4660. quit = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 224, 312, "ICANCEL", SDLK_RETURN);
  4661. build = new CAdventureMapButton(CGI->generaltexth->allTexts[598], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), 42, 312, "IBUY30", SDLK_RETURN);
  4662. build->callback += onBuy;
  4663. for(size_t i = 0; i < cost.size(); i++)
  4664. {
  4665. if(cost[i] > LOCPLINT->cb->getResourceAmount(i))
  4666. {
  4667. build->block(true);
  4668. break;
  4669. }
  4670. }
  4671. statusBar = new CGStatusBar(bg->pos.w/2, bg->pos.h-16);
  4672. title = new CLabel(164, 27, FONT_BIG, CENTER, Colors::Jasmine, CGI->generaltexth->jktexts[13]);
  4673. costLabel = new CLabel(164, 220, FONT_MEDIUM, CENTER, Colors::Cornsilk, CGI->generaltexth->jktexts[14]);
  4674. }
  4675. CPuzzleWindow::CPuzzleWindow(const int3 &grailPos, double discoveredRatio)
  4676. :animCount(0)
  4677. {
  4678. SDL_Surface * back = BitmapHandler::loadBitmap("PUZZLE.BMP", false);
  4679. graphics->blueToPlayersAdv(back, LOCPLINT->playerID);
  4680. //make transparency black
  4681. back->format->palette->colors[0].b = back->format->palette->colors[0].r = back->format->palette->colors[0].g = 0;
  4682. //the rest
  4683. background = SDL_ConvertSurface(back, screen->format, back->flags);
  4684. SDL_FreeSurface(back);
  4685. pos = genRect(background->h, background->w, (conf.cc.resx - background->w) / 2, (conf.cc.resy - background->h) / 2);
  4686. quitb = new CAdventureMapButton(CGI->generaltexth->allTexts[599], "", boost::bind(&CGuiHandler::popIntTotally, &GH, this), pos.x+670, pos.y+538, "IOK6432.DEF", SDLK_RETURN);
  4687. quitb->assignedKeys.insert(SDLK_ESCAPE);
  4688. quitb->borderColor = Colors::MetallicGold;
  4689. quitb->borderEnabled = true;
  4690. resdatabar = new CResDataBar("ZRESBAR.bmp", pos.x+3, pos.y+575, 32, 2, 85, 85);
  4691. resdatabar->pos.x = pos.x+3; resdatabar->pos.y = pos.y+575;
  4692. //printing necessary things to background
  4693. int3 moveInt = int3(8, 9, 0);
  4694. Rect mapRect = genRect(544, 591, 8, 8);
  4695. CGI->mh->terrainRect
  4696. (grailPos - moveInt, adventureInt->anim,
  4697. &LOCPLINT->cb->getVisibilityMap(), true, adventureInt->heroAnim,
  4698. background, &mapRect, 0, 0, true, moveInt);
  4699. int faction = LOCPLINT->cb->getStartInfo()->playerInfos.find(LOCPLINT->playerID)->second.castle;
  4700. std::vector<SPuzzleInfo> puzzlesToPrint;
  4701. for(int g=0; g<PUZZLES_PER_FACTION; ++g)
  4702. {
  4703. if(CGI->heroh->puzzleInfo[faction][g].whenUncovered > PUZZLES_PER_FACTION * discoveredRatio)
  4704. {
  4705. puzzlesToPrint.push_back(CGI->heroh->puzzleInfo[faction][g]);
  4706. }
  4707. else
  4708. {
  4709. SDL_Surface *buf = BitmapHandler::loadBitmap(CGI->heroh->puzzleInfo[faction][g].filename);
  4710. puzzlesToPullBack.push_back( std::make_pair(buf, &CGI->heroh->puzzleInfo[faction][g]) );
  4711. }
  4712. }
  4713. for(int b = 0; b < puzzlesToPrint.size(); ++b)
  4714. {
  4715. SDL_Surface * puzzle = BitmapHandler::loadBitmap(puzzlesToPrint[b].filename);
  4716. blitAt(puzzle, puzzlesToPrint[b].x, puzzlesToPrint[b].y, background);
  4717. SDL_FreeSurface(puzzle);
  4718. }
  4719. CCS->soundh->playSound(soundBase::OBELISK);
  4720. }
  4721. CPuzzleWindow::~CPuzzleWindow()
  4722. {
  4723. delete quitb;
  4724. delete resdatabar;
  4725. SDL_FreeSurface(background);
  4726. for(int g = 0; g < puzzlesToPullBack.size(); ++g)
  4727. SDL_FreeSurface( puzzlesToPullBack[g].first );
  4728. }
  4729. void CPuzzleWindow::activate()
  4730. {
  4731. quitb->activate();
  4732. }
  4733. void CPuzzleWindow::deactivate()
  4734. {
  4735. quitb->deactivate();
  4736. }
  4737. void CPuzzleWindow::show(SDL_Surface * to)
  4738. {
  4739. blitAt(background, pos.x, pos.y, to);
  4740. quitb->showAll(to);
  4741. resdatabar->draw(to);
  4742. //blitting disappearing puzzles
  4743. for(int g=0; g<2; ++g)
  4744. if(animCount != 255)
  4745. ++animCount;
  4746. if(animCount != 255)
  4747. {
  4748. for(int b = 0; b < puzzlesToPullBack.size(); ++b)
  4749. {
  4750. int xPos = puzzlesToPullBack[b].second->x + pos.x,
  4751. yPos = puzzlesToPullBack[b].second->y + pos.y;
  4752. SDL_Surface *from = puzzlesToPullBack[b].first;
  4753. SDL_SetAlpha(from, SDL_SRCALPHA, 255 - animCount);
  4754. blitAt(from, xPos, yPos, to);
  4755. }
  4756. }
  4757. //disappearing puzzles blitted
  4758. //printing border around window
  4759. if(screen->w != 800 || screen->h !=600)
  4760. CMessage::drawBorder(LOCPLINT->playerID,to,828,628,pos.x-14,pos.y-15);
  4761. }
  4762. void CTransformerWindow::CItem::showAll(SDL_Surface * to)
  4763. {
  4764. SDL_Surface * backgr = graphics->bigImgs[parent->army->getCreature(id)->idNumber];
  4765. blitAt(backgr, pos.x, pos.y, to);
  4766. printAtMiddle(boost::lexical_cast<std::string>(size),pos.x+28, pos.y+76,FONT_SMALL,Colors::Cornsilk,to);//stack size
  4767. }
  4768. void CTransformerWindow::CItem::move()
  4769. {
  4770. if (left)
  4771. pos.x += 289;
  4772. else
  4773. pos.x -= 289;
  4774. left = !left;
  4775. }
  4776. void CTransformerWindow::CItem::clickLeft(tribool down, bool previousState)
  4777. {
  4778. if(previousState && (!down))
  4779. {
  4780. move();
  4781. parent->showAll(screen2);
  4782. }
  4783. }
  4784. CTransformerWindow::CItem::CItem(CTransformerWindow * _parent, int _size, int _id):
  4785. id(_id), size(_size), parent(_parent)
  4786. {
  4787. used = LCLICK;
  4788. left = true;
  4789. pos.w = 58;
  4790. pos.h = 64;
  4791. pos.x += 45 + (id%3)*83 + id/6*83;
  4792. pos.y += 109 + (id/3)*98;
  4793. }
  4794. CTransformerWindow::CItem::~CItem()
  4795. {
  4796. }
  4797. void CTransformerWindow::showAll(SDL_Surface * to)
  4798. {
  4799. CIntObject::showAll(to);
  4800. printAtMiddleLoc( CGI->generaltexth->allTexts[485], 153, 29,FONT_SMALL, Colors::Jasmine,to);//holding area
  4801. printAtMiddleLoc( CGI->generaltexth->allTexts[486], 153+295, 29,FONT_SMALL, Colors::Jasmine,to);//transformer
  4802. printAtMiddleWBLoc(CGI->generaltexth->allTexts[487], 153, 75,FONT_MEDIUM, 32,Colors::Jasmine,to);//move creatures to create skeletons
  4803. printAtMiddleWBLoc(CGI->generaltexth->allTexts[488], 153+295, 75,FONT_MEDIUM, 32,Colors::Jasmine,to);//creatures here will become skeletons
  4804. }
  4805. void CTransformerWindow::makeDeal()
  4806. {
  4807. for (int i=0; i<items.size(); i++)
  4808. if (!items[i]->left)
  4809. LOCPLINT->cb->trade(town, EMarketMode::CREATURE_UNDEAD, items[i]->id, 0, 0, hero);
  4810. }
  4811. void CTransformerWindow::addAll()
  4812. {
  4813. for (int i=0; i<items.size(); i++)
  4814. if (items[i]->left)
  4815. items[i]->move();
  4816. showAll(screen2);
  4817. }
  4818. CTransformerWindow::CTransformerWindow(const CGHeroInstance * _hero, const CGTownInstance * _town):hero(_hero),town(_town)
  4819. {
  4820. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4821. bg = new CPicture ("SKTRNBK.PCX");
  4822. bg->colorizeAndConvert(LOCPLINT->playerID);
  4823. pos = center(bg->pos);
  4824. if (hero)
  4825. army = hero;
  4826. else
  4827. army = town;
  4828. for (int i=0; i<7; i++ )
  4829. if ( army->getCreature(i) )
  4830. items.push_back(new CItem(this, army->getStackCount(i), i));
  4831. all = new CAdventureMapButton(CGI->generaltexth->zelp[590],boost::bind(&CTransformerWindow::addAll,this), 146,416,"ALTARMY.DEF",SDLK_a);
  4832. convert= new CAdventureMapButton(CGI->generaltexth->zelp[591],boost::bind(&CTransformerWindow::makeDeal,this), 269,416,"ALTSACR.DEF",SDLK_RETURN);
  4833. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[592],boost::bind(&CGuiHandler::popIntTotally,&GH, this),392,416,"ICANCEL.DEF",SDLK_ESCAPE);
  4834. bar = new CGStatusBar(304, 469);
  4835. }
  4836. CTransformerWindow::~CTransformerWindow()
  4837. {
  4838. }
  4839. void CUniversityWindow::CItem::clickLeft(tribool down, bool previousState)
  4840. {
  4841. if(previousState && (!down))
  4842. {
  4843. if ( state() != 2 )
  4844. return;
  4845. CUnivConfirmWindow *win = new CUnivConfirmWindow(parent, ID, LOCPLINT->cb->getResourceAmount(6) >= 2000);
  4846. GH.pushInt(win);
  4847. }
  4848. }
  4849. void CUniversityWindow::CItem::clickRight(tribool down, bool previousState)
  4850. {
  4851. if(down)
  4852. {
  4853. CInfoPopup *message = new CInfoPopup();
  4854. message->free = true;
  4855. message->bitmap = CMessage::drawBoxTextBitmapSub
  4856. (LOCPLINT->playerID,
  4857. CGI->generaltexth->skillInfoTexts[ID][0],
  4858. graphics->abils82->ourImages[ID*3+3].bitmap,
  4859. CGI->generaltexth->skillName[ID]);
  4860. message->pos.x = screen->w/2 - message->bitmap->w/2;
  4861. message->pos.y = screen->h/2 - message->bitmap->h/2;
  4862. GH.pushInt(message);
  4863. }
  4864. }
  4865. void CUniversityWindow::CItem::hover(bool on)
  4866. {
  4867. if (on)
  4868. GH.statusbar->print(CGI->generaltexth->skillName[ID]);
  4869. else
  4870. GH.statusbar->clear();
  4871. }
  4872. int CUniversityWindow::CItem::state()
  4873. {
  4874. if (parent->hero->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ID)))//hero know this skill
  4875. return 1;
  4876. if (parent->hero->secSkills.size() >= GameConstants::SKILL_PER_HERO)//can't learn more skills
  4877. return 0;
  4878. if (parent->hero->type->heroClass->proSec[ID]==0)//can't learn this skill (like necromancy for most of non-necros)
  4879. return 0;
  4880. /* if (LOCPLINT->cb->getResourceAmount(6) < 2000 )//no gold - allowed in H3, confirm button is blocked instead
  4881. return 0;*/
  4882. return 2;
  4883. }
  4884. void CUniversityWindow::CItem::showAll(SDL_Surface * to)
  4885. {
  4886. CPicture * bar;
  4887. switch (state())
  4888. {
  4889. case 0: bar = parent->red;
  4890. break;
  4891. case 1: bar = parent->yellow;
  4892. break;
  4893. case 2: bar = parent->green;
  4894. break;
  4895. default:bar = NULL;
  4896. break;
  4897. }
  4898. assert(bar);
  4899. blitAtLoc(bar->bg, -28, -22, to);
  4900. blitAtLoc(bar->bg, -28, 48, to);
  4901. printAtMiddleLoc (CGI->generaltexth->skillName[ID], 22, -13, FONT_SMALL, Colors::Cornsilk,to);//Name
  4902. printAtMiddleLoc (CGI->generaltexth->levels[0], 22, 57, FONT_SMALL, Colors::Cornsilk,to);//Level(always basic)
  4903. CPicture::showAll(to);
  4904. }
  4905. CUniversityWindow::CItem::CItem(CUniversityWindow * _parent, int _ID, int X, int Y):
  4906. CPicture (graphics->abils44->ourImages[_ID*3+3].bitmap,X,Y,false),ID(_ID), parent(_parent)
  4907. {
  4908. used = LCLICK | RCLICK | HOVER;
  4909. }
  4910. CUniversityWindow::CUniversityWindow(const CGHeroInstance * _hero, const IMarket * _market):hero(_hero), market(_market)
  4911. {
  4912. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4913. bg = new CPicture("UNIVERS1.PCX");
  4914. bg->colorizeAndConvert(LOCPLINT->playerID);
  4915. green = new CPicture("UNIVGREN.PCX");
  4916. yellow = new CPicture("UNIVGOLD.PCX");//bars
  4917. red = new CPicture("UNIVRED.PCX");
  4918. green->recActions =
  4919. yellow->recActions =
  4920. red->recActions = DISPOSE;
  4921. if ( market->o->ID == 104 ) // this is adventure map university
  4922. {
  4923. SDL_Surface * titleImage = BitmapHandler::loadBitmap("UNIVBLDG.PCX");
  4924. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  4925. SDL_FreeSurface(titleImage);
  4926. }
  4927. else if (LOCPLINT->castleInt && LOCPLINT->castleInt->town->subID == 8)// this is town university
  4928. {
  4929. SDL_Surface * titleImage = LOCPLINT->castleInt->bicons->ourImages[21].bitmap;
  4930. blitAtLoc(titleImage, 232-titleImage->w/2, 76-titleImage->h/2, bg->bg);
  4931. }
  4932. else
  4933. tlog0<<"Error: Image for university was not found!\n";//This should not happen
  4934. printAtMiddleWBLoc(CGI->generaltexth->allTexts[603], 232, 153, FONT_SMALL, 70,Colors::Cornsilk,bg->bg);//Clerk speech
  4935. printAtMiddleLoc (CGI->generaltexth->allTexts[602], 231, 26 , FONT_MEDIUM ,Colors::Jasmine,bg->bg);//University
  4936. std::vector<int> list = market->availableItemsIds(EMarketMode::RESOURCE_SKILL);
  4937. if (list.size()!=4)
  4938. tlog0<<"\t\tIncorrect size of available items vector!\n";
  4939. for (int i=0; i<list.size(); i++)//prepare clickable items
  4940. items.push_back(new CItem(this, list[i], pos.x+54+i*104, pos.y+234));
  4941. pos = center(bg->pos);
  4942. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[632],
  4943. boost::bind(&CGuiHandler::popIntTotally,&GH, this),200,313,"IOKAY.DEF",SDLK_RETURN);
  4944. bar = new CGStatusBar(232, 371);
  4945. }
  4946. CUniversityWindow::~CUniversityWindow()
  4947. {
  4948. }
  4949. CUnivConfirmWindow::CUnivConfirmWindow(CUniversityWindow * PARENT, int SKILL, bool available ):parent(PARENT)
  4950. {
  4951. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4952. bg = new CPicture("UNIVERS2.PCX");
  4953. bg->colorizeAndConvert(LOCPLINT->playerID);
  4954. std::string text = CGI->generaltexth->allTexts[608];
  4955. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4956. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4957. boost::replace_first(text, "%d", "2000");
  4958. printAtMiddleWBLoc(text, 230, 163, FONT_SMALL, 65,Colors::Cornsilk,bg->bg);//Clerk speech
  4959. printAtMiddleLoc (CGI->generaltexth-> skillName[SKILL], 230, 37, FONT_SMALL, Colors::Cornsilk,bg->bg);//Skill name
  4960. printAtMiddleLoc (CGI->generaltexth->levels[1], 230, 107, FONT_SMALL, Colors::Cornsilk,bg->bg);//Skill level
  4961. printAtMiddleLoc ("2000", 230, 267, FONT_SMALL, Colors::Cornsilk,bg->bg);//Cost
  4962. blitAtLoc(graphics->abils44->ourImages[SKILL*3+3].bitmap, 211, 51, bg->bg);//skill
  4963. blitAtLoc(graphics->resources32->ourImages[6].bitmap, 210, 210, bg->bg);//gold
  4964. pos = center(bg->pos);
  4965. std::string hoverText = CGI->generaltexth->allTexts[609];
  4966. boost::replace_first(hoverText, "%s", CGI->generaltexth->levels[0]+ " " + CGI->generaltexth->skillName[SKILL]);
  4967. text = CGI->generaltexth->zelp[633].second;
  4968. boost::replace_first(text, "%s", CGI->generaltexth->levels[0]);
  4969. boost::replace_first(text, "%s", CGI->generaltexth->skillName[SKILL]);
  4970. boost::replace_first(text, "%d", "2000");
  4971. confirm= new CAdventureMapButton(hoverText, text, boost::bind(&CUnivConfirmWindow::makeDeal, this, SKILL),
  4972. 148,299,"IBY6432.DEF",SDLK_RETURN);
  4973. confirm->block(!available);
  4974. cancel = new CAdventureMapButton(CGI->generaltexth->zelp[631],boost::bind(&CGuiHandler::popIntTotally, &GH, this),
  4975. 252,299,"ICANCEL.DEF",SDLK_ESCAPE);
  4976. bar = new CGStatusBar(232, 371);
  4977. }
  4978. void CUnivConfirmWindow::makeDeal(int skill)
  4979. {
  4980. LOCPLINT->cb->trade(parent->market->o, EMarketMode::RESOURCE_SKILL, 6, skill, 1, parent->hero);
  4981. GH.popIntTotally(this);
  4982. }
  4983. CHillFortWindow::CHillFortWindow(const CGHeroInstance *visitor, const CGObjectInstance *object):
  4984. fort(object),hero(visitor)
  4985. {
  4986. OBJ_CONSTRUCTION_CAPTURING_ALL;
  4987. slotsCount=7;
  4988. resources = CDefHandler::giveDefEss("SMALRES.DEF");
  4989. bg = new CPicture("APHLFTBK.PCX");
  4990. bg->colorizeAndConvert(LOCPLINT->playerID);
  4991. printAtMiddleLoc (fort->hoverName, 325, 32, FONT_BIG, Colors::Jasmine, bg->bg);//Hill Fort
  4992. pos = center(bg->pos);
  4993. heroPic = new CHeroArea(30, 60, hero);
  4994. currState.resize(slotsCount+1);
  4995. costs.resize(slotsCount);
  4996. totalSumm.resize(GameConstants::RESOURCE_QUANTITY);
  4997. std::vector<std::string> files;
  4998. files += "APHLF1R.DEF", "APHLF1Y.DEF", "APHLF1G.DEF";
  4999. for (int i=0; i<slotsCount; i++)
  5000. {
  5001. currState[i] = getState(i);
  5002. upgrade[i] = new CAdventureMapButton(getTextForSlot(i),"",boost::bind(&CHillFortWindow::makeDeal, this, i),
  5003. 107+i*76, 171, "", SDLK_1+i, &files);
  5004. upgrade[i]->block(currState[i] == -1);
  5005. }
  5006. files.clear();
  5007. files += "APHLF4R.DEF", "APHLF4Y.DEF", "APHLF4G.DEF";
  5008. currState[slotsCount] = getState(slotsCount);
  5009. upgradeAll = new CAdventureMapButton(CGI->generaltexth->allTexts[432],"",boost::bind(&CHillFortWindow::makeDeal, this, slotsCount),
  5010. 30, 231, "", SDLK_0, &files);
  5011. quit = new CAdventureMapButton("","",boost::bind(&CGuiHandler::popIntTotally, &GH, this), 294, 275, "IOKAY.DEF", SDLK_RETURN);
  5012. bar = new CGStatusBar(327, 332);
  5013. garr = new CGarrisonInt(108, 60, 18, Point(),bg->bg,Point(108,60),hero,NULL);
  5014. updateGarrisons();
  5015. }
  5016. CHillFortWindow::~CHillFortWindow()
  5017. {
  5018. }
  5019. void CHillFortWindow::activate()
  5020. {
  5021. CIntObject::activate();
  5022. GH.statusbar = bar;
  5023. }
  5024. void CHillFortWindow::updateGarrisons()
  5025. {
  5026. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  5027. totalSumm[i]=0;
  5028. for (int i=0; i<slotsCount; i++)
  5029. {
  5030. costs[i].clear();
  5031. int newState = getState(i);
  5032. if (newState != -1)
  5033. {
  5034. UpgradeInfo info;
  5035. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  5036. if (info.newID.size())//we have upgrades here - update costs
  5037. {
  5038. costs[i] = info.cost[0] * hero->getStackCount(i);
  5039. totalSumm += costs[i];
  5040. }
  5041. }
  5042. currState[i] = newState;
  5043. upgrade[i]->setIndex(newState);
  5044. upgrade[i]->block(currState[i] == -1);
  5045. upgrade[i]->hoverTexts[0] = getTextForSlot(i);
  5046. }
  5047. int newState = getState(slotsCount);
  5048. currState[slotsCount] = newState;
  5049. upgradeAll->setIndex(newState);
  5050. garr->recreateSlots();
  5051. }
  5052. void CHillFortWindow::makeDeal(int slot)
  5053. {
  5054. int offset = (slot == slotsCount)?2:0;
  5055. switch (currState[slot])
  5056. {
  5057. case 0:
  5058. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[314 + offset],
  5059. std::vector<CComponent*>(), soundBase::sound_todo);
  5060. break;
  5061. case 1:
  5062. LOCPLINT->showInfoDialog(CGI->generaltexth->allTexts[313 + offset],
  5063. std::vector<CComponent*>(), soundBase::sound_todo);
  5064. break;
  5065. case 2:
  5066. for (int i=0; i<slotsCount; i++)
  5067. if ( slot ==i || ( slot == slotsCount && currState[i] == 2 ) )//this is activated slot or "upgrade all"
  5068. {
  5069. UpgradeInfo info;
  5070. LOCPLINT->cb->getUpgradeInfo(hero, i, info);
  5071. LOCPLINT->cb->upgradeCreature(hero, i, info.newID[0]);
  5072. }
  5073. break;
  5074. }
  5075. }
  5076. void CHillFortWindow::showAll (SDL_Surface *to)
  5077. {
  5078. CIntObject::showAll(to);
  5079. for ( int i=0; i<slotsCount; i++)
  5080. {
  5081. if ( currState[i] == 0 || currState[i] == 2 )
  5082. {
  5083. if ( costs[i].size() )//we have several elements
  5084. {
  5085. int curY = 128;//reverse iterator is used to display gold as first element
  5086. for(int j = costs[i].size()-1; j >= 0; j--)
  5087. {
  5088. int val = costs[i][j];
  5089. if(!val) continue;
  5090. blitAtLoc(resources->ourImages[j].bitmap, 104+76*i, curY, to);
  5091. printToLoc(boost::lexical_cast<std::string>(val), 168+76*i, curY+16, FONT_SMALL, Colors::Cornsilk, to);
  5092. curY += 20;
  5093. }
  5094. }
  5095. else//free upgrade - print gold image and "Free" text
  5096. {
  5097. blitAtLoc(resources->ourImages[6].bitmap, 104+76*i, 128, to);
  5098. printToLoc(CGI->generaltexth->allTexts[344], 168+76*i, 144, FONT_SMALL, Colors::Cornsilk, to);
  5099. }
  5100. }
  5101. }
  5102. for (int i=0; i<GameConstants::RESOURCE_QUANTITY; i++)
  5103. {
  5104. if (totalSumm[i])//this resource is used - display it
  5105. {
  5106. blitAtLoc(resources->ourImages[i].bitmap, 104+76*i, 237, to);
  5107. printToLoc(boost::lexical_cast<std::string>(totalSumm[i]), 166+76*i, 253, FONT_SMALL, Colors::Cornsilk, to);
  5108. }
  5109. }
  5110. }
  5111. std::string CHillFortWindow::getTextForSlot(int slot)
  5112. {
  5113. if ( !hero->getCreature(slot) )//we dont have creature here
  5114. return "";
  5115. std::string str = CGI->generaltexth->allTexts[318];
  5116. int amount = hero->getStackCount(slot);
  5117. if ( amount == 1 )
  5118. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->nameSing);
  5119. else
  5120. boost::algorithm::replace_first(str,"%s",hero->getCreature(slot)->namePl);
  5121. return str;
  5122. }
  5123. int CHillFortWindow::getState(int slot)
  5124. {
  5125. TResources myRes = LOCPLINT->cb->getResourceAmount();
  5126. if ( slot == slotsCount )//"Upgrade all" slot
  5127. {
  5128. bool allUpgraded = true;//All creatures are upgraded?
  5129. for (int i=0; i<slotsCount; i++)
  5130. allUpgraded &= currState[i] == 1 || currState[i] == -1;
  5131. if (allUpgraded)
  5132. return 1;
  5133. if(!totalSumm.canBeAfforded(myRes))
  5134. return 0;
  5135. return 2;
  5136. }
  5137. if (hero->slotEmpty(slot))//no creature here
  5138. return -1;
  5139. UpgradeInfo info;
  5140. LOCPLINT->cb->getUpgradeInfo(hero, slot, info);
  5141. if (!info.newID.size())//already upgraded
  5142. return 1;
  5143. if(!(info.cost[0] * hero->getStackCount(slot)).canBeAfforded(myRes))
  5144. return 0;
  5145. return 2;//can upgrade
  5146. }
  5147. void CThievesGuildWindow::activate()
  5148. {
  5149. CIntObject::activate();
  5150. GH.statusbar = statusBar;
  5151. }
  5152. void CThievesGuildWindow::show(SDL_Surface * to)
  5153. {
  5154. blitAt(background, pos.x, pos.y, to);
  5155. statusBar->show(to);
  5156. exitb->showAll(to);
  5157. resdatabar->show(to);
  5158. //showing border around window
  5159. if(screen->w != 800 || screen->h !=600)
  5160. {
  5161. CMessage::drawBorder(LOCPLINT->playerID, to, pos.w + 28, pos.h + 28, pos.x-14, pos.y-15);
  5162. }
  5163. }
  5164. void CThievesGuildWindow::bexitf()
  5165. {
  5166. GH.popIntTotally(this);
  5167. }
  5168. CThievesGuildWindow::CThievesGuildWindow(const CGObjectInstance * _owner)
  5169. :owner(_owner)
  5170. {
  5171. OBJ_CONSTRUCTION_CAPTURING_ALL;
  5172. SThievesGuildInfo tgi; //info to be displayed
  5173. LOCPLINT->cb->getThievesGuildInfo(tgi, owner);
  5174. pos = center(Rect(0,0,800,600));// Rect( (conf.cc.resx - 800) / 2, (conf.cc.resy - 600) / 2, 800, 600 );
  5175. //loading backround and converting to more bpp form
  5176. SDL_Surface * bg = background = BitmapHandler::loadBitmap("TpRank.bmp", false);
  5177. graphics->blueToPlayersAdv(bg,LOCPLINT->playerID);
  5178. background = newSurface(bg->w, bg->h);
  5179. blitAt(bg, 0, 0, background);
  5180. SDL_FreeSurface(bg);
  5181. exitb = new CAdventureMapButton (std::string(), std::string(), boost::bind(&CThievesGuildWindow::bexitf,this), 748, 556, "TPMAGE1.def", SDLK_RETURN);
  5182. statusBar = new CGStatusBar(3, 555, "TStatBar.bmp", 742);
  5183. resdatabar = new CMinorResDataBar();
  5184. resdatabar->pos.x += pos.x;
  5185. resdatabar->pos.y += pos.y;
  5186. static std::vector< std::list< ui8 > > SThievesGuildInfo::* fields[] = { &SThievesGuildInfo::numOfTowns, &SThievesGuildInfo::numOfHeroes, &SThievesGuildInfo::gold,
  5187. &SThievesGuildInfo::woodOre, &SThievesGuildInfo::mercSulfCrystGems, &SThievesGuildInfo::obelisks, &SThievesGuildInfo::artifacts, &SThievesGuildInfo::army,
  5188. &SThievesGuildInfo::income};
  5189. //printing texts & descriptions to background
  5190. for(int g=0; g<12; ++g)
  5191. {
  5192. int y;
  5193. if(g == 9) //best hero
  5194. {
  5195. y = 400;
  5196. }
  5197. else if(g == 10) //personality
  5198. {
  5199. y = 460;
  5200. }
  5201. else if(g == 11) //best monster
  5202. {
  5203. y = 510;
  5204. }
  5205. else
  5206. {
  5207. y = 52 + 32*g;
  5208. }
  5209. std::string text = CGI->generaltexth->jktexts[24+g];
  5210. boost::algorithm::trim_if(text,boost::algorithm::is_any_of("\""));
  5211. printAtMiddle(text, 135, y, FONT_MEDIUM, Colors::Jasmine, background);
  5212. }
  5213. CDefHandler * strips = CDefHandler::giveDef("PRSTRIPS.DEF");
  5214. static const std::string colorToBox[] = {"PRRED.BMP", "PRBLUE.BMP", "PRTAN.BMP", "PRGREEN.BMP", "PRORANGE.BMP", "PRPURPLE.BMP", "PRTEAL.BMP", "PRROSE.bmp"};
  5215. for(int g=0; g<tgi.playerColors.size(); ++g)
  5216. {
  5217. if(g > 0)
  5218. {
  5219. blitAt(strips->ourImages[g-1].bitmap, 250 + 66*g, 7, background);
  5220. }
  5221. printAtMiddle(CGI->generaltexth->jktexts[16+g], 283 + 66*g, 24, FONT_BIG, Colors::Jasmine, background);
  5222. SDL_Surface * box = BitmapHandler::loadBitmap(colorToBox[tgi.playerColors[g]]);
  5223. blitAt(box, 253 + 66*g, 334, background);
  5224. SDL_FreeSurface(box);
  5225. }
  5226. delete strips;
  5227. CDefHandler * flagPictures = CDefHandler::giveDef("itgflags.def");
  5228. //printing flags
  5229. for(int g = 0; g < ARRAY_COUNT(fields); ++g) //by lines
  5230. {
  5231. for(int b=0; b<(tgi .* fields[g]).size(); ++b) //by places (1st, 2nd, ...)
  5232. {
  5233. std::list<ui8> players = (tgi .* fields[g])[b]; //get players with this place in this line
  5234. //std::sort(players.begin(), players.end());
  5235. int counter = 0;
  5236. for(std::list<ui8>::const_iterator it = players.begin(); it != players.end(); ++it)
  5237. {
  5238. int xpos = 259 + 66 * b + 12 * (counter % 4) + 6 * (counter / 4);
  5239. int ypos = 41 + 32 * g + 4 * (counter / 4);
  5240. blitAt(flagPictures->ourImages[*it].bitmap, xpos, ypos, background);
  5241. counter++;
  5242. }
  5243. }
  5244. }
  5245. delete flagPictures;
  5246. flagPictures = NULL;
  5247. //printing best hero
  5248. int counter = 0;
  5249. for(std::map<ui8, InfoAboutHero>::const_iterator it = tgi.colorToBestHero.begin(); it != tgi.colorToBestHero.end(); ++it)
  5250. {
  5251. if(it->second.portrait >= 0)
  5252. blitAt(graphics->portraitSmall[it->second.portrait], 260 + 66 * counter, 360, background);
  5253. counter++;
  5254. //printing stats
  5255. if(it->second.details)
  5256. {
  5257. printAtWB(CGI->generaltexth->allTexts[184], 191 + 66*counter, 396, FONT_TINY, 10, Colors::Cornsilk, background);
  5258. for (int i=0; i<it->second.details->primskills.size(); ++i)
  5259. {
  5260. std::ostringstream skill;
  5261. skill << it->second.details->primskills[i];
  5262. printTo(skill.str(), 244 + 66 * counter, 407 + 11*i, FONT_TINY, Colors::Cornsilk, background);
  5263. }
  5264. }
  5265. }
  5266. //printing best creature
  5267. counter = 0;
  5268. for(std::map<ui8, si32>::const_iterator it = tgi.bestCreature.begin(); it != tgi.bestCreature.end(); ++it)
  5269. {
  5270. if(it->second >= 0)
  5271. blitAt(graphics->bigImgs[it->second], 255 + 66 * counter, 479, background);
  5272. counter++;
  5273. }
  5274. //printing personality
  5275. counter = 0;
  5276. for(std::map<ui8, si8>::const_iterator it = tgi.personality.begin(); it != tgi.personality.end(); ++it)
  5277. {
  5278. int toPrint = 0;
  5279. if(it->second == -1)
  5280. {
  5281. toPrint = 172;
  5282. }
  5283. else
  5284. {
  5285. toPrint = 168 + it->second;
  5286. }
  5287. printAtMiddle(CGI->generaltexth->arraytxt[toPrint], 283 + 66*counter, 459, FONT_SMALL, Colors::Cornsilk, background);
  5288. counter++;
  5289. }
  5290. }
  5291. CThievesGuildWindow::~CThievesGuildWindow()
  5292. {
  5293. SDL_FreeSurface(background);
  5294. // delete exitb;
  5295. // delete statusBar;
  5296. // delete resdatabar;
  5297. }
  5298. void MoraleLuckBox::set(const IBonusBearer *node)
  5299. {
  5300. const int textId[] = {62, 88}; //eg %s \n\n\n {Current Luck Modifiers:}
  5301. const int noneTxtId = 108; //Russian version uses same text for neutral morale\luck
  5302. const int neutralDescr[] = {60, 86}; //eg {Neutral Morale} \n\n Neutral morale means your armies will neither be blessed with extra attacks or freeze in combat.
  5303. const int componentType[] = {CComponent::luck, CComponent::morale};
  5304. const int hoverTextBase[] = {7, 4};
  5305. const Bonus::BonusType bonusType[] = {Bonus::LUCK, Bonus::MORALE};
  5306. int (IBonusBearer::*getValue[])() const = {&IBonusBearer::LuckVal, &IBonusBearer::MoraleVal};
  5307. int mrlt = -9;
  5308. TModDescr mrl;
  5309. if (node)
  5310. {
  5311. node->getModifiersWDescr(mrl, bonusType[morale]);
  5312. bonusValue = (node->*getValue[morale])();
  5313. }
  5314. else
  5315. bonusValue = 0;
  5316. mrlt = (bonusValue>0)-(bonusValue<0); //signum: -1 - bad luck / morale, 0 - neutral, 1 - good
  5317. hoverText = CGI->generaltexth->heroscrn[hoverTextBase[morale] - mrlt];
  5318. baseType = componentType[morale];
  5319. text = CGI->generaltexth->arraytxt[textId[morale]];
  5320. boost::algorithm::replace_first(text,"%s",CGI->generaltexth->arraytxt[neutralDescr[morale]-mrlt]);
  5321. if (!mrl.size())
  5322. text += CGI->generaltexth->arraytxt[noneTxtId];
  5323. else
  5324. {
  5325. if (node->hasBonusOfType (Bonus::UNDEAD) || node->hasBonusOfType(Bonus::BLOCK_MORALE) || node->hasBonusOfType(Bonus::NON_LIVING)) //it's a creature window
  5326. {
  5327. text += CGI->generaltexth->arraytxt[113]; //unaffected by morale
  5328. }
  5329. else
  5330. {
  5331. for(int it=0; it < mrl.size(); it++)
  5332. text += "\n" + mrl[it].second;
  5333. }
  5334. }
  5335. }
  5336. void MoraleLuckBox::showAll(SDL_Surface * to)
  5337. {
  5338. CDefEssential *def;
  5339. if (small)
  5340. def = morale ? graphics->morale30 : graphics->luck30;
  5341. else
  5342. def = morale ? graphics->morale42 : graphics->luck42;
  5343. SDL_Surface *img = def->ourImages[bonusValue + 3].bitmap;
  5344. blitAt(img, Rect(img).centerIn(pos), to); //put img in the center of our pos
  5345. }
  5346. MoraleLuckBox::MoraleLuckBox(bool Morale, const Rect &r, bool Small)
  5347. :morale(Morale),
  5348. small(Small)
  5349. {
  5350. bonusValue = 0;
  5351. pos = r + pos;
  5352. }
  5353. MoraleLuckBox::~MoraleLuckBox()
  5354. {
  5355. }
  5356. CArtifactHolder::CArtifactHolder()
  5357. {
  5358. type |= WITH_ARTIFACTS;
  5359. }
  5360. void CWindowWithArtifacts::artifactRemoved(const ArtifactLocation &artLoc)
  5361. {
  5362. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5363. aoh->artifactRemoved(artLoc);
  5364. }
  5365. void CWindowWithArtifacts::artifactMoved(const ArtifactLocation &artLoc, const ArtifactLocation &destLoc)
  5366. {
  5367. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5368. aoh->artifactMoved(artLoc, destLoc);
  5369. }
  5370. void CWindowWithArtifacts::artifactDisassembled(const ArtifactLocation &artLoc)
  5371. {
  5372. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5373. aoh->artifactDisassembled(artLoc);
  5374. }
  5375. void CWindowWithArtifacts::artifactAssembled(const ArtifactLocation &artLoc)
  5376. {
  5377. BOOST_FOREACH(CArtifactsOfHero *aoh, artSets)
  5378. aoh->artifactAssembled(artLoc);
  5379. }
  5380. void CArtifactsOfHero::SCommonPart::Artpos::clear()
  5381. {
  5382. slotID = -1;
  5383. AOH = NULL;
  5384. art = NULL;
  5385. }
  5386. CArtifactsOfHero::SCommonPart::Artpos::Artpos()
  5387. {
  5388. clear();
  5389. }
  5390. void CArtifactsOfHero::SCommonPart::Artpos::setTo(const CArtPlace *place, bool dontTakeBackpack)
  5391. {
  5392. slotID = place->slotID;
  5393. AOH = place->ourOwner;
  5394. if(slotID >= 19 && dontTakeBackpack)
  5395. art = NULL;
  5396. else
  5397. art = place->ourArt;
  5398. }
  5399. bool CArtifactsOfHero::SCommonPart::Artpos::operator==(const ArtifactLocation &al) const
  5400. {
  5401. if(!AOH)
  5402. return false;
  5403. bool ret = al.hero == AOH->curHero && al.slot == slotID;
  5404. //assert(al.getArt() == art);
  5405. return ret;
  5406. }
  5407. bool CArtifactsOfHero::SCommonPart::Artpos::valid()
  5408. {
  5409. assert(AOH && art);
  5410. return art == AOH->curHero->getArt(slotID);
  5411. }
  5412. void CRClickPopup::clickRight(tribool down, bool previousState)
  5413. {
  5414. if(down)
  5415. return;
  5416. close();
  5417. }
  5418. void CRClickPopup::activate()
  5419. {
  5420. activateRClick();
  5421. }
  5422. void CRClickPopup::deactivate()
  5423. {
  5424. deactivateRClick();
  5425. }
  5426. void CRClickPopup::close()
  5427. {
  5428. GH.popIntTotally(this);
  5429. }
  5430. void CRClickPopup::createAndPush(const std::string &txt, const CInfoWindow::TCompsInfo &comps)
  5431. {
  5432. int player = LOCPLINT ? LOCPLINT->playerID : 1; //if no player, then use blue
  5433. CSimpleWindow * temp = new CInfoWindow(txt, player, comps);
  5434. temp->center(Point(GH.current->motion)); //center on mouse
  5435. temp->fitToScreen(10);
  5436. CRClickPopupInt *rcpi = new CRClickPopupInt(temp,true);
  5437. GH.pushInt(rcpi);
  5438. }
  5439. void CRClickPopup::createAndPush(const CGObjectInstance *obj, const Point &p, EAlignment alignment /*= BOTTOMRIGHT*/)
  5440. {
  5441. SDL_Surface *iWin = LOCPLINT->infoWin(obj); //try get custom infowindow for this obj
  5442. if(iWin)
  5443. GH.pushInt(new CInfoPopup(iWin, p, alignment, true));
  5444. else
  5445. CRClickPopup::createAndPush(obj->getHoverText());
  5446. }
  5447. CRClickPopup::CRClickPopup()
  5448. {
  5449. }
  5450. CRClickPopup::~CRClickPopup()
  5451. {
  5452. }
  5453. void CRClickPopupInt::show(SDL_Surface * to)
  5454. {
  5455. inner->show(to);
  5456. }
  5457. CRClickPopupInt::CRClickPopupInt( IShowActivatable *our, bool deleteInt )
  5458. {
  5459. CCS->curh->hide();
  5460. inner = our;
  5461. delInner = deleteInt;
  5462. }
  5463. CRClickPopupInt::~CRClickPopupInt()
  5464. {
  5465. // //workaround for hero window issue - if it's our interface, call dispose to properly reset it's state
  5466. // //TODO? it might be better to rewrite hero window so it will bee newed/deleted on opening / closing (not effort-worthy now, but on some day...?)
  5467. // if(LOCPLINT && inner == adventureInt->heroWindow)
  5468. // adventureInt->heroWindow->dispose();
  5469. if(delInner)
  5470. delete inner;
  5471. CCS->curh->show();
  5472. }
  5473. void CRClickPopupInt::showAll(SDL_Surface * to)
  5474. {
  5475. inner->showAll(to);
  5476. }