CObjectHandler.cpp 141 KB

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