CObjectHandler.cpp 9.7 KB

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