Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10581 was 10252, checked in by nicolasc, 18 years ago

bump

File size: 16.7 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//   printf("firing WM: ");
434  Weapon* firingWeapon;
435  for(int i = 0; i < this->slotCount; i++)
436  {
437//     printf("%i ", i);
438          firingWeapon = this->currentSlotConfig[i].currentWeapon;
439      if( firingWeapon != NULL && firingWeapon->getCurrentState() == WS_SHOOTING) continue;
440          if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
441  }
442//   printf("\n");
443  /*
444        this->crosshair->setRotationSpeed(500);
445        this->crossHairSizeAnim->replay();
446  */
447}
448
449/**
450 * triggers fire of all weapons in the current weaponconfig
451 */
452void WeaponManager::releaseFire()
453{
454  Weapon* firingWeapon;
455  for(int i = 0; i < this->slotCount; i++)
456  {
457    firingWeapon = this->currentSlotConfig[i].currentWeapon;
458    if( firingWeapon != NULL) firingWeapon->requestAction(WA_NONE);
459  }
460
461  /*
462  this->crosshair->setRotationSpeed(500);
463  this->crossHairSizeAnim->replay();
464  */
465}
466
467/**
468 * triggers tick of all weapons in the current weaponconfig
469 * @param second passed since last tick
470 */
471void WeaponManager::tick(float dt)
472{
473  Weapon* tickWeapon;
474
475
476  for(int i = 0; i < this->slotCount; i++)
477  {
478
479    //NICE LITTLE DEBUG FUNCTION
480    /*   if (this->currentSlotConfig[i].currentWeapon != NULL || this->currentSlotConfig[i].nextWeapon != NULL)
481      printf("%p %p\n", this->currentSlotConfig[i].currentWeapon, this->currentSlotConfig[i].nextWeapon);*/
482
483    // current Weapon in Slot i
484    tickWeapon = this->currentSlotConfig[i].currentWeapon;
485    // On A change (current != next)
486    if (tickWeapon != this->currentSlotConfig[i].nextWeapon)
487    {
488      // if a Weapon is Active in slot i, deactivate it.
489      if (tickWeapon != NULL )
490      {
491        if (tickWeapon->isActive())
492        {
493          tickWeapon->requestAction(WA_DEACTIVATE);
494          continue;
495        }
496        else
497        {
498          tickWeapon->toList(OM_NULL);
499          this->currentSlotConfig[i].currentWeapon = NULL;
500        }
501      }
502      // switching to next Weapon
503      tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
504     
505      if (tickWeapon != NULL)
506      {
507        //if (this->parent != NULL)
508        tickWeapon->toList(this->parentEntity->getOMListNumber());
509        tickWeapon->requestAction(WA_ACTIVATE);
510        this->currentSlotConfig[i].position.activateNode();
511        tickWeapon->setParent(&this->currentSlotConfig[i].position);
512      }
513      else
514        this->currentSlotConfig[i].position.deactivateNode();
515      if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID()))
516        dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
517    }
518    else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING))
519      this->currentSlotConfig[i].nextWeapon = NULL;
520  }
521}
522
523
524/**
525 * triggers draw of all weapons in the current weaponconfig
526 */
527void WeaponManager::draw() const
528{
529  assert(false || "must not be called");
530  Weapon* drawWeapon;
531  for (int i = 0; i < this->slotCount; i++)
532  {
533    drawWeapon = this->currentSlotConfig[i].currentWeapon;
534    if( drawWeapon != NULL && drawWeapon->isVisible())
535      drawWeapon->draw();
536  }
537}
538
539
540/**
541 * private gets the next free slot in a certain weaponconfig
542 * @param the selected weaponconfig -1 if none found
543 */
544int WeaponManager::getNextFreeSlot(int configID, long capability)
545{
546  if (configID == -1)
547  {
548    for (configID = 0; configID < WM_MAX_CONFIGS; configID++)
549      for( int i = 0; i < this->slotCount; ++i)
550      {
551        if( this->configs[configID][i] == NULL &&
552            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
553            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
554          return i;
555      }
556  }
557  else
558  {
559    for( int i = 0; i < this->slotCount; ++i)
560    {
561      if( this->configs[configID][i] == NULL &&
562          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
563          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
564        return i;
565    }
566  }
567  return -1;
568}
569
570CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType)
571{
572  for (unsigned int i = 0; i < this->ammo.size(); i++)
573  {
574    if (this->ammo[i]->getProjectileType() == projectileType)
575      return this->ammo[i];
576  }
577  this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType)));
578  return this->ammo.back();
579}
580
581CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon)
582{
583  assert (weapon != NULL);
584  return (this->getAmmoContainer(weapon->getClassID()));
585}
586
587
588/**
589 * outputs some nice debug information about the WeaponManager
590 */
591void WeaponManager::debug() const
592{
593  PRINT(3)("WeaponManager Debug Information\n");
594  PRINT(3)("-------------------------------\n");
595  PRINT(3)("current Config is %d\n", this->currentConfigID);
596  for (int i = 0; i < WM_MAX_CONFIGS; i++)
597  {
598    PRINT(3)("Listing Weapons in Configuration %d\n", i);
599    for (int j = 0; j < WM_MAX_SLOTS; j++)
600    {
601      if (this->configs[i][j] != NULL)
602        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassCName());
603    }
604  }
605}
Note: See TracBrowser for help on using the repository browser.