Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/weapon_manager.cc @ 10536

Last change on this file since 10536 was 10528, checked in by patrick, 18 years ago

iterative mp part sys fix

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