Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/modularships/src/orxonox/items/PartDestructionEvent.cc @ 10053

Last change on this file since 10053 was 10053, checked in by noep, 11 years ago

Fixed yet another segfault (which we hadn't seen yet).
Cleared emptyLevel, created two testlevels (testing with boxes)

File size: 6.5 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 *      Noe Pedrazzini
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "PartDestructionEvent.h"
30
31#include <algorithm>
32
33#include "core/CoreIncludes.h"
34#include "core/GameMode.h"
35#include "core/XMLPort.h"
36#include "network/NetworkFunction.h"
37#include "Item.h"
38#include "worldentities/pawns/Pawn.h"
39#include "worldentities/pawns/ModularSpaceShip.h"
40#include "gametypes/Gametype.h"
41
42
43namespace orxonox
44{
45    RegisterClass(PartDestructionEvent);
46
47    PartDestructionEvent::PartDestructionEvent(Context* context)
48        : Item(context)
49    {
50        RegisterObject(PartDestructionEvent);
51        this->setValid(true);
52    }
53
54    PartDestructionEvent::~PartDestructionEvent()
55    {
56
57    }
58
59    void PartDestructionEvent::XMLPort(Element& xmlelement, XMLPort::Mode mode)
60    {
61        SUPER(PartDestructionEvent, XMLPort, xmlelement, mode);
62
63        XMLPortParam(PartDestructionEvent, "targetType", setTargetType, getTargetType, xmlelement, mode).defaultValues("NULL");
64        XMLPortParam(PartDestructionEvent, "targetName", setTargetName, getTargetName, xmlelement, mode).defaultValues("NULL");
65        XMLPortParam(PartDestructionEvent, "targetParam", setTargetParam, getTargetParam, xmlelement, mode).defaultValues("NULL");
66        XMLPortParam(PartDestructionEvent, "operation", setOperation, getOperation, xmlelement, mode).defaultValues("NULL");
67        XMLPortParam(PartDestructionEvent, "value", setEventValue, getEventValue, xmlelement, mode).defaultValues(0);
68
69        /*
70        XMLPortParam(PartDestructionEvent, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
71        XMLPortParam(PartDestructionEvent, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
72        XMLPortParam(PartDestructionEvent, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
73        */
74    }
75
76    void PartDestructionEvent::execute()
77    {
78        // Do not execute if this event is invalid
79        if(!isValid())
80        {
81            orxout(internal_warning) << "Attempted to execute an invalid PartDestructionEvent!" << endl;
82            return;
83        }
84
85        if (this->targetType_ == "ship")
86        {
87            switch (this->targetParam_) {
88            case shieldhealth:
89                this->parent_->getParent()->setShieldHealth(operate(this->parent_->getParent()->getShieldHealth()));
90                break;
91            default:
92                break;
93            }
94            this->setValid(false);
95            return;
96        }
97    }
98
99    void PartDestructionEvent::setParent(ShipPart* part)
100    {
101        this->parent_ = part;
102    }
103
104    void PartDestructionEvent::setTargetType(std::string type)
105    {
106        // ship engine weapon
107        if ((type == "ship") || (type == "engine") || (type == "weapon"))
108        {
109            this->targetType_ = type;
110            return;
111        }
112
113        // Error, if invalid target-type was entered.
114        orxout(internal_warning) << "\"" << type << "\" is not a valid target-type for a PartDestructionEvent. Valid types are: ship engine weapon" << endl;
115        this->setValid(false);
116        return;
117    }
118
119    void PartDestructionEvent::setTargetName(std::string name)
120    {
121        // Error, if the target-type is "weapon" or "engine", but the name of said target was not defined.
122        if (((this->targetType_ == "weapon") || (this->targetType_ == "engine")) && (name == "NULL"))
123        {
124            orxout(internal_warning) << "The target-name of a PartDestructionEvent with target-type \"" << this->targetType_ << "\" needs to be defined!" << endl;
125            return;
126        }
127
128        this->targetName_ = name;
129    }
130
131    void PartDestructionEvent::setTargetParam(std::string param)
132    {
133        if (this->targetType_ == "NULL")
134        {
135            orxout(internal_warning) << "No valid target-type defined. Cannot set target-param for this PartDestructionEvent." << endl;
136            this->setValid(false);
137            return;
138        }
139
140        // ship: shieldhealth maxshieldhealth shieldabsorption shieldrechargerate
141
142        // engine:
143
144        // weapon:
145
146        if (this->targetType_ == "ship")
147        {
148            if (param == "shieldhealth")
149            {
150                this->targetParam_ = shieldhealth;
151                return;
152            }
153
154            orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"ship\". Valid types are: shieldhealth maxshieldhealth shieldabsorption shieldrechargerate" << endl;
155            return;
156        }
157
158        orxout(internal_warning) << "No valid target-param defined. The chosen param is either invalid or not available for this target-type." << endl;
159        this->setValid(false);
160    }
161
162    void PartDestructionEvent::setOperation(std::string operation)
163    {
164        // * + - destroy
165        if ((operation == "*") || (operation == "+") || (operation == "-") || (operation == "destroy"))
166        {
167            this->operation_ = operation;
168            return;
169        }
170        this->operation_ = "NULL";
171        orxout(internal_warning) << "\"" << operation << "\" is not a valid operation for a PartDestructionEvent. Valid operations are: * + - destroy" << endl;
172    }
173
174    float PartDestructionEvent::operate(float input)
175    {
176        if (this->operation_ == "*")
177            return input * this->value_;
178        if (this->operation_ == "+")
179            return input + this->value_;
180        if (this->operation_ == "-")
181            return input - this->value_;
182        if (this->operation_ == "destroy")
183        {
184            return 0;
185        }
186        return 0;
187    }
188
189    void PartDestructionEvent::setEventValue(float value)
190    {
191        this->value_ = value;
192    }
193}
Note: See TracBrowser for help on using the repository browser.