Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

Deactivate Fix

File size: 16.6 KB
Line 
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
14   co-programmer: Benjamin Grauer
15
16   2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons.
17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
20
21#include "weapon_manager.h"
22#include "weapon.h"
23#include "crosshair.h"
24
25#include "playable.h"
26
27#include "util/loading/load_param_xml.h"
28#include "util/loading/factory.h"
29
30#include "t_animation.h"
31
32
33ObjectListDefinition(WeaponManager);
34/**
35 * @brief this initializes the weaponManager for a given nnumber of weapon slots
36 * @param number of weapon slots of the model/ship <= 8 (limitied)
37 */
38WeaponManager::WeaponManager(WorldEntity* parent)
39{
40  this->init();
41  this->setParentEntity(parent);
42
43  assert (parent != NULL);
44}
45
46WeaponManager::WeaponManager(const TiXmlElement* root)
47{
48  this->init();
49  this->loadParams(root);
50}
51
52/**
53 * @brief Destroys a WeaponManager
54 */
55WeaponManager::~WeaponManager()
56{
57  // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.)
58  // rennerc: crosshair seems not to delete itselve
59  //if (Crosshair::objectList().exists(this->crosshair))
60  //  delete this->crosshair;
61}
62
63/**
64 * @brief initializes the WeaponManager
65 */
66void WeaponManager::init()
67{
68  this->registerObject(this, WeaponManager::_objectList);
69
70  this->parentNode = NULL;
71  this->parentEntity = NULL;
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++)
78  {
79    this->currentSlotConfig[i].capability = WTYPE_ALL;
80    this->currentSlotConfig[i].currentWeapon = NULL;
81    this->currentSlotConfig[i].nextWeapon = NULL;
82
83    // NAMING
84    char* tmpName;
85    if (!this->getName().empty())
86    {
87      tmpName = new char[this->getName().size() + 10];
88      sprintf(tmpName, "%s_slot%d", this->getCName(), i);
89    }
90    else
91    {
92      tmpName = new char[30];
93      sprintf(tmpName, "WeaponMan_slot%d", i);
94    }
95    this->currentSlotConfig[i].position.setName(tmpName);
96    this->currentSlotConfig[i].position.deactivateNode();
97    delete[] tmpName;
98  }
99
100  for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++)
101    this->availiableWeapons[i] = NULL;
102
103
104  this->currentConfigID = 0;
105  this->slotCount = WM_MAX_SLOTS;
106  //this->weaponChange;
107
108  // CROSSHAIR INITIALISATION
109  this->crosshair = new Crosshair();
110  //this->crosshair->setRelCoor(1000,0,0);
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);
116
117  this->hideCrosshair();
118
119  this->bFire = false;
120}
121
122void WeaponManager::showCrosshair()
123{
124  this->crosshair->setVisibility( true);
125}
126
127void WeaponManager::hideCrosshair()
128{
129  this->crosshair->setVisibility( false);
130}
131
132void WeaponManager::setRotationSpeed(float speed)
133{
134  this->crosshair->setRotationSpeed(speed);
135}
136
137/**
138 * @brief loads the settings of the WeaponManager
139 * @param root the XML-element to load from
140 */
141void WeaponManager::loadParams(const TiXmlElement* root)
142{
143  BaseObject::loadParams(root);
144
145  LoadParam(root, "slot-count", this, WeaponManager, setSlotCount)
146  .describe("how many slots(cannons) the WeaponManager can handle");
147
148  LOAD_PARAM_START_CYCLE(root, element);
149  {
150    // CHECK IF THIS WORKS....
151    LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons)
152    .describe("loads Weapons");
153  }
154  LOAD_PARAM_END_CYCLE(element);
155}
156
157/**
158 * @brief loads a Weapon onto the WeaponManager
159 * @param root the XML-element to load the Weapons from
160 */
161void WeaponManager::loadWeapons(const TiXmlElement* root)
162{
163  LOAD_PARAM_START_CYCLE(root, element);
164
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  }
172  LOAD_PARAM_END_CYCLE(element);
173}
174
175/**
176 * @brief sets the Parent of the WeaponManager.
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.
181 *
182 * The reason this function exists is that the WeaponManager is neither a WorldEntity nor
183 * a PNode.
184 */
185void WeaponManager::setParentEntity(WorldEntity* parent)
186{
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)
199  {
200    for (int i = 0; i < WM_MAX_SLOTS; i++)
201      this->parentNode->addChild(&this->currentSlotConfig[i].position);
202  }
203
204}
205
206
207/**
208 * @brief sets the number of Slots the WeaponManager has
209 * @param slotCount the number of slots
210 */
211void WeaponManager::setSlotCount(unsigned int slotCount)
212{
213  if (slotCount <= WM_MAX_SLOTS)
214    this->slotCount = slotCount;
215  else
216    this->slotCount = WM_MAX_SLOTS;
217}
218
219
220/**
221 * @brief sets the position of the Slot relative to the parent
222 * @param slot the slot to set-up
223 * @param position the position of the given slot
224 */
225void WeaponManager::setSlotPosition(int slot, const Vector& position, PNode* parent)
226{
227  if (slot < this->slotCount)
228  {
229    this->currentSlotConfig[slot].position.setRelCoor(position);
230
231    if (parent != NULL)
232      this->currentSlotConfig[slot].position.setParent(parent);
233  }
234}
235
236
237/**
238 * @brief sets the relative rotation of the slot to its parent
239 * @param slot the slot to set-up
240 * @param rotation the relative rotation of the given slot
241 */
242void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
243{
244  if (slot < this->slotCount)
245    this->currentSlotConfig[slot].position.setRelDir(rotation);
246}
247
248
249/**
250 * @brief adds a weapon to the selected weaponconfiguration into the selected slot
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
254 *
255 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
256 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
257 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
258 * a error message.
259 */
260bool WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
261{
262  if ( weapon == NULL )
263    return false;
264
265  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
266  {
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;
272  }
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  }
283
284  if (configID > 0 && slotID > 0 && this->configs[configID][slotID] != NULL)
285  {
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());
287    return false;
288  }
289
290  if (slotID <= -1) // WM_FREE_SLOT
291  {
292    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
293    if( slotID < 0 || slotID >= this->slotCount)
294    {
295      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
296      return false;
297    }
298  }
299
300  if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) &&
301      this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS)
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);
305    return false;
306  }
307
308  //! @todo check if the weapon is already assigned to another config in another slot
309  if (this->configs[configID][slotID] != NULL)
310    return false;
311
312  this->configs[configID][slotID] = weapon;
313  weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType()));
314  if(configID == this->currentConfigID)
315    this->currentSlotConfig[slotID].nextWeapon = weapon;
316  //if (this->parent != NULL)
317  {
318    this->parentNode->addChild(weapon);
319    if (this->parentEntity->isA(Playable::staticClassID()))
320      dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
321   
322    weapon->setDefaultTarget(this->crosshair);
323  }
324  PRINTF(4)("Added a new Weapon (%s::%s) to the WeaponManager: config %i/ slot %i\n", weapon->getClassCName(), weapon->getCName(), configID, slotID);
325  return true;
326}
327
328/**
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 */
333float WeaponManager::increaseAmmunition(const ClassID& projectileType, float ammo)
334{
335  return this->getAmmoContainer(projectileType)->increaseEnergy(ammo);
336}
337
338/**
339 * @brief does the same as the funtion increaseAmmunition, added four your convenience
340 * @param weapon, the Weapon to read the ammo-info about.
341 * @param ammo how much ammo to add.
342 */
343float WeaponManager::increaseAmmunition(const Weapon* weapon, float ammo)
344{
345  assert (weapon != NULL);
346  return this->increaseAmmunition(weapon->getClassID(), ammo);
347
348}
349
350
351/**
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/**
365 * removes a Weapon from the WeaponManager
366 *
367 * !! The weapon must be inactive before you can delete it,    !!
368 * !! because it will still be deactivated (if it is selected) !!
369 */
370void WeaponManager::removeWeapon(Weapon* weapon, int configID)
371{
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  }
389}
390
391
392/**
393 * changes to the next weapon configuration
394 */
395void WeaponManager::nextWeaponConfig()
396{
397  ++this->currentConfigID;
398  if (this->currentConfigID >= WM_MAX_CONFIGS)
399    this->currentConfigID = 0;
400  this->changeWeaponConfig(this->currentConfigID);
401}
402
403/**
404 * changes to the previous configuration
405 */
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
414/**
415 * change to a desired configuration
416 * @param weaponConfig the configuration to jump to.
417 */
418void WeaponManager::changeWeaponConfig(int weaponConfig)
419{
420  this->currentConfigID = weaponConfig;
421  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
422
423  for (int i = 0; i < WM_MAX_SLOTS; i++)
424    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
425}
426
427
428/**
429 * triggers fire of all weapons in the current weaponconfig
430 */
431void WeaponManager::fire()
432{
433  Weapon* firingWeapon;
434  for(int i = 0; i < this->slotCount; i++)
435  {
436          firingWeapon = this->currentSlotConfig[i].currentWeapon;
437          if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
438  }
439
440  /*
441        this->crosshair->setRotationSpeed(500);
442        this->crossHairSizeAnim->replay();
443  */
444}
445
446/**
447 * triggers fire of all weapons in the current weaponconfig
448 */
449void WeaponManager::releaseFire()
450{
451  Weapon* firingWeapon;
452  for(int i = 0; i < this->slotCount; i++)
453  {
454    firingWeapon = this->currentSlotConfig[i].currentWeapon;
455    if( firingWeapon != NULL) firingWeapon->requestAction(WA_NONE);
456  }
457
458  /*
459  this->crosshair->setRotationSpeed(500);
460  this->crossHairSizeAnim->replay();
461  */
462}
463
464/**
465 * triggers tick of all weapons in the current weaponconfig
466 * @param second passed since last tick
467 */
468void WeaponManager::tick(float dt)
469{
470  Weapon* tickWeapon;
471
472
473  for(int i = 0; i < this->slotCount; i++)
474  {
475
476    //NICE LITTLE DEBUG FUNCTION
477    /*   if (this->currentSlotConfig[i].currentWeapon != NULL || this->currentSlotConfig[i].nextWeapon != NULL)
478      printf("%p %p\n", this->currentSlotConfig[i].currentWeapon, this->currentSlotConfig[i].nextWeapon);*/
479
480    // current Weapon in Slot i
481    tickWeapon = this->currentSlotConfig[i].currentWeapon;
482    // On A change (current != next)
483    if (tickWeapon != this->currentSlotConfig[i].nextWeapon)
484    {
485      // if a Weapon is Active in slot i, deactivate it.
486      if (tickWeapon != NULL )
487      {
488        if (tickWeapon->isActive())
489        {
490          tickWeapon->requestAction(WA_DEACTIVATE);
491          continue;
492        }
493        else
494        {
495          tickWeapon->toList(OM_NULL);
496          this->currentSlotConfig[i].currentWeapon = NULL;
497        }
498      }
499      // switching to next Weapon
500      tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
501     
502      if (tickWeapon != NULL)
503      {
504        //if (this->parent != NULL)
505        tickWeapon->toList(this->parentEntity->getOMListNumber());
506        tickWeapon->requestAction(WA_ACTIVATE);
507        this->currentSlotConfig[i].position.activateNode();
508        tickWeapon->setParent(&this->currentSlotConfig[i].position);
509      }
510      else
511        this->currentSlotConfig[i].position.deactivateNode();
512      if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID()))
513        dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
514    }
515    else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING))
516      this->currentSlotConfig[i].nextWeapon = NULL;
517  }
518}
519
520
521/**
522 * triggers draw of all weapons in the current weaponconfig
523 */
524void WeaponManager::draw() const
525{
526  assert(false || "must not be called");
527  Weapon* drawWeapon;
528  for (int i = 0; i < this->slotCount; i++)
529  {
530    drawWeapon = this->currentSlotConfig[i].currentWeapon;
531    if( drawWeapon != NULL && drawWeapon->isVisible())
532      drawWeapon->draw();
533  }
534}
535
536
537/**
538 * private gets the next free slot in a certain weaponconfig
539 * @param the selected weaponconfig -1 if none found
540 */
541int WeaponManager::getNextFreeSlot(int configID, long capability)
542{
543  if (configID == -1)
544  {
545    for (configID = 0; configID < WM_MAX_CONFIGS; configID++)
546      for( int i = 0; i < this->slotCount; ++i)
547      {
548        if( this->configs[configID][i] == NULL &&
549            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
550            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
551          return i;
552      }
553  }
554  else
555  {
556    for( int i = 0; i < this->slotCount; ++i)
557    {
558      if( this->configs[configID][i] == NULL &&
559          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
560          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
561        return i;
562    }
563  }
564  return -1;
565}
566
567CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType)
568{
569  for (unsigned int i = 0; i < this->ammo.size(); i++)
570  {
571    if (this->ammo[i]->getProjectileType() == projectileType)
572      return this->ammo[i];
573  }
574  this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType)));
575  return this->ammo.back();
576}
577
578CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon)
579{
580  assert (weapon != NULL);
581  return (this->getAmmoContainer(weapon->getClassID()));
582}
583
584
585/**
586 * outputs some nice debug information about the WeaponManager
587 */
588void WeaponManager::debug() const
589{
590  PRINT(3)("WeaponManager Debug Information\n");
591  PRINT(3)("-------------------------------\n");
592  PRINT(3)("current Config is %d\n", this->currentConfigID);
593  for (int i = 0; i < WM_MAX_CONFIGS; i++)
594  {
595    PRINT(3)("Listing Weapons in Configuration %d\n", i);
596    for (int j = 0; j < WM_MAX_SLOTS; j++)
597    {
598      if (this->configs[i][j] != NULL)
599        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassCName());
600    }
601  }
602}
Note: See TracBrowser for help on using the repository browser.