2
0

CObjectHandler.cpp 191 KB

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