#include "WagnisPlayer.h" #include #include namespace orxonox { RegisterClass(WagnisPlayer); //Constructor WagnisPlayer::WagnisPlayer(Context* context) : HumanPlayer(context){ RegisterObject(WagnisPlayer); this->gameBoard = nullptr; this->is_active = false; this->origin_province = nullptr; this->target_province = nullptr; this->province_selection_changed = false; this->gameStage = NOT_READY; } //Destructor WagnisPlayer::~WagnisPlayer(){ } //Tick void WagnisPlayer::tick(float dt){ SUPER(WagnisPlayer, tick, dt); if(this->is_active) { for(WagnisProvince* prov:this->gameBoard->provs){ //orxout()<<"province health: "<getHealth()<getHealth() < prov->getMaxHealth()){ //Check if next-player-button was hit if(prov->getID() == 1000){ master->playerFinishedStageCallback(this); break; } //Check left/right click if(prov->getHealth() <= prov->getMaxHealth()-1000.0f){ this->target_province = prov; this->province_selection_changed = true; }else{ this->origin_province = prov; this->province_selection_changed = true; } prov->setHealth(prov->getMaxHealth()); } } if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr) ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){ this->province_selection_changed = false; switch(gameStage){ case CHOOSE_PROVINCE_STAGE: { if (checkMove(SET_TROOPS_INITIAL)) this->target_province->owner_ID = this->Player_ID; break; } case REINFORCEMENT_STAGE: { if (checkMove(SET_TROOPS)) this->target_province->troops += 1; break; } case ATTACK_STAGE:{ if (checkMove(ATTACK)) { while ((this->origin_province->troops > 1) && (this->target_province->troops > 0)) //still troops available { while ((this->origin_province->troops >= 4) && (this->target_province->troops >= 2)) { //normal fight, 3 attackers, 2 defenders } if ((this->origin_province->troops == 3) && (this->target_province->troops >= 2)) { //2 attackers, 2 defenders } if((this->origin_province->troops == 2) && (this->target_province->troops >= 2)) { //1 attacker, 2 defenders } //TODO: implement other cases } if (this->target_province->troops == 0) //attacker won { this->target_province->owner_ID = this->Player_ID; this->target_province->troops = (this->origin_province->troops - 1); this->origin_province->troops = 1; } } break; } case MOVE_STAGE:{ if (checkMove(MOVE)) { this->target_province->troops += ((this->origin_province->troops) - 1); this->origin_province->troops = 1; } break; } default: break; } } } } //Manages a Players turn void WagnisPlayer::playerTurn(){ } //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL bool WagnisPlayer::checkMove(MoveType move_type) { if (move_type == ATTACK) { if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours { if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player { if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy return true; } } } if (move_type == MOVE) { if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player { if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player return true; } } if (move_type == SET_TROOPS) { if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player return true; } if (move_type == SET_TROOPS_INITIAL) { if (this->target_province->getOwner_ID() == -1)//target belongs to nobody return true; } return false; } // void WagnisPlayer::setTroops(WagnisProvince*){ } void WagnisPlayer::attack(WagnisProvince*,WagnisProvince*){ } void WagnisPlayer::moveTroops(WagnisProvince*,WagnisProvince*){ } //Return a "Player x" String std::string WagnisPlayer::toString(){ std::string str = "Player "; str.append(std::to_string(Player_ID)); return str; } //private function for CheckMove //checks if provinces are neighbours for move bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target) { for (unsigned int i = 0; i < origin->neighbors.size(); ++i) { if (target == origin->neighbors[i]) return true; } return false; } //private function for CheckMove //checks if path is complete with provinces owned by player bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target) { if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target)) return true; for (unsigned int i = 0; i < origin->neighbors.size(); ++i) { if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID()) return existPath(origin->neighbors[i], target); } return false; } }