Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

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

orxonox/trunk: states are now flow'n through

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