Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/wagnis_HS18/src/modules/wagnis/WagnisGameboard.cc @ 12124

Last change on this file since 12124 was 12124, checked in by stadlero, 5 years ago

Wagnis class wip

File size: 4.5 KB
Line 
1#include "WagnisGameboard.h"
2#include "core/CoreIncludes.h"
3#include "BulletDynamics/Dynamics/btRigidBody.h"
4#include <vector>
5#include <string>
6
7
8namespace orxonox
9{
10    RegisterClass(WagnisGameboard);
11   
12    WagnisGameboard::WagnisGameboard(Context* context) : StaticEntity(context){
13        RegisterObject(WagnisGameboard);
14    }
15
16    WagnisGameboard::~WagnisGameboard(){
17        for(WagnisProvince* prov:this->provs){
18            prov->destroy();
19        }
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);
26    }
27
28
29
30
31
32
33
34    //XML FUNCTIONS
35    //Adds a Province to the Gameboard
36    void WagnisGameboard::addProvince(WagnisProvince* province){
37        orxout() << "added" << endl;
38        orxout() << province->getID() << endl;
39       
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        std::string str = this->connections_string;
66        orxout() << "inizializing started" << endl;
67        orxout() << "String size:" << endl;
68        orxout() << str.size() << endl;
69        unsigned int n = 0;
70        while(n < str.size()){
71            orxout() << "test1" << endl;
72            int tmp = parse_int(str,n);
73            n = tmp | 0x0000FFFF;
74            int origin_ID = tmp / (2<<16);
75            if(n == str.size() || str[n] != '='){
76                orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl;
77                orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
78            }
79            int other_ID;
80            do{
81                n++;
82                tmp = parse_int(str,n);
83                n = tmp | 0x0000FFFF;
84                other_ID = tmp / (2<<16);
85
86                for(WagnisProvince* orig:this->provs){
87                    if(orig->getID() == origin_ID){
88                        for(WagnisProvince* other:this->provs){
89                            if(other->getID() == other_ID){
90                                orig->addNeighbor(other);
91                                orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl;
92                                break;
93                            }
94                        }
95                    }
96                    break;
97                }
98            }while(n < str.size() && str[n] == '+');
99            if(n == str.size()) return;
100            while(n < str.size() && str[n] == ' ') n++;
101            if(n == str.size()) return;
102            if(str[n] != ','){
103                orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl;
104                orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
105            }
106            n++;
107            while(n < str.size() && str[n] == ' ') n++;
108        }
109    }
110
111    //Returns the parsed int and the offset encoded in an int. the upper 16bit(incl MSB) is the number
112    //and the lower 16 bits is the new n(after the last digit)
113    int WagnisGameboard::parse_int(std::string str,unsigned int n){
114        if(n >= str.size()){
115            orxout() << "Error while parsing neighbors-string: Internal error at WagnisGameboard::parse_int() "<< endl;
116        }
117        int digit = str[n] - '0';
118        int number = digit;
119        if(digit < 0 || digit > 9){
120            orxout() << "Error while parsing neighbors-string: Digit expected at position: "<< n << endl;
121            orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
122        }
123
124        n++;
125        while(n < str.size() && str[n] - '0' >= 0 && str[n] - '0' < 10){
126            digit = str[n] - '0';
127            number = 10 * number;
128            number += digit;
129            n++;
130        }
131        return (number << 16)+n;
132    }
133}
Note: See TracBrowser for help on using the repository browser.