Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 12072


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

Added connections-string parser in WagnisGameboard which still needs work

Location:
code/branches/wagnis_HS18
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • code/branches/wagnis_HS18/data/levels/Wagnis.oxw

    r12067 r12072  
    4848    <SpawnPoint position="100, 45, 75" lookat="0, 45, 75" />
    4949
    50 <WagnisGameboard position="0,0,0">
    51  <Provinces>
     50<WagnisGameboard position="0,0,0" connections_string="1=2,2=1-1000,1000=2  ">
     51
     52  <Provinces>
     53
    5254    <WagnisProvince ID="1" continent="1" position="0,30,30" rotationrate="<?lua print(math.random() * 50) ?>" rotationaxis="<?lua print((math.random()-1)*5)?>,<?lua print((math.random()-1)*5)?>,<?lua print((math.random()-1)*5)?>">
    5355      <attached>
     
    6264    </WagnisProvince>
    6365
    64 
    65 
    66 
     66    <WagnisProvince ID="1000" continent="1" position="0,40,40" rotationrate="<?lua print(math.random() * 50) ?>" rotationaxis="<?lua print((math.random()-1)*5)?>,<?lua print((math.random()-1)*5)?>,<?lua print((math.random()-1)*5)?>">
     67      <attached>
     68        <Model position="0,0,0" mesh="ast1.mesh" scale3D="2,2,2" />
     69      </attached>
     70    </WagnisProvince>
    6771
    6872  </Provinces>
  • code/branches/wagnis_HS18/src/modules/wagnis/Wagnis.h

    r12068 r12072  
    22#include "WagnisProvince.h"
    33
    4 // todo: klassen zusammenfassen
     4/** Die Wagnis Klasse hat die folgenden Aufgaben:
     5 *  1. Das Spiel erstellen.
     6 *  - Gameboard initialisieren
     7 *  - Provinzen initialisieren
     8 *  - Provinzen verbinden (Nachbarschaften eintragen)
     9 *  - Spieler initialisieren
     10 *  -
     11 *
     12 *  2. Den Spielablauf steuern
     13 *  - Für einen Spieler berechnen wie viele Truppen er setzen darf.
     14 *  - Den Funktion "setTroops(int troops)" beim Spieler aufrufen.
     15 *  - Die Funktion "playerTurn()" bei jedem Spieler aufrufen.
     16 *  -
     17 *
     18 **/
    519
    620namespace orxonox
     
    1125
    1226        // to start the game
    13         void createGame;        // creates and links provinces
     27        void createGame();        // creates and links provinces
     28
    1429       
    15             void setPlayers;        // initialises players
    16             void chooseProvinces    // players can choose their provinces
    17        
    18 
    19         void playerTurn         // each players turn
    20            
    21             void setTroops;         // get and set your reinforcements
    22             void attack;            // attack neighbouring provinces
    23             void moveTroops;        // move troops from one province to another
    24        
    25             // additional checking funtions
    26             int  troopCounter;       // counts how many reinforcements player gets
    27             bool attackChecker;     // checks whether an attack move is valid
     30        // additional checking funtions
     31        int troopCounter(int);       // counts how many reinforcements player gets
     32        bool attackChecker;     // checks whether an attack move is valid
    2833                                    // (provinces linked, enough troops, no own province)
    29             bool moveChecker;       // checks whether a troop movement is valid
     34        bool moveChecker;       // checks whether a troop movement is valid
    3035                                    // (start and target belong to player, link existing)
    31             void attackSimulator;   // calculates outcome of battle
     36        void attackSimulator;   // calculates outcome of battle
    3237       
    3338   
    34         private:
    35            
    36             int owner_ID;
    37             int troops;
    38             int ID;
    39             int continent;
    4039    }
    4140
  • 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}
  • code/branches/wagnis_HS18/src/modules/wagnis/WagnisGameboard.h

    r12051 r12072  
    1515#include "core/XMLPort.h"
    1616#include "worldentities/StaticEntity.h"
     17#include <string>
    1718#include <vector>
    1819
     
    3031        void addProvince(WagnisProvince*);
    3132        WagnisProvince* getProvince(unsigned int) const;
     33        void setConnections_string(const std::string&);
     34        std::string getConnections_string() const;
    3235        //
     36
     37        void initializeNeighbors(std::string);
    3338
    3439   
    3540    private:
    3641        std::vector<WagnisProvince*> provs;
     42        std::string connections_string;
     43        int parse_int(std::string,unsigned int);
    3744    };
    3845}
  • code/branches/wagnis_HS18/src/modules/wagnis/WagnisProvince.cc

    r12067 r12072  
    7272    }
    7373
    74     //Creates a connection between two provinces.
     74    //Adds a connection to neighbors.
    7575    void WagnisProvince::addNeighbor(WagnisProvince* prov){
    7676        neighbors.push_back(prov);
    77         prov->neighbors.push_back(this);
    7877    }
    7978}
Note: See TracChangeset for help on using the changeset viewer.