Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 5567 was 5567, checked in by bensch, 19 years ago

orxonox/branches/world_entities: smoother aiming, and also displaying Distance (this is not too nice, but quite good to explain a few Things …

File size: 13.4 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
[5527]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
[5527]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);
[5527]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
[4834]124  LoadParam<WeaponManager>(root, "slot-count", this, &WeaponManager::setSlotCount)
125      .describe("how many slots(cannons) the WeaponManager can handle");
126
127  LOAD_PARAM_START_CYCLE;
128
[4972]129  LoadParam<WeaponManager>(element, "weapons", this, &WeaponManager::loadWeapons)
[4834]130      .describe("loads Weapons");
131      // LoadParam<WeaponManager>(root, "Weapon", this, &WeaponManager::addWeapon);
132
[4972]133  LOAD_PARAM_END_CYCLE;
[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{
142  LOAD_PARAM_START_CYCLE;
143
144  Weapon* newWeapon = dynamic_cast<Weapon*>(Factory::getFirst()->fabricate(element));
145
146
147  LOAD_PARAM_END_CYCLE;
148}
149
[4992]150/**
151 * 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.
[5435]156 *
157 * The reason this function exists is that the WeaponManager is neither a WorldEntity nor
158 * a PNode.
[4992]159 */
[4953]160void WeaponManager::setParent(PNode* parent)
161{
162  if (parent == NULL)
163    parent = NullParent::getInstance();
164  this->parent = parent;
165  if (this->parent != NULL)
166  {
167    for (int i = 0; i < WM_MAX_SLOTS; i++)
168      this->parent->addChild(&this->currentSlotConfig[i].position);
169  }
170}
171
[4834]172/**
173 * sets the number of Slots the WeaponManager has
[4926]174 * @param slotCount the number of slots
[4834]175 */
[4951]176void WeaponManager::setSlotCount(unsigned int slotCount)
[4834]177{
[4951]178  if (slotCount <= WM_MAX_SLOTS)
179    this->slotCount = slotCount;
180  else
181    this->slotCount = WM_MAX_SLOTS;
[4834]182}
183
[4972]184
185/**
186 * sets the position of the Slot relative to the parent
187 * @param slot the slot to set-up
188 * @param position the position of the given slot
189 */
[4953]190void WeaponManager::setSlotPosition(int slot, const Vector& position)
191{
192  if (slot < this->slotCount)
193    this->currentSlotConfig[slot].position.setRelCoor(position);
194}
195
[4972]196
197/**
198 * sets the relative rotation of the slot to its parent
199 * @param slot the slot to set-up
200 * @param rotation the relative rotation of the given slot
201 */
[4969]202void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
203{
204  if (slot < this->slotCount)
205    this->currentSlotConfig[slot].position.setRelDir(rotation);
206}
207
208
[4834]209/**
[4832]210 * adds a weapon to the selected weaponconfiguration into the selected slot
[4972]211 * @param weapon the weapon to add
212 * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
213 * @param slotID an identifier for the weapon configuration, number between 0..3
[4832]214 *
215 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
[4906]216 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
[4832]217 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
218 * a error message.
[4826]219 */
[4832]220void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
[4826]221{
[4951]222  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
[4826]223  {
[5440]224    PRINTF(2)("Slot %d of config %d is not availiabe (max: %d)\n", slotID, configID, this->slotCount);
[4951]225    return;
226  }
227
228  if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0)
229    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName());
230
231  if (slotID == -1) // WM_FREE_SLOT
232  {
[5441]233    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
[4951]234    if( slotID < 0 || slotID >= this->slotCount)
[4826]235    {
[5441]236      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
[4826]237      return;
238    }
239  }
[4953]240
[5441]241  if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) &&
242        this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS)
243  {
244    PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n",
245              slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability);
246    return;
247  }
248
[4953]249  //! @todo check if the weapon is already assigned to another config in another slot
[4951]250  this->configs[configID][slotID] = weapon;
[4953]251  if (this->parent != NULL)
252    weapon->setParent(parent);
[4826]253  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
254}
255
[4834]256/**
[4954]257 * sets the capabilities of a Slot
258 * @param slot the slot to set the capability
259 * @param slotCapability the capability @see WM_SlotCapability
260 */
261void WeaponManager::setSlotCapability(int slot, long slotCapability)
262{
263  if (slot > slotCount)
264    return;
265  this->currentSlotConfig[slot].capability = slotCapability;
266}
267
268
269/**
[4834]270 * removes a Weapon from the WeaponManager
[4954]271 *
272 * !! The weapon must be inactive before you can delete it,    !!
273 * !! because it will still be deactivated (if it is selected) !!
[4834]274 */
[4826]275void WeaponManager::removeWeapon(Weapon* weapon, int configID)
276{
[4954]277  if (weapon == NULL)
278    return;
279  if (configID < 0)
280  {
281    for (int j = 0; j < WM_MAX_SLOTS; j++)
282    {
283      for (int i = 0; i < WM_MAX_CONFIGS; i++)
284      {
285        if (this->configs[i][j] == weapon)
286          this->configs[i][j] = NULL;
287      }
288      if (this->currentSlotConfig[j].currentWeapon == weapon)
289      {
290        this->currentSlotConfig[j].nextWeapon = NULL;
291      }
292    }
293  }
[4826]294}
295
296
297/**
[4832]298 * changes to the next weapon configuration
[4826]299 */
[4954]300void WeaponManager::nextWeaponConfig()
[4826]301{
[4951]302  ++this->currentConfigID;
303  if (this->currentConfigID >= WM_MAX_CONFIGS)
304    this->currentConfigID = 0;
[4952]305  this->changeWeaponConfig(this->currentConfigID);
306}
[4826]307
[4953]308/**
309 * changes to the previous configuration
310 */
[4952]311void WeaponManager::previousWeaponConfig()
312{
313  --this->currentConfigID;
314  if (this->currentConfigID < 0)
315    this->currentConfigID = WM_MAX_CONFIGS -1;
316  this->changeWeaponConfig(this->currentConfigID);
317}
318
[4953]319/**
320 * change to a desired configuration
321 * @param weaponConfig the configuration to jump to.
322 */
[4952]323void WeaponManager::changeWeaponConfig(int weaponConfig)
324{
325  this->currentConfigID = weaponConfig;
326  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
[4951]327  for (int i = 0; i < WM_MAX_SLOTS; i++)
[4826]328  {
[4951]329    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
[4953]330    if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon)
331    {
332      if (this->currentSlotConfig[i].currentWeapon != NULL)
333        (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE));
334      if (this->currentSlotConfig[i].nextWeapon != NULL && this->currentSlotConfig[i].nextWeapon->isActive())
335        this->currentSlotConfig[i].nextWeapon = NULL;
336    }
[4951]337  }
[4826]338}
339
340
341/**
[5527]342 * Selects an Entity from the NPC-list, and returns the Target
343 */
344PNode* WeaponManager::getSomeTarget()
345{
346   if (this->targetIterator == NULL)
347   {
348     tList<BaseObject>* npcList = ClassList::getList(CL_NPC);
349     if (npcList != NULL)
350     {
351       this->targetIterator = npcList->getIterator();
352       this->targetIterator->firstElement();
353     }
354     else
355       return NULL;
356   }
357
358   PNode* retNode = dynamic_cast<PNode*>(targetIterator->nextElement());
359   if (retNode == NULL && this->targetIterator->getList()->getSize() > 0)
360     retNode =  dynamic_cast<PNode*>(targetIterator->firstElement());
361
362   return retNode;
363}
364
365
366/**
367 * Selects an Entity from the Entity-List, that is near of the carrier PNode.
368 * @param carrier: The PNode from which the distance should be measured
369 * @param distance: The Maximum Distance to Return.
370 */
371PNode* WeaponManager::getDistanceTarget(const PNode* carrier, float distance)
372{
373  tList<BaseObject>* npcList = ClassList::getList(CL_NPC);
374  if (npcList != NULL)
375  {
376    tIterator<BaseObject>* npcIT = npcList->getIterator();
377    PNode* tmpNPC = dynamic_cast<PNode*>(npcIT->firstElement());
378    while (tmpNPC != NULL)
379    {
380      if ((carrier->getAbsCoor() - tmpNPC->getAbsCoor()).len() < distance)
381      {
382        delete npcIT;
383        return tmpNPC;
384      }
385      tmpNPC = dynamic_cast<PNode*>(npcIT->nextElement());
386    }
387    delete npcIT;
388  }
389  return this->getFixedTarget();
390
391}
392
393
394/**
[4832]395 * triggers fire of all weapons in the current weaponconfig
[4826]396 */
[4832]397void WeaponManager::fire()
[4826]398{
399  Weapon* firingWeapon;
[4951]400  for(int i = 0; i < this->slotCount; i++)
[4826]401  {
[4951]402    firingWeapon = this->currentSlotConfig[i].currentWeapon;
[4885]403    if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
[4826]404  }
[4837]405  this->crosshair->setRotationSpeed(500);
406  this->crossHairSizeAnim->replay();
[4826]407}
408
409
410/**
[4832]411 * triggers tick of all weapons in the current weaponconfig
412 * @param second passed since last tick
[4826]413 */
[4833]414void WeaponManager::tick(float dt)
[4826]415{
[4951]416  Weapon* tickWeapon;
417
418  // all weapons
419  for(int i = 0; i < this->slotCount; i++)
[4826]420  {
[4951]421
422    tickWeapon = this->currentSlotConfig[i].currentWeapon;
423    if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures
424    {
425      if (tickWeapon != NULL && tickWeapon->isActive())
426      {
427        tickWeapon->requestAction(WA_DEACTIVATE);
428      }
429      else
430      {
431        tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
432        if (tickWeapon != NULL)
[4953]433        {
[4951]434          tickWeapon->requestAction(WA_ACTIVATE);
[4953]435          tickWeapon->setParent(&this->currentSlotConfig[i].position);
436        }
[4951]437      }
438    }
439
440    if( tickWeapon != NULL && tickWeapon->isActive())
441      tickWeapon->tickW(dt);
[4826]442  }
[4834]443
444  crosshair->setRotationSpeed(5);
[4826]445}
446
447
448/**
[4832]449 * triggers draw of all weapons in the current weaponconfig
[4826]450 */
[4951]451void WeaponManager::draw() const
[4826]452{
[4951]453  Weapon* drawWeapon;
454  for (int i = 0; i < this->slotCount; i++)
[4826]455  {
[4951]456    drawWeapon = this->currentSlotConfig[i].currentWeapon;
457    if( drawWeapon != NULL && drawWeapon->isVisible())
458      drawWeapon->draw();
[4826]459  }
460}
461
462
463/**
[4832]464 * private gets the next free slot in a certain weaponconfig
465 * @param the selected weaponconfig
[4826]466 */
[5440]467int WeaponManager::getNextFreeSlot(int configID, long capability)
[4826]468{
[4951]469  for( int i = 0; i < this->slotCount; ++i)
[4826]470  {
[5440]471    if( this->configs[configID][i] == NULL &&
[5441]472        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
473        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
[4826]474      return i;
475  }
[5567]476  printf("SHIT\n");
[4826]477  return -1;
478}
479
[4951]480
481
[4953]482/**
483 * outputs some nice debug information about the WeaponManager
484 */
[4951]485void WeaponManager::debug() const
486{
487  PRINT(3)("WeaponManager Debug Information\n");
488  PRINT(3)("-------------------------------\n");
489  PRINT(3)("current Config is %d\n", this->currentConfigID);
490  for (int i = 0; i < WM_MAX_CONFIGS; i++)
491  {
492    PRINT(3)("Listing Weapons in Configuration %d\n", i);
493    for (int j = 0; j < WM_MAX_SLOTS; j++)
494    {
495      if (this->configs[i][j] != NULL)
496        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName());
497    }
498  }
499}
Note: See TracBrowser for help on using the repository browser.