Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Expanded functionality of PartDestructionEvents, fixed cursor not showing when flying a ModularSpaceShip

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 *      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 "items/Engine.h"
41#include "gametypes/Gametype.h"
42#include "chat/ChatManager.h"
43
44
45namespace orxonox
46{
47    RegisterClass(PartDestructionEvent);
48
49    PartDestructionEvent::PartDestructionEvent(Context* context)
50        : Item(context)
51    {
52        RegisterObject(PartDestructionEvent);
53        this->setValid(true);
54    }
55
56    PartDestructionEvent::~PartDestructionEvent()
57    {
58
59    }
60
61    void PartDestructionEvent::XMLPort(Element& xmlelement, XMLPort::Mode mode)
62    {
63        SUPER(PartDestructionEvent, XMLPort, xmlelement, mode);
64
65        XMLPortParam(PartDestructionEvent, "targetType", setTargetType, getTargetType, xmlelement, mode).defaultValues("NULL");
66        XMLPortParam(PartDestructionEvent, "targetName", setTargetName, getTargetName, xmlelement, mode).defaultValues("NULL");
67        XMLPortParam(PartDestructionEvent, "operation", setOperation, getOperation, xmlelement, mode).defaultValues("NULL");
68        XMLPortParam(PartDestructionEvent, "targetParam", setTargetParam, getTargetParam, xmlelement, mode).defaultValues("NULL");
69        XMLPortParam(PartDestructionEvent, "value", setEventValue, getEventValue, xmlelement, mode).defaultValues(0);
70        XMLPortParam(PartDestructionEvent, "message", setMessage, getMessage, xmlelement, mode).defaultValues("NULL");
71
72        /*
73        XMLPortParam(PartDestructionEvent, "health", setHealth, getHealth, xmlelement, mode).defaultValues(100);
74        XMLPortParam(PartDestructionEvent, "maxhealth", setMaxHealth, getMaxHealth, xmlelement, mode).defaultValues(200);
75        XMLPortParam(PartDestructionEvent, "initialhealth", setInitialHealth, getInitialHealth, xmlelement, mode).defaultValues(100);
76        */
77    }
78
79    /**
80    @brief
81        Executes this event.
82    */
83    void PartDestructionEvent::execute()
84    {
85        // Do not execute if this event is invalid
86        if(!isValid())
87        {
88            orxout(internal_warning) << "Attempted to execute an invalid PartDestructionEvent!" << endl;
89            return;
90        }
91
92        // Output the destruction-message to the chat
93        if(this->message_ != "NULL")
94            ChatManager::message(this->message_);
95
96        // Modify parameters as configured for all cases
97        if (this->targetType_ == "ship")
98        {
99            switch (this->targetParam_) {
100            case shieldhealth:
101                this->parent_->getParent()->setShieldHealth(operate(this->parent_->getParent()->getShieldHealth()));
102                break;
103            case boostpower:
104                this->parent_->getParent()->setInitialBoostPower(operate(this->parent_->getParent()->getInitialBoostPower()));
105                break;
106            case boostpowerrate:
107                this->parent_->getParent()->setBoostPowerRate(operate(this->parent_->getParent()->getBoostPowerRate()));
108                break;
109            case rotationthrust:
110                this->parent_->getParent()->setRotationThrust(operate(this->parent_->getParent()->getRotationThrust()));
111                break;
112            default:
113                break;
114            }
115            this->setValid(false);
116            return;
117        }
118
119        if (this->targetType_ == "engine")
120        {
121            switch (this->targetParam_) {
122            case null:
123                this->parent_->getParent()->removeEngine(this->parent_->getParent()->getEngineByName(targetName_));
124                break;
125            case boostfactor:
126                this->parent_->getParent()->getEngineByName(targetName_)->setBoostFactor(operate(this->parent_->getParent()->getEngineByName(targetName_)->getBoostFactor()));
127                break;
128            case speedfront:
129                this->parent_->getParent()->getEngineByName(targetName_)->setMaxSpeedFront(operate(this->parent_->getParent()->getEngineByName(targetName_)->getMaxSpeedFront()));
130                break;
131            case accelerationfront:
132                this->parent_->getParent()->getEngineByName(targetName_)->setAccelerationFront(operate(this->parent_->getParent()->getEngineByName(targetName_)->getAccelerationFront()));
133                break;
134            default:
135                break;
136            }
137            this->setValid(false);
138            return;
139        }
140
141        if (this->targetType_ == "part")
142        {
143            switch (this->targetParam_) {
144            case null:
145                this->parent_->getParent()->getShipPartByName(targetName_)->setEventExecution(false);
146                this->parent_->getParent()->killShipPart(targetName_);
147                break;
148            default:
149                break;
150            }
151            this->setValid(false);
152            return;
153        }
154    }
155
156    void PartDestructionEvent::setParent(ShipPart* part)
157    {
158        this->parent_ = part;
159    }
160
161    /**
162    @brief
163        Set type of the target
164    @param param
165        The desired target-type as string. Valid target-types: ship engine weapon
166    */
167    void PartDestructionEvent::setTargetType(std::string type)
168    {
169        if ((type == "ship") || (type == "engine") || (type == "weapon") || (type == "part"))
170        {
171            this->targetType_ = type;
172            return;
173        }
174
175        // Error, if invalid target-type was entered.
176        orxout(internal_warning) << "\"" << type << "\" is not a valid target-type for a PartDestructionEvent. Valid types are: ship engine weapon part" << endl;
177        this->setValid(false);
178        return;
179    }
180
181    void PartDestructionEvent::setTargetName(std::string name)
182    {
183        // Error, if the target-type is "weapon" or "engine", but the name of said target was not defined.
184        if (((this->targetType_ == "weapon") || (this->targetType_ == "engine")) && (name == "NULL"))
185        {
186            orxout(internal_warning) << "The target-name of a PartDestructionEvent with target-type \"" << this->targetType_ << "\" needs to be defined!" << endl;
187            return;
188        }
189
190        this->targetName_ = name;
191    }
192
193    /**
194    @brief
195        Set the operation to be applied.
196    @param param
197        The desired parameter as string. Valid parameters: c.f. @ref orxnox::PartDestructionEvent::TargetParam
198    */
199    void PartDestructionEvent::setTargetParam(std::string param)
200    {
201        // A target-type needs to be defined in order to choose a parameter.
202        if (this->targetType_ == "NULL")
203        {
204            orxout(internal_warning) << "No valid target-type defined. Cannot set target-param for this PartDestructionEvent." << endl;
205            this->setValid(false);
206            return;
207        }
208
209        // engine: NULL boostfactor speedfront accelerationfront
210        if (this->targetType_ == "engine")
211        {
212            if (param == "NULL")
213            {
214                this->targetParam_ = null;
215                return;
216            }
217            if (param == "boostfactor")
218            {
219                this->targetParam_ = boostfactor;
220                return;
221            }
222            if (param == "speedfront")
223            {
224                this->targetParam_ = speedfront;
225                return;
226            }
227            if (param == "accelerationfront")
228            {
229                this->targetParam_ = accelerationfront;
230                return;
231            }
232
233            orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"engine\". Valid types are: boostfactor speedfront accelerationfront" << endl;
234            return;
235        }
236
237        // weapon:
238
239        // ship: shieldhealth (maxshieldhealth shieldabsorption shieldrechargerate) boostpower boostpowerrate rotationthrust
240        if (this->targetType_ == "ship")
241        {
242            if (param == "shieldhealth")
243            {
244                this->targetParam_ = shieldhealth;
245                return;
246            }
247            if (param == "boostpower")
248            {
249                this->targetParam_ = boostpower;
250                return;
251            }
252            if (param == "boostpowerrate")
253            {
254                this->targetParam_ = boostpowerrate;
255                return;
256            }
257            if (param == "rotationthrust")
258            {
259                this->targetParam_ = rotationthrust;
260                return;
261            }
262
263            orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"ship\". Valid types are: shieldhealth maxshieldhealth shieldabsorption shieldrechargerate boostpower boostpowerrate rotationthrust" << endl;
264            return;
265        }
266
267        if (this->targetType_ == "part")
268        {
269            if (param == "NULL")
270            {
271                this->targetParam_ = null;
272                return;
273            }
274
275            orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"part\". Valid types are: NULL (set operation to \"destroy\")" << endl;
276            return;
277        }
278
279        orxout(internal_warning) << "No valid target-param defined. The chosen param is either invalid or not available for this target-type." << endl;
280        this->setValid(false);
281    }
282
283    /**
284    @brief
285        Set the operation to be applied.
286    @param operation
287        The desired operator as string. Valid operators: * + - destroy
288    */
289    void PartDestructionEvent::setOperation(std::string operation)
290    {
291        // * + - destroy
292        if ((operation == "*") || (operation == "+") || (operation == "-") || (operation == "set") || (operation == "destroy"))
293        {
294            this->operation_ = operation;
295            return;
296        }
297        this->operation_ = "NULL";
298        orxout(internal_warning) << "\"" << operation << "\" is not a valid operation for a PartDestructionEvent. Valid operations are: * + - set destroy" << endl;
299    }
300
301    /**
302    @brief
303        Set the message to be shown upon execution of the vent.
304    @param msg
305        The desired message as string.
306    */
307    void PartDestructionEvent::setMessage(std::string msg)
308    {
309        this->message_ = msg;
310    }
311
312
313    /**
314    @brief
315        Apply the configured operation and value to an input.
316    @param input
317        The value which should be modified
318    @return
319        Returns the product / sum / difference of input and configured value,
320        the configured value if the operation is "set",
321        or 0 if the operation is "destroy"
322    */
323    float PartDestructionEvent::operate(float input)
324    {
325        if (this->operation_ == "*")
326            return input * this->value_;
327        if (this->operation_ == "+")
328            return input + this->value_;
329        if (this->operation_ == "-")
330            return input - this->value_;
331        if (this->operation_ == "set")
332            return this->value_;
333        if (this->operation_ == "destroy")
334        {
335            return 0;
336        }
337        return 0;
338    }
339
340    /**
341    @brief
342        Sets the value applied with the chosen operation.
343    @param value
344        The value as float.
345    */
346    void PartDestructionEvent::setEventValue(float value)
347    {
348        this->value_ = value;
349    }
350}
Note: See TracBrowser for help on using the repository browser.