Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12150 was 12150, checked in by stadlero, 5 years ago

Game progression finished. still bugs.

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        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                                this->reinforcements -= 1;
83                                orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
84                            }
85                        }   
86                       
87                        break;
88                    }
89                    case ATTACK_STAGE:{
90
91                        if (checkMove(ATTACK))
92                       
93                        {
94                            orxout()<<"Attack move check returned valid"<<endl;
95                            while ((this->origin_province->getTroops() > 1) && (this->target_province->getTroops() > 0)) //still troops available
96                            {
97                                while ((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() >= 2))
98                                {
99                                    //normal fight, 3 attackers, 2 defenders
100                                    int att1 = dice();
101                                    int att2 = dice();
102                                    int att3 = dice();
103                                    int def1 = dice();
104                                    int def2 = dice();
105                                    int attBest = best3(att1, att2, att3);
106                                    int attSecond = second3(att1, att2, att3);
107                                    int defBest = best2(def1, def2);
108                                    int defSecond = second2(def1, def2);
109
110                                    if(defBest >= attBest)
111                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
112                                    if (attBest > defBest)
113                                        this->target_province->setTroops(this->target_province->getTroops()-1);
114                                    if(defSecond >= attSecond)
115                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
116                                    if (attSecond > defSecond)
117                                        this->target_province->setTroops(this->target_province->getTroops()-1);
118                                }
119
120                                if ((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() >= 2))
121                                {
122                                    //2 attackers, 2 defenders
123                                    int att1 = dice();
124                                    int att2 = dice();
125                                    int def1 = dice();
126                                    int def2 = dice();
127                                    int attBest = best2(att1, att2);
128                                    int attSecond = second2(att1, att2);
129                                    int defBest = best2(def1, def2);
130                                    int defSecond = second2(def1, def2);
131
132                                    if(defBest >= attBest)
133                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
134                                    if (attBest > defBest)
135                                        this->target_province->setTroops(this->target_province->getTroops()-1);
136                                    if(defSecond >= attSecond)
137                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
138                                    if (attSecond > defSecond)
139                                        this->target_province->setTroops(this->target_province->getTroops()-1);
140                                }
141
142                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() >= 2))
143                                {
144                                    //1 attacker, 2 defenders
145                                    int attBest = dice();
146                                    int def1 = dice();
147                                    int def2 = dice();
148                                    int defBest = best2(def1, def2);
149
150                                    if(defBest >= attBest)
151                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
152                                    if (attBest > defBest)
153                                        this->target_province->setTroops(this->target_province->getTroops()-1);
154                                }
155
156                                if((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() == 1))
157                                {
158                                    //3 attackers, 1 defender
159                                    int att1 = dice();
160                                    int att2 = dice();
161                                    int att3 = dice();
162                                    int defBest = dice();
163                                    int attBest = best3(att1, att2, att3);
164
165                                    if(defBest >= attBest)
166                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
167                                    if (attBest > defBest)
168                                        this->target_province->setTroops(this->target_province->getTroops()-1);
169                                }
170
171                                if((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() == 1))
172                                {
173                                    //2 attackers, 1 defender
174                                    int att1 = dice();
175                                    int att2 = dice();
176                                    int defBest = dice();
177                                    int attBest = best2(att1, att2);
178
179                                    if(defBest >= attBest)
180                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
181                                    if (attBest > defBest)
182                                        this->target_province->setTroops(this->target_province->getTroops()-1);
183                                }
184
185                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() == 1))
186                                {
187                                    //1 attacker, 1 defender
188                                    int attBest = dice();
189                                    int defBest = dice();
190
191                                    if(defBest >= attBest)
192                                        this->origin_province->setTroops(this->origin_province->getTroops()-1);
193                                    if (attBest > defBest)
194                                        this->target_province->setTroops(this->target_province->getTroops()-1);
195                                }
196                            }
197
198                            if (this->target_province->getTroops() == 0) //attacker won
199                            {
200                                this->target_province->setOwner_ID(this->Player_ID);
201                                this->target_province->setTroops(this->origin_province->getTroops() - 1);
202                                this->origin_province->setTroops(1);
203                            }
204                        }else{
205                            orxout()<<"Attack move check returned false"<<endl;
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    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
228    bool WagnisPlayer::checkMove(MoveType move_type)
229    {
230        if (move_type == ATTACK)
231        {
232            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
233            {
234                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
235                {
236                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
237                        return true;
238                }
239            }
240        }
241
242        if (move_type == MOVE)
243        {
244            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
245            {
246                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
247                    return true;
248            }
249
250        }
251
252        if (move_type == SET_TROOPS)
253        {
254            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
255                return true;
256        }
257
258        if (move_type == SET_TROOPS_INITIAL)
259        {
260            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
261                return true;
262        }
263       
264        return false;
265    }
266
267    //Return a "Player x" String
268    std::string WagnisPlayer::toString(){
269        std::string str = "Player ";
270        str.append(std::to_string(Player_ID));
271        return str;
272    }
273
274    //private function for CheckMove
275    //checks if provinces are neighbours for move
276    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
277    {
278        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
279        {
280            if (target == origin->neighbors[i])
281                return true;
282        }
283
284        return false;
285    }
286
287    //private function for CheckMove
288    //checks if path is complete with provinces owned by player
289    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
290    {
291        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
292            return true;
293       
294        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
295        {
296            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
297                return existPath(origin->neighbors[i], target);
298        }
299           
300        return false;
301    }
302
303    int WagnisPlayer::dice() //returns random integer in range [1, 2, 3, 4, 5, 6]
304    {
305        srand(time(NULL));
306            return (rand()%6+1);
307    }
308
309    int WagnisPlayer::best3(int a, int b, int c) //returns best of 3 integers for attack stage
310    {
311        if(a >= b && a>= c)
312            return a;
313        if(b >= a && b>= c)
314            return b;
315        else   
316            return c;
317    }
318
319    int WagnisPlayer::best2(int a, int b) //returns best of 2 integers for attack stage
320    {
321        if(a >= b)
322            return a;
323        else 
324            return b; 
325    }
326
327    int WagnisPlayer::second3(int a, int b, int c) //returns second of 3 integers for attack stage
328    {
329        if((a >= b && a <= c)||(a <= b && a >= c))
330            return a;
331        if((b >= a && b <= c)||(b <= a && b >= c))
332            return b;
333        else   
334            return c;
335    }
336
337    int WagnisPlayer::second2(int a, int b) //returns second of 2 integers for attack stage
338    {
339        if(a <= b)
340            return a;
341        else 
342            return b;   
343    }
344
345    void WagnisPlayer::setActive(bool b){
346        this->is_active = b;
347        if(b == true) orxout()<<"Player "<<this->Player_ID<<"\'s turn"<<endl;
348    }
349
350    bool WagnisPlayer::isActive() const {
351        return this->is_active;
352    }
353}
Note: See TracBrowser for help on using the repository browser.