| [3573] | 1 |  | 
|---|
| [4597] | 2 | /* | 
|---|
| [3573] | 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 |  | 
|---|
| [4826] | 12 | ### File Specific | 
|---|
| [3573] | 13 |    main-programmer: Patrick Boenzli | 
|---|
| [4832] | 14 |    co-programmer: Benjamin Grauer | 
|---|
| [4885] | 15 |  | 
|---|
 | 16 |    2005-07-15: Benjamin Grauer: restructurating the entire Class | 
|---|
| [3573] | 17 | */ | 
|---|
 | 18 |  | 
|---|
| [4885] | 19 | #define DEBUG_SPECIAL_MODULE DEBUG_MODULE_WEAPON | 
|---|
 | 20 |  | 
|---|
| [4828] | 21 | #include "weapon.h" | 
|---|
 | 22 |  | 
|---|
| [5355] | 23 | #include "fast_factory.h" | 
|---|
| [6434] | 24 | #include "world_entities/projectiles/projectile.h" | 
|---|
| [4834] | 25 |  | 
|---|
| [7193] | 26 | #include "util/loading/resource_manager.h" | 
|---|
| [4894] | 27 | #include "class_list.h" | 
|---|
| [7350] | 28 | #include "util/loading/factory.h" | 
|---|
| [7193] | 29 | #include "util/loading/load_param.h" | 
|---|
| [4828] | 30 | #include "state.h" | 
|---|
| [4885] | 31 | #include "animation3d.h" | 
|---|
| [3573] | 32 |  | 
|---|
| [5930] | 33 | #include "sound_source.h" | 
|---|
 | 34 | #include "sound_buffer.h" | 
|---|
 | 35 |  | 
|---|
| [8976] | 36 | #include "elements/glgui_energywidget.h" | 
|---|
| [6438] | 37 |  | 
|---|
| [7350] | 38 |  | 
|---|
| [9406] | 39 |  | 
|---|
| [4892] | 40 | //////////////////// | 
|---|
 | 41 | // INITAILISATION // | 
|---|
 | 42 | // SETTING VALUES // | 
|---|
 | 43 | //////////////////// | 
|---|
| [3870] | 44 | /** | 
|---|
| [4885] | 45 |  * standard constructor | 
|---|
 | 46 |  * | 
|---|
 | 47 |  * creates a new weapon | 
|---|
| [3575] | 48 | */ | 
|---|
| [5750] | 49 | Weapon::Weapon () | 
|---|
| [3620] | 50 | { | 
|---|
| [4885] | 51 |   this->init(); | 
|---|
| [3620] | 52 | } | 
|---|
| [3573] | 53 |  | 
|---|
| [3575] | 54 | /** | 
|---|
| [4885] | 55 |  * standard deconstructor | 
|---|
| [3575] | 56 | */ | 
|---|
| [4597] | 57 | Weapon::~Weapon () | 
|---|
| [3573] | 58 | { | 
|---|
| [4885] | 59 |   for (int i = 0; i < WS_STATE_COUNT; i++) | 
|---|
| [4894] | 60 |     if (this->animation[i] && ClassList::exists(animation[i], CL_ANIMATION))  //!< @todo this should check animation3D | 
|---|
| [4885] | 61 |       delete this->animation[i]; | 
|---|
 | 62 |   for (int i = 0; i < WA_ACTION_COUNT; i++) | 
|---|
| [5302] | 63 |     if (this->soundBuffers[i] != NULL && ClassList::exists(this->soundBuffers[i], CL_SOUND_BUFFER)) | 
|---|
| [4885] | 64 |       ResourceManager::getInstance()->unload(this->soundBuffers[i]); | 
|---|
| [4959] | 65 |  | 
|---|
 | 66 |   if (ClassList::exists(this->soundSource, CL_SOUND_SOURCE)) | 
|---|
 | 67 |     delete this->soundSource; | 
|---|
| [4885] | 68 | } | 
|---|
| [4597] | 69 |  | 
|---|
| [4885] | 70 | /** | 
|---|
| [7350] | 71 |  * @brief creates a new Weapon of type weaponID and returns it. | 
|---|
 | 72 |  * @param weaponID the WeaponID type to create. | 
|---|
 | 73 |  * @returns the newly created Weapon. | 
|---|
 | 74 |  */ | 
|---|
 | 75 | Weapon* Weapon::createWeapon(ClassID weaponID) | 
|---|
 | 76 | { | 
|---|
 | 77 |   BaseObject* createdObject = Factory::fabricate(weaponID); | 
|---|
 | 78 |   if (createdObject != NULL) | 
|---|
 | 79 |   { | 
|---|
 | 80 |     if (createdObject->isA(CL_WEAPON)) | 
|---|
 | 81 |       return dynamic_cast<Weapon*>(createdObject); | 
|---|
 | 82 |     else | 
|---|
 | 83 |     { | 
|---|
 | 84 |       delete createdObject; | 
|---|
 | 85 |       return NULL; | 
|---|
 | 86 |     } | 
|---|
 | 87 |   } | 
|---|
| [8316] | 88 |   return NULL; | 
|---|
| [7350] | 89 | } | 
|---|
 | 90 |  | 
|---|
 | 91 | /** | 
|---|
| [4885] | 92 |  * initializes the Weapon with ALL default values | 
|---|
| [5498] | 93 |  * | 
|---|
 | 94 |  * This Sets the default values of the Weapon | 
|---|
| [4885] | 95 |  */ | 
|---|
 | 96 | void Weapon::init() | 
