CObjectHandler.cpp 123 KB

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