CGameState.cpp 3.3 KB

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