cell.cpp 8.5 KB

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