Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12127 was 12127, checked in by samuelbl, 5 years ago

Player moves implemented

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