CObjectHandler.cpp 146 KB

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