2
0

CObjectHandler.cpp 45 KB

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