cell.cpp 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. #include <cmath>
  12. class EffectsForCell{
  13. public:
  14. void OperateOnCell(Cell*){}
  15. };
  16. Cell::Cell()
  17. {
  18. leftUp_ = left_ = leftDown_ = nullptr;
  19. rightUp_ = right_ = rightDown_ = nullptr;
  20. clearCell_();
  21. AddedToQuery_ = true;
  22. col_ = row_ = 0;
  23. coor_x_ = coor_y_ = -1;
  24. }
  25. Cell * Cell::getleftUp() {
  26. return leftUp_;
  27. }
  28. void Cell::setleftUp(Cell * t) {
  29. leftUp_ = t;
  30. }
  31. Cell * Cell::getleft() {
  32. return left_;
  33. }
  34. void Cell::setleft(Cell * t) {
  35. left_ = t;
  36. }
  37. Cell * Cell::getleftDown() {
  38. return leftDown_;
  39. }
  40. void Cell::setleftDown(Cell * t) {
  41. leftDown_ = t;
  42. }
  43. Cell * Cell::getrightUp() {
  44. return rightUp_;
  45. }
  46. void Cell::setrightUp(Cell * t) {
  47. rightUp_ = t;
  48. }
  49. Cell * Cell::getright() {
  50. return right_;
  51. }
  52. void Cell::setright(Cell * t) {
  53. right_ = t;
  54. }
  55. Cell * Cell::getrightDown() {
  56. return rightDown_;
  57. }
  58. void Cell::setrightDown(Cell * t) {
  59. rightDown_ = t;
  60. }
  61. std::shared_ptr<Unit> Cell::getCharacter() {
  62. return character_;
  63. }
  64. void Cell::setCharacter(std::shared_ptr<Unit> t) {
  65. character_ = t;
  66. }
  67. double Cell::getXCoordinate()
  68. {
  69. return coor_x_;
  70. }
  71. void Cell::setXCoordinate(double coordinate)
  72. {
  73. coor_x_ = coordinate;
  74. }
  75. double Cell::getYCoordinate() {
  76. return coor_y_;
  77. }
  78. void Cell::setYCoordinate(double coordinate) {
  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. parent->getCharacter();
  265. t->AddedToQuery_ = true;
  266. Q.push(t);
  267. }
  268. };
  269. while (!Q.empty()) {
  270. Cell * Now = Q.front();
  271. recalcMoveable_(Now, false);
  272. recalcAttackable_(Now, false);
  273. Q.pop();
  274. f(Now->getleftUp(), Now);
  275. f(Now->getleft(), Now);
  276. f(Now->getleftDown(), Now);
  277. f(Now->getrightUp(), Now);
  278. f(Now->getright(), Now);
  279. f(Now->getrightDown(), Now);
  280. }
  281. }
  282. Cell* Cell::getRealShootTarget(Cell* next){
  283. if(next == this){
  284. return next;
  285. }
  286. int next_col = next->col_;
  287. int next_row = next->row_;
  288. if(next_col - col_ == next_row - row_){//GoMainDiagonal
  289. if(next_col > col_){
  290. return getrightDown()->getright();
  291. }
  292. if(next_col < col_){
  293. return getleftUp()->getleft();
  294. }
  295. throw std::string("Don`t recalculated");
  296. }
  297. if(-2 * (next_col - col_) == (next_row - row_)){//GoSecondaryDiagonal
  298. if(next_col < col_){
  299. return getrightUp()->getright();
  300. }
  301. if(next_col > col_){
  302. return getleftDown()->getleft();
  303. }
  304. throw std::string("Don`t recalculated");
  305. }
  306. if(next_col - col_ == -2 * (next_row - row_)){//GoUp
  307. if(next_col < col_){
  308. return (getleftUp() != nullptr ? getleftUp()->getrightUp() : getrightUp()->getleftUp());
  309. }
  310. if(next_col > col_){
  311. return (getleftDown() != nullptr ? getleftDown()->getrightDown() : getrightDown()->getleftDown());
  312. }
  313. throw std::string("Don`t recalculated");
  314. }
  315. int row_secondary_diag = row_ - 2 * (next_col - col_);
  316. int row_main_diag = row_ + next_col - col_;
  317. int twice_row_up_line = 2 * row_ + col_ - next_col;
  318. if(next_col > col_){
  319. if(next_row < row_main_diag)return getleft();
  320. if(2 * next_row < twice_row_up_line)return getleftUp();
  321. if(next_row < row_secondary_diag)return getrightUp();
  322. return getright();
  323. }
  324. else{
  325. if(next_row < row_secondary_diag)return getleft();
  326. if(2 * next_row < twice_row_up_line)return getleftDown();
  327. if(next_row < row_main_diag)return getrightDown();
  328. return getright();
  329. }
  330. }
  331. std::vector<std::pair<double, double> > Cell::getPoints(double radius, double start_angle) {
  332. const double delta_radians = acos(-1) / 180.0;
  333. std::vector<std::pair<double, double> > res;
  334. std::vector<double> angles = {0, 60, 120, 180, 240, 300};
  335. for(size_t i = 0;i < angles.size();++i){
  336. angles[i] += start_angle;
  337. res.push_back({coor_x_ + radius * std::cos(angles[i] * delta_radians),
  338. coor_y_ + radius * std::sin(angles[i] * delta_radians)});
  339. }
  340. return res;
  341. }