GUIClasses.cpp 172 KB

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