CObjectHandler.cpp 8.7 KB

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