|---|
 | 97 | { | 
|---|
| [7350] | 98 |   this->setClassID(CL_WEAPON, "Weapon"); | 
|---|
| [5498] | 99 |   this->currentState     = WS_INACTIVE;            //< Normaly the Weapon is Inactive | 
|---|
 | 100 |   this->requestedAction  = WA_NONE;                //< No action is requested by default | 
|---|
 | 101 |   this->stateDuration    = 0.0;                    //< All the States have zero duration | 
|---|
 | 102 |   for (int i = 0; i < WS_STATE_COUNT; i++)         //< Every State has: | 
|---|
| [6438] | 103 |   { | 
|---|
 | 104 |     this->times[i] = 0.0;                        //< An infinitesimal duration | 
|---|
 | 105 |     this->animation[i] = NULL;                   //< No animation | 
|---|
 | 106 |   } | 
|---|
| [4885] | 107 |   for (int i = 0; i < WA_ACTION_COUNT; i++) | 
|---|
| [5498] | 108 |     this->soundBuffers[i] = NULL;                  //< No Sounds | 
|---|
| [3888] | 109 |  | 
|---|
| [7460] | 110 |   this->soundSource = new OrxSound::SoundSource(this);       //< Every Weapon has exacty one SoundSource. | 
|---|
| [5498] | 111 |   this->emissionPoint.setParent(this);             //< One EmissionPoint, that is a PNode connected to the weapon. You can set this to the exitting point of the Projectiles | 
|---|
| [7126] | 112 |   this->emissionPoint.setName("EmissionPoint"); | 
|---|
 | 113 |   this->emissionPoint.addNodeFlags(PNODE_PROHIBIT_DELETE_WITH_PARENT); | 
|---|
| [4885] | 114 |  | 
|---|
| [6920] | 115 |   this->defaultTarget = NULL;                      //< Nothing is Targeted by default. | 
|---|
| [6756] | 116 |  | 
|---|
| [5498] | 117 |   this->projectile = CL_NULL;                      //< No Projectile Class is Connected to this weapon | 
|---|
 | 118 |   this->projectileFactory = NULL;                  //< No Factory generating Projectiles is selected. | 
|---|
| [4885] | 119 |  | 
|---|
| [5498] | 120 |   this->hideInactive = true;                       //< The Weapon will be hidden if it is inactive (by default) | 
|---|
| [4906] | 121 |  | 
|---|
| [5498] | 122 |   this->minCharge = 1.0;                           //< The minimum charge the Weapon can hold is 1 unit. | 
|---|
 | 123 |   this->maxCharge = 1.0;                           //< The maximum charge is also one unit. | 
|---|
| [4927] | 124 |  | 
|---|
| [6671] | 125 |   this->energy = 10;                               //< The secondary Buffer (before we have to reload) | 
|---|
| [5498] | 126 |   this->energyMax = 10.0;                          //< How much energy can be carried | 
|---|
 | 127 |   this->capability = WTYPE_ALL;                    //< The Weapon has all capabilities @see W_Capability. | 
|---|
| [6438] | 128 |  | 
|---|
 | 129 |   this->energyWidget = NULL; | 
|---|
| [6695] | 130 |  | 
|---|
 | 131 |   // set this object to be synchronized over network | 
|---|
 | 132 |   //this->setSynchronized(true); | 
|---|
| [3573] | 133 | } | 
|---|
 | 134 |  | 
|---|
| [5498] | 135 | /** | 
|---|
 | 136 |  * loads the Parameters of a Weapon | 
|---|
 | 137 |  * @param root the XML-Element to load the Weapons settings from | 
|---|
 | 138 |  */ | 
|---|
| [4972] | 139 | void Weapon::loadParams(const TiXmlElement* root) | 
|---|
 | 140 | { | 
|---|
| [6512] | 141 |   WorldEntity::loadParams(root); | 
|---|
| [4972] | 142 |  | 
|---|
| [7102] | 143 |   LoadParam(root, "projectile", this, Weapon, setProjectileTypeC) | 
|---|
| [6438] | 144 |   .describe("Sets the name of the Projectile to load onto the Entity"); | 
|---|
| [4972] | 145 |  | 
|---|
| [5671] | 146 |   LoadParam(root, "emission-point", this, Weapon, setEmissionPoint) | 
|---|
| [6438] | 147 |   .describe("Sets the Point of emission of this weapon"); | 
|---|
| [4972] | 148 |  | 
|---|
| [5671] | 149 |   LoadParam(root, "state-duration", this, Weapon, setStateDuration) | 
|---|
| [6438] | 150 |   .describe("Sets the duration of a given state (1: state-Name; 2: duration in seconds)"); | 
|---|
| [4972] | 151 |  | 
|---|
| [5671] | 152 |   LoadParam(root, "action-sound", this, Weapon, setActionSound) | 
|---|
| [6438] | 153 |   .describe("Sets a given sound to an action (1: action-Name; 2: name of the sound (relative to the Data-Path))"); | 
|---|
| [4972] | 154 | } | 
|---|
 | 155 |  | 
|---|
| [6728] | 156 |  | 
|---|
| [4947] | 157 | /** | 
|---|
 | 158 |  * sets the Projectile to use for this weapon. | 
|---|
 | 159 |  * @param projectile The ID of the Projectile to use | 
|---|
| [4950] | 160 |  * @returns true, if it was sucessfull, false on error | 
|---|
| [4947] | 161 |  * | 
|---|
| [5498] | 162 |  * be aware, that this function does not create Factories, as this is job of Projecitle/Bullet-classes. | 
|---|
 | 163 |  * What it does, is telling the Weapon what Projectiles it can Emit. | 
|---|
| [4947] | 164 |  */ | 
|---|
| [5356] | 165 | void Weapon::setProjectileType(ClassID projectile) | 
|---|
| [4947] | 166 | { | 
|---|
 | 167 |   if (projectile == CL_NULL) | 
|---|
| [4972] | 168 |     return; | 
|---|
| [4947] | 169 |   this->projectile = projectile; | 
|---|
 | 170 |   this->projectileFactory = FastFactory::searchFastFactory(projectile); | 
|---|
 | 171 |   if (this->projectileFactory == NULL) | 
|---|
| [4979] | 172 |   { | 
|---|
 | 173 |     PRINTF(1)("unable to find FastFactory for the Projectile.\n"); | 
|---|
| [4972] | 174 |     return; | 
|---|
| [4979] | 175 |   } | 
|---|
| [4948] | 176 |   else | 
|---|
 | 177 |   { | 
|---|
 | 178 |     // grabbing Parameters from the Projectile to have them at hand here. | 
|---|
 | 179 |     Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect()); | 
|---|
| [6431] | 180 |     this->minCharge = pj->getMinEnergy(); | 
|---|
| [6700] | 181 |     this->maxCharge = pj->getHealthMax(); | 
|---|
| [4948] | 182 |     this->chargeable = pj->isChageable(); | 
|---|
| [4979] | 183 |     this->projectileFactory->kill(pj); | 
|---|
| [4948] | 184 |   } | 
|---|
| [4979] | 185 | } | 
|---|
| [3573] | 186 |  | 
|---|
| [6728] | 187 |  | 
|---|
| [4891] | 188 | /** | 
|---|
| [4950] | 189 |  * @see bool Weapon::setProjectile(ClassID projectile) | 
|---|
 | 190 |  * @param projectile the Name of the Projectile. | 
|---|
 | 191 |  */ | 
