| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | #include "Wagnis.h" |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | namespace orxonox{ |
|---|
| 9 | |
|---|
| 10 | RegisterClass(Wagnis); |
|---|
| 11 | |
|---|
| 12 | //Constructor |
|---|
| 13 | Wagnis::Wagnis(Context* context) : Deathmatch(context){ |
|---|
| 14 | RegisterObject(Wagnis); |
|---|
| 15 | this->gameBoard = nullptr; |
|---|
| 16 | this->gameStage = NOT_READY; |
|---|
| 17 | this->active_player = 1; |
|---|
| 18 | this->initial_reinforcements_left = 2; //changed here |
|---|
| 19 | |
|---|
| 20 | int n = 4; |
|---|
| 21 | for(int i = 0;i < n;i++){ |
|---|
| 22 | WagnisPlayer* p = new WagnisPlayer(context); |
|---|
| 23 | p->Player_ID = i+1; |
|---|
| 24 | p->master = this; |
|---|
| 25 | this->players.push_back(p); |
|---|
| 26 | } |
|---|
| 27 | } |
|---|
| 28 | //Destructor |
|---|
| 29 | Wagnis::~Wagnis(){} |
|---|
| 30 | |
|---|
| 31 | |
|---|
| 32 | //Runs the game |
|---|
| 33 | void Wagnis::start(){ |
|---|
| 34 | Deathmatch::start(); |
|---|
| 35 | if(this->gameStage == NOT_READY){ |
|---|
| 36 | this->createGame(); |
|---|
| 37 | |
|---|
| 38 | for(WagnisPlayer* ptr: this->players){ |
|---|
| 39 | ptr->gameBoard = this->gameBoard; |
|---|
| 40 | } |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | this->gameStage = CHOOSE_PROVINCE_STAGE; |
|---|
| 44 | this->players.at(0)->gameStage = this->gameStage; |
|---|
| 45 | this->players.at(0)->setActive(true); |
|---|
| 46 | this->players.at(0)->reinforcements = 0; |
|---|
| 47 | orxout()<<"Player "<<1<<"\'s turn. Please choose province."<<endl; |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | //Tick |
|---|
| 51 | void Wagnis::tick(float dt){ |
|---|
| 52 | SUPER(Wagnis,tick,dt); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | |
|---|
| 56 | |
|---|
| 57 | /** |
|---|
| 58 | * Callback function for Player classes. Needs to be called for the game to go on. |
|---|
| 59 | * arg: player: pointer to the player which finished his stage. |
|---|
| 60 | * (used to deactivate player after finishing) |
|---|
| 61 | * enum GameStage { NOT_READY, CHOOSE_PROVINCE_STAGE, REINFORCEMENT_STAGE, ATTACK_STAGE, MOVE_STAGE }; |
|---|
| 62 | **/ |
|---|
| 63 | void Wagnis::playerFinishedStageCallback(WagnisPlayer* player){ |
|---|
| 64 | |
|---|
| 65 | player->resetProvinceSelection(); |
|---|
| 66 | |
|---|
| 67 | if(this->active_player != player->Player_ID){ |
|---|
| 68 | orxout()<<"shit happend. This player should not be activ. Wagnis::playerFinishedStage was called from wrong player"<<endl; |
|---|
| 69 | } |
|---|
| 70 | switch(this->gameStage){ |
|---|
| 71 | case CHOOSE_PROVINCE_STAGE:{ |
|---|
| 72 | player->setActive(false); |
|---|
| 73 | if(this->unoccupiedProvinces() > 0){ |
|---|
| 74 | //Still empty provinces left |
|---|
| 75 | |
|---|
| 76 | if(this->active_player < (int)this->players.size()){ |
|---|
| 77 | this->active_player++; |
|---|
| 78 | }else{ |
|---|
| 79 | this->active_player = 1; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | WagnisPlayer* next = this->players[this->active_player - 1]; |
|---|
| 83 | next->gameStage = CHOOSE_PROVINCE_STAGE; |
|---|
| 84 | next->setActive(true); |
|---|
| 85 | next->reinforcements = 0; |
|---|
| 86 | orxout()<<"Player "<<next->Player_ID<<"\'s turn. Please choose province."<<endl; |
|---|
| 87 | }else{ |
|---|
| 88 | //no empty provinces left |
|---|
| 89 | this->active_player = 1; |
|---|
| 90 | WagnisPlayer* next = this->players[this->active_player - 1]; |
|---|
| 91 | next->gameStage = REINFORCEMENT_STAGE; |
|---|
| 92 | this->gameStage = REINFORCEMENT_STAGE; |
|---|
| 93 | next->setActive(true); |
|---|
| 94 | next->reinforcements = 1; |
|---|
| 95 | orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl; |
|---|
| 96 | } |
|---|
| 97 | break; |
|---|
| 98 | } |
|---|
| 99 | case REINFORCEMENT_STAGE:{ |
|---|
| 100 | |
|---|
| 101 | if(this->initial_reinforcements_left > 0){ |
|---|
| 102 | player->setActive(false); |
|---|
| 103 | if(this->active_player == (int)this->players.size()){ |
|---|
| 104 | //Last player finished this round of initial troops. |
|---|
| 105 | this->initial_reinforcements_left -= 1; |
|---|
| 106 | WagnisPlayer* next = this->players.at(0); |
|---|
| 107 | this->active_player = 1; |
|---|
| 108 | next->setActive(true); |
|---|
| 109 | next->gameStage = REINFORCEMENT_STAGE; |
|---|
| 110 | if(this->initial_reinforcements_left > 0){ |
|---|
| 111 | //Still more troops left to place and player 1 is next. |
|---|
| 112 | next->reinforcements = 1; |
|---|
| 113 | }else{ |
|---|
| 114 | //No more troops left to place and player 1 is next. |
|---|
| 115 | next->reinforcements = this->reinforcementsCounter(1); |
|---|
| 116 | } |
|---|
| 117 | }else{ |
|---|
| 118 | //Player who finished was not the last player |
|---|
| 119 | WagnisPlayer* next = this->players.at(this->active_player); |
|---|
| 120 | this->active_player += 1; |
|---|
| 121 | next->setActive(true); |
|---|
| 122 | next->gameStage = REINFORCEMENT_STAGE; |
|---|
| 123 | next->reinforcements = 1; |
|---|
| 124 | } |
|---|
| 125 | break; |
|---|
| 126 | } |
|---|
| 127 | //Standard Reinforcement |
|---|
| 128 | |
|---|
| 129 | player->gameStage = ATTACK_STAGE; |
|---|
| 130 | this->gameStage = ATTACK_STAGE; |
|---|
| 131 | orxout()<<"Player "<<player->Player_ID<<"\'s turn. Attack."<<endl; |
|---|
| 132 | break; |
|---|
| 133 | } |
|---|
| 134 | case ATTACK_STAGE:{ |
|---|
| 135 | player->gameStage = MOVE_STAGE; |
|---|
| 136 | this->gameStage = MOVE_STAGE; |
|---|
| 137 | orxout()<<"Player "<<player->Player_ID<<"\'s turn. Move."<<endl; |
|---|
| 138 | break; |
|---|
| 139 | } |
|---|
| 140 | case MOVE_STAGE:{ |
|---|
| 141 | player->setActive(false); |
|---|
| 142 | int tc; |
|---|
| 143 | do{ |
|---|
| 144 | if(this->active_player < (int)this->players.size()){ |
|---|
| 145 | this->active_player++; |
|---|
| 146 | }else{ |
|---|
| 147 | this->active_player = 1; |
|---|
| 148 | } |
|---|
| 149 | |
|---|
| 150 | tc = 0; |
|---|
| 151 | for(WagnisProvince* p: this->gameBoard->provs){ |
|---|
| 152 | if(p->getOwner_ID() == this->active_player){ |
|---|
| 153 | tc++; |
|---|
| 154 | } |
|---|
| 155 | } |
|---|
| 156 | }while(tc == 0); //Skip players without provinces. |
|---|
| 157 | |
|---|
| 158 | if(player->Player_ID == this->active_player){ |
|---|
| 159 | //If all players except one got skipped, we got a winner |
|---|
| 160 | this->gameStage = WINNER_STAGE; |
|---|
| 161 | player->gameStage = WINNER_STAGE; |
|---|
| 162 | }else{ |
|---|
| 163 | WagnisPlayer* next = this->players[this->active_player - 1]; |
|---|
| 164 | orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl; |
|---|
| 165 | next->gameStage = REINFORCEMENT_STAGE; |
|---|
| 166 | this->gameStage = REINFORCEMENT_STAGE; |
|---|
| 167 | next->setActive(true); |
|---|
| 168 | next->reinforcements = reinforcementsCounter(next->Player_ID); |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | break; |
|---|
| 172 | } |
|---|
| 173 | default:{} |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | |
|---|
| 177 | |
|---|
| 178 | |
|---|
| 179 | |
|---|
| 180 | //Creates and links all needed classes |
|---|
| 181 | void Wagnis::createGame(){ |
|---|
| 182 | orxout() << "Game creation started" << endl; |
|---|
| 183 | |
|---|
| 184 | if(!findGameBoard()){ |
|---|
| 185 | orxout() << "Error: GameBoard not found" << endl; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | this->gameBoard->initializeNeighbors(); |
|---|
| 189 | this->gameBoard->initializeContinents(); |
|---|
| 190 | |
|---|
| 191 | orxout() << "Game creation finished" << endl; |
|---|
| 192 | } |
|---|
| 193 | |
|---|
| 194 | //Finds the pointer to the gameBoard |
|---|
| 195 | bool Wagnis::findGameBoard(){ |
|---|
| 196 | for (WagnisGameboard* gb : ObjectList<WagnisGameboard>()){ |
|---|
| 197 | this->gameBoard = gb; |
|---|
| 198 | orxout()<<"Gameboard pointer found and added"<<endl; |
|---|
| 199 | return true; |
|---|
| 200 | } |
|---|
| 201 | return false; |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | //Counts legit provinces(not including buttons) |
|---|
| 205 | int Wagnis::provinceCount(){ |
|---|
| 206 | int n = 0; |
|---|
| 207 | for(WagnisProvince* p: this->gameBoard->provs){ |
|---|
| 208 | if(p != nullptr){ |
|---|
| 209 | if(p->ID < 1000){ |
|---|
| 210 | n++; |
|---|
| 211 | } |
|---|
| 212 | }else{ |
|---|
| 213 | orxout()<<"Nullpointer found in provinces!!!"<<endl; |
|---|
| 214 | } |
|---|
| 215 | } |
|---|
| 216 | return n; |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | int Wagnis::unoccupiedProvinces(){ |
|---|
| 220 | int n = 0; |
|---|
| 221 | for(WagnisProvince* p: this->gameBoard->provs){ |
|---|
| 222 | if(p != nullptr){ |
|---|
| 223 | if(p->getOwner_ID() == -1 && p->ID < 1000){ |
|---|
| 224 | n++; |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | return n; |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | int Wagnis::reinforcementsCounter(int player){ |
|---|
| 232 | int n = 0; |
|---|
| 233 | for(WagnisProvince* p:this->gameBoard->provs){ |
|---|
| 234 | if(p != nullptr){ |
|---|
| 235 | if(p->getOwner_ID() == player){ |
|---|
| 236 | n++; |
|---|
| 237 | } |
|---|
| 238 | }else{ |
|---|
| 239 | orxout()<<"Nullpointer found in provinces!!!"<<endl; |
|---|
| 240 | } |
|---|
| 241 | } |
|---|
| 242 | |
|---|
| 243 | n = n/3; |
|---|
| 244 | if(n<3)n=3; |
|---|
| 245 | |
|---|
| 246 | int i = 0; |
|---|
| 247 | bool b = true; |
|---|
| 248 | |
|---|
| 249 | for(std::vector<WagnisProvince*>* cont_vec:this->gameBoard->continents){ |
|---|
| 250 | for(WagnisProvince* p: *(cont_vec)){ |
|---|
| 251 | if(p->getOwner_ID() != player) b = false; |
|---|
| 252 | } |
|---|
| 253 | |
|---|
| 254 | if(b) n += this->getContinentValue(i); |
|---|
| 255 | b = true; i++; |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | return n; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | int Wagnis::getContinentValue(int cont){ |
|---|
| 262 | switch(cont){ |
|---|
| 263 | case 1: return 7; |
|---|
| 264 | case 2: return 5; |
|---|
| 265 | case 3: return 5; |
|---|
| 266 | case 4: return 3; |
|---|
| 267 | case 5: return 2; |
|---|
| 268 | case 6: return 2; |
|---|
| 269 | default: return 0; |
|---|
| 270 | } |
|---|
| 271 | return 0; |
|---|
| 272 | } |
|---|
| 273 | |
|---|
| 274 | |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | |
|---|
| 278 | |
|---|
| 279 | |
|---|