CObjectHandler.cpp 186 KB

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