Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Changeset 11618 for code


Ignore:
Timestamp:
Dec 3, 2017, 7:32:18 PM (7 years ago)
Author:
remartin
Message:

Die überkomplizierte Verteilung in SpawnChildren funktioniert recht fehlerfrei, ausser beim riesigen Asteroiden. Weiter testen.

Location:
code/branches/AsteroidMining_HS17/src/modules/asteroidmining
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc

    r11615 r11618  
    4343++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    4444
     45FRAGEN:
     46* Zeilen-Loeschung beim output
     47* orxout()-Problem
     48* dokumentieren mit @brief?
     49* Warnumg aus Asteroidenzerstoerung
     50
     51
    4552KNACKPUNKTE:
    4653
    47 o Floating point exception
    48 o math.pi richtig einbinden.
    4954
    5055OFFEN:
     56
     57o Mass distribution -> weird for bigger asteroids
     58--> more testing
     59
     60o SpicedAsteroidField fertigstellen, Mineraldichten-Parameter
     61
    5162o Add custom pickup 'resources'. Weird template stuff -> Problems setting 'size' and other properties? setNumber of Resources.
    5263--> PickupTemplateName_ in PickupSpawner ist ein string. Wird via getBaseClassIdentifier ausgelesen, daraus wird ein Pickupable fabriziert.
     
    5566--> data_extern/images/effects: PNG's für die Pickups und GUI-Dinger.
    5667
     68o inherit parent velocity
     69o set collisionDamage? Just for static entities?
     70o Dokumentieren mit @brief?
     71
    5772o Explosion parts
    5873o custom HUD
    59 o asteroidField.lua anpassen, mit 'Mineraliendichten-Parameter'.
    60 
    61 o Dokumentieren mit @brief?
    62 
    63 o replace ugly random stuff with math.rnd()
    64 o set collisionDamage? Just for static entities?
    6574
    6675HANDBUCH:
     
    92101o Man kann keine Arrays der Groesse 0 initialisieren, aber auch nicht per IF 2x definieren.
    93102o i++ einfach ganz verhindern, ++i stattdessen.
     103o Discusting mixup with array length, accessing element a[len]...
     104o unitialised mass value -> obese asteroid, physics bug.
    94105
    95106
     
    163174        this->registerVariables();
    164175
    165         orxout() << "AsteroidMining:: Pseudo-Konstruktor passiert!" << endl;
     176        //orxout() << "AsteroidMining:: Pseudo-Konstruktor passiert!" << endl;
    166177
    167178    }
     
    181192        // Default Values
    182193        this->generateSmaller = true;
    183         //this->setHealth(50);
    184194        this->context = c;
    185         this->size = size; // customSize
    186         this->health_ = 15*size;// capped at 200 in pawn or smth?
     195        this->size = size;
     196        this->health_ = 15*size; 
    187197        this->maxHealth_ = this->health_;
    188198        this->acceptsPickups_ = false;
     
    190200        this->setPosition(position);
    191201        //this->roll = rand()*5; //etwas Drehung. richtige Variable
    192 
    193         // // Fliegt davon, irgendwieso. Dies scheint auch nicht zu nuetzen.
    194         // this->setVelocity(Vector3(0,0,0));
    195 
    196202
    197203
     
    200206        // random one of the 6 shapes
    201207        char meshThingy[] = "";
    202         sprintf(meshThingy, "ast%.0f.mesh", (round((5*(double)rand()/(double)RAND_MAX))+1)); //    sprintf(str, "Value of Pi = %f", M_PI);
     208        sprintf(meshThingy, "ast%.0f.mesh", round(5*rnd())+1); //    sprintf(str, "Value of Pi = %f", M_PI);
    203209        hull->setMeshSource(meshThingy);
    204210        hull->setScale(this->size);
     
    215221        this->initialised=true;
    216222
    217         orxout() << "AsteroidMining:: Initialisierung Zweitkonstruktor abgeschlosssssen." << endl;
     223        //orxout() << "AsteroidMining:: Initialisierung Zweitkonstruktor abgeschlosssssen." << endl;
    218224
    219225    }
     
    228234        AsteroidMinable* reborn = new AsteroidMinable(this->context, this->size, this->getPosition());
    229235        reborn->toggleShattering(true); // mainly here to avoid 'unused' warning.
    230         this->~AsteroidMinable(); // seems dangerous. Necessary for efficiency?
     236        this->~AsteroidMinable(); // seems dangerous, yields warning. Necessary for efficiency?
    231237
    232238    }
     
    249255    void AsteroidMinable::registerVariables()
    250256    {
    251         // registerVariable(this->bAlive_,            VariableDirection::ToClient);
    252         // registerVariable(this->bVulnerable_,       VariableDirection::ToClient);
    253         // registerVariable(this->health_,            VariableDirection::ToClient);
    254         // registerVariable(this->maxHealth_,         VariableDirection::ToClient);
    255257
    256258        registerVariable(this->size, VariableDirection::ToClient);
     
    258260
    259261        registerVariable(this->initialised, VariableDirection::ToClient);
    260         //registerVariable(this->context, VariableDirection::ToClient); // can't link that since it's a context
    261 
    262262
    263263            //         float size;
     
    270270    void AsteroidMinable::tick(float dt)
    271271    {
    272         if(!(this->initialised)){this->putStuff();}
     272        if(!(this->initialised)){this->putStuff();} 
    273273
    274274        if(this->health_ <=0){this->death();}
    275 
    276         // if(this->initialised){
    277         //     // this->setVelocity(Vector3(0,0,0));  // Funktioniert, scheint aber unsinnig.
    278         //     orxout() << "Flying at speed: " << this->getVelocity() << endl;
    279         // }
    280275
    281276    }
     
    328323
    329324        // Smaller Parts = 'Children'
    330         if(this->generateSmaller){
    331             this->spawnChildren();
    332         }
     325        if(this->generateSmaller){this->spawnChildren();}
    333326
    334327        // orxout() << "Wieder retour in death() geschafft. " << endl;
     
    345338    int num = round(rnd()*(massRem-1)) + 1; // random number of children, at least one
    346339    if(num > 10){num = 10;} // no max function in C!
    347     int masses[num] = {1}; // Masses of the asteroids, at least one.
    348     massRem = massRem-num;
    349 
    350     orxout() << "SpawnChildren(): passed basic stuff. num = " << num << endl;
     340    int masses[num]; // Masses of the asteroids, at least one.
     341    orxout() << "SpawnChildren(): Passed basic stuff. num = " << num << "; massRem(total) = "<< massRem << endl;
     342
     343    massRem = massRem-num; // mass is at least one, add again below.
    351344
    352345    // Randomnised spawning points for the new asteroids
    353346    float phi[num] = {0.0}; // assuming that it gets initialised to 0. Add (= {0.0})?
    354347    float theta[num] = {0.0};
    355     float pi = 3.14159;//math.pi;
    356 
    357     float d_p = 2*pi/num;
    358     float d_t = pi/num;
    359     float p = d_p/2;
    360     float t = d_t/2;
    361     // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system
     348    float piG = pi; // ist statisch oder so.
     349
     350    float d_p = 2*piG/num;
     351    float d_t = piG/num;
     352    float p = d_p/2.0;
     353    float t = d_t/2.0;
     354    // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system?
    362355    // float thetaOffset = rnd()*pi;
    363     float rScaling = tan(t); // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
    364 
    365     int pos;
    366     for(int it = 0; it<num; ++it){
    367         orxout() << "SpawnChildren(): Entering For. " << endl;
    368 
    369         pos = mod((int)(rnd()*num),num);
    370         orxout() << "SpawnChildren(): Trying position: " << pos << endl;
    371 
    372         while(phi[pos] != 0.0){// find empty spot in array
    373             pos = (int)mod(++pos, num);
    374             orxout() << "SpawnChildren(): Inside, testing position: " << pos << endl;
    375         }
    376         phi[pos] = p + it*d_p;// set angle there
    377 
    378         pos = mod((int)(rnd()*num),num);
    379         while(theta[pos] != 0.0){
    380             pos = (int)mod(++pos, num);
    381         }
    382         theta[pos] = t + it*d_t;
    383     }
    384 
    385     orxout() << "SpawnChildren(): passed angle stuff. " << endl;
     356    float rScaling; // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
     357    if(num == 1 ){
     358        rScaling = 1; // avoid tan(90). Unused.
     359    }else{
     360
     361        rScaling = tan(t);
     362
     363        int pos; // insert at random position (linear probing) in array, to get some randomness. 
     364        for(int it = 0; it<num; ++it){
     365
     366            pos = mod((int)(rnd()*num),num);
     367            while(phi[pos] != 0.0){// find empty spot in array
     368                pos = (int)mod(++pos, num);
     369            }
     370            phi[pos] = p + it*d_p;// set angle there
     371
     372            pos = mod((int)(rnd()*num),num);
     373            while(theta[pos] != 0.0){
     374                pos = (int)mod(++pos, num);
     375            }
     376            theta[pos] = t + it*d_t;
     377        }
     378    }
     379
     380    //orxout() << "SpawnChildren(): Phi: "; printArrayString(phi);
     381    //orxout() << "SpawnChildren(): Theta: "; printArrayString(theta);
     382    orxout() << "SpawnChildren(): Passed angle stuff. " << endl;
    386383
    387384    // 'Triangular', discrete probability density with max at the expected value massRem/num at a. a+b = c
     
    392389        int a = round(massRem/num);
    393390        int b = c-a;
    394         int m = 2/c; // Peak value, is reached at the average size.
     391       
    395392        int z = 0;
    396         for(z = 0; z<=a; ++z){probDensity[z] = m*z/a; } // rising part
    397         for(z = a+1; z<c; ++z){probDensity[z] = m - m*(z-a)/b;} // falling part
     393        float dProbA = 1.0/(a*a + 3.0*a + 2.0); // one 'probability unit' for discrete ramp function. Gauss stuff.
     394        for(z = 0; z<=a; ++z){probDensity[z] = (z+1)*dProbA; } // rising part
     395
     396        float dProbB = 1.0/(b*b +3.0*b + 2.0);
     397        for(z = 0; z<b; ++z){probDensity[c-z] = (z+1)*dProbB;} // falling part
    398398   
    399         // Distributing the mass
    400         for(int trav = 0; trav<num && massRem>0; ++trav){
    401 
    402             int result = 0;
    403             float rVal = rnd();// between 0 and 1
    404             float probSum = probDensity[0];
    405             while(rVal>probSum){
    406                 probSum = probSum+probDensity[result+1];
     399        // Testing
     400        for(int globi = 0; globi<c; ++globi){
     401            orxout() << "pDensity at [" << globi << "] is: " << probDensity[globi] << endl;
     402        }
     403
     404        // Distributing the mass to individual asteroids
     405        int result;
     406        float rVal;// between 0 and 1
     407        float probSum;
     408        for(int trav = 0; trav<num; ++trav){
     409            result = 0;// reset
     410            rVal = rnd();// between 0 and 1
     411            orxout() << "Random Value picked: " << rVal << endl;
     412            probSum = probDensity[0];
     413            //orxout() << "Sum at start: " << probSum << endl;
     414
     415            while(rVal>probSum && result<massRem){// Not yet found && there-s smth to distribute (Incrementing once inside!)
     416                if(result<(num-2)){probSum = probSum+probDensity[result+1];} // avoid logical/acess error
    407417                ++result;
     418                //orxout() << "Sum so far: " << probSum << endl;
    408419            }
    409             if(result>massRem){result = massRem;}
    410420
    411421            massRem = massRem-result;
    412             masses[trav] = masses[trav]+result;
    413 
    414         }
    415     }
     422            masses[trav] = 1 +result; // at least one
     423            orxout() << "Mass chosen for child " << trav << " is: " << masses[trav] << endl;
     424
     425        }
     426    }else{// Everyone has mass 1. Initialising the array to 1 doesn-t seem to work. Hideous C language!
     427        for(int schnaegg = 0; schnaegg<num; ++schnaegg){
     428            masses[schnaegg] = 1;
     429        }
     430    }
     431
     432    //orxout() << "SpawnChildren(): Masses: "; printArrayString(masses);
     433    orxout() << "SpawnChildren(): Passed mass stuff. " << endl;
     434
     435    // Creating the 'chlidren':
     436    for(int fisch = 0; fisch<num; ++fisch){
     437
     438        Vector3* pos = new Vector3(0,0,0); // Position offset
     439        if(num > 1){// not required if there-s just one child
     440            float r = masses[fisch]/rScaling;
     441            pos = new Vector3(r*sin(theta[fisch])*cos(phi[fisch]), r*sin(theta[fisch])*sin(phi[fisch]), r*cos(theta[fisch])); // convert spheric coordinates to vector
     442        }
    416443       
    417     orxout() << "SpawnChildren(): passed Mass stuff. " << endl;
    418 
    419     for(int fisch = 0; fisch<num; ++fisch){// create the children
    420 
    421         // Position offset:
    422         float r = masses[fisch]/rScaling;
    423         Vector3* pos = new Vector3(r*sin(theta[fisch])*cos(phi[fisch]), r*sin(theta[fisch])*sin(phi[fisch]), r*cos(theta[fisch]));
    424 
     444        //orxout() << "Creating asteroid with mass " << masses[fisch] << " at relative postition " << *pos << endl;
    425445        AsteroidMinable* child = new AsteroidMinable(this->context, masses[fisch], this->getPosition() + *pos);
    426446
     
    476496    }
    477497
    478 
    479 
     498    void AsteroidMinable::printArrayString(float thingy[]){ // Don-t work!
     499
     500        orxout() << "[" ; //<< endl; 
     501        char frag[] = "";
     502        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
     503        for(int m = 0; m< (len-2); ++m){
     504            sprintf(frag, "%.5f, ", thingy[m]);
     505            orxout() << frag << endl;//std::flush;
     506        }
     507        sprintf(frag, "%.5f]", thingy[len-1]);
     508        orxout() << frag << endl; // Just print it here! No ugly passing.
     509    }
     510
     511    void AsteroidMinable::printArrayString(int thingy[]){
     512
     513        orxout() << "[" ; //<< endl;
     514        char frag[] = "";
     515        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
     516        for(int m = 0; m< (len-2); ++m){
     517            sprintf(frag, "%.0i, ", thingy[m]);
     518            orxout() << frag << endl;//std::flush;
     519            printf("TEst");
     520        }
     521
     522        sprintf(frag, "%.0i]", thingy[len-1]); // last element
     523        orxout() << frag << endl; // Just print it here! No ugly passing.
     524    }
     525
     526
     527    // void AsteroidMinable::printArrayString(int thingy[]){
     528
     529    //     char res[] = "[";
     530    //     //strcat(res, "[");
     531    //     char frag[] = "";
     532
     533    //     int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
     534    //     for(int m = 0; m< (len-1); ++m){
     535    //         sprintf(frag, "%.0i, ", thingy[m]);
     536    //         strcat(res, frag);
     537    //     }
     538    //     sprintf(frag, "%.0i]", thingy[len]);
     539    //     strcat(res, frag); // last element
     540
     541    //     orxout() << res << endl; // Just print it here! No ugly passing.
     542
     543    //     // static char result[(sizeof(res)/sizeof("")] = res; // define as static, would get deleted otherwise.
     544    //     // char *result = malloc(sizeof(res)/sizeof("") + 1);
     545    //     // *result = res;
     546    //     // return result;
     547    // }
    480548
    481549
  • code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.h

    r11586 r11618  
    9292            virtual void spawnChildren();
    9393
     94            void printArrayString(float thingy[]);// Just for testing
     95            void printArrayString(int thingy[]);// Just for testing
     96
     97
     98
    9499
    95100
Note: See TracChangeset for help on using the changeset viewer.