1 | #include "WagnisGameboard.h" |
---|
2 | #include "core/CoreIncludes.h" |
---|
3 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
4 | #include <vector> |
---|
5 | #include <string> |
---|
6 | |
---|
7 | |
---|
8 | namespace 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 | orxout() << str<<endl; |
---|
67 | unsigned int n = 0; |
---|
68 | while(n < str.size()){ |
---|
69 | int tmp = parse_int(str,n); |
---|
70 | n = tmp & 0x0000FFFF; |
---|
71 | int origin_ID = tmp >> 16; |
---|
72 | if(n == str.size() || str[n] != '='){ |
---|
73 | orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl; |
---|
74 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
75 | } |
---|
76 | int other_ID; |
---|
77 | do{ |
---|
78 | n++; |
---|
79 | tmp = parse_int(str,n); |
---|
80 | n = tmp & 0x0000FFFF; |
---|
81 | other_ID = tmp >> 16; |
---|
82 | |
---|
83 | for(WagnisProvince* orig:this->provs){ |
---|
84 | if(orig->getID() == origin_ID){ |
---|
85 | for(WagnisProvince* other:this->provs){ |
---|
86 | if(other->getID() == other_ID){ |
---|
87 | orig->addNeighbor(other); |
---|
88 | //orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl; |
---|
89 | break; |
---|
90 | } |
---|
91 | } |
---|
92 | } |
---|
93 | } |
---|
94 | }while((n < str.size()) && (str[n] == '+')); |
---|
95 | |
---|
96 | if(n == str.size()) return; |
---|
97 | while((n < str.size()) && (str[n] == ' ')) n++; |
---|
98 | if(n == str.size()) return; |
---|
99 | if(str[n] != ','){ |
---|
100 | orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl; |
---|
101 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
102 | } |
---|
103 | n++; |
---|
104 | while(n < str.size() && str[n] == ' ') n++; |
---|
105 | orxout()<<"One province finished"<<endl; |
---|
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 | } |
---|