CObjectHandler.cpp 8.1 KB

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