GUIClasses.cpp 184 KB

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