GUIClasses.cpp 172 KB

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