CObjectHandler.cpp 138 KB

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