Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/weapon.cc @ 8346

Last change on this file since 8346 was 8316, checked in by bensch, 18 years ago

trunk: fixed most -Wall warnings… but there are still many missing :/

File size: 20.2 KB
RevLine 
[3573]1
[4597]2/*
[3573]3   orxonox - the future of 3D-vertical-scrollers
4
5   Copyright (C) 2004 orx
6
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2, or (at your option)
10   any later version.
11
[4826]12### File Specific
[3573]13   main-programmer: Patrick Boenzli
[4832]14   co-programmer: Benjamin Grauer
[4885]15
16   2005-07-15: Benjamin Grauer: restructurating the entire Class
[3573]17*/
18
[4885]19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
20
[4828]21#include "weapon.h"
22
[5355]23#include "fast_factory.h"
[6434]24#include "world_entities/projectiles/projectile.h"
[4834]25
[7193]26#include "util/loading/resource_manager.h"
[4894]27#include "class_list.h"
[7350]28#include "util/loading/factory.h"
[7193]29#include "util/loading/load_param.h"
[4828]30#include "state.h"
[4885]31#include "animation3d.h"
[3573]32
[5930]33#include "sound_source.h"
34#include "sound_buffer.h"
35
[6438]36#include "glgui_bar.h"
37
[7350]38using namespace std;
39
[4892]40////////////////////
41// INITAILISATION //
42// SETTING VALUES //
43////////////////////
[3870]44/**
[4885]45 * standard constructor
46 *
47 * creates a new weapon
[3575]48*/
[5750]49Weapon::Weapon ()
[3620]50{
[4885]51  this->init();
[3620]52}
[3573]53
[3575]54/**
[4885]55 * standard deconstructor
[3575]56*/
[4597]57Weapon::~Weapon ()
[3573]58{
[4885]59  for (int i = 0; i < WS_STATE_COUNT; i++)
[4894]60    if (this->animation[i] && ClassList::exists(animation[i], CL_ANIMATION))  //!< @todo this should check animation3D
[4885]61      delete this->animation[i];
62  for (int i = 0; i < WA_ACTION_COUNT; i++)
[5302]63    if (this->soundBuffers[i] != NULL && ClassList::exists(this->soundBuffers[i], CL_SOUND_BUFFER))
[4885]64      ResourceManager::getInstance()->unload(this->soundBuffers[i]);
[4959]65
66  if (ClassList::exists(this->soundSource, CL_SOUND_SOURCE))
67    delete this->soundSource;
[4885]68}
[4597]69
[4885]70/**
[7350]71 * @brief creates a new Weapon of type weaponID and returns it.
72 * @param weaponID the WeaponID type to create.
73 * @returns the newly created Weapon.
74 */
75Weapon* Weapon::createWeapon(ClassID weaponID)
76{
77  BaseObject* createdObject = Factory::fabricate(weaponID);
78  if (createdObject != NULL)
79  {
80    if (createdObject->isA(CL_WEAPON))
81      return dynamic_cast<Weapon*>(createdObject);
82    else
83    {
84      delete createdObject;
85      return NULL;
86    }
87  }
[8316]88  return NULL;
[7350]89}
90
91/**
[4885]92 * initializes the Weapon with ALL default values
[5498]93 *
94 * This Sets the default values of the Weapon
[4885]95 */
96void Weapon::init()
97{
[7350]98  this->setClassID(CL_WEAPON, "Weapon");
[5498]99  this->currentState     = WS_INACTIVE;            //< Normaly the Weapon is Inactive
100  this->requestedAction  = WA_NONE;                //< No action is requested by default
101  this->stateDuration    = 0.0;                    //< All the States have zero duration
102  for (int i = 0; i < WS_STATE_COUNT; i++)         //< Every State has:
[6438]103  {
104    this->times[i] = 0.0;                        //< An infinitesimal duration
105    this->animation[i] = NULL;                   //< No animation
106  }
[4885]107  for (int i = 0; i < WA_ACTION_COUNT; i++)
[5498]108    this->soundBuffers[i] = NULL;                  //< No Sounds
[3888]109
[7460]110  this->soundSource = new OrxSound::SoundSource(this);       //< Every Weapon has exacty one SoundSource.
[5498]111  this->emissionPoint.setParent(this);             //< One EmissionPoint, that is a PNode connected to the weapon. You can set this to the exitting point of the Projectiles
[7126]112  this->emissionPoint.setName("EmissionPoint");
113  this->emissionPoint.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT);
[4885]114
[6920]115  this->defaultTarget = NULL;                      //< Nothing is Targeted by default.
[6756]116
[5498]117  this->projectile = CL_NULL;                      //< No Projectile Class is Connected to this weapon
118  this->projectileFactory = NULL;                  //< No Factory generating Projectiles is selected.
[4885]119
[5498]120  this->hideInactive = true;                       //< The Weapon will be hidden if it is inactive (by default)
[4906]121
[5498]122  this->minCharge = 1.0;                           //< The minimum charge the Weapon can hold is 1 unit.
123  this->maxCharge = 1.0;                           //< The maximum charge is also one unit.
[4927]124
[6671]125  this->energy = 10;                               //< The secondary Buffer (before we have to reload)
[5498]126  this->energyMax = 10.0;                          //< How much energy can be carried
127  this->capability = WTYPE_ALL;                    //< The Weapon has all capabilities @see W_Capability.
[6438]128
129  this->energyWidget = NULL;
[6695]130
131  // set this object to be synchronized over network
132  //this->setSynchronized(true);
[3573]133}
134
[5498]135/**
136 * loads the Parameters of a Weapon
137 * @param root the XML-Element to load the Weapons settings from
138 */
[4972]139void Weapon::loadParams(const TiXmlElement* root)
140{
[6512]141  WorldEntity::loadParams(root);
[4972]142
[7102]143  LoadParam(root, "projectile", this, Weapon, setProjectileTypeC)
[6438]144  .describe("Sets the name of the Projectile to load onto the Entity");
[4972]145
[5671]146  LoadParam(root, "emission-point", this, Weapon, setEmissionPoint)
[6438]147  .describe("Sets the Point of emission of this weapon");
[4972]148
[5671]149  LoadParam(root, "state-duration", this, Weapon, setStateDuration)
[6438]150  .describe("Sets the duration of a given state (1: state-Name; 2: duration in seconds)");
[4972]151
[5671]152  LoadParam(root, "action-sound", this, Weapon, setActionSound)
[6438]153  .describe("Sets a given sound to an action (1: action-Name; 2: name of the sound (relative to the Data-Path))");
[4972]154}
155
[6728]156
[4947]157/**
158 * sets the Projectile to use for this weapon.
159 * @param projectile The ID of the Projectile to use
[4950]160 * @returns true, if it was sucessfull, false on error
[4947]161 *
[5498]162 * be aware, that this function does not create Factories, as this is job of Projecitle/Bullet-classes.
163 * What it does, is telling the Weapon what Projectiles it can Emit.
[4947]164 */
[5356]165void Weapon::setProjectileType(ClassID projectile)
[4947]166{
167  if (projectile == CL_NULL)
[4972]168    return;
[4947]169  this->projectile = projectile;
170  this->projectileFactory = FastFactory::searchFastFactory(projectile);
171  if (this->projectileFactory == NULL)
[4979]172  {
173    PRINTF(1)("unable to find FastFactory for the Projectile.\n");
[4972]174    return;
[4979]175  }
[4948]176  else
177  {
178    // grabbing Parameters from the Projectile to have them at hand here.
179    Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect());
[6431]180    this->minCharge = pj->getMinEnergy();
[6700]181    this->maxCharge = pj->getHealthMax();
[4948]182    this->chargeable = pj->isChageable();
[4979]183    this->projectileFactory->kill(pj);
[4948]184  }
[4979]185}
[3573]186
[6728]187
[4891]188/**
[4950]189 * @see bool Weapon::setProjectile(ClassID projectile)
190 * @param projectile the Name of the Projectile.
191 */
[7221]192void Weapon::setProjectileTypeC(const std::string& projectile)
[4950]193{
194  FastFactory* tmpFac = FastFactory::searchFastFactory(projectile);
195  if (tmpFac != NULL)
196  {
[5356]197    this->setProjectileType(tmpFac->getStoredID());
[4950]198  }
[4972]199  else
200  {
[7221]201    PRINTF(1)("Projectile %s does not exist for weapon %s\n", projectile.c_str(), this->getName());
[4972]202  }
[4950]203}
204
[6728]205
[4950]206/**
[5356]207 * prepares Projectiles of the Weapon
[5498]208 * @param count how many Projectiles to create (they will be stored in the ProjectileFactory)
[5356]209 */
210void Weapon::prepareProjectiles(unsigned int count)
211{
[5357]212  if (likely(this->projectileFactory != NULL))
[5356]213    projectileFactory->prepare(count);
214  else
[5357]215    PRINTF(2)("unable to create %d projectile for Weapon %s (%s)\n", count, this->getName(), this->getClassName());
[5356]216}
217
[6728]218
[5356]219/**
220 * resurects and returns a Projectile
[5498]221 * @returns a Projectile on success, NULL on error
222 *
223 * errors: 1. (ProjectileFastFactory not Found)
224 *         2. No more Projectiles availiable.
[5356]225 */
226Projectile* Weapon::getProjectile()
227{
[5357]228  if (likely (this->projectileFactory != NULL))
[6142]229  {
230    Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect());
231    pj->toList((OM_LIST)(this->getOMListNumber()+1));
232    return pj;
233  }
[5356]234  else
235  {
[5498]236    PRINTF(2)("No projectile defined for Weapon %s(%s) can't return any\n", this->getName(), this->getClassName());
[5356]237    return NULL;
238  }
239}
240
241
242/**
[4892]243 * sets the emissionPoint's relative position from the Weapon
244 * @param point the Point relative to the mass-point of the Weapon
245 */
246void Weapon::setEmissionPoint(const Vector& point)
247{
248  this->emissionPoint.setRelCoor(point);
249}
250
[6728]251
[4892]252/**
[4891]253 * assigns a Sound-file to an action
254 * @param action the action the sound should be assigned too
255 * @param soundFile the soundFile's relative position to the data-directory (will be looked for by the ResourceManager)
256 */
[7221]257void Weapon::setActionSound(WeaponAction action, const std::string& soundFile)
[4885]258{
259  if (action >= WA_ACTION_COUNT)
260    return;
[4930]261  if (this->soundBuffers[action] != NULL)
262    ResourceManager::getInstance()->unload(this->soundBuffers[action]);
263
[7221]264  else if (!soundFile.empty())
[4885]265  {
[7460]266    this->soundBuffers[action] = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(soundFile, WAV);
[4885]267    if (this->soundBuffers[action] != NULL)
268    {
[7221]269      PRINTF(4)("Loaded sound %s to action %s.\n", soundFile.c_str(), actionToChar(action));
[4885]270    }
271    else
272    {
[7221]273      PRINTF(2)("Failed to load sound %s to %s.\n.", soundFile.c_str(), actionToChar(action));
[4885]274    }
275  }
276  else
277    this->soundBuffers[action] = NULL;
278}
279
[6728]280
[4893]281/**
[4895]282 * creates/returns an Animation3D for a certain State.
283 * @param state what State should the Animation be created/returned for
284 * @param node the node this Animation should apply to. (NULL is fine if the animation was already created)
285 * @returns The created animation.Animation(), NULL on error (or if the animation does not yet exist).
[4893]286 *
287 * This function does only generate the Animation Object, and if set it will
288 * automatically be executed, when a certain State is reached.
289 * What this does not do, is set keyframes, you have to operate on the returned animation.
290 */
[4895]291Animation3D* Weapon::getAnimation(WeaponState state, PNode* node)
[4893]292{
[4895]293  if (state >= WS_STATE_COUNT) // if the state is not known
[4893]294    return NULL;
295
[4895]296  if (unlikely(this->animation[state] == NULL)) // if the animation does not exist yet create it.
[4893]297  {
[4895]298    if (likely(node != NULL))
299      return this->animation[state] = new Animation3D(node);
300    else
301    {
302      PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state));
303      return NULL;
304    }
[4893]305  }
[4895]306  else
307    return this->animation[state];
[4893]308}
309
[7779]310OrxGui::GLGuiWidget* Weapon::getEnergyWidget()
[6438]311{
312  if (this->energyWidget == NULL)
313  {
[7779]314    this->energyWidget = new OrxGui::GLGuiBar;
[6438]315    this->energyWidget->setSize2D( 20, 100);
316    this->energyWidget->setMaximum(this->getEnergyMax());
317    this->energyWidget->setValue(this->getEnergy());
318  }
319  return this->energyWidget;
320}
321
322void Weapon::updateWidgets()
323{
324  if (this->energyWidget != NULL)
325  {
326    this->energyWidget->setMaximum(this->energyMax);
327    this->energyWidget->setValue(this->energy);
328  }
329}
330
[4892]331/////////////////
332//  EXECUTION  //
333// GAME ACTION //
334/////////////////
[4597]335/**
[4885]336 * request an action that should be executed,
337 * @param action the next action to take
338 *
339 * This function must be called instead of the actions (like fire/reload...)
340 * to make all the checks needed to have a usefull WeaponSystem.
341 */
342void Weapon::requestAction(WeaponAction action)
343{
[4906]344  if (likely(this->isActive()))
[4885]345  {
[4906]346    if (this->requestedAction != WA_NONE)
347      return;
[5041]348    PRINTF(5)("next action will be %s in %f seconds\n", actionToChar(action), this->stateDuration);
[4885]349    this->requestedAction = action;
350  }
[4906]351  //else
352  else if (unlikely(action == WA_ACTIVATE))
353  {
354    this->currentState = WS_ACTIVATING;
[4926]355    this->requestedAction = WA_ACTIVATE;
[4906]356  }
[4885]357}
[3577]358
[6728]359
[4890]360/**
361 * adds energy to the Weapon
362 * @param energyToAdd The amount of energy
363 * @returns the amount of energy we did not pick up, because the weapon is already full
364 */
365float Weapon::increaseEnergy(float energyToAdd)
366{
367  float maxAddEnergy = this->energyMax - this->energy;
368
369  if (maxAddEnergy >= energyToAdd)
370  {
371    this->energy += energyToAdd;
372    return 0.0;
373  }
374  else
375  {
376    this->energy += maxAddEnergy;
377    return energyToAdd - maxAddEnergy;
378  }
379}
380
[6728]381
[5498]382////////////////////////////////////////////////////////////
383// WEAPON INTERNALS                                       //
384// These are functions, that no other Weapon should over- //
385// write. No class has direct Access to them, as it is    //
386// quite a complicated process, handling a Weapon from    //
387// the outside                                            //
388////////////////////////////////////////////////////////////
[4891]389/**
390 * executes an action, and with it starts a new State.
391 * @return true, if it worked, false otherwise
392 *
393 * This function checks, wheter the possibility of executing an action is valid,
394 * and does all the necessary stuff, to execute them. If an action does not succeed,
395 * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again)
396 */
[4885]397bool Weapon::execute()
[3583]398{
[7729]399#if DEBUG_LEVEL > 4
[4885]400  PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction));
401  this->debug();
[4906]402#endif
[4885]403
[4926]404  WeaponAction action = this->requestedAction;
405  this->requestedAction = WA_NONE;
406
407  switch (action)
[4885]408  {
[7350]409    case WA_SHOOT:
410      return this->fireW();
411      break;
412    case WA_CHARGE:
413      return this->chargeW();
414      break;
415    case WA_RELOAD:
416      return this->reloadW();
417      break;
418    case WA_DEACTIVATE:
419      return this->deactivateW();
420      break;
421    case WA_ACTIVATE:
422      return this->activateW();
423      break;
[8316]424    default:
425      PRINTF(2)("Action %s Not Implemented yet \n", Weapon::actionToChar(action));
426      return false;
[4885]427  }
[3583]428}
[3577]429
[4597]430/**
[4894]431 * checks and activates the Weapon.
432 * @return true on success.
[4892]433 */
434bool Weapon::activateW()
[3583]435{
[6438]436  //  if (this->currentState == WS_INACTIVE)
[4892]437  {
[6433]438    // play Sound
[4893]439    if (likely(this->soundBuffers[WA_ACTIVATE] != NULL))
[4892]440      this->soundSource->play(this->soundBuffers[WA_ACTIVATE]);
[6444]441    this->updateWidgets();
[6438]442    // activate
[4895]443    PRINTF(4)("Activating the Weapon %s\n", this->getName());
[4892]444    this->activate();
[4895]445    // setting up for next action
[4926]446    this->enterState(WS_ACTIVATING);
[4892]447  }
[8316]448  return true;
[3583]449}
[3577]450
[4597]451/**
[4894]452 * checks and deactivates the Weapon
453 * @return true on success.
[4892]454 */
455bool Weapon::deactivateW()
[3583]456{
[6438]457  //  if (this->currentState != WS_INACTIVE)
[4892]458  {
459    PRINTF(4)("Deactivating the Weapon %s\n", this->getName());
[6438]460    // play Sound
[4892]461    if (this->soundBuffers[WA_DEACTIVATE] != NULL)
462      this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]);
[4926]463    // deactivate
[4892]464    this->deactivate();
[4926]465    this->enterState(WS_DEACTIVATING);
[4892]466  }
[8316]467
468  return true;
[3583]469}
[3577]470
[4892]471/**
[4894]472 * checks and charges the Weapon
473 * @return true on success.
[4892]474 */
475bool Weapon::chargeW()
[4885]476{
[6671]477  if ( this->currentState != WS_INACTIVE && this->energy >= this->minCharge)
[4892]478  {
[6438]479    // playing Sound
[4892]480    if (this->soundBuffers[WA_CHARGE] != NULL)
481      this->soundSource->play(this->soundBuffers[WA_CHARGE]);
[4893]482
[6438]483    // charge
[4892]484    this->charge();
[6438]485    // setting up for the next state
[4926]486    this->enterState(WS_CHARGING);
[4892]487  }
488  else // deactivate the Weapon if we do not have enough energy
489  {
490    this->requestAction(WA_RELOAD);
491  }
[8316]492  return true;
[4885]493}
[3573]494
[4892]495/**
[4894]496 * checks and fires the Weapon
497 * @return true on success.
[4892]498 */
499bool Weapon::fireW()
[3575]500{
[6438]501  //if (likely(this->currentState != WS_INACTIVE))
[6671]502  if (this->minCharge <= this->energy)
[4892]503  {
[6438]504    // playing Sound
[4892]505    if (this->soundBuffers[WA_SHOOT] != NULL)
506      this->soundSource->play(this->soundBuffers[WA_SHOOT]);
[6444]507    this->updateWidgets();
[6438]508    // fire
[6671]509    this->energy -= this->minCharge;
[4892]510    this->fire();
[6438]511    // setting up for the next state
[4926]512    this->enterState(WS_SHOOTING);
[4892]513  }
514  else  // reload if we still have the charge
515  {
516    this->requestAction(WA_RELOAD);
[4930]517    this->execute();
[4892]518  }
[8316]519  return true;
[4892]520}
521
522/**
[4894]523 * checks and Reloads the Weapon
524 * @return true on success.
[4892]525 */
526bool Weapon::reloadW()
527{
[4885]528  PRINTF(4)("Reloading Weapon %s\n", this->getName());
[6671]529  if (this->ammoContainer.get() != NULL &&
[7350]530      unlikely(this->energy + this->ammoContainer->getStoredEnergy() < this->minCharge))
[4885]531  {
532    this->requestAction(WA_DEACTIVATE);
[4930]533    this->execute();
[4892]534    return false;
[4885]535  }
[3573]536
537
[4892]538  if (this->soundBuffers[WA_RELOAD] != NULL)
539    this->soundSource->play(this->soundBuffers[WA_RELOAD]);
540
[6671]541  if (this->ammoContainer.get() != NULL)
542    this->ammoContainer->fillWeapon(this);
[4885]543  else
544  {
[6671]545    this->energy = this->energyMax;
[4885]546  }
[6444]547  this->updateWidgets();
[4892]548  this->reload();
[4926]549  this->enterState(WS_RELOADING);
[8316]550
551  return true;
[4926]552}
[3575]553
[4926]554/**
555 * enters the requested State, plays back animations updates the timing.
556 * @param state the state to enter.
557 */
558inline void Weapon::enterState(WeaponState state)
559{
[5041]560  PRINTF(4)("ENTERING STATE %s\n", stateToChar(state));
[4926]561  // playing animation if availiable
562  if (likely(this->animation[state] != NULL))
563    this->animation[state]->replay();
564
[6728]565  this->stateDuration += this->times[state];
[4926]566  this->currentState = state;
[3575]567}
568
[4927]569///////////////////
570//  WORLD-ENTITY //
571// FUNCTIONALITY //
572///////////////////
[3575]573/**
[4885]574 * tick signal for time dependent/driven stuff
[3575]575*/
[6736]576bool Weapon::tickW(float dt)
[4885]577{
[4934]578  //printf("%s ", stateToChar(this->currentState));
[4910]579
[4885]580  // setting up the timing properties
581  this->stateDuration -= dt;
[3575]582
[4949]583  if (this->stateDuration <= 0.0)
[4885]584  {
[4949]585    if (unlikely (this->currentState == WS_DEACTIVATING))
[4885]586    {
[4949]587      this->currentState = WS_INACTIVE;
[6736]588      return false;
[4949]589    }
590    else
591      this->currentState = WS_IDLE;
[4906]592
[4949]593    if (this->requestedAction != WA_NONE)
594    {
595      this->stateDuration = -dt;
596      this->execute();
[4885]597    }
598  }
[6736]599  return true;
[4885]600}
601
[3575]602
603
604
[4885]605//////////////////////
606// HELPER FUNCTIONS //
607//////////////////////
[3576]608/**
[4891]609 * checks wether all the Weapons functions are valid, and if it is possible to go to action with it.
[5498]610 * @todo IMPLEMENT the Weapons Check
[4891]611 */
612bool Weapon::check() const
613{
614  bool retVal = true;
615
[6438]616  //  if (this->projectile == NULL)
[4891]617  {
[5041]618    PRINTF(1)("There was no projectile assigned to the Weapon.\n");
[4891]619    retVal = false;
620  }
621
622
623
624
625  return retVal;
626}
627
628/**
[4885]629 * some nice debugging information about this Weapon
630 */
631void Weapon::debug() const
632{
[6433]633  PRINT(0)("Weapon-Debug %s, state: %s (duration: %fs), nextAction: %s\n", this->getName(), Weapon::stateToChar(this->currentState), this->stateDuration, Weapon::actionToChar(requestedAction));
[6671]634  PRINT(0)("Energy: max: %f; current: %f; chargeMin: %f, chargeMax %f\n",
635           this->energyMax, this->energy, this->minCharge, this->maxCharge);
[4967]636
637
[4885]638}
[3575]639
[5498]640////////////////////////////////////////////////////////
641// static Definitions (transormators for readability) //
642////////////////////////////////////////////////////////
[4885]643/**
644 * Converts a String into an Action.
645 * @param action the String input holding the Action.
646 * @return The Action if known, WA_NONE otherwise.
647 */
[7221]648WeaponAction Weapon::charToAction(const std::string& action)
[4885]649{
[7221]650  if (action == "none")
[4885]651    return WA_NONE;
[7221]652  else if (action == "shoot")
[4885]653    return WA_SHOOT;
[7221]654  else if (action == "charge")
[4885]655    return WA_CHARGE;
[7221]656  else if (action == "reload")
[4885]657    return WA_RELOAD;
[7221]658  else if (action == "acitvate")
[4885]659    return WA_ACTIVATE;
[7221]660  else if (action == "deactivate")
[4885]661    return WA_DEACTIVATE;
[7221]662  else if (action == "special1")
[4885]663    return WA_SPECIAL1;
664  else
[6438]665  {
[7221]666    PRINTF(2)("action %s could not be identified.\n", action.c_str());
[6438]667    return WA_NONE;
668  }
[4885]669}
[3575]670
671/**
[4885]672 * converts an action into a String
673 * @param action the action to convert
674 * @return a String matching the name of the action
675 */
676const char* Weapon::actionToChar(WeaponAction action)
677{
678  switch (action)
679  {
[7350]680    case WA_SHOOT:
681      return "shoot";
682      break;
683    case WA_CHARGE:
684      return "charge";
685      break;
686    case WA_RELOAD:
687      return "reload";
688      break;
689    case WA_ACTIVATE:
690      return "activate";
691      break;
692    case WA_DEACTIVATE:
693      return "deactivate";
694      break;
695    case WA_SPECIAL1:
696      return "special1";
697      break;
698    default:
699      return "none";
700      break;
[4885]701  }
702}
[3577]703
704/**
[4885]705 * Converts a String into a State.
706 * @param state the String input holding the State.
707 * @return The State if known, WS_NONE otherwise.
708 */
[7221]709WeaponState Weapon::charToState(const std::string& state)
[4885]710{
[7221]711  if (state == "none")
[4885]712    return WS_NONE;
[7221]713  else if (state == "shooting")
[4885]714    return WS_SHOOTING;
[7221]715  else if (state == "charging")
[4885]716    return WS_CHARGING;
[7221]717  else if (state == "reloading")
[4885]718    return WS_RELOADING;
[7221]719  else if (state == "activating")
[4885]720    return WS_ACTIVATING;
[7221]721  else if (state == "deactivating")
[4885]722    return WS_DEACTIVATING;
[7221]723  else if (state == "inactive")
[4885]724    return WS_INACTIVE;
[7221]725  else if (state == "idle")
[4885]726    return WS_IDLE;
727  else
[6438]728  {
[7221]729    PRINTF(2)("state %s could not be identified.\n", state.c_str());
[6438]730    return WS_NONE;
731  }
[4885]732}
[3583]733
734/**
[4885]735 * converts a State into a String
736 * @param state the state to convert
737 * @return a String matching the name of the state
738 */
739const char* Weapon::stateToChar(WeaponState state)
740{
741  switch (state)
742  {
[7350]743    case WS_SHOOTING:
744      return "shooting";
745      break;
746    case WS_CHARGING:
747      return "charging";
748      break;
749    case WS_RELOADING:
750      return "reloading";
751      break;
752    case WS_ACTIVATING:
753      return "activating";
754      break;
755    case WS_DEACTIVATING:
756      return "deactivating";
757      break;
758    case WS_IDLE:
759      return "idle";
760      break;
761    case WS_INACTIVE:
762      return "inactive";
763      break;
764    default:
765      return "none";
766      break;
[4885]767  }
768}
Note: See TracBrowser for help on using the repository browser.