CObjectHandler.cpp 181 KB

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