Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Wagnis runs without crashing on startup

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