#include "WagnisGameboard.h" #include "core/CoreIncludes.h" #include "BulletDynamics/Dynamics/btRigidBody.h" #include #include namespace orxonox { RegisterClass(WagnisGameboard); WagnisGameboard::WagnisGameboard(Context* context) : StaticEntity(context){ RegisterObject(WagnisGameboard); this->connections_string = ""; } WagnisGameboard::~WagnisGameboard(){} void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){ SUPER(WagnisGameboard, XMLPort, xmlelement, mode); XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode); XMLPortParam(WagnisGameboard, "connections_string", setConnections_string, getConnections_string, xmlelement, mode); } //XML FUNCTIONS //Adds a Province to the Gameboard void WagnisGameboard::addProvince(WagnisProvince* province){ orxout() << "added" << endl; orxout() << province->getID() << endl; this->provs.push_back(province); } //XML get province WagnisProvince* WagnisGameboard::getProvince(unsigned int index) const{ if(this->provs.size() <= index) return nullptr; return this->provs.at(index); } //XML set connections_string void WagnisGameboard::setConnections_string(const std::string& str){ this->connections_string = str; } //XML get connections_string std::string WagnisGameboard::getConnections_string() const{ return this -> connections_string; } //Parses the string and initializes the neigbors vector of all provinces according //Syntax: 32=7-8-4 , 2=33+5+7+1+4 void WagnisGameboard::initializeNeighbors(){ std::string str = this->connections_string; orxout() << "inizializing started" << endl; orxout() << "String size:" << endl; orxout() << str.size() << endl; unsigned int n = 0; while(n < str.size()){ int tmp = parse_int(str,n); n = tmp & 0x0000FFFF; int origin_ID = tmp >> 16; if(n == str.size() || str[n] != '='){ orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl; orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; } int other_ID; do{ n++; tmp = parse_int(str,n); n = tmp & 0x0000FFFF; other_ID = tmp >> 16; for(WagnisProvince* orig:this->provs){ if(orig->getID() == origin_ID){ for(WagnisProvince* other:this->provs){ if(other->getID() == other_ID){ orig->addNeighbor(other); orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl; break; } } } break; } }while((n < str.size()) && (str[n] == '+')); if(n == str.size()) return; while((n < str.size()) && (str[n] == ' ')) n++; if(n == str.size()) return; if(str[n] != ','){ orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl; orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; } n++; while(n < str.size() && str[n] == ' ') n++; } } //Returns the parsed int and the offset encoded in an int. the upper 16bit(incl MSB) is the number //and the lower 16 bits is the new n(after the last digit) int WagnisGameboard::parse_int(std::string str,unsigned int n){ if(n >= str.size()){ orxout() << "Error while parsing neighbors-string: Internal error at WagnisGameboard::parse_int() "<< endl; } int digit = str[n] - '0'; int number = digit; if(digit < 0 || digit > 9){ orxout() << "Error while parsing neighbors-string: Digit expected at position: "<< n << endl; orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl; } n++; while(n < str.size() && str[n] - '0' >= 0 && str[n] - '0' < 10){ digit = str[n] - '0'; number = 10 * number; number += digit; n++; } return (number << 16)+n; } }