Changeset 10878 for code/branches/fabienHS15/src/orxonox/weaponsystem
- Timestamp:
- Nov 27, 2015, 6:49:54 PM (9 years ago)
- Location:
- code/branches/fabienHS15/src/orxonox/weaponsystem
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
code/branches/fabienHS15/src/orxonox/weaponsystem/Munition.cc
r10791 r10878 44 44 this->maxMunitionPerMagazine_ = 10; 45 45 this->maxMagazines_ = 10; 46 this-> magazines_ = 10;46 this->unassignedMagazines_ = 10; 47 47 48 48 this->deployment_ = MunitionDeployment::Stack; … … 57 57 Munition::~Munition() 58 58 { 59 for (std::map<WeaponMode*, Magazine*>::iterator it = this-> currentMagazines_.begin(); it != this->currentMagazines_.end(); ++it)59 for (std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.begin(); it != this->assignedMagazines_.end(); ++it) 60 60 delete it->second; 61 61 } … … 79 79 80 80 // Use the map to get the magazine assigned to the given user 81 std::map<WeaponMode*, Magazine*>::const_iterator it = this-> currentMagazines_.find(user);82 if (it != this-> currentMagazines_.end())81 std::map<WeaponMode*, Magazine*>::const_iterator it = this->assignedMagazines_.find(user); 82 if (it != this->assignedMagazines_.end()) 83 83 return it->second; 84 84 } … … 86 86 { 87 87 // We don't use separate magazines for each user, so just take the first magazine 88 if (this-> currentMagazines_.size() > 0)89 return this-> currentMagazines_.begin()->second;88 if (this->assignedMagazines_.size() > 0) 89 return this->assignedMagazines_.begin()->second; 90 90 } 91 91 … … 100 100 if (deployment_ == MunitionDeployment::Stack) 101 101 // With stacked munition every magazine contributes to the total amount 102 return this->maxMunitionPerMagazine_ * this-> magazines_ + magazine->munition_;102 return this->maxMunitionPerMagazine_ * this->unassignedMagazines_ + magazine->munition_; 103 103 else 104 104 // Wihtout stacked munition we just consider the current magazine … … 125 125 Magazine* magazine = this->getMagazine(0); 126 126 if (magazine && magazine->munition_ == this->maxMunitionPerMagazine_) 127 return this-> magazines_ + 1;128 } 129 130 return this-> magazines_;127 return this->unassignedMagazines_ + 1; 128 } 129 130 return this->unassignedMagazines_; 131 131 } 132 132 133 133 void Munition::setNumMagazines(unsigned int numMagazines) 134 134 { 135 this-> magazines_ = numMagazines;135 this->unassignedMagazines_ = numMagazines; 136 136 } 137 137 … … 154 154 if (deployment_ == MunitionDeployment::Stack) 155 155 { 156 munition += this->maxMunitionPerMagazine_ * this-> magazines_;156 munition += this->maxMunitionPerMagazine_ * this->unassignedMagazines_; 157 157 } 158 158 … … 200 200 magazine->munition_ = 0; 201 201 202 if (this->reload( 0))202 if (this->reload(NULL)) 203 203 // Successfully reloaded, continue recursively 204 204 return this->takeMunition(amount, 0); … … 224 224 { 225 225 // As long as we have enough magazines (and don't stack munition) we can reload 226 return (this-> magazines_ > 0 && deployment_ != MunitionDeployment::Stack);226 return (this->unassignedMagazines_ > 0 && deployment_ != MunitionDeployment::Stack); 227 227 } 228 228 … … 252 252 253 253 // Check if we actually can reload 254 if (this-> magazines_ == 0)254 if (this->unassignedMagazines_ == 0) 255 255 return false; 256 256 … … 266 266 267 267 // Remove the current magazine for the given user 268 std::map<WeaponMode*, Magazine*>::iterator it = this-> currentMagazines_.find(user);269 if (it != this-> currentMagazines_.end())268 std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.find(user); 269 if (it != this->assignedMagazines_.end()) 270 270 { 271 271 if (it->first == lastFilledWeaponMode_) … … 274 274 } 275 275 delete it->second; 276 this-> currentMagazines_.erase(it);276 this->assignedMagazines_.erase(it); 277 277 } 278 278 279 279 // Load a new magazine 280 this-> currentMagazines_[user] = new Magazine(this, bUseReloadTime);281 this-> magazines_--;280 this->assignedMagazines_[user] = new Magazine(this, bUseReloadTime); 281 this->unassignedMagazines_--; 282 282 283 283 return true; … … 299 299 { 300 300 // Return true if any of the current magazines is not full (loading counts as full although it returns 0 munition) 301 for (std::map<WeaponMode*, Magazine*>::const_iterator it = this-> currentMagazines_.begin(); it != this->currentMagazines_.end(); ++it)301 for (std::map<WeaponMode*, Magazine*>::const_iterator it = this->assignedMagazines_.begin(); it != this->assignedMagazines_.end(); ++it) 302 302 if (it->second->munition_ < this->maxMunitionPerMagazine_ && it->second->bLoaded_) 303 303 return true; … … 325 325 { 326 326 magazine->munition_ -= this->maxMunitionPerMagazine_; 327 this-> magazines_++;327 this->unassignedMagazines_++; 328 328 } 329 329 330 330 // If we reached the limit, reduce both magazines and munition to the maximum 331 if (this-> magazines_ >= this->maxMagazines_)332 { 333 this-> magazines_ = this->maxMagazines_;331 if (this->unassignedMagazines_ >= this->maxMagazines_) 332 { 333 this->unassignedMagazines_ = this->maxMagazines_; 334 334 magazine->munition_ = this->maxMunitionPerMagazine_; 335 335 } … … 349 349 if (lastFilledWeaponMode_ == NULL) 350 350 { 351 it = this-> currentMagazines_.begin();351 it = this->assignedMagazines_.begin(); 352 352 } 353 353 else 354 354 { 355 it = this-> currentMagazines_.find(lastFilledWeaponMode_);355 it = this->assignedMagazines_.find(lastFilledWeaponMode_); 356 356 ++ it; 357 357 } … … 362 362 { 363 363 bool change = false; 364 while (it != this-> currentMagazines_.end())364 while (it != this->assignedMagazines_.end()) 365 365 { 366 366 // Add munition if the magazine isn't full (but only to loaded magazines) … … 383 383 } 384 384 385 it = this-> currentMagazines_.begin();385 it = this->assignedMagazines_.begin(); 386 386 firstLoop = false; 387 387 } … … 400 400 else 401 401 // If we don't stack munition, we're more limited 402 return ((this->currentMagazines_.size() + this->magazines_) < this->maxMagazines_); 403 } 404 405 bool Munition::addMagazines(unsigned int amount) 406 { 402 return ((this->assignedMagazines_.size() + this->unassignedMagazines_) < this->maxMagazines_); 403 } 404 405 unsigned int Munition::addMagazines(unsigned int amount) 406 { 407 unsigned int addedMagazines = 0; 408 407 409 if (!this->canAddMagazines(amount)) 408 return false;410 return 0; 409 411 410 412 // Calculate how many magazines are needed 411 int needed_magazines = this->maxMagazines_ - this-> magazines_ - this->currentMagazines_.size();413 int needed_magazines = this->maxMagazines_ - this->unassignedMagazines_ - this->assignedMagazines_.size(); 412 414 413 415 // If zero or less magazines are needed, we definitively don't need more magazines (unless we stack munition - then a magazine contributes directly to the munition) 414 416 if (needed_magazines <= 0 && deployment_ != MunitionDeployment::Stack) 415 return false;417 return 0; 416 418 417 419 if (amount <= static_cast<unsigned int>(needed_magazines)) 418 420 { 419 421 // We need more magazines than we get, so just add them 420 this->magazines_ += amount; 422 this->unassignedMagazines_ += amount; 423 addedMagazines = amount; 421 424 } 422 425 else 423 426 { 424 427 // We get more magazines than we need, so just add the needed amount 425 this->magazines_ += needed_magazines; 428 this->unassignedMagazines_ += needed_magazines; 429 addedMagazines = needed_magazines; 426 430 if (deployment_ == MunitionDeployment::Stack) 427 431 { … … 433 437 } 434 438 435 return true; 439 // Reload as many empty magazines as possible 440 // Double loop and break is needed because the reload function changes the assigned magazines. This may confuse the iterator. 441 for (unsigned int i = 0; i < addedMagazines; ++i) 442 { 443 for (std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.begin(); it != this->assignedMagazines_.end(); ++it) 444 { 445 if (needReload(it->first)) 446 { 447 reload(it->first); 448 break; 449 } 450 } 451 } 452 453 return addedMagazines; 436 454 } 437 455 … … 440 458 if (deployment_ == MunitionDeployment::Stack) 441 459 { 442 if (this-> magazines_ >= amount)460 if (this->unassignedMagazines_ >= amount) 443 461 { 444 462 // We have enough magazines 445 463 return true; 446 464 } 447 else if (this-> magazines_ == amount - 1)465 else if (this->unassignedMagazines_ == amount - 1) 448 466 { 449 467 // We lack one magazine, check if the current magazine is still full, if yes we're fine … … 461 479 { 462 480 // In case we're not stacking munition, just check the number of magazines 463 return (this-> magazines_ >= amount);481 return (this->unassignedMagazines_ >= amount); 464 482 } 465 483 … … 472 490 return false; 473 491 474 if (this-> magazines_ >= amount)492 if (this->unassignedMagazines_ >= amount) 475 493 { 476 494 // We have enough magazines, just remove the amount 477 this-> magazines_ -= amount;495 this->unassignedMagazines_ -= amount; 478 496 } 479 497 else if (deployment_ == MunitionDeployment::Stack) 480 498 { 481 499 // We don't have enough magazines, but we're stacking munition, so additionally remove the bullets from the current magazine 482 this-> magazines_ = 0;500 this->unassignedMagazines_ = 0; 483 501 Magazine* magazine = this->getMagazine(0); 484 502 if (magazine) … … 500 518 501 519 // Remove the current magazine for the given user 502 std::map<WeaponMode*, Magazine*>::iterator it = this-> currentMagazines_.find(user);503 if (it != this-> currentMagazines_.end())520 std::map<WeaponMode*, Magazine*>::iterator it = this->assignedMagazines_.find(user); 521 if (it != this->assignedMagazines_.end()) 504 522 { 505 523 if (it->first == lastFilledWeaponMode_) … … 508 526 } 509 527 delete it->second; 510 this-> currentMagazines_.erase(it);528 this->assignedMagazines_.erase(it); 511 529 return true; 512 530 } -
code/branches/fabienHS15/src/orxonox/weaponsystem/Munition.h
r10794 r10878 97 97 98 98 bool canAddMagazines(unsigned int amount) const; 99 bool addMagazines(unsigned int amount); 99 /** 100 @brief Try to add magazines. 101 @param amount The amount of magazines tried to add. 102 @return The amount of magazines sucessfully added. 103 */ 104 unsigned int addMagazines(unsigned int amount); 100 105 101 106 bool canRemoveMagazines(unsigned int amount) const; … … 107 112 unsigned int maxMunitionPerMagazine_; 108 113 unsigned int maxMagazines_; 109 unsigned int magazines_;110 std::map<WeaponMode*, Magazine*> currentMagazines_; // Maps weapon modes to magazines that are currently used.114 unsigned int unassignedMagazines_; // Number of magazines that are not assigned to a weapon mode. These are alway treated as full. 115 std::map<WeaponMode*, Magazine*> assignedMagazines_; // Maps weapon modes to magazines that are currently used. 111 116 112 117 MunitionDeployment::Value deployment_; // Defines the behaviour how munition and magazines are distributed to the consuming weapon modes. … … 115 120 bool bAllowMultiMunitionRemovementUnderflow_; 116 121 117 float reloadTime_; 122 float reloadTime_; // The time needed to replace a magazine by a new one. 118 123 WeaponMode* lastFilledWeaponMode_; // Pointer to the weapon mode that got the last munition during the last call of addMunition. 119 124 -
code/branches/fabienHS15/src/orxonox/weaponsystem/ReplenishingMunition.cc
r10791 r10878 76 76 void ReplenishingMunition::replenish() 77 77 { 78 if (this->canAddM unition(this->replenishAmount_))78 if (this->canAddMagazines(this->replenishAmount_)) 79 79 { 80 80 // Make a temporary copy of bAllowMunitionRefilling_, because this might be disallowed in every … … 84 84 85 85 // Replenish munition 86 this->addM unition(this->replenishAmount_);86 this->addMagazines(this->replenishAmount_); 87 87 88 88 // Write back the temporary value -
code/branches/fabienHS15/src/orxonox/weaponsystem/ReplenishingMunition.h
r10791 r10878 64 64 protected: 65 65 float replenishInterval_; //!< The interval in which the munition is replenished. 66 unsigned int replenishAmount_; //!< The amount by which it is replenished.66 unsigned int replenishAmount_; //!< The amount of magazines by which it is replenished. 67 67 68 68 private: 69 void replenish(); 69 void replenish(); //!< Every call of this function replenishes the munition. This function is called by the timer. 70 70 void initializeTimer(); 71 71
Note: See TracChangeset
for help on using the changeset viewer.