CObjectHandler.cpp 190 KB

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