Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Player fixed

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