|---|
| [7221] | 192 | void Weapon::setProjectileTypeC(const std::string& projectile) | 
|---|
| [4950] | 193 | { | 
|---|
 | 194 |   FastFactory* tmpFac = FastFactory::searchFastFactory(projectile); | 
|---|
 | 195 |   if (tmpFac != NULL) | 
|---|
 | 196 |   { | 
|---|
| [5356] | 197 |     this->setProjectileType(tmpFac->getStoredID()); | 
|---|
| [4950] | 198 |   } | 
|---|
| [4972] | 199 |   else | 
|---|
 | 200 |   { | 
|---|
| [9406] | 201 |     PRINTF(1)("Projectile %s does not exist for weapon %s\n", projectile.c_str(), this->getCName()); | 
|---|
| [4972] | 202 |   } | 
|---|
| [4950] | 203 | } | 
|---|
 | 204 |  | 
|---|
| [6728] | 205 |  | 
|---|
| [4950] | 206 | /** | 
|---|
| [5356] | 207 |  * prepares Projectiles of the Weapon | 
|---|
| [5498] | 208 |  * @param count how many Projectiles to create (they will be stored in the ProjectileFactory) | 
|---|
| [5356] | 209 |  */ | 
|---|
 | 210 | void Weapon::prepareProjectiles(unsigned int count) | 
|---|
 | 211 | { | 
|---|
| [5357] | 212 |   if (likely(this->projectileFactory != NULL)) | 
|---|
| [5356] | 213 |     projectileFactory->prepare(count); | 
|---|
 | 214 |   else | 
|---|
| [9406] | 215 |     PRINTF(2)("unable to create %d projectile for Weapon %s::%s\n", count, this->getClassCName(), this->getCName()); | 
|---|
| [5356] | 216 | } | 
|---|
 | 217 |  | 
|---|
| [6728] | 218 |  | 
|---|
| [5356] | 219 | /** | 
|---|
 | 220 |  * resurects and returns a Projectile | 
|---|
| [5498] | 221 |  * @returns a Projectile on success, NULL on error | 
|---|
 | 222 |  * | 
|---|
 | 223 |  * errors: 1. (ProjectileFastFactory not Found) | 
|---|
 | 224 |  *         2. No more Projectiles availiable. | 
|---|
| [5356] | 225 |  */ | 
|---|
 | 226 | Projectile* Weapon::getProjectile() | 
|---|
 | 227 | { | 
|---|
| [5357] | 228 |   if (likely (this->projectileFactory != NULL)) | 
|---|
| [6142] | 229 |   { | 
|---|
 | 230 |     Projectile* pj = dynamic_cast<Projectile*>(this->projectileFactory->resurrect()); | 
|---|
 | 231 |     pj->toList((OM_LIST)(this->getOMListNumber()+1)); | 
|---|
 | 232 |     return pj; | 
|---|
 | 233 |   } | 
|---|
| [5356] | 234 |   else | 
|---|
 | 235 |   { | 
|---|
| [9406] | 236 |     PRINTF(2)("No projectile defined for Weapon %s(%s) can't return any\n", this->getCName(), this->getClassCName()); | 
|---|
| [5356] | 237 |     return NULL; | 
|---|
 | 238 |   } | 
|---|
 | 239 | } | 
|---|
 | 240 |  | 
|---|
 | 241 |  | 
|---|
 | 242 | /** | 
|---|
| [4892] | 243 |  * sets the emissionPoint's relative position from the Weapon | 
|---|
 | 244 |  * @param point the Point relative to the mass-point of the Weapon | 
|---|
 | 245 |  */ | 
|---|
 | 246 | void Weapon::setEmissionPoint(const Vector& point) | 
|---|
 | 247 | { | 
|---|
 | 248 |   this->emissionPoint.setRelCoor(point); | 
|---|
 | 249 | } | 
|---|
 | 250 |  | 
|---|
| [6728] | 251 |  | 
|---|
| [4892] | 252 | /** | 
|---|
| [4891] | 253 |  * assigns a Sound-file to an action | 
|---|
 | 254 |  * @param action the action the sound should be assigned too | 
|---|
 | 255 |  * @param soundFile the soundFile's relative position to the data-directory (will be looked for by the ResourceManager) | 
|---|
 | 256 |  */ | 
|---|
| [7221] | 257 | void Weapon::setActionSound(WeaponAction action, const std::string& soundFile) | 
|---|
| [4885] | 258 | { | 
|---|
 | 259 |   if (action >= WA_ACTION_COUNT) | 
|---|
 | 260 |     return; | 
|---|
| [4930] | 261 |   if (this->soundBuffers[action] != NULL) | 
|---|
 | 262 |     ResourceManager::getInstance()->unload(this->soundBuffers[action]); | 
|---|
 | 263 |  | 
|---|
| [7221] | 264 |   else if (!soundFile.empty()) | 
|---|
| [4885] | 265 |   { | 
|---|
| [7460] | 266 |     this->soundBuffers[action] = (OrxSound::SoundBuffer*)ResourceManager::getInstance()->load(soundFile, WAV); | 
|---|
| [4885] | 267 |     if (this->soundBuffers[action] != NULL) | 
|---|
 | 268 |     { | 
|---|
| [7221] | 269 |       PRINTF(4)("Loaded sound %s to action %s.\n", soundFile.c_str(), actionToChar(action)); | 
|---|
| [4885] | 270 |     } | 
|---|
 | 271 |     else | 
|---|
 | 272 |     { | 
|---|
| [7221] | 273 |       PRINTF(2)("Failed to load sound %s to %s.\n.", soundFile.c_str(), actionToChar(action)); | 
|---|
| [4885] | 274 |     } | 
|---|
 | 275 |   } | 
|---|
 | 276 |   else | 
|---|
 | 277 |     this->soundBuffers[action] = NULL; | 
|---|
 | 278 | } | 
