CObjectHandler.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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 ibject 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::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  124. {
  125. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  126. return true;
  127. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  128. return false;
  129. if(this->pos.y<cmp.pos.y)
  130. return true;
  131. if(this->pos.y>cmp.pos.y)
  132. return false;
  133. if(cmp.ID==34 && ID!=34)
  134. return true;
  135. if(cmp.ID!=34 && ID==34)
  136. return false;
  137. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  138. return true;
  139. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  140. return false;
  141. if(this->pos.x<cmp.pos.x)
  142. return true;
  143. return false;
  144. }
  145. bool CGHeroInstance::isHero() const
  146. {
  147. return true;
  148. }
  149. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype) const
  150. {
  151. unsigned int ret = type->heroClass->terrCosts[ttype];
  152. switch(rdtype)
  153. {
  154. case dirtRoad:
  155. ret*=0.75;
  156. break;
  157. case grazvelRoad:
  158. ret*=0.667;
  159. break;
  160. case cobblestoneRoad:
  161. ret*=0.5;
  162. break;
  163. }
  164. return ret;
  165. }
  166. unsigned int CGHeroInstance::getLowestCreatureSpeed()
  167. {
  168. unsigned int sl = 100;
  169. for(int h=0; h<army.slots.size(); ++h)
  170. {
  171. if(VLC->creh->creatures[army.slots[h].first].speed<sl)
  172. sl = VLC->creh->creatures[army.slots[h].first].speed;
  173. }
  174. return sl;
  175. }
  176. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  177. {
  178. if (toh3m)
  179. {
  180. src.x+=1;
  181. return src;
  182. }
  183. else
  184. {
  185. src.x-=1;
  186. return src;
  187. }
  188. }
  189. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  190. {
  191. if (h3m)
  192. return pos;
  193. else return convertPosition(pos,false);
  194. }
  195. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  196. {
  197. return 6;
  198. }
  199. void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  200. {
  201. if (h3m)
  202. pos = Pos;
  203. else
  204. pos = convertPosition(Pos,true);
  205. }
  206. bool CGHeroInstance::canWalkOnSea() const
  207. {
  208. //TODO: write it - it should check if hero is flying, or something similiar
  209. return false;
  210. }
  211. int CGHeroInstance::getCurrentLuck() const
  212. {
  213. //TODO: write it
  214. return 0;
  215. }
  216. int CGHeroInstance::getCurrentMorale() const
  217. {
  218. //TODO: write it
  219. return 0;
  220. }
  221. int CGHeroInstance::getSecSkillLevel(const int & ID) const
  222. {
  223. for(int i=0;i<secSkills.size();i++)
  224. if(secSkills[i].first==ID)
  225. return secSkills[i].second;
  226. return -1;
  227. }
  228. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  229. {
  230. if(pos<19)
  231. if(vstd::contains(artifWorn,pos))
  232. return artifWorn.find(pos)->second;
  233. else
  234. return -1;
  235. else
  236. if(pos-19 < artifacts.size())
  237. return artifacts[pos-19];
  238. else
  239. return -1;
  240. }
  241. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  242. {
  243. if(art<0)
  244. {
  245. if(pos<19)
  246. artifWorn.erase(pos);
  247. else
  248. artifacts -= artifacts[pos-19];
  249. }
  250. else
  251. {
  252. if(pos<19)
  253. artifWorn[pos] = art;
  254. else
  255. if(pos-19 < artifacts.size())
  256. artifacts[pos-19] = art;
  257. else
  258. artifacts.push_back(art);
  259. }
  260. }
  261. const CArtifact * CGHeroInstance::getArt(int pos)
  262. {
  263. int id = getArtAtPos(pos);
  264. if(id>=0)
  265. return &VLC->arth->artifacts[id];
  266. else
  267. return NULL;
  268. }
  269. int CGTownInstance::getSightDistance() const //returns sight distance
  270. {
  271. return 10;
  272. }
  273. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  274. {
  275. if((builtBuildings.find(9))!=builtBuildings.end())
  276. return 3;
  277. if((builtBuildings.find(8))!=builtBuildings.end())
  278. return 2;
  279. if((builtBuildings.find(7))!=builtBuildings.end())
  280. return 1;
  281. return 0;
  282. }
  283. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  284. {
  285. if ((builtBuildings.find(13))!=builtBuildings.end())
  286. return 3;
  287. if ((builtBuildings.find(12))!=builtBuildings.end())
  288. return 2;
  289. if ((builtBuildings.find(11))!=builtBuildings.end())
  290. return 1;
  291. if ((builtBuildings.find(10))!=builtBuildings.end())
  292. return 0;
  293. return -1;
  294. }
  295. int CGTownInstance::mageGuildLevel() const
  296. {
  297. if ((builtBuildings.find(4))!=builtBuildings.end())
  298. return 5;
  299. if ((builtBuildings.find(3))!=builtBuildings.end())
  300. return 4;
  301. if ((builtBuildings.find(2))!=builtBuildings.end())
  302. return 3;
  303. if ((builtBuildings.find(1))!=builtBuildings.end())
  304. return 2;
  305. if ((builtBuildings.find(0))!=builtBuildings.end())
  306. return 1;
  307. return 0;
  308. }
  309. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  310. {
  311. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  312. }
  313. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  314. {
  315. return town->hordeLvl[HID];
  316. }
  317. int CGTownInstance::creatureGrowth(const int & level) const
  318. {
  319. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  320. switch(fortLevel())
  321. {
  322. case 3:
  323. ret*=2;break;
  324. case 2:
  325. ret*=(1.5); break;
  326. }
  327. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  328. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  329. if(getHordeLevel(0)==level)
  330. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  331. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  332. if(getHordeLevel(1)==level)
  333. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  334. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  335. return ret;
  336. }
  337. int CGTownInstance::dailyIncome() const
  338. {
  339. int ret = 0;
  340. if ((builtBuildings.find(26))!=builtBuildings.end())
  341. ret+=5000;
  342. if ((builtBuildings.find(13))!=builtBuildings.end())
  343. ret+=4000;
  344. else if ((builtBuildings.find(12))!=builtBuildings.end())
  345. ret+=2000;
  346. else if ((builtBuildings.find(11))!=builtBuildings.end())
  347. ret+=1000;
  348. else if ((builtBuildings.find(10))!=builtBuildings.end())
  349. ret+=500;
  350. return ret;
  351. }
  352. bool CGTownInstance::hasFort() const
  353. {
  354. return (builtBuildings.find(7))!=builtBuildings.end();
  355. }
  356. bool CGTownInstance::hasCapitol() const
  357. {
  358. return (builtBuildings.find(13))!=builtBuildings.end();
  359. }
  360. CGTownInstance::CGTownInstance()
  361. {
  362. pos = int3(-1,-1,-1);
  363. builded=-1;
  364. destroyed=-1;
  365. garrisonHero=NULL;
  366. town=NULL;
  367. visitingHero = NULL;
  368. }
  369. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  370. {
  371. //std::cout << "Tworze obiekt "<<this<<std::endl;
  372. //state = new CLuaObjectScript();
  373. state = NULL;
  374. tempOwner = 254;
  375. blockVisit = false;
  376. }
  377. CGObjectInstance::~CGObjectInstance()
  378. {
  379. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  380. //if (state)
  381. // delete state;
  382. //state=NULL;
  383. }
  384. CGHeroInstance::CGHeroInstance()
  385. {
  386. inTownGarrison = false;
  387. portrait = level = exp = -1;
  388. isStanding = true;
  389. moveDir = 4;
  390. mana = 0;
  391. visitedTown = NULL;
  392. type = NULL;
  393. }
  394. CGHeroInstance::~CGHeroInstance()
  395. {
  396. }
  397. CGTownInstance::~CGTownInstance()
  398. {}
  399. CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  400. {
  401. pos = right.pos;
  402. ID = right.ID;
  403. subID = right.subID;
  404. id = right.id;
  405. defInfo = right.defInfo;
  406. info = right.info;
  407. blockVisit = right.blockVisit;
  408. //state = new CLuaObjectScript(right.state->);
  409. //*state = *right.state;
  410. //state = right.state;
  411. tempOwner = right.tempOwner;
  412. }
  413. CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  414. {
  415. pos = right.pos;
  416. ID = right.ID;
  417. subID = right.subID;
  418. id = right.id;
  419. defInfo = right.defInfo;
  420. info = right.info;
  421. blockVisit = right.blockVisit;
  422. //state = new CLuaObjectScript();
  423. //*state = *right.state;
  424. tempOwner = right.tempOwner;
  425. return *this;
  426. }