Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/weapons/src/orxonox/objects/weaponSystem/WeaponSystem.cc @ 2918

Last change on this file since 2918 was 2918, checked in by landauf, 15 years ago

Added many new features in the Munition class:

  • there are now 3 modes: a) every weapon has it's own magazine b) all weapons use the same magazin c) no magazines, just a big munition pool
  • the Munition class handles the reloading of the magazine

Split the Weapon class into Weapon and WeaponMode. WeaponMode creates the fire of the Weapon. A weapon can own several WeaponModes (for example primary and secondary fire). But it's also possible to have a weapon with several muzzles which all fire at the same time (there's a WeaponMode for each muzzle).

Renamed LaserGun to LaserFire and Fusion to FusionFire. They inherit now from WeaponMode.

Changed the code in the Weapon class to use the new Munition functionality.

Added ReplenishingMunition, a subclass of Munition that replenishes itself (used for LaserGunMunition).

Added a reload command to reload magazines.

  • Property svn:eol-style set to native
  • Property svn:mergeinfo set to (toggle deleted branches)
    /code/branches/gui/src/orxonox/objects/weaponSystem/WeaponSystem.ccmergedeligible
    /code/branches/lodfinal/src/orxonox/objects/weaponSystem/WeaponSystem.ccmergedeligible
    /code/branches/pickups2/src/orxonox/objects/weaponSystem/WeaponSystem.ccmergedeligible
    /code/branches/weaponsystem/src/orxonox/objects/weaponSystem/WeaponSystem.ccmergedeligible
    /code/branches/buildsystem2/src/orxonox/objects/weaponSystem/WeaponSystem.cc2506-2658
    /code/branches/buildsystem3/src/orxonox/objects/weaponSystem/WeaponSystem.cc2662-2708
    /code/branches/ceguilua/src/orxonox/objects/WeaponSystem.cc1802-1808
    /code/branches/core3/src/orxonox/objects/WeaponSystem.cc1572-1739
    /code/branches/gcc43/src/orxonox/objects/WeaponSystem.cc1580
    /code/branches/gui/src/orxonox/objects/WeaponSystem.cc1635-1723
    /code/branches/input/src/orxonox/objects/WeaponSystem.cc1629-1636
    /code/branches/miniprojects/src/orxonox/objects/weaponSystem/WeaponSystem.cc2754-2824
    /code/branches/network/src/orxonox/objects/weaponSystem/WeaponSystem.cc2356
    /code/branches/network64/src/orxonox/objects/weaponSystem/WeaponSystem.cc2210-2355
    /code/branches/objecthierarchy/src/orxonox/objects/WeaponSystem.cc1911-2085,​2100
    /code/branches/objecthierarchy2/src/orxonox/objects/weaponSystem/WeaponSystem.cc2171-2479
    /code/branches/overlay/src/orxonox/objects/weaponSystem/WeaponSystem.cc2117-2385
    /code/branches/physics/src/orxonox/objects/weaponSystem/WeaponSystem.cc2107-2439
    /code/branches/physics_merge/src/orxonox/objects/weaponSystem/WeaponSystem.cc2436-2457
    /code/branches/pickups/src/orxonox/objects/WeaponSystem.cc1926-2086
    /code/branches/presentation/src/orxonox/objects/weaponSystem/WeaponSystem.cc2369-2652,​2654-2660
    /code/branches/questsystem/src/orxonox/objects/WeaponSystem.cc1894-2088
    /code/branches/questsystem2/src/orxonox/objects/weaponSystem/WeaponSystem.cc2107-2259
    /code/branches/script_trigger/src/orxonox/objects/WeaponSystem.cc1295-1953,​1955
    /code/branches/weapon/src/orxonox/objects/WeaponSystem.cc1925-2047
    /code/branches/weapon2/src/orxonox/objects/weaponSystem/WeaponSystem.cc2107-2488
