Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wagnis_HS18/src/modules/wagnis/Wagnis.cc @ 12170

Last change on this file since 12170 was 12170, checked in by stadlero, 5 years ago

Reinforcements counter works

File size: 8.1 KB
RevLine 
[12114]1
2
3
[12068]4#include "Wagnis.h"
5
6
[12114]7
8namespace orxonox{
9
10RegisterClass(Wagnis);
11
[12119]12//Constructor
[12114]13Wagnis::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;
[12162]18    this->initial_reinforcements_left = 2; //changed here
[12150]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
30Wagnis::~Wagnis(){}
31
32
[12119]33//Runs the game
34void 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
53void 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]65void 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.
[12170]120                        next->reinforcements = this->reinforcementsCounter(1);
[12150]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;
136            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Attack."<<endl;
[12132]137            break;
[12130]138        }
139        case ATTACK_STAGE:{
[12133]140            player->gameStage = MOVE_STAGE;
141            this->gameStage = MOVE_STAGE;
142            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Move."<<endl;
[12132]143            break;
[12130]144        }
145        case MOVE_STAGE:{
[12150]146            player->setActive(false);
[12163]147            int tc;
148            do{
149                if(this->active_player < this->players.size()){
150                    this->active_player++;
151                }else{
152                    this->active_player = 1;
153                }
[12170]154               
155                tc = 0;
156                for(WagnisProvince* p: this->gameBoard->provs){
157                    if(p->getOwner_ID() == this->active_player){
158                        tc++;
159                    }
160                }
[12163]161            }while(tc == 0); //Skip players without provinces.
162
163            if(player->Player_ID == this->active_player){
164                //If all players except one got skipped, we got a winner
165                this->gameStage = WINNER_STAGE;
166                player->gameStage = WINNER_STAGE;
[12133]167            }else{
[12163]168                WagnisPlayer* next = this->players[this->active_player - 1];
169                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl;
170                next->gameStage = REINFORCEMENT_STAGE;
171                this->gameStage = REINFORCEMENT_STAGE;
172                next->setActive(true);
[12170]173                next->reinforcements = reinforcementsCounter(next->Player_ID);
[12133]174            }
[12163]175           
[12132]176            break;
[12130]177        }
[12132]178        default:{}
[12130]179    }
180}
181
182
183
184
[12119]185//Creates and links all needed classes
[12130]186void Wagnis::createGame(){
187    orxout() << "Game creation started" << endl;
[12114]188
[12124]189    if(!findGameBoard()){
[12130]190        orxout() << "Error: GameBoard not found" << endl;
[12124]191    }
192
[12160]193    this->gameBoard->initializeNeighbors();
[12170]194    this->gameBoard->initializeContinents();
[12130]195   
196    orxout() << "Game creation finished" << endl;
[12119]197}
198
[12124]199//Finds the pointer to the gameBoard
200bool Wagnis::findGameBoard(){
201    for (WagnisGameboard* gb : ObjectList<WagnisGameboard>()){
202        this->gameBoard = gb;
[12130]203        orxout()<<"Gameboard pointer found and added"<<endl;
[12124]204        return true;
205    }
206    return false;
207}
[12119]208
[12150]209//Counts legit provinces(not including buttons)
210int Wagnis::provinceCount(){
211    int n = 0;
212    for(WagnisProvince* p: this->gameBoard->provs){
213        if(p != nullptr){
214            if(p->ID < 1000){
215                n++;
216            }
217        }else{
[12160]218            orxout()<<"Nullpointer found in provinces!!!"<<endl;
[12150]219        }
220    }
221    return n;
222}
[12119]223
[12170]224int Wagnis::reinforcementsCounter(int player){
[12150]225    int n = 0;
226    for(WagnisProvince* p:this->gameBoard->provs){
227        if(p != nullptr){
228            if(p->getOwner_ID() == player){
229                n++;
230            }
231        }else{
[12160]232            orxout()<<"Nullpointer found in provinces!!!"<<endl;
[12150]233        }
234    }
[12119]235
[12170]236    n = n/3;
237    if(n<3)n=3;
[12119]238
[12170]239    int i = 0;
240    bool b = true;
[12119]241
[12170]242    for(std::vector<WagnisProvince*>* cont_vec:this->gameBoard->continents){
243        for(WagnisProvince* p: *(cont_vec)){
244            if(p->getOwner_ID() != player) b = false;
245        }
[12119]246
[12170]247        if(b) n += this->getContinentValue(i);
248        b = true; i++;
249    }
[12119]250
[12170]251    return n;
252}
[12119]253
[12170]254int Wagnis::getContinentValue(int cont){
255    switch(cont){
256        case 1: return 7;
257        case 2: return 5;
258        case 3: return 5;
259        case 4: return 3;
260        case 5: return 2;
261        case 6: return 2;
262        default: return 0;
263    }
264    return 0;   
265}
[12119]266
267
268}
269
270
[12068]271       
272   
Note: See TracBrowser for help on using the repository browser.