CObjectHandler.cpp 129 KB

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