Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Troop count is visible in the HUD

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