2
0

CObjectHandler.cpp 71 KB

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