Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/presentation/src/world_entities/weapons/weapon_manager.cc @ 10757

Last change on this file since 10757 was 10739, checked in by rennerc, 18 years ago

die enemy die

File size: 17.7 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  weapon->toList( this->parentEntity->getOMListNumber() );
271
272  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
273  {
274    PRINTF(2)("Slot %d of config %d is not availiabe (max: %d) searching for suitable slot\n", slotID, configID, this->slotCount);
275    if (configID >= WM_MAX_CONFIGS)
276      configID = -1;
277    if (slotID >= (int)this->slotCount)
278      slotID = -1;
279  }
280  // if no ConfigID is supplied set to Current Config.
281  if (configID <= -1)
282    configID = this->currentConfigID;
283  //
284  if (configID > -1 && slotID == -1)
285  {
286    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
287    if (slotID == -1)
288      configID = -1;
289  }
290
291  if (configID > 0 && slotID > 0 && this->configs[configID][slotID] != NULL)
292  {
293    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, remove weapon (%s::%s) first\n", configID, slotID, this->getCName(), weapon->getClassCName(), weapon->getCName());
294    return false;
295  }
296
297  if (slotID <= -1) // WM_FREE_SLOT
298  {
299    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
300    if( slotID < 0 || slotID >= this->slotCount)
301    {
302      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
303      return false;
304    }
305  }
306
307  if (!(this->currentSlotConfig[slotID]->getCapability() & weapon->getCapability() & WTYPE_ALLKINDS) &&
308      this->currentSlotConfig[slotID]->getCapability() & weapon->getCapability() & WTYPE_ALLDIRS)
309  {
310    PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n",
311              slotID, weapon->getCapability(), this->currentSlotConfig[slotID]->getCapability());
312    return false;
313  }
314
315  //! @todo check if the weapon is already assigned to another config in another slot
316  if (this->configs[configID][slotID] != NULL)
317    return false;
318
319  this->configs[configID][slotID] = weapon;
320  weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType()));
321  if(configID == this->currentConfigID)
322    this->currentSlotConfig[slotID]->setNextWeapon(weapon);
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->currentSlotConfig[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->configs[i][j] == weapon)
388          this->configs[i][j] = NULL;
389      }
390      if (this->currentSlotConfig[j]->getCurrentWeapon() == weapon)
391      {
392        this->currentSlotConfig[j]->setNextWeapon(NULL);
393      }
394    }
395  }
396}
397
398
399/**
400 * changes to the next weapon configuration
401 */
402void WeaponManager::nextWeaponConfig()
403{
404  ++this->currentConfigID;
405  if (this->currentConfigID >= WM_MAX_CONFIGS)
406    this->currentConfigID = 0;
407  this->changeWeaponConfig(this->currentConfigID);
408}
409
410/**
411 * changes to the previous configuration
412 */
413void WeaponManager::previousWeaponConfig()
414{
415  --this->currentConfigID;
416  if (this->currentConfigID < 0)
417    this->currentConfigID = WM_MAX_CONFIGS -1;
418  this->changeWeaponConfig(this->currentConfigID);
419}
420
421/**
422 * change to a desired configuration
423 * @param weaponConfig the configuration to jump to.
424 */
425void WeaponManager::changeWeaponConfig(int weaponConfig)
426{
427  this->currentConfigID = weaponConfig;
428  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
429
430  for (int i = 0; i < WM_MAX_SLOTS; i++)
431    this->currentSlotConfig[i]->setNextWeapon(this->configs[currentConfigID][i]);
432}
433
434
435/**
436 * triggers fire of all weapons in the current weaponconfig
437 */
438void WeaponManager::fire()
439{
440  Weapon* firingWeapon;
441  for(int i = 0; i < this->slotCount; i++)
442  {
443//     printf("%i ", i);
444    firingWeapon = this->currentSlotConfig[i]->getCurrentWeapon();
445    if( firingWeapon != NULL)
446    {
447      if( firingWeapon->getCurrentState() == WS_SHOOTING) continue;
448      firingWeapon->requestAction(WA_SHOOT);
449    }
450  }
451//   printf("\n");
452  /*
453        this->crosshair->setRotationSpeed(500);
454        this->crossHairSizeAnim->replay();
455  */
456}
457
458/**
459 * triggers release fire of all weapons in the current weaponconfig
460 */
461void WeaponManager::releaseFire()
462{
463  Weapon* firingWeapon;
464  for(int i = 0; i < this->slotCount; i++)
465  {
466    firingWeapon = this->currentSlotConfig[i]->getCurrentWeapon();
467    if( firingWeapon != NULL)
468      firingWeapon->requestAction(WA_NONE);
469  }
470
471  /*
472  this->crosshair->setRotationSpeed(500);
473  this->crossHairSizeAnim->replay();
474  */
475}
476
477/**
478 * triggers tick of all weapons in the current weaponconfig
479 * @param second passed since last tick
480 */
481void WeaponManager::tick(float dt)
482{
483  Weapon* tickWeapon;
484
485  for(int i = 0; i < this->slotCount; i++)
486  {
487
488    //NICE LITTLE DEBUG FUNCTION
489    /*   if (this->currentSlotConfig[i]->currentWeapon != NULL || this->currentSlotConfig[i]->nextWeapon != NULL)
490      printf("%p %p\n", this->currentSlotConfig[i]->currentWeapon, this->currentSlotConfig[i]->nextWeapon);*/
491
492    // current Weapon in Slot i
493    tickWeapon = this->currentSlotConfig[i]->getCurrentWeapon();
494    // On A change (current != next)
495    if (tickWeapon != this->currentSlotConfig[i]->getNextWeapon())
496    {
497      // if a Weapon is Active in slot i, deactivate it.
498      if (tickWeapon != NULL )
499      {
500        if (tickWeapon->isActive())
501        {
502          tickWeapon->requestAction(WA_DEACTIVATE);
503          continue;
504        }
505        else
506        {
507          tickWeapon->toList(OM_NULL);
508          this->currentSlotConfig[i]->setCurrentWeapon(NULL);
509        }
510      }
511      // switching to next Weapon
512      this->currentSlotConfig[i]->setCurrentWeapon(this->currentSlotConfig[i]->getNextWeapon());
513      tickWeapon = this->currentSlotConfig[i]->getCurrentWeapon();
514
515      if (tickWeapon != NULL)
516      {
517        //if (this->parent != NULL)
518        tickWeapon->toList(this->parentEntity->getOMListNumber());
519        tickWeapon->requestAction(WA_ACTIVATE);
520        this->currentSlotConfig[i]->activateNode();
521        tickWeapon->setParent(this->currentSlotConfig[i]);
522      }
523      else
524        this->currentSlotConfig[i]->deactivateNode();
525      if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID()))
526        dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
527    }
528    else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING))
529      this->currentSlotConfig[i]->setCurrentWeapon(NULL);
530  }
531}
532
533
534/**
535 * triggers draw of all weapons in the current weaponconfig
536 */
537void WeaponManager::draw() const
538{
539  assert(false || "must not be called");
540  Weapon* drawWeapon;
541  for (int i = 0; i < this->slotCount; i++)
542  {
543    drawWeapon = this->currentSlotConfig[i]->getCurrentWeapon();
544    if( drawWeapon != NULL && drawWeapon->isVisible())
545      drawWeapon->draw();
546  }
547}
548
549
550/**
551 * private gets the next free slot in a certain weaponconfig
552 * @param the selected weaponconfig -1 if none found
553 */
554int WeaponManager::getNextFreeSlot(int configID, long capability)
555{
556  if (configID == -1)
557  {
558    for (configID = 0; configID < WM_MAX_CONFIGS; configID++)
559      for( int i = 0; i < this->slotCount; ++i)
560      {
561        if( this->configs[configID][i] == NULL &&
562            (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLKINDS) &&
563            (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLDIRS))
564          return i;
565      }
566  }
567  else
568  {
569    for( int i = 0; i < this->slotCount; ++i)
570    {
571      if( this->configs[configID][i] == NULL &&
572          (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLKINDS) &&
573          (this->currentSlotConfig[i]->getCapability() & capability & WTYPE_ALLDIRS))
574        return i;
575    }
576  }
577  return -1;
578}
579
580CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType)
581{
582  for (unsigned int i = 0; i < this->ammo.size(); i++)
583  {
584    if (this->ammo[i]->getProjectileType() == projectileType)
585      return this->ammo[i];
586  }
587  this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType)));
588  return this->ammo.back();
589}
590
591CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon)
592{
593  assert (weapon != NULL);
594  return (this->getAmmoContainer(weapon->getClassID()));
595}
596
597
598/**
599 * outputs some nice debug information about the WeaponManager
600 */
601void WeaponManager::debug() const
602{
603  PRINT(3)("WeaponManager Debug Information\n");
604  PRINT(3)("-------------------------------\n");
605  PRINT(3)("current Config is %d\n", this->currentConfigID);
606  for (int i = 0; i < WM_MAX_CONFIGS; i++)
607  {
608    PRINT(3)("Listing Weapons in Configuration %d\n", i);
609    for (int j = 0; j < WM_MAX_SLOTS; j++)
610    {
611      if (this->configs[i][j] != NULL)
612        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassCName());
613    }
614  }
615}
616
617
618long WeaponManager::getSlotCapability(int slot) const
619{
620  return this->currentSlotConfig[slot]->getCapability();
621}
622
623const Vector& WeaponManager::getSlotPosition(int slot) const
624{
625  return this->currentSlotConfig[slot]->getRelCoor();
626}
627
628Weapon* WeaponManager::getWeapon(int slotID) const
629{
630  return (slotID >= 0 && slotID < this->slotCount)? this->currentSlotConfig[slotID]->getNextWeapon(): NULL;
631}
632
633/**
634 * processes the input
635 * @param event the Event coming as input
636 */
637// void WeaponManager::process(const Event &event)
638// {
639//   if  (event.type == EV_MOUSE_MOTION)
640//   {
641//     this->crosshair->process(event);
642// //     this->setAbsCoor2D(event.x, event.y);
643//   }
644// }
Note: See TracBrowser for help on using the repository browser.