Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: optimizations

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