Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 11705 was 11704, checked in by landauf, 7 years ago

[HUD_HS16] fixed wrong dependency between overlays and pickup module: pickup should NOT depend on overlays; instead overlays should use pickup.

also reverted all changes from HUD_HS16 in PickupManager for several reasons:

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