Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 10022 was 10004, checked in by marcscha, 18 years ago

see changelog

File size: 16.2 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
120void WeaponManager::showCrosshair()
121{
122  this->crosshair->setVisibility( true);
123}
124
125void WeaponManager::hideCrosshair()
126{
127  this->crosshair->setVisibility( false);
128}
129
130void WeaponManager::setRotationSpeed(float speed)
131{
132  this->crosshair->setRotationSpeed(speed);
133}
134
135/**
136 * @brief loads the settings of the WeaponManager
137 * @param root the XML-element to load from
138 */
139void WeaponManager::loadParams(const TiXmlElement* root)
140{
141  BaseObject::loadParams(root);
142
143  LoadParam(root, "slot-count", this, WeaponManager, setSlotCount)
144  .describe("how many slots(cannons) the WeaponManager can handle");
145
146  LOAD_PARAM_START_CYCLE(root, element);
147  {
148    // CHECK IF THIS WORKS....
149    LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons)
150    .describe("loads Weapons");
151  }
152  LOAD_PARAM_END_CYCLE(element);
153}
154
155/**
156 * @brief loads a Weapon onto the WeaponManager
157 * @param root the XML-element to load the Weapons from
158 */
159void WeaponManager::loadWeapons(const TiXmlElement* root)
160{
161  LOAD_PARAM_START_CYCLE(root, element);
162
163  BaseObject* object = Factory::fabricate(element);
164  if (object != NULL)
165  {
166    Weapon* newWeapon = dynamic_cast<Weapon*>(object);
167    if (newWeapon == NULL)
168      delete object;
169  }
170  LOAD_PARAM_END_CYCLE(element);
171}
172
173/**
174 * @brief sets the Parent of the WeaponManager.
175 * @param parent the parent of the WeaponManager
176 *
177 * this is used, to identify to which ship/man/whatever this WeaponManager is connected.
178 * also all the Slots will be subconnected to this parent.
179 *
180 * The reason this function exists is that the WeaponManager is neither a WorldEntity nor
181 * a PNode.
182 */
183void WeaponManager::setParentEntity(WorldEntity* parent)
184{
185  this->parentEntity = parent;
186  if (this->parentNode == NULL)
187    this->setParentNode(parent);
188}
189
190
191void WeaponManager::setParentNode(PNode* parent)
192{
193  this->parentNode = parent;
194  assert(parent != NULL);
195
196  if (this->parentNode != NULL)
197  {
198    for (int i = 0; i < WM_MAX_SLOTS; i++)
199      this->parentNode->addChild(&this->currentSlotConfig[i].position);
200  }
201
202}
203
204
205/**
206 * @brief sets the number of Slots the WeaponManager has
207 * @param slotCount the number of slots
208 */
209void WeaponManager::setSlotCount(unsigned int slotCount)
210{
211  if (slotCount <= WM_MAX_SLOTS)
212    this->slotCount = slotCount;
213  else
214    this->slotCount = WM_MAX_SLOTS;
215}
216
217
218/**
219 * @brief sets the position of the Slot relative to the parent
220 * @param slot the slot to set-up
221 * @param position the position of the given slot
222 */
223void WeaponManager::setSlotPosition(int slot, const Vector& position, PNode* parent)
224{
225  if (slot < this->slotCount)
226  {
227    this->currentSlotConfig[slot].position.setRelCoor(position);
228
229    if (parent != NULL)
230      this->currentSlotConfig[slot].position.setParent(parent);
231  }
232}
233
234
235/**
236 * @brief sets the relative rotation of the slot to its parent
237 * @param slot the slot to set-up
238 * @param rotation the relative rotation of the given slot
239 */
240void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
241{
242  if (slot < this->slotCount)
243    this->currentSlotConfig[slot].position.setRelDir(rotation);
244}
245
246
247/**
248 * @brief adds a weapon to the selected weaponconfiguration into the selected slot
249 * @param weapon the weapon to add
250 * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
251 * @param slotID an identifier for the weapon configuration, number between 0..3
252 *
253 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
254 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
255 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
256 * a error message.
257 */
258bool WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
259{
260  if ( weapon == NULL )
261    return false;
262
263  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
264  {
265    PRINTF(2)("Slot %d of config %d is not availiabe (max: %d) searching for suitable slot\n", slotID, configID, this->slotCount);
266    if (configID >= WM_MAX_CONFIGS)
267      configID = -1;
268    if (slotID >= (int)this->slotCount)
269      slotID = -1;
270  }
271  // if no ConfigID is supplied set to Current Config.
272  if (configID <= -1)
273    configID = this->currentConfigID;
274  //
275  if (configID > -1 && slotID == -1)
276  {
277    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
278    if (slotID == -1)
279      configID = -1;
280  }
281
282  if (configID > 0 && slotID > 0 && this->configs[configID][slotID] != NULL)
283  {
284    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, remove weapon (%s::%s) first\n", configID, slotID, this->getCName(), weapon->getClassCName(), weapon->getCName());
285    return false;
286  }
287
288  if (slotID <= -1) // WM_FREE_SLOT
289  {
290    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
291    if( slotID < 0 || slotID >= this->slotCount)
292    {
293      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
294      return false;
295    }
296  }
297
298  if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) &&
299      this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS)
300  {
301    PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n",
302              slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability);
303    return false;
304  }
305
306  //! @todo check if the weapon is already assigned to another config in another slot
307  if (this->configs[configID][slotID] != NULL)
308    return false;
309
310  this->configs[configID][slotID] = weapon;
311  weapon->setAmmoContainer(this->getAmmoContainer(weapon->getProjectileType()));
312  if(configID == this->currentConfigID)
313    this->currentSlotConfig[slotID].nextWeapon = weapon;
314  //if (this->parent != NULL)
315  {
316    this->parentNode->addChild(weapon);
317    if (this->parentEntity->isA(Playable::staticClassID()))
318      dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
319   
320    weapon->setDefaultTarget(this->crosshair);
321  }
322  PRINTF(4)("Added a new Weapon (%s::%s) to the WeaponManager: config %i/ slot %i\n", weapon->getClassCName(), weapon->getCName(), configID, slotID);
323  return true;
324}
325
326/**
327 * @brief increases the Energy of the WeaponContainer of type (projectileType)
328 * @param projectileType the type of weapon to increase Energy from
329 * @param ammo the ammo to increase
330 */
331float WeaponManager::increaseAmmunition(const ClassID& projectileType, float ammo)
332{
333  return this->getAmmoContainer(projectileType)->increaseEnergy(ammo);
334}
335
336/**
337 * @brief does the same as the funtion increaseAmmunition, added four your convenience
338 * @param weapon, the Weapon to read the ammo-info about.
339 * @param ammo how much ammo to add.
340 */
341float WeaponManager::increaseAmmunition(const Weapon* weapon, float ammo)
342{
343  assert (weapon != NULL);
344  return this->increaseAmmunition(weapon->getClassID(), ammo);
345
346}
347
348
349/**
350 * sets the capabilities of a Slot
351 * @param slot the slot to set the capability
352 * @param slotCapability the capability @see WM_SlotCapability
353 */
354void WeaponManager::setSlotCapability(int slot, long slotCapability)
355{
356  if (slot > slotCount)
357    return;
358  this->currentSlotConfig[slot].capability = slotCapability;
359}
360
361
362/**
363 * removes a Weapon from the WeaponManager
364 *
365 * !! The weapon must be inactive before you can delete it,    !!
366 * !! because it will still be deactivated (if it is selected) !!
367 */
368void WeaponManager::removeWeapon(Weapon* weapon, int configID)
369{
370  if (weapon == NULL)
371    return;
372  if (configID < 0)
373  {
374    for (int j = 0; j < WM_MAX_SLOTS; j++)
375    {
376      for (int i = 0; i < WM_MAX_CONFIGS; i++)
377      {
378        if (this->configs[i][j] == weapon)
379          this->configs[i][j] = NULL;
380      }
381      if (this->currentSlotConfig[j].currentWeapon == weapon)
382      {
383        this->currentSlotConfig[j].nextWeapon = NULL;
384      }
385    }
386  }
387}
388
389
390/**
391 * changes to the next weapon configuration
392 */
393void WeaponManager::nextWeaponConfig()
394{
395  ++this->currentConfigID;
396  if (this->currentConfigID >= WM_MAX_CONFIGS)
397    this->currentConfigID = 0;
398  this->changeWeaponConfig(this->currentConfigID);
399}
400
401/**
402 * changes to the previous configuration
403 */
404void WeaponManager::previousWeaponConfig()
405{
406  --this->currentConfigID;
407  if (this->currentConfigID < 0)
408    this->currentConfigID = WM_MAX_CONFIGS -1;
409  this->changeWeaponConfig(this->currentConfigID);
410}
411
412/**
413 * change to a desired configuration
414 * @param weaponConfig the configuration to jump to.
415 */
416void WeaponManager::changeWeaponConfig(int weaponConfig)
417{
418  this->currentConfigID = weaponConfig;
419  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
420  for (int i = 0; i < WM_MAX_SLOTS; i++)
421    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
422}
423
424
425/**
426 * triggers fire of all weapons in the current weaponconfig
427 */
428void WeaponManager::fire()
429{
430  Weapon* firingWeapon;
431  for(int i = 0; i < this->slotCount; i++)
432  {
433          firingWeapon = this->currentSlotConfig[i].currentWeapon;
434          if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
435  }
436
437  /*
438        this->crosshair->setRotationSpeed(500);
439        this->crossHairSizeAnim->replay();
440  */
441}
442
443
444/**
445 * triggers tick of all weapons in the current weaponconfig
446 * @param second passed since last tick
447 */
448void WeaponManager::tick(float dt)
449{
450  Weapon* tickWeapon;
451
452  for(int i = 0; i < this->slotCount; i++)
453  {
454/*
455    NICE LITTLE DEBUG FUNCTION
456       if (this->currentSlotConfig[i].currentWeapon != NULL || this->currentSlotConfig[i].nextWeapon != NULL)
457      printf("%p %p\n", this->currentSlotConfig[i].currentWeapon, this->currentSlotConfig[i].nextWeapon);*/
458
459    // current Weapon in Slot i
460    tickWeapon = this->currentSlotConfig[i].currentWeapon;
461    // On A change (current != next)
462    if (tickWeapon != this->currentSlotConfig[i].nextWeapon)
463    {
464      // if a Weapon is Active in slot i, deactivate it.
465      if (tickWeapon != NULL )
466      {
467        if (tickWeapon->isActive())
468        {
469          tickWeapon->requestAction(WA_DEACTIVATE);
470          continue;
471        }
472        else
473        {
474          tickWeapon->toList(OM_NULL);
475          this->currentSlotConfig[i].currentWeapon = NULL;
476        }
477      }
478
479      // switching to next Weapon
480      tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
481      if (tickWeapon != NULL)
482      {
483           //        if (this->parent != NULL)
484          tickWeapon->toList(this->parentEntity->getOMListNumber());
485        tickWeapon->requestAction(WA_ACTIVATE);
486        this->currentSlotConfig[i].position.activateNode();
487        tickWeapon->setParent(&this->currentSlotConfig[i].position);
488      }
489      else
490        this->currentSlotConfig[i].position.deactivateNode();
491      if (this->parentEntity != NULL && this->parentEntity->isA(Playable::staticClassID()))
492        dynamic_cast<Playable*>(this->parentEntity)->weaponConfigChanged();
493    }
494    else if (unlikely(tickWeapon != NULL && tickWeapon->getCurrentState() == WS_DEACTIVATING))
495      this->currentSlotConfig[i].nextWeapon = NULL;
496  }
497}
498
499
500/**
501 * triggers draw of all weapons in the current weaponconfig
502 */
503void WeaponManager::draw() const
504{
505  assert(false || "must not be called");
506  Weapon* drawWeapon;
507  for (int i = 0; i < this->slotCount; i++)
508  {
509    drawWeapon = this->currentSlotConfig[i].currentWeapon;
510    if( drawWeapon != NULL && drawWeapon->isVisible())
511      drawWeapon->draw();
512  }
513}
514
515
516/**
517 * private gets the next free slot in a certain weaponconfig
518 * @param the selected weaponconfig -1 if none found
519 */
520int WeaponManager::getNextFreeSlot(int configID, long capability)
521{
522  if (configID == -1)
523  {
524    for (configID = 0; configID < WM_MAX_CONFIGS; configID++)
525      for( int i = 0; i < this->slotCount; ++i)
526      {
527        if( this->configs[configID][i] == NULL &&
528            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
529            (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
530          return i;
531      }
532  }
533  else
534  {
535    for( int i = 0; i < this->slotCount; ++i)
536    {
537      if( this->configs[configID][i] == NULL &&
538          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
539          (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
540        return i;
541    }
542  }
543  return -1;
544}
545
546CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const ClassID& projectileType)
547{
548  for (unsigned int i = 0; i < this->ammo.size(); i++)
549  {
550    if (this->ammo[i]->getProjectileType() == projectileType)
551      return this->ammo[i];
552  }
553  this->ammo.push_back(CountPointer<AmmoContainer>(new AmmoContainer(projectileType)));
554  return this->ammo.back();
555}
556
557CountPointer<AmmoContainer>& WeaponManager::getAmmoContainer(const Weapon* weapon)
558{
559  assert (weapon != NULL);
560  return (this->getAmmoContainer(weapon->getClassID()));
561}
562
563
564/**
565 * outputs some nice debug information about the WeaponManager
566 */
567void WeaponManager::debug() const
568{
569  PRINT(3)("WeaponManager Debug Information\n");
570  PRINT(3)("-------------------------------\n");
571  PRINT(3)("current Config is %d\n", this->currentConfigID);
572  for (int i = 0; i < WM_MAX_CONFIGS; i++)
573  {
574    PRINT(3)("Listing Weapons in Configuration %d\n", i);
575    for (int j = 0; j < WM_MAX_SLOTS; j++)
576    {
577      if (this->configs[i][j] != NULL)
578        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassCName());
579    }
580  }
581}
Note: See TracBrowser for help on using the repository browser.