Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 2915 was 2915, checked in by landauf, 15 years ago
  • switched back to std::vector for the WeaponSlots to keep them in the same order as in the XML file
  • added DefaultWeaponmodeLink, a class which links weaponmodes (a property of a Weapon or a WeaponPack) with firemodes (one firemode corresponds to one WeaponSet). This can be changed later (for example in a nice GUI), but DefaultWeaponmodeLink defines the default value.
  • 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/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/pickups2/src/orxonox/objects/weaponSystem/WeaponSystem.cc2107-2497
    /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
40/* WeaponSystem
41 *
42 *  www.orxonox.net/wiki/WeaponSystem
43 */
44
45namespace orxonox
46{
47    CreateFactory(WeaponSystem);
48
49    WeaponSystem::WeaponSystem(BaseObject* creator) : BaseObject(creator)
50    {
51        RegisterObject(WeaponSystem);
52
53        this->pawn_ = 0;
54COUT(0) << "+WeaponSystem" << std::endl;
55    }
56
57    WeaponSystem::~WeaponSystem()
58    {
59COUT(0) << "~WeaponSystem" << std::endl;
60        if (this->isInitialized())
61        {
62            if (this->pawn_)
63                this->pawn_->setWeaponSystem(0);
64
65//            for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
66//                delete (it++)->second;
67            while (!this->weaponSets_.empty())
68                delete (this->weaponSets_.begin()->second);
69
70//            for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); )
71//                delete (*(it++));
72            while (!this->weaponPacks_.empty())
73                delete (*this->weaponPacks_.begin());
74
75//            for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); )
76//                delete (*(it++));
77            while (!this->weaponSlots_.empty())
78                delete (*this->weaponSlots_.begin());
79        }
80    }
81
82    void WeaponSystem::addWeaponSlot(WeaponSlot * wSlot)
83    {
84        if (!wSlot)
85            return;
86
87        this->weaponSlots_.push_back(wSlot);
88        wSlot->setWeaponSystem(this);
89    }
90
91    void WeaponSystem::removeWeaponSlot(WeaponSlot * wSlot)
92    {
93        if (!wSlot)
94            return;
95
96        if (wSlot->getWeapon())
97            this->removeWeaponPack(wSlot->getWeapon()->getWeaponPack());
98
99        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
100        {
101            if ((*it) == wSlot)
102            {
103                this->weaponSlots_.erase(it);
104                break;
105            }
106        }
107    }
108
109    WeaponSlot * WeaponSystem::getWeaponSlot(unsigned int index) const
110    {
111        unsigned int i = 0;
112        for (std::vector<WeaponSlot*>::const_iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
113        {
114            ++i;
115            if (i > index)
116                return (*it);
117        }
118        return 0;
119    }
120
121    bool WeaponSystem::addWeaponSet(WeaponSet * wSet)
122    {
123        if (wSet)
124            return this->addWeaponSet(wSet, wSet->getDesiredFiremode());
125        else
126            return false;
127    }
128
129    bool WeaponSystem::addWeaponSet(WeaponSet * wSet, unsigned int firemode)
130    {
131        if (!wSet || firemode >= WeaponSystem::MAX_FIRE_MODES)
132            return false;
133
134        std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.find(firemode);
135        if (it == this->weaponSets_.end())
136        {
137            this->weaponSets_[firemode] = wSet;
138            wSet->setWeaponSystem(this);
139            return true;
140        }
141
142        return false;
143    }
144
145    void WeaponSystem::removeWeaponSet(WeaponSet * wSet)
146    {
147        for (std::map<unsigned int, WeaponSet*>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); )
148        {
149            if (it->second == wSet)
150                this->weaponSets_.erase(it++);
151            else
152                ++it;
153        }
154    }
155
156    WeaponSet * WeaponSystem::getWeaponSet(unsigned int index) const
157    {
158        unsigned int i = 0;
159        for (std::map<unsigned int, WeaponSet*>::const_iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
160        {
161            ++i;
162            if (i > index)
163                return it->second;
164        }
165        return 0;
166    }
167
168    bool WeaponSystem::canAddWeaponPack(WeaponPack * wPack)
169    {
170        if (!wPack)
171            return false;
172
173        unsigned int freeSlots = 0;
174        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
175        {
176            if (!(*it)->isOccupied())
177                ++freeSlots;
178        }
179
180        return (freeSlots >= wPack->getNumWeapons());
181    }
182
183    bool WeaponSystem::addWeaponPack(WeaponPack * wPack)
184    {
185        if (!this->canAddWeaponPack(wPack))
186            return false;
187
188        // Attach all weapons to the first free slots (and to the Pawn)
189        unsigned int i = 0;
190        for (std::vector<WeaponSlot*>::iterator it = this->weaponSlots_.begin(); it != this->weaponSlots_.end(); ++it)
191        {
192            if (!(*it)->isOccupied() && i < wPack->getNumWeapons())
193            {
194                Weapon* weapon = wPack->getWeapon(i);
195                (*it)->attachWeapon(weapon);
196                this->getPawn()->attach(weapon);
197                ++i;
198            }
199        }
200
201        // Assign the desired weaponmode to the firemodes
202        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
203        {
204            unsigned int weaponmode = wPack->getDesiredWeaponmode(it->first);
205            if (weaponmode != WeaponSystem::WEAPON_MODE_UNASSIGNED)
206                it->second->setWeaponmodeLink(wPack, weaponmode);
207        }
208
209        this->weaponPacks_.insert(wPack);
210        wPack->setWeaponSystem(this);
211
212        return true;
213    }
214
215    void WeaponSystem::removeWeaponPack(WeaponPack * wPack)
216    {
217        // Remove all weapons from their WeaponSlot
218        unsigned int i = 0;
219        Weapon* weapon = 0;
220        while (weapon = wPack->getWeapon(i++))
221            weapon->getWeaponSlot()->removeWeapon();
222
223        // Remove all added links from the WeaponSets
224        for (std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.begin(); it != this->weaponSets_.end(); ++it)
225            it->second->removeWeaponmodeLink(wPack);
226
227        // Remove the WeaponPack from the WeaponSystem
228        this->weaponPacks_.erase(wPack);
229    }
230
231    WeaponPack * WeaponSystem::getWeaponPack(unsigned int index) const
232    {
233        unsigned int i = 0;
234        for (std::set<WeaponPack*>::iterator it = this->weaponPacks_.begin(); it != this->weaponPacks_.end(); ++it)
235        {
236            ++i;
237            if (i > index)
238                return (*it);
239        }
240        return 0;
241    }
242
243    bool WeaponSystem::swapWeaponSlots(WeaponSlot * wSlot1, WeaponSlot * wSlot2)
244    {
245        if (!wSlot1 || !wSlot2)
246            return false;
247
248        Weapon* weapon1 = wSlot1->getWeapon();
249        Weapon* weapon2 = wSlot2->getWeapon();
250
251        wSlot1->attachWeapon(weapon2);
252        wSlot2->attachWeapon(weapon1);
253
254        return true;
255        // In the future, certain weapons might not fit to some slots. Swapping would then be
256        // impossible and the returnvalue would be false.
257    }
258
259    void WeaponSystem::changeWeaponmode(WeaponPack * wPack, WeaponSet * wSet, unsigned int weaponmode)
260    {
261        if (!wPack || !wSet)
262            return;
263
264        // Check if the WeaponPack belongs to this WeaponSystem
265        std::set<WeaponPack *>::iterator it1 = this->weaponPacks_.find(wPack);
266        if (it1 == this->weaponPacks_.end())
267            return;
268
269        // Check if the WeaponSet belongs to this WeaponSystem
270        bool foundWeaponSet = false;
271        for (std::map<unsigned int, WeaponSet *>::iterator it2 = this->weaponSets_.begin(); it2 != this->weaponSets_.end(); ++it2)
272        {
273            if (it2->second == wSet)
274            {
275                foundWeaponSet = true;
276                break;
277            }
278        }
279        if (!foundWeaponSet)
280            return;
281
282        // Finally set the link between firemode and weaponmode
283        wSet->setWeaponmodeLink(wPack, weaponmode);
284    }
285
286    void WeaponSystem::setNewMunition(const std::string& munitionType, Munition * munitionToAdd)
287    {
288        this->munitionSet_[munitionType] = munitionToAdd;
289    }
290
291
292    //returns the Pointer to the munitionType, if this munitionType doesn't exist returns 0, see Weapon::attachNeededMunition
293    Munition * WeaponSystem::getMunitionType(const std::string& munitionType) const
294    {
295        std::map<std::string, Munition *>::const_iterator it = this->munitionSet_.find(munitionType);
296        if (it != this->munitionSet_.end())
297            return it->second;
298        else
299            return 0;
300    }
301
302    void WeaponSystem::fire(unsigned int firemode)
303    {
304        std::map<unsigned int, WeaponSet *>::iterator it = this->weaponSets_.find(firemode);
305        if (it != this->weaponSets_.end() && it->second)
306            it->second->fire();
307    }
308}
Note: See TracBrowser for help on using the repository browser.