CObjectHandler.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390
  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. #include "CTownHandler.h"
  13. void CObjectHandler::loadObjects()
  14. {
  15. int ID=0;
  16. std::string buf = CGameInfo::mainObj->bitmaph->getTextFile("OBJNAMES.TXT");
  17. int it=0;
  18. while (it<buf.length()-1)
  19. {
  20. CObject nobj;
  21. CGeneralTextHandler::loadToIt(nobj.name,buf,it,3);
  22. 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))
  23. nobj.name = nobj.name.substr(0, nobj.name.size()-1);
  24. objects.push_back(nobj);
  25. }
  26. buf = CGameInfo::mainObj->bitmaph->getTextFile("ADVEVENT.TXT");
  27. it=0;
  28. std::string temp;
  29. while (it<buf.length()-1)
  30. {
  31. CGeneralTextHandler::loadToIt(temp,buf,it,3);
  32. if (temp[0]=='\"')
  33. temp = temp.substr(1,temp.length()-2);
  34. boost::algorithm::replace_all(temp,"\"\"","\"");
  35. advobtxt.push_back(temp);
  36. }
  37. buf = CGameInfo::mainObj->bitmaph->getTextFile("XTRAINFO.TXT");
  38. it=0;
  39. while (it<buf.length()-1)
  40. {
  41. CGeneralTextHandler::loadToIt(temp,buf,it,3);
  42. xtrainfo.push_back(temp);
  43. }
  44. buf = CGameInfo::mainObj->bitmaph->getTextFile("MINENAME.TXT");
  45. it=0;
  46. while (it<buf.length()-1)
  47. {
  48. CGeneralTextHandler::loadToIt(temp,buf,it,3);
  49. mines.push_back(std::pair<std::string,std::string>(temp,""));
  50. }
  51. buf = CGameInfo::mainObj->bitmaph->getTextFile("MINEEVNT.TXT");
  52. it=0;
  53. int i=0;
  54. while (it<buf.length()-1)
  55. {
  56. CGeneralTextHandler::loadToIt(temp,buf,it,3);
  57. temp = temp.substr(1,temp.length()-2);
  58. mines[i++].second = temp;
  59. }
  60. buf = CGameInfo::mainObj->bitmaph->getTextFile("RESTYPES.TXT");
  61. it=0;
  62. while (it<buf.length()-1)
  63. {
  64. CGeneralTextHandler::loadToIt(temp,buf,it,3);
  65. restypes.push_back(temp);
  66. }
  67. cregens.resize(110); //TODO: hardcoded value - change
  68. for(int i=0; i<cregens.size();i++)
  69. cregens[i]=-1;
  70. std::ifstream ifs("config/cregens.txt");
  71. while(!ifs.eof())
  72. {
  73. int dw, cr;
  74. ifs >> dw >> cr;
  75. cregens[dw]=cr;
  76. }
  77. ifs.close();
  78. ifs.clear();
  79. buf = CGameInfo::mainObj->bitmaph->getTextFile("ZCRGN1.TXT");
  80. it=0;
  81. while (it<buf.length()-1)
  82. {
  83. CGeneralTextHandler::loadToIt(temp,buf,it,3);
  84. creGens.push_back(temp);
  85. }
  86. }
  87. bool CGObjectInstance::isHero() const
  88. {
  89. return false;
  90. }
  91. int CGObjectInstance::getOwner() const
  92. {
  93. //if (state)
  94. // return state->owner;
  95. //else
  96. return tempOwner; //won't have owner
  97. }
  98. void CGObjectInstance::setOwner(int ow)
  99. {
  100. //if (state)
  101. // state->owner = ow;
  102. //else
  103. tempOwner = ow;
  104. }
  105. int CGObjectInstance::getWidth() const//returns width of object graphic in tiles
  106. {
  107. return defInfo->handler->ourImages[0].bitmap->w/32;
  108. }
  109. int CGObjectInstance::getHeight() const //returns height of object graphic in tiles
  110. {
  111. return defInfo->handler->ourImages[0].bitmap->h/32;
  112. }
  113. 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)
  114. {
  115. if(x<0 || y<0 || x>=getWidth() || y>=getHeight() || defInfo==NULL)
  116. return false;
  117. if((defInfo->visitMap[y+6-getHeight()] >> (7-(8-getWidth()+x) )) & 1)
  118. return true;
  119. return false;
  120. }
  121. bool CGObjectInstance::operator<(const CGObjectInstance & cmp) const //screen printing priority comparing
  122. {
  123. if(defInfo->printPriority==1 && cmp.defInfo->printPriority==0)
  124. return true;
  125. if(cmp.defInfo->printPriority==1 && defInfo->printPriority==0)
  126. return false;
  127. if(this->pos.y<cmp.pos.y)
  128. return true;
  129. if(this->pos.y>cmp.pos.y)
  130. return false;
  131. if(cmp.ID==34 && ID!=34)
  132. return true;
  133. if(cmp.ID!=34 && ID==34)
  134. return false;
  135. if(!defInfo->isVisitable() && cmp.defInfo->isVisitable())
  136. return true;
  137. if(!cmp.defInfo->isVisitable() && defInfo->isVisitable())
  138. return false;
  139. //if(defInfo->isOnDefList && !(cmp.defInfo->isOnDefList))
  140. // return true;
  141. //if(cmp.defInfo->isOnDefList && !(defInfo->isOnDefList))
  142. // return false;
  143. if(this->pos.x<cmp.pos.x)
  144. return true;
  145. return false;
  146. }
  147. bool CGHeroInstance::isHero() const
  148. {
  149. return true;
  150. }
  151. unsigned int CGHeroInstance::getTileCost(const EterrainType & ttype, const Eroad & rdtype, const Eriver & rvtype) const
  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 CGHeroInstance::getSecSkillLevel(const int & ID) const
  224. {
  225. for(int i=0;i<secSkills.size();i++)
  226. if(secSkills[i].first==ID)
  227. return secSkills[i].second;
  228. return -1;
  229. }
  230. int CGTownInstance::getSightDistance() const //returns sight distance
  231. {
  232. return 10;
  233. }
  234. int CGTownInstance::fortLevel() const //0 - none, 1 - fort, 2 - citadel, 3 - castle
  235. {
  236. if((builtBuildings.find(9))!=builtBuildings.end())
  237. return 3;
  238. if((builtBuildings.find(8))!=builtBuildings.end())
  239. return 2;
  240. if((builtBuildings.find(7))!=builtBuildings.end())
  241. return 1;
  242. return 0;
  243. }
  244. int CGTownInstance::hallLevel() const // -1 - none, 0 - village, 1 - town, 2 - city, 3 - capitol
  245. {
  246. if ((builtBuildings.find(13))!=builtBuildings.end())
  247. return 3;
  248. if ((builtBuildings.find(12))!=builtBuildings.end())
  249. return 2;
  250. if ((builtBuildings.find(11))!=builtBuildings.end())
  251. return 1;
  252. if ((builtBuildings.find(10))!=builtBuildings.end())
  253. return 0;
  254. return -1;
  255. }
  256. bool CGTownInstance::creatureDwelling(const int & level, bool upgraded) const
  257. {
  258. return builtBuildings.find(30+level+upgraded*7)!=builtBuildings.end();
  259. }
  260. int CGTownInstance::getHordeLevel(const int & HID) const//HID - 0 or 1; returns creature level or -1 if that horde structure is not present
  261. {
  262. return town->hordeLvl[HID];
  263. }
  264. int CGTownInstance::creatureGrowth(const int & level) const
  265. {
  266. int ret = CGI->creh->creatures[town->basicCreatures[level]].growth;
  267. switch(fortLevel())
  268. {
  269. case 3:
  270. ret*=2;break;
  271. case 2:
  272. ret*=(1.5); break;
  273. }
  274. if(builtBuildings.find(26)!=builtBuildings.end()) //grail
  275. ret+=CGI->creh->creatures[town->basicCreatures[level]].growth;
  276. if(getHordeLevel(0)==level)
  277. if((builtBuildings.find(18)!=builtBuildings.end()) || (builtBuildings.find(19)!=builtBuildings.end()))
  278. ret+=CGI->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  279. if(getHordeLevel(1)==level)
  280. if((builtBuildings.find(24)!=builtBuildings.end()) || (builtBuildings.find(25)!=builtBuildings.end()))
  281. ret+=CGI->creh->creatures[town->basicCreatures[level]].hordeGrowth;
  282. return ret;
  283. }
  284. int CGTownInstance::dailyIncome() const
  285. {
  286. int ret = 0;
  287. if ((builtBuildings.find(26))!=builtBuildings.end())
  288. ret+=5000;
  289. if ((builtBuildings.find(13))!=builtBuildings.end())
  290. ret+=4000;
  291. else if ((builtBuildings.find(12))!=builtBuildings.end())
  292. ret+=2000;
  293. else if ((builtBuildings.find(11))!=builtBuildings.end())
  294. ret+=1000;
  295. else if ((builtBuildings.find(10))!=builtBuildings.end())
  296. ret+=500;
  297. return ret;
  298. }
  299. bool CGTownInstance::hasFort() const
  300. {
  301. return (builtBuildings.find(7))!=builtBuildings.end();
  302. }
  303. bool CGTownInstance::hasCapitol() const
  304. {
  305. return (builtBuildings.find(13))!=builtBuildings.end();
  306. }
  307. CGTownInstance::CGTownInstance()
  308. {
  309. pos = int3(-1,-1,-1);
  310. builded=-1;
  311. destroyed=-1;
  312. garrisonHero=NULL;
  313. //state->owner=-1;
  314. town=NULL;
  315. income = 500;
  316. visitingHero = NULL;
  317. }
  318. CGObjectInstance::CGObjectInstance(): animPhaseShift(rand()%0xff)
  319. {
  320. //std::cout << "Tworze obiekt "<<this<<std::endl;
  321. //state = new CLuaObjectScript();
  322. //state = NULL;
  323. tempOwner = 254;
  324. blockVisit = false;
  325. }
  326. CGObjectInstance::~CGObjectInstance()
  327. {
  328. //std::cout << "Usuwam obiekt "<<this<<std::endl;
  329. //if (state)
  330. // delete state;
  331. //state=NULL;
  332. }
  333. CGHeroInstance::CGHeroInstance()
  334. {
  335. level = exp = -1;
  336. isStanding = true;
  337. moveDir = 4;
  338. mana = 0;
  339. visitedTown = NULL;
  340. }
  341. CGHeroInstance::~CGHeroInstance()
  342. {
  343. }
  344. CGTownInstance::~CGTownInstance()
  345. {}
  346. CGObjectInstance::CGObjectInstance(const CGObjectInstance & right)
  347. {
  348. pos = right.pos;
  349. ID = right.ID;
  350. subID = right.subID;
  351. id = right.id;
  352. defInfo = right.defInfo;
  353. info = right.info;
  354. blockVisit = right.blockVisit;
  355. //state = new CLuaObjectScript(right.state->);
  356. //*state = *right.state;
  357. //state = right.state;
  358. tempOwner = right.tempOwner;
  359. }
  360. CGObjectInstance& CGObjectInstance::operator=(const CGObjectInstance & right)
  361. {
  362. pos = right.pos;
  363. ID = right.ID;
  364. subID = right.subID;
  365. id = right.id;
  366. defInfo = right.defInfo;
  367. info = right.info;
  368. blockVisit = right.blockVisit;
  369. //state = new CLuaObjectScript();
  370. //*state = *right.state;
  371. tempOwner = right.tempOwner;
  372. return *this;
  373. }