| [668] | 1 | /* | 
|---|
 | 2 |  *   ORXONOX - the hottest 3D action shooter ever to exist | 
|---|
 | 3 |  * | 
|---|
 | 4 |  * | 
|---|
 | 5 |  *   License notice: | 
|---|
 | 6 |  * | 
|---|
 | 7 |  *   This program is free software; you can redistribute it and/or | 
|---|
 | 8 |  *   modify it under the terms of the GNU General Public License | 
|---|
 | 9 |  *   as published by the Free Software Foundation; either version 2 | 
|---|
 | 10 |  *   of the License, or (at your option) any later version. | 
|---|
 | 11 |  * | 
|---|
 | 12 |  *   This program is distributed in the hope that it will be useful, | 
|---|
 | 13 |  *   but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
 | 14 |  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
 | 15 |  *   GNU General Public License for more details. | 
|---|
 | 16 |  * | 
|---|
 | 17 |  *   You should have received a copy of the GNU General Public License | 
|---|
 | 18 |  *   along with this program; if not, write to the Free Software | 
|---|
 | 19 |  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA. | 
|---|
 | 20 |  * | 
|---|
 | 21 |  *   Author: | 
|---|
 | 22 |  *      Reto Grieder | 
|---|
 | 23 |  *   Co-authors: | 
|---|
 | 24 |  *      ... | 
|---|
 | 25 |  * | 
|---|
 | 26 |  */ | 
|---|
 | 27 |  | 
|---|
| [786] | 28 | #include "OrxonoxStableHeaders.h" | 
|---|
| [784] | 29 |  | 
|---|
| [668] | 30 | #include "orxonox/Orxonox.h" | 
|---|
 | 31 | #include "BulletManager.h" | 
|---|
| [742] | 32 | #include "util/tinyxml/tinyxml.h" | 
|---|
| [668] | 33 |  | 
|---|
 | 34 | #include "AmmunitionDump.h" | 
|---|
 | 35 |  | 
|---|
 | 36 |  | 
|---|
 | 37 | namespace orxonox { | 
|---|
 | 38 |   CreateFactory(AmmunitionDump); | 
|---|
 | 39 |  | 
|---|
 | 40 |   AmmunitionDump::AmmunitionDump() | 
|---|
 | 41 |     : numberOfAmmos_(Orxonox::getSingleton()->getBulletMgr()->getNumberOfAmmos()), | 
|---|
 | 42 |       stock_(new int[numberOfAmmos_]), | 
|---|
 | 43 |       capacity_(new int[numberOfAmmos_]) | 
|---|
 | 44 |   { | 
|---|
 | 45 |     RegisterObject(AmmunitionDump); | 
|---|
 | 46 |  | 
|---|
 | 47 |     for (int i = 0; i < numberOfAmmos_; i++) | 
|---|
 | 48 |     { | 
|---|
 | 49 |       stock_[i] = 0; | 
|---|
 | 50 |       capacity_[i] = 0; | 
|---|
 | 51 |     } | 
|---|
 | 52 |   } | 
|---|
 | 53 |  | 
|---|
 | 54 |  | 
|---|
 | 55 |   AmmunitionDump::~AmmunitionDump() | 
|---|
 | 56 |   { | 
|---|
 | 57 |     if (stock_) | 
|---|
 | 58 |       delete stock_; | 
|---|
 | 59 |     if (capacity_) | 
|---|
 | 60 |       delete capacity_; | 
|---|
 | 61 |   } | 
|---|
 | 62 |  | 
|---|
| [715] | 63 |   void AmmunitionDump::setDumpSize(const std::string &name, int size) | 
|---|
| [668] | 64 |   { | 
|---|
 | 65 |     if (size < 0) | 
|---|
 | 66 |       return; | 
|---|
 | 67 |     int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name); | 
|---|
 | 68 |     if (id == -1) | 
|---|
 | 69 |       return; | 
|---|
 | 70 |     capacity_[id] = size; | 
|---|
 | 71 |   } | 
|---|
 | 72 |  | 
|---|
 | 73 |  | 
|---|
| [715] | 74 |   int AmmunitionDump::store(const std::string &name, int quantity) | 
|---|
| [668] | 75 |   { | 
|---|
 | 76 |     int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name); | 
|---|
 | 77 |     if (id == -1) | 
|---|
 | 78 |       return quantity; | 
|---|
 | 79 |     stock_[id] += quantity; | 
|---|
 | 80 |     if (stock_[id] > capacity_[id]) | 
|---|
 | 81 |     { | 
|---|
 | 82 |       quantity = capacity_[id] - stock_[id]; | 
|---|
 | 83 |       stock_[id] = capacity_[id]; | 
|---|
 | 84 |       return quantity; | 
|---|
 | 85 |     } | 
|---|
 | 86 |     else | 
|---|
 | 87 |       return 0; | 
|---|
 | 88 |   } | 
|---|
 | 89 |  | 
|---|
 | 90 |  | 
|---|
| [715] | 91 |   int AmmunitionDump::getAmmunition(const std::string &name, int quantity) | 
|---|
| [668] | 92 |   { | 
|---|
 | 93 |     int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name); | 
|---|
 | 94 |     if (id == -1) | 
|---|
 | 95 |       return 0; | 
|---|
 | 96 |     if (stock_[id] >= quantity) | 
|---|
 | 97 |       stock_[id] -= quantity; | 
|---|
 | 98 |     else | 
|---|
 | 99 |     { | 
|---|
 | 100 |       quantity = stock_[id]; | 
|---|
 | 101 |       stock_[id] = 0; | 
|---|
 | 102 |     } | 
|---|
 | 103 |     return quantity; | 
|---|
 | 104 |   } | 
|---|
 | 105 |  | 
|---|
 | 106 |  | 
|---|
| [715] | 107 |   int AmmunitionDump::getStockSize(const std::string &name) | 
|---|
| [668] | 108 |   { | 
|---|
 | 109 |     int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name); | 
|---|
 | 110 |     // FIXME changed = to ==, check if correct | 
|---|
 | 111 |     if (id == -1) | 
|---|
 | 112 |       return -1; | 
|---|
 | 113 |     return stock_[id]; | 
|---|
 | 114 |   } | 
|---|
 | 115 | } | 
|---|