[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 | } |
---|
[12130] | 16 | WagnisGameboard::~WagnisGameboard(){} |
---|
[12050] | 17 | |
---|
| 18 | void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){ |
---|
[12067] | 19 | SUPER(WagnisGameboard, XMLPort, xmlelement, mode); |
---|
[12050] | 20 | |
---|
[12067] | 21 | XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode); |
---|
[12072] | 22 | XMLPortParam(WagnisGameboard, "connections_string", setConnections_string, getConnections_string, xmlelement, mode); |
---|
[12049] | 23 | } |
---|
[12072] | 24 | |
---|
| 25 | |
---|
| 26 | |
---|
| 27 | |
---|
| 28 | |
---|
| 29 | |
---|
| 30 | |
---|
| 31 | //XML FUNCTIONS |
---|
[12050] | 32 | //Adds a Province to the Gameboard |
---|
| 33 | void WagnisGameboard::addProvince(WagnisProvince* province){ |
---|
[12067] | 34 | orxout() << "added" << endl; |
---|
| 35 | orxout() << province->getID() << endl; |
---|
[12050] | 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 | } |
---|
[12072] | 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 |
---|
[12077] | 59 | //Syntax: 32=7-8-4 , 2=33+5+7+1+4 |
---|
[12124] | 60 | void WagnisGameboard::initializeNeighbors(){ |
---|
[12130] | 61 | |
---|
[12124] | 62 | std::string str = this->connections_string; |
---|
[12072] | 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); |
---|
[12130] | 69 | n = tmp & 0x0000FFFF; |
---|
| 70 | int origin_ID = tmp >> 16; |
---|
[12072] | 71 | if(n == str.size() || str[n] != '='){ |
---|
| 72 | orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl; |
---|
[12077] | 73 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 74 | } |
---|
| 75 | int other_ID; |
---|
| 76 | do{ |
---|
| 77 | n++; |
---|
| 78 | tmp = parse_int(str,n); |
---|
[12130] | 79 | n = tmp & 0x0000FFFF; |
---|
| 80 | other_ID = tmp >> 16; |
---|
[12072] | 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 | } |
---|
[12130] | 94 | }while((n < str.size()) && (str[n] == '+')); |
---|
[12072] | 95 | if(n == str.size()) return; |
---|
[12130] | 96 | while((n < str.size()) && (str[n] == ' ')) n++; |
---|
[12072] | 97 | if(n == str.size()) return; |
---|
| 98 | if(str[n] != ','){ |
---|
| 99 | orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl; |
---|
[12077] | 100 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 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; |
---|
[12077] | 117 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 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 | } |
---|
[12049] | 129 | } |
---|