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