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