GUIClasses.cpp 183 KB

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