CObjectHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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. //applying pathfinding skill
  161. switch(getSecSkillLevel(0))
  162. {
  163. case 1: //basic
  164. switch(ttype)
  165. {
  166. case rough:
  167. ret = 100;
  168. break;
  169. case sand: case snow:
  170. if(ret>125)
  171. ret = 125;
  172. break;
  173. case swamp:
  174. if(ret>150)
  175. ret = 150;
  176. break;
  177. }
  178. break;
  179. case 2: //advanced
  180. switch(ttype)
  181. {
  182. case rough: case sand: case snow:
  183. ret = 100;
  184. break;
  185. case swamp:
  186. if(ret>125)
  187. ret = 125;
  188. break;
  189. }
  190. break;
  191. case 3: //expert
  192. ret = 100;
  193. break;
  194. }
  195. //calculating road influence
  196. switch(rdtype)
  197. {
  198. case dirtRoad:
  199. ret*=0.75;
  200. break;
  201. case grazvelRoad:
  202. ret*=0.667;
  203. break;
  204. case cobblestoneRoad:
  205. ret*=0.5;
  206. break;
  207. }
  208. return ret;
  209. }
  210. unsigned int CGHeroInstance::getLowestCreatureSpeed()
  211. {
  212. unsigned int sl = 100;
  213. for(int h=0; h<army.slots.size(); ++h)
  214. {
  215. if(VLC->creh->creatures[army.slots[h].first].speed<sl)
  216. sl = VLC->creh->creatures[army.slots[h].first].speed;
  217. }
  218. return sl;
  219. }
  220. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  221. {
  222. if (toh3m)
  223. {
  224. src.x+=1;
  225. return src;
  226. }
  227. else
  228. {
  229. src.x-=1;
  230. return src;
  231. }
  232. }
  233. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  234. {
  235. if (h3m)
  236. return pos;
  237. else return convertPosition(pos,false);
  238. }
  239. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  240. {
  241. return 6 + getSecSkillLevel(3); //default + scouting
  242. }
  243. void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  244. {
  245. if (h3m)
  246. pos = Pos;
  247. else
  248. pos = convertPosition(Pos,true);
  249. }
  250. bool CGHeroInstance::canWalkOnSea() const
  251. {
  252. //TODO: write it - it should check if hero is flying, or something similiar
  253. return false;
  254. }
  255. int CGHeroInstance::getCurrentLuck() const
  256. {
  257. //TODO: write it
  258. return 0;
  259. }
  260. int CGHeroInstance::getCurrentMorale() const
  261. {
  262. //TODO: write it
  263. return 0;
  264. }
  265. int CGHeroInstance::getPrimSkillLevel(int id) const
  266. {
  267. return primSkills[id];
  268. }
  269. int CGHeroInstance::getSecSkillLevel(const int & ID) const
  270. {
  271. for(int i=0;i<secSkills.size();i++)
  272. if(secSkills[i].first==ID)
  273. return secSkills[i].second;
  274. return 0;
  275. }
  276. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  277. {
  278. if(pos<19)
  279. if(vstd::contains(artifWorn,pos))
  280. return artifWorn.find(pos)->second;
  281. else
  282. return -1;
  283. else
  284. if(pos-19 < artifacts.size())
  285. return artifacts[pos-19];
  286. else
  287. return -1;
  288. }
  289. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  290. {
  291. if(art<0)
  292. {
  293. if(pos<19)
  294. artifWorn.erase(pos);
  295. else
  296. artifacts -= artifacts[pos-19];
  297. }
  298. else
  299. {
  300. if(pos<19)
  301. artifWorn[pos] = art;
  302. else
  303. if(pos-19 < artifacts.size())
  304. artifacts[pos-19] = art;
  305. else
  306. artifacts.push_back(art);
  307. }
  308. }
  309. const CArtifact * CGHeroInstance::getArt(int pos) const
  310. {
  311. int id = getArtAtPos(pos);
  312. if(id>=0)
  313. return &VLC->arth->artifacts[id];
  314. else
  315. return NULL;
  316. }
  317. int CGTownInstance::getSightDistance() const //returns sight distance
  318. {
  319. return 10;
  320. }
  321. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  322. {
  323. if((builtBuildings.find(9))!=builtBuildings.end())
  324. return 3;
  325. if((builtBuildings.find(8))!=builtBuildings.end())
  326. return 2;
  327. if((builtBuildings.find(7))!=builtBuildings.end())
  328. return 1;
  329. return 0;
  330. }
  331. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  332. {
  333. if ((builtBuildings.find(13))!=builtBuildings.end())
  334. return 3;
  335. if ((builtBuildings.find(12))!=builtBuildings.end())
  336. return 2;
  337. if ((builtBuildings.find(11))!=builtBuildings.end())
  338. return 1;
  339. if ((builtBuildings.find(10))!=builtBuildings.end())
  340. return 0;
  341. return -1;
  342. }
  343. int CGTownInstance::mageGuildLevel() const
  344. {
  345. if ((builtBuildings.find(4))!=builtBuildings.end())
  346. return 5;
  347. if ((builtBuildings.find(3))!=builtBuildings.end())
  348. return 4;
  349. if ((builtBuildings.find(2))!=builtBuildings.end())
  350. return 3;
  351. if ((builtBuildings.find(1))!=builtBuildings.end())
  352. return 2;
  353. if ((builtBuildings.find(0))!=builtBuildings.end())
  354. return 1;
  355. return 0;
  356. }
  357. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  358. {
  359. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  360. }
  361. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  362. {
  363. return town->hordeLvl[HID];
  364. }
  365. int CGTownInstance::creatureGrowth(const int & level) const
  366. {
  367. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  368. switch(fortLevel())
  369. {
  370. case 3:
  371. ret*=2;break;
  372. case 2:
  373. ret*=(1.5); break;
  374. }
  375. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  376. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  377. if(getHordeLevel(0)==level)
  378. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  379. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  380. if(getHordeLevel(1)==level)
  381. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  382. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  383. return ret;
  384. }
  385. int CGTownInstance::dailyIncome() const
  386. {
  387. int ret = 0;
  388. if ((builtBuildings.find(26))!=builtBuildings.end())
  389. ret+=5000;
  390. if ((builtBuildings.find(13))!=builtBuildings.end())
  391. ret+=4000;
  392. else if ((builtBuildings.find(12))!=builtBuildings.end())
  393. ret+=2000;
  394. else if ((builtBuildings.find(11))!=builtBuildings.end())
  395. ret+=1000;
  396. else if ((builtBuildings.find(10))!=builtBuildings.end())
  397. ret+=500;
  398. return ret;
  399. }
  400. bool CGTownInstance::hasFort() const
  401. {
  402. return (builtBuildings.find(7))!=builtBuildings.end();
  403. }
  404. bool CGTownInstance::hasCapitol() const
  405. {
  406. return (builtBuildings.find(13))!=builtBuildings.end();
  407. }
  408. CGTownInstance::CGTownInstance()
  409. {
  410. pos = int3(-1,-1,-1);
  411. builded=-1;
  412. destroyed=-1;
  413. garrisonHero=NULL;
  414. town=NULL;
  415. visitingHero = NULL;
  416. }
  417. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  418. {
  419. //std::cout << "Tworze obiekt "<<this<<std::endl;
  420. //state = new CLuaObjectScript();
  421. state = NULL;
  422. tempOwner = 254;
  423. blockVisit = false;
  424. }
  425. CGObjectInstance::~CGObjectInstance()
  426. {
  427. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  428. //if (state)
  429. // delete state;
  430. //state=NULL;
  431. }
  432. CGHeroInstance::CGHeroInstance()
  433. {
  434. tacticFormationEnabled = inTownGarrison = false;
  435. portrait = level = exp = -1;
  436. isStanding = true;
  437. moveDir = 4;
  438. mana = 0;
  439. visitedTown = NULL;
  440. type = NULL;
  441. }
  442. CGHeroInstance::~CGHeroInstance()
  443. {
  444. }
  445. CGTownInstance::~CGTownInstance()
  446. {}
  447. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  448. {
  449. if(checkGuild && mageGuildLevel() < level)
  450. return 0;
  451. int ret = 6 - level; //how many spells are available at this level
  452. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  453. ret++;
  454. return ret;
  455. }
  456. CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  457. {
  458. pos = right.pos;
  459. ID = right.ID;
  460. subID = right.subID;
  461. id = right.id;
  462. defInfo = right.defInfo;
  463. info = right.info;
  464. blockVisit = right.blockVisit;
  465. //state = new CLuaObjectScript(right.state->);
  466. //*state = *right.state;
  467. //state = right.state;
  468. tempOwner = right.tempOwner;
  469. }
  470. CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  471. {
  472. pos = right.pos;
  473. ID = right.ID;
  474. subID = right.subID;
  475. id = right.id;
  476. defInfo = right.defInfo;
  477. info = right.info;
  478. blockVisit = right.blockVisit;
  479. //state = new CLuaObjectScript();
  480. //*state = *right.state;
  481. tempOwner = right.tempOwner;
  482. return *this;
  483. }