|---|
 | 279 |  | 
|---|
| [6728] | 280 |  | 
|---|
| [4893] | 281 | /** | 
|---|
| [4895] | 282 |  * creates/returns an Animation3D for a certain State. | 
|---|
 | 283 |  * @param state what State should the Animation be created/returned for | 
|---|
 | 284 |  * @param node the node this Animation should apply to. (NULL is fine if the animation was already created) | 
|---|
 | 285 |  * @returns The created animation.Animation(), NULL on error (or if the animation does not yet exist). | 
|---|
| [4893] | 286 |  * | 
|---|
 | 287 |  * This function does only generate the Animation Object, and if set it will | 
|---|
 | 288 |  * automatically be executed, when a certain State is reached. | 
|---|
 | 289 |  * What this does not do, is set keyframes, you have to operate on the returned animation. | 
|---|
 | 290 |  */ | 
|---|
| [4895] | 291 | Animation3D* Weapon::getAnimation(WeaponState state, PNode* node) | 
|---|
| [4893] | 292 | { | 
|---|
| [4895] | 293 |   if (state >= WS_STATE_COUNT) // if the state is not known | 
|---|
| [4893] | 294 |     return NULL; | 
|---|
 | 295 |  | 
|---|
| [4895] | 296 |   if (unlikely(this->animation[state] == NULL)) // if the animation does not exist yet create it. | 
|---|
| [4893] | 297 |   { | 
|---|
| [4895] | 298 |     if (likely(node != NULL)) | 
|---|
 | 299 |       return this->animation[state] = new Animation3D(node); | 
|---|
 | 300 |     else | 
|---|
 | 301 |     { | 
|---|
 | 302 |       PRINTF(2)("Node not defined for the Creation of the 3D-animation of state %s\n", stateToChar(state)); | 
|---|
 | 303 |       return NULL; | 
|---|
 | 304 |     } | 
|---|
| [4893] | 305 |   } | 
|---|
| [4895] | 306 |   else | 
|---|
 | 307 |     return this->animation[state]; | 
|---|
| [4893] | 308 | } | 
|---|
 | 309 |  | 
|---|
| [7779] | 310 | OrxGui::GLGuiWidget* Weapon::getEnergyWidget() | 
|---|
| [6438] | 311 | { | 
|---|
 | 312 |   if (this->energyWidget == NULL) | 
|---|
 | 313 |   { | 
|---|
| [8977] | 314 |     this->energyWidget = new OrxGui::GLGuiEnergyWidget(); | 
|---|
| [9406] | 315 |     this->energyWidget->setDisplayedName(this->getClassCName()); | 
|---|
| [6438] | 316 |     this->energyWidget->setSize2D( 20, 100); | 
|---|
 | 317 |     this->energyWidget->setMaximum(this->getEnergyMax()); | 
|---|
 | 318 |     this->energyWidget->setValue(this->getEnergy()); | 
|---|
 | 319 |   } | 
|---|
 | 320 |   return this->energyWidget; | 
|---|
 | 321 | } | 
|---|
 | 322 |  | 
|---|
 | 323 | void Weapon::updateWidgets() | 
|---|
 | 324 | { | 
|---|
 | 325 |   if (this->energyWidget != NULL) | 
|---|
 | 326 |   { | 
|---|
 | 327 |     this->energyWidget->setMaximum(this->energyMax); | 
|---|
 | 328 |     this->energyWidget->setValue(this->energy); | 
|---|
 | 329 |   } | 
|---|
 | 330 | } | 
|---|
 | 331 |  | 
|---|
| [4892] | 332 | ///////////////// | 
|---|
 | 333 | //  EXECUTION  // | 
|---|
 | 334 | // GAME ACTION // | 
|---|
 | 335 | ///////////////// | 
|---|
| [4597] | 336 | /** | 
|---|
| [4885] | 337 |  * request an action that should be executed, | 
|---|
 | 338 |  * @param action the next action to take | 
|---|
 | 339 |  * | 
|---|
 | 340 |  * This function must be called instead of the actions (like fire/reload...) | 
|---|
 | 341 |  * to make all the checks needed to have a usefull WeaponSystem. | 
|---|
 | 342 |  */ | 
|---|
 | 343 | void Weapon::requestAction(WeaponAction action) | 
|---|
 | 344 | { | 
|---|
| [4906] | 345 |   if (likely(this->isActive())) | 
|---|
| [4885] | 346 |   { | 
|---|
| [4906] | 347 |     if (this->requestedAction != WA_NONE) | 
|---|
 | 348 |       return; | 
|---|
| [5041] | 349 |     PRINTF(5)("next action will be %s in %f seconds\n", actionToChar(action), this->stateDuration); | 
|---|
| [4885] | 350 |     this->requestedAction = action; | 
|---|
 | 351 |   } | 
|---|
| [4906] | 352 |   //else | 
|---|
 | 353 |   else if (unlikely(action == WA_ACTIVATE)) | 
|---|
 | 354 |   { | 
|---|
 | 355 |     this->currentState = WS_ACTIVATING; | 
|---|
| [4926] | 356 |     this->requestedAction = WA_ACTIVATE; | 
|---|
| [4906] | 357 |   } | 
|---|
| [4885] | 358 | } | 
|---|
| [3577] | 359 |  | 
|---|
| [6728] | 360 |  | 
|---|
| [4890] | 361 | /** | 
|---|
 | 362 |  * adds energy to the Weapon | 
|---|
 | 363 |  * @param energyToAdd The amount of energy | 
|---|
 | 364 |  * @returns the amount of energy we did not pick up, because the weapon is already full | 
|---|
 | 365 |  */ | 
