2
0

CObjectHandler.cpp 184 KB

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