CObjectHandler.cpp 179 KB

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