Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: merge the ObjectManager to the trunk
merged with command:
svn merge -r6082:HEAD objectmanager/ ../trunk/

conflicts resolution was easy this time :)
but specially merged the world to network_world

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