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