|---|
 | 366 | float Weapon::increaseEnergy(float energyToAdd) | 
|---|
 | 367 | { | 
|---|
 | 368 |   float maxAddEnergy = this->energyMax - this->energy; | 
|---|
 | 369 |  | 
|---|
 | 370 |   if (maxAddEnergy >= energyToAdd) | 
|---|
 | 371 |   { | 
|---|
 | 372 |     this->energy += energyToAdd; | 
|---|
 | 373 |     return 0.0; | 
|---|
 | 374 |   } | 
|---|
 | 375 |   else | 
|---|
 | 376 |   { | 
|---|
 | 377 |     this->energy += maxAddEnergy; | 
|---|
 | 378 |     return energyToAdd - maxAddEnergy; | 
|---|
 | 379 |   } | 
|---|
 | 380 | } | 
|---|
 | 381 |  | 
|---|
| [6728] | 382 |  | 
|---|
| [5498] | 383 | //////////////////////////////////////////////////////////// | 
|---|
 | 384 | // WEAPON INTERNALS                                       // | 
|---|
 | 385 | // These are functions, that no other Weapon should over- // | 
|---|
 | 386 | // write. No class has direct Access to them, as it is    // | 
|---|
 | 387 | // quite a complicated process, handling a Weapon from    // | 
|---|
 | 388 | // the outside                                            // | 
|---|
 | 389 | //////////////////////////////////////////////////////////// | 
|---|
| [4891] | 390 | /** | 
|---|
 | 391 |  * executes an action, and with it starts a new State. | 
|---|
 | 392 |  * @return true, if it worked, false otherwise | 
|---|
 | 393 |  * | 
|---|
 | 394 |  * This function checks, wheter the possibility of executing an action is valid, | 
|---|
 | 395 |  * and does all the necessary stuff, to execute them. If an action does not succeed, | 
|---|
 | 396 |  * it tries to go around it. (ex. shoot->noAmo->reload()->wait until shoot comes again) | 
|---|
 | 397 |  */ | 
|---|
| [4885] | 398 | bool Weapon::execute() | 
|---|
| [3583] | 399 | { | 
|---|
| [7729] | 400 | #if DEBUG_LEVEL > 4 | 
|---|
| [4885] | 401 |   PRINTF(4)("trying to execute action %s\n", actionToChar(this->requestedAction)); | 
|---|
 | 402 |   this->debug(); | 
|---|
| [4906] | 403 | #endif | 
|---|
| [4885] | 404 |  | 
|---|
| [4926] | 405 |   WeaponAction action = this->requestedAction; | 
|---|
 | 406 |   this->requestedAction = WA_NONE; | 
|---|
 | 407 |  | 
|---|
 | 408 |   switch (action) | 
|---|
| [4885] | 409 |   { | 
|---|
| [7350] | 410 |     case WA_SHOOT: | 
|---|
 | 411 |       return this->fireW(); | 
|---|
 | 412 |       break; | 
|---|
 | 413 |     case WA_CHARGE: | 
|---|
 | 414 |       return this->chargeW(); | 
|---|
 | 415 |       break; | 
|---|
 | 416 |     case WA_RELOAD: | 
|---|
 | 417 |       return this->reloadW(); | 
|---|
 | 418 |       break; | 
|---|
 | 419 |     case WA_DEACTIVATE: | 
|---|
 | 420 |       return this->deactivateW(); | 
|---|
 | 421 |       break; | 
|---|
 | 422 |     case WA_ACTIVATE: | 
|---|
 | 423 |       return this->activateW(); | 
|---|
 | 424 |       break; | 
|---|
| [8316] | 425 |     default: | 
|---|
 | 426 |       PRINTF(2)("Action %s Not Implemented yet \n", Weapon::actionToChar(action)); | 
|---|
 | 427 |       return false; | 
|---|
| [4885] | 428 |   } | 
|---|
| [3583] | 429 | } | 
|---|
| [3577] | 430 |  | 
|---|
| [4597] | 431 | /** | 
|---|
| [4894] | 432 |  * checks and activates the Weapon. | 
|---|
 | 433 |  * @return true on success. | 
|---|
| [4892] | 434 |  */ | 
|---|
 | 435 | bool Weapon::activateW() | 
|---|
| [3583] | 436 | { | 
|---|
| [6438] | 437 |   //  if (this->currentState == WS_INACTIVE) | 
|---|
| [4892] | 438 |   { | 
|---|
| [6433] | 439 |     // play Sound | 
|---|
| [4893] | 440 |     if (likely(this->soundBuffers[WA_ACTIVATE] != NULL)) | 
|---|
| [4892] | 441 |       this->soundSource->play(this->soundBuffers[WA_ACTIVATE]); | 
|---|
| [6444] | 442 |     this->updateWidgets(); | 
|---|
| [6438] | 443 |     // activate | 
|---|
| [9406] | 444 |     PRINTF(4)("Activating the Weapon %s\n", this->getCName()); | 
|---|
| [4892] | 445 |     this->activate(); | 
|---|
| [4895] | 446 |     // setting up for next action | 
|---|
| [4926] | 447 |     this->enterState(WS_ACTIVATING); | 
|---|
| [4892] | 448 |   } | 
|---|
| [8316] | 449 |   return true; | 
|---|
| [3583] | 450 | } | 
|---|
| [3577] | 451 |  | 
|---|
| [4597] | 452 | /** | 
|---|
| [4894] | 453 |  * checks and deactivates the Weapon | 
|---|
 | 454 |  * @return true on success. | 
|---|
| [4892] | 455 |  */ | 
|---|
 | 456 | bool Weapon::deactivateW() | 
