Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed gametype from Deathmatch to Wagnis in Wagnis.oxw + some wip in Wagnis.cc

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