Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/AsteroidMining_HS17/src/modules/asteroidmining/AsteroidMinable.cc @ 11646

Last change on this file since 11646 was 11641, checked in by remartin, 7 years ago

Changed detail in variable sized Array definition, should now build on Jenkins, too.

File size: 19.8 KB
Line 
1
2 /*   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Fabian 'x3n' Landau
24 *   Co-authors:
25 *      Simon Miescher
26 *
27 */
28
29/*
30
31*
32*
33* An asteroid which can be destroyed. Some smaller asteroids are created and a pickup
34* spawns.
35*
36*
37*
38
39*/
40
41/*
42Veraenderungstagebuch
43++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
44
45
46KNACKPUNKTE:
47
48OFFEN:
49
50o Add custom pickup 'resources'. Weird template stuff -> Problems setting 'size' and other properties? setNumber of Resources.
51--> PickupTemplateName_ in PickupSpawner ist ein string. Wird via getBaseClassIdentifier ausgelesen, daraus wird ein Pickupable fabriziert.
52--> MunitionPickup erbt von Pickup erbt von collectiblePickup erbt von Pickupable
53----> im MineralsPickup die isPickedUp()-Methode überschreiben?
54--> data_extern/images/effects: PNG's für die Pickups und GUI-Dinger.
55--> https://www.orxonox.net/jenkins/view/Management/job/orxonox_doxygen_trunk/javadoc/group___pickup.html
56
57o Anfangsgeschwindigkeit fuers Asteroidenfeld
58o Density doesn't add up to 1...
59o set collisionDamage? Just for static entities?
60o AsteroidBelt?
61
62o Dokumentieren mit @brief?
63o unregister -empty- asteroids from radar. (or turn them green or whatever)
64o Add sound effect (crunching etc. ) (No sound in space...)
65o Explosion parts
66o custom HUD
67
68
69HANDBUCH:
70o im Level-File includes/pickups.oxi importieren.
71o Bei der XML-Variante wird beim ersten Aufruf der Tick-Methode ein neuer Asteroid generiert,
72  damit die Groesse der Collision Shape korrekt initialisiert wird.
73
74FREMDANPASSUNGEN:
75Pickup-Zeug:
76o Pickup.h: Zugriffsänderung createSpawner
77o PickupSpawner.h: Zugriffsrechte setPickupTemplateName() und setMaxSpawnedItems()
78o PickupSpawner.h: In Tick() zwei Testbedingungen eingefuegt.
79o Pawn.h: Attribut acceptsPickups_ inklusive get/set.
80
81ERLEGTE FEHLER:
82o Rand() geht bis zu riesigen Nummern!
83o Pickupgenerierung: vgl. Fremdanpassungen.
84o Minimalbegrenzung fuer Masse vergessen.
85o Dynamische Definition -> putStuff-Methode, Werte noch nicht durchgesickert.
86o Error-Nachricht: Einfach Argumente leer lassen.
87o Pickups: setMaxSpawned funktioniert nicht -> Bastelloesung: Auf 2 statt eins setzen, erstes wird wohl direkt zerstoert.
88o setHealth: Wird mit Max verwurstelt, war 0.
89o Position nicht gesetzt -> alles im Ursprung, Kollision -> fliegt davon.
90o nimmt zuviel Schaden. -> wird mit maxHealth verwurstelt -> einfach auch setzen.
91
92o Groessenabhaengige Collison shape: Umweg ueber zweiten Konstruktor.
93o Absturz beim Asteroidengenerieren: Health war auf 0 gesetzt! Wurde nur bei der xml-Variante definiert.
94o Asteroiden fressen Pickups: Argument in Pawn, Test darauf in Tick() von PickupSpawner.
95o Man kann keine Arrays der Groesse 0 initialisieren, aber auch nicht per IF 2x definieren.
96o i++ einfach ganz verhindern, ++i stattdessen.
97o Discusting mixup with array length, accessing element a[len]...
98o unitialised mass value -> obese asteroid, physics bug.
99
100
101NOTIZEN:
102o SUPER entspricht ueberladen, andere Argumente der Methode.
103o Warnungsverhinderung anderswo: (void)pickedUp; // To avoid compiler warning.
104
105Was ist das?         friend class Pickupable;
106
107
108
109*/
110
111
112#include "../../orxonox/worldentities/pawns/Pawn.h"
113#include "../../orxonox/worldentities/WorldEntity.h"
114
115#include "AsteroidMinable.h"
116
117#include <algorithm>
118
119#include "core/CoreIncludes.h"
120#include "core/GameMode.h"
121#include "core/XMLPort.h"
122#include "core/EventIncludes.h"
123#include "network/NetworkFunction.h"
124#include "util/Math.h"
125
126
127// #include "infos/PlayerInfo.h"
128// #include "controllers/Controller.h"
129// #include "gametypes/Gametype.h"
130// #include "graphics/ParticleSpawner.h"
131// #include "worldentities/ExplosionChunk.h"
132// #include "worldentities/ExplosionPart.h"
133
134// #include "core/object/ObjectListIterator.h"
135// #include "controllers/FormationController.h"
136
137#include "../pickup/items/HealthPickup.h"
138#include "../pickup/PickupSpawner.h"
139#include "../pickup/Pickup.h"
140//#include "../pickup/pickup ..... pickupable
141#include "../objects/collisionshapes/SphereCollisionShape.h"
142#include "../../orxonox/graphics/Model.h"
143
144
145
146
147
148
149
150namespace orxonox
151{
152    RegisterClass(AsteroidMinable);
153
154    // @brief Standard constructor
155    AsteroidMinable::AsteroidMinable(Context* context) : Pawn(context){
156
157        RegisterObject(AsteroidMinable);
158
159        this->context = context;
160        this->initialised = false;
161        this->dropStuff = true; // Default
162
163        //Noetig, damit nicht sofort zerstoert?
164        this->setCollisionType(WorldEntity::CollisionType::Dynamic);
165
166        // Old from Pawn
167        this->registerVariables();
168
169        //orxout() << "AsteroidMining:: Pseudo-Konstruktor passiert!" << endl;
170
171    }
172
173    // @brief Use this constructor with care. Mainly used internally, arguments are passed directly.
174    AsteroidMinable::AsteroidMinable(Context* c, float size, Vector3 position, Vector3 v, bool dropStuff) : Pawn(c){
175
176        RegisterObject(AsteroidMinable);
177
178        // The radar is able to detect whether an asteroid contains resources....
179        if(dropStuff){
180            this->setRadarObjectColour(ColourValue(1.0f, 1.0f, 0.0f, 1.0f));
181            this->setRadarObjectShape(RadarViewable::Shape::Dot);
182        }else{
183            // Somehow remove from radar?
184        }
185
186        this->setCollisionType(WorldEntity::CollisionType::Dynamic);
187        this->enableCollisionCallback();
188
189        // Default Values
190        this->context = c;
191        this->size = size;
192        this->health_ = 15*size; 
193        this->maxHealth_ = this->health_;
194        this->acceptsPickups_ = false; 
195        this->generateSmaller = true; 
196        this->dropStuff = dropStuff; 
197
198        this->setPosition(position);
199        this->setVelocity(v); // velocity = v; // The right one?
200        //this->roll = rand()*5; //etwas Drehung. richtige Variable
201
202
203        // Add Model    //<Model position="0,-40,40" yaw="90" pitch="-90" roll="0" scale="4" mesh="ast6.mesh" />
204        Model* hull = new Model(this->context);
205        // random one of the 6 shapes
206        char meshThingy[] = "";
207        sprintf(meshThingy, "ast%.0f.mesh", round(5*rnd())+1); //    sprintf(str, "Value of Pi = %f", M_PI);
208        hull->setMeshSource(meshThingy);
209        hull->setScale(this->size);
210        this->attach(hull);
211
212        // Collision shape
213        SphereCollisionShape* cs = new SphereCollisionShape(this->context);
214        cs->setRadius((this->size)*2); //OFFEN: Feinabstimmung der Radien. 2.5 in AsteroidField.lua
215        this->attachCollisionShape(cs); 
216
217        // Old from Pawn
218        this->registerVariables();
219
220        this->initialised=true; 
221
222        //orxout() << "AsteroidMining:: Initialisierung Zweitkonstruktor abgeschlosssssen." << endl;
223
224    }
225
226    AsteroidMinable::~AsteroidMinable(){
227
228    }
229    // @brief Helper method. Create a new asteroid to have an appropriate size for the collision shape etc.
230    void AsteroidMinable::putStuff(){
231
232        // Just create a new asteroid to avoid Collision shape scale problems etc.
233        AsteroidMinable* reborn = new AsteroidMinable(this->context, this->size, this->getPosition(), this->getVelocity(), this->dropStuff);
234        reborn->toggleShattering(true); // mainly here to avoid 'unused' warning.
235        this->bAlive_ = false;
236        this->destroyLater();
237        //this->~AsteroidMinable(); // seems dangerous, yields warning.  Necessary for efficiency?
238
239    }
240
241    void AsteroidMinable::XMLPort(Element& xmlelement, XMLPort::Mode mode)
242    {
243        SUPER(AsteroidMinable, XMLPort, xmlelement, mode);
244        //        XMLPortParam(PickupSpawner, "pickup", setPickupTemplateName, getPickupTemplateName, xmlelement, mode);
245        XMLPortParam(AsteroidMinable, "size", setSize, getSize, xmlelement, mode);
246
247    }
248
249    void AsteroidMinable::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
250    {
251        SUPER(AsteroidMinable, XMLEventPort, xmlelement, mode);
252
253        XMLPortEventState(AsteroidMinable, BaseObject, "vulnerability", setVulnerable, xmlelement, mode);
254    }
255
256    void AsteroidMinable::registerVariables()
257    {
258
259        registerVariable(this->size, VariableDirection::ToClient);
260        registerVariable(this->generateSmaller, VariableDirection::ToClient);
261
262        registerVariable(this->initialised, VariableDirection::ToClient);
263
264            //         float size;
265            // bool generateSmaller;
266            // bool initialised;
267
268            // Context* context;
269    }
270
271    void AsteroidMinable::tick(float dt)
272    {
273        if(!(this->initialised)){this->putStuff();} 
274
275        if(this->health_ <=0){this->death();}
276
277    }
278
279
280
281
282    void AsteroidMinable::death() //ueberschreiben
283    {
284
285        // orxout() << "AsteroidMinable::Death() aufgerufen." << endl;
286
287        // OFFEN: Sauber kapputten
288        // just copied other stuff:
289        // this->setHealth(1);
290        this->bAlive_ = false;
291        this->destroyLater();
292        this->setDestroyWhenPlayerLeft(false);
293        // pawn -> addExplosionPart
294        // this->goWithStyle();
295
296
297        // Pickups which can be harvested.
298        if(dropStuff){
299            PickupSpawner* thingy = new PickupSpawner(this->context);
300            // OFFEN: more precise size relation in custom resource pickup.
301            char tname[] = ""; // can-t overwrite strings easily in C (strcat etc.)
302            if(this->size <= 5){
303                strcat(tname, "smallmunitionpickup");
304            }else if(this->size <= 20){
305                strcat(tname, "mediummunitionpickup");
306            }else{
307                strcat(tname, "hugemunitionpickup");
308            }
309            thingy->setPickupTemplateName(tname);
310            thingy->setPosition(this->getPosition());
311            thingy->setMaxSpawnedItems(1); // Would be default anyways
312            thingy->setRespawnTime(0.2f);
313        }
314        // orxout() << "AsteroidMining::Death(): Passed Pickup stuff!" << endl;
315
316        // Smaller Parts = 'Children'
317        if(this->generateSmaller){this->spawnChildren();}
318
319        // orxout() << "Wieder retour in death() geschafft. " << endl;
320
321    }
322
323
324void AsteroidMinable::spawnChildren(){// Spawn smaller Children
325
326   
327    if (this->size <=1){return;} // Absicherung trivialer Fall
328
329    int massRem = this->size-1; //some mass is lost
330    int num = round(rnd()*(massRem-1)) + 1; // random number of children, at least one
331    if(num > 10){num = 10;} // no max function in C!
332    int masses[num]; // Masses of the asteroids, at least one.
333    //orxout() << "SpawnChildren(): Passed basic stuff. num = " << num << "; massRem(total) = "<< massRem << endl;
334
335    massRem = massRem-num; // mass is at least one, add again below.
336
337    // Randomnised spawning points for the new asteroids
338    float phi[num]; // assuming that it gets initialised to 0. Add (= {0.0})?
339    float theta[num];
340    float piG = 3.1415927410125732421875; //pi; // Math.pi ist statisch oder so.
341
342    float d_p = 2*piG/num;
343    float d_t = piG/num;
344    float p = d_p/2.0;
345    float t = d_t/2.0;
346    // float phiOffset = rnd()*2*pi; // Added everywhere to become independent of the coordinate system?
347    // float thetaOffset = rnd()*pi;
348    float rScaling; // scale radius to prevent asteroids from touching. (distance=AsteroidRadius/tan(sector/2))
349    if(num == 1 ){
350        rScaling = 1; // avoid tan(90). Unused.
351    }else{
352
353        rScaling = tan(t); 
354
355        int pos; // insert at random position (linear probing) in array, to get some randomness. 
356        for(int it = 0; it<num; ++it){
357
358            pos = mod((int)(rnd()*num),num); 
359            while(phi[pos] != 0.0){// find empty spot in array
360                pos = (int)mod(++pos, num);
361            }
362            phi[pos] = p + it*d_p;// set angle there
363
364            pos = mod((int)(rnd()*num),num);
365            while(theta[pos] != 0.0){
366                pos = (int)mod(++pos, num);
367            }
368            theta[pos] = t + it*d_t;
369        }
370    }
371
372    //orxout() << "SpawnChildren(): Phi: "; printArrayString(phi);
373    //orxout() << "SpawnChildren(): Theta: "; printArrayString(theta);
374    //orxout() << "SpawnChildren(): Passed angle stuff. " << endl;
375
376    // 'Triangular', discrete probability "density" with max at the expected value massRem/num at a. a+b = c
377    if(massRem>0){ // Required to avoid array of size 0 or access problems
378        int c = massRem;
379        float probDensity[c];
380
381        int a = round(massRem/num);
382        int b = c-a;
383       
384        int z = 0;
385        float dProbA = 1.0/(a*a + 3.0*a + 2.0); // one 'probability unit' for discrete ramp function. Gauss stuff.
386        for(z = 0; z<=a; ++z){probDensity[z] = (z+1)*dProbA; } // rising part
387
388        float dProbB = 1.0/(b*b +3.0*b + 2.0);
389        for(z = 0; z<b; ++z){probDensity[c-z] = (z+1)*dProbB;} // falling part
390   
391        // // Just for testing:
392        // float sum = 0.0;
393        // for(int globi = 0; globi<c; ++globi){
394        //     orxout() << "pDensity at [" << globi << "] is: " << probDensity[globi] << endl;
395        //     sum = sum+ probDensity[globi];
396        // }
397        // orxout() << "Sum of densities should b 1, it is: " << sum << endl;
398
399        // Distributing the mass to individual asteroids
400        int result;
401        float rVal;// between 0 and 1
402        float probSum;
403        for(int trav = 0; trav<num; ++trav){
404            result = 0;// reset
405            rVal = rnd();// between 0 and 1
406            // orxout() << "Random Value picked: " << rVal << endl;
407            probSum = probDensity[0]; 
408            //orxout() << "Sum at start: " << probSum << endl;
409
410            while(rVal>probSum && result<massRem){// Not yet found && there-s smth to distribute (Incrementing once inside!)
411                if(result<(massRem-2)){probSum = probSum + probDensity[result+1];} // avoid logical/acess error
412                ++result;
413                // orxout() << "Sum so far: " << probSum << ". Just added " << probDensity[result+1] <<  endl;
414            }
415
416            massRem = massRem-result;
417            masses[trav] = 1 +result; // at least one
418            // orxout() << "Mass chosen for child " << trav << " is: " << masses[trav] << endl;
419
420        }
421    }else{// Everyone has mass 1. Initialising the array to 1 doesn-t seem to work. Hideous C language!
422        for(int schnaegg = 0; schnaegg<num; ++schnaegg){
423            masses[schnaegg] = 1;
424        }
425    }
426
427    // orxout() << "SpawnChildren(): Masses: "; printArrayString(masses);
428    // orxout() << "SpawnChildren(): Passed mass stuff. " << endl;
429
430    // Creating the 'chlidren':
431    for(int fisch = 0; fisch<num; ++fisch){
432
433        Vector3* pos = new Vector3(0,0,0); // Position offset
434        if(num > 1){// not required if there-s just one child
435            float r = masses[fisch]/rScaling;
436            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
437        }
438       
439        // orxout() << "Creating asteroid with mass " << masses[fisch] << " at relative postition " << *pos << endl;
440        AsteroidMinable* child = new AsteroidMinable(this->context, masses[fisch], this->getPosition() + *pos, this->getVelocity(), this->dropStuff);
441
442        if(child == nullptr){
443            orxout(internal_error, context::pickups) << "Weird, can't create new AsteroidMinable." << endl;
444        }
445
446    }
447    // orxout() << "Leaving spawnChildren() method. " << endl;
448}
449
450
451    void AsteroidMinable::hit(Pawn* originator, const Vector3& force, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage){
452
453        orxout() << "AsteroidMining::Hit(Variante 1) Dings aufgerufen. " << endl;
454
455        // Kollision mit anderem Asteroid oder Pickup verhindern. In diesem Fall einfach nichts tun.
456        // Wird staending aufgerufen -> Rechenleistung?
457        if(orxonox_cast<AsteroidMinable*>(originator) || orxonox_cast<Pickup*>(originator)){return;}
458        this->damage(damage, healthdamage, shielddamage, originator, cs);
459        this->setVelocity(this->getVelocity() + force);
460
461
462
463        // if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))// && (!this->getController() || !this->getController()->getGodMode()) )
464        // {
465        //     this->damage(damage, healthdamage, shielddamage, originator, cs);
466        //     this->setVelocity(this->getVelocity() + force);
467        // }
468    }
469
470    void AsteroidMinable::hit(Pawn* originator, btManifoldPoint& contactpoint, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage){
471
472        //orxout() << "AsteroidMining::Hit(Variante 2) Dings aufgerufen. " << endl;
473        // Kollision mit anderem Asteroid oder Pickup (OFFEN: evtl. Spawner oder irgendwas?) verhindern. In diesem Fall einfach nichts tun.
474        // Wird staending aufgerufen -> Rechenleistung?
475        if(orxonox_cast<AsteroidMinable*>(originator) || orxonox_cast<Pickup*>(originator)){return;}
476
477        //orxout() << "Schaden. HP: " << this->health_ << " Dmg: " << damage << " hDmg: " << healthdamage << " sDmg: " << shielddamage << endl;
478
479        this->damage(damage, healthdamage, shielddamage, originator, cs);
480
481
482
483
484        // if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))// && (!this->getController() || !this->getController()->getGodMode()) )
485        // {
486        //     this->damage(damage, healthdamage, shielddamage, originator, cs);
487
488        //     //if ( this->getController() )
489        //     //    this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage?
490        // }
491    }
492
493    // @brief Just for testing. Don-t work anyways.
494    void AsteroidMinable::printArrayString(float thingy[]){ // Don-t work!
495
496        orxout() << "[" ; //<< endl; 
497        char frag[] = "";
498        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
499        for(int m = 0; m< (len-2); ++m){
500            sprintf(frag, "%.5f, ", thingy[m]);
501            orxout() << frag << endl;//std::flush;
502        }
503        sprintf(frag, "%.5f]", thingy[len-1]);
504        orxout() << frag << endl; // Just print it here! No ugly passing.
505    }
506
507    // @brief Just for testing. Don-t work anyways.
508    void AsteroidMinable::printArrayString(int thingy[]){
509
510        orxout() << "[" ; //<< endl;
511        char frag[] = "";
512        int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
513        for(int m = 0; m< (len-2); ++m){
514            sprintf(frag, "%.0i, ", thingy[m]);
515            orxout() << frag << endl;//std::flush;
516            printf("TEst");
517        }
518
519        sprintf(frag, "%.0i]", thingy[len-1]); // last element
520        orxout() << frag << endl; // Just print it here! No ugly passing.
521    }
522
523    // void AsteroidMinable::printArrayString(int thingy[]){
524    //     char res[] = "[";
525    //     //strcat(res, "[");
526    //     char frag[] = "";
527
528    //     int len = (int)(sizeof(thingy)/sizeof(thingy[0]));
529    //     for(int m = 0; m< (len-1); ++m){
530    //         sprintf(frag, "%.0i, ", thingy[m]);
531    //         strcat(res, frag);
532    //     }
533    //     sprintf(frag, "%.0i]", thingy[len]);
534    //     strcat(res, frag); // last element
535
536    //     orxout() << res << endl; // Just print it here! No ugly passing.
537
538    //     // static char result[(sizeof(res)/sizeof("")] = res; // define as static, would get deleted otherwise.
539    //     // char *result = malloc(sizeof(res)/sizeof("") + 1);
540    //     // *result = res;
541    //     // return result;
542    // }
543
544}
Note: See TracBrowser for help on using the repository browser.