/* ORXONOX - the hottest 3D action shooter ever to exist * > www.orxonox.net < * * * License notice: * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. * * Author: * Fabian 'x3n' Landau * Co-authors: * Simon Miescher * */ /* @file @author remartin @brief An asteroid which can be destroyed. Some smaller asteroids are created and a pickup spawns. HANDBUCH: o Die Collision Shape kann nur im Konstruktor hinzugefügt werden. Die XML-Argumente werden aber erst nach dem Konstruktor gesetzt. Darum wird hier beim ersten Aufruf der tick()-Methode via putStuff() ein komplett neuer Asteroid generiert und der alte zerstört. o im Level-File includes/pickups.oxi importieren. OFFEN/Weiterentwicklung: o @TODO Add resource pickups. --> data_extern/images/effects: PNG's für die Pickups --> https://www.orxonox.net/jenkins/view/Management/job/orxonox_doxygen_trunk/javadoc/group___pickup.html o Density doesn't add up to 1... o Does collision damage work properly o Add sound effect (crunching etc. ) (No sound in space...) o Explosion parts ANDERORTS VERÄNDERTE SACHEN: Pickup-Zeug: o Pickup.h: createSpawner() neu public statt private o PickupSpawner.h: Zugriffsrechte setPickupTemplateName() und setMaxSpawnedItems() o PickupSpawner.h: In Tick() zwei Testbedingungen eingefügt. o Pawn.h: Attribut acceptsPickups_ inklusive get/set. ERLEGTE FEHLER: o Grössenabhängige Collision Shape -> putStuff-Methode, Werte noch nicht durchgesickert. o setHealth: maxHealth() des pawns setzen! o Asteroiden fressen Pickups: Argument in Pawn, Test darauf in Tick() von PickupSpawner. o i++ einfach ganz verhindern, ++i stattdessen. o Velocity didn-t get passed properly through the 2nd constructor. Used get/set instead. o Rand() geht bis zu riesigen Nummern! rnd() ist zwischen 0 und 1 NOTIZEN: o SUPER o Warnungsverhinderung anderswo: (void)pickedUp; // To avoid compiler warning. o friend class Pickupable; */ #include "../../orxonox/worldentities/pawns/Pawn.h" #include "../../orxonox/worldentities/WorldEntity.h" #include "AsteroidMinable.h" #include #include "core/CoreIncludes.h" #include "core/GameMode.h" #include "core/XMLPort.h" #include "core/EventIncludes.h" #include "network/NetworkFunction.h" #include "util/Math.h" #include "../pickup/PickupSpawner.h" #include "../pickup/Pickup.h" #include "../objects/collisionshapes/SphereCollisionShape.h" #include "../../orxonox/graphics/Model.h" namespace orxonox{ RegisterClass(AsteroidMinable); // @brief Standard constructor AsteroidMinable::AsteroidMinable(Context* context) : Pawn(context){ RegisterObject(AsteroidMinable); this->context = context; // Default Values: this->size = 10; this->dropStuff = true; this->generateSmaller = true; this->health_ = 15*size; this->maxHealth_ = this->health_; this->acceptsPickups_ = false; this->setCollisionType(WorldEntity::CollisionType::Dynamic); this->enableCollisionCallback(); // Old from Pawn this->registerVariables(); this->initialised = false; } // @brief Use this constructor with care. Mainly used internally, arguments are passed directly. AsteroidMinable::AsteroidMinable(Context* c, float size, Vector3 position, bool dropStuff) : Pawn(c){ RegisterObject(AsteroidMinable); // The radar is able to detect whether an asteroid contains resources.... if(dropStuff){ this->setRadarObjectColour(ColourValue(1.0f, 1.0f, 0.0f, 1.0f)); this->setRadarObjectShape(RadarViewable::Shape::Dot); }else{ // Somehow remove from radar? (all pawns get registered automatically... ) this->setRadarObjectColour(ColourValue(0.663f, 0.663f, 0.663f, 1.0f)); this->setRadarObjectShape(RadarViewable::Shape::Dot); } this->context = c; this->size = size; this->health_ = 15*size; this->maxHealth_ = this->health_; this->acceptsPickups_ = false; this->generateSmaller = true; this->dropStuff = dropStuff; this->setPosition(position); //this->roll = rand()*5; //etwas Drehung. Richtige Variable? this->setCollisionType(WorldEntity::CollisionType::Dynamic); this->enableCollisionCallback(); // Add Model, random one of the 6 shapes Model* hull = new Model(this->context); char meshThingy[] = ""; sprintf(meshThingy, "ast%.0f.mesh", round(5*rnd())+1); hull->setMeshSource(meshThingy); hull->setScale(this->size); this->attach(hull); // Collision shape SphereCollisionShape* cs = new SphereCollisionShape(this->context); cs->setRadius((this->size)*2); //OFFEN: Feinabstimmung der Radien. this->attachCollisionShape(cs); this->registerVariables(); this->initialised=true; } AsteroidMinable::~AsteroidMinable(){ } // @brief Helper method. void AsteroidMinable::putStuff(){ AsteroidMinable* reborn = new AsteroidMinable(this->context, this->size, this->getPosition(), this->dropStuff); reborn->setVelocity(this->getVelocity()); // reborn->setAngularVelocity(this->getAngularVelocity()); // Add all other stuff, too? (void)reborn; // avoid compiler warning this->bAlive_ = false; this->destroyLater(); } void AsteroidMinable::XMLPort(Element& xmlelement, XMLPort::Mode mode){ SUPER(AsteroidMinable, XMLPort, xmlelement, mode); XMLPortParam(AsteroidMinable, "size", setSize, getSize, xmlelement, mode); XMLPortParam(AsteroidMinable, "generateSmaller", toggleShattering, doesShatter, xmlelement, mode); XMLPortParam(AsteroidMinable, "dropStuff", toggleDropStuff, doesDropStuff, xmlelement, mode); } void AsteroidMinable::XMLEventPort(Element& xmlelement, XMLPort::Mode mode){ SUPER(AsteroidMinable, XMLEventPort, xmlelement, mode); } void AsteroidMinable::registerVariables(){ registerVariable(this->size, VariableDirection::ToClient); registerVariable(this->generateSmaller, VariableDirection::ToClient); registerVariable(this->dropStuff, VariableDirection::ToClient); registerVariable(this->initialised, VariableDirection::ToClient); } void AsteroidMinable::tick(float dt){ if(!(this->initialised)){this->putStuff();} if(this->health_ <=0){this->death();} } void AsteroidMinable::death(){ // @brief Überschreibt die Methode in Pawn // just copied that from somewhere else. this->bAlive_ = false; this->destroyLater(); this->setDestroyWhenPlayerLeft(false); // pawn -> addExplosionPart // this->goWithStyle(); // Pickups which can be harvested. It's munition at the moment, could be changed/extended. if(dropStuff){ PickupSpawner* thingy = new PickupSpawner(this->context); char tname[] = ""; // can-t overwrite strings easily in C (strcat etc.) if(this->size <= 5){ strcat(tname, "smallmunitionpickup"); }else if(this->size <= 20){ strcat(tname, "mediummunitionpickup"); }else{ strcat(tname, "hugemunitionpickup"); } thingy->setPickupTemplateName(tname); thingy->setPosition(this->getPosition()); thingy->setMaxSpawnedItems(1); // Would be default anyways thingy->setRespawnTime(0.2f); } // Smaller Parts = 'Children' if(this->generateSmaller){this->spawnChildren();} } // @brief If the option generateSmaller is enabled, individual fragments are added with this method. void AsteroidMinable::spawnChildren(){ if (this->size <=1){return;} // Absicherung trivialer Fall int massRem = this->size-1; //some mass is lost 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]; // Masses of the asteroids // orxout() << "SpawnChildren(): Passed basic stuff. num = " << num << "; massRem(total) = "<< massRem << endl; massRem = massRem-num; // mass must be at least one, add later. // Randomnised spawning points for the new asteroids float phi[num]; float theta[num]; // Discusting C stuff -> use that to initialise dynamic array values to 0. for(int twat = 0; twat0){ int c = massRem; float probDensity[c]; int a = round(massRem/num); int b = c-a; int z = 0; 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; zprobSum && result 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 } AsteroidMinable* child = new AsteroidMinable(this->context, masses[fisch], this->getPosition() + *pos, this->dropStuff); child->setVelocity(this->getVelocity()); if(child == nullptr){ orxout(internal_error, context::pickups) << "Weird, can't create new AsteroidMinable." << endl; } } // orxout() << "Leaving spawnChildren() method. " << endl; } // @brief overloading that to prevent asteroids from taking damage from each other (domino effect etc. ) void AsteroidMinable::hit(Pawn* originator, const Vector3& force, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage){ // orxout() << "AsteroidMining::Hit(Variante 1) Dings aufgerufen. " << endl; if(orxonox_cast(originator) || orxonox_cast(originator)){return;} this->damage(damage, healthdamage, shielddamage, originator, cs); this->setVelocity(this->getVelocity() + force); // if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))// && (!this->getController() || !this->getController()->getGodMode()) ) // { // this->damage(damage, healthdamage, shielddamage, originator, cs); // this->setVelocity(this->getVelocity() + force); // } } // @brief overloading that to prevent asteroids from taking damage from each other (domino effect etc. ) void AsteroidMinable::hit(Pawn* originator, btManifoldPoint& contactpoint, const btCollisionShape* cs, float damage, float healthdamage, float shielddamage){ //orxout() << "AsteroidMining::Hit(Variante 2) Dings aufgerufen. " << endl; if(orxonox_cast(originator) || orxonox_cast(originator)){return;} this->damage(damage, healthdamage, shielddamage, originator, cs); // if (this->getGametype() && this->getGametype()->allowPawnHit(this, originator))// && (!this->getController() || !this->getController()->getGodMode()) ) // { // this->damage(damage, healthdamage, shielddamage, originator, cs); // //if ( this->getController() ) // // this->getController()->hit(originator, contactpoint, damage); // changed to damage, why shielddamage? // } } // @brief Just for testing. Don-t work anyways. 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. } // @brief Just for testing. Don-t work anyways. 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; // } }