CObjectHandler.cpp 180 KB

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