GUIClasses.cpp 181 KB

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