CObjectHandler.cpp 192 KB

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