Index: code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc
===================================================================
--- code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc	(revision 11615)
+++ code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc	(revision 11618)
@@ -43,10 +43,21 @@
 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 
+FRAGEN:
+* Zeilen-Loeschung beim output
+* orxout()-Problem
+* dokumentieren mit @brief? 
+* Warnumg aus Asteroidenzerstoerung
+
+
 KNACKPUNKTE: 
 
-o Floating point exception
-o math.pi richtig einbinden. 
 
 OFFEN: 
+
+o Mass distribution -> weird for bigger asteroids
+--> more testing
+
+o SpicedAsteroidField fertigstellen, Mineraldichten-Parameter
+
 o Add custom pickup 'resources'. Weird template stuff -> Problems setting 'size' and other properties? setNumber of Resources. 
 --> PickupTemplateName_ in PickupSpawner ist ein string. Wird via getBaseClassIdentifier ausgelesen, daraus wird ein Pickupable fabriziert. 
@@ -55,12 +66,10 @@
 --> data_extern/images/effects: PNG's für die Pickups und GUI-Dinger. 
 
+o inherit parent velocity
+o set collisionDamage? Just for static entities?
+o Dokumentieren mit @brief?
+
 o Explosion parts
 o custom HUD
-o asteroidField.lua anpassen, mit 'Mineraliendichten-Parameter'. 
-
-o Dokumentieren mit @brief?
-
-o replace ugly random stuff with math.rnd()
-o set collisionDamage? Just for static entities?
 
 HANDBUCH:
@@ -92,4 +101,6 @@
 o Man kann keine Arrays der Groesse 0 initialisieren, aber auch nicht per IF 2x definieren. 
 o i++ einfach ganz verhindern, ++i stattdessen. 
+o Discusting mixup with array length, accessing element a[len]...
+o unitialised mass value -> obese asteroid, physics bug. 
 
 
@@ -163,5 +174,5 @@
         this->registerVariables();
 
-        orxout() << "AsteroidMining:: Pseudo-Konstruktor passiert!" << endl;
+        //orxout() << "AsteroidMining:: Pseudo-Konstruktor passiert!" << endl;
 
     }
@@ -181,8 +192,7 @@
         // Default Values
         this->generateSmaller = true; 
-        //this->setHealth(50);
         this->context = c;
-        this->size = size; // customSize
-        this->health_ = 15*size;// capped at 200 in pawn or smth?
+        this->size = size;
+        this->health_ = 15*size; 
         this->maxHealth_ = this->health_;
         this->acceptsPickups_ = false;
@@ -190,8 +200,4 @@
         this->setPosition(position);
         //this->roll = rand()*5; //etwas Drehung. richtige Variable
-
-        // // Fliegt davon, irgendwieso. Dies scheint auch nicht zu nuetzen. 
-        // this->setVelocity(Vector3(0,0,0));
-
 
 
@@ -200,5 +206,5 @@
         // random one of the 6 shapes
         char meshThingy[] = "";
-        sprintf(meshThingy, "ast%.0f.mesh", (round((5*(double)rand()/(double)RAND_MAX))+1)); //    sprintf(str, "Value of Pi = %f", M_PI);
+        sprintf(meshThingy, "ast%.0f.mesh", round(5*rnd())+1); //    sprintf(str, "Value of Pi = %f", M_PI);
         hull->setMeshSource(meshThingy);
         hull->setScale(this->size);
@@ -215,5 +221,5 @@
         this->initialised=true; 
 
-        orxout() << "AsteroidMining:: Initialisierung Zweitkonstruktor abgeschlosssssen." << endl;
+        //orxout() << "AsteroidMining:: Initialisierung Zweitkonstruktor abgeschlosssssen." << endl;
 
     }
@@ -228,5 +234,5 @@
         AsteroidMinable* reborn = new AsteroidMinable(this->context, this->size, this->getPosition());
         reborn->toggleShattering(true); // mainly here to avoid 'unused' warning. 
-        this->~AsteroidMinable(); // seems dangerous. Necessary for efficiency? 
+        this->~AsteroidMinable(); // seems dangerous, yields warning.  Necessary for efficiency? 
 
     }
