Cell.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. #include "Cell.h"
  2. #include "unit.h"
  3. #include <queue>
  4. #include <vector>
  5. Cell::Cell(Unit & character) {
  6. upLeft_ = up_ = upRight_ = nullptr;
  7. downLeft_ = down_ = downRight_ = nullptr;
  8. character_ = &character;
  9. clearCell_();
  10. AddedToQuery_ = true;
  11. }
  12. Cell * Cell::getUpLeft() {
  13. return upLeft_;
  14. }
  15. void Cell::setUpLeft(Cell * t) {
  16. upLeft_ = t;
  17. }
  18. Cell * Cell::getUp() {
  19. return up_;
  20. }
  21. void Cell::setUp(Cell * t) {
  22. up_ = t;
  23. }
  24. Cell * Cell::getUpRight() {
  25. return upRight_;
  26. }
  27. void Cell::setUpRight(Cell * t) {
  28. upRight_ = t;
  29. }
  30. Cell * Cell::getDownLeft() {
  31. return downLeft_;
  32. }
  33. void Cell::setDownLeft(Cell * t) {
  34. downLeft_ = t;
  35. }
  36. Cell * Cell::getDown() {
  37. return down_;
  38. }
  39. void Cell::setDown(Cell * t) {
  40. down_ = t;
  41. }
  42. Cell * Cell::getDownRight() {
  43. return downRight_;
  44. }
  45. void Cell::setDownRight(Cell * t) {
  46. downRight_ = 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->getUpLeft());
  96. f(Now->getUp());
  97. f(Now->getUpRight());
  98. f(Now->getDownLeft());
  99. f(Now->getDown());
  100. f(Now->getDownRight());
  101. }
  102. }
  103. void Cell::handleAllMoveableCellsAndUnmoveableCells_(std::queue<Cell*> & Q) {
  104. std::queue<Cell*> q;
  105. q.push(this);
  106. setDistance(0);
  107. while (!q.empty()) {
  108. Cell * Now = q.front();
  109. Now->setisMoveable(true);
  110. if (getCharacter() != NULL && getCharacter()->canAttack(Now->getDistance())) {
  111. Now->setisAttackable(true);
  112. }
  113. else {
  114. Now->setisAttackable(false);
  115. }
  116. q.pop();
  117. auto f = [&q, &Q](Cell * t, Cell * parent) {
  118. if (t && !t->AddedToQuery_) {
  119. t->AddedToQuery_ = true;
  120. if (t->getCharacter() != NULL) {
  121. t->setisMoveable(false);
  122. Q.push(t);
  123. return;
  124. }
  125. q.push(t);
  126. t->setDistance(parent->getDistance() + 1);
  127. }
  128. };
  129. f(Now->getUpLeft(), Now);
  130. f(Now->getUp(), Now);
  131. f(Now->getUpRight(), Now);
  132. f(Now->getDownLeft(), Now);
  133. f(Now->getDown(), Now);
  134. f(Now->getDownRight(), Now);
  135. }
  136. }
  137. void Cell::handleAllUnmoveableCells_(std::queue<Cell*> & Q) {
  138. while (!Q.empty()) {
  139. Cell * Now = Q.front();
  140. Now->setisMoveable(false);
  141. Now->setisAttackable(false);
  142. Q.pop();
  143. auto f = [&Q](Cell * t, Cell * parent) {
  144. if (t && !t->AddedToQuery_) {
  145. t->AddedToQuery_ = true;
  146. Q.push(t);
  147. }
  148. };
  149. f(Now->getUpLeft(), Now);
  150. f(Now->getUp(), Now);
  151. f(Now->getUpRight(), Now);
  152. f(Now->getDownLeft(), Now);
  153. f(Now->getDown(), Now);
  154. f(Now->getDownRight(), 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->getUpLeft(), to);
  174. f(to->getUp(), to);
  175. f(to->getUpRight(), to);
  176. f(to->getDownLeft(), to);
  177. f(to->getDown(), to);
  178. f(to->getDownRight(), to);
  179. to = parent;
  180. ret.push_back(to);
  181. }
  182. return ret;
  183. }