Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
Nov 3, 2018, 11:49:55 PM (7 years ago)
Author:
stadlero
Message:

Added connections-string parser in WagnisGameboard which still needs work

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/wagnis_HS18/src/modules/wagnis/WagnisGameboard.cc

    r12067 r12072  
    33#include "BulletDynamics/Dynamics/btRigidBody.h"
    44#include <vector>
     5#include <string>
    56
    67
     
    1516    WagnisGameboard::~WagnisGameboard(){
    1617        for(WagnisProvince* prov:this->provs){
    17             prov->destroy(); //destroys all Provinces
     18            prov->destroy();
    1819        }
     20    }
     21    void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){
     22        SUPER(WagnisGameboard, XMLPort, xmlelement, mode);
     23
     24        XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode);
     25        XMLPortParam(WagnisGameboard, "connections_string", setConnections_string, getConnections_string, xmlelement, mode);
    1926    }
    2027
    2128
    2229
    23     //XML Port
    24     void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){
    25         SUPER(WagnisGameboard, XMLPort, xmlelement, mode);
    2630
    27         XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode);
    28     }
    29     //XML add province
     31
     32
     33
     34    //XML FUNCTIONS
    3035    //Adds a Province to the Gameboard
    3136    void WagnisGameboard::addProvince(WagnisProvince* province){
     
    3338        orxout() << province->getID() << endl;
    3439        this->provs.push_back(province);
     40       
     41        if(province->getID() == 1000){
     42            orxout() << "You added a province with ID 1000.\nThis is the magic debug number.\nThe function to initialize the neighbors of all provinces will be called:" << endl;
     43            orxout() << "-----neighbors-----" << endl << endl;
     44            initializeNeighbors(this->connections_string);
     45        }
    3546    }
    3647    //XML get province
     
    3950        return this->provs.at(index);
    4051    }
     52    //XML set connections_string
     53    void WagnisGameboard::setConnections_string(const std::string& str){
     54        this->connections_string = str;
     55    }
     56    //XML get connections_string
     57    std::string WagnisGameboard::getConnections_string() const{
     58        return this -> connections_string;
     59    }
     60
     61
     62
     63
     64
     65
     66
     67    //Parses the string and initializes the neigbors vector of all provinces according
     68    //Syntax: 32=7-8-4  , 2=33-5-7-1-4
     69    void WagnisGameboard::initializeNeighbors(std::string str){
     70        orxout() << "inizializing started" << endl;
     71        orxout() << "String size:" << endl;
     72        orxout() << str.size() << endl;
     73        unsigned int n = 0;
     74        while(n < str.size()){
     75            orxout() << "test1" << endl;
     76            int tmp = parse_int(str,n);
     77            n = tmp | 0x0000FFFF;
     78            int origin_ID = tmp / (2<<16);
     79            if(n == str.size() || str[n] != '='){
     80                orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl;
     81                orxout() << "Correct syntax: 32=4-2-5-67, 54=8-1-12" << endl;
     82            }
     83            int other_ID;
     84            do{
     85                n++;
     86                tmp = parse_int(str,n);
     87                n = tmp | 0x0000FFFF;
     88                other_ID = tmp / (2<<16);
     89
     90                for(WagnisProvince* orig:this->provs){
     91                    if(orig->getID() == origin_ID){
     92                        for(WagnisProvince* other:this->provs){
     93                            if(other->getID() == other_ID){
     94                                orig->addNeighbor(other);
     95                                orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl;
     96                                break;
     97                            }
     98                        }
     99                    }
     100                    break;
     101                }
     102            }while(n < str.size() && str[n] == '-');
     103            if(n == str.size()) return;
     104            while(n < str.size() && str[n] == ' ') n++;
     105            if(n == str.size()) return;
     106            if(str[n] != ','){
     107                orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl;
     108                orxout() << "Correct syntax: 32=4-2-5-67, 54=8-1-12" << endl;
     109            }
     110            n++;
     111            while(n < str.size() && str[n] == ' ') n++;
     112        }
     113    }
     114
     115    //Returns the parsed int and the offset encoded in an int. the upper 16bit(incl MSB) is the number
     116    //and the lower 16 bits is the new n(after the last digit)
     117    int WagnisGameboard::parse_int(std::string str,unsigned int n){
     118        if(n >= str.size()){
     119            orxout() << "Error while parsing neighbors-string: Internal error at WagnisGameboard::parse_int() "<< endl;
     120        }
     121        int digit = str[n] - '0';
     122        int number = digit;
     123        if(digit < 0 || digit > 9){
     124            orxout() << "Error while parsing neighbors-string: Digit expected at position: "<< n << endl;
     125            orxout() << "Correct syntax: 32=4-2-5-67, 54=8-1-12" << endl;
     126        }
     127
     128        n++;
     129        while(n < str.size() && str[n] - '0' >= 0 && str[n] - '0' < 10){
     130            digit = str[n] - '0';
     131            number = 10 * number;
     132            number += digit;
     133            n++;
     134        }
     135        return (number << 16)+n;
     136    }
    41137}
Note: See TracChangeset for help on using the changeset viewer.