CObjectHandler.cpp 190 KB

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