GUIClasses.cpp 180 KB

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