Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wagnis_HS18/src/modules/wagnis/WagnisPlayer.cc @ 12133

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

Wagnis now progresses through stages

File size: 8.0 KB
RevLine 
[12069]1
2
3
4
5
6
7#include "WagnisPlayer.h"
8#include <vector>
9#include <string>
10
11namespace orxonox
12{
13    RegisterClass(WagnisPlayer);
14
15    //Constructor
[12109]16    WagnisPlayer::WagnisPlayer(Context* context) : HumanPlayer(context){
[12069]17        RegisterObject(WagnisPlayer);
[12109]18        this->gameBoard = nullptr;
[12114]19        this->is_active = false;
20        this->origin_province = nullptr;
21        this->target_province = nullptr;
22        this->province_selection_changed = false;
[12132]23        this->gameStage = NOT_READY;
[12069]24    }
25    //Destructor
26    WagnisPlayer::~WagnisPlayer(){
27
28    }
[12114]29    //Tick
30    void WagnisPlayer::tick(float dt){
31        SUPER(WagnisPlayer, tick, dt);
32
[12127]33        if(this->is_active)
34        {
[12132]35           
[12114]36            for(WagnisProvince* prov:this->gameBoard->provs){
[12132]37                //orxout()<<"province health: "<<prov->getHealth()<<endl;
[12114]38                if(prov->getHealth() < prov->getMaxHealth()){
[12132]39                    //Check if next-player-button was hit
40                    if(prov->getID() == 1000){
41                        master->playerFinishedStageCallback(this);
[12133]42                        prov->setHealth(prov->getMaxHealth());
[12132]43                        break;
44                    }
45                    //Check left/right click
[12114]46                    if(prov->getHealth() <= prov->getMaxHealth()-1000.0f){
47                        this->target_province = prov;
48                        this->province_selection_changed = true;
49                    }else{
50                        this->origin_province = prov;
51                        this->province_selection_changed = true;
52                    }
[12132]53                    prov->setHealth(prov->getMaxHealth());
[12114]54                }
55            }
56
[12127]57            if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr)
[12130]58            ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){
[12119]59               
[12114]60                this->province_selection_changed = false;
[12130]61                switch(gameStage){
[12114]62                    case CHOOSE_PROVINCE_STAGE: 
63                    {   
[12133]64                        if (checkMove(SET_TROOPS_INITIAL)){
[12127]65                            this->target_province->owner_ID = this->Player_ID;
[12133]66                            this->target_province->troops += 1;
67                            orxout()<<"Province "<<this->target_province->ID<<" owned by Player "<<this->target_province->owner_ID<<" troops: "<<this->target_province->troops<<endl;
68                            master->playerFinishedStageCallback(this);
69                        }else{
70                            orxout()<<"Sorry, someone already owns this provice"<<endl;
71                        }
72
[12114]73                        break;
74                    }
[12127]75                   
[12114]76                    case REINFORCEMENT_STAGE:
77                    {
[12133]78                        if (checkMove(SET_TROOPS)){
[12127]79                            this->target_province->troops += 1;
[12133]80                            orxout()<<"Province "<<this->target_province->ID<<" owned by Player "<<this->target_province->owner_ID<<" troops: "<<this->target_province->troops<<endl;
81                        }
82                           
[12114]83                        break;
84                    }
85                    case ATTACK_STAGE:{
[12127]86
87                        if (checkMove(ATTACK))
88                        {
89                            while ((this->origin_province->troops > 1) && (this->target_province->troops > 0)) //still troops available
90                            {
91                                while ((this->origin_province->troops >= 4) && (this->target_province->troops >= 2))
92                                {
93                                    //normal fight, 3 attackers, 2 defenders
94                                }
95
96                                if ((this->origin_province->troops == 3) && (this->target_province->troops >= 2))
97                                {
98                                    //2 attackers, 2 defenders
99                                }
100
101                                if((this->origin_province->troops == 2) && (this->target_province->troops >= 2))
102                                {
103                                    //1 attacker, 2 defenders
104                                }
105
106                                //TODO: implement other cases
107                            }
108
109                            if (this->target_province->troops == 0) //attacker won
110                            {
111                                this->target_province->owner_ID = this->Player_ID;
112                                this->target_province->troops = (this->origin_province->troops - 1);
113                                this->origin_province->troops = 1;
114                            }
115                        }
116                       
[12114]117                        break;
118                    }
119                    case MOVE_STAGE:{
[12127]120
121                        if (checkMove(MOVE))
122                        {
123                            this->target_province->troops += ((this->origin_province->troops) - 1);
124                            this->origin_province->troops = 1;
125                        }
[12114]126                        break;
127                    }
[12130]128
129                    default: break;
[12114]130                }
131            }
132        }
133    }
134
135
136
[12069]137    //Manages a Players turn
[12100]138    void WagnisPlayer::playerTurn(){
[12069]139       
140    }
[12100]141    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
[12127]142    bool WagnisPlayer::checkMove(MoveType move_type)
[12100]143    {
[12109]144        if (move_type == ATTACK)
[12100]145        {
[12127]146            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
[12100]147            {
[12114]148                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
[12100]149                {
[12114]150                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
[12100]151                        return true;
152                }
153            }
154        }
155
[12109]156        if (move_type == MOVE)
[12100]157        {
[12127]158            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
[12100]159            {
[12114]160                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
[12100]161                    return true;
162            }
163
164        }
165
[12109]166        if (move_type == SET_TROOPS)
[12100]167        {
[12114]168            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
[12100]169                return true;
170        }
171
[12109]172        if (move_type == SET_TROOPS_INITIAL)
[12100]173        {
[12127]174            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
[12100]175                return true;
176        }
177       
[12080]178        return false;
179    }
[12100]180   
[12069]181    //
[12100]182    void WagnisPlayer::setTroops(WagnisProvince*){
[12069]183
184    }
[12100]185    void WagnisPlayer::attack(WagnisProvince*,WagnisProvince*){
[12069]186
187    }
[12100]188    void WagnisPlayer::moveTroops(WagnisProvince*,WagnisProvince*){
[12069]189
190    }
191    //Return a "Player x" String
192    std::string WagnisPlayer::toString(){
193        std::string str = "Player ";
[12109]194        str.append(std::to_string(Player_ID));
[12069]195        return str;
196    }
[12103]197
198    //private function for CheckMove
199    //checks if provinces are neighbours for move
[12127]200    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
[12103]201    {
[12127]202        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
[12103]203        {
[12127]204            if (target == origin->neighbors[i])
[12103]205                return true;
206        }
207
208        return false;
209    }
210
211    //private function for CheckMove
212    //checks if path is complete with provinces owned by player
[12127]213    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
[12103]214    {
[12127]215        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
[12105]216            return true;
217       
[12127]218        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
[12105]219        {
[12127]220            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
221                return existPath(origin->neighbors[i], target);
[12105]222        }
223           
224        return false;
[12103]225    }
[12069]226}
Note: See TracBrowser for help on using the repository browser.