Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: Weapons update, Now one can really assign SlotCapabilities
Debug-redesign (small)

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