GUIClasses.cpp 181 KB

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