|---|
| [3583] | 457 | { | 
|---|
| [6438] | 458 |   //  if (this->currentState != WS_INACTIVE) | 
|---|
| [4892] | 459 |   { | 
|---|
| [9406] | 460 |     PRINTF(4)("Deactivating the Weapon %s\n", this->getCName()); | 
|---|
| [6438] | 461 |     // play Sound | 
|---|
| [4892] | 462 |     if (this->soundBuffers[WA_DEACTIVATE] != NULL) | 
|---|
 | 463 |       this->soundSource->play(this->soundBuffers[WA_DEACTIVATE]); | 
|---|
| [4926] | 464 |     // deactivate | 
|---|
| [4892] | 465 |     this->deactivate(); | 
|---|
| [4926] | 466 |     this->enterState(WS_DEACTIVATING); | 
|---|
| [4892] | 467 |   } | 
|---|
| [8316] | 468 |  | 
|---|
 | 469 |   return true; | 
|---|
| [3583] | 470 | } | 
|---|
| [3577] | 471 |  | 
|---|
| [4892] | 472 | /** | 
|---|
| [4894] | 473 |  * checks and charges the Weapon | 
|---|
 | 474 |  * @return true on success. | 
|---|
| [4892] | 475 |  */ | 
|---|
 | 476 | bool Weapon::chargeW() | 
|---|
| [4885] | 477 | { | 
|---|
| [6671] | 478 |   if ( this->currentState != WS_INACTIVE && this->energy >= this->minCharge) | 
|---|
| [4892] | 479 |   { | 
|---|
| [6438] | 480 |     // playing Sound | 
|---|
| [4892] | 481 |     if (this->soundBuffers[WA_CHARGE] != NULL) | 
|---|
 | 482 |       this->soundSource->play(this->soundBuffers[WA_CHARGE]); | 
|---|
| [4893] | 483 |  | 
|---|
| [6438] | 484 |     // charge | 
|---|
| [4892] | 485 |     this->charge(); | 
|---|
| [6438] | 486 |     // setting up for the next state | 
|---|
| [4926] | 487 |     this->enterState(WS_CHARGING); | 
|---|
| [4892] | 488 |   } | 
|---|
 | 489 |   else // deactivate the Weapon if we do not have enough energy | 
|---|
 | 490 |   { | 
|---|
 | 491 |     this->requestAction(WA_RELOAD); | 
|---|
 | 492 |   } | 
|---|
| [8316] | 493 |   return true; | 
|---|
| [4885] | 494 | } | 
|---|
| [3573] | 495 |  | 
|---|
| [4892] | 496 | /** | 
|---|
| [4894] | 497 |  * checks and fires the Weapon | 
|---|
 | 498 |  * @return true on success. | 
|---|
| [4892] | 499 |  */ | 
|---|
 | 500 | bool Weapon::fireW() | 
|---|
| [3575] | 501 | { | 
|---|
| [6438] | 502 |   //if (likely(this->currentState != WS_INACTIVE)) | 
|---|
| [6671] | 503 |   if (this->minCharge <= this->energy) | 
|---|
| [4892] | 504 |   { | 
|---|
| [6438] | 505 |     // playing Sound | 
|---|
| [4892] | 506 |     if (this->soundBuffers[WA_SHOOT] != NULL) | 
|---|
 | 507 |       this->soundSource->play(this->soundBuffers[WA_SHOOT]); | 
|---|
| [6444] | 508 |     this->updateWidgets(); | 
|---|
| [6438] | 509 |     // fire | 
|---|
| [6671] | 510 |     this->energy -= this->minCharge; | 
|---|
| [4892] | 511 |     this->fire(); | 
|---|
| [6438] | 512 |     // setting up for the next state | 
|---|
| [4926] | 513 |     this->enterState(WS_SHOOTING); | 
|---|
| [4892] | 514 |   } | 
|---|
 | 515 |   else  // reload if we still have the charge | 
|---|
 | 516 |   { | 
|---|
 | 517 |     this->requestAction(WA_RELOAD); | 
|---|
| [4930] | 518 |     this->execute(); | 
|---|
| [4892] | 519 |   } | 
|---|
| [8316] | 520 |   return true; | 
|---|
| [4892] | 521 | } | 
|---|
 | 522 |  | 
|---|
 | 523 | /** | 
|---|
| [4894] | 524 |  * checks and Reloads the Weapon | 
|---|
 | 525 |  * @return true on success. | 
|---|
| [4892] | 526 |  */ | 
|---|
 | 527 | bool Weapon::reloadW() | 
|---|
 | 528 | { | 
|---|
| [9406] | 529 |   PRINTF(4)("Reloading Weapon %s\n", this->getCName()); | 
|---|
| [6671] | 530 |   if (this->ammoContainer.get() != NULL && | 
|---|
| [7350] | 531 |       unlikely(this->energy + this->ammoContainer->getStoredEnergy() < this->minCharge)) | 
|---|
| [4885] | 532 |   { | 
|---|
 | 533 |     this->requestAction(WA_DEACTIVATE); | 
|---|
| [4930] | 534 |     this->execute(); | 
|---|
| [4892] | 535 |     return false; | 
|---|
| [4885] | 536 |   } | 
|---|
| [3573] | 537 |  | 
|---|
 | 538 |  | 
|---|
| [4892] | 539 |   if (this->soundBuffers[WA_RELOAD] != NULL) | 
|---|
 | 540 |     this->soundSource->play(this->soundBuffers[WA_RELOAD]); | 
|---|
 | 541 |  | 
|---|
| [6671] | 542 |   if (this->ammoContainer.get() != NULL) | 
|---|
 | 543 |     this->ammoContainer->fillWeapon(this); | 
|---|
| [4885] | 544 |   else | 
|---|
 | 545 |   { | 
|---|
| [6671] | 546 |     this->energy = this->energyMax; | 
|---|
| [4885] | 547 |   } | 
|---|
| [6444] | 548 |   this->updateWidgets(); | 
|---|
| [4892] | 549 |   this->reload(); | 
|---|
| [4926] | 550 |   this->enterState(WS_RELOADING); | 
|---|
| [8316] | 551 |  | 
|---|
 | 552 |   return true; | 
|---|
| [4926] | 553 | } | 
|---|
| [3575] | 554 |  | 
|---|
| [4926] | 555 | /** | 
|---|
 | 556 |  * enters the requested State, plays back animations updates the timing. | 
|---|
 | 557 |  * @param state the state to enter. | 
|---|
 | 558 |  */ | 
|---|
 | 559 | inline void Weapon::enterState(WeaponState state) | 
