CObjectHandler.cpp 192 KB

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