CObjectHandler.cpp 191 KB

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