Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Attack move fixed

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                            while ((this->origin_province->getTroops() > 1) && (this->target_province->getTroops() > 0)) //still troops available
98                            {
99                                while ((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() >= 2))
100                                {
101                                    //normal fight, 3 attackers, 2 defenders
102                                    int att1 = dice();
103                                    int att2 = dice();
104                                    int att3 = dice();
105                                    int def1 = dice();
106                                    int def2 = dice();
107                                   
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                                   
131                                    int attBest = best2(att1, att2);
132                                    int attSecond = second2(att1, att2);
133                                    int defBest = best2(def1, def2);
134                                    int defSecond = second2(def1, def2);
135
136                                    if(defBest >= attBest)
137                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
138                                    if (attBest > defBest)
139                                        this->target_province->setTroops(this->target_province->getTroops()-1);
140                                    if(defSecond >= attSecond)
141                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
142                                    if (attSecond > defSecond)
143                                        this->target_province->setTroops(this->target_province->getTroops()-1);
144                                }
145
146                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() >= 2))
147                                {
148                                    //1 attacker, 2 defenders
149                                    int attBest = dice();
150                                    int def1 = dice();
151                                    int def2 = dice();
152                                   
153                                    int defBest = best2(def1, def2);
154
155                                    if(defBest >= attBest)
156                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
157                                    if (attBest > defBest)
158                                        this->target_province->setTroops(this->target_province->getTroops()-1);
159                                }
160
161                                if((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() == 1))
162                                {
163                                    //3 attackers, 1 defender
164                                    int att1 = dice();
165                                    int att2 = dice();
166                                    int att3 = dice();
167                                    int defBest = dice();
168                                   
169                                    int attBest = best3(att1, att2, att3);
170
171                                    if(defBest >= attBest)
172                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
173                                    if (attBest > defBest)
174                                        this->target_province->setTroops(this->target_province->getTroops()-1);
175                                }
176
177                                if((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() == 1))
178                                {
179                                    //2 attackers, 1 defender
180                                    int att1 = dice();
181                                    int att2 = dice();
182                                    int defBest = dice();
183                                   
184                                    int attBest = best2(att1, att2);
185
186                                    if(defBest >= attBest)
187                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
188                                    if (attBest > defBest)
189                                        this->target_province->setTroops(this->target_province->getTroops()-1);
190                                }
191
192                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() == 1))
193                                {
194                                    //1 attacker, 1 defender
195                                    int attBest = dice();
196                                    int defBest = dice();
197
198                                    if(defBest >= attBest)
199                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
200                                    if (attBest > defBest)
201                                        this->target_province->setTroops(this->target_province->getTroops()-1);
202                                }
203                            }
204
205                            if (this->target_province->getTroops() == 0) //attacker won
206                            {
207                                this->target_province->setOwner_ID(this->Player_ID);
208                                this->target_province->setTroops(this->origin_province->getTroops() - 1);
209                                this->origin_province->setTroops(1);
210                            }
211                        }
212                       
213                        break;
214                    }
215                    case MOVE_STAGE:{
216
217                        if (checkMove(MOVE))
218                        {
219                            this->target_province->setTroops(this->target_province->getTroops() + this->origin_province->getTroops()-1);
220                            this->origin_province->setTroops(1);
221                            master->playerFinishedStageCallback(this);
222                        }
223                        break;
224                    }
225
226                    default: break;
227                }
228            }
229        }
230    }
231
232    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
233    bool WagnisPlayer::checkMove(MoveType move_type)
234    {
235        if (move_type == ATTACK)
236        {
237            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
238            {
239                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
240                {
241                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
242                        return true;
243                }
244            }
245        }
246
247        if (move_type == MOVE)
248        {
249            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
250            {
251                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
252                    return true;
253            }
254
255        }
256
257        if (move_type == SET_TROOPS)
258        {
259            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
260                return true;
261        }
262
263        if (move_type == SET_TROOPS_INITIAL)
264        {
265            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
266                return true;
267        }
268       
269        return false;
270    }
271
272    //Return a "Player x" String
273    std::string WagnisPlayer::toString(){
274        std::string str = "Player ";
275        str.append(std::to_string(Player_ID));
276        return str;
277    }
278
279    //private function for CheckMove
280    //checks if provinces are neighbours for move
281    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
282    {
283        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
284        {
285            if (target == origin->neighbors[i])
286                return true;
287        }
288
289        return false;
290    }
291
292    //private function for CheckMove
293    //checks if path is complete with provinces owned by player
294    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
295    {
296        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
297            return true;
298       
299        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
300        {
301            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
302                return existPath(origin->neighbors[i], target);
303        }
304           
305        return false;
306    }
307
308    int WagnisPlayer::dice() //returns random integer in range [1, 2, 3, 4, 5, 6]
309    {
310        return (rand()%6+1);
311    }
312
313    int WagnisPlayer::best3(int a, int b, int c) //returns best of 3 integers for attack stage
314    {
315        if(a >= b && a>= c)
316            return a;
317        if(b >= a && b>= c)
318            return b;
319        else   
320            return c;
321    }
322
323    int WagnisPlayer::best2(int a, int b) //returns best of 2 integers for attack stage
324    {
325        if(a >= b)
326            return a;
327        else 
328            return b; 
329    }
330
331    int WagnisPlayer::second3(int a, int b, int c) //returns second of 3 integers for attack stage
332    {
333        if((a >= b && a <= c)||(a <= b && a >= c))
334            return a;
335        if((b >= a && b <= c)||(b <= a && b >= c))
336            return b;
337        else   
338            return c;
339    }
340
341    int WagnisPlayer::second2(int a, int b) //returns second of 2 integers for attack stage
342    {
343        if(a <= b)
344            return a;
345        else 
346            return b;   
347    }
348
349    void WagnisPlayer::setActive(bool b){
350        this->is_active = b;
351        if(b == true) orxout()<<"Player "<<this->Player_ID<<"\'s turn"<<endl;
352    }
353
354    bool WagnisPlayer::isActive() const {
355        return this->is_active;
356    }
357
358    //Resets the two province pointers and dehighlights them.
359    void WagnisPlayer::resetProvinceSelection(){
360
361        if(this->origin_province != nullptr)this->origin_province->dehighlight();
362        if(this->target_province != nullptr)this->target_province->dehighlight();
363        this->origin_province = nullptr;
364        this->target_province = nullptr;
365    }
366}
Note: See TracBrowser for help on using the repository browser.