CObjectHandler.cpp 181 KB

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