Skip to content

Commit

Permalink
Merge pull request #5 from SharifAIChallenge/dev
Browse files Browse the repository at this point in the history
Some Edit in Paths
  • Loading branch information
MostafaOjaghi authored Feb 24, 2020
2 parents c5f7f5f + 51bbd57 commit a74169f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 16 deletions.
9 changes: 8 additions & 1 deletion client/src/Core/Message/Parse/TurnMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,11 +183,18 @@ void TurnMessage::parse_units(json json_units, Game *game, bool is_dead) {

int path_id = json_unit["pathId"];
unit_p->path_ = nullptr;
for (const Path *path : game->map_.paths_)
for (const Path *path : game->players_[unit_p->player_id_].getPathsFromPlayer())
if (path->getId() == path_id) {
unit_p->path_ = path;
break;
}
if (unit_p->path_ == nullptr) {
for (const Path *path : game->players_[game->give_friends_id(unit_p->player_id_)].getPathsFromPlayer())
if (path->getId() == path_id) {
unit_p->path_ = path;
break;
}
}

int row = json_unit["cell"]["row"];
int col = json_unit["cell"]["col"];
Expand Down
42 changes: 27 additions & 15 deletions client/src/Model/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -230,17 +230,14 @@ std::vector<const Unit *> Game::getCellUnits(int row, int col) {
}

const Path *Game::getShortestPathToCell(const Player* from_player,const Cell* cell) {
int pathID = shortestPath2Cell[from_player->getPlayerId()][cell->getRow()][cell->getCol()];
if(pathID == -1)
return nullptr;
return path_ptr_by_pathId(pathID);
return getShortestPathToCell(from_player, cell->getRow(), cell->getCol());
}

const Path *Game::getShortestPathToCell(const Player* from_player, int row, int col) {
int pathID = shortestPath2Cell[from_player->getPlayerId()][row][col];
if(pathID == -1)
return nullptr;
return path_ptr_by_pathId(pathID);
return path_from_player_by_pathId(from_player->player_id_, pathID);
}


Expand Down Expand Up @@ -561,6 +558,20 @@ const Path *Game::path_ptr_by_pathId(int pathId) {
assert(0);
}

const Path *Game::path_from_player_by_pathId(int player_id, int path_id) {
for (const Path *path : players_[player_id].getPathsFromPlayer())
if (path->getId() == path_id)
return path;
for (const Path *path : players_[give_friends_id(player_id)].getPathsFromPlayer())
if (path->getId() == path_id)
return path;
for (const Path *path: map_.getPaths())
if (path->getId() == path_id)
return path;

Logger::Get(LogLevel_ERROR) << "Game::path_from_player_by_pathId:: Wrong pathId" << std::endl;
assert(0);
}

const Spell *Game::give_spell_by_typeId(int spell_id) const {
for(const Spell *_spell: this->spells_){
Expand Down Expand Up @@ -605,17 +616,9 @@ void Game::calcShortestPaths() {

int Game::calcShortestPathToCell(const Player *from_player, int row, int col) {

//First check if it's on a friends path
int friend_id = give_friends_id(from_player->player_id_);
for(int i = 0; i < from_player->path_to_friend->getCells().size(); i++){
if(from_player->path_to_friend->getCells()[i]->getRow() == row &&
from_player->path_to_friend->getCells()[i]->getCol() == col){
//Find the players friend
return players_[friend_id].getPathsFromPlayer()[0]->getId();
}
}

//Second check if it's on a enemies friends path
//First check if it's on a enemies friends path
std::vector<const Path *> player_paths = from_player->getPathsFromPlayer();
std::vector<const Path *> friend_paths = players_[friend_id].getPathsFromPlayer();
size_t min = 0x7fffffff;
Expand Down Expand Up @@ -667,7 +670,7 @@ int Game::calcShortestPathToCell(const Player *from_player, int row, int col) {
}
}

//Third check the paths form the player
//Second check the paths form the player
for (const Path *path : player_paths) {
for (size_t i = 0; i < path->getCells().size(); i++) {
if (path->getCells()[i]->getRow() == row &&
Expand All @@ -680,6 +683,15 @@ int Game::calcShortestPathToCell(const Player *from_player, int row, int col) {
}
}

//Third check if it's on a friends path
for(int i = 0; i < from_player->path_to_friend->getCells().size(); i++){
if(from_player->path_to_friend->getCells()[i]->getRow() == row &&
from_player->path_to_friend->getCells()[i]->getCol() == col){
//Find the players friend
return players_[friend_id].getPathsFromPlayer()[0]->getId();
}
}

//Forth check the paths form the players friend
for (const Path *path : friend_paths) {
for (size_t i = 0; i < path->getCells().size(); i++) {
Expand Down
1 change: 1 addition & 0 deletions client/src/Model/Game.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ class Game final : public World {
Unit * unit_ptr_by_Id(int unitId);
const CastSpell* cast_spell_ptr_by_Id(int castSpellId);
const Path* path_ptr_by_pathId(int pathId);
const Path* path_from_player_by_pathId(int player_id, int pathId);

std::vector<const CastSpell *> cast_spell_; //For us

Expand Down

0 comments on commit a74169f

Please sign in to comment.