Skip to content

Commit a74169f

Browse files
Merge pull request #5 from SharifAIChallenge/dev
Some Edit in Paths
2 parents c5f7f5f + 51bbd57 commit a74169f

File tree

3 files changed

+36
-16
lines changed

3 files changed

+36
-16
lines changed

client/src/Core/Message/Parse/TurnMessage.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -183,11 +183,18 @@ void TurnMessage::parse_units(json json_units, Game *game, bool is_dead) {
183183

184184
int path_id = json_unit["pathId"];
185185
unit_p->path_ = nullptr;
186-
for (const Path *path : game->map_.paths_)
186+
for (const Path *path : game->players_[unit_p->player_id_].getPathsFromPlayer())
187187
if (path->getId() == path_id) {
188188
unit_p->path_ = path;
189189
break;
190190
}
191+
if (unit_p->path_ == nullptr) {
192+
for (const Path *path : game->players_[game->give_friends_id(unit_p->player_id_)].getPathsFromPlayer())
193+
if (path->getId() == path_id) {
194+
unit_p->path_ = path;
195+
break;
196+
}
197+
}
191198

192199
int row = json_unit["cell"]["row"];
193200
int col = json_unit["cell"]["col"];

client/src/Model/Game.cpp

+27-15
Original file line numberDiff line numberDiff line change
@@ -230,17 +230,14 @@ std::vector<const Unit *> Game::getCellUnits(int row, int col) {
230230
}
231231

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

239236
const Path *Game::getShortestPathToCell(const Player* from_player, int row, int col) {
240237
int pathID = shortestPath2Cell[from_player->getPlayerId()][row][col];
241238
if(pathID == -1)
242239
return nullptr;
243-
return path_ptr_by_pathId(pathID);
240+
return path_from_player_by_pathId(from_player->player_id_, pathID);
244241
}
245242

246243

@@ -561,6 +558,20 @@ const Path *Game::path_ptr_by_pathId(int pathId) {
561558
assert(0);
562559
}
563560

561+
const Path *Game::path_from_player_by_pathId(int player_id, int path_id) {
562+
for (const Path *path : players_[player_id].getPathsFromPlayer())
563+
if (path->getId() == path_id)
564+
return path;
565+
for (const Path *path : players_[give_friends_id(player_id)].getPathsFromPlayer())
566+
if (path->getId() == path_id)
567+
return path;
568+
for (const Path *path: map_.getPaths())
569+
if (path->getId() == path_id)
570+
return path;
571+
572+
Logger::Get(LogLevel_ERROR) << "Game::path_from_player_by_pathId:: Wrong pathId" << std::endl;
573+
assert(0);
574+
}
564575

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

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

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

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

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

686+
//Third check if it's on a friends path
687+
for(int i = 0; i < from_player->path_to_friend->getCells().size(); i++){
688+
if(from_player->path_to_friend->getCells()[i]->getRow() == row &&
689+
from_player->path_to_friend->getCells()[i]->getCol() == col){
690+
//Find the players friend
691+
return players_[friend_id].getPathsFromPlayer()[0]->getId();
692+
}
693+
}
694+
683695
//Forth check the paths form the players friend
684696
for (const Path *path : friend_paths) {
685697
for (size_t i = 0; i < path->getCells().size(); i++) {

client/src/Model/Game.h

+1
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class Game final : public World {
151151
Unit * unit_ptr_by_Id(int unitId);
152152
const CastSpell* cast_spell_ptr_by_Id(int castSpellId);
153153
const Path* path_ptr_by_pathId(int pathId);
154+
const Path* path_from_player_by_pathId(int player_id, int pathId);
154155

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

0 commit comments

Comments
 (0)