CObjectHandler.cpp 69 KB

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