Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/blink/src/world_entities/weapons/weapon_manager.cc @ 10648

Last change on this file since 10648 was 10438, checked in by patrick, 18 years ago

switch completed

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