CObjectHandler.cpp 41 KB

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