GUIClasses.cpp 195 KB

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