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
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                        prov->setHealth(prov->getMaxHealth());
43                        break;
44                    }
45                    //Check left/right click
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                    }
53                    prov->setHealth(prov->getMaxHealth());
54                }
55            }
56
57            if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr)
58            ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){
59               
60                this->province_selection_changed = false;
61                switch(gameStage){
62                    case CHOOSE_PROVINCE_STAGE: 
63                    {   
64                        if (checkMove(SET_TROOPS_INITIAL)){
65                            this->target_province->owner_ID = this->Player_ID;
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
73                        break;
74                    }
75                   
76                    case REINFORCEMENT_STAGE:
77                    {
78                        if (checkMove(SET_TROOPS)){
79                            this->target_province->troops += 1;
80                            orxout()<<"Province "<<this->target_province->ID<<" owned by Player "<<this->target_province->owner_ID<<" troops: "<<this->target_province->troops<<endl;
81                        }
82                           
83                        break;
84                    }
85                    case ATTACK_STAGE:{
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                       
117                        break;
118                    }
119                    case MOVE_STAGE:{
120
121                        if (checkMove(MOVE))
122                        {
123                            this->target_province->troops += ((this->origin_province->troops) - 1);
124                            this->origin_province->troops = 1;
125                        }
126                        break;
127                    }
128
129                    default: break;
130                }
131            }
132        }
133    }
134
135
136
137    //Manages a Players turn
138    void WagnisPlayer::playerTurn(){
139       
140    }
141    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
142    bool WagnisPlayer::checkMove(MoveType move_type)
143    {
144        if (move_type == ATTACK)
145        {
146            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
147            {
148                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
149                {
150                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
151                        return true;
152                }
153            }
154        }
155
156        if (move_type == MOVE)
157        {
158            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
159            {
160                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
161                    return true;
162            }
163
164        }
165
166        if (move_type == SET_TROOPS)
167        {
168            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
169                return true;
170        }
171
172        if (move_type == SET_TROOPS_INITIAL)
173        {
174            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
175                return true;
176        }
177       
178        return false;
179    }
180   
181    //
182    void WagnisPlayer::setTroops(WagnisProvince*){
183
184    }
185    void WagnisPlayer::attack(WagnisProvince*,WagnisProvince*){
186
187    }
188    void WagnisPlayer::moveTroops(WagnisProvince*,WagnisProvince*){
189
190    }
191    //Return a "Player x" String
192    std::string WagnisPlayer::toString(){
193        std::string str = "Player ";
194        str.append(std::to_string(Player_ID));
195        return str;
196    }
197
198    //private function for CheckMove
199    //checks if provinces are neighbours for move
200    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
201    {
202        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
203        {
204            if (target == origin->neighbors[i])
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
213    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
214    {
215        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
216            return true;
217       
218        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
219        {
220            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
221                return existPath(origin->neighbors[i], target);
222        }
223           
224        return false;
225    }
226}
Note: See TracBrowser for help on using the repository browser.