CObjectHandler.cpp 10.0 KB

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