Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

little modifications to swarm launcher

File size: 16.2 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();
[4834]118}
[4833]119
[9998]120void WeaponManager::showCrosshair()
121{
122  this->crosshair->setVisibility( true);
123}
124
125void WeaponManager::hideCrosshair()
126{
127  this->crosshair->setVisibility( false);
128}
129
[10004]130void WeaponManager::setRotationSpeed(float speed)
131{
132  this->crosshair->setRotationSpeed(speed);
133}
134
[4834]135/**
[7350]136 * @brief loads the settings of the WeaponManager
[4834]137 * @param root the XML-element to load from
138 */
139void WeaponManager::loadParams(const TiXmlElement* root)
140{
[6512]141  BaseObject::loadParams(root);
[4972]142
[5671]143  LoadParam(root, "slot-count", this, WeaponManager, setSlotCount)
[6736]144  .describe("how many slots(cannons) the WeaponManager can handle");
[4834]145
[5644]146  LOAD_PARAM_START_CYCLE(root, element);
[5654]147  {
148    // CHECK IF THIS WORKS....
149    LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons)
[6736]150    .describe("loads Weapons");
[5654]151  }
[5644]152  LOAD_PARAM_END_CYCLE(element);
[4833]153}
154
[4826]155/**
[7350]156 * @brief loads a Weapon onto the WeaponManager
[4834]157 * @param root the XML-element to load the Weapons from
158 */
159void WeaponManager::loadWeapons(const TiXmlElement* root)
160{
[5644]161  LOAD_PARAM_START_CYCLE(root, element);
[4834]162
[8315]163  BaseObject* object = Factory::fabricate(element);
164  if (object != NULL)
165  {
166    Weapon* newWeapon = dynamic_cast<Weapon*>(object);
167    if (newWeapon == NULL)
168      delete object;
169  }
[5644]170  LOAD_PARAM_END_CYCLE(element);
[4834]171}
172
[4992]173/**
[7350]174 * @brief sets the Parent of the WeaponManager.
[4992]175 * @param parent the parent of the WeaponManager
176 *
177 * this is used, to identify to which ship/man/whatever this WeaponManager is connected.
178 * also all the Slots will be subconnected to this parent.
[5435]179 *
180 * The reason this function exists is that the WeaponManager is neither a WorldEntity nor
181 * a PNode.
[4992]182 */
[8844]183void WeaponManager::setParentEntity(WorldEntity* parent)
[4953]184{
[8844]185  this->parentEntity = parent;
186  if (this->parentNode == NULL)
187    this->setParentNode(parent);
188}
189
190
191void WeaponManager::setParentNode(PNode* parent)
192{
193  this->parentNode = parent;
194  assert(parent != NULL);
195
196  if (this->parentNode != NULL)
[4953]197  {
198    for (int i = 0; i < WM_MAX_SLOTS; i++)
[8844]199      this->parentNode->addChild(&this->currentSlotConfig[i].position);
[4953]200  }
[8844]201
[4953]202}
203
[8844]204
[4834]205/**
[7350]206 * @brief sets the number of Slots the WeaponManager has
[4926]207 * @param slotCount the number of slots
[4834]208 */
[4951]209void WeaponManager::setSlotCount(unsigned int slotCount)
[4834]210{
[4951]211  if (slotCount <= WM_MAX_SLOTS)
212    this->slotCount = slotCount;
213  else
214    this->slotCount = WM_MAX_SLOTS;
[4834]215}
216
[4972]217
218/**
[7350]219 * @brief sets the position of the Slot relative to the parent
[4972]220 * @param slot the slot to set-up
221 * @param position the position of the given slot
222 */
[6803]223void WeaponManager::setSlotPosition(int slot, const Vector& position, PNode* parent)
[4953]224{
225  if (slot < this->slotCount)
[6803]226  {
[4953]227    this->currentSlotConfig[slot].position.setRelCoor(position);
[6803]228
229    if (parent != NULL)
230      this->currentSlotConfig[slot].position.setParent(parent);
231  }
[4953]232}
233
[4972]234
235/**
[7350]236 * @brief sets the relative rotation of the slot to its parent
[4972]237 * @param slot the slot to set-up
238 * @param rotation the relative rotation of the given slot
239 */
[4969]240void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
241{
242  if (slot < this->slotCount)
243    this->currentSlotConfig[slot].position.setRelDir(rotation);
244}
245
246
[4834]247/**
[7350]248 * @brief adds a weapon to the selected weaponconfiguration into the selected slot
[4972]249 * @param weapon the weapon to add
250 * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
251 * @param slotID an identifier for the weapon configuration, number between 0..3
[4832]252 *
253 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
[4906]254 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
[4832]255 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
256 * a error message.
[4826]257 */
[6561]258bool WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
[4826]259{
[6753]260  if ( weapon == NULL )
[6737]261    return false;
[6679]262
[4951]263  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
[4826]264  {
[6679]265    PRINTF(2)("Slot %d of config %d is not availiabe (max: %d) searching for suitable slot\n", slotID, configID, this->slotCount);
266    if (configID >= WM_MAX_CONFIGS)
267      configID = -1;
268    if (slotID >= (int)this->slotCount)
269      slotID = -1;
[4951]270  }
[6679]271  // if no ConfigID is supplied set to Current Config.
272  if (configID <= -1)
273    configID = this->currentConfigID;
274  //
275  if (configID > -1 && slotID == -1)
276  {
277    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
278    if (slotID == -1)
279      configID = -1;
280  }
[4951]281
[6676]282  if (configID > 0 && slotID > 0 && this->configs[configID][slotID] != NULL)
283  {
[9406]284    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]285    return false;
286  }
[4951]287
[6676]288  if (slotID <= -1) // WM_FREE_SLOT
[4951]289  {
[5441]290    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
[4951]291    if( slotID < 0 || slotID >= this->slotCount)
[4826]292    {
[5441]293      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
[6561]294      return false;
[4826]295    }
296  }
[4953]297
[5441]298  if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) &&
[6736]299      this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS)
[5441]300  {
301    PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n",
302              slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability);
[6561]303    return false;
[5441]304  }
305
[4953]306  //! @todo check if the weapon is already assigned to another config in another slot
[6714]307  if (this->configs[configID][slotID] != NULL)
308    return false;
[6676]309
[4951]310  this->configs[configID][slotID] = weapon;
[6669]311  weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType()));
[6736]312  if(configID == this->currentConfigID)
313    this->currentSlotConfig[slotID].nextWeapon = weapon;
[8844]314  //if (this->parent != NULL)
[6142]315  {
[8844]316    this->parentNode->addChild(weapon);
[9869]317    if (this->parentEntity->isA(Playable::staticClassID()))
[8844]318      dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
[9998]319   
[6920]320    weapon->setDefaultTarget(this->crosshair);
[6142]321  }
[9869]322  PRINTF(4)("Added a new Weapon (%s::%s) to the WeaponManager: config %i/ slot %i\n", weapon->getClassCName(), weapon->getCName(), configID, slotID);
[6561]323  return true;
[4826]324}
325
[4834]326/**
[6931]327 * @brief increases the Energy of the WeaponContainer of type (projectileType)
328 * @param projectileType the type of weapon to increase Energy from
329 * @param ammo the ammo to increase
330 */
[9869]331float WeaponManager::increaseAmmunition(const ClassID& projectileType, float ammo)
[6931]332{
333  return this->getAmmoContainer(projectileType)->increaseEnergy(ammo);
334}
335
[6972]336/**
[9965]337 * @brief does the same as the funtion increaseAmmunition, added four your convenience
[6972]338 * @param weapon, the Weapon to read the ammo-info about.
339 * @param ammo how much ammo to add.
340 */
[9965]341float WeaponManager::increaseAmmunition(const Weapon* weapon, float ammo)
[6972]342{
343  assert (weapon != NULL);
[9869]344  return this->increaseAmmunition(weapon->getClassID(), ammo);
[6931]345
[6972]346}
[6931]347
[6972]348
[6931]349/**
[4954]350 * sets the capabilities of a Slot
351 * @param slot the slot to set the capability
352 * @param slotCapability the capability @see WM_SlotCapability
353 */
354void WeaponManager::setSlotCapability(int slot, long slotCapability)
355{
356  if (slot > slotCount)
357    return;
358  this->currentSlotConfig[slot].capability = slotCapability;
359}
360
361
362/**
[4834]363 * removes a Weapon from the WeaponManager
[4954]364 *
365 * !! The weapon must be inactive before you can delete it,    !!
366 * !! because it will still be deactivated (if it is selected) !!
[4834]367 */
[4826]368void WeaponManager::removeWeapon(Weapon* weapon, int configID)
369{
[4954]370  if (weapon == NULL)
371    return;
372  if (configID < 0)
373  {
374    for (int j = 0; j < WM_MAX_SLOTS; j++)
375    {
376      for (int i = 0; i < WM_MAX_CONFIGS; i++)
377      {
378        if (this->configs[i][j] == weapon)
379          this->configs[i][j] = NULL;
380      }
381      if (this->currentSlotConfig[j].currentWeapon == weapon)
382      {
383        this->currentSlotConfig[j].nextWeapon = NULL;
384      }
385    }
386  }
[4826]387}
388
389
390/**
[4832]391 * changes to the next weapon configuration
[4826]392 */
[4954]393void WeaponManager::nextWeaponConfig()
[4826]394{
[4951]395  ++this->currentConfigID;
396  if (this->currentConfigID >= WM_MAX_CONFIGS)
397    this->currentConfigID = 0;
[4952]398  this->changeWeaponConfig(this->currentConfigID);
399}
[4826]400
[4953]401/**
402 * changes to the previous configuration
403 */
[4952]404void WeaponManager::previousWeaponConfig()
405{
406  --this->currentConfigID;
407  if (this->currentConfigID < 0)
408    this->currentConfigID = WM_MAX_CONFIGS -1;
409  this->changeWeaponConfig(this->currentConfigID);
410}
411
[4953]412/**
413 * change to a desired configuration
414 * @param weaponConfig the configuration to jump to.
415 */
[4952]416void WeaponManager::changeWeaponConfig(int weaponConfig)
417{
418  this->currentConfigID = weaponConfig;
419  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
[4951]420  for (int i = 0; i < WM_MAX_SLOTS; i++)
421    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
[4826]422}
423
424
425/**
[4832]426 * triggers fire of all weapons in the current weaponconfig
[4826]427 */
[4832]428void WeaponManager::fire()
[4826]429{
430  Weapon* firingWeapon;
[4951]431  for(int i = 0; i < this->slotCount; i++)
[4826]432  {
[9961]433          firingWeapon = this->currentSlotConfig[i].currentWeapon;
434          if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
[4826]435  }
[9998]436
437  /*
438        this->crosshair->setRotationSpeed(500);
439        this->crossHairSizeAnim->replay();
[10004]440  */
[4826]441}
442
443
444/**
[4832]445 * triggers tick of all weapons in the current weaponconfig
446 * @param second passed since last tick
[4826]447 */
[4833]448void WeaponManager::tick(float dt)
[4826]449{
[4951]450  Weapon* tickWeapon;
451
452  for(int i = 0; i < this->slotCount; i++)
[4826]453  {
[6736]454/*
455    NICE LITTLE DEBUG FUNCTION
456       if (this->currentSlotConfig[i].currentWeapon != NULL || this->currentSlotConfig[i].nextWeapon != NULL)
457      printf("%p %p\n", this->currentSlotConfig[i].currentWeapon, this->currentSlotConfig[i].nextWeapon);*/
[4951]458
[6736]459    // current Weapon in Slot i
[4951]460    tickWeapon = this->currentSlotConfig[i].currentWeapon;
[6736]461    // On A change (current != next)
462    if (tickWeapon != this->currentSlotConfig[i].nextWeapon)
[4951]463    {
[6736]464      // if a Weapon is Active in slot i, deactivate it.
465      if (tickWeapon != NULL )
[4951]466      {
[6736]467        if (tickWeapon->isActive())
[4953]468        {
[6736]469          tickWeapon->requestAction(WA_DEACTIVATE);
470          continue;
[4953]471        }
[6055]472        else
[6736]473        {
474          tickWeapon->toList(OM_NULL);
475          this->currentSlotConfig[i].currentWeapon = NULL;
476        }
[4951]477      }
[6736]478
479      // switching to next Weapon
480      tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
481      if (tickWeapon != NULL)
482      {
[10023]483        //if (this->parent != NULL)
484        tickWeapon->toList(this->parentEntity->getOMListNumber());
[6736]485        tickWeapon->requestAction(WA_ACTIVATE);
486        this->currentSlotConfig[i].position.activateNode();
487        tickWeapon->setParent(&this->currentSlotConfig[i].position);
488      }
489      else
490        this->currentSlotConfig[i].position.deactivateNode();
[9869]491      if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID()))
[8844]492        dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
[4951]493    }
[6918]494    else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING))
495      this->currentSlotConfig[i].nextWeapon = NULL;
[4826]496  }
497}
498
499
500/**
[4832]501 * triggers draw of all weapons in the current weaponconfig
[4826]502 */
[4951]503void WeaponManager::draw() const
[4826]504{
[8315]505  assert(false || "must not be called");
[4951]506  Weapon* drawWeapon;
507  for (int i = 0; i < this->slotCount; i++)
[4826]508  {
[4951]509    drawWeapon = this->currentSlotConfig[i].currentWeapon;
510    if( drawWeapon != NULL && drawWeapon->isVisible())
511      drawWeapon->draw();
[4826]512  }
513}
514
515
516/**
[4832]517 * private gets the next free slot in a certain weaponconfig
[6669]518 * @param the selected weaponconfig -1 if none found
[4826]519 */
[5440]520int WeaponManager::getNextFreeSlot(int configID, long capability)
[4826]521{
[6676]522  if (configID == -1)
[4826]523  {
[6676]524    for (configID = 0; configID < WM_MAX_CONFIGS; configID++)
525      for( int i = 0; i < this->slotCount; ++i)
526      {
527        if( this->configs[configID][i] == NULL &&
528            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
529            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
530          return i;
[6736]531      }
[4826]532  }
[6676]533  else
534  {
535    for( int i = 0; i < this->slotCount; ++i)
536    {
537      if( this->configs[configID][i] == NULL &&
538          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
539          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
540        return i;
541    }
542  }
[4826]543  return -1;
544}
545
[9869]546CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType)
[6669]547{
548  for (unsigned int i = 0; i < this->ammo.size(); i++)
549  {
550    if (this->ammo[i]->getProjectileType() == projectileType)
551      return this->ammo[i];
552  }
553  this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType)));
554  return this->ammo.back();
555}
[4951]556
[6972]557CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon)
558{
559  assert (weapon != NULL);
[9869]560  return (this->getAmmoContainer(weapon->getClassID()));
[6972]561}
[4951]562
[6972]563
[4953]564/**
565 * outputs some nice debug information about the WeaponManager
566 */
[4951]567void WeaponManager::debug() const
568{
569  PRINT(3)("WeaponManager Debug Information\n");
570  PRINT(3)("-------------------------------\n");
571  PRINT(3)("current Config is %d\n", this->currentConfigID);
572  for (int i = 0; i < WM_MAX_CONFIGS; i++)
573  {
574    PRINT(3)("Listing Weapons in Configuration %d\n", i);
575    for (int j = 0; j < WM_MAX_SLOTS; j++)
576    {
577      if (this->configs[i][j] != NULL)
[9406]578        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassCName());
[4951]579    }
580  }
581}
Note: See TracBrowser for help on using the repository browser.