CCallback.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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, int3 destPoint, int idtype, unsigned char posType)
  42. {
  43. if(ID<0 || ID>CGI->heroh->heroInstances.size())
  44. return false;
  45. if(destPoint.x<0 || destPoint.x>CGI->ac->map.width)
  46. return false;
  47. if(destPoint.y<0 || destPoint.y>CGI->ac->map.height)
  48. return false;
  49. if(destPoint.z<0 || destPoint.z>CGI->mh->ttiles[0][0].size()-1)
  50. return false;
  51. CPath * ourPath = CGI->pathf->getPath(CGI->heroh->heroInstances[ID]->pos, destPoint, CGI->heroh->heroInstances[ID], posType);
  52. if(!ourPath)
  53. return false;
  54. for(int i=ourPath->nodes.size()-1; i>0; i--)
  55. {
  56. int3 stpos, endpos;
  57. stpos = int3(ourPath->nodes[i].coord.x, ourPath->nodes[i].coord.y, CGI->heroh->heroInstances[ID]->pos.z);
  58. endpos = int3(ourPath->nodes[i-1].coord.x, ourPath->nodes[i-1].coord.y, CGI->heroh->heroInstances[ID]->pos.z);
  59. HeroMoveDetails curd;
  60. curd.src = stpos;
  61. curd.dst = endpos;
  62. curd.heroID = ID;
  63. curd.owner = CGI->heroh->heroInstances[ID]->owner;
  64. //if(CGI->heroh->heroInstances[ID]->movement>=CGI->mh->getCost(stpos, endpos, CGI->heroh->heroInstances[ID]))
  65. { //performing move
  66. int nn=0; //number of interfece of currently browsed player
  67. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  68. {
  69. if(j->second.fogOfWarMap[stpos.x][stpos.y][stpos.z] || j->second.fogOfWarMap[endpos.x][endpos.y][endpos.z])
  70. { //player should be notified
  71. CGI->playerint[nn]->heroMoved(curd);
  72. }
  73. ++nn;
  74. break; //for testing only
  75. }
  76. }
  77. //else
  78. //return true; //move ended - no more movement points
  79. }
  80. return true;
  81. }
  82. int CCallback::howManyTowns()
  83. {
  84. return gs->players[gs->currentPlayer].towns.size();
  85. }
  86. const CTownInstance * CCallback::getTownInfo(int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  87. {
  88. if (!mode)
  89. return gs->players[gs->currentPlayer].towns[val];
  90. else
  91. {
  92. //TODO: add some smart ID to the CTownInstance
  93. //for (int i=0; i<gs->players[gs->currentPlayer].towns.size();i++)
  94. //{
  95. // if (gs->players[gs->currentPlayer].towns[i]->someID==val)
  96. // return gs->players[gs->currentPlayer].towns[i];
  97. //}
  98. return NULL;
  99. }
  100. return NULL;
  101. }
  102. int CCallback::howManyHeroes(int player)
  103. {
  104. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  105. return -1;
  106. return gs->players[player].heroes.size();
  107. }
  108. const CHeroInstance * CCallback::getHeroInfo(int player, int val, bool mode) //mode = 0 -> val = serial; mode = 1 -> val = ID
  109. {
  110. if (gs->currentPlayer!=player) //TODO: checking if we are allowed to give that info
  111. return NULL;
  112. if (!mode)
  113. return gs->players[player].heroes[val];
  114. else
  115. {
  116. for (int i=0; i<gs->players[player].heroes.size();i++)
  117. {
  118. if (gs->players[player].heroes[i]->type->ID==val)
  119. return gs->players[player].heroes[i];
  120. }
  121. }
  122. return NULL;
  123. }
  124. int CCallback::getResourceAmount(int type)
  125. {
  126. return gs->players[gs->currentPlayer].resources[type];
  127. }
  128. int CCallback::getDate(int mode)
  129. {
  130. int temp;
  131. switch (mode)
  132. {
  133. case 0:
  134. return gs->day;
  135. case 1:
  136. temp = (gs->day)%7;
  137. if (temp)
  138. return temp;
  139. else return 7;
  140. case 2:
  141. temp = ((gs->day-1)/7)+1;
  142. if (temp%4)
  143. return temp;
  144. else return 4;
  145. case 3:
  146. return ((gs->day-1)/28)+1;
  147. }
  148. }