Cell.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. #include "Cell.h"
  2. #include "unit.h"
  3. #include <queue>
  4. #include <vector>
  5. Cell::Cell(Unit * character) {
  6. leftUp_ = left_ = leftDown_ = nullptr;
  7. rightUp_ = right_ = rightDown_ = nullptr;
  8. character_ = &character;
  9. clearCell_();
  10. AddedToQuery_ = true;
  11. }
  12. Cell * Cell::getleftUp() {
  13. return leftUp_;
  14. }
  15. void Cell::setleftUp(Cell * t) {
  16. leftUp_ = t;
  17. }
  18. Cell * Cell::getleft() {
  19. return left_;
  20. }
  21. void Cell::setleft(Cell * t) {
  22. left_ = t;
  23. }
  24. Cell * Cell::getleftDown() {
  25. return leftDown_;
  26. }
  27. void Cell::setleftDown(Cell * t) {
  28. leftDown_ = t;
  29. }
  30. Cell * Cell::getrightUp() {
  31. return rightUp_;
  32. }
  33. void Cell::setrightUp(Cell * t) {
  34. rightUp_ = t;
  35. }
  36. Cell * Cell::getright() {
  37. return right_;
  38. }
  39. void Cell::setright(Cell * t) {
  40. right_ = t;
  41. }
  42. Cell * Cell::getrightDown() {
  43. return rightDown_;
  44. }
  45. void Cell::setrightDown(Cell * t) {
  46. rightDown_ = t;
  47. }
  48. Unit * Cell::getCharacter() {
  49. return character_;
  50. }
  51. void Cell::setCharacter(Unit * t) {
  52. character_ = t;
  53. }
  54. bool Cell::getisMoveable() {
  55. return isMoveable_;
  56. }
  57. void Cell::setisMoveable(bool t) {
  58. isMoveable_ = t;
  59. }
  60. bool Cell::getisAttackable() {
  61. return isAttackable_;
  62. }
  63. void Cell::setisAttackable(bool t) {
  64. isAttackable_ = t;
  65. }
  66. int Cell::getDistance() {
  67. return distance_;
  68. }
  69. void Cell::setDistance(int t) {
  70. distance_ = t;
  71. }
  72. bool Cell::isEmpty() {
  73. return character_ == NULL;
  74. }
  75. void Cell::clearCell_() {
  76. setisMoveable(false);
  77. setisAttackable(false);
  78. setDistance(-1);
  79. }
  80. void Cell::clearTable_() {
  81. std::queue<Cell*> q;
  82. q.push(this);
  83. clearCell_();
  84. this->AddedToQuery_ = false;
  85. auto f = [&q](Cell * t) {
  86. if (t && t->AddedToQuery_ == true) {
  87. q.push(t);
  88. t->AddedToQuery_ = false;
  89. }
  90. };
  91. while (!q.empty()) {
  92. Cell * Now = q.front();
  93. q.pop();
  94. Now->clearCell_();
  95. f(Now->getleftUp());
  96. f(Now->getleft());
  97. f(Now->getleftDown());
  98. f(Now->getrightUp());
  99. f(Now->getright());
  100. f(Now->getrightDown());
  101. }
  102. }
  103. void Cell::handleAllMoveableCellsAndUnmoveableCells_(std::queue<Cell*> & Q) {
  104. std::queue<Cell*> q;
  105. q.push(this);
  106. setDistance(0);
  107. auto f = [&q, &Q](Cell * t, Cell * parent) {
  108. if (t && !t->AddedToQuery_) {
  109. t->AddedToQuery_ = true;
  110. if (t->getCharacter() != NULL) {
  111. t->setisMoveable(false);
  112. Q.push(t);
  113. return;
  114. }
  115. q.push(t);
  116. t->setDistance(parent->getDistance() + 1);
  117. }
  118. };
  119. while (!q.empty()) {
  120. Cell * Now = q.front();
  121. Now->setisMoveable(true);
  122. if (getCharacter() != NULL && getCharacter()->СanAttack(Now->getDistance())) {
  123. Now->setisAttackable(true);
  124. }
  125. else {
  126. Now->setisAttackable(false);
  127. }
  128. q.pop();
  129. f(Now->getleftUp(), Now);
  130. f(Now->getleft(), Now);
  131. f(Now->getleftDown(), Now);
  132. f(Now->getrightUp(), Now);
  133. f(Now->getright(), Now);
  134. f(Now->getrightDown(), Now);
  135. }
  136. }
  137. void Cell::handleAllUnmoveableCells_(std::queue<Cell*> & Q) {
  138. auto f = [&Q](Cell * t, Cell * parent) {
  139. if (t && !t->AddedToQuery_) {
  140. t->AddedToQuery_ = true;
  141. Q.push(t);
  142. }
  143. };
  144. while (!Q.empty()) {
  145. Cell * Now = Q.front();
  146. Now->setisMoveable(false);
  147. Now->setisAttackable(false);
  148. Q.pop();
  149. f(Now->getleftUp(), Now);
  150. f(Now->getleft(), Now);
  151. f(Now->getleftDown(), Now);
  152. f(Now->getrightUp(), Now);
  153. f(Now->getright(), Now);
  154. f(Now->getrightDown(), Now);
  155. }
  156. }
  157. void Cell::RecalculateTableWithCenterThisPoint() {
  158. clearTable_();
  159. std::queue<Cell*> qWithoutMoveable;
  160. handleAllMoveableCellsAndUnmoveableCells_(qWithoutMoveable);
  161. handleAllUnmoveableCells_(qWithoutMoveable);
  162. }
  163. std::vector <Cell*> Cell::actualPath(Cell* to) {//std::vector<Cell*> âêëþ÷àåòñÿ â ñåáÿ è this, è end
  164. if (!to || !to->getisMoveable())return std::vector<Cell*>();
  165. auto ret = std::vector<Cell*>(1, to);
  166. while (to != this) {
  167. Cell * parent = NULL;
  168. auto f = [&parent](Cell * TestParent, Cell * Now) {
  169. if (TestParent && TestParent->getDistance() + 1 == Now->getDistance() && TestParent->getisMoveable()) {
  170. parent = TestParent;
  171. }
  172. };
  173. f(to->getleftUp(), to);
  174. f(to->getleft(), to);
  175. f(to->getleftDown(), to);
  176. f(to->getrightUp(), to);
  177. f(to->getright(), to);
  178. f(to->getrightDown(), to);
  179. to = parent;
  180. ret.push_back(to);
  181. }
  182. return ret;
  183. }