CObjectHandler.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  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. void CObjectHandler::loadObjects()
  11. {
  12. int ID=0;
  13. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("OBJNAMES.TXT");
  14. int it=0;
  15. while (it<buf.length()-1)
  16. {
  17. CObject nobj;
  18. CGeneralTextHandler::loadToIt(nobj.name,buf,it,3);
  19. 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))
  20. nobj.name = nobj.name.substr(0, nobj.name.size()-1);
  21. objects.push_back(nobj);
  22. }
  23. }
  24. bool CObjectInstance::operator <(const CObjectInstance &cmp) const
  25. {
  26. //if(CGI->ac->map.defy[this->defNumber].printPriority==1 && CGI->ac->map.defy[cmp.defNumber].printPriority==0)
  27. // return true;
  28. //if(CGI->ac->map.defy[cmp.defNumber].printPriority==1 && CGI->ac->map.defy[this->defNumber].printPriority==0)
  29. // return false;
  30. //if(this->pos.y<cmp.pos.y)
  31. // return true;
  32. //if(this->pos.y>cmp.pos.y)
  33. // return false;
  34. //if(CGI->ac->map.defy[this->defNumber].isOnDefList && !(CGI->ac->map.defy[cmp.defNumber].isOnDefList))
  35. // return true;
  36. //if(CGI->ac->map.defy[cmp.defNumber].isOnDefList && !(CGI->ac->map.defy[this->defNumber].isOnDefList))
  37. // return false;
  38. //if(!CGI->ac->map.defy[this->defNumber].isVisitable() && CGI->ac->map.defy[cmp.defNumber].isVisitable())
  39. // return true;
  40. //if(!CGI->ac->map.defy[cmp.defNumber].isVisitable() && CGI->ac->map.defy[this->defNumber].isVisitable())
  41. // return false;
  42. //if(this->pos.x<cmp.pos.x)
  43. // return true;
  44. return false;
  45. }
  46. int CObjectInstance::getWidth() const
  47. {
  48. return -1;//CGI->mh->reader->map.defy[defNumber].handler->ourImages[0].bitmap->w/32;
  49. }
  50. int CObjectInstance::getHeight() const
  51. {
  52. return -1;//CGI->mh->reader->map.defy[defNumber].handler->ourImages[0].bitmap->h/32;
  53. }
  54. bool CObjectInstance::visitableAt(int x, int y) const
  55. {
  56. //if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defObjInfoNumber<0)
  57. // return false;
  58. //if((CGI->dobjinfo->objs[defObjInfoNumber].visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  59. // return true;
  60. return false;
  61. }
  62. bool CGObjectInstance::isHero() const
  63. {
  64. return false;
  65. }
  66. int CGObjectInstance::getOwner() const
  67. {
  68. return state->getOwner();
  69. }
  70. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  71. {
  72. return defInfo->handler->ourImages[0].bitmap->w/32;
  73. }
  74. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  75. {
  76. return defInfo->handler->ourImages[0].bitmap->h/32;
  77. }
  78. 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)
  79. {
  80. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  81. return false;
  82. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  83. return true;
  84. return false;
  85. }
  86. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  87. {
  88. if(defInfo->printPriority==1 && defInfo->printPriority==0)
  89. return true;
  90. if(defInfo->printPriority==1 && defInfo->printPriority==0)
  91. return false;
  92. if(this->pos.y<cmp.pos.y)
  93. return true;
  94. if(this->pos.y>cmp.pos.y)
  95. return false;
  96. if(defInfo->isOnDefList && !(cmp.defInfo->isOnDefList))
  97. return true;
  98. if(cmp.defInfo->isOnDefList && !(defInfo->isOnDefList))
  99. return false;
  100. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  101. return true;
  102. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  103. return false;
  104. if(this->pos.x<cmp.pos.x)
  105. return true;
  106. return false;
  107. }
  108. bool CGDefInfo::isVisitable()
  109. {
  110. for (int i=6; i<12; i++)
  111. {
  112. if (bytes[i])
  113. return true;
  114. }
  115. return false;
  116. }
  117. bool CGHeroInstance::isHero() const
  118. {
  119. return true;
  120. }
  121. unsigned int CGHeroInstance::getTileCost(EterrainType & ttype, Eroad & rdtype, Eriver & rvtype)
  122. {
  123. unsigned int ret = type->heroClass->terrCosts[ttype];
  124. switch(rdtype)
  125. {
  126. case Eroad::dirtRoad:
  127. ret*=0.75;
  128. break;
  129. case Eroad::grazvelRoad:
  130. ret*=0.667;
  131. break;
  132. case Eroad::cobblestoneRoad:
  133. ret*=0.5;
  134. break;
  135. }
  136. return ret;
  137. }
  138. unsigned int CGHeroInstance::getLowestCreatureSpeed()
  139. {
  140. unsigned int sl = 100;
  141. for(int h=0; h<army.slots.size(); ++h)
  142. {
  143. if(army.slots[h].first->speed<sl)
  144. sl = army.slots[h].first->speed;
  145. }
  146. return sl;
  147. }
  148. int3 CGHeroInstance::convertPosition(int3 src, bool toh3m) //toh3m=true: manifest->h3m; toh3m=false: h3m->manifest
  149. {
  150. if (toh3m)
  151. {
  152. src.x+=1;
  153. return src;
  154. }
  155. else
  156. {
  157. src.x-=1;
  158. return src;
  159. }
  160. }
  161. int3 CGHeroInstance::getPosition(bool h3m) const //h3m=true - returns position of hero object; h3m=false - returns position of hero 'manifestation'
  162. {
  163. if (h3m)
  164. return pos;
  165. else return convertPosition(pos,false);
  166. }
  167. int CGHeroInstance::getSightDistance() const //returns sight distance of this hero
  168. {
  169. return 6;
  170. }
  171. void CGHeroInstance::setPosition(int3 Pos, bool h3m) //as above, but sets position
  172. {
  173. if (h3m)
  174. pos = Pos;
  175. else
  176. pos = convertPosition(Pos,true);
  177. }
  178. bool CGHeroInstance::canWalkOnSea() const
  179. {
  180. //TODO: write it - it should check if hero is flying, or something similiar
  181. return false;
  182. }
  183. int CGHeroInstance::getCurrentLuck() const
  184. {
  185. //TODO: write it
  186. return 0;
  187. }
  188. int CGHeroInstance::getCurrentMorale() const
  189. {
  190. //TODO: write it
  191. return 0;
  192. }
  193. int CGTownInstance::getSightDistance() const //returns sight distance
  194. {
  195. return 10;
  196. }
  197. CGTownInstance::CGTownInstance()
  198. {
  199. pos = int3(-1,-1,-1);
  200. builded=-1;
  201. destroyed=-1;
  202. garrisonHero=NULL;
  203. state->owner=-1;
  204. town=NULL;
  205. }
  206. CGObjectInstance::CGObjectInstance()
  207. {
  208. //std::cout << "Tworze obiekt "<<this<<std::endl;
  209. state = new CLuaObjectScript();
  210. defObjInfoNumber = -1;
  211. }
  212. CGObjectInstance::~CGObjectInstance()
  213. {
  214. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  215. if (state)
  216. delete state;
  217. state=NULL;
  218. }
  219. CGHeroInstance::~CGHeroInstance()
  220. {
  221. }
  222. CGTownInstance::~CGTownInstance()
  223. {}
  224. CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  225. {
  226. pos = right.pos;
  227. ID = right.ID;
  228. subID = right.subID;
  229. id = right.id;
  230. defInfo = right.defInfo;
  231. info = right.info;
  232. defObjInfoNumber = right.defObjInfoNumber;
  233. state = new CLuaObjectScript();
  234. *state = *right.state;
  235. }
  236. CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  237. {
  238. pos = right.pos;
  239. ID = right.ID;
  240. subID = right.subID;
  241. id = right.id;
  242. defInfo = right.defInfo;
  243. info = right.info;
  244. defObjInfoNumber = right.defObjInfoNumber;
  245. //state = new CLuaObjectScript();
  246. *state = *right.state;
  247. return *this;
  248. }