Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12158 was 12150, checked in by stadlero, 6 years ago

Game progression finished. still bugs.

File size: 6.8 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;
[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
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
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:{
[12150]72            player->setActive(false);
73            this->empty_provinces_left -= 1;
74            if(this->empty_provinces_left > 0){
75                //Still empty provinces left
76                orxout()<<"DEBUG: Empty provs: "<<this->empty_provinces_left<<endl;
77
78                if(this->active_player < this->players.size()){
79                    this->active_player++;
80                }else{
81                    this->active_player = 1;
82                }
83
[12133]84                WagnisPlayer* next = this->players[this->active_player - 1];
[12132]85                next->gameStage = CHOOSE_PROVINCE_STAGE;
[12150]86                next->setActive(true);
87                next->reinforcements = 0;
[12133]88                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Please choose province."<<endl;
[12130]89            }else{
[12150]90                //no empty provinces left
91                orxout()<<"DEBUG: Empty provs: "<<this->empty_provinces_left<<endl;
[12130]92                this->active_player = 1;
[12133]93                WagnisPlayer* next = this->players[this->active_player - 1];
[12132]94                next->gameStage = REINFORCEMENT_STAGE;
95                this->gameStage = REINFORCEMENT_STAGE;
[12150]96                next->setActive(true);
97                next->reinforcements = 1;
[12133]98                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl;
[12130]99            }
100            break;
101        }
102        case REINFORCEMENT_STAGE:{
[12150]103           
104            if(this->initial_reinforcements_left > 0){
105                player->setActive(false);
106                if(this->active_player == this->players.size()){
107                    //Last player finished this round of initial troops.
108                    this->initial_reinforcements_left -= 1;
109                    WagnisPlayer* next = this->players.at(0);
110                    this->active_player = 1;
111                    next->setActive(true);
112                    next->gameStage = REINFORCEMENT_STAGE;
113                    if(this->initial_reinforcements_left > 0){
114                        //Still more troops left to place and player 1 is next.
115                        next->reinforcements = 1;
116                    }else{
117                        //No more troops left to place and player 1 is next.
118                        next->reinforcements = provincesOfPlayerCounter(1);
119                    }
120                }else{
121                    //Player who finished was not the last player
122                    WagnisPlayer* next = this->players.at(this->active_player);
123                    this->active_player += 1;
124                    next->setActive(true);
125                    next->gameStage = REINFORCEMENT_STAGE;
126                    next->reinforcements = 1;
127                }
128                break;
129            }
130            //Standard Reinforcement
131
[12133]132            player->gameStage = ATTACK_STAGE;
133            this->gameStage = ATTACK_STAGE;
134            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Attack."<<endl;
[12132]135            break;
[12130]136        }
137        case ATTACK_STAGE:{
[12133]138            player->gameStage = MOVE_STAGE;
139            this->gameStage = MOVE_STAGE;
140            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Move."<<endl;
[12132]141            break;
[12130]142        }
143        case MOVE_STAGE:{
[12150]144            player->setActive(false);
[12133]145            if(this->active_player < this->players.size()){
146                this->active_player++;
147            }else{
148                this->active_player = 1;
149            }
150            WagnisPlayer* next = this->players[this->active_player - 1];
151            orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl;
152            next->gameStage = REINFORCEMENT_STAGE;
153            this->gameStage = REINFORCEMENT_STAGE;
[12150]154            next->setActive(true);
[12132]155            break;
[12130]156        }
[12132]157        default:{}
[12130]158    }
159}
160
161
162
163
[12119]164//Creates and links all needed classes
[12130]165void Wagnis::createGame(){
166    orxout() << "Game creation started" << endl;
[12114]167
[12124]168    if(!findGameBoard()){
[12130]169        orxout() << "Error: GameBoard not found" << endl;
[12124]170    }
171
[12130]172    //this->gameBoard->initializeNeighbors();
173
174    //for(WagnisPlayer* p: this->players){
175        //this->playerEntered(p);
176    //}
177   
178    orxout() << "Game creation finished" << endl;
[12119]179}
180
[12124]181//Finds the pointer to the gameBoard
182bool Wagnis::findGameBoard(){
183    for (WagnisGameboard* gb : ObjectList<WagnisGameboard>()){
184        this->gameBoard = gb;
[12130]185        orxout()<<"Gameboard pointer found and added"<<endl;
[12124]186        return true;
187    }
188    return false;
189}
[12119]190
[12150]191//Counts legit provinces(not including buttons)
192int Wagnis::provinceCount(){
193    int n = 0;
194    for(WagnisProvince* p: this->gameBoard->provs){
195        if(p != nullptr){
196            if(p->ID < 1000){
197                n++;
198            }
199        }else{
200            orxout()<<"Nullpointer found in provines!!!"<<endl;
201        }
202    }
203    return n;
204}
[12119]205
[12150]206int Wagnis::provincesOfPlayerCounter(int player){
207    int n = 0;
208    for(WagnisProvince* p:this->gameBoard->provs){
209        if(p != nullptr){
210            if(p->getOwner_ID() == player){
211                n++;
212            }
213        }else{
214            orxout()<<"Nullpointer found in provines!!!"<<endl;
215        }
216    }
217    return n;
218}
[12119]219
220
221
222
223
224
225
226
227
228
229
[12150]230
231
[12119]232}
233
234
[12068]235       
236   
Note: See TracBrowser for help on using the repository browser.