Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed gametype from Deathmatch to Wagnis in Wagnis.oxw + some wip in Wagnis.cc

File size: 7.1 KB
RevLine 
[12069]1
2
3
4
5
6
7#include "WagnisPlayer.h"
8#include <vector>
9#include <string>
10
11namespace orxonox
12{
13    RegisterClass(WagnisPlayer);
14
15    //Constructor
[12109]16    WagnisPlayer::WagnisPlayer(Context* context) : HumanPlayer(context){
[12069]17        RegisterObject(WagnisPlayer);
[12109]18        this->gameBoard = nullptr;
[12114]19        this->is_active = false;
20        this->origin_province = nullptr;
21        this->target_province = nullptr;
22        this->province_selection_changed = false;
[12069]23    }
24    //Destructor
25    WagnisPlayer::~WagnisPlayer(){
26
27    }
[12114]28    //Tick
29    void WagnisPlayer::tick(float dt){
30        SUPER(WagnisPlayer, tick, dt);
31
[12130]32        ///////////TEST//////////////
33        /**
34        if(this->is_active){
35            (master ->* ((orxonox::WagnisPlayer*)this)->orxonox::WagnisPlayer::finishedStageCallback) (this);
36        }
37
38        **/
39        ///////////TEST//////////////
40
41
42
[12127]43        if(this->is_active)
44        {
[12114]45            for(WagnisProvince* prov:this->gameBoard->provs){
46                if(prov->getHealth() < prov->getMaxHealth()){
47                    if(prov->getHealth() <= prov->getMaxHealth()-1000.0f){
48                        this->target_province = prov;
49                        this->province_selection_changed = true;
50                    }else{
51                        this->origin_province = prov;
52                        this->province_selection_changed = true;
53                    }
54                }
55            }
56
[12127]57            if((this->province_selection_changed && this->origin_province != nullptr && this->target_province != nullptr)
[12130]58            ||(this->province_selection_changed && this->target_province != nullptr && ((gameStage == CHOOSE_PROVINCE_STAGE)||(gameStage == REINFORCEMENT_STAGE)))){
[12119]59               
[12114]60                this->province_selection_changed = false;
[12130]61                switch(gameStage){
[12114]62                    case CHOOSE_PROVINCE_STAGE: 
63                    {   
[12127]64                        if (checkMove(SET_TROOPS_INITIAL))
65                            this->target_province->owner_ID = this->Player_ID;
66                       
[12114]67                        break;
68                    }
[12127]69                   
[12114]70                    case REINFORCEMENT_STAGE:
71                    {
[12127]72                        if (checkMove(SET_TROOPS))
73                            this->target_province->troops += 1;
74                       
[12114]75                        break;
76                    }
77                    case ATTACK_STAGE:{
[12127]78
79                        if (checkMove(ATTACK))
80                        {
81                            while ((this->origin_province->troops > 1) && (this->target_province->troops > 0)) //still troops available
82                            {
83                                while ((this->origin_province->troops >= 4) && (this->target_province->troops >= 2))
84                                {
85                                    //normal fight, 3 attackers, 2 defenders
86                                }
87
88                                if ((this->origin_province->troops == 3) && (this->target_province->troops >= 2))
89                                {
90                                    //2 attackers, 2 defenders
91                                }
92
93                                if((this->origin_province->troops == 2) && (this->target_province->troops >= 2))
94                                {
95                                    //1 attacker, 2 defenders
96                                }
97
98                                //TODO: implement other cases
99                            }
100
101                            if (this->target_province->troops == 0) //attacker won
102                            {
103                                this->target_province->owner_ID = this->Player_ID;
104                                this->target_province->troops = (this->origin_province->troops - 1);
105                                this->origin_province->troops = 1;
106                            }
107                        }
108                       
[12114]109                        break;
110                    }
111                    case MOVE_STAGE:{
[12127]112
113                        if (checkMove(MOVE))
114                        {
115                            this->target_province->troops += ((this->origin_province->troops) - 1);
116                            this->origin_province->troops = 1;
117                        }
[12114]118                        break;
119                    }
[12130]120
121                    default: break;
[12114]122                }
123            }
124        }
125    }
126
127
128
[12069]129    //Manages a Players turn
[12100]130    void WagnisPlayer::playerTurn(){
[12069]131       
132    }
[12100]133    //checks if a move is valid, possible MoveTypes: ATTACK, MOVE, SET_TROOPS, SET_TROOPS_INITIAL
[12127]134    bool WagnisPlayer::checkMove(MoveType move_type)
[12100]135    {
[12109]136        if (move_type == ATTACK)
[12100]137        {
[12127]138            if (isNeighbour(this->origin_province, this->target_province))//provinces neighbours
[12100]139            {
[12114]140                if (this->origin_province->getOwner_ID() == this->Player_ID) //origin belongs to player
[12100]141                {
[12114]142                    if (this->target_province->getOwner_ID() != this->Player_ID)//target belongs to enemy
[12100]143                        return true;
144                }
145            }
146        }
147
[12109]148        if (move_type == MOVE)
[12100]149        {
[12127]150            if (existPath(this->origin_province, this->target_province))//path exists, all belong to same player
[12100]151            {
[12114]152                if (this->origin_province->getOwner_ID() == this->Player_ID)//origin belongs to player
[12100]153                    return true;
154            }
155
156        }
157
[12109]158        if (move_type == SET_TROOPS)
[12100]159        {
[12114]160            if (this->target_province->getOwner_ID() == this->Player_ID)//target belongs to player
[12100]161                return true;
162        }
163
[12109]164        if (move_type == SET_TROOPS_INITIAL)
[12100]165        {
[12127]166            if (this->target_province->getOwner_ID() == -1)//target belongs to nobody
[12100]167                return true;
168        }
169       
[12080]170        return false;
171    }
[12100]172   
[12069]173    //
[12100]174    void WagnisPlayer::setTroops(WagnisProvince*){
[12069]175
176    }
[12100]177    void WagnisPlayer::attack(WagnisProvince*,WagnisProvince*){
[12069]178
179    }
[12100]180    void WagnisPlayer::moveTroops(WagnisProvince*,WagnisProvince*){
[12069]181
182    }
183    //Return a "Player x" String
184    std::string WagnisPlayer::toString(){
185        std::string str = "Player ";
[12109]186        str.append(std::to_string(Player_ID));
[12069]187        return str;
188    }
[12103]189
190    //private function for CheckMove
191    //checks if provinces are neighbours for move
[12127]192    bool WagnisPlayer::isNeighbour(WagnisProvince* origin, WagnisProvince* target)
[12103]193    {
[12127]194        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
[12103]195        {
[12127]196            if (target == origin->neighbors[i])
[12103]197                return true;
198        }
199
200        return false;
201    }
202
203    //private function for CheckMove
204    //checks if path is complete with provinces owned by player
[12127]205    bool WagnisPlayer::existPath(WagnisProvince* origin, WagnisProvince* target)
[12103]206    {
[12127]207        if (origin->getOwner_ID() == target->getOwner_ID() && isNeighbour(origin, target))
[12105]208            return true;
209       
[12127]210        for (unsigned int i = 0; i < origin->neighbors.size(); ++i)
[12105]211        {
[12127]212            if (origin->getOwner_ID() == origin->neighbors[i]->getOwner_ID())
213                return existPath(origin->neighbors[i], target);
[12105]214        }
215           
216        return false;
[12103]217    }
[12069]218}
Note: See TracBrowser for help on using the repository browser.