CObjectHandler.cpp 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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/random/linear_congruential.hpp>
  13. #include "CTownHandler.h"
  14. #include "CArtHandler.h"
  15. #include "../lib/VCMI_Lib.h"
  16. #include "../lib/IGameCallback.h"
  17. #include "../CGameState.h"
  18. #include "../lib/NetPacks.h"
  19. std::map<int,std::map<int, std::vector<int> > > CGTeleport::objs;
  20. IGameCallback * IObjectInterface::cb = NULL;
  21. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  22. extern CLodHandler * bitmaph;
  23. extern boost::rand48 ran;
  24. void IObjectInterface::onHeroVisit(const CGHeroInstance * h) const
  25. {};
  26. void IObjectInterface::onHeroLeave(const CGHeroInstance * h) const
  27. {};
  28. void IObjectInterface::newTurn () const
  29. {};
  30. IObjectInterface::~IObjectInterface()
  31. {}
  32. IObjectInterface::IObjectInterface()
  33. {}
  34. void IObjectInterface::initObj()
  35. {}
  36. void CObjectHandler::loadObjects()
  37. {
  38. tlog5 << "\t\tReading cregens \n";
  39. cregens.resize(110); //TODO: hardcoded value - change
  40. for(size_t i=0; i < cregens.size(); ++i)
  41. {
  42. cregens[i]=-1;
  43. }
  44. std::ifstream ifs("config/cregens.txt");
  45. while(!ifs.eof())
  46. {
  47. int dw, cr;
  48. ifs >> dw >> cr;
  49. cregens[dw]=cr;
  50. }
  51. ifs.close();
  52. ifs.clear();
  53. tlog5 << "\t\tDone loading objects!\n";
  54. }
  55. int CGObjectInstance::getOwner() const
  56. {
  57. //if (state)
  58. // return state->owner;
  59. //else
  60. return tempOwner; //won't have owner
  61. }
  62. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  63. {
  64. pos = int3(-1,-1,-1);
  65. //std::cout << "Tworze obiekt "<<this<<std::endl;
  66. //state = new CLuaObjectScript();
  67. ID = subID = id = -1;
  68. defInfo = NULL;
  69. info = NULL;
  70. tempOwner = 254;
  71. blockVisit = false;
  72. }
  73. CGObjectInstance::~CGObjectInstance()
  74. {
  75. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  76. //if (state)
  77. // delete state;
  78. //state=NULL;
  79. }
  80. //CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  81. //{
  82. // pos = right.pos;
  83. // ID = right.ID;
  84. // subID = right.subID;
  85. // id = right.id;
  86. // defInfo = right.defInfo;
  87. // info = right.info;
  88. // blockVisit = right.blockVisit;
  89. // //state = new CLuaObjectScript(right.state->);
  90. // //*state = *right.state;
  91. // //state = right.state;
  92. // tempOwner = right.tempOwner;
  93. //}
  94. //CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  95. //{
  96. // pos = right.pos;
  97. // ID = right.ID;
  98. // subID = right.subID;
  99. // id = right.id;
  100. // defInfo = right.defInfo;
  101. // info = right.info;
  102. // blockVisit = right.blockVisit;
  103. // //state = new CLuaObjectScript();
  104. // //*state = *right.state;
  105. // tempOwner = right.tempOwner;
  106. // return *this;
  107. //}
  108. const std::string & CGObjectInstance::getHoverText() const
  109. {
  110. return hoverName;
  111. }
  112. void CGObjectInstance::setOwner(int ow)
  113. {
  114. //if (state)
  115. // state->owner = ow;
  116. //else
  117. tempOwner = ow;
  118. }
  119. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  120. {
  121. return defInfo->width;
  122. }
  123. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  124. {
  125. return defInfo->width;
  126. }
  127. 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)
  128. {
  129. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  130. return false;
  131. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  132. return true;
  133. return false;
  134. }
  135. bool CGObjectInstance::blockingAt(int x, int y) const
  136. {
  137. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  138. return false;
  139. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  140. return true;
  141. return false;
  142. }
  143. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  144. {
  145. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  146. return true;
  147. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  148. return false;
  149. if(this->pos.y<cmp.pos.y)
  150. return true;
  151. if(this->pos.y>cmp.pos.y)
  152. return false;
  153. if(cmp.ID==34 && ID!=34)
  154. return true;
  155. if(cmp.ID!=34 && ID==34)
  156. return false;
  157. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  158. return true;
  159. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  160. return false;
  161. if(this->pos.x<cmp.pos.x)
  162. return true;
  163. return false;
  164. }
  165. void CGObjectInstance::initObj()
  166. {
  167. }
  168. int lowestSpeed(const CGHeroInstance * chi)
  169. {
  170. if(!chi->army.slots.size())
  171. {
  172. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  173. return 20;
  174. }
  175. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  176. ui32 ret = VLC->creh->creatures[(*i++).second.first].speed;
  177. for (;i!=chi->army.slots.end();i++)
  178. {
  179. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  180. }
  181. return ret;
  182. }
  183. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype) const
  184. {
  185. unsigned int ret = type->heroClass->terrCosts[ttype];
  186. //applying pathfinding skill
  187. switch(getSecSkillLevel(0))
  188. {
  189. case 1: //basic
  190. switch(ttype)
  191. {
  192. case rough:
  193. ret = 100;
  194. break;
  195. case sand: case snow:
  196. if(ret>125)
  197. ret = 125;
  198. break;
  199. case swamp:
  200. if(ret>150)
  201. ret = 150;
  202. break;
  203. default:
  204. //TODO do something nasty here throw maybe? or some def value asing
  205. break;
  206. }
  207. break;
  208. case 2: //advanced
  209. switch(ttype)
  210. {
  211. case rough:
  212. case sand:
  213. case snow:
  214. ret = 100;
  215. break;
  216. case swamp:
  217. if(ret>125)
  218. ret = 125;
  219. break;
  220. default:
  221. //TODO look up
  222. break;
  223. }
  224. break;
  225. case 3: //expert
  226. ret = 100;
  227. break;
  228. default:
  229. //TODO look up
  230. break;
  231. }
  232. //calculating road influence
  233. switch(rdtype)
  234. {
  235. case dirtRoad:
  236. ret*=0.75;
  237. break;
  238. case grazvelRoad:
  239. ret*=0.667;
  240. break;
  241. case cobblestoneRoad:
  242. ret*=0.5;
  243. break;
  244. default:
  245. //TODO killllll me
  246. break;
  247. }
  248. return ret;
  249. }
  250. unsigned int CGHeroInstance::getLowestCreatureSpeed() const
  251. {
  252. unsigned int sl = 100;
  253. for(size_t h=0; h < army.slots.size(); ++h)
  254. {
  255. if(VLC->creh->creatures[army.slots.find(h)->first].speed<sl)
  256. sl = VLC->creh->creatures[army.slots.find(h)->first].speed;
  257. }
  258. return sl;
  259. }
  260. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  261. {
  262. if (toh3m)
  263. {
  264. src.x+=1;
  265. return src;
  266. }
  267. else
  268. {
  269. src.x-=1;
  270. return src;
  271. }
  272. }
  273. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  274. {
  275. if (h3m)
  276. {
  277. return pos;
  278. }
  279. else
  280. {
  281. return convertPosition(pos,false);
  282. }
  283. }
  284. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  285. {
  286. return 6 + getSecSkillLevel(3); //default + scouting
  287. }
  288. si32 CGHeroInstance::manaLimit() const
  289. {
  290. double modifier = 1.0;
  291. switch(getSecSkillLevel(24)) //intelligence level
  292. {
  293. case 1: modifier+=0.25; break;
  294. case 2: modifier+=0.5; break;
  295. case 3: modifier+=1.0; break;
  296. }
  297. return 10*getPrimSkillLevel(3)*modifier;
  298. }
  299. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  300. //{
  301. // if (h3m)
  302. // pos = Pos;
  303. // else
  304. // pos = convertPosition(Pos,true);
  305. //}
  306. bool CGHeroInstance::canWalkOnSea() const
  307. {
  308. //TODO: write it - it should check if hero is flying, or something similiar
  309. return false;
  310. }
  311. int CGHeroInstance::getCurrentLuck() const
  312. {
  313. //TODO: write it
  314. return 0;
  315. }
  316. int CGHeroInstance::getCurrentMorale() const
  317. {
  318. //TODO: write it
  319. return 0;
  320. }
  321. int CGHeroInstance::getPrimSkillLevel(int id) const
  322. {
  323. return primSkills[id];
  324. }
  325. ui8 CGHeroInstance::getSecSkillLevel(const int & ID) const
  326. {
  327. for(size_t i=0; i < secSkills.size(); ++i)
  328. if(secSkills[i].first==ID)
  329. return secSkills[i].second;
  330. return 0;
  331. }
  332. int CGHeroInstance::maxMovePoints(bool onLand) const
  333. {
  334. int ret = 1270+70*lowestSpeed(this);
  335. if (ret>2000)
  336. ret=2000;
  337. if(onLand)
  338. {
  339. //logistics:
  340. switch(getSecSkillLevel(2))
  341. {
  342. case 1:
  343. ret *= 1.1f;
  344. break;
  345. case 2:
  346. ret *= 1.2f;
  347. break;
  348. case 3:
  349. ret *= 1.3f;
  350. break;
  351. }
  352. }
  353. else
  354. {
  355. //navigation:
  356. switch(getSecSkillLevel(2))
  357. {
  358. case 1:
  359. ret *= 1.5f;
  360. break;
  361. case 2:
  362. ret *= 2.0f;
  363. break;
  364. case 3:
  365. ret *= 2.5f;
  366. break;
  367. }
  368. }
  369. return ret;
  370. }
  371. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  372. {
  373. if(pos<19)
  374. if(vstd::contains(artifWorn,pos))
  375. return artifWorn.find(pos)->second;
  376. else
  377. return -1;
  378. else
  379. if(pos-19 < artifacts.size())
  380. return artifacts[pos-19];
  381. else
  382. return -1;
  383. }
  384. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  385. {
  386. if(art<0)
  387. {
  388. if(pos<19)
  389. artifWorn.erase(pos);
  390. else
  391. artifacts -= artifacts[pos-19];
  392. }
  393. else
  394. {
  395. if(pos<19)
  396. artifWorn[pos] = art;
  397. else
  398. if(pos-19 < artifacts.size())
  399. artifacts[pos-19] = art;
  400. else
  401. artifacts.push_back(art);
  402. }
  403. }
  404. const CArtifact * CGHeroInstance::getArt(int pos) const
  405. {
  406. int id = getArtAtPos(pos);
  407. if(id>=0)
  408. return &VLC->arth->artifacts[id];
  409. else
  410. return NULL;
  411. }
  412. int CGHeroInstance::getSpellSecLevel(int spell) const
  413. {
  414. int bestslvl = 0;
  415. if(VLC->spellh->spells[spell].air)
  416. if(getSecSkillLevel(15) >= bestslvl)
  417. {
  418. bestslvl = getSecSkillLevel(15);
  419. }
  420. if(VLC->spellh->spells[spell].fire)
  421. if(getSecSkillLevel(14) >= bestslvl)
  422. {
  423. bestslvl = getSecSkillLevel(14);
  424. }
  425. if(VLC->spellh->spells[spell].water)
  426. if(getSecSkillLevel(16) >= bestslvl)
  427. {
  428. bestslvl = getSecSkillLevel(16);
  429. }
  430. if(VLC->spellh->spells[spell].earth)
  431. if(getSecSkillLevel(17) >= bestslvl)
  432. {
  433. bestslvl = getSecSkillLevel(17);
  434. }
  435. return bestslvl;
  436. }
  437. CGHeroInstance::CGHeroInstance()
  438. {
  439. ID = 34;
  440. tacticFormationEnabled = inTownGarrison = false;
  441. mana = movement = portrait = level = -1;
  442. isStanding = true;
  443. moveDir = 4;
  444. exp = 0xffffffff;
  445. visitedTown = NULL;
  446. type = NULL;
  447. secSkills.push_back(std::make_pair(-1, -1));
  448. }
  449. void CGHeroInstance::initHero(int SUBID)
  450. {
  451. subID = SUBID;
  452. initHero();
  453. }
  454. void CGHeroInstance::initHero()
  455. {
  456. if(!defInfo)
  457. {
  458. defInfo = new CGDefInfo();
  459. defInfo->id = 34;
  460. defInfo->subid = subID;
  461. defInfo->printPriority = 0;
  462. defInfo->visitDir = 0xff;
  463. }
  464. if(!type)
  465. type = VLC->heroh->heroes[subID];
  466. for(int i=0;i<6;i++)
  467. {
  468. defInfo->blockMap[i]=255;
  469. defInfo->visitMap[i]=0;
  470. }
  471. defInfo->handler=NULL;
  472. defInfo->blockMap[5] = 253;
  473. defInfo->visitMap[5] = 2;
  474. artifWorn[16] = 3;
  475. if(type->heroType % 2 == 1) //it's a magical hero
  476. {
  477. artifWorn[17] = 0; //give him spellbook
  478. }
  479. if(portrait < 0 || portrait == 255)
  480. portrait = subID;
  481. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  482. {
  483. primSkills.resize(4);
  484. primSkills[0] = type->heroClass->initialAttack;
  485. primSkills[1] = type->heroClass->initialDefence;
  486. primSkills[2] = type->heroClass->initialPower;
  487. primSkills[3] = type->heroClass->initialKnowledge;
  488. }
  489. if(secSkills.size() == 1 && secSkills[0] == std::pair<ui8,ui8>(-1, -1)) //set secondary skills to default
  490. secSkills = type->secSkillsInit;
  491. if(mana < 0)
  492. mana = manaLimit();
  493. if (!name.length())
  494. name = type->name;
  495. if (exp == 0xffffffff)
  496. {
  497. exp=40+ (ran()) % 50;
  498. level = 1;
  499. }
  500. else
  501. {
  502. level = VLC->heroh->level(exp);
  503. }
  504. if (!army.slots.size()) //standard army//initial army
  505. {
  506. int pom, pom2=0;
  507. for(int x=0;x<3;x++)
  508. {
  509. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  510. if(pom>=145 && pom<=149) //war machine
  511. {
  512. pom2++;
  513. switch (pom)
  514. {
  515. case 145: //catapult
  516. artifWorn[16] = 3;
  517. break;
  518. default:
  519. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  520. break;
  521. }
  522. continue;
  523. }
  524. army.slots[x-pom2].first = pom;
  525. if((pom = (type->highStack[x]-type->lowStack[x])) > 0)
  526. army.slots[x-pom2].second = (ran()%pom)+type->lowStack[x];
  527. else
  528. army.slots[x-pom2].second = +type->lowStack[x];
  529. army.formation = false;
  530. }
  531. }
  532. hoverName = VLC->generaltexth->allTexts[15];
  533. boost::algorithm::replace_first(hoverName,"%s",name);
  534. boost::algorithm::replace_first(hoverName,"%s", type->heroClass->name);
  535. }
  536. CGHeroInstance::~CGHeroInstance()
  537. {
  538. }
  539. bool CGHeroInstance::needsLastStack() const
  540. {
  541. return true;
  542. }
  543. void CGHeroInstance::onHeroVisit(const CGHeroInstance * h) const
  544. {
  545. //TODO: check for allies
  546. if(tempOwner == h->tempOwner) //our hero
  547. {
  548. //exchange
  549. }
  550. else
  551. {
  552. cb->startBattleI(
  553. &h->army,
  554. &army,
  555. h->pos,
  556. h,
  557. this,
  558. 0);
  559. }
  560. }
  561. const std::string & CGHeroInstance::getBiography() const
  562. {
  563. if (biography.length())
  564. return biography;
  565. else
  566. return VLC->generaltexth->hTxts[subID].biography;
  567. }
  568. void CGHeroInstance::initObj()
  569. {
  570. blockVisit = true;
  571. }
  572. int CGTownInstance::getSightDistance() const //returns sight distance
  573. {
  574. return 10;
  575. }
  576. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  577. {
  578. if((builtBuildings.find(9))!=builtBuildings.end())
  579. return 3;
  580. if((builtBuildings.find(8))!=builtBuildings.end())
  581. return 2;
  582. if((builtBuildings.find(7))!=builtBuildings.end())
  583. return 1;
  584. return 0;
  585. }
  586. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  587. {
  588. if ((builtBuildings.find(13))!=builtBuildings.end())
  589. return 3;
  590. if ((builtBuildings.find(12))!=builtBuildings.end())
  591. return 2;
  592. if ((builtBuildings.find(11))!=builtBuildings.end())
  593. return 1;
  594. if ((builtBuildings.find(10))!=builtBuildings.end())
  595. return 0;
  596. return -1;
  597. }
  598. int CGTownInstance::mageGuildLevel() const
  599. {
  600. if ((builtBuildings.find(4))!=builtBuildings.end())
  601. return 5;
  602. if ((builtBuildings.find(3))!=builtBuildings.end())
  603. return 4;
  604. if ((builtBuildings.find(2))!=builtBuildings.end())
  605. return 3;
  606. if ((builtBuildings.find(1))!=builtBuildings.end())
  607. return 2;
  608. if ((builtBuildings.find(0))!=builtBuildings.end())
  609. return 1;
  610. return 0;
  611. }
  612. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  613. {
  614. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  615. }
  616. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  617. {
  618. return town->hordeLvl[HID];
  619. }
  620. int CGTownInstance::creatureGrowth(const int & level) const
  621. {
  622. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  623. switch(fortLevel())
  624. {
  625. case 3:
  626. ret*=2;break;
  627. case 2:
  628. ret*=(1.5); break;
  629. }
  630. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  631. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  632. if(getHordeLevel(0)==level)
  633. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  634. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  635. if(getHordeLevel(1)==level)
  636. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  637. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  638. return ret;
  639. }
  640. int CGTownInstance::dailyIncome() const
  641. {
  642. int ret = 0;
  643. if ((builtBuildings.find(26))!=builtBuildings.end())
  644. ret+=5000;
  645. if ((builtBuildings.find(13))!=builtBuildings.end())
  646. ret+=4000;
  647. else if ((builtBuildings.find(12))!=builtBuildings.end())
  648. ret+=2000;
  649. else if ((builtBuildings.find(11))!=builtBuildings.end())
  650. ret+=1000;
  651. else if ((builtBuildings.find(10))!=builtBuildings.end())
  652. ret+=500;
  653. return ret;
  654. }
  655. bool CGTownInstance::hasFort() const
  656. {
  657. return (builtBuildings.find(7))!=builtBuildings.end();
  658. }
  659. bool CGTownInstance::hasCapitol() const
  660. {
  661. return (builtBuildings.find(13))!=builtBuildings.end();
  662. }
  663. CGTownInstance::CGTownInstance()
  664. {
  665. builded=-1;
  666. destroyed=-1;
  667. garrisonHero=NULL;
  668. town=NULL;
  669. visitingHero = NULL;
  670. }
  671. CGTownInstance::~CGTownInstance()
  672. {}
  673. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  674. {
  675. if(checkGuild && mageGuildLevel() < level)
  676. return 0;
  677. int ret = 6 - level; //how many spells are available at this level
  678. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  679. ret++;
  680. return ret;
  681. }
  682. bool CGTownInstance::needsLastStack() const
  683. {
  684. if(garrisonHero)
  685. return true;
  686. else return false;
  687. }
  688. void CGTownInstance::onHeroVisit(const CGHeroInstance * h) const
  689. {
  690. if(getOwner() != h->getOwner())
  691. {
  692. return;
  693. }
  694. cb->heroVisitCastle(id,h->id);
  695. }
  696. void CGTownInstance::onHeroLeave(const CGHeroInstance * h) const
  697. {
  698. cb->stopHeroVisitCastle(id,h->id);
  699. }
  700. void CGTownInstance::initObj()
  701. {
  702. MetaString ms;
  703. ms << name << ", " << town->Name();
  704. hoverName = toString(ms);
  705. }
  706. void CGVisitableOPH::onHeroVisit( const CGHeroInstance * h ) const
  707. {
  708. if(visitors.find(h->id)==visitors.end())
  709. {
  710. onNAHeroVisit(h->id, false);
  711. if(ID != 102) //not tree
  712. cb->setObjProperty(id,4,h->id); //add to the visitors
  713. }
  714. else
  715. {
  716. onNAHeroVisit(h->id, true);
  717. }
  718. }
  719. void CGVisitableOPH::initObj()
  720. {
  721. if(ID==102)
  722. ttype = ran()%3;
  723. else
  724. ttype = -1;
  725. }
  726. void CGVisitableOPH::treeSelected( int heroID, int resType, int resVal, int expVal, ui32 result ) const
  727. {
  728. if(result==0) //player agreed to give res for exp
  729. {
  730. cb->giveResource(cb->getOwner(heroID),resType,-resVal); //take resource
  731. cb->changePrimSkill(heroID,4,expVal); //give exp
  732. cb->setObjProperty(id,4,heroID); //add to the visitors
  733. }
  734. }
  735. void CGVisitableOPH::onNAHeroVisit(int heroID, bool alreadyVisited) const
  736. {
  737. int id=0, subid=0, ot=0, val=1;
  738. switch(ID)
  739. {
  740. case 51:
  741. subid=0;
  742. ot=80;
  743. break;
  744. case 23:
  745. subid=1;
  746. ot=39;
  747. break;
  748. case 61:
  749. subid=2;
  750. ot=100;
  751. break;
  752. case 32:
  753. subid=3;
  754. ot=59;
  755. break;
  756. case 100:
  757. id=5;
  758. ot=143;
  759. val=1000;
  760. break;
  761. case 102:
  762. id = 5;
  763. subid = 1;
  764. ot = 146;
  765. val = 1;
  766. break;
  767. }
  768. if (!alreadyVisited)
  769. {
  770. switch (ID)
  771. {
  772. case 51:
  773. case 23:
  774. case 61:
  775. case 32:
  776. {
  777. cb->changePrimSkill(heroID,subid,val);
  778. InfoWindow iw;
  779. iw.components.push_back(Component(0,subid,val,0));
  780. iw.text << std::pair<ui8,ui32>(11,ot);
  781. iw.player = cb->getOwner(heroID);
  782. cb->showInfoDialog(&iw);
  783. break;
  784. }
  785. case 100: //give exp
  786. {
  787. InfoWindow iw;
  788. iw.components.push_back(Component(id,subid,val,0));
  789. iw.player = cb->getOwner(heroID);
  790. iw.text << std::pair<ui8,ui32>(11,ot);
  791. cb->showInfoDialog(&iw);
  792. cb->changePrimSkill(heroID,4,val);
  793. break;
  794. }
  795. case 102:
  796. {
  797. const CGHeroInstance *h = cb->getHero(heroID);
  798. val = VLC->heroh->reqExp(h->level+val) - VLC->heroh->reqExp(h->level);
  799. if(!ttype)
  800. {
  801. cb->setObjProperty(id,4,heroID); //add to the visitors
  802. InfoWindow iw;
  803. iw.components.push_back(Component(id,subid,1,0));
  804. iw.player = cb->getOwner(heroID);
  805. iw.text << std::pair<ui8,ui32>(11,148);
  806. cb->showInfoDialog(&iw);
  807. cb->changePrimSkill(heroID,4,val);
  808. break;
  809. }
  810. else
  811. {
  812. int res, resval;
  813. if(ttype==1)
  814. {
  815. res = 6;
  816. resval = 2000;
  817. ot = 149;
  818. }
  819. else
  820. {
  821. res = 5;
  822. resval = 10;
  823. ot = 151;
  824. }
  825. if(cb->getResource(h->tempOwner,res) < resval) //not enough resources
  826. {
  827. ot++;
  828. InfoWindow iw;
  829. iw.player = h->tempOwner;
  830. iw.text << std::pair<ui8,ui32>(11,ot);
  831. cb->showInfoDialog(&iw);
  832. return;
  833. }
  834. YesNoDialog sd;
  835. sd.player = cb->getOwner(heroID);
  836. sd.text << std::pair<ui8,ui32>(11,ot);
  837. sd.components.push_back(Component(id,subid,val,0));
  838. cb->showYesNoDialog(&sd,boost::bind(&CGVisitableOPH::treeSelected,this,heroID,res,resval,val,_1));
  839. }
  840. break;
  841. }
  842. }
  843. }
  844. else
  845. {
  846. ot++;
  847. InfoWindow iw;
  848. iw.player = cb->getOwner(heroID);
  849. iw.text << std::pair<ui8,ui32>(11,ot);
  850. cb->showInfoDialog(&iw);
  851. }
  852. }
  853. const std::string & CGVisitableOPH::getHoverText() const
  854. {
  855. int pom;
  856. switch(ID)
  857. {
  858. case 51:
  859. pom = 8;
  860. break;
  861. case 23:
  862. pom = 7;
  863. break;
  864. case 61:
  865. pom = 11;
  866. break;
  867. case 32:
  868. pom = 4;
  869. break;
  870. case 100:
  871. pom = 5;
  872. break;
  873. case 102:
  874. pom = 18;
  875. break;
  876. default:
  877. throw "Wrong CGVisitableOPH object ID!\n";
  878. }
  879. hoverName = VLC->generaltexth->names[ID] + " " + VLC->generaltexth->xtrainfo[pom];
  880. const CGHeroInstance *h = cb->getSelectedHero(cb->getCurrentPlayer());
  881. if(h)
  882. {
  883. hoverName += ' ';
  884. hoverName += (vstd::contains(visitors,h->id))
  885. ? (VLC->generaltexth->allTexts[352]) //visited
  886. : ( VLC->generaltexth->allTexts[353]); //not visited
  887. }
  888. return hoverName;
  889. }
  890. bool CArmedInstance::needsLastStack() const
  891. {
  892. return false;
  893. }
  894. void CGCreature::onHeroVisit( const CGHeroInstance * h ) const
  895. {
  896. cb->startBattleI(h->id,army,pos,boost::bind(&CGCreature::endBattle,this,_1));
  897. }
  898. void CGCreature::endBattle( BattleResult *result ) const
  899. {
  900. if(result->winner==0)
  901. {
  902. cb->removeObject(id);
  903. }
  904. else
  905. {
  906. int killedAmount=0;
  907. for(std::set<std::pair<ui32,si32> >::iterator i=result->casualties[1].begin(); i!=result->casualties[1].end(); i++)
  908. if(i->first == subID)
  909. killedAmount += i->second;
  910. cb->setAmount(id, army.slots.find(0)->second.second - killedAmount);
  911. MetaString ms;
  912. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  913. pom = 174 + 3*pom + 1;
  914. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  915. cb->setHoverName(id,&ms);
  916. }
  917. }
  918. void CGCreature::initObj()
  919. {
  920. army.slots[0].first = subID;
  921. si32 &amount = army.slots[0].second;
  922. CCreature &c = VLC->creh->creatures[subID];
  923. if(!amount)
  924. if(c.ammMax == c.ammMin)
  925. amount = c.ammMax;
  926. else
  927. amount = c.ammMin + (ran() % (c.ammMax - c.ammMin));
  928. MetaString ms;
  929. int pom = CCreature::getQuantityID(army.slots.find(0)->second.second);
  930. pom = 174 + 3*pom + 1;
  931. ms << std::pair<ui8,ui32>(6,pom) << " " << std::pair<ui8,ui32>(7,subID);
  932. hoverName = toString(ms);
  933. }
  934. void CGMine::onHeroVisit( const CGHeroInstance * h ) const
  935. {
  936. if(subID == 7) //TODO: support for abandoned mine
  937. return;
  938. if(h->tempOwner == tempOwner) //we're visiting our mine
  939. return; //TODO: leaving garrison
  940. //TODO: check if mine is guarded
  941. cb->setOwner(id,h->tempOwner); //not ours? flag it!
  942. MetaString ms;
  943. ms << std::pair<ui8,ui32>(9,subID) << " (" << std::pair<ui8,ui32>(6,23+h->tempOwner) << ")";
  944. cb->setHoverName(id,&ms);
  945. int vv=1; //amount of resource per turn
  946. if (subID==0 || subID==2)
  947. vv++;
  948. else if (subID==6)
  949. vv = 1000;
  950. InfoWindow iw;
  951. iw.text << std::pair<ui8,ui32>(10,subID);
  952. iw.player = h->tempOwner;
  953. iw.components.push_back(Component(2,subID,vv,-1));
  954. cb->showInfoDialog(&iw);
  955. }
  956. void CGMine::newTurn() const
  957. {
  958. if (tempOwner == NEUTRAL_PLAYER)
  959. return;
  960. int vv = 1;
  961. if (subID==0 || subID==2)
  962. vv++;
  963. else if (subID==6)
  964. vv = 1000;
  965. cb->giveResource(tempOwner,subID,vv);
  966. }
  967. void CGMine::initObj()
  968. {
  969. MetaString ms;
  970. ms << std::pair<ui8,ui32>(9,subID);
  971. if(tempOwner >= PLAYER_LIMIT)
  972. tempOwner = NEUTRAL_PLAYER;
  973. else
  974. ms << " (" << std::pair<ui8,ui32>(6,23+tempOwner) << ")";
  975. hoverName = toString(ms);
  976. }
  977. void CGResource::initObj()
  978. {
  979. blockVisit = true;
  980. hoverName = VLC->generaltexth->restypes[subID];
  981. if(!amount)
  982. {
  983. switch(subID)
  984. {
  985. case 6:
  986. amount = 500 + (rand()%6)*100;
  987. break;
  988. case 0: case 2:
  989. amount = 6 + (rand()%5);
  990. break;
  991. default:
  992. amount = 3 + (rand()%3);
  993. break;
  994. }
  995. }
  996. }
  997. void CGResource::onHeroVisit( const CGHeroInstance * h ) const
  998. {
  999. //TODO: handle guards (when battles are finished)
  1000. if(message.length())
  1001. {
  1002. InfoWindow iw;
  1003. iw.player = h->tempOwner;
  1004. iw.text << message;
  1005. cb->showInfoDialog(&iw);
  1006. }
  1007. cb->giveResource(h->tempOwner,subID,amount);
  1008. ShowInInfobox sii;
  1009. sii.player = h->tempOwner;
  1010. sii.c = Component(2,subID,amount,0);
  1011. sii.text << std::pair<ui8,ui32>(11,113);
  1012. sii.text.replacements.push_back(VLC->generaltexth->restypes[subID]);
  1013. cb->showCompInfo(&sii);
  1014. cb->removeObject(id);
  1015. }
  1016. void CGVisitableOPW::newTurn() const
  1017. {
  1018. if (cb->getDate(1)==1) //first day of week
  1019. {
  1020. cb->setObjProperty(id,5,false);
  1021. MetaString ms; //set text to "not visited"
  1022. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,353);
  1023. cb->setHoverName(id,&ms);
  1024. }
  1025. }
  1026. void CGVisitableOPW::onHeroVisit( const CGHeroInstance * h ) const
  1027. {
  1028. int mid;
  1029. switch (ID)
  1030. {
  1031. case 55:
  1032. mid = 92;
  1033. break;
  1034. case 112:
  1035. mid = 170;
  1036. break;
  1037. case 109:
  1038. mid = 164;
  1039. break;
  1040. }
  1041. if (visited)
  1042. {
  1043. if (ID!=112)
  1044. mid++;
  1045. else
  1046. mid--;
  1047. InfoWindow iw;
  1048. iw.player = h->tempOwner;
  1049. iw.text << std::pair<ui8,ui32>(11,mid);
  1050. cb->showInfoDialog(&iw);
  1051. }
  1052. else
  1053. {
  1054. int type, sub, val;
  1055. type = 2;
  1056. switch (ID)
  1057. {
  1058. case 55:
  1059. if (rand()%2)
  1060. {
  1061. sub = 5;
  1062. val = 5;
  1063. }
  1064. else
  1065. {
  1066. sub = 6;
  1067. val = 500;
  1068. }
  1069. break;
  1070. case 112:
  1071. mid = 170;
  1072. sub = (rand() % 5) + 1;
  1073. val = (rand() % 4) + 3;
  1074. break;
  1075. case 109:
  1076. mid = 164;
  1077. sub = 6;
  1078. if(cb->getDate(2)<2)
  1079. val = 500;
  1080. else
  1081. val = 1000;
  1082. }
  1083. cb->giveResource(h->tempOwner,sub,val);
  1084. InfoWindow iw;
  1085. iw.player = h->tempOwner;
  1086. iw.components.push_back(Component(type,sub,val,0));
  1087. iw.text << std::pair<ui8,ui32>(11,mid);
  1088. cb->showInfoDialog(&iw);
  1089. cb->setObjProperty(id,5,true);
  1090. MetaString ms; //set text to "visited"
  1091. ms << std::pair<ui8,ui32>(3,ID) << " " << std::pair<ui8,ui32>(1,352);
  1092. cb->setHoverName(id,&ms);
  1093. }
  1094. }
  1095. void CGTeleport::onHeroVisit( const CGHeroInstance * h ) const
  1096. {
  1097. int destinationid=-1;
  1098. switch(ID)
  1099. {
  1100. case 43: //one way - find correspong exit monolith
  1101. if(vstd::contains(objs,44) && vstd::contains(objs[44],subID) && objs[44][subID].size())
  1102. destinationid = objs[44][subID][rand()%objs[44][subID].size()];
  1103. else
  1104. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1105. break;
  1106. case 45: //two way monolith - pick any other one
  1107. if(vstd::contains(objs,45) && vstd::contains(objs[45],subID) && objs[45][subID].size()>1)
  1108. while ((destinationid = objs[45][subID][rand()%objs[45][subID].size()])==id);
  1109. else
  1110. tlog2 << "Cannot find corresponding exit monolith for "<< id << std::endl;
  1111. break;
  1112. case 103: //find nearest subterranean gate on the other level
  1113. {
  1114. std::pair<int,double> best(-1,150000); //pair<id,dist>
  1115. for(int i=0; i<objs[103][0].size(); i++)
  1116. {
  1117. if(cb->getObj(objs[103][0][i])->pos.z == pos.z) continue; //gates on our level are not interesting
  1118. double hlp = cb->getObj(objs[103][0][i])->pos.dist2d(pos);
  1119. if(hlp<best.second)
  1120. {
  1121. best.first = objs[103][0][i];
  1122. best.second = hlp;
  1123. }
  1124. }
  1125. if(best.first<0)
  1126. return;
  1127. else
  1128. destinationid = best.first;
  1129. break;
  1130. }
  1131. }
  1132. if(destinationid < 0)
  1133. {
  1134. tlog2 << "Cannot find exit... :( \n";
  1135. return;
  1136. }
  1137. cb->moveHero(h->id,
  1138. (ID!=103)
  1139. ? (CGHeroInstance::convertPosition(cb->getObj(destinationid)->pos,true))
  1140. : (cb->getObj(destinationid)->pos),
  1141. true);
  1142. }
  1143. void CGTeleport::initObj()
  1144. {
  1145. objs[ID][subID].push_back(id);
  1146. }
  1147. void CGArtifact::initObj()
  1148. {
  1149. blockVisit = true;
  1150. if(ID == 5)
  1151. hoverName = VLC->arth->artifacts[subID].Name();
  1152. }
  1153. void CGArtifact::onHeroVisit( const CGHeroInstance * h ) const
  1154. {
  1155. cb->giveHeroArtifact(subID,h->id,-2);
  1156. InfoWindow iw;
  1157. iw.player = h->tempOwner;
  1158. iw.components.push_back(Component(4,subID,0,0));
  1159. iw.text << std::pair<ui8,ui32>(12,subID);
  1160. cb->showInfoDialog(&iw);
  1161. }
  1162. void CGPickable::initObj()
  1163. {
  1164. blockVisit = true;
  1165. switch(ID)
  1166. {
  1167. case 12: //campfire
  1168. val2 = (ran()%3) + 4; //4 - 6
  1169. val1 = val2 * 100;
  1170. type = ran()%6; //given resource
  1171. break;
  1172. case 101: //treasure chest
  1173. {
  1174. int hlp = ran()%100;
  1175. if(hlp >= 95)
  1176. {
  1177. type = 1;
  1178. val1 = VLC->arth->treasures[ran()%VLC->arth->treasures.size()]->id;
  1179. return;
  1180. }
  1181. else if (hlp >= 65)
  1182. {
  1183. val1 = 2000;
  1184. }
  1185. else if(hlp >= 33)
  1186. {
  1187. val1 = 1500;
  1188. }
  1189. else
  1190. {
  1191. val1 = 1000;
  1192. }
  1193. val2 = val1 - 500;
  1194. type = 0;
  1195. break;
  1196. }
  1197. }
  1198. }
  1199. void CGPickable::onHeroVisit( const CGHeroInstance * h ) const
  1200. {
  1201. switch(ID)
  1202. {
  1203. case 12: //campfire
  1204. {
  1205. cb->giveResource(h->tempOwner,type,val2); //non-gold resource
  1206. cb->giveResource(h->tempOwner,6,val1);//gold
  1207. InfoWindow iw;
  1208. iw.player = h->tempOwner;
  1209. iw.components.push_back(Component(2,6,val1,0));
  1210. iw.components.push_back(Component(2,type,val2,0));
  1211. iw.text << std::pair<ui8,ui32>(11,23);
  1212. cb->showInfoDialog(&iw);
  1213. break;
  1214. }
  1215. case 101: //treasure chest
  1216. {
  1217. if (subID) //not OH3 treasure chest
  1218. {
  1219. tlog2 << "Not supported WoG treasure chest!\n";
  1220. return;
  1221. }
  1222. if(type) //there is an artifact
  1223. {
  1224. cb->giveHeroArtifact(val1,h->id,-2);
  1225. InfoWindow iw;
  1226. iw.player = h->tempOwner;
  1227. iw.components.push_back(Component(4,val1,1,0));
  1228. iw.text << std::pair<ui8,ui32>(11,145);
  1229. iw.text.replacements.push_back(VLC->arth->artifacts[val1].Name());
  1230. cb->showInfoDialog(&iw);
  1231. break;
  1232. }
  1233. else
  1234. {
  1235. SelectionDialog sd;
  1236. sd.player = h->tempOwner;
  1237. sd.text << std::pair<ui8,ui32>(11,146);
  1238. sd.components.push_back(Component(2,6,val1,0));
  1239. sd.components.push_back(Component(5,0,val2,0));
  1240. boost::function<void(ui32)> fun = boost::bind(&CGPickable::chosen,this,_1,h->id);
  1241. cb->showSelectionDialog(&sd,fun);
  1242. return;
  1243. }
  1244. }
  1245. }
  1246. cb->removeObject(id);
  1247. }
  1248. void CGPickable::chosen( int which, int heroID ) const
  1249. {
  1250. switch(which)
  1251. {
  1252. case 0: //player pick gold
  1253. cb->giveResource(cb->getOwner(heroID),6,val1);
  1254. break;
  1255. case 1: //player pick exp
  1256. cb->changePrimSkill(heroID, 4, val2);
  1257. break;
  1258. default:
  1259. throw std::string("Unhandled treasure choice");
  1260. }
  1261. }