CObjectHandler.cpp 181 KB

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