1 | /* |
---|
2 | * ORXONOX - the hottest 3D action shooter ever to exist |
---|
3 | * > www.orxonox.net < |
---|
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 | |
---|
29 | #include "OrxonoxStableHeaders.h" |
---|
30 | #include "AmmunitionDump.h" |
---|
31 | |
---|
32 | #include "orxonox/Orxonox.h" |
---|
33 | #include "BulletManager.h" |
---|
34 | #include "util/tinyxml/tinyxml.h" |
---|
35 | #include "core/CoreIncludes.h" |
---|
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); |
---|
47 | registerAllVariables(); |
---|
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 | |
---|
65 | void AmmunitionDump::setDumpSize(const std::string &name, int size) |
---|
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 | |
---|
76 | int AmmunitionDump::store(const std::string &name, int quantity) |
---|
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 | |
---|
93 | int AmmunitionDump::getAmmunition(const std::string &name, int quantity) |
---|
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 | |
---|
109 | int AmmunitionDump::getStockSize(const std::string &name) |
---|
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 | } |
---|
117 | |
---|
118 | void AmmunitionDump::registerAllVariables(){ |
---|
119 | registerVar( &numberOfAmmos_, sizeof(int), network::DATA); |
---|
120 | |
---|
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 | } |
---|
127 | } |
---|