Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/playability/src/world_entities/weapons/weapon_manager.cc @ 10217

Last change on this file since 10217 was 10132, checked in by marcscha, 17 years ago

Firing Echo fix on WM

File size: 16.7 KB
RevLine 
[4826]1
2/*
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
12### File Specific
13   main-programmer: Patrick Boenzli
[4832]14   co-programmer: Benjamin Grauer
[4951]15
16   2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons.
[4826]17*/
18
[9869]19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
[4826]20
21#include "weapon_manager.h"
22#include "weapon.h"
[4834]23#include "crosshair.h"
[4828]24
[6561]25#include "playable.h"
26
[9869]27#include "util/loading/load_param_xml.h"
[7193]28#include "util/loading/factory.h"
[6055]29
[4837]30#include "t_animation.h"
[4826]31
32
[9869]33ObjectListDefinition(WeaponManager);
[4826]34/**
[6054]35 * @brief this initializes the weaponManager for a given nnumber of weapon slots
[4826]36 * @param number of weapon slots of the model/ship <= 8 (limitied)
37 */
[6142]38WeaponManager::WeaponManager(WorldEntity* parent)
[4826]39{
[4833]40  this->init();
[8844]41  this->setParentEntity(parent);
42
43  assert (parent != NULL);
[4826]44}
45
[4949]46WeaponManager::WeaponManager(const TiXmlElement* root)
47{
48  this->init();
49  this->loadParams(root);
50}
[4826]51
[4833]52/**
[6054]53 * @brief Destroys a WeaponManager
[4833]54 */
[4826]55WeaponManager::~WeaponManager()
56{
[4834]57  // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.)
[8147]58  // rennerc: crosshair seems not to delete itselve
[9869]59  //if (Crosshair::objectList().exists(this->crosshair))
60  //  delete this->crosshair;
[4826]61}
62
[4834]63/**
[6054]64 * @brief initializes the WeaponManager
[4834]65 */
[4833]66void WeaponManager::init()
67{
[9869]68  this->registerObject(this, WeaponManager::_objectList);
[4826]69
[8844]70  this->parentNode = NULL;
71  this->parentEntity = NULL;
[4951]72
73  for (int i = 0; i < WM_MAX_CONFIGS; i++)
74    for (int j = 0; j < WM_MAX_SLOTS; j++)
75      this->configs[i][j] = NULL;
76
77  for (int i = 0; i < WM_MAX_SLOTS; i++)
[4833]78  {
[4959]79    this->currentSlotConfig[i].capability = WTYPE_ALL;
[4951]80    this->currentSlotConfig[i].currentWeapon = NULL;
81    this->currentSlotConfig[i].nextWeapon = NULL;
[4992]82
83    // NAMING
84    char* tmpName;
[9406]85    if (!this->getName().empty())
[4992]86    {
[9406]87      tmpName = new char[this->getName().size() + 10];
88      sprintf(tmpName, "%s_slot%d", this->getCName(), i);
[4992]89    }
90    else
91    {
92      tmpName = new char[30];
93      sprintf(tmpName, "WeaponMan_slot%d", i);
94    }
95    this->currentSlotConfig[i].position.setName(tmpName);
[6056]96    this->currentSlotConfig[i].position.deactivateNode();
[5208]97    delete[] tmpName;
[4833]98  }
[4895]99
[4951]100  for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++)
101    this->availiableWeapons[i] = NULL;
[4895]102
103
[4951]104  this->currentConfigID = 0;
[9998]105  this->slotCount = WM_MAX_SLOTS;
[8315]106  //this->weaponChange;
[4895]107
[4951]108  // CROSSHAIR INITIALISATION
[4834]109  this->crosshair = new Crosshair();
[6807]110  //this->crosshair->setRelCoor(1000,0,0);
[4837]111  this->crossHairSizeAnim = new tAnimation<Crosshair>(this->crosshair, &Crosshair::setSize);
112  this->crossHairSizeAnim->setInfinity(ANIM_INF_REWIND);
113  this->crossHairSizeAnim->addKeyFrame(50, .1, ANIM_LINEAR);
114  this->crossHairSizeAnim->addKeyFrame(100, .05, ANIM_LINEAR);
115  this->crossHairSizeAnim->addKeyFrame(50, .01, ANIM_LINEAR);
[9998]116
117  this->hideCrosshair();
[10036]118
119  this->bFire = false;
[4834]120}
[4833]121
[9998]122void WeaponManager::showCrosshair()
123{
124  this->crosshair->setVisibility( true);
125}
126
127void WeaponManager::hideCrosshair()
128{
129  this->crosshair->setVisibility( false);
130}
131
[10004]132void WeaponManager::setRotationSpeed(float speed)
133{
134  this->crosshair->setRotationSpeed(speed);
135}
136
[4834]137/**
[7350]138 * @brief loads the settings of the WeaponManager
[4834]139 * @param root the XML-element to load from
140 */
141void WeaponManager::loadParams(const TiXmlElement* root)
142{
[6512]143  BaseObject::loadParams(root);
[4972]144
[5671]145  LoadParam(root, "slot-count", this, WeaponManager, setSlotCount)
[6736]146  .describe("how many slots(cannons) the WeaponManager can handle");
[4834]147
[5644]148  LOAD_PARAM_START_CYCLE(root, element);
[5654]149  {
150    // CHECK IF THIS WORKS....
151    LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons)
[6736]152    .describe("loads Weapons");
[5654]153  }
[5644]154  LOAD_PARAM_END_CYCLE(element);
[4833]155}
156
[4826]157/**
[7350]158 * @brief loads a Weapon onto the WeaponManager
[4834]159 * @param root the XML-element to load the Weapons from
160 */
161void WeaponManager::loadWeapons(const TiXmlElement* root)
162{
[5644]163  LOAD_PARAM_START_CYCLE(root, element);
[4834]164
[8315]165  BaseObject* object = Factory::fabricate(element);
166  if (object != NULL)
167  {
168    Weapon* newWeapon = dynamic_cast<Weapon*>(object);
169    if (newWeapon == NULL)
170      delete object;
171  }
[5644]172  LOAD_PARAM_END_CYCLE(element);
[4834]173}
174
[4992]175/**
[7350]176 * @brief sets the Parent of the WeaponManager.
[4992]177 * @param parent the parent of the WeaponManager
178 *
179 * this is used, to identify to which ship/man/whatever this WeaponManager is connected.
180 * also all the Slots will be subconnected to this parent.
[5435]181 *
182 * The reason this function exists is that the WeaponManager is neither a WorldEntity nor
183 * a PNode.
[4992]184 */
[8844]185void WeaponManager::setParentEntity(WorldEntity* parent)
[4953]186{
[8844]187  this->parentEntity = parent;
188  if (this->parentNode == NULL)
189    this->setParentNode(parent);
190}
191
192
193void WeaponManager::setParentNode(PNode* parent)
194{
195  this->parentNode = parent;
196  assert(parent != NULL);
197
198  if (this->parentNode != NULL)
[4953]199  {
200    for (int i = 0; i < WM_MAX_SLOTS; i++)
[8844]201      this->parentNode->addChild(&this->currentSlotConfig[i].position);
[4953]202  }
[8844]203
[4953]204}
205
[8844]206
[4834]207/**
[7350]208 * @brief sets the number of Slots the WeaponManager has
[4926]209 * @param slotCount the number of slots
[4834]210 */
[4951]211void WeaponManager::setSlotCount(unsigned int slotCount)
[4834]212{
[4951]213  if (slotCount <= WM_MAX_SLOTS)
214    this->slotCount = slotCount;
215  else
216    this->slotCount = WM_MAX_SLOTS;
[4834]217}
218
[4972]219
220/**
[7350]221 * @brief sets the position of the Slot relative to the parent
[4972]222 * @param slot the slot to set-up
223 * @param position the position of the given slot
224 */
[6803]225void WeaponManager::setSlotPosition(int slot, const Vector& position, PNode* parent)
[4953]226{
227  if (slot < this->slotCount)
[6803]228  {
[4953]229    this->currentSlotConfig[slot].position.setRelCoor(position);
[6803]230
231    if (parent != NULL)
232      this->currentSlotConfig[slot].position.setParent(parent);
233  }
[4953]234}
235
[4972]236
237/**
[7350]238 * @brief sets the relative rotation of the slot to its parent
[4972]239 * @param slot the slot to set-up
240 * @param rotation the relative rotation of the given slot
241 */
[4969]242void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
243{
244  if (slot < this->slotCount)
245    this->currentSlotConfig[slot].position.setRelDir(rotation);
246}
247
248
[4834]249/**
[7350]250 * @brief adds a weapon to the selected weaponconfiguration into the selected slot
[4972]251 * @param weapon the weapon to add
252 * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
253 * @param slotID an identifier for the weapon configuration, number between 0..3
[4832]254 *
255 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
[4906]256 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
[4832]257 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
258 * a error message.
[4826]259 */
[6561]260bool WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
[4826]261{
[6753]262  if ( weapon == NULL )
[6737]263    return false;
[6679]264
[4951]265  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
[4826]266  {
[6679]267    PRINTF(2)("Slot %d of config %d is not availiabe (max: %d) searching for suitable slot\n", slotID, configID, this->slotCount);
268    if (configID >= WM_MAX_CONFIGS)
269      configID = -1;
270    if (slotID >= (int)this->slotCount)
271      slotID = -1;
[4951]272  }
[6679]273  // if no ConfigID is supplied set to Current Config.
274  if (configID <= -1)
275    configID = this->currentConfigID;
276  //
277  if (configID > -1 && slotID == -1)
278  {
279    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
280    if (slotID == -1)
281      configID = -1;
282  }
[4951]283
[6676]284  if (configID > 0 && slotID > 0 && this->configs[configID][slotID] != NULL)
285  {
[9406]286    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, remove weapon (%s::%s) first\n", configID, slotID, this->getCName(), weapon->getClassCName(), weapon->getCName());
[6676]287    return false;
288  }
[4951]289
[6676]290  if (slotID <= -1) // WM_FREE_SLOT
[4951]291  {
[5441]292    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
[4951]293    if( slotID < 0 || slotID >= this->slotCount)
[4826]294    {
[5441]295      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
[6561]296      return false;
[4826]297    }
298  }
[4953]299
[5441]300  if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) &&
[6736]301      this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS)
[5441]302  {
303    PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n",
304              slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability);
[6561]305    return false;
[5441]306  }
307
[4953]308  //! @todo check if the weapon is already assigned to another config in another slot
[6714]309  if (this->configs[configID][slotID] != NULL)
310    return false;
[6676]311
[4951]312  this->configs[configID][slotID] = weapon;
[6669]313  weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType()));
[6736]314  if(configID == this->currentConfigID)
315    this->currentSlotConfig[slotID].nextWeapon = weapon;
[8844]316  //if (this->parent != NULL)
[6142]317  {
[8844]318    this->parentNode->addChild(weapon);
[9869]319    if (this->parentEntity->isA(Playable::staticClassID()))
[8844]320      dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
[9998]321   
[6920]322    weapon->setDefaultTarget(this->crosshair);
[6142]323  }
[9869]324  PRINTF(4)("Added a new Weapon (%s::%s) to the WeaponManager: config %i/ slot %i\n", weapon->getClassCName(), weapon->getCName(), configID, slotID);
[6561]325  return true;
[4826]326}
327
[4834]328/**
[6931]329 * @brief increases the Energy of the WeaponContainer of type (projectileType)
330 * @param projectileType the type of weapon to increase Energy from
331 * @param ammo the ammo to increase
332 */
[9869]333float WeaponManager::increaseAmmunition(const ClassID& projectileType, float ammo)
[6931]334{
335  return this->getAmmoContainer(projectileType)->increaseEnergy(ammo);
336}
337
[6972]338/**
[9965]339 * @brief does the same as the funtion increaseAmmunition, added four your convenience
[6972]340 * @param weapon, the Weapon to read the ammo-info about.
341 * @param ammo how much ammo to add.
342 */
[9965]343float WeaponManager::increaseAmmunition(const Weapon* weapon, float ammo)
[6972]344{
345  assert (weapon != NULL);
[9869]346  return this->increaseAmmunition(weapon->getClassID(), ammo);
[6931]347
[6972]348}
[6931]349
[6972]350
[6931]351/**
[4954]352 * sets the capabilities of a Slot
353 * @param slot the slot to set the capability
354 * @param slotCapability the capability @see WM_SlotCapability
355 */
356void WeaponManager::setSlotCapability(int slot, long slotCapability)
357{
358  if (slot > slotCount)
359    return;
360  this->currentSlotConfig[slot].capability = slotCapability;
361}
362
363
364/**
[4834]365 * removes a Weapon from the WeaponManager
[4954]366 *
367 * !! The weapon must be inactive before you can delete it,    !!
368 * !! because it will still be deactivated (if it is selected) !!
[4834]369 */
[4826]370void WeaponManager::removeWeapon(Weapon* weapon, int configID)
371{
[4954]372  if (weapon == NULL)
373    return;
374  if (configID < 0)
375  {
376    for (int j = 0; j < WM_MAX_SLOTS; j++)
377    {
378      for (int i = 0; i < WM_MAX_CONFIGS; i++)
379      {
380        if (this->configs[i][j] == weapon)
381          this->configs[i][j] = NULL;
382      }
383      if (this->currentSlotConfig[j].currentWeapon == weapon)
384      {
385        this->currentSlotConfig[j].nextWeapon = NULL;
386      }
387    }
388  }
[4826]389}
390
391
392/**
[4832]393 * changes to the next weapon configuration
[4826]394 */
[4954]395void WeaponManager::nextWeaponConfig()
[4826]396{
[4951]397  ++this->currentConfigID;
398  if (this->currentConfigID >= WM_MAX_CONFIGS)
399    this->currentConfigID = 0;
[4952]400  this->changeWeaponConfig(this->currentConfigID);
401}
[4826]402
[4953]403/**
404 * changes to the previous configuration
405 */
[4952]406void WeaponManager::previousWeaponConfig()
407{
408  --this->currentConfigID;
409  if (this->currentConfigID < 0)
410    this->currentConfigID = WM_MAX_CONFIGS -1;
411  this->changeWeaponConfig(this->currentConfigID);
412}
413
[4953]414/**
415 * change to a desired configuration
416 * @param weaponConfig the configuration to jump to.
417 */
[4952]418void WeaponManager::changeWeaponConfig(int weaponConfig)
419{
420  this->currentConfigID = weaponConfig;
421  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
[10121]422
[4951]423  for (int i = 0; i < WM_MAX_SLOTS; i++)
424    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
[4826]425}
426
427
428/**
[4832]429 * triggers fire of all weapons in the current weaponconfig
[4826]430 */
[4832]431void WeaponManager::fire()
[4826]432{
433  Weapon* firingWeapon;
[4951]434  for(int i = 0; i < this->slotCount; i++)
[4826]435  {
[9961]436          firingWeapon = this->currentSlotConfig[i].currentWeapon;
[10132]437      if( firingWeapon != NULL && firingWeapon->getCurrentState() == WS_SHOOTING) continue;
[9961]438          if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
[4826]439  }
[9998]440
441  /*
442        this->crosshair->setRotationSpeed(500);
443        this->crossHairSizeAnim->replay();
[10004]444  */
[4826]445}
446
[10036]447/**
448 * triggers fire of all weapons in the current weaponconfig
449 */
450void WeaponManager::releaseFire()
451{
452  Weapon* firingWeapon;
453  for(int i = 0; i < this->slotCount; i++)
454  {
455    firingWeapon = this->currentSlotConfig[i].currentWeapon;
456    if( firingWeapon != NULL) firingWeapon->requestAction(WA_NONE);
457  }
[4826]458
[10036]459  /*
460  this->crosshair->setRotationSpeed(500);
461  this->crossHairSizeAnim->replay();
462  */
463}
464
[4826]465/**
[4832]466 * triggers tick of all weapons in the current weaponconfig
467 * @param second passed since last tick
[4826]468 */
[4833]469void WeaponManager::tick(float dt)
[4826]470{
[4951]471  Weapon* tickWeapon;
472
[10036]473
[4951]474  for(int i = 0; i < this->slotCount; i++)
[4826]475  {
[10121]476
477    //NICE LITTLE DEBUG FUNCTION
478    /*   if (this->currentSlotConfig[i].currentWeapon != NULL || this->currentSlotConfig[i].nextWeapon != NULL)
[6736]479      printf("%p %p\n", this->currentSlotConfig[i].currentWeapon, this->currentSlotConfig[i].nextWeapon);*/
[4951]480
[6736]481    // current Weapon in Slot i
[4951]482    tickWeapon = this->currentSlotConfig[i].currentWeapon;
[6736]483    // On A change (current != next)
484    if (tickWeapon != this->currentSlotConfig[i].nextWeapon)
[4951]485    {
[6736]486      // if a Weapon is Active in slot i, deactivate it.
487      if (tickWeapon != NULL )
[4951]488      {
[6736]489        if (tickWeapon->isActive())
[4953]490        {
[6736]491          tickWeapon->requestAction(WA_DEACTIVATE);
492          continue;
[4953]493        }
[6055]494        else
[6736]495        {
496          tickWeapon->toList(OM_NULL);
497          this->currentSlotConfig[i].currentWeapon = NULL;
498        }
[4951]499      }
[6736]500      // switching to next Weapon
501      tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
[10121]502     
[6736]503      if (tickWeapon != NULL)
504      {
[10023]505        //if (this->parent != NULL)
506        tickWeapon->toList(this->parentEntity->getOMListNumber());
[6736]507        tickWeapon->requestAction(WA_ACTIVATE);
508        this->currentSlotConfig[i].position.activateNode();
509        tickWeapon->setParent(&this->currentSlotConfig[i].position);
510      }
511      else
512        this->currentSlotConfig[i].position.deactivateNode();
[9869]513      if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID()))
[8844]514        dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
[4951]515    }
[6918]516    else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING))
517      this->currentSlotConfig[i].nextWeapon = NULL;
[4826]518  }
519}
520
521
522/**
[4832]523 * triggers draw of all weapons in the current weaponconfig
[4826]524 */
[4951]525void WeaponManager::draw() const
[4826]526{
[8315]527  assert(false || "must not be called");
[4951]528  Weapon* drawWeapon;
529  for (int i = 0; i < this->slotCount; i++)
[4826]530  {
[4951]531    drawWeapon = this->currentSlotConfig[i].currentWeapon;
532    if( drawWeapon != NULL && drawWeapon->isVisible())
533      drawWeapon->draw();
[4826]534  }
535}
536
537
538/**
[4832]539 * private gets the next free slot in a certain weaponconfig
[6669]540 * @param the selected weaponconfig -1 if none found
[4826]541 */
[5440]542int WeaponManager::getNextFreeSlot(int configID, long capability)
[4826]543{
[6676]544  if (configID == -1)
[4826]545  {
[6676]546    for (configID = 0; configID < WM_MAX_CONFIGS; configID++)
547      for( int i = 0; i < this->slotCount; ++i)
548      {
549        if( this->configs[configID][i] == NULL &&
550            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
551            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
552          return i;
[6736]553      }
[4826]554  }
[6676]555  else
556  {
557    for( int i = 0; i < this->slotCount; ++i)
558    {
559      if( this->configs[configID][i] == NULL &&
560          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
561          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
562        return i;
563    }
564  }
[4826]565  return -1;
566}
567
[9869]568CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType)
[6669]569{
570  for (unsigned int i = 0; i < this->ammo.size(); i++)
571  {
572    if (this->ammo[i]->getProjectileType() == projectileType)
573      return this->ammo[i];
574  }
575  this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType)));
576  return this->ammo.back();
577}
[4951]578
[6972]579CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon)
580{
581  assert (weapon != NULL);
[9869]582  return (this->getAmmoContainer(weapon->getClassID()));
[6972]583}
[4951]584
[6972]585
[4953]586/**
587 * outputs some nice debug information about the WeaponManager
588 */
[4951]589void WeaponManager::debug() const
590{
591  PRINT(3)("WeaponManager Debug Information\n");
592  PRINT(3)("-------------------------------\n");
593  PRINT(3)("current Config is %d\n", this->currentConfigID);
594  for (int i = 0; i < WM_MAX_CONFIGS; i++)
595  {
596    PRINT(3)("Listing Weapons in Configuration %d\n", i);
597    for (int j = 0; j < WM_MAX_SLOTS; j++)
598    {
599      if (this->configs[i][j] != NULL)
[9406]600        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassCName());
[4951]601    }
602  }
603}
Note: See TracBrowser for help on using the repository browser.