Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/branches/terrain/src/world_entities/weapons/weapon_manager.cc @ 8471

Last change on this file since 8471 was 8147, checked in by bensch, 18 years ago

orxonox/trunk: merged the network branche back here
merged with command:
svn merge -r8070:HEAD https://svn.orxonox.net/orxonox/branches/network .
no conflicts

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