CObjectHandler.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672
  1. #define VCMI_DLL
  2. #include "../stdafx.h"
  3. #include "CObjectHandler.h"
  4. #include "CDefObjInfoHandler.h"
  5. #include "CLodHandler.h"
  6. #include "CDefObjInfoHandler.h"
  7. #include "CHeroHandler.h"
  8. #include <boost/algorithm/string/replace.hpp>
  9. #include <boost/random/linear_congruential.hpp>
  10. #include "CTownHandler.h"
  11. #include "CArtHandler.h"
  12. #include "../lib/VCMI_Lib.h"
  13. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  14. extern CLodHandler * bitmaph;
  15. extern boost::rand48 ran;
  16. void CObjectHandler::loadObjects()
  17. {
  18. VLC->objh = this;
  19. int ID=0;
  20. tlog5 << "\t\tReading OBJNAMES \n";
  21. std::string buf = bitmaph->getTextFile("OBJNAMES.TXT");
  22. int it=0;
  23. while (it<buf.length()-1)
  24. {
  25. std::string nobj;
  26. loadToIt(nobj,buf,it,3);
  27. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  28. nobj = nobj.substr(0, nobj.size()-1);
  29. names.push_back(nobj);
  30. }
  31. tlog5 << "\t\tReading ADVEVENT \n";
  32. buf = bitmaph->getTextFile("ADVEVENT.TXT");
  33. it=0;
  34. std::string temp;
  35. while (it<buf.length()-1)
  36. {
  37. loadToIt(temp,buf,it,3);
  38. if (temp[0]=='\"')
  39. temp = temp.substr(1,temp.length()-2);
  40. boost::algorithm::replace_all(temp,"\"\"","\"");
  41. advobtxt.push_back(temp);
  42. }
  43. tlog5 << "\t\tReading XTRAINFO \n";
  44. buf = bitmaph->getTextFile("XTRAINFO.TXT");
  45. it=0;
  46. while (it<buf.length()-1)
  47. {
  48. loadToIt(temp,buf,it,3);
  49. xtrainfo.push_back(temp);
  50. }
  51. tlog5 << "\t\tReading MINENAME \n";
  52. buf = bitmaph->getTextFile("MINENAME.TXT");
  53. it=0;
  54. while (it<buf.length()-1)
  55. {
  56. loadToIt(temp,buf,it,3);
  57. mines.push_back(std::pair<std::string,std::string>(temp,""));
  58. }
  59. tlog5 << "\t\tReading MINEEVNT \n";
  60. buf = bitmaph->getTextFile("MINEEVNT.TXT");
  61. it=0;
  62. int i=0;
  63. while (it<buf.length()-1)
  64. {
  65. loadToIt(temp,buf,it,3);
  66. temp = temp.substr(1,temp.length()-2);
  67. mines[i++].second = temp;
  68. }
  69. tlog5 << "\t\tReading RESTYPES \n";
  70. buf = bitmaph->getTextFile("RESTYPES.TXT");
  71. it=0;
  72. while (it<buf.length()-1)
  73. {
  74. loadToIt(temp,buf,it,3);
  75. restypes.push_back(temp);
  76. }
  77. tlog5 << "\t\tReading cregens \n";
  78. cregens.resize(110); //TODO: hardcoded value - change
  79. for(int i=0; i<cregens.size();i++)
  80. cregens[i]=-1;
  81. std::ifstream ifs("config/cregens.txt");
  82. while(!ifs.eof())
  83. {
  84. int dw, cr;
  85. ifs >> dw >> cr;
  86. cregens[dw]=cr;
  87. }
  88. ifs.close();
  89. ifs.clear();
  90. tlog5 << "\t\tReading ZCRGN1 \n";
  91. buf = bitmaph->getTextFile("ZCRGN1.TXT");
  92. it=0;
  93. while (it<buf.length()-1)
  94. {
  95. loadToIt(temp,buf,it,3);
  96. creGens.push_back(temp);
  97. }
  98. tlog5 << "\t\tDone loading objects!\n";
  99. }
  100. bool CGObjectInstance::isHero() const
  101. {
  102. return false;
  103. }
  104. int CGObjectInstance::getOwner() const
  105. {
  106. //if (state)
  107. // return state->owner;
  108. //else
  109. return tempOwner; //won't have owner
  110. }
  111. void CGObjectInstance::setOwner(int ow)
  112. {
  113. //if (state)
  114. // state->owner = ow;
  115. //else
  116. tempOwner = ow;
  117. }
  118. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  119. {
  120. return defInfo->width;
  121. }
  122. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  123. {
  124. return defInfo->width;
  125. }
  126. 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)
  127. {
  128. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  129. return false;
  130. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  131. return true;
  132. return false;
  133. }
  134. bool CGObjectInstance::blockingAt(int x, int y) const
  135. {
  136. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  137. return false;
  138. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  139. return true;
  140. return false;
  141. }
  142. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  143. {
  144. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  145. return true;
  146. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  147. return false;
  148. if(this->pos.y<cmp.pos.y)
  149. return true;
  150. if(this->pos.y>cmp.pos.y)
  151. return false;
  152. if(cmp.ID==34 && ID!=34)
  153. return true;
  154. if(cmp.ID!=34 && ID==34)
  155. return false;
  156. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  157. return true;
  158. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  159. return false;
  160. if(this->pos.x<cmp.pos.x)
  161. return true;
  162. return false;
  163. }
  164. bool CGHeroInstance::isHero() const
  165. {
  166. return true;
  167. }
  168. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype) const
  169. {
  170. unsigned int ret = type->heroClass->terrCosts[ttype];
  171. //applying pathfinding skill
  172. switch(getSecSkillLevel(0))
  173. {
  174. case 1: //basic
  175. switch(ttype)
  176. {
  177. case rough:
  178. ret = 100;
  179. break;
  180. case sand: case snow:
  181. if(ret>125)
  182. ret = 125;
  183. break;
  184. case swamp:
  185. if(ret>150)
  186. ret = 150;
  187. break;
  188. }
  189. break;
  190. case 2: //advanced
  191. switch(ttype)
  192. {
  193. case rough: case sand: case snow:
  194. ret = 100;
  195. break;
  196. case swamp:
  197. if(ret>125)
  198. ret = 125;
  199. break;
  200. }
  201. break;
  202. case 3: //expert
  203. ret = 100;
  204. break;
  205. }
  206. //calculating road influence
  207. switch(rdtype)
  208. {
  209. case dirtRoad:
  210. ret*=0.75;
  211. break;
  212. case grazvelRoad:
  213. ret*=0.667;
  214. break;
  215. case cobblestoneRoad:
  216. ret*=0.5;
  217. break;
  218. }
  219. return ret;
  220. }
  221. unsigned int CGHeroInstance::getLowestCreatureSpeed()
  222. {
  223. unsigned int sl = 100;
  224. for(int h=0; h<army.slots.size(); ++h)
  225. {
  226. if(VLC->creh->creatures[army.slots[h].first].speed<sl)
  227. sl = VLC->creh->creatures[army.slots[h].first].speed;
  228. }
  229. return sl;
  230. }
  231. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  232. {
  233. if (toh3m)
  234. {
  235. src.x+=1;
  236. return src;
  237. }
  238. else
  239. {
  240. src.x-=1;
  241. return src;
  242. }
  243. }
  244. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  245. {
  246. if (h3m)
  247. return pos;
  248. else return convertPosition(pos,false);
  249. }
  250. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  251. {
  252. return 6 + getSecSkillLevel(3); //default + scouting
  253. }
  254. int CGHeroInstance::manaLimit() const
  255. {
  256. double modifier = 1.0;
  257. switch(getSecSkillLevel(24)) //intelligence level
  258. {
  259. case 1: modifier+=0.25; break;
  260. case 2: modifier+=0.5; break;
  261. case 3: modifier+=1.0; break;
  262. }
  263. return 10*getPrimSkillLevel(3)*modifier;
  264. }
  265. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  266. //{
  267. // if (h3m)
  268. // pos = Pos;
  269. // else
  270. // pos = convertPosition(Pos,true);
  271. //}
  272. bool CGHeroInstance::canWalkOnSea() const
  273. {
  274. //TODO: write it - it should check if hero is flying, or something similiar
  275. return false;
  276. }
  277. int CGHeroInstance::getCurrentLuck() const
  278. {
  279. //TODO: write it
  280. return 0;
  281. }
  282. int CGHeroInstance::getCurrentMorale() const
  283. {
  284. //TODO: write it
  285. return 0;
  286. }
  287. int CGHeroInstance::getPrimSkillLevel(int id) const
  288. {
  289. return primSkills[id];
  290. }
  291. int CGHeroInstance::getSecSkillLevel(const int & ID) const
  292. {
  293. for(int i=0;i<secSkills.size();i++)
  294. if(secSkills[i].first==ID)
  295. return secSkills[i].second;
  296. return 0;
  297. }
  298. int lowestSpeed(const CGHeroInstance * chi)
  299. {
  300. if(!chi->army.slots.size())
  301. {
  302. tlog1 << "Error! Hero " << chi->id << " ("<<chi->name<<") has no army!\n";
  303. return 20;
  304. }
  305. std::map<si32,std::pair<ui32,si32> >::const_iterator i = chi->army.slots.begin();
  306. int ret = VLC->creh->creatures[(*i++).second.first].speed;
  307. for (;i!=chi->army.slots.end();i++)
  308. {
  309. ret = std::min(ret,VLC->creh->creatures[(*i).second.first].speed);
  310. }
  311. return ret;
  312. }
  313. int CGHeroInstance::maxMovePoints(bool onLand) const
  314. {
  315. int ret = 1270+70*lowestSpeed(this);
  316. if (ret>2000)
  317. ret=2000;
  318. if(onLand)
  319. {
  320. //logistics:
  321. switch(getSecSkillLevel(2))
  322. {
  323. case 1:
  324. ret *= 1.1f;
  325. break;
  326. case 2:
  327. ret *= 1.2f;
  328. break;
  329. case 3:
  330. ret *= 1.3f;
  331. break;
  332. }
  333. }
  334. else
  335. {
  336. //navigation:
  337. switch(getSecSkillLevel(2))
  338. {
  339. case 1:
  340. ret *= 1.5f;
  341. break;
  342. case 2:
  343. ret *= 2.0f;
  344. break;
  345. case 3:
  346. ret *= 2.5f;
  347. break;
  348. }
  349. }
  350. return ret;
  351. }
  352. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  353. {
  354. if(pos<19)
  355. if(vstd::contains(artifWorn,pos))
  356. return artifWorn.find(pos)->second;
  357. else
  358. return -1;
  359. else
  360. if(pos-19 < artifacts.size())
  361. return artifacts[pos-19];
  362. else
  363. return -1;
  364. }
  365. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  366. {
  367. if(art<0)
  368. {
  369. if(pos<19)
  370. artifWorn.erase(pos);
  371. else
  372. artifacts -= artifacts[pos-19];
  373. }
  374. else
  375. {
  376. if(pos<19)
  377. artifWorn[pos] = art;
  378. else
  379. if(pos-19 < artifacts.size())
  380. artifacts[pos-19] = art;
  381. else
  382. artifacts.push_back(art);
  383. }
  384. }
  385. const CArtifact * CGHeroInstance::getArt(int pos) const
  386. {
  387. int id = getArtAtPos(pos);
  388. if(id>=0)
  389. return &VLC->arth->artifacts[id];
  390. else
  391. return NULL;
  392. }
  393. int CGTownInstance::getSightDistance() const //returns sight distance
  394. {
  395. return 10;
  396. }
  397. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  398. {
  399. if((builtBuildings.find(9))!=builtBuildings.end())
  400. return 3;
  401. if((builtBuildings.find(8))!=builtBuildings.end())
  402. return 2;
  403. if((builtBuildings.find(7))!=builtBuildings.end())
  404. return 1;
  405. return 0;
  406. }
  407. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  408. {
  409. if ((builtBuildings.find(13))!=builtBuildings.end())
  410. return 3;
  411. if ((builtBuildings.find(12))!=builtBuildings.end())
  412. return 2;
  413. if ((builtBuildings.find(11))!=builtBuildings.end())
  414. return 1;
  415. if ((builtBuildings.find(10))!=builtBuildings.end())
  416. return 0;
  417. return -1;
  418. }
  419. int CGTownInstance::mageGuildLevel() const
  420. {
  421. if ((builtBuildings.find(4))!=builtBuildings.end())
  422. return 5;
  423. if ((builtBuildings.find(3))!=builtBuildings.end())
  424. return 4;
  425. if ((builtBuildings.find(2))!=builtBuildings.end())
  426. return 3;
  427. if ((builtBuildings.find(1))!=builtBuildings.end())
  428. return 2;
  429. if ((builtBuildings.find(0))!=builtBuildings.end())
  430. return 1;
  431. return 0;
  432. }
  433. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  434. {
  435. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  436. }
  437. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  438. {
  439. return town->hordeLvl[HID];
  440. }
  441. int CGTownInstance::creatureGrowth(const int & level) const
  442. {
  443. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  444. switch(fortLevel())
  445. {
  446. case 3:
  447. ret*=2;break;
  448. case 2:
  449. ret*=(1.5); break;
  450. }
  451. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  452. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  453. if(getHordeLevel(0)==level)
  454. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  455. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  456. if(getHordeLevel(1)==level)
  457. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  458. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  459. return ret;
  460. }
  461. int CGTownInstance::dailyIncome() const
  462. {
  463. int ret = 0;
  464. if ((builtBuildings.find(26))!=builtBuildings.end())
  465. ret+=5000;
  466. if ((builtBuildings.find(13))!=builtBuildings.end())
  467. ret+=4000;
  468. else if ((builtBuildings.find(12))!=builtBuildings.end())
  469. ret+=2000;
  470. else if ((builtBuildings.find(11))!=builtBuildings.end())
  471. ret+=1000;
  472. else if ((builtBuildings.find(10))!=builtBuildings.end())
  473. ret+=500;
  474. return ret;
  475. }
  476. bool CGTownInstance::hasFort() const
  477. {
  478. return (builtBuildings.find(7))!=builtBuildings.end();
  479. }
  480. bool CGTownInstance::hasCapitol() const
  481. {
  482. return (builtBuildings.find(13))!=builtBuildings.end();
  483. }
  484. CGTownInstance::CGTownInstance()
  485. {
  486. builded=-1;
  487. destroyed=-1;
  488. garrisonHero=NULL;
  489. town=NULL;
  490. visitingHero = NULL;
  491. }
  492. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  493. {
  494. pos = int3(-1,-1,-1);
  495. //std::cout << "Tworze obiekt "<<this<<std::endl;
  496. //state = new CLuaObjectScript();
  497. ID = subID = id = -1;
  498. defInfo = NULL;
  499. state = NULL;
  500. info = NULL;
  501. tempOwner = 254;
  502. blockVisit = false;
  503. }
  504. CGObjectInstance::~CGObjectInstance()
  505. {
  506. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  507. //if (state)
  508. // delete state;
  509. //state=NULL;
  510. }
  511. CGHeroInstance::CGHeroInstance()
  512. {
  513. ID = 34;
  514. tacticFormationEnabled = inTownGarrison = false;
  515. mana = movement = portrait = level = -1;
  516. isStanding = true;
  517. moveDir = 4;
  518. exp = 0;
  519. visitedTown = NULL;
  520. type = NULL;
  521. secSkills.push_back(std::make_pair(-1, -1));
  522. }
  523. void CGHeroInstance::initHero(int SUBID)
  524. {
  525. subID = SUBID;
  526. initHero();
  527. }
  528. void CGHeroInstance::initHero()
  529. {
  530. if(!defInfo)
  531. {
  532. defInfo = new CGDefInfo();
  533. defInfo->id = 34;
  534. defInfo->subid = subID;
  535. defInfo->printPriority = 0;
  536. defInfo->visitDir = 0xff;
  537. }
  538. if(!type)
  539. type = VLC->heroh->heroes[subID];
  540. for(int i=0;i<6;i++)
  541. {
  542. defInfo->blockMap[i]=255;
  543. defInfo->visitMap[i]=0;
  544. }
  545. defInfo->handler=NULL;
  546. defInfo->blockMap[5] = 253;
  547. defInfo->visitMap[5] = 2;
  548. artifWorn[16] = 3;
  549. if(type->heroType % 2 == 1) //it's a magical hero
  550. {
  551. artifWorn[17] = 0; //give him spellbook
  552. }
  553. if(portrait < 0)
  554. portrait = subID;
  555. if((!primSkills.size()) || (getPrimSkillLevel(0)<0))
  556. {
  557. primSkills.resize(4);
  558. primSkills[0] = type->heroClass->initialAttack;
  559. primSkills[1] = type->heroClass->initialDefence;
  560. primSkills[2] = type->heroClass->initialPower;
  561. primSkills[3] = type->heroClass->initialKnowledge;
  562. }
  563. if(secSkills.size() == 1 && secSkills[0] == std::make_pair(-1, -1)) //set secondary skills to default
  564. secSkills = type->secSkillsInit;
  565. if(mana < 0)
  566. mana = manaLimit();
  567. if (!name.length())
  568. name = type->name;
  569. if (!biography.length())
  570. biography = type->biography;
  571. if (level<1)
  572. {
  573. exp=40+ (ran()) % 50;
  574. level = 1;
  575. }
  576. if (!army.slots.size()) //standard army//initial army
  577. {
  578. int pom, pom2=0;
  579. for(int x=0;x<3;x++)
  580. {
  581. pom = (VLC->creh->nameToID[type->refTypeStack[x]]);
  582. if(pom>=145 && pom<=149) //war machine
  583. {
  584. pom2++;
  585. switch (pom)
  586. {
  587. case 145: //catapult
  588. artifWorn[16] = 3;
  589. break;
  590. default:
  591. artifWorn[9+CArtHandler::convertMachineID(pom,true)] = CArtHandler::convertMachineID(pom,true);
  592. break;
  593. }
  594. continue;
  595. }
  596. army.slots[x-pom2].first = pom;
  597. if((pom = (type->highStack[x]-type->lowStack[x])) > 0)
  598. army.slots[x-pom2].second = (ran()%pom)+type->lowStack[x];
  599. else
  600. army.slots[x-pom2].second = +type->lowStack[x];
  601. army.formation = false;
  602. }
  603. }
  604. }
  605. CGHeroInstance::~CGHeroInstance()
  606. {
  607. }
  608. CGTownInstance::~CGTownInstance()
  609. {}
  610. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  611. {
  612. if(checkGuild && mageGuildLevel() < level)
  613. return 0;
  614. int ret = 6 - level; //how many spells are available at this level
  615. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  616. ret++;
  617. return ret;
  618. }
  619. CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  620. {
  621. pos = right.pos;
  622. ID = right.ID;
  623. subID = right.subID;
  624. id = right.id;
  625. defInfo = right.defInfo;
  626. info = right.info;
  627. blockVisit = right.blockVisit;
  628. //state = new CLuaObjectScript(right.state->);
  629. //*state = *right.state;
  630. //state = right.state;
  631. tempOwner = right.tempOwner;
  632. }
  633. CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  634. {
  635. pos = right.pos;
  636. ID = right.ID;
  637. subID = right.subID;
  638. id = right.id;
  639. defInfo = right.defInfo;
  640. info = right.info;
  641. blockVisit = right.blockVisit;
  642. //state = new CLuaObjectScript();
  643. //*state = *right.state;
  644. tempOwner = right.tempOwner;
  645. return *this;
  646. }