Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12207 was 12173, checked in by stadlero, 6 years ago

Continent color Billboards and arrows added.

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
[12124]19
[12173]20    int n = 4;
[12132]21    for(int i = 0;i < n;i++){
[12124]22        WagnisPlayer* p = new WagnisPlayer(context);
[12132]23        p->Player_ID = i+1;
[12130]24        p->master = this;
[12124]25        this->players.push_back(p);
26    }
[12114]27}
28//Destructor
29Wagnis::~Wagnis(){}
30
31
[12119]32//Runs the game
33void Wagnis::start(){
[12130]34    Deathmatch::start();
35    if(this->gameStage == NOT_READY){
36        this->createGame();
[12132]37
38        for(WagnisPlayer* ptr: this->players){
39            ptr->gameBoard = this->gameBoard;
40        }
[12130]41    }
42
[12132]43    this->gameStage = CHOOSE_PROVINCE_STAGE;
44    this->players.at(0)->gameStage = this->gameStage;
[12150]45    this->players.at(0)->setActive(true);
46    this->players.at(0)->reinforcements = 0;
[12133]47    orxout()<<"Player "<<1<<"\'s turn. Please choose province."<<endl;
[12114]48}
49
[12124]50//Tick
51void Wagnis::tick(float dt){
52    SUPER(Wagnis,tick,dt);
53}
54
[12130]55
56
57/**
58 * Callback function for Player classes. Needs to be called for the game to go on.
59 * arg: player: pointer to the player which finished his stage.
60 * (used to deactivate player after finishing)
61 * enum GameStage { NOT_READY, CHOOSE_PROVINCE_STAGE, REINFORCEMENT_STAGE, ATTACK_STAGE, MOVE_STAGE };
62 **/
[12132]63void Wagnis::playerFinishedStageCallback(WagnisPlayer* player){
[12130]64
[12160]65    player->resetProvinceSelection();
66
[12130]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);
[12172]73            if(this->unoccupiedProvinces() > 0){
[12150]74                //Still empty provinces left
75
[12173]76                if(this->active_player < (int)this->players.size()){
[12150]77                    this->active_player++;
78                }else{
79                    this->active_player = 1;
80                }
81
[12133]82                WagnisPlayer* next = this->players[this->active_player - 1];
[12132]83                next->gameStage = CHOOSE_PROVINCE_STAGE;
[12150]84                next->setActive(true);
85                next->reinforcements = 0;
[12133]86                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Please choose province."<<endl;
[12130]87            }else{
[12150]88                //no empty provinces left
[12130]89                this->active_player = 1;
[12133]90                WagnisPlayer* next = this->players[this->active_player - 1];
[12132]91                next->gameStage = REINFORCEMENT_STAGE;
92                this->gameStage = REINFORCEMENT_STAGE;
[12150]93                next->setActive(true);
94                next->reinforcements = 1;
[12133]95                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl;
[12130]96            }
97            break;
98        }
99        case REINFORCEMENT_STAGE:{
[12150]100           
101            if(this->initial_reinforcements_left > 0){
102                player->setActive(false);
[12173]103                if(this->active_player == (int)this->players.size()){
[12150]104                    //Last player finished this round of initial troops.
105                    this->initial_reinforcements_left -= 1;
106                    WagnisPlayer* next = this->players.at(0);
107                    this->active_player = 1;
108                    next->setActive(true);
109                    next->gameStage = REINFORCEMENT_STAGE;
110                    if(this->initial_reinforcements_left > 0){
111                        //Still more troops left to place and player 1 is next.
112                        next->reinforcements = 1;
113                    }else{
114                        //No more troops left to place and player 1 is next.
[12170]115                        next->reinforcements = this->reinforcementsCounter(1);
[12150]116                    }
117                }else{
118                    //Player who finished was not the last player
119                    WagnisPlayer* next = this->players.at(this->active_player);
120                    this->active_player += 1;
121                    next->setActive(true);
122                    next->gameStage = REINFORCEMENT_STAGE;
123                    next->reinforcements = 1;
124                }
125                break;
126            }
127            //Standard Reinforcement
128
[12133]129            player->gameStage = ATTACK_STAGE;
130            this->gameStage = ATTACK_STAGE;
131            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Attack."<<endl;
[12132]132            break;
[12130]133        }
134        case ATTACK_STAGE:{
[12133]135            player->gameStage = MOVE_STAGE;
136            this->gameStage = MOVE_STAGE;
137            orxout()<<"Player "<<player->Player_ID<<"\'s turn. Move."<<endl;
[12132]138            break;
[12130]139        }
140        case MOVE_STAGE:{
[12150]141            player->setActive(false);
[12163]142            int tc;
143            do{
[12173]144                if(this->active_player < (int)this->players.size()){
[12163]145                    this->active_player++;
146                }else{
147                    this->active_player = 1;
148                }
[12170]149               
150                tc = 0;
151                for(WagnisProvince* p: this->gameBoard->provs){
152                    if(p->getOwner_ID() == this->active_player){
153                        tc++;
154                    }
155                }
[12163]156            }while(tc == 0); //Skip players without provinces.
157
158            if(player->Player_ID == this->active_player){
159                //If all players except one got skipped, we got a winner
160                this->gameStage = WINNER_STAGE;
161                player->gameStage = WINNER_STAGE;
[12133]162            }else{
[12163]163                WagnisPlayer* next = this->players[this->active_player - 1];
164                orxout()<<"Player "<<next->Player_ID<<"\'s turn. Reinforcement."<<endl;
165                next->gameStage = REINFORCEMENT_STAGE;
166                this->gameStage = REINFORCEMENT_STAGE;
167                next->setActive(true);
[12170]168                next->reinforcements = reinforcementsCounter(next->Player_ID);
[12133]169            }
[12163]170           
[12132]171            break;
[12130]172        }
[12132]173        default:{}
[12130]174    }
175}
176
177
178
179
[12119]180//Creates and links all needed classes
[12130]181void Wagnis::createGame(){
182    orxout() << "Game creation started" << endl;
[12114]183
[12124]184    if(!findGameBoard()){
[12130]185        orxout() << "Error: GameBoard not found" << endl;
[12124]186    }
187
[12160]188    this->gameBoard->initializeNeighbors();
[12170]189    this->gameBoard->initializeContinents();
[12130]190   
191    orxout() << "Game creation finished" << endl;
[12119]192}
193
[12124]194//Finds the pointer to the gameBoard
195bool Wagnis::findGameBoard(){
196    for (WagnisGameboard* gb : ObjectList<WagnisGameboard>()){
197        this->gameBoard = gb;
[12130]198        orxout()<<"Gameboard pointer found and added"<<endl;
[12124]199        return true;
200    }
201    return false;
202}
[12119]203
[12150]204//Counts legit provinces(not including buttons)
205int Wagnis::provinceCount(){
206    int n = 0;
207    for(WagnisProvince* p: this->gameBoard->provs){
208        if(p != nullptr){
209            if(p->ID < 1000){
210                n++;
211            }
212        }else{
[12160]213            orxout()<<"Nullpointer found in provinces!!!"<<endl;
[12150]214        }
215    }
216    return n;
217}
[12119]218
[12172]219int Wagnis::unoccupiedProvinces(){
220    int n = 0;
221    for(WagnisProvince* p: this->gameBoard->provs){
222        if(p != nullptr){
223            if(p->getOwner_ID() == -1 && p->ID < 1000){
224                n++;
225            }
226        }
227    }
228    return n;
229}
230
[12170]231int Wagnis::reinforcementsCounter(int player){
[12150]232    int n = 0;
233    for(WagnisProvince* p:this->gameBoard->provs){
234        if(p != nullptr){
235            if(p->getOwner_ID() == player){
236                n++;
237            }
238        }else{
[12160]239            orxout()<<"Nullpointer found in provinces!!!"<<endl;
[12150]240        }
241    }
[12119]242
[12170]243    n = n/3;
244    if(n<3)n=3;
[12119]245
[12170]246    int i = 0;
247    bool b = true;
[12119]248
[12170]249    for(std::vector<WagnisProvince*>* cont_vec:this->gameBoard->continents){
250        for(WagnisProvince* p: *(cont_vec)){
251            if(p->getOwner_ID() != player) b = false;
252        }
[12119]253
[12170]254        if(b) n += this->getContinentValue(i);
255        b = true; i++;
256    }
[12119]257
[12170]258    return n;
259}
[12119]260
[12170]261int Wagnis::getContinentValue(int cont){
262    switch(cont){
263        case 1: return 7;
264        case 2: return 5;
265        case 3: return 5;
266        case 4: return 3;
267        case 5: return 2;
268        case 6: return 2;
269        default: return 0;
270    }
271    return 0;   
272}
[12119]273
274
275}
276
277
[12068]278       
279   
Note: See TracBrowser for help on using the repository browser.