CObjectHandler.cpp 184 KB

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