GUIClasses.cpp 172 KB

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