GUIClasses.cpp 197 KB

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