GUIClasses.cpp 173 KB

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