[12049] | 1 | #include "WagnisGameboard.h" |
---|
| 2 | #include "core/CoreIncludes.h" |
---|
| 3 | #include "BulletDynamics/Dynamics/btRigidBody.h" |
---|
| 4 | #include <vector> |
---|
[12072] | 5 | #include <string> |
---|
[12049] | 6 | |
---|
| 7 | |
---|
| 8 | namespace orxonox |
---|
| 9 | { |
---|
| 10 | RegisterClass(WagnisGameboard); |
---|
| 11 | |
---|
| 12 | WagnisGameboard::WagnisGameboard(Context* context) : StaticEntity(context){ |
---|
| 13 | RegisterObject(WagnisGameboard); |
---|
[12130] | 14 | this->connections_string = ""; |
---|
[12049] | 15 | } |
---|
[12170] | 16 | WagnisGameboard::~WagnisGameboard(){ |
---|
| 17 | for(std::vector<WagnisProvince*>* vec: this->continents){ |
---|
| 18 | delete vec; |
---|
| 19 | } |
---|
| 20 | } |
---|
[12050] | 21 | |
---|
| 22 | void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){ |
---|
[12067] | 23 | SUPER(WagnisGameboard, XMLPort, xmlelement, mode); |
---|
[12050] | 24 | |
---|
[12067] | 25 | XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode); |
---|
[12072] | 26 | XMLPortParam(WagnisGameboard, "connections_string", setConnections_string, getConnections_string, xmlelement, mode); |
---|
[12049] | 27 | } |
---|
[12072] | 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | |
---|
| 32 | |
---|
| 33 | |
---|
| 34 | |
---|
| 35 | //XML FUNCTIONS |
---|
[12050] | 36 | //Adds a Province to the Gameboard |
---|
| 37 | void WagnisGameboard::addProvince(WagnisProvince* province){ |
---|
[12067] | 38 | orxout() << "added" << endl; |
---|
| 39 | orxout() << province->getID() << endl; |
---|
[12050] | 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 | } |
---|
[12072] | 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 |
---|
[12077] | 63 | //Syntax: 32=7-8-4 , 2=33+5+7+1+4 |
---|
[12124] | 64 | void WagnisGameboard::initializeNeighbors(){ |
---|
[12130] | 65 | |
---|
[12124] | 66 | std::string str = this->connections_string; |
---|
[12072] | 67 | orxout() << "inizializing started" << endl; |
---|
| 68 | orxout() << "String size:" << endl; |
---|
| 69 | orxout() << str.size() << endl; |
---|
[12160] | 70 | orxout() << str<<endl; |
---|
[12072] | 71 | unsigned int n = 0; |
---|
| 72 | while(n < str.size()){ |
---|
| 73 | int tmp = parse_int(str,n); |
---|
[12130] | 74 | n = tmp & 0x0000FFFF; |
---|
| 75 | int origin_ID = tmp >> 16; |
---|
[12072] | 76 | if(n == str.size() || str[n] != '='){ |
---|
| 77 | orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl; |
---|
[12077] | 78 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 79 | } |
---|
| 80 | int other_ID; |
---|
| 81 | do{ |
---|
| 82 | n++; |
---|
| 83 | tmp = parse_int(str,n); |
---|
[12130] | 84 | n = tmp & 0x0000FFFF; |
---|
| 85 | other_ID = tmp >> 16; |
---|
[12072] | 86 | |
---|
| 87 | for(WagnisProvince* orig:this->provs){ |
---|
| 88 | if(orig->getID() == origin_ID){ |
---|
| 89 | for(WagnisProvince* other:this->provs){ |
---|
| 90 | if(other->getID() == other_ID){ |
---|
| 91 | orig->addNeighbor(other); |
---|
[12160] | 92 | //orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl; |
---|
[12072] | 93 | break; |
---|
| 94 | } |
---|
| 95 | } |
---|
| 96 | } |
---|
| 97 | } |
---|
[12130] | 98 | }while((n < str.size()) && (str[n] == '+')); |
---|
[12160] | 99 | |
---|
[12072] | 100 | if(n == str.size()) return; |
---|
[12130] | 101 | while((n < str.size()) && (str[n] == ' ')) n++; |
---|
[12072] | 102 | if(n == str.size()) return; |
---|
| 103 | if(str[n] != ','){ |
---|
| 104 | orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl; |
---|
[12077] | 105 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 106 | } |
---|
| 107 | n++; |
---|
| 108 | while(n < str.size() && str[n] == ' ') n++; |
---|
[12160] | 109 | orxout()<<"One province finished"<<endl; |
---|
[12072] | 110 | } |
---|
| 111 | } |
---|
| 112 | |
---|
[12170] | 113 | void WagnisGameboard::initializeContinents(){ |
---|
| 114 | int maxconts=0; |
---|
| 115 | for(WagnisProvince* p:this->provs){ |
---|
| 116 | if(p->getContinent() > maxconts){ |
---|
| 117 | maxconts = p->getContinent(); |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | for(int i=0;i<=maxconts;i++){ |
---|
| 121 | this->continents.push_back(new std::vector<WagnisProvince*>()); |
---|
| 122 | for(WagnisProvince* p:this->provs){ |
---|
| 123 | if(p->getContinent() == i){ |
---|
| 124 | this->continents[i]->push_back(p); |
---|
| 125 | } |
---|
| 126 | } |
---|
| 127 | orxout()<<"There are "<<this->continents[i]->size()<<" Provinces in Continent "<< i << endl; |
---|
| 128 | } |
---|
| 129 | } |
---|
| 130 | |
---|
[12072] | 131 | //Returns the parsed int and the offset encoded in an int. the upper 16bit(incl MSB) is the number |
---|
| 132 | //and the lower 16 bits is the new n(after the last digit) |
---|
| 133 | int WagnisGameboard::parse_int(std::string str,unsigned int n){ |
---|
| 134 | if(n >= str.size()){ |
---|
| 135 | orxout() << "Error while parsing neighbors-string: Internal error at WagnisGameboard::parse_int() "<< endl; |
---|
| 136 | } |
---|
| 137 | int digit = str[n] - '0'; |
---|
| 138 | int number = digit; |
---|
| 139 | if(digit < 0 || digit > 9){ |
---|
| 140 | orxout() << "Error while parsing neighbors-string: Digit expected at position: "<< n << endl; |
---|
[12077] | 141 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 142 | } |
---|
| 143 | |
---|
| 144 | n++; |
---|
| 145 | while(n < str.size() && str[n] - '0' >= 0 && str[n] - '0' < 10){ |
---|
| 146 | digit = str[n] - '0'; |
---|
| 147 | number = 10 * number; |
---|
| 148 | number += digit; |
---|
| 149 | n++; |
---|
| 150 | } |
---|
| 151 | return (number << 16)+n; |
---|
| 152 | } |
---|
[12049] | 153 | } |
---|