CObjectHandler.cpp 73 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989
  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 TerrainTile::dirtRoad:
  255. ret = 75;
  256. break;
  257. case TerrainTile::grazvelRoad:
  258. ret = 65;
  259. break;
  260. case TerrainTile::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. InfoWindow iw;
  580. iw.player = h->tempOwner;
  581. iw.soundID = soundBase::ROGUE;
  582. if(cb->getHeroCount(h->tempOwner,false) < 8) //free hero slot
  583. {
  584. cb->changeObjPos(id,pos+int3(1,0,0),0);
  585. cb->setObjProperty(id,6,HEROI_TYPE); //set ID to 34
  586. cb->giveHero(id,h->tempOwner); //recreates def and adds hero to player
  587. iw.text << std::pair<ui8,ui32>(11,102);
  588. }
  589. else //already 8 wandering heroes
  590. {
  591. iw.text << std::pair<ui8,ui32>(11,103);
  592. }
  593. cb->showInfoDialog(&iw);
  594. }
  595. }
  596. const std::string & CGHeroInstance::getBiography() const
  597. {
  598. if (biography.length())
  599. return biography;
  600. else
  601. return VLC->generaltexth->hTxts[subID].biography;
  602. }
  603. void CGHeroInstance::initObj()
  604. {
  605. blockVisit = true;
  606. }
  607. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  608. {
  609. int ret = 0;
  610. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  611. for(int i=0; i < mods.size(); i++)
  612. ret += mods[i].first;
  613. if(ret > 3)
  614. return 3;
  615. if(ret < -3)
  616. return -3;
  617. return ret;
  618. }
  619. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  620. {
  621. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  622. std::vector<std::pair<int,std::string> > ret;
  623. //various morale bonuses (from buildings, artifacts, etc)
  624. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  625. {
  626. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  627. {
  628. ret.push_back(std::make_pair(i->val, i->description));
  629. }
  630. }
  631. //leadership
  632. if(getSecSkillLevel(6))
  633. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  634. //town structures
  635. if(town && visitedTown)
  636. {
  637. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  638. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  639. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  640. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  641. }
  642. //number of alignments and presence of undead
  643. if(stack>=0)
  644. {
  645. bool archangelInArmy = false;
  646. std::set<si8> factions;
  647. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  648. {
  649. factions.insert(VLC->creh->creatures[i->second.first].faction);
  650. if(i->second.first == 13)
  651. archangelInArmy = true;
  652. }
  653. if(factions.size() == 1)
  654. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  655. else
  656. {
  657. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  658. {
  659. char buf[150];
  660. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  661. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  662. }
  663. else
  664. {
  665. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  666. }
  667. }
  668. if(vstd::contains(factions,4))
  669. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  670. if(archangelInArmy)
  671. {
  672. char buf[100];
  673. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl.c_str());
  674. ret.push_back(std::pair<int,std::string>(1,buf)); //%s in group +1
  675. }
  676. }
  677. return ret;
  678. }
  679. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  680. {
  681. int ret = 0;
  682. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  683. for(int i=0; i < mods.size(); i++)
  684. ret += mods[i].first;
  685. if(ret > 3)
  686. return 3;
  687. if(ret < -3)
  688. return -3;
  689. return ret;
  690. }
  691. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  692. {
  693. std::vector<std::pair<int,std::string> > ret;
  694. //various morale bonuses (from buildings, artifacts, etc)
  695. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  696. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  697. ret.push_back(std::make_pair(i->val, i->description));
  698. //luck skill
  699. if(getSecSkillLevel(9))
  700. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  701. return ret;
  702. }
  703. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  704. {
  705. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  706. if(i->source == from && i->id == id)
  707. return &*i;
  708. return NULL;
  709. }
  710. void CGHeroInstance::setPropertyDer( ui8 what, ui32 val )
  711. {
  712. if(what == 3)
  713. army.slots[0].second = val;
  714. }
  715. double CGHeroInstance::getHeroStrength() const
  716. {
  717. return sqrt((1.0 + 0.05*getPrimSkillLevel(0)) * (1.0 + 0.05*getPrimSkillLevel(1)));
  718. }
  719. int CGHeroInstance::getTotalStrength() const
  720. {
  721. double ret = getHeroStrength() * getArmyStrength();
  722. return (int) ret;
  723. }
  724. ui8 CGHeroInstance::getSpellSchoolLevel(const CSpell * spell) const
  725. {
  726. //TODO: skill level may be different on special terrain
  727. ui8 skill = 0; //skill level
  728. if(spell->fire)
  729. skill = std::max(skill,getSecSkillLevel(14));
  730. if(spell->air)
  731. skill = std::max(skill,getSecSkillLevel(15));
  732. if(spell->water)
  733. skill = std::max(skill,getSecSkillLevel(16));
  734. if(spell->earth)
  735. skill = std::max(skill,getSecSkillLevel(17));
  736. return skill;
  737. }
  738. bool CGHeroInstance::canCastThisSpell(const CSpell * spell) const
  739. {
  740. if(!getArt(17)) //if hero has no spellbook
  741. return false;
  742. if(vstd::contains(spells, spell->id) //hero does not have this spell in spellbook
  743. || (spell->air && hasBonusOfType(HeroBonus::AIR_SPELLS)) // this is air spell and hero can cast all air spells
  744. || (spell->fire && hasBonusOfType(HeroBonus::FIRE_SPELLS)) // this is fire spell and hero can cast all fire spells
  745. || (spell->water && hasBonusOfType(HeroBonus::WATER_SPELLS)) // this is water spell and hero can cast all water spells
  746. || (spell->earth && hasBonusOfType(HeroBonus::EARTH_SPELLS)) // this is earth spell and hero can cast all earth spells
  747. )
  748. return true;
  749. for(std::list<HeroBonus>::const_iterator it = bonuses.begin(); it != bonuses.end(); ++it)
  750. {
  751. if(it->type == HeroBonus::SPELL && it->subtype == spell->id)
  752. {
  753. return true;
  754. }
  755. if(it->type == HeroBonus::SPELLS_OF_LEVEL && it->subtype == spell->level)
  756. {
  757. return true;
  758. }
  759. }
  760. return false;
  761. }
  762. int3 CGHeroInstance::getSightCenter() const
  763. {
  764. return getPosition(false);
  765. }
  766. int CGHeroInstance::getSightRadious() const
  767. {
  768. return 5 + getSecSkillLevel(3) + valOfBonuses(HeroBonus::SIGHT_RADIOUS); //default + scouting
  769. }
  770. si32 CGHeroInstance::manaRegain() const
  771. {
  772. return 1 + getSecSkillLevel(8) + valOfBonuses(HeroBonus::MANA_REGENERATION); //1 + Mysticism level
  773. }
  774. int CGHeroInstance::valOfBonuses( HeroBonus::BonusType type, int subtype /*= -1*/ ) const
  775. {
  776. int ret = 0;
  777. if(subtype == -1)
  778. {
  779. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  780. if(i->type == type)
  781. ret += i->val;
  782. }
  783. else
  784. {
  785. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  786. if(i->type == type && i->subtype == subtype)
  787. ret += i->val;
  788. }
  789. return ret;
  790. }
  791. bool CGHeroInstance::hasBonusOfType(HeroBonus::BonusType type, int subtype /*= -1*/) const
  792. {
  793. if(subtype == -1) //any subtype
  794. {
  795. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  796. if(i->type == type)
  797. return true;
  798. }
  799. else //given subtype
  800. {
  801. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  802. if(i->type == type && i->subtype == subtype)
  803. return true;
  804. }
  805. throw "CGHeroInstance::hasBonusOfType - we shouln't be here!";
  806. }
  807. int CGTownInstance::getSightRadious() const //returns sight distance
  808. {
  809. return 5;
  810. }
  811. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  812. {
  813. if((builtBuildings.find(9))!=builtBuildings.end())
  814. return 3;
  815. if((builtBuildings.find(8))!=builtBuildings.end())
  816. return 2;
  817. if((builtBuildings.find(7))!=builtBuildings.end())
  818. return 1;
  819. return 0;
  820. }
  821. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  822. {
  823. if ((builtBuildings.find(13))!=builtBuildings.end())
  824. return 3;
  825. if ((builtBuildings.find(12))!=builtBuildings.end())
  826. return 2;
  827. if ((builtBuildings.find(11))!=builtBuildings.end())
  828. return 1;
  829. if ((builtBuildings.find(10))!=builtBuildings.end())
  830. return 0;
  831. return -1;
  832. }
  833. int CGTownInstance::mageGuildLevel() const
  834. {
  835. if ((builtBuildings.find(4))!=builtBuildings.end())
  836. return 5;
  837. if ((builtBuildings.find(3))!=builtBuildings.end())
  838. return 4;
  839. if ((builtBuildings.find(2))!=builtBuildings.end())
  840. return 3;
  841. if ((builtBuildings.find(1))!=builtBuildings.end())
  842. return 2;
  843. if ((builtBuildings.find(0))!=builtBuildings.end())
  844. return 1;
  845. return 0;
  846. }
  847. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  848. {
  849. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  850. }
  851. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  852. {
  853. return town->hordeLvl[HID];
  854. }
  855. int CGTownInstance::creatureGrowth(const int & level) const
  856. {
  857. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  858. switch(fortLevel())
  859. {
  860. case 3:
  861. ret*=2;break;
  862. case 2:
  863. ret*=(1.5); break;
  864. }
  865. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  866. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  867. if(getHordeLevel(0)==level)
  868. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  869. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  870. if(getHordeLevel(1)==level)
  871. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  872. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  873. return ret;
  874. }
  875. int CGTownInstance::dailyIncome() const
  876. {
  877. int ret = 0;
  878. if ((builtBuildings.find(26))!=builtBuildings.end())
  879. ret+=5000;
  880. if ((builtBuildings.find(13))!=builtBuildings.end())
  881. ret+=4000;
  882. else if ((builtBuildings.find(12))!=builtBuildings.end())
  883. ret+=2000;
  884. else if ((builtBuildings.find(11))!=builtBuildings.end())
  885. ret+=1000;
  886. else if ((builtBuildings.find(10))!=builtBuildings.end())
  887. ret+=500;
  888. return ret;
  889. }
  890. bool CGTownInstance::hasFort() const
  891. {
  892. return (builtBuildings.find(7))!=builtBuildings.end();
  893. }
  894. bool CGTownInstance::hasCapitol() const
  895. {
  896. return (builtBuildings.find(13))!=builtBuildings.end();
  897. }
  898. CGTownInstance::CGTownInstance()
  899. {
  900. builded=-1;
  901. destroyed=-1;
  902. garrisonHero=NULL;
  903. town=NULL;
  904. visitingHero = NULL;
  905. }
  906. CGTownInstance::~CGTownInstance()
  907. {}
  908. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  909. {
  910. if(checkGuild && mageGuildLevel() < level)
  911. return 0;
  912. int ret = 6 - level; //how many spells are available at this level
  913. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  914. ret++;
  915. return ret;
  916. }
  917. bool CGTownInstance::needsLastStack() const
  918. {
  919. if(garrisonHero)
  920. return true;
  921. else return false;
  922. }
  923. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  924. {
  925. if(getOwner() != h->getOwner())
  926. {
  927. return;
  928. }
  929. cb->heroVisitCastle(id,h->id);
  930. }
  931. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  932. {
  933. cb->stopHeroVisitCastle(id,h->id);
  934. }
  935. void CGTownInstance::initObj()
  936. {
  937. MetaString ms;
  938. ms << name << ", " << town->Name();
  939. hoverName = toString(ms);
  940. }
  941. int3 CGTownInstance::getSightCenter() const
  942. {
  943. return pos - int3(2,0,0);
  944. }
  945. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  946. {
  947. if(visitors.find(h->id)==visitors.end())
  948. {
  949. onNAHeroVisit(h->id, false);
  950. switch(ID)
  951. {
  952. case 102: //tree
  953. case 4: //arena
  954. case 41://library
  955. case 47: //School of Magic
  956. case 107://School of War
  957. break;
  958. default:
  959. cb->setObjProperty(id,4,h->id); //add to the visitors
  960. break;
  961. }
  962. }
  963. else
  964. {
  965. onNAHeroVisit(h->id, true);
  966. }
  967. }
  968. void CGVisitableOPH::initObj()
  969. {
  970. if(ID==102)
  971. ttype = ran()%3;
  972. else
  973. ttype = -1;
  974. }
  975. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  976. {
  977. if(result) //player agreed to give res for exp
  978. {
  979. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  980. cb->changePrimSkill(heroID,4,expVal); //give exp
  981. cb->setObjProperty(id,4,heroID); //add to the visitors
  982. }
  983. }
  984. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  985. {
  986. int id=0, subid=0, ot=0, val=1, sound = 0;
  987. switch(ID)
  988. {
  989. case 4: //arena
  990. sound = soundBase::NOMAD;
  991. ot = 0;
  992. break;
  993. case 51: //mercenary camp
  994. sound = soundBase::NOMAD;
  995. subid=0;
  996. ot=80;
  997. break;
  998. case 23: //marletto tower
  999. sound = soundBase::NOMAD;
  1000. subid=1;
  1001. ot=39;
  1002. break;
  1003. case 61:
  1004. sound = soundBase::gazebo;
  1005. subid=2;
  1006. ot=100;
  1007. break;
  1008. case 32:
  1009. sound = soundBase::GETPROTECTION;
  1010. subid=3;
  1011. ot=59;
  1012. break;
  1013. case 100:
  1014. sound = soundBase::gazebo;
  1015. id=5;
  1016. ot=143;
  1017. val=1000;
  1018. break;
  1019. case 102:
  1020. sound = soundBase::gazebo;
  1021. id = 5;
  1022. subid = 1;
  1023. ot = 146;
  1024. val = 1;
  1025. break;
  1026. case 41:
  1027. sound = soundBase::gazebo;
  1028. ot = 66;
  1029. break;
  1030. case 47: //School of Magic
  1031. sound = soundBase::faerie;
  1032. ot = 71;
  1033. break;
  1034. case 107://School of War
  1035. sound = soundBase::MILITARY;
  1036. ot = 158;
  1037. break;
  1038. }
  1039. if (!alreadyVisited)
  1040. {
  1041. switch (ID)
  1042. {
  1043. case 4: //arena
  1044. {
  1045. BlockingDialog sd(false,true);
  1046. sd.soundID = sound;
  1047. sd.text << std::pair<ui8,ui32>(11,ot);
  1048. sd.components.push_back(Component(0,0,2,0));
  1049. sd.components.push_back(Component(0,1,2,0));
  1050. sd.player = cb->getOwner(heroID);
  1051. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  1052. return;
  1053. }
  1054. case 51:
  1055. case 23:
  1056. case 61:
  1057. case 32:
  1058. {
  1059. cb->changePrimSkill(heroID,subid,val);
  1060. InfoWindow iw;
  1061. iw.soundID = sound;
  1062. iw.components.push_back(Component(0,subid,val,0));
  1063. iw.text << std::pair<ui8,ui32>(11,ot);
  1064. iw.player = cb->getOwner(heroID);
  1065. cb->showInfoDialog(&iw);
  1066. break;
  1067. }
  1068. case 100: //give exp
  1069. {
  1070. InfoWindow iw;
  1071. iw.soundID = sound;
  1072. iw.components.push_back(Component(id,subid,val,0));
  1073. iw.player = cb->getOwner(heroID);
  1074. iw.text << std::pair<ui8,ui32>(11,ot);
  1075. iw.soundID = soundBase::gazebo;
  1076. cb->showInfoDialog(&iw);
  1077. cb->changePrimSkill(heroID,4,val);
  1078. break;
  1079. }
  1080. case 102://tree
  1081. {
  1082. const CGHeroInstance *h = cb->getHero(heroID);
  1083. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  1084. if(!ttype)
  1085. {
  1086. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1087. InfoWindow iw;
  1088. iw.soundID = sound;
  1089. iw.components.push_back(Component(id,subid,1,0));
  1090. iw.player = cb->getOwner(heroID);
  1091. iw.text << std::pair<ui8,ui32>(11,148);
  1092. cb->showInfoDialog(&iw);
  1093. cb->changePrimSkill(heroID,4,val);
  1094. break;
  1095. }
  1096. else
  1097. {
  1098. int res, resval;
  1099. if(ttype==1)
  1100. {
  1101. res = 6;
  1102. resval = 2000;
  1103. ot = 149;
  1104. }
  1105. else
  1106. {
  1107. res = 5;
  1108. resval = 10;
  1109. ot = 151;
  1110. }
  1111. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  1112. {
  1113. ot++;
  1114. InfoWindow iw;
  1115. iw.soundID = sound;
  1116. iw.player = h->tempOwner;
  1117. iw.text << std::pair<ui8,ui32>(11,ot);
  1118. cb->showInfoDialog(&iw);
  1119. return;
  1120. }
  1121. BlockingDialog sd(true,false);
  1122. sd.soundID = sound;
  1123. sd.player = cb->getOwner(heroID);
  1124. sd.text << std::pair<ui8,ui32>(11,ot);
  1125. sd.components.push_back(Component(id,subid,val,0));
  1126. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  1127. }
  1128. break;
  1129. }
  1130. case 41://library of enlightenment
  1131. {
  1132. const CGHeroInstance *h = cb->getHero(heroID);
  1133. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  1134. {
  1135. InfoWindow iw;
  1136. iw.soundID = sound;
  1137. iw.player = cb->getOwner(heroID);
  1138. iw.text << std::pair<ui8,ui32>(11,68);
  1139. cb->showInfoDialog(&iw);
  1140. }
  1141. else
  1142. {
  1143. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  1144. cb->changePrimSkill(heroID,0,2);
  1145. cb->changePrimSkill(heroID,1,2);
  1146. cb->changePrimSkill(heroID,2,2);
  1147. cb->changePrimSkill(heroID,3,2);
  1148. InfoWindow iw;
  1149. iw.soundID = sound;
  1150. iw.player = cb->getOwner(heroID);
  1151. iw.text << std::pair<ui8,ui32>(11,66);
  1152. cb->showInfoDialog(&iw);
  1153. }
  1154. break;
  1155. }
  1156. case 47: //School of Magic
  1157. case 107://School of War
  1158. {
  1159. int skill = (ID==47 ? 2 : 0);
  1160. if(cb->getResource(cb->getOwner(heroID),6) < 1000) //not enough resources
  1161. {
  1162. InfoWindow iw;
  1163. iw.soundID = sound;
  1164. iw.player = cb->getOwner(heroID);
  1165. iw.text << std::pair<ui8,ui32>(MetaString::ADVOB_TXT,ot+2);
  1166. cb->showInfoDialog(&iw);
  1167. }
  1168. else
  1169. {
  1170. BlockingDialog sd(true,true);
  1171. sd.soundID = sound;
  1172. sd.player = cb->getOwner(heroID);
  1173. sd.text << std::pair<ui8,ui32>(11,ot);
  1174. sd.components.push_back(Component(Component::PRIM_SKILL, skill, +1, 0));
  1175. sd.components.push_back(Component(Component::PRIM_SKILL, skill+1, +1, 0));
  1176. cb->showBlockingDialog(&sd,boost::bind(&CGVisitableOPH::schoolSelected,this,heroID,_1));
  1177. }
  1178. }
  1179. break;
  1180. }
  1181. }
  1182. else
  1183. {
  1184. ot++;
  1185. InfoWindow iw;
  1186. iw.soundID = sound;
  1187. iw.player = cb->getOwner(heroID);
  1188. iw.text << std::pair<ui8,ui32>(11,ot);
  1189. cb->showInfoDialog(&iw);
  1190. }
  1191. }
  1192. const std::string & CGVisitableOPH::getHoverText() const
  1193. {
  1194. int pom = -1;
  1195. switch(ID)
  1196. {
  1197. case 4:
  1198. pom = -1;
  1199. break;
  1200. case 51:
  1201. pom = 8;
  1202. break;
  1203. case 23:
  1204. pom = 7;
  1205. break;
  1206. case 61:
  1207. pom = 11;
  1208. break;
  1209. case 32:
  1210. pom = 4;
  1211. break;
  1212. case 100:
  1213. pom = 5;
  1214. break;
  1215. case 102:
  1216. pom = 18;
  1217. break;
  1218. case 41:
  1219. break;
  1220. case 47: //School of Magic
  1221. pom = 9;
  1222. break;
  1223. case 107://School of War
  1224. pom = 10;
  1225. break;
  1226. default:
  1227. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1228. }
  1229. hoverName = VLC->generaltexth->names[ID];
  1230. if(pom >= 0)
  1231. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1232. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1233. if(h)
  1234. {
  1235. hoverName += ' ';
  1236. hoverName += (vstd::contains(visitors,h->id))
  1237. ? (VLC->generaltexth->allTexts[352]) //visited
  1238. : ( VLC->generaltexth->allTexts[353]); //not visited
  1239. }
  1240. return hoverName;
  1241. }
  1242. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1243. {
  1244. cb->setObjProperty(id,4,heroID); //add to the visitors
  1245. cb->changePrimSkill(heroID,primSkill-1,2);
  1246. }
  1247. void CGVisitableOPH::setPropertyDer( ui8 what, ui32 val )
  1248. {
  1249. if(what == 4)
  1250. visitors.insert(val);
  1251. }
  1252. void CGVisitableOPH::schoolSelected(int heroID, ui32 which) const
  1253. {
  1254. if(!which) //player refused to pay
  1255. return;
  1256. int base = (ID == 47 ? 2 : 0);
  1257. cb->setObjProperty(id,4,heroID); //add to the visitors
  1258. cb->giveResource(cb->getOwner(heroID),6,-1000); //take 1000 gold
  1259. cb->changePrimSkill(heroID, base + which-1, +1); //give appropriate skill
  1260. }
  1261. bool CArmedInstance::needsLastStack() const
  1262. {
  1263. return false;
  1264. }
  1265. int CArmedInstance::getArmyStrength() const
  1266. {
  1267. int ret = 0;
  1268. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  1269. ret += VLC->creh->creatures[i->second.first].AIValue * i->second.second;
  1270. return ret;
  1271. }
  1272. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1273. {
  1274. int action = takenAction(h);
  1275. switch( action ) //decide what we do...
  1276. {
  1277. case -2: //fight
  1278. fight(h);
  1279. break;
  1280. case -1: //flee
  1281. {
  1282. flee(h);
  1283. break;
  1284. }
  1285. case 0: //join for free
  1286. {
  1287. BlockingDialog ynd(true,false);
  1288. ynd.player = h->tempOwner;
  1289. ynd.text << std::pair<ui8,ui32>(11,86);
  1290. ynd.text.replacements.push_back(VLC->creh->creatures[subID].namePl);
  1291. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,0,_1));
  1292. break;
  1293. }
  1294. default: //join for gold
  1295. {
  1296. assert(action > 0);
  1297. //ask if player agrees to pay gold
  1298. BlockingDialog ynd(true,false);
  1299. ynd.player = h->tempOwner;
  1300. std::string tmp = VLC->generaltexth->advobtxt[90];
  1301. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(army.slots.find(0)->second.second));
  1302. boost::algorithm::replace_first(tmp,"%d",boost::lexical_cast<std::string>(action));
  1303. boost::algorithm::replace_first(tmp,"%s",VLC->creh->creatures[subID].namePl);
  1304. ynd.text << tmp;
  1305. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::joinDecision,this,h,action,_1));
  1306. break;
  1307. }
  1308. }
  1309. }
  1310. void CGCreature::endBattle( BattleResult *result ) const
  1311. {
  1312. if(result->winner==0)
  1313. {
  1314. cb->removeObject(id);
  1315. }
  1316. else
  1317. {
  1318. int killedAmount=0;
  1319. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1320. if(i->first == subID)
  1321. killedAmount += i->second;
  1322. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1323. MetaString ms;
  1324. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1325. pom = 174 + 3*pom + 1;
  1326. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1327. cb->setHoverName(id,&ms);
  1328. }
  1329. }
  1330. void CGCreature::initObj()
  1331. {
  1332. blockVisit = true;
  1333. switch(character)
  1334. {
  1335. case 0:
  1336. character = 0;
  1337. break;
  1338. case 1:
  1339. character = 1 + ran()%7;
  1340. break;
  1341. case 2:
  1342. character = 1 + ran()%10;
  1343. break;
  1344. case 3:
  1345. character = 4 + ran()%7;
  1346. break;
  1347. case 4:
  1348. character = 10;
  1349. break;
  1350. }
  1351. army.slots[0].first = subID;
  1352. si32 &amount = army.slots[0].second;
  1353. CCreature &c = VLC->creh->creatures[subID];
  1354. if(!amount)
  1355. if(c.ammMax == c.ammMin)
  1356. amount = c.ammMax;
  1357. else
  1358. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1359. MetaString ms;
  1360. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1361. pom = 174 + 3*pom + 1;
  1362. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1363. hoverName = toString(ms);
  1364. }
  1365. int CGCreature::takenAction(const CGHeroInstance *h, bool allowJoin) const
  1366. {
  1367. double hlp = h->getTotalStrength() / getArmyStrength();
  1368. if(!character) //compliant creatures will always join
  1369. return 0;
  1370. else if(allowJoin)//test for joining
  1371. {
  1372. int factor;
  1373. if(hlp >= 7)
  1374. factor = 11;
  1375. else if(hlp >= 1)
  1376. factor = 2*(hlp-1);
  1377. else if(hlp >= 0.5)
  1378. factor = -1;
  1379. else if(hlp >= 0.333)
  1380. factor = -2;
  1381. else
  1382. factor = -3;
  1383. int sympathy = 0;
  1384. std::set<ui32> myKindCres; //what creatures are the same kind as we
  1385. myKindCres.insert(subID); //we
  1386. myKindCres.insert(VLC->creh->creatures[subID].upgrades.begin(),VLC->creh->creatures[subID].upgrades.end()); //our upgrades
  1387. for(std::vector<CCreature>::iterator i=VLC->creh->creatures.begin(); i!=VLC->creh->creatures.end(); i++)
  1388. if(vstd::contains(i->upgrades,id)) //it's our base creatures
  1389. myKindCres.insert(i->idNumber);
  1390. int count = 0, //how many creatures of our kind has hero
  1391. totalCount = 0;
  1392. for (std::map<si32,std::pair<ui32,si32> >::const_iterator i = h->army.slots.begin(); i != h->army.slots.end(); i++)
  1393. {
  1394. if(vstd::contains(myKindCres,i->second.first))
  1395. count += i->second.second;
  1396. totalCount += i->second.second;
  1397. }
  1398. if(count*2 > totalCount)
  1399. sympathy++;
  1400. if(count)
  1401. sympathy++;
  1402. int charisma = factor + h->getSecSkillLevel(4) + sympathy;
  1403. if(charisma >= character) //creatures might join...
  1404. {
  1405. if(h->getSecSkillLevel(4) + sympathy + 1 >= character)
  1406. return 0; //join for free
  1407. else if(h->getSecSkillLevel(4) * 2 + sympathy + 1 >= character)
  1408. return VLC->creh->creatures[subID].cost[6] * army.slots.find(0)->second.second; //join for gold
  1409. }
  1410. }
  1411. //we are still here - creatures not joined heroes, test for fleeing
  1412. //TODO: it's provisional formula, should be replaced with original one (or something closer to it)
  1413. //TODO: should be deterministic (will be needed for Vision spell)
  1414. int hlp2 = (int) (hlp - 2)*1000;
  1415. if(!neverFlees
  1416. && hlp2 >= 0
  1417. && rand()%2000 < hlp2
  1418. )
  1419. return -1; //flee
  1420. else
  1421. return -2; //fight
  1422. }
  1423. void CGCreature::fleeDecision(const CGHeroInstance *h, ui32 pursue) const
  1424. {
  1425. if(pursue)
  1426. {
  1427. fight(h);
  1428. }
  1429. else
  1430. {
  1431. cb->removeObject(id);
  1432. }
  1433. }
  1434. void CGCreature::joinDecision(const CGHeroInstance *h, int cost, ui32 accept) const
  1435. {
  1436. if(!accept)
  1437. {
  1438. if(takenAction(h,false) == -1) //they flee
  1439. {
  1440. flee(h);
  1441. }
  1442. else //they fight
  1443. {
  1444. InfoWindow iw;
  1445. iw.player = h->tempOwner;
  1446. iw.text << std::pair<ui8,ui32>(11,87); //Insulted by your refusal of their offer, the monsters attack!
  1447. cb->showInfoDialog(&iw);
  1448. fight(h);
  1449. }
  1450. }
  1451. else //accepted
  1452. {
  1453. if (cb->getResource(h->tempOwner,6) < cost) //player don't have enough gold!
  1454. {
  1455. InfoWindow iw;
  1456. iw.player = h->tempOwner;
  1457. iw.text << std::pair<ui8,ui32>(1,29); //You don't have enough gold
  1458. cb->showInfoDialog(&iw);
  1459. //act as if player refused
  1460. joinDecision(h,cost,true);
  1461. return;
  1462. }
  1463. //take gold
  1464. if(cost)
  1465. cb->giveResource(h->tempOwner,6,-cost);
  1466. int slot = h->army.getSlotFor(subID);
  1467. if(slot >= 0) //there is place
  1468. {
  1469. //add creatures
  1470. SetGarrisons sg;
  1471. sg.garrs[h->id] = h->army;
  1472. if(vstd::contains(h->army.slots,slot)) //add to already present stack
  1473. {
  1474. sg.garrs[h->id].slots[slot].second += army.slots.find(0)->second.second;
  1475. }
  1476. else //add as a new stack
  1477. {
  1478. sg.garrs[h->id].slots[slot] = army.slots.find(0)->second;
  1479. }
  1480. cb->sendAndApply(&sg);
  1481. cb->removeObject(id);
  1482. }
  1483. else
  1484. {
  1485. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id)); //show garrison window and remove ourselves from map when player ends
  1486. }
  1487. }
  1488. }
  1489. void CGCreature::fight( const CGHeroInstance *h ) const
  1490. {
  1491. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1492. }
  1493. void CGCreature::flee( const CGHeroInstance * h ) const
  1494. {
  1495. BlockingDialog ynd(true,false);
  1496. ynd.player = h->tempOwner;
  1497. ynd.text << std::pair<ui8,ui32>(11,91);
  1498. ynd.text.replacements.push_back(VLC->creh->creatures[subID].namePl);
  1499. cb->showBlockingDialog(&ynd,boost::bind(&CGCreature::fleeDecision,this,h,_1));
  1500. }
  1501. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1502. {
  1503. if(subID == 7) //TODO: support for abandoned mine
  1504. return;
  1505. if(h->tempOwner == tempOwner) //we're visiting our mine
  1506. {
  1507. cb->showGarrisonDialog(id,h->id,0);
  1508. return;
  1509. }
  1510. //TODO: check if mine is guarded
  1511. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1512. MetaString ms;
  1513. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1514. cb->setHoverName(id,&ms);
  1515. int vv=1; //amount of resource per turn
  1516. if (subID==0 || subID==2)
  1517. vv++;
  1518. else if (subID==6)
  1519. vv = 1000;
  1520. InfoWindow iw;
  1521. iw.soundID = soundBase::FLAGMINE;
  1522. iw.text << std::pair<ui8,ui32>(10,subID);
  1523. iw.player = h->tempOwner;
  1524. iw.components.push_back(Component(2,subID,vv,-1));
  1525. cb->showInfoDialog(&iw);
  1526. }
  1527. void CGMine::newTurn() const
  1528. {
  1529. if (tempOwner == NEUTRAL_PLAYER)
  1530. return;
  1531. int vv = 1;
  1532. if (subID==0 || subID==2)
  1533. vv++;
  1534. else if (subID==6)
  1535. vv = 1000;
  1536. cb->giveResource(tempOwner,subID,vv);
  1537. }
  1538. void CGMine::initObj()
  1539. {
  1540. MetaString ms;
  1541. ms << std::pair<ui8,ui32>(9,subID);
  1542. if(tempOwner >= PLAYER_LIMIT)
  1543. tempOwner = NEUTRAL_PLAYER;
  1544. else
  1545. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1546. hoverName = toString(ms);
  1547. }
  1548. void CGResource::initObj()
  1549. {
  1550. blockVisit = true;
  1551. hoverName = VLC->generaltexth->restypes[subID];
  1552. if(!amount)
  1553. {
  1554. switch(subID)
  1555. {
  1556. case 6:
  1557. amount = 500 + (rand()%6)*100;
  1558. break;
  1559. case 0: case 2:
  1560. amount = 6 + (rand()%5);
  1561. break;
  1562. default:
  1563. amount = 3 + (rand()%3);
  1564. break;
  1565. }
  1566. }
  1567. }
  1568. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1569. {
  1570. if(army.slots.size())
  1571. {
  1572. if(message.size())
  1573. {
  1574. BlockingDialog ynd(true,false);
  1575. ynd.player = h->getOwner();
  1576. ynd.text << message;
  1577. cb->showBlockingDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1578. }
  1579. else
  1580. {
  1581. fightForRes(1,h);
  1582. }
  1583. }
  1584. else
  1585. {
  1586. if(message.length())
  1587. {
  1588. InfoWindow iw;
  1589. iw.player = h->tempOwner;
  1590. iw.text << message;
  1591. cb->showInfoDialog(&iw);
  1592. }
  1593. collectRes(h->getOwner());
  1594. }
  1595. }
  1596. void CGResource::collectRes( int player ) const
  1597. {
  1598. cb->giveResource(player,subID,amount);
  1599. ShowInInfobox sii;
  1600. sii.player = player;
  1601. sii.c = Component(2,subID,amount,0);
  1602. sii.text << std::pair<ui8,ui32>(11,113);
  1603. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1604. cb->showCompInfo(&sii);
  1605. cb->removeObject(id);
  1606. }
  1607. void CGResource::fightForRes(ui32 agreed, const CGHeroInstance *h) const
  1608. {
  1609. if(agreed)
  1610. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1611. }
  1612. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1613. {
  1614. if(result->winner == 0) //attacker won
  1615. collectRes(h->getOwner());
  1616. }
  1617. void CGVisitableOPW::newTurn() const
  1618. {
  1619. if (cb->getDate(1)==1) //first day of week
  1620. {
  1621. cb->setObjProperty(id,5,false);
  1622. MetaString ms; //set text to "not visited"
  1623. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1624. cb->setHoverName(id,&ms);
  1625. }
  1626. }
  1627. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1628. {
  1629. int mid, sound = 0;
  1630. switch (ID)
  1631. {
  1632. case 55: //mystical garden
  1633. sound = soundBase::experience;
  1634. mid = 92;
  1635. break;
  1636. case 112://windmill
  1637. sound = soundBase::GENIE;
  1638. mid = 170;
  1639. break;
  1640. case 109://waterwheel
  1641. sound = soundBase::GENIE;
  1642. mid = 164;
  1643. break;
  1644. }
  1645. if (visited)
  1646. {
  1647. if (ID!=112)
  1648. mid++;
  1649. else
  1650. mid--;
  1651. InfoWindow iw;
  1652. iw.soundID = sound;
  1653. iw.player = h->tempOwner;
  1654. iw.text << std::pair<ui8,ui32>(11,mid);
  1655. cb->showInfoDialog(&iw);
  1656. }
  1657. else
  1658. {
  1659. int type, sub, val;
  1660. type = 2;
  1661. switch (ID)
  1662. {
  1663. case 55:
  1664. if (rand()%2)
  1665. {
  1666. sub = 5;
  1667. val = 5;
  1668. }
  1669. else
  1670. {
  1671. sub = 6;
  1672. val = 500;
  1673. }
  1674. break;
  1675. case 112:
  1676. mid = 170;
  1677. sub = (rand() % 5) + 1;
  1678. val = (rand() % 4) + 3;
  1679. break;
  1680. case 109:
  1681. mid = 164;
  1682. sub = 6;
  1683. if(cb->getDate(0)<8)
  1684. val = 500;
  1685. else
  1686. val = 1000;
  1687. }
  1688. cb->giveResource(h->tempOwner,sub,val);
  1689. InfoWindow iw;
  1690. iw.soundID = sound;
  1691. iw.player = h->tempOwner;
  1692. iw.components.push_back(Component(type,sub,val,0));
  1693. iw.text << std::pair<ui8,ui32>(11,mid);
  1694. cb->showInfoDialog(&iw);
  1695. cb->setObjProperty(id,5,true);
  1696. MetaString ms; //set text to "visited"
  1697. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1698. cb->setHoverName(id,&ms);
  1699. }
  1700. }
  1701. void CGVisitableOPW::setPropertyDer( ui8 what, ui32 val )
  1702. {
  1703. if(what == 5)
  1704. visited = val;
  1705. }
  1706. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1707. {
  1708. int destinationid=-1;
  1709. switch(ID)
  1710. {
  1711. case 43: //one way - find correspong exit monolith
  1712. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1713. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1714. else
  1715. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1716. break;
  1717. case 45: //two way monolith - pick any other one
  1718. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1719. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1720. else
  1721. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1722. break;
  1723. case 103: //find nearest subterranean gate on the other level
  1724. {
  1725. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1726. for(int i=0; i<objs[103][0].size(); i++)
  1727. {
  1728. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1729. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1730. if(hlp<best.second)
  1731. {
  1732. best.first = objs[103][0][i];
  1733. best.second = hlp;
  1734. }
  1735. }
  1736. if(best.first<0)
  1737. return;
  1738. else
  1739. destinationid = best.first;
  1740. break;
  1741. }
  1742. }
  1743. if(destinationid < 0)
  1744. {
  1745. tlog2 << "Cannot find exit... :( \n";
  1746. return;
  1747. }
  1748. cb->moveHero(h->id,
  1749. (ID!=103)
  1750. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1751. : (cb->getObj(destinationid)->pos),
  1752. true);
  1753. }
  1754. void CGTeleport::initObj()
  1755. {
  1756. objs[ID][subID].push_back(id);
  1757. }
  1758. void CGArtifact::initObj()
  1759. {
  1760. blockVisit = true;
  1761. if(ID == 5)
  1762. hoverName = VLC->arth->artifacts[subID].Name();
  1763. }
  1764. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1765. {
  1766. if(!army.slots.size())
  1767. {
  1768. if(ID == 5)
  1769. {
  1770. InfoWindow iw;
  1771. iw.player = h->tempOwner;
  1772. iw.components.push_back(Component(4,subID,0,0));
  1773. if(message.length())
  1774. iw.text << message;
  1775. else
  1776. iw.text << std::pair<ui8,ui32>(12,subID);
  1777. cb->showInfoDialog(&iw);
  1778. }
  1779. pick(h);
  1780. }
  1781. else
  1782. {
  1783. if(message.size())
  1784. {
  1785. BlockingDialog ynd(true,false);
  1786. ynd.player = h->getOwner();
  1787. ynd.text << message;
  1788. cb->showBlockingDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1789. }
  1790. else
  1791. {
  1792. fightForArt(0,h);
  1793. }
  1794. }
  1795. }
  1796. void CGArtifact::pick(const CGHeroInstance * h) const
  1797. {
  1798. if(ID == 5) //Artifact
  1799. {
  1800. cb->giveHeroArtifact(subID,h->id,-2);
  1801. }
  1802. else if(ID == 93) // Spell scroll
  1803. {
  1804. //TODO: support for the spell scroll
  1805. }
  1806. cb->removeObject(id);
  1807. }
  1808. void CGArtifact::fightForArt( ui32 agreed, const CGHeroInstance *h ) const
  1809. {
  1810. if(agreed)
  1811. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1812. }
  1813. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1814. {
  1815. if(result->winner == 0) //attacker won
  1816. pick(h);
  1817. }
  1818. void CGPickable::initObj()
  1819. {
  1820. blockVisit = true;
  1821. switch(ID)
  1822. {
  1823. case 12: //campfire
  1824. val2 = (ran()%3) + 4; //4 - 6
  1825. val1 = val2 * 100;
  1826. type = ran()%6; //given resource
  1827. break;
  1828. case 101: //treasure chest
  1829. {
  1830. int hlp = ran()%100;
  1831. if(hlp >= 95)
  1832. {
  1833. type = 1;
  1834. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1835. return;
  1836. }
  1837. else if (hlp >= 65)
  1838. {
  1839. val1 = 2000;
  1840. }
  1841. else if(hlp >= 33)
  1842. {
  1843. val1 = 1500;
  1844. }
  1845. else
  1846. {
  1847. val1 = 1000;
  1848. }
  1849. val2 = val1 - 500;
  1850. type = 0;
  1851. break;
  1852. }
  1853. }
  1854. }
  1855. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1856. {
  1857. switch(ID)
  1858. {
  1859. case 12: //campfire
  1860. {
  1861. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1862. cb->giveResource(h->tempOwner,6,val1);//gold
  1863. InfoWindow iw;
  1864. iw.soundID = soundBase::experience;
  1865. iw.player = h->tempOwner;
  1866. iw.components.push_back(Component(2,6,val1,0));
  1867. iw.components.push_back(Component(2,type,val2,0));
  1868. iw.text << std::pair<ui8,ui32>(11,23);
  1869. cb->showInfoDialog(&iw);
  1870. break;
  1871. }
  1872. case 101: //treasure chest
  1873. {
  1874. if (subID) //not OH3 treasure chest
  1875. {
  1876. tlog2 << "Not supported WoG treasure chest!\n";
  1877. return;
  1878. }
  1879. if(type) //there is an artifact
  1880. {
  1881. cb->giveHeroArtifact(val1,h->id,-2);
  1882. InfoWindow iw;
  1883. iw.soundID = soundBase::treasure;
  1884. iw.player = h->tempOwner;
  1885. iw.components.push_back(Component(4,val1,1,0));
  1886. iw.text << std::pair<ui8,ui32>(11,145);
  1887. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1888. cb->showInfoDialog(&iw);
  1889. break;
  1890. }
  1891. else
  1892. {
  1893. BlockingDialog sd(false,true);
  1894. sd.player = h->tempOwner;
  1895. sd.text << std::pair<ui8,ui32>(11,146);
  1896. sd.components.push_back(Component(2,6,val1,0));
  1897. sd.components.push_back(Component(5,0,val2,0));
  1898. sd.soundID = soundBase::chest;
  1899. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1900. cb->showBlockingDialog(&sd,fun);
  1901. return;
  1902. }
  1903. }
  1904. }
  1905. cb->removeObject(id);
  1906. }
  1907. void CGPickable::chosen( int which, int heroID ) const
  1908. {
  1909. switch(which)
  1910. {
  1911. case 1: //player pick gold
  1912. cb->giveResource(cb->getOwner(heroID),6,val1);
  1913. break;
  1914. case 2: //player pick exp
  1915. cb->changePrimSkill(heroID, 4, val2);
  1916. break;
  1917. default:
  1918. throw std::string("Unhandled treasure choice");
  1919. }
  1920. cb->removeObject(id);
  1921. }
  1922. void CGWitchHut::initObj()
  1923. {
  1924. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1925. }
  1926. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1927. {
  1928. InfoWindow iw;
  1929. iw.soundID = soundBase::gazebo;
  1930. iw.player = h->getOwner();
  1931. if(!hasVisited(h->tempOwner))
  1932. cb->setObjProperty(id,10,h->tempOwner);
  1933. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1934. {
  1935. iw.text << std::pair<ui8,ui32>(11,172);
  1936. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1937. }
  1938. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1939. {
  1940. iw.text << std::pair<ui8,ui32>(11,173);
  1941. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1942. }
  1943. else //give sec skill
  1944. {
  1945. iw.components.push_back(Component(1, ability, 1, 0));
  1946. iw.text << std::pair<ui8,ui32>(11,171);
  1947. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1948. cb->changeSecSkill(h->id,ability,1,true);
  1949. }
  1950. cb->showInfoDialog(&iw);
  1951. }
  1952. const std::string & CGWitchHut::getHoverText() const
  1953. {
  1954. hoverName = VLC->generaltexth->names[ID];
  1955. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  1956. {
  1957. hoverName += "\n" + VLC->generaltexth->allTexts[356]; // + (learn %s)
  1958. boost::algorithm::replace_first(hoverName,"%s",VLC->generaltexth->skillName[ability]);
  1959. if(cb->getSelectedHero(cb->getCurrentPlayer())->getSecSkillLevel(ability)) //hero knows that ability
  1960. hoverName += "\n\n" + VLC->generaltexth->allTexts[357]; // (Already learned)
  1961. }
  1962. return hoverName;
  1963. }
  1964. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1965. {
  1966. }
  1967. void CGDwelling::initObj()
  1968. {
  1969. }
  1970. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  1971. {
  1972. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  1973. int messageID, bonusType, bonusVal;
  1974. int bonusMove = 0, sound = -1;
  1975. InfoWindow iw;
  1976. iw.player = h->tempOwner;
  1977. GiveBonus gbonus;
  1978. gbonus.hid = h->id;
  1979. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  1980. gbonus.bonus.source = HeroBonus::OBJECT;
  1981. gbonus.bonus.id = ID;
  1982. switch(ID)
  1983. {
  1984. case 14: //swan pond
  1985. messageID = 29;
  1986. sound = soundBase::LUCK;
  1987. gbonus.bonus.type = HeroBonus::LUCK;
  1988. gbonus.bonus.val = 2;
  1989. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  1990. bonusMove = -h->movement;
  1991. break;
  1992. case 28: //Faerie Ring
  1993. messageID = 49;
  1994. sound = soundBase::LUCK;
  1995. gbonus.bonus.type = HeroBonus::LUCK;
  1996. gbonus.bonus.val = 1;
  1997. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  1998. break;
  1999. case 30: //fountain of fortune
  2000. messageID = 55;
  2001. sound = soundBase::LUCK;
  2002. gbonus.bonus.type = HeroBonus::LUCK;
  2003. gbonus.bonus.val = rand()%5 - 1;
  2004. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  2005. gbonus.bdescr.replacements.push_back((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  2006. break;
  2007. case 38: //idol of fortune
  2008. messageID = 62;
  2009. iw.soundID = soundBase::experience;
  2010. if(cb->getDate(1) == 7) //7th day of week
  2011. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2012. else
  2013. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  2014. gbonus.bonus.val = 1;
  2015. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  2016. break;
  2017. case 64: //Rally Flag
  2018. sound = soundBase::MORALE;
  2019. messageID = 111;
  2020. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  2021. gbonus.bonus.val = 1;
  2022. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  2023. bonusMove = 400;
  2024. break;
  2025. case 56: //oasis
  2026. messageID = 95;
  2027. gbonus.bonus.type = HeroBonus::MORALE;
  2028. gbonus.bonus.val = 1;
  2029. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  2030. bonusMove = 800;
  2031. break;
  2032. case 96: //temple
  2033. messageID = 140;
  2034. iw.soundID = soundBase::temple;
  2035. gbonus.bonus.type = HeroBonus::MORALE;
  2036. if(cb->getDate(1)==7) //sunday
  2037. {
  2038. gbonus.bonus.val = 2;
  2039. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  2040. }
  2041. else
  2042. {
  2043. gbonus.bonus.val = 1;
  2044. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  2045. }
  2046. break;
  2047. case 110://Watering Hole
  2048. sound = soundBase::MORALE;
  2049. messageID = 166;
  2050. gbonus.bonus.type = HeroBonus::MORALE;
  2051. gbonus.bonus.val = 1;
  2052. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  2053. bonusMove = 400;
  2054. break;
  2055. case 31: //Fountain of Youth
  2056. sound = soundBase::MORALE;
  2057. messageID = 57;
  2058. gbonus.bonus.type = HeroBonus::MORALE;
  2059. gbonus.bonus.val = 1;
  2060. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  2061. bonusMove = 400;
  2062. break;
  2063. }
  2064. if(visited)
  2065. {
  2066. if(ID==64 || ID==96 || ID==56)
  2067. messageID--;
  2068. else
  2069. messageID++;
  2070. }
  2071. else
  2072. {
  2073. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2074. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  2075. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  2076. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  2077. cb->giveHeroBonus(&gbonus);
  2078. if(bonusMove) //swan pond - take all move points
  2079. {
  2080. SetMovePoints smp;
  2081. smp.hid = h->id;
  2082. smp.val = h->movement + bonusMove;
  2083. cb->setMovePoints(&smp);
  2084. }
  2085. }
  2086. iw.soundID = sound;
  2087. iw.text << std::pair<ui8,ui32>(11,messageID);
  2088. cb->showInfoDialog(&iw);
  2089. }
  2090. const std::string & CGBonusingObject::getHoverText() const
  2091. {
  2092. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2093. hoverName = VLC->generaltexth->names[ID];
  2094. if(h)
  2095. {
  2096. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2097. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2098. else
  2099. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2100. }
  2101. return hoverName;
  2102. }
  2103. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  2104. {
  2105. int message;
  2106. InfoWindow iw;
  2107. iw.soundID = soundBase::faerie;
  2108. iw.player = h->tempOwner;
  2109. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  2110. {
  2111. message = 78;
  2112. }
  2113. else if(h->mana < h->manaLimit())
  2114. {
  2115. GiveBonus gbonus;
  2116. gbonus.bonus.type = HeroBonus::NONE;
  2117. gbonus.hid = h->id;
  2118. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  2119. gbonus.bonus.source = HeroBonus::OBJECT;
  2120. gbonus.bonus.id = ID;
  2121. cb->giveHeroBonus(&gbonus);
  2122. cb->setManaPoints(h->id,h->manaLimit());
  2123. message = 77;
  2124. }
  2125. else
  2126. {
  2127. message = 79;
  2128. }
  2129. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  2130. cb->showInfoDialog(&iw);
  2131. }
  2132. const std::string & CGMagicWell::getHoverText() const
  2133. {
  2134. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2135. hoverName = VLC->generaltexth->names[ID];
  2136. if(h)
  2137. {
  2138. if(!h->getBonus(HeroBonus::OBJECT,ID))
  2139. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  2140. else
  2141. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  2142. }
  2143. return hoverName;
  2144. }
  2145. void CGEvent::onHeroVisit( const CGHeroInstance * h ) const
  2146. {
  2147. if(!(availableFor & (1 << h->tempOwner)))
  2148. return;
  2149. if(cb->getPlayerSettings(h->tempOwner)->human)
  2150. {
  2151. if(humanActivate)
  2152. activated(h);
  2153. }
  2154. else if(computerActivate)
  2155. activated(h);
  2156. }
  2157. void CGEvent::endBattle( const CGHeroInstance *h, BattleResult *result ) const
  2158. {
  2159. if(result->winner)
  2160. return;
  2161. giveContents(h,true);
  2162. }
  2163. void CGEvent::activated( const CGHeroInstance * h ) const
  2164. {
  2165. if(army)
  2166. {
  2167. InfoWindow iw;
  2168. iw.player = h->tempOwner;
  2169. iw.text << message;
  2170. cb->showInfoDialog(&iw);
  2171. cb->startBattleI(h->id,army,pos,boost::bind(&CGEvent::endBattle,this,h,_1));
  2172. }
  2173. else
  2174. {
  2175. giveContents(h,false);
  2176. }
  2177. }
  2178. void CGEvent::giveContents( const CGHeroInstance *h, bool afterBattle ) const
  2179. {
  2180. InfoWindow iw;
  2181. iw.player = h->getOwner();
  2182. bool changesPrimSkill = false;
  2183. for (int i = 0; i < primskills.size(); i++)
  2184. {
  2185. if(primskills[i])
  2186. {
  2187. changesPrimSkill = true;
  2188. break;
  2189. }
  2190. }
  2191. if(gainedExp || changesPrimSkill || abilities.size())
  2192. {
  2193. getText(iw,afterBattle,175,h);
  2194. if(gainedExp)
  2195. iw.components.push_back(Component(Component::EXPERIENCE,0,gainedExp,0));
  2196. for(int i=0; i<primskills.size(); i++)
  2197. if(primskills[i])
  2198. iw.components.push_back(Component(Component::PRIM_SKILL,i,primskills[i],0));
  2199. for(int i=0; i<abilities.size(); i++)
  2200. iw.components.push_back(Component(Component::SEC_SKILL,abilities[i],abilityLevels[i],0));
  2201. cb->showInfoDialog(&iw);
  2202. //give exp
  2203. if(gainedExp)
  2204. cb->changePrimSkill(h->id,5,gainedExp,false);
  2205. //give prim skills
  2206. for(int i=0; i<primskills.size(); i++)
  2207. if(primskills[i])
  2208. cb->changePrimSkill(h->id,i,primskills[i],false);
  2209. //give sec skills
  2210. for(int i=0; i<abilities.size(); i++)
  2211. {
  2212. int curLev = h->getSecSkillLevel(abilities[i]);
  2213. if(curLev && curLev < abilityLevels[i]
  2214. || h->secSkills.size() < SKILL_PER_HERO )
  2215. {
  2216. cb->changeSecSkill(h->id,abilities[i],abilityLevels[i],true);
  2217. }
  2218. }
  2219. }
  2220. if(spells.size())
  2221. {
  2222. std::set<ui32> spellsToGive;
  2223. iw.components.clear();
  2224. for(int i=0; i<spells.size(); i++)
  2225. {
  2226. iw.components.push_back(Component(Component::SPELL,spells[i],0,0));
  2227. spellsToGive.insert(spells[i]);
  2228. }
  2229. if(spellsToGive.size())
  2230. {
  2231. cb->changeSpells(h->id,true,spellsToGive);
  2232. cb->showInfoDialog(&iw);
  2233. }
  2234. }
  2235. if(manaDiff)
  2236. {
  2237. getText(iw,afterBattle,manaDiff,176,177,h);
  2238. iw.components.push_back(Component(Component::PRIM_SKILL,5,manaDiff,0));
  2239. cb->showInfoDialog(&iw);
  2240. cb->setManaPoints(h->id, h->mana + manaDiff);
  2241. }
  2242. if(moraleDiff)
  2243. {
  2244. getText(iw,afterBattle,moraleDiff,178,179,h);
  2245. iw.components.push_back(Component(Component::MORALE,0,moraleDiff,0));
  2246. cb->showInfoDialog(&iw);
  2247. GiveBonus gb;
  2248. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,moraleDiff,id,"");
  2249. gb.hid = h->id;
  2250. cb->giveHeroBonus(&gb);
  2251. }
  2252. if(luckDiff)
  2253. {
  2254. getText(iw,afterBattle,luckDiff,180,181,h);
  2255. iw.components.push_back(Component(Component::LUCK,0,luckDiff,0));
  2256. cb->showInfoDialog(&iw);
  2257. GiveBonus gb;
  2258. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::LUCK,HeroBonus::OBJECT,luckDiff,id,"");
  2259. gb.hid = h->id;
  2260. cb->giveHeroBonus(&gb);
  2261. }
  2262. iw.components.clear();
  2263. iw.text.clear();
  2264. for(int i=0; i<resources.size(); i++)
  2265. {
  2266. if(resources[i] < 0)
  2267. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2268. }
  2269. if(iw.components.size())
  2270. {
  2271. getText(iw,afterBattle,182,h);
  2272. cb->showInfoDialog(&iw);
  2273. }
  2274. iw.components.clear();
  2275. iw.text.clear();
  2276. for(int i=0; i<resources.size(); i++)
  2277. {
  2278. if(resources[i] > 0)
  2279. iw.components.push_back(Component(Component::RESOURCE,i,resources[i],0));
  2280. }
  2281. if(iw.components.size())
  2282. {
  2283. getText(iw,afterBattle,183,h);
  2284. cb->showInfoDialog(&iw);
  2285. }
  2286. iw.components.clear();
  2287. for(int i=0; i<artifacts.size(); i++)
  2288. {
  2289. iw.components.push_back(Component(Component::ARTIFACT,artifacts[i],0,0));
  2290. }
  2291. if(iw.components.size())
  2292. {
  2293. cb->showInfoDialog(&iw);
  2294. }
  2295. for(int i=0; i<resources.size(); i++)
  2296. if(resources[i])
  2297. cb->giveResource(h->getOwner(),i,resources[i]);
  2298. for(int i=0; i<artifacts.size(); i++)
  2299. cb->giveHeroArtifact(artifacts[i],h->id,-2);
  2300. //show dialog with given creatures
  2301. iw.components.clear();
  2302. iw.text.clear();
  2303. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i = creatures.slots.begin(); i != creatures.slots.end(); i++)
  2304. {
  2305. iw.components.push_back(Component(Component::CREATURE,i->second.first,i->second.second,0));
  2306. }
  2307. if(iw.components.size())
  2308. {
  2309. if(afterBattle)
  2310. {
  2311. if(iw.components.front().val == 1)
  2312. {
  2313. iw.text.addTxt(MetaString::ADVOB_TXT,185);//A %s joins %s's army.
  2314. iw.text.replacements.push_back(VLC->creh->creatures[iw.components.front().subtype].nameSing);
  2315. }
  2316. else
  2317. {
  2318. iw.text.addTxt(MetaString::ADVOB_TXT,186);//%s join %s's army.
  2319. iw.text.replacements.push_back(VLC->creh->creatures[iw.components.front().subtype].namePl);
  2320. }
  2321. iw.text.replacements.push_back(h->name);
  2322. }
  2323. else
  2324. {
  2325. iw.text << message;
  2326. afterBattle = true;
  2327. }
  2328. cb->showInfoDialog(&iw);
  2329. }
  2330. //check if creatures can be moved to hero army
  2331. CCreatureSet heroArmy = h->army;
  2332. CCreatureSet ourArmy = creatures;
  2333. while(ourArmy)
  2334. {
  2335. int slot = heroArmy.getSlotFor(ourArmy.slots.begin()->second.first);
  2336. if(slot < 0)
  2337. break;
  2338. heroArmy.slots[slot].first = ourArmy.slots.begin()->second.first;
  2339. heroArmy.slots[slot].second += ourArmy.slots.begin()->second.second;
  2340. ourArmy.slots.erase(ourArmy.slots.begin());
  2341. }
  2342. if(!ourArmy) //all creatures can be moved to hero army - do that
  2343. {
  2344. SetGarrisons sg;
  2345. sg.garrs[h->id] = heroArmy;
  2346. cb->sendAndApply(&sg);
  2347. }
  2348. else //show garrison window and let player pick creatures
  2349. {
  2350. SetGarrisons sg;
  2351. sg.garrs[id] = creatures;
  2352. cb->sendAndApply(&sg);
  2353. if(removeAfterVisit)
  2354. cb->showGarrisonDialog(id,h->id,boost::bind(&IGameCallback::removeObject,cb,id));
  2355. else
  2356. cb->showGarrisonDialog(id,h->id,0);
  2357. return;
  2358. }
  2359. if(!afterBattle)
  2360. {
  2361. iw.text << message;
  2362. cb->showInfoDialog(&iw);
  2363. }
  2364. if(removeAfterVisit)
  2365. cb->removeObject(id);
  2366. }
  2367. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int text, const CGHeroInstance * h ) const
  2368. {
  2369. if(afterBattle)
  2370. {
  2371. iw.text.addTxt(MetaString::ADVOB_TXT,text);//%s has lost treasure.
  2372. iw.text.replacements.push_back(h->name);
  2373. }
  2374. else
  2375. {
  2376. iw.text << message;
  2377. afterBattle = true;
  2378. }
  2379. }
  2380. void CGEvent::getText( InfoWindow &iw, bool &afterBattle, int val, int positive, int negative, const CGHeroInstance * h ) const
  2381. {
  2382. iw.components.clear();
  2383. iw.text.clear();
  2384. if(afterBattle)
  2385. {
  2386. iw.text.addTxt(MetaString::ADVOB_TXT,val < 0 ? negative : positive); //%s's luck takes a turn for the worse / %s's luck increases
  2387. iw.text.replacements.push_back(h->name);
  2388. }
  2389. else
  2390. {
  2391. iw.text << message;
  2392. afterBattle = true;
  2393. }
  2394. }
  2395. void CGObservatory::onHeroVisit( const CGHeroInstance * h ) const
  2396. {
  2397. InfoWindow iw;
  2398. iw.soundID = soundBase::LIGHTHOUSE;
  2399. iw.player = h->tempOwner;
  2400. iw.text.addTxt(MetaString::ADVOB_TXT,98 + (ID==60));
  2401. cb->showInfoDialog(&iw);
  2402. FoWChange fw;
  2403. fw.player = h->tempOwner;
  2404. fw.mode = 1;
  2405. cb->getTilesInRange(fw.tiles,pos,20,h->tempOwner,1);
  2406. cb->sendAndApply(&fw);
  2407. }
  2408. void CGShrine::onHeroVisit( const CGHeroInstance * h ) const
  2409. {
  2410. if(spell == 255)
  2411. {
  2412. tlog1 << "Not initialized shrine visited!\n";
  2413. return;
  2414. }
  2415. if(!hasVisited(h->tempOwner))
  2416. cb->setObjProperty(id,10,h->tempOwner);
  2417. InfoWindow iw;
  2418. iw.soundID = soundBase::temple;
  2419. iw.player = h->getOwner();
  2420. iw.text.addTxt(MetaString::ADVOB_TXT,127 + ID - 88);
  2421. iw.text.addTxt(MetaString::SPELL_NAME,spell);
  2422. iw.text << ".";
  2423. if(!h->getArt(17)) //no spellbook
  2424. {
  2425. iw.text.addTxt(MetaString::ADVOB_TXT,131);
  2426. }
  2427. else if(ID == 90 && !h->getSecSkillLevel(7)) //it's third level spell and hero doesn't have wisdom
  2428. {
  2429. iw.text.addTxt(MetaString::ADVOB_TXT,130);
  2430. }
  2431. else if(vstd::contains(h->spells,spell))//hero already knows the spell
  2432. {
  2433. iw.text.addTxt(MetaString::ADVOB_TXT,174);
  2434. }
  2435. else //give spell
  2436. {
  2437. std::set<ui32> spells;
  2438. spells.insert(spell);
  2439. cb->changeSpells(h->id,true,spells);
  2440. iw.components.push_back(Component(Component::SPELL,spell,0,0));
  2441. }
  2442. cb->showInfoDialog(&iw);
  2443. }
  2444. void CGShrine::initObj()
  2445. {
  2446. if(spell == 255) //spell not set
  2447. {
  2448. int level = ID-87;
  2449. std::vector<ui32> possibilities;
  2450. //add all allowed spells of wanted level
  2451. for(int i=0; i<SPELLS_QUANTITY; i++)
  2452. {
  2453. if(VLC->spellh->spells[i].level == level
  2454. && cb->isAllowed(0,VLC->spellh->spells[i].id))
  2455. {
  2456. possibilities.push_back(VLC->spellh->spells[i].id);
  2457. }
  2458. }
  2459. if(!possibilities.size())
  2460. {
  2461. tlog1 << "Error: cannot init shrine, no allowed spells!\n";
  2462. return;
  2463. }
  2464. spell = possibilities[ran() % possibilities.size()];
  2465. }
  2466. }
  2467. const std::string & CGShrine::getHoverText() const
  2468. {
  2469. hoverName = VLC->generaltexth->names[ID];
  2470. if(hasVisited(cb->getCurrentPlayer())) //TODO: use local player, not current
  2471. {
  2472. hoverName += "\n" + VLC->generaltexth->allTexts[355]; // + (learn %s)
  2473. boost::algorithm::replace_first(hoverName,"%s",VLC->spellh->spells[spell].name);
  2474. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  2475. if(h && vstd::contains(h->spells,spell)) //hero knows that ability
  2476. hoverName += "\n\n" + VLC->generaltexth->allTexts[354]; // (Already learned)
  2477. }
  2478. return hoverName;
  2479. }
  2480. void CGSignBottle::initObj()
  2481. {
  2482. //if no text is set than we pick random from the predefined ones
  2483. if(!message.size())
  2484. message = VLC->generaltexth->randsign[ran()%VLC->generaltexth->randsign.size()];
  2485. }
  2486. void CGSignBottle::onHeroVisit( const CGHeroInstance * h ) const
  2487. {
  2488. InfoWindow iw;
  2489. iw.soundID = soundBase::STORE;
  2490. iw.player = h->getOwner();
  2491. iw.text << message;
  2492. cb->showInfoDialog(&iw);
  2493. }
  2494. void CGScholar::giveAnyBonus( const CGHeroInstance * h ) const
  2495. {
  2496. }
  2497. void CGScholar::onHeroVisit( const CGHeroInstance * h ) const
  2498. {
  2499. int type = bonusType;
  2500. int bid = bonusID;
  2501. //check if the bonus if applicable, if not - give primary skill (always possible)
  2502. int ssl = h->getSecSkillLevel(bid); //current sec skill level, used if bonusType == 1
  2503. if((type == 1
  2504. && ((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)
  2505. || (type == 2 && (!h->getArt(17) || vstd::contains(h->spells,bid)))) //hero doesn't have a spellbook or already knows the spell
  2506. {
  2507. type = 0;
  2508. bid = ran() % PRIMARY_SKILLS;
  2509. }
  2510. InfoWindow iw;
  2511. iw.soundID = soundBase::gazebo;
  2512. iw.player = h->getOwner();
  2513. iw.text.addTxt(MetaString::ADVOB_TXT,115);
  2514. switch (type)
  2515. {
  2516. case 0:
  2517. cb->changePrimSkill(h->id,bid,+1);
  2518. iw.components.push_back(Component(Component::PRIM_SKILL,bid,1,0));
  2519. break;
  2520. case 1:
  2521. {
  2522. cb->changeSecSkill(h->id,bid,ssl+1);
  2523. iw.components.push_back(Component(Component::SEC_SKILL,bid,ssl+1,0));
  2524. }
  2525. break;
  2526. case 2:
  2527. {
  2528. std::set<ui32> hlp;
  2529. hlp.insert(bid);
  2530. cb->changeSpells(h->id,true,hlp);
  2531. iw.components.push_back(Component(Component::SPELL,bid,0,0));
  2532. }
  2533. break;
  2534. default:
  2535. tlog1 << "Error: wrong bonustype (" << (int)type << ") for Scholar!\n";
  2536. return;
  2537. }
  2538. cb->showInfoDialog(&iw);
  2539. cb->removeObject(id);
  2540. }
  2541. void CGScholar::initObj()
  2542. {
  2543. blockVisit = true;
  2544. if(bonusType == 255)
  2545. {
  2546. bonusType = ran()%3;
  2547. switch(bonusType)
  2548. {
  2549. case 0:
  2550. bonusID = ran() % PRIMARY_SKILLS;
  2551. break;
  2552. case 1:
  2553. bonusID = ran() % SKILL_QUANTITY;
  2554. break;
  2555. case 2:
  2556. bonusID = ran() % SPELLS_QUANTITY;
  2557. break;
  2558. }
  2559. }
  2560. }
  2561. void CGOnceVisitable::onHeroVisit( const CGHeroInstance * h ) const
  2562. {
  2563. int sound = 0;
  2564. int txtid = -1;
  2565. switch(ID)
  2566. {
  2567. case 22: //Corpse
  2568. txtid = 37;
  2569. sound = soundBase::MYSTERY;
  2570. break;
  2571. case 39: //Lean To
  2572. sound = soundBase::GENIE;
  2573. txtid = 64;
  2574. break;
  2575. case 105://Wagon
  2576. sound = soundBase::GENIE;
  2577. txtid = 154;
  2578. break;
  2579. case 108:
  2580. break;
  2581. default:
  2582. tlog1 << "Error: Unknown object (" << ID <<") treated as CGOnceVisitable!\n";
  2583. return;
  2584. }
  2585. if(ID == 108)//Warrior's Tomb
  2586. {
  2587. //ask if player wants to search the Tomb
  2588. BlockingDialog bd(true, false);
  2589. sound = soundBase::GRAVEYARD;
  2590. bd.player = h->getOwner();
  2591. bd.text.addTxt(MetaString::ADVOB_TXT,161);
  2592. cb->showBlockingDialog(&bd,boost::bind(&CGOnceVisitable::searchTomb,this,h,_1));
  2593. return;
  2594. }
  2595. InfoWindow iw;
  2596. iw.soundID = sound;
  2597. iw.player = h->getOwner();
  2598. if(players.size()) //we have been already visited...
  2599. {
  2600. txtid++;
  2601. if(ID == 105) //wagon has extra text (for finding art) we need to ommit
  2602. txtid++;
  2603. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2604. }
  2605. else //first visit - give bonus!
  2606. {
  2607. if(ID == 105 && artOrRes == 1)
  2608. {
  2609. txtid++;
  2610. iw.text.replacements.push_back(VLC->arth->artifacts[bonusType].Name());
  2611. }
  2612. switch(artOrRes)
  2613. {
  2614. case 0:
  2615. txtid++;
  2616. break;
  2617. case 1: //art
  2618. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2619. cb->giveHeroArtifact(bonusType,h->id,-2);
  2620. break;
  2621. case 2: //res
  2622. iw.components.push_back(Component(Component::RESOURCE,bonusType,bonusVal,0));
  2623. cb->giveResource(h->getOwner(),bonusType,bonusVal);
  2624. break;
  2625. }
  2626. iw.text.addTxt(MetaString::ADVOB_TXT, txtid);
  2627. }
  2628. cb->showInfoDialog(&iw);
  2629. cb->setObjProperty(id,10,h->getOwner());
  2630. }
  2631. const std::string & CGOnceVisitable::getHoverText() const
  2632. {
  2633. hoverName = VLC->generaltexth->names[ID] + " ";
  2634. hoverName += (hasVisited(cb->getCurrentPlayer())
  2635. ? (VLC->generaltexth->allTexts[352]) //visited
  2636. : ( VLC->generaltexth->allTexts[353])); //not visited
  2637. return hoverName;
  2638. }
  2639. void CGOnceVisitable::initObj()
  2640. {
  2641. switch(ID)
  2642. {
  2643. case 22: //Corpse
  2644. {
  2645. blockVisit = true;
  2646. int hlp = ran()%100;
  2647. if(hlp < 20)
  2648. {
  2649. artOrRes = 1;
  2650. std::vector<CArtifact*> arts;
  2651. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR | CArtifact::ART_MAJOR);
  2652. bonusType = arts[ran() % arts.size()]->id;
  2653. }
  2654. else
  2655. {
  2656. artOrRes = 0;
  2657. }
  2658. }
  2659. break;
  2660. case 39: //Lean To
  2661. {
  2662. artOrRes = 2;
  2663. bonusType = ran()%6; //any basic resource without gold
  2664. bonusVal = ran()%4 + 1;
  2665. break;
  2666. }
  2667. case 108://Warrior's Tomb
  2668. {
  2669. artOrRes = 1;
  2670. std::vector<CArtifact*> arts;
  2671. int hlp = ran()%100;
  2672. if(hlp < 30)
  2673. cb->getAllowed(arts,CArtifact::ART_TREASURE);
  2674. else if(hlp < 80)
  2675. cb->getAllowed(arts,CArtifact::ART_MINOR);
  2676. else if(hlp < 95)
  2677. cb->getAllowed(arts,CArtifact::ART_MAJOR);
  2678. else
  2679. cb->getAllowed(arts,CArtifact::ART_RELIC);
  2680. bonusType = arts[ran() % arts.size()]->id;
  2681. }
  2682. break;
  2683. case 105://Wagon
  2684. {
  2685. int hlp = ran()%100;
  2686. if(hlp < 10)
  2687. {
  2688. artOrRes = 0; // nothing... :(
  2689. }
  2690. else if(hlp < 50) //minor or treasure art
  2691. {
  2692. artOrRes = 1;
  2693. std::vector<CArtifact*> arts;
  2694. cb->getAllowed(arts, CArtifact::ART_TREASURE | CArtifact::ART_MINOR);
  2695. bonusType = arts[ran() % arts.size()]->id;
  2696. }
  2697. else //2 - 5 of non-gold resource
  2698. {
  2699. artOrRes = 2;
  2700. bonusType = ran()%6;
  2701. bonusVal = ran()%4 + 2;
  2702. }
  2703. break;
  2704. }
  2705. }
  2706. }
  2707. void CGOnceVisitable::searchTomb(const CGHeroInstance *h, ui32 accept) const
  2708. {
  2709. if(accept)
  2710. {
  2711. InfoWindow iw;
  2712. iw.player = h->getOwner();
  2713. iw.components.push_back(Component(Component::MORALE,0,-3,0));
  2714. if(players.size()) //we've been already visited, player found nothing
  2715. {
  2716. iw.text.addTxt(MetaString::ADVOB_TXT,163);
  2717. }
  2718. else //first visit - give artifact
  2719. {
  2720. iw.text.addTxt(MetaString::ADVOB_TXT,162);
  2721. iw.components.push_back(Component(Component::ARTIFACT,bonusType,0,0));
  2722. iw.text.replacements.push_back(VLC->arth->artifacts[bonusType].Name());
  2723. cb->giveHeroArtifact(bonusType,h->id,-2);
  2724. }
  2725. if(!h->getBonus(HeroBonus::OBJECT,ID)) //we don't have modifier from this object yet
  2726. {
  2727. //ruin morale
  2728. GiveBonus gb;
  2729. gb.hid = h->id;
  2730. gb.bonus = HeroBonus(HeroBonus::ONE_BATTLE,HeroBonus::MORALE,HeroBonus::OBJECT,-3,id,"");
  2731. gb.bdescr.addTxt(MetaString::ARRAY_TXT,104); //Warrior Tomb Visited -3
  2732. cb->giveHeroBonus(&gb);
  2733. }
  2734. cb->showInfoDialog(&iw);
  2735. //add player to the visitors (for visited tooltop)
  2736. cb->setObjProperty(id,10,h->getOwner());
  2737. }
  2738. }