Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: code/branches/core7/src/modules/pickup/PickupManager.cc @ 10478

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

callStaticNetworkFunction() and callMemberNetworkFunction() are now template functions instead of macros.
simplified function call interface: always pass five MultiType-references. Check if MultiType.null() evaluates to true to see if the argument was defined.
moved references to NetworkFunctionManager to NetworkFunctionIncludes.cc.
this also fixed a linker error in MSVC by including NetworkFunctionIncludes.h in a build-unit inside the network library.

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