CObjectHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522
  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 "CTownHandler.h"
  10. #include "CArtHandler.h"
  11. #include "../lib/VCMI_Lib.h"
  12. DLL_EXPORT void loadToIt(std::string &dest, std::string &src, int &iter, int mode);
  13. extern CLodHandler * bitmaph;
  14. void CObjectHandler::loadObjects()
  15. {
  16. VLC->objh = this;
  17. int ID=0;
  18. tlog5 << "\t\tReading OBJNAMES \n";
  19. std::string buf = bitmaph->getTextFile("OBJNAMES.TXT");
  20. int it=0;
  21. while (it<buf.length()-1)
  22. {
  23. std::string nobj;
  24. loadToIt(nobj,buf,it,3);
  25. if(nobj.size() && (nobj[nobj.size()-1]==(char)10 || nobj[nobj.size()-1]==(char)13 || nobj[nobj.size()-1]==(char)9))
  26. nobj = nobj.substr(0, nobj.size()-1);
  27. names.push_back(nobj);
  28. }
  29. tlog5 << "\t\tReading ADVEVENT \n";
  30. buf = bitmaph->getTextFile("ADVEVENT.TXT");
  31. it=0;
  32. std::string temp;
  33. while (it<buf.length()-1)
  34. {
  35. loadToIt(temp,buf,it,3);
  36. if (temp[0]=='\"')
  37. temp = temp.substr(1,temp.length()-2);
  38. boost::algorithm::replace_all(temp,"\"\"","\"");
  39. advobtxt.push_back(temp);
  40. }
  41. tlog5 << "\t\tReading XTRAINFO \n";
  42. buf = bitmaph->getTextFile("XTRAINFO.TXT");
  43. it=0;
  44. while (it<buf.length()-1)
  45. {
  46. loadToIt(temp,buf,it,3);
  47. xtrainfo.push_back(temp);
  48. }
  49. tlog5 << "\t\tReading MINENAME \n";
  50. buf = bitmaph->getTextFile("MINENAME.TXT");
  51. it=0;
  52. while (it<buf.length()-1)
  53. {
  54. loadToIt(temp,buf,it,3);
  55. mines.push_back(std::pair<std::string,std::string>(temp,""));
  56. }
  57. tlog5 << "\t\tReading MINEEVNT \n";
  58. buf = bitmaph->getTextFile("MINEEVNT.TXT");
  59. it=0;
  60. int i=0;
  61. while (it<buf.length()-1)
  62. {
  63. loadToIt(temp,buf,it,3);
  64. temp = temp.substr(1,temp.length()-2);
  65. mines[i++].second = temp;
  66. }
  67. tlog5 << "\t\tReading RESTYPES \n";
  68. buf = bitmaph->getTextFile("RESTYPES.TXT");
  69. it=0;
  70. while (it<buf.length()-1)
  71. {
  72. loadToIt(temp,buf,it,3);
  73. restypes.push_back(temp);
  74. }
  75. tlog5 << "\t\tReading cregens \n";
  76. cregens.resize(110); //TODO: hardcoded value - change
  77. for(int i=0; i<cregens.size();i++)
  78. cregens[i]=-1;
  79. std::ifstream ifs("config/cregens.txt");
  80. while(!ifs.eof())
  81. {
  82. int dw, cr;
  83. ifs >> dw >> cr;
  84. cregens[dw]=cr;
  85. }
  86. ifs.close();
  87. ifs.clear();
  88. tlog5 << "\t\tReading ZCRGN1 \n";
  89. buf = bitmaph->getTextFile("ZCRGN1.TXT");
  90. it=0;
  91. while (it<buf.length()-1)
  92. {
  93. loadToIt(temp,buf,it,3);
  94. creGens.push_back(temp);
  95. }
  96. tlog5 << "\t\tDone loading objects!\n";
  97. }
  98. bool CGObjectInstance::isHero() const
  99. {
  100. return false;
  101. }
  102. int CGObjectInstance::getOwner() const
  103. {
  104. //if (state)
  105. // return state->owner;
  106. //else
  107. return tempOwner; //won't have owner
  108. }
  109. void CGObjectInstance::setOwner(int ow)
  110. {
  111. //if (state)
  112. // state->owner = ow;
  113. //else
  114. tempOwner = ow;
  115. }
  116. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  117. {
  118. return defInfo->width;
  119. }
  120. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  121. {
  122. return defInfo->width;
  123. }
  124. 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)
  125. {
  126. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  127. return false;
  128. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  129. return true;
  130. return false;
  131. }
  132. bool CGObjectInstance::blockingAt(int x, int y) const
  133. {
  134. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  135. return false;
  136. if((defInfo->blockMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  137. return true;
  138. return false;
  139. }
  140. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  141. {
  142. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  143. return true;
  144. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  145. return false;
  146. if(this->pos.y<cmp.pos.y)
  147. return true;
  148. if(this->pos.y>cmp.pos.y)
  149. return false;
  150. if(cmp.ID==34 && ID!=34)
  151. return true;
  152. if(cmp.ID!=34 && ID==34)
  153. return false;
  154. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  155. return true;
  156. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  157. return false;
  158. if(this->pos.x<cmp.pos.x)
  159. return true;
  160. return false;
  161. }
  162. bool CGHeroInstance::isHero() const
  163. {
  164. return true;
  165. }
  166. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype) const
  167. {
  168. unsigned int ret = type->heroClass->terrCosts[ttype];
  169. //applying pathfinding skill
  170. switch(getSecSkillLevel(0))
  171. {
  172. case 1: //basic
  173. switch(ttype)
  174. {
  175. case rough:
  176. ret = 100;
  177. break;
  178. case sand: case snow:
  179. if(ret>125)
  180. ret = 125;
  181. break;
  182. case swamp:
  183. if(ret>150)
  184. ret = 150;
  185. break;
  186. }
  187. break;
  188. case 2: //advanced
  189. switch(ttype)
  190. {
  191. case rough: case sand: case snow:
  192. ret = 100;
  193. break;
  194. case swamp:
  195. if(ret>125)
  196. ret = 125;
  197. break;
  198. }
  199. break;
  200. case 3: //expert
  201. ret = 100;
  202. break;
  203. }
  204. //calculating road influence
  205. switch(rdtype)
  206. {
  207. case dirtRoad:
  208. ret*=0.75;
  209. break;
  210. case grazvelRoad:
  211. ret*=0.667;
  212. break;
  213. case cobblestoneRoad:
  214. ret*=0.5;
  215. break;
  216. }
  217. return ret;
  218. }
  219. unsigned int CGHeroInstance::getLowestCreatureSpeed()
  220. {
  221. unsigned int sl = 100;
  222. for(int h=0; h<army.slots.size(); ++h)
  223. {
  224. if(VLC->creh->creatures[army.slots[h].first].speed<sl)
  225. sl = VLC->creh->creatures[army.slots[h].first].speed;
  226. }
  227. return sl;
  228. }
  229. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  230. {
  231. if (toh3m)
  232. {
  233. src.x+=1;
  234. return src;
  235. }
  236. else
  237. {
  238. src.x-=1;
  239. return src;
  240. }
  241. }
  242. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  243. {
  244. if (h3m)
  245. return pos;
  246. else return convertPosition(pos,false);
  247. }
  248. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  249. {
  250. return 6 + getSecSkillLevel(3); //default + scouting
  251. }
  252. int CGHeroInstance::manaLimit() const
  253. {
  254. double modifier = 1.0;
  255. switch(getSecSkillLevel(24)) //intelligence level
  256. {
  257. case 1: modifier+=0.25; break;
  258. case 2: modifier+=0.5; break;
  259. case 3: modifier+=1.0; break;
  260. }
  261. return 10*primSkills[3]*modifier;
  262. }
  263. //void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  264. //{
  265. // if (h3m)
  266. // pos = Pos;
  267. // else
  268. // pos = convertPosition(Pos,true);
  269. //}
  270. bool CGHeroInstance::canWalkOnSea() const
  271. {
  272. //TODO: write it - it should check if hero is flying, or something similiar
  273. return false;
  274. }
  275. int CGHeroInstance::getCurrentLuck() const
  276. {
  277. //TODO: write it
  278. return 0;
  279. }
  280. int CGHeroInstance::getCurrentMorale() const
  281. {
  282. //TODO: write it
  283. return 0;
  284. }
  285. int CGHeroInstance::getPrimSkillLevel(int id) const
  286. {
  287. return primSkills[id];
  288. }
  289. int CGHeroInstance::getSecSkillLevel(const int & ID) const
  290. {
  291. for(int i=0;i<secSkills.size();i++)
  292. if(secSkills[i].first==ID)
  293. return secSkills[i].second;
  294. return 0;
  295. }
  296. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  297. {
  298. if(pos<19)
  299. if(vstd::contains(artifWorn,pos))
  300. return artifWorn.find(pos)->second;
  301. else
  302. return -1;
  303. else
  304. if(pos-19 < artifacts.size())
  305. return artifacts[pos-19];
  306. else
  307. return -1;
  308. }
  309. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  310. {
  311. if(art<0)
  312. {
  313. if(pos<19)
  314. artifWorn.erase(pos);
  315. else
  316. artifacts -= artifacts[pos-19];
  317. }
  318. else
  319. {
  320. if(pos<19)
  321. artifWorn[pos] = art;
  322. else
  323. if(pos-19 < artifacts.size())
  324. artifacts[pos-19] = art;
  325. else
  326. artifacts.push_back(art);
  327. }
  328. }
  329. const CArtifact * CGHeroInstance::getArt(int pos) const
  330. {
  331. int id = getArtAtPos(pos);
  332. if(id>=0)
  333. return &VLC->arth->artifacts[id];
  334. else
  335. return NULL;
  336. }
  337. int CGTownInstance::getSightDistance() const //returns sight distance
  338. {
  339. return 10;
  340. }
  341. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  342. {
  343. if((builtBuildings.find(9))!=builtBuildings.end())
  344. return 3;
  345. if((builtBuildings.find(8))!=builtBuildings.end())
  346. return 2;
  347. if((builtBuildings.find(7))!=builtBuildings.end())
  348. return 1;
  349. return 0;
  350. }
  351. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  352. {
  353. if ((builtBuildings.find(13))!=builtBuildings.end())
  354. return 3;
  355. if ((builtBuildings.find(12))!=builtBuildings.end())
  356. return 2;
  357. if ((builtBuildings.find(11))!=builtBuildings.end())
  358. return 1;
  359. if ((builtBuildings.find(10))!=builtBuildings.end())
  360. return 0;
  361. return -1;
  362. }
  363. int CGTownInstance::mageGuildLevel() const
  364. {
  365. if ((builtBuildings.find(4))!=builtBuildings.end())
  366. return 5;
  367. if ((builtBuildings.find(3))!=builtBuildings.end())
  368. return 4;
  369. if ((builtBuildings.find(2))!=builtBuildings.end())
  370. return 3;
  371. if ((builtBuildings.find(1))!=builtBuildings.end())
  372. return 2;
  373. if ((builtBuildings.find(0))!=builtBuildings.end())
  374. return 1;
  375. return 0;
  376. }
  377. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  378. {
  379. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  380. }
  381. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  382. {
  383. return town->hordeLvl[HID];
  384. }
  385. int CGTownInstance::creatureGrowth(const int & level) const
  386. {
  387. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  388. switch(fortLevel())
  389. {
  390. case 3:
  391. ret*=2;break;
  392. case 2:
  393. ret*=(1.5); break;
  394. }
  395. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  396. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  397. if(getHordeLevel(0)==level)
  398. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  399. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  400. if(getHordeLevel(1)==level)
  401. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  402. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  403. return ret;
  404. }
  405. int CGTownInstance::dailyIncome() const
  406. {
  407. int ret = 0;
  408. if ((builtBuildings.find(26))!=builtBuildings.end())
  409. ret+=5000;
  410. if ((builtBuildings.find(13))!=builtBuildings.end())
  411. ret+=4000;
  412. else if ((builtBuildings.find(12))!=builtBuildings.end())
  413. ret+=2000;
  414. else if ((builtBuildings.find(11))!=builtBuildings.end())
  415. ret+=1000;
  416. else if ((builtBuildings.find(10))!=builtBuildings.end())
  417. ret+=500;
  418. return ret;
  419. }
  420. bool CGTownInstance::hasFort() const
  421. {
  422. return (builtBuildings.find(7))!=builtBuildings.end();
  423. }
  424. bool CGTownInstance::hasCapitol() const
  425. {
  426. return (builtBuildings.find(13))!=builtBuildings.end();
  427. }
  428. CGTownInstance::CGTownInstance()
  429. {
  430. pos = int3(-1,-1,-1);
  431. builded=-1;
  432. destroyed=-1;
  433. garrisonHero=NULL;
  434. town=NULL;
  435. visitingHero = NULL;
  436. }
  437. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  438. {
  439. //std::cout << "Tworze obiekt "<<this<<std::endl;
  440. //state = new CLuaObjectScript();
  441. state = NULL;
  442. tempOwner = 254;
  443. blockVisit = false;
  444. }
  445. CGObjectInstance::~CGObjectInstance()
  446. {
  447. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  448. //if (state)
  449. // delete state;
  450. //state=NULL;
  451. }
  452. CGHeroInstance::CGHeroInstance()
  453. {
  454. tacticFormationEnabled = inTownGarrison = false;
  455. portrait = level = exp = -1;
  456. isStanding = true;
  457. moveDir = 4;
  458. mana = 0;
  459. visitedTown = NULL;
  460. type = NULL;
  461. }
  462. CGHeroInstance::~CGHeroInstance()
  463. {
  464. }
  465. CGTownInstance::~CGTownInstance()
  466. {}
  467. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  468. {
  469. if(checkGuild && mageGuildLevel() < level)
  470. return 0;
  471. int ret = 6 - level; //how many spells are available at this level
  472. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  473. ret++;
  474. return ret;
  475. }
  476. CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  477. {
  478. pos = right.pos;
  479. ID = right.ID;
  480. subID = right.subID;
  481. id = right.id;
  482. defInfo = right.defInfo;
  483. info = right.info;
  484. blockVisit = right.blockVisit;
  485. //state = new CLuaObjectScript(right.state->);
  486. //*state = *right.state;
  487. //state = right.state;
  488. tempOwner = right.tempOwner;
  489. }
  490. CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  491. {
  492. pos = right.pos;
  493. ID = right.ID;
  494. subID = right.subID;
  495. id = right.id;
  496. defInfo = right.defInfo;
  497. info = right.info;
  498. blockVisit = right.blockVisit;
  499. //state = new CLuaObjectScript();
  500. //*state = *right.state;
  501. tempOwner = right.tempOwner;
  502. return *this;
  503. }