|
@@ -1,67 +1,56 @@
|
|
#include "Cell.h"
|
|
#include "Cell.h"
|
|
-//#include "unit.h"
|
|
|
|
|
|
+#include "unit.h"
|
|
#include <queue>
|
|
#include <queue>
|
|
#include <vector>
|
|
#include <vector>
|
|
|
|
|
|
-/*
|
|
|
|
- * Добавьте в Unit эту функцию
|
|
|
|
- */
|
|
|
|
-class Unit {
|
|
|
|
-public:
|
|
|
|
- bool canAttackForDistance(int) { return true; }
|
|
|
|
- bool canAttackUnit(Unit*) { return true; }
|
|
|
|
-};
|
|
|
|
-
|
|
|
|
Cell::Cell(Unit * character) {
|
|
Cell::Cell(Unit * character) {
|
|
- left_up_ = left_ = left_down_ = nullptr;
|
|
|
|
- right_up_ = right_ = right_down_ = nullptr;
|
|
|
|
- character_ = character;
|
|
|
|
|
|
+ leftUp_ = left_ = leftDown_ = nullptr;
|
|
|
|
+ rightUp_ = right_ = rightDown_ = nullptr;
|
|
|
|
+ character_ = &character;
|
|
clearCell_();
|
|
clearCell_();
|
|
AddedToQuery_ = true;
|
|
AddedToQuery_ = true;
|
|
}
|
|
}
|
|
|
|
|
|
-Cell::Cell(int i, int j) :i(i), j(j), AddedToQuery_(true) {}
|
|
|
|
-
|
|
|
|
-Cell * Cell::getLeftUp() {
|
|
|
|
- return left_up_;
|
|
|
|
|
|
+Cell * Cell::getleftUp() {
|
|
|
|
+ return leftUp_;
|
|
}
|
|
}
|
|
-void Cell::setLeftUp(Cell * t) {
|
|
|
|
- left_up_ = t;
|
|
|
|
|
|
+void Cell::setleftUp(Cell * t) {
|
|
|
|
+ leftUp_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
-Cell * Cell::getLeft() {
|
|
|
|
|
|
+Cell * Cell::getleft() {
|
|
return left_;
|
|
return left_;
|
|
}
|
|
}
|
|
-void Cell::setLeft(Cell * t) {
|
|
|
|
|
|
+void Cell::setleft(Cell * t) {
|
|
left_ = t;
|
|
left_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
-Cell * Cell::getLeftDown() {
|
|
|
|
- return left_down_;
|
|
|
|
|
|
+Cell * Cell::getleftDown() {
|
|
|
|
+ return leftDown_;
|
|
}
|
|
}
|
|
-void Cell::setLeftDown(Cell * t) {
|
|
|
|
- left_down_ = t;
|
|
|
|
|
|
+void Cell::setleftDown(Cell * t) {
|
|
|
|
+ leftDown_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
-Cell * Cell::getRightUp() {
|
|
|
|
- return right_up_;
|
|
|
|
|
|
+Cell * Cell::getrightUp() {
|
|
|
|
+ return rightUp_;
|
|
}
|
|
}
|
|
-void Cell::setRightUp(Cell * t) {
|
|
|
|
- right_up_ = t;
|
|
|
|
|
|
+void Cell::setrightUp(Cell * t) {
|
|
|
|
+ rightUp_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
-Cell * Cell::getRight() {
|
|
|
|
|
|
+Cell * Cell::getright() {
|
|
return right_;
|
|
return right_;
|
|
}
|
|
}
|
|
-void Cell::setRight(Cell * t) {
|
|
|
|
|
|
+void Cell::setright(Cell * t) {
|
|
right_ = t;
|
|
right_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
-Cell * Cell::getRightDown() {
|
|
|
|
- return right_down_;
|
|
|
|
|
|
+Cell * Cell::getrightDown() {
|
|
|
|
+ return rightDown_;
|
|
}
|
|
}
|
|
-void Cell::setRightDown(Cell * t) {
|
|
|
|
- right_down_ = t;
|
|
|
|
|
|
+void Cell::setrightDown(Cell * t) {
|
|
|
|
+ rightDown_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
Unit * Cell::getCharacter() {
|
|
Unit * Cell::getCharacter() {
|
|
@@ -71,17 +60,17 @@ void Cell::setCharacter(Unit * t) {
|
|
character_ = t;
|
|
character_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
-bool Cell::getIsMoveable() {
|
|
|
|
|
|
+bool Cell::getisMoveable() {
|
|
return isMoveable_;
|
|
return isMoveable_;
|
|
}
|
|
}
|
|
-void Cell::setIsMoveable(bool t) {
|
|
|
|
|
|
+void Cell::setisMoveable(bool t) {
|
|
isMoveable_ = t;
|
|
isMoveable_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
-bool Cell::getIsAttackable() {
|
|
|
|
|
|
+bool Cell::getisAttackable() {
|
|
return isAttackable_;
|
|
return isAttackable_;
|
|
}
|
|
}
|
|
-void Cell::setIsAttackable(bool t) {
|
|
|
|
|
|
+void Cell::setisAttackable(bool t) {
|
|
isAttackable_ = t;
|
|
isAttackable_ = t;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -97,17 +86,15 @@ bool Cell::isEmpty() {
|
|
}
|
|
}
|
|
|
|
|
|
void Cell::clearCell_() {
|
|
void Cell::clearCell_() {
|
|
- setIsMoveable(false);
|
|
|
|
|
|
+ setisMoveable(false);
|
|
|
|
+ setisAttackable(false);
|
|
setDistance(-1);
|
|
setDistance(-1);
|
|
}
|
|
}
|
|
|
|
|
|
-void Cell::clearTable_(bool NeedClearCell = true) {
|
|
|
|
|
|
+void Cell::clearTable_() {
|
|
std::queue<Cell*> q;
|
|
std::queue<Cell*> q;
|
|
q.push(this);
|
|
q.push(this);
|
|
- setIsAttackable(false);
|
|
|
|
- if (NeedClearCell) {
|
|
|
|
- clearCell_();
|
|
|
|
- }
|
|
|
|
|
|
+ clearCell_();
|
|
this->AddedToQuery_ = false;
|
|
this->AddedToQuery_ = false;
|
|
auto f = [&q](Cell * t) {
|
|
auto f = [&q](Cell * t) {
|
|
if (t && t->AddedToQuery_ == true) {
|
|
if (t && t->AddedToQuery_ == true) {
|
|
@@ -118,16 +105,13 @@ void Cell::clearTable_(bool NeedClearCell = true) {
|
|
while (!q.empty()) {
|
|
while (!q.empty()) {
|
|
Cell * Now = q.front();
|
|
Cell * Now = q.front();
|
|
q.pop();
|
|
q.pop();
|
|
- Now->setIsAttackable(false);
|
|
|
|
- if (NeedClearCell) {
|
|
|
|
- Now->clearCell_();
|
|
|
|
- }
|
|
|
|
- f(Now->getLeftUp());
|
|
|
|
- f(Now->getLeft());
|
|
|
|
- f(Now->getLeftDown());
|
|
|
|
- f(Now->getRightUp());
|
|
|
|
- f(Now->getRight());
|
|
|
|
- f(Now->getRightDown());
|
|
|
|
|
|
+ Now->clearCell_();
|
|
|
|
+ f(Now->getleftUp());
|
|
|
|
+ f(Now->getleft());
|
|
|
|
+ f(Now->getleftDown());
|
|
|
|
+ f(Now->getrightUp());
|
|
|
|
+ f(Now->getright());
|
|
|
|
+ f(Now->getrightDown());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -135,38 +119,34 @@ void Cell::handleAllMoveableCellsAndUnmoveableCells_(std::queue<Cell*> & Q) {
|
|
std::queue<Cell*> q;
|
|
std::queue<Cell*> q;
|
|
q.push(this);
|
|
q.push(this);
|
|
setDistance(0);
|
|
setDistance(0);
|
|
- AddedToQuery_ = true;
|
|
|
|
- setIsMoveable(true);
|
|
|
|
- setIsAttackable(true);
|
|
|
|
auto f = [&q, &Q](Cell * t, Cell * parent) {
|
|
auto f = [&q, &Q](Cell * t, Cell * parent) {
|
|
if (t && !t->AddedToQuery_) {
|
|
if (t && !t->AddedToQuery_) {
|
|
t->AddedToQuery_ = true;
|
|
t->AddedToQuery_ = true;
|
|
- t->setDistance(parent->getDistance() + 1);
|
|
|
|
if (t->getCharacter() != NULL) {
|
|
if (t->getCharacter() != NULL) {
|
|
- t->setIsMoveable(false);
|
|
|
|
|
|
+ t->setisMoveable(false);
|
|
Q.push(t);
|
|
Q.push(t);
|
|
return;
|
|
return;
|
|
}
|
|
}
|
|
q.push(t);
|
|
q.push(t);
|
|
|
|
+ t->setDistance(parent->getDistance() + 1);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
while (!q.empty()) {
|
|
while (!q.empty()) {
|
|
Cell * Now = q.front();
|
|
Cell * Now = q.front();
|
|
- Now->setIsMoveable(true);
|
|
|
|
- if (getCharacter() != NULL && getCharacter()->canAttackForDistance(Now->getDistance()) &&
|
|
|
|
- (Now->getCharacter() == NULL || getCharacter()->canAttackUnit(Now->getCharacter()))) {
|
|
|
|
- Now->setIsAttackable(true);
|
|
|
|
|
|
+ Now->setisMoveable(true);
|
|
|
|
+ if (getCharacter() != NULL && getCharacter()->СanAttack(Now->getDistance())) {
|
|
|
|
+ Now->setisAttackable(true);
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
- Now->setIsAttackable(false);
|
|
|
|
|
|
+ Now->setisAttackable(false);
|
|
}
|
|
}
|
|
q.pop();
|
|
q.pop();
|
|
- f(Now->getLeftUp(), Now);
|
|
|
|
- f(Now->getLeft(), Now);
|
|
|
|
- f(Now->getLeftDown(), Now);
|
|
|
|
- f(Now->getRightUp(), Now);
|
|
|
|
- f(Now->getRight(), Now);
|
|
|
|
- f(Now->getRightDown(), Now);
|
|
|
|
|
|
+ f(Now->getleftUp(), Now);
|
|
|
|
+ f(Now->getleft(), Now);
|
|
|
|
+ f(Now->getleftDown(), Now);
|
|
|
|
+ f(Now->getrightUp(), Now);
|
|
|
|
+ f(Now->getright(), Now);
|
|
|
|
+ f(Now->getrightDown(), Now);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -174,71 +154,46 @@ void Cell::handleAllUnmoveableCells_(std::queue<Cell*> & Q) {
|
|
auto f = [&Q](Cell * t, Cell * parent) {
|
|
auto f = [&Q](Cell * t, Cell * parent) {
|
|
if (t && !t->AddedToQuery_) {
|
|
if (t && !t->AddedToQuery_) {
|
|
t->AddedToQuery_ = true;
|
|
t->AddedToQuery_ = true;
|
|
- t->setDistance(parent->getDistance() + 1);
|
|
|
|
Q.push(t);
|
|
Q.push(t);
|
|
}
|
|
}
|
|
};
|
|
};
|
|
while (!Q.empty()) {
|
|
while (!Q.empty()) {
|
|
Cell * Now = Q.front();
|
|
Cell * Now = Q.front();
|
|
- Now->setIsMoveable(false);
|
|
|
|
- Now->setIsAttackable(false);
|
|
|
|
|
|
+ Now->setisMoveable(false);
|
|
|
|
+ Now->setisAttackable(false);
|
|
Q.pop();
|
|
Q.pop();
|
|
- f(Now->getLeftUp(), Now);
|
|
|
|
- f(Now->getLeft(), Now);
|
|
|
|
- f(Now->getLeftDown(), Now);
|
|
|
|
- f(Now->getRightUp(), Now);
|
|
|
|
- f(Now->getRight(), Now);
|
|
|
|
- f(Now->getRightDown(), Now);
|
|
|
|
|
|
+ f(Now->getleftUp(), Now);
|
|
|
|
+ f(Now->getleft(), Now);
|
|
|
|
+ f(Now->getleftDown(), Now);
|
|
|
|
+ f(Now->getrightUp(), Now);
|
|
|
|
+ f(Now->getright(), Now);
|
|
|
|
+ f(Now->getrightDown(), Now);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
-void Cell::recalculateTableWithCenterThisPoint() {
|
|
|
|
|
|
+void Cell::RecalculateTableWithCenterThisPoint() {
|
|
clearTable_();
|
|
clearTable_();
|
|
std::queue<Cell*> qWithoutMoveable;
|
|
std::queue<Cell*> qWithoutMoveable;
|
|
handleAllMoveableCellsAndUnmoveableCells_(qWithoutMoveable);
|
|
handleAllMoveableCellsAndUnmoveableCells_(qWithoutMoveable);
|
|
handleAllUnmoveableCells_(qWithoutMoveable);
|
|
handleAllUnmoveableCells_(qWithoutMoveable);
|
|
}
|
|
}
|
|
|
|
|
|
-void Cell::buffTableinDistance(int distance) {
|
|
|
|
- clearTable_(false);
|
|
|
|
- std::queue<Cell*> q;
|
|
|
|
- q.push(this);
|
|
|
|
- setIsAttackable(true);
|
|
|
|
- auto f = [&q, &distance](Cell * Now) {
|
|
|
|
- if (Now != NULL && Now->getDistance() <= distance && !Now->AddedToQuery_) {
|
|
|
|
- q.push(Now);
|
|
|
|
- Now->AddedToQuery_ = true;
|
|
|
|
- Now->setIsAttackable(true);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
- while (!q.empty()) {
|
|
|
|
- Cell * Now = q.front();
|
|
|
|
- q.pop();
|
|
|
|
- f(Now->getLeftUp());
|
|
|
|
- f(Now->getLeft());
|
|
|
|
- f(Now->getLeftDown());
|
|
|
|
- f(Now->getRightUp());
|
|
|
|
- f(Now->getRight());
|
|
|
|
- f(Now->getRightDown());
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-std::vector <Cell*> Cell::actualPath(Cell* to) {
|
|
|
|
- if (!to || !to->getIsMoveable())return std::vector<Cell*>();
|
|
|
|
|
|
+std::vector <Cell*> Cell::actualPath(Cell* to) {//std::vector<Cell*> âêëþ÷àåòñÿ â ñåáÿ è this, è end
|
|
|
|
+ if (!to || !to->getisMoveable())return std::vector<Cell*>();
|
|
auto ret = std::vector<Cell*>(1, to);
|
|
auto ret = std::vector<Cell*>(1, to);
|
|
while (to != this) {
|
|
while (to != this) {
|
|
Cell * parent = NULL;
|
|
Cell * parent = NULL;
|
|
auto f = [&parent](Cell * TestParent, Cell * Now) {
|
|
auto f = [&parent](Cell * TestParent, Cell * Now) {
|
|
- if (TestParent && TestParent->getDistance() + 1 == Now->getDistance() && TestParent->getIsMoveable()) {
|
|
|
|
|
|
+ if (TestParent && TestParent->getDistance() + 1 == Now->getDistance() && TestParent->getisMoveable()) {
|
|
parent = TestParent;
|
|
parent = TestParent;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
- f(to->getLeftUp(), to);
|
|
|
|
- f(to->getLeft(), to);
|
|
|
|
- f(to->getLeftDown(), to);
|
|
|
|
- f(to->getRightUp(), to);
|
|
|
|
- f(to->getRight(), to);
|
|
|
|
- f(to->getRightDown(), to);
|
|
|
|
|
|
+ f(to->getleftUp(), to);
|
|
|
|
+ f(to->getleft(), to);
|
|
|
|
+ f(to->getleftDown(), to);
|
|
|
|
+ f(to->getrightUp(), to);
|
|
|
|
+ f(to->getright(), to);
|
|
|
|
+ f(to->getrightDown(), to);
|
|
to = parent;
|
|
to = parent;
|
|
ret.push_back(to);
|
|
ret.push_back(to);
|
|
}
|
|
}
|