|---|
 | 560 | { | 
|---|
| [5041] | 561 |   PRINTF(4)("ENTERING STATE %s\n", stateToChar(state)); | 
|---|
| [4926] | 562 |   // playing animation if availiable | 
|---|
 | 563 |   if (likely(this->animation[state] != NULL)) | 
|---|
 | 564 |     this->animation[state]->replay(); | 
|---|
 | 565 |  | 
|---|
| [6728] | 566 |   this->stateDuration += this->times[state]; | 
|---|
| [4926] | 567 |   this->currentState = state; | 
|---|
| [3575] | 568 | } | 
|---|
 | 569 |  | 
|---|
| [4927] | 570 | /////////////////// | 
|---|
 | 571 | //  WORLD-ENTITY // | 
|---|
 | 572 | // FUNCTIONALITY // | 
|---|
 | 573 | /////////////////// | 
|---|
| [3575] | 574 | /** | 
|---|
| [4885] | 575 |  * tick signal for time dependent/driven stuff | 
|---|
| [3575] | 576 | */ | 
|---|
| [6736] | 577 | bool Weapon::tickW(float dt) | 
|---|
| [4885] | 578 | { | 
|---|
| [4934] | 579 |   //printf("%s ", stateToChar(this->currentState)); | 
|---|
| [4910] | 580 |  | 
|---|
| [4885] | 581 |   // setting up the timing properties | 
|---|
 | 582 |   this->stateDuration -= dt; | 
|---|
| [3575] | 583 |  | 
|---|
| [4949] | 584 |   if (this->stateDuration <= 0.0) | 
|---|
| [4885] | 585 |   { | 
|---|
| [4949] | 586 |     if (unlikely (this->currentState == WS_DEACTIVATING)) | 
|---|
| [4885] | 587 |     { | 
|---|
| [4949] | 588 |       this->currentState = WS_INACTIVE; | 
|---|
| [6736] | 589 |       return false; | 
|---|
| [4949] | 590 |     } | 
|---|
 | 591 |     else | 
|---|
 | 592 |       this->currentState = WS_IDLE; | 
|---|
| [4906] | 593 |  | 
|---|
| [4949] | 594 |     if (this->requestedAction != WA_NONE) | 
|---|
 | 595 |     { | 
|---|
 | 596 |       this->stateDuration = -dt; | 
|---|
 | 597 |       this->execute(); | 
|---|
| [4885] | 598 |     } | 
|---|
 | 599 |   } | 
|---|
| [6736] | 600 |   return true; | 
|---|
| [4885] | 601 | } | 
|---|
 | 602 |  | 
|---|
| [3575] | 603 |  | 
|---|
 | 604 |  | 
|---|
 | 605 |  | 
|---|
| [4885] | 606 | ////////////////////// | 
|---|
 | 607 | // HELPER FUNCTIONS // | 
|---|
 | 608 | ////////////////////// | 
|---|
| [3576] | 609 | /** | 
|---|
| [4891] | 610 |  * checks wether all the Weapons functions are valid, and if it is possible to go to action with it. | 
|---|
| [5498] | 611 |  * @todo IMPLEMENT the Weapons Check | 
|---|
| [4891] | 612 |  */ | 
|---|
 | 613 | bool Weapon::check() const | 
|---|
 | 614 | { | 
|---|
 | 615 |   bool retVal = true; | 
|---|
 | 616 |  | 
|---|
| [6438] | 617 |   //  if (this->projectile == NULL) | 
|---|
| [4891] | 618 |   { | 
|---|
| [5041] | 619 |     PRINTF(1)("There was no projectile assigned to the Weapon.\n"); | 
|---|
| [4891] | 620 |     retVal = false; | 
|---|
 | 621 |   } | 
|---|
 | 622 |  | 
|---|
 | 623 |  | 
|---|
 | 624 |  | 
|---|
 | 625 |  | 
|---|
 | 626 |   return retVal; | 
|---|
 | 627 | } | 
|---|
 | 628 |  | 
|---|
 | 629 | /** | 
|---|
| [4885] | 630 |  * some nice debugging information about this Weapon | 
|---|
 | 631 |  */ | 
|---|
 | 632 | void Weapon::debug() const | 
|---|
 | 633 | { | 
|---|
| [9406] | 634 |   PRINT(0)("Weapon-Debug %s, state: %s (duration: %fs), nextAction: %s\n", this->getCName(), Weapon::stateToChar(this->currentState), this->stateDuration, Weapon::actionToChar(requestedAction)); | 
|---|
| [6671] | 635 |   PRINT(0)("Energy: max: %f; current: %f; chargeMin: %f, chargeMax %f\n", | 
|---|
 | 636 |            this->energyMax, this->energy, this->minCharge, this->maxCharge); | 
|---|
| [4967] | 637 |  | 
|---|
 | 638 |  | 
|---|
| [4885] | 639 | } | 
|---|
| [3575] | 640 |  | 
|---|
| [5498] | 641 | //////////////////////////////////////////////////////// | 
|---|
 | 642 | // static Definitions (transormators for readability) // | 
|---|
 | 643 | //////////////////////////////////////////////////////// | 
|---|
| [4885] | 644 | /** | 
|---|
 | 645 |  * Converts a String into an Action. | 
|---|
 | 646 |  * @param action the String input holding the Action. | 
|---|
 | 647 |  * @return The Action if known, WA_NONE otherwise. | 
|---|
 | 648 |  */ | 
