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