[12069] | 1 | |
---|
| 2 | |
---|
| 3 | |
---|
| 4 | #include "WagnisPlayer.h" |
---|
| 5 | #include <vector> |
---|
| 6 | #include <string> |
---|
[12135] | 7 | #include <cstdlib> |
---|
| 8 | #include <ctime> |
---|
[12069] | 9 | |
---|
| 10 | namespace orxonox |
---|
| 11 | { |
---|
| 12 | RegisterClass(WagnisPlayer); |
---|
| 13 | |
---|
| 14 | //Constructor |
---|
[12109] | 15 | WagnisPlayer::WagnisPlayer(Context* context) : HumanPlayer(context){ |
---|
[12069] | 16 | RegisterObject(WagnisPlayer); |
---|
[12109] | 17 | this->gameBoard = nullptr; |
---|
[12114] | 18 | this->is_active = false; |
---|
| 19 | this->origin_province = nullptr; |
---|
| 20 | this->target_province = nullptr; |
---|
| 21 | this->province_selection_changed = false; |
---|
[12132] | 22 | this->gameStage = NOT_READY; |
---|
[12145] | 23 | this->reinforcements = 0; |
---|
[12069] | 24 | } |
---|
| 25 | //Destructor |
---|
| 26 | WagnisPlayer::~WagnisPlayer(){ |
---|
| 27 | |
---|
| 28 | } |
---|
[12114] | 29 | //Tick |
---|
| 30 | void WagnisPlayer::tick(float dt){ |
---|
| 31 | SUPER(WagnisPlayer, tick, dt); |
---|
| 32 | |
---|
[12127] | 33 | if(this->is_active) |
---|
[12136] | 34 | { |
---|
[12114] | 35 | for(WagnisProvince* prov:this->gameBoard->provs){ |
---|
[12132] | 36 | //orxout()<<"province health: "<<prov->getHealth()<<endl; |
---|
[12114] | 37 | if(prov->getHealth() < prov->getMaxHealth()){ |
---|
[12132] | 38 | //Check if next-player-button was hit |
---|
| 39 | if(prov->getID() == 1000){ |
---|
| 40 | master->playerFinishedStageCallback(this); |
---|
[12133] | 41 | prov->setHealth(prov->getMaxHealth()); |
---|
[12132] | 42 | break; |
---|
| 43 | } |
---|
| 44 | //Check left/right click |
---|
[12114] | 45 | if(prov->getHealth() <= prov->getMaxHealth()-1000.0f){ |
---|
| 46 | this->target_province = prov; |
---|
| 47 | this->province_selection_changed = true; |
---|
| 48 | }else{ |
---|
| 49 | this->origin_province = prov; |
---|
| 50 | this->province_selection_changed = true; |
---|
| 51 | } |
---|
[12132] | 52 | prov->setHealth(prov->getMaxHealth()); |
---|
[12114] | 53 | } |
---|
| 54 | } |
---|
| 55 | |
---|
[12127] | 56 | if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr) |
---|
[12130] | 57 | ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){ |
---|
[12119] | 58 | |
---|
[12114] | 59 | this->province_selection_changed = false; |
---|
[12130] | 60 | switch(gameStage){ |
---|
[12114] | 61 | case CHOOSE_PROVINCE_STAGE: |
---|
| 62 | { |
---|
[12133] | 63 | if (checkMove(SET_TROOPS_INITIAL)){ |
---|
[12135] | 64 | this->target_province->setOwner_ID(this->Player_ID); |
---|
[12134] | 65 | this->target_province->setTroops(this->target_province->getTroops()+1); |
---|
[12135] | 66 | orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl; |
---|
[12133] | 67 | master->playerFinishedStageCallback(this); |
---|
| 68 | }else{ |
---|
| 69 | orxout()<<"Sorry, someone already owns this provice"<<endl; |
---|
| 70 | } |
---|
| 71 | |
---|
[12114] | 72 | break; |
---|
| 73 | } |
---|
[12127] | 74 | |
---|
[12114] | 75 | case REINFORCEMENT_STAGE: |
---|
| 76 | { |
---|
[12136] | 77 | if ( reinforcements > 0) |
---|
[12135] | 78 | { |
---|
| 79 | if (checkMove(SET_TROOPS)) |
---|
| 80 | { |
---|
| 81 | this->target_province->setTroops(this->target_province->getTroops()+1); |
---|
| 82 | orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl; |
---|
| 83 | } |
---|
[12136] | 84 | } |
---|
[12135] | 85 | |
---|
[12114] | 86 | break; |
---|
| 87 | } |
---|
| 88 | case ATTACK_STAGE:{ |
---|
[12127] | 89 | |
---|
| 90 | if (checkMove(ATTACK)) |
---|
| 91 | { |
---|
[12134] | 92 | while ((this->origin_province->getTroops() > 1) && (this->target_province->getTroops() > 0)) //still troops available |
---|
[12127] | 93 | { |
---|
[12134] | 94 | while ((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() >= 2)) |
---|
[12127] | 95 | { |
---|
| 96 | //normal fight, 3 attackers, 2 defenders |
---|
[12135] | 97 | int att1 = dice(); |
---|
| 98 | int att2 = dice(); |
---|
| 99 | int att3 = dice(); |
---|
| 100 | int def1 = dice(); |
---|
| 101 | int def2 = dice(); |
---|
| 102 | int attBest = best3(att1, att2, att3); |
---|
| 103 | int attSecond = second3(att1, att2, att3); |
---|
| 104 | int defBest = best2(def1, def2); |
---|
| 105 | int defSecond = second2(def1, def2); |
---|
| 106 | |
---|
| 107 | if(defBest >= attBest) |
---|
| 108 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 109 | if (attBest > defBest) |
---|
| 110 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
| 111 | if(defSecond >= attSecond) |
---|
| 112 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 113 | if (attSecond > defSecond) |
---|
| 114 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
[12127] | 115 | } |
---|
| 116 | |
---|
[12134] | 117 | if ((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() >= 2)) |
---|
[12127] | 118 | { |
---|
| 119 | //2 attackers, 2 defenders |
---|
[12135] | 120 | int att1 = dice(); |
---|
| 121 | int att2 = dice(); |
---|
| 122 | int def1 = dice(); |
---|
| 123 | int def2 = dice(); |
---|
| 124 | int attBest = best2(att1, att2); |
---|
| 125 | int attSecond = second2(att1, att2); |
---|
| 126 | int defBest = best2(def1, def2); |
---|
| 127 | int defSecond = second2(def1, def2); |
---|
| 128 | |
---|
| 129 | if(defBest >= attBest) |
---|
| 130 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 131 | if (attBest > defBest) |
---|
| 132 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
| 133 | if(defSecond >= attSecond) |
---|
| 134 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 135 | if (attSecond > defSecond) |
---|
| 136 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
[12127] | 137 | } |
---|
| 138 | |
---|
[12134] | 139 | if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() >= 2)) |
---|
[12127] | 140 | { |
---|
| 141 | //1 attacker, 2 defenders |
---|
[12135] | 142 | int attBest = dice(); |
---|
| 143 | int def1 = dice(); |
---|
| 144 | int def2 = dice(); |
---|
| 145 | int defBest = best2(def1, def2); |
---|
| 146 | |
---|
| 147 | if(defBest >= attBest) |
---|
| 148 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 149 | if (attBest > defBest) |
---|
| 150 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
[12127] | 151 | } |
---|
| 152 | |
---|
[12135] | 153 | if((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() == 1)) |
---|
| 154 | { |
---|
| 155 | //3 attackers, 1 defender |
---|
| 156 | int att1 = dice(); |
---|
| 157 | int att2 = dice(); |
---|
| 158 | int att3 = dice(); |
---|
| 159 | int defBest = dice(); |
---|
| 160 | int attBest = best3(att1, att2, att3); |
---|
| 161 | |
---|
| 162 | if(defBest >= attBest) |
---|
| 163 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 164 | if (attBest > defBest) |
---|
| 165 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
| 166 | } |
---|
| 167 | |
---|
| 168 | if((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() == 1)) |
---|
| 169 | { |
---|
| 170 | //2 attackers, 1 defender |
---|
| 171 | int att1 = dice(); |
---|
| 172 | int att2 = dice(); |
---|
| 173 | int defBest = dice(); |
---|
| 174 | int attBest = best2(att1, att2); |
---|
| 175 | |
---|
| 176 | if(defBest >= attBest) |
---|
| 177 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 178 | if (attBest > defBest) |
---|
| 179 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
| 180 | } |
---|
| 181 | |
---|
| 182 | if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() == 1)) |
---|
| 183 | { |
---|
| 184 | //1 attacker, 1 defender |
---|
| 185 | int attBest = dice(); |
---|
| 186 | int defBest = dice(); |
---|
| 187 | |
---|
| 188 | if(defBest >= attBest) |
---|
| 189 | this->origin_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 190 | if (attBest > defBest) |
---|
| 191 | this->target_province->setTroops(this->target_province->getTroops()-1); |
---|
| 192 | } |
---|
[12127] | 193 | } |
---|
| 194 | |
---|
[12134] | 195 | if (this->target_province->getTroops() == 0) //attacker won |
---|
[12127] | 196 | { |
---|
[12135] | 197 | this->target_province->setOwner_ID(this->Player_ID); |
---|
[12134] | 198 | this->target_province->setTroops(this->origin_province->getTroops() - 1); |
---|
| 199 | this->origin_province->setTroops(1); |
---|
[12127] | 200 | } |
---|
| 201 | } |
---|
| 202 | |
---|
[12114] | 203 | break; |
---|
| 204 | } |
---|
| 205 | case MOVE_STAGE:{ |
---|
[12127] | 206 | |
---|
| 207 | if (checkMove(MOVE)) |
---|
| 208 | { |
---|
[12134] | 209 | this->target_province->setTroops(this->origin_province->getTroops()-1); |
---|
| 210 | this->origin_province->setTroops(1); |
---|
[12135] | 211 | master->playerFinishedStageCallback(this); |
---|
[12127] | 212 | } |
---|
[12114] | 213 | break; |
---|
| 214 | } |
---|
[12130] | 215 | |
---|
| 216 | default: break; |
---|
[12114] | 217 | } |
---|
| 218 | } |
---|
| 219 | } |
---|
| 220 | } |
---|
| 221 | |
---|
[12100] | 222 | //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL |
---|
[12127] | 223 | bool WagnisPlayer::checkMove(MoveType move_type) |
---|
[12100] | 224 | { |
---|
[12109] | 225 | if (move_type == ATTACK) |
---|
[12100] | 226 | { |
---|
[12127] | 227 | if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours |
---|
[12100] | 228 | { |
---|
[12114] | 229 | if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player |
---|
[12100] | 230 | { |
---|
[12114] | 231 | if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy |
---|
[12100] | 232 | return true; |
---|
| 233 | } |
---|
| 234 | } |
---|
| 235 | } |
---|
| 236 | |
---|
[12109] | 237 | if (move_type == MOVE) |
---|
[12100] | 238 | { |
---|
[12127] | 239 | if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player |
---|
[12100] | 240 | { |
---|
[12114] | 241 | if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player |
---|
[12100] | 242 | return true; |
---|
| 243 | } |
---|
| 244 | |
---|
| 245 | } |
---|
| 246 | |
---|
[12109] | 247 | if (move_type == SET_TROOPS) |
---|
[12100] | 248 | { |
---|
[12114] | 249 | if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player |
---|
[12100] | 250 | return true; |
---|
| 251 | } |
---|
| 252 | |
---|
[12109] | 253 | if (move_type == SET_TROOPS_INITIAL) |
---|
[12100] | 254 | { |
---|
[12127] | 255 | if (this->target_province->getOwner_ID() == -1)//target belongs to nobody |
---|
[12100] | 256 | return true; |
---|
| 257 | } |
---|
| 258 | |
---|
[12080] | 259 | return false; |
---|
| 260 | } |
---|
[12069] | 261 | |
---|
| 262 | //Return a "Player x" String |
---|
| 263 | std::string WagnisPlayer::toString(){ |
---|
| 264 | std::string str = "Player "; |
---|
[12109] | 265 | str.append(std::to_string(Player_ID)); |
---|
[12069] | 266 | return str; |
---|
| 267 | } |
---|
[12103] | 268 | |
---|
| 269 | //private function for CheckMove |
---|
| 270 | //checks if provinces are neighbours for move |
---|
[12127] | 271 | bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target) |
---|
[12103] | 272 | { |
---|
[12127] | 273 | for (unsigned int i = 0; i < origin->neighbors.size(); ++i) |
---|
[12103] | 274 | { |
---|
[12127] | 275 | if (target == origin->neighbors[i]) |
---|
[12103] | 276 | return true; |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | return false; |
---|
| 280 | } |
---|
| 281 | |
---|
| 282 | //private function for CheckMove |
---|
| 283 | //checks if path is complete with provinces owned by player |
---|
[12127] | 284 | bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target) |
---|
[12103] | 285 | { |
---|
[12127] | 286 | if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target)) |
---|
[12105] | 287 | return true; |
---|
| 288 | |
---|
[12127] | 289 | for (unsigned int i = 0; i < origin->neighbors.size(); ++i) |
---|
[12105] | 290 | { |
---|
[12127] | 291 | if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID()) |
---|
| 292 | return existPath(origin->neighbors[i], target); |
---|
[12105] | 293 | } |
---|
| 294 | |
---|
| 295 | return false; |
---|
[12103] | 296 | } |
---|
[12135] | 297 | |
---|
| 298 | int WagnisPlayer::dice() //returns random integer in range [1, 2, 3, 4, 5, 6] |
---|
| 299 | { |
---|
| 300 | srand(time(NULL)); |
---|
| 301 | return (rand()%6+1); |
---|
| 302 | } |
---|
| 303 | |
---|
[12145] | 304 | int WagnisPlayer::best3(int a, int b, int c) //returns best of 3 integers for attack stage |
---|
[12135] | 305 | { |
---|
| 306 | if(a >= b && a>= c) |
---|
| 307 | return a; |
---|
| 308 | if(b >= a && b>= c) |
---|
| 309 | return b; |
---|
| 310 | else |
---|
| 311 | return c; |
---|
| 312 | } |
---|
| 313 | |
---|
[12145] | 314 | int WagnisPlayer::best2(int a, int b) //returns best of 2 integers for attack stage |
---|
[12135] | 315 | { |
---|
| 316 | if(a >= b) |
---|
| 317 | return a; |
---|
| 318 | else |
---|
| 319 | return b; |
---|
| 320 | } |
---|
| 321 | |
---|
[12145] | 322 | int WagnisPlayer::second3(int a, int b, int c) //returns second of 3 integers for attack stage |
---|
[12135] | 323 | { |
---|
| 324 | if((a >= b && a <= c)||(a <= b && a >= c)) |
---|
| 325 | return a; |
---|
| 326 | if((b >= a && b <= c)||(b <= a && b >= c)) |
---|
| 327 | return b; |
---|
| 328 | else |
---|
| 329 | return c; |
---|
| 330 | } |
---|
| 331 | |
---|
[12145] | 332 | int WagnisPlayer::second2(int a, int b) //returns second of 2 integers for attack stage |
---|
[12135] | 333 | { |
---|
| 334 | if(a <= b) |
---|
| 335 | return a; |
---|
| 336 | else |
---|
| 337 | return b; |
---|
| 338 | } |
---|
[12069] | 339 | } |
---|