CObjectHandler.cpp 11 KB

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