Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/orxonox/objects/weapon/AmmunitionDump.cc @ 1039

Last change on this file since 1039 was 1039, checked in by rgrieder, 16 years ago
  • train riding doesn't have to be boring
  • added some license notes
  • removed certain header dependencies in audio
  • changed order of header file inclusion in orxonox and audio (coding style guide will be updated)
File size: 3.2 KB
Line 
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
28#include "OrxonoxStableHeaders.h"
29#include "AmmunitionDump.h"
30
31#include "orxonox/Orxonox.h"
32#include "BulletManager.h"
33#include "util/tinyxml/tinyxml.h"
34#include "core/CoreIncludes.h"
35
36
37namespace 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    registerAllVariables();
47
48    for (int i = 0; i < numberOfAmmos_; i++)
49    {
50      stock_[i] = 0;
51      capacity_[i] = 0;
52    }
53  }
54
55
56  AmmunitionDump::~AmmunitionDump()
57  {
58    if (stock_)
59      delete stock_;
60    if (capacity_)
61      delete capacity_;
62  }
63
64  void AmmunitionDump::setDumpSize(const std::string &name, int size)
65  {
66    if (size < 0)
67      return;
68    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
69    if (id == -1)
70      return;
71    capacity_[id] = size;
72  }
73
74
75  int AmmunitionDump::store(const std::string &name, int quantity)
76  {
77    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
78    if (id == -1)
79      return quantity;
80    stock_[id] += quantity;
81    if (stock_[id] > capacity_[id])
82    {
83      quantity = capacity_[id] - stock_[id];
84      stock_[id] = capacity_[id];
85      return quantity;
86    }
87    else
88      return 0;
89  }
90
91
92  int AmmunitionDump::getAmmunition(const std::string &name, int quantity)
93  {
94    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
95    if (id == -1)
96      return 0;
97    if (stock_[id] >= quantity)
98      stock_[id] -= quantity;
99    else
100    {
101      quantity = stock_[id];
102      stock_[id] = 0;
103    }
104    return quantity;
105  }
106
107
108  int AmmunitionDump::getStockSize(const std::string &name)
109  {
110    int id = Orxonox::getSingleton()->getBulletMgr()->getAmmunitionID(name);
111    // FIXME changed = to ==, check if correct
112    if (id == -1)
113      return -1;
114    return stock_[id];
115  }
116 
117  void AmmunitionDump::registerAllVariables(){
118    registerVar( &numberOfAmmos_, sizeof(int), network::DATA);
119   
120    for (int i = 0; i < numberOfAmmos_; i++)
121    {
122      registerVar(&stock_[i], sizeof(int), network::DATA);
123      registerVar(&capacity_[i], sizeof(int), network::DATA);
124    }
125  }
126}
Note: See TracBrowser for help on using the repository browser.