File size: 9.5 KB
Line 
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 *      Martin Polak
24 *   Co-authors:
25 *      ...
26 *
27 */
28
29#include "OrxonoxStableHeaders.h"
30#include "WeaponSystem.h"
31
32#include "core/CoreIncludes.h"
33#include "objects/worldentities/pawns/Pawn.h"
34
35#include "WeaponSlot.h"
36#include "WeaponPack.h"
37#include "WeaponSet.h"
38#include "Weapon.h"
39#include "Munition.h"
40
41/* WeaponSystem
42 *
43 *  www.orxonox.net/wiki/WeaponSystem
44 */
45
46namespace orxonox
47{
48    CreateFactory(WeaponSystem);
49
50    WeaponSystem::WeaponSystem(BaseObject* creator) : BaseObject(creator)
51    {
52        RegisterObject(WeaponSystem);
53
54        this->pawn_ = 0;
55COUT(0) << "+WeaponSystem" << std::endl;
56    }
57
58    WeaponSystem::~WeaponSystem()
59    {
60COUT(0) << "~WeaponSystem" << std::endl;
61        if (this->isInitialized())
62        {
63            if (this->pawn_)
64                this->pawn_->setWeaponSystem(0);
65
66            while (!this->weaponSets_.empty())
67                delete (this->weaponSets_.begin()->second);
68
69            while (!this->weaponPacks_.empty())
70                delete (*this->weaponPacks_.begin());
71
72            while (!this->weaponSlots_.empty())
73                delete (*this->weaponSlots_.begin());
74
75            while (!this->munitions_.empty())
76                { delete (this->munitions_.begin()->second); this->munitions_.erase(this->munitions_.begin()); }
77        }
78    }
79
80    void WeaponSystem::addWeaponSlot(WeaponSlot * wSlot)
81    {
82        if (!wSlot)
83            return;
84
85        this->weaponSlots_.push_back(wSlot);
86        wSlot->setWeaponSystem(this);
87    }
88
89    void WeaponSystem::removeWeaponSlot(WeaponSlot * wSlot)
90    {
91        if (!wSlot)
92            return;
93
94        if (wSlot->getWeapon())
95            this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack());
96
97        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
98        {
99            if ((*it) == wSlot)
100            {
101                this->weaponSlots_.erase(it);
102                break;
103            }
104        }
105    }
106
107    WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
108    {
109        unsigned int i = 0;
110        for (std::vector<WeaponSlot*>::const_iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
111        {
112            ++i;
113            if (i > index)
114                return (*it);
115        }
116        return 0;
117    }
118
119    bool WeaponSystem::addWeaponSet(WeaponSet * wSet)
120    {
121        if (wSet)
122            return this->addWeaponSet(wSet, wSet->getDesiredFiremode());
123        else
124            return false;
125    }
126
127    bool WeaponSystem::addWeaponSet(WeaponSet * wSet, unsigned int firemode)
128    {
129        if (!wSet || firemode >= WeaponSystem::MAX_FIRE_MODES)
130            return false;
131
132        std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.find(firemode);
133        if (it == this->weaponSets_.end())
134        {
135            this->weaponSets_[firemode] = wSet;
136            wSet->setWeaponSystem(this);
137            return true;
138        }
139
140        return false;
141    }
142
143    void WeaponSystem::removeWeaponSet(WeaponSet * wSet)
144    {
145        for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
146        {
147            if (it->second == wSet)
148                this->weaponSets_.erase(it++);
149            else
150                ++it;
151        }
152    }
153
154    WeaponSet * WeaponSystem::getWeaponSet(unsigned int index) const
155    {
156        unsigned int i = 0;
157        for (std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
158        {
159            ++i;
160            if (i > index)
161                return it->second;
162        }
163        return 0;
164    }
165
166    bool WeaponSystem::canAddWeaponPack(WeaponPack * wPack)
167    {
168        if (!wPack)
169            return false;
170
171        unsigned int freeSlots = 0;
172        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
173        {
174            if (!(*it)->isOccupied())
175                ++freeSlots;
176        }
177
178        return (freeSlots >= wPack->getNumWeapons());
179    }
180
181    bool WeaponSystem::addWeaponPack(WeaponPack * wPack)
182    {
183        if (!this->canAddWeaponPack(wPack))
184            return false;
185
186        // Attach all weapons to the first free slots (and to the Pawn)
187        unsigned int i = 0;
188        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
189        {
190            if (!(*it)->isOccupied() && i < wPack->getNumWeapons())
191            {
192                Weapon* weapon = wPack->getWeapon(i);
193                (*it)->attachWeapon(weapon);
194                this->getPawn()->attach(weapon);
195                ++i;
196            }
197        }
198
199        // Assign the desired weaponmode to the firemodes
200        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
201        {
202            unsigned int weaponmode = wPack->getDesiredWeaponmode(it->first);
203            if (weaponmode != WeaponSystem::WEAPON_MODE_UNASSIGNED)
204                it->second->setWeaponmodeLink(wPack, weaponmode);
205        }
206
207        this->weaponPacks_.insert(wPack);
208        wPack->setWeaponSystem(this);
209
210        return true;
211    }
212
213    void WeaponSystem::removeWeaponPack(WeaponPack * wPack)
214    {
215        // Remove all weapons from their WeaponSlot
216        unsigned int i = 0;
217        Weapon* weapon = 0;
218        while (weapon = wPack->getWeapon(i++))
219            weapon->getWeaponSlot()->removeWeapon();
220
221        // Remove all added links from the WeaponSets
222        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
223            it->second->removeWeaponmodeLink(wPack);
224
225        // Remove the WeaponPack from the WeaponSystem
226        this->weaponPacks_.erase(wPack);
227    }
228
229    WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const
230    {
231        unsigned int i = 0;
232        for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); ++it)
233        {
234            ++i;
235            if (i > index)
236                return (*it);
237        }
238        return 0;
239    }
240
241    bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2)
242    {
243        if (!wSlot1 || !wSlot2)
244            return false;
245
246        Weapon* weapon1 = wSlot1->getWeapon();
247        Weapon* weapon2 = wSlot2->getWeapon();
248
249        wSlot1->attachWeapon(weapon2);
250        wSlot2->attachWeapon(weapon1);
251
252        return true;
253        // In the future, certain weapons might not fit to some slots. Swapping would then be
254        // impossible and the returnvalue would be false.
255    }
256
257    void WeaponSystem::changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode)
258    {
259        if (!wPack || !wSet)
260            return;
261
262        // Check if the WeaponPack belongs to this WeaponSystem
263        std::set<WeaponPack *>::iterator it1 = this->weaponPacks_.find(wPack);
264        if (it1 == this->weaponPacks_.end())
265            return;
266
267        // Check if the WeaponSet belongs to this WeaponSystem
268        bool foundWeaponSet = false;
269        for (std::map<unsigned int, WeaponSet *>::iterator it2 = this->weaponSets_.begin(); it2 != this->weaponSets_.end(); ++it2)
270        {
271            if (it2->second == wSet)
272            {
273                foundWeaponSet = true;
274                break;
275            }
276        }
277        if (!foundWeaponSet)
278            return;
279
280        // Finally set the link between firemode and weaponmode
281        wSet->setWeaponmodeLink(wPack, weaponmode);
282    }
283
284    void WeaponSystem::fire(unsigned int firemode)
285    {
286        std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(firemode);
287        if (it != this->weaponSets_.end() && it->second)
288            it->second->fire();
289    }
290
291    void WeaponSystem::reload()
292    {
293        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
294            it->second->reload();
295    }
296
297    Munition * WeaponSystem::getMunition(SubclassIdentifier<Munition> * identifier)
298    {
299        if (!identifier || !identifier->getIdentifier())
300            return 0;
301
302        std::map<Identifier *, Munition *>::iterator it = this->munitions_.find(identifier->getIdentifier());
303        if (it != this->munitions_.end())
304        {
305            return it->second;
306        }
307        else if (identifier->getIdentifier()->isA(Class(Munition)))
308        {
309            Munition* munition = identifier->fabricate(this);
310            this->munitions_[identifier->getIdentifier()] = munition;
311            return munition;
312        }
313        else
314        {
315            return 0;
316        }
317    }
318}
Note: See TracBrowser for help on using the repository browser.