Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/hud/src/world_entities/weapons/weapon_manager.cc @ 10649

Last change on this file since 10649 was 10544, checked in by nicolasc, 18 years ago

bump

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