CObjectHandler.cpp 171 KB

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