Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

changed gametype from Deathmatch to Wagnis in Wagnis.oxw + some wip in Wagnis.cc

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        this->connections_string = "";
15    }
16    WagnisGameboard::~WagnisGameboard(){}
17
18    void WagnisGameboard::XMLPort(Element& xmlelement,XMLPort::Mode mode){
19        SUPER(WagnisGameboard, XMLPort, xmlelement, mode);
20
21        XMLPortObject(WagnisGameboard, WagnisProvince, "Provinces", addProvince, getProvince, xmlelement, mode);
22        XMLPortParam(WagnisGameboard, "connections_string", setConnections_string, getConnections_string, xmlelement, mode);
23    }
24
25
26
27
28
29
30
31    //XML FUNCTIONS
32    //Adds a Province to the Gameboard
33    void WagnisGameboard::addProvince(WagnisProvince* province){
34        orxout() << "added" << endl;
35        orxout() << province->getID() << endl;
36       
37        this->provs.push_back(province);
38    }
39    //XML get province
40    WagnisProvince* WagnisGameboard::getProvince(unsigned int index) const{
41        if(this->provs.size() <= index) return nullptr;
42        return this->provs.at(index);
43    }
44    //XML set connections_string
45    void WagnisGameboard::setConnections_string(const std::string& str){
46        this->connections_string = str;
47    }
48    //XML get connections_string
49    std::string WagnisGameboard::getConnections_string() const{
50        return this -> connections_string;
51    }
52
53
54
55
56
57
58
59    //Parses the string and initializes the neigbors vector of all provinces according
60    //Syntax: 32=7-8-4  , 2=33+5+7+1+4
61    void WagnisGameboard::initializeNeighbors(){
62       
63        std::string str = this->connections_string;
64        orxout() << "inizializing started" << endl;
65        orxout() << "String size:" << endl;
66        orxout() << str.size() << endl;
67        unsigned int n = 0;
68        while(n < str.size()){
69            int tmp = parse_int(str,n);
70            n = tmp & 0x0000FFFF;
71            int origin_ID = tmp >> 16;
72            if(n == str.size() || str[n] != '='){
73                orxout() << "Error while parsing neighbors-string: '=' expected at position: "<< n << endl;
74                orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
75            }
76            int other_ID;
77            do{
78                n++;
79                tmp = parse_int(str,n);
80                n = tmp & 0x0000FFFF;
81                other_ID = tmp >> 16;
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);
88                                orxout() << "Added neighbor province "<< other_ID << " to province " << origin_ID << endl;
89                                break;
90                            }
91                        }
92                    }
93                    break;
94                }
95            }while((n < str.size()) && (str[n] == '+'));
96            if(n == str.size()) return;
97            while((n < str.size()) && (str[n] == ' ')) n++;
98            if(n == str.size()) return;
99            if(str[n] != ','){
100                orxout() << "Error while parsing neighbors-string: ',' expected at position: "<< n << endl;
101                orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
102            }
103            n++;
104            while(n < str.size() && str[n] == ' ') n++;
105        }
106    }
107
108    //Returns the parsed int and the offset encoded in an int. the upper 16bit(incl MSB) is the number
109    //and the lower 16 bits is the new n(after the last digit)
110    int WagnisGameboard::parse_int(std::string str,unsigned int n){
111        if(n >= str.size()){
112            orxout() << "Error while parsing neighbors-string: Internal error at WagnisGameboard::parse_int() "<< endl;
113        }
114        int digit = str[n] - '0';
115        int number = digit;
116        if(digit < 0 || digit > 9){
117            orxout() << "Error while parsing neighbors-string: Digit expected at position: "<< n << endl;
118            orxout() << "Correct syntax: 32=4+2+5+67, 54=8+1+12" << endl;
119        }
120
121        n++;
122        while(n < str.size() && str[n] - '0' >= 0 && str[n] - '0' < 10){
123            digit = str[n] - '0';
124            number = 10 * number;
125            number += digit;
126            n++;
127        }
128        return (number << 16)+n;
129    }
130}
Note: See TracBrowser for help on using the repository browser.