Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10766 was 10698, checked in by snellen, 18 years ago

merged adm, hud, vs-enhancements : beni's responsible for this commit. blame him!

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