CObjectHandler.cpp 182 KB

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