CObjectHandler.cpp 7.6 KB

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