cell.cpp 9.2 KB

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