Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12136 was 12136, checked in by kunzro, 5 years ago

HUD fixed

File size: 14.9 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            for(WagnisProvince* prov:this->gameBoard->provs){
35                //orxout()<<"province health: "<<prov->getHealth()<<endl;
36                if(prov->getHealth() < prov->getMaxHealth()){
37                    //Check if next-player-button was hit
38                    if(prov->getID() == 1000){
39                        master->playerFinishedStageCallback(this);
40                        prov->setHealth(prov->getMaxHealth());
41                        break;
42                    }
43                    //Check left/right click
44                    if(prov->getHealth() <= prov->getMaxHealth()-1000.0f){
45                        this->target_province = prov;
46                        this->province_selection_changed = true;
47                    }else{
48                        this->origin_province = prov;
49                        this->province_selection_changed = true;
50                    }
51                    prov->setHealth(prov->getMaxHealth());
52                }
53            }
54
55            if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr)
56            ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){
57               
58                this->province_selection_changed = false;
59                switch(gameStage){
60                    case CHOOSE_PROVINCE_STAGE: 
61                    {   
62                        if (checkMove(SET_TROOPS_INITIAL)){
63                            this->target_province->setOwner_ID(this->Player_ID);
64                            this->target_province->setTroops(this->target_province->getTroops()+1);
65                            orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
66                            master->playerFinishedStageCallback(this);
67                        }else{
68                            orxout()<<"Sorry, someone already owns this provice"<<endl;
69                        }
70                        //TODO: Set more troops to provinces before game begins
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
223
224    //Manages a Players turn
225    void WagnisPlayer::playerTurn(){
226       
227    }
228    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
229    bool WagnisPlayer::checkMove(MoveType move_type)
230    {
231        if (move_type == ATTACK)
232        {
233            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
234            {
235                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
236                {
237                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
238                        return true;
239                }
240            }
241        }
242
243        if (move_type == MOVE)
244        {
245            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
246            {
247                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
248                    return true;
249            }
250
251        }
252
253        if (move_type == SET_TROOPS)
254        {
255            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
256                return true;
257        }
258
259        if (move_type == SET_TROOPS_INITIAL)
260        {
261            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
262                return true;
263        }
264       
265        return false;
266    }
267   
268    //
269    void WagnisPlayer::setTroops(WagnisProvince*){
270
271    }
272    void WagnisPlayer::attack(WagnisProvince*,WagnisProvince*){
273
274    }
275    void WagnisPlayer::moveTroops(WagnisProvince*,WagnisProvince*){
276
277    }
278    //Return a "Player x" String
279    std::string WagnisPlayer::toString(){
280        std::string str = "Player ";
281        str.append(std::to_string(Player_ID));
282        return str;
283    }
284
285    //private function for CheckMove
286    //checks if provinces are neighbours for move
287    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
288    {
289        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
290        {
291            if (target == origin->neighbors[i])
292                return true;
293        }
294
295        return false;
296    }
297
298    //private function for CheckMove
299    //checks if path is complete with provinces owned by player
300    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
301    {
302        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
303            return true;
304       
305        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
306        {
307            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
308                return existPath(origin->neighbors[i], target);
309        }
310           
311        return false;
312    }
313
314    int WagnisPlayer::dice() //returns random integer in range [1, 2, 3, 4, 5, 6]
315    {
316        srand(time(NULL));
317            return (rand()%6+1);
318    }
319
320    int WagnisPlayer::best3(int a, int b, int c)
321    {
322        if(a >= b && a>= c)
323            return a;
324        if(b >= a && b>= c)
325            return b;
326        else   
327            return c;
328    }
329
330    int WagnisPlayer::best2(int a, int b)
331    {
332        if(a >= b)
333            return a;
334        else 
335            return b; 
336    }
337
338    int WagnisPlayer::second3(int a, int b, int c)
339    {
340        if((a >= b && a <= c)||(a <= b && a >= c))
341            return a;
342        if((b >= a && b <= c)||(b <= a && b >= c))
343            return b;
344        else   
345            return c;
346    }
347
348    int WagnisPlayer::second2(int a, int b)
349    {
350        if(a <= b)
351            return a;
352        else 
353            return b;   
354    }
355
356    int WagnisPlayer::reinforcementCounter() //calculates and sets reeinforcements how many troops a player gets at beginning of his turn
357    {
358       
359        reinforcements = 10;
360        orxout()<<"derp"<<endl;
361        /*
362        int counter = 0;
363
364        for (int i = 0; i <= 100; ++i)
365        {
366            if
367        }
368        */
369
370    }
371}
Note: See TracBrowser for help on using the repository browser.