CObjectHandler.cpp 184 KB

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