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, 6 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
Line 
1/*
2 *   ORXONOX - the hottest 3D action shooter ever to exist
3 *                    > www.orxonox.net <
4 *
5 *
6 *   License notice:
7 *
8 *   This program is free software; you can redistribute it and/or
9 *   modify it under the terms of the GNU General Public License
10 *   as published by the Free Software Foundation; either version 2
11 *   of the License, or (at your option) any later version.
12 *
13 *   This program is distributed in the hope that it will be useful,
14 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
15 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 *   GNU General Public License for more details.
17 *
18 *   You should have received a copy of the GNU General Public License
19 *   along with this program; if not, write to the Free Software
20 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
21 *
22 *   Author:
23 *      Damian 'Mozork' Frick
24 *   Co-authors:
25 *      ...
26 *
27*/
28
29/**
30    @file PickupManager.cc
31    @brief Implementation of the PickupManager class.
32*/
33
34#include "PickupManager.h"
35
36#include "core/CoreIncludes.h"
37#include "core/LuaState.h"
38#include "core/GUIManager.h"
39#include "core/class/Identifier.h"
40#include "core/singleton/ScopedSingletonIncludes.h"
41#include "network/Host.h"
42#include "network/NetworkFunctionIncludes.h"
43
44#include "infos/PlayerInfo.h"
45#include "interfaces/PickupCarrier.h"
46#include "worldentities/pawns/Pawn.h"
47
48#include "CollectiblePickup.h"
49#include "PickupRepresentation.h"
50
51namespace orxonox
52{
53    ManageScopedSingleton(PickupManager, ScopeID::ROOT, false);
54
55    // Initialization of the name of the PickupInventory GUI.
56    /*static*/ const std::string PickupManager::guiName_s = "PickupInventory";
57
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.
59    registerStaticNetworkFunction(PickupManager::pickupChangedUsedNetwork);
60    registerStaticNetworkFunction(PickupManager::pickupChangedPickedUpNetwork);
61    registerStaticNetworkFunction(PickupManager::dropPickupNetworked);
62    registerStaticNetworkFunction(PickupManager::usePickupNetworked);
63
64    RegisterAbstractClass(PickupManager).inheritsFrom<PickupListener>();
65
66    /**
67    @brief
68        Constructor. Registers the PickupManager and creates the default PickupRepresentation.
69    */
70    PickupManager::PickupManager() : guiLoaded_(false), pickupHighestIndex_(0), defaultRepresentation_(NULL)
71    {
72        RegisterObject(PickupManager);
73
74        this->defaultRepresentation_ = new PickupRepresentation();
75
76        orxout(internal_info, context::pickups) << "PickupManager created." << endl;
77    }
78
79    /**
80    @brief
81        Destructor.
82        Destroys the default PickupRepresentation and does some cleanup.
83    */
84    PickupManager::~PickupManager()
85    {
86        // Destroying the default representation.
87        if(this->defaultRepresentation_ != NULL)
88            this->defaultRepresentation_->destroy();
89
90        this->representations_.clear();
91
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();
96
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
104        orxout(internal_info, context::pickups) << "PickupManager destroyed." << endl;
105    }
106
107    /**
108    @brief
109        Registers a PickupRepresentation.
110    @param name
111        The representation's name.
112    @param representation
113        A pointer to the PickupRepresentation.
114    @return
115        Returns true if successful and false if not.
116    */
117    bool PickupManager::registerRepresentation(const std::string& name, PickupRepresentation* representation)
118    {
119        assert(representation);
120
121        // If the list is not empty and Pickupable already has a Representation registered.
122        if(!this->representations_.empty() && this->representations_.find(name) != this->representations_.end())
123            return false;
124
125        this->representations_[name] = representation;
126
127        orxout(verbose, context::pickups) << "PickupRepresentation &" << representation << " registered with the PickupManager." << endl;
128        return true;
129    }
130
131    /**
132    @brief
133        Unegisters a PickupRepresentation.
134    @param name
135        The representation's name.
136    @return
137        Returns true if successful and false if not.
138    */
139    bool PickupManager::unregisterRepresentation(const std::string& name)
140    {
141        std::map<std::string, PickupRepresentation*>::iterator it = this->representations_.find(name);
142        if(it == this->representations_.end()) // If the Pickupable is not registered in the first place.
143            return false;
144
145        this->representations_.erase(it);
146
147        orxout(verbose, context::pickups) << "PickupRepresentation &" << name << " unregistered with the PickupManager." << endl;
148        return true;
149    }
150
151    /**
152    @brief
153        Get the PickupRepresentation with the given name.
154    @param name
155        The name of the PickupRepresentation.
156    @return
157        Returns a pointer to the PickupRepresentation.
158    */
159    PickupRepresentation* PickupManager::getRepresentation(const std::string& name)
160    {
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.
163        {
164            orxout(verbose, context::pickups) << "PickupManager::getRepresentation() returned default representation." << endl;
165            return this->defaultRepresentation_;
166        }
167
168        return it->second;
169    }
170
171    /**
172    @brief
173        Is called by the PickupListener to notify the PickupManager, that the input Pickupable has transited to the input used state.
174    @param pickup
175        The Pickupable whose used status changed.
176    @param used
177        The used status the Pickupable changed to.
178    */
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);
187        // If the Pickupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is.
188        if(collectible != NULL && collectible->isInCollection())
189            return;
190
191        // Getting clientId of the host this change of the pickup's used status concerns.
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
203        // Get the number identifying the pickup.
204        std::map<Pickupable*, uint32_t>::iterator it = this->indexes_.find(pickup);
205        assert(it != this->indexes_.end());
206        uint32_t index = it->second;
207
208        // If we're either in standalone mode or this is the host whom the change of the pickup's status concerns.
209        if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
210        {
211            PickupManager::pickupChangedUsedNetwork(index, used, pickup->isUsable(), pickup->isUnusable());
212        }
213        // If the concerned host is somewhere in the network, we call pickupChangedUsedNetwork() on its PickupManager.
214        else
215        {
216            callStaticNetworkFunction(&PickupManager::pickupChangedUsedNetwork, clientId, index, used, pickup->isUsable(), pickup->isUnusable());
217        }
218    }
219
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)
235    {
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.
238        if(manager.pickupInventoryContainers_.find(pickup) == manager.pickupInventoryContainers_.end())
239        {
240            orxout(internal_error, context::pickups) << "Pickupable &(" << pickup << ") was not registered with PickupManager for the PickupInventory, when it changed used." << endl;
241            return;
242        }
243
244        // Update the Pickupable's container with the information transferred.
245        manager.pickupInventoryContainers_[pickup]->inUse = inUse;
246        manager.pickupInventoryContainers_[pickup]->usable = usable;
247        manager.pickupInventoryContainers_[pickup]->unusable = unusable;
248
249        manager.updateGUI(); // Tell the PickupInventory that something has changed.
250    }
251
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    */
260    void PickupManager::pickupChangedPickedUp(Pickupable* pickup, bool pickedUp)
261    {
262        assert(pickup);
263
264        if(!GameMode::isMaster()) // If this is neither standalone nor the server.
265            return;
266
267        CollectiblePickup* collectible = orxonox_cast<CollectiblePickup*>(pickup);
268        // If the Pickupable is part of a PickupCollection it isn't displayed in the PickupInventory, just the PickupCollection is.
269        if(collectible != NULL && collectible->isInCollection())
270            return;
271
272        // Getting clientId of the host this change of the pickup's pickedUp status concerns.
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;
279        PlayerInfo* info = pawn->getFormerPlayer();
280        if(info == NULL)
281            return;
282        unsigned int clientId = info->getClientID();
283
284        uint32_t index = 0;
285        if(pickedUp) // If the Pickupable has changed to picked up, it is added to the required lists.
286        {
287            index = this->getPickupIndex(); // Ge a new identifier (index) for the Pickupable.
288            // Add the Pickupable to the indexes_ and pickups_ lists.
289            this->indexes_[pickup] = index;
290            this->pickups_[index] = new WeakPtr<Pickupable>(pickup);
291        }
292        else // If it was dropped, it is removed from the required lists.
293        {
294            // Get the indentifier (index) that identifies the input Pickupable.
295            std::map<Pickupable*, uint32_t>::iterator it = this->indexes_.find(pickup);
296            index = it->second;
297
298            // Remove the Pickupable form the indexes_ and pickups_ list.
299            WeakPtr<Pickupable>* ptr = this->pickups_[index];
300            this->indexes_.erase(it);
301            this->pickups_.erase(index);
302            delete ptr;
303        }
304
305        // If we're either in standalone mode or this is the host whom the change of the pickup's status concerns.
306        if(GameMode::isStandalone() || Host::getPlayerID() == clientId)
307        {
308            // If there is no PickupRepresentation registered the default representation is used.
309            if(this->representations_.find(pickup->getRepresentationName()) == this->representations_.end())
310                PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), this->defaultRepresentation_->getObjectID(), pickup->getRepresentationName(), pickedUp);
311            else
312                PickupManager::pickupChangedPickedUpNetwork(index, pickup->isUsable(), this->representations_[pickup->getRepresentationName()]->getObjectID(), pickup->getRepresentationName(), pickedUp);
313        }
314        // If the concerned host is somewhere in the network, we call pickupChangedPickedUpNetwork() on its PickupManager.
315        else
316        {
317            // If there is no PickupRepresentation registered the default representation is used.
318            if(this->representations_.find(pickup->getRepresentationName()) == this->representations_.end())
319            {
320                callStaticNetworkFunction(&PickupManager::pickupChangedPickedUpNetwork, clientId, index, pickup->isUsable(), this->defaultRepresentation_->getObjectID(), pickedUp);
321            }
322            else
323            {
324                callStaticNetworkFunction(&PickupManager::pickupChangedPickedUpNetwork, clientId, index, pickup->isUsable(), this->representations_[pickup->getRepresentationName()]->getObjectID(), pickedUp);
325            }
326        }
327
328    }
329
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.
337    @param usable
338        Whether the Pickupable's used status can be changed to used in the PickupInventory.
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    */
344    /*static*/ void PickupManager::pickupChangedPickedUpNetwork(uint32_t pickup, bool usable, uint32_t representationObjectId, const std::string& representationName, bool pickedUp)
345    {
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.
348        if(pickedUp)
349        {
350            // Create a new PickupInventoryContainer for the Pickupable and set all the necessary information.
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;
358            container->representationName = representationName;
359            // Insert the container into the pickupInventoryContainers_ list.
360            manager.pickupInventoryContainers_.insert(std::pair<uint32_t, PickupInventoryContainer*>(pickup, container));
361
362            manager.updateGUI(); // Tell the PickupInventory that something has changed.
363        }
364        // If the Pickupable has been dropped, we remove it from the pickupInventoryContainers_ list.
365        else
366        {
367            std::map<uint32_t, PickupInventoryContainer*>::iterator it = manager.pickupInventoryContainers_.find(pickup);
368            if(it != manager.pickupInventoryContainers_.end())
369                delete it->second;
370            manager.pickupInventoryContainers_.erase(pickup);
371
372            manager.updateGUI(); // Tell the PickupInventory that something has changed.
373        }
374    }
375
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.
400        if(GameMode::isMaster())
401        {
402            if(this->pickups_.empty())
403                return;
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        {
411            callStaticNetworkFunction(&PickupManager::dropPickupNetworked, 0, pickup);
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();
427            manager.dropPickup(pickup);
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.
443        if(GameMode::isMaster())
444        {
445            if(this->pickups_.empty())
446                return;
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        {
454            callStaticNetworkFunction(&PickupManager::usePickupNetworked, 0, pickup, use);
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();
472            manager.usePickup(pickup, use);
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    */
481    inline void PickupManager::updateGUI(void)
482    {
483        // We only need to update (and load) the GUI if this host shows graphics.
484        if(GameMode::showsGraphics())
485        {
486            if(!this->guiLoaded_) // If the GUI hasn't been loaded, yet, we load it.
487            {
488                GUIManager::getInstance().loadGUI(PickupManager::guiName_s);
489                this->guiLoaded_ = true;
490            }
491
492            // Update the GUI.
493            GUIManager::getInstance().getLuaState()->doString(PickupManager::guiName_s + ".update()");
494        }
495    }
496
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)
505    {
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_++;
509    }
510
511}
Note: See TracBrowser for help on using the repository browser.