2
0

CObjectHandler.cpp 184 KB

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