Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11561 was 11561, checked in by remartin, 6 years ago

Pickup generating works now after changing privacy settings of set methods in PickupSpawner.h. Added lots of debugging messages, spawnChildren does lead to a crash, still.

File size: 12.1 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
45KNACKPUNKTE:
46o Collision Shape-Problem (Funktioniert nur, wenn im Konstruktor)
47o Maximale Hitpunkte = 200?
48
49o Neue Asteroiden generieren -> Absturz
50---> Weil Elterndings zerstoert wird?
51---> Nicht moeglich, zur Laufzeit zu generieren?
52
53o Pickups: setMaxSpawned funktioniert nicht
54
55
56OFFEN:
57o Add custom pickup 'resources'. Weird template stuff -> Problems setting 'size' and other properties? setNumber of Resources.
58o Explosion parts
59o custom HUD
60
61HANDBUCH:
62o im Level-File includes/pickups.oxi importieren.
63
64Anpassungen Pickup-Zeug:
65o Pickup.h: Zugriffsänderung createSpawner
66o PickupSpawner.h: Zugriffsrechte setPickupTemplateName() und setMaxSpawnedItems()
67
68ERLEGT:
69o Rand() geht bis zu riesigen Nummern!
70o Pickupgenerierung: vgl. Anpassungen.
71o Dummes fisch-- statt fisch++ , Endlosschleife!
72o Dynamische Definition -> putStuff-Methode, Werte noch nicht durchgesickert.
73o Error-Nachricht: Einfach Argumente leer lassen.
74*/
75
76
77#include "../../orxonox/worldentities/pawns/Pawn.h"
78#include "../../orxonox/worldentities/WorldEntity.h"
79
80#include "AsteroidMinable.h"
81
82#include <algorithm>
83
84#include "core/CoreIncludes.h"
85#include "core/GameMode.h"
86#include "core/XMLPort.h"
87#include "core/EventIncludes.h"
88#include "network/NetworkFunction.h"
89
90// #include "infos/PlayerInfo.h"
91// #include "controllers/Controller.h"
92// #include "gametypes/Gametype.h"
93// #include "graphics/ParticleSpawner.h"
94// #include "worldentities/ExplosionChunk.h"
95// #include "worldentities/ExplosionPart.h"
96
97// #include "core/object/ObjectListIterator.h"
98// #include "controllers/FormationController.h"
99
100#include "../pickup/items/HealthPickup.h"
101#include "../pickup/PickupSpawner.h"
102#include "../pickup/Pickup.h"
103//#include "../pickup/pickup ..... pickupable
104#include "../objects/collisionshapes/SphereCollisionShape.h"
105#include "../../orxonox/graphics/Model.h"
106
107
108
109
110
111
112namespace orxonox
113{
114    RegisterClass(AsteroidMinable);
115
116    AsteroidMinable::AsteroidMinable(Context* context) : Pawn(context)
117    {
118        RegisterObject(AsteroidMinable);
119
120        // Old stuff from pawn
121        this->setRadarObjectColour(ColourValue(1.0f, 1.0f, 0.0f, 1.0f));
122        this->setRadarObjectShape(RadarViewable::Shape::Dot);
123        this->setCollisionType(WorldEntity::CollisionType::Dynamic);
124        this->bAlive_ = true;
125        this->bVulnerable_ = true;
126
127        // Default Values
128        this->generateSmaller = true;
129        this->size = 10; // customSize
130        this->context = context;
131        this->initialised = false;
132        //this->roll = rand()*5; //etwas Drehung. richtige Variable
133
134
135
136
137        // DELETE if other stuff works! (wrong size etc.)
138        SphereCollisionShape* cs = new SphereCollisionShape(this->context);
139        cs->setRadius((this->size)*2); //OFFEN: Feinabstimmung der Radien
140        this->attachCollisionShape(cs); 
141
142
143
144
145        // Old from Pawn
146        this->registerVariables();
147
148    }
149
150    AsteroidMinable::~AsteroidMinable()
151    {
152
153    }
154
155    void AsteroidMinable::putStuff(){
156
157        // Add Model
158        //<Model position="0,-40,40" yaw="90" pitch="-90" roll="0" scale="4" mesh="ast6.mesh" />
159        Model* hull = new Model(this->context);
160        // random one of the 6 shapes
161        char meshThingy[] = "";
162        sprintf(meshThingy, "ast%.0f.mesh", (round((5*(double)rand()/(double)RAND_MAX))+1)); //    sprintf(str, "Value of Pi = %f", M_PI);
163        hull->setMeshSource(meshThingy);
164        hull->setScale(this->size);
165        this->attach(hull);
166
167        // Collision shape
168        SphereCollisionShape* cs = new SphereCollisionShape(this->context);
169        cs->setRadius((this->size)*2); //OFFEN: Feinabstimmung der Radien. 2.5 in AsteroidField.lua
170        this->attachCollisionShape(cs); 
171
172
173        this->initialised=true;
174    }
175
176    void AsteroidMinable::XMLPort(Element& xmlelement, XMLPort::Mode mode)
177    {
178        SUPER(AsteroidMinable, XMLPort, xmlelement, mode);
179        //        XMLPortParam(PickupSpawner, "pickup", setPickupTemplateName, getPickupTemplateName, xmlelement, mode);
180        XMLPortParam(AsteroidMinable, "size", setSize, getSize, xmlelement, mode);
181
182    }
183
184    void AsteroidMinable::XMLEventPort(Element& xmlelement, XMLPort::Mode mode)
185    {
186        SUPER(AsteroidMinable, XMLEventPort, xmlelement, mode);
187
188        XMLPortEventState(AsteroidMinable, BaseObject, "vulnerability", setVulnerable, xmlelement, mode);
189    }
190
191    void AsteroidMinable::registerVariables()
192    {
193        registerVariable(this->bAlive_,            VariableDirection::ToClient);
194        registerVariable(this->bVulnerable_,       VariableDirection::ToClient);
195        registerVariable(this->health_,            VariableDirection::ToClient);
196        registerVariable(this->maxHealth_,         VariableDirection::ToClient);
197
198        registerVariable(this->size, VariableDirection::ToClient);
199        registerVariable(this->generateSmaller, VariableDirection::ToClient);
200
201        registerVariable(this->initialised, VariableDirection::ToClient);
202        //registerVariable(this->context, VariableDirection::ToClient); // can't link that since it's a context
203
204
205            //         float size;
206            // bool generateSmaller;
207            // bool initialised;
208
209            // Context* context;
210    }
211
212    void AsteroidMinable::tick(float dt)
213    {
214        // SUPER(Pawn, tick, dt);
215        if(!(this->initialised)){this->putStuff();}
216
217        if(this->health_ <=0){this->death();}
218
219    }
220
221    void AsteroidMinable::setSize(float s){
222        this->size = s;
223        this->health_ = 5*s;// capped at 200 in pawn or smth?
224        this->setHealth(health_);
225    }
226
227    float AsteroidMinable::getSize(){
228        return this->size;
229    }
230
231    void AsteroidMinable::death() //ueberschreiben
232    {
233
234        // Stuff that can be harvested.
235        PickupSpawner* thingy = new PickupSpawner(this->context);
236
237        // OFFEN: more precise size relation in custom resource pickup.
238        char tname[] = ""; // can-t overwrite strings easily in C (strcat etc.)
239        if(this->size <= 5){
240            strcat(tname, "smallmunitionpickup");
241        }else if(this->size <= 20){
242            strcat(tname, "mediummunitionpickup");
243        }else{
244            strcat(tname, "hugemunitionpickup");
245        }
246        //orxout() << "Zeugsname:" << tname << endl;
247        thingy->setPickupTemplateName(tname);
248        thingy->setPosition(this->getPosition());
249        //thingy->setMaxSpawnedItems(1); // Somehow doesn-t work yet.
250        thingy->setRespawnTime(0.1f);// instantly
251       
252        orxout() << "AsteroidMining::Death(): Passed Pickup stuff!" << endl;
253
254
255
256
257
258        // Smaller Parts = 'Children'
259        if(this->generateSmaller){
260            this->spawnChildren();
261        }
262
263
264
265
266
267        // OFFEN: Sauber kapputten
268        // just copied other stuff:
269        this->setHealth(1);
270        this->bAlive_ = false;
271        this->destroyLater();
272        this->setDestroyWhenPlayerLeft(false);
273
274
275        // pawn -> addExplosionPart
276        // this->goWithStyle();
277        orxout() << "Wieder retour in death() geschafft. " << endl;
278
279        // if (this->getGametype() && this->getGametype()->allowPawnDeath(this, this->lastHitOriginator_))
280        // {
281        //     // Set bAlive_ to false and wait for destroyLater() to do the destruction
282        //     this->bAlive_ = false;
283        //     this->destroyLater();
284
285        //     this->setDestroyWhenPlayerLeft(false);
286
287        // }
288    }
289
290
291void AsteroidMinable::spawnChildren(){
292   
293    // Spawn smaller Children
294    int massRem = this->size-1; //some mass is lost
295    int num = round((massRem-1)*(double)rand() / (double)RAND_MAX)+1; // random number of children, at least one // Tweak towards bigger junks?
296    num = 1; // zum Testen mal nur einen generieren.
297   
298    massRem = massRem-num;   
299    int extra = 0;
300
301
302    orxout() << "Number of Children: " << num << endl;
303
304
305    for(int fisch=num; fisch>=1; fisch--){
306        // to distribute remaining mass
307        if(fisch==1){ 
308            extra = massRem;
309        }else{
310            extra = round(massRem*(double)rand() / (double)RAND_MAX);
311            massRem = massRem-extra;
312
313        }
314
315        orxout() << "Mass chosen: " << extra+1 << endl;
316
317        orxout() << "AsteroidMining::spawnChildren(): Inside for-loop." << endl;
318
319        //Spawn this child  Game crashes!
320        AsteroidMinable* child = new AsteroidMinable(this->context);
321        // if(!(child == nullptr)){//Errorgebastel
322
323        // Position zu spaet gesetzt? Tick nicht atomar -> falsche Groesse evtl?
324
325            if(child == nullptr){
326                orxout(internal_error, context::pickups) << "Weird, can't create new AsteroidMinable." << endl;
327            }
328
329            child->setSize(extra + 1);
330               
331            //OFFEN:Kollision der Kinder verhindern
332            //Relativ zu Elternteil automatisch?
333            //Typ position:rand()*Vektoriwas?
334            //pawn->getWorldPosition() + Vector3(30,0,-30);
335            child->setPosition(this->getPosition() + Vector3(num*5, 0, 0));
336
337
338        orxout() << "Done: Creating new Asteroid" << endl;
339
340        // }
341
342
343                //         Pawn* pawn = this->carrierToPawnHelper();
344                // if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
345                //     this->Pickupable::destroy();
346
347                // //Attach to pawn
348                // Drone* drone = new Drone(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something)
349                // drone->addTemplate(this->getDroneTemplate());
350
351
352    }
353    orxout() << "Leaving spawnChildren() method. " << endl;
354}
355
356
357
358
359}
360
361
362
363
364
365/*
366    void DronePickup::changedUsed(void)
367    {
368        SUPER(DronePickup, changedUsed);
369
370        // If the pickup has transited to used.
371        if(this->isUsed())
372        {
373
374                Pawn* pawn = this->carrierToPawnHelper();
375                if(pawn == nullptr) // If the PickupCarrier is no Pawn, then this pickup is useless and therefore is destroyed.
376                    this->Pickupable::destroy();
377
378                //Attach to pawn
379                Drone* drone = new Drone(pawn->getContext()); // this is neccessary because the projectiles fired need a valid creator for the particlespawner (when colliding against something)
380                drone->addTemplate(this->getDroneTemplate());
381
382                Controller* controller = drone->getController();
383                DroneController* droneController = orxonox_cast<DroneController*>(controller);
384                if(droneController != nullptr)
385                {
386                    droneController->setOwner(pawn);
387                }
388
389                Vector3 spawnPosition = pawn->getWorldPosition() + Vector3(30,0,-30);
390                drone->setPosition(spawnPosition);
391
392                // The pickup has been used up.
393                this->setUsed(false);
394        }
395        else
396        {
397            // If either the pickup can only be used once or it is continuous and used up, it is destroyed upon setting it to unused.
398            if(this->isOnce() || (this->isContinuous() ))
399            {
400                this->Pickupable::destroy();
401            }
402        }
403    }
404*/
Note: See TracBrowser for help on using the repository browser.