Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/script/src/orxonox/objects/weapon/AmmunitionDump.cc @ 986

Last change on this file since 986 was 927, checked in by scheusso, 17 years ago

Made various changes to Classes WorldEntity, Model, SpaceShip,
BulletManager and AmmunitionDump in order to make them really
synchronisable.
I hope, that everythings still working now. I only did some small tests
(ie no segfault when running/starting and acting)

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
30#include "orxonox/Orxonox.h"
31#include "BulletManager.h"
32#include "util/tinyxml/tinyxml.h"
33#include "core/CoreIncludes.h"
34
35#include "AmmunitionDump.h"
36
37
38namespace 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}
Note: See TracBrowser for help on using the repository browser.