Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: orxonox.OLD/trunk/src/world_entities/weapons/weapon.cc @ 5212

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

orxonox/trunk: minor cleanup

File size: 16.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-15: Benjamin Grauer: restructurating the entire Class
17*/
18
19#define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON
20
21#include "weapon.h"
22
23#include "projectile.h"
24
25#include "resource_manager.h"
26#include "class_list.h"
27#include "load_param.h"
28#include "state.h"
29#include "sound_engine.h"
30#include "animation3d.h"
31#include "vector.h"
32
33////////////////////
34// INITAILISATION //
35// SETTING VALUES //
36////////////////////
37/**
38 * standard constructor
39 *
40 * creates a new weapon
41*/
42Weapon::Weapon (WeaponManager* weaponManager)
43{
44  this->init();
45  this->setWeaponManager(weaponManager);
46}
47
48/**
49 * standard deconstructor
50*/
51Weapon::~Weapon ()
52{
53  for (int i = 0; i < WS_STATE_COUNT; i++)
54    if (this->animation[i] && ClassList::exists(animation[i], CL_ANIMATION))  //!< @todo this should check animation3D
55      delete this->animation[i];
56  for (int i = 0; i < WA_ACTION_COUNT; i++)
57    if (this->soundBuffers[i])
58      ResourceManager::getInstance()->unload(this->soundBuffers[i]);
59
60  if (ClassList::exists(this->soundSource, CL_SOUND_SOURCE))
61    delete this->soundSource;
62}
63
64/**
65 * initializes the Weapon with ALL default values
66 */
67void Weapon::init()
68{
69  this->currentState     = WS_INACTIVE;
70  this->requestedAction  = WA_NONE;
71  this->stateDuration    = 0.0;
72  for (int i = 0; i < WS_STATE_COUNT; i++)
73    {
74      this->times[i] = 0.0;
75      this->animation[i] = NULL;
76    }
77  for (int i = 0; i < WA_ACTION_COUNT; i++)
78    this->soundBuffers[i] = NULL;
79
80  this->soundSource = new SoundSource(this);
81  this->emissionPoint.setParent(this);
82
83  this->projectile = CL_NULL;
84  this->projectileFactory = NULL;
85
86  this->hideInactive = true;
87
88  this->minCharge = 1.0;
89  this->maxCharge = 1.0;
90
91  this->energyLoaded = .0;
92  this->energyLoadedMax = 5.0;
93  this->energy = .0;
94  this->energyMax = 10.0;
95
96  this->setWeaponManager(NULL);
97}
98
99void Weapon::loadParams(const TiXmlElement* root)
100{
101  static_cast<WorldEntity*>(this)->loadParams(root);
102
103  LoadParam<Weapon>(root, "projectile", this, &Weapon::setProjectile)
104      .describe("Sets the name of the Projectile to load onto the Entity");
105
106  LoadParam<Weapon>(root, "emission-point", this, &Weapon::setEmissionPoint)
107      .describe("Sets the Point of emission of this weapon");
108
109  LoadParam<Weapon>(root, "state-duration", this, &Weapon::setStateDuration)
110      .describe("Sets the duration of a given state (1: state-Name; 2: duration in seconds)");
111
112  LoadParam<Weapon>(root, "action-sound", this, &Weapon::setActionSound)
113      .describe("Sets a given sound to an action (1: action-Name; 2: name of the sound (relative to the Data-Path))");
114}
115
116/**
117 * sets the Projectile to use for this weapon.
118 * @param projectile The ID of the Projectile to use
119 * @returns true, if it was sucessfull, false on error
120 *
121 * be aware, that this function does not create Factories, as this is job of Bullet-classes.
122 */
123void Weapon::setProjectile(ClassID projectile)
124{
125  if (projectile == CL_NULL)
126    return;
127  this->projectile = projectile;
128  this->projectileFactory = FastFactory::searchFastFactory(projectile);
129  if (this->projectileFactory == NULL)
130  {
131    PRINTF(1)("unable to find FastFactory for the Projectile.\n");
132    return;
133  }
134  else
135  {
136    // grabbing Parameters from the Projectile to have them at hand here.
137    Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect());
138    this->minCharge = pj->getEnergyMin();
139    this->maxCharge = pj->getEnergyMax();
140    this->chargeable = pj->isChageable();
141    this->projectileFactory->kill(pj);
142  }
143}
144
145/**
146 * @see bool Weapon::setProjectile(ClassID projectile)
147 * @param projectile the Name of the Projectile.
148 */
149void Weapon::setProjectile(const char* projectile)
150{
151  if (projectile == NULL)
152    return;
153  FastFactory* tmpFac = FastFactory::searchFastFactory(projectile);
154  if (tmpFac != NULL)
155  {
156    this->setProjectile(tmpFac->getStoredID());
157  }
158  else
159  {
160    PRINTF(2)("Projectile %s does not exist for weapon %s\n", projectile, this->getName());
161  }
162}
163
164/**
165 * sets the emissionPoint's relative position from the Weapon
166 * @param point the Point relative to the mass-point of the Weapon
167 */
168void Weapon::setEmissionPoint(const Vector& point)
169{
170  this->emissionPoint.setRelCoor(point);
171}
172
173/**
174 * assigns a Sound-file to an action
175 * @param action the action the sound should be assigned too
176 * @param soundFile the soundFile's relative position to the data-directory (will be looked for by the ResourceManager)
177 */
178void Weapon::setActionSound(WeaponAction action, const char* soundFile)
179{
180  if (action >= WA_ACTION_COUNT)
181    return;
182  if (this->soundBuffers[action] != NULL)
183    ResourceManager::getInstance()->unload(this->soundBuffers[action]);
184
185  else if (soundFile != NULL)
186  {
187    this->soundBuffers[action] = (SoundBuffer*)ResourceManager::getInstance()->load(soundFile, WAV);
188    if (this->soundBuffers[action] != NULL)
189    {
190      PRINTF(4)("Loaded sound %s to action %s.\n", soundFile, actionToChar(action));
191    }
192    else
193    {
194      PRINTF(2)("Failed to load sound %s to %s.\n.", soundFile, actionToChar(action));
195    }
196  }
197  else
198    this->soundBuffers[action] = NULL;
199}
200
201/**
202 * creates/returns an Animation3D for a certain State.
203 * @param state what State should the Animation be created/returned for
204 * @param node the node this Animation should apply to. (NULL is fine if the animation was already created)
205 * @returns The created animation.Animation(), NULL on error (or if the animation does not yet exist).
206 *
207 * This function does only generate the Animation Object, and if set it will
208 * automatically be executed, when a certain State is reached.
209 * What this does not do, is set keyframes, you have to operate on the returned animation.
210 */
211Animation3D* Weapon::getAnimation(WeaponState state, PNode* node)
212{
213  if (state >= WS_STATE_COUNT) // if the state is not known
214    return NULL;
215
216  if (unlikely(this->animation[state] == NULL)) // if the animation does not exist yet create it.
217  {
218    if (likely(node != NULL))
219      return this->animation[state] = new Animation3D(node);
220    else
221    {
222      PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state));
223      return NULL;
224    }
225  }
226  else
227    return this->animation[state];
228}
229
230/////////////////
231//  EXECUTION  //
232// GAME ACTION //
233/////////////////
234/**
235 * request an action that should be executed,
236 * @param action the next action to take
237 *
238 * This function must be called instead of the actions (like fire/reload...)
239 * to make all the checks needed to have a usefull WeaponSystem.
240 */
241void Weapon::requestAction(WeaponAction action)
242{
243  if (likely(this->isActive()))
244  {
245    if (this->requestedAction != WA_NONE)
246      return;
247    PRINTF(5)("next action will be %s in %f seconds\n", actionToChar(action), this->stateDuration);
248    this->requestedAction = action;
249  }
250  //else
251  else if (unlikely(action == WA_ACTIVATE))
252  {
253    this->currentState = WS_ACTIVATING;
254    this->requestedAction = WA_ACTIVATE;
255  }
256}
257
258/**
259 * adds energy to the Weapon
260 * @param energyToAdd The amount of energy
261 * @returns the amount of energy we did not pick up, because the weapon is already full
262 */
263float Weapon::increaseEnergy(float energyToAdd)
264{
265  float maxAddEnergy = this->energyMax - this->energy;
266
267  if (maxAddEnergy >= energyToAdd)
268  {
269    this->energy += energyToAdd;
270    return 0.0;
271  }
272  else
273  {
274    this->energy += maxAddEnergy;
275    return energyToAdd - maxAddEnergy;
276  }
277}
278
279//////////////////////
280// WEAPON INTERNALS //
281//////////////////////
282/**
283 * executes an action, and with it starts a new State.
284 * @return true, if it worked, false otherwise
285 *
286 * This function checks, wheter the possibility of executing an action is valid,
287 * and does all the necessary stuff, to execute them. If an action does not succeed,
288 * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again)
289 */
290bool Weapon::execute()
291{
292#if DEBUG > 4
293  PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction));
294  this->debug();
295#endif
296
297  WeaponAction action = this->requestedAction;
298  this->requestedAction = WA_NONE;
299
300  switch (action)
301  {
302    case WA_SHOOT:
303      return this->fireW();
304      break;
305    case WA_CHARGE:
306      return this->chargeW();
307      break;
308    case WA_RELOAD:
309      return this->reloadW();
310      break;
311    case WA_DEACTIVATE:
312      return this->deactivateW();
313      break;
314    case WA_ACTIVATE:
315      return this->activateW();
316      break;
317  }
318}
319
320/**
321 * checks and activates the Weapon.
322 * @return true on success.
323 */
324bool Weapon::activateW()
325{
326//  if (this->currentState == WS_INACTIVE)
327  {
328        // play Sound
329    if (likely(this->soundBuffers[WA_ACTIVATE] != NULL))
330      this->soundSource->play(this->soundBuffers[WA_ACTIVATE]);
331        // activate
332    PRINTF(4)("Activating the Weapon %s\n", this->getName());
333    this->activate();
334    // setting up for next action
335    this->enterState(WS_ACTIVATING);
336  }
337}
338
339/**
340 * checks and deactivates the Weapon
341 * @return true on success.
342 */
343bool Weapon::deactivateW()
344{
345//  if (this->currentState != WS_INACTIVE)
346  {
347    PRINTF(4)("Deactivating the Weapon %s\n", this->getName());
348        // play Sound
349    if (this->soundBuffers[WA_DEACTIVATE] != NULL)
350      this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]);
351    // deactivate
352    this->deactivate();
353    this->enterState(WS_DEACTIVATING);
354  }
355}
356
357/**
358 * checks and charges the Weapon
359 * @return true on success.
360 */
361bool Weapon::chargeW()
362{
363  if ( this->currentState != WS_INACTIVE && this->energyLoaded >= this->minCharge)
364  {
365        // playing Sound
366    if (this->soundBuffers[WA_CHARGE] != NULL)
367      this->soundSource->play(this->soundBuffers[WA_CHARGE]);
368
369        // charge
370    this->charge();
371        // setting up for the next state
372    this->enterState(WS_CHARGING);
373  }
374  else // deactivate the Weapon if we do not have enough energy
375  {
376    this->requestAction(WA_RELOAD);
377  }
378}
379
380/**
381 * checks and fires the Weapon
382 * @return true on success.
383 */
384bool Weapon::fireW()
385{
386     //if (likely(this->currentState != WS_INACTIVE))
387  if (this->minCharge <= this->energyLoaded)
388  {
389          // playing Sound
390    if (this->soundBuffers[WA_SHOOT] != NULL)
391      this->soundSource->play(this->soundBuffers[WA_SHOOT]);
392          // fire
393    this->fire();
394    this->energyLoaded -= this->minCharge;
395          // setting up for the next state
396    this->enterState(WS_SHOOTING);
397  }
398  else  // reload if we still have the charge
399  {
400    this->requestAction(WA_RELOAD);
401    this->execute();
402  }
403}
404
405/**
406 * checks and Reloads the Weapon
407 * @return true on success.
408 */
409bool Weapon::reloadW()
410{
411  PRINTF(4)("Reloading Weapon %s\n", this->getName());
412  if (unlikely(this->energy + this->energyLoaded < this->minCharge))
413  {
414    this->requestAction(WA_DEACTIVATE);
415    this->execute();
416    return false;
417  }
418
419  float chargeSize = this->energyLoadedMax - this->energyLoaded;       //!< The energy to be charged
420
421  if (this->soundBuffers[WA_RELOAD] != NULL)
422    this->soundSource->play(this->soundBuffers[WA_RELOAD]);
423
424  if (chargeSize > this->energy)
425  {
426    this->energyLoaded += this->energy;
427    this->energy = 0.0;
428    PRINT(5)("Energy depleted\n");
429  }
430  else
431  {
432    PRINTF(5)("Loaded %f energy into the Guns Buffer\n", chargeSize);
433    this->energyLoaded += chargeSize;
434    this->energy -= chargeSize;
435  }
436  this->reload();
437  this->enterState(WS_RELOADING);
438}
439
440/**
441 * enters the requested State, plays back animations updates the timing.
442 * @param state the state to enter.
443 */
444inline void Weapon::enterState(WeaponState state)
445{
446  PRINTF(4)("ENTERING STATE %s\n", stateToChar(state));
447  // playing animation if availiable
448  if (likely(this->animation[state] != NULL))
449    this->animation[state]->replay();
450
451  this->stateDuration = this->times[state] + this->stateDuration;
452  this->currentState = state;
453}
454
455///////////////////
456//  WORLD-ENTITY //
457// FUNCTIONALITY //
458///////////////////
459/**
460 * tick signal for time dependent/driven stuff
461*/
462void Weapon::tickW(float dt)
463{
464  //printf("%s ", stateToChar(this->currentState));
465
466  // setting up the timing properties
467  this->stateDuration -= dt;
468
469  if (this->stateDuration <= 0.0)
470  {
471    if (unlikely (this->currentState == WS_DEACTIVATING))
472    {
473      this->currentState = WS_INACTIVE;
474      return;
475    }
476    else
477      this->currentState = WS_IDLE;
478
479    if (this->requestedAction != WA_NONE)
480    {
481      this->stateDuration = -dt;
482      this->execute();
483    }
484  }
485  tick(dt);
486}
487
488/**
489 *  this will draw the weapon
490*/
491void Weapon::draw ()
492{}
493
494
495
496
497
498//////////////////////
499// HELPER FUNCTIONS //
500//////////////////////
501/**
502 * checks wether all the Weapons functions are valid, and if it is possible to go to action with it.
503 *
504 */
505bool Weapon::check() const
506{
507  bool retVal = true;
508
509//  if (this->projectile == NULL)
510  {
511    PRINTF(1)("There was no projectile assigned to the Weapon.\n");
512    retVal = false;
513  }
514
515
516
517
518  return retVal;
519}
520
521/**
522 * some nice debugging information about this Weapon
523 */
524void Weapon::debug() const
525{
526  PRINT(3)("Weapon-Debug %s, state: %s, nextAction: %s\n", this->getName(), Weapon::stateToChar(this->currentState), Weapon::actionToChar(requestedAction));
527  PRINT(3)("Energy: max: %f; current: %f;  loadedMax: %f; loadedCurrent: %f; chargeMin: %f, chargeMax %f\n",
528            this->energyMax, this->energy, this->energyLoadedMax, this->energyLoaded, this->minCharge, this->maxCharge);
529
530
531}
532
533
534// static
535/**
536 * Converts a String into an Action.
537 * @param action the String input holding the Action.
538 * @return The Action if known, WA_NONE otherwise.
539 */
540WeaponAction Weapon::charToAction(const char* action)
541{
542  if (!strcmp(action, "none"))
543    return WA_NONE;
544  else if (!strcmp(action, "shoot"))
545    return WA_SHOOT;
546  else if (!strcmp(action, "charge"))
547    return WA_CHARGE;
548  else if (!strcmp(action, "reload"))
549    return WA_RELOAD;
550  else if (!strcmp(action, "acitvate"))
551    return WA_ACTIVATE;
552  else if (!strcmp(action, "deactivate"))
553    return WA_DEACTIVATE;
554  else if (!strcmp(action, "special1"))
555    return WA_SPECIAL1;
556  else
557    {
558      PRINTF(2)("action %s could not be identified.\n", action);
559      return WA_NONE;
560    }
561}
562
563/**
564 * converts an action into a String
565 * @param action the action to convert
566 * @return a String matching the name of the action
567 */
568const char* Weapon::actionToChar(WeaponAction action)
569{
570  switch (action)
571  {
572    case WA_SHOOT:
573      return "shoot";
574      break;
575    case WA_CHARGE:
576      return "charge";
577      break;
578    case WA_RELOAD:
579      return "reload";
580      break;
581    case WA_ACTIVATE:
582      return "activate";
583      break;
584    case WA_DEACTIVATE:
585      return "deactivate";
586      break;
587    case WA_SPECIAL1:
588      return "special1";
589      break;
590    default:
591      return "none";
592      break;
593  }
594}
595
596/**
597 * Converts a String into a State.
598 * @param state the String input holding the State.
599 * @return The State if known, WS_NONE otherwise.
600 */
601WeaponState Weapon::charToState(const char* state)
602{
603  if (!strcmp(state, "none"))
604    return WS_NONE;
605  else if (!strcmp(state, "shooting"))
606    return WS_SHOOTING;
607  else if (!strcmp(state, "charging"))
608    return WS_CHARGING;
609  else if (!strcmp(state, "reloading"))
610    return WS_RELOADING;
611  else if (!strcmp(state, "activating"))
612    return WS_ACTIVATING;
613  else if (!strcmp(state, "deactivating"))
614    return WS_DEACTIVATING;
615  else if (!strcmp(state, "inactive"))
616    return WS_INACTIVE;
617  else if (!strcmp(state, "idle"))
618    return WS_IDLE;
619  else
620    {
621      PRINTF(2)("state %s could not be identified.\n", state);
622      return WS_NONE;
623    }
624}
625
626/**
627 * converts a State into a String
628 * @param state the state to convert
629 * @return a String matching the name of the state
630 */
631const char* Weapon::stateToChar(WeaponState state)
632{
633  switch (state)
634  {
635    case WS_SHOOTING:
636      return "shooting";
637      break;
638    case WS_CHARGING:
639      return "charging";
640      break;
641    case WS_RELOADING:
642      return "reloading";
643      break;
644    case WS_ACTIVATING:
645      return "activating";
646      break;
647    case WS_DEACTIVATING:
648      return "deactivating";
649      break;
650    case WS_IDLE:
651      return "idle";
652      break;
653    case WS_INACTIVE:
654      return "inactive";
655      break;
656    default:
657      return "none";
658      break;
659  }
660}
Note: See TracBrowser for help on using the repository browser.