CObjectHandler.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  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::getSecSkillLevel(const int & ID) const
  266. {
  267. for(int i=0;i<secSkills.size();i++)
  268. if(secSkills[i].first==ID)
  269. return secSkills[i].second;
  270. return 0;
  271. }
  272. ui32 CGHeroInstance::getArtAtPos(ui16 pos) const
  273. {
  274. if(pos<19)
  275. if(vstd::contains(artifWorn,pos))
  276. return artifWorn.find(pos)->second;
  277. else
  278. return -1;
  279. else
  280. if(pos-19 < artifacts.size())
  281. return artifacts[pos-19];
  282. else
  283. return -1;
  284. }
  285. void CGHeroInstance::setArtAtPos(ui16 pos, int art)
  286. {
  287. if(art<0)
  288. {
  289. if(pos<19)
  290. artifWorn.erase(pos);
  291. else
  292. artifacts -= artifacts[pos-19];
  293. }
  294. else
  295. {
  296. if(pos<19)
  297. artifWorn[pos] = art;
  298. else
  299. if(pos-19 < artifacts.size())
  300. artifacts[pos-19] = art;
  301. else
  302. artifacts.push_back(art);
  303. }
  304. }
  305. const CArtifact * CGHeroInstance::getArt(int pos) const
  306. {
  307. int id = getArtAtPos(pos);
  308. if(id>=0)
  309. return &VLC->arth->artifacts[id];
  310. else
  311. return NULL;
  312. }
  313. int CGTownInstance::getSightDistance() const //returns sight distance
  314. {
  315. return 10;
  316. }
  317. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  318. {
  319. if((builtBuildings.find(9))!=builtBuildings.end())
  320. return 3;
  321. if((builtBuildings.find(8))!=builtBuildings.end())
  322. return 2;
  323. if((builtBuildings.find(7))!=builtBuildings.end())
  324. return 1;
  325. return 0;
  326. }
  327. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  328. {
  329. if ((builtBuildings.find(13))!=builtBuildings.end())
  330. return 3;
  331. if ((builtBuildings.find(12))!=builtBuildings.end())
  332. return 2;
  333. if ((builtBuildings.find(11))!=builtBuildings.end())
  334. return 1;
  335. if ((builtBuildings.find(10))!=builtBuildings.end())
  336. return 0;
  337. return -1;
  338. }
  339. int CGTownInstance::mageGuildLevel() const
  340. {
  341. if ((builtBuildings.find(4))!=builtBuildings.end())
  342. return 5;
  343. if ((builtBuildings.find(3))!=builtBuildings.end())
  344. return 4;
  345. if ((builtBuildings.find(2))!=builtBuildings.end())
  346. return 3;
  347. if ((builtBuildings.find(1))!=builtBuildings.end())
  348. return 2;
  349. if ((builtBuildings.find(0))!=builtBuildings.end())
  350. return 1;
  351. return 0;
  352. }
  353. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  354. {
  355. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  356. }
  357. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  358. {
  359. return town->hordeLvl[HID];
  360. }
  361. int CGTownInstance::creatureGrowth(const int & level) const
  362. {
  363. int ret = VLC->creh->creatures[town->basicCreatures[level]].growth;
  364. switch(fortLevel())
  365. {
  366. case 3:
  367. ret*=2;break;
  368. case 2:
  369. ret*=(1.5); break;
  370. }
  371. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  372. ret+=VLC->creh->creatures[town->basicCreatures[level]].growth;
  373. if(getHordeLevel(0)==level)
  374. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  375. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  376. if(getHordeLevel(1)==level)
  377. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  378. ret+=VLC->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  379. return ret;
  380. }
  381. int CGTownInstance::dailyIncome() const
  382. {
  383. int ret = 0;
  384. if ((builtBuildings.find(26))!=builtBuildings.end())
  385. ret+=5000;
  386. if ((builtBuildings.find(13))!=builtBuildings.end())
  387. ret+=4000;
  388. else if ((builtBuildings.find(12))!=builtBuildings.end())
  389. ret+=2000;
  390. else if ((builtBuildings.find(11))!=builtBuildings.end())
  391. ret+=1000;
  392. else if ((builtBuildings.find(10))!=builtBuildings.end())
  393. ret+=500;
  394. return ret;
  395. }
  396. bool CGTownInstance::hasFort() const
  397. {
  398. return (builtBuildings.find(7))!=builtBuildings.end();
  399. }
  400. bool CGTownInstance::hasCapitol() const
  401. {
  402. return (builtBuildings.find(13))!=builtBuildings.end();
  403. }
  404. CGTownInstance::CGTownInstance()
  405. {
  406. pos = int3(-1,-1,-1);
  407. builded=-1;
  408. destroyed=-1;
  409. garrisonHero=NULL;
  410. town=NULL;
  411. visitingHero = NULL;
  412. }
  413. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  414. {
  415. //std::cout << "Tworze obiekt "<<this<<std::endl;
  416. //state = new CLuaObjectScript();
  417. state = NULL;
  418. tempOwner = 254;
  419. blockVisit = false;
  420. }
  421. CGObjectInstance::~CGObjectInstance()
  422. {
  423. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  424. //if (state)
  425. // delete state;
  426. //state=NULL;
  427. }
  428. CGHeroInstance::CGHeroInstance()
  429. {
  430. tacticFormationEnabled = inTownGarrison = false;
  431. portrait = level = exp = -1;
  432. isStanding = true;
  433. moveDir = 4;
  434. mana = 0;
  435. visitedTown = NULL;
  436. type = NULL;
  437. }
  438. CGHeroInstance::~CGHeroInstance()
  439. {
  440. }
  441. CGTownInstance::~CGTownInstance()
  442. {}
  443. int CGTownInstance::spellsAtLevel(int level, bool checkGuild) const
  444. {
  445. if(checkGuild && mageGuildLevel() < level)
  446. return 0;
  447. int ret = 6 - level; //how many spells are available at this level
  448. if(subID == 2 && vstd::contains(builtBuildings,22)) //magic library in Tower
  449. ret++;
  450. return ret;
  451. }
  452. CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  453. {
  454. pos = right.pos;
  455. ID = right.ID;
  456. subID = right.subID;
  457. id = right.id;
  458. defInfo = right.defInfo;
  459. info = right.info;
  460. blockVisit = right.blockVisit;
  461. //state = new CLuaObjectScript(right.state->);
  462. //*state = *right.state;
  463. //state = right.state;
  464. tempOwner = right.tempOwner;
  465. }
  466. CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  467. {
  468. pos = right.pos;
  469. ID = right.ID;
  470. subID = right.subID;
  471. id = right.id;
  472. defInfo = right.defInfo;
  473. info = right.info;
  474. blockVisit = right.blockVisit;
  475. //state = new CLuaObjectScript();
  476. //*state = *right.state;
  477. tempOwner = right.tempOwner;
  478. return *this;
  479. }