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