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
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 = 2; //changed here
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 = this->reinforcementsCounter(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            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Attack."<<endl;
137            break;
138        }
139        case ATTACK_STAGE:{
140            player->gameStage = MOVE_STAGE;
141            this->gameStage = MOVE_STAGE;
142            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Move."<<endl;
143            break;
144        }
145        case MOVE_STAGE:{
146            player->setActive(false);
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                }
154               
155                tc = 0;
156                for(WagnisProvince* p: this->gameBoard->provs){
157                    if(p->getOwner_ID() == this->active_player){
158                        tc++;
159                    }
160                }
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;
167            }else{
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);
173                next->reinforcements = reinforcementsCounter(next->Player_ID);
174            }
175           
176            break;
177        }
178        default:{}
179    }
180}
181
182
183
184
185//Creates and links all needed classes
186void Wagnis::createGame(){
187    orxout() << "Game creation started" << endl;
188
189    if(!findGameBoard()){
190        orxout() << "Error: GameBoard not found" << endl;
191    }
192
193    this->gameBoard->initializeNeighbors();
194    this->gameBoard->initializeContinents();
195   
196    orxout() << "Game creation finished" << endl;
197}
198
199//Finds the pointer to the gameBoard
200bool Wagnis::findGameBoard(){
201    for (WagnisGameboard* gb : ObjectList<WagnisGameboard>()){
202        this->gameBoard = gb;
203        orxout()<<"Gameboard pointer found and added"<<endl;
204        return true;
205    }
206    return false;
207}
208
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{
218            orxout()<<"Nullpointer found in provinces!!!"<<endl;
219        }
220    }
221    return n;
222}
223
224int Wagnis::reinforcementsCounter(int player){
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{
232            orxout()<<"Nullpointer found in provinces!!!"<<endl;
233        }
234    }
235
236    n = n/3;
237    if(n<3)n=3;
238
239    int i = 0;
240    bool b = true;
241
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        }
246
247        if(b) n += this->getContinentValue(i);
248        b = true; i++;
249    }
250
251    return n;
252}
253
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}
266
267
268}
269
270
271       
272   
Note: See TracBrowser for help on using the repository browser.