Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

Ignore:
Timestamp:
May 14, 2014, 8:25:08 PM (10 years ago)
Author:
noep
Message:

Cleaned up code. Added console command "ModularSpaceShip killshippart [string]" which allows manual destruction of a ShipPart by name. Added more functionality to PartDestructionEvents.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • code/branches/modularships/src/orxonox/items/PartDestructionEvent.cc

    r10053 r10055  
    3838#include "worldentities/pawns/Pawn.h"
    3939#include "worldentities/pawns/ModularSpaceShip.h"
     40#include "items/Engine.h"
    4041#include "gametypes/Gametype.h"
     42#include "chat/ChatManager.h"
    4143
    4244
     
    6365        XMLPortParam(PartDestructionEvent, "targetType", setTargetType, getTargetType, xmlelement, mode).defaultValues("NULL");
    6466        XMLPortParam(PartDestructionEvent, "targetName", setTargetName, getTargetName, xmlelement, mode).defaultValues("NULL");
     67        XMLPortParam(PartDestructionEvent, "operation", setOperation, getOperation, xmlelement, mode).defaultValues("NULL");
    6568        XMLPortParam(PartDestructionEvent, "targetParam", setTargetParam, getTargetParam, xmlelement, mode).defaultValues("NULL");
    66         XMLPortParam(PartDestructionEvent, "operation", setOperation, getOperation, xmlelement, mode).defaultValues("NULL");
    6769        XMLPortParam(PartDestructionEvent, "value", setEventValue, getEventValue, xmlelement, mode).defaultValues(0);
     70        XMLPortParam(PartDestructionEvent, "message", setMessage, getMessage, xmlelement, mode).defaultValues("NULL");
    6871
    6972        /*
     
    7477    }
    7578
     79    /**
     80    @brief
     81        Executes this event.
     82    */
    7683    void PartDestructionEvent::execute()
    7784    {
     85        orxout() << "Executing PartDestructionEvent " << this->getName() << endl;
     86
    7887        // Do not execute if this event is invalid
    7988        if(!isValid())
     
    8392        }
    8493
     94        // Output the destruction-message to the chat
     95        if(this->message_ != "NULL")
     96            ChatManager::message(this->message_);
     97
     98        // Modify parameters as configured for all cases
    8599        if (this->targetType_ == "ship")
    86100        {
     
    89103                this->parent_->getParent()->setShieldHealth(operate(this->parent_->getParent()->getShieldHealth()));
    90104                break;
     105            case boostpower:
     106                this->parent_->getParent()->setInitialBoostPower(operate(this->parent_->getParent()->getInitialBoostPower()));
     107                break;
     108            case boostpowerrate:
     109                this->parent_->getParent()->setBoostPowerRate(operate(this->parent_->getParent()->getBoostPowerRate()));
     110                break;
    91111            default:
    92112                break;
     
    95115            return;
    96116        }
     117
     118        if (this->targetType_ == "engine")
     119        {
     120            switch (this->targetParam_) {
     121            case null:
     122                for(unsigned int i = 0; i < this->parent_->getParent()->getEngineList().size(); i++) // FIXME: (noep) segfault on .size()
     123                {
     124                    if(this->parent_->getParent()->getEngine(i)->getName() == this->targetName_)
     125                    {
     126                        orxout() << "engine found" << endl;
     127                        this->parent_->getParent()->removeEngine(this->parent_->getParent()->getEngine(i));
     128                        break;
     129                    }
     130                }
     131                break;
     132            case boostfactor:
     133                for(unsigned int i = 0; i < this->parent_->getParent()->getEngineList().size(); i++)
     134                {
     135                    if(this->parent_->getParent()->getEngine(i)->getName() == this->targetName_)
     136                        this->parent_->getParent()->getEngine(i)->setBoostFactor(operate(this->parent_->getParent()->getEngine(i)->getBoostFactor()));
     137                    break;
     138                }
     139                break;
     140            default:
     141                break;
     142            }
     143            this->setValid(false);
     144            return;
     145        }
    97146    }
    98147
     
    102151    }
    103152
     153    /**
     154    @brief
     155        Set type of the target
     156    @param param
     157        The desired target-type as string. Valid target-types: ship engine weapon
     158    */
    104159    void PartDestructionEvent::setTargetType(std::string type)
    105160    {
    106         // ship engine weapon
    107161        if ((type == "ship") || (type == "engine") || (type == "weapon"))
    108162        {
     
    129183    }
    130184
     185    /**
     186    @brief
     187        Set the operation to be applied.
     188    @param param
     189        The desired parameter as string. Valid parameters: c.f. @ref orxnox::PartDestructionEvent::TargetParam
     190    */
    131191    void PartDestructionEvent::setTargetParam(std::string param)
    132192    {
     193        // A target-type needs to be defined in order to choose a parameter.
    133194        if (this->targetType_ == "NULL")
    134195        {
     
    138199        }
    139200
    140         // ship: shieldhealth maxshieldhealth shieldabsorption shieldrechargerate
    141 
    142         // engine:
     201        // engine: NULL boostfactor speedfront accelerationfront
     202        if (this->targetType_ == "engine")
     203        {
     204            if (param == "NULL")
     205            {
     206                this->targetParam_ = null;
     207                return;
     208            }
     209            if (param == "boostfactor")
     210            {
     211                this->targetParam_ = boostfactor;
     212                return;
     213            }
     214            if (param == "speedfront")
     215            {
     216                this->targetParam_ = speedfront;
     217                return;
     218            }
     219            if (param == "accelerationfront")
     220            {
     221                this->targetParam_ = accelerationfront;
     222                return;
     223            }
     224
     225            orxout(internal_warning) << "\"" << param << "\" is not a valid target-param for a PartDestructionEvent with target-type \"engine\". Valid types are: boostfactor speedfront accelerationfront" << endl;
     226            return;
     227        }
    143228
    144229        // weapon:
    145230
     231        // ship: shieldhealth (maxshieldhealth shieldabsorption shieldrechargerate) boostpower boostpowerrate
    146232        if (this->targetType_ == "ship")
    147233        {
     
    151237                return;
    152238            }
    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;
     239            if (param == "boostpower")
     240            {
     241                this->targetParam_ = boostpower;
     242                return;
     243            }
     244            if (param == "boostpowerrate")
     245            {
     246                this->targetParam_ = boostpowerrate;
     247                return;
     248            }
     249
     250            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" << endl;
    155251            return;
    156252        }
     
    160256    }
    161257
     258    /**
     259    @brief
     260        Set the operation to be applied.
     261    @param operation
     262        The desired operator as string. Valid operators: * + - destroy
     263    */
    162264    void PartDestructionEvent::setOperation(std::string operation)
    163265    {
     
    172274    }
    173275
     276    /**
     277    @brief
     278        Set the message to be shown upon execution of the vent.
     279    @param msg
     280        The desired message as string.
     281    */
     282    void PartDestructionEvent::setMessage(std::string msg)
     283    {
     284        this->message_ = msg;
     285    }
     286
     287
     288    /**
     289    @brief
     290        Apply the configured operation and value to an input.
     291    @param input
     292        The value which should be modified
     293    @return
     294        Returns the product / sum / difference of input and configured value, or 0 if the operation is "destroy"
     295    */
    174296    float PartDestructionEvent::operate(float input)
    175297    {
     
    187309    }
    188310
     311    /**
     312    @brief
     313        Sets the value applied with the chosen operation.
     314    @param value
     315        The value as float.
     316    */
    189317    void PartDestructionEvent::setEventValue(float value)
    190318    {
Note: See TracChangeset for help on using the changeset viewer.