CObjectHandler.cpp 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190
  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/lexical_cast.hpp>
  13. #include <boost/random/linear_congruential.hpp>
  14. #include "CTownHandler.h"
  15. #include "CArtHandler.h"
  16. #include "CSoundBase.h"
  17. #include "../lib/VCMI_Lib.h"
  18. #include "../lib/IGameCallback.h"
  19. #include "../lib/CGameState.h"
  20. #include "../lib/NetPacks.h"
  21. #include "../StartInfo.h"
  22. #include "../lib/map.h"
  23. /*
  24. * CObjectHandler.cpp, part of VCMI engine
  25. *
  26. * Authors: listed in file AUTHORS in main folder
  27. *
  28. * License: GNU General Public License v2.0 or later
  29. * Full text of license available in license.txt file, in main folder
  30. *
  31. */
  32. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  33. IGameCallback * IObjectInterface::cb = NULL;
  34. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  35. extern CLodHandler * bitmaph;
  36. extern boost::rand48 ran;
  37. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  38. {};
  39. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  40. {};
  41. void IObjectInterface::newTurn () const
  42. {};
  43. IObjectInterface::~IObjectInterface()
  44. {}
  45. IObjectInterface::IObjectInterface()
  46. {}
  47. void IObjectInterface::initObj()
  48. {}
  49. void IObjectInterface::setProperty( ui8 what, ui32 val )
  50. {}
  51. void CPlayersVisited::setPropertyDer( ui8 what, ui32 val )
  52. {
  53. if(what == 10)
  54. players.insert(val);
  55. }
  56. bool CPlayersVisited::hasVisited( ui8 player ) const
  57. {
  58. return vstd::contains(players,player);
  59. }
  60. void CObjectHandler::loadObjects()
  61. {
  62. tlog5 << "\t\tReading cregens \n";
  63. cregens.resize(110); //TODO: hardcoded value - change
  64. for(size_t i=0; i < cregens.size(); ++i)
  65. {
  66. cregens[i]=-1;
  67. }
  68. std::ifstream ifs("config/cregens.txt");
  69. while(!ifs.eof())
  70. {
  71. int dw, cr;
  72. ifs >> dw >> cr;
  73. cregens[dw]=cr;
  74. }
  75. ifs.close();
  76. ifs.clear();
  77. tlog5 << "\t\tDone loading objects!\n";
  78. }
  79. int CGObjectInstance::getOwner() const
  80. {
  81. //if (state)
  82. // return state->owner;
  83. //else
  84. return tempOwner; //won't have owner
  85. }
  86. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  87. {
  88. pos = int3(-1,-1,-1);
  89. //std::cout << "Tworze obiekt "<<this<<std::endl;
  90. //state = new CLuaObjectScript();
  91. ID = subID = id = -1;
  92. defInfo = NULL;
  93. info = NULL;
  94. tempOwner = 254;
  95. blockVisit = false;
  96. }
  97. CGObjectInstance::~CGObjectInstance()
  98. {
  99. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  100. //if (state)
  101. // delete state;
  102. //state=NULL;
  103. }
  104. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  105. //{
  106. // pos = right.pos;
  107. // ID = right.ID;
  108. // subID = right.subID;
  109. // id = right.id;
  110. // defInfo = right.defInfo;
  111. // info = right.info;
  112. // blockVisit = right.blockVisit;
  113. // //state = new CLuaObjectScript(right.state->);
  114. // //*state = *right.state;
  115. // //state = right.state;
  116. // tempOwner = right.tempOwner;
  117. //}
  118. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  119. //{
  120. // pos = right.pos;
  121. // ID = right.ID;
  122. // subID = right.subID;
  123. // id = right.id;
  124. // defInfo = right.defInfo;
  125. // info = right.info;
  126. // blockVisit = right.blockVisit;
  127. // //state = new CLuaObjectScript();
  128. // //*state = *right.state;
  129. // tempOwner = right.tempOwner;
  130. // return *this;
  131. //}
  132. const std::string & CGObjectInstance::getHoverText() const
  133. {
  134. return hoverName;
  135. }
  136. void CGObjectInstance::setOwner(int ow)
  137. {
  138. //if (state)
  139. // state->owner = ow;
  140. //else
  141. tempOwner = ow;
  142. }
  143. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  144. {
  145. return defInfo->width;
  146. }
  147. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  148. {
  149. return defInfo->height;
  150. }
  151. 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)
  152. {
  153. if(defInfo==NULL)
  154. {
  155. tlog2 << "Warning: VisitableAt for obj "<<id<<": NULL defInfo!\n";
  156. return false;
  157. }
  158. if((defInfo->visitMap[y] >> (7-x) ) & 1)
  159. {
  160. return true;
  161. }
  162. return false;
  163. }
  164. bool CGObjectInstance::blockingAt(int x, int y) const
  165. {
  166. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  167. return false;
  168. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  169. return true;
  170. return false;
  171. }
  172. bool CGObjectInstance::coveringAt(int x, int y) const
  173. {
  174. if((defInfo->coverageMap[y] >> (7-(x) )) & 1)
  175. return true;
  176. return false;
  177. }
  178. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  179. {
  180. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  181. return true;
  182. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  183. return false;
  184. if(this->pos.y<cmp.pos.y)
  185. return true;
  186. if(this->pos.y>cmp.pos.y)
  187. return false;
  188. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  189. return true;
  190. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  191. return false;
  192. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  193. return true;
  194. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  195. return false;
  196. if(this->pos.x<cmp.pos.x)
  197. return true;
  198. return false;
  199. }
  200. void CGObjectInstance::initObj()
  201. {
  202. }
  203. void CGObjectInstance::setProperty( ui8 what, ui32 val )
  204. {
  205. switch(what)
  206. {
  207. case 1:
  208. tempOwner = val;
  209. break;
  210. case 2:
  211. blockVisit = val;
  212. break;
  213. case 6:
  214. ID = val;
  215. break;
  216. }
  217. setPropertyDer(what, val);
  218. }
  219. void CGObjectInstance::setPropertyDer( ui8 what, ui32 val )
  220. {}
  221. int3 CGObjectInstance::getSightCenter() const
  222. {
  223. //return vistiable tile if possible
  224. for(int i=0; i < 8; i++)
  225. for(int j=0; j < 6; j++)
  226. if(visitableAt(i,j))
  227. return(pos + int3(i-7, j-5, 0));
  228. return pos;
  229. }
  230. int CGObjectInstance::getSightRadious() const
  231. {
  232. return 3;
  233. }
  234. static int lowestSpeed(const CGHeroInstance * chi)
  235. {
  236. if(!chi->army.slots.size())
  237. {
  238. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  239. return 20;
  240. }
  241. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  242. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  243. for (;i!=chi->army.slots.end();i++)
  244. {
  245. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  246. }
  247. return ret;
  248. }
  249. unsigned int CGHeroInstance::getTileCost(const TerrainTile &dest, const TerrainTile &from) const
  250. {
  251. //TODO: check if all creatures are on its native terrain and change cost appropriately
  252. //base move cost
  253. unsigned ret = 100;
  254. //if there is road both on dest and src tiles - use road movement cost
  255. if(dest.malle && from.malle)
  256. {
  257. int road = std::min(dest.malle,from.malle); //used road ID
  258. switch(road)
  259. {
  260. case TerrainTile::dirtRoad:
  261. ret = 75;
  262. break;
  263. case TerrainTile::grazvelRoad:
  264. ret = 65;
  265. break;
  266. case TerrainTile::cobblestoneRoad:
  267. ret = 50;
  268. break;
  269. default:
  270. tlog1 << "Unknown road type: " << road << "... Something wrong!\n";
  271. break;
  272. }
  273. }
  274. else
  275. {
  276. ret = std::max(type->heroClass->terrCosts[dest.tertype],type->heroClass->terrCosts[from.tertype]); //take cost of worse of two tiles
  277. ret = std::max(ret - 25*unsigned(getSecSkillLevel(0)), 100u); //reduce 25% of terrain penalty for each pathfinding level
  278. }
  279. return ret;
  280. }
  281. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  282. {
  283. unsigned int sl = 100;
  284. for(size_t h=0; h < army.slots.size(); ++h)
  285. {
  286. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  287. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  288. }
  289. return sl;
  290. }
  291. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  292. {
  293. if (toh3m)
  294. {
  295. src.x+=1;
  296. return src;
  297. }
  298. else
  299. {
  300. src.x-=1;
  301. return src;
  302. }
  303. }
  304. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  305. {
  306. if (h3m)
  307. {
  308. return pos;
  309. }
  310. else
  311. {
  312. return convertPosition(pos,false);
  313. }
  314. }
  315. si32 CGHeroInstance::manaLimit() const
  316. {
  317. double modifier = 1.0;
  318. switch(getSecSkillLevel(24)) //intelligence level
  319. {
  320. case 1: modifier+=0.25; break;
  321. case 2: modifier+=0.5; break;
  322. case 3: modifier+=1.0; break;
  323. }
  324. return si32(10*getPrimSkillLevel(3)*modifier);
  325. }
  326. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  327. //{
  328. // if (h3m)
  329. // pos = Pos;
  330. // else
  331. // pos = convertPosition(Pos,true);
  332. //}
  333. bool CGHeroInstance::canWalkOnSea() const
  334. {
  335. //TODO: write it - it should check if hero is flying, or something similiar
  336. return false;
  337. }
  338. int CGHeroInstance::getPrimSkillLevel(int id) const
  339. {
  340. int ret = primSkills[id];
  341. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  342. if(i->type == HeroBonus::PRIMARY_SKILL && i->subtype==id)
  343. ret += i->val;
  344. amax(ret, id/2);//minimum value for attack and defense is 0 and for spell power and knowledge - 1
  345. return ret;
  346. }
  347. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  348. {
  349. for(size_t i=0; i < secSkills.size(); ++i)
  350. if(secSkills[i].first==ID)
  351. return secSkills[i].second;
  352. return 0;
  353. }
  354. int CGHeroInstance::maxMovePoints(bool onLand) const
  355. {
  356. int ret = std::min(2000, 1270+70*lowestSpeed(this)),
  357. bonus = valOfBonuses(HeroBonus::MOVEMENT) + (onLand ? valOfBonuses(HeroBonus::LAND_MOVEMENT) : valOfBonuses(HeroBonus::SEA_MOVEMENT));
  358. double modifier = 0;
  359. if(onLand)
  360. {
  361. //logistics:
  362. switch(getSecSkillLevel(2))
  363. {
  364. case 1:
  365. modifier = 0.1;
  366. break;
  367. case 2:
  368. modifier = 0.2;
  369. break;
  370. case 3:
  371. modifier = 0.3;
  372. break;
  373. }
  374. }
  375. else
  376. {
  377. //navigation:
  378. switch(getSecSkillLevel(2))
  379. {
  380. case 1:
  381. modifier = 0.5;
  382. break;
  383. case 2:
  384. modifier = 1.0;
  385. break;
  386. case 3:
  387. modifier = 1.5;
  388. break;
  389. }
  390. }
  391. return int(ret + ret*modifier) + bonus;
  392. }
  393. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  394. {
  395. if(pos<19)
  396. if(vstd::contains(artifWorn,pos))
  397. return artifWorn.find(pos)->second;
  398. else
  399. return -1;
  400. else
  401. if(pos-19 < artifacts.size())
  402. return artifacts[pos-19];
  403. else
  404. return -1;
  405. }
  406. const CArtifact * CGHeroInstance::getArt(int pos) const
  407. {
  408. int id = getArtAtPos(pos);
  409. if(id>=0)
  410. return &VLC->arth->artifacts[id];
  411. else
  412. return NULL;
  413. }
  414. int CGHeroInstance::getSpellSecLevel(int spell) const
  415. {
  416. int bestslvl = 0;
  417. if(VLC->spellh->spells[spell].air)
  418. if(getSecSkillLevel(15) >= bestslvl)
  419. {
  420. bestslvl = getSecSkillLevel(15);
  421. }
  422. if(VLC->spellh->spells[spell].fire)
  423. if(getSecSkillLevel(14) >= bestslvl)
  424. {
  425. bestslvl = getSecSkillLevel(14);
  426. }
  427. if(VLC->spellh->spells[spell].water)
  428. if(getSecSkillLevel(16) >= bestslvl)
  429. {
  430. bestslvl = getSecSkillLevel(16);
  431. }
  432. if(VLC->spellh->spells[spell].earth)
  433. if(getSecSkillLevel(17) >= bestslvl)
  434. {
  435. bestslvl = getSecSkillLevel(17);
  436. }
  437. return bestslvl;
  438. }
  439. CGHeroInstance::CGHeroInstance()
  440. {
  441. ID = HEROI_TYPE;
  442. tacticFormationEnabled = inTownGarrison = false;
  443. mana = movement = portrait = level = -1;
  444. isStanding = true;
  445. moveDir = 4;
  446. exp = 0xffffffff;
  447. visitedTown = NULL;
  448. type = NULL;
  449. secSkills.push_back(std::make_pair(-1, -1));
  450. }
  451. void CGHeroInstance::initHero(int SUBID)
  452. {
  453. subID = SUBID;
  454. initHero();
  455. }
  456. void CGHeroInstance::initHero()
  457. {
  458. if(ID == HEROI_TYPE)
  459. initHeroDefInfo();
  460. if(!type)
  461. type = VLC->heroh->heroes[subID];
  462. artifWorn[16] = 3;
  463. if(type->startingSpell >= 0) //hero starts with a spell
  464. {
  465. artifWorn[17] = 0; //give him spellbook
  466. spells.insert(type->startingSpell);
  467. }
  468. if(portrait < 0 || portrait == 255)
  469. portrait = subID;
  470. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  471. {
  472. primSkills.resize(4);
  473. primSkills[0] = type->heroClass->initialAttack;
  474. primSkills[1] = type->heroClass->initialDefence;
  475. primSkills[2] = type->heroClass->initialPower;
  476. primSkills[3] = type->heroClass->initialKnowledge;
  477. }
  478. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  479. secSkills = type->secSkillsInit;
  480. if(mana < 0)
  481. mana = manaLimit();
  482. if (!name.length())
  483. name = type->name;
  484. if (exp == 0xffffffff)
  485. {
  486. exp=40+ (ran()) % 50;
  487. level = 1;
  488. }
  489. else
  490. {
  491. level = VLC->heroh->level(exp);
  492. }
  493. if (!army.slots.size()) //standard army//initial army
  494. {
  495. int pom, pom2=0;
  496. int x = 0; //how many stacks will hero receives <1 - 3>
  497. pom = ran()%100;
  498. if(pom < 9)
  499. x = 1;
  500. else if(pom < 79)
  501. x = 2;
  502. else
  503. x = 3;
  504. for(int x=0;x<3;x++)
  505. {
  506. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  507. if(pom>=145 && pom<=149) //war machine
  508. {
  509. pom2++;
  510. switch (pom)
  511. {
  512. case 145: //catapult
  513. artifWorn[16] = 3;
  514. break;
  515. default:
  516. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  517. break;
  518. }
  519. continue;
  520. }
  521. army.slots[x-pom2].first = pom;
  522. pom = type->highStack[x] - type->lowStack[x];
  523. army.slots[x-pom2].second = ran()%(pom+1) + type->lowStack[x];
  524. army.formation = false;
  525. }
  526. }
  527. hoverName = VLC->generaltexth->allTexts[15];
  528. boost::algorithm::replace_first(hoverName,"%s",name);
  529. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  530. recreateArtBonuses();
  531. }
  532. void CGHeroInstance::initHeroDefInfo()
  533. {
  534. if(!defInfo || defInfo->id != HEROI_TYPE)
  535. {
  536. defInfo = new CGDefInfo();
  537. defInfo->id = HEROI_TYPE;
  538. defInfo->subid = subID;
  539. defInfo->printPriority = 0;
  540. defInfo->visitDir = 0xff;
  541. }
  542. for(int i=0;i<6;i++)
  543. {
  544. defInfo->blockMap[i]=255;
  545. defInfo->visitMap[i]=0;
  546. }
  547. defInfo->handler=NULL;
  548. defInfo->blockMap[5] = 253;
  549. defInfo->visitMap[5] = 2;
  550. }
  551. CGHeroInstance::~CGHeroInstance()
  552. {
  553. }
  554. bool CGHeroInstance::needsLastStack() const
  555. {
  556. return true;
  557. }
  558. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  559. {
  560. if(h == this) return; //exclude potential self-visiting
  561. if (ID == HEROI_TYPE) //hero
  562. {
  563. //TODO: check for allies
  564. if(tempOwner == h->tempOwner) //our hero
  565. {
  566. //exchange
  567. cb->heroExchange(id, h->id);
  568. }
  569. else
  570. {
  571. //battle
  572. cb->startBattleI(
  573. &h->army,
  574. &army,
  575. h->pos,
  576. h,
  577. this,
  578. 0);
  579. }
  580. }
  581. else if(ID == 62) //prison
  582. {
  583. InfoWindow iw;
  584. iw.player = h->tempOwner;
  585. iw.soundID = soundBase::ROGUE;
  586. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  587. {
  588. cb->changeObjPos(id,pos+int3(1,0,0),0);
  589. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  590. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  591. iw.text << std::pair<ui8,ui32>(11,102);
  592. }
  593. else //already 8 wandering heroes
  594. {
  595. iw.text << std::pair<ui8,ui32>(11,103);
  596. }
  597. cb->showInfoDialog(&iw);
  598. }
  599. }
  600. const std::string & CGHeroInstance::getBiography() const
  601. {
  602. if (biography.length())
  603. return biography;
  604. else
  605. return VLC->generaltexth->hTxts[subID].biography;
  606. }
  607. void CGHeroInstance::initObj()
  608. {
  609. blockVisit = true;
  610. }
  611. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  612. {
  613. int ret = 0;
  614. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  615. for(int i=0; i < mods.size(); i++)
  616. ret += mods[i].first;
  617. if(ret > 3)
  618. return 3;
  619. if(ret < -3)
  620. return -3;
  621. return ret;
  622. }
  623. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  624. {
  625. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  626. std::vector<std::pair<int,std::string> > ret;
  627. //various morale bonuses (from buildings, artifacts, etc)
  628. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  629. {
  630. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  631. {
  632. ret.push_back(std::make_pair(i->val, i->description));
  633. }
  634. }
  635. //leadership
  636. if(getSecSkillLevel(6))
  637. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  638. //town structures
  639. if(town && visitedTown)
  640. {
  641. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  642. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  643. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  644. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  645. }
  646. //number of alignments and presence of undead
  647. if(stack>=0)
  648. {
  649. bool archangelInArmy = false;
  650. std::set<si8> factions;
  651. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  652. {
  653. factions.insert(VLC->creh->creatures[i->second.first].faction);
  654. if(i->second.first == 13)
  655. archangelInArmy = true;
  656. }
  657. if(factions.size() == 1)
  658. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  659. else
  660. {
  661. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  662. {
  663. char buf[150];
  664. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  665. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  666. }
  667. else
  668. {
  669. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  670. }
  671. }
  672. if(vstd::contains(factions,4))
  673. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  674. if(archangelInArmy)
  675. {
  676. char buf[100];
  677. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  678. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  679. }
  680. }
  681. return ret;
  682. }
  683. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  684. {
  685. int ret = 0;
  686. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  687. for(int i=0; i < mods.size(); i++)
  688. ret += mods[i].first;
  689. if(ret > 3)
  690. return 3;
  691. if(ret < -3)
  692. return -3;
  693. return ret;
  694. }
  695. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  696. {
  697. std::vector<std::pair<int,std::string> > ret;
  698. //various morale bonuses (from buildings, artifacts, etc)
  699. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  700. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  701. ret.push_back(std::make_pair(i->val, i->description));
  702. //luck skill
  703. if(getSecSkillLevel(9))
  704. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  705. return ret;
  706. }
  707. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  708. {
  709. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  710. if(i->source == from && i->id == id)
  711. return &*i;
  712. return NULL;
  713. }
  714. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  715. {
  716. if(what == 3)
  717. army.slots[0].second = val;
  718. }
  719. double CGHeroInstance::getHeroStrength() const
  720. {
  721. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  722. }
  723. int CGHeroInstance::getTotalStrength() const
  724. {
  725. double ret = getHeroStrength() * getArmyStrength();
  726. return (int) ret;
  727. }
  728. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  729. {
  730. //TODO: skill level may be different on special terrain
  731. ui8 skill = 0; //skill level
  732. if(spell->fire)
  733. skill = std::max(skill,getSecSkillLevel(14));
  734. if(spell->air)
  735. skill = std::max(skill,getSecSkillLevel(15));
  736. if(spell->water)
  737. skill = std::max(skill,getSecSkillLevel(16));
  738. if(spell->earth)
  739. skill = std::max(skill,getSecSkillLevel(17));
  740. return skill;
  741. }
  742. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  743. {
  744. if(!getArt(17)) //if hero has no spellbook
  745. return false;
  746. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  747. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  748. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  749. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  750. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  751. )
  752. return true;
  753. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  754. {
  755. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  756. {
  757. return true;
  758. }
  759. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  760. {
  761. return true;
  762. }
  763. }
  764. return false;
  765. }
  766. int3 CGHeroInstance::getSightCenter() const
  767. {
  768. return getPosition(false);
  769. }
  770. int CGHeroInstance::getSightRadious() const
  771. {
  772. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  773. }
  774. si32 CGHeroInstance::manaRegain() const
  775. {
  776. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  777. }
  778. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  779. {
  780. int ret = 0;
  781. if(subtype == -1)
  782. {
  783. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  784. if(i->type == type)
  785. ret += i->val;
  786. }
  787. else
  788. {
  789. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  790. if(i->type == type && i->subtype == subtype)
  791. ret += i->val;
  792. }
  793. return ret;
  794. }
  795. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  796. {
  797. if(subtype == -1) //any subtype
  798. {
  799. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  800. if(i->type == type)
  801. return true;
  802. }
  803. else //given subtype
  804. {
  805. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  806. if(i->type == type && i->subtype == subtype)
  807. return true;
  808. }
  809. return false;
  810. }
  811. si32 CGHeroInstance::getArtPos(int aid) const
  812. {
  813. for(std::map<ui16,ui32>::const_iterator i = artifWorn.begin(); i != artifWorn.end(); i++)
  814. if(i->second == aid)
  815. return i->first;
  816. return -1;
  817. }
  818. void CGHeroInstance::recreateArtBonuses()
  819. {
  820. //clear all bonuses from artifacts (if present) and give them again
  821. std::remove_if(bonuses.begin(), bonuses.end(), boost::bind(HeroBonus::IsFrom,_1,HeroBonus::ARTIFACT,0xffffff));
  822. for (std::map<ui16,ui32>::iterator ari = artifWorn.begin(); ari != artifWorn.end(); ari++)
  823. {
  824. CArtifact &art = VLC->arth->artifacts[ari->second];
  825. for(std::list<HeroBonus>::iterator i = art.bonuses.begin(); i != art.bonuses.end(); i++)
  826. bonuses.push_back(*i);
  827. }
  828. }
  829. void CGDwelling::initObj()
  830. {
  831. switch(ID)
  832. {
  833. case 17:
  834. {
  835. int crid = VLC->objh->cregens[subID];
  836. CCreature *crs = &VLC->creh->creatures[crid];
  837. creatures.resize(1);
  838. creatures[0].second.push_back(crid);
  839. hoverName = VLC->generaltexth->creGens[subID];
  840. if(crs->level > 4)
  841. {
  842. army.slots[0].first = crs->idNumber;
  843. army.slots[0].second = crs->getRandomAmount(ran);
  844. }
  845. }
  846. break;
  847. case 20:
  848. creatures.resize(4);
  849. if(subID == 0) // Elemental Conflux
  850. {
  851. creatures[0].second.push_back(32); //Stone Golem
  852. creatures[1].second.push_back(33); //Iron Golem
  853. creatures[2].second.push_back(116); //Gold Golem
  854. creatures[3].second.push_back(117); //Diamond Golem
  855. }
  856. else if(subID == 1) //Golem Factory
  857. {
  858. creatures[0].second.push_back(112); //Air Elemental
  859. creatures[1].second.push_back(113); //Earth Elemental
  860. creatures[2].second.push_back(114); //Fire Elemental
  861. creatures[3].second.push_back(115); //Water Elemental
  862. }
  863. else
  864. {
  865. assert(0);
  866. }
  867. hoverName = VLC->generaltexth->creGens4[subID];
  868. break;
  869. default:
  870. assert(0);
  871. break;
  872. }
  873. }
  874. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  875. {
  876. if(h->tempOwner != tempOwner && army) //object is guarded
  877. {
  878. BlockingDialog bd;
  879. bd.player = h->tempOwner;
  880. bd.flags = BlockingDialog::ALLOW_CANCEL;
  881. 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?
  882. bd.text.addReplacement(MetaString::CREGENS, subID);
  883. bd.text.addReplacement(MetaString::ARRAY_TXT, 176 + CCreature::getQuantityID(army.slots.begin()->second.second)*3);
  884. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[0].second[0]);
  885. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::wantsFight, this, h, _1));
  886. return;
  887. }
  888. if(h->tempOwner != tempOwner)
  889. cb->setOwner(id, h->tempOwner);
  890. BlockingDialog bd;
  891. bd.player = h->tempOwner;
  892. bd.flags = BlockingDialog::ALLOW_CANCEL;
  893. bd.text.addTxt(MetaString::ADVOB_TXT, 35); //{%s} Would you like to recruit %s?
  894. bd.text.addReplacement(MetaString::CREGENS, subID);
  895. bd.text.addReplacement(MetaString::CRE_PL_NAMES, creatures[0].second[0]);
  896. cb->showBlockingDialog(&bd, boost::bind(&CGDwelling::heroAcceptsCreatures, this, h, _1));
  897. }
  898. void CGDwelling::newTurn() const
  899. {
  900. if(cb->getDate(1) != 1) //not first day of week
  901. return;
  902. bool change = false;
  903. SetAvailableCreatures sac;
  904. sac.creatures = creatures;
  905. sac.tid = id;
  906. for (size_t i = 0; i < creatures.size(); i++)
  907. {
  908. if(creatures[i].second.size())
  909. {
  910. sac.creatures[i].first += VLC->creh->creatures[creatures[i].second[0]].growth;
  911. change = true;
  912. }
  913. }
  914. if(change)
  915. cb->sendAndApply(&sac);
  916. }
  917. void CGDwelling::heroAcceptsCreatures( const CGHeroInstance *h, ui32 answer ) const
  918. {
  919. if(!answer)
  920. return;
  921. int crid = creatures[0].second[0];
  922. CCreature *crs = &VLC->creh->creatures[crid];
  923. if(crs->level == 1) //first level - creatures are for free
  924. {
  925. if(creatures[0].first) //there are available creatures
  926. {
  927. int slot = h->army.getSlotFor(crid);
  928. if(slot < 0) //no available slot
  929. {
  930. InfoWindow iw;
  931. iw.player = h->tempOwner;
  932. iw.text.addTxt(MetaString::GENERAL_TXT, 425);//The %s would join your hero, but there aren't enough provisions to support them.
  933. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  934. cb->showInfoDialog(&iw);
  935. }
  936. else //give creatures
  937. {
  938. SetAvailableCreatures sac;
  939. sac.tid = id;
  940. sac.creatures = creatures;
  941. sac.creatures[0].first = 0;
  942. SetGarrisons sg;
  943. sg.garrs[h->id] = h->army;
  944. sg.garrs[h->id].slots[slot].first = crid;
  945. sg.garrs[h->id].slots[slot].second += creatures[0].first;
  946. InfoWindow iw;
  947. iw.player = h->tempOwner;
  948. iw.text.addTxt(MetaString::GENERAL_TXT, 423); //%d %s join your army.
  949. iw.text.addReplacement(creatures[0].first);
  950. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  951. cb->showInfoDialog(&iw);
  952. cb->sendAndApply(&sac);
  953. cb->sendAndApply(&sg);
  954. }
  955. }
  956. else //there no creatures
  957. {
  958. InfoWindow iw;
  959. iw.text.addTxt(MetaString::GENERAL_TXT, 422); //There are no %s here to recruit.
  960. iw.text.addReplacement(MetaString::CRE_PL_NAMES, crid);
  961. iw.player = h->tempOwner;
  962. cb->sendAndApply(&iw);
  963. }
  964. }
  965. else if(ID == 17)
  966. {
  967. OpenWindow ow;
  968. ow.id1 = id;
  969. ow.id2 = id;
  970. ow.window = OpenWindow::RECRUITMENT_FIRST;
  971. cb->sendAndApply(&ow);
  972. }
  973. }
  974. void CGDwelling::wantsFight( const CGHeroInstance *h, ui32 answer ) const
  975. {
  976. if(answer)
  977. cb->startBattleI(h->id,army,pos,boost::bind(&CGDwelling::fightOver, this, h, _1));
  978. }
  979. void CGDwelling::fightOver(const CGHeroInstance *h, BattleResult *result) const
  980. {
  981. if (result->winner == 0)
  982. {
  983. onHeroVisit(h);
  984. }
  985. }
  986. int CGTownInstance::getSightRadious() const //returns sight distance
  987. {
  988. return 5;
  989. }
  990. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  991. {
  992. if((builtBuildings.find(9))!=builtBuildings.end())
  993. return 3;
  994. if((builtBuildings.find(8))!=builtBuildings.end())
  995. return 2;
  996. if((builtBuildings.find(7))!=builtBuildings.end())
  997. return 1;
  998. return 0;
  999. }
  1000. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  1001. {
  1002. if ((builtBuildings.find(13))!=builtBuildings.end())
  1003. return 3;
  1004. if ((builtBuildings.find(12))!=builtBuildings.end())
  1005. return 2;
  1006. if ((builtBuildings.find(11))!=builtBuildings.end())
  1007. return 1;
  1008. if ((builtBuildings.find(10))!=builtBuildings.end())
  1009. return 0;
  1010. return -1;
  1011. }
  1012. int CGTownInstance::mageGuildLevel() const
  1013. {
  1014. if ((builtBuildings.find(4))!=builtBuildings.end())
  1015. return 5;
  1016. if ((builtBuildings.find(3))!=builtBuildings.end())
  1017. return 4;
  1018. if ((builtBuildings.find(2))!=builtBuildings.end())
  1019. return 3;
  1020. if ((builtBuildings.find(1))!=builtBuildings.end())
  1021. return 2;
  1022. if ((builtBuildings.find(0))!=builtBuildings.end())
  1023. return 1;
  1024. return 0;
  1025. }
  1026. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  1027. {
  1028. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  1029. }
  1030. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  1031. {
  1032. return town->hordeLvl[HID];
  1033. }
  1034. int CGTownInstance::creatureGrowth(const int & level) const
  1035. {
  1036. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  1037. switch(fortLevel())
  1038. {
  1039. case 3:
  1040. ret*=2;break;
  1041. case 2:
  1042. ret*=(1.5); break;
  1043. }
  1044. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  1045. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  1046. if(getHordeLevel(0)==level)
  1047. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  1048. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1049. if(getHordeLevel(1)==level)
  1050. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  1051. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  1052. return ret;
  1053. }
  1054. int CGTownInstance::dailyIncome() const
  1055. {
  1056. int ret = 0;
  1057. if ((builtBuildings.find(26))!=builtBuildings.end())
  1058. ret+=5000;
  1059. if ((builtBuildings.find(13))!=builtBuildings.end())
  1060. ret+=4000;
  1061. else if ((builtBuildings.find(12))!=builtBuildings.end())
  1062. ret+=2000;
  1063. else if ((builtBuildings.find(11))!=builtBuildings.end())
  1064. ret+=1000;
  1065. else if ((builtBuildings.find(10))!=builtBuildings.end())
  1066. ret+=500;
  1067. return ret;
  1068. }
  1069. bool CGTownInstance::hasFort() const
  1070. {
  1071. return (builtBuildings.find(7))!=builtBuildings.end();
  1072. }
  1073. bool CGTownInstance::hasCapitol() const
  1074. {
  1075. return (builtBuildings.find(13))!=builtBuildings.end();
  1076. }
  1077. CGTownInstance::CGTownInstance()
  1078. {
  1079. builded=-1;
  1080. destroyed=-1;
  1081. garrisonHero=NULL;
  1082. town=NULL;
  1083. visitingHero = NULL;
  1084. }
  1085. CGTownInstance::~CGTownInstance()
  1086. {}
  1087. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  1088. {
  1089. if(checkGuild && mageGuildLevel() < level)
  1090. return 0;
  1091. int ret = 6 - level; //how many spells are available at this level
  1092. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  1093. ret++;
  1094. return ret;
  1095. }
  1096. bool CGTownInstance::needsLastStack() const
  1097. {
  1098. if(garrisonHero)
  1099. return true;
  1100. else return false;
  1101. }
  1102. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  1103. {
  1104. if(getOwner() != h->getOwner())
  1105. {
  1106. return;
  1107. }
  1108. cb->heroVisitCastle(id,h->id);
  1109. }
  1110. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  1111. {
  1112. cb->stopHeroVisitCastle(id,h->id);
  1113. }
  1114. void CGTownInstance::initObj()
  1115. {
  1116. hoverName = name + ", " + town->Name();
  1117. creatures.resize(CREATURES_PER_TOWN);
  1118. for (int i = 0; i < CREATURES_PER_TOWN; i++)
  1119. {
  1120. if(creatureDwelling(i,false))
  1121. creatures[i].second.push_back(town->basicCreatures[i]);
  1122. if(creatureDwelling(i,true))
  1123. creatures[i].second.push_back(town->upgradedCreatures[i]);
  1124. }
  1125. }
  1126. int3 CGTownInstance::getSightCenter() const
  1127. {
  1128. return pos - int3(2,0,0);
  1129. }
  1130. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  1131. {
  1132. if(visitors.find(h->id)==visitors.end())
  1133. {
  1134. onNAHeroVisit(h->id, false);
  1135. switch(ID)
  1136. {
  1137. case 102: //tree
  1138. case 4: //arena
  1139. case 41://library
  1140. case 47: //School of Magic
  1141. case 107://School of War
  1142. break;
  1143. default:
  1144. cb->setObjProperty(id,4,h->id); //add to the visitors
  1145. break;
  1146. }
  1147. }
  1148. else
  1149. {
  1150. onNAHeroVisit(h->id, true);
  1151. }
  1152. }
  1153. void CGVisitableOPH::initObj()
  1154. {
  1155. if(ID==102)
  1156. ttype = ran()%3;
  1157. else
  1158. ttype = -1;
  1159. }
  1160. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  1161. {
  1162. if(result) //player agreed to give res for exp
  1163. {
  1164. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  1165. cb->changePrimSkill(heroID,4,expVal); //give exp
  1166. cb->setObjProperty(id,4,heroID); //add to the visitors
  1167. }
  1168. }
  1169. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  1170. {
  1171. int id=0, subid=0, ot=0, val=1, sound = 0;
  1172. switch(ID)
  1173. {
  1174. case 4: //arena
  1175. sound = soundBase::NOMAD;
  1176. ot = 0;
  1177. break;
  1178. case 51: //mercenary camp
  1179. sound = soundBase::NOMAD;
  1180. subid=0;
  1181. ot=80;
  1182. break;
  1183. case 23: //marletto tower
  1184. sound = soundBase::NOMAD;
  1185. subid=1;
  1186. ot=39;
  1187. break;
  1188. case 61:
  1189. sound = soundBase::gazebo;
  1190. subid=2;
  1191. ot=100;
  1192. break;
  1193. case 32:
  1194. sound = soundBase::GETPROTECTION;
  1195. subid=3;
  1196. ot=59;
  1197. break;
  1198. case 100:
  1199. sound = soundBase::gazebo;
  1200. id=5;
  1201. ot=143;
  1202. val=1000;
  1203. break;
  1204. case 102:
  1205. sound = soundBase::gazebo;
  1206. id = 5;
  1207. subid = 1;
  1208. ot = 146;
  1209. val = 1;
  1210. break;
  1211. case 41:
  1212. sound = soundBase::gazebo;
  1213. ot = 66;
  1214. break;
  1215. case 47: //School of Magic
  1216. sound = soundBase::faerie;
  1217. ot = 71;
  1218. break;
  1219. case 107://School of War
  1220. sound = soundBase::MILITARY;
  1221. ot = 158;
  1222. break;
  1223. }
  1224. if (!alreadyVisited)
  1225. {
  1226. switch (ID)
  1227. {
  1228. case 4: //arena
  1229. {
  1230. BlockingDialog sd(false,true);
  1231. sd.soundID = sound;
  1232. sd.text << std::pair<ui8,ui32>(11,ot);
  1233. sd.components.push_back(Component(0,0,2,0));
  1234. sd.components.push_back(Component(0,1,2,0));
  1235. sd.player = cb->getOwner(heroID);
  1236. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1237. return;
  1238. }
  1239. case 51:
  1240. case 23:
  1241. case 61:
  1242. case 32:
  1243. {
  1244. cb->changePrimSkill(heroID,subid,val);
  1245. InfoWindow iw;
  1246. iw.soundID = sound;
  1247. iw.components.push_back(Component(0,subid,val,0));
  1248. iw.text << std::pair<ui8,ui32>(11,ot);
  1249. iw.player = cb->getOwner(heroID);
  1250. cb->showInfoDialog(&iw);
  1251. break;
  1252. }
  1253. case 100: //give exp
  1254. {
  1255. InfoWindow iw;
  1256. iw.soundID = sound;
  1257. iw.components.push_back(Component(id,subid,val,0));
  1258. iw.player = cb->getOwner(heroID);
  1259. iw.text << std::pair<ui8,ui32>(11,ot);
  1260. iw.soundID = soundBase::gazebo;
  1261. cb->showInfoDialog(&iw);
  1262. cb->changePrimSkill(heroID,4,val);
  1263. break;
  1264. }
  1265. case 102://tree
  1266. {
  1267. const CGHeroInstance *h = cb->getHero(heroID);
  1268. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1269. if(!ttype)
  1270. {
  1271. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1272. InfoWindow iw;
  1273. iw.soundID = sound;
  1274. iw.components.push_back(Component(id,subid,1,0));
  1275. iw.player = cb->getOwner(heroID);
  1276. iw.text << std::pair<ui8,ui32>(11,148);
  1277. cb->showInfoDialog(&iw);
  1278. cb->changePrimSkill(heroID,4,val);
  1279. break;
  1280. }
  1281. else
  1282. {
  1283. int res, resval;
  1284. if(ttype==1)
  1285. {
  1286. res = 6;
  1287. resval = 2000;
  1288. ot = 149;
  1289. }
  1290. else
  1291. {
  1292. res = 5;
  1293. resval = 10;
  1294. ot = 151;
  1295. }
  1296. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1297. {
  1298. ot++;
  1299. InfoWindow iw;
  1300. iw.soundID = sound;
  1301. iw.player = h->tempOwner;
  1302. iw.text << std::pair<ui8,ui32>(11,ot);
  1303. cb->showInfoDialog(&iw);
  1304. return;
  1305. }
  1306. BlockingDialog sd(true,false);
  1307. sd.soundID = sound;
  1308. sd.player = cb->getOwner(heroID);
  1309. sd.text << std::pair<ui8,ui32>(11,ot);
  1310. sd.components.push_back(Component(id,subid,val,0));
  1311. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1312. }
  1313. break;
  1314. }
  1315. case 41://library of enlightenment
  1316. {
  1317. const CGHeroInstance *h = cb->getHero(heroID);
  1318. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1319. {
  1320. InfoWindow iw;
  1321. iw.soundID = sound;
  1322. iw.player = cb->getOwner(heroID);
  1323. iw.text << std::pair<ui8,ui32>(11,68);
  1324. cb->showInfoDialog(&iw);
  1325. }
  1326. else
  1327. {
  1328. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1329. cb->changePrimSkill(heroID,0,2);
  1330. cb->changePrimSkill(heroID,1,2);
  1331. cb->changePrimSkill(heroID,2,2);
  1332. cb->changePrimSkill(heroID,3,2);
  1333. InfoWindow iw;
  1334. iw.soundID = sound;
  1335. iw.player = cb->getOwner(heroID);
  1336. iw.text << std::pair<ui8,ui32>(11,66);
  1337. cb->showInfoDialog(&iw);
  1338. }
  1339. break;
  1340. }
  1341. case 47: //School of Magic
  1342. case 107://School of War
  1343. {
  1344. int skill = (ID==47 ? 2 : 0);
  1345. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1346. {
  1347. InfoWindow iw;
  1348. iw.soundID = sound;
  1349. iw.player = cb->getOwner(heroID);
  1350. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1351. cb->showInfoDialog(&iw);
  1352. }
  1353. else
  1354. {
  1355. BlockingDialog sd(true,true);
  1356. sd.soundID = sound;
  1357. sd.player = cb->getOwner(heroID);
  1358. sd.text << std::pair<ui8,ui32>(11,ot);
  1359. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1360. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1361. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1362. }
  1363. }
  1364. break;
  1365. }
  1366. }
  1367. else
  1368. {
  1369. ot++;
  1370. InfoWindow iw;
  1371. iw.soundID = sound;
  1372. iw.player = cb->getOwner(heroID);
  1373. iw.text << std::pair<ui8,ui32>(11,ot);
  1374. cb->showInfoDialog(&iw);
  1375. }
  1376. }
  1377. const std::string & CGVisitableOPH::getHoverText() const
  1378. {
  1379. int pom = -1;
  1380. switch(ID)
  1381. {
  1382. case 4:
  1383. pom = -1;
  1384. break;
  1385. case 51:
  1386. pom = 8;
  1387. break;
  1388. case 23:
  1389. pom = 7;
  1390. break;
  1391. case 61:
  1392. pom = 11;
  1393. break;
  1394. case 32:
  1395. pom = 4;
  1396. break;
  1397. case 100:
  1398. pom = 5;
  1399. break;
  1400. case 102:
  1401. pom = 18;
  1402. break;
  1403. case 41:
  1404. break;
  1405. case 47: //School of Magic
  1406. pom = 9;
  1407. break;
  1408. case 107://School of War
  1409. pom = 10;
  1410. break;
  1411. default:
  1412. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1413. }
  1414. hoverName = VLC->generaltexth->names[ID];
  1415. if(pom >= 0)
  1416. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1417. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1418. if(h)
  1419. {
  1420. hoverName += ' ';
  1421. hoverName += (vstd::contains(visitors,h->id))
  1422. ? (VLC->generaltexth->allTexts[352]) //visited
  1423. : ( VLC->generaltexth->allTexts[353]); //not visited
  1424. }
  1425. return hoverName;
  1426. }
  1427. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1428. {
  1429. cb->setObjProperty(id,4,heroID); //add to the visitors
  1430. cb->changePrimSkill(heroID,primSkill-1,2);
  1431. }
  1432. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1433. {
  1434. if(what == 4)
  1435. visitors.insert(val);
  1436. }
  1437. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1438. {
  1439. if(!which) //player refused to pay
  1440. return;
  1441. int base = (ID == 47 ? 2 : 0);
  1442. cb->setObjProperty(id,4,heroID); //add to the visitors
  1443. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1444. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1445. }
  1446. bool CArmedInstance::needsLastStack() const
  1447. {
  1448. return false;
  1449. }
  1450. int CArmedInstance::getArmyStrength() const
  1451. {
  1452. int ret = 0;
  1453. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1454. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1455. return ret;
  1456. }
  1457. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1458. {
  1459. int action = takenAction(h);
  1460. switch( action ) //decide what we do...
  1461. {
  1462. case -2: //fight
  1463. fight(h);
  1464. break;
  1465. case -1: //flee
  1466. {
  1467. flee(h);
  1468. break;
  1469. }
  1470. case 0: //join for free
  1471. {
  1472. BlockingDialog ynd(true,false);
  1473. ynd.player = h->tempOwner;
  1474. ynd.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT, 86);
  1475. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1476. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1477. break;
  1478. }
  1479. default: //join for gold
  1480. {
  1481. assert(action > 0);
  1482. //ask if player agrees to pay gold
  1483. BlockingDialog ynd(true,false);
  1484. ynd.player = h->tempOwner;
  1485. std::string tmp = VLC->generaltexth->advobtxt[90];
  1486. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1487. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1488. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1489. ynd.text << tmp;
  1490. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1491. break;
  1492. }
  1493. }
  1494. }
  1495. void CGCreature::endBattle( BattleResult *result ) const
  1496. {
  1497. if(result->winner==0)
  1498. {
  1499. cb->removeObject(id);
  1500. }
  1501. else
  1502. {
  1503. int killedAmount=0;
  1504. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1505. if(i->first == subID)
  1506. killedAmount += i->second;
  1507. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1508. MetaString ms;
  1509. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1510. pom = 174 + 3*pom + 1;
  1511. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1512. cb->setHoverName(id,&ms);
  1513. }
  1514. }
  1515. void CGCreature::initObj()
  1516. {
  1517. blockVisit = true;
  1518. switch(character)
  1519. {
  1520. case 0:
  1521. character = 0;
  1522. break;
  1523. case 1:
  1524. character = 1 + ran()%7;
  1525. break;
  1526. case 2:
  1527. character = 1 + ran()%10;
  1528. break;
  1529. case 3:
  1530. character = 4 + ran()%7;
  1531. break;
  1532. case 4:
  1533. character = 10;
  1534. break;
  1535. }
  1536. army.slots[0].first = subID;
  1537. si32 &amount = army.slots[0].second;
  1538. CCreature &c = VLC->creh->creatures[subID];
  1539. if(!amount)
  1540. if(c.ammMax == c.ammMin)
  1541. amount = c.ammMax;
  1542. else
  1543. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1544. MetaString ms;
  1545. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1546. pom = 174 + 3*pom + 1;
  1547. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1548. ms.toString(hoverName);
  1549. }
  1550. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1551. {
  1552. double hlp = h->getTotalStrength() / getArmyStrength();
  1553. if(!character) //compliant creatures will always join
  1554. return 0;
  1555. else if(allowJoin)//test for joining
  1556. {
  1557. int factor;
  1558. if(hlp >= 7)
  1559. factor = 11;
  1560. else if(hlp >= 1)
  1561. factor = 2*(hlp-1);
  1562. else if(hlp >= 0.5)
  1563. factor = -1;
  1564. else if(hlp >= 0.333)
  1565. factor = -2;
  1566. else
  1567. factor = -3;
  1568. int sympathy = 0;
  1569. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1570. myKindCres.insert(subID); //we
  1571. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1572. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1573. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1574. myKindCres.insert(i->idNumber);
  1575. int count = 0, //how many creatures of our kind has hero
  1576. totalCount = 0;
  1577. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1578. {
  1579. if(vstd::contains(myKindCres,i->second.first))
  1580. count += i->second.second;
  1581. totalCount += i->second.second;
  1582. }
  1583. if(count*2 > totalCount)
  1584. sympathy++;
  1585. if(count)
  1586. sympathy++;
  1587. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1588. if(charisma >= character) //creatures might join...
  1589. {
  1590. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1591. return 0; //join for free
  1592. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1593. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1594. }
  1595. }
  1596. //we are still here - creatures not joined heroes, test for fleeing
  1597. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1598. //TODO: should be deterministic (will be needed for Vision spell)
  1599. int hlp2 = (int) (hlp - 2)*1000;
  1600. if(!neverFlees
  1601. && hlp2 >= 0
  1602. && rand()%2000 < hlp2
  1603. )
  1604. return -1; //flee
  1605. else
  1606. return -2; //fight
  1607. }
  1608. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1609. {
  1610. if(pursue)
  1611. {
  1612. fight(h);
  1613. }
  1614. else
  1615. {
  1616. cb->removeObject(id);
  1617. }
  1618. }
  1619. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1620. {
  1621. if(!accept)
  1622. {
  1623. if(takenAction(h,false) == -1) //they flee
  1624. {
  1625. flee(h);
  1626. }
  1627. else //they fight
  1628. {
  1629. InfoWindow iw;
  1630. iw.player = h->tempOwner;
  1631. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1632. cb->showInfoDialog(&iw);
  1633. fight(h);
  1634. }
  1635. }
  1636. else //accepted
  1637. {
  1638. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1639. {
  1640. InfoWindow iw;
  1641. iw.player = h->tempOwner;
  1642. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1643. cb->showInfoDialog(&iw);
  1644. //act as if player refused
  1645. joinDecision(h,cost,true);
  1646. return;
  1647. }
  1648. //take gold
  1649. if(cost)
  1650. cb->giveResource(h->tempOwner,6,-cost);
  1651. int slot = h->army.getSlotFor(subID);
  1652. if(slot >= 0) //there is place
  1653. {
  1654. //add creatures
  1655. SetGarrisons sg;
  1656. sg.garrs[h->id] = h->army;
  1657. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1658. {
  1659. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1660. }
  1661. else //add as a new stack
  1662. {
  1663. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1664. }
  1665. cb->sendAndApply(&sg);
  1666. cb->removeObject(id);
  1667. }
  1668. else
  1669. {
  1670. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1671. }
  1672. }
  1673. }
  1674. void CGCreature::fight( const CGHeroInstance *h ) const
  1675. {
  1676. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1677. }
  1678. void CGCreature::flee( const CGHeroInstance * h ) const
  1679. {
  1680. BlockingDialog ynd(true,false);
  1681. ynd.player = h->tempOwner;
  1682. ynd.text << std::pair<ui8,ui32>(11,91);
  1683. ynd.text.addReplacement(MetaString::CRE_PL_NAMES, subID);
  1684. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1685. }
  1686. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1687. {
  1688. if(subID == 7) //TODO: support for abandoned mine
  1689. return;
  1690. if(h->tempOwner == tempOwner) //we're visiting our mine
  1691. {
  1692. cb->showGarrisonDialog(id,h->id,0);
  1693. return;
  1694. }
  1695. //TODO: check if mine is guarded
  1696. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1697. MetaString ms;
  1698. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1699. cb->setHoverName(id,&ms);
  1700. int vv=1; //amount of resource per turn
  1701. if (subID==0 || subID==2)
  1702. vv++;
  1703. else if (subID==6)
  1704. vv = 1000;
  1705. InfoWindow iw;
  1706. iw.soundID = soundBase::FLAGMINE;
  1707. iw.text << std::pair<ui8,ui32>(10,subID);
  1708. iw.player = h->tempOwner;
  1709. iw.components.push_back(Component(2,subID,vv,-1));
  1710. cb->showInfoDialog(&iw);
  1711. }
  1712. void CGMine::newTurn() const
  1713. {
  1714. if(cb->getDate() == 1)
  1715. return;
  1716. if (tempOwner == NEUTRAL_PLAYER)
  1717. return;
  1718. int vv = 1;
  1719. if (subID==0 || subID==2)
  1720. vv++;
  1721. else if (subID==6)
  1722. vv = 1000;
  1723. cb->giveResource(tempOwner,subID,vv);
  1724. }
  1725. void CGMine::initObj()
  1726. {
  1727. MetaString ms;
  1728. ms << std::pair<ui8,ui32>(9,subID);
  1729. if(tempOwner >= PLAYER_LIMIT)
  1730. tempOwner = NEUTRAL_PLAYER;
  1731. else
  1732. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1733. ms.toString(hoverName);
  1734. }
  1735. void CGResource::initObj()
  1736. {
  1737. blockVisit = true;
  1738. hoverName = VLC->generaltexth->restypes[subID];
  1739. if(!amount)
  1740. {
  1741. switch(subID)
  1742. {
  1743. case 6:
  1744. amount = 500 + (rand()%6)*100;
  1745. break;
  1746. case 0: case 2:
  1747. amount = 6 + (rand()%5);
  1748. break;
  1749. default:
  1750. amount = 3 + (rand()%3);
  1751. break;
  1752. }
  1753. }
  1754. }
  1755. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1756. {
  1757. if(army.slots.size())
  1758. {
  1759. if(message.size())
  1760. {
  1761. BlockingDialog ynd(true,false);
  1762. ynd.player = h->getOwner();
  1763. ynd.text << message;
  1764. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1765. }
  1766. else
  1767. {
  1768. fightForRes(1,h);
  1769. }
  1770. }
  1771. else
  1772. {
  1773. if(message.length())
  1774. {
  1775. InfoWindow iw;
  1776. iw.player = h->tempOwner;
  1777. iw.text << message;
  1778. cb->showInfoDialog(&iw);
  1779. }
  1780. collectRes(h->getOwner());
  1781. }
  1782. }
  1783. void CGResource::collectRes( int player ) const
  1784. {
  1785. cb->giveResource(player,subID,amount);
  1786. ShowInInfobox sii;
  1787. sii.player = player;
  1788. sii.c = Component(2,subID,amount,0);
  1789. sii.text << std::pair<ui8,ui32>(11,113);
  1790. sii.text.addReplacement(MetaString::RES_NAMES, subID);
  1791. cb->showCompInfo(&sii);
  1792. cb->removeObject(id);
  1793. }
  1794. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1795. {
  1796. if(agreed)
  1797. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1798. }
  1799. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1800. {
  1801. if(result->winner == 0) //attacker won
  1802. collectRes(h->getOwner());
  1803. }
  1804. void CGVisitableOPW::newTurn() const
  1805. {
  1806. if (cb->getDate(1)==1) //first day of week
  1807. {
  1808. cb->setObjProperty(id,5,false);
  1809. MetaString ms; //set text to "not visited"
  1810. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1811. cb->setHoverName(id,&ms);
  1812. }
  1813. }
  1814. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1815. {
  1816. int mid, sound = 0;
  1817. switch (ID)
  1818. {
  1819. case 55: //mystical garden
  1820. sound = soundBase::experience;
  1821. mid = 92;
  1822. break;
  1823. case 112://windmill
  1824. sound = soundBase::GENIE;
  1825. mid = 170;
  1826. break;
  1827. case 109://waterwheel
  1828. sound = soundBase::GENIE;
  1829. mid = 164;
  1830. break;
  1831. }
  1832. if (visited)
  1833. {
  1834. if (ID!=112)
  1835. mid++;
  1836. else
  1837. mid--;
  1838. InfoWindow iw;
  1839. iw.soundID = sound;
  1840. iw.player = h->tempOwner;
  1841. iw.text << std::pair<ui8,ui32>(11,mid);
  1842. cb->showInfoDialog(&iw);
  1843. }
  1844. else
  1845. {
  1846. int type, sub, val;
  1847. type = 2;
  1848. switch (ID)
  1849. {
  1850. case 55:
  1851. if (rand()%2)
  1852. {
  1853. sub = 5;
  1854. val = 5;
  1855. }
  1856. else
  1857. {
  1858. sub = 6;
  1859. val = 500;
  1860. }
  1861. break;
  1862. case 112:
  1863. mid = 170;
  1864. sub = (rand() % 5) + 1;
  1865. val = (rand() % 4) + 3;
  1866. break;
  1867. case 109:
  1868. mid = 164;
  1869. sub = 6;
  1870. if(cb->getDate(0)<8)
  1871. val = 500;
  1872. else
  1873. val = 1000;
  1874. }
  1875. cb->giveResource(h->tempOwner,sub,val);
  1876. InfoWindow iw;
  1877. iw.soundID = sound;
  1878. iw.player = h->tempOwner;
  1879. iw.components.push_back(Component(type,sub,val,0));
  1880. iw.text << std::pair<ui8,ui32>(11,mid);
  1881. cb->showInfoDialog(&iw);
  1882. cb->setObjProperty(id,5,true);
  1883. MetaString ms; //set text to "visited"
  1884. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1885. cb->setHoverName(id,&ms);
  1886. }
  1887. }
  1888. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1889. {
  1890. if(what == 5)
  1891. visited = val;
  1892. }
  1893. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1894. {
  1895. int destinationid=-1;
  1896. switch(ID)
  1897. {
  1898. case 43: //one way - find correspong exit monolith
  1899. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1900. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1901. else
  1902. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1903. break;
  1904. case 45: //two way monolith - pick any other one
  1905. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1906. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1907. else
  1908. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1909. break;
  1910. case 103: //find nearest subterranean gate on the other level
  1911. {
  1912. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1913. for(int i=0; i<objs[103][0].size(); i++)
  1914. {
  1915. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1916. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1917. if(hlp<best.second)
  1918. {
  1919. best.first = objs[103][0][i];
  1920. best.second = hlp;
  1921. }
  1922. }
  1923. if(best.first<0)
  1924. return;
  1925. else
  1926. destinationid = best.first;
  1927. break;
  1928. }
  1929. }
  1930. if(destinationid < 0)
  1931. {
  1932. tlog2 << "Cannot find exit... :( \n";
  1933. return;
  1934. }
  1935. cb->moveHero(h->id,
  1936. (ID!=103)
  1937. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1938. : (cb->getObj(destinationid)->pos),
  1939. true);
  1940. }
  1941. void CGTeleport::initObj()
  1942. {
  1943. objs[ID][subID].push_back(id);
  1944. }
  1945. void CGArtifact::initObj()
  1946. {
  1947. blockVisit = true;
  1948. if(ID == 5)
  1949. hoverName = VLC->arth->artifacts[subID].Name();
  1950. }
  1951. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1952. {
  1953. if(!army.slots.size())
  1954. {
  1955. if(ID == 5)
  1956. {
  1957. InfoWindow iw;
  1958. iw.player = h->tempOwner;
  1959. iw.components.push_back(Component(4,subID,0,0));
  1960. if(message.length())
  1961. iw.text << message;
  1962. else
  1963. iw.text << std::pair<ui8,ui32>(12,subID);
  1964. cb->showInfoDialog(&iw);
  1965. }
  1966. pick(h);
  1967. }
  1968. else
  1969. {
  1970. if(message.size())
  1971. {
  1972. BlockingDialog ynd(true,false);
  1973. ynd.player = h->getOwner();
  1974. ynd.text << message;
  1975. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1976. }
  1977. else
  1978. {
  1979. fightForArt(0,h);
  1980. }
  1981. }
  1982. }
  1983. void CGArtifact::pick(const CGHeroInstance * h) const
  1984. {
  1985. if(ID == 5) //Artifact
  1986. {
  1987. cb->giveHeroArtifact(subID,h->id,-2);
  1988. }
  1989. else if(ID == 93) // Spell scroll
  1990. {
  1991. //TODO: support for the spell scroll
  1992. }
  1993. cb->removeObject(id);
  1994. }
  1995. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  1996. {
  1997. if(agreed)
  1998. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1999. }
  2000. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  2001. {
  2002. if(result->winner == 0) //attacker won
  2003. pick(h);
  2004. }
  2005. void CGPickable::initObj()
  2006. {
  2007. blockVisit = true;
  2008. switch(ID)
  2009. {
  2010. case 12: //campfire
  2011. val2 = (ran()%3) + 4; //4 - 6
  2012. val1 = val2 * 100;
  2013. type = ran()%6; //given resource
  2014. break;
  2015. case 101: //treasure chest
  2016. {
  2017. int hlp = ran()%100;
  2018. if(hlp >= 95)
  2019. {
  2020. type = 1;
  2021. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  2022. return;
  2023. }
  2024. else if (hlp >= 65)
  2025. {
  2026. val1 = 2000;
  2027. }
  2028. else if(hlp >= 33)
  2029. {
  2030. val1 = 1500;
  2031. }
  2032. else
  2033. {
  2034. val1 = 1000;
  2035. }
  2036. val2 = val1 - 500;
  2037. type = 0;
  2038. break;
  2039. }
  2040. }
  2041. }
  2042. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  2043. {
  2044. switch(ID)
  2045. {
  2046. case 12: //campfire
  2047. {
  2048. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  2049. cb->giveResource(h->tempOwner,6,val1);//gold
  2050. InfoWindow iw;
  2051. iw.soundID = soundBase::experience;
  2052. iw.player = h->tempOwner;
  2053. iw.components.push_back(Component(2,6,val1,0));
  2054. iw.components.push_back(Component(2,type,val2,0));
  2055. iw.text << std::pair<ui8,ui32>(11,23);
  2056. cb->showInfoDialog(&iw);
  2057. break;
  2058. }
  2059. case 101: //treasure chest
  2060. {
  2061. if (subID) //not OH3 treasure chest
  2062. {
  2063. tlog2 << "Not supported WoG treasure chest!\n";
  2064. return;
  2065. }
  2066. if(type) //there is an artifact
  2067. {
  2068. cb->giveHeroArtifact(val1,h->id,-2);
  2069. InfoWindow iw;
  2070. iw.soundID = soundBase::treasure;
  2071. iw.player = h->tempOwner;
  2072. iw.components.push_back(Component(4,val1,1,0));
  2073. iw.text << std::pair<ui8,ui32>(11,145);
  2074. iw.text.addReplacement(MetaString::ART_NAMES, val1);
  2075. cb->showInfoDialog(&iw);
  2076. break;
  2077. }
  2078. else
  2079. {
  2080. BlockingDialog sd(false,true);
  2081. sd.player = h->tempOwner;
  2082. sd.text << std::pair<ui8,ui32>(11,146);
  2083. sd.components.push_back(Component(2,6,val1,0));
  2084. sd.components.push_back(Component(5,0,val2,0));
  2085. sd.soundID = soundBase::chest;
  2086. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  2087. cb->showBlockingDialog(&sd,fun);
  2088. return;
  2089. }
  2090. }
  2091. }
  2092. cb->removeObject(id);
  2093. }
  2094. void CGPickable::chosen( int which, int heroID ) const
  2095. {
  2096. switch(which)
  2097. {
  2098. case 1: //player pick gold
  2099. cb->giveResource(cb->getOwner(heroID),6,val1);
  2100. break;
  2101. case 2: //player pick exp
  2102. cb->changePrimSkill(heroID, 4, val2);
  2103. break;
  2104. default:
  2105. throw std::string("Unhandled treasure choice");
  2106. }
  2107. cb->removeObject(id);
  2108. }
  2109. void CGWitchHut::initObj()
  2110. {
  2111. ability = allowedAbilities[ran()%allowedAbilities.size()];
  2112. }
  2113. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  2114. {
  2115. InfoWindow iw;
  2116. iw.soundID = soundBase::gazebo;
  2117. iw.player = h->getOwner();
  2118. if(!hasVisited(h->tempOwner))
  2119. cb->setObjProperty(id,10,h->tempOwner);
  2120. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  2121. {
  2122. iw.text << std::pair<ui8,ui32>(11,172);
  2123. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2124. }
  2125. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  2126. {
  2127. iw.text << std::pair<ui8,ui32>(11,173);
  2128. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2129. }
  2130. else //give sec skill
  2131. {
  2132. iw.components.push_back(Component(1, ability, 1, 0));
  2133. iw.text << std::pair<ui8,ui32>(11,171);
  2134. iw.text.addReplacement(MetaString::SEC_SKILL_NAME, ability);
  2135. cb->changeSecSkill(h->id,ability,1,true);
  2136. }
  2137. cb->showInfoDialog(&iw);
  2138. }
  2139. const std::string & CGWitchHut::getHoverText() const
  2140. {
  2141. hoverName = VLC->generaltexth->names[ID];
  2142. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2143. {
  2144. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  2145. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  2146. if(cb->getSelectedHero(cb->getCurrentPlayer())->getSecSkillLevel(ability)) //hero knows that ability
  2147. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  2148. }
  2149. return hoverName;
  2150. }
  2151. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  2152. {
  2153. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  2154. int messageID, bonusType, bonusVal;
  2155. int bonusMove = 0, sound = -1;
  2156. InfoWindow iw;
  2157. iw.player = h->tempOwner;
  2158. GiveBonus gbonus;
  2159. gbonus.hid = h->id;
  2160. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  2161. gbonus.bonus.source = HeroBonus::OBJECT;
  2162. gbonus.bonus.id = ID;
  2163. switch(ID)
  2164. {
  2165. case 14: //swan pond
  2166. messageID = 29;
  2167. sound = soundBase::LUCK;
  2168. gbonus.bonus.type = HeroBonus::LUCK;
  2169. gbonus.bonus.val = 2;
  2170. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  2171. bonusMove = -h->movement;
  2172. break;
  2173. case 28: //Faerie Ring
  2174. messageID = 49;
  2175. sound = soundBase::LUCK;
  2176. gbonus.bonus.type = HeroBonus::LUCK;
  2177. gbonus.bonus.val = 1;
  2178. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  2179. break;
  2180. case 30: //fountain of fortune
  2181. messageID = 55;
  2182. sound = soundBase::LUCK;
  2183. gbonus.bonus.type = HeroBonus::LUCK;
  2184. gbonus.bonus.val = rand()%5 - 1;
  2185. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2186. gbonus.bdescr.addReplacement((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2187. break;
  2188. case 38: //idol of fortune
  2189. messageID = 62;
  2190. iw.soundID = soundBase::experience;
  2191. if(cb->getDate(1) == 7) //7th day of week
  2192. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2193. else
  2194. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2195. gbonus.bonus.val = 1;
  2196. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2197. break;
  2198. case 64: //Rally Flag
  2199. sound = soundBase::MORALE;
  2200. messageID = 111;
  2201. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2202. gbonus.bonus.val = 1;
  2203. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2204. bonusMove = 400;
  2205. break;
  2206. case 56: //oasis
  2207. messageID = 95;
  2208. gbonus.bonus.type = HeroBonus::MORALE;
  2209. gbonus.bonus.val = 1;
  2210. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2211. bonusMove = 800;
  2212. break;
  2213. case 96: //temple
  2214. messageID = 140;
  2215. iw.soundID = soundBase::temple;
  2216. gbonus.bonus.type = HeroBonus::MORALE;
  2217. if(cb->getDate(1)==7) //sunday
  2218. {
  2219. gbonus.bonus.val = 2;
  2220. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2221. }
  2222. else
  2223. {
  2224. gbonus.bonus.val = 1;
  2225. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2226. }
  2227. break;
  2228. case 110://Watering Hole
  2229. sound = soundBase::MORALE;
  2230. messageID = 166;
  2231. gbonus.bonus.type = HeroBonus::MORALE;
  2232. gbonus.bonus.val = 1;
  2233. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2234. bonusMove = 400;
  2235. break;
  2236. case 31: //Fountain of Youth
  2237. sound = soundBase::MORALE;
  2238. messageID = 57;
  2239. gbonus.bonus.type = HeroBonus::MORALE;
  2240. gbonus.bonus.val = 1;
  2241. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2242. bonusMove = 400;
  2243. break;
  2244. }
  2245. if(visited)
  2246. {
  2247. if(ID==64 || ID==96 || ID==56)
  2248. messageID--;
  2249. else
  2250. messageID++;
  2251. }
  2252. else
  2253. {
  2254. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2255. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2256. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2257. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2258. cb->giveHeroBonus(&gbonus);
  2259. if(bonusMove) //swan pond - take all move points
  2260. {
  2261. SetMovePoints smp;
  2262. smp.hid = h->id;
  2263. smp.val = h->movement + bonusMove;
  2264. cb->setMovePoints(&smp);
  2265. }
  2266. }
  2267. iw.soundID = sound;
  2268. iw.text << std::pair<ui8,ui32>(11,messageID);
  2269. cb->showInfoDialog(&iw);
  2270. }
  2271. const std::string & CGBonusingObject::getHoverText() const
  2272. {
  2273. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2274. hoverName = VLC->generaltexth->names[ID];
  2275. if(h)
  2276. {
  2277. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2278. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2279. else
  2280. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2281. }
  2282. return hoverName;
  2283. }
  2284. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2285. {
  2286. int message;
  2287. InfoWindow iw;
  2288. iw.soundID = soundBase::faerie;
  2289. iw.player = h->tempOwner;
  2290. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2291. {
  2292. message = 78;
  2293. }
  2294. else if(h->mana < h->manaLimit())
  2295. {
  2296. GiveBonus gbonus;
  2297. gbonus.bonus.type = HeroBonus::NONE;
  2298. gbonus.hid = h->id;
  2299. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  2300. gbonus.bonus.source = HeroBonus::OBJECT;
  2301. gbonus.bonus.id = ID;
  2302. cb->giveHeroBonus(&gbonus);
  2303. cb->setManaPoints(h->id,h->manaLimit());
  2304. message = 77;
  2305. }
  2306. else
  2307. {
  2308. message = 79;
  2309. }
  2310. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2311. cb->showInfoDialog(&iw);
  2312. }
  2313. const std::string & CGMagicWell::getHoverText() const
  2314. {
  2315. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2316. hoverName = VLC->generaltexth->names[ID];
  2317. if(h)
  2318. {
  2319. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2320. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2321. else
  2322. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2323. }
  2324. return hoverName;
  2325. }
  2326. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2327. {
  2328. if(!(availableFor & (1 << h->tempOwner)))
  2329. return;
  2330. if(cb->getPlayerSettings(h->tempOwner)->human)
  2331. {
  2332. if(humanActivate)
  2333. activated(h);
  2334. }
  2335. else if(computerActivate)
  2336. activated(h);
  2337. }
  2338. void CGEvent::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2339. {
  2340. if(result->winner)
  2341. return;
  2342. giveContents(h,true);
  2343. }
  2344. void CGEvent::activated( const CGHeroInstance * h ) const
  2345. {
  2346. if(army)
  2347. {
  2348. InfoWindow iw;
  2349. iw.player = h->tempOwner;
  2350. iw.text << message;
  2351. cb->showInfoDialog(&iw);
  2352. cb->startBattleI(h->id,army,pos,boost::bind(&CGEvent::endBattle,this,h,_1));
  2353. }
  2354. else
  2355. {
  2356. giveContents(h,false);
  2357. }
  2358. }
  2359. void CGEvent::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2360. {
  2361. InfoWindow iw;
  2362. iw.player = h->getOwner();
  2363. bool changesPrimSkill = false;
  2364. for (int i = 0; i < primskills.size(); i++)
  2365. {
  2366. if(primskills[i])
  2367. {
  2368. changesPrimSkill = true;
  2369. break;
  2370. }
  2371. }
  2372. if(gainedExp || changesPrimSkill || abilities.size())
  2373. {
  2374. getText(iw,afterBattle,175,h);
  2375. if(gainedExp)
  2376. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2377. for(int i=0; i<primskills.size(); i++)
  2378. if(primskills[i])
  2379. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2380. for(int i=0; i<abilities.size(); i++)
  2381. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2382. cb->showInfoDialog(&iw);
  2383. //give exp
  2384. if(gainedExp)
  2385. cb->changePrimSkill(h->id,5,gainedExp,false);
  2386. //give prim skills
  2387. for(int i=0; i<primskills.size(); i++)
  2388. if(primskills[i])
  2389. cb->changePrimSkill(h->id,i,primskills[i],false);
  2390. //give sec skills
  2391. for(int i=0; i<abilities.size(); i++)
  2392. {
  2393. int curLev = h->getSecSkillLevel(abilities[i]);
  2394. if(curLev && curLev < abilityLevels[i]
  2395. || h->secSkills.size() < SKILL_PER_HERO )
  2396. {
  2397. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2398. }
  2399. }
  2400. }
  2401. if(spells.size())
  2402. {
  2403. std::set<ui32> spellsToGive;
  2404. iw.components.clear();
  2405. for(int i=0; i<spells.size(); i++)
  2406. {
  2407. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2408. spellsToGive.insert(spells[i]);
  2409. }
  2410. if(spellsToGive.size())
  2411. {
  2412. cb->changeSpells(h->id,true,spellsToGive);
  2413. cb->showInfoDialog(&iw);
  2414. }
  2415. }
  2416. if(manaDiff)
  2417. {
  2418. getText(iw,afterBattle,manaDiff,176,177,h);
  2419. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2420. cb->showInfoDialog(&iw);
  2421. cb->setManaPoints(h->id, h->mana + manaDiff);
  2422. }
  2423. if(moraleDiff)
  2424. {
  2425. getText(iw,afterBattle,moraleDiff,178,179,h);
  2426. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2427. cb->showInfoDialog(&iw);
  2428. GiveBonus gb;
  2429. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2430. gb.hid = h->id;
  2431. cb->giveHeroBonus(&gb);
  2432. }
  2433. if(luckDiff)
  2434. {
  2435. getText(iw,afterBattle,luckDiff,180,181,h);
  2436. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2437. cb->showInfoDialog(&iw);
  2438. GiveBonus gb;
  2439. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2440. gb.hid = h->id;
  2441. cb->giveHeroBonus(&gb);
  2442. }
  2443. iw.components.clear();
  2444. iw.text.clear();
  2445. for(int i=0; i<resources.size(); i++)
  2446. {
  2447. if(resources[i] < 0)
  2448. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2449. }
  2450. if(iw.components.size())
  2451. {
  2452. getText(iw,afterBattle,182,h);
  2453. cb->showInfoDialog(&iw);
  2454. }
  2455. iw.components.clear();
  2456. iw.text.clear();
  2457. for(int i=0; i<resources.size(); i++)
  2458. {
  2459. if(resources[i] > 0)
  2460. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2461. }
  2462. if(iw.components.size())
  2463. {
  2464. getText(iw,afterBattle,183,h);
  2465. cb->showInfoDialog(&iw);
  2466. }
  2467. iw.components.clear();
  2468. for(int i=0; i<artifacts.size(); i++)
  2469. {
  2470. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2471. }
  2472. if(iw.components.size())
  2473. {
  2474. cb->showInfoDialog(&iw);
  2475. }
  2476. for(int i=0; i<resources.size(); i++)
  2477. if(resources[i])
  2478. cb->giveResource(h->getOwner(),i,resources[i]);
  2479. for(int i=0; i<artifacts.size(); i++)
  2480. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2481. //show dialog with given creatures
  2482. iw.components.clear();
  2483. iw.text.clear();
  2484. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2485. {
  2486. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2487. }
  2488. if(iw.components.size())
  2489. {
  2490. if(afterBattle)
  2491. {
  2492. if(iw.components.front().val == 1)
  2493. {
  2494. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2495. iw.text.addReplacement(MetaString::CRE_SING_NAMES, iw.components.front().subtype);
  2496. }
  2497. else
  2498. {
  2499. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2500. iw.text.addReplacement(MetaString::CRE_PL_NAMES, iw.components.front().subtype);
  2501. }
  2502. iw.text.addReplacement(h->name);
  2503. }
  2504. else
  2505. {
  2506. iw.text << message;
  2507. afterBattle = true;
  2508. }
  2509. cb->showInfoDialog(&iw);
  2510. }
  2511. //check if creatures can be moved to hero army
  2512. CCreatureSet heroArmy = h->army;
  2513. CCreatureSet ourArmy = creatures;
  2514. while(ourArmy)
  2515. {
  2516. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2517. if(slot < 0)
  2518. break;
  2519. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2520. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2521. ourArmy.slots.erase(ourArmy.slots.begin());
  2522. }
  2523. if(!ourArmy) //all creatures can be moved to hero army - do that
  2524. {
  2525. SetGarrisons sg;
  2526. sg.garrs[h->id] = heroArmy;
  2527. cb->sendAndApply(&sg);
  2528. }
  2529. else //show garrison window and let player pick creatures
  2530. {
  2531. SetGarrisons sg;
  2532. sg.garrs[id] = creatures;
  2533. cb->sendAndApply(&sg);
  2534. if(removeAfterVisit)
  2535. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2536. else
  2537. cb->showGarrisonDialog(id,h->id,0);
  2538. return;
  2539. }
  2540. if(!afterBattle)
  2541. {
  2542. iw.text << message;
  2543. cb->showInfoDialog(&iw);
  2544. }
  2545. if(removeAfterVisit)
  2546. cb->removeObject(id);
  2547. }
  2548. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2549. {
  2550. if(afterBattle)
  2551. {
  2552. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2553. iw.text.addReplacement(h->name);
  2554. }
  2555. else
  2556. {
  2557. iw.text << message;
  2558. afterBattle = true;
  2559. }
  2560. }
  2561. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int val, int positive, int negative, const CGHeroInstance * h ) const
  2562. {
  2563. iw.components.clear();
  2564. iw.text.clear();
  2565. if(afterBattle)
  2566. {
  2567. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2568. iw.text.addReplacement(h->name);
  2569. }
  2570. else
  2571. {
  2572. iw.text << message;
  2573. afterBattle = true;
  2574. }
  2575. }
  2576. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2577. {
  2578. InfoWindow iw;
  2579. iw.soundID = soundBase::LIGHTHOUSE;
  2580. iw.player = h->tempOwner;
  2581. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2582. cb->showInfoDialog(&iw);
  2583. FoWChange fw;
  2584. fw.player = h->tempOwner;
  2585. fw.mode = 1;
  2586. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2587. cb->sendAndApply(&fw);
  2588. }
  2589. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2590. {
  2591. if(spell == 255)
  2592. {
  2593. tlog1 << "Not initialized shrine visited!\n";
  2594. return;
  2595. }
  2596. if(!hasVisited(h->tempOwner))
  2597. cb->setObjProperty(id,10,h->tempOwner);
  2598. InfoWindow iw;
  2599. iw.soundID = soundBase::temple;
  2600. iw.player = h->getOwner();
  2601. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2602. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2603. iw.text << ".";
  2604. if(!h->getArt(17)) //no spellbook
  2605. {
  2606. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2607. }
  2608. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2609. {
  2610. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2611. }
  2612. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2613. {
  2614. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2615. }
  2616. else //give spell
  2617. {
  2618. std::set<ui32> spells;
  2619. spells.insert(spell);
  2620. cb->changeSpells(h->id,true,spells);
  2621. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2622. }
  2623. cb->showInfoDialog(&iw);
  2624. }
  2625. void CGShrine::initObj()
  2626. {
  2627. if(spell == 255) //spell not set
  2628. {
  2629. int level = ID-87;
  2630. std::vector<ui32> possibilities;
  2631. //add all allowed spells of wanted level
  2632. for(int i=0; i<SPELLS_QUANTITY; i++)
  2633. {
  2634. if(VLC->spellh->spells[i].level == level
  2635. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2636. {
  2637. possibilities.push_back(VLC->spellh->spells[i].id);
  2638. }
  2639. }
  2640. if(!possibilities.size())
  2641. {
  2642. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  2643. return;
  2644. }
  2645. spell = possibilities[ran() % possibilities.size()];
  2646. }
  2647. }
  2648. const std::string & CGShrine::getHoverText() const
  2649. {
  2650. hoverName = VLC->generaltexth->names[ID];
  2651. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2652. {
  2653. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  2654. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  2655. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2656. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  2657. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  2658. }
  2659. return hoverName;
  2660. }
  2661. void CGSignBottle::initObj()
  2662. {
  2663. //if no text is set than we pick random from the predefined ones
  2664. if(!message.size())
  2665. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  2666. }
  2667. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  2668. {
  2669. InfoWindow iw;
  2670. iw.soundID = soundBase::STORE;
  2671. iw.player = h->getOwner();
  2672. iw.text << message;
  2673. cb->showInfoDialog(&iw);
  2674. }
  2675. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  2676. {
  2677. }
  2678. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  2679. {
  2680. int type = bonusType;
  2681. int bid = bonusID;
  2682. //check if the bonus if applicable, if not - give primary skill (always possible)
  2683. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  2684. if((type == 1
  2685. && ((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)
  2686. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  2687. {
  2688. type = 0;
  2689. bid = ran() % PRIMARY_SKILLS;
  2690. }
  2691. InfoWindow iw;
  2692. iw.soundID = soundBase::gazebo;
  2693. iw.player = h->getOwner();
  2694. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  2695. switch (type)
  2696. {
  2697. case 0:
  2698. cb->changePrimSkill(h->id,bid,+1);
  2699. iw.components.push_back(Component(Component::PRIM_SKILL,bid,ssl+1,0));
  2700. break;
  2701. case 1:
  2702. cb->changeSecSkill(h->id,bid,+1);
  2703. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  2704. break;
  2705. case 2:
  2706. {
  2707. std::set<ui32> hlp;
  2708. hlp.insert(bid);
  2709. cb->changeSpells(h->id,true,hlp);
  2710. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  2711. }
  2712. break;
  2713. default:
  2714. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  2715. return;
  2716. }
  2717. cb->showInfoDialog(&iw);
  2718. cb->removeObject(id);
  2719. }
  2720. void CGScholar::initObj()
  2721. {
  2722. blockVisit = true;
  2723. if(bonusType == 255)
  2724. {
  2725. bonusType = ran()%3;
  2726. switch(bonusType)
  2727. {
  2728. case 0:
  2729. bonusID = ran() % PRIMARY_SKILLS;
  2730. break;
  2731. case 1:
  2732. bonusID = ran() % SKILL_QUANTITY;
  2733. break;
  2734. case 2:
  2735. bonusID = ran() % SPELLS_QUANTITY;
  2736. break;
  2737. }
  2738. }
  2739. }
  2740. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  2741. {
  2742. int sound = 0;
  2743. int txtid = -1;
  2744. switch(ID)
  2745. {
  2746. case 22: //Corpse
  2747. txtid = 37;
  2748. sound = soundBase::MYSTERY;
  2749. break;
  2750. case 39: //Lean To
  2751. sound = soundBase::GENIE;
  2752. txtid = 64;
  2753. break;
  2754. case 105://Wagon
  2755. sound = soundBase::GENIE;
  2756. txtid = 154;
  2757. break;
  2758. case 108:
  2759. break;
  2760. default:
  2761. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  2762. return;
  2763. }
  2764. if(ID == 108)//Warrior's Tomb
  2765. {
  2766. //ask if player wants to search the Tomb
  2767. BlockingDialog bd(true, false);
  2768. sound = soundBase::GRAVEYARD;
  2769. bd.player = h->getOwner();
  2770. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  2771. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  2772. return;
  2773. }
  2774. InfoWindow iw;
  2775. iw.soundID = sound;
  2776. iw.player = h->getOwner();
  2777. if(players.size()) //we have been already visited...
  2778. {
  2779. txtid++;
  2780. if(ID == 105) //wagon has extra text (for finding art) we need to ommit
  2781. txtid++;
  2782. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2783. }
  2784. else //first visit - give bonus!
  2785. {
  2786. switch(artOrRes)
  2787. {
  2788. case 0:
  2789. txtid++;
  2790. break;
  2791. case 1: //art
  2792. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2793. cb->giveHeroArtifact(bonusType,h->id,-2);
  2794. break;
  2795. case 2: //res
  2796. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  2797. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  2798. break;
  2799. }
  2800. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2801. if(ID == 105 && artOrRes == 1)
  2802. {
  2803. iw.text.localStrings.back().second++;
  2804. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  2805. }
  2806. }
  2807. cb->showInfoDialog(&iw);
  2808. cb->setObjProperty(id,10,h->getOwner());
  2809. }
  2810. const std::string & CGOnceVisitable::getHoverText() const
  2811. {
  2812. hoverName = VLC->generaltexth->names[ID] + " ";
  2813. hoverName += (hasVisited(cb->getCurrentPlayer())
  2814. ? (VLC->generaltexth->allTexts[352]) //visited
  2815. : ( VLC->generaltexth->allTexts[353])); //not visited
  2816. return hoverName;
  2817. }
  2818. void CGOnceVisitable::initObj()
  2819. {
  2820. switch(ID)
  2821. {
  2822. case 22: //Corpse
  2823. {
  2824. blockVisit = true;
  2825. int hlp = ran()%100;
  2826. if(hlp < 20)
  2827. {
  2828. artOrRes = 1;
  2829. std::vector<CArtifact*> arts;
  2830. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  2831. bonusType = arts[ran() % arts.size()]->id;
  2832. }
  2833. else
  2834. {
  2835. artOrRes = 0;
  2836. }
  2837. }
  2838. break;
  2839. case 39: //Lean To
  2840. {
  2841. artOrRes = 2;
  2842. bonusType = ran()%6; //any basic resource without gold
  2843. bonusVal = ran()%4 + 1;
  2844. break;
  2845. }
  2846. case 108://Warrior's Tomb
  2847. {
  2848. artOrRes = 1;
  2849. std::vector<CArtifact*> arts;
  2850. int hlp = ran()%100;
  2851. if(hlp < 30)
  2852. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  2853. else if(hlp < 80)
  2854. cb->getAllowed(arts,CArtifact::ART_MINOR);
  2855. else if(hlp < 95)
  2856. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  2857. else
  2858. cb->getAllowed(arts,CArtifact::ART_RELIC);
  2859. bonusType = arts[ran() % arts.size()]->id;
  2860. }
  2861. break;
  2862. case 105://Wagon
  2863. {
  2864. int hlp = ran()%100;
  2865. if(hlp < 10)
  2866. {
  2867. artOrRes = 0; // nothing... :(
  2868. }
  2869. else if(hlp < 50) //minor or treasure art
  2870. {
  2871. artOrRes = 1;
  2872. std::vector<CArtifact*> arts;
  2873. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  2874. bonusType = arts[ran() % arts.size()]->id;
  2875. }
  2876. else //2 - 5 of non-gold resource
  2877. {
  2878. artOrRes = 2;
  2879. bonusType = ran()%6;
  2880. bonusVal = ran()%4 + 2;
  2881. }
  2882. break;
  2883. }
  2884. }
  2885. }
  2886. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  2887. {
  2888. if(accept)
  2889. {
  2890. InfoWindow iw;
  2891. iw.player = h->getOwner();
  2892. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  2893. if(players.size()) //we've been already visited, player found nothing
  2894. {
  2895. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  2896. }
  2897. else //first visit - give artifact
  2898. {
  2899. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  2900. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2901. iw.text.addReplacement(MetaString::ART_NAMES, bonusType);
  2902. cb->giveHeroArtifact(bonusType,h->id,-2);
  2903. }
  2904. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  2905. {
  2906. //ruin morale
  2907. GiveBonus gb;
  2908. gb.hid = h->id;
  2909. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  2910. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  2911. cb->giveHeroBonus(&gb);
  2912. }
  2913. cb->showInfoDialog(&iw);
  2914. //add player to the visitors (for visited tooltop)
  2915. cb->setObjProperty(id,10,h->getOwner());
  2916. }
  2917. }