Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/branches/objectmanager: all the WorldEntities regigister/unregister ath the ObjectManager as they should

File size: 12.1 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  weapon->toList(parent->getOMListNumber());
247  if (this->parent != NULL)
248    this->parent->addChild(weapon);
249  PRINTF(3)("Added a new Weapon to the WeaponManager: config %i/ slot %i\n", configID, slotID);
250}
251
252/**
253 * sets the capabilities of a Slot
254 * @param slot the slot to set the capability
255 * @param slotCapability the capability @see WM_SlotCapability
256 */
257void WeaponManager::setSlotCapability(int slot, long slotCapability)
258{
259  if (slot > slotCount)
260    return;
261  this->currentSlotConfig[slot].capability = slotCapability;
262}
263
264
265/**
266 * removes a Weapon from the WeaponManager
267 *
268 * !! The weapon must be inactive before you can delete it,    !!
269 * !! because it will still be deactivated (if it is selected) !!
270 */
271void WeaponManager::removeWeapon(Weapon* weapon, int configID)
272{
273  if (weapon == NULL)
274    return;
275  if (configID < 0)
276  {
277    for (int j = 0; j < WM_MAX_SLOTS; j++)
278    {
279      for (int i = 0; i < WM_MAX_CONFIGS; i++)
280      {
281        if (this->configs[i][j] == weapon)
282          this->configs[i][j] = NULL;
283      }
284      if (this->currentSlotConfig[j].currentWeapon == weapon)
285      {
286        this->currentSlotConfig[j].nextWeapon = NULL;
287      }
288    }
289  }
290}
291
292
293/**
294 * changes to the next weapon configuration
295 */
296void WeaponManager::nextWeaponConfig()
297{
298  ++this->currentConfigID;
299  if (this->currentConfigID >= WM_MAX_CONFIGS)
300    this->currentConfigID = 0;
301  this->changeWeaponConfig(this->currentConfigID);
302}
303
304/**
305 * changes to the previous configuration
306 */
307void WeaponManager::previousWeaponConfig()
308{
309  --this->currentConfigID;
310  if (this->currentConfigID < 0)
311    this->currentConfigID = WM_MAX_CONFIGS -1;
312  this->changeWeaponConfig(this->currentConfigID);
313}
314
315/**
316 * change to a desired configuration
317 * @param weaponConfig the configuration to jump to.
318 */
319void WeaponManager::changeWeaponConfig(int weaponConfig)
320{
321  this->currentConfigID = weaponConfig;
322  PRINTF(4)("Changing weapon configuration: to %i\n", this->currentConfigID);
323  for (int i = 0; i < WM_MAX_SLOTS; i++)
324  {
325    this->currentSlotConfig[i].nextWeapon = this->configs[currentConfigID][i];
326    if (this->currentSlotConfig[i].currentWeapon != this->currentSlotConfig[i].nextWeapon)
327    {
328      if (this->currentSlotConfig[i].currentWeapon != NULL)
329        (this->currentSlotConfig[i].currentWeapon->requestAction(WA_DEACTIVATE));
330      if (this->currentSlotConfig[i].nextWeapon != NULL && this->currentSlotConfig[i].nextWeapon->isActive())
331        this->currentSlotConfig[i].nextWeapon = NULL;
332    }
333  }
334}
335
336
337/**
338 * triggers fire of all weapons in the current weaponconfig
339 */
340void WeaponManager::fire()
341{
342  Weapon* firingWeapon;
343  for(int i = 0; i < this->slotCount; i++)
344  {
345    firingWeapon = this->currentSlotConfig[i].currentWeapon;
346    if( firingWeapon != NULL) firingWeapon->requestAction(WA_SHOOT);
347  }
348  this->crosshair->setRotationSpeed(500);
349  this->crossHairSizeAnim->replay();
350}
351
352
353/**
354 * triggers tick of all weapons in the current weaponconfig
355 * @param second passed since last tick
356 */
357void WeaponManager::tick(float dt)
358{
359  Weapon* tickWeapon;
360
361  // all weapons
362  for(int i = 0; i < this->slotCount; i++)
363  {
364
365    tickWeapon = this->currentSlotConfig[i].currentWeapon;
366    if (tickWeapon != this->currentSlotConfig[i].nextWeapon) // if no change occures
367    {
368      if (tickWeapon != NULL && tickWeapon->isActive())
369      {
370        tickWeapon->requestAction(WA_DEACTIVATE);
371      }
372      else
373      {
374        tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
375        if (tickWeapon != NULL)
376        {
377          tickWeapon->requestAction(WA_ACTIVATE);
378          tickWeapon->setParent(&this->currentSlotConfig[i].position);
379          this->currentSlotConfig[i].position.activateNode();
380        }
381        else
382          this->currentSlotConfig[i].position.deactivateNode();
383      }
384    }
385
386    if( tickWeapon != NULL && tickWeapon->isActive())
387      tickWeapon->tickW(dt);
388  }
389
390  crosshair->setRotationSpeed(5);
391}
392
393
394/**
395 * triggers draw of all weapons in the current weaponconfig
396 */
397void WeaponManager::draw() const
398{
399  Weapon* drawWeapon;
400  for (int i = 0; i < this->slotCount; i++)
401  {
402    drawWeapon = this->currentSlotConfig[i].currentWeapon;
403    if( drawWeapon != NULL && drawWeapon->isVisible())
404      drawWeapon->draw();
405  }
406}
407
408
409/**
410 * private gets the next free slot in a certain weaponconfig
411 * @param the selected weaponconfig
412 */
413int WeaponManager::getNextFreeSlot(int configID, long capability)
414{
415  for( int i = 0; i < this->slotCount; ++i)
416  {
417    if( this->configs[configID][i] == NULL &&
418        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
419        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
420      return i;
421  }
422  return -1;
423}
424
425
426
427/**
428 * outputs some nice debug information about the WeaponManager
429 */
430void WeaponManager::debug() const
431{
432  PRINT(3)("WeaponManager Debug Information\n");
433  PRINT(3)("-------------------------------\n");
434  PRINT(3)("current Config is %d\n", this->currentConfigID);
435  for (int i = 0; i < WM_MAX_CONFIGS; i++)
436  {
437    PRINT(3)("Listing Weapons in Configuration %d\n", i);
438    for (int j = 0; j < WM_MAX_SLOTS; j++)
439    {
440      if (this->configs[i][j] != NULL)
441        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName());
442    }
443  }
444}
Note: See TracBrowser for help on using the repository browser.