Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: taken out NullParent.
THE TRUNK IS NOT RUNNING FOR THE TIME BEING. DO NOT BE ALARMED, THIS IS TEMPORARY

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(PNode* 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(PNode* parent)
158{
159  if (parent == NULL)
160    parent = PNode::getNullParent();
161  this->parent = parent;
162  if (this->parent != NULL)
163  {
164    for (int i = 0; i < WM_MAX_SLOTS; i++)
165      this->parent->addChild(&this->currentSlotConfig[i].position);
166  }
167}
168
169/**
170 * sets the number of Slots the WeaponManager has
171 * @param slotCount the number of slots
172 */
173void WeaponManager::setSlotCount(unsigned int slotCount)
174{
175  if (slotCount <= WM_MAX_SLOTS)
176    this->slotCount = slotCount;
177  else
178    this->slotCount = WM_MAX_SLOTS;
179}
180
181
182/**
183 * sets the position of the Slot relative to the parent
184 * @param slot the slot to set-up
185 * @param position the position of the given slot
186 */
187void WeaponManager::setSlotPosition(int slot, const Vector& position)
188{
189  if (slot < this->slotCount)
190    this->currentSlotConfig[slot].position.setRelCoor(position);
191}
192
193
194/**
195 * sets the relative rotation of the slot to its parent
196 * @param slot the slot to set-up
197 * @param rotation the relative rotation of the given slot
198 */
199void WeaponManager::setSlotDirection(int slot, const Quaternion& rotation)
200{
201  if (slot < this->slotCount)
202    this->currentSlotConfig[slot].position.setRelDir(rotation);
203}
204
205
206/**
207 * adds a weapon to the selected weaponconfiguration into the selected slot
208 * @param weapon the weapon to add
209 * @param configID an identifier for the slot: number between 0..7 if not specified: slotID=next free slot
210 * @param slotID an identifier for the weapon configuration, number between 0..3
211 *
212 * if you add explicitly a weapon at config:n, slot:m, the weapon placed at this location will be
213 * replaced by the weapon specified. if you use the WM_FREE_SLOT, the manager will look for a free
214 * slot in this weaponconfiguration. if there is non, the weapon won't be added and there will be
215 * a error message.
216 */
217void WeaponManager::addWeapon(Weapon* weapon, int configID, int slotID)
218{
219  if (unlikely(configID >= WM_MAX_CONFIGS || slotID >= (int)this->slotCount))
220  {
221    PRINTF(2)("Slot %d of config %d is not availiabe (max: %d)\n", slotID, configID, this->slotCount);
222    return;
223  }
224
225  if (this->configs[configID][slotID] != NULL && configID > 0 && slotID > 0)
226    PRINTF(3)("Weapon-slot %d/%d of %s already poulated, overwriting\n", configID, slotID, this->getName());
227
228  if (slotID == -1) // WM_FREE_SLOT
229  {
230    slotID = this->getNextFreeSlot(configID, weapon->getCapability());
231    if( slotID < 0 || slotID >= this->slotCount)
232    {
233      PRINTF(1)("There is no free slot in this WeaponConfig to dock this weapon at! Aborting\n");
234      return;
235    }
236  }
237
238  if (!(this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLKINDS) &&
239        this->currentSlotConfig[slotID].capability & weapon->getCapability() & WTYPE_ALLDIRS)
240  {
241    PRINTF(2)("Unable to add Weapon with wrong capatibility to Slot %d (W:%d M:%d)\n",
242              slotID, weapon->getCapability(), this->currentSlotConfig[slotID].capability);
243    return;
244  }
245
246  //! @todo check if the weapon is already assigned to another config in another slot
247  this->configs[configID][slotID] = weapon;
248  if (this->parent != NULL)
249    weapon->setParent(parent);
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        tickWeapon = this->currentSlotConfig[i].currentWeapon = this->currentSlotConfig[i].nextWeapon;
376        if (tickWeapon != NULL)
377        {
378          tickWeapon->requestAction(WA_ACTIVATE);
379          tickWeapon->setParent(&this->currentSlotConfig[i].position);
380          this->currentSlotConfig[i].position.activateNode();
381        }
382        else
383          this->currentSlotConfig[i].position.deactivateNode();
384      }
385    }
386
387    if( tickWeapon != NULL && tickWeapon->isActive())
388      tickWeapon->tickW(dt);
389  }
390
391  crosshair->setRotationSpeed(5);
392}
393
394
395/**
396 * triggers draw of all weapons in the current weaponconfig
397 */
398void WeaponManager::draw() const
399{
400  Weapon* drawWeapon;
401  for (int i = 0; i < this->slotCount; i++)
402  {
403    drawWeapon = this->currentSlotConfig[i].currentWeapon;
404    if( drawWeapon != NULL && drawWeapon->isVisible())
405      drawWeapon->draw();
406  }
407}
408
409
410/**
411 * private gets the next free slot in a certain weaponconfig
412 * @param the selected weaponconfig
413 */
414int WeaponManager::getNextFreeSlot(int configID, long capability)
415{
416  for( int i = 0; i < this->slotCount; ++i)
417  {
418    if( this->configs[configID][i] == NULL &&
419        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLKINDS) &&
420        (this->currentSlotConfig[i].capability & capability & WTYPE_ALLDIRS))
421      return i;
422  }
423  return -1;
424}
425
426
427
428/**
429 * outputs some nice debug information about the WeaponManager
430 */
431void WeaponManager::debug() const
432{
433  PRINT(3)("WeaponManager Debug Information\n");
434  PRINT(3)("-------------------------------\n");
435  PRINT(3)("current Config is %d\n", this->currentConfigID);
436  for (int i = 0; i < WM_MAX_CONFIGS; i++)
437  {
438    PRINT(3)("Listing Weapons in Configuration %d\n", i);
439    for (int j = 0; j < WM_MAX_SLOTS; j++)
440    {
441      if (this->configs[i][j] != NULL)
442        PRINT(3)("Slot %d loaded a %s\n", j, this->configs[i][j]->getClassName());
443    }
444  }
445}
Note: See TracBrowser for help on using the repository browser.