CObjectHandler.cpp 179 KB

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