2
0

CObjectHandler.cpp 182 KB

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