Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/Boss_FS17/src/orxonox/items/PartDestructionEvent.cc @ 11443

Last change on this file since 11443 was 11443, checked in by nyogaraj, 7 years ago

fixed error

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