GUIClasses.cpp 181 KB

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