CObjectHandler.cpp 193 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304
  1. #include "StdInc.h"
  2. #include "CObjectHandler.h"
  3. #include "CDefObjInfoHandler.h"
  4. #include "CGeneralTextHandler.h"
  5. #include "CDefObjInfoHandler.h"
  6. #include "CHeroHandler.h"
  7. #include "CSpellHandler.h"
  8. #include "CModHandler.h"
  9. #include "../client/CSoundBase.h"
  10. #include <boost/random/linear_congruential.hpp>
  11. #include "CTownHandler.h"
  12. #include "CCreatureHandler.h"
  13. #include "VCMI_Lib.h"
  14. #include "IGameCallback.h"
  15. #include "CGameState.h"
  16. #include "NetPacks.h"
  17. #include "StartInfo.h"
  18. #include "Mapping/CMap.h"
  19. #include <SDL_stdinc.h>
  20. #include "CBuildingHandler.h"
  21. #include "JsonNode.h"
  22. #include "Filesystem/CResourceLoader.h"
  23. #include "GameConstants.h"
  24. using namespace boost::assign;
  25. /*
  26. * CObjectHandler.cpp, part of VCMI engine
  27. *
  28. * Authors: listed in file AUTHORS in main folder
  29. *
  30. * License: GNU General Public License v2.0 or later
  31. * Full text of license available in license.txt file, in main folder
  32. *
  33. */
  34. // It looks that we can't rely on shadowCoverage correctness (Mantis #866). This may result
  35. // in notable performance decrease (SDL blit with custom alpha blit) not notable on my system (Ivan)
  36. #define USE_COVERAGE_MAP 0
  37. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  38. std::vector<std::pair<int, int> > CGTeleport::gates;
  39. IGameCallback * IObjectInterface::cb = NULL;
  40. extern boost::rand48 ran;
  41. std::map <ui8, std::set <ui8> > CGKeys::playerKeyMap;
  42. std::map <si32, std::vector<si32> > CGMagi::eyelist;
  43. ui8 CGObelisk::obeliskCount; //how many obelisks are on map
  44. std::map<ui8, ui8> CGObelisk::visited; //map: team_id => how many obelisks has been visited
  45. std::vector<const CArtifact *> CGTownInstance::merchantArtifacts;
  46. std::vector<int> CGTownInstance::universitySkills;
  47. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  48. {}
  49. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  50. {}
  51. void IObjectInterface::newTurn () const
  52. {}
  53. IObjectInterface::~IObjectInterface()
  54. {}
  55. IObjectInterface::IObjectInterface()
  56. {}
  57. void IObjectInterface::initObj()
  58. {}
  59. void IObjectInterface::setProperty( ui8 what, ui32 val )
  60. {}
  61. bool IObjectInterface::wasVisited (ui8 player) const
  62. {
  63. return false;
  64. }
  65. bool IObjectInterface::wasVisited (const CGHeroInstance * h) const
  66. {
  67. return false;
  68. }
  69. void IObjectInterface::postInit()
  70. {}
  71. void IObjectInterface::preInit()
  72. {}
  73. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  74. {
  75. if(what == 10)
  76. players.insert((ui8)val);
  77. }
  78. bool CPlayersVisited::wasVisited( ui8 player ) const
  79. {
  80. return vstd::contains(players,player);
  81. }
  82. // Bank helper. Find the creature ID and their number, and store the
  83. // result in storage (either guards or reward creatures).
  84. static void readCreatures(const JsonNode &creature, std::vector< std::pair <ui16, ui32> > &storage)
  85. {
  86. std::pair<si16, si32> creInfo = std::make_pair(-1, 0);
  87. creInfo.second = creature["number"].Float();
  88. creInfo.first = creature["id"].Float();
  89. storage.push_back(creInfo);
  90. }
  91. // Bank helper. Process a bank level.
  92. static void readBankLevel(const JsonNode &level, BankConfig &bc)
  93. {
  94. int idx;
  95. bc.chance = level["chance"].Float();
  96. BOOST_FOREACH(const JsonNode &creature, level["guards"].Vector())
  97. {
  98. readCreatures(creature, bc.guards);
  99. }
  100. bc.upgradeChance = level["upgrade_chance"].Float();
  101. bc.combatValue = level["combat_value"].Float();
  102. bc.resources.resize(GameConstants::RESOURCE_QUANTITY);
  103. idx = 0;
  104. BOOST_FOREACH(const JsonNode &resource, level["reward_resources"].Vector())
  105. {
  106. bc.resources[idx] = resource.Float();
  107. idx ++;
  108. }
  109. BOOST_FOREACH(const JsonNode &creature, level["reward_creatures"].Vector())
  110. {
  111. readCreatures(creature, bc.creatures);
  112. }
  113. bc.artifacts.resize(4);
  114. idx = 0;
  115. BOOST_FOREACH(const JsonNode &artifact, level["reward_artifacts"].Vector())
  116. {
  117. bc.artifacts[idx] = artifact.Float();
  118. idx ++;
  119. }
  120. bc.value = level["value"].Float();
  121. bc.rewardDifficulty = level["profitability"].Float();
  122. bc.easiest = level["easiest"].Float();
  123. }
  124. void CObjectHandler::loadObjects()
  125. {
  126. tlog5 << "\t\tReading cregens \n";
  127. const JsonNode config(ResourceID("config/dwellings.json"));
  128. BOOST_FOREACH(const JsonNode &dwelling, config["dwellings"].Vector())
  129. {
  130. cregens[dwelling["dwelling"].Float()] = dwelling["creature"].Float();
  131. }
  132. tlog5 << "\t\tDone loading cregens!\n";
  133. tlog5 << "\t\tReading resources prices \n";
  134. const JsonNode config2(ResourceID("config/resources.json"));
  135. BOOST_FOREACH(const JsonNode &price, config2["resources_prices"].Vector())
  136. {
  137. resVals.push_back(price.Float());
  138. }
  139. tlog5 << "\t\tDone loading resource prices!\n";
  140. tlog5 << "\t\tReading banks configs \n";
  141. const JsonNode config3(ResourceID("config/bankconfig.json"));
  142. int bank_num = 0;
  143. BOOST_FOREACH(const JsonNode &bank, config3["banks"].Vector())
  144. {
  145. creBanksNames[bank_num] = bank["name"].String();
  146. int level_num = 0;
  147. BOOST_FOREACH(const JsonNode &level, bank["levels"].Vector())
  148. {
  149. banksInfo[bank_num].push_back(new BankConfig);
  150. BankConfig &bc = *banksInfo[bank_num].back();
  151. bc.level = level_num;
  152. readBankLevel(level, bc);
  153. level_num ++;
  154. }
  155. bank_num ++;
  156. }
  157. tlog5 << "\t\tDone loading banks configs \n";
  158. }
  159. int CObjectHandler::bankObjToIndex (const CGObjectInstance * obj)
  160. {
  161. switch (obj->ID) //find appriopriate key
  162. {
  163. case Obj::CREATURE_BANK:
  164. return obj->subID;
  165. case Obj::DERELICT_SHIP:
  166. return 8;
  167. case Obj::DRAGON_UTOPIA:
  168. return 10;
  169. case Obj::CRYPT:
  170. return 9;
  171. case Obj::SHIPWRECK:
  172. return 7;
  173. default:
  174. tlog2 << "Unrecognized Bank indetifier!\n";
  175. return 0;
  176. }
  177. }
  178. int CGObjectInstance::getOwner() const
  179. {
  180. //if (state)
  181. // return state->owner;
  182. //else
  183. return tempOwner; //won't have owner
  184. }
  185. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  186. {
  187. pos = int3(-1,-1,-1);
  188. //std::cout << "Tworze obiekt "<<this<<std::endl;
  189. //state = new CLuaObjectScript();
  190. ID = subID = id = -1;
  191. defInfo = NULL;
  192. tempOwner = 254;
  193. blockVisit = false;
  194. }
  195. CGObjectInstance::~CGObjectInstance()
  196. {
  197. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  198. //if (state)
  199. // delete state;
  200. //state=NULL;
  201. }
  202. const std::string & CGObjectInstance::getHoverText() const
  203. {
  204. return hoverName;
  205. }
  206. void CGObjectInstance::setOwner(int ow)
  207. {
  208. //if (state)
  209. // state->owner = ow;
  210. //else
  211. tempOwner = ow;
  212. }
  213. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  214. {
  215. return defInfo->width;
  216. }
  217. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  218. {
  219. return defInfo->height;
  220. }
  221. bool CGObjectInstance::visitableAt(int x, int y) const //returns true if object is visitable at location (x, y) form left top tile of image (x, y in tiles)
  222. {
  223. if(defInfo==NULL)
  224. {
  225. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  226. return false;
  227. }
  228. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  229. {
  230. return true;
  231. }
  232. return false;
  233. }
  234. bool CGObjectInstance::blockingAt(int x, int y) const
  235. {
  236. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  237. return false;
  238. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  239. return false;
  240. return true;
  241. }
  242. bool CGObjectInstance::coveringAt(int x, int y) const
  243. {
  244. //input coordinates are always negative
  245. x = -x;
  246. y = -y;
  247. #if USE_COVERAGE_MAP
  248. //NOTE: this code may be broken
  249. if((defInfo->coverageMap[y] >> (7-(x) )) & 1
  250. || (defInfo->shadowCoverage[y] >> (7-(x) )) & 1)
  251. return true;
  252. return false;
  253. #else
  254. return x >= 0 && y >= 0 && x < getWidth() && y < getHeight();
  255. #endif
  256. }
  257. bool CGObjectInstance::hasShadowAt( int x, int y ) const
  258. {
  259. #if USE_COVERAGE_MAP
  260. //NOTE: this code may be broken
  261. if( (defInfo->shadowCoverage[y] >> (7-(x) )) & 1 )
  262. return true;
  263. return false;
  264. #else
  265. return coveringAt(x,y);// ignore unreliable shadowCoverage map
  266. #endif
  267. }
  268. std::set<int3> CGObjectInstance::getBlockedPos() const
  269. {
  270. std::set<int3> ret;
  271. for(int w=0; w<getWidth(); ++w)
  272. {
  273. for(int h=0; h<getHeight(); ++h)
  274. {
  275. if(blockingAt(w, h))
  276. ret.insert(int3(pos.x - getWidth() + w + 1, pos.y - getHeight() + h + 1, pos.z));
  277. }
  278. }
  279. return ret;
  280. }
  281. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  282. {
  283. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  284. return true;
  285. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  286. return false;
  287. if(this->pos.y<cmp.pos.y)
  288. return true;
  289. if(this->pos.y>cmp.pos.y)
  290. return false;
  291. if(cmp.ID==Obj::HERO && ID!=Obj::HERO)
  292. return true;
  293. if(cmp.ID!=Obj::HERO && ID==Obj::HERO)
  294. return false;
  295. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  296. return true;
  297. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  298. return false;
  299. if(this->pos.x<cmp.pos.x)
  300. return true;
  301. return false;
  302. }
  303. void CGObjectInstance::initObj()
  304. {
  305. switch(ID)
  306. {
  307. case Obj::TAVERN:
  308. blockVisit = true;
  309. break;
  310. }
  311. }
  312. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  313. {
  314. switch(what)
  315. {
  316. case ObjProperty::OWNER:
  317. tempOwner = val;
  318. break;
  319. case ObjProperty::BLOCKVIS:
  320. blockVisit = val;
  321. break;
  322. case ObjProperty::ID:
  323. ID = val;
  324. break;
  325. case ObjProperty::SUBID:
  326. subID = val;
  327. break;
  328. }
  329. setPropertyDer(what, val);
  330. }
  331. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  332. {}
  333. int3 CGObjectInstance::getSightCenter() const
  334. {
  335. //return vistiable tile if possible
  336. for(int i=0; i < 8; i++)
  337. for(int j=0; j < 6; j++)
  338. if(visitableAt(i,j))
  339. return(pos + int3(i-7, j-5, 0));
  340. return pos;
  341. }
  342. int CGObjectInstance::getSightRadious() const
  343. {
  344. return 3;
  345. }
  346. void CGObjectInstance::getSightTiles(boost::unordered_set<int3, ShashInt3> &tiles) const //returns reference to the set
  347. {
  348. cb->getTilesInRange(tiles, getSightCenter(), getSightRadious(), tempOwner, 1);
  349. }
  350. void CGObjectInstance::hideTiles(int ourplayer, int radius) const
  351. {
  352. for (auto i = cb->gameState()->teams.begin(); i != cb->gameState()->teams.end(); i++)
  353. {
  354. if ( !vstd::contains(i->second.players, ourplayer ))//another team
  355. {
  356. for (auto j = i->second.players.begin(); j != i->second.players.end(); j++)
  357. if ( cb->getPlayer(*j)->status == PlayerState::INGAME )//seek for living player (if any)
  358. {
  359. FoWChange fw;
  360. fw.mode = 0;
  361. fw.player = *j;
  362. cb->getTilesInRange (fw.tiles, pos, radius, (*j), -1);
  363. cb->sendAndApply (&fw);
  364. break;
  365. }
  366. }
  367. }
  368. }
  369. int3 CGObjectInstance::getVisitableOffset() const
  370. {
  371. for(int y = 0; y < 6; y++)
  372. for (int x = 0; x < 8; x++)
  373. if((defInfo->visitMap[5-y] >> x) & 1)
  374. return int3(x,y,0);
  375. tlog2 << "Warning: getVisitableOffset called on non-visitable obj!\n";
  376. return int3(-1,-1,-1);
  377. }
  378. void CGObjectInstance::getNameVis( std::string &hname ) const
  379. {
  380. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  381. hname = VLC->generaltexth->names[ID];
  382. if(h)
  383. {
  384. if(!h->hasBonusFrom(Bonus::OBJECT,ID))
  385. hname += " " + VLC->generaltexth->allTexts[353]; //not visited
  386. else
  387. hname += " " + VLC->generaltexth->allTexts[352]; //visited
  388. }
  389. }
  390. void CGObjectInstance::giveDummyBonus(int heroID, ui8 duration) const
  391. {
  392. GiveBonus gbonus;
  393. gbonus.bonus.type = Bonus::NONE;
  394. gbonus.id = heroID;
  395. gbonus.bonus.duration = duration;
  396. gbonus.bonus.source = Bonus::OBJECT;
  397. gbonus.bonus.sid = ID;
  398. cb->giveHeroBonus(&gbonus);
  399. }
  400. void CGObjectInstance::onHeroVisit( const CGHeroInstance * h ) const
  401. {
  402. switch(ID)
  403. {
  404. case Obj::HILL_FORT:
  405. {
  406. OpenWindow ow;
  407. ow.window = OpenWindow::HILL_FORT_WINDOW;
  408. ow.id1 = id;
  409. ow.id2 = h->id;
  410. cb->sendAndApply(&ow);
  411. }
  412. break;
  413. case Obj::SANCTUARY:
  414. {
  415. InfoWindow iw;
  416. iw.player = h->tempOwner;
  417. iw.soundID = soundBase::GETPROTECTION;
  418. iw.text.addTxt(MetaString::ADVOB_TXT, 114); //You enter the sanctuary and immediately feel as if a great weight has been lifted off your shoulders. You feel safe here.
  419. cb->sendAndApply(&iw);
  420. }
  421. break;
  422. case Obj::TAVERN:
  423. {
  424. OpenWindow ow;
  425. ow.window = OpenWindow::TAVERN_WINDOW;
  426. ow.id1 = h->id;
  427. ow.id2 = id;
  428. cb->sendAndApply(&ow);
  429. }
  430. break;
  431. }
  432. }
  433. ui8 CGObjectInstance::getPassableness() const
  434. {
  435. return 0;
  436. }
  437. int3 CGObjectInstance::visitablePos() const
  438. {
  439. return pos - getVisitableOffset();
  440. }
  441. bool CGObjectInstance::isVisitable() const
  442. {
  443. for(int g=0; g<ARRAY_COUNT(defInfo->visitMap); ++g)
  444. {
  445. if(defInfo->visitMap[g] != 0)
  446. {
  447. return true;
  448. }
  449. }
  450. return false;
  451. }
  452. static int lowestSpeed(const CGHeroInstance * chi)
  453. {
  454. if(!chi->Slots().size())
  455. {
  456. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  457. return 20;
  458. }
  459. TSlots::const_iterator i = chi->Slots().begin();
  460. //TODO? should speed modifiers (eg from artifacts) affect hero movement?
  461. int ret = (i++)->second->valOfBonuses(Bonus::STACKS_SPEED);
  462. for (;i!=chi->Slots().end();i++)
  463. {
  464. ret = std::min(ret, i->second->valOfBonuses(Bonus::STACKS_SPEED));
  465. }
  466. return ret;
  467. }
  468. ui32 CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  469. {
  470. //base move cost
  471. unsigned ret = 100;
  472. //if there is road both on dest and src tiles - use road movement cost
  473. if(dest.roadType != ERoadType::NO_ROAD && from.roadType != ERoadType::NO_ROAD)
  474. {
  475. int road = std::min(dest.roadType,from.roadType); //used road ID
  476. switch(road)
  477. {
  478. case ERoadType::DIRT_ROAD:
  479. ret = 75;
  480. break;
  481. case ERoadType::GRAVEL_ROAD:
  482. ret = 65;
  483. break;
  484. case ERoadType::COBBLESTONE_ROAD:
  485. ret = 50;
  486. break;
  487. default:
  488. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  489. break;
  490. }
  491. }
  492. else
  493. {
  494. //FIXME: in H3 presence of Nomad in army will remove terrain penalty for sand. Bonus not implemented in VCMI
  495. // NOTE: in H3 neutral stacks will ignore terrain penalty only if placed as topmost stack(s) in hero army.
  496. // This is clearly bug in H3 however intended behaviour is not clear.
  497. // Current VCMI behaviour will ignore neutrals in calculations so army in VCMI
  498. // will always have best penalty without any influence from player-defined stacks order
  499. bool nativeArmy = true;
  500. BOOST_FOREACH(auto stack, stacks)
  501. {
  502. int nativeTerrain = VLC->townh->factions[stack.second->type->faction].nativeTerrain;
  503. if (nativeTerrain != -1 && nativeTerrain != from.terType)
  504. {
  505. nativeArmy = false;
  506. break;
  507. }
  508. }
  509. if (!nativeArmy)
  510. ret = VLC->heroh->terrCosts[from.terType];
  511. }
  512. return ret;
  513. }
  514. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  515. {
  516. if (toh3m)
  517. {
  518. src.x+=1;
  519. return src;
  520. }
  521. else
  522. {
  523. src.x-=1;
  524. return src;
  525. }
  526. }
  527. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  528. {
  529. if (h3m)
  530. {
  531. return pos;
  532. }
  533. else
  534. {
  535. return convertPosition(pos,false);
  536. }
  537. }
  538. bool CGHeroInstance::canWalkOnSea() const
  539. {
  540. return hasBonusOfType(Bonus::FLYING_MOVEMENT) || hasBonusOfType(Bonus::WATER_WALKING);
  541. }
  542. ui8 CGHeroInstance::getSecSkillLevel(SecondarySkill skill) const
  543. {
  544. for(size_t i=0; i < secSkills.size(); ++i)
  545. if(secSkills[i].first == skill)
  546. return secSkills[i].second;
  547. return 0;
  548. }
  549. void CGHeroInstance::setSecSkillLevel(SecondarySkill which, int val, bool abs)
  550. {
  551. if(getSecSkillLevel(which) == 0)
  552. {
  553. secSkills.push_back(std::pair<int,int>(which, val));
  554. updateSkill(which, val);
  555. }
  556. else
  557. {
  558. for (unsigned i=0; i<secSkills.size(); i++)
  559. {
  560. if(secSkills[i].first == which)
  561. {
  562. if(abs)
  563. secSkills[i].second = val;
  564. else
  565. secSkills[i].second += val;
  566. if(secSkills[i].second > 3) //workaround to avoid crashes when same sec skill is given more than once
  567. {
  568. tlog1 << "Warning: Skill " << which << " increased over limit! Decreasing to Expert.\n";
  569. secSkills[i].second = 3;
  570. }
  571. updateSkill(which, secSkills[i].second); //when we know final value
  572. }
  573. }
  574. }
  575. }
  576. bool CGHeroInstance::canLearnSkill() const
  577. {
  578. return secSkills.size() < GameConstants::SKILL_PER_HERO;
  579. }
  580. int CGHeroInstance::maxMovePoints(bool onLand) const
  581. {
  582. int base;
  583. if(onLand)
  584. {
  585. // used function is f(x) = 66.6x + 1300, rounded to second digit, where x is lowest speed in army
  586. static const int baseSpeed = 1300; // base speed from creature with 0 speed
  587. int armySpeed = lowestSpeed(this) * 20 / 3;
  588. base = armySpeed * 10 + baseSpeed; // separate *10 is intentional to receive same rounding as in h3
  589. vstd::abetween(base, 1500, 2000); // base speed is limited by these values
  590. }
  591. else
  592. {
  593. base = 1500; //on water base movement is always 1500 (speed of army doesn't matter)
  594. }
  595. int bonus = valOfBonuses(Bonus::MOVEMENT) + (onLand ? valOfBonuses(Bonus::LAND_MOVEMENT) : valOfBonuses(Bonus::SEA_MOVEMENT));
  596. double modifier = 0;
  597. if(onLand)
  598. {
  599. modifier = valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, LOGISTICS) / 100.0;
  600. }
  601. else
  602. {
  603. modifier = valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, NAVIGATION) / 100.0;
  604. }
  605. return int(base + base*modifier) + bonus;
  606. }
  607. CGHeroInstance::CGHeroInstance()
  608. : IBoatGenerator(this)
  609. {
  610. setNodeType(HERO);
  611. ID = Obj::HERO;
  612. tacticFormationEnabled = inTownGarrison = false;
  613. mana = movement = portrait = level = -1;
  614. isStanding = true;
  615. moveDir = 4;
  616. exp = 0xffffffff;
  617. visitedTown = NULL;
  618. type = NULL;
  619. boat = NULL;
  620. commander = NULL;
  621. sex = 0xff;
  622. secSkills.push_back(std::make_pair(-1, -1));
  623. speciality.setNodeType(CBonusSystemNode::SPECIALITY);
  624. attachTo(&speciality); //do we ever need to detach it?
  625. }
  626. void CGHeroInstance::initHero(int SUBID)
  627. {
  628. subID = SUBID;
  629. initHero();
  630. }
  631. void CGHeroInstance::initHero()
  632. {
  633. assert(validTypes(true));
  634. if(ID == Obj::HERO)
  635. initHeroDefInfo();
  636. if(!type)
  637. type = VLC->heroh->heroes[subID];
  638. if(!vstd::contains(spells, 0xffffffff)) //hero starts with a spell
  639. {
  640. BOOST_FOREACH(auto spellID, type->spells)
  641. spells.insert(spellID);
  642. }
  643. else //remove placeholder
  644. spells -= 0xffffffff;
  645. if(!getArt(ArtifactPosition::MACH4) && !getArt(ArtifactPosition::SPELLBOOK) && !type->spells.empty()) //no catapult means we haven't read pre-existent set -> use default rules for spellbook
  646. putArtifact(ArtifactPosition::SPELLBOOK, CArtifactInstance::createNewArtifactInstance(0));
  647. if(!getArt(ArtifactPosition::MACH4))
  648. putArtifact(ArtifactPosition::MACH4, CArtifactInstance::createNewArtifactInstance(3)); //everyone has a catapult
  649. if(portrait < 0 || portrait == 255)
  650. portrait = type->imageIndex;
  651. if(!hasBonus(Selector::sourceType(Bonus::HERO_BASE_SKILL)))
  652. {
  653. for(int g=0; g<GameConstants::PRIMARY_SKILLS; ++g)
  654. {
  655. pushPrimSkill(static_cast<PrimarySkill::PrimarySkill>(g), type->heroClass->primarySkillInitial[g]);
  656. }
  657. }
  658. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  659. secSkills = type->secSkillsInit;
  660. if (!name.length())
  661. name = type->name;
  662. if (exp == 0xffffffff)
  663. {
  664. initExp();
  665. }
  666. else
  667. {
  668. level = VLC->heroh->level(exp);
  669. }
  670. if (sex == 0xFF)//sex is default
  671. sex = type->sex;
  672. setFormation(false);
  673. if (!stacksCount()) //standard army//initial army
  674. {
  675. initArmy();
  676. }
  677. assert(validTypes());
  678. if (VLC->modh->modules.COMMANDERS)
  679. {
  680. commander = new CCommanderInstance (VLC->townh->factions[type->heroClass->faction].commander);
  681. commander->setArmyObj (castToArmyObj()); //TODO: separate function for setting commanders
  682. }
  683. hoverName = VLC->generaltexth->allTexts[15];
  684. boost::algorithm::replace_first(hoverName,"%s",name);
  685. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  686. if (mana < 0)
  687. mana = manaLimit();
  688. }
  689. void CGHeroInstance::initArmy(IArmyDescriptor *dst /*= NULL*/)
  690. {
  691. if(!dst)
  692. dst = this;
  693. int howManyStacks = 0; //how many stacks will hero receives <1 - 3>
  694. int pom = ran()%100;
  695. int warMachinesGiven = 0;
  696. if(pom < 9)
  697. howManyStacks = 1;
  698. else if(pom < 79)
  699. howManyStacks = 2;
  700. else
  701. howManyStacks = 3;
  702. vstd::amin(howManyStacks, type->initialArmy.size());
  703. for(int stackNo=0; stackNo < howManyStacks; stackNo++)
  704. {
  705. auto & stack = type->initialArmy[stackNo];
  706. int range = stack.maxAmount - stack.minAmount;
  707. int count = ran()%(range+1) + stack.minAmount;
  708. if(stack.creature >= 145 &&
  709. stack.creature <= 149) //war machine
  710. {
  711. warMachinesGiven++;
  712. if(dst != this)
  713. continue;
  714. int slot = -1, aid = -1;
  715. switch (stack.creature)
  716. {
  717. case 145: //catapult
  718. slot = ArtifactPosition::MACH4;
  719. aid = 3;
  720. break;
  721. default:
  722. aid = CArtHandler::convertMachineID(stack.creature, true);
  723. slot = 9 + aid;
  724. break;
  725. }
  726. if(!getArt(slot))
  727. putArtifact(slot, CArtifactInstance::createNewArtifactInstance(aid));
  728. else
  729. tlog3 << "Hero " << name << " already has artifact at " << slot << ", omitting giving " << aid << std::endl;
  730. }
  731. else
  732. dst->setCreature(stackNo-warMachinesGiven, stack.creature, count);
  733. }
  734. }
  735. void CGHeroInstance::initHeroDefInfo()
  736. {
  737. if(!defInfo || defInfo->id != Obj::HERO)
  738. {
  739. defInfo = new CGDefInfo();
  740. defInfo->id = Obj::HERO;
  741. defInfo->subid = subID;
  742. defInfo->printPriority = 0;
  743. defInfo->visitDir = 0xff;
  744. }
  745. for(int i=0;i<6;i++)
  746. {
  747. defInfo->blockMap[i] = 255;
  748. defInfo->visitMap[i] = 0;
  749. defInfo->coverageMap[i] = 0;
  750. defInfo->shadowCoverage[i] = 0;
  751. }
  752. defInfo->blockMap[5] = 253;
  753. defInfo->visitMap[5] = 2;
  754. defInfo->coverageMap[4] = defInfo->coverageMap[5] = 224;
  755. }
  756. CGHeroInstance::~CGHeroInstance()
  757. {
  758. }
  759. bool CGHeroInstance::needsLastStack() const
  760. {
  761. return true;
  762. }
  763. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  764. {
  765. if(h == this) return; //exclude potential self-visiting
  766. if (ID == Obj::HERO)
  767. {
  768. if( cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner)) //our or ally hero
  769. {
  770. //exchange
  771. cb->heroExchange(h->id, id);
  772. }
  773. else //battle
  774. {
  775. if(visitedTown) //we're in town
  776. visitedTown->onHeroVisit(h); //town will handle attacking
  777. else
  778. cb->startBattleI(h, this);
  779. }
  780. }
  781. else if(ID == Obj::PRISON)
  782. {
  783. InfoWindow iw;
  784. iw.player = h->tempOwner;
  785. iw.soundID = soundBase::ROGUE;
  786. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  787. {
  788. cb->changeObjPos(id,pos+int3(1,0,0),0);
  789. cb->setObjProperty(id, ObjProperty::ID, Obj::HERO); //set ID to 34
  790. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  791. iw.text << std::pair<ui8,ui32>(11,102);
  792. }
  793. else //already 8 wandering heroes
  794. {
  795. iw.text << std::pair<ui8,ui32>(11,103);
  796. }
  797. cb->showInfoDialog(&iw);
  798. }
  799. }
  800. const std::string & CGHeroInstance::getBiography() const
  801. {
  802. if (biography.length())
  803. return biography;
  804. return type->biography;
  805. }
  806. void CGHeroInstance::initObj() //TODO: use bonus system
  807. {
  808. blockVisit = true;
  809. speciality.growthsWithLevel = false;
  810. if(!type)
  811. return; //TODO support prison
  812. for (std::vector<SSpecialtyInfo>::const_iterator it = type->spec.begin(); it != type->spec.end(); it++)
  813. {
  814. Bonus *bonus = new Bonus();
  815. bonus->val = it->val;
  816. bonus->sid = id; //from the hero, speciality has no unique id
  817. bonus->duration = Bonus::PERMANENT;
  818. bonus->source = Bonus::HERO_SPECIAL;
  819. switch (it->type)
  820. {
  821. case 1:// creature speciality
  822. {
  823. speciality.growthsWithLevel = true;
  824. const CCreature &specCreature = *VLC->creh->creatures[it->additionalinfo]; //creature in which we have specialty
  825. int creLevel = specCreature.level;
  826. if(!creLevel)
  827. {
  828. if(it->additionalinfo == 146)
  829. creLevel = 5; //treat ballista as 5-level
  830. else
  831. {
  832. tlog2 << "Warning: unknown level of " << specCreature.namePl << std::endl;
  833. continue;
  834. }
  835. }
  836. bonus->limiter.reset(new CCreatureTypeLimiter (specCreature, true)); //with upgrades
  837. bonus->type = Bonus::PRIMARY_SKILL;
  838. bonus->additionalInfo = it->additionalinfo;
  839. bonus->valType = Bonus::ADDITIVE_VALUE;
  840. bonus->subtype = PrimarySkill::ATTACK;
  841. speciality.addNewBonus(bonus);
  842. bonus = new Bonus(*bonus);
  843. bonus->subtype = PrimarySkill::DEFENSE;
  844. speciality.addNewBonus(bonus);
  845. //values will be calculated later
  846. bonus = new Bonus(*bonus);
  847. bonus->type = Bonus::STACKS_SPEED;
  848. bonus->val = 1; //+1 speed
  849. speciality.addNewBonus(bonus);
  850. }
  851. break;
  852. case 2://secondary skill
  853. speciality.growthsWithLevel = true;
  854. bonus->type = Bonus::SPECIAL_SECONDARY_SKILL; //needs to be recalculated with level, based on this value
  855. bonus->valType = Bonus::BASE_NUMBER; // to receive nonzero value
  856. bonus->subtype = it->subtype; //skill id
  857. bonus->val = it->val; //value per level, in percent
  858. speciality.addNewBonus(bonus);
  859. bonus = new Bonus(*bonus);
  860. switch (it->additionalinfo)
  861. {
  862. case 0: //normal
  863. bonus->valType = Bonus::PERCENT_TO_BASE;
  864. break;
  865. case 1: //when it's navigation or there's no 'base' at all
  866. bonus->valType = Bonus::PERCENT_TO_ALL;
  867. break;
  868. }
  869. bonus->type = Bonus::SECONDARY_SKILL_PREMY; //value will be calculated later
  870. speciality.addNewBonus(bonus);
  871. break;
  872. case 3://spell damage bonus, level dependent but calculated elsewhere
  873. bonus->type = Bonus::SPECIAL_SPELL_LEV;
  874. bonus->subtype = it->subtype;
  875. speciality.addNewBonus(bonus);
  876. break;
  877. case 4://creature stat boost
  878. switch (it->subtype)
  879. {
  880. case 1://attack
  881. bonus->type = Bonus::PRIMARY_SKILL;
  882. bonus->subtype = PrimarySkill::ATTACK;
  883. break;
  884. case 2://defense
  885. bonus->type = Bonus::PRIMARY_SKILL;
  886. bonus->subtype = PrimarySkill::DEFENSE;
  887. break;
  888. case 3:
  889. bonus->type = Bonus::CREATURE_DAMAGE;
  890. bonus->subtype = 0; //both min and max
  891. break;
  892. case 4://hp
  893. bonus->type = Bonus::STACK_HEALTH;
  894. break;
  895. case 5:
  896. bonus->type = Bonus::STACKS_SPEED;
  897. break;
  898. default:
  899. continue;
  900. }
  901. bonus->valType = Bonus::ADDITIVE_VALUE;
  902. bonus->limiter.reset(new CCreatureTypeLimiter (*VLC->creh->creatures[it->additionalinfo], true));
  903. speciality.addNewBonus(bonus);
  904. break;
  905. case 5://spell damage bonus in percent
  906. bonus->type = Bonus::SPECIFIC_SPELL_DAMAGE;
  907. bonus->valType = Bonus::BASE_NUMBER; // current spell system is screwed
  908. bonus->subtype = it->subtype; //spell id
  909. speciality.addNewBonus(bonus);
  910. break;
  911. case 6://damage bonus for bless (Adela)
  912. bonus->type = Bonus::SPECIAL_BLESS_DAMAGE;
  913. bonus->subtype = it->subtype; //spell id if you ever wanted to use it otherwise
  914. bonus->additionalInfo = it->additionalinfo; //damage factor
  915. speciality.addNewBonus(bonus);
  916. break;
  917. case 7://maxed mastery for spell
  918. bonus->type = Bonus::MAXED_SPELL;
  919. bonus->subtype = it->subtype; //spell i
  920. speciality.addNewBonus(bonus);
  921. break;
  922. case 8://peculiar spells - enchantments
  923. bonus->type = Bonus::SPECIAL_PECULIAR_ENCHANT;
  924. bonus->subtype = it->subtype; //spell id
  925. bonus->additionalInfo = it->additionalinfo;//0, 1 for Coronius
  926. speciality.addNewBonus(bonus);
  927. break;
  928. case 9://upgrade creatures
  929. {
  930. std::vector< ConstTransitivePtr<CCreature> >* creatures = &VLC->creh->creatures;
  931. bonus->type = Bonus::SPECIAL_UPGRADE;
  932. bonus->subtype = it->subtype; //base id
  933. bonus->additionalInfo = it->additionalinfo; //target id
  934. speciality.addNewBonus(bonus);
  935. bonus = new Bonus(*bonus);
  936. for (std::set<ui32>::iterator i = (*creatures)[it->subtype]->upgrades.begin();
  937. i != (*creatures)[it->subtype]->upgrades.end(); i++)
  938. {
  939. bonus->subtype = *i; //propagate for regular upgrades of base creature
  940. speciality.addNewBonus(bonus);
  941. bonus = new Bonus(*bonus);
  942. }
  943. vstd::clear_pointer(bonus);
  944. break;
  945. }
  946. case 10://resource generation
  947. bonus->type = Bonus::GENERATE_RESOURCE;
  948. bonus->subtype = it->subtype;
  949. speciality.addNewBonus(bonus);
  950. break;
  951. case 11://starting skill with mastery (Adrienne)
  952. cb->changeSecSkill(id, it->val, it->additionalinfo); //simply give it and forget
  953. break;
  954. case 12://army speed
  955. bonus->type = Bonus::STACKS_SPEED;
  956. speciality.addNewBonus(bonus);
  957. break;
  958. case 13://Dragon bonuses (Mutare)
  959. bonus->type = Bonus::PRIMARY_SKILL;
  960. bonus->valType = Bonus::ADDITIVE_VALUE;
  961. switch (it->subtype)
  962. {
  963. case 1:
  964. bonus->subtype = PrimarySkill::ATTACK;
  965. break;
  966. case 2:
  967. bonus->subtype = PrimarySkill::DEFENSE;
  968. break;
  969. }
  970. bonus->limiter.reset(new HasAnotherBonusLimiter(Bonus::DRAGON_NATURE));
  971. speciality.addNewBonus(bonus);
  972. break;
  973. default:
  974. tlog2 << "Unexpected hero speciality " << type <<'\n';
  975. }
  976. }
  977. //initialize bonuses
  978. for (std::vector<std::pair<ui8,ui8> >::iterator it = secSkills.begin(); it != secSkills.end(); it++)
  979. updateSkill(it->first, it->second);
  980. UpdateSpeciality();
  981. mana = manaLimit(); //after all bonuses are taken into account, make sure this line is the last one
  982. type->name = name;
  983. }
  984. void CGHeroInstance::UpdateSpeciality() //TODO: calculate special value of bonuses on-the-fly?
  985. {
  986. if (speciality.growthsWithLevel)
  987. {
  988. std::vector< ConstTransitivePtr<CCreature> > & creatures = VLC->creh->creatures;
  989. BOOST_FOREACH(Bonus *it, speciality.getBonusList())
  990. {
  991. switch (it->type)
  992. {
  993. case Bonus::SECONDARY_SKILL_PREMY:
  994. it->val = (speciality.valOfBonuses(Bonus::SPECIAL_SECONDARY_SKILL, it->subtype) * level);
  995. break; //use only hero skills as bonuses to avoid feedback loop
  996. case Bonus::PRIMARY_SKILL: //for crearures, that is
  997. int creLevel = creatures[it->additionalInfo]->level;
  998. if(!creLevel)
  999. {
  1000. if(it->additionalInfo == 146)
  1001. creLevel = 5; //treat ballista as 5-level
  1002. else
  1003. {
  1004. tlog2 << "Warning: unknown level of " << creatures[it->additionalInfo]->namePl << std::endl;
  1005. continue;
  1006. }
  1007. }
  1008. double primSkillModifier = (int)(level / creLevel) / 20.0;
  1009. int param;
  1010. switch (it->subtype)
  1011. {
  1012. case PrimarySkill::ATTACK:
  1013. param = creatures[it->additionalInfo]->Attack();
  1014. break;
  1015. case PrimarySkill::DEFENSE:
  1016. param = creatures[it->additionalInfo]->Defense();
  1017. break;
  1018. default:
  1019. assert(0);
  1020. param = 0;
  1021. }
  1022. it->val = ceil(param * (1 + primSkillModifier)) - param; //yep, overcomplicated but matches original
  1023. break;
  1024. }
  1025. }
  1026. }
  1027. }
  1028. void CGHeroInstance::updateSkill(int which, int val)
  1029. {
  1030. if(which == LEADERSHIP || which == LUCK)
  1031. { //luck-> VLC->generaltexth->arraytxt[73+luckSkill]; VLC->generaltexth->arraytxt[104+moraleSkill]
  1032. bool luck = which == LUCK;
  1033. Bonus::BonusType type[] = {Bonus::MORALE, Bonus::LUCK};
  1034. Bonus *b = getBonusLocalFirst(Selector::type(type[luck]) && Selector::sourceType(Bonus::SECONDARY_SKILL));
  1035. if(!b)
  1036. {
  1037. b = new Bonus(Bonus::PERMANENT, type[luck], Bonus::SECONDARY_SKILL, +val, which, which, Bonus::BASE_NUMBER);
  1038. addNewBonus(b);
  1039. }
  1040. else
  1041. b->val = +val;
  1042. }
  1043. else if(which == DIPLOMACY) //surrender discount: 20% per level
  1044. {
  1045. if(Bonus *b = getBonusLocalFirst(Selector::type(Bonus::SURRENDER_DISCOUNT) && Selector::sourceType(Bonus::SECONDARY_SKILL)))
  1046. b->val = +val;
  1047. else
  1048. addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::SURRENDER_DISCOUNT, Bonus::SECONDARY_SKILL, val * 20, which));
  1049. }
  1050. int skillVal = 0;
  1051. switch (which)
  1052. {
  1053. case ARCHERY:
  1054. switch (val)
  1055. {
  1056. case 1:
  1057. skillVal = 10; break;
  1058. case 2:
  1059. skillVal = 25; break;
  1060. case 3:
  1061. skillVal = 50; break;
  1062. }
  1063. break;
  1064. case LOGISTICS:
  1065. skillVal = 10 * val; break;
  1066. case NAVIGATION:
  1067. skillVal = 50 * val; break;
  1068. case MYSTICISM:
  1069. skillVal = val; break;
  1070. case EAGLE_EYE:
  1071. skillVal = 30 + 10 * val; break;
  1072. case NECROMANCY:
  1073. skillVal = 10 * val; break;
  1074. case LEARNING:
  1075. skillVal = 5 * val; break;
  1076. case OFFENCE:
  1077. skillVal = 10 * val; break;
  1078. case ARMORER:
  1079. skillVal = 5 * val; break;
  1080. case INTELLIGENCE:
  1081. skillVal = 25 << (val-1); break;
  1082. case SORCERY:
  1083. skillVal = 5 * val; break;
  1084. case RESISTANCE:
  1085. skillVal = 5 << (val-1); break;
  1086. case FIRST_AID:
  1087. skillVal = 25 + 25*val; break;
  1088. case ESTATES:
  1089. skillVal = 125 << (val-1); break;
  1090. }
  1091. int skillValType = skillVal ? Bonus::BASE_NUMBER : Bonus::INDEPENDENT_MIN;
  1092. if(Bonus * b = getBonusList().getFirst(Selector::typeSubtype(Bonus::SECONDARY_SKILL_PREMY, which) && Selector::sourceType(Bonus::SECONDARY_SKILL))) //only local hero bonus
  1093. {
  1094. b->val = skillVal;
  1095. b->valType = skillValType;
  1096. }
  1097. else
  1098. {
  1099. Bonus *bonus = new Bonus(Bonus::PERMANENT, Bonus::SECONDARY_SKILL_PREMY, id, skillVal, ID, which, skillValType);
  1100. bonus->source = Bonus::SECONDARY_SKILL;
  1101. addNewBonus(bonus);
  1102. }
  1103. }
  1104. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  1105. {
  1106. if(what == ObjProperty::PRIMARY_STACK_COUNT)
  1107. setStackCount(0, val);
  1108. }
  1109. double CGHeroInstance::getHeroStrength() const
  1110. {
  1111. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  1112. }
  1113. ui64 CGHeroInstance::getTotalStrength() const
  1114. {
  1115. double ret = getHeroStrength() * getArmyStrength();
  1116. return (ui64) ret;
  1117. }
  1118. TExpType CGHeroInstance::calculateXp(TExpType exp) const
  1119. {
  1120. return exp * (100 + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, CGHeroInstance::LEARNING))/100.0;
  1121. }
  1122. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell, int *outSelectedSchool) const
  1123. {
  1124. si16 skill = -1; //skill level
  1125. #define TRY_SCHOOL(schoolName, schoolMechanicsId, schoolOutId) \
  1126. if(spell-> schoolName) \
  1127. { \
  1128. int thisSchool = std::max<int>(getSecSkillLevel( \
  1129. static_cast<CGHeroInstance::SecondarySkill>(14 + (schoolMechanicsId))), \
  1130. valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 1 << (schoolMechanicsId))); \
  1131. if(thisSchool > skill) \
  1132. { \
  1133. skill = thisSchool; \
  1134. if(outSelectedSchool) \
  1135. *outSelectedSchool = schoolOutId; \
  1136. } \
  1137. }
  1138. TRY_SCHOOL(fire, 0, 1)
  1139. TRY_SCHOOL(air, 1, 0)
  1140. TRY_SCHOOL(water, 2, 2)
  1141. TRY_SCHOOL(earth, 3, 3)
  1142. #undef TRY_SCHOOL
  1143. vstd::amax(skill, valOfBonuses(Bonus::MAGIC_SCHOOL_SKILL, 0)); //any school bonus
  1144. vstd::amax(skill, valOfBonuses(Bonus::SPELL, spell->id)); //given by artifact or other effect
  1145. if (hasBonusOfType(Bonus::MAXED_SPELL, spell->id))//hero speciality (Daremyth, Melodia)
  1146. skill = 3;
  1147. assert(skill >= 0 && skill <= 3);
  1148. return skill;
  1149. }
  1150. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  1151. {
  1152. if(!getArt(17)) //if hero has no spellbook
  1153. return false;
  1154. if(vstd::contains(spells, spell->id) //hero has this spell in spellbook
  1155. || (spell->air && hasBonusOfType(Bonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  1156. || (spell->fire && hasBonusOfType(Bonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  1157. || (spell->water && hasBonusOfType(Bonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  1158. || (spell->earth && hasBonusOfType(Bonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  1159. || hasBonusOfType(Bonus::SPELL, spell->id)
  1160. || hasBonusOfType(Bonus::SPELLS_OF_LEVEL, spell->level)
  1161. )
  1162. return true;
  1163. return false;
  1164. }
  1165. /**
  1166. * Calculates what creatures and how many to be raised from a battle.
  1167. * @param battleResult The results of the battle.
  1168. * @return Returns a pair with the first value indicating the ID of the creature
  1169. * type and second value the amount. Both values are returned as -1 if necromancy
  1170. * could not be applied.
  1171. */
  1172. CStackBasicDescriptor CGHeroInstance::calculateNecromancy (const BattleResult &battleResult) const
  1173. {
  1174. const ui8 necromancyLevel = getSecSkillLevel(CGHeroInstance::NECROMANCY);
  1175. // Hero knows necromancy.
  1176. if (necromancyLevel > 0)
  1177. {
  1178. double necromancySkill = valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, NECROMANCY)/100.0;
  1179. vstd::amin(necromancySkill, 1.0); //it's impossible to raise more creatures than all...
  1180. const std::map<ui32,si32> &casualties = battleResult.casualties[!battleResult.winner];
  1181. ui32 raisedUnits = 0;
  1182. // Figure out what to raise and how many.
  1183. const ui32 creatureTypes[] = {56, 58, 60, 64}; // IDs for Skeletons, Walking Dead, Wights and Liches respectively.
  1184. const bool improvedNecromancy = hasBonusOfType(Bonus::IMPROVED_NECROMANCY);
  1185. const CCreature *raisedUnitType = VLC->creh->creatures[creatureTypes[improvedNecromancy ? necromancyLevel : 0]];
  1186. const ui32 raisedUnitHP = raisedUnitType->valOfBonuses(Bonus::STACK_HEALTH);
  1187. //calculate creatures raised from each defeated stack
  1188. for (std::map<ui32,si32>::const_iterator it = casualties.begin(); it != casualties.end(); it++)
  1189. {
  1190. // Get lost enemy hit points convertible to units.
  1191. CCreature * c = VLC->creh->creatures[it->first];
  1192. if (c->isLiving())
  1193. {
  1194. const ui32 raisedHP = c->valOfBonuses(Bonus::STACK_HEALTH) * it->second * necromancySkill;
  1195. raisedUnits += std::min<ui32>(raisedHP / raisedUnitHP, it->second * necromancySkill); //limit to % of HP and % of original stack count
  1196. }
  1197. }
  1198. // Make room for new units.
  1199. int slot = getSlotFor(raisedUnitType->idNumber);
  1200. if (slot == -1)
  1201. {
  1202. // If there's no room for unit, try it's upgraded version 2/3rds the size.
  1203. raisedUnitType = VLC->creh->creatures[*raisedUnitType->upgrades.begin()];
  1204. raisedUnits = (raisedUnits*2)/3;
  1205. slot = getSlotFor(raisedUnitType->idNumber);
  1206. }
  1207. if (raisedUnits <= 0)
  1208. raisedUnits = 1;
  1209. return CStackBasicDescriptor(raisedUnitType->idNumber, raisedUnits);
  1210. }
  1211. return CStackBasicDescriptor();
  1212. }
  1213. /**
  1214. * Show the necromancy dialog with information about units raised.
  1215. * @param raisedStack Pair where the first element represents ID of the raised creature
  1216. * and the second element the amount.
  1217. */
  1218. void CGHeroInstance::showNecromancyDialog(const CStackBasicDescriptor &raisedStack) const
  1219. {
  1220. InfoWindow iw;
  1221. iw.soundID = soundBase::pickup01 + ran() % 7;
  1222. iw.player = tempOwner;
  1223. iw.components.push_back(Component(raisedStack));
  1224. if (raisedStack.count > 1) // Practicing the dark arts of necromancy, ... (plural)
  1225. {
  1226. iw.text.addTxt(MetaString::GENERAL_TXT, 145);
  1227. iw.text.addReplacement(raisedStack.count);
  1228. iw.text.addReplacement(MetaString::CRE_PL_NAMES, raisedStack.type->idNumber);
  1229. }
  1230. else // Practicing the dark arts of necromancy, ... (singular)
  1231. {
  1232. iw.text.addTxt(MetaString::GENERAL_TXT, 146);
  1233. iw.text.addReplacement(MetaString::CRE_SING_NAMES, raisedStack.type->idNumber);
  1234. }
  1235. cb->showInfoDialog(&iw);
  1236. }
  1237. int3 CGHeroInstance::getSightCenter() const
  1238. {
  1239. return getPosition(false);
  1240. }
  1241. int CGHeroInstance::getSightRadious() const
  1242. {
  1243. return 5 + getSecSkillLevel(CGHeroInstance::SCOUTING) + valOfBonuses(Bonus::SIGHT_RADIOUS); //default + scouting
  1244. }
  1245. si32 CGHeroInstance::manaRegain() const
  1246. {
  1247. if (hasBonusOfType(Bonus::FULL_MANA_REGENERATION))
  1248. return manaLimit();
  1249. return 1 + valOfBonuses(Bonus::SECONDARY_SKILL_PREMY, 8) + valOfBonuses(Bonus::MANA_REGENERATION); //1 + Mysticism level
  1250. }
  1251. // /**
  1252. // * Places an artifact in hero's backpack. If it's a big artifact equips it
  1253. // * or discards it if it cannot be equipped.
  1254. // */
  1255. // void CGHeroInstance::giveArtifact (ui32 aid) //use only for fixed artifacts
  1256. // {
  1257. // CArtifact * const artifact = VLC->arth->artifacts[aid]; //pointer to constant object
  1258. // CArtifactInstance *ai = CArtifactInstance::createNewArtifactInstance(artifact);
  1259. // ai->putAt(this, ai->firstAvailableSlot(this));
  1260. // }
  1261. int CGHeroInstance::getBoatType() const
  1262. {
  1263. switch(type->heroClass->getAlignment())
  1264. {
  1265. case EAlignment::GOOD:
  1266. return 1;
  1267. case EAlignment::EVIL:
  1268. return 0;
  1269. case EAlignment::NEUTRAL:
  1270. return 2;
  1271. default:
  1272. throw std::runtime_error("Wrong alignment!");
  1273. }
  1274. }
  1275. void CGHeroInstance::getOutOffsets(std::vector<int3> &offsets) const
  1276. {
  1277. static int3 dirs[] = { int3(0,1,0),int3(0,-1,0),int3(-1,0,0),int3(+1,0,0), int3(1,1,0),int3(-1,1,0),int3(1,-1,0),int3(-1,-1,0) };
  1278. for (size_t i = 0; i < ARRAY_COUNT(dirs); i++)
  1279. offsets += dirs[i];
  1280. }
  1281. int CGHeroInstance::getSpellCost(const CSpell *sp) const
  1282. {
  1283. return sp->costs[getSpellSchoolLevel(sp)];
  1284. }
  1285. void CGHeroInstance::pushPrimSkill( int which, int val )
  1286. {
  1287. addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::PRIMARY_SKILL, Bonus::HERO_BASE_SKILL, val, id, which));
  1288. }
  1289. EAlignment::EAlignment CGHeroInstance::getAlignment() const
  1290. {
  1291. return type->heroClass->getAlignment();
  1292. }
  1293. void CGHeroInstance::initExp()
  1294. {
  1295. exp=40+ (ran()) % 50;
  1296. level = 1;
  1297. }
  1298. std::string CGHeroInstance::nodeName() const
  1299. {
  1300. return "Hero " + name;
  1301. }
  1302. void CGHeroInstance::putArtifact(ui16 pos, CArtifactInstance *art)
  1303. {
  1304. assert(!getArt(pos));
  1305. art->putAt(ArtifactLocation(this, pos));
  1306. }
  1307. void CGHeroInstance::putInBackpack(CArtifactInstance *art)
  1308. {
  1309. putArtifact(art->firstBackpackSlot(this), art);
  1310. }
  1311. bool CGHeroInstance::hasSpellbook() const
  1312. {
  1313. return getArt(ArtifactPosition::SPELLBOOK);
  1314. }
  1315. void CGHeroInstance::deserializationFix()
  1316. {
  1317. artDeserializationFix(this);
  1318. }
  1319. CBonusSystemNode * CGHeroInstance::whereShouldBeAttached(CGameState *gs)
  1320. {
  1321. if(visitedTown)
  1322. {
  1323. if(inTownGarrison)
  1324. return visitedTown;
  1325. else
  1326. return &visitedTown->townAndVis;
  1327. }
  1328. else
  1329. return CArmedInstance::whereShouldBeAttached(gs);
  1330. }
  1331. int CGHeroInstance::movementPointsAfterEmbark(int MPsBefore, int basicCost, bool disembark /*= false*/) const
  1332. {
  1333. if(hasBonusOfType(Bonus::FREE_SHIP_BOARDING))
  1334. return (MPsBefore - basicCost) * static_cast<double>(maxMovePoints(disembark)) / maxMovePoints(!disembark);
  1335. return 0; //take all MPs otherwise
  1336. }
  1337. CGHeroInstance::ECanDig CGHeroInstance::diggingStatus() const
  1338. {
  1339. if(movement < maxMovePoints(true))
  1340. return LACK_OF_MOVEMENT;
  1341. else if(cb->getTile(getPosition(false))->terType == ETerrainType::WATER)
  1342. return WRONG_TERRAIN;
  1343. else
  1344. {
  1345. const TerrainTile *t = cb->getTile(getPosition());
  1346. //TODO look for hole
  1347. //CGI->mh->getTerrainDescr(h->getPosition(false), hlp, false);
  1348. if(/*hlp.length() || */t->blockingObjects.size() > 1)
  1349. return TILE_OCCUPIED;
  1350. else
  1351. return CAN_DIG;
  1352. }
  1353. }
  1354. ui8 CGHeroInstance::bearerType() const
  1355. {
  1356. return ArtBearer::HERO;
  1357. }
  1358. void CGDwelling::initObj()
  1359. {
  1360. switch(ID)
  1361. {
  1362. case Obj::CREATURE_GENERATOR1:
  1363. {
  1364. int crid = VLC->objh->cregens[subID];
  1365. const CCreature *crs = VLC->creh->creatures[crid];
  1366. creatures.resize(1);
  1367. creatures[0].second.push_back(crid);
  1368. if (subID >= VLC->generaltexth->creGens.size()) //very messy workaround
  1369. {
  1370. int faction = VLC->creh->creatures[subID]->faction;
  1371. assert (VLC->townh->towns[faction].dwellingNames.size());
  1372. hoverName = VLC->townh->towns[faction].dwellingNames[VLC->creh->creatures[subID]->level - 1];
  1373. }
  1374. else
  1375. hoverName = VLC->generaltexth->creGens[subID];
  1376. if(crs->level > 4)
  1377. putStack(0, new CStackInstance(crs, (crs->growth) * 3));
  1378. if (getOwner() != GameConstants::NEUTRAL_PLAYER)
  1379. cb->gameState()->players[getOwner()].dwellings.push_back (this);
  1380. }
  1381. break;
  1382. case Obj::CREATURE_GENERATOR4:
  1383. creatures.resize(4);
  1384. if(subID == 1) //Golem Factory
  1385. {
  1386. creatures[0].second.push_back(32); //Stone Golem
  1387. creatures[1].second.push_back(33); //Iron Golem
  1388. creatures[2].second.push_back(116); //Gold Golem
  1389. creatures[3].second.push_back(117); //Diamond Golem
  1390. //guards
  1391. putStack(0, new CStackInstance(116, 9));
  1392. putStack(1, new CStackInstance(117, 6));
  1393. }
  1394. else if(subID == 0) // Elemental Conflux
  1395. {
  1396. creatures[0].second.push_back(112); //Air Elemental
  1397. creatures[1].second.push_back(114); //Fire Elemental
  1398. creatures[2].second.push_back(113); //Earth Elemental
  1399. creatures[3].second.push_back(115); //Water Elemental
  1400. //guards
  1401. putStack(0, new CStackInstance(113, 12));
  1402. }
  1403. else
  1404. {
  1405. assert(0);
  1406. }
  1407. hoverName = VLC->generaltexth->creGens4[subID];
  1408. break;
  1409. case Obj::REFUGEE_CAMP:
  1410. //is handled within newturn func
  1411. break;
  1412. case Obj::WAR_MACHINE_FACTORY:
  1413. creatures.resize(3);
  1414. creatures[0].second.push_back(146); //Ballista
  1415. creatures[1].second.push_back(147); //First Aid Tent
  1416. creatures[2].second.push_back(148); //Ammo Cart
  1417. break;
  1418. default:
  1419. assert(0);
  1420. break;
  1421. }
  1422. }
  1423. void CGDwelling::setProperty(ui8 what, ui32 val)
  1424. {
  1425. switch (what)
  1426. {
  1427. case ObjProperty::OWNER: //change owner
  1428. if (ID == Obj::CREATURE_GENERATOR1) //single generators
  1429. {
  1430. if (tempOwner != GameConstants::NEUTRAL_PLAYER)
  1431. {
  1432. std::vector<ConstTransitivePtr<CGDwelling> >* dwellings = &cb->gameState()->players[tempOwner].dwellings;
  1433. dwellings->erase (std::find(dwellings->begin(), dwellings->end(), this));
  1434. }
  1435. if (val != GameConstants::NEUTRAL_PLAYER) //can new owner be neutral?
  1436. cb->gameState()->players[val].dwellings.push_back (this);
  1437. }
  1438. break;
  1439. case ObjProperty::AVAILABLE_CREATURE:
  1440. creatures.resize(1);
  1441. creatures[0].second.resize(1);
  1442. creatures[0].second[0] = val;
  1443. break;
  1444. }
  1445. CGObjectInstance::setProperty(what,val);
  1446. }
  1447. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1448. {
  1449. if(ID == Obj::REFUGEE_CAMP && !creatures[0].first) //Refugee Camp, no available cres
  1450. {
  1451. InfoWindow iw;
  1452. iw.player = h->tempOwner;
  1453. iw.text.addTxt(MetaString::ADVOB_TXT, 44); //{%s} \n\n The camp is deserted. Perhaps you should try next week.
  1454. iw.text.addReplacement(MetaString::OBJ_NAMES, ID);
  1455. cb->sendAndApply(&iw);
  1456. return;
  1457. }
  1458. int relations = cb->gameState()->getPlayerRelations( h->tempOwner, tempOwner );
  1459. if ( relations == 1 )//ally
  1460. return;//do not allow recruiting or capturing
  1461. if( !relations && stacksCount() > 0) //object is guarded, owned by enemy
  1462. {
  1463. BlockingDialog bd;
  1464. bd.player = h->tempOwner;
  1465. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1466. bd.text.addTxt(MetaString::GENERAL_TXT, 421); //Much to your dismay, the %s is guarded by %s %s. Do you wish to fight the guards?
  1467. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1468. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + Slots().begin()->second->getQuantityID()*3);
  1469. bd.text.addReplacement(*Slots().begin()->second);
  1470. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  1471. return;
  1472. }
  1473. if(!relations && ID != Obj::WAR_MACHINE_FACTORY)
  1474. {
  1475. cb->setOwner(id, h->tempOwner);
  1476. }
  1477. BlockingDialog bd;
  1478. bd.player = h->tempOwner;
  1479. bd.flags = BlockingDialog::ALLOW_CANCEL;
  1480. if(ID == Obj::CREATURE_GENERATOR1 || ID == Obj::CREATURE_GENERATOR4)
  1481. {
  1482. bd.text.addTxt(MetaString::ADVOB_TXT, ID == Obj::CREATURE_GENERATOR1 ? 35 : 36); //{%s} Would you like to recruit %s? / {%s} Would you like to recruit %s, %s, %s, or %s?
  1483. bd.text.addReplacement(ID == Obj::CREATURE_GENERATOR1 ? MetaString::CREGENS : MetaString::CREGENS4, subID);
  1484. for(size_t i = 0; i < creatures.size(); i++)
  1485. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1486. }
  1487. else if(ID == Obj::REFUGEE_CAMP)
  1488. {
  1489. bd.text.addTxt(MetaString::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  1490. bd.text.addReplacement(MetaString::OBJ_NAMES, ID);
  1491. for(size_t i = 0; i < creatures.size(); i++)
  1492. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[i].second[0]);
  1493. }
  1494. else if(ID == Obj::WAR_MACHINE_FACTORY)
  1495. bd.text.addTxt(MetaString::ADVOB_TXT, 157); //{War Machine Factory} Would you like to purchase War Machines?
  1496. else
  1497. throw std::runtime_error("Illegal dwelling!");
  1498. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  1499. }
  1500. void CGDwelling::newTurn() const
  1501. {
  1502. if(cb->getDate(1) != 1) //not first day of week
  1503. return;
  1504. //town growths and War Machines Factories are handled separately
  1505. if(ID == Obj::TOWN || ID == Obj::WAR_MACHINE_FACTORY)
  1506. return;
  1507. if(ID == Obj::REFUGEE_CAMP) //if it's a refugee camp, we need to pick an available creature
  1508. {
  1509. cb->setObjProperty(id, ObjProperty::AVAILABLE_CREATURE, VLC->creh->pickRandomMonster());
  1510. }
  1511. bool change = false;
  1512. SetAvailableCreatures sac;
  1513. sac.creatures = creatures;
  1514. sac.tid = id;
  1515. for (size_t i = 0; i < creatures.size(); i++)
  1516. {
  1517. if(creatures[i].second.size())
  1518. {
  1519. CCreature *cre = VLC->creh->creatures[creatures[i].second[0]];
  1520. TQuantity amount = cre->growth * (1 + cre->valOfBonuses(Bonus::CREATURE_GROWTH_PERCENT)/100) + cre->valOfBonuses(Bonus::CREATURE_GROWTH);
  1521. if (VLC->modh->settings.DWELLINGS_ACCUMULATE_CREATURES)
  1522. sac.creatures[i].first += amount;
  1523. else
  1524. sac.creatures[i].first = amount;
  1525. change = true;
  1526. }
  1527. }
  1528. if(change)
  1529. cb->sendAndApply(&sac);
  1530. }
  1531. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  1532. {
  1533. if(!answer)
  1534. return;
  1535. int crid = creatures[0].second[0];
  1536. CCreature *crs = VLC->creh->creatures[crid];
  1537. TQuantity count = creatures[0].first;
  1538. if(crs->level == 1 && ID != Obj::REFUGEE_CAMP) //first level - creatures are for free
  1539. {
  1540. if(count) //there are available creatures
  1541. {
  1542. int slot = h->getSlotFor(crid);
  1543. if(slot < 0) //no available slot
  1544. {
  1545. InfoWindow iw;
  1546. iw.player = h->tempOwner;
  1547. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  1548. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1549. cb->showInfoDialog(&iw);
  1550. }
  1551. else //give creatures
  1552. {
  1553. SetAvailableCreatures sac;
  1554. sac.tid = id;
  1555. sac.creatures = creatures;
  1556. sac.creatures[0].first = 0;
  1557. InfoWindow iw;
  1558. iw.player = h->tempOwner;
  1559. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  1560. iw.text.addReplacement(count);
  1561. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1562. cb->showInfoDialog(&iw);
  1563. cb->sendAndApply(&sac);
  1564. cb->addToSlot(StackLocation(h, slot), crs, count);
  1565. }
  1566. }
  1567. else //there no creatures
  1568. {
  1569. InfoWindow iw;
  1570. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  1571. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  1572. iw.player = h->tempOwner;
  1573. cb->sendAndApply(&iw);
  1574. }
  1575. }
  1576. else
  1577. {
  1578. if(ID == Obj::WAR_MACHINE_FACTORY) //pick available War Machines
  1579. {
  1580. //there is 1 war machine available to recruit if hero doesn't have one
  1581. SetAvailableCreatures sac;
  1582. sac.tid = id;
  1583. sac.creatures = creatures;
  1584. sac.creatures[0].first = !h->getArt(13); //ballista
  1585. sac.creatures[1].first = !h->getArt(15); //first aid tent
  1586. sac.creatures[2].first = !h->getArt(14); //ammo cart
  1587. cb->sendAndApply(&sac);
  1588. }
  1589. OpenWindow ow;
  1590. ow.id1 = id;
  1591. ow.id2 = h->id;
  1592. ow.window = (ID == Obj::CREATURE_GENERATOR1 || ID == Obj::REFUGEE_CAMP)
  1593. ? OpenWindow::RECRUITMENT_FIRST
  1594. : OpenWindow::RECRUITMENT_ALL;
  1595. cb->sendAndApply(&ow);
  1596. }
  1597. }
  1598. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  1599. {
  1600. if(answer)
  1601. cb->startBattleI(h, this, boost::bind(&CGDwelling::fightOver, this, h, _1));
  1602. }
  1603. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  1604. {
  1605. if (result->winner == 0)
  1606. {
  1607. onHeroVisit(h);
  1608. }
  1609. }
  1610. int CGTownInstance::getSightRadious() const //returns sight distance
  1611. {
  1612. if (subID == ETownType::TOWER)
  1613. {
  1614. if (hasBuilt(EBuilding::GRAIL)) //skyship
  1615. return -1; //entire map
  1616. else if (hasBuilt(EBuilding::LOOKOUT_TOWER)) //lookout tower
  1617. return 20;
  1618. }
  1619. return 5;
  1620. }
  1621. void CGTownInstance::setPropertyDer(ui8 what, ui32 val)
  1622. {
  1623. ///this is freakin' overcomplicated solution
  1624. switch (what)
  1625. {
  1626. case 11: //add visitor of town building
  1627. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, visitingHero->id);
  1628. break;
  1629. case 12:
  1630. bonusingBuildings[val]->setProperty (12, 0);
  1631. break;
  1632. case 13: //add garrisoned hero to visitors
  1633. bonusingBuildings[val]->setProperty (ObjProperty::VISITORS, garrisonHero->id);
  1634. break;
  1635. case 14:
  1636. bonusValue.first = val;
  1637. break;
  1638. case 15:
  1639. bonusValue.second = val;
  1640. break;
  1641. }
  1642. }
  1643. CGTownInstance::EFortLevel CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  1644. {
  1645. if (hasBuilt(EBuilding::CASTLE))
  1646. return CASTLE;
  1647. if (hasBuilt(EBuilding::CITADEL))
  1648. return CITADEL;
  1649. if (hasBuilt(EBuilding::FORT))
  1650. return FORT;
  1651. return NONE;
  1652. }
  1653. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1654. {
  1655. if (hasBuilt(EBuilding::CAPITOL))
  1656. return 3;
  1657. if (hasBuilt(EBuilding::CITY_HALL))
  1658. return 2;
  1659. if (hasBuilt(EBuilding::TOWN_HALL))
  1660. return 1;
  1661. if (hasBuilt(EBuilding::VILLAGE_HALL))
  1662. return 0;
  1663. return -1;
  1664. }
  1665. int CGTownInstance::mageGuildLevel() const
  1666. {
  1667. if (hasBuilt(EBuilding::MAGES_GUILD_5))
  1668. return 5;
  1669. if (hasBuilt(EBuilding::MAGES_GUILD_4))
  1670. return 4;
  1671. if (hasBuilt(EBuilding::MAGES_GUILD_3))
  1672. return 3;
  1673. if (hasBuilt(EBuilding::MAGES_GUILD_2))
  1674. return 2;
  1675. if (hasBuilt(EBuilding::MAGES_GUILD_1))
  1676. return 1;
  1677. return 0;
  1678. }
  1679. int CGTownInstance::creatureDwellingLevel(int dwelling) const
  1680. {
  1681. if ( dwelling<0 || dwelling >= GameConstants::CREATURES_PER_TOWN )
  1682. return -1;
  1683. for (int i=0; ; i++)
  1684. {
  1685. if (!hasBuilt(EBuilding::DWELL_FIRST+dwelling+i*GameConstants::CREATURES_PER_TOWN))
  1686. return i-1;
  1687. }
  1688. }
  1689. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1690. {
  1691. return town->hordeLvl[HID];
  1692. }
  1693. int CGTownInstance::creatureGrowth(const int & level) const
  1694. {
  1695. return getGrowthInfo(level).totalGrowth();
  1696. }
  1697. GrowthInfo CGTownInstance::getGrowthInfo(int level) const
  1698. {
  1699. GrowthInfo ret;
  1700. if (level<0 || level >=GameConstants::CREATURES_PER_TOWN)
  1701. return ret;
  1702. if (!hasBuilt(EBuilding::DWELL_FIRST+level))
  1703. return ret; //no dwelling
  1704. const CCreature *creature = VLC->creh->creatures[creatures[level].second.back()];
  1705. const int base = creature->growth;
  1706. int castleBonus = 0;
  1707. ret.entries.push_back(GrowthInfo::Entry(VLC->generaltexth->allTexts[590], base));// \n\nBasic growth %d"
  1708. if (hasBuilt(EBuilding::CASTLE))
  1709. ret.entries.push_back(GrowthInfo::Entry(subID, EBuilding::CASTLE, castleBonus = base));
  1710. else if (hasBuilt(EBuilding::CITADEL))
  1711. ret.entries.push_back(GrowthInfo::Entry(subID, EBuilding::CITADEL, castleBonus = base / 2));
  1712. if(town->hordeLvl[0] == level)//horde 1
  1713. if(hasBuilt(EBuilding::HORDE_1))
  1714. ret.entries.push_back(GrowthInfo::Entry(subID, EBuilding::HORDE_1, creature->hordeGrowth));
  1715. if(town->hordeLvl[1] == level)//horde 2
  1716. if(hasBuilt(EBuilding::HORDE_2))
  1717. ret.entries.push_back(GrowthInfo::Entry(subID, EBuilding::HORDE_2, creature->hordeGrowth));
  1718. int dwellingBonus = 0;
  1719. if(const PlayerState *p = cb->getPlayer(tempOwner, false))
  1720. {
  1721. BOOST_FOREACH(const CGDwelling *dwelling, p->dwellings)
  1722. if(vstd::contains(creatures[level].second, dwelling->creatures[0].second[0]))
  1723. dwellingBonus++;
  1724. }
  1725. if(dwellingBonus)
  1726. ret.entries.push_back(GrowthInfo::Entry(VLC->generaltexth->allTexts[591], dwellingBonus));// \nExternal dwellings %+d
  1727. //other *-of-legion-like bonuses (%d to growth cumulative with grail)
  1728. TBonusListPtr bonuses = getBonuses(Selector::type(Bonus::CREATURE_GROWTH) && Selector::subtype(level));
  1729. BOOST_FOREACH(const Bonus *b, *bonuses)
  1730. ret.entries.push_back(GrowthInfo::Entry(b->Description() + " %+d", b->val));
  1731. //statue-of-legion-like bonus: % to base+castle
  1732. TBonusListPtr bonuses2 = getBonuses(Selector::type(Bonus::CREATURE_GROWTH_PERCENT));
  1733. BOOST_FOREACH(const Bonus *b, *bonuses2)
  1734. ret.entries.push_back(GrowthInfo::Entry(b->Description() + " %+d", b->val * (base + castleBonus) / 100));
  1735. if(hasBuilt(EBuilding::GRAIL)) //grail - +50% to ALL (so far added) growth
  1736. ret.entries.push_back(GrowthInfo::Entry(subID, EBuilding::GRAIL, ret.totalGrowth() / 2));
  1737. return ret;
  1738. }
  1739. int CGTownInstance::dailyIncome() const
  1740. {
  1741. int ret = 0;
  1742. if (hasBuilt(EBuilding::GRAIL))
  1743. ret+=5000;
  1744. if (hasBuilt(EBuilding::CAPITOL))
  1745. ret+=4000;
  1746. else if (hasBuilt(EBuilding::CITY_HALL))
  1747. ret+=2000;
  1748. else if (hasBuilt(EBuilding::TOWN_HALL))
  1749. ret+=1000;
  1750. else if (hasBuilt(EBuilding::VILLAGE_HALL))
  1751. ret+=500;
  1752. return ret;
  1753. }
  1754. bool CGTownInstance::hasFort() const
  1755. {
  1756. return hasBuilt(EBuilding::FORT);
  1757. }
  1758. bool CGTownInstance::hasCapitol() const
  1759. {
  1760. return hasBuilt(EBuilding::CAPITOL);
  1761. }
  1762. CGTownInstance::CGTownInstance()
  1763. :IShipyard(this), IMarket(this), town(nullptr), builded(0), destroyed(0), identifier(0), alignment(0xff)
  1764. {
  1765. }
  1766. CGTownInstance::~CGTownInstance()
  1767. {
  1768. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i != bonusingBuildings.end(); i++)
  1769. delete *i;
  1770. }
  1771. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1772. {
  1773. if(checkGuild && mageGuildLevel() < level)
  1774. return 0;
  1775. int ret = 6 - level; //how many spells are available at this level
  1776. if (hasBuilt(EBuilding::LIBRARY, ETownType::TOWER))
  1777. ret++;
  1778. return ret;
  1779. }
  1780. bool CGTownInstance::needsLastStack() const
  1781. {
  1782. if(garrisonHero)
  1783. return true;
  1784. else return false;
  1785. }
  1786. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1787. {
  1788. if( !cb->gameState()->getPlayerRelations( getOwner(), h->getOwner() ))//if this is enemy
  1789. {
  1790. if(armedGarrison() || visitingHero)
  1791. {
  1792. const CGHeroInstance *defendingHero = NULL;
  1793. const CArmedInstance *defendingArmy = this;
  1794. if(visitingHero)
  1795. defendingHero = visitingHero;
  1796. else if(garrisonHero)
  1797. defendingHero = garrisonHero;
  1798. if(defendingHero)
  1799. defendingArmy = defendingHero;
  1800. bool outsideTown = (defendingHero == visitingHero && garrisonHero);
  1801. //TODO
  1802. //"borrowing" army from garrison to visiting hero
  1803. cb->startBattleI(h, defendingArmy, getSightCenter(), h, defendingHero, false, boost::bind(&CGTownInstance::fightOver, this, h, _1), (outsideTown ? NULL : this));
  1804. }
  1805. else
  1806. {
  1807. cb->setOwner(id, h->tempOwner);
  1808. removeCapitols(h->getOwner());
  1809. cb->heroVisitCastle(id, h->id);
  1810. }
  1811. }
  1812. else
  1813. {
  1814. if (h->commander && !h->commander->alive) //rise commander. TODO: interactive script
  1815. {
  1816. SetCommanderProperty scp;
  1817. scp.heroid = h->id;
  1818. scp.which = SetCommanderProperty::ALIVE;
  1819. scp.amount = 1;
  1820. cb->sendAndApply (&scp);
  1821. }
  1822. cb->heroVisitCastle(id, h->id);
  1823. }
  1824. }
  1825. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1826. {
  1827. cb->stopHeroVisitCastle(id,h->id);
  1828. }
  1829. void CGTownInstance::initObj()
  1830. ///initialize town structures
  1831. {
  1832. blockVisit = true;
  1833. hoverName = name + ", " + VLC->townh->factions[town->typeID].name;
  1834. if (subID == ETownType::DUNGEON)
  1835. creatures.resize(GameConstants::CREATURES_PER_TOWN+1);//extra dwelling for Dungeon
  1836. else
  1837. creatures.resize(GameConstants::CREATURES_PER_TOWN);
  1838. for (int i = 0; i < GameConstants::CREATURES_PER_TOWN; i++)
  1839. {
  1840. int dwellingLevel = creatureDwellingLevel(i);
  1841. int creaturesTotal = town->creatures[i].size();
  1842. for (int j=0; j< std::min(dwellingLevel + 1, creaturesTotal); j++)
  1843. creatures[i].second.push_back(town->creatures[i][j]);
  1844. }
  1845. switch (subID)
  1846. { //add new visitable objects
  1847. case 0:
  1848. bonusingBuildings.push_back (new COPWBonus(21, this)); //Stables
  1849. break;
  1850. case 5:
  1851. bonusingBuildings.push_back (new COPWBonus(21, this)); //Vortex
  1852. case 2: case 3: case 6:
  1853. bonusingBuildings.push_back (new CTownBonus(23, this));
  1854. break;
  1855. case 7:
  1856. bonusingBuildings.push_back (new CTownBonus(17, this));
  1857. break;
  1858. }
  1859. //add special bonuses from buildings
  1860. recreateBuildingsBonuses();
  1861. }
  1862. void CGTownInstance::newTurn() const
  1863. {
  1864. if (cb->getDate(1) == 1) //reset on new week
  1865. {
  1866. //give resources for Rampart, Mystic Pond
  1867. if (hasBuilt(EBuilding::MYSTIC_POND, ETownType::RAMPART)
  1868. && cb->getDate(0) != 1 && (tempOwner < GameConstants::PLAYER_LIMIT))
  1869. {
  1870. int resID = rand()%4+2;//bonus to random rare resource
  1871. resID = (resID==2)?1:resID;
  1872. int resVal = rand()%4+1;//with size 1..4
  1873. cb->giveResource(tempOwner, resID, resVal);
  1874. cb->setObjProperty (id, 14, resID);
  1875. cb->setObjProperty (id, 15, resVal);
  1876. }
  1877. if ( subID == ETownType::DUNGEON )
  1878. for (std::vector<CGTownBuilding*>::const_iterator i = bonusingBuildings.begin(); i!=bonusingBuildings.end(); i++)
  1879. {
  1880. if ((*i)->ID == EBuilding::MANA_VORTEX)
  1881. cb->setObjProperty (id, 12, (*i)->id); //reset visitors for Mana Vortex
  1882. }
  1883. if (tempOwner == GameConstants::NEUTRAL_PLAYER) //garrison growth for neutral towns
  1884. {
  1885. std::vector<ui8> nativeCrits; //slots
  1886. for (TSlots::const_iterator it = Slots().begin(); it != Slots().end(); it++)
  1887. {
  1888. if (it->second->type->faction == subID) //native
  1889. {
  1890. nativeCrits.push_back(it->first); //collect matching slots
  1891. }
  1892. }
  1893. if (nativeCrits.size())
  1894. {
  1895. TSlot pos = nativeCrits[rand() % nativeCrits.size()];
  1896. StackLocation sl(this, pos);
  1897. const CCreature *c = getCreature(pos);
  1898. if (rand()%100 < 90 || c->upgrades.empty()) //increase number if no upgrade available
  1899. {
  1900. cb->changeStackCount(sl, c->growth);
  1901. }
  1902. else //upgrade
  1903. {
  1904. cb->changeStackType(sl, VLC->creh->creatures[*c->upgrades.begin()]);
  1905. }
  1906. }
  1907. if ((stacksCount() < GameConstants::ARMY_SIZE && rand()%100 < 25) || Slots().empty()) //add new stack
  1908. {
  1909. int i = rand() % std::min (GameConstants::ARMY_SIZE, cb->getDate(3)<<1);
  1910. TCreature c = town->creatures[i][0];
  1911. TSlot n = -1;
  1912. TQuantity count = creatureGrowth(i);
  1913. if (!count) // no dwelling
  1914. count = VLC->creh->creatures[c]->growth;
  1915. {//no lower tiers or above current month
  1916. if ((n = getSlotFor(c))>=0)
  1917. {
  1918. StackLocation sl(this, n);
  1919. if (slotEmpty(n))
  1920. cb->insertNewStack(sl, VLC->creh->creatures[c], count);
  1921. else //add to existing
  1922. cb->changeStackCount(sl, count);
  1923. }
  1924. }
  1925. }
  1926. }
  1927. }
  1928. }
  1929. int3 CGTownInstance::getSightCenter() const
  1930. {
  1931. return pos - int3(2,0,0);
  1932. }
  1933. ui8 CGTownInstance::getPassableness() const
  1934. {
  1935. if (!armedGarrison())//empty castle - anyone can visit
  1936. return GameConstants::ALL_PLAYERS;
  1937. if ( tempOwner == GameConstants::NEUTRAL_PLAYER )//neutral guarded - no one can visit
  1938. return 0;
  1939. ui8 mask = 0;
  1940. TeamState * ts = cb->gameState()->getPlayerTeam(tempOwner);
  1941. BOOST_FOREACH(ui8 it, ts->players)
  1942. mask |= 1<<it;//allies - add to possible visitors
  1943. return mask;
  1944. }
  1945. void CGTownInstance::getOutOffsets( std::vector<int3> &offsets ) const
  1946. {
  1947. offsets += int3(-1,2,0), int3(-3,2,0);
  1948. }
  1949. void CGTownInstance::fightOver( const CGHeroInstance *h, BattleResult *result ) const
  1950. {
  1951. if(result->winner == 0)
  1952. {
  1953. removeCapitols (h->getOwner());
  1954. cb->setOwner (id, h->tempOwner); //give control after checkout is done
  1955. FoWChange fw;
  1956. fw.player = h->tempOwner;
  1957. fw.mode = 1;
  1958. getSightTiles (fw.tiles); //update visibility for castle structures
  1959. cb->sendAndApply (&fw);
  1960. }
  1961. }
  1962. void CGTownInstance::removeCapitols (ui8 owner) const
  1963. {
  1964. if (hasCapitol()) // search if there's an older capitol
  1965. {
  1966. PlayerState* state = cb->gameState()->getPlayer (owner); //get all towns owned by player
  1967. for (std::vector<ConstTransitivePtr<CGTownInstance> >::const_iterator i = state->towns.begin(); i < state->towns.end(); ++i)
  1968. {
  1969. if (*i != this && (*i)->hasCapitol())
  1970. {
  1971. RazeStructures rs;
  1972. rs.tid = id;
  1973. rs.bid.insert(13);
  1974. rs.destroyed = destroyed;
  1975. cb->sendAndApply(&rs);
  1976. return;
  1977. }
  1978. }
  1979. }
  1980. }
  1981. int CGTownInstance::getBoatType() const
  1982. {
  1983. const CCreature *c = VLC->creh->creatures[town->creatures.front().front()];
  1984. if (c->isGood())
  1985. return 1;
  1986. else if (c->isEvil())
  1987. return 0;
  1988. else //neutral
  1989. return 2;
  1990. }
  1991. int CGTownInstance::getMarketEfficiency() const
  1992. {
  1993. if (!hasBuilt(EBuilding::MARKETPLACE))
  1994. return 0;
  1995. const PlayerState *p = cb->getPlayer(tempOwner);
  1996. assert(p);
  1997. int marketCount = 0;
  1998. BOOST_FOREACH(const CGTownInstance *t, p->towns)
  1999. if(t->hasBuilt(EBuilding::MARKETPLACE))
  2000. marketCount++;
  2001. return marketCount;
  2002. }
  2003. bool CGTownInstance::allowsTrade(EMarketMode::EMarketMode mode) const
  2004. {
  2005. switch(mode)
  2006. {
  2007. case EMarketMode::RESOURCE_RESOURCE:
  2008. case EMarketMode::RESOURCE_PLAYER:
  2009. return hasBuilt(EBuilding::MARKETPLACE);
  2010. case EMarketMode::ARTIFACT_RESOURCE:
  2011. case EMarketMode::RESOURCE_ARTIFACT:
  2012. return hasBuilt(EBuilding::ARTIFACT_MERCHANT, ETownType::TOWER)
  2013. || hasBuilt(EBuilding::ARTIFACT_MERCHANT, ETownType::DUNGEON)
  2014. || hasBuilt(EBuilding::ARTIFACT_MERCHANT, ETownType::CONFLUX);
  2015. case EMarketMode::CREATURE_RESOURCE:
  2016. return hasBuilt(EBuilding::FREELANCERS_GUILD, ETownType::STRONGHOLD);
  2017. case EMarketMode::CREATURE_UNDEAD:
  2018. return hasBuilt(EBuilding::SKELETON_TRANSFORMER, ETownType::NECROPOLIS);
  2019. case EMarketMode::RESOURCE_SKILL:
  2020. return hasBuilt(EBuilding::MAGIC_UNIVERSITY, ETownType::CONFLUX);
  2021. default:
  2022. assert(0);
  2023. return false;
  2024. }
  2025. }
  2026. std::vector<int> CGTownInstance::availableItemsIds(EMarketMode::EMarketMode mode) const
  2027. {
  2028. if(mode == EMarketMode::RESOURCE_ARTIFACT)
  2029. {
  2030. std::vector<int> ret;
  2031. BOOST_FOREACH(const CArtifact *a, merchantArtifacts)
  2032. if(a)
  2033. ret.push_back(a->id);
  2034. else
  2035. ret.push_back(-1);
  2036. return ret;
  2037. }
  2038. else if ( mode == EMarketMode::RESOURCE_SKILL )
  2039. {
  2040. return universitySkills;
  2041. }
  2042. else
  2043. return IMarket::availableItemsIds(mode);
  2044. }
  2045. std::string CGTownInstance::nodeName() const
  2046. {
  2047. return "Town (" + (town ? VLC->townh->factions[town->typeID].name : "unknown") + ") of " + name;
  2048. }
  2049. void CGTownInstance::deserializationFix()
  2050. {
  2051. attachTo(&townAndVis);
  2052. if(visitingHero)
  2053. visitingHero->attachTo(&townAndVis);
  2054. if(garrisonHero)
  2055. garrisonHero->attachTo(this);
  2056. }
  2057. void CGTownInstance::recreateBuildingsBonuses()
  2058. {
  2059. BonusList bl;
  2060. getExportedBonusList().getBonuses(bl, Selector::sourceType(Bonus::TOWN_STRUCTURE));
  2061. BOOST_FOREACH(Bonus *b, bl)
  2062. removeBonus(b);
  2063. //tricky! -> checks tavern only if no bratherhood of sword or not a castle
  2064. if(subID != ETownType::CASTLE || !addBonusIfBuilt(EBuilding::BROTHERHOOD, Bonus::MORALE, +2))
  2065. addBonusIfBuilt(EBuilding::TAVERN, Bonus::MORALE, +1);
  2066. if(subID == ETownType::CASTLE) //castle
  2067. {
  2068. addBonusIfBuilt(EBuilding::LIGHTHOUSE, Bonus::SEA_MOVEMENT, +500, make_shared<CPropagatorNodeType>(PLAYER));
  2069. addBonusIfBuilt(EBuilding::GRAIL, Bonus::MORALE, +2, make_shared<CPropagatorNodeType>(PLAYER)); //colossus
  2070. }
  2071. else if(subID == ETownType::RAMPART) //rampart
  2072. {
  2073. addBonusIfBuilt(EBuilding::FOUNTAIN_OF_FORTUNE, Bonus::LUCK, +2); //fountain of fortune
  2074. addBonusIfBuilt(EBuilding::GRAIL, Bonus::LUCK, +2, make_shared<CPropagatorNodeType>(PLAYER)); //guardian spirit
  2075. }
  2076. else if(subID == ETownType::TOWER) //tower
  2077. {
  2078. addBonusIfBuilt(EBuilding::GRAIL, Bonus::PRIMARY_SKILL, +15, PrimarySkill::KNOWLEDGE); //grail
  2079. }
  2080. else if(subID == ETownType::INFERNO) //Inferno
  2081. {
  2082. addBonusIfBuilt(EBuilding::STORMCLOUDS, Bonus::PRIMARY_SKILL, +2, PrimarySkill::SPELL_POWER); //Brimstone Clouds
  2083. }
  2084. else if(subID == ETownType::NECROPOLIS) //necropolis
  2085. {
  2086. addBonusIfBuilt(EBuilding::COVER_OF_DARKNESS, Bonus::DARKNESS, +20);
  2087. addBonusIfBuilt(EBuilding::NECROMANCY_AMPLIFIER, Bonus::SECONDARY_SKILL_PREMY, +10, make_shared<CPropagatorNodeType>(PLAYER), CGHeroInstance::NECROMANCY); //necromancy amplifier
  2088. addBonusIfBuilt(EBuilding::GRAIL, Bonus::SECONDARY_SKILL_PREMY, +20, make_shared<CPropagatorNodeType>(PLAYER), CGHeroInstance::NECROMANCY); //Soul prison
  2089. }
  2090. else if(subID == ETownType::DUNGEON) //Dungeon
  2091. {
  2092. addBonusIfBuilt(EBuilding::GRAIL, Bonus::PRIMARY_SKILL, +12, PrimarySkill::SPELL_POWER); //grail
  2093. }
  2094. else if(subID == ETownType::STRONGHOLD) //Stronghold
  2095. {
  2096. addBonusIfBuilt(EBuilding::GRAIL, Bonus::PRIMARY_SKILL, +20, PrimarySkill::ATTACK); //grail
  2097. }
  2098. else if(subID == ETownType::FORTRESS) //Fortress
  2099. {
  2100. addBonusIfBuilt(EBuilding::GLYPHS_OF_FEAR, Bonus::PRIMARY_SKILL, +2, PrimarySkill::DEFENSE); //Glyphs of Fear
  2101. addBonusIfBuilt(EBuilding::BLOOD_OBELISK, Bonus::PRIMARY_SKILL, +2, PrimarySkill::ATTACK); //Blood Obelisk
  2102. addBonusIfBuilt(EBuilding::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::ATTACK); //grail
  2103. addBonusIfBuilt(EBuilding::GRAIL, Bonus::PRIMARY_SKILL, +10, PrimarySkill::DEFENSE); //grail
  2104. }
  2105. else if(subID == ETownType::CONFLUX)
  2106. {
  2107. }
  2108. }
  2109. bool CGTownInstance::addBonusIfBuilt(int building, int type, int val, int subtype /*= -1*/)
  2110. {
  2111. return addBonusIfBuilt(building, type, val, TPropagatorPtr(), subtype);
  2112. }
  2113. bool CGTownInstance::addBonusIfBuilt(int building, int type, int val, TPropagatorPtr prop, int subtype /*= -1*/)
  2114. {
  2115. if(hasBuilt(building))
  2116. {
  2117. std::ostringstream descr;
  2118. descr << town->buildings[building]->Name() << " ";
  2119. if(val > 0)
  2120. descr << "+";
  2121. else if(val < 0)
  2122. descr << "-";
  2123. descr << val;
  2124. Bonus *b = new Bonus(Bonus::PERMANENT, type, Bonus::TOWN_STRUCTURE, val, building, descr.str(), subtype);
  2125. if(prop)
  2126. b->addPropagator(prop);
  2127. addNewBonus(b);
  2128. return true;
  2129. }
  2130. return false;
  2131. }
  2132. void CGTownInstance::setVisitingHero(CGHeroInstance *h)
  2133. {
  2134. assert(!!visitingHero == !h);
  2135. if(h)
  2136. {
  2137. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  2138. assert(p);
  2139. h->detachFrom(p);
  2140. h->attachTo(&townAndVis);
  2141. visitingHero = h;
  2142. h->visitedTown = this;
  2143. h->inTownGarrison = false;
  2144. }
  2145. else
  2146. {
  2147. PlayerState *p = cb->gameState()->getPlayer(visitingHero->tempOwner);
  2148. visitingHero->visitedTown = NULL;
  2149. visitingHero->detachFrom(&townAndVis);
  2150. visitingHero->attachTo(p);
  2151. visitingHero = NULL;
  2152. }
  2153. }
  2154. void CGTownInstance::setGarrisonedHero(CGHeroInstance *h)
  2155. {
  2156. assert(!!garrisonHero == !h);
  2157. if(h)
  2158. {
  2159. PlayerState *p = cb->gameState()->getPlayer(h->tempOwner);
  2160. assert(p);
  2161. h->detachFrom(p);
  2162. h->attachTo(this);
  2163. garrisonHero = h;
  2164. h->visitedTown = this;
  2165. h->inTownGarrison = true;
  2166. }
  2167. else
  2168. {
  2169. PlayerState *p = cb->gameState()->getPlayer(garrisonHero->tempOwner);
  2170. garrisonHero->visitedTown = NULL;
  2171. garrisonHero->inTownGarrison = false;
  2172. garrisonHero->detachFrom(this);
  2173. garrisonHero->attachTo(p);
  2174. garrisonHero = NULL;
  2175. }
  2176. }
  2177. bool CGTownInstance::armedGarrison() const
  2178. {
  2179. return stacksCount() || garrisonHero;
  2180. }
  2181. CBonusSystemNode * CGTownInstance::whatShouldBeAttached()
  2182. {
  2183. return &townAndVis;
  2184. }
  2185. const CArmedInstance * CGTownInstance::getUpperArmy() const
  2186. {
  2187. if(garrisonHero)
  2188. return garrisonHero;
  2189. return this;
  2190. }
  2191. bool CGTownInstance::hasBuilt(int buildingID, int townID) const
  2192. {
  2193. if (townID == town->typeID || townID == ETownType::ANY)
  2194. return hasBuilt(buildingID);
  2195. return false;
  2196. }
  2197. bool CGTownInstance::hasBuilt(int buildingID) const
  2198. {
  2199. return vstd::contains(builtBuildings, buildingID);
  2200. }
  2201. bool CGVisitableOPH::wasVisited (const CGHeroInstance * h) const
  2202. {
  2203. return vstd::contains(visitors, h->id);
  2204. }
  2205. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  2206. {
  2207. if(visitors.find(h->id)==visitors.end())
  2208. {
  2209. onNAHeroVisit(h->id, false);
  2210. switch(ID)
  2211. {
  2212. case Obj::TREE_OF_KNOWLEDGE:
  2213. case Obj::ARENA:
  2214. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  2215. case Obj::SCHOOL_OF_MAGIC:
  2216. case Obj::SCHOOL_OF_WAR:
  2217. break;
  2218. default:
  2219. cb->setObjProperty(id, ObjProperty::VISITORS, h->id); //add to the visitors
  2220. break;
  2221. }
  2222. }
  2223. else
  2224. {
  2225. onNAHeroVisit(h->id, true);
  2226. }
  2227. }
  2228. void CGVisitableOPH::initObj()
  2229. {
  2230. if(ID==Obj::TREE_OF_KNOWLEDGE)
  2231. ttype = ran()%3;
  2232. else
  2233. ttype = -1;
  2234. }
  2235. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, TExpType expVal, ui32 result ) const
  2236. {
  2237. if(result) //player agreed to give res for exp
  2238. {
  2239. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  2240. cb->changePrimSkill(heroID,4,expVal); //give exp
  2241. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  2242. }
  2243. }
  2244. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  2245. {
  2246. Component::EComponentType id = (Component::EComponentType)0;
  2247. int subid=0, ot=0, sound = 0;
  2248. TExpType val=1;
  2249. switch(ID)
  2250. {
  2251. case Obj::ARENA:
  2252. sound = soundBase::NOMAD;
  2253. ot = 0;
  2254. break;
  2255. case Obj::MERCENARY_CAMP:
  2256. sound = soundBase::NOMAD;
  2257. subid=0;
  2258. ot=80;
  2259. break;
  2260. case Obj::MARLETTO_TOWER:
  2261. sound = soundBase::NOMAD;
  2262. subid=1;
  2263. ot=39;
  2264. break;
  2265. case Obj::STAR_AXIS:
  2266. sound = soundBase::gazebo;
  2267. subid=2;
  2268. ot=100;
  2269. break;
  2270. case Obj::GARDEN_OF_REVELATION:
  2271. sound = soundBase::GETPROTECTION;
  2272. subid=3;
  2273. ot=59;
  2274. break;
  2275. case Obj::LEARNING_STONE:
  2276. sound = soundBase::gazebo;
  2277. id=Component::EXPERIENCE;
  2278. ot=143;
  2279. val=1000;
  2280. break;
  2281. case Obj::TREE_OF_KNOWLEDGE:
  2282. sound = soundBase::gazebo;
  2283. id = Component::EXPERIENCE;
  2284. subid = 1;
  2285. ot = 146;
  2286. val = 1;
  2287. break;
  2288. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  2289. sound = soundBase::gazebo;
  2290. ot = 66;
  2291. break;
  2292. case Obj::SCHOOL_OF_MAGIC:
  2293. sound = soundBase::faerie;
  2294. ot = 71;
  2295. break;
  2296. case Obj::SCHOOL_OF_WAR:
  2297. sound = soundBase::MILITARY;
  2298. ot = 158;
  2299. break;
  2300. }
  2301. if (!alreadyVisited)
  2302. {
  2303. switch (ID)
  2304. {
  2305. case Obj::ARENA:
  2306. {
  2307. BlockingDialog sd(false,true);
  2308. sd.soundID = sound;
  2309. sd.text << std::pair<ui8,ui32>(11,ot);
  2310. sd.components.push_back(Component(Component::PRIM_SKILL, PrimarySkill::ATTACK, 2, 0));
  2311. sd.components.push_back(Component(Component::PRIM_SKILL, PrimarySkill::DEFENSE, 2, 0));
  2312. sd.player = cb->getOwner(heroID);
  2313. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  2314. return;
  2315. }
  2316. case Obj::MERCENARY_CAMP:
  2317. case Obj::MARLETTO_TOWER:
  2318. case Obj::STAR_AXIS:
  2319. case Obj::GARDEN_OF_REVELATION:
  2320. {
  2321. cb->changePrimSkill(heroID,subid,val);
  2322. InfoWindow iw;
  2323. iw.soundID = sound;
  2324. iw.components.push_back(Component(Component::PRIM_SKILL, subid,val,0));
  2325. iw.text << std::pair<ui8,ui32>(11,ot);
  2326. iw.player = cb->getOwner(heroID);
  2327. cb->showInfoDialog(&iw);
  2328. break;
  2329. }
  2330. case Obj::LEARNING_STONE: //give exp
  2331. {
  2332. const CGHeroInstance *h = cb->getHero(heroID);
  2333. val = h->calculateXp(val);
  2334. InfoWindow iw;
  2335. iw.soundID = sound;
  2336. iw.components.push_back(Component(id,subid,val,0));
  2337. iw.player = cb->getOwner(heroID);
  2338. iw.text << std::pair<ui8,ui32>(11,ot);
  2339. iw.soundID = soundBase::gazebo;
  2340. cb->showInfoDialog(&iw);
  2341. cb->changePrimSkill(heroID,4,val);
  2342. break;
  2343. }
  2344. case Obj::TREE_OF_KNOWLEDGE:
  2345. {
  2346. const CGHeroInstance *h = cb->getHero(heroID);
  2347. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  2348. if(!ttype)
  2349. {
  2350. cb->setObjProperty(this->id, ObjProperty::VISITORS, heroID); //add to the visitors
  2351. InfoWindow iw;
  2352. iw.soundID = sound;
  2353. iw.components.push_back(Component(id,subid,1,0));
  2354. iw.player = cb->getOwner(heroID);
  2355. iw.text << std::pair<ui8,ui32>(11,148);
  2356. cb->showInfoDialog(&iw);
  2357. cb->changePrimSkill(heroID,4,val);
  2358. break;
  2359. }
  2360. else
  2361. {
  2362. ui32 res;
  2363. TExpType resval;
  2364. if(ttype==1)
  2365. {
  2366. res = 6;
  2367. resval = 2000;
  2368. ot = 149;
  2369. }
  2370. else
  2371. {
  2372. res = 5;
  2373. resval = 10;
  2374. ot = 151;
  2375. }
  2376. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  2377. {
  2378. ot++;
  2379. InfoWindow iw;
  2380. iw.soundID = sound;
  2381. iw.player = h->tempOwner;
  2382. iw.text << std::pair<ui8,ui32>(11,ot);
  2383. cb->showInfoDialog(&iw);
  2384. return;
  2385. }
  2386. BlockingDialog sd (true, false);
  2387. sd.soundID = sound;
  2388. sd.player = cb->getOwner(heroID);
  2389. sd.text << std::pair<ui8,ui32>(11,ot);
  2390. sd.components.push_back (Component (Component::RESOURCE, res, resval, 0));
  2391. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  2392. }
  2393. break;
  2394. }
  2395. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  2396. {
  2397. const CGHeroInstance *h = cb->getHero(heroID);
  2398. if(h->level < 10 - 2*h->getSecSkillLevel(CGHeroInstance::DIPLOMACY)) //not enough level
  2399. {
  2400. InfoWindow iw;
  2401. iw.soundID = sound;
  2402. iw.player = cb->getOwner(heroID);
  2403. iw.text << std::pair<ui8,ui32>(11,68);
  2404. cb->showInfoDialog(&iw);
  2405. }
  2406. else
  2407. {
  2408. cb->setObjProperty(this->id, ObjProperty::VISITORS, heroID); //add to the visitors
  2409. cb->changePrimSkill(heroID,0,2);
  2410. cb->changePrimSkill(heroID,1,2);
  2411. cb->changePrimSkill(heroID,2,2);
  2412. cb->changePrimSkill(heroID,3,2);
  2413. InfoWindow iw;
  2414. iw.soundID = sound;
  2415. iw.player = cb->getOwner(heroID);
  2416. iw.text << std::pair<ui8,ui32>(11,66);
  2417. cb->showInfoDialog(&iw);
  2418. }
  2419. break;
  2420. }
  2421. case Obj::SCHOOL_OF_MAGIC:
  2422. case Obj::SCHOOL_OF_WAR:
  2423. {
  2424. int skill = (ID==Obj::SCHOOL_OF_MAGIC ? 2 : 0);
  2425. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  2426. {
  2427. InfoWindow iw;
  2428. iw.soundID = sound;
  2429. iw.player = cb->getOwner(heroID);
  2430. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  2431. cb->showInfoDialog(&iw);
  2432. }
  2433. else
  2434. {
  2435. BlockingDialog sd(true,true);
  2436. sd.soundID = sound;
  2437. sd.player = cb->getOwner(heroID);
  2438. sd.text << std::pair<ui8,ui32>(11,ot);
  2439. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  2440. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  2441. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  2442. }
  2443. }
  2444. break;
  2445. }
  2446. }
  2447. else
  2448. {
  2449. ot++;
  2450. InfoWindow iw;
  2451. iw.soundID = sound;
  2452. iw.player = cb->getOwner(heroID);
  2453. iw.text << std::pair<ui8,ui32>(11,ot);
  2454. cb->showInfoDialog(&iw);
  2455. }
  2456. }
  2457. const std::string & CGVisitableOPH::getHoverText() const
  2458. {
  2459. int pom = -1;
  2460. switch(ID)
  2461. {
  2462. case Obj::ARENA:
  2463. pom = -1;
  2464. break;
  2465. case Obj::MERCENARY_CAMP:
  2466. pom = 8;
  2467. break;
  2468. case Obj::MARLETTO_TOWER:
  2469. pom = 7;
  2470. break;
  2471. case Obj::STAR_AXIS:
  2472. pom = 11;
  2473. break;
  2474. case Obj::GARDEN_OF_REVELATION:
  2475. pom = 4;
  2476. break;
  2477. case Obj::LEARNING_STONE:
  2478. pom = 5;
  2479. break;
  2480. case Obj::TREE_OF_KNOWLEDGE:
  2481. pom = 18;
  2482. break;
  2483. case Obj::LIBRARY_OF_ENLIGHTENMENT:
  2484. break;
  2485. case Obj::SCHOOL_OF_MAGIC:
  2486. pom = 9;
  2487. break;
  2488. case Obj::SCHOOL_OF_WAR:
  2489. pom = 10;
  2490. break;
  2491. default:
  2492. throw std::runtime_error("Wrong CGVisitableOPH object ID!\n");
  2493. }
  2494. hoverName = VLC->generaltexth->names[ID];
  2495. if(pom >= 0)
  2496. hoverName += ("\n" + VLC->generaltexth->xtrainfo[pom]);
  2497. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2498. if(h)
  2499. {
  2500. hoverName += "\n\n";
  2501. hoverName += (vstd::contains(visitors,h->id))
  2502. ? (VLC->generaltexth->allTexts[352]) //visited
  2503. : ( VLC->generaltexth->allTexts[353]); //not visited
  2504. }
  2505. return hoverName;
  2506. }
  2507. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  2508. {
  2509. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  2510. cb->changePrimSkill(heroID,primSkill-1,2);
  2511. }
  2512. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  2513. {
  2514. if(what == ObjProperty::VISITORS)
  2515. visitors.insert(val);
  2516. }
  2517. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  2518. {
  2519. if(!which) //player refused to pay
  2520. return;
  2521. int base = (ID == Obj::SCHOOL_OF_MAGIC ? 2 : 0);
  2522. cb->setObjProperty(id, ObjProperty::VISITORS, heroID); //add to the visitors
  2523. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  2524. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  2525. }
  2526. COPWBonus::COPWBonus (int index, CGTownInstance *TOWN)
  2527. {
  2528. ID = index;
  2529. town = TOWN;
  2530. id = town->bonusingBuildings.size();
  2531. }
  2532. void COPWBonus::setProperty(ui8 what, ui32 val)
  2533. {
  2534. switch (what)
  2535. {
  2536. case 4:
  2537. visitors.insert(val);
  2538. break;
  2539. case 12:
  2540. visitors.clear();
  2541. break;
  2542. }
  2543. }
  2544. void COPWBonus::onHeroVisit (const CGHeroInstance * h) const
  2545. {
  2546. int heroID = h->id;
  2547. if (town->hasBuilt(ID))
  2548. {
  2549. InfoWindow iw;
  2550. iw.player = h->tempOwner;
  2551. switch (town->subID)
  2552. {
  2553. case ETownType::CASTLE: //Stables
  2554. if (!h->hasBonusFrom(Bonus::OBJECT, 94)) //does not stack with advMap Stables
  2555. {
  2556. GiveBonus gb;
  2557. gb.bonus = Bonus(Bonus::ONE_WEEK, Bonus::LAND_MOVEMENT, Bonus::OBJECT, 600, 94, VLC->generaltexth->arraytxt[100]);
  2558. gb.id = heroID;
  2559. cb->giveHeroBonus(&gb);
  2560. iw.text << VLC->generaltexth->allTexts[580];
  2561. cb->showInfoDialog(&iw);
  2562. }
  2563. break;
  2564. case ETownType::DUNGEON: //Mana Vortex
  2565. if (visitors.empty() && h->mana <= h->manaLimit() * 2)
  2566. {
  2567. cb->setManaPoints (heroID, 2 * h->manaLimit());
  2568. cb->setObjProperty (id, ObjProperty::VISITED, true);
  2569. iw.text << VLC->generaltexth->allTexts[579];
  2570. cb->showInfoDialog(&iw);
  2571. cb->setObjProperty (town->id, 11, id); //add to visitors
  2572. }
  2573. break;
  2574. }
  2575. }
  2576. }
  2577. CTownBonus::CTownBonus (int index, CGTownInstance *TOWN)
  2578. {
  2579. ID = index;
  2580. town = TOWN;
  2581. id = town->bonusingBuildings.size();
  2582. }
  2583. void CTownBonus::setProperty (ui8 what, ui32 val)
  2584. {
  2585. if(what == 4)
  2586. visitors.insert(val);
  2587. }
  2588. void CTownBonus::onHeroVisit (const CGHeroInstance * h) const
  2589. {
  2590. int heroID = h->id;
  2591. if (town->hasBuilt(ID) && visitors.find(heroID) == visitors.end())
  2592. {
  2593. InfoWindow iw;
  2594. int what=0, val=0, mid=0;
  2595. switch (ID)
  2596. {
  2597. case EBuilding::SPECIAL_4:
  2598. switch(town->subID)
  2599. {
  2600. case ETownType::TOWER: //wall
  2601. what = 3;
  2602. val = 1;
  2603. mid = 581;
  2604. iw.components.push_back (Component(Component::PRIM_SKILL, 3, 1, 0));
  2605. break;
  2606. case ETownType::INFERNO: //order of fire
  2607. what = 2;
  2608. val = 1;
  2609. mid = 582;
  2610. iw.components.push_back (Component(Component::PRIM_SKILL, 2, 1, 0));
  2611. break;
  2612. case ETownType::STRONGHOLD://hall of valhalla
  2613. what = 0;
  2614. val = 1;
  2615. mid = 584;
  2616. iw.components.push_back (Component(Component::PRIM_SKILL, 0, 1, 0));
  2617. break;
  2618. case ETownType::DUNGEON://academy of battle scholars
  2619. what = 4;
  2620. val = 1000*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0;
  2621. mid = 583;
  2622. iw.components.push_back (Component(Component::EXPERIENCE, 0, val, 0));
  2623. break;
  2624. }
  2625. break;
  2626. case EBuilding::SPECIAL_1:
  2627. switch(town->subID)
  2628. {
  2629. case ETownType::FORTRESS: //cage of warlords
  2630. what = 1;
  2631. val = 1;
  2632. mid = 585;
  2633. iw.components.push_back (Component(Component::PRIM_SKILL, 1, 1, 0));
  2634. break;
  2635. }
  2636. break;
  2637. }
  2638. assert(mid);
  2639. iw.player = cb->getOwner(heroID);
  2640. iw.text << VLC->generaltexth->allTexts[mid];
  2641. cb->showInfoDialog(&iw);
  2642. cb->changePrimSkill (heroID, what, val);
  2643. if (town->visitingHero == h)
  2644. cb->setObjProperty (town->id, 11, id); //add to visitors
  2645. else
  2646. cb->setObjProperty (town->id, 13, id); //then it must be garrisoned hero
  2647. }
  2648. }
  2649. const std::string & CGCreature::getHoverText() const
  2650. {
  2651. MetaString ms;
  2652. int pom = stacks.begin()->second->getQuantityID();
  2653. pom = 172 + 3*pom;
  2654. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2655. ms.toString(hoverName);
  2656. if(const CGHeroInstance *selHero = cb->getSelectedHero(cb->getCurrentPlayer()))
  2657. {
  2658. std::vector<std::string> * texts = &VLC->generaltexth->threat;
  2659. hoverName += "\n\n ";
  2660. hoverName += (*texts)[0];
  2661. int choice;
  2662. double ratio = ((double)getArmyStrength() / selHero->getTotalStrength());
  2663. if (ratio < 0.1) choice = 1;
  2664. else if (ratio < 0.25) choice = 2;
  2665. else if (ratio < 0.6) choice = 3;
  2666. else if (ratio < 0.9) choice = 4;
  2667. else if (ratio < 1.1) choice = 5;
  2668. else if (ratio < 1.3) choice = 6;
  2669. else if (ratio < 1.8) choice = 7;
  2670. else if (ratio < 2.5) choice = 8;
  2671. else if (ratio < 4) choice = 9;
  2672. else if (ratio < 8) choice = 10;
  2673. else if (ratio < 20) choice = 11;
  2674. else choice = 12;
  2675. hoverName += (*texts)[choice];
  2676. }
  2677. return hoverName;
  2678. }
  2679. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  2680. {
  2681. int action = takenAction(h);
  2682. switch( action ) //decide what we do...
  2683. {
  2684. case -2: //fight
  2685. fight(h);
  2686. break;
  2687. case -1: //flee
  2688. {
  2689. flee(h);
  2690. break;
  2691. }
  2692. case 0: //join for free
  2693. {
  2694. BlockingDialog ynd(true,false);
  2695. ynd.player = h->tempOwner;
  2696. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  2697. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2698. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  2699. break;
  2700. }
  2701. default: //join for gold
  2702. {
  2703. assert(action > 0);
  2704. //ask if player agrees to pay gold
  2705. BlockingDialog ynd(true,false);
  2706. ynd.player = h->tempOwner;
  2707. std::string tmp = VLC->generaltexth->advobtxt[90];
  2708. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(getStackCount(0)));
  2709. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  2710. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID]->namePl);
  2711. ynd.text << tmp;
  2712. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  2713. break;
  2714. }
  2715. }
  2716. }
  2717. void CGCreature::endBattle( BattleResult *result ) const
  2718. {
  2719. if(result->winner==0)
  2720. {
  2721. cb->removeObject(id);
  2722. }
  2723. else
  2724. {
  2725. //int killedAmount=0;
  2726. //for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  2727. // if(i->first == subID)
  2728. // killedAmount += i->second;
  2729. //cb->setAmount(id, slots.find(0)->second.second - killedAmount);
  2730. /*
  2731. MetaString ms;
  2732. int pom = slots.find(0)->second.getQuantityID();
  2733. pom = 174 + 3*pom + 1;
  2734. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  2735. cb->setHoverName(id,&ms);
  2736. cb->setObjProperty(id, 11, slots.begin()->second.count * 1000);
  2737. */
  2738. //merge stacks into one
  2739. TSlots::const_iterator i;
  2740. CCreature * cre = VLC->creh->creatures[restore.basicType];
  2741. for (i = stacks.begin(); i != stacks.end(); i++)
  2742. {
  2743. if (cre->isMyUpgrade(i->second->type))
  2744. {
  2745. cb->changeStackType (StackLocation(this, i->first), cre); //un-upgrade creatures
  2746. }
  2747. }
  2748. //first stack has to be at slot 0 -> if original one got killed, move there first remaining stack
  2749. if(!hasStackAtSlot(0))
  2750. cb->moveStack(StackLocation(this, stacks.begin()->first), StackLocation(this, 0), stacks.begin()->second->count);
  2751. while (stacks.size() > 1) //hopefully that's enough
  2752. {
  2753. // TODO it's either overcomplicated (if we assume there'll be only one stack) or buggy (if we allow multiple stacks... but that'll also cause troubles elsewhere)
  2754. i = stacks.end();
  2755. i--;
  2756. TSlot slot = getSlotFor(i->second->type);
  2757. if (slot == i->first) //no reason to move stack to its own slot
  2758. break;
  2759. else
  2760. cb->moveStack (StackLocation(this, i->first), StackLocation(this, slot), i->second->count);
  2761. }
  2762. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, stacks.begin()->second->count * 1000); //remember casualties
  2763. }
  2764. }
  2765. void CGCreature::initObj()
  2766. {
  2767. blockVisit = true;
  2768. switch(character)
  2769. {
  2770. case 0:
  2771. character = -4;
  2772. break;
  2773. case 1:
  2774. character = 1 + ran()%7;
  2775. break;
  2776. case 2:
  2777. character = 1 + ran()%10;
  2778. break;
  2779. case 3:
  2780. character = 4 + ran()%7;
  2781. break;
  2782. case 4:
  2783. character = 10;
  2784. break;
  2785. }
  2786. stacks[0]->setType(subID);
  2787. TQuantity &amount = stacks[0]->count;
  2788. CCreature &c = *VLC->creh->creatures[subID];
  2789. if(!amount)
  2790. {
  2791. if(c.ammMax == c.ammMin)
  2792. amount = c.ammMax;
  2793. else
  2794. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  2795. }
  2796. temppower = stacks[0]->count * 1000;
  2797. }
  2798. void CGCreature::newTurn() const
  2799. {//Works only for stacks of single type of size up to 2 millions
  2800. if (stacks.begin()->second->count < VLC->modh->settings.CREEP_SIZE && cb->getDate(1) == 1 && cb->getDate(0) > 1)
  2801. {
  2802. ui32 power = temppower * (100 + VLC->modh->settings.WEEKLY_GROWTH)/100;
  2803. cb->setObjProperty(id, ObjProperty::MONSTER_COUNT, std::min (power/1000 , (ui32)VLC->modh->settings.CREEP_SIZE)); //set new amount
  2804. cb->setObjProperty(id, ObjProperty::MONSTER_POWER, power); //increase temppower
  2805. }
  2806. if (VLC->modh->modules.STACK_EXP)
  2807. cb->setObjProperty(id, ObjProperty::MONSTER_EXP, VLC->modh->settings.NEUTRAL_STACK_EXP); //for testing purpose
  2808. }
  2809. void CGCreature::setPropertyDer(ui8 what, ui32 val)
  2810. {
  2811. switch (what)
  2812. {
  2813. case ObjProperty::MONSTER_COUNT:
  2814. stacks[0]->count = val;
  2815. break;
  2816. case ObjProperty::MONSTER_POWER:
  2817. temppower = val;
  2818. break;
  2819. case ObjProperty::MONSTER_EXP:
  2820. giveStackExp(val);
  2821. break;
  2822. case ObjProperty::MONSTER_RESTORE_TYPE:
  2823. restore.basicType = val;
  2824. break;
  2825. }
  2826. }
  2827. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  2828. {
  2829. //calculate relative strength of hero and creatures armies
  2830. double relStrength = double(h->getTotalStrength()) / getArmyStrength();
  2831. int powerFactor;
  2832. if(relStrength >= 7)
  2833. powerFactor = 11;
  2834. else if(relStrength >= 1)
  2835. powerFactor = (int)(2*(relStrength-1));
  2836. else if(relStrength >= 0.5)
  2837. powerFactor = -1;
  2838. else if(relStrength >= 0.333)
  2839. powerFactor = -2;
  2840. else
  2841. powerFactor = -3;
  2842. std::set<ui32> myKindCres; //what creatures are the same kind as we
  2843. myKindCres.insert(subID); //we
  2844. myKindCres.insert(VLC->creh->creatures[subID]->upgrades.begin(),VLC->creh->creatures[subID]->upgrades.end()); //our upgrades
  2845. BOOST_FOREACH(ConstTransitivePtr<CCreature> &crea, VLC->creh->creatures)
  2846. {
  2847. if(vstd::contains(crea->upgrades, (ui32) id)) //it's our base creatures
  2848. myKindCres.insert(crea->idNumber);
  2849. }
  2850. int count = 0, //how many creatures of similar kind has hero
  2851. totalCount = 0;
  2852. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  2853. {
  2854. if(vstd::contains(myKindCres,i->second->type->idNumber))
  2855. count += i->second->count;
  2856. totalCount += i->second->count;
  2857. }
  2858. int sympathy = 0; // 0 if hero have no similar creatures
  2859. if(count)
  2860. sympathy++; // 1 - if hero have at least 1 similar creature
  2861. if(count*2 > totalCount)
  2862. sympathy++; // 2 - hero have similar creatures more that 50%
  2863. int charisma = powerFactor + h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) + sympathy;
  2864. if(charisma < character) //creatures will fight
  2865. return -2;
  2866. if (allowJoin)
  2867. {
  2868. if(h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) + sympathy + 1 >= character)
  2869. return 0; //join for free
  2870. else if(h->getSecSkillLevel(CGHeroInstance::DIPLOMACY) * 2 + sympathy + 1 >= character)
  2871. return VLC->creh->creatures[subID]->cost[6] * getStackCount(0); //join for gold
  2872. }
  2873. //we are still here - creatures have not joined hero, flee or fight
  2874. if (charisma > character)
  2875. return -1; //flee
  2876. else
  2877. return -2; //fight
  2878. }
  2879. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  2880. {
  2881. if(pursue)
  2882. {
  2883. fight(h);
  2884. }
  2885. else
  2886. {
  2887. cb->removeObject(id);
  2888. }
  2889. }
  2890. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  2891. {
  2892. if(!accept)
  2893. {
  2894. if(takenAction(h,false) == -1) //they flee
  2895. {
  2896. flee(h);
  2897. }
  2898. else //they fight
  2899. {
  2900. InfoWindow iw;
  2901. iw.player = h->tempOwner;
  2902. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  2903. cb->showInfoDialog(&iw);
  2904. fight(h);
  2905. }
  2906. }
  2907. else //accepted
  2908. {
  2909. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  2910. {
  2911. InfoWindow iw;
  2912. iw.player = h->tempOwner;
  2913. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  2914. cb->showInfoDialog(&iw);
  2915. //act as if player refused
  2916. joinDecision(h,cost,false);
  2917. return;
  2918. }
  2919. //take gold
  2920. if(cost)
  2921. cb->giveResource(h->tempOwner,6,-cost);
  2922. cb->tryJoiningArmy(this, h, true, true);
  2923. }
  2924. }
  2925. void CGCreature::fight( const CGHeroInstance *h ) const
  2926. {
  2927. //split stacks
  2928. //TODO: multiple creature types in a stack?
  2929. int basicType = stacks.begin()->second->type->idNumber;
  2930. cb->setObjProperty(id, ObjProperty::MONSTER_RESTORE_TYPE, basicType); //store info about creature stack
  2931. double relativePower = static_cast<double>(h->getTotalStrength()) / getArmyStrength();
  2932. int stacksCount;
  2933. //TODO: number depends on tile type
  2934. if (relativePower < 0.5)
  2935. {
  2936. stacksCount = 7;
  2937. }
  2938. else if (relativePower < 0.67)
  2939. {
  2940. stacksCount = 7;
  2941. }
  2942. else if (relativePower < 1)
  2943. {
  2944. stacksCount = 6;
  2945. }
  2946. else if (relativePower < 1.5)
  2947. {
  2948. stacksCount = 5;
  2949. }
  2950. else if (relativePower < 2)
  2951. {
  2952. stacksCount = 4;
  2953. }
  2954. else
  2955. {
  2956. stacksCount = 3;
  2957. }
  2958. int stackSize;
  2959. TSlot sourceSlot = stacks.begin()->first;
  2960. TSlot destSlot;
  2961. for (int stacksLeft = stacksCount; stacksLeft > 1; --stacksLeft)
  2962. {
  2963. stackSize = stacks.begin()->second->count / stacksLeft;
  2964. if (stackSize)
  2965. {
  2966. if ((destSlot = getFreeSlot()) > -1)
  2967. cb->moveStack(StackLocation(this, sourceSlot), StackLocation(this, destSlot), stackSize);
  2968. else
  2969. {
  2970. tlog2 <<"Warning! Not enough empty slots to split stack!";
  2971. break;
  2972. }
  2973. }
  2974. else break;
  2975. }
  2976. if (stacksCount > 1)
  2977. {
  2978. if (rand()%100 < 50) //upgrade
  2979. {
  2980. TSlot slotId = (stacks.size() / 2);
  2981. if(ui32 upgradesSize = getStack(slotId).type->upgrades.size())
  2982. {
  2983. std::set<TCreature>::const_iterator it = getStack(slotId).type->upgrades.begin(); //pick random in case there are more
  2984. std::advance (it, rand() % upgradesSize);
  2985. cb->changeStackType(StackLocation(this, slotId), VLC->creh->creatures[*it]);
  2986. }
  2987. }
  2988. }
  2989. cb->startBattleI(h, this, boost::bind(&CGCreature::endBattle,this,_1));
  2990. }
  2991. void CGCreature::flee( const CGHeroInstance * h ) const
  2992. {
  2993. BlockingDialog ynd(true,false);
  2994. ynd.player = h->tempOwner;
  2995. ynd.text << std::pair<ui8,ui32>(11,91);
  2996. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  2997. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  2998. }
  2999. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  3000. {
  3001. int relations = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  3002. if(relations == 2) //we're visiting our mine
  3003. {
  3004. cb->showGarrisonDialog(id,h->id,true,0);
  3005. return;
  3006. }
  3007. else if (relations == 1)//ally
  3008. return;
  3009. if(stacksCount()) //Mine is guarded
  3010. {
  3011. BlockingDialog ynd(true,false);
  3012. ynd.player = h->tempOwner;
  3013. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, subID == 7 ? 84 : 187);
  3014. cb->showBlockingDialog(&ynd,boost::bind(&CGMine::fight, this, _1, h));
  3015. return;
  3016. }
  3017. flagMine(h->tempOwner);
  3018. }
  3019. void CGMine::newTurn() const
  3020. {
  3021. if(cb->getDate() == 1)
  3022. return;
  3023. if (tempOwner == GameConstants::NEUTRAL_PLAYER)
  3024. return;
  3025. cb->giveResource(tempOwner, producedResource, producedQuantity);
  3026. }
  3027. void CGMine::initObj()
  3028. {
  3029. if(subID >= 7) //Abandoned Mine
  3030. {
  3031. //set guardians
  3032. int howManyTroglodytes = 100 + ran()%100;
  3033. CStackInstance *troglodytes = new CStackInstance(70, howManyTroglodytes);
  3034. putStack(0, troglodytes);
  3035. //after map reading tempOwner placeholds bitmask for allowed resources
  3036. std::vector<int> possibleResources;
  3037. for (int i = 0; i < 8; i++)
  3038. if(tempOwner & 1<<i)
  3039. possibleResources.push_back(i);
  3040. assert(possibleResources.size());
  3041. producedResource = possibleResources[ran()%possibleResources.size()];
  3042. tempOwner = GameConstants::NEUTRAL_PLAYER;
  3043. hoverName = VLC->generaltexth->mines[7].first + "\n" + VLC->generaltexth->allTexts[202] + " " + troglodytes->getQuantityTXT(false) + " " + troglodytes->type->namePl;
  3044. }
  3045. else
  3046. {
  3047. producedResource = subID;
  3048. MetaString ms;
  3049. ms << std::pair<ui8,ui32>(9,producedResource);
  3050. if(tempOwner >= GameConstants::PLAYER_LIMIT)
  3051. tempOwner = GameConstants::NEUTRAL_PLAYER;
  3052. else
  3053. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  3054. ms.toString(hoverName);
  3055. }
  3056. producedQuantity = defaultResProduction();
  3057. }
  3058. void CGMine::fight(ui32 agreed, const CGHeroInstance *h) const
  3059. {
  3060. cb->startBattleI(h, this, boost::bind(&CGMine::endBattle, this, _1, h->tempOwner));
  3061. }
  3062. void CGMine::endBattle(BattleResult *result, ui8 attackingPlayer) const
  3063. {
  3064. if(result->winner == 0) //attacker won
  3065. {
  3066. if(subID == 7)
  3067. {
  3068. InfoWindow iw;
  3069. iw.player = attackingPlayer;
  3070. iw.text.addTxt(MetaString::ADVOB_TXT, 85);
  3071. cb->showInfoDialog(&iw);
  3072. }
  3073. flagMine(attackingPlayer);
  3074. }
  3075. }
  3076. void CGMine::flagMine(ui8 player) const
  3077. {
  3078. assert(tempOwner != player);
  3079. cb->setOwner(id,player); //not ours? flag it!
  3080. MetaString ms;
  3081. ms << std::pair<ui8,ui32>(9,subID) << "\n(" << std::pair<ui8,ui32>(6,23+player) << ")";
  3082. if(subID == 7)
  3083. {
  3084. ms << "(%s)";
  3085. ms.addReplacement(MetaString::RES_NAMES, producedResource);
  3086. }
  3087. cb->setHoverName(id,&ms);
  3088. InfoWindow iw;
  3089. iw.soundID = soundBase::FLAGMINE;
  3090. iw.text.addTxt(MetaString::MINE_EVNTS,producedResource); //not use subID, abandoned mines uses default mine texts
  3091. iw.player = player;
  3092. iw.components.push_back(Component(Component::RESOURCE,producedResource,producedQuantity,-1));
  3093. cb->showInfoDialog(&iw);
  3094. }
  3095. ui32 CGMine::defaultResProduction()
  3096. {
  3097. switch(producedResource)
  3098. {
  3099. case Res::WOOD:
  3100. case Res::ORE:
  3101. return 2;
  3102. case Res::GOLD:
  3103. return 1000;
  3104. default:
  3105. return 1;
  3106. }
  3107. }
  3108. void CGResource::initObj()
  3109. {
  3110. blockVisit = true;
  3111. hoverName = VLC->generaltexth->restypes[subID];
  3112. if(!amount)
  3113. {
  3114. switch(subID)
  3115. {
  3116. case 6:
  3117. amount = 500 + (rand()%6)*100;
  3118. break;
  3119. case 0: case 2:
  3120. amount = 6 + (rand()%5);
  3121. break;
  3122. default:
  3123. amount = 3 + (rand()%3);
  3124. break;
  3125. }
  3126. }
  3127. }
  3128. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  3129. {
  3130. if(stacksCount())
  3131. {
  3132. if(message.size())
  3133. {
  3134. BlockingDialog ynd(true,false);
  3135. ynd.player = h->getOwner();
  3136. ynd.text << message;
  3137. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  3138. }
  3139. else
  3140. {
  3141. fightForRes(1,h);
  3142. }
  3143. }
  3144. else
  3145. {
  3146. if(message.length())
  3147. {
  3148. InfoWindow iw;
  3149. iw.player = h->tempOwner;
  3150. iw.text << message;
  3151. cb->showInfoDialog(&iw);
  3152. }
  3153. collectRes(h->getOwner());
  3154. }
  3155. }
  3156. void CGResource::collectRes( int player ) const
  3157. {
  3158. cb->giveResource(player,subID,amount);
  3159. ShowInInfobox sii;
  3160. sii.player = player;
  3161. sii.c = Component(Component::RESOURCE,subID,amount,0);
  3162. sii.text << std::pair<ui8,ui32>(11,113);
  3163. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  3164. cb->showCompInfo(&sii);
  3165. cb->removeObject(id);
  3166. }
  3167. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  3168. {
  3169. if(agreed)
  3170. cb->startBattleI(h, this, boost::bind(&CGResource::endBattle,this,_1,h));
  3171. }
  3172. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  3173. {
  3174. if(result->winner == 0) //attacker won
  3175. collectRes(h->getOwner());
  3176. }
  3177. void CGVisitableOPW::newTurn() const
  3178. {
  3179. if (cb->getDate(1) == 1) //first day of week = 1
  3180. {
  3181. cb->setObjProperty(id, ObjProperty::VISITED, false);
  3182. MetaString ms; //set text to "not visited"
  3183. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  3184. cb->setHoverName(id,&ms);
  3185. }
  3186. }
  3187. bool CGVisitableOPW::wasVisited(ui8 player) const
  3188. {
  3189. return visited; //TODO: other players should see object as unvisited
  3190. }
  3191. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  3192. {
  3193. int mid=0, sound = 0;
  3194. switch (ID)
  3195. {
  3196. case Obj::MYSTICAL_GARDEN:
  3197. sound = soundBase::experience;
  3198. mid = 92;
  3199. break;
  3200. case Obj::WINDMILL:
  3201. sound = soundBase::GENIE;
  3202. mid = 170;
  3203. break;
  3204. case Obj::WATER_WHEEL:
  3205. sound = soundBase::GENIE;
  3206. mid = 164;
  3207. break;
  3208. default:
  3209. assert(0);
  3210. }
  3211. if (visited)
  3212. {
  3213. if (ID!=Obj::WINDMILL)
  3214. mid++;
  3215. else
  3216. mid--;
  3217. InfoWindow iw;
  3218. iw.soundID = sound;
  3219. iw.player = h->tempOwner;
  3220. iw.text << std::pair<ui8,ui32>(11,mid);
  3221. cb->showInfoDialog(&iw);
  3222. }
  3223. else
  3224. {
  3225. Component::EComponentType type = Component::RESOURCE;
  3226. int sub=0, val=0;
  3227. switch (ID)
  3228. {
  3229. case Obj::MYSTICAL_GARDEN:
  3230. if (rand()%2)
  3231. {
  3232. sub = 5;
  3233. val = 5;
  3234. }
  3235. else
  3236. {
  3237. sub = 6;
  3238. val = 500;
  3239. }
  3240. break;
  3241. case Obj::WINDMILL:
  3242. mid = 170;
  3243. sub = (rand() % 5) + 1;
  3244. val = (rand() % 4) + 3;
  3245. break;
  3246. case Obj::WATER_WHEEL:
  3247. mid = 164;
  3248. sub = 6;
  3249. if(cb->getDate(0)<8)
  3250. val = 500;
  3251. else
  3252. val = 1000;
  3253. }
  3254. cb->giveResource(h->tempOwner,sub,val);
  3255. InfoWindow iw;
  3256. iw.soundID = sound;
  3257. iw.player = h->tempOwner;
  3258. iw.components.push_back(Component(type,sub,val,0));
  3259. iw.text << std::pair<ui8,ui32>(11,mid);
  3260. cb->showInfoDialog(&iw);
  3261. cb->setObjProperty(id, ObjProperty::VISITED, true);
  3262. MetaString ms; //set text to "visited"
  3263. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  3264. cb->setHoverName(id,&ms);
  3265. }
  3266. }
  3267. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  3268. {
  3269. if(what == ObjProperty::VISITED)
  3270. visited = val;
  3271. }
  3272. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  3273. {
  3274. int destinationid=-1;
  3275. switch(ID)
  3276. {
  3277. case Obj::MONOLITH1: //one way - find corresponding exit monolith
  3278. if(vstd::contains(objs,Obj::MONOLITH2) && vstd::contains(objs[Obj::MONOLITH2],subID) && objs[Obj::MONOLITH2][subID].size())
  3279. destinationid = objs[Obj::MONOLITH2][subID][rand()%objs[Obj::MONOLITH2][subID].size()];
  3280. else
  3281. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  3282. break;
  3283. case Obj::MONOLITH3://two way monolith - pick any other one
  3284. case Obj::WHIRLPOOL: //Whirlpool
  3285. if(vstd::contains(objs,ID) && vstd::contains(objs[ID],subID) && objs[ID][subID].size()>1)
  3286. {
  3287. while ((destinationid = objs[ID][subID][rand()%objs[ID][subID].size()]) == id); //choose another exit
  3288. if (ID == Obj::WHIRLPOOL)
  3289. {
  3290. if (!h->hasBonusOfType(Bonus::WHIRLPOOL_PROTECTION))
  3291. {
  3292. if (h->Slots().size() > 1 || h->Slots().begin()->second->count > 1)
  3293. { //we can't remove last unit
  3294. TSlot targetstack = h->Slots().begin()->first; //slot numbers may vary
  3295. for(TSlots::const_reverse_iterator i = h->Slots().rbegin(); i != h->Slots().rend(); i++)
  3296. {
  3297. if (h->getPower(targetstack) > h->getPower(i->first))
  3298. {
  3299. targetstack = (i->first);
  3300. }
  3301. }
  3302. TQuantity countToTake = h->getStackCount(targetstack) * 0.5;
  3303. vstd::amax(countToTake, 1);
  3304. InfoWindow iw;
  3305. iw.player = h->tempOwner;
  3306. iw.text.addTxt (MetaString::ADVOB_TXT, 168);
  3307. iw.components.push_back (Component(CStackBasicDescriptor(h->getCreature(targetstack), countToTake)));
  3308. cb->showInfoDialog(&iw);
  3309. cb->changeStackCount(StackLocation(h, targetstack), -countToTake);
  3310. }
  3311. }
  3312. }
  3313. }
  3314. else
  3315. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  3316. break;
  3317. case Obj::SUBTERRANEAN_GATE: //find nearest subterranean gate on the other level
  3318. {
  3319. destinationid = getMatchingGate(id);
  3320. if(destinationid < 0) //no exit
  3321. {
  3322. InfoWindow iw;
  3323. iw.player = h->tempOwner;
  3324. iw.text.addTxt(MetaString::ADVOB_TXT, 153);//Just inside the entrance you find a large pile of rubble blocking the tunnel. You leave discouraged.
  3325. cb->sendAndApply(&iw);
  3326. }
  3327. break;
  3328. }
  3329. }
  3330. if(destinationid < 0)
  3331. {
  3332. tlog2 << "Cannot find exit... (obj at " << pos << ") :( \n";
  3333. return;
  3334. }
  3335. if (ID == Obj::WHIRLPOOL)
  3336. {
  3337. std::set<int3> tiles = cb->getObj(destinationid)->getBlockedPos();
  3338. std::set<int3>::iterator it = tiles.begin();
  3339. std::advance (it, rand() % tiles.size()); //picking random element of set is tiring
  3340. cb->moveHero (h->id, *it + int3(1,0,0), true);
  3341. }
  3342. else
  3343. cb->moveHero (h->id,CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true) - getVisitableOffset(), true);
  3344. }
  3345. void CGTeleport::initObj()
  3346. {
  3347. int si = subID;
  3348. switch (ID)
  3349. {
  3350. case Obj::SUBTERRANEAN_GATE://ignore subterranean gates subid
  3351. case Obj::WHIRLPOOL:
  3352. {
  3353. si = 0;
  3354. break;
  3355. }
  3356. default:
  3357. break;
  3358. }
  3359. objs[ID][si].push_back(id);
  3360. }
  3361. void CGTeleport::postInit() //matches subterranean gates into pairs
  3362. {
  3363. //split on underground and surface gates
  3364. std::vector<const CGObjectInstance *> gatesSplit[2]; //surface and underground gates
  3365. for(size_t i = 0; i < objs[Obj::SUBTERRANEAN_GATE][0].size(); i++)
  3366. {
  3367. const CGObjectInstance *hlp = cb->getObj(objs[Obj::SUBTERRANEAN_GATE][0][i]);
  3368. gatesSplit[hlp->pos.z].push_back(hlp);
  3369. }
  3370. //sort by position
  3371. std::sort(gatesSplit[0].begin(), gatesSplit[0].end(), boost::bind(&CGObjectInstance::pos, _1) < boost::bind(&CGObjectInstance::pos, _2));
  3372. for(size_t i = 0; i < gatesSplit[0].size(); i++)
  3373. {
  3374. const CGObjectInstance *cur = gatesSplit[0][i];
  3375. //find nearest underground exit
  3376. std::pair<int,double> best(-1,150000); //pair<pos_in_vector, distance>
  3377. for(int j = 0; j < gatesSplit[1].size(); j++)
  3378. {
  3379. const CGObjectInstance *checked = gatesSplit[1][j];
  3380. if(!checked)
  3381. continue;
  3382. double hlp = checked->pos.dist2d(cur->pos);
  3383. if(hlp < best.second)
  3384. {
  3385. best.first = j;
  3386. best.second = hlp;
  3387. }
  3388. }
  3389. if(best.first >= 0) //found pair
  3390. {
  3391. gates.push_back(std::pair<int, int>(cur->id, gatesSplit[1][best.first]->id));
  3392. gatesSplit[1][best.first] = NULL;
  3393. }
  3394. else
  3395. {
  3396. gates.push_back(std::pair<int, int>(cur->id, -1));
  3397. }
  3398. }
  3399. objs.erase(103);
  3400. }
  3401. int CGTeleport::getMatchingGate(int id)
  3402. {
  3403. for(int i=0; i < gates.size(); i++)
  3404. {
  3405. if(gates[i].first == id)
  3406. return gates[i].second;
  3407. if(gates[i].second == id)
  3408. return gates[i].first;
  3409. }
  3410. return -1;
  3411. }
  3412. void CGArtifact::initObj()
  3413. {
  3414. blockVisit = true;
  3415. if(ID == Obj::ARTIFACT)
  3416. {
  3417. hoverName = VLC->arth->artifacts[subID]->Name();
  3418. if(!storedArtifact->artType)
  3419. storedArtifact->setType(VLC->arth->artifacts[subID]);
  3420. }
  3421. if(ID == Obj::SPELL_SCROLL)
  3422. subID = 1;
  3423. assert(storedArtifact->artType);
  3424. assert(storedArtifact->getParentNodes().size());
  3425. }
  3426. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  3427. {
  3428. if(!stacksCount())
  3429. {
  3430. InfoWindow iw;
  3431. iw.player = h->tempOwner;
  3432. switch(ID)
  3433. {
  3434. case Obj::ARTIFACT:
  3435. {
  3436. iw.soundID = soundBase::treasure; //play sound only for non-scroll arts
  3437. iw.components.push_back(Component(Component::ARTIFACT,subID,0,0));
  3438. if(message.length())
  3439. iw.text << message;
  3440. else
  3441. iw.text << std::pair<ui8, ui32> (MetaString::ART_EVNTS, subID);
  3442. }
  3443. break;
  3444. case Obj::SPELL_SCROLL:
  3445. {
  3446. int spellID = storedArtifact->getGivenSpellID();
  3447. iw.components.push_back (Component(Component::SPELL, spellID,0,0));
  3448. iw.text.addTxt (MetaString::ADVOB_TXT,135);
  3449. iw.text.addReplacement(MetaString::SPELL_NAME, spellID);
  3450. }
  3451. break;
  3452. }
  3453. cb->showInfoDialog(&iw);
  3454. pick(h);
  3455. }
  3456. else
  3457. {
  3458. if(message.size())
  3459. {
  3460. BlockingDialog ynd(true,false);
  3461. ynd.player = h->getOwner();
  3462. ynd.text << message;
  3463. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  3464. }
  3465. else
  3466. {
  3467. fightForArt(0,h);
  3468. }
  3469. }
  3470. }
  3471. void CGArtifact::pick(const CGHeroInstance * h) const
  3472. {
  3473. cb->giveHeroArtifact(h, storedArtifact, -2);
  3474. cb->removeObject(id);
  3475. }
  3476. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  3477. {
  3478. if(agreed)
  3479. cb->startBattleI(h, this, boost::bind(&CGArtifact::endBattle,this,_1,h));
  3480. }
  3481. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  3482. {
  3483. if(result->winner == 0) //attacker won
  3484. pick(h);
  3485. }
  3486. void CGPickable::initObj()
  3487. {
  3488. blockVisit = true;
  3489. switch(ID)
  3490. {
  3491. case Obj::CAMPFIRE:
  3492. val2 = (ran()%3) + 4; //4 - 6
  3493. val1 = val2 * 100;
  3494. type = ran()%6; //given resource
  3495. break;
  3496. case Obj::FLOTSAM:
  3497. switch(type = ran()%4)
  3498. {
  3499. case 0:
  3500. val1 = val2 = 0;
  3501. break;
  3502. case 1:
  3503. val1 = 5;
  3504. val2 = 0;
  3505. break;
  3506. case 2:
  3507. val1 = 5;
  3508. val2 = 200;
  3509. break;
  3510. case 3:
  3511. val1 = 10;
  3512. val2 = 500;
  3513. break;
  3514. }
  3515. break;
  3516. case Obj::SEA_CHEST:
  3517. {
  3518. int hlp = ran()%100;
  3519. if(hlp < 20)
  3520. {
  3521. val1 = 0;
  3522. type = 0;
  3523. }
  3524. else if(hlp < 90)
  3525. {
  3526. val1 = 1500;
  3527. type = 2;
  3528. }
  3529. else
  3530. {
  3531. val1 = 1000;
  3532. val2 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3533. type = 1;
  3534. }
  3535. }
  3536. break;
  3537. case Obj::SHIPWRECK_SURVIVOR:
  3538. {
  3539. int hlp = ran()%100;
  3540. if(hlp < 55)
  3541. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3542. else if(hlp < 75)
  3543. val1 = cb->getRandomArt (CArtifact::ART_MINOR);
  3544. else if(hlp < 95)
  3545. val1 = cb->getRandomArt (CArtifact::ART_MAJOR);
  3546. else
  3547. val1 = cb->getRandomArt (CArtifact::ART_RELIC);
  3548. }
  3549. break;
  3550. case Obj::TREASURE_CHEST:
  3551. {
  3552. int hlp = ran()%100;
  3553. if(hlp >= 95)
  3554. {
  3555. type = 1;
  3556. val1 = cb->getRandomArt (CArtifact::ART_TREASURE);
  3557. return;
  3558. }
  3559. else if (hlp >= 65)
  3560. {
  3561. val1 = 2000;
  3562. }
  3563. else if(hlp >= 33)
  3564. {
  3565. val1 = 1500;
  3566. }
  3567. else
  3568. {
  3569. val1 = 1000;
  3570. }
  3571. val2 = val1 - 500;
  3572. type = 0;
  3573. break;
  3574. }
  3575. }
  3576. }
  3577. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  3578. {
  3579. switch(ID)
  3580. {
  3581. case Obj::CAMPFIRE:
  3582. {
  3583. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  3584. cb->giveResource(h->tempOwner,Res::GOLD,val1);//gold
  3585. InfoWindow iw;
  3586. iw.soundID = soundBase::experience;
  3587. iw.player = h->tempOwner;
  3588. iw.components.push_back(Component(Component::RESOURCE,Res::GOLD,val1,0));
  3589. iw.components.push_back(Component(Component::RESOURCE,type,val2,0));
  3590. iw.text << std::pair<ui8,ui32>(11,23);
  3591. cb->showInfoDialog(&iw);
  3592. break;
  3593. }
  3594. case Obj::FLOTSAM:
  3595. {
  3596. cb->giveResource(h->tempOwner,0,val1); //wood
  3597. cb->giveResource(h->tempOwner,6,val2);//gold
  3598. InfoWindow iw;
  3599. iw.soundID = soundBase::GENIE;
  3600. iw.player = h->tempOwner;
  3601. if(val1)
  3602. iw.components.push_back(Component(Component::RESOURCE,Res::WOOD,val1,0));
  3603. if(val2)
  3604. iw.components.push_back(Component(Component::RESOURCE,Res::GOLD,val2,0));
  3605. iw.text.addTxt(MetaString::ADVOB_TXT, 51+type);
  3606. cb->showInfoDialog(&iw);
  3607. break;
  3608. }
  3609. case Obj::SEA_CHEST:
  3610. {
  3611. InfoWindow iw;
  3612. iw.soundID = soundBase::chest;
  3613. iw.player = h->tempOwner;
  3614. iw.text.addTxt(MetaString::ADVOB_TXT, 116 + type);
  3615. if(val1) //there is gold
  3616. {
  3617. iw.components.push_back(Component(Component::RESOURCE,Res::GOLD,val1,0));
  3618. cb->giveResource(h->tempOwner,6,val1);
  3619. }
  3620. if(type == 1) //art
  3621. {
  3622. //TODO: what if no space in backpack?
  3623. iw.components.push_back(Component(Component::ARTIFACT, val2, 1, 0));
  3624. iw.text.addReplacement(MetaString::ART_NAMES, val2);
  3625. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val2],-2);
  3626. }
  3627. cb->showInfoDialog(&iw);
  3628. break;
  3629. }
  3630. case Obj::SHIPWRECK_SURVIVOR:
  3631. {
  3632. //TODO: what if no space in backpack?
  3633. InfoWindow iw;
  3634. iw.soundID = soundBase::experience;
  3635. iw.player = h->tempOwner;
  3636. iw.components.push_back(Component(Component::ARTIFACT,val1,1,0));
  3637. iw.text.addTxt(MetaString::ADVOB_TXT, 125);
  3638. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  3639. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val1],-2);
  3640. cb->showInfoDialog(&iw);
  3641. break;
  3642. }
  3643. case Obj::TREASURE_CHEST:
  3644. {
  3645. if (subID) //not OH3 treasure chest
  3646. {
  3647. tlog2 << "Not supported WoG treasure chest!\n";
  3648. return;
  3649. }
  3650. if(type) //there is an artifact
  3651. {
  3652. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[val1],-2);
  3653. InfoWindow iw;
  3654. iw.soundID = soundBase::treasure;
  3655. iw.player = h->tempOwner;
  3656. iw.components.push_back(Component(Component::ARTIFACT,val1,1,0));
  3657. iw.text << std::pair<ui8,ui32>(11,145);
  3658. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  3659. cb->showInfoDialog(&iw);
  3660. break;
  3661. }
  3662. else
  3663. {
  3664. BlockingDialog sd(false,true);
  3665. sd.player = h->tempOwner;
  3666. sd.text << std::pair<ui8,ui32>(11,146);
  3667. sd.components.push_back(Component(Component::RESOURCE,Res::GOLD,val1,0));
  3668. TExpType expVal = h->calculateXp(val2);
  3669. sd.components.push_back(Component(Component::EXPERIENCE,0,expVal, 0));
  3670. sd.soundID = soundBase::chest;
  3671. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  3672. cb->showBlockingDialog(&sd,fun);
  3673. return;
  3674. }
  3675. }
  3676. }
  3677. cb->removeObject(id);
  3678. }
  3679. void CGPickable::chosen( int which, int heroID ) const
  3680. {
  3681. const CGHeroInstance *h = cb->getHero(heroID);
  3682. switch(which)
  3683. {
  3684. case 1: //player pick gold
  3685. cb->giveResource(cb->getOwner(heroID),6,val1);
  3686. break;
  3687. case 2: //player pick exp
  3688. cb->changePrimSkill(heroID, 4, h->calculateXp(val2));
  3689. break;
  3690. default:
  3691. throw std::runtime_error("Unhandled treasure choice");
  3692. }
  3693. cb->removeObject(id);
  3694. }
  3695. bool CQuest::checkQuest (const CGHeroInstance * h) const
  3696. {
  3697. switch (missionType)
  3698. {
  3699. case MISSION_NONE:
  3700. return true;
  3701. case MISSION_LEVEL:
  3702. if (m13489val <= h->level)
  3703. return true;
  3704. return false;
  3705. case MISSION_PRIMARY_STAT:
  3706. for (int i = 0; i < 4; ++i)
  3707. {
  3708. if (h->getPrimSkillLevel(i) < m2stats[i])
  3709. return false;
  3710. }
  3711. return true;
  3712. case MISSION_KILL_HERO:
  3713. case MISSION_KILL_CREATURE:
  3714. if (!h->cb->getObjByQuestIdentifier(m13489val))
  3715. return true;
  3716. return false;
  3717. case MISSION_ART:
  3718. for (int i = 0; i < m5arts.size(); ++i)
  3719. {
  3720. if (h->hasArt(m5arts[i]))
  3721. continue;
  3722. return false; //if the artifact was not found
  3723. }
  3724. return true;
  3725. case MISSION_ARMY:
  3726. {
  3727. std::vector<CStackBasicDescriptor>::const_iterator cre;
  3728. TSlots::const_iterator it;
  3729. ui32 count;
  3730. for (cre = m6creatures.begin(); cre != m6creatures.end(); ++cre)
  3731. {
  3732. for (count = 0, it = h->Slots().begin(); it != h->Slots().end(); ++it)
  3733. {
  3734. if (it->second->type == cre->type)
  3735. count += it->second->count;
  3736. }
  3737. if (count < cre->count) //not enough creatures of this kind
  3738. return false;
  3739. }
  3740. }
  3741. return true;
  3742. case MISSION_RESOURCES:
  3743. for (int i = 0; i < 7; ++i) //including Mithril ?
  3744. { //Quest has no direct access to callback
  3745. if (h->cb->getResource (h->tempOwner, i) < m7resources[i])
  3746. return false;
  3747. }
  3748. return true;
  3749. case MISSION_HERO:
  3750. if (m13489val == h->type->ID)
  3751. return true;
  3752. return false;
  3753. case MISSION_PLAYER:
  3754. if (m13489val == h->getOwner())
  3755. return true;
  3756. return false;
  3757. default:
  3758. return false;
  3759. }
  3760. }
  3761. void CQuest::getVisitText (MetaString &iwText, std::vector<Component> &components, bool isCustom, bool firstVisit, const CGHeroInstance * h) const
  3762. {
  3763. std::string text;
  3764. bool failRequirements = (h ? !checkQuest(h) : true);
  3765. if (firstVisit)
  3766. {
  3767. isCustom = isCustomFirst;
  3768. iwText << firstVisitText;
  3769. }
  3770. else if (failRequirements)
  3771. {
  3772. isCustom = isCustomNext;
  3773. iwText << nextVisitText;
  3774. }
  3775. switch (missionType)
  3776. {
  3777. case MISSION_LEVEL:
  3778. components.push_back(Component (Component::EXPERIENCE, 1, m13489val, 0));
  3779. if (!isCustom)
  3780. iwText.addReplacement(m13489val);
  3781. break;
  3782. case MISSION_PRIMARY_STAT:
  3783. {
  3784. MetaString loot;
  3785. for (int i = 0; i < 4; ++i)
  3786. {
  3787. if (m2stats[i])
  3788. {
  3789. components.push_back(Component (Component::PRIM_SKILL, i, m2stats[i], 0));
  3790. loot << "%d %s";
  3791. loot.addReplacement(m2stats[i]);
  3792. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3793. }
  3794. }
  3795. if (!isCustom)
  3796. iwText.addReplacement(loot.buildList());
  3797. }
  3798. break;
  3799. case MISSION_KILL_HERO:
  3800. components.push_back(Component(Component::HERO, heroPortrait, 0, 0));
  3801. if (!isCustom)
  3802. addReplacements(iwText, text);
  3803. break;
  3804. case MISSION_HERO:
  3805. components.push_back(Component (Component::HERO, m13489val, 0, 0));
  3806. if (!isCustom)
  3807. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3808. break;
  3809. case MISSION_KILL_CREATURE:
  3810. {
  3811. components.push_back(Component(stackToKill));
  3812. if (!isCustom)
  3813. addReplacements(iwText, text);
  3814. }
  3815. break;
  3816. case MISSION_ART:
  3817. {
  3818. MetaString loot;
  3819. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3820. {
  3821. components.push_back(Component (Component::ARTIFACT, *it, 0, 0));
  3822. loot << "%s";
  3823. loot.addReplacement(MetaString::ART_NAMES, *it);
  3824. }
  3825. if (!isCustom)
  3826. iwText.addReplacement(loot.buildList());
  3827. }
  3828. break;
  3829. case MISSION_ARMY:
  3830. {
  3831. MetaString loot;
  3832. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3833. {
  3834. components.push_back(Component(*it));
  3835. loot << "%s";
  3836. loot.addReplacement(*it);
  3837. }
  3838. if (!isCustom)
  3839. iwText.addReplacement(loot.buildList());
  3840. }
  3841. break;
  3842. case MISSION_RESOURCES:
  3843. {
  3844. MetaString loot;
  3845. for (int i = 0; i < 7; ++i)
  3846. {
  3847. if (m7resources[i])
  3848. {
  3849. components.push_back(Component (Component::RESOURCE, i, m7resources[i], 0));
  3850. loot << "%d %s";
  3851. loot.addReplacement(m7resources[i]);
  3852. loot.addReplacement(MetaString::RES_NAMES, i);
  3853. }
  3854. }
  3855. if (!isCustom)
  3856. iwText.addReplacement(loot.buildList());
  3857. }
  3858. break;
  3859. case MISSION_PLAYER:
  3860. components.push_back(Component (Component::FLAG, m13489val, 0, 0));
  3861. if (!isCustom)
  3862. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  3863. break;
  3864. }
  3865. }
  3866. void CQuest::getRolloverText (MetaString &ms, bool onHover) const
  3867. {
  3868. if (onHover)
  3869. ms << "\n\n";
  3870. ms << VLC->generaltexth->quests[missionType-1][onHover ? 3 : 4][textOption];
  3871. switch (missionType)
  3872. {
  3873. case MISSION_LEVEL:
  3874. ms.addReplacement(m13489val);
  3875. break;
  3876. case MISSION_PRIMARY_STAT:
  3877. {
  3878. MetaString loot;
  3879. for (int i = 0; i < 4; ++i)
  3880. {
  3881. if (m2stats[i])
  3882. {
  3883. loot << "%d %s";
  3884. loot.addReplacement(m2stats[i]);
  3885. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3886. }
  3887. }
  3888. ms.addReplacement(loot.buildList());
  3889. }
  3890. break;
  3891. case MISSION_KILL_HERO:
  3892. ms.addReplacement(heroName);
  3893. break;
  3894. case MISSION_KILL_CREATURE:
  3895. ms.addReplacement(stackToKill);
  3896. break;
  3897. case MISSION_ART:
  3898. {
  3899. MetaString loot;
  3900. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3901. {
  3902. loot << "%s";
  3903. loot.addReplacement(MetaString::ART_NAMES, *it);
  3904. }
  3905. ms.addReplacement(loot.buildList());
  3906. }
  3907. break;
  3908. case MISSION_ARMY:
  3909. {
  3910. MetaString loot;
  3911. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3912. {
  3913. loot << "%s";
  3914. loot.addReplacement(*it);
  3915. }
  3916. ms.addReplacement(loot.buildList());
  3917. }
  3918. break;
  3919. case MISSION_RESOURCES:
  3920. {
  3921. MetaString loot;
  3922. for (int i = 0; i < 7; ++i)
  3923. {
  3924. if (m7resources[i])
  3925. {
  3926. loot << "%d %s";
  3927. loot.addReplacement(m7resources[i]);
  3928. loot.addReplacement(MetaString::RES_NAMES, i);
  3929. }
  3930. }
  3931. ms.addReplacement(loot.buildList());
  3932. }
  3933. break;
  3934. case MISSION_HERO:
  3935. ms.addReplacement(VLC->heroh->heroes[m13489val]->name);
  3936. break;
  3937. case MISSION_PLAYER:
  3938. ms.addReplacement(VLC->generaltexth->colors[m13489val]);
  3939. break;
  3940. default:
  3941. break;
  3942. }
  3943. }
  3944. void CQuest::getCompletionText (MetaString &iwText, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  3945. {
  3946. iwText << completedText;
  3947. switch (missionType)
  3948. {
  3949. case CQuest::MISSION_LEVEL:
  3950. if (!isCustomComplete)
  3951. iwText.addReplacement(m13489val);
  3952. break;
  3953. case CQuest::MISSION_PRIMARY_STAT:
  3954. if (vstd::contains (completedText,'%')) //there's one case when there's nothing to replace
  3955. {
  3956. MetaString loot;
  3957. for (int i = 0; i < 4; ++i)
  3958. {
  3959. if (m2stats[i])
  3960. {
  3961. loot << "%d %s";
  3962. loot.addReplacement(m2stats[i]);
  3963. loot.addReplacement(VLC->generaltexth->primarySkillNames[i]);
  3964. }
  3965. }
  3966. if (!isCustomComplete)
  3967. iwText.addReplacement(loot.buildList());
  3968. }
  3969. break;
  3970. case CQuest::MISSION_ART:
  3971. {
  3972. MetaString loot;
  3973. for (std::vector<ui16>::const_iterator it = m5arts.begin(); it != m5arts.end(); ++it)
  3974. {
  3975. loot << "%s";
  3976. loot.addReplacement(MetaString::ART_NAMES, *it);
  3977. }
  3978. if (!isCustomComplete)
  3979. iwText.addReplacement(loot.buildList());
  3980. }
  3981. break;
  3982. case CQuest::MISSION_ARMY:
  3983. {
  3984. MetaString loot;
  3985. for (std::vector<CStackBasicDescriptor>::const_iterator it = m6creatures.begin(); it != m6creatures.end(); ++it)
  3986. {
  3987. loot << "%s";
  3988. loot.addReplacement(*it);
  3989. }
  3990. if (!isCustomComplete)
  3991. iwText.addReplacement(loot.buildList());
  3992. }
  3993. break;
  3994. case CQuest::MISSION_RESOURCES:
  3995. {
  3996. MetaString loot;
  3997. for (int i = 0; i < 7; ++i)
  3998. {
  3999. if (m7resources[i])
  4000. {
  4001. loot << "%d %s";
  4002. loot.addReplacement(m7resources[i]);
  4003. loot.addReplacement(MetaString::RES_NAMES, i);
  4004. }
  4005. }
  4006. if (!isCustomComplete)
  4007. iwText.addReplacement(loot.buildList());
  4008. }
  4009. break;
  4010. case MISSION_KILL_HERO:
  4011. case MISSION_KILL_CREATURE:
  4012. if (!isCustomComplete)
  4013. addReplacements(iwText, completedText);
  4014. break;
  4015. case MISSION_HERO:
  4016. if (!isCustomComplete)
  4017. iwText.addReplacement(VLC->heroh->heroes[m13489val]->name);
  4018. break;
  4019. case MISSION_PLAYER:
  4020. if (!isCustomComplete)
  4021. iwText.addReplacement(VLC->generaltexth->colors[m13489val]);
  4022. break;
  4023. }
  4024. }
  4025. void CGSeerHut::setObjToKill()
  4026. {
  4027. if (quest->missionType == CQuest::MISSION_KILL_CREATURE)
  4028. {
  4029. quest->stackToKill = getCreatureToKill(false)->getStack(0); //FIXME: stacks tend to dissapear (desync?) on server :?
  4030. assert(quest->stackToKill.type);
  4031. quest->stackToKill.count = 0; //no count in info window
  4032. quest->stackDirection = checkDirection();
  4033. }
  4034. else if (quest->missionType == CQuest::MISSION_KILL_HERO)
  4035. {
  4036. quest->heroName = getHeroToKill(false)->name;
  4037. quest->heroPortrait = getHeroToKill(false)->portrait;
  4038. }
  4039. }
  4040. void CGSeerHut::initObj()
  4041. {
  4042. seerName = VLC->generaltexth->seerNames[ran()%VLC->generaltexth->seerNames.size()];
  4043. quest->textOption = ran()%3;
  4044. quest->progress = 0;
  4045. if (quest->missionType)
  4046. {
  4047. if (!quest->isCustomFirst)
  4048. quest->firstVisitText = VLC->generaltexth->quests[quest->missionType-1][0][quest->textOption];
  4049. if (!quest->isCustomNext)
  4050. quest->nextVisitText = VLC->generaltexth->quests[quest->missionType-1][1][quest->textOption];
  4051. if (!quest->isCustomComplete)
  4052. quest->completedText = VLC->generaltexth->quests[quest->missionType-1][2][quest->textOption];
  4053. }
  4054. else
  4055. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->textOption];
  4056. }
  4057. void CGSeerHut::getRolloverText (MetaString &text, bool onHover) const
  4058. {
  4059. quest->getRolloverText (text, onHover);//TODO: simplify?
  4060. if (!onHover)
  4061. text.addReplacement(seerName);
  4062. }
  4063. const std::string & CGSeerHut::getHoverText() const
  4064. {
  4065. switch (ID)
  4066. {
  4067. case Obj::SEER_HUT:
  4068. if (quest->progress)
  4069. {
  4070. hoverName = VLC->generaltexth->allTexts[347];
  4071. boost::algorithm::replace_first(hoverName,"%s", seerName);
  4072. }
  4073. else //just seer hut
  4074. hoverName = VLC->generaltexth->names[ID];
  4075. break;
  4076. case Obj::QUEST_GUARD:
  4077. hoverName = VLC->generaltexth->names[ID];
  4078. break;
  4079. default:
  4080. tlog5 << "unrecognized quest object\n";
  4081. }
  4082. if (quest->progress & quest->missionType) //rollover when the quest is active
  4083. {
  4084. MetaString ms;
  4085. getRolloverText (ms, true);
  4086. hoverName += ms.toString();
  4087. }
  4088. return hoverName;
  4089. }
  4090. void CQuest::addReplacements(MetaString &out, const std::string &base) const
  4091. {
  4092. switch(missionType)
  4093. {
  4094. case MISSION_KILL_CREATURE:
  4095. out.addReplacement(stackToKill);
  4096. if (std::count(base.begin(), base.end(), '%') == 2) //say where is placed monster
  4097. {
  4098. out.addReplacement(VLC->generaltexth->arraytxt[147+stackDirection]);
  4099. }
  4100. break;
  4101. case MISSION_KILL_HERO:
  4102. out.addReplacement(heroName);
  4103. break;
  4104. }
  4105. }
  4106. bool IQuestObject::checkQuest(const CGHeroInstance* h) const
  4107. {
  4108. return quest->checkQuest(h);
  4109. }
  4110. void IQuestObject::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  4111. {
  4112. quest->getVisitText (text,components, isCustom, FirstVisit, h);
  4113. }
  4114. void CGSeerHut::getCompletionText(MetaString &text, std::vector<Component> &components, bool isCustom, const CGHeroInstance * h) const
  4115. {
  4116. quest->getCompletionText (text, components, isCustom, h);
  4117. switch (rewardType)
  4118. {
  4119. case 1: components.push_back(Component (Component::EXPERIENCE, 0, rVal*(100+h->getSecSkillLevel(CGHeroInstance::LEARNING)*5)/100.0, 0));
  4120. break;
  4121. case 2: components.push_back(Component (Component::PRIM_SKILL, 5, rVal, 0));
  4122. break;
  4123. case 3: components.push_back(Component (Component::MORALE, 0, rVal, 0));
  4124. break;
  4125. case 4: components.push_back(Component (Component::LUCK, 0, rVal, 0));
  4126. break;
  4127. case 5: components.push_back(Component (Component::RESOURCE, rID, rVal, 0));
  4128. break;
  4129. case 6: components.push_back(Component (Component::PRIM_SKILL, rID, rVal, 0));
  4130. break;
  4131. case 7: components.push_back(Component (Component::SEC_SKILL, rID, rVal, 0));
  4132. break;
  4133. case 8: components.push_back(Component (Component::ARTIFACT, rID, 0, 0));
  4134. break;
  4135. case 9: components.push_back(Component (Component::SPELL, rID, 0, 0));
  4136. break;
  4137. case 10: components.push_back(Component (Component::CREATURE, rID, rVal, 0));
  4138. break;
  4139. }
  4140. }
  4141. void CGSeerHut::setPropertyDer (ui8 what, ui32 val)
  4142. {
  4143. switch (what)
  4144. {
  4145. case 10:
  4146. quest->progress = val;
  4147. break;
  4148. case 11:
  4149. quest->missionType = CQuest::MISSION_NONE;
  4150. break;
  4151. }
  4152. }
  4153. void CGSeerHut::newTurn() const
  4154. {
  4155. if (quest->lastDay >= 0 && quest->lastDay < cb->getDate(0)) //time is up
  4156. {
  4157. cb->setObjProperty (id, 11, 0);
  4158. cb->setObjProperty (id, 10, 0);
  4159. }
  4160. }
  4161. void CGSeerHut::onHeroVisit( const CGHeroInstance * h ) const
  4162. {
  4163. InfoWindow iw;
  4164. iw.player = h->getOwner();
  4165. if (quest->missionType)
  4166. {
  4167. bool firstVisit = !quest->progress;
  4168. bool failRequirements = !checkQuest(h);
  4169. bool isCustom=false;
  4170. if (firstVisit)
  4171. {
  4172. isCustom = quest->isCustomFirst;
  4173. cb->setObjProperty (id, 10, CQuest::IN_PROGRESS);
  4174. AddQuest aq;
  4175. aq.quest = QuestInfo (quest, this, visitablePos());
  4176. aq.player = h->tempOwner;
  4177. cb->sendAndApply (&aq); //TODO: merge with setObjProperty?
  4178. }
  4179. else if (failRequirements)
  4180. {
  4181. isCustom = quest->isCustomNext;
  4182. }
  4183. if (firstVisit || failRequirements)
  4184. {
  4185. getVisitText (iw.text, iw.components, isCustom, firstVisit, h);
  4186. cb->showInfoDialog(&iw);
  4187. }
  4188. if (!failRequirements) // propose completion, also on first visit
  4189. {
  4190. BlockingDialog bd (true, false);
  4191. bd.player = h->getOwner();
  4192. bd.soundID = soundBase::QUEST;
  4193. getCompletionText (bd.text, bd.components, isCustom, h);
  4194. cb->showBlockingDialog (&bd, boost::bind (&CGSeerHut::finishQuest, this, h, _1));
  4195. return;
  4196. }
  4197. }
  4198. else
  4199. {
  4200. iw.text << VLC->generaltexth->seerEmpty[quest->textOption];
  4201. if (ID == Obj::SEER_HUT)
  4202. iw.text.addReplacement(seerName);
  4203. cb->showInfoDialog(&iw);
  4204. }
  4205. }
  4206. int CGSeerHut::checkDirection() const
  4207. {
  4208. int3 cord = getCreatureToKill()->pos;
  4209. if ((double)cord.x/(double)cb->getMapSize().x < 0.34) //north
  4210. {
  4211. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //northwest
  4212. return 8;
  4213. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //north
  4214. return 1;
  4215. else //northeast
  4216. return 2;
  4217. }
  4218. else if ((double)cord.x/(double)cb->getMapSize().x < 0.67) //horizontal
  4219. {
  4220. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //west
  4221. return 7;
  4222. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //central
  4223. return 9;
  4224. else //east
  4225. return 3;
  4226. }
  4227. else //south
  4228. {
  4229. if ((double)cord.y/(double)cb->getMapSize().y < 0.34) //southwest
  4230. return 6;
  4231. else if ((double)cord.y/(double)cb->getMapSize().y < 0.67) //south
  4232. return 5;
  4233. else //southeast
  4234. return 4;
  4235. }
  4236. }
  4237. void CGSeerHut::finishQuest(const CGHeroInstance * h, ui32 accept) const
  4238. {
  4239. if (accept)
  4240. {
  4241. switch (quest->missionType)
  4242. {
  4243. case CQuest::MISSION_ART:
  4244. for (std::vector<ui16>::const_iterator it = quest->m5arts.begin(); it != quest->m5arts.end(); ++it)
  4245. {
  4246. cb->removeArtifact(ArtifactLocation(h, h->getArtPos(*it, false)));
  4247. }
  4248. break;
  4249. case CQuest::MISSION_ARMY:
  4250. cb->takeCreatures(h->id, quest->m6creatures);
  4251. break;
  4252. case CQuest::MISSION_RESOURCES:
  4253. for (int i = 0; i < 7; ++i)
  4254. {
  4255. cb->giveResource(h->getOwner(), i, -quest->m7resources[i]);
  4256. }
  4257. break;
  4258. default:
  4259. break;
  4260. }
  4261. cb->setObjProperty (id, 10, CQuest::COMPLETE); //mission complete - for AI
  4262. cb->setObjProperty (id, 11, 0); //no more mission available - redundant?
  4263. completeQuest(h); //make sure to remove QuestQuard at the very end
  4264. }
  4265. }
  4266. void CGSeerHut::completeQuest (const CGHeroInstance * h) const //reward
  4267. {
  4268. switch (rewardType)
  4269. {
  4270. case 1: //experience
  4271. {
  4272. TExpType expVal = h->calculateXp(rVal);
  4273. cb->changePrimSkill(h->id, 4, expVal, false);
  4274. break;
  4275. }
  4276. case 2: //mana points
  4277. {
  4278. cb->setManaPoints(h->id, h->mana+rVal);
  4279. break;
  4280. }
  4281. case 3: case 4: //morale /luck
  4282. {
  4283. Bonus hb(Bonus::ONE_WEEK, (rewardType == 3 ? Bonus::MORALE : Bonus::LUCK),
  4284. Bonus::OBJECT, rVal, h->id, "", -1);
  4285. GiveBonus gb;
  4286. gb.id = h->id;
  4287. gb.bonus = hb;
  4288. cb->giveHeroBonus(&gb);
  4289. }
  4290. break;
  4291. case 5: //resources
  4292. cb->giveResource(h->getOwner(), rID, rVal);
  4293. break;
  4294. case 6: //main ability bonus (attak, defence etd.)
  4295. cb->changePrimSkill(h->id, rID, rVal, false);
  4296. break;
  4297. case 7: // secondary ability gain
  4298. cb->changeSecSkill(h->id, rID, rVal, false);
  4299. break;
  4300. case 8: // artifact
  4301. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[rID],-2);
  4302. break;
  4303. case 9:// spell
  4304. {
  4305. std::set<ui32> spell;
  4306. spell.insert (rID);
  4307. cb->changeSpells(h->id, true, spell);
  4308. }
  4309. break;
  4310. case 10:// creature
  4311. {
  4312. CCreatureSet creatures;
  4313. creatures.setCreature(0, rID, rVal);
  4314. cb->giveCreatures(this, h, creatures, false);
  4315. }
  4316. break;
  4317. default:
  4318. break;
  4319. }
  4320. }
  4321. const CGHeroInstance * CGSeerHut::getHeroToKill(bool allowNull) const
  4322. {
  4323. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  4324. if(allowNull && !o)
  4325. return NULL;
  4326. assert(o && o->ID == Obj::HERO);
  4327. return static_cast<const CGHeroInstance*>(o);
  4328. }
  4329. const CGCreature * CGSeerHut::getCreatureToKill(bool allowNull) const
  4330. {
  4331. const CGObjectInstance *o = cb->getObjByQuestIdentifier(quest->m13489val);
  4332. if(allowNull && !o)
  4333. return NULL;
  4334. assert(o && o->ID == Obj::MONSTER);
  4335. return static_cast<const CGCreature*>(o);
  4336. }
  4337. void CGQuestGuard::initObj()
  4338. {
  4339. blockVisit = true;
  4340. quest->progress = 0;
  4341. quest->textOption = ran()%3 + 3; //3-5
  4342. if (quest->missionType)
  4343. {
  4344. if (!quest->isCustomFirst)
  4345. quest->firstVisitText = VLC->generaltexth->quests[quest->missionType-1][0][quest->textOption];
  4346. if (!quest->isCustomNext)
  4347. quest->nextVisitText = VLC->generaltexth->quests[quest->missionType-1][1][quest->textOption];
  4348. if (!quest->isCustomComplete)
  4349. quest->completedText = VLC->generaltexth->quests[quest->missionType-1][2][quest->textOption];
  4350. }
  4351. else
  4352. quest->firstVisitText = VLC->generaltexth->seerEmpty[quest->textOption];
  4353. }
  4354. void CGQuestGuard::completeQuest(const CGHeroInstance *h) const
  4355. {
  4356. cb->removeObject(id);
  4357. }
  4358. void CGWitchHut::initObj()
  4359. {
  4360. ability = allowedAbilities[ran()%allowedAbilities.size()];
  4361. }
  4362. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  4363. {
  4364. InfoWindow iw;
  4365. iw.soundID = soundBase::gazebo;
  4366. iw.player = h->getOwner();
  4367. if(!wasVisited(h->tempOwner))
  4368. cb->setObjProperty(id,10,h->tempOwner);
  4369. if(h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ability))) //you alredy know this skill
  4370. {
  4371. iw.text << std::pair<ui8,ui32>(11,172);
  4372. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4373. }
  4374. else if(!h->canLearnSkill()) //already all skills slots used
  4375. {
  4376. iw.text << std::pair<ui8,ui32>(11,173);
  4377. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4378. }
  4379. else //give sec skill
  4380. {
  4381. iw.components.push_back(Component(Component::SEC_SKILL, ability, 1, 0));
  4382. iw.text << std::pair<ui8,ui32>(11,171);
  4383. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  4384. cb->changeSecSkill(h->id,ability,1,true);
  4385. }
  4386. cb->showInfoDialog(&iw);
  4387. }
  4388. const std::string & CGWitchHut::getHoverText() const
  4389. {
  4390. hoverName = VLC->generaltexth->names[ID];
  4391. if(wasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  4392. {
  4393. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  4394. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  4395. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4396. if(h && h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(ability))) //hero knows that ability
  4397. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  4398. }
  4399. return hoverName;
  4400. }
  4401. bool CGBonusingObject::wasVisited (const CGHeroInstance * h) const
  4402. {
  4403. return h->hasBonusFrom(Bonus::OBJECT, ID);
  4404. }
  4405. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  4406. {
  4407. bool visited = h->hasBonusFrom(Bonus::OBJECT,ID);
  4408. int messageID=0;
  4409. int bonusMove = 0, sound = -1;
  4410. InfoWindow iw;
  4411. iw.player = h->tempOwner;
  4412. GiveBonus gbonus;
  4413. gbonus.id = h->id;
  4414. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  4415. gbonus.bonus.source = Bonus::OBJECT;
  4416. gbonus.bonus.sid = ID;
  4417. bool second = false;
  4418. Bonus secondBonus;
  4419. switch(ID)
  4420. {
  4421. case Obj::BUOY:
  4422. messageID = 21;
  4423. sound = soundBase::MORALE;
  4424. gbonus.bonus.type = Bonus::MORALE;
  4425. gbonus.bonus.val = +1;
  4426. gbonus.bdescr << std::pair<ui8,ui32>(6,94);
  4427. break;
  4428. case Obj::SWAN_POND:
  4429. messageID = 29;
  4430. sound = soundBase::LUCK;
  4431. gbonus.bonus.type = Bonus::LUCK;
  4432. gbonus.bonus.val = 2;
  4433. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  4434. bonusMove = -h->movement;
  4435. break;
  4436. case Obj::FAERIE_RING:
  4437. messageID = 49;
  4438. sound = soundBase::LUCK;
  4439. gbonus.bonus.type = Bonus::LUCK;
  4440. gbonus.bonus.val = 1;
  4441. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  4442. break;
  4443. case Obj::FOUNTAIN_OF_FORTUNE:
  4444. messageID = 55;
  4445. sound = soundBase::LUCK;
  4446. gbonus.bonus.type = Bonus::LUCK;
  4447. gbonus.bonus.val = rand()%5 - 1;
  4448. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  4449. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  4450. break;
  4451. case Obj::IDOL_OF_FORTUNE:
  4452. messageID = 62;
  4453. sound = soundBase::experience;
  4454. gbonus.bonus.val = 1;
  4455. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  4456. if(cb->getDate(1) == 7) //7th day of week
  4457. {
  4458. gbonus.bonus.type = Bonus::MORALE;
  4459. second = true;
  4460. secondBonus = gbonus.bonus;
  4461. secondBonus.type = Bonus::LUCK;
  4462. }
  4463. else
  4464. {
  4465. gbonus.bonus.type = (cb->getDate(1)%2) ? Bonus::LUCK : Bonus::MORALE;
  4466. }
  4467. break;
  4468. case Obj::MERMAID:
  4469. messageID = 83;
  4470. sound = soundBase::LUCK;
  4471. gbonus.bonus.type = Bonus::LUCK;
  4472. gbonus.bonus.val = 1;
  4473. gbonus.bdescr << std::pair<ui8,ui32>(6,72);
  4474. break;
  4475. case Obj::RALLY_FLAG:
  4476. sound = soundBase::MORALE;
  4477. messageID = 111;
  4478. gbonus.bonus.type = Bonus::MORALE;
  4479. gbonus.bonus.val = 1;
  4480. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  4481. second = true;
  4482. secondBonus = gbonus.bonus;
  4483. secondBonus.type = Bonus::LUCK;
  4484. bonusMove = 400;
  4485. break;
  4486. case Obj::OASIS:
  4487. messageID = 95;
  4488. gbonus.bonus.type = Bonus::MORALE;
  4489. gbonus.bonus.val = 1;
  4490. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  4491. bonusMove = 800;
  4492. break;
  4493. case Obj::TEMPLE:
  4494. messageID = 140;
  4495. iw.soundID = soundBase::temple;
  4496. gbonus.bonus.type = Bonus::MORALE;
  4497. if(cb->getDate(1)==7) //sunday
  4498. {
  4499. gbonus.bonus.val = 2;
  4500. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  4501. }
  4502. else
  4503. {
  4504. gbonus.bonus.val = 1;
  4505. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  4506. }
  4507. break;
  4508. case Obj::WATERING_HOLE:
  4509. sound = soundBase::MORALE;
  4510. messageID = 166;
  4511. gbonus.bonus.type = Bonus::MORALE;
  4512. gbonus.bonus.val = 1;
  4513. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  4514. bonusMove = 400;
  4515. break;
  4516. case Obj::FOUNTAIN_OF_YOUTH:
  4517. sound = soundBase::MORALE;
  4518. messageID = 57;
  4519. gbonus.bonus.type = Bonus::MORALE;
  4520. gbonus.bonus.val = 1;
  4521. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  4522. bonusMove = 400;
  4523. break;
  4524. case Obj::STABLES:
  4525. sound = soundBase::STORE;
  4526. bool someUpgradeDone = false;
  4527. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); ++i)
  4528. {
  4529. if(i->second->type->idNumber == 10)
  4530. {
  4531. cb->changeStackType(StackLocation(h, i->first), VLC->creh->creatures[11]);
  4532. someUpgradeDone = true;
  4533. }
  4534. }
  4535. if (someUpgradeDone)
  4536. {
  4537. messageID = 138;
  4538. iw.components.push_back(Component(Component::CREATURE,11,0,1));
  4539. }
  4540. else
  4541. messageID = 137;
  4542. gbonus.bonus.type = Bonus::LAND_MOVEMENT;
  4543. gbonus.bonus.val = 600;
  4544. bonusMove = 600;
  4545. gbonus.bonus.duration = Bonus::ONE_WEEK;
  4546. gbonus.bdescr << std::pair<ui8,ui32>(6, 100);
  4547. break;
  4548. }
  4549. assert(messageID);
  4550. if(visited)
  4551. {
  4552. if(ID==Obj::RALLY_FLAG || ID==Obj::OASIS || ID==Obj::MERMAID || ID==Obj::STABLES)
  4553. messageID--;
  4554. else
  4555. messageID++;
  4556. }
  4557. else
  4558. {
  4559. //TODO: fix if second bonus val != main bonus val
  4560. if(gbonus.bonus.type == Bonus::MORALE || secondBonus.type == Bonus::MORALE)
  4561. iw.components.push_back(Component(Component::MORALE,0,gbonus.bonus.val,0));
  4562. if(gbonus.bonus.type == Bonus::LUCK || secondBonus.type == Bonus::LUCK)
  4563. iw.components.push_back(Component(Component::LUCK,0,gbonus.bonus.val,0));
  4564. cb->giveHeroBonus(&gbonus);
  4565. if(second)
  4566. {
  4567. gbonus.bonus = secondBonus;
  4568. cb->giveHeroBonus(&gbonus);
  4569. }
  4570. if(bonusMove) //swan pond - take all move points, stables - give move point this day
  4571. {
  4572. SetMovePoints smp;
  4573. smp.hid = h->id;
  4574. smp.val = h->movement + bonusMove;
  4575. cb->setMovePoints(&smp);
  4576. }
  4577. }
  4578. iw.soundID = sound;
  4579. iw.text << std::pair<ui8,ui32>(11,messageID);
  4580. cb->showInfoDialog(&iw);
  4581. }
  4582. const std::string & CGBonusingObject::getHoverText() const
  4583. {
  4584. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  4585. hoverName = VLC->generaltexth->names[ID];
  4586. if(h)
  4587. {
  4588. if(!h->hasBonusFrom(Bonus::OBJECT,ID))
  4589. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  4590. else
  4591. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  4592. }
  4593. return hoverName;
  4594. }
  4595. void CGBonusingObject::initObj()
  4596. {
  4597. if(ID == Obj::BUOY || ID == Obj::MERMAID)
  4598. {
  4599. blockVisit = true;
  4600. }
  4601. }
  4602. void CGMagicSpring::onHeroVisit(const CGHeroInstance * h) const
  4603. {
  4604. int messageID;
  4605. InfoWindow iw;
  4606. iw.player = h->tempOwner;
  4607. iw.soundID = soundBase::GENIE;
  4608. if (!visited)
  4609. {
  4610. if (h->mana > h->manaLimit())
  4611. messageID = 76;
  4612. else
  4613. {
  4614. messageID = 74;
  4615. cb->setManaPoints (h->id, 2 * h->manaLimit());
  4616. cb->setObjProperty (id, ObjProperty::VISITED, true);
  4617. }
  4618. }
  4619. else
  4620. messageID = 75;
  4621. iw.text << std::pair<ui8,ui32>(11,messageID);
  4622. cb->showInfoDialog(&iw);
  4623. }
  4624. const std::string & CGMagicSpring::getHoverText() const
  4625. {
  4626. hoverName = VLC->generaltexth->names[ID];
  4627. if(!visited)
  4628. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  4629. else
  4630. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  4631. return hoverName;
  4632. }
  4633. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  4634. {
  4635. int message;
  4636. InfoWindow iw;
  4637. iw.soundID = soundBase::faerie;
  4638. iw.player = h->tempOwner;
  4639. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Well today
  4640. {
  4641. message = 78;
  4642. }
  4643. else if(h->mana < h->manaLimit())
  4644. {
  4645. giveDummyBonus(h->id);
  4646. cb->setManaPoints(h->id,h->manaLimit());
  4647. message = 77;
  4648. }
  4649. else
  4650. {
  4651. message = 79;
  4652. }
  4653. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  4654. cb->showInfoDialog(&iw);
  4655. }
  4656. const std::string & CGMagicWell::getHoverText() const
  4657. {
  4658. getNameVis(hoverName);
  4659. return hoverName;
  4660. }
  4661. void CGPandoraBox::initObj()
  4662. {
  4663. blockVisit = (ID==Obj::PANDORAS_BOX); //block only if it's really pandora's box (events also derive from that class)
  4664. }
  4665. void CGPandoraBox::onHeroVisit(const CGHeroInstance * h) const
  4666. {
  4667. BlockingDialog bd (true, false);
  4668. bd.player = h->getOwner();
  4669. bd.soundID = soundBase::QUEST;
  4670. bd.text.addTxt (MetaString::ADVOB_TXT, 14);
  4671. cb->showBlockingDialog (&bd, boost::bind (&CGPandoraBox::open, this, h, _1));
  4672. }
  4673. void CGPandoraBox::open( const CGHeroInstance * h, ui32 accept ) const
  4674. {
  4675. if (accept)
  4676. {
  4677. if (stacksCount() > 0) //if pandora's box is protected by army
  4678. {
  4679. InfoWindow iw;
  4680. iw.player = h->tempOwner;
  4681. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4682. cb->showInfoDialog(&iw);
  4683. cb->startBattleI(h, this, boost::bind(&CGPandoraBox::endBattle, this, h, _1)); //grants things after battle
  4684. }
  4685. else if (message.size() == 0 && resources.size() == 0
  4686. && primskills.size() == 0 && abilities.size() == 0
  4687. && abilityLevels.size() == 0 && artifacts.size() == 0
  4688. && spells.size() == 0 && creatures.Slots().size() > 0
  4689. && gainedExp == 0 && manaDiff == 0 && moraleDiff == 0 && luckDiff == 0) //if it gives nothing without battle
  4690. {
  4691. InfoWindow iw;
  4692. iw.player = h->tempOwner;
  4693. iw.text.addTxt(MetaString::ADVOB_TXT, 15);
  4694. cb->showInfoDialog(&iw);
  4695. }
  4696. else //if it gives something without battle
  4697. {
  4698. giveContents (h, false);
  4699. }
  4700. }
  4701. }
  4702. void CGPandoraBox::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  4703. {
  4704. if(result->winner)
  4705. return;
  4706. giveContents(h,true);
  4707. }
  4708. void CGPandoraBox::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  4709. {
  4710. InfoWindow iw;
  4711. iw.player = h->getOwner();
  4712. std::string msg = message; //in case box is removed in the meantime
  4713. bool changesPrimSkill = false;
  4714. for (int i = 0; i < primskills.size(); i++)
  4715. {
  4716. if(primskills[i])
  4717. {
  4718. changesPrimSkill = true;
  4719. break;
  4720. }
  4721. }
  4722. if(gainedExp || changesPrimSkill || abilities.size())
  4723. {
  4724. TExpType expVal = h->calculateXp(gainedExp);
  4725. //getText(iw,afterBattle,175,h); //wtf?
  4726. iw.text.addTxt(MetaString::ADVOB_TXT, 175); //%s learns something
  4727. iw.text.addReplacement(h->name);
  4728. if(expVal)
  4729. iw.components.push_back(Component(Component::EXPERIENCE,0,expVal,0));
  4730. for(int i=0; i<primskills.size(); i++)
  4731. if(primskills[i])
  4732. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  4733. for(int i=0; i<abilities.size(); i++)
  4734. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  4735. cb->showInfoDialog(&iw);
  4736. //give exp
  4737. if(expVal)
  4738. cb->changePrimSkill(h->id,4,expVal,false);
  4739. //give prim skills
  4740. for(int i=0; i<primskills.size(); i++)
  4741. if(primskills[i])
  4742. cb->changePrimSkill(h->id,i,primskills[i],false);
  4743. //give sec skills
  4744. for(int i=0; i<abilities.size(); i++)
  4745. {
  4746. int curLev = h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(abilities[i]));
  4747. if( (curLev && curLev < abilityLevels[i]) || (h->canLearnSkill() ))
  4748. {
  4749. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  4750. }
  4751. }
  4752. }
  4753. if(spells.size())
  4754. {
  4755. std::set<ui32> spellsToGive;
  4756. iw.components.clear();
  4757. if (spells.size() > 1)
  4758. {
  4759. iw.text.addTxt(MetaString::ADVOB_TXT, 188); //%s learns spells
  4760. }
  4761. else
  4762. {
  4763. iw.text.addTxt(MetaString::ADVOB_TXT, 184); //%s learns a spell
  4764. }
  4765. iw.text.addReplacement(h->name);
  4766. std::vector<ConstTransitivePtr<CSpell> > * sp = &VLC->spellh->spells;
  4767. for(std::vector<si32>::const_iterator i=spells.begin(); i != spells.end(); i++)
  4768. {
  4769. if ((*sp)[*i]->level <= h->getSecSkillLevel(CGHeroInstance::WISDOM) + 2) //enough wisdom
  4770. {
  4771. iw.components.push_back(Component(Component::SPELL,*i,0,0));
  4772. spellsToGive.insert(*i);
  4773. }
  4774. }
  4775. if(spellsToGive.size())
  4776. {
  4777. cb->changeSpells(h->id,true,spellsToGive);
  4778. cb->showInfoDialog(&iw);
  4779. }
  4780. }
  4781. if(manaDiff)
  4782. {
  4783. getText(iw,afterBattle,manaDiff,176,177,h);
  4784. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  4785. cb->showInfoDialog(&iw);
  4786. cb->setManaPoints(h->id, h->mana + manaDiff);
  4787. }
  4788. if(moraleDiff)
  4789. {
  4790. getText(iw,afterBattle,moraleDiff,178,179,h);
  4791. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  4792. cb->showInfoDialog(&iw);
  4793. GiveBonus gb;
  4794. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,moraleDiff,id,"");
  4795. gb.id = h->id;
  4796. cb->giveHeroBonus(&gb);
  4797. }
  4798. if(luckDiff)
  4799. {
  4800. getText(iw,afterBattle,luckDiff,180,181,h);
  4801. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  4802. cb->showInfoDialog(&iw);
  4803. GiveBonus gb;
  4804. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,luckDiff,id,"");
  4805. gb.id = h->id;
  4806. cb->giveHeroBonus(&gb);
  4807. }
  4808. iw.components.clear();
  4809. iw.text.clear();
  4810. for(int i=0; i<resources.size(); i++)
  4811. {
  4812. if(resources[i] < 0)
  4813. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4814. }
  4815. if(iw.components.size())
  4816. {
  4817. getText(iw,afterBattle,182,h);
  4818. cb->showInfoDialog(&iw);
  4819. }
  4820. iw.components.clear();
  4821. iw.text.clear();
  4822. for(int i=0; i<resources.size(); i++)
  4823. {
  4824. if(resources[i] > 0)
  4825. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  4826. }
  4827. if(iw.components.size())
  4828. {
  4829. getText(iw,afterBattle,183,h);
  4830. cb->showInfoDialog(&iw);
  4831. }
  4832. iw.components.clear();
  4833. // getText(iw,afterBattle,183,h);
  4834. iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure
  4835. iw.text.addReplacement(h->name);
  4836. for(int i=0; i<artifacts.size(); i++)
  4837. {
  4838. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  4839. if(iw.components.size() >= 14)
  4840. {
  4841. cb->showInfoDialog(&iw);
  4842. iw.components.clear();
  4843. iw.text.addTxt(MetaString::ADVOB_TXT, 183); //% has found treasure - once more?
  4844. iw.text.addReplacement(h->name);
  4845. }
  4846. }
  4847. if(iw.components.size())
  4848. {
  4849. cb->showInfoDialog(&iw);
  4850. }
  4851. for(int i=0; i<resources.size(); i++)
  4852. if(resources[i])
  4853. cb->giveResource(h->getOwner(),i,resources[i]);
  4854. for(int i=0; i<artifacts.size(); i++)
  4855. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[artifacts[i]],-2);
  4856. iw.components.clear();
  4857. iw.text.clear();
  4858. if (creatures.Slots().size())
  4859. { //this part is taken straight from creature bank
  4860. MetaString loot;
  4861. for(TSlots::const_iterator i = creatures.Slots().begin(); i != creatures.Slots().end(); i++)
  4862. { //build list of joined creatures
  4863. iw.components.push_back(Component(*i->second));
  4864. loot << "%s";
  4865. loot.addReplacement(*i->second);
  4866. }
  4867. if (creatures.Slots().size() == 1 && creatures.Slots().begin()->second->count == 1)
  4868. iw.text.addTxt(MetaString::ADVOB_TXT, 185);
  4869. else
  4870. iw.text.addTxt(MetaString::ADVOB_TXT, 186);
  4871. iw.text.addReplacement(loot.buildList());
  4872. iw.text.addReplacement(h->name);
  4873. cb->showInfoDialog(&iw);
  4874. cb->giveCreatures(this, h, creatures, true);
  4875. }
  4876. if(!afterBattle && msg.size())
  4877. {
  4878. iw.text << msg;
  4879. cb->showInfoDialog(&iw);
  4880. }
  4881. if (!creatures.Slots().size())
  4882. cb->removeObject(id); //only when we don't need to display garrison window
  4883. }
  4884. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  4885. {
  4886. if(afterBattle || !message.size())
  4887. {
  4888. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  4889. iw.text.addReplacement(h->name);
  4890. }
  4891. else
  4892. {
  4893. iw.text << message;
  4894. afterBattle = true;
  4895. }
  4896. }
  4897. void CGPandoraBox::getText( InfoWindow &iw, bool &afterBattle, int val, int negative, int positive, const CGHeroInstance * h ) const
  4898. {
  4899. iw.components.clear();
  4900. iw.text.clear();
  4901. if(afterBattle || !message.size())
  4902. {
  4903. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  4904. iw.text.addReplacement(h->name);
  4905. }
  4906. else
  4907. {
  4908. iw.text << message;
  4909. afterBattle = true;
  4910. }
  4911. }
  4912. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  4913. {
  4914. if(!(availableFor & (1 << h->tempOwner)))
  4915. return;
  4916. if(cb->getPlayerSettings(h->tempOwner)->playerID)
  4917. {
  4918. if(humanActivate)
  4919. activated(h);
  4920. }
  4921. else if(computerActivate)
  4922. activated(h);
  4923. }
  4924. void CGEvent::activated( const CGHeroInstance * h ) const
  4925. {
  4926. if(stacksCount() > 0)
  4927. {
  4928. InfoWindow iw;
  4929. iw.player = h->tempOwner;
  4930. if(message.size())
  4931. iw.text << message;
  4932. else
  4933. iw.text.addTxt(MetaString::ADVOB_TXT, 16);
  4934. cb->showInfoDialog(&iw);
  4935. cb->startBattleI(h, this, boost::bind(&CGEvent::endBattle,this,h,_1));
  4936. }
  4937. else
  4938. {
  4939. giveContents(h,false);
  4940. }
  4941. }
  4942. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  4943. {
  4944. InfoWindow iw;
  4945. iw.player = h->tempOwner;
  4946. switch (ID)
  4947. {
  4948. case Obj::REDWOOD_OBSERVATORY:
  4949. case Obj::PILLAR_OF_FIRE:
  4950. {
  4951. iw.soundID = soundBase::LIGHTHOUSE;
  4952. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==Obj::PILLAR_OF_FIRE));
  4953. FoWChange fw;
  4954. fw.player = h->tempOwner;
  4955. fw.mode = 1;
  4956. cb->getTilesInRange (fw.tiles, pos, 20, h->tempOwner, 1);
  4957. cb->sendAndApply (&fw);
  4958. break;
  4959. }
  4960. case Obj::COVER_OF_DARKNESS:
  4961. {
  4962. iw.text.addTxt (MetaString::ADVOB_TXT, 31);
  4963. hideTiles(h->tempOwner, 20);
  4964. break;
  4965. }
  4966. }
  4967. cb->showInfoDialog(&iw);
  4968. }
  4969. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  4970. {
  4971. if(spell == 255)
  4972. {
  4973. tlog1 << "Not initialized shrine visited!\n";
  4974. return;
  4975. }
  4976. if(!wasVisited(h->tempOwner))
  4977. cb->setObjProperty(id,10,h->tempOwner);
  4978. InfoWindow iw;
  4979. iw.soundID = soundBase::temple;
  4980. iw.player = h->getOwner();
  4981. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  4982. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  4983. iw.text << ".";
  4984. if(!h->getArt(ArtifactPosition::SPELLBOOK))
  4985. {
  4986. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  4987. }
  4988. else if(ID == Obj::SHRINE_OF_MAGIC_THOUGHT && !h->getSecSkillLevel(CGHeroInstance::WISDOM)) //it's third level spell and hero doesn't have wisdom
  4989. {
  4990. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  4991. }
  4992. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  4993. {
  4994. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  4995. }
  4996. else //give spell
  4997. {
  4998. std::set<ui32> spells;
  4999. spells.insert(spell);
  5000. cb->changeSpells(h->id, true, spells);
  5001. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  5002. }
  5003. cb->showInfoDialog(&iw);
  5004. }
  5005. void CGShrine::initObj()
  5006. {
  5007. if(spell == 255) //spell not set
  5008. {
  5009. int level = ID-87;
  5010. std::vector<ui16> possibilities;
  5011. cb->getAllowedSpells (possibilities, level);
  5012. if(!possibilities.size())
  5013. {
  5014. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  5015. return;
  5016. }
  5017. spell = possibilities[ran() % possibilities.size()];
  5018. }
  5019. }
  5020. const std::string & CGShrine::getHoverText() const
  5021. {
  5022. hoverName = VLC->generaltexth->names[ID];
  5023. if(wasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  5024. {
  5025. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  5026. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell]->name);
  5027. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  5028. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  5029. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  5030. }
  5031. return hoverName;
  5032. }
  5033. void CGSignBottle::initObj()
  5034. {
  5035. //if no text is set than we pick random from the predefined ones
  5036. if(!message.size())
  5037. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  5038. if(ID == Obj::OCEAN_BOTTLE)
  5039. {
  5040. blockVisit = true;
  5041. }
  5042. }
  5043. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  5044. {
  5045. InfoWindow iw;
  5046. iw.soundID = soundBase::STORE;
  5047. iw.player = h->getOwner();
  5048. iw.text << message;
  5049. cb->showInfoDialog(&iw);
  5050. if(ID == Obj::OCEAN_BOTTLE)
  5051. cb->removeObject(id);
  5052. }
  5053. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  5054. {
  5055. }
  5056. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  5057. {
  5058. int type = bonusType;
  5059. int bid = bonusID;
  5060. //check if the bonus if applicable, if not - give primary skill (always possible)
  5061. int ssl = h->getSecSkillLevel(static_cast<CGHeroInstance::SecondarySkill>(bid)); //current sec skill level, used if bonusType == 1
  5062. if((type == 1
  5063. && ((ssl == 3) || (!ssl && !h->canLearnSkill()))) ////hero already has expert level in the skill or (don't know skill and doesn't have free slot)
  5064. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells, (ui32) bid)
  5065. || (VLC->spellh->spells[bid]->level > h->getSecSkillLevel(CGHeroInstance::WISDOM) + 2)
  5066. ))) //hero doesn't have a spellbook or already knows the spell or doesn't have Wisdom
  5067. {
  5068. type = 0;
  5069. bid = ran() % GameConstants::PRIMARY_SKILLS;
  5070. }
  5071. InfoWindow iw;
  5072. iw.soundID = soundBase::gazebo;
  5073. iw.player = h->getOwner();
  5074. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  5075. switch (type)
  5076. {
  5077. case 0:
  5078. cb->changePrimSkill(h->id,bid,+1);
  5079. iw.components.push_back(Component(Component::PRIM_SKILL,bid,+1,0));
  5080. break;
  5081. case 1:
  5082. cb->changeSecSkill(h->id,bid,+1);
  5083. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  5084. break;
  5085. case 2:
  5086. {
  5087. std::set<ui32> hlp;
  5088. hlp.insert(bid);
  5089. cb->changeSpells(h->id,true,hlp);
  5090. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  5091. }
  5092. break;
  5093. default:
  5094. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  5095. return;
  5096. }
  5097. cb->showInfoDialog(&iw);
  5098. cb->removeObject(id);
  5099. }
  5100. void CGScholar::initObj()
  5101. {
  5102. blockVisit = true;
  5103. if(bonusType == 255)
  5104. {
  5105. bonusType = ran()%3;
  5106. switch(bonusType)
  5107. {
  5108. case 0:
  5109. bonusID = ran() % GameConstants::PRIMARY_SKILLS;
  5110. break;
  5111. case 1:
  5112. bonusID = ran() % GameConstants::SKILL_QUANTITY;
  5113. break;
  5114. case 2:
  5115. std::vector<ui16> possibilities;
  5116. for (int i = 1; i < 6; ++i)
  5117. cb->getAllowedSpells (possibilities, i);
  5118. bonusID = possibilities[ran() % possibilities.size()];
  5119. break;
  5120. }
  5121. }
  5122. }
  5123. void CGGarrison::onHeroVisit (const CGHeroInstance *h) const
  5124. {
  5125. int ally = cb->gameState()->getPlayerRelations(h->tempOwner, tempOwner);
  5126. if (!ally && stacksCount() > 0) {
  5127. //TODO: Find a way to apply magic garrison effects in battle.
  5128. cb->startBattleI(h, this, boost::bind(&CGGarrison::fightOver, this, h, _1));
  5129. return;
  5130. }
  5131. //New owner.
  5132. if (!ally)
  5133. cb->setOwner(id, h->tempOwner);
  5134. cb->showGarrisonDialog(id, h->id, removableUnits, 0);
  5135. }
  5136. void CGGarrison::fightOver (const CGHeroInstance *h, BattleResult *result) const
  5137. {
  5138. if (result->winner == 0)
  5139. onHeroVisit(h);
  5140. }
  5141. ui8 CGGarrison::getPassableness() const
  5142. {
  5143. if ( !stacksCount() )//empty - anyone can visit
  5144. return GameConstants::ALL_PLAYERS;
  5145. if ( tempOwner == GameConstants::NEUTRAL_PLAYER )//neutral guarded - no one can visit
  5146. return 0;
  5147. ui8 mask = 0;
  5148. TeamState * ts = cb->gameState()->getPlayerTeam(tempOwner);
  5149. BOOST_FOREACH(ui8 it, ts->players)
  5150. mask |= 1<<it;//allies - add to possible visitors
  5151. return mask;
  5152. }
  5153. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  5154. {
  5155. int sound;
  5156. int txtid;
  5157. switch(ID)
  5158. {
  5159. case Obj::CORPSE:
  5160. txtid = 37;
  5161. sound = soundBase::MYSTERY;
  5162. break;
  5163. case Obj::LEAN_TO:
  5164. sound = soundBase::GENIE;
  5165. txtid = 64;
  5166. break;
  5167. case Obj::WAGON:
  5168. sound = soundBase::GENIE;
  5169. txtid = 154;
  5170. break;
  5171. case Obj::WARRIORS_TOMB:
  5172. {
  5173. //ask if player wants to search the Tomb
  5174. BlockingDialog bd(true, false);
  5175. bd.soundID = soundBase::GRAVEYARD;
  5176. bd.player = h->getOwner();
  5177. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  5178. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  5179. return;
  5180. }
  5181. break;
  5182. default:
  5183. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  5184. return;
  5185. }
  5186. InfoWindow iw;
  5187. iw.soundID = sound;
  5188. iw.player = h->getOwner();
  5189. if(players.size()) //we have been already visited...
  5190. {
  5191. txtid++;
  5192. if(ID == Obj::WAGON) //wagon has extra text (for finding art) we need to omit
  5193. txtid++;
  5194. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5195. }
  5196. else //first visit - give bonus!
  5197. {
  5198. switch(artOrRes)
  5199. {
  5200. case 0: // first visit but empty
  5201. if (ID == Obj::CORPSE)
  5202. ++txtid;
  5203. else
  5204. txtid+=2;
  5205. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5206. break;
  5207. case 1: //art
  5208. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  5209. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[bonusType],-2);
  5210. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5211. if (ID == Obj::CORPSE)
  5212. {
  5213. iw.text << "%s";
  5214. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5215. }
  5216. break;
  5217. case 2: //res
  5218. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  5219. iw.components.push_back (Component(Component::RESOURCE, bonusType, bonusVal, 0));
  5220. cb->giveResource(h->getOwner(), bonusType, bonusVal);
  5221. break;
  5222. }
  5223. if(ID == Obj::WAGON && artOrRes == 1)
  5224. {
  5225. iw.text.localStrings.back().second++;
  5226. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5227. }
  5228. }
  5229. cb->showInfoDialog(&iw);
  5230. cb->setObjProperty(id,10,h->getOwner());
  5231. }
  5232. const std::string & CGOnceVisitable::getHoverText() const
  5233. {
  5234. hoverName = VLC->generaltexth->names[ID] + " ";
  5235. hoverName += (wasVisited(cb->getCurrentPlayer())
  5236. ? (VLC->generaltexth->allTexts[352]) //visited
  5237. : ( VLC->generaltexth->allTexts[353])); //not visited
  5238. return hoverName;
  5239. }
  5240. void CGOnceVisitable::initObj()
  5241. {
  5242. switch(ID)
  5243. {
  5244. case Obj::CORPSE:
  5245. {
  5246. blockVisit = true;
  5247. int hlp = ran()%100;
  5248. if(hlp < 20)
  5249. {
  5250. artOrRes = 1;
  5251. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  5252. }
  5253. else
  5254. {
  5255. artOrRes = 0;
  5256. }
  5257. }
  5258. break;
  5259. case Obj::LEAN_TO:
  5260. {
  5261. artOrRes = 2;
  5262. bonusType = ran()%6; //any basic resource without gold
  5263. bonusVal = ran()%4 + 1;
  5264. }
  5265. break;
  5266. case Obj::WARRIORS_TOMB:
  5267. {
  5268. artOrRes = 1;
  5269. int hlp = ran()%100;
  5270. if(hlp < 30)
  5271. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE);
  5272. else if(hlp < 80)
  5273. bonusType = cb->getRandomArt (CArtifact::ART_MINOR);
  5274. else if(hlp < 95)
  5275. bonusType = cb->getRandomArt (CArtifact::ART_MAJOR);
  5276. else
  5277. bonusType = cb->getRandomArt (CArtifact::ART_RELIC);
  5278. }
  5279. break;
  5280. case Obj::WAGON:
  5281. {
  5282. int hlp = ran()%100;
  5283. if(hlp < 10)
  5284. {
  5285. artOrRes = 0; // nothing... :(
  5286. }
  5287. else if(hlp < 50) //minor or treasure art
  5288. {
  5289. artOrRes = 1;
  5290. bonusType = cb->getRandomArt (CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  5291. }
  5292. else //2 - 5 of non-gold resource
  5293. {
  5294. artOrRes = 2;
  5295. bonusType = ran()%6;
  5296. bonusVal = ran()%4 + 2;
  5297. }
  5298. }
  5299. break;
  5300. }
  5301. }
  5302. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  5303. {
  5304. if(accept)
  5305. {
  5306. InfoWindow iw;
  5307. iw.player = h->getOwner();
  5308. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  5309. if(players.size()) //we've been already visited, player found nothing
  5310. {
  5311. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  5312. }
  5313. else //first visit - give artifact
  5314. {
  5315. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  5316. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  5317. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  5318. cb->giveHeroNewArtifact(h, VLC->arth->artifacts[bonusType],-2);
  5319. }
  5320. if(!h->hasBonusFrom(Bonus::OBJECT,ID)) //we don't have modifier from this object yet
  5321. {
  5322. //ruin morale
  5323. GiveBonus gb;
  5324. gb.id = h->id;
  5325. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::MORALE,Bonus::OBJECT,-3,id,"");
  5326. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  5327. cb->giveHeroBonus(&gb);
  5328. }
  5329. cb->showInfoDialog(&iw);
  5330. cb->setObjProperty(id,10,h->getOwner());
  5331. }
  5332. }
  5333. void CBank::initObj()
  5334. {
  5335. index = VLC->objh->bankObjToIndex(this);
  5336. bc = NULL;
  5337. daycounter = 0;
  5338. multiplier = 1;
  5339. }
  5340. const std::string & CBank::getHoverText() const
  5341. {
  5342. hoverName = VLC->objh->creBanksNames[index];
  5343. if (bc == NULL)
  5344. hoverName += " " + VLC->generaltexth->allTexts[352];
  5345. else
  5346. hoverName += " " + VLC->generaltexth->allTexts[353];
  5347. return hoverName;
  5348. }
  5349. void CBank::reset(ui16 var1) //prevents desync
  5350. {
  5351. ui8 chance = 0;
  5352. for (ui8 i = 0; i < VLC->objh->banksInfo[index].size(); i++)
  5353. {
  5354. if (var1 < (chance += VLC->objh->banksInfo[index][i]->chance))
  5355. {
  5356. bc = VLC->objh->banksInfo[index][i];
  5357. break;
  5358. }
  5359. }
  5360. artifacts.clear();
  5361. }
  5362. void CBank::initialize() const
  5363. {
  5364. cb->setObjProperty (id, 14, ran()); //synchronous reset
  5365. for (ui8 i = 0; i <= 3; i++)
  5366. {
  5367. for (ui8 n = 0; n < bc->artifacts[i]; n++) //new function using proper randomization algorithm
  5368. {
  5369. cb->setObjProperty (id, 18 + i, ran()); //synchronic artifacts
  5370. }
  5371. }
  5372. cb->setObjProperty (id, 17, ran()); //get army
  5373. }
  5374. void CBank::setPropertyDer (ui8 what, ui32 val)
  5375. /// random values are passed as arguments and processed identically on all clients
  5376. {
  5377. switch (what)
  5378. {
  5379. case 11: //daycounter
  5380. if (val == 0)
  5381. daycounter = 1; //yes, 1
  5382. else
  5383. daycounter++;
  5384. break;
  5385. case 12: //multiplier, in percent
  5386. multiplier = val / 100.0;
  5387. break;
  5388. case 13: //bank preset
  5389. bc = VLC->objh->banksInfo[index][val];
  5390. break;
  5391. case 14:
  5392. reset (val%100);
  5393. break;
  5394. case 15:
  5395. bc = NULL;
  5396. break;
  5397. case 16: //remove rewards from Derelict Ship
  5398. artifacts.clear();
  5399. break;
  5400. case 17: //set ArmedInstance army
  5401. {
  5402. int upgraded = 0;
  5403. if (val%100 < bc->upgradeChance) //once again anti-desync
  5404. upgraded = 1;
  5405. switch (bc->guards.size())
  5406. {
  5407. case 1:
  5408. for (int i = 0; i < 4; ++i)
  5409. setCreature (i, bc->guards[0].first, bc->guards[0].second / 5 );
  5410. setCreature (4, bc->guards[0].first + upgraded, bc->guards[0].second / 5 );
  5411. break;
  5412. case 4:
  5413. {
  5414. std::vector< std::pair <ui16, ui32> >::const_iterator it;
  5415. if (bc->guards.back().second) //all stacks are present
  5416. {
  5417. for (it = bc->guards.begin(); it != bc->guards.end(); it++)
  5418. {
  5419. setCreature (stacksCount(), it->first, it->second);
  5420. }
  5421. }
  5422. else if (bc->guards[2].second)//Wraiths are present, split two stacks in Crypt
  5423. {
  5424. setCreature (0, bc->guards[0].first, bc->guards[0].second / 2 );
  5425. setCreature (1, bc->guards[1].first, bc->guards[1].second / 2);
  5426. setCreature (2, bc->guards[2].first + upgraded, bc->guards[2].second);
  5427. setCreature (3, bc->guards[1].first, bc->guards[1].second / 2 );
  5428. setCreature (4, bc->guards[0].first, bc->guards[0].second - (bc->guards[0].second / 2) );
  5429. }
  5430. else //split both stacks
  5431. {
  5432. for (int i = 0; i < 3; ++i) //skellies
  5433. setCreature (2*i, bc->guards[0].first, bc->guards[0].second / 3);
  5434. for (int i = 0; i < 2; ++i) //zombies
  5435. setCreature (2*i+1, bc->guards[1].first, bc->guards[1].second / 2);
  5436. }
  5437. }
  5438. break;
  5439. default:
  5440. tlog2 << "Error: Unexpected army data: " << bc->guards.size() <<" items found";
  5441. return;
  5442. }
  5443. }
  5444. break;
  5445. case 18: //add Artifact
  5446. {
  5447. int id = cb->getArtSync(val, CArtifact::ART_TREASURE);
  5448. artifacts.push_back (id);
  5449. cb->erasePickedArt(id);
  5450. break;
  5451. }
  5452. case 19: //add Artifact
  5453. {
  5454. int id = cb->getArtSync(val, CArtifact::ART_MINOR);
  5455. artifacts.push_back (id);
  5456. cb->erasePickedArt(id);
  5457. break;
  5458. }
  5459. case 20: //add Artifact
  5460. {
  5461. int id = cb->getArtSync(val, CArtifact::ART_MAJOR);
  5462. artifacts.push_back (id);
  5463. cb->erasePickedArt(id);
  5464. break;
  5465. }
  5466. case 21: //add Artifact
  5467. {
  5468. int id = cb->getArtSync(val, CArtifact::ART_RELIC);
  5469. artifacts.push_back (id);
  5470. cb->erasePickedArt(id);
  5471. break;
  5472. }
  5473. }
  5474. }
  5475. void CBank::newTurn() const
  5476. {
  5477. if (bc == NULL)
  5478. {
  5479. if (cb->getDate(0) == 1)
  5480. initialize(); //initialize on first day
  5481. else if (daycounter >= 28 && (subID < 13 || subID > 16)) //no reset for Emissaries
  5482. {
  5483. initialize();
  5484. cb->setObjProperty (id, 11, 0); //daycounter 0
  5485. if (ID == Obj::DERELICT_SHIP && cb->getDate(0) > 1)
  5486. {
  5487. cb->setObjProperty (id, 12, 0);//ugly hack to make derelict ships usable only once
  5488. cb->setObjProperty (id, 16, 0);
  5489. }
  5490. }
  5491. else
  5492. cb->setObjProperty (id, 11, 1); //daycounter++
  5493. }
  5494. }
  5495. bool CBank::wasVisited (ui8 player) const
  5496. {
  5497. return !bc;
  5498. }
  5499. void CBank::onHeroVisit (const CGHeroInstance * h) const
  5500. {
  5501. if (bc)
  5502. {
  5503. int banktext = 0;
  5504. switch (ID)
  5505. {
  5506. case Obj::CREATURE_BANK:
  5507. banktext = 32;
  5508. break;
  5509. case Obj::DERELICT_SHIP:
  5510. banktext = 41;
  5511. break;
  5512. case Obj::DRAGON_UTOPIA:
  5513. banktext = 47;
  5514. break;
  5515. case Obj::CRYPT:
  5516. banktext = 119;
  5517. break;
  5518. case Obj::SHIPWRECK:
  5519. banktext = 122;
  5520. break;
  5521. }
  5522. BlockingDialog bd (true, false);
  5523. bd.player = h->getOwner();
  5524. bd.soundID = soundBase::ROGUE;
  5525. bd.text << VLC->generaltexth->advobtxt[banktext];
  5526. if (ID == Obj::CREATURE_BANK)
  5527. bd.text.addReplacement(VLC->objh->creBanksNames[index]);
  5528. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  5529. }
  5530. else
  5531. {
  5532. InfoWindow iw;
  5533. iw.soundID = soundBase::GRAVEYARD;
  5534. iw.player = h->getOwner();
  5535. if (ID == Obj::CRYPT) //morale penalty for empty Crypt
  5536. {
  5537. GiveBonus gbonus;
  5538. gbonus.id = h->id;
  5539. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5540. gbonus.bonus.source = Bonus::OBJECT;
  5541. gbonus.bonus.sid = ID;
  5542. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[98];
  5543. gbonus.bonus.type = Bonus::MORALE;
  5544. gbonus.bonus.val = -1;
  5545. cb->giveHeroBonus(&gbonus);
  5546. iw.text << VLC->generaltexth->advobtxt[120];
  5547. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5548. }
  5549. else
  5550. {
  5551. iw.text << VLC->generaltexth->advobtxt[33];
  5552. iw.text.addReplacement(VLC->objh->creBanksNames[index]);
  5553. }
  5554. cb->showInfoDialog(&iw);
  5555. }
  5556. }
  5557. void CBank::fightGuards (const CGHeroInstance * h, ui32 accept) const
  5558. {
  5559. if (accept)
  5560. {
  5561. cb->startBattleI (h, this, boost::bind (&CBank::endBattle, this, h, _1), true);
  5562. }
  5563. }
  5564. void CBank::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  5565. {
  5566. if (result->winner == 0)
  5567. {
  5568. int textID = -1;
  5569. InfoWindow iw;
  5570. iw.player = h->getOwner();
  5571. MetaString loot;
  5572. switch (ID)
  5573. {
  5574. case Obj::CREATURE_BANK: case Obj::DRAGON_UTOPIA:
  5575. textID = 34;
  5576. break;
  5577. case Obj::DERELICT_SHIP:
  5578. if (multiplier)
  5579. textID = 43;
  5580. else
  5581. {
  5582. GiveBonus gbonus;
  5583. gbonus.id = h->id;
  5584. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5585. gbonus.bonus.source = Bonus::OBJECT;
  5586. gbonus.bonus.sid = ID;
  5587. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[101];
  5588. gbonus.bonus.type = Bonus::MORALE;
  5589. gbonus.bonus.val = -1;
  5590. cb->giveHeroBonus(&gbonus);
  5591. textID = 42;
  5592. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5593. }
  5594. break;
  5595. case Obj::CRYPT:
  5596. if (bc->resources.size() != 0)
  5597. textID = 121;
  5598. else
  5599. {
  5600. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5601. GiveBonus gbonus;
  5602. gbonus.id = h->id;
  5603. gbonus.bonus.duration = Bonus::ONE_BATTLE;
  5604. gbonus.bonus.source = Bonus::OBJECT;
  5605. gbonus.bonus.sid = ID;
  5606. gbonus.bdescr << "\n" << VLC->generaltexth->arraytxt[ID];
  5607. gbonus.bonus.type = Bonus::MORALE;
  5608. gbonus.bonus.val = -1;
  5609. cb->giveHeroBonus(&gbonus);
  5610. textID = 120;
  5611. iw.components.push_back (Component (Component::MORALE, 0 , -1, 0));
  5612. }
  5613. break;
  5614. case Obj::SHIPWRECK:
  5615. if (bc->resources.size())
  5616. textID = 124;
  5617. else
  5618. textID = 123;
  5619. break;
  5620. }
  5621. //grant resources
  5622. if (textID != 42) //empty derelict ship gives no cash
  5623. {
  5624. for (int it = 0; it < bc->resources.size(); it++)
  5625. {
  5626. if (bc->resources[it] != 0)
  5627. {
  5628. iw.components.push_back (Component (Component::RESOURCE, it, bc->resources[it], 0));
  5629. loot << "%d %s";
  5630. loot.addReplacement(iw.components.back().val);
  5631. loot.addReplacement(MetaString::RES_NAMES, iw.components.back().subtype);
  5632. cb->giveResource (h->getOwner(), it, bc->resources[it]);
  5633. }
  5634. }
  5635. }
  5636. //grant artifacts
  5637. for (std::vector<ui32>::const_iterator it = artifacts.begin(); it != artifacts.end(); it++)
  5638. {
  5639. iw.components.push_back (Component (Component::ARTIFACT, *it, 0, 0));
  5640. loot << "%s";
  5641. loot.addReplacement(MetaString::ART_NAMES, *it);
  5642. cb->giveHeroNewArtifact (h, VLC->arth->artifacts[*it], -2);
  5643. }
  5644. //display loot
  5645. if (!iw.components.empty())
  5646. {
  5647. if (textID == 34)
  5648. {
  5649. iw.text.addTxt(MetaString::ADVOB_TXT, 34);//Heaving defeated %s, you discover %s
  5650. iw.text.addReplacement(MetaString::CRE_PL_NAMES, result->casualties[1].begin()->first);
  5651. iw.text.addReplacement(loot.buildList());
  5652. }
  5653. else
  5654. iw.text.addTxt (MetaString::ADVOB_TXT, textID);
  5655. cb->showInfoDialog(&iw);
  5656. }
  5657. loot.clear();
  5658. iw.components.clear();
  5659. iw.text.clear();
  5660. //grant creatures
  5661. CCreatureSet ourArmy;
  5662. for (std::vector< std::pair <ui16, ui32> >::const_iterator it = bc->creatures.begin(); it != bc->creatures.end(); it++)
  5663. {
  5664. int slot = ourArmy.getSlotFor(it->first);
  5665. ourArmy.addToSlot(slot, it->first, it->second);
  5666. }
  5667. for (TSlots::const_iterator i = ourArmy.Slots().begin(); i != ourArmy.Slots().end(); i++)
  5668. {
  5669. iw.components.push_back(Component(*i->second));
  5670. loot << "%s";
  5671. loot.addReplacement(*i->second);
  5672. }
  5673. if (ourArmy.Slots().size())
  5674. {
  5675. if (ourArmy.Slots().size() == 1 && ourArmy.Slots().begin()->second->count == 1)
  5676. iw.text.addTxt (MetaString::ADVOB_TXT, 185);
  5677. else
  5678. iw.text.addTxt (MetaString::ADVOB_TXT, 186);
  5679. iw.text.addReplacement(loot.buildList());
  5680. iw.text.addReplacement(h->name);
  5681. cb->showInfoDialog(&iw);
  5682. cb->giveCreatures(this, h, ourArmy, false);
  5683. }
  5684. cb->setObjProperty (id, 15, 0); //bc = NULL
  5685. }
  5686. }
  5687. void CGPyramid::initObj()
  5688. {
  5689. std::vector<ui16> available;
  5690. cb->getAllowedSpells (available, 5);
  5691. if (available.size())
  5692. {
  5693. bc = VLC->objh->banksInfo[21].front(); //TODO: remove hardcoded value?
  5694. spell = (available[rand()%available.size()]);
  5695. }
  5696. else
  5697. {
  5698. tlog1 <<"No spells available for Pyramid! Object set to empty.\n";
  5699. }
  5700. setPropertyDer (17,ran()); //set guards at game start
  5701. }
  5702. const std::string & CGPyramid::getHoverText() const
  5703. {
  5704. hoverName = VLC->objh->creBanksNames[21];
  5705. if (bc == NULL)
  5706. hoverName += " " + VLC->generaltexth->allTexts[352];
  5707. else
  5708. hoverName += " " + VLC->generaltexth->allTexts[353];
  5709. return hoverName;
  5710. }
  5711. void CGPyramid::onHeroVisit (const CGHeroInstance * h) const
  5712. {
  5713. if (bc)
  5714. {
  5715. BlockingDialog bd (true, false);
  5716. bd.player = h->getOwner();
  5717. bd.soundID = soundBase::MYSTERY;
  5718. bd.text << VLC->generaltexth->advobtxt[105];
  5719. cb->showBlockingDialog (&bd, boost::bind (&CBank::fightGuards, this, h, _1));
  5720. }
  5721. else
  5722. {
  5723. InfoWindow iw;
  5724. iw.player = h->getOwner();
  5725. iw.text << VLC->generaltexth->advobtxt[107];
  5726. iw.components.push_back (Component (Component::LUCK, 0 , -2, 0));
  5727. GiveBonus gb;
  5728. gb.bonus = Bonus(Bonus::ONE_BATTLE,Bonus::LUCK,Bonus::OBJECT,-2,id,VLC->generaltexth->arraytxt[70]);
  5729. gb.id = h->id;
  5730. cb->giveHeroBonus(&gb);
  5731. cb->showInfoDialog(&iw);
  5732. }
  5733. }
  5734. void CGPyramid::endBattle (const CGHeroInstance *h, const BattleResult *result) const
  5735. {
  5736. if (result->winner == 0)
  5737. {
  5738. InfoWindow iw;
  5739. iw.player = h->getOwner();
  5740. iw.text.addTxt (MetaString::ADVOB_TXT, 106);
  5741. iw.text.addTxt (MetaString::SPELL_NAME, spell);
  5742. if (!h->getArt(17))
  5743. iw.text.addTxt (MetaString::ADVOB_TXT, 109); //no spellbook
  5744. else if (h->getSecSkillLevel(CGHeroInstance::WISDOM) < 3)
  5745. iw.text.addTxt (MetaString::ADVOB_TXT, 108); //no expert Wisdom
  5746. else
  5747. {
  5748. std::set<ui32> spells;
  5749. spells.insert (spell);
  5750. cb->changeSpells (h->id, true, spells);
  5751. iw.components.push_back(Component (Component::SPELL, spell, 0, 0));
  5752. }
  5753. cb->showInfoDialog(&iw);
  5754. cb->setObjProperty (id, 15, 0);
  5755. }
  5756. }
  5757. void CGKeys::setPropertyDer (ui8 what, ui32 val) //101-108 - enable key for player 1-8
  5758. {
  5759. if (what >= 101 && what <= (100 + GameConstants::PLAYER_LIMIT))
  5760. playerKeyMap.find(what-101)->second.insert((ui8)val);
  5761. }
  5762. bool CGKeys::wasMyColorVisited (int player) const
  5763. {
  5764. if (vstd::contains(playerKeyMap[player], subID)) //creates set if it's not there
  5765. return true;
  5766. else
  5767. return false;
  5768. }
  5769. const std::string CGKeys::getName() const
  5770. {
  5771. std::string name;
  5772. name = VLC->generaltexth->tentColors[subID] + " " + VLC->generaltexth->names[ID];
  5773. return name;
  5774. }
  5775. const std::string & CGKeymasterTent::getHoverText() const
  5776. {
  5777. hoverName = getName();
  5778. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  5779. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  5780. else
  5781. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  5782. return hoverName;
  5783. }
  5784. bool CGKeymasterTent::wasVisited (ui8 player) const
  5785. {
  5786. return wasMyColorVisited (player);
  5787. }
  5788. void CGKeymasterTent::onHeroVisit( const CGHeroInstance * h ) const
  5789. {
  5790. InfoWindow iw;
  5791. iw.soundID = soundBase::CAVEHEAD;
  5792. iw.player = h->getOwner();
  5793. if (!wasMyColorVisited (h->getOwner()) )
  5794. {
  5795. cb->setObjProperty(id, h->tempOwner+101, subID);
  5796. iw.text << std::pair<ui8,ui32>(11,19);
  5797. }
  5798. else
  5799. iw.text << std::pair<ui8,ui32>(11,20);
  5800. cb->showInfoDialog(&iw);
  5801. }
  5802. void CGBorderGuard::initObj()
  5803. {
  5804. //ui32 m13489val = subID; //store color as quest info
  5805. blockVisit = true;
  5806. }
  5807. const std::string & CGBorderGuard::getHoverText() const
  5808. {
  5809. hoverName = getName();
  5810. if (wasMyColorVisited (cb->getCurrentPlayer()) )//TODO: use local player, not current
  5811. hoverName += "\n" + VLC->generaltexth->allTexts[352];
  5812. else
  5813. hoverName += "\n" + VLC->generaltexth->allTexts[353];
  5814. return hoverName;
  5815. }
  5816. void CGBorderGuard::getVisitText (MetaString &text, std::vector<Component> &components, bool isCustom, bool FirstVisit, const CGHeroInstance * h) const
  5817. {
  5818. text << std::pair<ui8,ui32>(11,18);
  5819. }
  5820. void CGBorderGuard::getRolloverText (MetaString &text, bool onHover) const
  5821. {
  5822. if (!onHover)
  5823. text << VLC->generaltexth->tentColors[subID] << " " << VLC->generaltexth->names[Obj::KEYMASTER];
  5824. }
  5825. bool CGBorderGuard::checkQuest (const CGHeroInstance * h) const
  5826. {
  5827. return wasMyColorVisited (h->tempOwner);
  5828. }
  5829. void CGBorderGuard::onHeroVisit( const CGHeroInstance * h ) const
  5830. {
  5831. if (wasMyColorVisited (h->getOwner()) )
  5832. {
  5833. BlockingDialog bd (true, false);
  5834. bd.player = h->getOwner();
  5835. bd.soundID = soundBase::QUEST;
  5836. bd.text.addTxt (MetaString::ADVOB_TXT, 17);
  5837. cb->showBlockingDialog (&bd, boost::bind (&CGBorderGuard::openGate, this, h, _1));
  5838. }
  5839. else
  5840. {
  5841. InfoWindow iw;
  5842. iw.player = h->getOwner();
  5843. iw.soundID = soundBase::CAVEHEAD;
  5844. iw.text << std::pair<ui8,ui32>(11,18);
  5845. cb->showInfoDialog (&iw);
  5846. AddQuest aq;
  5847. aq.quest = QuestInfo (quest, this, visitablePos());
  5848. aq.player = h->tempOwner;
  5849. cb->sendAndApply (&aq);
  5850. //TODO: add this quest only once OR check for multiple instances later
  5851. }
  5852. }
  5853. void CGBorderGuard::openGate(const CGHeroInstance *h, ui32 accept) const
  5854. {
  5855. if (accept)
  5856. cb->removeObject(id);
  5857. }
  5858. void CGBorderGate::onHeroVisit( const CGHeroInstance * h ) const //TODO: passability
  5859. {
  5860. if (!wasMyColorVisited (h->getOwner()) )
  5861. {
  5862. InfoWindow iw;
  5863. iw.player = h->getOwner();
  5864. iw.text << std::pair<ui8,ui32>(11,18);
  5865. cb->showInfoDialog(&iw);
  5866. AddQuest aq;
  5867. aq.quest = QuestInfo (quest, this, visitablePos());
  5868. aq.player = h->tempOwner;
  5869. cb->sendAndApply (&aq);
  5870. }
  5871. }
  5872. ui8 CGBorderGate::getPassableness() const
  5873. {
  5874. ui8 ret = 0;
  5875. for (int i = 0; i < GameConstants::PLAYER_LIMIT; i++)
  5876. ret |= wasMyColorVisited(i)<<i;
  5877. return ret;
  5878. }
  5879. void CGMagi::initObj()
  5880. {
  5881. if (ID == Obj::EYE_OF_MAGI)
  5882. {
  5883. blockVisit = true;
  5884. eyelist[subID].push_back(id);
  5885. }
  5886. }
  5887. void CGMagi::onHeroVisit(const CGHeroInstance * h) const
  5888. {
  5889. if (ID == Obj::HUT_OF_MAGI)
  5890. {
  5891. InfoWindow iw;
  5892. CenterView cv;
  5893. FoWChange fw;
  5894. cv.player = iw.player = fw.player = h->tempOwner;
  5895. iw.soundID = soundBase::LIGHTHOUSE;
  5896. iw.player = h->tempOwner;
  5897. iw.text.addTxt (MetaString::ADVOB_TXT, 61);
  5898. cb->showInfoDialog(&iw);
  5899. fw.mode = 1;
  5900. std::vector<si32>::iterator it;
  5901. for (it = eyelist[subID].begin(); it < eyelist[subID].end(); it++)
  5902. {
  5903. const CGObjectInstance *eye = cb->getObj(*it);
  5904. cb->getTilesInRange (fw.tiles, eye->pos, 10, h->tempOwner, 1);
  5905. cb->sendAndApply(&fw);
  5906. cv.pos = eye->pos;
  5907. cv.focusTime = 2000;
  5908. cb->sendAndApply(&cv);
  5909. }
  5910. cv.pos = h->getPosition(false);
  5911. cb->sendAndApply(&cv);
  5912. }
  5913. else if (ID == Obj::EYE_OF_MAGI)
  5914. {
  5915. InfoWindow iw;
  5916. iw.player = h->tempOwner;
  5917. iw.text.addTxt (MetaString::ADVOB_TXT, 48);
  5918. cb->showInfoDialog(&iw);
  5919. }
  5920. }
  5921. void CGBoat::initObj()
  5922. {
  5923. hero = NULL;
  5924. }
  5925. void CGSirens::initObj()
  5926. {
  5927. blockVisit = true;
  5928. }
  5929. const std::string & CGSirens::getHoverText() const
  5930. {
  5931. getNameVis(hoverName);
  5932. return hoverName;
  5933. }
  5934. void CGSirens::onHeroVisit( const CGHeroInstance * h ) const
  5935. {
  5936. InfoWindow iw;
  5937. iw.soundID = soundBase::DANGER;
  5938. iw.player = h->tempOwner;
  5939. if(h->hasBonusFrom(Bonus::OBJECT,ID)) //has already visited Sirens
  5940. {
  5941. iw.text.addTxt(11,133);
  5942. }
  5943. else
  5944. {
  5945. giveDummyBonus(h->id, Bonus::ONE_BATTLE);
  5946. int xp = 0;
  5947. for (TSlots::const_iterator i = h->Slots().begin(); i != h->Slots().end(); i++)
  5948. {
  5949. TQuantity drown = i->second->count * 0.3;
  5950. if(drown)
  5951. {
  5952. cb->changeStackCount(StackLocation(h, i->first), -drown);
  5953. xp += drown * i->second->type->valOfBonuses(Bonus::STACK_HEALTH);
  5954. }
  5955. }
  5956. if(xp)
  5957. {
  5958. xp = h->calculateXp(xp);
  5959. iw.text.addTxt(11,132);
  5960. iw.text.addReplacement(xp);
  5961. cb->changePrimSkill(h->id, 4, xp, false);
  5962. }
  5963. else
  5964. {
  5965. iw.text.addTxt(11,134);
  5966. }
  5967. }
  5968. cb->showInfoDialog(&iw);
  5969. }
  5970. //bool IShipyard::validLocation() const
  5971. //{
  5972. // std::vector<int3> offsets;
  5973. // getOutOffsets(offsets);
  5974. //
  5975. // TerrainTile *tile;
  5976. // for(int i = 0; i < offsets.size(); i++)
  5977. // if((tile = IObjectInterface::cb->getTile(o->pos + offsets[i])) && tile->terType == TerrainTile::water) //tile is in the map and is water
  5978. // return true;
  5979. // return false;
  5980. //}
  5981. int3 IBoatGenerator::bestLocation() const
  5982. {
  5983. std::vector<int3> offsets;
  5984. getOutOffsets(offsets);
  5985. for (int i = 0; i < offsets.size(); ++i)
  5986. {
  5987. if (const TerrainTile *tile = IObjectInterface::cb->getTile(o->pos + offsets[i], false)) //tile is in the map
  5988. {
  5989. if (tile->terType == ETerrainType::WATER && (!tile->blocked || tile->blockingObjects.front()->ID == 8)) //and is water and is not blocked or is blocked by boat
  5990. return o->pos + offsets[i];
  5991. }
  5992. }
  5993. return int3 (-1,-1,-1);
  5994. }
  5995. IBoatGenerator::EGeneratorState IBoatGenerator::state() const
  5996. {
  5997. int3 tile = bestLocation();
  5998. const TerrainTile *t = IObjectInterface::cb->getTile(tile);
  5999. if(!t)
  6000. return TILE_BLOCKED; //no available water
  6001. else if(!t->blockingObjects.size())
  6002. return GOOD; //OK
  6003. else if(t->blockingObjects.front()->ID == Obj::BOAT)
  6004. return BOAT_ALREADY_BUILT; //blocked with boat
  6005. else
  6006. return TILE_BLOCKED; //blocked
  6007. }
  6008. int IBoatGenerator::getBoatType() const
  6009. {
  6010. //We make good ships by default
  6011. return 1;
  6012. }
  6013. IBoatGenerator::IBoatGenerator(const CGObjectInstance *O)
  6014. : o(O)
  6015. {
  6016. }
  6017. void IBoatGenerator::getProblemText(MetaString &out, const CGHeroInstance *visitor) const
  6018. {
  6019. switch(state())
  6020. {
  6021. case BOAT_ALREADY_BUILT:
  6022. out.addTxt(MetaString::GENERAL_TXT, 51);
  6023. break;
  6024. case TILE_BLOCKED:
  6025. if(visitor)
  6026. {
  6027. out.addTxt(MetaString::GENERAL_TXT, 134);
  6028. out.addReplacement(visitor->name);
  6029. }
  6030. else
  6031. out.addTxt(MetaString::ADVOB_TXT, 189);
  6032. break;
  6033. case NO_WATER:
  6034. tlog1 << "Shipyard without water!!! " << o->pos << "\t" << o->id << std::endl;
  6035. return;
  6036. }
  6037. }
  6038. void IShipyard::getBoatCost( std::vector<si32> &cost ) const
  6039. {
  6040. cost.resize(GameConstants::RESOURCE_QUANTITY);
  6041. cost[0] = 10;
  6042. cost[6] = 1000;
  6043. }
  6044. IShipyard::IShipyard(const CGObjectInstance *O)
  6045. : IBoatGenerator(O)
  6046. {
  6047. }
  6048. IShipyard * IShipyard::castFrom( CGObjectInstance *obj )
  6049. {
  6050. if(!obj)
  6051. return NULL;
  6052. if(obj->ID == Obj::TOWN)
  6053. {
  6054. return static_cast<CGTownInstance*>(obj);
  6055. }
  6056. else if(obj->ID == Obj::SHIPYARD)
  6057. {
  6058. return static_cast<CGShipyard*>(obj);
  6059. }
  6060. else
  6061. {
  6062. //tlog1 << "Cannot cast to IShipyard object with ID " << obj->ID << std::endl;
  6063. return NULL;
  6064. }
  6065. }
  6066. const IShipyard * IShipyard::castFrom( const CGObjectInstance *obj )
  6067. {
  6068. return castFrom(const_cast<CGObjectInstance*>(obj));
  6069. }
  6070. CGShipyard::CGShipyard()
  6071. :IShipyard(this)
  6072. {
  6073. }
  6074. void CGShipyard::getOutOffsets( std::vector<int3> &offsets ) const
  6075. {
  6076. // H J L K I
  6077. // A x S x B
  6078. // C E G F D
  6079. offsets += int3(-3,0,0), int3(1,0,0), //AB
  6080. int3(-3,1,0), int3(1,1,0), int3(-2,1,0), int3(0,1,0), int3(-1,1,0), //CDEFG
  6081. int3(-3,-1,0), int3(1,-1,0), int3(-2,-1,0), int3(0,-1,0), int3(-1,-1,0); //HIJKL
  6082. }
  6083. void CGShipyard::onHeroVisit( const CGHeroInstance * h ) const
  6084. {
  6085. if(!cb->gameState()->getPlayerRelations(tempOwner, h->tempOwner))
  6086. cb->setOwner(id, h->tempOwner);
  6087. auto s = state();
  6088. if(s != IBoatGenerator::GOOD)
  6089. {
  6090. InfoWindow iw;
  6091. iw.player = tempOwner;
  6092. getProblemText(iw.text, h);
  6093. cb->showInfoDialog(&iw);
  6094. }
  6095. else
  6096. {
  6097. OpenWindow ow;
  6098. ow.id1 = id;
  6099. ow.id2 = h->id;
  6100. ow.window = OpenWindow::SHIPYARD_WINDOW;
  6101. cb->sendAndApply(&ow);
  6102. }
  6103. }
  6104. void CCartographer::onHeroVisit( const CGHeroInstance * h ) const
  6105. {
  6106. if (!wasVisited (h->getOwner()) ) //if hero has not visited yet this cartographer
  6107. {
  6108. if (cb->getResource(h->tempOwner, 6) >= 1000) //if he can afford a map
  6109. {
  6110. //ask if he wants to buy one
  6111. int text=0;
  6112. switch (subID)
  6113. {
  6114. case 0:
  6115. text = 25;
  6116. break;
  6117. case 1:
  6118. text = 26;
  6119. break;
  6120. case 2:
  6121. text = 27;
  6122. break;
  6123. default:
  6124. tlog2 << "Unrecognized subtype of cartographer" << std::endl;
  6125. }
  6126. assert(text);
  6127. BlockingDialog bd (true, false);
  6128. bd.player = h->getOwner();
  6129. bd.soundID = soundBase::LIGHTHOUSE;
  6130. bd.text.addTxt (MetaString::ADVOB_TXT, text);
  6131. cb->showBlockingDialog (&bd, boost::bind (&CCartographer::buyMap, this, h, _1));
  6132. }
  6133. else //if he cannot afford
  6134. {
  6135. InfoWindow iw;
  6136. iw.player = h->getOwner();
  6137. iw.soundID = soundBase::CAVEHEAD;
  6138. iw.text << std::pair<ui8,ui32>(11,28);
  6139. cb->showInfoDialog (&iw);
  6140. }
  6141. }
  6142. else //if he already visited carographer
  6143. {
  6144. InfoWindow iw;
  6145. iw.player = h->getOwner();
  6146. iw.soundID = soundBase::CAVEHEAD;
  6147. iw.text << std::pair<ui8,ui32>(11,24);
  6148. cb->showInfoDialog (&iw);
  6149. }
  6150. }
  6151. void CCartographer::buyMap (const CGHeroInstance *h, ui32 accept) const
  6152. {
  6153. if (accept) //if hero wants to buy map
  6154. {
  6155. cb->giveResource (h->tempOwner, 6, -1000);
  6156. FoWChange fw;
  6157. fw.mode = 1;
  6158. fw.player = h->tempOwner;
  6159. //subIDs of different types of cartographers:
  6160. //water = 0; land = 1; underground = 2;
  6161. cb->getAllTiles (fw.tiles, h->tempOwner, subID - 1, !subID + 1); //reveal appropriate tiles
  6162. cb->sendAndApply (&fw);
  6163. cb->setObjProperty (id, 10, h->tempOwner);
  6164. }
  6165. }
  6166. void CGDenOfthieves::onHeroVisit (const CGHeroInstance * h) const
  6167. {
  6168. cb->showThievesGuildWindow(h->tempOwner, id);
  6169. }
  6170. void CGObelisk::onHeroVisit( const CGHeroInstance * h ) const
  6171. {
  6172. InfoWindow iw;
  6173. iw.player = h->tempOwner;
  6174. TeamState *ts = cb->gameState()->getPlayerTeam(h->tempOwner);
  6175. assert(ts);
  6176. int team = ts->id;
  6177. if(!wasVisited(team))
  6178. {
  6179. iw.text.addTxt(MetaString::ADVOB_TXT, 96);
  6180. cb->sendAndApply(&iw);
  6181. cb->setObjProperty(id,20,team); //increment general visited obelisks counter
  6182. OpenWindow ow;
  6183. ow.id1 = h->tempOwner;
  6184. ow.window = OpenWindow::PUZZLE_MAP;
  6185. cb->sendAndApply(&ow);
  6186. cb->setObjProperty(id,10,team); //mark that particular obelisk as visited
  6187. }
  6188. else
  6189. {
  6190. iw.text.addTxt(MetaString::ADVOB_TXT, 97);
  6191. cb->sendAndApply(&iw);
  6192. }
  6193. }
  6194. void CGObelisk::initObj()
  6195. {
  6196. obeliskCount++;
  6197. }
  6198. const std::string & CGObelisk::getHoverText() const
  6199. {
  6200. hoverName = VLC->generaltexth->names[ID];
  6201. if(wasVisited(cb->getCurrentPlayer()))
  6202. hoverName += " " + VLC->generaltexth->allTexts[352]; //not visited
  6203. else
  6204. hoverName += " " + VLC->generaltexth->allTexts[353]; //visited
  6205. return hoverName;
  6206. }
  6207. void CGObelisk::setPropertyDer( ui8 what, ui32 val )
  6208. {
  6209. CPlayersVisited::setPropertyDer(what, val);
  6210. switch(what)
  6211. {
  6212. case 20:
  6213. assert(val < GameConstants::PLAYER_LIMIT);
  6214. visited[val]++;
  6215. if(visited[val] > obeliskCount)
  6216. {
  6217. tlog0 << "Error: Visited " << visited[val] << "\t\t" << obeliskCount << std::endl;
  6218. assert(0);
  6219. }
  6220. break;
  6221. }
  6222. }
  6223. void CGLighthouse::onHeroVisit( const CGHeroInstance * h ) const
  6224. {
  6225. if(h->tempOwner != tempOwner)
  6226. {
  6227. ui8 oldOwner = tempOwner;
  6228. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  6229. InfoWindow iw;
  6230. iw.player = h->tempOwner;
  6231. iw.text.addTxt(MetaString::ADVOB_TXT, 69);
  6232. iw.soundID = soundBase::LIGHTHOUSE;
  6233. cb->sendAndApply(&iw);
  6234. giveBonusTo(h->tempOwner);
  6235. if(oldOwner < GameConstants::PLAYER_LIMIT) //remove bonus from old owner
  6236. {
  6237. RemoveBonus rb(RemoveBonus::PLAYER);
  6238. rb.whoID = oldOwner;
  6239. rb.source = Bonus::OBJECT;
  6240. rb.id = id;
  6241. cb->sendAndApply(&rb);
  6242. }
  6243. }
  6244. }
  6245. void CGLighthouse::initObj()
  6246. {
  6247. if(tempOwner < GameConstants::PLAYER_LIMIT)
  6248. {
  6249. giveBonusTo(tempOwner);
  6250. }
  6251. }
  6252. const std::string & CGLighthouse::getHoverText() const
  6253. {
  6254. hoverName = VLC->generaltexth->names[ID];
  6255. //TODO: owned by %s player
  6256. return hoverName;
  6257. }
  6258. void CGLighthouse::giveBonusTo( ui8 player ) const
  6259. {
  6260. GiveBonus gb(GiveBonus::PLAYER);
  6261. gb.bonus.type = Bonus::SEA_MOVEMENT;
  6262. gb.bonus.val = 500;
  6263. gb.id = player;
  6264. gb.bonus.duration = Bonus::PERMANENT;
  6265. gb.bonus.source = Bonus::OBJECT;
  6266. gb.bonus.sid = id;
  6267. cb->sendAndApply(&gb);
  6268. }
  6269. void CArmedInstance::randomizeArmy(int type)
  6270. {
  6271. int max = VLC->creh->creatures.size();
  6272. for (TSlots::iterator j = stacks.begin(); j != stacks.end();j++)
  6273. {
  6274. int randID = j->second->idRand;
  6275. if(randID > max)
  6276. {
  6277. int level = (randID-VLC->creh->creatures.size()) / 2 -1;
  6278. bool upgrade = !(randID % 2);
  6279. j->second->setType(VLC->townh->towns[type].creatures[level][upgrade]);
  6280. randID = -1;
  6281. }
  6282. assert(j->second->armyObj == this);
  6283. }
  6284. return;
  6285. }
  6286. CArmedInstance::CArmedInstance()
  6287. {
  6288. battle = NULL;
  6289. }
  6290. int CArmedInstance::valOfGlobalBonuses(CSelector selector) const
  6291. {
  6292. //if (tempOwner != NEUTRAL_PLAYER)
  6293. return cb->gameState()->players[tempOwner].valOfBonuses(selector);
  6294. }
  6295. void CArmedInstance::updateMoraleBonusFromArmy()
  6296. {
  6297. if(!validTypes(false)) //object not randomized, don't bother
  6298. return;
  6299. Bonus *b = getBonusList().getFirst(Selector::sourceType(Bonus::ARMY) && Selector::type(Bonus::MORALE));
  6300. if(!b)
  6301. {
  6302. b = new Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, 0, -1);
  6303. addNewBonus(b);
  6304. }
  6305. //number of alignments and presence of undead
  6306. std::set<TFaction> factions;
  6307. bool hasUndead = false;
  6308. BOOST_FOREACH(auto slot, Slots())
  6309. {
  6310. const CStackInstance * inst = slot.second;
  6311. const CCreature * creature = VLC->creh->creatures[inst->getCreatureID()];
  6312. factions.insert(creature->faction);
  6313. // Check for undead flag instead of faction (undead mummies are neutral)
  6314. hasUndead |= inst->hasBonusOfType(Bonus::UNDEAD);
  6315. }
  6316. size_t factionsInArmy = factions.size();
  6317. // Take Angelic Alliance troop-mixing freedom of non-evil units into account.
  6318. if (hasBonusOfType(Bonus::NONEVIL_ALIGNMENT_MIX))
  6319. {
  6320. size_t mixableFactions = 0;
  6321. BOOST_FOREACH(TFaction f, factions)
  6322. {
  6323. if (VLC->townh->factions[f].alignment != EAlignment::EVIL)
  6324. mixableFactions++;
  6325. }
  6326. if (mixableFactions > 0)
  6327. factionsInArmy -= mixableFactions - 1;
  6328. }
  6329. if(factionsInArmy == 1)
  6330. {
  6331. b->val = +1;
  6332. b->description = VLC->generaltexth->arraytxt[115]; //All troops of one alignment +1
  6333. }
  6334. else
  6335. {
  6336. b->val = 2 - factionsInArmy;
  6337. b->description = boost::str(boost::format(VLC->generaltexth->arraytxt[114]) % factionsInArmy % b->val); //Troops of %d alignments %d
  6338. }
  6339. boost::algorithm::trim(b->description);
  6340. //-1 modifier for any Undead unit in army
  6341. const ui8 UNDEAD_MODIFIER_ID = -2;
  6342. Bonus *undeadModifier = getBonusList().getFirst(Selector::source(Bonus::ARMY, UNDEAD_MODIFIER_ID));
  6343. if(hasUndead)
  6344. {
  6345. if(!undeadModifier)
  6346. addNewBonus(new Bonus(Bonus::PERMANENT, Bonus::MORALE, Bonus::ARMY, -1, UNDEAD_MODIFIER_ID, VLC->generaltexth->arraytxt[116]));
  6347. }
  6348. else if(undeadModifier)
  6349. removeBonus(undeadModifier);
  6350. }
  6351. void CArmedInstance::armyChanged()
  6352. {
  6353. updateMoraleBonusFromArmy();
  6354. }
  6355. CBonusSystemNode * CArmedInstance::whereShouldBeAttached(CGameState *gs)
  6356. {
  6357. if(tempOwner < GameConstants::PLAYER_LIMIT)
  6358. return gs->getPlayer(tempOwner);
  6359. else
  6360. return &gs->globalEffects;
  6361. }
  6362. CBonusSystemNode * CArmedInstance::whatShouldBeAttached()
  6363. {
  6364. return this;
  6365. }
  6366. bool IMarket::getOffer(int id1, int id2, int &val1, int &val2, EMarketMode::EMarketMode mode) const
  6367. {
  6368. switch(mode)
  6369. {
  6370. case EMarketMode::RESOURCE_RESOURCE:
  6371. {
  6372. double effectiveness = std::min((getMarketEfficiency() + 1.0) / 20.0, 0.5);
  6373. double r = VLC->objh->resVals[id1], //value of given resource
  6374. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  6375. if(r>g) //if given resource is more expensive than wanted
  6376. {
  6377. val2 = ceil(r / g);
  6378. val1 = 1;
  6379. }
  6380. else //if wanted resource is more expensive
  6381. {
  6382. val1 = (g / r) + 0.5;
  6383. val2 = 1;
  6384. }
  6385. }
  6386. break;
  6387. case EMarketMode::CREATURE_RESOURCE:
  6388. {
  6389. const double effectivenessArray[] = {0.0, 0.3, 0.45, 0.50, 0.65, 0.7, 0.85, 0.9, 1.0};
  6390. double effectiveness = effectivenessArray[std::min(getMarketEfficiency(), 8)];
  6391. double r = VLC->creh->creatures[id1]->cost[6], //value of given creature in gold
  6392. g = VLC->objh->resVals[id2] / effectiveness; //value of wanted resource
  6393. if(r>g) //if given resource is more expensive than wanted
  6394. {
  6395. val2 = ceil(r / g);
  6396. val1 = 1;
  6397. }
  6398. else //if wanted resource is more expensive
  6399. {
  6400. val1 = (g / r) + 0.5;
  6401. val2 = 1;
  6402. }
  6403. }
  6404. break;
  6405. case EMarketMode::RESOURCE_PLAYER:
  6406. val1 = 1;
  6407. val2 = 1;
  6408. break;
  6409. case EMarketMode::RESOURCE_ARTIFACT:
  6410. {
  6411. double effectiveness = std::min((getMarketEfficiency() + 3.0) / 20.0, 0.6);
  6412. double r = VLC->objh->resVals[id1], //value of offered resource
  6413. g = VLC->arth->artifacts[id2]->price / effectiveness; //value of bought artifact in gold
  6414. if(id1 != 6) //non-gold prices are doubled
  6415. r /= 2;
  6416. val1 = std::max(1, (int)((g / r) + 0.5)); //don't sell arts for less than 1 resource
  6417. val2 = 1;
  6418. }
  6419. break;
  6420. case EMarketMode::ARTIFACT_RESOURCE:
  6421. {
  6422. double effectiveness = std::min((getMarketEfficiency() + 3.0) / 20.0, 0.6);
  6423. double r = VLC->arth->artifacts[id1]->price * effectiveness,
  6424. g = VLC->objh->resVals[id2];
  6425. // if(id2 != 6) //non-gold prices are doubled
  6426. // r /= 2;
  6427. val1 = 1;
  6428. val2 = std::max(1, (int)((r / g) + 0.5)); //at least one resource is given in return
  6429. }
  6430. break;
  6431. case EMarketMode::CREATURE_EXP:
  6432. {
  6433. val1 = 1;
  6434. val2 = (VLC->creh->creatures[id1]->AIValue / 40) * 5;
  6435. }
  6436. break;
  6437. case EMarketMode::ARTIFACT_EXP:
  6438. {
  6439. val1 = 1;
  6440. int givenClass = VLC->arth->artifacts[id1]->getArtClassSerial();
  6441. if(givenClass < 0 || givenClass > 3)
  6442. {
  6443. val2 = 0;
  6444. return false;
  6445. }
  6446. static const int expPerClass[] = {1000, 1500, 3000, 6000};
  6447. val2 = expPerClass[givenClass];
  6448. }
  6449. break;
  6450. default:
  6451. assert(0);
  6452. return false;
  6453. }
  6454. return true;
  6455. }
  6456. bool IMarket::allowsTrade(EMarketMode::EMarketMode mode) const
  6457. {
  6458. return false;
  6459. }
  6460. int IMarket::availableUnits(EMarketMode::EMarketMode mode, int marketItemSerial) const
  6461. {
  6462. switch(mode)
  6463. {
  6464. case EMarketMode::RESOURCE_RESOURCE:
  6465. case EMarketMode::ARTIFACT_RESOURCE:
  6466. case EMarketMode::CREATURE_RESOURCE:
  6467. return -1;
  6468. default:
  6469. return 1;
  6470. }
  6471. }
  6472. std::vector<int> IMarket::availableItemsIds(EMarketMode::EMarketMode mode) const
  6473. {
  6474. std::vector<int> ret;
  6475. switch(mode)
  6476. {
  6477. case EMarketMode::RESOURCE_RESOURCE:
  6478. case EMarketMode::ARTIFACT_RESOURCE:
  6479. case EMarketMode::CREATURE_RESOURCE:
  6480. for (int i = 0; i < 7; i++)
  6481. ret.push_back(i);
  6482. default:
  6483. break;
  6484. }
  6485. return ret;
  6486. }
  6487. const IMarket * IMarket::castFrom(const CGObjectInstance *obj, bool verbose /*= true*/)
  6488. {
  6489. switch(obj->ID)
  6490. {
  6491. case Obj::TOWN:
  6492. return static_cast<const CGTownInstance*>(obj);
  6493. case Obj::ALTAR_OF_SACRIFICE:
  6494. case Obj::BLACK_MARKET:
  6495. case Obj::TRADING_POST:
  6496. case Obj::TRADING_POST_SNOW:
  6497. case Obj::FREELANCERS_GUILD:
  6498. return static_cast<const CGMarket*>(obj);
  6499. case Obj::UNIVERSITY:
  6500. return static_cast<const CGUniversity*>(obj);
  6501. default:
  6502. if(verbose)
  6503. tlog1 << "Cannot cast to IMarket object with ID " << obj->ID << std::endl;
  6504. return NULL;
  6505. }
  6506. }
  6507. IMarket::IMarket(const CGObjectInstance *O)
  6508. :o(O)
  6509. {
  6510. }
  6511. std::vector<EMarketMode::EMarketMode> IMarket::availableModes() const
  6512. {
  6513. std::vector<EMarketMode::EMarketMode> ret;
  6514. for (int i = 0; i < EMarketMode::MARTKET_AFTER_LAST_PLACEHOLDER; i++)
  6515. if(allowsTrade((EMarketMode::EMarketMode)i))
  6516. ret.push_back((EMarketMode::EMarketMode)i);
  6517. return ret;
  6518. }
  6519. void CGMarket::onHeroVisit(const CGHeroInstance * h) const
  6520. {
  6521. OpenWindow ow;
  6522. ow.id1 = id;
  6523. ow.id2 = h->id;
  6524. ow.window = OpenWindow::MARKET_WINDOW;
  6525. cb->sendAndApply(&ow);
  6526. }
  6527. int CGMarket::getMarketEfficiency() const
  6528. {
  6529. return 5;
  6530. }
  6531. bool CGMarket::allowsTrade(EMarketMode::EMarketMode mode) const
  6532. {
  6533. switch(mode)
  6534. {
  6535. case EMarketMode::RESOURCE_RESOURCE:
  6536. case EMarketMode::RESOURCE_PLAYER:
  6537. switch(ID)
  6538. {
  6539. case Obj::TRADING_POST:
  6540. case Obj::TRADING_POST_SNOW:
  6541. return true;
  6542. default:
  6543. return false;
  6544. }
  6545. case EMarketMode::CREATURE_RESOURCE:
  6546. return ID == Obj::FREELANCERS_GUILD;
  6547. //case ARTIFACT_RESOURCE:
  6548. case EMarketMode::RESOURCE_ARTIFACT:
  6549. return ID == Obj::BLACK_MARKET;
  6550. case EMarketMode::ARTIFACT_EXP:
  6551. case EMarketMode::CREATURE_EXP:
  6552. return ID == Obj::ALTAR_OF_SACRIFICE; //TODO? check here for alignment of visiting hero? - would not be coherent with other checks here
  6553. case EMarketMode::RESOURCE_SKILL:
  6554. return ID == Obj::UNIVERSITY;
  6555. default:
  6556. return false;
  6557. }
  6558. }
  6559. int CGMarket::availableUnits(EMarketMode::EMarketMode mode, int marketItemSerial) const
  6560. {
  6561. return -1;
  6562. }
  6563. std::vector<int> CGMarket::availableItemsIds(EMarketMode::EMarketMode mode) const
  6564. {
  6565. switch(mode)
  6566. {
  6567. case EMarketMode::RESOURCE_RESOURCE:
  6568. case EMarketMode::RESOURCE_PLAYER:
  6569. return IMarket::availableItemsIds(mode);
  6570. default:
  6571. return std::vector<int>();
  6572. }
  6573. }
  6574. CGMarket::CGMarket()
  6575. :IMarket(this)
  6576. {
  6577. }
  6578. std::vector<int> CGBlackMarket::availableItemsIds(EMarketMode::EMarketMode mode) const
  6579. {
  6580. switch(mode)
  6581. {
  6582. case EMarketMode::ARTIFACT_RESOURCE:
  6583. return IMarket::availableItemsIds(mode);
  6584. case EMarketMode::RESOURCE_ARTIFACT:
  6585. {
  6586. std::vector<int> ret;
  6587. BOOST_FOREACH(const CArtifact *a, artifacts)
  6588. if(a)
  6589. ret.push_back(a->id);
  6590. else
  6591. ret.push_back(-1);
  6592. return ret;
  6593. }
  6594. default:
  6595. return std::vector<int>();
  6596. }
  6597. }
  6598. void CGBlackMarket::newTurn() const
  6599. {
  6600. if(cb->getDate(2) != 1)
  6601. return;
  6602. SetAvailableArtifacts saa;
  6603. saa.id = id;
  6604. cb->pickAllowedArtsSet(saa.arts);
  6605. cb->sendAndApply(&saa);
  6606. }
  6607. void CGUniversity::initObj()
  6608. {
  6609. std::vector <int> toChoose;
  6610. for (int i=0; i<GameConstants::SKILL_QUANTITY; i++)
  6611. if (cb->isAllowed(2,i))
  6612. toChoose.push_back(i);
  6613. if (toChoose.size() < 4)
  6614. {
  6615. tlog0<<"Warning: less then 4 available skills was found by University initializer!\n";
  6616. return;
  6617. }
  6618. for (int i=0; i<4; i++)//get 4 skills
  6619. {
  6620. int skillPos = ran()%toChoose.size();
  6621. skills.push_back(toChoose[skillPos]);//move it to selected
  6622. toChoose.erase(toChoose.begin()+skillPos);//remove from list
  6623. }
  6624. }
  6625. std::vector<int> CGUniversity::availableItemsIds(EMarketMode::EMarketMode mode) const
  6626. {
  6627. switch (mode)
  6628. {
  6629. case EMarketMode::RESOURCE_SKILL:
  6630. return skills;
  6631. default:
  6632. return std::vector <int> ();
  6633. }
  6634. }
  6635. void CGUniversity::onHeroVisit(const CGHeroInstance * h) const
  6636. {
  6637. OpenWindow ow;
  6638. ow.id1 = id;
  6639. ow.id2 = h->id;
  6640. ow.window = OpenWindow::UNIVERSITY_WINDOW;
  6641. cb->sendAndApply(&ow);
  6642. }
  6643. GrowthInfo::Entry::Entry(const std::string &format, int _count)
  6644. : count(_count)
  6645. {
  6646. description = boost::str(boost::format(format) % count);
  6647. }
  6648. GrowthInfo::Entry::Entry(int subID, EBuilding::EBuilding building, int _count)
  6649. : count(_count)
  6650. {
  6651. description = boost::str(boost::format("%s %+d") % VLC->townh->towns[subID].buildings[building]->Name() % count);
  6652. }
  6653. CTownAndVisitingHero::CTownAndVisitingHero()
  6654. {
  6655. setNodeType(TOWN_AND_VISITOR);
  6656. }
  6657. int GrowthInfo::totalGrowth() const
  6658. {
  6659. int ret = 0;
  6660. BOOST_FOREACH(const Entry &entry, entries)
  6661. ret += entry.count;
  6662. return ret;
  6663. }