CObjectHandler.cpp 169 KB

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