Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Game progression finished. still bugs.

File size: 14.9 KB
RevLine 
[12069]1
2
3
4#include "WagnisPlayer.h"
5#include <vector>
6#include <string>
[12135]7#include <cstdlib>
8#include <ctime>
[12069]9
10namespace orxonox
11{
12    RegisterClass(WagnisPlayer);
13
14    //Constructor
[12109]15    WagnisPlayer::WagnisPlayer(Context* context) : HumanPlayer(context){
[12069]16        RegisterObject(WagnisPlayer);
[12109]17        this->gameBoard = nullptr;
[12114]18        this->is_active = false;
19        this->origin_province = nullptr;
20        this->target_province = nullptr;
21        this->province_selection_changed = false;
[12132]22        this->gameStage = NOT_READY;
[12145]23        this->reinforcements = 0;
[12069]24    }
25    //Destructor
26    WagnisPlayer::~WagnisPlayer(){
27
28    }
[12114]29    //Tick
30    void WagnisPlayer::tick(float dt){
31        SUPER(WagnisPlayer, tick, dt);
32
[12127]33        if(this->is_active)
[12136]34        {           
[12114]35            for(WagnisProvince* prov:this->gameBoard->provs){
[12132]36                //orxout()<<"province health: "<<prov->getHealth()<<endl;
[12114]37                if(prov->getHealth() < prov->getMaxHealth()){
[12132]38                    //Check if next-player-button was hit
39                    if(prov->getID() == 1000){
40                        master->playerFinishedStageCallback(this);
[12133]41                        prov->setHealth(prov->getMaxHealth());
[12132]42                        break;
43                    }
44                    //Check left/right click
[12114]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                    }
[12132]52                    prov->setHealth(prov->getMaxHealth());
[12114]53                }
54            }
55
[12127]56            if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr)
[12130]57            ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){
[12119]58               
[12114]59                this->province_selection_changed = false;
[12130]60                switch(gameStage){
[12114]61                    case CHOOSE_PROVINCE_STAGE: 
62                    {   
[12133]63                        if (checkMove(SET_TROOPS_INITIAL)){
[12135]64                            this->target_province->setOwner_ID(this->Player_ID);
[12134]65                            this->target_province->setTroops(this->target_province->getTroops()+1);
[12135]66                            orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
[12133]67                            master->playerFinishedStageCallback(this);
68                        }else{
69                            orxout()<<"Sorry, someone already owns this provice"<<endl;
70                        }
71
[12114]72                        break;
73                    }
[12127]74                   
[12114]75                    case REINFORCEMENT_STAGE:
76                    {
[12136]77                        if ( reinforcements > 0) 
[12135]78                        {
79                            if (checkMove(SET_TROOPS))
80                            {
81                                this->target_province->setTroops(this->target_province->getTroops()+1);
[12150]82                                this->reinforcements -= 1;
[12135]83                                orxout()<<"Province "<<this->target_province->getID()<<" owned by Player "<<this->target_province->getOwner_ID()<<" troops: "<<this->target_province->getTroops()<<endl;
84                            }
[12136]85                        }   
[12135]86                       
[12114]87                        break;
88                    }
89                    case ATTACK_STAGE:{
[12127]90
91                        if (checkMove(ATTACK))
[12150]92                       
[12127]93                        {
[12150]94                            orxout()<<"Attack move check returned valid"<<endl;
[12134]95                            while ((this->origin_province->getTroops() > 1) && (this->target_province->getTroops() > 0)) //still troops available
[12127]96                            {
[12134]97                                while ((this->origin_province->getTroops() >= 4) && (this->target_province->getTroops() >= 2))
[12127]98                                {
99                                    //normal fight, 3 attackers, 2 defenders
[12135]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);
[12127]118                                }
119
[12134]120                                if ((this->origin_province->getTroops() == 3) && (this->target_province->getTroops() >= 2))
[12127]121                                {
122                                    //2 attackers, 2 defenders
[12135]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);
[12127]140                                }
141
[12134]142                                if((this->origin_province->getTroops() == 2) && (this->target_province->getTroops() >= 2))
[12127]143                                {
144                                    //1 attacker, 2 defenders
[12135]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);
[12127]154                                }
155
[12135]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                                }
[12127]196                            }
197
[12134]198                            if (this->target_province->getTroops() == 0) //attacker won
[12127]199                            {
[12135]200                                this->target_province->setOwner_ID(this->Player_ID);
[12134]201                                this->target_province->setTroops(this->origin_province->getTroops() - 1);
202                                this->origin_province->setTroops(1);
[12127]203                            }
[12150]204                        }else{
205                            orxout()<<"Attack move check returned false"<<endl;
[12127]206                        }
207                       
[12114]208                        break;
209                    }
210                    case MOVE_STAGE:{
[12127]211
212                        if (checkMove(MOVE))
213                        {
[12134]214                            this->target_province->setTroops(this->origin_province->getTroops()-1);
215                            this->origin_province->setTroops(1);
[12135]216                            master->playerFinishedStageCallback(this);
[12127]217                        }
[12114]218                        break;
219                    }
[12130]220
221                    default: break;
[12114]222                }
223            }
224        }
225    }
226
[12100]227    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
[12127]228    bool WagnisPlayer::checkMove(MoveType move_type)
[12100]229    {
[12109]230        if (move_type == ATTACK)
[12100]231        {
[12127]232            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
[12100]233            {
[12114]234                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
[12100]235                {
[12114]236                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
[12100]237                        return true;
238                }
239            }
240        }
241
[12109]242        if (move_type == MOVE)
[12100]243        {
[12127]244            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
[12100]245            {
[12114]246                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
[12100]247                    return true;
248            }
249
250        }
251
[12109]252        if (move_type == SET_TROOPS)
[12100]253        {
[12114]254            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
[12100]255                return true;
256        }
257
[12109]258        if (move_type == SET_TROOPS_INITIAL)
[12100]259        {
[12127]260            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
[12100]261                return true;
262        }
263       
[12080]264        return false;
265    }
[12069]266
267    //Return a "Player x" String
268    std::string WagnisPlayer::toString(){
269        std::string str = "Player ";
[12109]270        str.append(std::to_string(Player_ID));
[12069]271        return str;
272    }
[12103]273
274    //private function for CheckMove
275    //checks if provinces are neighbours for move
[12127]276    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
[12103]277    {
[12127]278        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
[12103]279        {
[12127]280            if (target == origin->neighbors[i])
[12103]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
[12127]289    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
[12103]290    {
[12127]291        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
[12105]292            return true;
293       
[12127]294        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
[12105]295        {
[12127]296            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
297                return existPath(origin->neighbors[i], target);
[12105]298        }
299           
300        return false;
[12103]301    }
[12135]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
[12145]309    int WagnisPlayer::best3(int a, int b, int c) //returns best of 3 integers for attack stage
[12135]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
[12145]319    int WagnisPlayer::best2(int a, int b) //returns best of 2 integers for attack stage
[12135]320    {
321        if(a >= b)
322            return a;
323        else 
324            return b; 
325    }
326
[12145]327    int WagnisPlayer::second3(int a, int b, int c) //returns second of 3 integers for attack stage
[12135]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
[12145]337    int WagnisPlayer::second2(int a, int b) //returns second of 2 integers for attack stage
[12135]338    {
339        if(a <= b)
340            return a;
341        else 
342            return b;   
343    }
[12150]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    }
[12069]353}
Note: See TracBrowser for help on using the repository browser.