Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wagnis_HS18/src/modules/wagnis/WagnisGameboard.cc @ 12077

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

gameboard string fixed

File size: 4.5 KB
Line 
1#include "WagnisGameboard.h"
2#include "core/CoreIncludes.h"
3#include "BulletDynamics/Dynamics/btRigidBody.h"
4#include <vector>
5#include <string>
6
7
8namespace orxonox
9{
10    RegisterClass(WagnisGameboard);
11   
12    WagnisGameboard::WagnisGameboard(Context* context) : StaticEntity(context){
13        RegisterObject(WagnisGameboard);
14    }
15
16    WagnisGameboard::~WagnisGameboard(){
17        for(WagnisProvince* prov:this->provs){
18            prov->destroy();
19        }
20    }
21    void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){
22        SUPER(WagnisGameboard, XMLPort, xmlelement, mode);
23
24        XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode);
25        XMLPortParam(WagnisGameboard, "connections_string", setConnections_string, getConnections_string, xmlelement, mode);
26    }
27
28
29
30
31
32
33
34    //XML FUNCTIONS
35    //Adds a Province to the Gameboard
36    void WagnisGameboard::addProvince(WagnisProvince* province){
37        orxout() << "added" << endl;
38        orxout() << province->getID() << endl;
39        this->provs.push_back(province);
40    }
41    //XML get province
42    WagnisProvince* WagnisGameboard::getProvince(unsigned int index) const{
43        if(this->provs.size() <= index) return nullptr;
44        return this->provs.at(index);
45    }
46    //XML set connections_string
47    void WagnisGameboard::setConnections_string(const std::string& str){
48        this->connections_string = str;
49    }
50    //XML get connections_string
51    std::string WagnisGameboard::getConnections_string() const{
52        return this -> connections_string;
53    }
54
55
56
57
58
59
60
61    //Parses the string and initializes the neigbors vector of all provinces according
62    //Syntax: 32=7-8-4  , 2=33+5+7+1+4
63    void WagnisGameboard::initializeNeighbors(std::string str){
64        orxout() << "inizializing started" << endl;
65        orxout() << "String size:" << endl;
66        orxout() << str.size() << endl;
67        unsigned int n = 0;
68        while(n < str.size()){
69            orxout() << "test1" << endl;
70            int tmp = parse_int(str,n);
71            n = tmp | 0x0000FFFF;
72            int origin_ID = tmp / (2<<16);
73            if(n == str.size() || str[n] != '='){
74                orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl;
75                orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
76            }
77            int other_ID;
78            do{
79                n++;
80                tmp = parse_int(str,n);
81                n = tmp | 0x0000FFFF;
82                other_ID = tmp / (2<<16);
83
84                for(WagnisProvince* orig:this->provs){
85                    if(orig->getID() == origin_ID){
86                        for(WagnisProvince* other:this->provs){
87                            if(other->getID() == other_ID){
88                                orig->addNeighbor(other);
89                                orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl;
90                                break;
91                            }
92                        }
93                    }
94                    break;
95                }
96            }while(n < str.size() && str[n] == '+');
97            if(n == str.size()) return;
98            while(n < str.size() && str[n] == ' ') n++;
99            if(n == str.size()) return;
100            if(str[n] != ','){
101                orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl;
102                orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
103            }
104            n++;
105            while(n < str.size() && str[n] == ' ') n++;
106        }
107    }
108
109    //Returns the parsed int and the offset encoded in an int. the upper 16bit(incl MSB) is the number
110    //and the lower 16 bits is the new n(after the last digit)
111    int WagnisGameboard::parse_int(std::string str,unsigned int n){
112        if(n >= str.size()){
113            orxout() << "Error while parsing neighbors-string: Internal error at WagnisGameboard::parse_int() "<< endl;
114        }
115        int digit = str[n] - '0';
116        int number = digit;
117        if(digit < 0 || digit > 9){
118            orxout() << "Error while parsing neighbors-string: Digit expected at position: "<< n << endl;
119            orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
120        }
121
122        n++;
123        while(n < str.size() && str[n] - '0' >= 0 && str[n] - '0' < 10){
124            digit = str[n] - '0';
125            number = 10 * number;
126            number += digit;
127            n++;
128        }
129        return (number << 16)+n;
130    }
131}
Note: See TracBrowser for help on using the repository browser.