@@ -249,8 +255,4 @@
     void AsteroidMinable::registerVariables()
     {
-        // registerVariable(this->bAlive_,            VariableDirection::ToClient);
-        // registerVariable(this->bVulnerable_,       VariableDirection::ToClient);
-        // registerVariable(this->health_,            VariableDirection::ToClient);
-        // registerVariable(this->maxHealth_,         VariableDirection::ToClient);
 
         registerVariable(this->size, VariableDirection::ToClient);
@@ -258,6 +260,4 @@
 
         registerVariable(this->initialised, VariableDirection::ToClient);
-        //registerVariable(this->context, VariableDirection::ToClient); // can't link that since it's a context
-
 
             //         float size;
@@ -270,12 +270,7 @@
     void AsteroidMinable::tick(float dt)
     {
-        if(!(this->initialised)){this->putStuff();}
+        if(!(this->initialised)){this->putStuff();} 
 
         if(this->health_ <=0){this->death();}
-
-        // if(this->initialised){
-        //     // this->setVelocity(Vector3(0,0,0));  // Funktioniert, scheint aber unsinnig. 
-        //     orxout() << "Flying at speed: " << this->getVelocity() << endl;
-        // }
 
     }
@@ -328,7 +323,5 @@
 
         // Smaller Parts = 'Children'
-        if(this->generateSmaller){
-            this->spawnChildren();
-        }
+        if(this->generateSmaller){this->spawnChildren();}
 
         // orxout() << "Wieder retour in death() geschafft. " << endl;
@@ -345,43 +338,47 @@
     int num = round(rnd()*(massRem-1)) + 1; // random number of children, at least one
     if(num > 10){num = 10;} // no max function in C!
-    int masses[num] = {1}; // Masses of the asteroids, at least one. 
-    massRem = massRem-num; 
-
-    orxout() << "SpawnChildren(): passed basic stuff. num = " << num << endl;
+    int masses[num]; // Masses of the asteroids, at least one. 
+    orxout() << "SpawnChildren(): Passed basic stuff. num = " << num << "; massRem(total) = "<< massRem << endl;
+
+    massRem = massRem-num; // mass is at least one, add again below. 
 
     // Randomnised spawning points for the new asteroids
     float phi[num] = {0.0}; // assuming that it gets initialised to 0. Add (= {0.0})?
     float theta[num] = {0.0};
-    float pi = 3.14159;//math.pi;
-
-    float d_p = 2*pi/num;
-    float d_t = pi/num;
-    float p = d_p/2;
-    float t = d_t/2;
-    // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system
+    float piG = pi; // ist statisch oder so. 
+
+    float d_p = 2*piG/num;
+    float d_t = piG/num;
+    float p = d_p/2.0;
+    float t = d_t/2.0;
+    // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system?
     // float thetaOffset = rnd()*pi;
-    float rScaling = tan(t); // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
-
-    int pos;
-    for(int it = 0; it<num; ++it){
-        orxout() << "SpawnChildren(): Entering For. " << endl;
-
-        pos = mod((int)(rnd()*num),num); 
-        orxout() << "SpawnChildren(): Trying position: " << pos << endl;
-
-        while(phi[pos] != 0.0){// find empty spot in array
-            pos = (int)mod(++pos, num);
-            orxout() << "SpawnChildren(): Inside, testing position: " << pos << endl;
-        }
-        phi[pos] = p + it*d_p;// set angle there
-
-        pos = mod((int)(rnd()*num),num);
-        while(theta[pos] != 0.0){
-            pos = (int)mod(++pos, num);
-        }
-        theta[pos] = t + it*d_t;
-    }
-
-    orxout() << "SpawnChildren(): passed angle stuff. " << endl;
+    float rScaling; // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
+    if(num == 1 ){
+        rScaling = 1; // avoid tan(90). Unused. 
+    }else{
+
+        rScaling = tan(t); 
+
+        int pos; // insert at random position (linear probing) in array, to get some randomness.  
+        for(int it = 0; it<num; ++it){
+
+            pos = mod((int)(rnd()*num),num); 
+            while(phi[pos] != 0.0){// find empty spot in array
+                pos = (int)mod(++pos, num);
+            }
+            phi[pos] = p + it*d_p;// set angle there
+
+            pos = mod((int)(rnd()*num),num);
+            while(theta[pos] != 0.0){
+                pos = (int)mod(++pos, num);
+            }
+            theta[pos] = t + it*d_t;
+        }
+    }
+
+    //orxout() << "SpawnChildren(): Phi: "; printArrayString(phi);
+    //orxout() << "SpawnChildren(): Theta: "; printArrayString(theta);
+    orxout() << "SpawnChildren(): Passed angle stuff. " << endl;
 
     // 'Triangular', discrete probability density with max at the expected value massRem/num at a. a+b = c
@@ -392,35 +389,58 @@
         int a = round(massRem/num);
         int b = c-a;
-        int m = 2/c; // Peak value, is reached at the average size. 
+        
         int z = 0;
-        for(z = 0; z<=a; ++z){probDensity[z] = m*z/a; } // rising part
-        for(z = a+1; z<c; ++z){probDensity[z] = m - m*(z-a)/b;} // falling part
+        float dProbA = 1.0/(a*a + 3.0*a + 2.0); // one 'probability unit' for discrete ramp function. Gauss stuff. 
+        for(z = 0; z<=a; ++z){probDensity[z] = (z+1)*dProbA; } // rising part
+
+        float dProbB = 1.0/(b*b +3.0*b + 2.0);
+        for(z = 0; z<b; ++z){probDensity[c-z] = (z+1)*dProbB;} // falling part
     
-        // Distributing the mass
-        for(int trav = 0; trav<num && massRem>0; ++trav){
-
-            int result = 0;
-            float rVal = rnd();// between 0 and 1
-            float probSum = probDensity[0];
-            while(rVal>probSum){
-                probSum = probSum+probDensity[result+1];
+        // Testing
+        for(int globi = 0; globi<c; ++globi){
+            orxout() << "pDensity at [" << globi << "] is: " << probDensity[globi] << endl;
+        }
+
+        // Distributing the mass to individual asteroids
+        int result;
+        float rVal;// between 0 and 1
+        float probSum;
+        for(int trav = 0; trav<num; ++trav){
+            result = 0;// reset
+            rVal = rnd();// between 0 and 1
+            orxout() << "Random Value picked: " << rVal << endl;
+            probSum = probDensity[0]; 
+            //orxout() << "Sum at start: " << probSum << endl;
+
+            while(rVal>probSum && result<massRem){// Not yet found && there-s smth to distribute (Incrementing once inside!)
+                if(result<(num-2)){probSum = probSum+probDensity[result+1];} // avoid logical/acess error
                 ++result;
+                //orxout() << "Sum so far: " << probSum << endl;
             }
-            if(result>massRem){result = massRem;}
 
             massRem = massRem-result;
-            masses[trav] = masses[trav]+result; 
-
-        }
-    }
+            masses[trav] = 1 +result; // at least one 
+            orxout() << "Mass chosen for child " << trav << " is: " << masses[trav] << endl;
+
+        }
+    }else{// Everyone has mass 1. Initialising the array to 1 doesn-t seem to work. Hideous C language!
+        for(int schnaegg = 0; schnaegg<num; ++schnaegg){
+            masses[schnaegg] = 1;
+        }
+    }
+
+    //orxout() << "SpawnChildren(): Masses: "; printArrayString(masses);
+    orxout() << "SpawnChildren(): Passed mass stuff. " << endl;
+
+    // Creating the 'chlidren':
+    for(int fisch = 0; fisch<num; ++fisch){
+
+        Vector3* pos = new Vector3(0,0,0); // Position offset
+        if(num > 1){// not required if there-s just one child
+            float r = masses[fisch]/rScaling;
+            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
+        }
         
-    orxout() << "SpawnChildren(): passed Mass stuff. " << endl;
-
-    for(int fisch = 0; fisch<num; ++fisch){// create the children
-
-        // Position offset:
-        float r = masses[fisch]/rScaling;
-        Vector3* pos = new Vector3(r*sin(theta[fisch])*cos(phi[fisch]), r*sin(theta[fisch])*sin(phi[fisch]), r*cos(theta[fisch]));
-
+        //orxout() << "Creating asteroid with mass " << masses[fisch] << " at relative postition " << *pos << endl;
         AsteroidMinable* child = new AsteroidMinable(this->context, masses[fisch], this->getPosition() + *pos);
 
@@ -476,6 +496,54 @@
     }
 
-
-
+    void AsteroidMinable::printArrayString(float thingy[]){ // Don-t work!
+
+        orxout() << "[" ; //<< endl;  
+        char frag[] = "";
+        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
+        for(int m = 0; m< (len-2); ++m){
+            sprintf(frag, "%.5f, ", thingy[m]);
+            orxout() << frag << endl;//std::flush;
+        }
+        sprintf(frag, "%.5f]", thingy[len-1]);
+        orxout() << frag << endl; // Just print it here! No ugly passing. 
+    }
+
+    void AsteroidMinable::printArrayString(int thingy[]){
+
+        orxout() << "[" ; //<< endl; 
+        char frag[] = "";
+        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
+        for(int m = 0; m< (len-2); ++m){
+            sprintf(frag, "%.0i, ", thingy[m]);
+            orxout() << frag << endl;//std::flush;
+            printf("TEst");
+        }
+
+        sprintf(frag, "%.0i]", thingy[len-1]); // last element
+        orxout() << frag << endl; // Just print it here! No ugly passing. 
+    }
+
+
+    // void AsteroidMinable::printArrayString(int thingy[]){
+
+    //     char res[] = "[";
+    //     //strcat(res, "[");
+    //     char frag[] = "";
+
+    //     int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
+    //     for(int m = 0; m< (len-1); ++m){
+    //         sprintf(frag, "%.0i, ", thingy[m]);
+    //         strcat(res, frag);
+    //     }
+    //     sprintf(frag, "%.0i]", thingy[len]);
+    //     strcat(res, frag); // last element
+
+    //     orxout() << res << endl; // Just print it here! No ugly passing. 
+
+    //     // static char result[(sizeof(res)/sizeof("")] = res; // define as static, would get deleted otherwise. 
+    //     // char *result = malloc(sizeof(res)/sizeof("") + 1); 
+    //     // *result = res;
+    //     // return result;
+    // }
 
 
