Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/vs-enhencements/src/world_entities/weapons/weapon_manager.cc @ 10692

Last change on this file since 10692 was 10692, checked in by nicolasc, 17 years ago

xfer, WFM…

File size: 17.4 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->slotConfigs[i] = new WeaponSlot();
81    this->slotConfigs[i]->setCapability(WTYPE_ALL);
82//     this->slotConfigs[i]->setCurrentWeapon(NULL);
83//     this->slotConfigs[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->slotConfigs[i]->setName(tmpName);
98    this->slotConfigs[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->slotConfigs[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->slotConfigs[slot]->setRelCoor(position);
235
236    if (parent != NULL)
237      this->slotConfigs[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->slotConfigs[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->slotConfigs[slotID]->getWeapon(configID) != 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->slotConfigs[slotID]->getCapability() & weapon->getCapability() & WTYPE_ALLKINDS) &&
306      this->slotConfigs[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->slotConfigs[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  if( this->slotConfigs[slotID]->getWeapon(configID) != NULL )
317    return false;
318
319  this->slotConfigs[slotID]->setWeapon(weapon, configID);
320  weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType()));
321  if(configID == this->currentConfigID)
322    this->slotConfigs[slotID]->setNextWeapon(configID);
323  //if (this->parent != NULL)
324  {
325    this->parentNode->addChild(weapon);
326    if (this->parentEntity->isA(Playable::staticClassID()))
327      dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
328
329    weapon->setDefaultTarget(this->crosshair);
330  }
331  PRINTF(4)("Added a new Weapon (%s::%s) to the WeaponManager: config %i/ slot %i\n", weapon->getClassCName(), weapon->getCName(), configID, slotID);
332  return true;
333}
334
335/**
336 * @brief increases the Energy of the WeaponContainer of type (projectileType)
337 * @param projectileType the type of weapon to increase Energy from
338 * @param ammo the ammo to increase
339 */
340float WeaponManager::increaseAmmunition(const ClassID& projectileType, float ammo)
341{
342  return this->getAmmoContainer(projectileType)->increaseEnergy(ammo);
343}
344
345/**
346 * @brief does the same as the funtion increaseAmmunition, added four your convenience
347 * @param weapon, the Weapon to read the ammo-info about.
348 * @param ammo how much ammo to add.
349 */
350float WeaponManager::increaseAmmunition(const Weapon* weapon, float ammo)
351{
352  assert (weapon != NULL);
353  return this->increaseAmmunition(weapon->getClassID(), ammo);
354
355}
356
357
358/**
359 * sets the capabilities of a Slot
360 * @param slot the slot to set the capability
361 * @param slotCapability the capability @see WeaponSlotCapability
362 */
363void WeaponManager::setSlotCapability(int slot, unsigned long slotCapability)
364{
365  if (slot > slotCount)
366    return;
367  this->slotConfigs[slot]->setCapability(slotCapability);
368}
369
370
371/**
372 * removes a Weapon from the WeaponManager
373 *
374 * !! The weapon must be inactive before you can delete it,    !!
375 * !! because it will still be deactivated (if it is selected) !!
376 */
377void WeaponManager::removeWeapon(Weapon* weapon, int configID)
378{
379  if (weapon == NULL)
380    return;
381  if (configID < 0)
382  {
383    for (int j = 0; j < WM_MAX_SLOTS; j++)
384    {
385      for (int i = 0; i < WM_MAX_CONFIGS; i++)
386      {
387        if (this->slotConfigs[j]->getWeapon(i) == weapon)
388          this->slotConfigs[j]->setWeapon(NULL, i);
389      }
390//       if (this->slotConfigs[j]->getCurrentWeapon() == weapon)
391//       {
392//         this->slotConfigs[j]->setNextWeapon(NULL);
393//       }
394    }
395  }
396  else {
397
398  }
399}
400
401
402/**
403 * changes to the next weapon configuration
404 */
405void WeaponManager::nextWeaponConfig()
406{
407  ++this->currentConfigID;
408  if (this->currentConfigID >= WM_MAX_CONFIGS)
409    this->currentConfigID = 0;
410  this->changeWeaponConfig(this->currentConfigID);
411}
412
413/**
414 * changes to the previous configuration
415 */
416void WeaponManager::previousWeaponConfig()
417{
418  --this->currentConfigID;
419  if (this->currentConfigID < 0)
420    this->currentConfigID = WM_MAX_CONFIGS -1;
421  this->changeWeaponConfig(this->currentConfigID);
422}
423
424/**
425 * change to a desired configuration
426 * @param weaponConfig the configuration to jump to.
427 */
428void WeaponManager::changeWeaponConfig(int weaponConfig)
429{
430  this->currentConfigID = weaponConfig;
431  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
432
433  for (int i = 0; i < WM_MAX_SLOTS; i++)
434    this->slotConfigs[i]->setNextWeapon(currentConfigID);
435}
436
437
438/**
439 * triggers fire of all weapons in the current weaponconfig
440 */
441void WeaponManager::fire()
442{
443  Weapon* firingWeapon;
444  for(int i = 0; i < this->slotCount; i++)
445  {
446//     printf("%i ", i);
447    firingWeapon = this->slotConfigs[i]->getCurrentWeapon();
448    if( firingWeapon != NULL)
449    {
450      if( firingWeapon->getCurrentState() == WS_SHOOTING) continue;
451      firingWeapon->requestAction(WA_SHOOT);
452    }
453  }
454//   printf("\n");
455  /*
456  this->crosshair->setRotationSpeed(500);
457  this->crossHairSizeAnim->replay();
458  */
459}
460
461/**
462 * triggers release fire of all weapons in the current weaponconfig
463 */
464void WeaponManager::releaseFire()
465{
466  Weapon* firingWeapon;
467  for(int i = 0; i < this->slotCount; i++)
468  {
469    firingWeapon = this->slotConfigs[i]->getCurrentWeapon();
470    if( firingWeapon != NULL)
471      firingWeapon->requestAction(WA_NONE);
472  }
473
474  /*
475  this->crosshair->setRotationSpeed(500);
476  this->crossHairSizeAnim->replay();
477  */
478}
479
480/**
481 * triggers tick of all weapons in the current weaponconfig
482 * @param second passed since last tick
483 */
484void WeaponManager::tick(float dt)
485{
486  Weapon* tickWeapon;
487
488  for(int i = 0; i < this->slotCount; i++)
489  {
490    //NICE LITTLE DEBUG FUNCTION
491    /*   if (this->slotConfigs[i]->currentWeapon != NULL || this->slotConfigs[i]->nextWeapon != NULL)
492      printf("%p %p\n", this->slotConfigs[i]->currentWeapon, this->slotConfigs[i]->nextWeapon);*/
493
494    // current Weapon in Slot i
495    tickWeapon = this->slotConfigs[i]->getCurrentWeapon();
496    // On A change (current != next)
497    if (tickWeapon != this->slotConfigs[i]->getNextWeapon())
498    {
499      // if a Weapon is Active in slot i, deactivate it.
500      if (tickWeapon != NULL )
501      {
502        if (tickWeapon->isActive())
503        {
504          tickWeapon->requestAction(WA_DEACTIVATE);
505          continue;
506        }
507        else
508        {
509          tickWeapon->toList(OM_NULL);
510          this->slotConfigs[i]->setCurrentWeapon(-1);
511        }
512      }
513      // switching to next Weapon
514//       this->slotConfigs[i]->setCurrentWeapon(this->slotConfigs[i]->getNextWeapon());
515      this->slotConfigs[i]->setNextToCurrent();
516      tickWeapon = this->slotConfigs[i]->getCurrentWeapon();
517
518      if (tickWeapon != NULL)
519      {
520        //if (this->parent != NULL)
521        tickWeapon->toList(this->parentEntity->getOMListNumber());
522        tickWeapon->requestAction(WA_ACTIVATE);
523        this->slotConfigs[i]->activateNode();
524        tickWeapon->setParent(this->slotConfigs[i]);
525      }
526      else
527        this->slotConfigs[i]->deactivateNode();
528      if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID()))
529        dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
530    }
531    else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING))
532      this->slotConfigs[i]->setCurrentWeapon(-1);
533  }
534}
535
536
537/**
538 * triggers draw of all weapons in the current weaponconfig
539 */
540void WeaponManager::draw() const
541{
542  assert(false || "must not be called");
543  Weapon* drawWeapon;
544  for (int i = 0; i < this->slotCount; i++)
545  {
546    drawWeapon = this->slotConfigs[i]->getCurrentWeapon();
547    if( drawWeapon != NULL && drawWeapon->isVisible())
548      drawWeapon->draw();
549  }
550}
551
552
553/**
554 * private gets the next free slot in a certain weaponconfig
555 * @param the selected weaponconfig -1 if none found
556 */
557int WeaponManager::getNextFreeSlot(int configID, long capability)
558{
559  if (configID == -1)
560  {
561    for (configID = 0; configID < WM_MAX_CONFIGS; configID++)
562      for( int i = 0; i < this->slotCount; ++i)
563      {
564        if( this->slotConfigs[i]->getWeapon(configID) == NULL &&
565            (this->slotConfigs[i]->getCapability() & capability & WTYPE_ALLKINDS) &&
566            (this->slotConfigs[i]->getCapability() & capability & WTYPE_ALLDIRS))
567          return i;
568      }
569  }
570  else
571  {
572    for( int i = 0; i < this->slotCount; ++i)
573    {
574      if( this->slotConfigs[i]->getWeapon(configID) == NULL &&
575          (this->slotConfigs[i]->getCapability() & capability & WTYPE_ALLKINDS) &&
576          (this->slotConfigs[i]->getCapability() & capability & WTYPE_ALLDIRS))
577        return i;
578    }
579  }
580  return -1;
581}
582
583CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType)
584{
585  for (unsigned int i = 0; i < this->ammo.size(); i++)
586  {
587    if (this->ammo[i]->getProjectileType() == projectileType)
588      return this->ammo[i];
589  }
590  this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType)));
591  return this->ammo.back();
592}
593
594CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon)
595{
596  assert (weapon != NULL);
597  return (this->getAmmoContainer(weapon->getClassID()));
598}
599
600
601/**
602 * outputs some nice debug information about the WeaponManager
603 */
604void WeaponManager::debug() const
605{
606  PRINT(3)("WeaponManager Debug Information\n");
607  PRINT(3)("-------------------------------\n");
608  PRINT(3)("current Config is %d\n", this->currentConfigID);
609  for (int i = 0; i < WM_MAX_CONFIGS; i++)
610  {
611    PRINT(3)("Listing Weapons in Configuration %d\n", i);
612    for (int j = 0; j < WM_MAX_SLOTS; j++)
613    {
614      if (this->slotConfigs[j]->getWeapon(i) != NULL)
615        PRINT(3)("Slot %d loaded a %s\n", j, this->slotConfigs[j]->getWeapon(i)->getClassCName());
616    }
617  }
618}
619
620
621long WeaponManager::getSlotCapability(int slot) const
622{
623  return this->slotConfigs[slot]->getCapability();
624}
625
626const Vector& WeaponManager::getSlotPosition(int slot) const
627{
628  return this->slotConfigs[slot]->getRelCoor();
629}
630
631Weapon* WeaponManager::getWeapon(int slotID) const
632{
633  return (slotID >= 0 && slotID < this->slotCount)? this->slotConfigs[slotID]->getNextWeapon(): NULL;
634}
635
636
Note: See TracBrowser for help on using the repository browser.