Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Wagnis runs without crashing on startup

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