Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

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

Last change on this file since 12031 was 11708, checked in by landauf, 7 years ago

[HUD_HS16] fixed issue that the hud shows pickups of ALL players/bots instead of just the own spaceship. fixed this by comparing objectIDs
note: network calls currently support only 5 arguments, so I removed representationObjectId which doesn't seem to be used (neither in C++ nor in Lua)

  • Property svn:eol-style set to native
File size: 20.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        {
[11708]303            PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), pickup->getRepresentationName(), pickedUp, pawn->getObjectID());
[7504]304        }
[7533]305        // If the concerned host is somewhere in the network, we call pickupChangedPickedUpNetwork() on its PickupManager.
[7504]306        else
307        {
[11708]308            callStaticNetworkFunction(&PickupManager::pickupChangedPickedUpNetwork, clientId, index, pickup->isUsable(), pickedUp, pawn->getObjectID());
[7504]309        }
310
311    }
312
[7533]313    /**
314    @brief
315        Helper method to react to the change in the pickedUp status of a Pickupable.
316        Static method that is used by the server to inform the client it concerns about the status change.
317        The parameters that are given are used to update the information (i.e. the PickupInventoryContainer) the concerning PickupManager has about the Pickupable that changed.
318    @param pickup
319        A number identifying the Pickupable that changed its pickedUp status.
[7547]320    @param usable
321        Whether the Pickupable's used status can be changed to used in the PickupInventory.
[11099]322    @param representationName
323        The name of the associated PickupRepresentation
[7533]324    @param pickedUp
325        The pickedUp status the Pickupable changed to.
[11708]326    @param carrierPawnId
327        The objectId identifier (over the network) the Pawn that carries this Pickupable
[7533]328    */
[11708]329    /*static*/ void PickupManager::pickupChangedPickedUpNetwork(uint32_t pickup, bool usable, const std::string& representationName, bool pickedUp, uint32_t carrierPawnId)
[7504]330    {
[7533]331        PickupManager& manager = PickupManager::getInstance(); // Get the PickupManager singleton on this host.
332        // If the Pickupable has been picked up, we create a new PickupInventoryContainer for it.
[7504]333        if(pickedUp)
334        {
[7533]335            // Create a new PickupInventoryContainer for the Pickupable and set all the necessary information.
[7504]336            PickupInventoryContainer* container = new PickupInventoryContainer;
337            container->pickup = pickup;
338            container->inUse = false;
339            container->pickedUp = pickedUp;
340            container->usable = usable;
341            container->unusable = false;
[9348]342            container->representationName = representationName;
[11708]343            container->carrierPawnId = carrierPawnId;
[7533]344            // Insert the container into the pickupInventoryContainers_ list.
345            manager.pickupInventoryContainers_.insert(std::pair<uint32_t, PickupInventoryContainer*>(pickup, container));
[7504]346
[7533]347            manager.updateGUI(); // Tell the PickupInventory that something has changed.
[7504]348        }
[7533]349        // If the Pickupable has been dropped, we remove it from the pickupInventoryContainers_ list.
[7504]350        else
351        {
[7533]352            std::map<uint32_t, PickupInventoryContainer*>::iterator it = manager.pickupInventoryContainers_.find(pickup);
[7504]353            if(it != manager.pickupInventoryContainers_.end())
354                delete it->second;
355            manager.pickupInventoryContainers_.erase(pickup);
356
[7533]357            manager.updateGUI(); // Tell the PickupInventory that something has changed.
[7504]358        }
359    }
360
[7533]361    /**
362    @brief
363        Get the number of pickups currently picked up by the player.
364        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.
365    @return
366        Returns the number of the players picked up Pickupables.
367    */
368    int PickupManager::getNumPickups(void)
369    {
370        this->pickupsIterator_ = this->pickupInventoryContainers_.begin(); // Reset iterator.
371
372        return this->pickupInventoryContainers_.size();
373    }
374
375    /**
376    @brief
377        Drop the input Pickupable.
378        This method checks whether the input Pickupable still exists and drops it, if so.
379    @param pickup
380        The identifier of the Pickupable to be dropped.
381    */
382    void PickupManager::dropPickup(uint32_t pickup)
383    {
384        // If we're either server or standalone and the list of pickups is not empty, we find and drop the input pickup.
[7539]385        if(GameMode::isMaster())
[7533]386        {
[7539]387            if(this->pickups_.empty())
388                return;
[10624]389            Pickupable* pickupable = this->pickups_.find(pickup)->second;
[11071]390            if(pickupable != nullptr)
[7533]391                pickupable->drop();
392        }
393        // If we're neither server nor standalone we drop the pickup by calling dropPickupNetworked() of the PickupManager on the server.
394        else
395        {
[10624]396            callStaticNetworkFunction(&PickupManager::dropPickupNetworked, 0, pickup);
[7533]397        }
398    }
399
400    /**
401    @brief
402        Helper method to drop the input pickup on the server.
403        Static method that is used by clients to instruct the server to drop the input pickup.
404    @param pickup
405        The identifier of the Pickupable to be dropped.
406    */
407    /*static*/ void PickupManager::dropPickupNetworked(uint32_t pickup)
408    {
409        if(GameMode::isServer()) // Obviously we only want to do this on the server.
410        {
411            PickupManager& manager = PickupManager::getInstance();
[7539]412            manager.dropPickup(pickup);
[7533]413        }
414    }
415
416    /**
417    @brief
418        Use (or unuse) the input Pickupable.
419        This method checks whether the input Pickupable still exists and uses (or unuses) it, if so,
420    @param pickup
421        The identifier of the Pickupable to be used (or unused).
422    @param use
423        If true the input Pickupable is used, if false it is unused.
424    */
425    void PickupManager::usePickup(uint32_t pickup, bool use)
426    {
427        // 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]428        if(GameMode::isMaster())
[7533]429        {
[7539]430            if(this->pickups_.empty())
431                return;
[10624]432            Pickupable* pickupable = this->pickups_.find(pickup)->second;
[11071]433            if(pickupable != nullptr)
[7533]434                pickupable->setUsed(use);
435        }
436        // If we're neither server nor standalone we change the used status of the pickup by calling usePickupNetworked() of the PickupManager on the server.
437        else
438        {
[10624]439            callStaticNetworkFunction(&PickupManager::usePickupNetworked, 0, pickup, use);
[7533]440        }
441    }
442
443    /**
444    @brief
445        Helper method to use (or unuse) the input Pickupable on the server.
446        Static method that is used by clients to instruct the server to use (or unuse) the input pickup.
447    @param pickup
448        The identifier of the Pickupable to be used (or unused).
449    @param use
450        If true the input Pickupable is used, if false it is unused.
451    */
452    /*static*/ void PickupManager::usePickupNetworked(uint32_t pickup, bool use)
453    {
454        if(GameMode::isServer())
455        {
456            PickupManager& manager = PickupManager::getInstance();
[7539]457            manager.usePickup(pickup, use);
[7533]458        }
459    }
460
461    /**
462    @brief
463        Updates the PickupInventory GUI.
464        Also loads the PickupInventory GUI if is hasn't been done already.
465    */
[7504]466    inline void PickupManager::updateGUI(void)
467    {
[7533]468        // We only need to update (and load) the GUI if this host shows graphics.
[7504]469        if(GameMode::showsGraphics())
470        {
[7533]471            if(!this->guiLoaded_) // If the GUI hasn't been loaded, yet, we load it.
[7504]472            {
473                GUIManager::getInstance().loadGUI(PickupManager::guiName_s);
474                this->guiLoaded_ = true;
475            }
[7533]476
477            // Update the GUI.
[7504]478            GUIManager::getInstance().getLuaState()->doString(PickupManager::guiName_s + ".update()");
479        }
480    }
481
[7533]482    /**
483    @brief
[11704]484        Get a new index for a Pickupable.
485        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]486    @return
487        Returns the new index.
488    */
489    uint32_t PickupManager::getPickupIndex(void)
[7504]490    {
[11704]491        if(this->pickupHighestIndex_ == uint32_t(~0x0)-1) // If we've reached the highest possible number, we wrap around.
492            this->pickupHighestIndex_ = 0;
493        return this->pickupHighestIndex_++;
[7504]494    }
495
[6474]496}
Note: See TracBrowser for help on using the repository browser.