CObjectHandler.cpp 70 KB

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