CObjectHandler.cpp 73 KB

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