CObjectHandler.cpp 103 KB

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