Cell.cpp 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  1. /*
  2. * I love Qt
  3. */
  4. #include "Cell.h"
  5. #include "units/unit.h"
  6. #include <string>
  7. #include <queue>
  8. #include <vector>
  9. #include <map>
  10. #include <iostream>
  11. class EffectsForCell{
  12. public:
  13. void OperateOnCell(Cell*){}
  14. };
  15. Cell::Cell(Unit* character) {
  16. leftUp_ = left_ = leftDown_ = nullptr;
  17. rightUp_ = right_ = rightDown_ = nullptr;
  18. character_ = character;
  19. clearCell_();
  20. AddedToQuery_ = true;
  21. col_ = row_ = 0;
  22. }
  23. Cell * Cell::getleftUp() {
  24. return leftUp_;
  25. }
  26. void Cell::setleftUp(Cell * t) {
  27. leftUp_ = t;
  28. }
  29. Cell * Cell::getleft() {
  30. return left_;
  31. }
  32. void Cell::setleft(Cell * t) {
  33. left_ = t;
  34. }
  35. Cell * Cell::getleftDown() {
  36. return leftDown_;
  37. }
  38. void Cell::setleftDown(Cell * t) {
  39. leftDown_ = t;
  40. }
  41. Cell * Cell::getrightUp() {
  42. return rightUp_;
  43. }
  44. void Cell::setrightUp(Cell * t) {
  45. rightUp_ = t;
  46. }
  47. Cell * Cell::getright() {
  48. return right_;
  49. }
  50. void Cell::setright(Cell * t) {
  51. right_ = t;
  52. }
  53. Cell * Cell::getrightDown() {
  54. return rightDown_;
  55. }
  56. void Cell::setrightDown(Cell * t) {
  57. rightDown_ = t;
  58. }
  59. Unit * Cell::getCharacter() {
  60. return character_;
  61. }
  62. void Cell::setCharacter(Unit * t) {
  63. character_ = t;
  64. }
  65. int Cell::getdistance_barrier(){
  66. return distance_barrier_;
  67. }
  68. void Cell::setdistance_barrier(int distance_barrier){
  69. distance_barrier_ = distance_barrier;
  70. }
  71. int Cell::getdistance_through(){
  72. return distance_through_;
  73. }
  74. void Cell::setdistance_through(int distance_through){
  75. distance_through_ = distance_through;
  76. }
  77. bool Cell::getisMoveAble(){
  78. return isMoveAble_;
  79. }
  80. void Cell::setisMoveAble(bool isMoveAble){
  81. isMoveAble_ = isMoveAble;
  82. }
  83. bool Cell::getisMeleeAttackAble(){
  84. return isMeleeAttackAble_;
  85. }
  86. void Cell::setisMeleeAttackAble(bool isMeleeAttackAble){
  87. isMeleeAttackAble_ = isMeleeAttackAble;
  88. }
  89. bool Cell::getisRangeAttackAble(){
  90. return isRangeAttackAble_;
  91. }
  92. void Cell::setisRangeAttackAble(bool isRangeAttackAble){
  93. isRangeAttackAble_ = isRangeAttackAble;
  94. }
  95. bool Cell::isEmpty() {
  96. return character_ == nullptr;
  97. }
  98. void Cell::recalculateAllEffectsList(){
  99. for(std::vector<EffectsForCell*>::iterator it = beginIteratorEffectsList();
  100. it != endIteratorEffectsList();++it){
  101. (*it)->OperateOnCell(this);
  102. }
  103. }
  104. void Cell::add(EffectsForCell* effect){
  105. if(effect == nullptr)
  106. throw new int(228);
  107. effects_list_.push_back(effect);
  108. }
  109. void Cell::remove(std::vector<EffectsForCell*>::iterator it){
  110. if(beginIteratorEffectsList() <= it && it < endIteratorEffectsList()){
  111. effects_list_.erase(it);
  112. }
  113. }
  114. void Cell::remove(EffectsForCell* effect){
  115. for(std::vector<EffectsForCell*>::iterator it = beginIteratorEffectsList();
  116. it != endIteratorEffectsList();++it){
  117. if((*it) == effect){
  118. remove(it);
  119. return;
  120. }
  121. }
  122. }
  123. std::vector<EffectsForCell*>::iterator Cell::beginIteratorEffectsList(){
  124. return effects_list_.begin();
  125. }
  126. std::vector<EffectsForCell*>::iterator Cell::endIteratorEffectsList(){
  127. return effects_list_.end();
  128. }
  129. void Cell::RecalculateTableWithCenterThisPoint() {
  130. clearTable_();
  131. std::queue<Cell*> qWithUnMoveable;
  132. updateMoveableCells_(qWithUnMoveable);
  133. updateUnMovealeCells_(qWithUnMoveable);
  134. }
  135. std::vector <Cell*> Cell::actualPath(Cell* to) {
  136. if (!to || !to->getisMoveAble())return std::vector<Cell*>();
  137. auto ret = std::vector<Cell*>(1, to);
  138. while (to != this) {
  139. Cell * parent = nullptr;
  140. auto f = [&parent](Cell * TestParent, Cell * Now) {
  141. if (TestParent && TestParent->getdistance_barrier() + 1 == Now->getdistance_barrier()
  142. && TestParent->getisMoveAble()) {
  143. parent = TestParent;
  144. }
  145. };
  146. if(parent == nullptr) throw std::string("Don`t recalculated");
  147. f(to->getleftUp(), to);
  148. f(to->getleft(), to);
  149. f(to->getleftDown(), to);
  150. f(to->getrightUp(), to);
  151. f(to->getright(), to);
  152. f(to->getrightDown(), to);
  153. to = parent;
  154. ret.push_back(to);
  155. }
  156. return ret;
  157. }
  158. void Cell::clearCell_() {
  159. setdistance_barrier(-1);
  160. setdistance_through(-1);
  161. setisMeleeAttackAble(false);
  162. setisMoveAble(false);
  163. setisRangeAttackAble(false);
  164. }
  165. void Cell::clearTable_() {
  166. std::queue<Cell*> q;
  167. q.push(this);
  168. clearCell_();
  169. setdistance_through(0);
  170. this->AddedToQuery_ = false;
  171. auto f = [&q](Cell * t, Cell * parent, int delta_col, int delta_row) {
  172. if (t && t->AddedToQuery_ == true) {
  173. q.push(t);
  174. t->col_ = parent->col_ + delta_col;
  175. t->row_ = parent->row_ + delta_row;
  176. t->setdistance_through(
  177. parent->getdistance_through() + 1
  178. );
  179. t->AddedToQuery_ = false;
  180. }
  181. };
  182. while (!q.empty()) {
  183. Cell * Now = q.front();
  184. q.pop();
  185. Now->clearCell_();
  186. f(Now->getleftUp(), Now, -1, 0);
  187. f(Now->getleft(), Now, 0, -1);
  188. f(Now->getleftDown(), Now, 1, -1);
  189. f(Now->getrightUp(), Now, -1, 1);
  190. f(Now->getright(), Now, 0, 1);
  191. f(Now->getrightDown(), Now, 1, 0);
  192. }
  193. }
  194. void Cell::recalcAttackable_(Cell * Now,bool isReached){
  195. /* if(Now == nullptr)return;
  196. if (!isEmpty() && !Now->isEmpty() &&
  197. getCharacter()->canAttackForDistance("Melee", Now->getdistance_barrier())
  198. ) {
  199. Now->setisMeleeAttackAble(isReached);
  200. }
  201. if (!isEmpty() && !Now->isEmpty() &&
  202. getCharacter()->canAttackForDistance("Range", Now->getdistance_barrier())
  203. ) {
  204. Now->setisRangeAttackAble(true);
  205. }*/
  206. return;
  207. }
  208. void Cell::recalcMoveable_(Cell * Now, bool isReached){
  209. if(Now == nullptr || Now->isEmpty() || isEmpty())return;
  210. if(!isEmpty() && Now->isEmpty() &&
  211. getCharacter()->canMoveForDistance(Now->getdistance_barrier())){
  212. Now->setisMoveAble(isReached);
  213. }
  214. }
  215. void Cell::updateMoveableCells_(std::queue<Cell*> & Q) {
  216. std::queue<Cell*> q;
  217. q.push(this);
  218. setdistance_barrier(0);
  219. auto f = [&q, &Q](Cell * t, Cell * parent) {
  220. if (t && !t->AddedToQuery_) {
  221. t->AddedToQuery_ = true;
  222. if (t->getCharacter() != NULL) {
  223. Q.push(t);
  224. return;
  225. }
  226. q.push(t);
  227. t->setdistance_barrier(
  228. parent->getdistance_barrier() + 1
  229. );
  230. }
  231. };
  232. while (!q.empty()) {
  233. Cell * Now = q.front();
  234. q.pop();
  235. recalcMoveable_(Now, true);
  236. recalcAttackable_(Now, true);
  237. f(Now->getleftUp(), Now);
  238. f(Now->getleft(), Now);
  239. f(Now->getleftDown(), Now);
  240. f(Now->getrightUp(), Now);
  241. f(Now->getright(), Now);
  242. f(Now->getrightDown(), Now);
  243. }
  244. }
  245. void Cell::updateUnMovealeCells_(std::queue<Cell*> & Q) {
  246. auto f = [&Q](Cell * t, Cell * parent) {
  247. if (t && !t->AddedToQuery_) {
  248. t->AddedToQuery_ = true;
  249. Q.push(t);
  250. }
  251. };
  252. while (!Q.empty()) {
  253. Cell * Now = Q.front();
  254. recalcMoveable_(Now, false);
  255. recalcAttackable_(Now, false);
  256. Q.pop();
  257. f(Now->getleftUp(), Now);
  258. f(Now->getleft(), Now);
  259. f(Now->getleftDown(), Now);
  260. f(Now->getrightUp(), Now);
  261. f(Now->getright(), Now);
  262. f(Now->getrightDown(), Now);
  263. }
  264. }
  265. Cell* Cell::getRealShootTarget(Cell* next){
  266. if(next == this){
  267. return next;
  268. }
  269. int next_col = next->col_;
  270. int next_row = next->row_;
  271. if(next_col - col_ == next_row - row_){//GoMainDiagonal
  272. if(next_col > col_){
  273. return getrightDown()->getright();
  274. }
  275. if(next_col < col_){
  276. return getleftUp()->getleft();
  277. }
  278. throw std::string("Don`t recalculated");
  279. }
  280. if(-2 * (next_col - col_) == (next_row - row_)){//GoSecondaryDiagonal
  281. if(next_col < col_){
  282. return getrightUp()->getright();
  283. }
  284. if(next_col > col_){
  285. return getleftDown()->getleft();
  286. }
  287. throw std::string("Don`t recalculated");
  288. }
  289. if(next_col - col_ == -2 * (next_row - row_)){//GoUp
  290. if(next_col < col_){
  291. return (getleftUp() != nullptr ? getleftUp()->getrightUp() : getrightUp()->getleftUp());
  292. }
  293. if(next_col > col_){
  294. return (getleftDown() != nullptr ? getleftDown()->getrightDown() : getrightDown()->getleftDown());
  295. }
  296. throw std::string("Don`t recalculated");
  297. }
  298. int row_secondary_diag = row_ - 2 * (next_col - col_);
  299. int row_main_diag = row_ + next_col - col_;
  300. int twice_row_up_line = 2 * row_ + col_ - next_col;
  301. if(next_col > col_){
  302. if(next_row < row_main_diag)return getleft();
  303. if(2 * next_row < twice_row_up_line)return getleftUp();
  304. if(next_row < row_secondary_diag)return getrightUp();
  305. return getright();
  306. }
  307. else{
  308. if(next_row < row_secondary_diag)return getleft();
  309. if(2 * next_row < twice_row_up_line)return getleftDown();
  310. if(next_row < row_main_diag)return getrightDown();
  311. return getright();
  312. }
  313. }