Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/weapon_manager.cc @ 7193

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

orxonox/trunk: new style for resources (prework/movement)

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