CObjectHandler.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CObjectHandler.h"
  4. #include "CDefObjInfoHandler.h"
  5. #include "CLodHandler.h"
  6. #include "CGeneralTextHandler.h"
  7. #include "CDefObjInfoHandler.h"
  8. #include "CHeroHandler.h"
  9. #include "CSpellHandler.h"
  10. #include <boost/bind.hpp>
  11. #include <boost/algorithm/string/replace.hpp>
  12. #include <boost/lexical_cast.hpp>
  13. #include <boost/random/linear_congruential.hpp>
  14. #include "CTownHandler.h"
  15. #include "CArtHandler.h"
  16. #include "../lib/VCMI_Lib.h"
  17. #include "../lib/IGameCallback.h"
  18. #include "../CGameState.h"
  19. #include "../lib/NetPacks.h"
  20. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  21. IGameCallback * IObjectInterface::cb = NULL;
  22. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  23. extern CLodHandler * bitmaph;
  24. extern boost::rand48 ran;
  25. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  26. {};
  27. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  28. {};
  29. void IObjectInterface::newTurn () const
  30. {};
  31. IObjectInterface::~IObjectInterface()
  32. {}
  33. IObjectInterface::IObjectInterface()
  34. {}
  35. void IObjectInterface::initObj()
  36. {}
  37. void CObjectHandler::loadObjects()
  38. {
  39. tlog5 << "\t\tReading cregens \n";
  40. cregens.resize(110); //TODO: hardcoded value - change
  41. for(size_t i=0; i < cregens.size(); ++i)
  42. {
  43. cregens[i]=-1;
  44. }
  45. std::ifstream ifs("config/cregens.txt");
  46. while(!ifs.eof())
  47. {
  48. int dw, cr;
  49. ifs >> dw >> cr;
  50. cregens[dw]=cr;
  51. }
  52. ifs.close();
  53. ifs.clear();
  54. tlog5 << "\t\tDone loading objects!\n";
  55. }
  56. int CGObjectInstance::getOwner() const
  57. {
  58. //if (state)
  59. // return state->owner;
  60. //else
  61. return tempOwner; //won't have owner
  62. }
  63. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  64. {
  65. pos = int3(-1,-1,-1);
  66. //std::cout << "Tworze obiekt "<<this<<std::endl;
  67. //state = new CLuaObjectScript();
  68. ID = subID = id = -1;
  69. defInfo = NULL;
  70. info = NULL;
  71. tempOwner = 254;
  72. blockVisit = false;
  73. }
  74. CGObjectInstance::~CGObjectInstance()
  75. {
  76. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  77. //if (state)
  78. // delete state;
  79. //state=NULL;
  80. }
  81. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  82. //{
  83. // pos = right.pos;
  84. // ID = right.ID;
  85. // subID = right.subID;
  86. // id = right.id;
  87. // defInfo = right.defInfo;
  88. // info = right.info;
  89. // blockVisit = right.blockVisit;
  90. // //state = new CLuaObjectScript(right.state->);
  91. // //*state = *right.state;
  92. // //state = right.state;
  93. // tempOwner = right.tempOwner;
  94. //}
  95. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  96. //{
  97. // pos = right.pos;
  98. // ID = right.ID;
  99. // subID = right.subID;
  100. // id = right.id;
  101. // defInfo = right.defInfo;
  102. // info = right.info;
  103. // blockVisit = right.blockVisit;
  104. // //state = new CLuaObjectScript();
  105. // //*state = *right.state;
  106. // tempOwner = right.tempOwner;
  107. // return *this;
  108. //}
  109. const std::string & CGObjectInstance::getHoverText() const
  110. {
  111. return hoverName;
  112. }
  113. void CGObjectInstance::setOwner(int ow)
  114. {
  115. //if (state)
  116. // state->owner = ow;
  117. //else
  118. tempOwner = ow;
  119. }
  120. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  121. {
  122. return defInfo->width;
  123. }
  124. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  125. {
  126. return defInfo->width;
  127. }
  128. 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)
  129. {
  130. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  131. return false;
  132. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  133. return true;
  134. return false;
  135. }
  136. bool CGObjectInstance::blockingAt(int x, int y) const
  137. {
  138. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  139. return false;
  140. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  141. return true;
  142. return false;
  143. }
  144. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  145. {
  146. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  147. return true;
  148. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  149. return false;
  150. if(this->pos.y<cmp.pos.y)
  151. return true;
  152. if(this->pos.y>cmp.pos.y)
  153. return false;
  154. if(cmp.ID==HEROI_TYPE && ID!=HEROI_TYPE)
  155. return true;
  156. if(cmp.ID!=HEROI_TYPE && ID==HEROI_TYPE)
  157. return false;
  158. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  159. return true;
  160. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  161. return false;
  162. if(this->pos.x<cmp.pos.x)
  163. return true;
  164. return false;
  165. }
  166. void CGObjectInstance::initObj()
  167. {
  168. }
  169. int lowestSpeed(const CGHeroInstance * chi)
  170. {
  171. if(!chi->army.slots.size())
  172. {
  173. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  174. return 20;
  175. }
  176. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  177. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  178. for (;i!=chi->army.slots.end();i++)
  179. {
  180. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  181. }
  182. return ret;
  183. }
  184. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype, const int & remaingMP) const
  185. {
  186. if(remaingMP <= 100) return 100; //workaround for strange behaviour manifested by Heroes III
  187. unsigned int ret = type->heroClass->terrCosts[ttype];
  188. //applying pathfinding skill
  189. switch(getSecSkillLevel(0))
  190. {
  191. case 1: //basic
  192. switch(ttype)
  193. {
  194. case rough:
  195. ret = 100;
  196. break;
  197. case sand: case snow:
  198. if(ret>125)
  199. ret = 125;
  200. break;
  201. case swamp:
  202. if(ret>150)
  203. ret = 150;
  204. break;
  205. default:
  206. //TODO do something nasty here throw maybe? or some def value asing
  207. break;
  208. }
  209. break;
  210. case 2: //advanced
  211. switch(ttype)
  212. {
  213. case rough:
  214. case sand:
  215. case snow:
  216. ret = 100;
  217. break;
  218. case swamp:
  219. if(ret>125)
  220. ret = 125;
  221. break;
  222. default:
  223. //TODO look up
  224. break;
  225. }
  226. break;
  227. case 3: //expert
  228. ret = 100;
  229. break;
  230. default:
  231. //TODO look up
  232. break;
  233. }
  234. //calculating road influence
  235. switch(rdtype)
  236. {
  237. case dirtRoad:
  238. ret*=0.75;
  239. break;
  240. case grazvelRoad:
  241. ret*=0.667;
  242. break;
  243. case cobblestoneRoad:
  244. ret*=0.5;
  245. break;
  246. default:
  247. //TODO killllll me
  248. break;
  249. }
  250. return ret;
  251. }
  252. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  253. {
  254. unsigned int sl = 100;
  255. for(size_t h=0; h < army.slots.size(); ++h)
  256. {
  257. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  258. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  259. }
  260. return sl;
  261. }
  262. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  263. {
  264. if (toh3m)
  265. {
  266. src.x+=1;
  267. return src;
  268. }
  269. else
  270. {
  271. src.x-=1;
  272. return src;
  273. }
  274. }
  275. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  276. {
  277. if (h3m)
  278. {
  279. return pos;
  280. }
  281. else
  282. {
  283. return convertPosition(pos,false);
  284. }
  285. }
  286. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  287. {
  288. return 6 + getSecSkillLevel(3); //default + scouting
  289. }
  290. si32 CGHeroInstance::manaLimit() const
  291. {
  292. double modifier = 1.0;
  293. switch(getSecSkillLevel(24)) //intelligence level
  294. {
  295. case 1: modifier+=0.25; break;
  296. case 2: modifier+=0.5; break;
  297. case 3: modifier+=1.0; break;
  298. }
  299. return 10*getPrimSkillLevel(3)*modifier;
  300. }
  301. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  302. //{
  303. // if (h3m)
  304. // pos = Pos;
  305. // else
  306. // pos = convertPosition(Pos,true);
  307. //}
  308. bool CGHeroInstance::canWalkOnSea() const
  309. {
  310. //TODO: write it - it should check if hero is flying, or something similiar
  311. return false;
  312. }
  313. int CGHeroInstance::getPrimSkillLevel(int id) const
  314. {
  315. return primSkills[id];
  316. }
  317. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  318. {
  319. for(size_t i=0; i < secSkills.size(); ++i)
  320. if(secSkills[i].first==ID)
  321. return secSkills[i].second;
  322. return 0;
  323. }
  324. int CGHeroInstance::maxMovePoints(bool onLand) const
  325. {
  326. int ret = 1270+70*lowestSpeed(this);
  327. if (ret>2000)
  328. ret=2000;
  329. if(onLand)
  330. {
  331. //logistics:
  332. switch(getSecSkillLevel(2))
  333. {
  334. case 1:
  335. ret *= 1.1f;
  336. break;
  337. case 2:
  338. ret *= 1.2f;
  339. break;
  340. case 3:
  341. ret *= 1.3f;
  342. break;
  343. }
  344. }
  345. else
  346. {
  347. //navigation:
  348. switch(getSecSkillLevel(2))
  349. {
  350. case 1:
  351. ret *= 1.5f;
  352. break;
  353. case 2:
  354. ret *= 2.0f;
  355. break;
  356. case 3:
  357. ret *= 2.5f;
  358. break;
  359. }
  360. }
  361. return ret;
  362. }
  363. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  364. {
  365. if(pos<19)
  366. if(vstd::contains(artifWorn,pos))
  367. return artifWorn.find(pos)->second;
  368. else
  369. return -1;
  370. else
  371. if(pos-19 < artifacts.size())
  372. return artifacts[pos-19];
  373. else
  374. return -1;
  375. }
  376. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  377. {
  378. if(art<0)
  379. {
  380. if(pos<19)
  381. artifWorn.erase(pos);
  382. else
  383. artifacts -= artifacts[pos-19];
  384. }
  385. else
  386. {
  387. if(pos<19)
  388. artifWorn[pos] = art;
  389. else
  390. if(pos-19 < artifacts.size())
  391. artifacts[pos-19] = art;
  392. else
  393. artifacts.push_back(art);
  394. }
  395. }
  396. const CArtifact * CGHeroInstance::getArt(int pos) const
  397. {
  398. int id = getArtAtPos(pos);
  399. if(id>=0)
  400. return &VLC->arth->artifacts[id];
  401. else
  402. return NULL;
  403. }
  404. int CGHeroInstance::getSpellSecLevel(int spell) const
  405. {
  406. int bestslvl = 0;
  407. if(VLC->spellh->spells[spell].air)
  408. if(getSecSkillLevel(15) >= bestslvl)
  409. {
  410. bestslvl = getSecSkillLevel(15);
  411. }
  412. if(VLC->spellh->spells[spell].fire)
  413. if(getSecSkillLevel(14) >= bestslvl)
  414. {
  415. bestslvl = getSecSkillLevel(14);
  416. }
  417. if(VLC->spellh->spells[spell].water)
  418. if(getSecSkillLevel(16) >= bestslvl)
  419. {
  420. bestslvl = getSecSkillLevel(16);
  421. }
  422. if(VLC->spellh->spells[spell].earth)
  423. if(getSecSkillLevel(17) >= bestslvl)
  424. {
  425. bestslvl = getSecSkillLevel(17);
  426. }
  427. return bestslvl;
  428. }
  429. CGHeroInstance::CGHeroInstance()
  430. {
  431. ID = 34;
  432. tacticFormationEnabled = inTownGarrison = false;
  433. mana = movement = portrait = level = -1;
  434. isStanding = true;
  435. moveDir = 4;
  436. exp = 0xffffffff;
  437. visitedTown = NULL;
  438. type = NULL;
  439. secSkills.push_back(std::make_pair(-1, -1));
  440. }
  441. void CGHeroInstance::initHero(int SUBID)
  442. {
  443. subID = SUBID;
  444. initHero();
  445. }
  446. void CGHeroInstance::initHero()
  447. {
  448. initHeroDefInfo();
  449. if(!type)
  450. type = VLC->heroh->heroes[subID];
  451. artifWorn[16] = 3;
  452. if(type->heroType % 2 == 1) //it's a magical hero
  453. {
  454. artifWorn[17] = 0; //give him spellbook
  455. }
  456. if(portrait < 0 || portrait == 255)
  457. portrait = subID;
  458. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  459. {
  460. primSkills.resize(4);
  461. primSkills[0] = type->heroClass->initialAttack;
  462. primSkills[1] = type->heroClass->initialDefence;
  463. primSkills[2] = type->heroClass->initialPower;
  464. primSkills[3] = type->heroClass->initialKnowledge;
  465. }
  466. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  467. secSkills = type->secSkillsInit;
  468. if(mana < 0)
  469. mana = manaLimit();
  470. if (!name.length())
  471. name = type->name;
  472. if (exp == 0xffffffff)
  473. {
  474. exp=40+ (ran()) % 50;
  475. level = 1;
  476. }
  477. else
  478. {
  479. level = VLC->heroh->level(exp);
  480. }
  481. if (!army.slots.size()) //standard army//initial army
  482. {
  483. int pom, pom2=0;
  484. for(int x=0;x<3;x++)
  485. {
  486. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  487. if(pom>=145 && pom<=149) //war machine
  488. {
  489. pom2++;
  490. switch (pom)
  491. {
  492. case 145: //catapult
  493. artifWorn[16] = 3;
  494. break;
  495. default:
  496. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  497. break;
  498. }
  499. continue;
  500. }
  501. army.slots[x-pom2].first = pom;
  502. if((pom = (type->highStack[x]-type->lowStack[x])) > 0)
  503. army.slots[x-pom2].second = (ran()%pom)+type->lowStack[x];
  504. else
  505. army.slots[x-pom2].second = +type->lowStack[x];
  506. army.formation = false;
  507. }
  508. }
  509. hoverName = VLC->generaltexth->allTexts[15];
  510. boost::algorithm::replace_first(hoverName,"%s",name);
  511. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  512. }
  513. void CGHeroInstance::initHeroDefInfo()
  514. {
  515. if(!defInfo)
  516. {
  517. defInfo = new CGDefInfo();
  518. defInfo->id = 34;
  519. defInfo->subid = subID;
  520. defInfo->printPriority = 0;
  521. defInfo->visitDir = 0xff;
  522. }
  523. for(int i=0;i<6;i++)
  524. {
  525. defInfo->blockMap[i]=255;
  526. defInfo->visitMap[i]=0;
  527. }
  528. defInfo->handler=NULL;
  529. defInfo->blockMap[5] = 253;
  530. defInfo->visitMap[5] = 2;
  531. }
  532. CGHeroInstance::~CGHeroInstance()
  533. {
  534. }
  535. bool CGHeroInstance::needsLastStack() const
  536. {
  537. return true;
  538. }
  539. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  540. {
  541. //TODO: check for allies
  542. if(tempOwner == h->tempOwner) //our hero
  543. {
  544. //exchange
  545. }
  546. else
  547. {
  548. cb->startBattleI(
  549. &h->army,
  550. &army,
  551. h->pos,
  552. h,
  553. this,
  554. 0);
  555. }
  556. }
  557. const std::string & CGHeroInstance::getBiography() const
  558. {
  559. if (biography.length())
  560. return biography;
  561. else
  562. return VLC->generaltexth->hTxts[subID].biography;
  563. }
  564. void CGHeroInstance::initObj()
  565. {
  566. blockVisit = true;
  567. }
  568. int CGHeroInstance::getCurrentMorale( int stack, bool town ) const
  569. {
  570. int ret = 0;
  571. std::vector<std::pair<int,std::string> > mods = getCurrentMoraleModifiers(stack,town);
  572. for(int i=0; i < mods.size(); i++)
  573. ret += mods[i].first;
  574. if(ret > 3)
  575. return 3;
  576. if(ret < -3)
  577. return -3;
  578. return ret;
  579. }
  580. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentMoraleModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  581. {
  582. //TODO: check if stack is undead/mechanic/elemental => always neutrl morale
  583. std::vector<std::pair<int,std::string> > ret;
  584. //various morale bonuses (from buildings, artifacts, etc)
  585. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  586. if(i->type == HeroBonus::MORALE || i->type == HeroBonus::MORALE_AND_LUCK)
  587. ret.push_back(std::make_pair(i->val, i->description));
  588. //leadership
  589. if(getSecSkillLevel(6))
  590. ret.push_back(std::make_pair(getSecSkillLevel(6),VLC->generaltexth->arraytxt[104+getSecSkillLevel(6)]));
  591. //town structures
  592. if(town && visitedTown)
  593. {
  594. if(visitedTown->subID == 0 && vstd::contains(visitedTown->builtBuildings,22)) //castle, brotherhood of sword built
  595. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][22].first + " +2"));
  596. else if(vstd::contains(visitedTown->builtBuildings,5)) //tavern is built
  597. ret.push_back(std::pair<int,std::string>(2,VLC->generaltexth->buildings[0][5].first + " +1"));
  598. }
  599. //number of alignments and presence of undead
  600. if(stack>=0)
  601. {
  602. bool archangelInArmy = false;
  603. std::set<si8> factions;
  604. for(std::map<si32,std::pair<ui32,si32> >::const_iterator i=army.slots.begin(); i!=army.slots.end(); i++)
  605. {
  606. factions.insert(VLC->creh->creatures[i->second.first].faction);
  607. if(i->second.first == 13)
  608. archangelInArmy = true;
  609. }
  610. if(factions.size() == 1)
  611. ret.push_back(std::pair<int,std::string>(1,VLC->generaltexth->arraytxt[115])); //All troops of one alignment +1
  612. else
  613. {
  614. if(VLC->generaltexth->arraytxt[114].length() <= 100)
  615. {
  616. char buf[150];
  617. std::sprintf(buf,VLC->generaltexth->arraytxt[114].c_str(),factions.size(),2-factions.size());
  618. ret.push_back(std::pair<int,std::string>(2-factions.size(),buf)); //Troops of %d alignments %d
  619. }
  620. else
  621. {
  622. ret.push_back(std::pair<int,std::string>(2-factions.size(),"")); //Troops of %d alignments %d
  623. }
  624. }
  625. if(vstd::contains(factions,4))
  626. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //Undead in group -1
  627. if(archangelInArmy)
  628. {
  629. char buf[100];
  630. sprintf(buf,VLC->generaltexth->arraytxt[117].c_str(),VLC->creh->creatures[13].namePl);
  631. ret.push_back(std::pair<int,std::string>(-1,VLC->generaltexth->arraytxt[116])); //%s in group +1
  632. }
  633. }
  634. return ret;
  635. }
  636. int CGHeroInstance::getCurrentLuck( int stack/*=-1*/, bool town/*=false*/ ) const
  637. {
  638. int ret = 0;
  639. std::vector<std::pair<int,std::string> > mods = getCurrentLuckModifiers(stack,town);
  640. for(int i=0; i < mods.size(); i++)
  641. ret += mods[i].first;
  642. if(ret > 3)
  643. return 3;
  644. if(ret < -3)
  645. return -3;
  646. return ret;
  647. }
  648. std::vector<std::pair<int,std::string> > CGHeroInstance::getCurrentLuckModifiers( int stack/*=-1*/, bool town/*=false*/ ) const
  649. {
  650. std::vector<std::pair<int,std::string> > ret;
  651. //various morale bonuses (from buildings, artifacts, etc)
  652. for(std::list<HeroBonus>::const_iterator i=bonuses.begin(); i != bonuses.end(); i++)
  653. if(i->type == HeroBonus::LUCK || i->type == HeroBonus::MORALE_AND_LUCK)
  654. ret.push_back(std::make_pair(i->val, i->description));
  655. //luck skill
  656. if(getSecSkillLevel(9))
  657. ret.push_back(std::make_pair(getSecSkillLevel(9),VLC->generaltexth->arraytxt[73+getSecSkillLevel(9)]));
  658. return ret;
  659. }
  660. const HeroBonus * CGHeroInstance::getBonus( int from, int id ) const
  661. {
  662. for (std::list<HeroBonus>::const_iterator i=bonuses.begin(); i!=bonuses.end(); i++)
  663. if(i->source == from && i->id == id)
  664. return &*i;
  665. return NULL;
  666. }
  667. int CGTownInstance::getSightDistance() const //returns sight distance
  668. {
  669. return 10;
  670. }
  671. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  672. {
  673. if((builtBuildings.find(9))!=builtBuildings.end())
  674. return 3;
  675. if((builtBuildings.find(8))!=builtBuildings.end())
  676. return 2;
  677. if((builtBuildings.find(7))!=builtBuildings.end())
  678. return 1;
  679. return 0;
  680. }
  681. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  682. {
  683. if ((builtBuildings.find(13))!=builtBuildings.end())
  684. return 3;
  685. if ((builtBuildings.find(12))!=builtBuildings.end())
  686. return 2;
  687. if ((builtBuildings.find(11))!=builtBuildings.end())
  688. return 1;
  689. if ((builtBuildings.find(10))!=builtBuildings.end())
  690. return 0;
  691. return -1;
  692. }
  693. int CGTownInstance::mageGuildLevel() const
  694. {
  695. if ((builtBuildings.find(4))!=builtBuildings.end())
  696. return 5;
  697. if ((builtBuildings.find(3))!=builtBuildings.end())
  698. return 4;
  699. if ((builtBuildings.find(2))!=builtBuildings.end())
  700. return 3;
  701. if ((builtBuildings.find(1))!=builtBuildings.end())
  702. return 2;
  703. if ((builtBuildings.find(0))!=builtBuildings.end())
  704. return 1;
  705. return 0;
  706. }
  707. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  708. {
  709. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  710. }
  711. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  712. {
  713. return town->hordeLvl[HID];
  714. }
  715. int CGTownInstance::creatureGrowth(const int & level) const
  716. {
  717. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  718. switch(fortLevel())
  719. {
  720. case 3:
  721. ret*=2;break;
  722. case 2:
  723. ret*=(1.5); break;
  724. }
  725. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  726. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  727. if(getHordeLevel(0)==level)
  728. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  729. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  730. if(getHordeLevel(1)==level)
  731. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  732. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  733. return ret;
  734. }
  735. int CGTownInstance::dailyIncome() const
  736. {
  737. int ret = 0;
  738. if ((builtBuildings.find(26))!=builtBuildings.end())
  739. ret+=5000;
  740. if ((builtBuildings.find(13))!=builtBuildings.end())
  741. ret+=4000;
  742. else if ((builtBuildings.find(12))!=builtBuildings.end())
  743. ret+=2000;
  744. else if ((builtBuildings.find(11))!=builtBuildings.end())
  745. ret+=1000;
  746. else if ((builtBuildings.find(10))!=builtBuildings.end())
  747. ret+=500;
  748. return ret;
  749. }
  750. bool CGTownInstance::hasFort() const
  751. {
  752. return (builtBuildings.find(7))!=builtBuildings.end();
  753. }
  754. bool CGTownInstance::hasCapitol() const
  755. {
  756. return (builtBuildings.find(13))!=builtBuildings.end();
  757. }
  758. CGTownInstance::CGTownInstance()
  759. {
  760. builded=-1;
  761. destroyed=-1;
  762. garrisonHero=NULL;
  763. town=NULL;
  764. visitingHero = NULL;
  765. }
  766. CGTownInstance::~CGTownInstance()
  767. {}
  768. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  769. {
  770. if(checkGuild && mageGuildLevel() < level)
  771. return 0;
  772. int ret = 6 - level; //how many spells are available at this level
  773. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  774. ret++;
  775. return ret;
  776. }
  777. bool CGTownInstance::needsLastStack() const
  778. {
  779. if(garrisonHero)
  780. return true;
  781. else return false;
  782. }
  783. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  784. {
  785. if(getOwner() != h->getOwner())
  786. {
  787. return;
  788. }
  789. cb->heroVisitCastle(id,h->id);
  790. }
  791. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  792. {
  793. cb->stopHeroVisitCastle(id,h->id);
  794. }
  795. void CGTownInstance::initObj()
  796. {
  797. MetaString ms;
  798. ms << name << ", " << town->Name();
  799. hoverName = toString(ms);
  800. }
  801. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  802. {
  803. if(visitors.find(h->id)==visitors.end())
  804. {
  805. onNAHeroVisit(h->id, false);
  806. if(ID != 102 && ID!=4 && ID!=41) //not tree nor arena nor library of enlightenment
  807. cb->setObjProperty(id,4,h->id); //add to the visitors
  808. }
  809. else
  810. {
  811. onNAHeroVisit(h->id, true);
  812. }
  813. }
  814. void CGVisitableOPH::initObj()
  815. {
  816. if(ID==102)
  817. ttype = ran()%3;
  818. else
  819. ttype = -1;
  820. }
  821. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  822. {
  823. if(result==0) //player agreed to give res for exp
  824. {
  825. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  826. cb->changePrimSkill(heroID,4,expVal); //give exp
  827. cb->setObjProperty(id,4,heroID); //add to the visitors
  828. }
  829. }
  830. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  831. {
  832. int id=0, subid=0, ot=0, val=1;
  833. switch(ID)
  834. {
  835. case 4:
  836. ot = 0;
  837. break;
  838. case 51:
  839. subid=0;
  840. ot=80;
  841. break;
  842. case 23:
  843. subid=1;
  844. ot=39;
  845. break;
  846. case 61:
  847. subid=2;
  848. ot=100;
  849. break;
  850. case 32:
  851. subid=3;
  852. ot=59;
  853. break;
  854. case 100:
  855. id=5;
  856. ot=143;
  857. val=1000;
  858. break;
  859. case 102:
  860. id = 5;
  861. subid = 1;
  862. ot = 146;
  863. val = 1;
  864. break;
  865. case 41:
  866. ot = 66;
  867. break;
  868. }
  869. if (!alreadyVisited)
  870. {
  871. switch (ID)
  872. {
  873. case 4: //arena
  874. {
  875. SelectionDialog sd;
  876. sd.text << std::pair<ui8,ui32>(11,ot);
  877. sd.components.push_back(Component(0,0,2,0));
  878. sd.components.push_back(Component(0,1,2,0));
  879. sd.player = cb->getOwner(heroID);
  880. cb->showSelectionDialog(&sd,boost::bind(&CGVisitableOPH::arenaSelected,this,heroID,_1));
  881. return;
  882. }
  883. case 51:
  884. case 23:
  885. case 61:
  886. case 32:
  887. {
  888. cb->changePrimSkill(heroID,subid,val);
  889. InfoWindow iw;
  890. iw.components.push_back(Component(0,subid,val,0));
  891. iw.text << std::pair<ui8,ui32>(11,ot);
  892. iw.player = cb->getOwner(heroID);
  893. cb->showInfoDialog(&iw);
  894. break;
  895. }
  896. case 100: //give exp
  897. {
  898. InfoWindow iw;
  899. iw.components.push_back(Component(id,subid,val,0));
  900. iw.player = cb->getOwner(heroID);
  901. iw.text << std::pair<ui8,ui32>(11,ot);
  902. cb->showInfoDialog(&iw);
  903. cb->changePrimSkill(heroID,4,val);
  904. break;
  905. }
  906. case 102:
  907. {
  908. const CGHeroInstance *h = cb->getHero(heroID);
  909. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  910. if(!ttype)
  911. {
  912. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  913. InfoWindow iw;
  914. iw.components.push_back(Component(id,subid,1,0));
  915. iw.player = cb->getOwner(heroID);
  916. iw.text << std::pair<ui8,ui32>(11,148);
  917. cb->showInfoDialog(&iw);
  918. cb->changePrimSkill(heroID,4,val);
  919. break;
  920. }
  921. else
  922. {
  923. int res, resval;
  924. if(ttype==1)
  925. {
  926. res = 6;
  927. resval = 2000;
  928. ot = 149;
  929. }
  930. else
  931. {
  932. res = 5;
  933. resval = 10;
  934. ot = 151;
  935. }
  936. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  937. {
  938. ot++;
  939. InfoWindow iw;
  940. iw.player = h->tempOwner;
  941. iw.text << std::pair<ui8,ui32>(11,ot);
  942. cb->showInfoDialog(&iw);
  943. return;
  944. }
  945. YesNoDialog sd;
  946. sd.player = cb->getOwner(heroID);
  947. sd.text << std::pair<ui8,ui32>(11,ot);
  948. sd.components.push_back(Component(id,subid,val,0));
  949. cb->showYesNoDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  950. }
  951. break;
  952. }
  953. case 41:
  954. {
  955. const CGHeroInstance *h = cb->getHero(heroID);
  956. if(h->level < 10 - 2*h->getSecSkillLevel(4)) //not enough level
  957. {
  958. InfoWindow iw;
  959. iw.player = cb->getOwner(heroID);
  960. iw.text << std::pair<ui8,ui32>(11,68);
  961. cb->showInfoDialog(&iw);
  962. }
  963. else
  964. {
  965. cb->setObjProperty(this->id,4,heroID); //add to the visitors
  966. cb->changePrimSkill(heroID,0,2);
  967. cb->changePrimSkill(heroID,1,2);
  968. cb->changePrimSkill(heroID,2,2);
  969. cb->changePrimSkill(heroID,3,2);
  970. InfoWindow iw;
  971. iw.player = cb->getOwner(heroID);
  972. iw.text << std::pair<ui8,ui32>(11,66);
  973. cb->showInfoDialog(&iw);
  974. }
  975. break;
  976. }
  977. }
  978. }
  979. else
  980. {
  981. ot++;
  982. InfoWindow iw;
  983. iw.player = cb->getOwner(heroID);
  984. iw.text << std::pair<ui8,ui32>(11,ot);
  985. cb->showInfoDialog(&iw);
  986. }
  987. }
  988. const std::string & CGVisitableOPH::getHoverText() const
  989. {
  990. int pom = -1;
  991. switch(ID)
  992. {
  993. case 4:
  994. pom = -1;
  995. break;
  996. case 51:
  997. pom = 8;
  998. break;
  999. case 23:
  1000. pom = 7;
  1001. break;
  1002. case 61:
  1003. pom = 11;
  1004. break;
  1005. case 32:
  1006. pom = 4;
  1007. break;
  1008. case 100:
  1009. pom = 5;
  1010. break;
  1011. case 102:
  1012. pom = 18;
  1013. break;
  1014. case 41:
  1015. break;
  1016. default:
  1017. throw std::string("Wrong CGVisitableOPH object ID!\n");
  1018. }
  1019. hoverName = VLC->generaltexth->names[ID];
  1020. if(pom >= 0)
  1021. hoverName += (" " + VLC->generaltexth->xtrainfo[pom]);
  1022. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1023. if(h)
  1024. {
  1025. hoverName += ' ';
  1026. hoverName += (vstd::contains(visitors,h->id))
  1027. ? (VLC->generaltexth->allTexts[352]) //visited
  1028. : ( VLC->generaltexth->allTexts[353]); //not visited
  1029. }
  1030. return hoverName;
  1031. }
  1032. void CGVisitableOPH::arenaSelected( int heroID, int primSkill ) const
  1033. {
  1034. cb->setObjProperty(id,4,heroID); //add to the visitors
  1035. cb->changePrimSkill(heroID,primSkill,2);
  1036. }
  1037. bool CArmedInstance::needsLastStack() const
  1038. {
  1039. return false;
  1040. }
  1041. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  1042. {
  1043. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  1044. }
  1045. void CGCreature::endBattle( BattleResult *result ) const
  1046. {
  1047. if(result->winner==0)
  1048. {
  1049. cb->removeObject(id);
  1050. }
  1051. else
  1052. {
  1053. int killedAmount=0;
  1054. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  1055. if(i->first == subID)
  1056. killedAmount += i->second;
  1057. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  1058. MetaString ms;
  1059. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1060. pom = 174 + 3*pom + 1;
  1061. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1062. cb->setHoverName(id,&ms);
  1063. }
  1064. }
  1065. void CGCreature::initObj()
  1066. {
  1067. army.slots[0].first = subID;
  1068. si32 &amount = army.slots[0].second;
  1069. CCreature &c = VLC->creh->creatures[subID];
  1070. if(!amount)
  1071. if(c.ammMax == c.ammMin)
  1072. amount = c.ammMax;
  1073. else
  1074. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  1075. MetaString ms;
  1076. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  1077. pom = 174 + 3*pom + 1;
  1078. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  1079. hoverName = toString(ms);
  1080. }
  1081. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  1082. {
  1083. if(subID == 7) //TODO: support for abandoned mine
  1084. return;
  1085. if(h->tempOwner == tempOwner) //we're visiting our mine
  1086. return; //TODO: leaving garrison
  1087. //TODO: check if mine is guarded
  1088. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  1089. MetaString ms;
  1090. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  1091. cb->setHoverName(id,&ms);
  1092. int vv=1; //amount of resource per turn
  1093. if (subID==0 || subID==2)
  1094. vv++;
  1095. else if (subID==6)
  1096. vv = 1000;
  1097. InfoWindow iw;
  1098. iw.text << std::pair<ui8,ui32>(10,subID);
  1099. iw.player = h->tempOwner;
  1100. iw.components.push_back(Component(2,subID,vv,-1));
  1101. cb->showInfoDialog(&iw);
  1102. }
  1103. void CGMine::newTurn() const
  1104. {
  1105. if (tempOwner == NEUTRAL_PLAYER)
  1106. return;
  1107. int vv = 1;
  1108. if (subID==0 || subID==2)
  1109. vv++;
  1110. else if (subID==6)
  1111. vv = 1000;
  1112. cb->giveResource(tempOwner,subID,vv);
  1113. }
  1114. void CGMine::initObj()
  1115. {
  1116. MetaString ms;
  1117. ms << std::pair<ui8,ui32>(9,subID);
  1118. if(tempOwner >= PLAYER_LIMIT)
  1119. tempOwner = NEUTRAL_PLAYER;
  1120. else
  1121. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  1122. hoverName = toString(ms);
  1123. }
  1124. void CGResource::initObj()
  1125. {
  1126. blockVisit = true;
  1127. hoverName = VLC->generaltexth->restypes[subID];
  1128. if(!amount)
  1129. {
  1130. switch(subID)
  1131. {
  1132. case 6:
  1133. amount = 500 + (rand()%6)*100;
  1134. break;
  1135. case 0: case 2:
  1136. amount = 6 + (rand()%5);
  1137. break;
  1138. default:
  1139. amount = 3 + (rand()%3);
  1140. break;
  1141. }
  1142. }
  1143. }
  1144. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  1145. {
  1146. if(army.slots.size())
  1147. {
  1148. if(message.size())
  1149. {
  1150. YesNoDialog ynd;
  1151. ynd.player = h->getOwner();
  1152. ynd.text << message;
  1153. cb->showYesNoDialog(&ynd,boost::bind(&CGResource::fightForRes,this,_1,h));
  1154. }
  1155. else
  1156. {
  1157. fightForRes(0,h);
  1158. }
  1159. }
  1160. else
  1161. {
  1162. if(message.length())
  1163. {
  1164. InfoWindow iw;
  1165. iw.player = h->tempOwner;
  1166. iw.text << message;
  1167. cb->showInfoDialog(&iw);
  1168. }
  1169. collectRes(h->getOwner());
  1170. }
  1171. }
  1172. void CGResource::collectRes( int player ) const
  1173. {
  1174. cb->giveResource(player,subID,amount);
  1175. ShowInInfobox sii;
  1176. sii.player = player;
  1177. sii.c = Component(2,subID,amount,0);
  1178. sii.text << std::pair<ui8,ui32>(11,113);
  1179. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1180. cb->showCompInfo(&sii);
  1181. cb->removeObject(id);
  1182. }
  1183. void CGResource::fightForRes(ui32 refusedFight, const CGHeroInstance *h) const
  1184. {
  1185. if(!refusedFight)
  1186. cb->startBattleI(h->id,army,pos,boost::bind(&CGResource::endBattle,this,_1,h));
  1187. }
  1188. void CGResource::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1189. {
  1190. if(result->winner == 0) //attacker won
  1191. collectRes(h->getOwner());
  1192. }
  1193. void CGVisitableOPW::newTurn() const
  1194. {
  1195. if (cb->getDate(1)==1) //first day of week
  1196. {
  1197. cb->setObjProperty(id,5,false);
  1198. MetaString ms; //set text to "not visited"
  1199. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1200. cb->setHoverName(id,&ms);
  1201. }
  1202. }
  1203. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1204. {
  1205. int mid;
  1206. switch (ID)
  1207. {
  1208. case 55:
  1209. mid = 92;
  1210. break;
  1211. case 112:
  1212. mid = 170;
  1213. break;
  1214. case 109:
  1215. mid = 164;
  1216. break;
  1217. }
  1218. if (visited)
  1219. {
  1220. if (ID!=112)
  1221. mid++;
  1222. else
  1223. mid--;
  1224. InfoWindow iw;
  1225. iw.player = h->tempOwner;
  1226. iw.text << std::pair<ui8,ui32>(11,mid);
  1227. cb->showInfoDialog(&iw);
  1228. }
  1229. else
  1230. {
  1231. int type, sub, val;
  1232. type = 2;
  1233. switch (ID)
  1234. {
  1235. case 55:
  1236. if (rand()%2)
  1237. {
  1238. sub = 5;
  1239. val = 5;
  1240. }
  1241. else
  1242. {
  1243. sub = 6;
  1244. val = 500;
  1245. }
  1246. break;
  1247. case 112:
  1248. mid = 170;
  1249. sub = (rand() % 5) + 1;
  1250. val = (rand() % 4) + 3;
  1251. break;
  1252. case 109:
  1253. mid = 164;
  1254. sub = 6;
  1255. if(cb->getDate(2)<2)
  1256. val = 500;
  1257. else
  1258. val = 1000;
  1259. }
  1260. cb->giveResource(h->tempOwner,sub,val);
  1261. InfoWindow iw;
  1262. iw.player = h->tempOwner;
  1263. iw.components.push_back(Component(type,sub,val,0));
  1264. iw.text << std::pair<ui8,ui32>(11,mid);
  1265. cb->showInfoDialog(&iw);
  1266. cb->setObjProperty(id,5,true);
  1267. MetaString ms; //set text to "visited"
  1268. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1269. cb->setHoverName(id,&ms);
  1270. }
  1271. }
  1272. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1273. {
  1274. int destinationid=-1;
  1275. switch(ID)
  1276. {
  1277. case 43: //one way - find correspong exit monolith
  1278. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1279. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1280. else
  1281. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1282. break;
  1283. case 45: //two way monolith - pick any other one
  1284. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1285. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1286. else
  1287. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1288. break;
  1289. case 103: //find nearest subterranean gate on the other level
  1290. {
  1291. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1292. for(int i=0; i<objs[103][0].size(); i++)
  1293. {
  1294. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1295. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1296. if(hlp<best.second)
  1297. {
  1298. best.first = objs[103][0][i];
  1299. best.second = hlp;
  1300. }
  1301. }
  1302. if(best.first<0)
  1303. return;
  1304. else
  1305. destinationid = best.first;
  1306. break;
  1307. }
  1308. }
  1309. if(destinationid < 0)
  1310. {
  1311. tlog2 << "Cannot find exit... :( \n";
  1312. return;
  1313. }
  1314. cb->moveHero(h->id,
  1315. (ID!=103)
  1316. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1317. : (cb->getObj(destinationid)->pos),
  1318. true);
  1319. }
  1320. void CGTeleport::initObj()
  1321. {
  1322. objs[ID][subID].push_back(id);
  1323. }
  1324. void CGArtifact::initObj()
  1325. {
  1326. blockVisit = true;
  1327. if(ID == 5)
  1328. hoverName = VLC->arth->artifacts[subID].Name();
  1329. }
  1330. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1331. {
  1332. if(!army.slots.size())
  1333. {
  1334. InfoWindow iw;
  1335. iw.player = h->tempOwner;
  1336. iw.components.push_back(Component(4,subID,0,0));
  1337. if(message.length())
  1338. iw.text << message;
  1339. else
  1340. iw.text << std::pair<ui8,ui32>(12,subID);
  1341. cb->showInfoDialog(&iw);
  1342. pick(h);
  1343. }
  1344. else
  1345. {
  1346. if(message.size())
  1347. {
  1348. YesNoDialog ynd;
  1349. ynd.player = h->getOwner();
  1350. ynd.text << message;
  1351. cb->showYesNoDialog(&ynd,boost::bind(&CGArtifact::fightForArt,this,_1,h));
  1352. }
  1353. else
  1354. {
  1355. fightForArt(0,h);
  1356. }
  1357. }
  1358. }
  1359. void CGArtifact::pick(const CGHeroInstance * h) const
  1360. {
  1361. cb->giveHeroArtifact(subID,h->id,-2);
  1362. cb->removeObject(id);
  1363. }
  1364. void CGArtifact::fightForArt( ui32 refusedFight, const CGHeroInstance *h ) const
  1365. {
  1366. if(!refusedFight)
  1367. cb->startBattleI(h->id,army,pos,boost::bind(&CGArtifact::endBattle,this,_1,h));
  1368. }
  1369. void CGArtifact::endBattle( BattleResult *result, const CGHeroInstance *h ) const
  1370. {
  1371. if(result->winner == 0) //attacker won
  1372. pick(h);
  1373. }
  1374. void CGPickable::initObj()
  1375. {
  1376. blockVisit = true;
  1377. switch(ID)
  1378. {
  1379. case 12: //campfire
  1380. val2 = (ran()%3) + 4; //4 - 6
  1381. val1 = val2 * 100;
  1382. type = ran()%6; //given resource
  1383. break;
  1384. case 101: //treasure chest
  1385. {
  1386. int hlp = ran()%100;
  1387. if(hlp >= 95)
  1388. {
  1389. type = 1;
  1390. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1391. return;
  1392. }
  1393. else if (hlp >= 65)
  1394. {
  1395. val1 = 2000;
  1396. }
  1397. else if(hlp >= 33)
  1398. {
  1399. val1 = 1500;
  1400. }
  1401. else
  1402. {
  1403. val1 = 1000;
  1404. }
  1405. val2 = val1 - 500;
  1406. type = 0;
  1407. break;
  1408. }
  1409. }
  1410. }
  1411. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1412. {
  1413. switch(ID)
  1414. {
  1415. case 12: //campfire
  1416. {
  1417. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1418. cb->giveResource(h->tempOwner,6,val1);//gold
  1419. InfoWindow iw;
  1420. iw.player = h->tempOwner;
  1421. iw.components.push_back(Component(2,6,val1,0));
  1422. iw.components.push_back(Component(2,type,val2,0));
  1423. iw.text << std::pair<ui8,ui32>(11,23);
  1424. cb->showInfoDialog(&iw);
  1425. break;
  1426. }
  1427. case 101: //treasure chest
  1428. {
  1429. if (subID) //not OH3 treasure chest
  1430. {
  1431. tlog2 << "Not supported WoG treasure chest!\n";
  1432. return;
  1433. }
  1434. if(type) //there is an artifact
  1435. {
  1436. cb->giveHeroArtifact(val1,h->id,-2);
  1437. InfoWindow iw;
  1438. iw.player = h->tempOwner;
  1439. iw.components.push_back(Component(4,val1,1,0));
  1440. iw.text << std::pair<ui8,ui32>(11,145);
  1441. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1442. cb->showInfoDialog(&iw);
  1443. break;
  1444. }
  1445. else
  1446. {
  1447. SelectionDialog sd;
  1448. sd.player = h->tempOwner;
  1449. sd.text << std::pair<ui8,ui32>(11,146);
  1450. sd.components.push_back(Component(2,6,val1,0));
  1451. sd.components.push_back(Component(5,0,val2,0));
  1452. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1453. cb->showSelectionDialog(&sd,fun);
  1454. return;
  1455. }
  1456. }
  1457. }
  1458. cb->removeObject(id);
  1459. }
  1460. void CGPickable::chosen( int which, int heroID ) const
  1461. {
  1462. switch(which)
  1463. {
  1464. case 0: //player pick gold
  1465. cb->giveResource(cb->getOwner(heroID),6,val1);
  1466. break;
  1467. case 1: //player pick exp
  1468. cb->changePrimSkill(heroID, 4, val2);
  1469. break;
  1470. default:
  1471. throw std::string("Unhandled treasure choice");
  1472. }
  1473. cb->removeObject(id);
  1474. }
  1475. void CGWitchHut::initObj()
  1476. {
  1477. ability = allowedAbilities[ran()%allowedAbilities.size()];
  1478. }
  1479. void CGWitchHut::onHeroVisit( const CGHeroInstance * h ) const
  1480. {
  1481. InfoWindow iw;
  1482. iw.player = h->getOwner();
  1483. if(h->getSecSkillLevel(ability)) //you alredy know this skill
  1484. {
  1485. iw.text << std::pair<ui8,ui32>(11,172);
  1486. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1487. }
  1488. else if(h->secSkills.size() >= SKILL_PER_HERO) //already all skills slots used
  1489. {
  1490. iw.text << std::pair<ui8,ui32>(11,173);
  1491. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1492. }
  1493. else //give sec skill
  1494. {
  1495. iw.components.push_back(Component(1, ability, 1, 0));
  1496. iw.text << std::pair<ui8,ui32>(11,171);
  1497. iw.text.replacements.push_back(VLC->generaltexth->skillName[ability]);
  1498. cb->changeSecSkill(h->id,ability,1,true);
  1499. }
  1500. cb->showInfoDialog(&iw);
  1501. }
  1502. void CGDwelling::onHeroVisit( const CGHeroInstance * h ) const
  1503. {
  1504. }
  1505. void CGDwelling::initObj()
  1506. {
  1507. }
  1508. void CGBonusingObject::onHeroVisit( const CGHeroInstance * h ) const
  1509. {
  1510. bool visited = h->getBonus(HeroBonus::OBJECT,ID);
  1511. int messageID, bonusType, bonusVal;
  1512. int bonusMove = 0;
  1513. InfoWindow iw;
  1514. iw.player = h->tempOwner;
  1515. GiveBonus gbonus;
  1516. gbonus.hid = h->id;
  1517. gbonus.bonus.duration = HeroBonus::ONE_BATTLE;
  1518. gbonus.bonus.source = HeroBonus::OBJECT;
  1519. gbonus.bonus.id = ID;
  1520. switch(ID)
  1521. {
  1522. case 14: //swan pond
  1523. messageID = 29;
  1524. gbonus.bonus.type = HeroBonus::LUCK;
  1525. gbonus.bonus.val = 2;
  1526. gbonus.bdescr << std::pair<ui8,ui32>(6,67);
  1527. bonusMove = -h->movement;
  1528. break;
  1529. case 28: //Faerie Ring
  1530. messageID = 49;
  1531. gbonus.bonus.type = HeroBonus::LUCK;
  1532. gbonus.bonus.val = 1;
  1533. gbonus.bdescr << std::pair<ui8,ui32>(6,71);
  1534. break;
  1535. case 30: //fountain of fortune
  1536. messageID = 55;
  1537. gbonus.bonus.type = HeroBonus::LUCK;
  1538. gbonus.bonus.val = rand()%5 - 1;
  1539. gbonus.bdescr << std::pair<ui8,ui32>(6,69);
  1540. gbonus.bdescr.replacements.push_back((gbonus.bonus.val<0 ? "-" : "+") + boost::lexical_cast<std::string>(gbonus.bonus.val));
  1541. break;
  1542. case 38: //idol of fortune
  1543. messageID = 62;
  1544. if(cb->getDate(1) == 7) //7th day of week
  1545. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1546. else
  1547. gbonus.bonus.type = (cb->getDate(1)%2) ? HeroBonus::LUCK : HeroBonus::MORALE;
  1548. gbonus.bonus.val = 1;
  1549. gbonus.bdescr << std::pair<ui8,ui32>(6,68);
  1550. break;
  1551. case 64: //Rally Flag
  1552. messageID = 111;
  1553. gbonus.bonus.type = HeroBonus::MORALE_AND_LUCK;
  1554. gbonus.bonus.val = 1;
  1555. gbonus.bdescr << std::pair<ui8,ui32>(6,102);
  1556. bonusMove = 400;
  1557. break;
  1558. case 56: //oasis
  1559. messageID = 95;
  1560. gbonus.bonus.type = HeroBonus::MORALE;
  1561. gbonus.bonus.val = 1;
  1562. gbonus.bdescr << std::pair<ui8,ui32>(6,95);
  1563. bonusMove = 800;
  1564. break;
  1565. case 96: //temple
  1566. messageID = 140;
  1567. gbonus.bonus.type = HeroBonus::MORALE;
  1568. if(cb->getDate(1)==7) //sunday
  1569. {
  1570. gbonus.bonus.val = 2;
  1571. gbonus.bdescr << std::pair<ui8,ui32>(6,97);
  1572. }
  1573. else
  1574. {
  1575. gbonus.bonus.val = 1;
  1576. gbonus.bdescr << std::pair<ui8,ui32>(6,96);
  1577. }
  1578. break;
  1579. case 110://Watering Hole
  1580. messageID = 166;
  1581. gbonus.bonus.type = HeroBonus::MORALE;
  1582. gbonus.bonus.val = 1;
  1583. gbonus.bdescr << std::pair<ui8,ui32>(6,100);
  1584. bonusMove = 400;
  1585. break;
  1586. case 31: //Fountain of Youth
  1587. messageID = 57;
  1588. gbonus.bonus.type = HeroBonus::MORALE;
  1589. gbonus.bonus.val = 1;
  1590. gbonus.bdescr << std::pair<ui8,ui32>(6,103);
  1591. bonusMove = 400;
  1592. break;
  1593. }
  1594. if(visited)
  1595. {
  1596. if(ID==64 || ID==96 || ID==56)
  1597. messageID--;
  1598. else
  1599. messageID++;
  1600. }
  1601. else
  1602. {
  1603. if(gbonus.bonus.type == HeroBonus::MORALE || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1604. iw.components.push_back(Component(8,0,gbonus.bonus.val,0));
  1605. if(gbonus.bonus.type == HeroBonus::LUCK || gbonus.bonus.type == HeroBonus::MORALE_AND_LUCK)
  1606. iw.components.push_back(Component(9,0,gbonus.bonus.val,0));
  1607. cb->giveHeroBonus(&gbonus);
  1608. if(bonusMove) //swan pond - take all move points
  1609. {
  1610. SetMovePoints smp;
  1611. smp.hid = h->id;
  1612. smp.val = h->movement + bonusMove;
  1613. cb->setMovePoints(&smp);
  1614. }
  1615. }
  1616. iw.text << std::pair<ui8,ui32>(11,messageID);
  1617. cb->showInfoDialog(&iw);
  1618. }
  1619. const std::string & CGBonusingObject::getHoverText() const
  1620. {
  1621. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1622. hoverName = VLC->generaltexth->names[ID];
  1623. if(h)
  1624. {
  1625. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1626. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1627. else
  1628. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1629. }
  1630. return hoverName;
  1631. }
  1632. void CGMagicWell::onHeroVisit( const CGHeroInstance * h ) const
  1633. {
  1634. int message;
  1635. InfoWindow iw;
  1636. iw.player = h->tempOwner;
  1637. if(h->getBonus(HeroBonus::OBJECT,ID)) //has already visited Well today
  1638. {
  1639. message = 78;
  1640. }
  1641. else if(h->mana < h->manaLimit())
  1642. {
  1643. GiveBonus gbonus;
  1644. gbonus.bonus.type = HeroBonus::NONE;
  1645. gbonus.hid = h->id;
  1646. gbonus.bonus.duration = HeroBonus::ONE_DAY;
  1647. gbonus.bonus.source = HeroBonus::OBJECT;
  1648. gbonus.bonus.id = ID;
  1649. cb->giveHeroBonus(&gbonus);
  1650. cb->setManaPoints(h->id,h->manaLimit());
  1651. message = 77;
  1652. }
  1653. else
  1654. {
  1655. message = 79;
  1656. }
  1657. iw.text << std::pair<ui8,ui32>(11,message); //"A second drink at the well in one day will not help you."
  1658. cb->showInfoDialog(&iw);
  1659. }
  1660. const std::string & CGMagicWell::getHoverText() const
  1661. {
  1662. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  1663. hoverName = VLC->generaltexth->names[ID];
  1664. if(h)
  1665. {
  1666. if(!h->getBonus(HeroBonus::OBJECT,ID))
  1667. hoverName += " " + VLC->generaltexth->allTexts[353]; //not visited
  1668. else
  1669. hoverName += " " + VLC->generaltexth->allTexts[352]; //visited
  1670. }
  1671. return hoverName;
  1672. }