Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/pch/src/orxonox/objects/weaponsystem/Munition.cc @ 3175

Last change on this file since 3175 was 3110, checked in by rgrieder, 15 years ago

Removed old msvc specific support for precompiled header files.

  • Property svn:eol-style set to native
File size: 16.0 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 *   Authors:
23 *      Martin Polak
24 *      Fabian 'x3n' Landau
25 *   Co-authors:
26 *      ...
27 *
28 */
29
30#include "Munition.h"
31
32#include "core/CoreIncludes.h"
33
34namespace orxonox
35{
36    CreateFactory(Munition);
37
38    Munition::Munition(BaseObject* creator) : BaseObject(creator)
39    {
40        RegisterObject(Munition);
41
42        this->maxMunitionPerMagazine_ = 10;
43        this->maxMagazines_ = 10;
44        this->magazines_ = 10;
45
46        this->bUseSeparateMagazines_ = false;
47        this->bStackMunition_ = true;
48        this->bAllowMunitionRefilling_ = true;
49        this->bAllowMultiMunitionRemovementUnderflow_ = true;
50
51        this->reloadTime_ = 0;
52    }
53
54    Munition::~Munition()
55    {
56        for (std::map<WeaponMode*, Magazine*>::iterator it = this->currentMagazines_.begin(); it != this->currentMagazines_.end(); ++it)
57            delete it->second;
58    }
59
60    Munition::Magazine* Munition::getMagazine(WeaponMode* user) const
61    {
62        if (this->bUseSeparateMagazines_)
63        {
64            // For separated magazines we definitively need a given user
65            if (!user)
66                return 0;
67
68            // Use the map to get the magazine assigned to the given user
69            std::map<WeaponMode*, Magazine*>::const_iterator it = this->currentMagazines_.find(user);
70            if (it != this->currentMagazines_.end())
71                return it->second;
72        }
73        else
74        {
75            // We don't use separate magazines for each user, so just take the first magazine
76            if (this->currentMagazines_.size() > 0)
77                return this->currentMagazines_.begin()->second;
78        }
79
80        return 0;
81    }
82
83    unsigned int Munition::getNumMunition(WeaponMode* user) const
84    {
85        Magazine* magazine = this->getMagazine(user);
86        if (magazine)
87        {
88            if (this->bStackMunition_)
89                // With stacked munition every magazine contributes to the total amount
90                return this->maxMunitionPerMagazine_ * this->magazines_ + magazine->munition_;
91            else
92                // Wihtout stacked munition we just consider the current magazine
93                return magazine->munition_;
94        }
95        return 0;
96    }
97
98    unsigned int Munition::getNumMunitionInCurrentMagazine(WeaponMode* user) const
99    {
100        // In contrast to getNumMunition() we really just consider the current magazine, even if we're stacking munition
101        Magazine* magazine = this->getMagazine(user);
102        if (magazine)
103            return magazine->munition_;
104        else
105            return 0;
106    }
107
108    unsigned int Munition::getNumMagazines() const
109    {
110        if (this->bStackMunition_)
111        {
112            // If we stack munition and the current magazine is still full, it counts too
113            Magazine* magazine = this->getMagazine(0);
114            if (magazine && magazine->munition_ == this->maxMunitionPerMagazine_)
115                return this->magazines_ + 1;
116        }
117
118        return this->magazines_;
119    }
120
121    unsigned int Munition::getMaxMunition() const
122    {
123        if (this->bStackMunition_)
124            return this->maxMunitionPerMagazine_ * this->maxMagazines_;
125        else
126            return this->maxMunitionPerMagazine_;
127    }
128
129    bool Munition::canTakeMunition(unsigned int amount, WeaponMode* user) const
130    {
131        Magazine* magazine = this->getMagazine(user);
132        if (magazine && magazine->bLoaded_)
133        {
134            unsigned int munition = magazine->munition_;
135
136            // If we stack munition, we con't care about the current magazine - we just need enough munition in total
137            if (this->bStackMunition_)
138                munition += this->maxMunitionPerMagazine_ * this->magazines_;
139
140            if (munition == 0)
141                // Absolutely no munition - no chance to take munition
142                return false;
143            else if (this->bAllowMultiMunitionRemovementUnderflow_)
144                // We're not empty AND we allow underflow, so this will always work
145                return true;
146            else
147                // We don't allow underflow, so we have to check the amount
148                return (munition >= amount);
149        }
150        return false;
151    }
152
153    bool Munition::takeMunition(unsigned int amount, WeaponMode* user)
154    {
155        if (!this->canTakeMunition(amount, user))
156            return false;
157
158        Magazine* magazine = this->getMagazine(user);
159        if (magazine && magazine->bLoaded_)
160        {
161            if (magazine->munition_ >= amount)
162            {
163                // Enough munition
164                magazine->munition_ -= amount;
165                return true;
166            }
167            else
168            {
169                // Not enough munition
170                if (this->bStackMunition_)
171                {
172                    // We stack munition, so just take what we can and then load the next magazine
173                    amount -= magazine->munition_;
174                    magazine->munition_ = 0;
175
176                    if (this->reload(0))
177                        // Successfully reloaded, continue recursively
178                        return this->takeMunition(amount, 0);
179                    else
180                        // We don't have more magazines, so let's just hope we allow underflow
181                        return this->bAllowMultiMunitionRemovementUnderflow_;
182                }
183                else
184                {
185                    // We don't stack, so we can only take munition if this is allowed
186                    if (magazine->munition_ > 0 && this->bAllowMultiMunitionRemovementUnderflow_)
187                    {
188                        magazine->munition_ -= 0;
189                        return true;
190                    }
191                }
192            }
193        }
194        return false;
195    }
196
197    bool Munition::canReload() const
198    {
199        // As long as we have enough magazines (and don't stack munition) we can reload
200        return (this->magazines_ > 0 && !this->bStackMunition_);
201    }
202
203    bool Munition::needReload(WeaponMode* user) const
204    {
205        Magazine* magazine = this->getMagazine(user);
206        if (magazine)
207        {
208            if (this->bStackMunition_)
209                // With stacked munition, we never have to reload
210                return false;
211            else
212                // We need to reload if an already loaded magazine is empty
213                return (magazine->bLoaded_ && magazine->munition_ == 0);
214        }
215        else
216            // No magazine - we definitively need to reload
217            return true;
218    }
219
220    bool Munition::reload(WeaponMode* user, bool bUseReloadTime)
221    {
222        // Don't reload if we're already reloading
223        Magazine* magazine = this->getMagazine(user);
224        if (magazine && !magazine->bLoaded_)
225            return false;
226
227        // Check if we actually can reload
228        if (this->magazines_ == 0)
229            return false;
230
231        // If we use separate magazines for each user, we definitively need a user given
232        if (this->bUseSeparateMagazines_ && !user)
233            return false;
234
235        // If we don't use separate magazines, set user to 0
236        if (!this->bUseSeparateMagazines_)
237            user = 0;
238
239        // Remove the current magazine for the given user
240        std::map<WeaponMode*, Magazine*>::iterator it = this->currentMagazines_.find(user);
241        if (it != this->currentMagazines_.end())
242        {
243            delete it->second;
244            this->currentMagazines_.erase(it);
245        }
246
247        // Load a new magazine
248        this->currentMagazines_[user] = new Magazine(this, bUseReloadTime);
249        this->magazines_--;
250
251        return true;
252    }
253
254    bool Munition::canAddMunition(unsigned int amount) const
255    {
256        if (!this->bAllowMunitionRefilling_)
257            return false;
258
259        if (this->bStackMunition_)
260        {
261            // If we stack munition, we can always add munition until we reach the limit
262            return (this->getNumMunition(0) < this->getMaxMunition());
263        }
264        else
265        {
266            // Return true if any of the current magazines is not full (loading counts as full although it returns 0 munition)
267            for (std::map<WeaponMode*, Magazine*>::const_iterator it = this->currentMagazines_.begin(); it != this->currentMagazines_.end(); ++it)
268                if (it->second->munition_ < this->maxMunitionPerMagazine_ && it->second->bLoaded_)
269                    return true;
270        }
271
272        return false;
273    }
274
275    bool Munition::addMunition(unsigned int amount)
276    {
277        if (!this->canAddMunition(amount))
278            return false;
279
280        if (this->bStackMunition_)
281        {
282            // Stacking munition means, if a magazine gets full, the munition adds to a new magazine
283            Magazine* magazine = this->getMagazine(0);
284            if (magazine)
285            {
286                // Add the whole amount
287                magazine->munition_ += amount;
288
289                // Add new magazines if the current magazine is overfull
290                while (magazine->munition_ > this->maxMunitionPerMagazine_)
291                {
292                    magazine->munition_ -= this->maxMunitionPerMagazine_;
293                    this->magazines_++;
294                }
295
296                // If we reached the limit, reduze both magazines and munition to the maximum
297                if (this->magazines_ >= this->maxMagazines_)
298                {
299                    this->magazines_ = this->maxMagazines_ - 1;
300                    magazine->munition_ = this->maxMunitionPerMagazine_;
301                }
302
303                return true;
304            }
305
306            // Something went wrong
307            return false;
308        }
309        else
310        {
311            // Share the munition equally to the current magazines
312            while (amount > 0)
313            {
314                bool change = false;
315                for (std::map<WeaponMode*, Magazine*>::iterator it = this->currentMagazines_.begin(); it != this->currentMagazines_.end(); ++it)
316                {
317                    // Add munition if the magazine isn't full (but only to loaded magazines)
318                    if (amount > 0 && it->second->munition_ < this->maxMunitionPerMagazine_ && it->second->bLoaded_)
319                    {
320                        it->second->munition_++;
321                        amount--;
322                        change = true;
323                    }
324                }
325
326                // If there was no change in a loop, all magazines are full (or locked due to loading)
327                if (!change)
328                    break;
329            }
330
331            return true;
332        }
333    }
334
335    bool Munition::canAddMagazines(unsigned int amount) const
336    {
337        if (this->bStackMunition_)
338            // If we stack munition, we can always add new magazines because they contribute directly to the munition
339            return (this->getNumMunition(0) < this->getMaxMunition());
340        else
341            // If we don't stack munition, we're more limited
342            return ((this->currentMagazines_.size() + this->magazines_) < this->maxMagazines_);
343    }
344
345    bool Munition::addMagazines(unsigned int amount)
346    {
347        if (!this->canAddMagazines(amount))
348            return false;
349
350        // Calculate how many magazines are needed
351        int needed_magazines = this->maxMagazines_ - this->magazines_ - this->currentMagazines_.size();
352
353        // If zero or less magazines are needed, we definitively don't need more magazines (unless we stack munition - then a magazine contributes directly to the munition)
354        if (needed_magazines <= 0 && !this->bStackMunition_)
355            return false;
356
357        if (amount <= (unsigned int)needed_magazines)
358        {
359            // We need more magazines than we get, so just add them
360            this->magazines_ += amount;
361        }
362        else
363        {
364            // We get more magazines than we need, so just add the needed amount
365            this->magazines_ += needed_magazines;
366            if (this->bStackMunition_)
367            {
368                // We stack munition, so the additional amount contributes directly to the munition of the current magazine
369                Magazine* magazine = this->getMagazine(0);
370                if (magazine)
371                    magazine->munition_ = this->maxMunitionPerMagazine_;
372            }
373        }
374
375        return true;
376    }
377
378    bool Munition::canRemoveMagazines(unsigned int amount) const
379    {
380        if (this->bStackMunition_)
381        {
382            if (this->magazines_ >= amount)
383            {
384                // We have enough magazines
385                return true;
386            }
387            else if (this->magazines_ == amount - 1)
388            {
389                // We lack one magazine, check if the current magazine is still full, if yes we're fine
390                Magazine* magazine = this->getMagazine(0);
391                if (magazine && magazine->munition_ == this->maxMunitionPerMagazine_)
392                    return true;
393            }
394            else
395            {
396                // We don't have enough magazines
397                return false;
398            }
399        }
400        else
401        {
402            // In case we're not stacking munition, just check the number of magazines
403            return (this->magazines_ >= amount);
404        }
405
406        return false;
407    }
408
409    bool Munition::removeMagazines(unsigned int amount)
410    {
411        if (!this->canRemoveMagazines(amount))
412            return false;
413
414        if (this->magazines_ >= amount)
415        {
416            // We have enough magazines, just remove the amount
417            this->magazines_ -= amount;
418        }
419        else if (this->bStackMunition_)
420        {
421            // We don't have enough magazines, but we're stacking munition, so additionally remove the bullets from the current magazine
422            this->magazines_ = 0;
423            Magazine* magazine = this->getMagazine(0);
424            if (magazine)
425                magazine->munition_ = 0;
426        }
427
428        return true;
429    }
430
431    bool Munition::dropMagazine(WeaponMode* user)
432    {
433        // If we use separate magazines, we need a user
434        if (this->bUseSeparateMagazines_ && !user)
435            return false;
436
437        // If we don't use separate magazines, set user to 0
438        if (!this->bUseSeparateMagazines_)
439            user = 0;
440
441        // Remove the current magazine for the given user
442        std::map<WeaponMode*, Magazine*>::iterator it = this->currentMagazines_.find(user);
443        if (it != this->currentMagazines_.end())
444        {
445            delete it->second;
446            this->currentMagazines_.erase(it);
447            return true;
448        }
449
450        return false;
451    }
452
453
454    /////////////////////
455    // Magazine struct //
456    /////////////////////
457    Munition::Magazine::Magazine(Munition* munition, bool bUseReloadTime)
458    {
459        this->munition_ = 0;
460        this->bLoaded_ = false;
461
462        if (bUseReloadTime && (munition->reloadTime_ > 0 || munition->bStackMunition_))
463        {
464            ExecutorMember<Magazine>* executor = createExecutor(createFunctor(&Magazine::loaded));
465            executor->setDefaultValues(munition);
466
467            this->loadTimer_.setTimer(munition->reloadTime_, false, this, executor);
468        }
469        else
470            this->loaded(munition);
471    }
472
473    void Munition::Magazine::loaded(Munition* munition)
474    {
475        this->bLoaded_ = true;
476        this->munition_ = munition->maxMunitionPerMagazine_;
477    }
478}
Note: See TracBrowser for help on using the repository browser.