CObjectHandler.cpp 173 KB

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