[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; |
---|
[12160] | 66 | orxout() << str<<endl; |
---|
[12072] | 67 | unsigned int n = 0; |
---|
| 68 | while(n < str.size()){ |
---|
| 69 | int tmp = parse_int(str,n); |
---|
[12130] | 70 | n = tmp & 0x0000FFFF; |
---|
| 71 | int origin_ID = tmp >> 16; |
---|
[12072] | 72 | if(n == str.size() || str[n] != '='){ |
---|
| 73 | orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl; |
---|
[12077] | 74 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 75 | } |
---|
| 76 | int other_ID; |
---|
| 77 | do{ |
---|
| 78 | n++; |
---|
| 79 | tmp = parse_int(str,n); |
---|
[12130] | 80 | n = tmp & 0x0000FFFF; |
---|
| 81 | other_ID = tmp >> 16; |
---|
[12072] | 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); |
---|
[12160] | 88 | //orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl; |
---|
[12072] | 89 | break; |
---|
| 90 | } |
---|
| 91 | } |
---|
| 92 | } |
---|
| 93 | } |
---|
[12130] | 94 | }while((n < str.size()) && (str[n] == '+')); |
---|
[12160] | 95 | |
---|
[12072] | 96 | if(n == str.size()) return; |
---|
[12130] | 97 | while((n < str.size()) && (str[n] == ' ')) n++; |
---|
[12072] | 98 | if(n == str.size()) return; |
---|
| 99 | if(str[n] != ','){ |
---|
| 100 | orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl; |
---|
[12077] | 101 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 102 | } |
---|
| 103 | n++; |
---|
| 104 | while(n < str.size() && str[n] == ' ') n++; |
---|
[12160] | 105 | orxout()<<"One province finished"<<endl; |
---|
[12072] | 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; |
---|
[12077] | 119 | orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; |
---|
[12072] | 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 | } |
---|
[12049] | 131 | } |
---|