GUIClasses.cpp 181 KB

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