Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some bugfixes

File size: 6.8 KB
Line 
1
2
3
4#include "Wagnis.h"
5
6
7
8namespace orxonox{
9
10RegisterClass(Wagnis);
11
12//Constructor
13Wagnis::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 = 5;
19    this->empty_provinces_left = 0;
20
21    int n = 3;
22    for(int i = 0;i < n;i++){
23        WagnisPlayer* p = new WagnisPlayer(context);
24        p->Player_ID = i+1;
25        p->master = this;
26        this->players.push_back(p);
27    }
28}
29//Destructor
30Wagnis::~Wagnis(){}
31
32
33//Runs the game
34void Wagnis::start(){
35    Deathmatch::start();
36    if(this->gameStage == NOT_READY){
37        this->createGame();
38
39        for(WagnisPlayer* ptr: this->players){
40            ptr->gameBoard = this->gameBoard;
41        }
42    }
43
44    this->gameStage = CHOOSE_PROVINCE_STAGE;
45    this->empty_provinces_left = this->provinceCount();
46    this->players.at(0)->gameStage = this->gameStage;
47    this->players.at(0)->setActive(true);
48    this->players.at(0)->reinforcements = 0;
49    orxout()<<"Player "<<1<<"\'s turn. Please choose province."<<endl;
50}
51
52//Tick
53void Wagnis::tick(float dt){
54    SUPER(Wagnis,tick,dt);
55}
56
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 **/
65void Wagnis::playerFinishedStageCallback(WagnisPlayer* player){
66
67    player->resetProvinceSelection();
68
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:{
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
86                WagnisPlayer* next = this->players[this->active_player - 1];
87                next->gameStage = CHOOSE_PROVINCE_STAGE;
88                next->setActive(true);
89                next->reinforcements = 0;
90                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Please choose province."<<endl;
91            }else{
92                //no empty provinces left
93                orxout()<<"DEBUG: Empty provs: "<<this->empty_provinces_left<<endl;
94                this->active_player = 1;
95                WagnisPlayer* next = this->players[this->active_player - 1];
96                next->gameStage = REINFORCEMENT_STAGE;
97                this->gameStage = REINFORCEMENT_STAGE;
98                next->setActive(true);
99                next->reinforcements = 1;
100                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl;
101            }
102            break;
103        }
104        case REINFORCEMENT_STAGE:{
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
134            player->gameStage = ATTACK_STAGE;
135            this->gameStage = ATTACK_STAGE;
136            player->reinforcements = provincesOfPlayerCounter(player->Player_ID);
137            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Attack."<<endl;
138            break;
139        }
140        case ATTACK_STAGE:{
141            player->gameStage = MOVE_STAGE;
142            this->gameStage = MOVE_STAGE;
143            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Move."<<endl;
144            break;
145        }
146        case MOVE_STAGE:{
147            player->setActive(false);
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;
157            next->setActive(true);
158            break;
159        }
160        default:{}
161    }
162}
163
164
165
166
167//Creates and links all needed classes
168void Wagnis::createGame(){
169    orxout() << "Game creation started" << endl;
170
171    if(!findGameBoard()){
172        orxout() << "Error: GameBoard not found" << endl;
173    }
174
175    this->gameBoard->initializeNeighbors();
176   
177    orxout() << "Game creation finished" << endl;
178}
179
180//Finds the pointer to the gameBoard
181bool Wagnis::findGameBoard(){
182    for (WagnisGameboard* gb : ObjectList<WagnisGameboard>()){
183        this->gameBoard = gb;
184        orxout()<<"Gameboard pointer found and added"<<endl;
185        return true;
186    }
187    return false;
188}
189
190//Counts legit provinces(not including buttons)
191int 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{
199            orxout()<<"Nullpointer found in provinces!!!"<<endl;
200        }
201    }
202    return n;
203}
204
205int 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{
213            orxout()<<"Nullpointer found in provinces!!!"<<endl;
214        }
215    }
216    return n;
217}
218
219
220
221
222
223
224
225
226
227
228
229
230
231}
232
233
234       
235   
Note: See TracBrowser for help on using the repository browser.