CCallback.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. #include "stdafx.h"
  2. #include "CCallback.h"
  3. #include "CPathfinder.h"
  4. #include "hch\CHeroHandler.h"
  5. #include "hch\CTownHandler.h"
  6. #include "CGameInfo.h"
  7. #include "hch\CAmbarCendamo.h"
  8. #include "mapHandler.h"
  9. #include "CGameState.h"
  10. #include "CGameInterface.h"
  11. int CCallback::lowestSpeed(CHeroInstance * chi)
  12. {
  13. int min = 150;
  14. for ( std::map<int,std::pair<CCreature*,int> >::iterator i = chi->army.slots.begin();
  15. i!=chi->army.slots.end(); i++ )
  16. {
  17. if (min>(*i).second.first->speed)
  18. min = (*i).second.first->speed;
  19. }
  20. return min;
  21. }
  22. int CCallback::valMovePoints(CHeroInstance * chi)
  23. {
  24. int ret = 1270+70*lowestSpeed(chi);
  25. if (ret>2000)
  26. ret=2000;
  27. //TODO: additional bonuses (but they aren't currently stored in chi)
  28. return ret;
  29. }
  30. void CCallback::newTurn()
  31. {
  32. //std::map<int, PlayerState>::iterator i = gs->players.begin() ;
  33. for ( std::map<int, PlayerState>::iterator i=gs->players.begin() ; i!=gs->players.end();i++)
  34. {
  35. for (int j=0;j<(*i).second.heroes.size();j++)
  36. {
  37. (*i).second.heroes[j]->movement = valMovePoints((*i).second.heroes[j]);
  38. }
  39. }
  40. }
  41. bool CCallback::moveHero(int ID, CPath * path, int idtype, int pathType)
  42. {
  43. CHeroInstance * hero = NULL;
  44. if (idtype==0)
  45. {
  46. if (player==-1)
  47. hero=gs->players[player+1].heroes[ID];
  48. else
  49. hero=gs->players[player].heroes[ID];
  50. }
  51. else if (idtype==1 && player>=0) //looking for it in local area
  52. {
  53. for (int i=0; i<gs->players[player].heroes.size();i++)
  54. {
  55. if (gs->players[player].heroes[i]->type->ID == ID)
  56. hero = gs->players[player].heroes[i];
  57. }
  58. }
  59. else //idtype==1; player<0
  60. {
  61. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)
  62. {
  63. for (int i=0; i<(*j).second.heroes.size();i++)
  64. {
  65. if ((*j).second.heroes[i]->type->ID == ID)
  66. {
  67. hero = (*j).second.heroes[i];
  68. }
  69. }
  70. }
  71. }
  72. if (!hero)
  73. return false; //can't find hero
  74. if(!verifyPath(path,!hero->canWalkOnSea()))//TODO: not check sea, if hero has flying or walking on water
  75. return false; //invalid path
  76. //check path format
  77. if (pathType==0)
  78. CPathfinder::convertPath(path,pathType);
  79. if (pathType>1)
  80. throw std::exception("Unknown path format");
  81. CPath * ourPath = path;
  82. if(!ourPath)
  83. return false;
  84. for(int i=ourPath->nodes.size()-1; i>0; i--)
  85. {
  86. int3 stpos, endpos;
  87. stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, hero->pos.z);
  88. endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, hero->pos.z);
  89. HeroMoveDetails curd;
  90. curd.src = stpos;
  91. curd.dst = endpos;
  92. curd.ho = hero->ourObject;
  93. curd.owner = hero->owner;
  94. /*if(player!=-1)
  95. {
  96. hero->pos = endpos;
  97. }*/
  98. if((hero->movement>=CGI->mh->getCost(stpos, endpos, hero)) || player==-1)
  99. { //performing move
  100. hero->movement-=CGI->mh->getCost(stpos, endpos, hero);
  101. int nn=0; //number of interfece of currently browsed player
  102. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  103. {
  104. if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
  105. { //player should be notified
  106. CGI->playerint[nn]->heroMoved(curd);
  107. }
  108. ++nn;
  109. break; //for testing only
  110. }
  111. }
  112. else
  113. return true; //move ended - no more movement points
  114. hero->pos = curd.dst;
  115. hero->ourObject->pos = curd.dst;
  116. }
  117. return true;
  118. }
  119. int CCallback::howManyTowns()
  120. {
  121. return gs->players[gs->currentPlayer].towns.size();
  122. }
  123. const CTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  124. {
  125. if (!mode)
  126. return gs->players[gs->currentPlayer].towns[val];
  127. else
  128. {
  129. //TODO: add some smart ID to the CTownInstance
  130. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  131. //{
  132. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  133. // return gs->players[gs->currentPlayer].towns[i];
  134. //}
  135. return NULL;
  136. }
  137. return NULL;
  138. }
  139. int CCallback::howManyHeroes(int player)
  140. {
  141. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  142. return -1;
  143. return gs->players[player].heroes.size();
  144. }
  145. const CHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  146. {
  147. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  148. return NULL;
  149. if (!mode)
  150. return gs->players[player].heroes[val];
  151. else
  152. {
  153. for (int i=0; i<gs->players[player].heroes.size();i++)
  154. {
  155. if (gs->players[player].heroes[i]->type->ID==val)
  156. return gs->players[player].heroes[i];
  157. }
  158. }
  159. return NULL;
  160. }
  161. int CCallback::getResourceAmount(int type)
  162. {
  163. return gs->players[gs->currentPlayer].resources[type];
  164. }
  165. int CCallback::getDate(int mode)
  166. {
  167. int temp;
  168. switch (mode)
  169. {
  170. case 0:
  171. return gs->day;
  172. case 1:
  173. temp = (gs->day)%7;
  174. if (temp)
  175. return temp;
  176. else return 7;
  177. case 2:
  178. temp = ((gs->day-1)/7)+1;
  179. if (temp%4)
  180. return temp;
  181. else return 4;
  182. case 3:
  183. return ((gs->day-1)/28)+1;
  184. }
  185. }
  186. bool CCallback::verifyPath(CPath * path, bool blockSea)
  187. {
  188. for (int i=0;i<path->nodes.size();i++)
  189. {
  190. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  191. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  192. return false; //path is wrong - one of the tiles is blocked
  193. if (blockSea)
  194. {
  195. if (i==0)
  196. continue;
  197. if (
  198. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
  199. &&
  200. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
  201. ||
  202. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
  203. &&
  204. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::water))
  205. ||
  206. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType==EterrainType::rock)
  207. )
  208. return false;
  209. }
  210. }
  211. return true;
  212. }
  213. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  214. {
  215. if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
  216. return CGI->mh->getObjDescriptions(pos);
  217. else return std::vector< std::string > ();
  218. }
  219. PseudoV< PseudoV< PseudoV<unsigned char> > > & CCallback::getVisibilityMap()
  220. {
  221. return gs->players[player].fogOfWarMap;
  222. }