CObjectHandler.cpp 126 KB

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