GUIClasses.cpp 196 KB

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