CGameState.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. #include "CGameState.h"
  2. #include "CGameInterface.h"
  3. #include "CPlayerInterface.h"
  4. #include <algorithm>
  5. class CStack
  6. {
  7. public:
  8. int ID;
  9. CCreature * creature;
  10. int amount;
  11. int owner;
  12. int position;
  13. bool alive;
  14. CStack(CCreature * C, int A, int O, int I):creature(C),amount(A),owner(O), alive(true), position(-1), ID(I){};
  15. };
  16. class CMP_stack
  17. {
  18. public:
  19. bool operator ()(const CStack* a, const CStack* b)
  20. {
  21. return (a->creature->speed)<(b->creature->speed);
  22. }
  23. } cmpst ;
  24. void CGameState::battle(CCreatureSet * army1, CCreatureSet * army2, int3 tile, CArmedInstance *hero1, CArmedInstance *hero2)
  25. {
  26. curB = new BattleInfo();
  27. std::vector<CStack*> & stacks = (curB->stacks);
  28. curB->army1=army1;
  29. curB->army2=army2;
  30. curB->hero1=dynamic_cast<CGHeroInstance*>(hero1);
  31. curB->hero2=dynamic_cast<CGHeroInstance*>(hero2);
  32. curB->side1=(hero1)?(hero1->tempOwner):(-1);
  33. curB->side2=(hero2)?(hero2->tempOwner):(-1);
  34. curB->round = -2;
  35. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army1->slots.begin(); i!=army1->slots.end(); i++)
  36. stacks.push_back(new CStack(i->second.first,i->second.second,0, stacks.size()));
  37. for(std::map<int,std::pair<CCreature*,int> >::iterator i = army2->slots.begin(); i!=army2->slots.end(); i++)
  38. stacks.push_back(new CStack(i->second.first,i->second.second,1, stacks.size()));
  39. std::stable_sort(stacks.begin(),stacks.end(),cmpst);
  40. //for start inform players about battle
  41. for(std::map<int, PlayerState>::iterator j=CGI->state->players.begin(); j!=CGI->state->players.end(); ++j)//CGI->state->players.size(); ++j) //for testing
  42. {
  43. if (j->first > PLAYER_LIMIT)
  44. break;
  45. if(j->second.fogOfWarMap[tile.x][tile.y][tile.z])
  46. { //player should be notified
  47. tribool side = tribool::indeterminate_value;
  48. if(j->first == curB->side1) //player is attacker
  49. side = false;
  50. else if(j->first == curB->side2) //player is defender
  51. side = true;
  52. else
  53. return; //no witnesses
  54. if(CGI->playerint[j->second.serial]->human)
  55. {
  56. ((CPlayerInterface*)( CGI->playerint[j->second.serial] ))->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  57. }
  58. else
  59. {
  60. //CGI->playerint[j->second.serial]->battleStart(army1, army2, tile, curB->hero1, curB->hero2, side);
  61. }
  62. }
  63. }
  64. curB->round++;
  65. if( (curB->hero1 && curB->hero1->getSecSkillLevel(19)>=0) || ( curB->hero2 && curB->hero2->getSecSkillLevel(19)>=0) )//someone has tactics
  66. {
  67. //TODO: wywolania dla rundy -1, ograniczenie pola ruchu, etc
  68. }
  69. curB->round++;
  70. //while(true) //do zwyciestwa jednej ze stron
  71. //{
  72. // for(int i=0;i<stacks.size();i++)
  73. // {
  74. // curB->activeStack = i;
  75. // if(stacks[i]->alive) //niech interfejs ruszy oddzialem
  76. // {
  77. // if(CGI->playerint[(stacks[i]->owner)?(hero2->tempOwner):(hero1->tempOwner)]->human)
  78. // {
  79. // ((CPlayerInterface*)CGI->playerint[(stacks[i]->owner)?(hero2->tempOwner):(hero1->tempOwner)])->activeStack(stacks[i]->ID);
  80. // }
  81. // else
  82. // {
  83. // //CGI->playerint[(stacks[i]->owner)?(hero2->tempOwner):(hero1->tempOwner)]->activeStack(stacks[i]->ID);
  84. // }
  85. // }
  86. // //sprawdzic czy po tej akcji ktoras strona nie wygrala bitwy
  87. // }
  88. //}
  89. for(int i=0;i<stacks.size();i++)
  90. delete stacks[i];
  91. delete curB;
  92. curB = NULL;
  93. }