|
@@ -1,76 +1,94 @@
|
|
#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 СanAttack(int) { return true; }
|
|
|
|
+};
|
|
|
|
+
|
|
Cell::Cell(Unit & character) {
|
|
Cell::Cell(Unit & character) {
|
|
- upLeft_ = up_ = upRight_ = nullptr;
|
|
|
|
- downLeft_ = down_ = downRight_ = nullptr;
|
|
|
|
|
|
+ leftUp_ = left_ = leftDown_ = nullptr;
|
|
|
|
+ rightUp_ = right_ = rightDown_ = nullptr;
|
|
character_ = &character;
|
|
character_ = &character;
|
|
clearCell_();
|
|
clearCell_();
|
|
AddedToQuery_ = true;
|
|
AddedToQuery_ = true;
|
|
}
|
|
}
|
|
|
|
|
|
-Cell * Cell::getUpLeft() {
|
|
|
|
- return upLeft_;
|
|
|
|
|
|
+Cell * Cell::getleftUp() {
|
|
|
|
+ return leftUp_;
|
|
}
|
|
}
|
|
-void Cell::setUpLeft(Cell * t) {
|
|
|
|
- upLeft_ = t;
|
|
|
|
|
|
+void Cell::setleftUp(Cell * t) {
|
|
|
|
+ leftUp_ = t;
|
|
}
|
|
}
|
|
-Cell * Cell::getUp() {
|
|
|
|
- return up_;
|
|
|
|
|
|
+
|
|
|
|
+Cell * Cell::getleft() {
|
|
|
|
+ return left_;
|
|
}
|
|
}
|
|
-void Cell::setUp(Cell * t) {
|
|
|
|
- up_ = t;
|
|
|
|
|
|
+void Cell::setleft(Cell * t) {
|
|
|
|
+ left_ = t;
|
|
}
|
|
}
|
|
-Cell * Cell::getUpRight() {
|
|
|
|
- return upRight_;
|
|
|
|
|
|
+
|
|
|
|
+Cell * Cell::getleftDown() {
|
|
|
|
+ return leftDown_;
|
|
}
|
|
}
|
|
-void Cell::setUpRight(Cell * t) {
|
|
|
|
- upRight_ = t;
|
|
|
|
|
|
+void Cell::setleftDown(Cell * t) {
|
|
|
|
+ leftDown_ = t;
|
|
}
|
|
}
|
|
-Cell * Cell::getDownLeft() {
|
|
|
|
- return downLeft_;
|
|
|
|
|
|
+
|
|
|
|
+Cell * Cell::getrightUp() {
|
|
|
|
+ return rightUp_;
|
|
}
|
|
}
|
|
-void Cell::setDownLeft(Cell * t) {
|
|
|
|
- downLeft_ = t;
|
|
|
|
|
|
+void Cell::setrightUp(Cell * t) {
|
|
|
|
+ rightUp_ = t;
|
|
}
|
|
}
|
|
-Cell * Cell::getDown() {
|
|
|
|
- return down_;
|
|
|
|
|
|
+
|
|
|
|
+Cell * Cell::getright() {
|
|
|
|
+ return right_;
|
|
}
|
|
}
|
|
-void Cell::setDown(Cell * t) {
|
|
|
|
- down_ = t;
|
|
|
|
|
|
+void Cell::setright(Cell * t) {
|
|
|
|
+ right_ = t;
|
|
}
|
|
}
|
|
-Cell * Cell::getDownRight() {
|
|
|
|
- return downRight_;
|
|
|
|
|
|
+
|
|
|
|
+Cell * Cell::getrightDown() {
|
|
|
|
+ return rightDown_;
|
|
}
|
|
}
|
|
-void Cell::setDownRight(Cell * t) {
|
|
|
|
- downRight_ = t;
|
|
|
|
|
|
+void Cell::setrightDown(Cell * t) {
|
|
|
|
+ rightDown_ = t;
|
|
}
|
|
}
|
|
|
|
+
|
|
Unit * Cell::getCharacter() {
|
|
Unit * Cell::getCharacter() {
|
|
return character_;
|
|
return character_;
|
|
}
|
|
}
|
|
void Cell::setCharacter(Unit * t) {
|
|
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;
|
|
}
|
|
}
|
|
|
|
+
|
|
int Cell::getDistance() {
|
|
int Cell::getDistance() {
|
|
return distance_;
|
|
return distance_;
|
|
}
|
|
}
|
|
void Cell::setDistance(int t) {
|
|
void Cell::setDistance(int t) {
|
|
distance_ = t;
|
|
distance_ = t;
|
|
}
|
|
}
|
|
|
|
+
|
|
bool Cell::isEmpty() {
|
|
bool Cell::isEmpty() {
|
|
return character_ == NULL;
|
|
return character_ == NULL;
|
|
}
|
|
}
|
|
@@ -80,6 +98,7 @@ void Cell::clearCell_() {
|
|
setisAttackable(false);
|
|
setisAttackable(false);
|
|
setDistance(-1);
|
|
setDistance(-1);
|
|
}
|
|
}
|
|
|
|
+
|
|
void Cell::clearTable_() {
|
|
void Cell::clearTable_() {
|
|
std::queue<Cell*> q;
|
|
std::queue<Cell*> q;
|
|
q.push(this);
|
|
q.push(this);
|
|
@@ -95,74 +114,78 @@ void Cell::clearTable_() {
|
|
Cell * Now = q.front();
|
|
Cell * Now = q.front();
|
|
q.pop();
|
|
q.pop();
|
|
Now->clearCell_();
|
|
Now->clearCell_();
|
|
- f(Now->getUpLeft());
|
|
|
|
- f(Now->getUp());
|
|
|
|
- f(Now->getUpRight());
|
|
|
|
- f(Now->getDownLeft());
|
|
|
|
- f(Now->getDown());
|
|
|
|
- f(Now->getDownRight());
|
|
|
|
|
|
+ f(Now->getleftUp());
|
|
|
|
+ f(Now->getleft());
|
|
|
|
+ f(Now->getleftDown());
|
|
|
|
+ f(Now->getrightUp());
|
|
|
|
+ f(Now->getright());
|
|
|
|
+ f(Now->getrightDown());
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
void Cell::handleAllMoveableCellsAndUnmoveableCells_(std::queue<Cell*> & Q) {
|
|
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);
|
|
|
|
+ auto f = [&q, &Q](Cell * t, Cell * parent) {
|
|
|
|
+ if (t && !t->AddedToQuery_) {
|
|
|
|
+ t->AddedToQuery_ = true;
|
|
|
|
+ if (t->getCharacter() != NULL) {
|
|
|
|
+ t->setisMoveable(false);
|
|
|
|
+ Q.push(t);
|
|
|
|
+ return;
|
|
|
|
+ }
|
|
|
|
+ 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);
|
|
Now->setisMoveable(true);
|
|
- if (getCharacter() != NULL && getCharacter()->canAttack(Now->getDistance())) {
|
|
|
|
|
|
+ if (getCharacter() != NULL && getCharacter()->СanAttack(Now->getDistance())) {
|
|
Now->setisAttackable(true);
|
|
Now->setisAttackable(true);
|
|
}
|
|
}
|
|
else {
|
|
else {
|
|
Now->setisAttackable(false);
|
|
Now->setisAttackable(false);
|
|
}
|
|
}
|
|
q.pop();
|
|
q.pop();
|
|
- auto f = [&q, &Q](Cell * t, Cell * parent) {
|
|
|
|
- if (t && !t->AddedToQuery_) {
|
|
|
|
- t->AddedToQuery_ = true;
|
|
|
|
- if (t->getCharacter() != NULL) {
|
|
|
|
- t->setisMoveable(false);
|
|
|
|
- Q.push(t);
|
|
|
|
- return;
|
|
|
|
- }
|
|
|
|
- q.push(t);
|
|
|
|
- t->setDistance(parent->getDistance() + 1);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
- f(Now->getUpLeft(), Now);
|
|
|
|
- f(Now->getUp(), Now);
|
|
|
|
- f(Now->getUpRight(), Now);
|
|
|
|
- f(Now->getDownLeft(), Now);
|
|
|
|
- f(Now->getDown(), Now);
|
|
|
|
- f(Now->getDownRight(), 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::handleAllUnmoveableCells_(std::queue<Cell*> & Q) {
|
|
void Cell::handleAllUnmoveableCells_(std::queue<Cell*> & Q) {
|
|
|
|
+ auto f = [&Q](Cell * t, Cell * parent) {
|
|
|
|
+ if (t && !t->AddedToQuery_) {
|
|
|
|
+ t->AddedToQuery_ = true;
|
|
|
|
+ Q.push(t);
|
|
|
|
+ }
|
|
|
|
+ };
|
|
while (!Q.empty()) {
|
|
while (!Q.empty()) {
|
|
Cell * Now = Q.front();
|
|
Cell * Now = Q.front();
|
|
Now->setisMoveable(false);
|
|
Now->setisMoveable(false);
|
|
Now->setisAttackable(false);
|
|
Now->setisAttackable(false);
|
|
Q.pop();
|
|
Q.pop();
|
|
- auto f = [&Q](Cell * t, Cell * parent) {
|
|
|
|
- if (t && !t->AddedToQuery_) {
|
|
|
|
- t->AddedToQuery_ = true;
|
|
|
|
- Q.push(t);
|
|
|
|
- }
|
|
|
|
- };
|
|
|
|
- f(Now->getUpLeft(), Now);
|
|
|
|
- f(Now->getUp(), Now);
|
|
|
|
- f(Now->getUpRight(), Now);
|
|
|
|
- f(Now->getDownLeft(), Now);
|
|
|
|
- f(Now->getDown(), Now);
|
|
|
|
- f(Now->getDownRight(), 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);
|
|
}
|
|
}
|
|
|
|
+
|
|
std::vector <Cell*> Cell::actualPath(Cell* to) {//std::vector<Cell*> âêëþ÷àåòñÿ â ñåáÿ è this, è end
|
|
std::vector <Cell*> Cell::actualPath(Cell* to) {//std::vector<Cell*> âêëþ÷àåòñÿ â ñåáÿ è this, è end
|
|
if (!to || !to->getisMoveable())return std::vector<Cell*>();
|
|
if (!to || !to->getisMoveable())return std::vector<Cell*>();
|
|
auto ret = std::vector<Cell*>(1, to);
|
|
auto ret = std::vector<Cell*>(1, to);
|
|
@@ -173,12 +196,12 @@ std::vector <Cell*> Cell::actualPath(Cell* to) {//std::vector<Cell*> âêëþ÷
|
|
parent = TestParent;
|
|
parent = TestParent;
|
|
}
|
|
}
|
|
};
|
|
};
|
|
- f(to->getUpLeft(), to);
|
|
|
|
- f(to->getUp(), to);
|
|
|
|
- f(to->getUpRight(), to);
|
|
|
|
- f(to->getDownLeft(), to);
|
|
|
|
- f(to->getDown(), to);
|
|
|
|
- f(to->getDownRight(), 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);
|
|
}
|
|
}
|