| 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 |         this->connections_string = ""; | 
|---|
| 15 |     } | 
|---|
| 16 |     WagnisGameboard::~WagnisGameboard(){ | 
|---|
| 17 |         for(std::vector<WagnisProvince*>* vec: this->continents){ | 
|---|
| 18 |             delete vec; | 
|---|
| 19 |         } | 
|---|
| 20 |     } | 
|---|
| 21 |  | 
|---|
| 22 |     void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){ | 
|---|
| 23 |         SUPER(WagnisGameboard, XMLPort, xmlelement, mode); | 
|---|
| 24 |  | 
|---|
| 25 |         XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode); | 
|---|
| 26 |         XMLPortParam(WagnisGameboard, "connections_string", setConnections_string, getConnections_string, xmlelement, mode); | 
|---|
| 27 |     } | 
|---|
| 28 |  | 
|---|
| 29 |  | 
|---|
| 30 |  | 
|---|
| 31 |  | 
|---|
| 32 |  | 
|---|
| 33 |  | 
|---|
| 34 |  | 
|---|
| 35 |     //XML FUNCTIONS | 
|---|
| 36 |     //Adds a Province to the Gameboard | 
|---|
| 37 |     void WagnisGameboard::addProvince(WagnisProvince* province){ | 
|---|
| 38 |         orxout() << "added" << endl; | 
|---|
| 39 |         orxout() << province->getID() << endl; | 
|---|
| 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(){ | 
|---|
| 65 |          | 
|---|
| 66 |         std::string str = this->connections_string; | 
|---|
| 67 |         orxout() << "inizializing started" << endl; | 
|---|
| 68 |         orxout() << "String size:" << endl; | 
|---|
| 69 |         orxout() << str.size() << endl; | 
|---|
| 70 |         orxout() << str<<endl; | 
|---|
| 71 |         unsigned int n = 0; | 
|---|
| 72 |         while(n < str.size()){ | 
|---|
| 73 |             int tmp = parse_int(str,n); | 
|---|
| 74 |             n = tmp & 0x0000FFFF; | 
|---|
| 75 |             int origin_ID = tmp >> 16; | 
|---|
| 76 |             if(n == str.size() || str[n] != '='){ | 
|---|
| 77 |                 orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl; | 
|---|
| 78 |                 orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; | 
|---|
| 79 |             } | 
|---|
| 80 |             int other_ID; | 
|---|
| 81 |             do{ | 
|---|
| 82 |                 n++; | 
|---|
| 83 |                 tmp = parse_int(str,n); | 
|---|
| 84 |                 n = tmp & 0x0000FFFF; | 
|---|
| 85 |                 other_ID = tmp >> 16; | 
|---|
| 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); | 
|---|
| 92 |                                 //orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl; | 
|---|
| 93 |                                 break; | 
|---|
| 94 |                             } | 
|---|
| 95 |                         } | 
|---|
| 96 |                     } | 
|---|
| 97 |                 } | 
|---|
| 98 |             }while((n < str.size()) && (str[n] == '+')); | 
|---|
| 99 |  | 
|---|
| 100 |             if(n == str.size()) return; | 
|---|
| 101 |             while((n < str.size()) && (str[n] == ' ')) n++; | 
|---|
| 102 |             if(n == str.size()) return; | 
|---|
| 103 |             if(str[n] != ','){ | 
|---|
| 104 |                 orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl; | 
|---|
| 105 |                 orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; | 
|---|
| 106 |             } | 
|---|
| 107 |             n++; | 
|---|
| 108 |             while(n < str.size() && str[n] == ' ') n++; | 
|---|
| 109 |             orxout()<<"One province finished"<<endl; | 
|---|
| 110 |         } | 
|---|
| 111 |     } | 
|---|
| 112 |  | 
|---|
| 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 |  | 
|---|
| 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; | 
|---|
| 141 |             orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; | 
|---|
| 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 |     } | 
|---|
| 153 | } | 
|---|