CCallback.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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(CGI->heroh->heroInstances[ID]->movement>=CGI->mh->getCost(stpos, endpos, CGI->heroh->heroInstances[ID]))
  99. { //performing move
  100. int nn=0; //number of interfece of currently browsed player
  101. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  102. {
  103. if(j->second.fogOfWarMap[stpos.x-1][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x-1][endpos.y][endpos.z])
  104. { //player should be notified
  105. CGI->playerint[nn]->heroMoved(curd);
  106. }
  107. ++nn;
  108. break; //for testing only
  109. }
  110. }
  111. //else
  112. //return true; //move ended - no more movement points
  113. hero->pos = curd.dst;
  114. }
  115. return true;
  116. }
  117. int CCallback::howManyTowns()
  118. {
  119. return gs->players[gs->currentPlayer].towns.size();
  120. }
  121. const CTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  122. {
  123. if (!mode)
  124. return gs->players[gs->currentPlayer].towns[val];
  125. else
  126. {
  127. //TODO: add some smart ID to the CTownInstance
  128. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  129. //{
  130. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  131. // return gs->players[gs->currentPlayer].towns[i];
  132. //}
  133. return NULL;
  134. }
  135. return NULL;
  136. }
  137. int CCallback::howManyHeroes(int player)
  138. {
  139. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  140. return -1;
  141. return gs->players[player].heroes.size();
  142. }
  143. const CHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  144. {
  145. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  146. return NULL;
  147. if (!mode)
  148. return gs->players[player].heroes[val];
  149. else
  150. {
  151. for (int i=0; i<gs->players[player].heroes.size();i++)
  152. {
  153. if (gs->players[player].heroes[i]->type->ID==val)
  154. return gs->players[player].heroes[i];
  155. }
  156. }
  157. return NULL;
  158. }
  159. int CCallback::getResourceAmount(int type)
  160. {
  161. return gs->players[gs->currentPlayer].resources[type];
  162. }
  163. int CCallback::getDate(int mode)
  164. {
  165. int temp;
  166. switch (mode)
  167. {
  168. case 0:
  169. return gs->day;
  170. case 1:
  171. temp = (gs->day)%7;
  172. if (temp)
  173. return temp;
  174. else return 7;
  175. case 2:
  176. temp = ((gs->day-1)/7)+1;
  177. if (temp%4)
  178. return temp;
  179. else return 4;
  180. case 3:
  181. return ((gs->day-1)/28)+1;
  182. }
  183. }
  184. bool CCallback::verifyPath(CPath * path, bool blockSea)
  185. {
  186. for (int i=0;i<path->nodes.size();i++)
  187. {
  188. if ( CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].blocked
  189. && (! (CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].visitable)))
  190. return false; //path is wrong - one of the tiles is blocked
  191. if (blockSea)
  192. {
  193. if (i==0)
  194. continue;
  195. if (
  196. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType==EterrainType::water)
  197. &&
  198. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].coord.z].terType!=EterrainType::water))
  199. ||
  200. ((CGI->mh->ttiles[path->nodes[i].coord.x][path->nodes[i].coord.y][path->nodes[i].coord.z].terType!=EterrainType::water)
  201. &&
  202. (CGI->mh->ttiles[path->nodes[i-1].coord.x][path->nodes[i-1].coord.y][path->nodes[i-1].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::rock)
  205. )
  206. return false;
  207. }
  208. }
  209. return true;
  210. }
  211. std::vector < std::string > CCallback::getObjDescriptions(int3 pos)
  212. {
  213. if(gs->players[player].fogOfWarMap[pos.x][pos.y][pos.z])
  214. return CGI->mh->getObjDescriptions(pos);
  215. else return std::vector< std::string > ();
  216. }