|---|
| [7221] | 649 | WeaponAction Weapon::charToAction(const std::string& action) | 
|---|
| [4885] | 650 | { | 
|---|
| [7221] | 651 |   if (action == "none") | 
|---|
| [4885] | 652 |     return WA_NONE; | 
|---|
| [7221] | 653 |   else if (action == "shoot") | 
|---|
| [4885] | 654 |     return WA_SHOOT; | 
|---|
| [7221] | 655 |   else if (action == "charge") | 
|---|
| [4885] | 656 |     return WA_CHARGE; | 
|---|
| [7221] | 657 |   else if (action == "reload") | 
|---|
| [4885] | 658 |     return WA_RELOAD; | 
|---|
| [7221] | 659 |   else if (action == "acitvate") | 
|---|
| [4885] | 660 |     return WA_ACTIVATE; | 
|---|
| [7221] | 661 |   else if (action == "deactivate") | 
|---|
| [4885] | 662 |     return WA_DEACTIVATE; | 
|---|
| [7221] | 663 |   else if (action == "special1") | 
|---|
| [4885] | 664 |     return WA_SPECIAL1; | 
|---|
 | 665 |   else | 
|---|
| [6438] | 666 |   { | 
|---|
| [7221] | 667 |     PRINTF(2)("action %s could not be identified.\n", action.c_str()); | 
|---|
| [6438] | 668 |     return WA_NONE; | 
|---|
 | 669 |   } | 
|---|
| [4885] | 670 | } | 
|---|
| [3575] | 671 |  | 
|---|
 | 672 | /** | 
|---|
| [4885] | 673 |  * converts an action into a String | 
|---|
 | 674 |  * @param action the action to convert | 
|---|
 | 675 |  * @return a String matching the name of the action | 
|---|
 | 676 |  */ | 
|---|
 | 677 | const char* Weapon::actionToChar(WeaponAction action) | 
|---|
 | 678 | { | 
|---|
 | 679 |   switch (action) | 
|---|
 | 680 |   { | 
|---|
| [7350] | 681 |     case WA_SHOOT: | 
|---|
 | 682 |       return "shoot"; | 
|---|
 | 683 |       break; | 
|---|
 | 684 |     case WA_CHARGE: | 
|---|
 | 685 |       return "charge"; | 
|---|
 | 686 |       break; | 
|---|
 | 687 |     case WA_RELOAD: | 
|---|
 | 688 |       return "reload"; | 
|---|
 | 689 |       break; | 
|---|
 | 690 |     case WA_ACTIVATE: | 
|---|
 | 691 |       return "activate"; | 
|---|
 | 692 |       break; | 
|---|
 | 693 |     case WA_DEACTIVATE: | 
|---|
 | 694 |       return "deactivate"; | 
|---|
 | 695 |       break; | 
|---|
 | 696 |     case WA_SPECIAL1: | 
|---|
 | 697 |       return "special1"; | 
|---|
 | 698 |       break; | 
|---|
 | 699 |     default: | 
|---|
 | 700 |       return "none"; | 
|---|
 | 701 |       break; | 
|---|
| [4885] | 702 |   } | 
|---|
 | 703 | } | 
|---|
| [3577] | 704 |  | 
|---|
 | 705 | /** | 
|---|
| [4885] | 706 |  * Converts a String into a State. | 
|---|
 | 707 |  * @param state the String input holding the State. | 
|---|
 | 708 |  * @return The State if known, WS_NONE otherwise. | 
|---|
 | 709 |  */ | 
|---|
| [7221] | 710 | WeaponState Weapon::charToState(const std::string& state) | 
|---|
| [4885] | 711 | { | 
|---|
| [7221] | 712 |   if (state == "none") | 
|---|
| [4885] | 713 |     return WS_NONE; | 
|---|
| [7221] | 714 |   else if (state == "shooting") | 
|---|
| [4885] | 715 |     return WS_SHOOTING; | 
|---|
| [7221] | 716 |   else if (state == "charging") | 
|---|
| [4885] | 717 |     return WS_CHARGING; | 
|---|
| [7221] | 718 |   else if (state == "reloading") | 
|---|
| [4885] | 719 |     return WS_RELOADING; | 
|---|
| [7221] | 720 |   else if (state == "activating") | 
|---|
| [4885] | 721 |     return WS_ACTIVATING; | 
|---|
| [7221] | 722 |   else if (state == "deactivating") | 
|---|
| [4885] | 723 |     return WS_DEACTIVATING; | 
|---|
| [7221] | 724 |   else if (state == "inactive") | 
|---|
| [4885] | 725 |     return WS_INACTIVE; | 
|---|
| [7221] | 726 |   else if (state == "idle") | 
|---|
| [4885] | 727 |     return WS_IDLE; | 
|---|
 | 728 |   else | 
|---|
| [6438] | 729 |   { | 
|---|
| [7221] | 730 |     PRINTF(2)("state %s could not be identified.\n", state.c_str()); | 
|---|
| [6438] | 731 |     return WS_NONE; | 
|---|
 | 732 |   } | 
|---|
| [4885] | 733 | } | 
|---|
| [3583] | 734 |  | 
|---|
 | 735 | /** | 
|---|
| [4885] | 736 |  * converts a State into a String | 
|---|
 | 737 |  * @param state the state to convert | 
|---|
 | 738 |  * @return a String matching the name of the state | 
|---|
 | 739 |  */ | 
|---|
 | 740 | const char* Weapon::stateToChar(WeaponState state) | 
|---|
 | 741 | { | 
|---|
 | 742 |   switch (state) | 
|---|
 | 743 |   { | 
|---|
| [7350] | 744 |     case WS_SHOOTING: | 
|---|
 | 745 |       return "shooting"; | 
|---|
 | 746 |       break; | 
|---|
 | 747 |     case WS_CHARGING: | 
|---|
 | 748 |       return "charging"; | 
|---|
 | 749 |       break; | 
|---|
 | 750 |     case WS_RELOADING: | 
|---|
 | 751 |       return "reloading"; | 
|---|
 | 752 |       break; | 
|---|
 | 753 |     case WS_ACTIVATING: | 
|---|
 | 754 |       return "activating"; | 
|---|
 | 755 |       break; | 
|---|
 | 756 |     case WS_DEACTIVATING: | 
|---|
 | 757 |       return "deactivating"; | 
|---|
 | 758 |       break; | 
|---|
 | 759 |     case WS_IDLE: | 
|---|
 | 760 |       return "idle"; | 
|---|
 | 761 |       break; | 
|---|
 | 762 |     case WS_INACTIVE: | 
|---|
 | 763 |       return "inactive"; | 
|---|
 | 764 |       break; | 
|---|
 | 765 |     default: | 
|---|
 | 766 |       return "none"; | 
|---|
 | 767 |       break; | 
|---|
| [4885] | 768 |   } | 
|---|
 | 769 | } | 
|---|