CObjectHandler.cpp 120 KB

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