Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: ClassList is now in std::list style
ShellCommand is now in std::list style

File size: 13.2 KB
RevLine 
[4826]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
[4832]14   co-programmer: Benjamin Grauer
[4951]15
16   2005-07-24: Benjamin Grauer: restructurate, so it can handle the new Weapons.
[4826]17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
20
21#include "weapon_manager.h"
22#include "weapon.h"
[4834]23#include "crosshair.h"
[4828]24
[5750]25#include "class_list.h"
[4834]26#include "load_param.h"
27#include "factory.h"
[4826]28#include "list.h"
[4837]29#include "t_animation.h"
[4953]30#include "null_parent.h"
[4826]31
[5750]32
[4826]33using namespace std;
34
35
36/**
[4832]37 * this initializes the weaponManager for a given nnumber of weapon slots
[4826]38 * @param number of weapon slots of the model/ship <= 8 (limitied)
39 */
[4953]40WeaponManager::WeaponManager(PNode* parent)
[4826]41{
[4833]42  this->init();
[4953]43  this->setParent(parent);
[4826]44}
45
[4949]46WeaponManager::WeaponManager(const TiXmlElement* root)
47{
48  this->init();
49  this->loadParams(root);
50}
[4826]51
[4833]52/**
53 * Destroys a WeaponManager
54 */
[4826]55WeaponManager::~WeaponManager()
56{
[4834]57  // crosshair being a PNode it must not be deleted (this is because PNodes delete themselves.)
58  //delete this->crosshair;
[4826]59}
60
[4834]61/**
62 * initializes the WeaponManager
63 */
[4833]64void WeaponManager::init()
65{
66  this->setClassID(CL_WEAPON_MANAGER, "WeaponManager");
[4826]67
[4951]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++)
[4833]75  {
[4959]76    this->currentSlotConfig[i].capability = WTYPE_ALL;
[4951]77    this->currentSlotConfig[i].currentWeapon = NULL;
78    this->currentSlotConfig[i].nextWeapon = NULL;
[4992]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);
[5208]93    delete[] tmpName;
[4833]94  }
[4895]95
[4951]96  for (int i = 0; i < WM_MAX_LOADED_WEAPONS; i++)
97    this->availiableWeapons[i] = NULL;
[4895]98
99
[4951]100  this->currentConfigID = 0;
101  this->slotCount = 2;
102  this->weaponChange;
[4895]103
[4951]104  // CROSSHAIR INITIALISATION
[4834]105  this->crosshair = new Crosshair();
[4837]106
107  this->crossHairSizeAnim = new tAnimation<Crosshair>(this->crosshair, &Crosshair::setSize);
108  this->crossHairSizeAnim->setInfinity(ANIM_INF_REWIND);
109  this->crossHairSizeAnim->addKeyFrame(50, .1, ANIM_LINEAR);
110  this->crossHairSizeAnim->addKeyFrame(100, .05, ANIM_LINEAR);
111  this->crossHairSizeAnim->addKeyFrame(50, .01, ANIM_LINEAR);
[5750]112
113  this->targetIterator = NULL;
[4834]114}
[4833]115
[4834]116/**
[4837]117 * loads the settings of the WeaponManager
[4834]118 * @param root the XML-element to load from
119 */
120void WeaponManager::loadParams(const TiXmlElement* root)
121{
122  static_cast<BaseObject*>(this)->loadParams(root);
[4972]123
[5671]124  LoadParam(root, "slot-count", this, WeaponManager, setSlotCount)
[4834]125      .describe("how many slots(cannons) the WeaponManager can handle");
126
[5644]127  LOAD_PARAM_START_CYCLE(root, element);
[5654]128  {
129    // CHECK IF THIS WORKS....
130    LoadParamXML_CYCLE(element, "weapons", this, WeaponManager, loadWeapons)
131        .describe("loads Weapons");
132  }
[5644]133  LOAD_PARAM_END_CYCLE(element);
[4833]134}
135
[4826]136/**
[4834]137 * loads a Weapon onto the WeaponManager
138 * @param root the XML-element to load the Weapons from
139 */
140void WeaponManager::loadWeapons(const TiXmlElement* root)
141{
[5644]142  LOAD_PARAM_START_CYCLE(root, element);
[4834]143
144  Weapon* newWeapon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(element));
[5653]145  /// @todo implement this !!
[4834]146
147
[5644]148  LOAD_PARAM_END_CYCLE(element);
[4834]149}
150
[4992]151/**
152 * sets the Parent of the WeaponManager.
153 * @param parent the parent of the WeaponManager
154 *
155 * this is used, to identify to which ship/man/whatever this WeaponManager is connected.
156 * also all the Slots will be subconnected to this parent.
[5435]157 *
158 * The reason this function exists is that the WeaponManager is neither a WorldEntity nor
159 * a PNode.
[4992]160 */
[4953]161void WeaponManager::setParent(PNode* parent)
162{
163  if (parent == NULL)
164    parent = NullParent::getInstance();
165  this->parent = parent;
166  if (this->parent != NULL)
167  {
168    for (int i = 0; i < WM_MAX_SLOTS; i++)
169      this->parent->addChild(&this->currentSlotConfig[i].position);
170  }
171}
172
[4834]173/**
174 * sets the number of Slots the WeaponManager has
[4926]175 * @param slotCount the number of slots
[4834]176 */
[4951]177void WeaponManager::setSlotCount(unsigned int slotCount)
[4834]178{
[4951]179  if (slotCount <= WM_MAX_SLOTS)
180    this->slotCount = slotCount;
181  else
182    this->slotCount = WM_MAX_SLOTS;
[4834]183}
184
[4972]185
186/**
187 * sets the position of the Slot relative to the parent
188 * @param slot the slot to set-up
189 * @param position the position of the given slot
190 */
[4953]191void WeaponManager::setSlotPosition(int slot, const Vector& position)
192{
193  if (slot < this->slotCount)
194    this->currentSlotConfig[slot].position.setRelCoor(position);
195}
196
[4972]197
198/**
199 * sets the relative rotation of the slot to its parent
200 * @param slot the slot to set-up
201 * @param rotation the relative rotation of the given slot
202 */
[4969]203void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
204{
205  if (slot < this->slotCount)
206    this->currentSlotConfig[slot].position.setRelDir(rotation);
207}
208
209
[4834]210/**
[4832]211 * adds a weapon to the selected weaponconfiguration into the selected slot
[4972]212 * @param weapon the weapon to add
213 * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
214 * @param slotID an identifier for the weapon configuration, number between 0..3
[4832]215 *
216 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
[4906]217 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
[4832]218 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
219 * a error message.
[4826]220 */
[4832]221void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
[4826]222{
[4951]223  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
[4826]224  {
[5440]225    PRINTF(2)("Slot %d of config %d is not availiabe (max: %d)\n", slotID, configID, this->slotCount);
[4951]226    return;
227  }
228
229  if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0)
230    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName());
231
232  if (slotID == -1) // WM_FREE_SLOT
233  {
[5441]234    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
[4951]235    if( slotID < 0 || slotID >= this->slotCount)
[4826]236    {
[5441]237      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
[4826]238      return;
239    }
240  }
[4953]241
[5441]242  if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) &&
243        this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS)
244  {
245    PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n",
246              slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability);
247    return;
248  }
249
[4953]250  //! @todo check if the weapon is already assigned to another config in another slot
[4951]251  this->configs[configID][slotID] = weapon;
[4953]252  if (this->parent != NULL)
253    weapon->setParent(parent);
[4826]254  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
255}
256
[4834]257/**
[4954]258 * sets the capabilities of a Slot
259 * @param slot the slot to set the capability
260 * @param slotCapability the capability @see WM_SlotCapability
261 */
262void WeaponManager::setSlotCapability(int slot, long slotCapability)
263{
264  if (slot > slotCount)
265    return;
266  this->currentSlotConfig[slot].capability = slotCapability;
267}
268
269
270/**
[4834]271 * removes a Weapon from the WeaponManager
[4954]272 *
273 * !! The weapon must be inactive before you can delete it,    !!
274 * !! because it will still be deactivated (if it is selected) !!
[4834]275 */
[4826]276void WeaponManager::removeWeapon(Weapon* weapon, int configID)
277{
[4954]278  if (weapon == NULL)
279    return;
280  if (configID < 0)
281  {
282    for (int j = 0; j < WM_MAX_SLOTS; j++)
283    {
284      for (int i = 0; i < WM_MAX_CONFIGS; i++)
285      {
286        if (this->configs[i][j] == weapon)
287          this->configs[i][j] = NULL;
288      }
289      if (this->currentSlotConfig[j].currentWeapon == weapon)
290      {
291        this->currentSlotConfig[j].nextWeapon = NULL;
292      }
293    }
294  }
[4826]295}
296
297
298/**
[4832]299 * changes to the next weapon configuration
[4826]300 */
[4954]301void WeaponManager::nextWeaponConfig()
[4826]302{
[4951]303  ++this->currentConfigID;
304  if (this->currentConfigID >= WM_MAX_CONFIGS)
305    this->currentConfigID = 0;
[4952]306  this->changeWeaponConfig(this->currentConfigID);
307}
[4826]308
[4953]309/**
310 * changes to the previous configuration
311 */
[4952]312void WeaponManager::previousWeaponConfig()
313{
314  --this->currentConfigID;
315  if (this->currentConfigID < 0)
316    this->currentConfigID = WM_MAX_CONFIGS -1;
317  this->changeWeaponConfig(this->currentConfigID);
318}
319
[4953]320/**
321 * change to a desired configuration
322 * @param weaponConfig the configuration to jump to.
323 */
[4952]324void WeaponManager::changeWeaponConfig(int weaponConfig)
325{
326  this->currentConfigID = weaponConfig;
327  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
[4951]328  for (int i = 0; i < WM_MAX_SLOTS; i++)
[4826]329  {
[4951]330    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
[4953]331    if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon)
332    {
333      if (this->currentSlotConfig[i].currentWeapon != NULL)
334        (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE));
335      if (this->currentSlotConfig[i].nextWeapon != NULL && this->currentSlotConfig[i].nextWeapon->isActive())
336        this->currentSlotConfig[i].nextWeapon = NULL;
337    }
[4951]338  }
[4826]339}
340
341
342/**
[5750]343 * Selects an Entity from the NPC-list, and returns the Target
344 */
345PNode* WeaponManager::getSomeTarget()
346{
347   if (this->targetIterator == NULL)
348   {
[5779]349     std::list<BaseObject*>* npcList = ClassList::getList(CL_NPC);
[5750]350     if (npcList != NULL)
351     {
[5779]352       this->targetIterator = npcList->begin();
[5750]353     }
354     else
355       return NULL;
356   }
357
[5779]358   ///FIXME
359//   this->targetIterator++;
360   PNode* retNode = dynamic_cast<PNode*>((*targetIterator));
361//   if (retNode == NULL && this->targetIterator->getList()->getSize() > 0)
362//     retNode =  dynamic_cast<PNode*>(targetIterator->firstElement());
[5750]363
364   return retNode;
365}
366
367
368/**
369 * Selects an Entity from the Entity-List, that is near of the carrier PNode.
370 * @param carrier: The PNode from which the distance should be measured
371 * @param distance: The Maximum Distance to Return.
372 */
373PNode* WeaponManager::getDistanceTarget(const PNode* carrier, float distance)
374{
[5779]375  std::list<BaseObject*>* npcList = ClassList::getList(CL_NPC);
[5750]376  if (npcList != NULL)
377  {
[5779]378    list<BaseObject*>::iterator node;
379    for (node= npcList->begin(); node != npcList->end(); node++)
380      if ((carrier->getAbsCoor() - dynamic_cast<PNode*>(*node)->getAbsCoor()).len() < distance)
381        return dynamic_cast<PNode*>(*node);
[5750]382  }
383  return this->getFixedTarget();
384}
385
386
387/**
[4832]388 * triggers fire of all weapons in the current weaponconfig
[4826]389 */
[4832]390void WeaponManager::fire()
[4826]391{
392  Weapon* firingWeapon;
[4951]393  for(int i = 0; i < this->slotCount; i++)
[4826]394  {
[4951]395    firingWeapon = this->currentSlotConfig[i].currentWeapon;
[4885]396    if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
[4826]397  }
[4837]398  this->crosshair->setRotationSpeed(500);
399  this->crossHairSizeAnim->replay();
[4826]400}
401
402
403/**
[4832]404 * triggers tick of all weapons in the current weaponconfig
405 * @param second passed since last tick
[4826]406 */
[4833]407void WeaponManager::tick(float dt)
[4826]408{
[4951]409  Weapon* tickWeapon;
410
411  // all weapons
412  for(int i = 0; i < this->slotCount; i++)
[4826]413  {
[4951]414
415    tickWeapon = this->currentSlotConfig[i].currentWeapon;
416    if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures
417    {
418      if (tickWeapon != NULL && tickWeapon->isActive())
419      {
420        tickWeapon->requestAction(WA_DEACTIVATE);
421      }
422      else
423      {
424        tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
425        if (tickWeapon != NULL)
[4953]426        {
[4951]427          tickWeapon->requestAction(WA_ACTIVATE);
[4953]428          tickWeapon->setParent(&this->currentSlotConfig[i].position);
429        }
[4951]430      }
431    }
432
433    if( tickWeapon != NULL && tickWeapon->isActive())
434      tickWeapon->tickW(dt);
[4826]435  }
[4834]436
437  crosshair->setRotationSpeed(5);
[4826]438}
439
440
441/**
[4832]442 * triggers draw of all weapons in the current weaponconfig
[4826]443 */
[4951]444void WeaponManager::draw() const
[4826]445{
[4951]446  Weapon* drawWeapon;
447  for (int i = 0; i < this->slotCount; i++)
[4826]448  {
[4951]449    drawWeapon = this->currentSlotConfig[i].currentWeapon;
450    if( drawWeapon != NULL && drawWeapon->isVisible())
451      drawWeapon->draw();
[4826]452  }
453}
454
455
456/**
[4832]457 * private gets the next free slot in a certain weaponconfig
458 * @param the selected weaponconfig
[4826]459 */
[5440]460int WeaponManager::getNextFreeSlot(int configID, long capability)
[4826]461{
[4951]462  for( int i = 0; i < this->slotCount; ++i)
[4826]463  {
[5440]464    if( this->configs[configID][i] == NULL &&
[5441]465        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
466        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
[4826]467      return i;
468  }
469  return -1;
470}
471
[4951]472
473
[4953]474/**
475 * outputs some nice debug information about the WeaponManager
476 */
[4951]477void WeaponManager::debug() const
478{
479  PRINT(3)("WeaponManager Debug Information\n");
480  PRINT(3)("-------------------------------\n");
481  PRINT(3)("current Config is %d\n", this->currentConfigID);
482  for (int i = 0; i < WM_MAX_CONFIGS; i++)
483  {
484    PRINT(3)("Listing Weapons in Configuration %d\n", i);
485    for (int j = 0; j < WM_MAX_SLOTS; j++)
486    {
487      if (this->configs[i][j] != NULL)
488        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName());
489    }
490  }
491}
Note: See TracBrowser for help on using the repository browser.