2
0

CObjectHandler.cpp 8.5 KB

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