CObjectHandler.cpp 7.0 KB

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