CObjectHandler.cpp 184 KB

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