CObjectHandler.cpp 152 KB

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