Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Player functionalities added

File size: 15.0 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    }
24    //Destructor
25    WagnisPlayer::~WagnisPlayer(){
26
27    }
28    //Tick
29    void WagnisPlayer::tick(float dt){
30        SUPER(WagnisPlayer, tick, dt);
31
32        if(this->is_active)
33        {
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                        //TODO: Set more troops to provinces before game begins
72
73                        break;
74                    }
75                   
76                    case REINFORCEMENT_STAGE:
77                    {
78                        int i = reinforcementCounter(); //i tells how many troops player gets
79                        while (i > 0) 
80                        {
81                            if (checkMove(SET_TROOPS))
82                            {
83                                this->target_province->setTroops(this->target_province->getTroops()+1);
84                                orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
85                                --i;
86                            }
87                        }
88
89                        master->playerFinishedStageCallback(this);   
90                       
91                        break;
92                    }
93                    case ATTACK_STAGE:{
94
95                        if (checkMove(ATTACK))
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                                    int attBest = best3(att1, att2, att3);
108                                    int attSecond = second3(att1, att2, att3);
109                                    int defBest = best2(def1, def2);
110                                    int defSecond = second2(def1, def2);
111
112                                    if(defBest >= attBest)
113                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
114                                    if (attBest > defBest)
115                                        this->target_province->setTroops(this->target_province->getTroops()-1);
116                                    if(defSecond >= attSecond)
117                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
118                                    if (attSecond > defSecond)
119                                        this->target_province->setTroops(this->target_province->getTroops()-1);
120                                }
121
122                                if ((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() >= 2))
123                                {
124                                    //2 attackers, 2 defenders
125                                    int att1 = dice();
126                                    int att2 = dice();
127                                    int def1 = dice();
128                                    int def2 = dice();
129                                    int attBest = best2(att1, att2);
130                                    int attSecond = second2(att1, att2);
131                                    int defBest = best2(def1, def2);
132                                    int defSecond = second2(def1, def2);
133
134                                    if(defBest >= attBest)
135                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
136                                    if (attBest > defBest)
137                                        this->target_province->setTroops(this->target_province->getTroops()-1);
138                                    if(defSecond >= attSecond)
139                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
140                                    if (attSecond > defSecond)
141                                        this->target_province->setTroops(this->target_province->getTroops()-1);
142                                }
143
144                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() >= 2))
145                                {
146                                    //1 attacker, 2 defenders
147                                    int attBest = dice();
148                                    int def1 = dice();
149                                    int def2 = dice();
150                                    int defBest = best2(def1, def2);
151
152                                    if(defBest >= attBest)
153                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
154                                    if (attBest > defBest)
155                                        this->target_province->setTroops(this->target_province->getTroops()-1);
156                                }
157
158                                if((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() == 1))
159                                {
160                                    //3 attackers, 1 defender
161                                    int att1 = dice();
162                                    int att2 = dice();
163                                    int att3 = dice();
164                                    int defBest = dice();
165                                    int attBest = best3(att1, att2, att3);
166
167                                    if(defBest >= attBest)
168                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
169                                    if (attBest > defBest)
170                                        this->target_province->setTroops(this->target_province->getTroops()-1);
171                                }
172
173                                if((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() == 1))
174                                {
175                                    //2 attackers, 1 defender
176                                    int att1 = dice();
177                                    int att2 = dice();
178                                    int defBest = dice();
179                                    int attBest = best2(att1, att2);
180
181                                    if(defBest >= attBest)
182                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
183                                    if (attBest > defBest)
184                                        this->target_province->setTroops(this->target_province->getTroops()-1);
185                                }
186
187                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() == 1))
188                                {
189                                    //1 attacker, 1 defender
190                                    int attBest = dice();
191                                    int defBest = dice();
192
193                                    if(defBest >= attBest)
194                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
195                                    if (attBest > defBest)
196                                        this->target_province->setTroops(this->target_province->getTroops()-1);
197                                }
198                            }
199
200                            if (this->target_province->getTroops() == 0) //attacker won
201                            {
202                                this->target_province->setOwner_ID(this->Player_ID);
203                                this->target_province->setTroops(this->origin_province->getTroops() - 1);
204                                this->origin_province->setTroops(1);
205                            }
206                        }
207                       
208                        break;
209                    }
210                    case MOVE_STAGE:{
211
212                        if (checkMove(MOVE))
213                        {
214                            this->target_province->setTroops(this->origin_province->getTroops()-1);
215                            this->origin_province->setTroops(1);
216                            master->playerFinishedStageCallback(this);
217                        }
218                        break;
219                    }
220
221                    default: break;
222                }
223            }
224        }
225    }
226
227
228
229    //Manages a Players turn
230    void WagnisPlayer::playerTurn(){
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            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
251            {
252                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
253                    return true;
254            }
255
256        }
257
258        if (move_type == SET_TROOPS)
259        {
260            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
261                return true;
262        }
263
264        if (move_type == SET_TROOPS_INITIAL)
265        {
266            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
267                return true;
268        }
269       
270        return false;
271    }
272   
273    //
274    void WagnisPlayer::setTroops(WagnisProvince*){
275
276    }
277    void WagnisPlayer::attack(WagnisProvince*,WagnisProvince*){
278
279    }
280    void WagnisPlayer::moveTroops(WagnisProvince*,WagnisProvince*){
281
282    }
283    //Return a "Player x" String
284    std::string WagnisPlayer::toString(){
285        std::string str = "Player ";
286        str.append(std::to_string(Player_ID));
287        return str;
288    }
289
290    //private function for CheckMove
291    //checks if provinces are neighbours for move
292    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
293    {
294        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
295        {
296            if (target == origin->neighbors[i])
297                return true;
298        }
299
300        return false;
301    }
302
303    //private function for CheckMove
304    //checks if path is complete with provinces owned by player
305    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
306    {
307        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
308            return true;
309       
310        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
311        {
312            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
313                return existPath(origin->neighbors[i], target);
314        }
315           
316        return false;
317    }
318
319    int WagnisPlayer::dice() //returns random integer in range [1, 2, 3, 4, 5, 6]
320    {
321        srand(time(NULL));
322            return (rand()%6+1);
323    }
324
325    int WagnisPlayer::best3(int a, int b, int c)
326    {
327        if(a >= b && a>= c)
328            return a;
329        if(b >= a && b>= c)
330            return b;
331        else   
332            return c;
333    }
334
335    int WagnisPlayer::best2(int a, int b)
336    {
337        if(a >= b)
338            return a;
339        else 
340            return b; 
341    }
342
343    int WagnisPlayer::second3(int a, int b, int c)
344    {
345        if((a >= b && a <= c)||(a <= b && a >= c))
346            return a;
347        if((b >= a && b <= c)||(b <= a && b >= c))
348            return b;
349        else   
350            return c;
351    }
352
353    int WagnisPlayer::second2(int a, int b)
354    {
355        if(a <= b)
356            return a;
357        else 
358            return b;   
359    }
360
361    int WagnisPlayer::reinforcementCounter() //calculates how many troops a player gets at beginning of his turn
362    {
363       
364        return 10;
365        /*
366        int counter = 0;
367
368        for (int i = 0; i <= 100; ++i)
369        {
370            if
371        }
372        */
373
374    }
375}
Note: See TracBrowser for help on using the repository browser.