Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

some bugfixes

File size: 15.5 KB
Line 
1
2
3
4#include "WagnisPlayer.h"
5#include <vector>
6#include <string>
7#include <cstdlib>
8#include <ctime>
9
10namespace orxonox
11{
12    RegisterClass(WagnisPlayer);
13
14    //Constructor
15    WagnisPlayer::WagnisPlayer(Context* context) : HumanPlayer(context){
16        RegisterObject(WagnisPlayer);
17        this->gameBoard = nullptr;
18        this->is_active = false;
19        this->origin_province = nullptr;
20        this->target_province = nullptr;
21        this->province_selection_changed = false;
22        this->gameStage = NOT_READY;
23        this->reinforcements = 0;
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            for(WagnisProvince* prov:this->gameBoard->provs){
36                //orxout()<<"province health: "<<prov->getHealth()<<endl;
37                if(prov->getHealth() < prov->getMaxHealth()){
38                    //Check if next-player-button was hit
39                    if(prov->getID() == 1000){
40                        master->playerFinishedStageCallback(this);
41                        prov->setHealth(prov->getMaxHealth());
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                        if(this->origin_province != nullptr) this->origin_province->dehighlight();
50                        this->origin_province = prov;
51                        this->origin_province->highlight();
52                        this->province_selection_changed = true;
53                    }
54                    prov->setHealth(prov->getMaxHealth());
55                }
56            }
57
58            if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr)
59            ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){
60               
61                this->province_selection_changed = false;
62                switch(gameStage){
63                    case CHOOSE_PROVINCE_STAGE: 
64                    {   
65                        if (checkMove(SET_TROOPS_INITIAL)){
66                            this->target_province->setOwner_ID(this->Player_ID);
67                            this->target_province->setTroops(this->target_province->getTroops()+1);
68                            orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
69                            master->playerFinishedStageCallback(this);
70                        }else{
71                            orxout()<<"Sorry, someone already owns this provice"<<endl;
72                        }
73
74                        break;
75                    }
76                   
77                    case REINFORCEMENT_STAGE:
78                    {
79                        if ( reinforcements > 0) 
80                        {
81                            if (checkMove(SET_TROOPS))
82                            {
83                                this->target_province->setTroops(this->target_province->getTroops()+1);
84                                this->reinforcements -= 1;
85                                orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
86                                if(reinforcements == 0) master->playerFinishedStageCallback(this);
87                            }
88                        }   
89                       
90                        break;
91                    }
92                    case ATTACK_STAGE:{
93
94                        if (checkMove(ATTACK))
95                       
96                        {
97                            orxout()<<"Attack move check returned valid"<<endl;
98                            while ((this->origin_province->getTroops() > 1) && (this->target_province->getTroops() > 0)) //still troops available
99                            {
100                                while ((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() >= 2))
101                                {
102                                    //normal fight, 3 attackers, 2 defenders
103                                    int att1 = dice();
104                                    int att2 = dice();
105                                    int att3 = dice();
106                                    int def1 = dice();
107                                    int def2 = dice();
108                                    int attBest = best3(att1, att2, att3);
109                                    int attSecond = second3(att1, att2, att3);
110                                    int defBest = best2(def1, def2);
111                                    int defSecond = second2(def1, def2);
112
113                                    if(defBest >= attBest)
114                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
115                                    if (attBest > defBest)
116                                        this->target_province->setTroops(this->target_province->getTroops()-1);
117                                    if(defSecond >= attSecond)
118                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
119                                    if (attSecond > defSecond)
120                                        this->target_province->setTroops(this->target_province->getTroops()-1);
121                                }
122
123                                if ((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() >= 2))
124                                {
125                                    //2 attackers, 2 defenders
126                                    int att1 = dice();
127                                    int att2 = dice();
128                                    int def1 = dice();
129                                    int def2 = dice();
130                                    int attBest = best2(att1, att2);
131                                    int attSecond = second2(att1, att2);
132                                    int defBest = best2(def1, def2);
133                                    int defSecond = second2(def1, def2);
134
135                                    if(defBest >= attBest)
136                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
137                                    if (attBest > defBest)
138                                        this->target_province->setTroops(this->target_province->getTroops()-1);
139                                    if(defSecond >= attSecond)
140                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
141                                    if (attSecond > defSecond)
142                                        this->target_province->setTroops(this->target_province->getTroops()-1);
143                                }
144
145                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() >= 2))
146                                {
147                                    //1 attacker, 2 defenders
148                                    int attBest = dice();
149                                    int def1 = dice();
150                                    int def2 = dice();
151                                    int defBest = best2(def1, def2);
152
153                                    if(defBest >= attBest)
154                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
155                                    if (attBest > defBest)
156                                        this->target_province->setTroops(this->target_province->getTroops()-1);
157                                }
158
159                                if((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() == 1))
160                                {
161                                    //3 attackers, 1 defender
162                                    int att1 = dice();
163                                    int att2 = dice();
164                                    int att3 = dice();
165                                    int defBest = dice();
166                                    int attBest = best3(att1, att2, att3);
167
168                                    if(defBest >= attBest)
169                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
170                                    if (attBest > defBest)
171                                        this->target_province->setTroops(this->target_province->getTroops()-1);
172                                }
173
174                                if((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() == 1))
175                                {
176                                    //2 attackers, 1 defender
177                                    int att1 = dice();
178                                    int att2 = dice();
179                                    int defBest = dice();
180                                    int attBest = best2(att1, att2);
181
182                                    if(defBest >= attBest)
183                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
184                                    if (attBest > defBest)
185                                        this->target_province->setTroops(this->target_province->getTroops()-1);
186                                }
187
188                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() == 1))
189                                {
190                                    //1 attacker, 1 defender
191                                    int attBest = dice();
192                                    int defBest = dice();
193
194                                    if(defBest >= attBest)
195                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
196                                    if (attBest > defBest)
197                                        this->target_province->setTroops(this->target_province->getTroops()-1);
198                                }
199                            }
200
201                            if (this->target_province->getTroops() == 0) //attacker won
202                            {
203                                this->target_province->setOwner_ID(this->Player_ID);
204                                this->target_province->setTroops(this->origin_province->getTroops() - 1);
205                                this->origin_province->setTroops(1);
206                            }
207                        }else{
208                            orxout()<<"Attack move check returned false"<<endl;
209                        }
210                       
211                        break;
212                    }
213                    case MOVE_STAGE:{
214
215                        if (checkMove(MOVE))
216                        {
217                            this->target_province->setTroops(this->origin_province->getTroops()-1);
218                            this->origin_province->setTroops(1);
219                            master->playerFinishedStageCallback(this);
220                        }
221                        break;
222                    }
223
224                    default: break;
225                }
226            }
227        }
228    }
229
230    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
231    bool WagnisPlayer::checkMove(MoveType move_type)
232    {
233        if (move_type == ATTACK)
234        {
235            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
236            {
237                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
238                {
239                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
240                        return true;
241                }
242            }
243        }
244
245        if (move_type == MOVE)
246        {
247            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
248            {
249                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
250                    return true;
251            }
252
253        }
254
255        if (move_type == SET_TROOPS)
256        {
257            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
258                return true;
259        }
260
261        if (move_type == SET_TROOPS_INITIAL)
262        {
263            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
264                return true;
265        }
266       
267        return false;
268    }
269
270    //Return a "Player x" String
271    std::string WagnisPlayer::toString(){
272        std::string str = "Player ";
273        str.append(std::to_string(Player_ID));
274        return str;
275    }
276
277    //private function for CheckMove
278    //checks if provinces are neighbours for move
279    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
280    {
281        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
282        {
283            if (target == origin->neighbors[i])
284                return true;
285        }
286
287        return false;
288    }
289
290    //private function for CheckMove
291    //checks if path is complete with provinces owned by player
292    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
293    {
294        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
295            return true;
296       
297        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
298        {
299            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
300                return existPath(origin->neighbors[i], target);
301        }
302           
303        return false;
304    }
305
306    int WagnisPlayer::dice() //returns random integer in range [1, 2, 3, 4, 5, 6]
307    {
308        srand(time(NULL));
309            return (rand()%6+1);
310    }
311
312    int WagnisPlayer::best3(int a, int b, int c) //returns best of 3 integers for attack stage
313    {
314        if(a >= b && a>= c)
315            return a;
316        if(b >= a && b>= c)
317            return b;
318        else   
319            return c;
320    }
321
322    int WagnisPlayer::best2(int a, int b) //returns best of 2 integers for attack stage
323    {
324        if(a >= b)
325            return a;
326        else 
327            return b; 
328    }
329
330    int WagnisPlayer::second3(int a, int b, int c) //returns second of 3 integers for attack stage
331    {
332        if((a >= b && a <= c)||(a <= b && a >= c))
333            return a;
334        if((b >= a && b <= c)||(b <= a && b >= c))
335            return b;
336        else   
337            return c;
338    }
339
340    int WagnisPlayer::second2(int a, int b) //returns second of 2 integers for attack stage
341    {
342        if(a <= b)
343            return a;
344        else 
345            return b;   
346    }
347
348    void WagnisPlayer::setActive(bool b){
349        this->is_active = b;
350        if(b == true) orxout()<<"Player "<<this->Player_ID<<"\'s turn"<<endl;
351    }
352
353    bool WagnisPlayer::isActive() const {
354        return this->is_active;
355    }
356
357    //Resets the two province pointers and dehighlights them.
358    void WagnisPlayer::resetProvinceSelection(){
359
360        if(this->origin_province != nullptr)this->origin_province->dehighlight();
361        if(this->target_province != nullptr)this->target_province->dehighlight();
362        this->origin_province = nullptr;
363        this->target_province = nullptr;
364    }
365}
Note: See TracBrowser for help on using the repository browser.