Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Checkmove function fixed

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