Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/trunk/src/modules/pickup/PickupManager.cc @ 10985

Last change on this file since 10985 was 10624, checked in by landauf, 9 years ago

merged branch core7 back to trunk

  • Property svn:eol-style set to native
File size: 20.9 KB
RevLine 
[6474]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:
[7494]23 *      Damian 'Mozork' Frick
[6474]24 *   Co-authors:
25 *      ...
26 *
27*/
28
29/**
[6540]30    @file PickupManager.cc
[6474]31    @brief Implementation of the PickupManager class.
32*/
33
34#include "PickupManager.h"
35
36#include "core/CoreIncludes.h"
[6711]37#include "core/LuaState.h"
38#include "core/GUIManager.h"
[9667]39#include "core/class/Identifier.h"
[10624]40#include "core/singleton/ScopedSingletonIncludes.h"
[7504]41#include "network/Host.h"
[10624]42#include "network/NetworkFunctionIncludes.h"
[7533]43
[7539]44#include "infos/PlayerInfo.h"
[6474]45#include "interfaces/PickupCarrier.h"
46#include "worldentities/pawns/Pawn.h"
[7533]47
[7163]48#include "CollectiblePickup.h"
[6474]49#include "PickupRepresentation.h"
50
51namespace orxonox
52{
[10624]53    ManageScopedSingleton(PickupManager, ScopeID::ROOT, false);
[7163]54
[7533]55    // Initialization of the name of the PickupInventory GUI.
[6711]56    /*static*/ const std::string PickupManager::guiName_s = "PickupInventory";
[7163]57
[7801]58    // Register static network functions that are used to communicate changes to pickups over the network, such that the PickupInventory can display the information about the pickups properly.
[7504]59    registerStaticNetworkFunction(PickupManager::pickupChangedUsedNetwork);
60    registerStaticNetworkFunction(PickupManager::pickupChangedPickedUpNetwork);
61    registerStaticNetworkFunction(PickupManager::dropPickupNetworked);
62    registerStaticNetworkFunction(PickupManager::usePickupNetworked);
63
[10624]64    RegisterAbstractClass(PickupManager).inheritsFrom<PickupListener>();
65
[6474]66    /**
67    @brief
68        Constructor. Registers the PickupManager and creates the default PickupRepresentation.
69    */
[7533]70    PickupManager::PickupManager() : guiLoaded_(false), pickupHighestIndex_(0), defaultRepresentation_(NULL)
[6474]71    {
[7504]72        RegisterObject(PickupManager);
[7163]73
[6474]74        this->defaultRepresentation_ = new PickupRepresentation();
[7163]75
[8858]76        orxout(internal_info, context::pickups) << "PickupManager created." << endl;
[6474]77    }
[7163]78
[6474]79    /**
80    @brief
81        Destructor.
[7533]82        Destroys the default PickupRepresentation and does some cleanup.
[6474]83    */
84    PickupManager::~PickupManager()
85    {
[7533]86        // Destroying the default representation.
[6474]87        if(this->defaultRepresentation_ != NULL)
88            this->defaultRepresentation_->destroy();
[7163]89
[6725]90        this->representations_.clear();
[7163]91
[7533]92        // Destroying all the PickupInventoryContainers that are still there.
93        for(std::map<uint32_t, PickupInventoryContainer*>::iterator it = this->pickupInventoryContainers_.begin(); it != this->pickupInventoryContainers_.end(); it++)
94            delete it->second;
95        this->pickupInventoryContainers_.clear();
[7504]96
[7533]97        // Destroying all the WeakPointers that are still there.
98        this->pickups_.clear();
99
100        this->indexes_.clear();
101
[8858]102        orxout(internal_info, context::pickups) << "PickupManager destroyed." << endl;
[6474]103    }
[7163]104
[6474]105    /**
106    @brief
[9348]107        Registers a PickupRepresentation.
108    @param name
109        The representation's name.
[6474]110    @param representation
111        A pointer to the PickupRepresentation.
112    @return
113        Returns true if successful and false if not.
114    */
[9348]115    bool PickupManager::registerRepresentation(const std::string& name, PickupRepresentation* representation)
[7163]116    {
[7494]117        assert(representation);
118
[7533]119        // If the list is not empty and Pickupable already has a Representation registered.
[9348]120        if(!this->representations_.empty() && this->representations_.find(name) != this->representations_.end())
[6474]121            return false;
[7163]122
[9348]123        this->representations_[name] = representation;
[7163]124
[8858]125        orxout(verbose, context::pickups) << "PickupRepresentation &" << representation << " registered with the PickupManager." << endl;
[6474]126        return true;
127    }
[7163]128
[6474]129    /**
130    @brief
[9348]131        Unegisters a PickupRepresentation.
132    @param name
133        The representation's name.
[6725]134    @return
135        Returns true if successful and false if not.
136    */
[9348]137    bool PickupManager::unregisterRepresentation(const std::string& name)
[7163]138    {
[9348]139        std::map<std::string, PickupRepresentation*>::iterator it = this->representations_.find(name);
[7533]140        if(it == this->representations_.end()) // If the Pickupable is not registered in the first place.
[6725]141            return false;
[7163]142
[6725]143        this->representations_.erase(it);
[7163]144
[9348]145        orxout(verbose, context::pickups) << "PickupRepresentation &" << name << " unregistered with the PickupManager." << endl;
[6725]146        return true;
147    }
[7163]148
[7533]149    /**
150    @brief
[9348]151        Get the PickupRepresentation with the given name.
152    @param name
153        The name of the PickupRepresentation.
[7533]154    @return
[6474]155        Returns a pointer to the PickupRepresentation.
156    */
[9348]157    PickupRepresentation* PickupManager::getRepresentation(const std::string& name)
[6474]158    {
[9348]159        std::map<std::string, PickupRepresentation*>::iterator it = this->representations_.find(name);
160        if(it == this->representations_.end()) // If there is no PickupRepresentation associated with the input name.
[6474]161        {
[8858]162            orxout(verbose, context::pickups) << "PickupManager::getRepresentation() returned default representation." << endl;
[6474]163            return this->defaultRepresentation_;
164        }
[7163]165
[6474]166        return it->second;
167    }
[7163]168
[7206]169    /**
170    @brief
[7533]171        Is called by the PickupListener to notify the PickupManager, that the input Pickupable has transited to the input used state.
[7206]172    @param pickup
[7533]173        The Pickupable whose used status changed.
174    @param used
175        The used status the Pickupable changed to.
[7206]176    */
[7504]177    void PickupManager::pickupChangedUsed(Pickupable* pickup, bool used)
178    {
179        assert(pickup);
180
181        if(!GameMode::isMaster()) // If this is neither standalone nor the server.
182            return;
183
184        CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(pickup);
[7533]185        // If the Pickupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is.
[7504]186        if(collectible != NULL && collectible->isInCollection())
187            return;
188
[7533]189        // Getting clientId of the host this change of the pickup's used status concerns.
[7504]190        PickupCarrier* carrier = pickup->getCarrier();
191        while(carrier->getCarrierParent() != NULL)
192            carrier = carrier->getCarrierParent();
193        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
194        if(pawn == NULL)
195            return;
196        PlayerInfo* info = pawn->getPlayer();
197        if(info == NULL)
198            return;
199        unsigned int clientId = info->getClientID();
200
[7533]201        // Get the number identifying the pickup.
202        std::map<Pickupable*, uint32_t>::iterator it = this->indexes_.find(pickup);
[7504]203        assert(it != this->indexes_.end());
[7533]204        uint32_t index = it->second;
[7504]205
[7533]206        // If we're either in standalone mode or this is the host whom the change of the pickup's status concerns.
[7504]207        if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
208        {
209            PickupManager::pickupChangedUsedNetwork(index, used, pickup->isUsable(), pickup->isUnusable());
210        }
[7533]211        // If the concerned host is somewhere in the network, we call pickupChangedUsedNetwork() on its PickupManager.
[7504]212        else
213        {
[10624]214            callStaticNetworkFunction(&PickupManager::pickupChangedUsedNetwork, clientId, index, used, pickup->isUsable(), pickup->isUnusable());
[7504]215        }
216    }
217
[7533]218    /**
219    @brief
220        Helper method to react to the change in the used status of a Pickupable.
221        Static method that is used by the server to inform the client it concerns about the status change.
222        The parameters that are given are used to update the information (i.e. the PickupInventoryContainer) the concerning PickupManager has about the Pickupable that changed.
223    @param pickup
224        A number identifying the Pickupable that changed its used status.
225    @param inUse
226        The used status the Pickupable changed to. (i.e. whether the Pickupable is in use or not).
227    @param usable
228        Whether the Pickupable's used status can be changed used in the PickupInventory.
229    @param unusable
230        Whether the Pickupable's used status can be changed to unused in the PickupInventory.
231    */
232    /*static*/ void PickupManager::pickupChangedUsedNetwork(uint32_t pickup, bool inUse, bool usable, bool unusable)
[7504]233    {
[7533]234        PickupManager& manager = PickupManager::getInstance(); // Get the PickupManager singleton on this host.
235        // If the input Pickupable (i.e its identifier) is not present in the list the PickupManager has.
[7504]236        if(manager.pickupInventoryContainers_.find(pickup) == manager.pickupInventoryContainers_.end())
237        {
[8858]238            orxout(internal_error, context::pickups) << "Pickupable &(" << pickup << ") was not registered with PickupManager for the PickupInventory, when it changed used." << endl;
[7504]239            return;
240        }
241
[7533]242        // Update the Pickupable's container with the information transferred.
[7504]243        manager.pickupInventoryContainers_[pickup]->inUse = inUse;
244        manager.pickupInventoryContainers_[pickup]->usable = usable;
245        manager.pickupInventoryContainers_[pickup]->unusable = unusable;
246
[7533]247        manager.updateGUI(); // Tell the PickupInventory that something has changed.
[7504]248    }
249
[7533]250    /**
251    @brief
252        Is called by the PickupListener to notify the PickupManager, that the input Pickupable has transited to the input pickedUp state.
253    @param pickup
254        The Pickupable whose pickedUp status changed.
255    @param pickedUp
256        The pickedUp status the Pickupable changed to.
257    */
[7504]258    void PickupManager::pickupChangedPickedUp(Pickupable* pickup, bool pickedUp)
259    {
260        assert(pickup);
261
[7533]262        if(!GameMode::isMaster()) // If this is neither standalone nor the server.
[7504]263            return;
264
265        CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(pickup);
[7533]266        // If the Pickupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is.
[7504]267        if(collectible != NULL && collectible->isInCollection())
268            return;
269
[7533]270        // Getting clientId of the host this change of the pickup's pickedUp status concerns.
[7504]271        PickupCarrier* carrier = pickup->getCarrier();
272        while(carrier->getCarrierParent() != NULL)
273            carrier = carrier->getCarrierParent();
274        Pawn* pawn = orxonox_cast<Pawn*>(carrier);
275        if(pawn == NULL)
276            return;
[7533]277        PlayerInfo* info = pawn->getFormerPlayer();
[7504]278        if(info == NULL)
279            return;
280        unsigned int clientId = info->getClientID();
281
[7533]282        uint32_t index = 0;
283        if(pickedUp) // If the Pickupable has changed to picked up, it is added to the required lists.
[7504]284        {
[7533]285            index = this->getPickupIndex(); // Ge a new identifier (index) for the Pickupable.
286            // Add the Pickupable to the indexes_ and pickups_ lists.
[7504]287            this->indexes_[pickup] = index;
[10624]288            this->pickups_[index] = pickup;
[7504]289        }
[7533]290        else // If it was dropped, it is removed from the required lists.
[7504]291        {
[7533]292            // Get the indentifier (index) that identifies the input Pickupable.
293            std::map<Pickupable*, uint32_t>::iterator it = this->indexes_.find(pickup);
[7504]294            index = it->second;
[7533]295
[10624]296            // Remove the Pickupable from the indexes_ and pickups_ list.
[7504]297            this->indexes_.erase(it);
298            this->pickups_.erase(index);
299        }
300
[7533]301        // If we're either in standalone mode or this is the host whom the change of the pickup's status concerns.
[7504]302        if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
303        {
[7533]304            // If there is no PickupRepresentation registered the default representation is used.
[9348]305            if(this->representations_.find(pickup->getRepresentationName()) == this->representations_.end())
306                PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), this->defaultRepresentation_->getObjectID(), pickup->getRepresentationName(), pickedUp);
[7531]307            else
[9348]308                PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), this->representations_[pickup->getRepresentationName()]->getObjectID(), pickup->getRepresentationName(), pickedUp);
[7504]309        }
[7533]310        // If the concerned host is somewhere in the network, we call pickupChangedPickedUpNetwork() on its PickupManager.
[7504]311        else
312        {
[7533]313            // If there is no PickupRepresentation registered the default representation is used.
[9348]314            if(this->representations_.find(pickup->getRepresentationName()) == this->representations_.end())
[7531]315            {
[10624]316                callStaticNetworkFunction(&PickupManager::pickupChangedPickedUpNetwork, clientId, index, pickup->isUsable(), this->defaultRepresentation_->getObjectID(), pickedUp);
[7531]317            }
318            else
319            {
[10624]320                callStaticNetworkFunction(&PickupManager::pickupChangedPickedUpNetwork, clientId, index, pickup->isUsable(), this->representations_[pickup->getRepresentationName()]->getObjectID(), pickedUp);
[7531]321            }
[7504]322        }
323
324    }
325
[7533]326    /**
327    @brief
328        Helper method to react to the change in the pickedUp status of a Pickupable.
329        Static method that is used by the server to inform the client it concerns about the status change.
330        The parameters that are given are used to update the information (i.e. the PickupInventoryContainer) the concerning PickupManager has about the Pickupable that changed.
331    @param pickup
332        A number identifying the Pickupable that changed its pickedUp status.
[7547]333    @param usable
334        Whether the Pickupable's used status can be changed to used in the PickupInventory.
[7533]335    @param representationObjectId
336        The objectId identifying (over the network) the PickupRepresentation that represents this Pickupable.
337    @param pickedUp
338        The pickedUp status the Pickupable changed to.
339    */
[9348]340    /*static*/ void PickupManager::pickupChangedPickedUpNetwork(uint32_t pickup, bool usable, uint32_t representationObjectId, const std::string& representationName, bool pickedUp)
[7504]341    {
[7533]342        PickupManager& manager = PickupManager::getInstance(); // Get the PickupManager singleton on this host.
343        // If the Pickupable has been picked up, we create a new PickupInventoryContainer for it.
[7504]344        if(pickedUp)
345        {
[7533]346            // Create a new PickupInventoryContainer for the Pickupable and set all the necessary information.
[7504]347            PickupInventoryContainer* container = new PickupInventoryContainer;
348            container->pickup = pickup;
349            container->inUse = false;
350            container->pickedUp = pickedUp;
351            container->usable = usable;
352            container->unusable = false;
353            container->representationObjectId = representationObjectId;
[9348]354            container->representationName = representationName;
[7533]355            // Insert the container into the pickupInventoryContainers_ list.
356            manager.pickupInventoryContainers_.insert(std::pair<uint32_t, PickupInventoryContainer*>(pickup, container));
[7504]357
[7533]358            manager.updateGUI(); // Tell the PickupInventory that something has changed.
[7504]359        }
[7533]360        // If the Pickupable has been dropped, we remove it from the pickupInventoryContainers_ list.
[7504]361        else
362        {
[7533]363            std::map<uint32_t, PickupInventoryContainer*>::iterator it = manager.pickupInventoryContainers_.find(pickup);
[7504]364            if(it != manager.pickupInventoryContainers_.end())
365                delete it->second;
366            manager.pickupInventoryContainers_.erase(pickup);
367
[7533]368            manager.updateGUI(); // Tell the PickupInventory that something has changed.
[7504]369        }
370    }
371
[7533]372    /**
373    @brief
374        Get the number of pickups currently picked up by the player.
375        This method is used in lua to populate the PickupInventory. The intended usage is to call this method to reset the iterator of the list of PickupInventoryContainers and then use popPickup() to get the individual PickupInventoryContainers.
376    @return
377        Returns the number of the players picked up Pickupables.
378    */
379    int PickupManager::getNumPickups(void)
380    {
381        this->pickupsIterator_ = this->pickupInventoryContainers_.begin(); // Reset iterator.
382
383        return this->pickupInventoryContainers_.size();
384    }
385
386    /**
387    @brief
388        Drop the input Pickupable.
389        This method checks whether the input Pickupable still exists and drops it, if so.
390    @param pickup
391        The identifier of the Pickupable to be dropped.
392    */
393    void PickupManager::dropPickup(uint32_t pickup)
394    {
395        // If we're either server or standalone and the list of pickups is not empty, we find and drop the input pickup.
[7539]396        if(GameMode::isMaster())
[7533]397        {
[7539]398            if(this->pickups_.empty())
399                return;
[10624]400            Pickupable* pickupable = this->pickups_.find(pickup)->second;
[7533]401            if(pickupable != NULL)
402                pickupable->drop();
403        }
404        // If we're neither server nor standalone we drop the pickup by calling dropPickupNetworked() of the PickupManager on the server.
405        else
406        {
[10624]407            callStaticNetworkFunction(&PickupManager::dropPickupNetworked, 0, pickup);
[7533]408        }
409    }
410
411    /**
412    @brief
413        Helper method to drop the input pickup on the server.
414        Static method that is used by clients to instruct the server to drop the input pickup.
415    @param pickup
416        The identifier of the Pickupable to be dropped.
417    */
418    /*static*/ void PickupManager::dropPickupNetworked(uint32_t pickup)
419    {
420        if(GameMode::isServer()) // Obviously we only want to do this on the server.
421        {
422            PickupManager& manager = PickupManager::getInstance();
[7539]423            manager.dropPickup(pickup);
[7533]424        }
425    }
426
427    /**
428    @brief
429        Use (or unuse) the input Pickupable.
430        This method checks whether the input Pickupable still exists and uses (or unuses) it, if so,
431    @param pickup
432        The identifier of the Pickupable to be used (or unused).
433    @param use
434        If true the input Pickupable is used, if false it is unused.
435    */
436    void PickupManager::usePickup(uint32_t pickup, bool use)
437    {
438        // If we're either server or standalone and the list of pickups is not empty, we find and change the used status of the input pickup.
[7539]439        if(GameMode::isMaster())
[7533]440        {
[7539]441            if(this->pickups_.empty())
442                return;
[10624]443            Pickupable* pickupable = this->pickups_.find(pickup)->second;
[7533]444            if(pickupable != NULL)
445                pickupable->setUsed(use);
446        }
447        // If we're neither server nor standalone we change the used status of the pickup by calling usePickupNetworked() of the PickupManager on the server.
448        else
449        {
[10624]450            callStaticNetworkFunction(&PickupManager::usePickupNetworked, 0, pickup, use);
[7533]451        }
452    }
453
454    /**
455    @brief
456        Helper method to use (or unuse) the input Pickupable on the server.
457        Static method that is used by clients to instruct the server to use (or unuse) the input pickup.
458    @param pickup
459        The identifier of the Pickupable to be used (or unused).
460    @param use
461        If true the input Pickupable is used, if false it is unused.
462    */
463    /*static*/ void PickupManager::usePickupNetworked(uint32_t pickup, bool use)
464    {
465        if(GameMode::isServer())
466        {
467            PickupManager& manager = PickupManager::getInstance();
[7539]468            manager.usePickup(pickup, use);
[7533]469        }
470    }
471
472    /**
473    @brief
474        Updates the PickupInventory GUI.
475        Also loads the PickupInventory GUI if is hasn't been done already.
476    */
[7504]477    inline void PickupManager::updateGUI(void)
478    {
[7533]479        // We only need to update (and load) the GUI if this host shows graphics.
[7504]480        if(GameMode::showsGraphics())
481        {
[7533]482            if(!this->guiLoaded_) // If the GUI hasn't been loaded, yet, we load it.
[7504]483            {
484                GUIManager::getInstance().loadGUI(PickupManager::guiName_s);
485                this->guiLoaded_ = true;
486            }
[7533]487
488            // Update the GUI.
[7504]489            GUIManager::getInstance().getLuaState()->doString(PickupManager::guiName_s + ".update()");
490        }
491    }
492
[7533]493    /**
494    @brief
495        Get a new index for a Pickupable.
496        This will work as long as the number of Pickupables that are picked up is sufficiently small and as long as they don't exist forever.
497    @return
498        Returns the new index.
499    */
500    uint32_t PickupManager::getPickupIndex(void)
[7504]501    {
[7533]502        if(this->pickupHighestIndex_ == uint32_t(~0x0)-1) // If we've reached the highest possible number, we wrap around.
503            this->pickupHighestIndex_ = 0;
504        return this->pickupHighestIndex_++;
[7504]505    }
506
[6474]507}
Note: See TracBrowser for help on using the repository browser.