Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

trunk: WeaponSystem working again:
reverted the changed from revision 6435, because they made the WeaponSystem unusable

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