CObjectHandler.cpp 157 KB

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